]> The Tcpdump Group git mirrors - libpcap/blob - pcap-usb-linux.c
From Paolo Abeni:
[libpcap] / pcap-usb-linux.c
1 /*
2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * USB sniffing API implementation for Linux platform
33 * By Paolo Abeni <paolo.abeni@email.com>
34 *
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "pcap-int.h"
42 #include "pcap-usb-linux.h"
43 #include "pcap/usb.h"
44
45 #ifdef NEED_STRERROR_H
46 #include "strerror.h"
47 #endif
48
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <fcntl.h>
53 #include <string.h>
54 #include <dirent.h>
55 #include <netinet/in.h>
56
57 #define USB_IFACE "usb"
58 #define USB_DIR "/sys/kernel/debug/usbmon"
59 #define USB_LINE_LEN 4096
60
61 /* forward declaration */
62 static int usb_read_linux(pcap_t *, int , pcap_handler , u_char *);
63 static int usb_inject_linux(pcap_t *, const void *, size_t);
64 static int usb_setfilter_linux(pcap_t *, struct bpf_program *);
65 static int usb_setdirection_linux(pcap_t *, pcap_direction_t);
66 static int usb_stats_linux(pcap_t *, struct pcap_stat *);
67 static void usb_close_linux(pcap_t *);
68
69 int
70 usb_platform_finddevs(pcap_if_t **alldevsp, char *err_str)
71 {
72 pcap_if_t *devlist = *alldevsp;
73 struct dirent* data;
74 DIR* dir = opendir(USB_DIR);
75 if (!dir) {
76 /* it's not fatal, but it would be useful to give a message
77 about debugfs:
78 modprobe usbmon
79 mount -t debugfs none_debugs /sys/kernel/debug
80 */
81 return 0;
82 }
83
84 /* scan usbmon directory */
85 int ret = 0;
86 while ((data = readdir(dir)) != 0)
87 {
88 char* name = data->d_name;
89 int len = strlen(name);
90
91 if ((len >= 2) && name[len -1]== 't')
92 {
93 int n = name[0] - '0';
94 char dev_name[10], dev_descr[30];
95 snprintf(dev_name, 10, USB_IFACE"%d", n);
96 snprintf(dev_descr, 30, "usb bus number %d", n);
97
98 if (pcap_add_if(&devlist, dev_name, 0,
99 dev_descr, err_str) < 0)
100 {
101 ret = -1;
102 break;
103 }
104 }
105 }
106 closedir(dir);
107
108 *alldevsp = devlist;
109 return ret;
110 }
111
112 pcap_t*
113 usb_open_live(const char* bus, int snaplen, int promisc , int to_ms, char* errmsg)
114 {
115 char full_path[USB_LINE_LEN];
116 pcap_t *handle;
117
118 /* Allocate a handle for this session. */
119 handle = malloc(sizeof(*handle));
120 if (handle == NULL) {
121 snprintf(errmsg, PCAP_ERRBUF_SIZE, "malloc: %s",
122 pcap_strerror(errno));
123 return NULL;
124 }
125
126 /* Initialize some components of the pcap structure. */
127 memset(handle, 0, sizeof(*handle));
128 handle->snapshot = snaplen;
129 handle->md.timeout = to_ms;
130 handle->bufsize = USB_LINE_LEN;
131 handle->offset = 0;
132 handle->linktype = DLT_USB;
133
134 /* get usb bus index from device name */
135 if (sscanf(bus, USB_IFACE"%d", &handle->md.ifindex) != 1)
136 {
137 snprintf(errmsg, PCAP_ERRBUF_SIZE,
138 "Can't get usb bus index from %s", bus);
139 free(handle);
140 return NULL;
141 }
142
143 /* open text output file*/
144 snprintf(full_path, USB_LINE_LEN, USB_DIR"/%dt", handle->md.ifindex);
145 handle->fd = open(full_path, O_RDONLY, 0);
146 if (handle->fd < 0)
147 {
148 snprintf(errmsg, PCAP_ERRBUF_SIZE,
149 "Can't open usb bus file %s: %s", full_path, strerror(errno));
150 free(handle);
151 return NULL;
152 }
153
154 handle->buffer = malloc(handle->bufsize + handle->offset);
155 if (!handle->buffer) {
156 snprintf(errmsg, PCAP_ERRBUF_SIZE,
157 "malloc: %s", pcap_strerror(errno));
158 usb_close_linux(handle);
159 return NULL;
160 }
161
162 /*
163 * "handle->fd" is a real file , so "select()" and "poll()"
164 * work on it.
165 */
166 handle->selectable_fd = handle->fd;
167
168 handle->read_op = usb_read_linux;
169 handle->inject_op = usb_inject_linux;
170 handle->setfilter_op = usb_setfilter_linux;
171 handle->setdirection_op = usb_setdirection_linux;
172 handle->set_datalink_op = NULL; /* can't change data link type */
173 handle->getnonblock_op = pcap_getnonblock_fd;
174 handle->setnonblock_op = pcap_setnonblock_fd;
175 handle->stats_op = usb_stats_linux;
176 handle->close_op = usb_close_linux;
177
178 return handle;
179
180 }
181
182 static inline int
183 ascii_to_int(char c)
184 {
185 return c < 'A' ? c- '0': ((c<'a') ? c - 'A' + 10: c-'a'+10);
186 }
187
188 /*
189 * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
190 * <linux-kernel-source>/drivers/usb/mon/mon_text.c for urb string
191 * format description
192 */
193 static int
194 usb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
195 {
196 /* see:
197 * /usr/src/linux/Documentation/usb/usbmon.txt
198 * for message format
199 */
200 unsigned timestamp;
201 int tag, cnt, ep_num, dev_addr, dummy, ret;
202 char etype, pipeid1, pipeid2, status[16], urb_tag, line[4096];
203 char *string = line;
204 u_char * rawdata = handle->buffer;
205 struct pcap_pkthdr pkth;
206 pcap_usb_header* uhdr = (pcap_usb_header*)rawdata;
207 pcap_urb_type_t urb_type = URB_UNKNOWN;
208
209 /* ignore interrupt system call errors */
210 do {
211 ret = read(handle->fd, line, USB_LINE_LEN - 1);
212 if (handle->break_loop)
213 {
214 handle->break_loop = 0;
215 return -2;
216 }
217 } while ((ret == -1) && (errno == EINTR));
218 if (ret < 0)
219 {
220 if (errno == EAGAIN)
221 return 0; /* no data there */
222
223 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
224 "Can't read from fd %d: %s", handle->fd, strerror(errno));
225 return -1;
226 }
227
228 /* read urb header; %n argument may increment return value, but it's
229 * not mandatory, so does not count on it*/
230 string[ret] = 0;
231 ret = sscanf(string, "%x %d %c %c%c:%d:%d %s%n", &tag, &timestamp, &etype,
232 &pipeid1, &pipeid2, &dev_addr, &ep_num, status,
233 &cnt);
234 if (ret < 8)
235 {
236 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
237 "Can't parse usb bus message '%s', too few token (expected 8 got %d)",
238 string, ret);
239 return -1;
240 }
241 uhdr->endpoint_number = htonl(ep_num);
242 uhdr->device_address = htonl(dev_addr);
243 string += cnt;
244
245 /* don't use usbmon provided timestamp, since it have low precision*/
246 if (gettimeofday(&pkth.ts, NULL) < 0)
247 {
248 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
249 "Can't get timestamp for message '%s' %d:%s",
250 string, errno, strerror(errno));
251 return -1;
252 }
253
254 /* parse endpoint information */
255 if (pipeid1 == 'C')
256 {
257 if (pipeid2 =='i')
258 urb_type = URB_CONTROL_INPUT;
259 else
260 urb_type = URB_CONTROL_OUTPUT;
261 }
262 else if (pipeid1 == 'Z')
263 {
264 if (pipeid2 == 'i')
265 urb_type = URB_ISOCHRONOUS_INPUT;
266 else
267 urb_type = URB_ISOCHRONOUS_OUTPUT;
268 }
269 else if (pipeid1 == 'I')
270 {
271 if (pipeid2 == 'i')
272 urb_type = URB_INTERRUPT_INPUT;
273 else
274 urb_type = URB_INTERRUPT_OUTPUT;
275 }
276 else if (pipeid1 == 'B')
277 {
278 if (pipeid2 == 'i')
279 urb_type = URB_BULK_INPUT;
280 else
281 urb_type = URB_BULK_OUTPUT;
282 }
283
284 /* direction check*/
285 if ((urb_type == URB_BULK_INPUT) || (urb_type == URB_INTERRUPT_INPUT) ||
286 (urb_type == URB_ISOCHRONOUS_INPUT) || (urb_type == URB_CONTROL_INPUT))
287 {
288 if (handle->direction == PCAP_D_OUT)
289 return 0;
290 }
291 else
292 if (handle->direction == PCAP_D_IN)
293 return 0;
294
295 uhdr->urb_type = htonl(urb_type);
296 pkth.caplen = sizeof(pcap_usb_header);
297 rawdata += sizeof(pcap_usb_header);
298
299 /* check if this is a setup packet */
300 ret = sscanf(status, "%d", &dummy);
301 if (ret != 1)
302 {
303 /* this a setup packet, setup data can be filled with underscore if
304 * usbmon has not been able to read them, so we must parse this fields as
305 * strings */
306 pcap_usb_setup* shdr;
307 char str1[3], str2[3], str3[5], str4[5], str5[5];
308 ret = sscanf(string, "%s %s %s %s %s%n", str1, str2, str3, str4,
309 str5, &cnt);
310 if (ret < 5)
311 {
312 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
313 "Can't parse usb bus message '%s', too few token (expected 5 got %d)",
314 string, ret);
315 return -1;
316 }
317 string += cnt;
318
319 /* try to convert to corresponding integer */
320 shdr = (pcap_usb_setup*)rawdata;
321 shdr->bmRequestType = strtoul(str1, 0, 16);
322 shdr->bRequest = strtoul(str2, 0, 16);
323 shdr->wValue = htons(strtoul(str3, 0, 16));
324 shdr->wIndex = htons(strtoul(str4, 0, 16));
325 shdr->wLength = htons(strtoul(str5, 0, 16));
326 uhdr->setup_packet = 1;
327
328
329 pkth.caplen += sizeof(pcap_usb_setup);
330 rawdata += sizeof(pcap_usb_setup);
331 }
332 else
333 uhdr->setup_packet = 0;
334
335 /* read urb data */
336 ret = sscanf(string, " %d%n", &pkth.len, &cnt);
337 if (ret < 1)
338 {
339 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
340 "Can't parse urb length from '%s'", string);
341 return -1;
342 }
343 string += cnt;
344 handle->md.packets_read++;
345
346 /* urb tag is not present if urb length is 0, so we can stop here
347 * text parsing */
348 pkth.len += pkth.caplen;
349 if (pkth.len == pkth.caplen)
350 return 1;
351
352 /* check for data presence; data is present if and only if urb tag is '=' */
353 if (sscanf(string, " %c", &urb_tag) != 1)
354 {
355 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
356 "Can't parse urb tag from '%s'", string);
357 return -1;
358 }
359
360 if (urb_tag != '=')
361 goto got;
362
363 /* skip urb tag and following space */
364 string += 3;
365
366 /* read all urb data; if urb length is greater then the usbmon internal
367 * buffer length used by the kernel to spool the URB, we get only
368 * a partial information.
369 * At least until linux 2.6.17 there is no way to set usbmon intenal buffer
370 * length and default value is 130. */
371 while ((string[0] != 0) && (string[1] != 0) && (pkth.caplen < handle->snapshot))
372 {
373 rawdata[0] = ascii_to_int(string[0]) * 16 + ascii_to_int(string[1]);
374 rawdata++;
375 string+=2;
376 if (string[0] == ' ')
377 string++;
378 pkth.caplen++;
379 }
380
381 got:
382 handle->md.packets_read++;
383 if (pkth.caplen > handle->snapshot)
384 pkth.caplen = handle->snapshot;
385
386
387 callback(user, &pkth, handle->buffer);
388 return 1;
389 }
390
391 static int
392 usb_inject_linux(pcap_t *handle, const void *buf, size_t size)
393 {
394 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on "
395 "usb devices");
396 return (-1);
397 }
398
399
400 static void
401 usb_close_linux(pcap_t* handle)
402 {
403 /* handle fill be freed in pcap_close() 'common' code */
404 close(handle->fd);
405 free(handle->buffer);
406 }
407
408
409 static int
410 usb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
411 {
412 int dummy, ret;
413 char string[USB_LINE_LEN];
414 snprintf(string, USB_LINE_LEN, USB_DIR"/%ds", handle->md.ifindex);
415
416 int fd = open(string, O_RDONLY, 0);
417 if (fd < 0)
418 {
419 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
420 "Can't open usb stats file %s: %s",
421 string, strerror(errno));
422 return -1;
423 }
424
425 /* read stats line */
426 do {
427 ret = read(fd, string, USB_LINE_LEN-1);
428 } while ((ret == -1) && (errno == EINTR));
429 close(fd);
430
431 if (ret < 0)
432 {
433 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
434 "Can't read stats from fd %d ", fd);
435 return -1;
436 }
437 string[ret] = 0;
438
439 /* extract info on dropped urbs */
440 ret = sscanf(string, "nreaders %d text_lost %d", &dummy, &stats->ps_drop);
441 if (ret != 2)
442 {
443 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
444 "Can't parse stat line '%s' expected 2 token got %d",
445 string, ret);
446 return -1;
447 }
448
449 stats->ps_recv = handle->md.packets_read;
450 stats->ps_ifdrop = 0;
451 return 0;
452 }
453
454 static int
455 usb_setfilter_linux(pcap_t *p, struct bpf_program *fp)
456 {
457 return 0;
458 }
459
460
461 static int
462 usb_setdirection_linux(pcap_t *p, pcap_direction_t d)
463 {
464 p->direction = d;
465 return 0;
466 }