2 * Copyright (c) 2010 Weongyo Jeong <weongyo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
32 #include <sys/param.h>
33 #include <sys/endian.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 #include <sys/utsname.h>
39 #include <sys/queue.h>
42 #include <dev/usb/usb.h>
43 #include <dev/usb/usb_pf.h>
44 #include <dev/usb/usbdi.h>
57 #define BPF_STORE_JUMP(x,_c,_k,_jt,_jf) do { \
64 #define BPF_STORE_STMT(x,_c,_k) do { \
72 STAILQ_ENTRY(usb_filt) entry;
78 int fd; /* fd for /dev/usbpf */
90 struct usbcap_filehdr {
92 #define USBCAP_FILEHDR_MAGIC 0x9a90000e
98 #define HEADER_ALIGN(x,a) (((x) + (a) - 1) & ~((a) - 1))
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 static const char *xfertype_table[4] = {
153 [UE_CONTROL] = "CTRL",
154 [UE_ISOCHRONOUS] = "ISOC",
156 [UE_INTERRUPT] = "INTR"
159 static const char *speed_table[USB_SPEED_MAX] = {
160 [USB_SPEED_FULL] = "FULL",
161 [USB_SPEED_HIGH] = "HIGH",
162 [USB_SPEED_LOW] = "LOW",
163 [USB_SPEED_VARIABLE] = "VARI",
164 [USB_SPEED_SUPER] = "SUPER",
167 static STAILQ_HEAD(,usb_filt) usb_filt_head =
168 STAILQ_HEAD_INITIALIZER(usb_filt_head);
171 add_filter(int usb_filt_unit, int usb_filt_ep)
173 struct usb_filt *puf;
175 puf = malloc(sizeof(struct usb_filt));
177 errx(EX_SOFTWARE, "Out of memory.");
179 puf->unit = usb_filt_unit;
180 puf->endpoint = usb_filt_ep;
182 STAILQ_INSERT_TAIL(&usb_filt_head, puf, entry);
186 make_filter(struct bpf_program *pprog, int snapshot)
188 struct usb_filt *puf;
189 struct bpf_insn *dynamic_insn;
194 STAILQ_FOREACH(puf, &usb_filt_head, entry)
197 dynamic_insn = malloc(((len * 5) + 1) * sizeof(struct bpf_insn));
199 if (dynamic_insn == NULL)
200 errx(EX_SOFTWARE, "Out of memory.");
205 /* accept all packets */
207 BPF_STORE_STMT(dynamic_insn[0], BPF_RET | BPF_K, snapshot);
214 STAILQ_FOREACH(puf, &usb_filt_head, entry) {
215 const int addr_off = (uintptr_t)&((struct usbpf_pkthdr *)0)->up_address;
216 const int addr_ep = (uintptr_t)&((struct usbpf_pkthdr *)0)->up_endpoint;
218 if (puf->unit != -1) {
219 if (puf->endpoint != -1) {
220 BPF_STORE_STMT(dynamic_insn[len],
221 BPF_LD | BPF_B | BPF_ABS, addr_off);
223 BPF_STORE_JUMP(dynamic_insn[len],
224 BPF_JMP | BPF_JEQ | BPF_K, (uint8_t)puf->unit, 0, 3);
226 BPF_STORE_STMT(dynamic_insn[len],
227 BPF_LD | BPF_W | BPF_ABS, addr_ep);
229 BPF_STORE_JUMP(dynamic_insn[len],
230 BPF_JMP | BPF_JEQ | BPF_K, htobe32(puf->endpoint), 0, 1);
233 BPF_STORE_STMT(dynamic_insn[len],
234 BPF_LD | BPF_B | BPF_ABS, addr_off);
236 BPF_STORE_JUMP(dynamic_insn[len],
237 BPF_JMP | BPF_JEQ | BPF_K, (uint8_t)puf->unit, 0, 1);
241 if (puf->endpoint != -1) {
242 BPF_STORE_STMT(dynamic_insn[len],
243 BPF_LD | BPF_W | BPF_ABS, addr_ep);
245 BPF_STORE_JUMP(dynamic_insn[len],
246 BPF_JMP | BPF_JEQ | BPF_K, htobe32(puf->endpoint), 0, 1);
250 BPF_STORE_STMT(dynamic_insn[len],
251 BPF_RET | BPF_K, snapshot);
255 BPF_STORE_STMT(dynamic_insn[len], BPF_RET | BPF_K, 0);
260 pprog->bf_insns = dynamic_insn;
264 match_filter(int unit, int endpoint)
266 struct usb_filt *puf;
268 if (STAILQ_FIRST(&usb_filt_head) == NULL)
271 STAILQ_FOREACH(puf, &usb_filt_head, entry) {
272 if ((puf->unit == -1 || puf->unit == unit) &&
273 (puf->endpoint == -1 || puf->endpoint == endpoint))
280 free_filter(struct bpf_program *pprog)
282 struct usb_filt *puf;
284 while ((puf = STAILQ_FIRST(&usb_filt_head)) != NULL) {
285 STAILQ_REMOVE_HEAD(&usb_filt_head, entry);
288 free(pprog->bf_insns);
292 handle_sigint(int sig)
299 #define FLAGS(x, name) \
300 (((x) & USBPF_FLAG_##name) ? #name "|" : "")
302 #define STATUS(x, name) \
303 (((x) & USBPF_STATUS_##name) ? #name "|" : "")
306 usb_errstr(uint32_t error)
308 if (error >= USB_ERR_MAX || errstr_table[error] == NULL)
311 return (errstr_table[error]);
315 usb_speedstr(uint8_t speed)
317 if (speed >= USB_SPEED_MAX || speed_table[speed] == NULL)
320 return (speed_table[speed]);
324 print_flags(uint32_t flags)
326 printf(" flags %#x <%s%s%s%s%s%s%s%s%s0>\n",
328 FLAGS(flags, FORCE_SHORT_XFER),
329 FLAGS(flags, SHORT_XFER_OK),
330 FLAGS(flags, SHORT_FRAMES_OK),
331 FLAGS(flags, PIPE_BOF),
332 FLAGS(flags, PROXY_BUFFER),
333 FLAGS(flags, EXT_BUFFER),
334 FLAGS(flags, MANUAL_STATUS),
335 FLAGS(flags, NO_PIPE_OK),
336 FLAGS(flags, STALL_PIPE));
340 print_status(uint32_t status)
342 printf(" status %#x <%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s0>\n",
344 STATUS(status, OPEN),
345 STATUS(status, TRANSFERRING),
346 STATUS(status, DID_DMA_DELAY),
347 STATUS(status, DID_CLOSE),
348 STATUS(status, DRAINING),
349 STATUS(status, STARTED),
350 STATUS(status, BW_RECLAIMED),
351 STATUS(status, CONTROL_XFR),
352 STATUS(status, CONTROL_HDR),
353 STATUS(status, CONTROL_ACT),
354 STATUS(status, CONTROL_STALL),
355 STATUS(status, SHORT_FRAMES_OK),
356 STATUS(status, SHORT_XFER_OK),
357 STATUS(status, BDMA_ENABLE),
358 STATUS(status, BDMA_NO_POST_SYNC),
359 STATUS(status, BDMA_SETUP),
360 STATUS(status, ISOCHRONOUS_XFR),
361 STATUS(status, CURR_DMA_SET),
362 STATUS(status, CAN_CANCEL_IMMED),
363 STATUS(status, DOING_CALLBACK));
367 * Dump a byte into hex format.
370 hexbyte(char *buf, uint8_t temp)
381 buf[0] = 'A' + hi - 10;
386 buf[1] = 'A' + lo - 10;
390 * Display a region in traditional hexdump format.
393 hexdump(const uint8_t *region, uint32_t len)
401 for (line = region; line < (region + len); line += 16) {
406 hexbyte(linebuf + i + 1, ((line - region) >> 8) & 0xFF);
407 hexbyte(linebuf + i + 3, (line - region) & 0xFF);
408 linebuf[i + 5] = ' ';
409 linebuf[i + 6] = ' ';
412 for (x = 0; x < 16; x++) {
413 if ((line + x) < (region + len)) {
415 *(const u_int8_t *)(line + x));
418 linebuf[i + 1] = '-';
420 linebuf[i + 2] = ' ';
422 linebuf[i + 3] = ' ';
429 linebuf[i + 1] = '|';
431 for (x = 0; x < 16; x++) {
432 if ((line + x) < (region + len)) {
433 c = *(const u_int8_t *)(line + x);
435 if ((c < ' ') || (c > '~'))
451 print_apacket(const struct header_32 *hdr, const uint8_t *ptr, int ptr_len)
454 struct usbpf_pkthdr up_temp;
455 struct usbpf_pkthdr *up;
461 ptr += USBPF_HDR_LEN;
462 ptr_len -= USBPF_HDR_LEN;
466 /* make sure we don't change the source buffer */
467 memcpy(&up_temp, ptr - USBPF_HDR_LEN, sizeof(up_temp));
471 * A packet from the kernel is based on little endian byte
474 up->up_totlen = le32toh(up->up_totlen);
475 up->up_busunit = le32toh(up->up_busunit);
476 up->up_flags = le32toh(up->up_flags);
477 up->up_status = le32toh(up->up_status);
478 up->up_error = le32toh(up->up_error);
479 up->up_interval = le32toh(up->up_interval);
480 up->up_frames = le32toh(up->up_frames);
481 up->up_packet_size = le32toh(up->up_packet_size);
482 up->up_packet_count = le32toh(up->up_packet_count);
483 up->up_endpoint = le32toh(up->up_endpoint);
485 if (!match_filter(up->up_address, up->up_endpoint))
488 tv.tv_sec = hdr->ts_sec;
489 tv.tv_usec = hdr->ts_usec;
490 tm = localtime(&tv.tv_sec);
492 len = strftime(buf, sizeof(buf), "%H:%M:%S", tm);
495 printf("%.*s.%06ld usbus%d.%d %s-%s-EP=%08x,SPD=%s,NFR=%d,SLEN=%d,IVAL=%d%s%s\n",
496 (int)len, buf, tv.tv_usec,
497 (int)up->up_busunit, (int)up->up_address,
498 (up->up_type == USBPF_XFERTAP_SUBMIT) ? "SUBM" : "DONE",
499 xfertype_table[up->up_xfertype],
500 (unsigned int)up->up_endpoint,
501 usb_speedstr(up->up_speed),
503 (int)(up->up_totlen - USBPF_HDR_LEN -
504 (USBPF_FRAME_HDR_LEN * up->up_frames)),
505 (int)up->up_interval,
506 (up->up_type == USBPF_XFERTAP_DONE) ? ",ERR=" : "",
507 (up->up_type == USBPF_XFERTAP_DONE) ?
508 usb_errstr(up->up_error) : "");
511 if (verbose >= 1 || b_arg != NULL) {
512 for (x = 0; x != up->up_frames; x++) {
513 const struct usbpf_framehdr *uf;
517 uf = (const struct usbpf_framehdr *)ptr;
518 ptr += USBPF_FRAME_HDR_LEN;
519 ptr_len -= USBPF_FRAME_HDR_LEN;
523 framelen = le32toh(uf->length);
524 flags = le32toh(uf->flags);
527 printf(" frame[%u] %s %d bytes\n",
529 (flags & USBPF_FRAMEFLAG_READ) ? "READ" : "WRITE",
533 if (flags & USBPF_FRAMEFLAG_DATA_FOLLOWS) {
537 tot_frame_len = USBPF_FRAME_ALIGN(framelen);
539 ptr_len -= tot_frame_len;
541 if (tot_frame_len < 0 ||
542 (int)framelen < 0 || (int)ptr_len < 0)
546 struct usbcap *p = &uc;
548 ret = write(p->bfd, ptr, framelen);
549 if (ret != (int)framelen)
550 err(EXIT_FAILURE, "Could not write binary data");
553 hexdump(ptr, framelen);
555 ptr += tot_frame_len;
560 print_flags(up->up_flags);
562 print_status(up->up_status);
566 fix_packets(uint8_t *data, const int datalen)
568 struct header_32 temp;
574 for (ptr = data; ptr < (data + datalen); ptr = next) {
576 const struct bpf_hdr *hdr;
578 hdr = (const struct bpf_hdr *)ptr;
580 temp.ts_sec = htole32(hdr->bh_tstamp.tv_sec);
581 temp.ts_usec = htole32(hdr->bh_tstamp.tv_usec);
582 temp.caplen = htole32(hdr->bh_caplen);
583 temp.datalen = htole32(hdr->bh_datalen);
584 temp.hdrlen = hdr->bh_hdrlen;
585 temp.align = BPF_WORDALIGN(1);
587 hdrlen = hdr->bh_hdrlen;
588 caplen = hdr->bh_caplen;
590 if ((hdrlen >= sizeof(temp)) && (hdrlen <= 255) &&
591 ((ptr + hdrlen) <= (data + datalen))) {
592 memcpy(ptr, &temp, sizeof(temp));
593 memset(ptr + sizeof(temp), 0, hdrlen - sizeof(temp));
595 err(EXIT_FAILURE, "Invalid header length %d", hdrlen);
598 next = ptr + BPF_WORDALIGN(hdrlen + caplen);
601 err(EXIT_FAILURE, "Invalid length");
606 print_packets(uint8_t *data, const int datalen)
608 struct header_32 temp;
612 for (ptr = data; ptr < (data + datalen); ptr = next) {
614 const struct header_32 *hdr32;
616 hdr32 = (const struct header_32 *)ptr;
618 temp.ts_sec = le32toh(hdr32->ts_sec);
619 temp.ts_usec = le32toh(hdr32->ts_usec);
620 temp.caplen = le32toh(hdr32->caplen);
621 temp.datalen = le32toh(hdr32->datalen);
622 temp.hdrlen = hdr32->hdrlen;
623 temp.align = hdr32->align;
625 next = ptr + HEADER_ALIGN(temp.hdrlen + temp.caplen, temp.align);
628 err(EXIT_FAILURE, "Invalid length");
630 if (verbose >= 0 || r_arg != NULL || b_arg != NULL) {
631 print_apacket(&temp, ptr +
632 temp.hdrlen, temp.caplen);
639 write_packets(struct usbcap *p, const uint8_t *data, const int datalen)
641 int len = htole32(datalen);
644 ret = write(p->wfd, &len, sizeof(int));
645 if (ret != sizeof(int)) {
646 err(EXIT_FAILURE, "Could not write length "
647 "field of USB data payload");
649 ret = write(p->wfd, data, datalen);
650 if (ret != datalen) {
651 err(EXIT_FAILURE, "Could not write "
652 "complete USB data payload");
657 read_file(struct usbcap *p)
663 while ((ret = read(p->rfd, &datalen, sizeof(int))) == sizeof(int)) {
664 datalen = le32toh(datalen);
665 data = malloc(datalen);
667 errx(EX_SOFTWARE, "Out of memory.");
668 ret = read(p->rfd, data, datalen);
669 if (ret != datalen) {
670 err(EXIT_FAILURE, "Could not read complete "
674 fix_packets(data, datalen);
676 print_packets(data, datalen);
682 do_loop(struct usbcap *p)
686 while (doexit == 0) {
687 cc = read(p->fd, (uint8_t *)p->buffer, p->bufsize);
693 fprintf(stderr, "read: %s\n", strerror(errno));
701 fix_packets(p->buffer, cc);
704 write_packets(p, p->buffer, cc);
705 print_packets(p->buffer, cc);
710 init_rfile(struct usbcap *p)
712 struct usbcap_filehdr uf;
715 p->rfd = open(r_arg, O_RDONLY);
717 err(EXIT_FAILURE, "Could not open "
718 "'%s' for read", r_arg);
720 ret = read(p->rfd, &uf, sizeof(uf));
721 if (ret != sizeof(uf)) {
722 err(EXIT_FAILURE, "Could not read USB capture "
725 if (le32toh(uf.magic) != USBCAP_FILEHDR_MAGIC) {
726 errx(EX_SOFTWARE, "Invalid magic field(0x%08x) "
727 "in USB capture file header.",
728 (unsigned int)le32toh(uf.magic));
731 errx(EX_SOFTWARE, "Invalid major version(%d) "
732 "field in USB capture file header.", (int)uf.major);
737 if (uf.minor != 3 && uf.minor != 2) {
738 errx(EX_SOFTWARE, "Invalid minor version(%d) "
739 "field in USB capture file header.", (int)uf.minor);
744 init_wfile(struct usbcap *p)
746 struct usbcap_filehdr uf;
749 p->wfd = open(w_arg, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
751 err(EXIT_FAILURE, "Could not open "
752 "'%s' for write", w_arg);
754 memset(&uf, 0, sizeof(uf));
755 uf.magic = htole32(USBCAP_FILEHDR_MAGIC);
758 ret = write(p->wfd, (const void *)&uf, sizeof(uf));
759 if (ret != sizeof(uf)) {
760 err(EXIT_FAILURE, "Could not write "
761 "USB capture header");
769 #define FMT " %-14s %s\n"
770 fprintf(stderr, "usage: usbdump [options]\n");
771 fprintf(stderr, FMT, "-i <usbusX>", "Listen on USB bus interface");
772 fprintf(stderr, FMT, "-f <unit[.endpoint]>", "Specify a device and endpoint filter");
773 fprintf(stderr, FMT, "-r <file>", "Read the raw packets from file");
774 fprintf(stderr, FMT, "-s <snaplen>", "Snapshot bytes from each packet");
775 fprintf(stderr, FMT, "-v", "Increase the verbose level");
776 fprintf(stderr, FMT, "-b <file>", "Save raw version of all recorded data to file");
777 fprintf(stderr, FMT, "-w <file>", "Write the raw packets to file");
778 fprintf(stderr, FMT, "-h", "Display summary of command line options");
784 check_usb_pf_sysctl(void)
788 size_t no_pf_len = sizeof(int);
790 /* check "hw.usb.no_pf" sysctl for 8- and 9- stable */
792 error = sysctlbyname("hw.usb.no_pf", &no_pf_val,
793 &no_pf_len, NULL, 0);
794 if (error == 0 && no_pf_val != 0) {
795 warnx("The USB packet filter might be disabled.");
796 warnx("See the \"hw.usb.no_pf\" sysctl for more information.");
801 main(int argc, char *argv[])
804 struct bpf_program total_prog;
806 struct bpf_version bv;
807 struct usbcap *p = &uc;
815 const char *optstring;
818 optstring = "b:hi:r:s:vw:f:";
819 while ((o = getopt(argc, argv, optstring)) != -1) {
829 snapshot = strtol(optarg, &pp, 10);
831 if (pp != NULL && *pp != 0)
833 if (snapshot == 0 && errno == EINVAL)
835 /* snapeshot == 0 is special */
850 filt_unit = strtol(optarg, &pp, 10);
854 filt_ep = strtol(pp + 1, &pp, 10);
855 if (pp != NULL && *pp != 0)
857 } else if (*pp != 0) {
861 add_filter(filt_unit, filt_ep);
870 p->bfd = open(b_arg, O_CREAT | O_TRUNC |
871 O_WRONLY, S_IRUSR | S_IWUSR);
873 err(EXIT_FAILURE, "Could not open "
874 "'%s' for write", b_arg);
879 * Require more verbosity to print anything when -w or -b is
880 * specified on the command line:
882 if (w_arg != NULL || b_arg != NULL)
890 check_usb_pf_sysctl();
892 p->fd = fd = open("/dev/bpf", O_RDONLY);
894 err(EXIT_FAILURE, "Could not open BPF device");
896 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
897 err(EXIT_FAILURE, "BIOCVERSION ioctl failed");
899 if (bv.bv_major != BPF_MAJOR_VERSION ||
900 bv.bv_minor < BPF_MINOR_VERSION)
901 errx(EXIT_FAILURE, "Kernel BPF filter out of date");
903 /* USB transfers can be greater than 64KByte */
906 /* clear ifr structure */
907 memset(&ifr, 0, sizeof(ifr));
909 for ( ; v >= USBPF_HDR_LEN; v >>= 1) {
910 (void)ioctl(fd, BIOCSBLEN, (caddr_t)&v);
911 (void)strncpy(ifr.ifr_name, i_arg, sizeof(ifr.ifr_name));
912 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
916 errx(EXIT_FAILURE, "No buffer size worked.");
918 if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0)
919 err(EXIT_FAILURE, "BIOCGBLEN ioctl failed");
922 p->buffer = (uint8_t *)malloc(p->bufsize);
923 if (p->buffer == NULL)
924 errx(EX_SOFTWARE, "Out of memory.");
926 make_filter(&total_prog, snapshot);
928 if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0)
929 err(EXIT_FAILURE, "BIOCSETF ioctl failed");
931 free_filter(&total_prog);
933 /* 1 second read timeout */
936 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&tv) < 0)
937 err(EXIT_FAILURE, "BIOCSRTIMEOUT ioctl failed");
939 (void)signal(SIGINT, handle_sigint);
943 if (ioctl(fd, BIOCGSTATS, (caddr_t)&us) < 0)
944 err(EXIT_FAILURE, "BIOCGSTATS ioctl failed");
946 /* XXX what's difference between pkt_captured and us.us_recv? */
948 printf("%d packets captured\n", pkt_captured);
949 printf("%d packets received by filter\n", us.bs_recv);
950 printf("%d packets dropped by kernel\n", us.bs_drop);
961 return (EXIT_SUCCESS);