2 * Copyright (c) 2009 Felix Obenhuber
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Sockettrace sniffing API implementation for Linux platform
31 * By Felix Obenhuber <felix@obenhuber.de>
39 #include <libusb-1.0/libusb.h>
49 #include "pcap-canusb-linux.h"
51 #define CANUSB_IFACE "canusb"
53 #define CANUSB_VID 0x0403
54 #define CANUSB_PID 0x8990
63 /* forward declaration */
64 static int canusb_activate(pcap_t
*);
65 static int canusb_read_linux(pcap_t
*, int , pcap_handler
, u_char
*);
66 static int canusb_inject_linux(pcap_t
*, const void *, size_t);
67 static int canusb_setfilter_linux(pcap_t
*, struct bpf_program
*);
68 static int canusb_setdirection_linux(pcap_t
*, pcap_direction_t
);
69 static int canusb_stats_linux(pcap_t
*, struct pcap_stat
*);
80 * Private data for capturing on Linux CANbus USB devices.
84 libusb_device_handle
*dev
;
90 int canusb_findalldevs(pcap_if_t
**alldevsp
, char *err_str
)
92 libusb_context
*fdctx
;
94 unsigned char sernum
[65];
97 if (libusb_init(&fdctx
) != 0) {
99 * XXX - if this doesn't just mean "no USB file system mounted",
100 * perhaps we should report a real error rather than just
101 * saying "no CANUSB devices".
106 cnt
= libusb_get_device_list(fdctx
,&devs
);
111 // Check if this device is interesting.
112 struct libusb_device_descriptor desc
;
113 libusb_get_device_descriptor(devs
[i
],&desc
);
115 if ((desc
.idVendor
!= CANUSB_VID
) || (desc
.idProduct
!= CANUSB_PID
))
116 continue; //It is not, check next device
119 libusb_device_handle
*dh
= NULL
;
121 if ((ret
= libusb_open(devs
[i
],&dh
)) == 0)
125 int n
= libusb_get_string_descriptor_ascii(dh
,desc
.iSerialNumber
,sernum
,64);
128 snprintf(dev_name
, 30, CANUSB_IFACE
"%s", sernum
);
129 snprintf(dev_descr
, 50, "CanUSB [%s]", sernum
);
133 if (pcap_add_if(alldevsp
, dev_name
, 0, dev_descr
, err_str
) < 0)
135 libusb_free_device_list(devs
,1);
142 libusb_free_device_list(devs
,1);
147 static libusb_device_handle
* canusb_opendevice(struct libusb_context
*ctx
, char* devserial
)
149 libusb_device
** devs
;
150 unsigned char serial
[65];
153 cnt
= libusb_get_device_list(ctx
,&devs
);
157 // Check if this device is interesting.
158 struct libusb_device_descriptor desc
;
159 libusb_get_device_descriptor(devs
[i
],&desc
);
161 if ((desc
.idVendor
!= CANUSB_VID
) || (desc
.idProduct
!= CANUSB_PID
))
165 libusb_device_handle
*dh
= NULL
;
167 if (libusb_open(devs
[i
],&dh
) != 0) continue;
169 n
= libusb_get_string_descriptor_ascii(dh
,desc
.iSerialNumber
,serial
,64);
172 if ((devserial
) && (strcmp((char *)serial
,devserial
) != 0))
178 if ((libusb_kernel_driver_active(dh
,0)) && (libusb_detach_kernel_driver(dh
,0) != 0))
184 if (libusb_set_configuration(dh
,1) != 0)
190 if (libusb_claim_interface(dh
,0) != 0)
197 libusb_free_device_list(devs
,1);
201 libusb_free_device_list(devs
,1);
207 canusb_create(const char *device
, char *ebuf
, int *is_ours
)
213 struct pcap_canusb
*canusb
;
215 /* Does this look like a DAG device? */
216 cp
= strrchr(device
, '/');
219 /* Does it begin with "canusb"? */
220 if (strncmp(cp
, "canusb", 6) != 0) {
221 /* Nope, doesn't begin with "canusb" */
225 /* Yes - is "canusb" followed by a number? */
227 devnum
= strtol(cp
, &cpend
, 10);
228 if (cpend
== cp
|| *cpend
!= '\0') {
229 /* Not followed by a number. */
234 /* Followed by a non-valid number. */
239 /* OK, it's probably ours. */
242 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_canusb
));
252 p
->activate_op
= canusb_activate
;
258 static void* canusb_capture_thread(void *arg
)
260 struct pcap_canusb
*canusb
= arg
;
267 fcntl(canusb
->wrpipe
, F_SETFL
, O_NONBLOCK
);
274 libusb_interrupt_transfer(canusb
->dev
, 0x81, (unsigned char*)&status
, sizeof(status
), &sz
, 100);
275 //HACK!!!!! -> drop buffered data, read new one by reading twice.
276 libusb_interrupt_transfer(canusb
->dev
, 0x81, (unsigned char*)&status
, sizeof(status
), &sz
, 100);
278 for(i
= 0; i
<status
.rxsz
; i
++)
280 libusb_bulk_transfer(canusb
->dev
, 0x85, (unsigned char*)&msg
, sizeof(msg
), &sz
, 100);
281 if(write(canusb
->wrpipe
, &msg
, sizeof(msg
)) < 0)
282 fprintf(stderr
,"write() error: %s\n", strerror(errno
));
290 static int canusb_startcapture(struct pcap_canusb
* this)
294 if (pipe(pipefd
) == -1)
297 this->rdpipe
= pipefd
[0];
298 this->wrpipe
= pipefd
[1];
301 pthread_create(&this->worker
, NULL
, canusb_capture_thread
, this);
306 static void canusb_clearbufs(struct pcap_canusb
* this)
308 unsigned char cmd
[16];
311 cmd
[0] = 1; //Empty incoming buffer
312 cmd
[1] = 1; //Empty outgoing buffer
313 cmd
[3] = 0; //Not a write to serial number
314 memset(&cmd
[4],0,16-4);
316 libusb_interrupt_transfer(this->dev
, 0x1,cmd
,16,&al
,100);
320 static void canusb_close(pcap_t
* handle
)
322 struct pcap_canusb
*canusb
= handle
->priv
;
325 pthread_join(canusb
->worker
, NULL
);
329 libusb_close(canusb
->dev
);
334 libusb_exit(canusb
->ctx
);
341 static int canusb_activate(pcap_t
* handle
)
343 struct pcap_canusb
*canusb
= handle
->priv
;
346 if (libusb_init(&canusb
->ctx
) != 0) {
348 * XXX - what causes this to fail?
350 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "libusb_init() failed");
354 handle
->read_op
= canusb_read_linux
;
356 handle
->inject_op
= canusb_inject_linux
;
357 handle
->setfilter_op
= canusb_setfilter_linux
;
358 handle
->setdirection_op
= canusb_setdirection_linux
;
359 handle
->getnonblock_op
= pcap_getnonblock_fd
;
360 handle
->setnonblock_op
= pcap_setnonblock_fd
;
361 handle
->stats_op
= canusb_stats_linux
;
362 handle
->cleanup_op
= canusb_close
;
364 /* Initialize some components of the pcap structure. */
365 handle
->bufsize
= 32;
367 handle
->linktype
= DLT_CAN_SOCKETCAN
;
368 handle
->set_datalink_op
= NULL
;
370 serial
= handle
->opt
.source
+ strlen(CANUSB_IFACE
);
372 canusb
->dev
= canusb_opendevice(canusb
->ctx
, serial
);
375 libusb_exit(canusb
->ctx
);
376 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't open USB Device");
380 canusb_clearbufs(canusb
);
382 handle
->fd
= canusb_startcapture(canusb
);
383 handle
->selectable_fd
= handle
->fd
;
392 canusb_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
394 static struct timeval firstpacket
= { -1, -1};
397 struct pcap_pkthdr pkth
;
399 while(i
< max_packets
)
403 n
= read(handle
->fd
, &msg
, sizeof(msg
));
406 pkth
.caplen
= pkth
.len
= n
;
408 pkth
.caplen
-= 8 - msg
.length
;
410 if ((firstpacket
.tv_sec
== -1) && (firstpacket
.tv_usec
== -1))
411 gettimeofday(&firstpacket
, NULL
);
413 pkth
.ts
.tv_usec
= firstpacket
.tv_usec
+ (msg
.timestamp
% 100) * 10000;
414 pkth
.ts
.tv_sec
= firstpacket
.tv_usec
+ (msg
.timestamp
/ 100);
415 if (pkth
.ts
.tv_usec
> 1000000)
417 pkth
.ts
.tv_usec
-= 1000000;
421 callback(user
, &pkth
, (void*)&msg
.id
);
430 canusb_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
432 /* not yet implemented */
433 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on canusb devices");
439 canusb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
441 /* not yet implemented */
442 stats
->ps_recv
= 0; /* number of packets received */
443 stats
->ps_drop
= 0; /* number of packets dropped */
444 stats
->ps_ifdrop
= 0; /* drops by interface -- only supported on some platforms */
450 canusb_setfilter_linux(pcap_t
*p
, struct bpf_program
*fp
)
452 /* not yet implemented */
458 canusb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
460 /* no support for PCAP_D_OUT */
463 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
464 "Setting direction to PCAP_D_OUT is not supported on this interface");