xref: /freebsd/contrib/libpcap/pcap-usb-linux.c (revision afdbf109c6a661a729938f68211054a0a50d38ac)
1a8e07101SRui Paulo /*
2a8e07101SRui Paulo  * Copyright (c) 2006 Paolo Abeni (Italy)
3a8e07101SRui Paulo  * All rights reserved.
4a8e07101SRui Paulo  *
5a8e07101SRui Paulo  * Redistribution and use in source and binary forms, with or without
6a8e07101SRui Paulo  * modification, are permitted provided that the following conditions
7a8e07101SRui Paulo  * are met:
8a8e07101SRui Paulo  *
9a8e07101SRui Paulo  * 1. Redistributions of source code must retain the above copyright
10a8e07101SRui Paulo  * notice, this list of conditions and the following disclaimer.
11a8e07101SRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
12a8e07101SRui Paulo  * notice, this list of conditions and the following disclaimer in the
13a8e07101SRui Paulo  * documentation and/or other materials provided with the distribution.
14a8e07101SRui Paulo  * 3. The name of the author may not be used to endorse or promote
15a8e07101SRui Paulo  * products derived from this software without specific prior written
16a8e07101SRui Paulo  * permission.
17a8e07101SRui Paulo  *
18a8e07101SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a8e07101SRui Paulo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20a8e07101SRui Paulo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a8e07101SRui Paulo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22a8e07101SRui Paulo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23a8e07101SRui Paulo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24a8e07101SRui Paulo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a8e07101SRui Paulo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a8e07101SRui Paulo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a8e07101SRui Paulo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28a8e07101SRui Paulo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a8e07101SRui Paulo  *
30a8e07101SRui Paulo  * USB sniffing API implementation for Linux platform
31a8e07101SRui Paulo  * By Paolo Abeni <paolo.abeni@email.it>
32a8e07101SRui Paulo  * Modifications: Kris Katterjohn <katterjohn@gmail.com>
33a8e07101SRui Paulo  *
34a8e07101SRui Paulo  */
35a8e07101SRui Paulo 
36b00ab754SHans Petter Selasky #include <config.h>
37a8e07101SRui Paulo 
38*afdbf109SJoseph Mingrone #include "pcap/usb.h"
39a8e07101SRui Paulo #include "pcap-int.h"
40a8e07101SRui Paulo #include "pcap-usb-linux.h"
416f9cba8fSJoseph Mingrone #include "pcap-usb-linux-common.h"
42a8e07101SRui Paulo 
436f9cba8fSJoseph Mingrone #include "extract.h"
446f9cba8fSJoseph Mingrone 
45a8e07101SRui Paulo #ifdef NEED_STRERROR_H
46a8e07101SRui Paulo #include "strerror.h"
47a8e07101SRui Paulo #endif
48a8e07101SRui Paulo 
49a8e07101SRui Paulo #include <errno.h>
50a8e07101SRui Paulo #include <stdlib.h>
51a8e07101SRui Paulo #include <unistd.h>
52a8e07101SRui Paulo #include <fcntl.h>
5357e22627SCy Schubert #include <limits.h>
54a8e07101SRui Paulo #include <string.h>
55a8e07101SRui Paulo #include <dirent.h>
56a8e07101SRui Paulo #include <byteswap.h>
57a8e07101SRui Paulo #include <netinet/in.h>
58a8e07101SRui Paulo #include <sys/ioctl.h>
59a8e07101SRui Paulo #include <sys/mman.h>
60b00ab754SHans Petter Selasky #include <sys/utsname.h>
61a0ee43a1SRui Paulo #ifdef HAVE_LINUX_USBDEVICE_FS_H
62d1e87331SXin LI /*
63d1e87331SXin LI  * We might need <linux/compiler.h> to define __user for
64d1e87331SXin LI  * <linux/usbdevice_fs.h>.
65d1e87331SXin LI  */
66d1e87331SXin LI #ifdef HAVE_LINUX_COMPILER_H
67d1e87331SXin LI #include <linux/compiler.h>
68d1e87331SXin LI #endif /* HAVE_LINUX_COMPILER_H */
69a0ee43a1SRui Paulo #include <linux/usbdevice_fs.h>
70d1e87331SXin LI #endif /* HAVE_LINUX_USBDEVICE_FS_H */
71a8e07101SRui Paulo 
726f9cba8fSJoseph Mingrone #include "diag-control.h"
736f9cba8fSJoseph Mingrone 
74a0ee43a1SRui Paulo #define USB_IFACE "usbmon"
756f9cba8fSJoseph Mingrone 
766f9cba8fSJoseph Mingrone #define USBMON_DEV_PREFIX "usbmon"
776f9cba8fSJoseph Mingrone #define USBMON_DEV_PREFIX_LEN	(sizeof USBMON_DEV_PREFIX - 1)
78a8e07101SRui Paulo #define USB_LINE_LEN 4096
79a8e07101SRui Paulo 
80a8e07101SRui Paulo #if __BYTE_ORDER == __LITTLE_ENDIAN
81a8e07101SRui Paulo #define htols(s) s
82a8e07101SRui Paulo #define htoll(l) l
83a8e07101SRui Paulo #define htol64(ll) ll
84a8e07101SRui Paulo #else
85a8e07101SRui Paulo #define htols(s) bswap_16(s)
86a8e07101SRui Paulo #define htoll(l) bswap_32(l)
87a8e07101SRui Paulo #define htol64(ll) bswap_64(ll)
88a8e07101SRui Paulo #endif
89a8e07101SRui Paulo 
90a8e07101SRui Paulo struct mon_bin_stats {
91b00ab754SHans Petter Selasky 	uint32_t queued;
92b00ab754SHans Petter Selasky 	uint32_t dropped;
93a8e07101SRui Paulo };
94a8e07101SRui Paulo 
95a8e07101SRui Paulo struct mon_bin_get {
96a8e07101SRui Paulo 	pcap_usb_header *hdr;
97a8e07101SRui Paulo 	void *data;
98a8e07101SRui Paulo 	size_t data_len;   /* Length of data (can be zero) */
99a8e07101SRui Paulo };
100a8e07101SRui Paulo 
101a8e07101SRui Paulo struct mon_bin_mfetch {
102a8e07101SRui Paulo 	int32_t *offvec;   /* Vector of events fetched */
103a8e07101SRui Paulo 	int32_t nfetch;    /* Number of events to fetch (out: fetched) */
104a8e07101SRui Paulo 	int32_t nflush;    /* Number of events to flush */
105a8e07101SRui Paulo };
106a8e07101SRui Paulo 
107a8e07101SRui Paulo #define MON_IOC_MAGIC 0x92
108a8e07101SRui Paulo 
109a8e07101SRui Paulo #define MON_IOCQ_URB_LEN _IO(MON_IOC_MAGIC, 1)
110a8e07101SRui Paulo #define MON_IOCX_URB  _IOWR(MON_IOC_MAGIC, 2, struct mon_bin_hdr)
111a8e07101SRui Paulo #define MON_IOCG_STATS _IOR(MON_IOC_MAGIC, 3, struct mon_bin_stats)
112a8e07101SRui Paulo #define MON_IOCT_RING_SIZE _IO(MON_IOC_MAGIC, 4)
113a8e07101SRui Paulo #define MON_IOCQ_RING_SIZE _IO(MON_IOC_MAGIC, 5)
114a8e07101SRui Paulo #define MON_IOCX_GET   _IOW(MON_IOC_MAGIC, 6, struct mon_bin_get)
115a8e07101SRui Paulo #define MON_IOCX_MFETCH _IOWR(MON_IOC_MAGIC, 7, struct mon_bin_mfetch)
116a8e07101SRui Paulo #define MON_IOCH_MFLUSH _IO(MON_IOC_MAGIC, 8)
117a8e07101SRui Paulo 
118a8e07101SRui Paulo #define MON_BIN_SETUP	0x1 /* setup hdr is present*/
119a8e07101SRui Paulo #define MON_BIN_SETUP_ZERO	0x2 /* setup buffer is not available */
120a8e07101SRui Paulo #define MON_BIN_DATA_ZERO	0x4 /* data buffer is not available */
121a8e07101SRui Paulo #define MON_BIN_ERROR	0x8
122a8e07101SRui Paulo 
123681ed54cSXin LI /*
124681ed54cSXin LI  * Private data for capturing on Linux USB.
125681ed54cSXin LI  */
126681ed54cSXin LI struct pcap_usb_linux {
127681ed54cSXin LI 	u_char *mmapbuf;	/* memory-mapped region pointer */
128681ed54cSXin LI 	size_t mmapbuflen;	/* size of region */
129681ed54cSXin LI 	int bus_index;
130681ed54cSXin LI 	u_int packets_read;
131681ed54cSXin LI };
132681ed54cSXin LI 
133a8e07101SRui Paulo /* forward declaration */
134a8e07101SRui Paulo static int usb_activate(pcap_t *);
135a8e07101SRui Paulo static int usb_stats_linux_bin(pcap_t *, struct pcap_stat *);
136a8e07101SRui Paulo static int usb_read_linux_bin(pcap_t *, int , pcap_handler , u_char *);
137a8e07101SRui Paulo static int usb_read_linux_mmap(pcap_t *, int , pcap_handler , u_char *);
1386f9cba8fSJoseph Mingrone static int usb_inject_linux(pcap_t *, const void *, int);
139a8e07101SRui Paulo static int usb_setdirection_linux(pcap_t *, pcap_direction_t);
140a8e07101SRui Paulo static void usb_cleanup_linux_mmap(pcap_t *);
141a8e07101SRui Paulo 
142a8e07101SRui Paulo /* facility to add an USB device to the device list*/
143a8e07101SRui Paulo static int
usb_dev_add(pcap_if_list_t * devlistp,int n,char * err_str)144b00ab754SHans Petter Selasky usb_dev_add(pcap_if_list_t *devlistp, int n, char *err_str)
145a8e07101SRui Paulo {
146a8e07101SRui Paulo 	char dev_name[10];
147a8e07101SRui Paulo 	char dev_descr[30];
1486f9cba8fSJoseph Mingrone 	snprintf(dev_name, 10, USB_IFACE"%d", n);
149b00ab754SHans Petter Selasky 	/*
150b00ab754SHans Petter Selasky 	 * XXX - is there any notion of "up" and "running"?
151b00ab754SHans Petter Selasky 	 */
152b00ab754SHans Petter Selasky 	if (n == 0) {
153b00ab754SHans Petter Selasky 		/*
154b00ab754SHans Petter Selasky 		 * As this refers to all buses, there's no notion of
155b00ab754SHans Petter Selasky 		 * "connected" vs. "disconnected", as that's a property
156b00ab754SHans Petter Selasky 		 * that would apply to a particular USB interface.
157b00ab754SHans Petter Selasky 		 */
158*afdbf109SJoseph Mingrone 		if (pcapint_add_dev(devlistp, dev_name,
159b00ab754SHans Petter Selasky 		    PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE,
16057e22627SCy Schubert 		    "Raw USB traffic, all USB buses", err_str) == NULL)
161a8e07101SRui Paulo 			return -1;
162b00ab754SHans Petter Selasky 	} else {
163b00ab754SHans Petter Selasky 		/*
164b00ab754SHans Petter Selasky 		 * XXX - is there a way to determine whether anything's
165b00ab754SHans Petter Selasky 		 * plugged into this bus interface or not, and set
166b00ab754SHans Petter Selasky 		 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
167b00ab754SHans Petter Selasky 		 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
168b00ab754SHans Petter Selasky 		 */
1696f9cba8fSJoseph Mingrone 		snprintf(dev_descr, 30, "Raw USB traffic, bus number %d", n);
170*afdbf109SJoseph Mingrone 		if (pcapint_add_dev(devlistp, dev_name, 0, dev_descr, err_str) == NULL)
171b00ab754SHans Petter Selasky 			return -1;
172b00ab754SHans Petter Selasky 	}
173b00ab754SHans Petter Selasky 
174a8e07101SRui Paulo 	return 0;
175a8e07101SRui Paulo }
176a8e07101SRui Paulo 
177a8e07101SRui Paulo int
usb_findalldevs(pcap_if_list_t * devlistp,char * err_str)178b00ab754SHans Petter Selasky usb_findalldevs(pcap_if_list_t *devlistp, char *err_str)
179a8e07101SRui Paulo {
180a8e07101SRui Paulo 	struct dirent* data;
181a8e07101SRui Paulo 	int ret = 0;
182a8e07101SRui Paulo 	DIR* dir;
183a8e07101SRui Paulo 	int n;
184a0ee43a1SRui Paulo 	char* name;
185ada6f083SXin LI 
186ada6f083SXin LI 	/*
1876f9cba8fSJoseph Mingrone 	 * We require 2.6.27 or later kernels, so we have binary-mode support.
1886f9cba8fSJoseph Mingrone 	 * The devices are of the form /dev/usbmon{N}.
1896f9cba8fSJoseph Mingrone 	 * Open /dev and scan it.
190b00ab754SHans Petter Selasky 	 */
1916f9cba8fSJoseph Mingrone 	dir = opendir("/dev");
192b00ab754SHans Petter Selasky 	if (dir != NULL) {
193b00ab754SHans Petter Selasky 		while ((ret == 0) && ((data = readdir(dir)) != 0)) {
194b00ab754SHans Petter Selasky 			name = data->d_name;
195b00ab754SHans Petter Selasky 
196b00ab754SHans Petter Selasky 			/*
197b00ab754SHans Petter Selasky 			 * Is this a usbmon device?
198b00ab754SHans Petter Selasky 			 */
1996f9cba8fSJoseph Mingrone 			if (strncmp(name, USBMON_DEV_PREFIX,
2006f9cba8fSJoseph Mingrone 			    USBMON_DEV_PREFIX_LEN) != 0)
201b00ab754SHans Petter Selasky 				continue;	/* no */
202b00ab754SHans Petter Selasky 
203b00ab754SHans Petter Selasky 			/*
204b00ab754SHans Petter Selasky 			 * What's the device number?
205b00ab754SHans Petter Selasky 			 */
2066f9cba8fSJoseph Mingrone 			if (sscanf(&name[USBMON_DEV_PREFIX_LEN], "%d", &n) == 0)
207b00ab754SHans Petter Selasky 				continue;	/* failed */
208b00ab754SHans Petter Selasky 
209b00ab754SHans Petter Selasky 			ret = usb_dev_add(devlistp, n, err_str);
210b00ab754SHans Petter Selasky 		}
211b00ab754SHans Petter Selasky 
212b00ab754SHans Petter Selasky 		closedir(dir);
213b00ab754SHans Petter Selasky 	}
214b00ab754SHans Petter Selasky 	return 0;
215b00ab754SHans Petter Selasky }
216a0ee43a1SRui Paulo 
21757e22627SCy Schubert /*
21857e22627SCy Schubert  * Matches what's in mon_bin.c in the Linux kernel.
21957e22627SCy Schubert  */
22057e22627SCy Schubert #define MIN_RING_SIZE	(8*1024)
22157e22627SCy Schubert #define MAX_RING_SIZE	(1200*1024)
22257e22627SCy Schubert 
22357e22627SCy Schubert static int
usb_set_ring_size(pcap_t * handle,int header_size)22457e22627SCy Schubert usb_set_ring_size(pcap_t* handle, int header_size)
22557e22627SCy Schubert {
22657e22627SCy Schubert 	/*
22757e22627SCy Schubert 	 * A packet from binary usbmon has:
22857e22627SCy Schubert 	 *
22957e22627SCy Schubert 	 *  1) a fixed-length header, of size header_size;
23057e22627SCy Schubert 	 *  2) descriptors, for isochronous transfers;
23157e22627SCy Schubert 	 *  3) the payload.
23257e22627SCy Schubert 	 *
23357e22627SCy Schubert 	 * The kernel buffer has a size, defaulting to 300KB, with a
23457e22627SCy Schubert 	 * minimum of 8KB and a maximum of 1200KB.  The size is set with
23557e22627SCy Schubert 	 * the MON_IOCT_RING_SIZE ioctl; the size passed in is rounded up
23657e22627SCy Schubert 	 * to a page size.
23757e22627SCy Schubert 	 *
23857e22627SCy Schubert 	 * No more than {buffer size}/5 bytes worth of payload is saved.
23957e22627SCy Schubert 	 * Therefore, if we subtract the fixed-length size from the
24057e22627SCy Schubert 	 * snapshot length, we have the biggest payload we want (we
24157e22627SCy Schubert 	 * don't worry about the descriptors - if we have descriptors,
24257e22627SCy Schubert 	 * we'll just discard the last bit of the payload to get it
24357e22627SCy Schubert 	 * to fit).  We multiply that result by 5 and set the buffer
24457e22627SCy Schubert 	 * size to that value.
24557e22627SCy Schubert 	 */
24657e22627SCy Schubert 	int ring_size;
24757e22627SCy Schubert 
24857e22627SCy Schubert 	if (handle->snapshot < header_size)
24957e22627SCy Schubert 		handle->snapshot = header_size;
25057e22627SCy Schubert 	/* The maximum snapshot size is small enough that this won't overflow */
25157e22627SCy Schubert 	ring_size = (handle->snapshot - header_size) * 5;
25257e22627SCy Schubert 
25357e22627SCy Schubert 	/*
25457e22627SCy Schubert 	 * Will this get an error?
255*afdbf109SJoseph Mingrone 	 * (There's no way to query the minimum or maximum, so we just
25657e22627SCy Schubert 	 * copy the value from the kernel source.  We don't round it
25757e22627SCy Schubert 	 * up to a multiple of the page size.)
25857e22627SCy Schubert 	 */
25957e22627SCy Schubert 	if (ring_size > MAX_RING_SIZE) {
26057e22627SCy Schubert 		/*
26157e22627SCy Schubert 		 * Yes.  Lower the ring size to the maximum, and set the
26257e22627SCy Schubert 		 * snapshot length to the value that would give us a
26357e22627SCy Schubert 		 * maximum-size ring.
26457e22627SCy Schubert 		 */
26557e22627SCy Schubert 		ring_size = MAX_RING_SIZE;
26657e22627SCy Schubert 		handle->snapshot = header_size + (MAX_RING_SIZE/5);
26757e22627SCy Schubert 	} else if (ring_size < MIN_RING_SIZE) {
26857e22627SCy Schubert 		/*
26957e22627SCy Schubert 		 * Yes.  Raise the ring size to the minimum, but leave
27057e22627SCy Schubert 		 * the snapshot length unchanged, so we show the
27157e22627SCy Schubert 		 * callback no more data than specified by the
27257e22627SCy Schubert 		 * snapshot length.
27357e22627SCy Schubert 		 */
27457e22627SCy Schubert 		ring_size = MIN_RING_SIZE;
27557e22627SCy Schubert 	}
27657e22627SCy Schubert 
27757e22627SCy Schubert 	if (ioctl(handle->fd, MON_IOCT_RING_SIZE, ring_size) == -1) {
278*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
27957e22627SCy Schubert 		    errno, "Can't set ring size from fd %d", handle->fd);
28057e22627SCy Schubert 		return -1;
28157e22627SCy Schubert 	}
28257e22627SCy Schubert 	return ring_size;
28357e22627SCy Schubert }
28457e22627SCy Schubert 
285a8e07101SRui Paulo static
usb_mmap(pcap_t * handle)286a8e07101SRui Paulo int usb_mmap(pcap_t* handle)
287a8e07101SRui Paulo {
288681ed54cSXin LI 	struct pcap_usb_linux *handlep = handle->priv;
28957e22627SCy Schubert 	int len;
29057e22627SCy Schubert 
29157e22627SCy Schubert 	/*
29257e22627SCy Schubert 	 * Attempt to set the ring size as appropriate for the snapshot
29357e22627SCy Schubert 	 * length, reducing the snapshot length if that'd make the ring
29457e22627SCy Schubert 	 * bigger than the kernel supports.
29557e22627SCy Schubert 	 */
29657e22627SCy Schubert 	len = usb_set_ring_size(handle, (int)sizeof(pcap_usb_header_mmapped));
29757e22627SCy Schubert 	if (len == -1) {
29857e22627SCy Schubert 		/* Failed.  Fall back on non-memory-mapped access. */
299a8e07101SRui Paulo 		return 0;
30057e22627SCy Schubert 	}
301a8e07101SRui Paulo 
302681ed54cSXin LI 	handlep->mmapbuflen = len;
303681ed54cSXin LI 	handlep->mmapbuf = mmap(0, handlep->mmapbuflen, PROT_READ,
304a0ee43a1SRui Paulo 	    MAP_SHARED, handle->fd, 0);
30557e22627SCy Schubert 	if (handlep->mmapbuf == MAP_FAILED) {
30657e22627SCy Schubert 		/*
30757e22627SCy Schubert 		 * Failed.  We don't treat that as a fatal error, we
30857e22627SCy Schubert 		 * just try to fall back on non-memory-mapped access.
30957e22627SCy Schubert 		 */
31057e22627SCy Schubert 		return 0;
31157e22627SCy Schubert 	}
31257e22627SCy Schubert 	return 1;
313a0ee43a1SRui Paulo }
314a0ee43a1SRui Paulo 
315681ed54cSXin LI #ifdef HAVE_LINUX_USBDEVICE_FS_H
316681ed54cSXin LI 
317a0ee43a1SRui Paulo #define CTRL_TIMEOUT    (5*1000)        /* milliseconds */
318a0ee43a1SRui Paulo 
319a0ee43a1SRui Paulo #define USB_DIR_IN		0x80
320a0ee43a1SRui Paulo #define USB_TYPE_STANDARD	0x00
321a0ee43a1SRui Paulo #define USB_RECIP_DEVICE	0x00
322a0ee43a1SRui Paulo 
323a0ee43a1SRui Paulo #define USB_REQ_GET_DESCRIPTOR	6
324a0ee43a1SRui Paulo 
325a0ee43a1SRui Paulo #define USB_DT_DEVICE		1
3266f9cba8fSJoseph Mingrone #define USB_DT_CONFIG		2
3276f9cba8fSJoseph Mingrone 
3286f9cba8fSJoseph Mingrone #define USB_DEVICE_DESCRIPTOR_SIZE	18
3296f9cba8fSJoseph Mingrone #define USB_CONFIG_DESCRIPTOR_SIZE	9
330a0ee43a1SRui Paulo 
331a0ee43a1SRui Paulo /* probe the descriptors of the devices attached to the bus */
332a0ee43a1SRui Paulo /* the descriptors will end up in the captured packet stream */
333a0ee43a1SRui Paulo /* and be decoded by external apps like wireshark */
334a0ee43a1SRui Paulo /* without these identifying probes packet data can't be fully decoded */
335a0ee43a1SRui Paulo static void
probe_devices(int bus)336a0ee43a1SRui Paulo probe_devices(int bus)
337a0ee43a1SRui Paulo {
338a0ee43a1SRui Paulo 	struct usbdevfs_ctrltransfer ctrl;
339a0ee43a1SRui Paulo 	struct dirent* data;
340a0ee43a1SRui Paulo 	int ret = 0;
3416f9cba8fSJoseph Mingrone 	char busdevpath[sizeof("/dev/bus/usb/000/") + NAME_MAX];
342a0ee43a1SRui Paulo 	DIR* dir;
3436f9cba8fSJoseph Mingrone 	uint8_t descriptor[USB_DEVICE_DESCRIPTOR_SIZE];
3446f9cba8fSJoseph Mingrone 	uint8_t configdesc[USB_CONFIG_DESCRIPTOR_SIZE];
345a0ee43a1SRui Paulo 
346a0ee43a1SRui Paulo 	/* scan usb bus directories for device nodes */
3476f9cba8fSJoseph Mingrone 	snprintf(busdevpath, sizeof(busdevpath), "/dev/bus/usb/%03d", bus);
3486f9cba8fSJoseph Mingrone 	dir = opendir(busdevpath);
349a0ee43a1SRui Paulo 	if (!dir)
350a0ee43a1SRui Paulo 		return;
351a0ee43a1SRui Paulo 
352a0ee43a1SRui Paulo 	while ((ret >= 0) && ((data = readdir(dir)) != 0)) {
353a0ee43a1SRui Paulo 		int fd;
354a0ee43a1SRui Paulo 		char* name = data->d_name;
355a0ee43a1SRui Paulo 
356a0ee43a1SRui Paulo 		if (name[0] == '.')
357a0ee43a1SRui Paulo 			continue;
358a0ee43a1SRui Paulo 
3596f9cba8fSJoseph Mingrone 		snprintf(busdevpath, sizeof(busdevpath), "/dev/bus/usb/%03d/%s", bus, data->d_name);
360a0ee43a1SRui Paulo 
3616f9cba8fSJoseph Mingrone 		fd = open(busdevpath, O_RDWR);
362a0ee43a1SRui Paulo 		if (fd == -1)
363a0ee43a1SRui Paulo 			continue;
364a0ee43a1SRui Paulo 
365a0ee43a1SRui Paulo 		/*
366a0ee43a1SRui Paulo 		 * Sigh.  Different kernels have different member names
367a0ee43a1SRui Paulo 		 * for this structure.
368a0ee43a1SRui Paulo 		 */
369b00ab754SHans Petter Selasky #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
370a0ee43a1SRui Paulo 		ctrl.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
371a0ee43a1SRui Paulo 		ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
372a0ee43a1SRui Paulo 		ctrl.wValue = USB_DT_DEVICE << 8;
373a0ee43a1SRui Paulo 		ctrl.wIndex = 0;
3746f9cba8fSJoseph Mingrone 		ctrl.wLength = sizeof(descriptor);
375a0ee43a1SRui Paulo #else
376a0ee43a1SRui Paulo 		ctrl.requesttype = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
377a0ee43a1SRui Paulo 		ctrl.request = USB_REQ_GET_DESCRIPTOR;
378a0ee43a1SRui Paulo 		ctrl.value = USB_DT_DEVICE << 8;
379a0ee43a1SRui Paulo 		ctrl.index = 0;
3806f9cba8fSJoseph Mingrone 		ctrl.length = sizeof(descriptor);
381a0ee43a1SRui Paulo #endif
3826f9cba8fSJoseph Mingrone 		ctrl.data = descriptor;
383a0ee43a1SRui Paulo 		ctrl.timeout = CTRL_TIMEOUT;
384a0ee43a1SRui Paulo 
385a0ee43a1SRui Paulo 		ret = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
386a0ee43a1SRui Paulo 
3876f9cba8fSJoseph Mingrone 		/* Request CONFIGURATION descriptor alone to know wTotalLength */
3886f9cba8fSJoseph Mingrone #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
3896f9cba8fSJoseph Mingrone 		ctrl.wValue = USB_DT_CONFIG << 8;
3906f9cba8fSJoseph Mingrone 		ctrl.wLength = sizeof(configdesc);
3916f9cba8fSJoseph Mingrone #else
3926f9cba8fSJoseph Mingrone 		ctrl.value = USB_DT_CONFIG << 8;
3936f9cba8fSJoseph Mingrone 		ctrl.length = sizeof(configdesc);
3946f9cba8fSJoseph Mingrone #endif
3956f9cba8fSJoseph Mingrone 		ctrl.data = configdesc;
3966f9cba8fSJoseph Mingrone 		ret = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
3976f9cba8fSJoseph Mingrone 		if (ret >= 0) {
3986f9cba8fSJoseph Mingrone 			uint16_t wtotallength;
3996f9cba8fSJoseph Mingrone 			wtotallength = EXTRACT_LE_U_2(&configdesc[2]);
4006f9cba8fSJoseph Mingrone #ifdef HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE
4016f9cba8fSJoseph Mingrone 			ctrl.wLength = wtotallength;
4026f9cba8fSJoseph Mingrone #else
4036f9cba8fSJoseph Mingrone 			ctrl.length = wtotallength;
4046f9cba8fSJoseph Mingrone #endif
4056f9cba8fSJoseph Mingrone 			ctrl.data = malloc(wtotallength);
4066f9cba8fSJoseph Mingrone 			if (ctrl.data) {
4076f9cba8fSJoseph Mingrone 				ret = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
4086f9cba8fSJoseph Mingrone 				free(ctrl.data);
4096f9cba8fSJoseph Mingrone 			}
4106f9cba8fSJoseph Mingrone 		}
411a0ee43a1SRui Paulo 		close(fd);
412a0ee43a1SRui Paulo 	}
413a0ee43a1SRui Paulo 	closedir(dir);
414a8e07101SRui Paulo }
415681ed54cSXin LI #endif /* HAVE_LINUX_USBDEVICE_FS_H */
416a8e07101SRui Paulo 
417a8e07101SRui Paulo pcap_t *
usb_create(const char * device,char * ebuf,int * is_ours)418edc89b24SXin LI usb_create(const char *device, char *ebuf, int *is_ours)
419a8e07101SRui Paulo {
420edc89b24SXin LI 	const char *cp;
421edc89b24SXin LI 	char *cpend;
422edc89b24SXin LI 	long devnum;
423a8e07101SRui Paulo 	pcap_t *p;
424a8e07101SRui Paulo 
425edc89b24SXin LI 	/* Does this look like a USB monitoring device? */
426edc89b24SXin LI 	cp = strrchr(device, '/');
427edc89b24SXin LI 	if (cp == NULL)
428edc89b24SXin LI 		cp = device;
429edc89b24SXin LI 	/* Does it begin with USB_IFACE? */
430edc89b24SXin LI 	if (strncmp(cp, USB_IFACE, sizeof USB_IFACE - 1) != 0) {
431edc89b24SXin LI 		/* Nope, doesn't begin with USB_IFACE */
432edc89b24SXin LI 		*is_ours = 0;
433edc89b24SXin LI 		return NULL;
434edc89b24SXin LI 	}
435edc89b24SXin LI 	/* Yes - is USB_IFACE followed by a number? */
436edc89b24SXin LI 	cp += sizeof USB_IFACE - 1;
437edc89b24SXin LI 	devnum = strtol(cp, &cpend, 10);
438edc89b24SXin LI 	if (cpend == cp || *cpend != '\0') {
439edc89b24SXin LI 		/* Not followed by a number. */
440edc89b24SXin LI 		*is_ours = 0;
441edc89b24SXin LI 		return NULL;
442edc89b24SXin LI 	}
443edc89b24SXin LI 	if (devnum < 0) {
444edc89b24SXin LI 		/* Followed by a non-valid number. */
445edc89b24SXin LI 		*is_ours = 0;
446edc89b24SXin LI 		return NULL;
447edc89b24SXin LI 	}
448edc89b24SXin LI 
449edc89b24SXin LI 	/* OK, it's probably ours. */
450edc89b24SXin LI 	*is_ours = 1;
451edc89b24SXin LI 
4526f9cba8fSJoseph Mingrone 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_usb_linux);
453a8e07101SRui Paulo 	if (p == NULL)
454a8e07101SRui Paulo 		return (NULL);
455a8e07101SRui Paulo 
456a8e07101SRui Paulo 	p->activate_op = usb_activate;
457a8e07101SRui Paulo 	return (p);
458a8e07101SRui Paulo }
459a8e07101SRui Paulo 
460a8e07101SRui Paulo static int
usb_activate(pcap_t * handle)461a8e07101SRui Paulo usb_activate(pcap_t* handle)
462a8e07101SRui Paulo {
463681ed54cSXin LI 	struct pcap_usb_linux *handlep = handle->priv;
464a8e07101SRui Paulo 	char		full_path[USB_LINE_LEN];
465a8e07101SRui Paulo 
466b00ab754SHans Petter Selasky 	/*
467b00ab754SHans Petter Selasky 	 * Turn a negative snapshot value (invalid), a snapshot value of
468b00ab754SHans Petter Selasky 	 * 0 (unspecified), or a value bigger than the normal maximum
469b00ab754SHans Petter Selasky 	 * value, into the maximum allowed value.
470b00ab754SHans Petter Selasky 	 *
471b00ab754SHans Petter Selasky 	 * If some application really *needs* a bigger snapshot
472b00ab754SHans Petter Selasky 	 * length, we should just increase MAXIMUM_SNAPLEN.
473b00ab754SHans Petter Selasky 	 */
474b00ab754SHans Petter Selasky 	if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
475b00ab754SHans Petter Selasky 		handle->snapshot = MAXIMUM_SNAPLEN;
476b00ab754SHans Petter Selasky 
477a8e07101SRui Paulo 	/* Initialize some components of the pcap structure. */
478a8e07101SRui Paulo 	handle->bufsize = handle->snapshot;
479a8e07101SRui Paulo 	handle->offset = 0;
480a8e07101SRui Paulo 	handle->linktype = DLT_USB_LINUX;
481a8e07101SRui Paulo 
482a8e07101SRui Paulo 	handle->inject_op = usb_inject_linux;
483*afdbf109SJoseph Mingrone 	handle->setfilter_op = pcapint_install_bpf_program; /* no kernel filtering */
484a8e07101SRui Paulo 	handle->setdirection_op = usb_setdirection_linux;
485a8e07101SRui Paulo 	handle->set_datalink_op = NULL;	/* can't change data link type */
486*afdbf109SJoseph Mingrone 	handle->getnonblock_op = pcapint_getnonblock_fd;
487*afdbf109SJoseph Mingrone 	handle->setnonblock_op = pcapint_setnonblock_fd;
488a8e07101SRui Paulo 
489a8e07101SRui Paulo 	/*get usb bus index from device name */
490ada6f083SXin LI 	if (sscanf(handle->opt.device, USB_IFACE"%d", &handlep->bus_index) != 1)
491a8e07101SRui Paulo 	{
4926f9cba8fSJoseph Mingrone 		snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
493ada6f083SXin LI 			"Can't get USB bus index from %s", handle->opt.device);
494a8e07101SRui Paulo 		return PCAP_ERROR;
495a8e07101SRui Paulo 	}
496a8e07101SRui Paulo 
497b00ab754SHans Petter Selasky 	/*
4986f9cba8fSJoseph Mingrone 	 * We require 2.6.27 or later kernels, so we have binary-mode support.
499b00ab754SHans Petter Selasky 	 * Try to open the binary interface.
500b00ab754SHans Petter Selasky 	 */
5016f9cba8fSJoseph Mingrone 	snprintf(full_path, USB_LINE_LEN, "/dev/"USBMON_DEV_PREFIX"%d",
5026f9cba8fSJoseph Mingrone 	    handlep->bus_index);
503a8e07101SRui Paulo 	handle->fd = open(full_path, O_RDONLY, 0);
504b00ab754SHans Petter Selasky 	if (handle->fd < 0)
505a8e07101SRui Paulo 	{
506b00ab754SHans Petter Selasky 		/*
507b00ab754SHans Petter Selasky 		 * The attempt failed; why?
508b00ab754SHans Petter Selasky 		 */
509b00ab754SHans Petter Selasky 		switch (errno) {
510b00ab754SHans Petter Selasky 
511b00ab754SHans Petter Selasky 		case ENOENT:
512b00ab754SHans Petter Selasky 			/*
513b00ab754SHans Petter Selasky 			 * The device doesn't exist.
514b00ab754SHans Petter Selasky 			 * That could either mean that there's
515b00ab754SHans Petter Selasky 			 * no support for monitoring USB buses
516b00ab754SHans Petter Selasky 			 * (which probably means "the usbmon
517b00ab754SHans Petter Selasky 			 * module isn't loaded") or that there
518b00ab754SHans Petter Selasky 			 * is but that *particular* device
519b00ab754SHans Petter Selasky 			 * doesn't exist (no "scan all buses"
520b00ab754SHans Petter Selasky 			 * device if the bus index is 0, no
521b00ab754SHans Petter Selasky 			 * such bus if the bus index isn't 0).
5226f9cba8fSJoseph Mingrone 			 *
5236f9cba8fSJoseph Mingrone 			 * For now, don't provide an error message;
5246f9cba8fSJoseph Mingrone 			 * if we can determine what the particular
5256f9cba8fSJoseph Mingrone 			 * problem is, we should report that.
526b00ab754SHans Petter Selasky 			 */
5276f9cba8fSJoseph Mingrone 			handle->errbuf[0] = '\0';
528b00ab754SHans Petter Selasky 			return PCAP_ERROR_NO_SUCH_DEVICE;
529b00ab754SHans Petter Selasky 
530b00ab754SHans Petter Selasky 		case EACCES:
531b00ab754SHans Petter Selasky 			/*
532b00ab754SHans Petter Selasky 			 * We didn't have permission to open it.
533b00ab754SHans Petter Selasky 			 */
5346f9cba8fSJoseph Mingrone DIAG_OFF_FORMAT_TRUNCATION
5356f9cba8fSJoseph Mingrone 			snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
5366f9cba8fSJoseph Mingrone 			    "Attempt to open %s failed with EACCES - root privileges may be required",
5376f9cba8fSJoseph Mingrone 			    full_path);
5386f9cba8fSJoseph Mingrone DIAG_ON_FORMAT_TRUNCATION
539b00ab754SHans Petter Selasky 			return PCAP_ERROR_PERM_DENIED;
540b00ab754SHans Petter Selasky 
541b00ab754SHans Petter Selasky 		default:
542b00ab754SHans Petter Selasky 			/*
543b00ab754SHans Petter Selasky 			 * Something went wrong.
544b00ab754SHans Petter Selasky 			 */
545*afdbf109SJoseph Mingrone 			pcapint_fmt_errmsg_for_errno(handle->errbuf,
546b00ab754SHans Petter Selasky 			    PCAP_ERRBUF_SIZE, errno,
547b00ab754SHans Petter Selasky 			    "Can't open USB bus file %s", full_path);
548b00ab754SHans Petter Selasky 			return PCAP_ERROR;
549b00ab754SHans Petter Selasky 		}
550b00ab754SHans Petter Selasky 	}
551b00ab754SHans Petter Selasky 
552b00ab754SHans Petter Selasky 	if (handle->opt.rfmon)
553b00ab754SHans Petter Selasky 	{
554a8e07101SRui Paulo 		/*
555a8e07101SRui Paulo 		 * Monitor mode doesn't apply to USB devices.
556a8e07101SRui Paulo 		 */
557a0ee43a1SRui Paulo 		close(handle->fd);
558a8e07101SRui Paulo 		return PCAP_ERROR_RFMON_NOTSUP;
559a8e07101SRui Paulo 	}
560a8e07101SRui Paulo 
561b00ab754SHans Petter Selasky 	/* try to use fast mmap access */
562b00ab754SHans Petter Selasky 	if (usb_mmap(handle))
563b00ab754SHans Petter Selasky 	{
56457e22627SCy Schubert 		/* We succeeded. */
565a0ee43a1SRui Paulo 		handle->linktype = DLT_USB_LINUX_MMAPPED;
566a8e07101SRui Paulo 		handle->stats_op = usb_stats_linux_bin;
567a8e07101SRui Paulo 		handle->read_op = usb_read_linux_mmap;
568a8e07101SRui Paulo 		handle->cleanup_op = usb_cleanup_linux_mmap;
569681ed54cSXin LI #ifdef HAVE_LINUX_USBDEVICE_FS_H
570681ed54cSXin LI 		probe_devices(handlep->bus_index);
571681ed54cSXin LI #endif
572a8e07101SRui Paulo 
573a8e07101SRui Paulo 		/*
574b00ab754SHans Petter Selasky 		 * "handle->fd" is a real file, so
575b00ab754SHans Petter Selasky 		 * "select()" and "poll()" work on it.
576a8e07101SRui Paulo 		 */
577a8e07101SRui Paulo 		handle->selectable_fd = handle->fd;
578a8e07101SRui Paulo 		return 0;
579a8e07101SRui Paulo 	}
580a8e07101SRui Paulo 
58157e22627SCy Schubert 	/*
58257e22627SCy Schubert 	 * We failed; try plain binary interface access.
58357e22627SCy Schubert 	 *
58457e22627SCy Schubert 	 * Attempt to set the ring size as appropriate for
58557e22627SCy Schubert 	 * the snapshot length, reducing the snapshot length
58657e22627SCy Schubert 	 * if that'd make the ring bigger than the kernel
58757e22627SCy Schubert 	 * supports.
58857e22627SCy Schubert 	 */
58957e22627SCy Schubert 	if (usb_set_ring_size(handle, (int)sizeof(pcap_usb_header)) == -1) {
59057e22627SCy Schubert 		/* Failed. */
59157e22627SCy Schubert 		close(handle->fd);
59257e22627SCy Schubert 		return PCAP_ERROR;
59357e22627SCy Schubert 	}
594a8e07101SRui Paulo 	handle->stats_op = usb_stats_linux_bin;
595a8e07101SRui Paulo 	handle->read_op = usb_read_linux_bin;
596681ed54cSXin LI #ifdef HAVE_LINUX_USBDEVICE_FS_H
597681ed54cSXin LI 	probe_devices(handlep->bus_index);
598681ed54cSXin LI #endif
599a0ee43a1SRui Paulo 
600a8e07101SRui Paulo 	/*
601a8e07101SRui Paulo 	 * "handle->fd" is a real file, so "select()" and "poll()"
602a8e07101SRui Paulo 	 * work on it.
603a8e07101SRui Paulo 	 */
604a8e07101SRui Paulo 	handle->selectable_fd = handle->fd;
605a8e07101SRui Paulo 
606a8e07101SRui Paulo 	/* for plain binary access and text access we need to allocate the read
607a8e07101SRui Paulo 	 * buffer */
608a8e07101SRui Paulo 	handle->buffer = malloc(handle->bufsize);
609a8e07101SRui Paulo 	if (!handle->buffer) {
610*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
611b00ab754SHans Petter Selasky 		    errno, "malloc");
612a0ee43a1SRui Paulo 		close(handle->fd);
613a8e07101SRui Paulo 		return PCAP_ERROR;
614a8e07101SRui Paulo 	}
615a8e07101SRui Paulo 	return 0;
616a8e07101SRui Paulo }
617a8e07101SRui Paulo 
618a8e07101SRui Paulo static int
usb_inject_linux(pcap_t * handle,const void * buf _U_,int size _U_)6196f9cba8fSJoseph Mingrone usb_inject_linux(pcap_t *handle, const void *buf _U_, int size _U_)
620a8e07101SRui Paulo {
6216f9cba8fSJoseph Mingrone 	snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
62257e22627SCy Schubert 	    "Packet injection is not supported on USB devices");
623a8e07101SRui Paulo 	return (-1);
624a8e07101SRui Paulo }
625a8e07101SRui Paulo 
626a8e07101SRui Paulo static int
usb_setdirection_linux(pcap_t * p,pcap_direction_t d)627a8e07101SRui Paulo usb_setdirection_linux(pcap_t *p, pcap_direction_t d)
628a8e07101SRui Paulo {
6296f9cba8fSJoseph Mingrone 	/*
6306f9cba8fSJoseph Mingrone 	 * It's guaranteed, at this point, that d is a valid
6316f9cba8fSJoseph Mingrone 	 * direction value.
6326f9cba8fSJoseph Mingrone 	 */
633a8e07101SRui Paulo 	p->direction = d;
634a8e07101SRui Paulo 	return 0;
635a8e07101SRui Paulo }
636a8e07101SRui Paulo 
637a8e07101SRui Paulo static int
usb_stats_linux_bin(pcap_t * handle,struct pcap_stat * stats)638a8e07101SRui Paulo usb_stats_linux_bin(pcap_t *handle, struct pcap_stat *stats)
639a8e07101SRui Paulo {
640681ed54cSXin LI 	struct pcap_usb_linux *handlep = handle->priv;
641a8e07101SRui Paulo 	int ret;
642a8e07101SRui Paulo 	struct mon_bin_stats st;
643a8e07101SRui Paulo 	ret = ioctl(handle->fd, MON_IOCG_STATS, &st);
644a8e07101SRui Paulo 	if (ret < 0)
645a8e07101SRui Paulo 	{
646*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
647b00ab754SHans Petter Selasky 		    errno, "Can't read stats from fd %d", handle->fd);
648a8e07101SRui Paulo 		return -1;
649a8e07101SRui Paulo 	}
650a8e07101SRui Paulo 
651681ed54cSXin LI 	stats->ps_recv = handlep->packets_read + st.queued;
652a0ee43a1SRui Paulo 	stats->ps_drop = st.dropped;
653a0ee43a1SRui Paulo 	stats->ps_ifdrop = 0;
654a8e07101SRui Paulo 	return 0;
655a8e07101SRui Paulo }
656a8e07101SRui Paulo 
657a8e07101SRui Paulo /*
658a8e07101SRui Paulo  * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
659a8e07101SRui Paulo  * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
660a8e07101SRui Paulo  */
661a8e07101SRui Paulo static int
usb_read_linux_bin(pcap_t * handle,int max_packets _U_,pcap_handler callback,u_char * user)662b00ab754SHans Petter Selasky usb_read_linux_bin(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_char *user)
663a8e07101SRui Paulo {
664681ed54cSXin LI 	struct pcap_usb_linux *handlep = handle->priv;
665a8e07101SRui Paulo 	struct mon_bin_get info;
666a8e07101SRui Paulo 	int ret;
667a8e07101SRui Paulo 	struct pcap_pkthdr pkth;
668ada6f083SXin LI 	u_int clen = handle->snapshot - sizeof(pcap_usb_header);
669a8e07101SRui Paulo 
670a8e07101SRui Paulo 	/* the usb header is going to be part of 'packet' data*/
671a8e07101SRui Paulo 	info.hdr = (pcap_usb_header*) handle->buffer;
672ada6f083SXin LI 	info.data = (u_char *)handle->buffer + sizeof(pcap_usb_header);
673a8e07101SRui Paulo 	info.data_len = clen;
674a8e07101SRui Paulo 
675a8e07101SRui Paulo 	/* ignore interrupt system call errors */
676a8e07101SRui Paulo 	do {
677a8e07101SRui Paulo 		ret = ioctl(handle->fd, MON_IOCX_GET, &info);
678a8e07101SRui Paulo 		if (handle->break_loop)
679a8e07101SRui Paulo 		{
680a8e07101SRui Paulo 			handle->break_loop = 0;
681a8e07101SRui Paulo 			return -2;
682a8e07101SRui Paulo 		}
683a8e07101SRui Paulo 	} while ((ret == -1) && (errno == EINTR));
684a8e07101SRui Paulo 	if (ret < 0)
685a8e07101SRui Paulo 	{
686a8e07101SRui Paulo 		if (errno == EAGAIN)
687a8e07101SRui Paulo 			return 0;	/* no data there */
688a8e07101SRui Paulo 
689*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
690b00ab754SHans Petter Selasky 		    errno, "Can't read from fd %d", handle->fd);
691a8e07101SRui Paulo 		return -1;
692a8e07101SRui Paulo 	}
693a8e07101SRui Paulo 
69457e22627SCy Schubert 	/*
69557e22627SCy Schubert 	 * info.hdr->data_len is the number of bytes of isochronous
69657e22627SCy Schubert 	 * descriptors (if any) plus the number of bytes of data
69757e22627SCy Schubert 	 * provided.  There are no isochronous descriptors here,
69857e22627SCy Schubert 	 * because we're using the old 48-byte header.
69957e22627SCy Schubert 	 *
70057e22627SCy Schubert 	 * If info.hdr->data_flag is non-zero, there's no URB data;
70157e22627SCy Schubert 	 * info.hdr->urb_len is the size of the buffer into which
70257e22627SCy Schubert 	 * data is to be placed; it does not represent the amount
70357e22627SCy Schubert 	 * of data transferred.  If info.hdr->data_flag is zero,
70457e22627SCy Schubert 	 * there is URB data, and info.hdr->urb_len is the number
70557e22627SCy Schubert 	 * of bytes transmitted or received; it doesn't include
70657e22627SCy Schubert 	 * isochronous descriptors.
70757e22627SCy Schubert 	 *
70857e22627SCy Schubert 	 * The kernel may give us more data than the snaplen; if it did,
70957e22627SCy Schubert 	 * reduce the data length so that the total number of bytes we
71057e22627SCy Schubert 	 * tell our client we have is not greater than the snaplen.
71157e22627SCy Schubert 	 */
712a8e07101SRui Paulo 	if (info.hdr->data_len < clen)
713a8e07101SRui Paulo 		clen = info.hdr->data_len;
714a8e07101SRui Paulo 	info.hdr->data_len = clen;
71557e22627SCy Schubert 	pkth.caplen = sizeof(pcap_usb_header) + clen;
71657e22627SCy Schubert 	if (info.hdr->data_flag) {
71757e22627SCy Schubert 		/*
718*afdbf109SJoseph Mingrone 		 * No data; just base the original length on
71957e22627SCy Schubert 		 * info.hdr->data_len (so that it's >= the captured
72057e22627SCy Schubert 		 * length).
72157e22627SCy Schubert 		 */
72257e22627SCy Schubert 		pkth.len = sizeof(pcap_usb_header) + info.hdr->data_len;
72357e22627SCy Schubert 	} else {
72457e22627SCy Schubert 		/*
725*afdbf109SJoseph Mingrone 		 * We got data; base the original length on
72657e22627SCy Schubert 		 * info.hdr->urb_len, so that it includes data
72757e22627SCy Schubert 		 * discarded by the USB monitor device due to
72857e22627SCy Schubert 		 * its buffer being too small.
72957e22627SCy Schubert 		 */
73057e22627SCy Schubert 		pkth.len = sizeof(pcap_usb_header) + info.hdr->urb_len;
73157e22627SCy Schubert 	}
7326f9cba8fSJoseph Mingrone 	pkth.ts.tv_sec = (time_t)info.hdr->ts_sec;
733a8e07101SRui Paulo 	pkth.ts.tv_usec = info.hdr->ts_usec;
734a8e07101SRui Paulo 
735d1e87331SXin LI 	if (handle->fcode.bf_insns == NULL ||
736*afdbf109SJoseph Mingrone 	    pcapint_filter(handle->fcode.bf_insns, handle->buffer,
737d1e87331SXin LI 	      pkth.len, pkth.caplen)) {
738681ed54cSXin LI 		handlep->packets_read++;
739a8e07101SRui Paulo 		callback(user, &pkth, handle->buffer);
740a8e07101SRui Paulo 		return 1;
741a8e07101SRui Paulo 	}
742a8e07101SRui Paulo 
743d1e87331SXin LI 	return 0;	/* didn't pass filter */
744d1e87331SXin LI }
745d1e87331SXin LI 
746a8e07101SRui Paulo /*
747a8e07101SRui Paulo  * see <linux-kernel-source>/Documentation/usb/usbmon.txt and
748a8e07101SRui Paulo  * <linux-kernel-source>/drivers/usb/mon/mon_bin.c binary ABI
749a8e07101SRui Paulo  */
750a8e07101SRui Paulo #define VEC_SIZE 32
751a8e07101SRui Paulo static int
usb_read_linux_mmap(pcap_t * handle,int max_packets,pcap_handler callback,u_char * user)752a8e07101SRui Paulo usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
753a8e07101SRui Paulo {
754681ed54cSXin LI 	struct pcap_usb_linux *handlep = handle->priv;
755a8e07101SRui Paulo 	struct mon_bin_mfetch fetch;
756a8e07101SRui Paulo 	int32_t vec[VEC_SIZE];
757a8e07101SRui Paulo 	struct pcap_pkthdr pkth;
7586f9cba8fSJoseph Mingrone 	u_char *bp;
75957e22627SCy Schubert 	pcap_usb_header_mmapped* hdr;
760a8e07101SRui Paulo 	int nflush = 0;
761a8e07101SRui Paulo 	int packets = 0;
762ada6f083SXin LI 	u_int clen, max_clen;
763a0ee43a1SRui Paulo 
76457e22627SCy Schubert 	max_clen = handle->snapshot - sizeof(pcap_usb_header_mmapped);
765a8e07101SRui Paulo 
766a8e07101SRui Paulo 	for (;;) {
767a8e07101SRui Paulo 		int i, ret;
7686f9cba8fSJoseph Mingrone 		int limit;
7696f9cba8fSJoseph Mingrone 
7706f9cba8fSJoseph Mingrone 		if (PACKET_COUNT_IS_UNLIMITED(max_packets)) {
7716f9cba8fSJoseph Mingrone 			/*
7726f9cba8fSJoseph Mingrone 			 * There's no limit on the number of packets
7736f9cba8fSJoseph Mingrone 			 * to process, so try to fetch VEC_SIZE packets.
7746f9cba8fSJoseph Mingrone 			 */
775a8e07101SRui Paulo 			limit = VEC_SIZE;
7766f9cba8fSJoseph Mingrone 		} else {
7776f9cba8fSJoseph Mingrone 			/*
7786f9cba8fSJoseph Mingrone 			 * Try to fetch as many packets as we have left
7796f9cba8fSJoseph Mingrone 			 * to process, or VEC_SIZE packets, whichever
7806f9cba8fSJoseph Mingrone 			 * is less.
7816f9cba8fSJoseph Mingrone 			 *
7826f9cba8fSJoseph Mingrone 			 * At this point, max_packets > 0 (otherwise,
7836f9cba8fSJoseph Mingrone 			 * PACKET_COUNT_IS_UNLIMITED(max_packets)
7846f9cba8fSJoseph Mingrone 			 * would be true) and max_packets > packets
7856f9cba8fSJoseph Mingrone 			 * (packet starts out as 0, and the test
7866f9cba8fSJoseph Mingrone 			 * at the bottom of the loop exits if
7876f9cba8fSJoseph Mingrone 			 * max_packets <= packets), so limit is
7886f9cba8fSJoseph Mingrone 			 * guaranteed to be > 0.
7896f9cba8fSJoseph Mingrone 			 */
7906f9cba8fSJoseph Mingrone 			limit = max_packets - packets;
791a8e07101SRui Paulo 			if (limit > VEC_SIZE)
792a8e07101SRui Paulo 				limit = VEC_SIZE;
7936f9cba8fSJoseph Mingrone 		}
794a8e07101SRui Paulo 
7956f9cba8fSJoseph Mingrone 		/*
7966f9cba8fSJoseph Mingrone 		 * Try to fetch as many events as possible, up to
7976f9cba8fSJoseph Mingrone 		 * the limit, and flush the events we've processed
7986f9cba8fSJoseph Mingrone 		 * earlier (nflush) - MON_IOCX_MFETCH does both
7996f9cba8fSJoseph Mingrone 		 * (presumably to reduce the number of system
8006f9cba8fSJoseph Mingrone 		 * calls in loops like this).
8016f9cba8fSJoseph Mingrone 		 */
802a8e07101SRui Paulo 		fetch.offvec = vec;
803a8e07101SRui Paulo 		fetch.nfetch = limit;
804a8e07101SRui Paulo 		fetch.nflush = nflush;
805a8e07101SRui Paulo 		/* ignore interrupt system call errors */
806a8e07101SRui Paulo 		do {
807a8e07101SRui Paulo 			ret = ioctl(handle->fd, MON_IOCX_MFETCH, &fetch);
808a8e07101SRui Paulo 			if (handle->break_loop)
809a8e07101SRui Paulo 			{
810a8e07101SRui Paulo 				handle->break_loop = 0;
811a8e07101SRui Paulo 				return -2;
812a8e07101SRui Paulo 			}
813a8e07101SRui Paulo 		} while ((ret == -1) && (errno == EINTR));
814a8e07101SRui Paulo 		if (ret < 0)
815a8e07101SRui Paulo 		{
816a8e07101SRui Paulo 			if (errno == EAGAIN)
817a8e07101SRui Paulo 				return 0;	/* no data there */
818a8e07101SRui Paulo 
819*afdbf109SJoseph Mingrone 			pcapint_fmt_errmsg_for_errno(handle->errbuf,
820b00ab754SHans Petter Selasky 			    PCAP_ERRBUF_SIZE, errno, "Can't mfetch fd %d",
821b00ab754SHans Petter Selasky 			    handle->fd);
822a8e07101SRui Paulo 			return -1;
823a8e07101SRui Paulo 		}
824a8e07101SRui Paulo 
825a8e07101SRui Paulo 		/* keep track of processed events, we will flush them later */
826a8e07101SRui Paulo 		nflush = fetch.nfetch;
827a8e07101SRui Paulo 		for (i=0; i<fetch.nfetch; ++i) {
8286f9cba8fSJoseph Mingrone 			/*
8296f9cba8fSJoseph Mingrone 			 * XXX - we can't check break_loop here, as
8306f9cba8fSJoseph Mingrone 			 * we read the indices of packets into a
8316f9cba8fSJoseph Mingrone 			 * local variable, so if we're later called
8326f9cba8fSJoseph Mingrone 			 * to fetch more packets, those packets will
8336f9cba8fSJoseph Mingrone 			 * not be seen - and won't be flushed, either.
8346f9cba8fSJoseph Mingrone 			 *
8356f9cba8fSJoseph Mingrone 			 * Instead, we would have to keep the array
8366f9cba8fSJoseph Mingrone 			 * of indices in our private data, along
8376f9cba8fSJoseph Mingrone 			 * with the count of packets to flush - or
8386f9cba8fSJoseph Mingrone 			 * would have to flush the already-processed
8396f9cba8fSJoseph Mingrone 			 * packets if we break out of the loop here.
8406f9cba8fSJoseph Mingrone 			 */
8416f9cba8fSJoseph Mingrone 
8426f9cba8fSJoseph Mingrone 			/* Get a pointer to this packet's buffer */
8436f9cba8fSJoseph Mingrone 			bp = &handlep->mmapbuf[vec[i]];
8446f9cba8fSJoseph Mingrone 
8456f9cba8fSJoseph Mingrone 			/* That begins with a metadata header */
8466f9cba8fSJoseph Mingrone 			hdr = (pcap_usb_header_mmapped*) bp;
8476f9cba8fSJoseph Mingrone 
848a8e07101SRui Paulo 			/* discard filler */
849a8e07101SRui Paulo 			if (hdr->event_type == '@')
850a8e07101SRui Paulo 				continue;
851a8e07101SRui Paulo 
85257e22627SCy Schubert 			/*
85357e22627SCy Schubert 			 * hdr->data_len is the number of bytes of
85457e22627SCy Schubert 			 * isochronous descriptors (if any) plus the
85557e22627SCy Schubert 			 * number of bytes of data provided.
85657e22627SCy Schubert 			 *
85757e22627SCy Schubert 			 * If hdr->data_flag is non-zero, there's no
85857e22627SCy Schubert 			 * URB data; hdr->urb_len is the size of the
85957e22627SCy Schubert 			 * buffer into which data is to be placed; it does
86057e22627SCy Schubert 			 * not represent the amount of data transferred.
86157e22627SCy Schubert 			 * If hdr->data_flag is zero, there is URB data,
86257e22627SCy Schubert 			 * and hdr->urb_len is the number of bytes
86357e22627SCy Schubert 			 * transmitted or received; it doesn't include
86457e22627SCy Schubert 			 * isochronous descriptors.
86557e22627SCy Schubert 			 *
86657e22627SCy Schubert 			 * The kernel may give us more data than the
86757e22627SCy Schubert 			 * snaplen; if it did, reduce the data length
86857e22627SCy Schubert 			 * so that the total number of bytes we
86957e22627SCy Schubert 			 * tell our client we have is not greater than
87057e22627SCy Schubert 			 * the snaplen.
87157e22627SCy Schubert 			 */
872a0ee43a1SRui Paulo 			clen = max_clen;
873a0ee43a1SRui Paulo 			if (hdr->data_len < clen)
874a0ee43a1SRui Paulo 				clen = hdr->data_len;
87557e22627SCy Schubert 			pkth.caplen = sizeof(pcap_usb_header_mmapped) + clen;
87657e22627SCy Schubert 			if (hdr->data_flag) {
87757e22627SCy Schubert 				/*
878*afdbf109SJoseph Mingrone 				 * No data; just base the original length
87957e22627SCy Schubert 				 * on hdr->data_len (so that it's >= the
880*afdbf109SJoseph Mingrone 				 * captured length).  Clamp the result
881*afdbf109SJoseph Mingrone 				 * at UINT_MAX, so it fits in an unsigned
882*afdbf109SJoseph Mingrone 				 * int.
88357e22627SCy Schubert 				 */
884*afdbf109SJoseph Mingrone 				pkth.len = u_int_sum(sizeof(pcap_usb_header_mmapped),
885*afdbf109SJoseph Mingrone 				    hdr->data_len);
88657e22627SCy Schubert 			} else {
88757e22627SCy Schubert 				/*
888*afdbf109SJoseph Mingrone 				 * We got data.
88957e22627SCy Schubert 				 */
890*afdbf109SJoseph Mingrone 				if (is_isochronous_transfer_completion(hdr)) {
8916f9cba8fSJoseph Mingrone 					/*
892*afdbf109SJoseph Mingrone 					 * For isochronous transfer completion
893*afdbf109SJoseph Mingrone 					 * events, hdr->urb_len doesn't take
894*afdbf109SJoseph Mingrone 					 * into account the way the data is
895*afdbf109SJoseph Mingrone 					 * put into the buffer, as it doesn't
896*afdbf109SJoseph Mingrone 					 * count any padding between the
897*afdbf109SJoseph Mingrone 					 * chunks of isochronous data, so
898*afdbf109SJoseph Mingrone 					 * we have to calculate the amount
899*afdbf109SJoseph Mingrone 					 * of data from the isochronous
900*afdbf109SJoseph Mingrone 					 * descriptors.
9016f9cba8fSJoseph Mingrone 					 */
902*afdbf109SJoseph Mingrone 					pkth.len = incoming_isochronous_transfer_completed_len(&pkth, bp);
903*afdbf109SJoseph Mingrone 				} else {
904*afdbf109SJoseph Mingrone 					/*
905*afdbf109SJoseph Mingrone 					 * For everything else, the original
906*afdbf109SJoseph Mingrone 					 * data length is just the length of
907*afdbf109SJoseph Mingrone 					 * the memory-mapped Linux USB header
908*afdbf109SJoseph Mingrone 					 * plus hdr->urb_len; we use
909*afdbf109SJoseph Mingrone 					 * hdr->urb_len so that it includes
910*afdbf109SJoseph Mingrone 					 * data discarded by the USB monitor
911*afdbf109SJoseph Mingrone 					 * device due to its buffer being
912*afdbf109SJoseph Mingrone 					 * too small.  Clamp the result at
913*afdbf109SJoseph Mingrone 					 * UINT_MAX, so it fits in an
914*afdbf109SJoseph Mingrone 					 * unsigned int.
915*afdbf109SJoseph Mingrone 					 */
916*afdbf109SJoseph Mingrone 					pkth.len = u_int_sum(sizeof(pcap_usb_header_mmapped),
917*afdbf109SJoseph Mingrone 					    hdr->urb_len);
918*afdbf109SJoseph Mingrone 				}
91957e22627SCy Schubert 			}
9206f9cba8fSJoseph Mingrone 			pkth.ts.tv_sec = (time_t)hdr->ts_sec;
921a8e07101SRui Paulo 			pkth.ts.tv_usec = hdr->ts_usec;
922a8e07101SRui Paulo 
923d1e87331SXin LI 			if (handle->fcode.bf_insns == NULL ||
924*afdbf109SJoseph Mingrone 			    pcapint_filter(handle->fcode.bf_insns, (u_char*) hdr,
925d1e87331SXin LI 			      pkth.len, pkth.caplen)) {
926681ed54cSXin LI 				handlep->packets_read++;
927a8e07101SRui Paulo 				callback(user, &pkth, (u_char*) hdr);
928a8e07101SRui Paulo 				packets++;
929a8e07101SRui Paulo 			}
930d1e87331SXin LI 		}
931a8e07101SRui Paulo 
9326f9cba8fSJoseph Mingrone 		/*
933*afdbf109SJoseph Mingrone 		 * If max_packets specifies "unlimited", we stop after
9346f9cba8fSJoseph Mingrone 		 * the first chunk.
9356f9cba8fSJoseph Mingrone 		 */
9366f9cba8fSJoseph Mingrone 		if (PACKET_COUNT_IS_UNLIMITED(max_packets) ||
9376f9cba8fSJoseph Mingrone 		    (packets >= max_packets))
938a8e07101SRui Paulo 			break;
939a8e07101SRui Paulo 	}
940a8e07101SRui Paulo 
941a8e07101SRui Paulo 	/* flush pending events*/
942ada6f083SXin LI 	if (ioctl(handle->fd, MON_IOCH_MFLUSH, nflush) == -1) {
943*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE,
944b00ab754SHans Petter Selasky 		    errno, "Can't mflush fd %d", handle->fd);
945ada6f083SXin LI 		return -1;
946ada6f083SXin LI 	}
947a8e07101SRui Paulo 	return packets;
948a8e07101SRui Paulo }
949a8e07101SRui Paulo 
950a8e07101SRui Paulo static void
usb_cleanup_linux_mmap(pcap_t * handle)951a8e07101SRui Paulo usb_cleanup_linux_mmap(pcap_t* handle)
952a8e07101SRui Paulo {
953681ed54cSXin LI 	struct pcap_usb_linux *handlep = handle->priv;
954681ed54cSXin LI 
955a0ee43a1SRui Paulo 	/* if we have a memory-mapped buffer, unmap it */
956681ed54cSXin LI 	if (handlep->mmapbuf != NULL) {
957681ed54cSXin LI 		munmap(handlep->mmapbuf, handlep->mmapbuflen);
958681ed54cSXin LI 		handlep->mmapbuf = NULL;
959a0ee43a1SRui Paulo 	}
960*afdbf109SJoseph Mingrone 	pcapint_cleanup_live_common(handle);
961a8e07101SRui Paulo }
962