2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Weongyo Jeong <weongyo@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
34 #include <sys/param.h>
35 #include <sys/endian.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/utsname.h>
41 #include <sys/queue.h>
44 #include <dev/usb/usb.h>
45 #include <dev/usb/usb_pf.h>
46 #include <dev/usb/usbdi.h>
59 #define BPF_STORE_JUMP(x,_c,_k,_jt,_jf) do { \
66 #define BPF_STORE_STMT(x,_c,_k) do { \
74 STAILQ_ENTRY(usb_filt) entry;
80 int fd; /* fd for /dev/usbpf */
92 struct usbcap_filehdr {
94 #define USBCAP_FILEHDR_MAGIC 0x9a90000e
101 /* capture timestamp */
104 /* data length and alignment information */
111 static int doexit = 0;
112 static int pkt_captured = 0;
113 static int verbose = 0;
115 static const char *i_arg = "usbus0";
116 static const char *r_arg = NULL;
117 static const char *w_arg = NULL;
118 static const char *b_arg = NULL;
119 static struct usbcap uc;
120 static const char *errstr_table[USB_ERR_MAX] = {
121 [USB_ERR_NORMAL_COMPLETION] = "0",
122 [USB_ERR_PENDING_REQUESTS] = "PENDING_REQUESTS",
123 [USB_ERR_NOT_STARTED] = "NOT_STARTED",
124 [USB_ERR_INVAL] = "INVAL",
125 [USB_ERR_NOMEM] = "NOMEM",
126 [USB_ERR_CANCELLED] = "CANCELLED",
127 [USB_ERR_BAD_ADDRESS] = "BAD_ADDRESS",
128 [USB_ERR_BAD_BUFSIZE] = "BAD_BUFSIZE",
129 [USB_ERR_BAD_FLAG] = "BAD_FLAG",
130 [USB_ERR_NO_CALLBACK] = "NO_CALLBACK",
131 [USB_ERR_IN_USE] = "IN_USE",
132 [USB_ERR_NO_ADDR] = "NO_ADDR",
133 [USB_ERR_NO_PIPE] = "NO_PIPE",
134 [USB_ERR_ZERO_NFRAMES] = "ZERO_NFRAMES",
135 [USB_ERR_ZERO_MAXP] = "ZERO_MAXP",
136 [USB_ERR_SET_ADDR_FAILED] = "SET_ADDR_FAILED",
137 [USB_ERR_NO_POWER] = "NO_POWER",
138 [USB_ERR_TOO_DEEP] = "TOO_DEEP",
139 [USB_ERR_IOERROR] = "IOERROR",
140 [USB_ERR_NOT_CONFIGURED] = "NOT_CONFIGURED",
141 [USB_ERR_TIMEOUT] = "TIMEOUT",
142 [USB_ERR_SHORT_XFER] = "SHORT_XFER",
143 [USB_ERR_STALLED] = "STALLED",
144 [USB_ERR_INTERRUPTED] = "INTERRUPTED",
145 [USB_ERR_DMA_LOAD_FAILED] = "DMA_LOAD_FAILED",
146 [USB_ERR_BAD_CONTEXT] = "BAD_CONTEXT",
147 [USB_ERR_NO_ROOT_HUB] = "NO_ROOT_HUB",
148 [USB_ERR_NO_INTR_THREAD] = "NO_INTR_THREAD",
149 [USB_ERR_NOT_LOCKED] = "NOT_LOCKED",
152 #define USB_XFERTYPE_MAX 4
154 static const char *xfertype_table[USB_XFERTYPE_MAX] = {
155 [UE_CONTROL] = "CTRL",
156 [UE_ISOCHRONOUS] = "ISOC",
158 [UE_INTERRUPT] = "INTR"
161 static const char *speed_table[USB_SPEED_MAX] = {
162 [USB_SPEED_FULL] = "FULL",
163 [USB_SPEED_HIGH] = "HIGH",
164 [USB_SPEED_LOW] = "LOW",
165 [USB_SPEED_VARIABLE] = "VARI",
166 [USB_SPEED_SUPER] = "SUPER",
169 static STAILQ_HEAD(,usb_filt) usb_filt_head =
170 STAILQ_HEAD_INITIALIZER(usb_filt_head);
173 add_filter(int usb_filt_unit, int usb_filt_ep)
175 struct usb_filt *puf;
177 puf = malloc(sizeof(struct usb_filt));
179 errx(EX_SOFTWARE, "Out of memory.");
181 puf->unit = usb_filt_unit;
182 puf->endpoint = usb_filt_ep;
184 STAILQ_INSERT_TAIL(&usb_filt_head, puf, entry);
188 make_filter(struct bpf_program *pprog, int snapshot)
190 struct usb_filt *puf;
191 struct bpf_insn *dynamic_insn;
196 STAILQ_FOREACH(puf, &usb_filt_head, entry)
199 dynamic_insn = malloc(((len * 5) + 1) * sizeof(struct bpf_insn));
201 if (dynamic_insn == NULL)
202 errx(EX_SOFTWARE, "Out of memory.");
207 /* accept all packets */
209 BPF_STORE_STMT(dynamic_insn[0], BPF_RET | BPF_K, snapshot);
216 STAILQ_FOREACH(puf, &usb_filt_head, entry) {
217 const int addr_off = (uintptr_t)&((struct usbpf_pkthdr *)0)->up_address;
218 const int addr_ep = (uintptr_t)&((struct usbpf_pkthdr *)0)->up_endpoint;
220 if (puf->unit != -1) {
221 if (puf->endpoint != -1) {
222 BPF_STORE_STMT(dynamic_insn[len],
223 BPF_LD | BPF_B | BPF_ABS, addr_off);
225 BPF_STORE_JUMP(dynamic_insn[len],
226 BPF_JMP | BPF_JEQ | BPF_K, (uint8_t)puf->unit, 0, 3);
228 BPF_STORE_STMT(dynamic_insn[len],
229 BPF_LD | BPF_W | BPF_ABS, addr_ep);
231 BPF_STORE_JUMP(dynamic_insn[len],
232 BPF_JMP | BPF_JEQ | BPF_K, htobe32(puf->endpoint), 0, 1);
235 BPF_STORE_STMT(dynamic_insn[len],
236 BPF_LD | BPF_B | BPF_ABS, addr_off);
238 BPF_STORE_JUMP(dynamic_insn[len],
239 BPF_JMP | BPF_JEQ | BPF_K, (uint8_t)puf->unit, 0, 1);
243 if (puf->endpoint != -1) {
244 BPF_STORE_STMT(dynamic_insn[len],
245 BPF_LD | BPF_W | BPF_ABS, addr_ep);
247 BPF_STORE_JUMP(dynamic_insn[len],
248 BPF_JMP | BPF_JEQ | BPF_K, htobe32(puf->endpoint), 0, 1);
252 BPF_STORE_STMT(dynamic_insn[len],
253 BPF_RET | BPF_K, snapshot);
257 BPF_STORE_STMT(dynamic_insn[len], BPF_RET | BPF_K, 0);
262 pprog->bf_insns = dynamic_insn;
266 match_filter(int unit, int endpoint)
268 struct usb_filt *puf;
270 if (STAILQ_FIRST(&usb_filt_head) == NULL)
273 STAILQ_FOREACH(puf, &usb_filt_head, entry) {
274 if ((puf->unit == -1 || puf->unit == unit) &&
275 (puf->endpoint == -1 || puf->endpoint == endpoint))
282 free_filter(struct bpf_program *pprog)
284 struct usb_filt *puf;
286 while ((puf = STAILQ_FIRST(&usb_filt_head)) != NULL) {
287 STAILQ_REMOVE_HEAD(&usb_filt_head, entry);
290 free(pprog->bf_insns);
294 handle_sigint(int sig)
301 #define FLAGS(x, name) \
302 (((x) & USBPF_FLAG_##name) ? #name "|" : "")
304 #define STATUS(x, name) \
305 (((x) & USBPF_STATUS_##name) ? #name "|" : "")
308 usb_errstr(uint32_t error)
310 if (error >= USB_ERR_MAX || errstr_table[error] == NULL)
313 return (errstr_table[error]);
317 usb_speedstr(uint8_t speed)
319 if (speed >= USB_SPEED_MAX || speed_table[speed] == NULL)
322 return (speed_table[speed]);
326 usb_xferstr(uint8_t type)
328 if (type >= USB_XFERTYPE_MAX || xfertype_table[type] == NULL)
331 return (xfertype_table[type]);
335 print_flags(uint32_t flags)
337 printf(" flags %#x <%s%s%s%s%s%s%s%s%s0>\n",
339 FLAGS(flags, FORCE_SHORT_XFER),
340 FLAGS(flags, SHORT_XFER_OK),
341 FLAGS(flags, SHORT_FRAMES_OK),
342 FLAGS(flags, PIPE_BOF),
343 FLAGS(flags, PROXY_BUFFER),
344 FLAGS(flags, EXT_BUFFER),
345 FLAGS(flags, MANUAL_STATUS),
346 FLAGS(flags, NO_PIPE_OK),
347 FLAGS(flags, STALL_PIPE));
351 print_status(uint32_t status)
353 printf(" status %#x <%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s0>\n",
355 STATUS(status, OPEN),
356 STATUS(status, TRANSFERRING),
357 STATUS(status, DID_DMA_DELAY),
358 STATUS(status, DID_CLOSE),
359 STATUS(status, DRAINING),
360 STATUS(status, STARTED),
361 STATUS(status, BW_RECLAIMED),
362 STATUS(status, CONTROL_XFR),
363 STATUS(status, CONTROL_HDR),
364 STATUS(status, CONTROL_ACT),
365 STATUS(status, CONTROL_STALL),
366 STATUS(status, SHORT_FRAMES_OK),
367 STATUS(status, SHORT_XFER_OK),
368 STATUS(status, BDMA_ENABLE),
369 STATUS(status, BDMA_NO_POST_SYNC),
370 STATUS(status, BDMA_SETUP),
371 STATUS(status, ISOCHRONOUS_XFR),
372 STATUS(status, CURR_DMA_SET),
373 STATUS(status, CAN_CANCEL_IMMED),
374 STATUS(status, DOING_CALLBACK));
378 * Dump a byte into hex format.
381 hexbyte(char *buf, uint8_t temp)
392 buf[0] = 'A' + hi - 10;
397 buf[1] = 'A' + lo - 10;
401 * Display a region in traditional hexdump format.
404 hexdump(const uint8_t *region, uint32_t len)
412 for (line = region; line < (region + len); line += 16) {
417 hexbyte(linebuf + i + 1, ((line - region) >> 8) & 0xFF);
418 hexbyte(linebuf + i + 3, (line - region) & 0xFF);
419 linebuf[i + 5] = ' ';
420 linebuf[i + 6] = ' ';
423 for (x = 0; x < 16; x++) {
424 if ((line + x) < (region + len)) {
426 *(const u_int8_t *)(line + x));
429 linebuf[i + 1] = '-';
431 linebuf[i + 2] = ' ';
433 linebuf[i + 3] = ' ';
440 linebuf[i + 1] = '|';
442 for (x = 0; x < 16; x++) {
443 if ((line + x) < (region + len)) {
444 c = *(const u_int8_t *)(line + x);
446 if ((c < ' ') || (c > '~'))
462 print_apacket(const struct header_32 *hdr, const uint8_t *ptr, int ptr_len)
465 struct usbpf_pkthdr up_temp;
466 struct usbpf_pkthdr *up;
472 ptr += USBPF_HDR_LEN;
473 ptr_len -= USBPF_HDR_LEN;
477 /* make sure we don't change the source buffer */
478 memcpy(&up_temp, ptr - USBPF_HDR_LEN, sizeof(up_temp));
482 * A packet from the kernel is based on little endian byte
485 up->up_totlen = le32toh(up->up_totlen);
486 up->up_busunit = le32toh(up->up_busunit);
487 up->up_flags = le32toh(up->up_flags);
488 up->up_status = le32toh(up->up_status);
489 up->up_error = le32toh(up->up_error);
490 up->up_interval = le32toh(up->up_interval);
491 up->up_frames = le32toh(up->up_frames);
492 up->up_packet_size = le32toh(up->up_packet_size);
493 up->up_packet_count = le32toh(up->up_packet_count);
494 up->up_endpoint = le32toh(up->up_endpoint);
496 if (!match_filter(up->up_address, up->up_endpoint))
499 tv.tv_sec = hdr->ts_sec;
500 tv.tv_usec = hdr->ts_usec;
501 tm = localtime(&tv.tv_sec);
503 len = strftime(buf, sizeof(buf), "%H:%M:%S", tm);
506 printf("%.*s.%06ld usbus%d.%d %s-%s-EP=%08x,SPD=%s,NFR=%d,SLEN=%d,IVAL=%d%s%s\n",
507 (int)len, buf, tv.tv_usec,
508 (int)up->up_busunit, (int)up->up_address,
509 (up->up_type == USBPF_XFERTAP_SUBMIT) ? "SUBM" : "DONE",
510 usb_xferstr(up->up_xfertype),
511 (unsigned int)up->up_endpoint,
512 usb_speedstr(up->up_speed),
514 (int)(up->up_totlen - USBPF_HDR_LEN -
515 (USBPF_FRAME_HDR_LEN * up->up_frames)),
516 (int)up->up_interval,
517 (up->up_type == USBPF_XFERTAP_DONE) ? ",ERR=" : "",
518 (up->up_type == USBPF_XFERTAP_DONE) ?
519 usb_errstr(up->up_error) : "");
522 if (verbose >= 1 || b_arg != NULL) {
523 for (x = 0; x != up->up_frames; x++) {
524 const struct usbpf_framehdr *uf;
528 uf = (const struct usbpf_framehdr *)ptr;
529 ptr += USBPF_FRAME_HDR_LEN;
530 ptr_len -= USBPF_FRAME_HDR_LEN;
534 framelen = le32toh(uf->length);
535 flags = le32toh(uf->flags);
538 printf(" frame[%u] %s %d bytes\n",
540 (flags & USBPF_FRAMEFLAG_READ) ? "READ" : "WRITE",
544 if (flags & USBPF_FRAMEFLAG_DATA_FOLLOWS) {
548 tot_frame_len = USBPF_FRAME_ALIGN(framelen);
550 ptr_len -= tot_frame_len;
552 if (tot_frame_len < 0 ||
553 (int)framelen < 0 || (int)ptr_len < 0)
557 struct usbcap *p = &uc;
559 ret = write(p->bfd, ptr, framelen);
560 if (ret != (int)framelen)
561 err(EXIT_FAILURE, "Could not write binary data");
564 hexdump(ptr, framelen);
566 ptr += tot_frame_len;
571 print_flags(up->up_flags);
573 print_status(up->up_status);
577 fix_packets(uint8_t *data, const int datalen)
579 struct header_32 temp;
585 for (ptr = data; ptr < (data + datalen); ptr = next) {
587 const struct bpf_hdr *hdr;
589 hdr = (const struct bpf_hdr *)ptr;
591 temp.ts_sec = htole32(hdr->bh_tstamp.tv_sec);
592 temp.ts_usec = htole32(hdr->bh_tstamp.tv_usec);
593 temp.caplen = htole32(hdr->bh_caplen);
594 temp.datalen = htole32(hdr->bh_datalen);
595 temp.hdrlen = hdr->bh_hdrlen;
596 temp.align = BPF_WORDALIGN(1);
598 hdrlen = hdr->bh_hdrlen;
599 caplen = hdr->bh_caplen;
601 if ((hdrlen >= sizeof(temp)) && (hdrlen <= 255) &&
602 ((ptr + hdrlen) <= (data + datalen))) {
603 memcpy(ptr, &temp, sizeof(temp));
604 memset(ptr + sizeof(temp), 0, hdrlen - sizeof(temp));
606 err(EXIT_FAILURE, "Invalid header length %d", hdrlen);
609 next = ptr + BPF_WORDALIGN(hdrlen + caplen);
612 err(EXIT_FAILURE, "Invalid length");
617 print_packets(uint8_t *data, const int datalen)
619 struct header_32 temp;
623 for (ptr = data; ptr < (data + datalen); ptr = next) {
625 const struct header_32 *hdr32;
627 hdr32 = (const struct header_32 *)ptr;
629 temp.ts_sec = le32toh(hdr32->ts_sec);
630 temp.ts_usec = le32toh(hdr32->ts_usec);
631 temp.caplen = le32toh(hdr32->caplen);
632 temp.datalen = le32toh(hdr32->datalen);
633 temp.hdrlen = hdr32->hdrlen;
634 temp.align = hdr32->align;
636 next = ptr + roundup2(temp.hdrlen + temp.caplen, temp.align);
639 err(EXIT_FAILURE, "Invalid length");
641 if (verbose >= 0 || r_arg != NULL || b_arg != NULL) {
642 print_apacket(&temp, ptr +
643 temp.hdrlen, temp.caplen);
650 write_packets(struct usbcap *p, const uint8_t *data, const int datalen)
652 int len = htole32(datalen);
655 ret = write(p->wfd, &len, sizeof(int));
656 if (ret != sizeof(int)) {
657 err(EXIT_FAILURE, "Could not write length "
658 "field of USB data payload");
660 ret = write(p->wfd, data, datalen);
661 if (ret != datalen) {
662 err(EXIT_FAILURE, "Could not write "
663 "complete USB data payload");
668 read_file(struct usbcap *p)
674 while ((ret = read(p->rfd, &datalen, sizeof(int))) == sizeof(int)) {
675 datalen = le32toh(datalen);
676 data = malloc(datalen);
678 errx(EX_SOFTWARE, "Out of memory.");
679 ret = read(p->rfd, data, datalen);
680 if (ret != datalen) {
681 err(EXIT_FAILURE, "Could not read complete "
685 fix_packets(data, datalen);
687 print_packets(data, datalen);
693 do_loop(struct usbcap *p)
697 while (doexit == 0) {
698 cc = read(p->fd, (uint8_t *)p->buffer, p->bufsize);
704 fprintf(stderr, "read: %s\n", strerror(errno));
712 fix_packets(p->buffer, cc);
715 write_packets(p, p->buffer, cc);
716 print_packets(p->buffer, cc);
721 init_rfile(struct usbcap *p)
723 struct usbcap_filehdr uf;
726 p->rfd = open(r_arg, O_RDONLY);
728 err(EXIT_FAILURE, "Could not open "
729 "'%s' for read", r_arg);
731 ret = read(p->rfd, &uf, sizeof(uf));
732 if (ret != sizeof(uf)) {
733 err(EXIT_FAILURE, "Could not read USB capture "
736 if (le32toh(uf.magic) != USBCAP_FILEHDR_MAGIC) {
737 errx(EX_SOFTWARE, "Invalid magic field(0x%08x) "
738 "in USB capture file header.",
739 (unsigned int)le32toh(uf.magic));
742 errx(EX_SOFTWARE, "Invalid major version(%d) "
743 "field in USB capture file header.", (int)uf.major);
748 if (uf.minor != 3 && uf.minor != 2) {
749 errx(EX_SOFTWARE, "Invalid minor version(%d) "
750 "field in USB capture file header.", (int)uf.minor);
755 init_wfile(struct usbcap *p)
757 struct usbcap_filehdr uf;
760 p->wfd = open(w_arg, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
762 err(EXIT_FAILURE, "Could not open "
763 "'%s' for write", w_arg);
765 memset(&uf, 0, sizeof(uf));
766 uf.magic = htole32(USBCAP_FILEHDR_MAGIC);
769 ret = write(p->wfd, (const void *)&uf, sizeof(uf));
770 if (ret != sizeof(uf)) {
771 err(EXIT_FAILURE, "Could not write "
772 "USB capture header");
780 #define FMT " %-14s %s\n"
781 fprintf(stderr, "usage: usbdump [options]\n");
782 fprintf(stderr, FMT, "-i <usbusX>", "Listen on USB bus interface");
783 fprintf(stderr, FMT, "-f <unit[.endpoint]>", "Specify a device and endpoint filter");
784 fprintf(stderr, FMT, "-r <file>", "Read the raw packets from file");
785 fprintf(stderr, FMT, "-s <snaplen>", "Snapshot bytes from each packet");
786 fprintf(stderr, FMT, "-v", "Increase the verbose level");
787 fprintf(stderr, FMT, "-b <file>", "Save raw version of all recorded data to file");
788 fprintf(stderr, FMT, "-w <file>", "Write the raw packets to file");
789 fprintf(stderr, FMT, "-h", "Display summary of command line options");
795 check_usb_pf_sysctl(void)
799 size_t no_pf_len = sizeof(int);
801 /* check "hw.usb.no_pf" sysctl for 8- and 9- stable */
803 error = sysctlbyname("hw.usb.no_pf", &no_pf_val,
804 &no_pf_len, NULL, 0);
805 if (error == 0 && no_pf_val != 0) {
806 warnx("The USB packet filter might be disabled.");
807 warnx("See the \"hw.usb.no_pf\" sysctl for more information.");
812 main(int argc, char *argv[])
815 struct bpf_program total_prog;
817 struct bpf_version bv;
818 struct usbcap *p = &uc;
828 const char *optstring;
831 optstring = "b:hi:r:s:vw:f:";
832 while ((o = getopt(argc, argv, optstring)) != -1) {
842 snapshot = strtol(optarg, &pp, 10);
844 if (pp != NULL && *pp != 0)
846 if (snapshot == 0 && errno == EINVAL)
848 /* snapeshot == 0 is special */
863 filt_unit = strtol(optarg, &pp, 10);
867 filt_ep = strtol(pp + 1, &pp, 10);
868 if (pp != NULL && *pp != 0)
870 } else if (*pp != 0) {
874 add_filter(filt_unit, filt_ep);
883 p->bfd = open(b_arg, O_CREAT | O_TRUNC |
884 O_WRONLY, S_IRUSR | S_IWUSR);
886 err(EXIT_FAILURE, "Could not open "
887 "'%s' for write", b_arg);
892 * Require more verbosity to print anything when -w or -b is
893 * specified on the command line:
895 if (w_arg != NULL || b_arg != NULL)
903 check_usb_pf_sysctl();
905 p->fd = fd = open("/dev/bpf", O_RDONLY);
907 err(EXIT_FAILURE, "Could not open BPF device");
909 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
910 err(EXIT_FAILURE, "BIOCVERSION ioctl failed");
912 if (bv.bv_major != BPF_MAJOR_VERSION ||
913 bv.bv_minor < BPF_MINOR_VERSION)
914 errx(EXIT_FAILURE, "Kernel BPF filter out of date");
916 /* USB transfers can be greater than 64KByte */
919 /* clear ifr structure */
920 memset(&ifr, 0, sizeof(ifr));
922 /* Try to create usbusN interface if it is not available. */
923 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
925 errx(EXIT_FAILURE, "Could not open a socket");
926 ifindex = if_nametoindex(i_arg);
928 (void)strlcpy(ifr.ifr_name, i_arg, sizeof(ifr.ifr_name));
929 if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
930 errx(EXIT_FAILURE, "Invalid bus interface: %s", i_arg);
933 for ( ; v >= USBPF_HDR_LEN; v >>= 1) {
934 (void)ioctl(fd, BIOCSBLEN, (caddr_t)&v);
935 (void)strlcpy(ifr.ifr_name, i_arg, sizeof(ifr.ifr_name));
936 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
940 errx(EXIT_FAILURE, "No buffer size worked.");
942 if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0)
943 err(EXIT_FAILURE, "BIOCGBLEN ioctl failed");
946 p->buffer = (uint8_t *)malloc(p->bufsize);
947 if (p->buffer == NULL)
948 errx(EX_SOFTWARE, "Out of memory.");
950 make_filter(&total_prog, snapshot);
952 if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0)
953 err(EXIT_FAILURE, "BIOCSETF ioctl failed");
955 free_filter(&total_prog);
957 /* 1 second read timeout */
960 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&tv) < 0)
961 err(EXIT_FAILURE, "BIOCSRTIMEOUT ioctl failed");
963 (void)signal(SIGINT, handle_sigint);
967 if (ioctl(fd, BIOCGSTATS, (caddr_t)&us) < 0)
968 err(EXIT_FAILURE, "BIOCGSTATS ioctl failed");
970 /* XXX what's difference between pkt_captured and us.us_recv? */
972 printf("%d packets captured\n", pkt_captured);
973 printf("%d packets received by filter\n", us.bs_recv);
974 printf("%d packets dropped by kernel\n", us.bs_drop);
977 * Destroy the usbusN interface only if it was created by
978 * usbdump(8). Ignore when it was already destroyed.
980 if (ifindex == 0 && if_nametoindex(i_arg) > 0) {
981 (void)strlcpy(ifr.ifr_name, i_arg, sizeof(ifr.ifr_name));
982 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
983 warn("SIOCIFDESTROY ioctl failed");
996 return (EXIT_SUCCESS);