1215651Sweongyo/*- 2215651Sweongyo * Copyright (c) 2010 Weongyo Jeong <weongyo@freebsd.org> 3215651Sweongyo * All rights reserved. 4215651Sweongyo * 5215651Sweongyo * Redistribution and use in source and binary forms, with or without 6215651Sweongyo * modification, are permitted provided that the following conditions 7215651Sweongyo * are met: 8215651Sweongyo * 1. Redistributions of source code must retain the above copyright 9215651Sweongyo * notice, this list of conditions and the following disclaimer, 10215651Sweongyo * without modification. 11215651Sweongyo * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12215651Sweongyo * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13215651Sweongyo * redistribution must be conditioned upon including a substantially 14215651Sweongyo * similar Disclaimer requirement for further binary redistribution. 15215651Sweongyo * 16215651Sweongyo * NO WARRANTY 17215651Sweongyo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18215651Sweongyo * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19215651Sweongyo * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20215651Sweongyo * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21215651Sweongyo * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22215651Sweongyo * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23215651Sweongyo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24215651Sweongyo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25215651Sweongyo * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26215651Sweongyo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27215651Sweongyo * THE POSSIBILITY OF SUCH DAMAGES. 28215651Sweongyo * 29215651Sweongyo * $FreeBSD$ 30215651Sweongyo */ 31215651Sweongyo 32215651Sweongyo#include <sys/param.h> 33215651Sweongyo#include <sys/endian.h> 34215651Sweongyo#include <sys/ioctl.h> 35215803Sweongyo#include <sys/socket.h> 36215651Sweongyo#include <sys/stat.h> 37215651Sweongyo#include <sys/utsname.h> 38231835Shselasky#include <sys/queue.h> 39215803Sweongyo#include <net/if.h> 40215803Sweongyo#include <net/bpf.h> 41215651Sweongyo#include <dev/usb/usb.h> 42215651Sweongyo#include <dev/usb/usb_pf.h> 43215651Sweongyo#include <dev/usb/usbdi.h> 44215651Sweongyo#include <errno.h> 45215651Sweongyo#include <fcntl.h> 46215651Sweongyo#include <limits.h> 47215651Sweongyo#include <stdio.h> 48215651Sweongyo#include <stdlib.h> 49231835Shselasky#include <stdint.h> 50215651Sweongyo#include <string.h> 51215651Sweongyo#include <time.h> 52215651Sweongyo#include <unistd.h> 53221604Shselasky#include <sysexits.h> 54221604Shselasky#include <err.h> 55215651Sweongyo 56231835Shselasky#define BPF_STORE_JUMP(x,_c,_k,_jt,_jf) do { \ 57231835Shselasky (x).code = (_c); \ 58231835Shselasky (x).k = (_k); \ 59231835Shselasky (x).jt = (_jt); \ 60231835Shselasky (x).jf = (_jf); \ 61231835Shselasky} while (0) 62231835Shselasky 63231835Shselasky#define BPF_STORE_STMT(x,_c,_k) do { \ 64231835Shselasky (x).code = (_c); \ 65231835Shselasky (x).k = (_k); \ 66231835Shselasky (x).jt = 0; \ 67231835Shselasky (x).jf = 0; \ 68231835Shselasky} while (0) 69231835Shselasky 70231835Shselaskystruct usb_filt { 71231835Shselasky STAILQ_ENTRY(usb_filt) entry; 72231835Shselasky int unit; 73231835Shselasky int endpoint; 74231835Shselasky}; 75231835Shselasky 76215651Sweongyostruct usbcap { 77215651Sweongyo int fd; /* fd for /dev/usbpf */ 78220301Shselasky uint32_t bufsize; 79220301Shselasky uint8_t *buffer; 80215651Sweongyo 81215651Sweongyo /* for -w option */ 82215651Sweongyo int wfd; 83215651Sweongyo /* for -r option */ 84215651Sweongyo int rfd; 85234636Shselasky /* for -b option */ 86234636Shselasky int bfd; 87215651Sweongyo}; 88215651Sweongyo 89215651Sweongyostruct usbcap_filehdr { 90220301Shselasky uint32_t magic; 91215651Sweongyo#define USBCAP_FILEHDR_MAGIC 0x9a90000e 92220301Shselasky uint8_t major; 93220301Shselasky uint8_t minor; 94220301Shselasky uint8_t reserved[26]; 95215651Sweongyo} __packed; 96215651Sweongyo 97233037Shselasky#define HEADER_ALIGN(x,a) (((x) + (a) - 1) & ~((a) - 1)) 98233037Shselasky 99233037Shselaskystruct header_32 { 100233039Shselasky /* capture timestamp */ 101233037Shselasky uint32_t ts_sec; 102233037Shselasky uint32_t ts_usec; 103233039Shselasky /* data length and alignment information */ 104233037Shselasky uint32_t caplen; 105233037Shselasky uint32_t datalen; 106233039Shselasky uint8_t hdrlen; 107233039Shselasky uint8_t align; 108233037Shselasky} __packed; 109233037Shselasky 110215651Sweongyostatic int doexit = 0; 111215651Sweongyostatic int pkt_captured = 0; 112215651Sweongyostatic int verbose = 0; 113233039Shselaskystatic int uf_minor; 114218010Shselaskystatic const char *i_arg = "usbus0"; 115215651Sweongyostatic const char *r_arg = NULL; 116215651Sweongyostatic const char *w_arg = NULL; 117234636Shselaskystatic const char *b_arg = NULL; 118234636Shselaskystatic struct usbcap uc; 119215651Sweongyostatic const char *errstr_table[USB_ERR_MAX] = { 120220301Shselasky [USB_ERR_NORMAL_COMPLETION] = "0", 121215651Sweongyo [USB_ERR_PENDING_REQUESTS] = "PENDING_REQUESTS", 122215651Sweongyo [USB_ERR_NOT_STARTED] = "NOT_STARTED", 123215651Sweongyo [USB_ERR_INVAL] = "INVAL", 124215651Sweongyo [USB_ERR_NOMEM] = "NOMEM", 125215651Sweongyo [USB_ERR_CANCELLED] = "CANCELLED", 126215651Sweongyo [USB_ERR_BAD_ADDRESS] = "BAD_ADDRESS", 127215651Sweongyo [USB_ERR_BAD_BUFSIZE] = "BAD_BUFSIZE", 128215651Sweongyo [USB_ERR_BAD_FLAG] = "BAD_FLAG", 129215651Sweongyo [USB_ERR_NO_CALLBACK] = "NO_CALLBACK", 130215651Sweongyo [USB_ERR_IN_USE] = "IN_USE", 131215651Sweongyo [USB_ERR_NO_ADDR] = "NO_ADDR", 132215651Sweongyo [USB_ERR_NO_PIPE] = "NO_PIPE", 133215651Sweongyo [USB_ERR_ZERO_NFRAMES] = "ZERO_NFRAMES", 134215651Sweongyo [USB_ERR_ZERO_MAXP] = "ZERO_MAXP", 135215651Sweongyo [USB_ERR_SET_ADDR_FAILED] = "SET_ADDR_FAILED", 136215651Sweongyo [USB_ERR_NO_POWER] = "NO_POWER", 137215651Sweongyo [USB_ERR_TOO_DEEP] = "TOO_DEEP", 138215651Sweongyo [USB_ERR_IOERROR] = "IOERROR", 139215651Sweongyo [USB_ERR_NOT_CONFIGURED] = "NOT_CONFIGURED", 140215651Sweongyo [USB_ERR_TIMEOUT] = "TIMEOUT", 141215651Sweongyo [USB_ERR_SHORT_XFER] = "SHORT_XFER", 142215651Sweongyo [USB_ERR_STALLED] = "STALLED", 143215651Sweongyo [USB_ERR_INTERRUPTED] = "INTERRUPTED", 144215651Sweongyo [USB_ERR_DMA_LOAD_FAILED] = "DMA_LOAD_FAILED", 145215651Sweongyo [USB_ERR_BAD_CONTEXT] = "BAD_CONTEXT", 146215651Sweongyo [USB_ERR_NO_ROOT_HUB] = "NO_ROOT_HUB", 147215651Sweongyo [USB_ERR_NO_INTR_THREAD] = "NO_INTR_THREAD", 148215651Sweongyo [USB_ERR_NOT_LOCKED] = "NOT_LOCKED", 149215651Sweongyo}; 150215651Sweongyo 151220301Shselaskystatic const char *xfertype_table[4] = { 152215651Sweongyo [UE_CONTROL] = "CTRL", 153215651Sweongyo [UE_ISOCHRONOUS] = "ISOC", 154215651Sweongyo [UE_BULK] = "BULK", 155215651Sweongyo [UE_INTERRUPT] = "INTR" 156215651Sweongyo}; 157215651Sweongyo 158220301Shselaskystatic const char *speed_table[USB_SPEED_MAX] = { 159220301Shselasky [USB_SPEED_FULL] = "FULL", 160220301Shselasky [USB_SPEED_HIGH] = "HIGH", 161220301Shselasky [USB_SPEED_LOW] = "LOW", 162220301Shselasky [USB_SPEED_VARIABLE] = "VARI", 163220301Shselasky [USB_SPEED_SUPER] = "SUPER", 164220301Shselasky}; 165220301Shselasky 166231835Shselaskystatic STAILQ_HEAD(,usb_filt) usb_filt_head = 167231835Shselasky STAILQ_HEAD_INITIALIZER(usb_filt_head); 168231835Shselasky 169215651Sweongyostatic void 170231835Shselaskyadd_filter(int usb_filt_unit, int usb_filt_ep) 171231835Shselasky{ 172231835Shselasky struct usb_filt *puf; 173231835Shselasky 174231835Shselasky puf = malloc(sizeof(struct usb_filt)); 175231835Shselasky if (puf == NULL) 176231835Shselasky errx(EX_SOFTWARE, "Out of memory."); 177231835Shselasky 178231835Shselasky puf->unit = usb_filt_unit; 179231835Shselasky puf->endpoint = usb_filt_ep; 180231835Shselasky 181231835Shselasky STAILQ_INSERT_TAIL(&usb_filt_head, puf, entry); 182231835Shselasky} 183231835Shselasky 184231835Shselaskystatic void 185231835Shselaskymake_filter(struct bpf_program *pprog, int snapshot) 186231835Shselasky{ 187231835Shselasky struct usb_filt *puf; 188231835Shselasky struct bpf_insn *dynamic_insn; 189231835Shselasky int len; 190231835Shselasky 191231835Shselasky len = 0; 192231835Shselasky 193231835Shselasky STAILQ_FOREACH(puf, &usb_filt_head, entry) 194231835Shselasky len++; 195231835Shselasky 196231835Shselasky dynamic_insn = malloc(((len * 5) + 1) * sizeof(struct bpf_insn)); 197231835Shselasky 198231835Shselasky if (dynamic_insn == NULL) 199231835Shselasky errx(EX_SOFTWARE, "Out of memory."); 200231835Shselasky 201231835Shselasky len++; 202231835Shselasky 203231835Shselasky if (len == 1) { 204231835Shselasky /* accept all packets */ 205231835Shselasky 206231835Shselasky BPF_STORE_STMT(dynamic_insn[0], BPF_RET | BPF_K, snapshot); 207231835Shselasky 208231835Shselasky goto done; 209231835Shselasky } 210231835Shselasky 211231835Shselasky len = 0; 212231835Shselasky 213231835Shselasky STAILQ_FOREACH(puf, &usb_filt_head, entry) { 214231835Shselasky const int addr_off = (uintptr_t)&((struct usbpf_pkthdr *)0)->up_address; 215231835Shselasky const int addr_ep = (uintptr_t)&((struct usbpf_pkthdr *)0)->up_endpoint; 216231835Shselasky 217231835Shselasky if (puf->unit != -1) { 218231835Shselasky if (puf->endpoint != -1) { 219231835Shselasky BPF_STORE_STMT(dynamic_insn[len], 220231835Shselasky BPF_LD | BPF_B | BPF_ABS, addr_off); 221231835Shselasky len++; 222231835Shselasky BPF_STORE_JUMP(dynamic_insn[len], 223231835Shselasky BPF_JMP | BPF_JEQ | BPF_K, (uint8_t)puf->unit, 0, 3); 224231835Shselasky len++; 225231835Shselasky BPF_STORE_STMT(dynamic_insn[len], 226231835Shselasky BPF_LD | BPF_W | BPF_ABS, addr_ep); 227231835Shselasky len++; 228231835Shselasky BPF_STORE_JUMP(dynamic_insn[len], 229231835Shselasky BPF_JMP | BPF_JEQ | BPF_K, htobe32(puf->endpoint), 0, 1); 230231835Shselasky len++; 231231835Shselasky } else { 232231835Shselasky BPF_STORE_STMT(dynamic_insn[len], 233231835Shselasky BPF_LD | BPF_B | BPF_ABS, addr_off); 234231835Shselasky len++; 235231835Shselasky BPF_STORE_JUMP(dynamic_insn[len], 236231835Shselasky BPF_JMP | BPF_JEQ | BPF_K, (uint8_t)puf->unit, 0, 1); 237231835Shselasky len++; 238231835Shselasky } 239231835Shselasky } else { 240231835Shselasky if (puf->endpoint != -1) { 241231835Shselasky BPF_STORE_STMT(dynamic_insn[len], 242231835Shselasky BPF_LD | BPF_W | BPF_ABS, addr_ep); 243231835Shselasky len++; 244231835Shselasky BPF_STORE_JUMP(dynamic_insn[len], 245231835Shselasky BPF_JMP | BPF_JEQ | BPF_K, htobe32(puf->endpoint), 0, 1); 246231835Shselasky len++; 247231835Shselasky } 248231835Shselasky } 249231835Shselasky BPF_STORE_STMT(dynamic_insn[len], 250231835Shselasky BPF_RET | BPF_K, snapshot); 251231835Shselasky len++; 252231835Shselasky } 253231835Shselasky 254231835Shselasky BPF_STORE_STMT(dynamic_insn[len], BPF_RET | BPF_K, 0); 255231835Shselasky len++; 256231835Shselasky 257231835Shselaskydone: 258231835Shselasky pprog->bf_len = len; 259231835Shselasky pprog->bf_insns = dynamic_insn; 260231835Shselasky} 261231835Shselasky 262234636Shselaskystatic int 263234636Shselaskymatch_filter(int unit, int endpoint) 264234636Shselasky{ 265234636Shselasky struct usb_filt *puf; 266234636Shselasky 267234636Shselasky if (STAILQ_FIRST(&usb_filt_head) == NULL) 268234636Shselasky return (1); 269234636Shselasky 270234636Shselasky STAILQ_FOREACH(puf, &usb_filt_head, entry) { 271234636Shselasky if ((puf->unit == -1 || puf->unit == unit) && 272234636Shselasky (puf->endpoint == -1 || puf->endpoint == endpoint)) 273234636Shselasky return (1); 274234636Shselasky } 275234636Shselasky return (0); 276234636Shselasky} 277234636Shselasky 278231835Shselaskystatic void 279231835Shselaskyfree_filter(struct bpf_program *pprog) 280231835Shselasky{ 281231835Shselasky struct usb_filt *puf; 282231835Shselasky 283231835Shselasky while ((puf = STAILQ_FIRST(&usb_filt_head)) != NULL) { 284231835Shselasky STAILQ_REMOVE_HEAD(&usb_filt_head, entry); 285231835Shselasky free(puf); 286231835Shselasky } 287231835Shselasky free(pprog->bf_insns); 288231835Shselasky} 289231835Shselasky 290231835Shselaskystatic void 291215651Sweongyohandle_sigint(int sig) 292215651Sweongyo{ 293215651Sweongyo 294215651Sweongyo (void)sig; 295215651Sweongyo doexit = 1; 296215651Sweongyo} 297215651Sweongyo 298220301Shselasky#define FLAGS(x, name) \ 299220301Shselasky (((x) & USBPF_FLAG_##name) ? #name "|" : "") 300220301Shselasky 301220301Shselasky#define STATUS(x, name) \ 302220301Shselasky (((x) & USBPF_STATUS_##name) ? #name "|" : "") 303220301Shselasky 304220301Shselaskystatic const char * 305220301Shselaskyusb_errstr(uint32_t error) 306220301Shselasky{ 307220301Shselasky if (error >= USB_ERR_MAX || errstr_table[error] == NULL) 308220301Shselasky return ("UNKNOWN"); 309220301Shselasky else 310220301Shselasky return (errstr_table[error]); 311220301Shselasky} 312220301Shselasky 313220301Shselaskystatic const char * 314220301Shselaskyusb_speedstr(uint8_t speed) 315220301Shselasky{ 316220301Shselasky if (speed >= USB_SPEED_MAX || speed_table[speed] == NULL) 317220301Shselasky return ("UNKNOWN"); 318220301Shselasky else 319220301Shselasky return (speed_table[speed]); 320220301Shselasky} 321220301Shselasky 322215651Sweongyostatic void 323220301Shselaskyprint_flags(uint32_t flags) 324215651Sweongyo{ 325220301Shselasky printf(" flags %#x <%s%s%s%s%s%s%s%s%s0>\n", 326220301Shselasky flags, 327220301Shselasky FLAGS(flags, FORCE_SHORT_XFER), 328220301Shselasky FLAGS(flags, SHORT_XFER_OK), 329220301Shselasky FLAGS(flags, SHORT_FRAMES_OK), 330220301Shselasky FLAGS(flags, PIPE_BOF), 331220301Shselasky FLAGS(flags, PROXY_BUFFER), 332220301Shselasky FLAGS(flags, EXT_BUFFER), 333220301Shselasky FLAGS(flags, MANUAL_STATUS), 334220301Shselasky FLAGS(flags, NO_PIPE_OK), 335220301Shselasky FLAGS(flags, STALL_PIPE)); 336215651Sweongyo} 337215651Sweongyo 338215651Sweongyostatic void 339220301Shselaskyprint_status(uint32_t status) 340215651Sweongyo{ 341220301Shselasky printf(" status %#x <%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s0>\n", 342220301Shselasky status, 343220301Shselasky STATUS(status, OPEN), 344220301Shselasky STATUS(status, TRANSFERRING), 345220301Shselasky STATUS(status, DID_DMA_DELAY), 346220301Shselasky STATUS(status, DID_CLOSE), 347220301Shselasky STATUS(status, DRAINING), 348220301Shselasky STATUS(status, STARTED), 349220301Shselasky STATUS(status, BW_RECLAIMED), 350220301Shselasky STATUS(status, CONTROL_XFR), 351220301Shselasky STATUS(status, CONTROL_HDR), 352220301Shselasky STATUS(status, CONTROL_ACT), 353220301Shselasky STATUS(status, CONTROL_STALL), 354220301Shselasky STATUS(status, SHORT_FRAMES_OK), 355220301Shselasky STATUS(status, SHORT_XFER_OK), 356220301Shselasky STATUS(status, BDMA_ENABLE), 357220301Shselasky STATUS(status, BDMA_NO_POST_SYNC), 358220301Shselasky STATUS(status, BDMA_SETUP), 359220301Shselasky STATUS(status, ISOCHRONOUS_XFR), 360220301Shselasky STATUS(status, CURR_DMA_SET), 361220301Shselasky STATUS(status, CAN_CANCEL_IMMED), 362220301Shselasky STATUS(status, DOING_CALLBACK)); 363220301Shselasky} 364215651Sweongyo 365220301Shselasky/* 366220301Shselasky * Dump a byte into hex format. 367220301Shselasky */ 368220301Shselaskystatic void 369220301Shselaskyhexbyte(char *buf, uint8_t temp) 370220301Shselasky{ 371220301Shselasky uint8_t lo; 372220301Shselasky uint8_t hi; 373220301Shselasky 374220301Shselasky lo = temp & 0xF; 375220301Shselasky hi = temp >> 4; 376220301Shselasky 377220301Shselasky if (hi < 10) 378220301Shselasky buf[0] = '0' + hi; 379220301Shselasky else 380220301Shselasky buf[0] = 'A' + hi - 10; 381220301Shselasky 382220301Shselasky if (lo < 10) 383220301Shselasky buf[1] = '0' + lo; 384220301Shselasky else 385220301Shselasky buf[1] = 'A' + lo - 10; 386215651Sweongyo} 387215651Sweongyo 388215651Sweongyo/* 389215651Sweongyo * Display a region in traditional hexdump format. 390215651Sweongyo */ 391215651Sweongyostatic void 392220301Shselaskyhexdump(const uint8_t *region, uint32_t len) 393215651Sweongyo{ 394220301Shselasky const uint8_t *line; 395220301Shselasky char linebuf[128]; 396220301Shselasky int i; 397218010Shselasky int x; 398218010Shselasky int c; 399215651Sweongyo 400215651Sweongyo for (line = region; line < (region + len); line += 16) { 401220301Shselasky 402220301Shselasky i = 0; 403220301Shselasky 404220301Shselasky linebuf[i] = ' '; 405220301Shselasky hexbyte(linebuf + i + 1, ((line - region) >> 8) & 0xFF); 406220301Shselasky hexbyte(linebuf + i + 3, (line - region) & 0xFF); 407220301Shselasky linebuf[i + 5] = ' '; 408220301Shselasky linebuf[i + 6] = ' '; 409220301Shselasky i += 7; 410220301Shselasky 411215651Sweongyo for (x = 0; x < 16; x++) { 412220301Shselasky if ((line + x) < (region + len)) { 413220301Shselasky hexbyte(linebuf + i, 414220301Shselasky *(const u_int8_t *)(line + x)); 415220301Shselasky } else { 416220301Shselasky linebuf[i] = '-'; 417220301Shselasky linebuf[i + 1] = '-'; 418220301Shselasky } 419220301Shselasky linebuf[i + 2] = ' '; 420220301Shselasky if (x == 7) { 421220301Shselasky linebuf[i + 3] = ' '; 422220301Shselasky i += 4; 423220301Shselasky } else { 424220301Shselasky i += 3; 425220301Shselasky } 426215651Sweongyo } 427220301Shselasky linebuf[i] = ' '; 428220301Shselasky linebuf[i + 1] = '|'; 429220301Shselasky i += 2; 430215651Sweongyo for (x = 0; x < 16; x++) { 431215651Sweongyo if ((line + x) < (region + len)) { 432215651Sweongyo c = *(const u_int8_t *)(line + x); 433215651Sweongyo /* !isprint(c) */ 434215651Sweongyo if ((c < ' ') || (c > '~')) 435215651Sweongyo c = '.'; 436220301Shselasky linebuf[i] = c; 437220301Shselasky } else { 438220301Shselasky linebuf[i] = ' '; 439220301Shselasky } 440220301Shselasky i++; 441215651Sweongyo } 442220301Shselasky linebuf[i] = '|'; 443220301Shselasky linebuf[i + 1] = 0; 444220301Shselasky i += 2; 445220301Shselasky puts(linebuf); 446215651Sweongyo } 447215651Sweongyo} 448215651Sweongyo 449215651Sweongyostatic void 450233037Shselaskyprint_apacket(const struct header_32 *hdr, const uint8_t *ptr, int ptr_len) 451215651Sweongyo{ 452215651Sweongyo struct tm *tm; 453220301Shselasky struct usbpf_pkthdr up_temp; 454220301Shselasky struct usbpf_pkthdr *up; 455215651Sweongyo struct timeval tv; 456215651Sweongyo size_t len; 457220301Shselasky uint32_t x; 458215651Sweongyo char buf[64]; 459215651Sweongyo 460220301Shselasky ptr += USBPF_HDR_LEN; 461220301Shselasky ptr_len -= USBPF_HDR_LEN; 462220301Shselasky if (ptr_len < 0) 463220301Shselasky return; 464220301Shselasky 465220301Shselasky /* make sure we don't change the source buffer */ 466220301Shselasky memcpy(&up_temp, ptr - USBPF_HDR_LEN, sizeof(up_temp)); 467220301Shselasky up = &up_temp; 468220301Shselasky 469220301Shselasky /* 470220301Shselasky * A packet from the kernel is based on little endian byte 471220301Shselasky * order. 472220301Shselasky */ 473220301Shselasky up->up_totlen = le32toh(up->up_totlen); 474215651Sweongyo up->up_busunit = le32toh(up->up_busunit); 475220301Shselasky up->up_address = le32toh(up->up_address); 476215651Sweongyo up->up_flags = le32toh(up->up_flags); 477215651Sweongyo up->up_status = le32toh(up->up_status); 478215651Sweongyo up->up_error = le32toh(up->up_error); 479215651Sweongyo up->up_interval = le32toh(up->up_interval); 480220301Shselasky up->up_frames = le32toh(up->up_frames); 481220301Shselasky up->up_packet_size = le32toh(up->up_packet_size); 482220301Shselasky up->up_packet_count = le32toh(up->up_packet_count); 483220301Shselasky up->up_endpoint = le32toh(up->up_endpoint); 484215651Sweongyo 485234636Shselasky if (!match_filter(up->up_address, up->up_endpoint)) 486234636Shselasky return; 487234636Shselasky 488233037Shselasky tv.tv_sec = hdr->ts_sec; 489233037Shselasky tv.tv_usec = hdr->ts_usec; 490215651Sweongyo tm = localtime(&tv.tv_sec); 491215651Sweongyo 492215651Sweongyo len = strftime(buf, sizeof(buf), "%H:%M:%S", tm); 493220301Shselasky 494234636Shselasky if (verbose >= 0) { 495234636Shselasky printf("%.*s.%06ld usbus%d.%d %s-%s-EP=%08x,SPD=%s,NFR=%d,SLEN=%d,IVAL=%d%s%s\n", 496234636Shselasky (int)len, buf, tv.tv_usec, 497234636Shselasky (int)up->up_busunit, (int)up->up_address, 498234636Shselasky (up->up_type == USBPF_XFERTAP_SUBMIT) ? "SUBM" : "DONE", 499234636Shselasky xfertype_table[up->up_xfertype], 500234636Shselasky (unsigned int)up->up_endpoint, 501234636Shselasky usb_speedstr(up->up_speed), 502234636Shselasky (int)up->up_frames, 503234636Shselasky (int)(up->up_totlen - USBPF_HDR_LEN - 504234636Shselasky (USBPF_FRAME_HDR_LEN * up->up_frames)), 505234636Shselasky (int)up->up_interval, 506234636Shselasky (up->up_type == USBPF_XFERTAP_DONE) ? ",ERR=" : "", 507234636Shselasky (up->up_type == USBPF_XFERTAP_DONE) ? 508234636Shselasky usb_errstr(up->up_error) : ""); 509234636Shselasky } 510215651Sweongyo 511234636Shselasky if (verbose >= 1 || b_arg != NULL) { 512220301Shselasky for (x = 0; x != up->up_frames; x++) { 513220301Shselasky const struct usbpf_framehdr *uf; 514220301Shselasky uint32_t framelen; 515220301Shselasky uint32_t flags; 516220301Shselasky 517220301Shselasky uf = (const struct usbpf_framehdr *)ptr; 518220301Shselasky ptr += USBPF_FRAME_HDR_LEN; 519220301Shselasky ptr_len -= USBPF_FRAME_HDR_LEN; 520220301Shselasky if (ptr_len < 0) 521220301Shselasky return; 522220301Shselasky 523220301Shselasky framelen = le32toh(uf->length); 524220301Shselasky flags = le32toh(uf->flags); 525220301Shselasky 526234636Shselasky if (verbose >= 1) { 527234636Shselasky printf(" frame[%u] %s %d bytes\n", 528234636Shselasky (unsigned int)x, 529234636Shselasky (flags & USBPF_FRAMEFLAG_READ) ? "READ" : "WRITE", 530234636Shselasky (int)framelen); 531234636Shselasky } 532220301Shselasky 533220301Shselasky if (flags & USBPF_FRAMEFLAG_DATA_FOLLOWS) { 534220301Shselasky 535220301Shselasky int tot_frame_len; 536220301Shselasky 537220301Shselasky tot_frame_len = USBPF_FRAME_ALIGN(framelen); 538220301Shselasky 539220301Shselasky ptr_len -= tot_frame_len; 540220301Shselasky 541220301Shselasky if (tot_frame_len < 0 || 542220301Shselasky (int)framelen < 0 || (int)ptr_len < 0) 543220301Shselasky break; 544220301Shselasky 545234636Shselasky if (b_arg != NULL) { 546234636Shselasky struct usbcap *p = &uc; 547234636Shselasky int ret; 548234636Shselasky ret = write(p->bfd, ptr, framelen); 549234636Shselasky if (ret != (int)framelen) 550234636Shselasky err(EXIT_FAILURE, "Could not write binary data"); 551234636Shselasky } 552234636Shselasky if (verbose >= 1) 553234636Shselasky hexdump(ptr, framelen); 554220301Shselasky 555220301Shselasky ptr += tot_frame_len; 556220301Shselasky } 557215651Sweongyo } 558215651Sweongyo } 559220301Shselasky if (verbose >= 2) 560215651Sweongyo print_flags(up->up_flags); 561220301Shselasky if (verbose >= 3) 562215651Sweongyo print_status(up->up_status); 563215651Sweongyo} 564215651Sweongyo 565215651Sweongyostatic void 566233039Shselaskyfix_packets(uint8_t *data, const int datalen) 567215651Sweongyo{ 568233037Shselasky struct header_32 temp; 569220301Shselasky uint8_t *ptr; 570220301Shselasky uint8_t *next; 571233039Shselasky uint32_t hdrlen; 572233039Shselasky uint32_t caplen; 573215651Sweongyo 574215651Sweongyo for (ptr = data; ptr < (data + datalen); ptr = next) { 575215651Sweongyo 576233039Shselasky const struct bpf_hdr *hdr; 577233037Shselasky 578233039Shselasky hdr = (const struct bpf_hdr *)ptr; 579233037Shselasky 580233039Shselasky temp.ts_sec = htole32(hdr->bh_tstamp.tv_sec); 581233039Shselasky temp.ts_usec = htole32(hdr->bh_tstamp.tv_usec); 582233039Shselasky temp.caplen = htole32(hdr->bh_caplen); 583233039Shselasky temp.datalen = htole32(hdr->bh_datalen); 584233039Shselasky temp.hdrlen = hdr->bh_hdrlen; 585233039Shselasky temp.align = BPF_WORDALIGN(1); 586233037Shselasky 587233039Shselasky hdrlen = hdr->bh_hdrlen; 588233039Shselasky caplen = hdr->bh_caplen; 589233037Shselasky 590233039Shselasky if ((hdrlen >= sizeof(temp)) && (hdrlen <= 255) && 591233039Shselasky ((ptr + hdrlen) <= (data + datalen))) { 592233039Shselasky memcpy(ptr, &temp, sizeof(temp)); 593233039Shselasky memset(ptr + sizeof(temp), 0, hdrlen - sizeof(temp)); 594233039Shselasky } else { 595233039Shselasky err(EXIT_FAILURE, "Invalid header length %d", hdrlen); 596233039Shselasky } 597233037Shselasky 598233039Shselasky next = ptr + BPF_WORDALIGN(hdrlen + caplen); 599233037Shselasky 600233039Shselasky if (next <= ptr) 601233039Shselasky err(EXIT_FAILURE, "Invalid length"); 602233039Shselasky } 603233039Shselasky} 604233037Shselasky 605233039Shselaskystatic void 606233039Shselaskyprint_packets(uint8_t *data, const int datalen) 607233039Shselasky{ 608233039Shselasky struct header_32 temp; 609233039Shselasky uint8_t *ptr; 610233039Shselasky uint8_t *next; 611233037Shselasky 612233039Shselasky for (ptr = data; ptr < (data + datalen); ptr = next) { 613233037Shselasky 614233039Shselasky const struct header_32 *hdr32; 615233037Shselasky 616233039Shselasky hdr32 = (const struct header_32 *)ptr; 617233039Shselasky 618233039Shselasky temp.ts_sec = le32toh(hdr32->ts_sec); 619233039Shselasky temp.ts_usec = le32toh(hdr32->ts_usec); 620233039Shselasky temp.caplen = le32toh(hdr32->caplen); 621233039Shselasky temp.datalen = le32toh(hdr32->datalen); 622233039Shselasky temp.hdrlen = hdr32->hdrlen; 623233039Shselasky temp.align = hdr32->align; 624233039Shselasky 625233039Shselasky next = ptr + HEADER_ALIGN(temp.hdrlen + temp.caplen, temp.align); 626233039Shselasky 627233037Shselasky if (next <= ptr) 628233039Shselasky err(EXIT_FAILURE, "Invalid length"); 629233037Shselasky 630234636Shselasky if (verbose >= 0 || r_arg != NULL || b_arg != NULL) { 631233037Shselasky print_apacket(&temp, ptr + 632233037Shselasky temp.hdrlen, temp.caplen); 633220301Shselasky } 634215651Sweongyo pkt_captured++; 635215651Sweongyo } 636215651Sweongyo} 637215651Sweongyo 638215651Sweongyostatic void 639220301Shselaskywrite_packets(struct usbcap *p, const uint8_t *data, const int datalen) 640215651Sweongyo{ 641220301Shselasky int len = htole32(datalen); 642220301Shselasky int ret; 643215651Sweongyo 644215651Sweongyo ret = write(p->wfd, &len, sizeof(int)); 645221604Shselasky if (ret != sizeof(int)) { 646221604Shselasky err(EXIT_FAILURE, "Could not write length " 647221604Shselasky "field of USB data payload"); 648221604Shselasky } 649215651Sweongyo ret = write(p->wfd, data, datalen); 650221604Shselasky if (ret != datalen) { 651221604Shselasky err(EXIT_FAILURE, "Could not write " 652221604Shselasky "complete USB data payload"); 653221604Shselasky } 654215651Sweongyo} 655215651Sweongyo 656215651Sweongyostatic void 657215651Sweongyoread_file(struct usbcap *p) 658215651Sweongyo{ 659220301Shselasky int datalen; 660220301Shselasky int ret; 661220301Shselasky uint8_t *data; 662215651Sweongyo 663215651Sweongyo while ((ret = read(p->rfd, &datalen, sizeof(int))) == sizeof(int)) { 664215651Sweongyo datalen = le32toh(datalen); 665215651Sweongyo data = malloc(datalen); 666221604Shselasky if (data == NULL) 667221604Shselasky errx(EX_SOFTWARE, "Out of memory."); 668215651Sweongyo ret = read(p->rfd, data, datalen); 669221604Shselasky if (ret != datalen) { 670221604Shselasky err(EXIT_FAILURE, "Could not read complete " 671221604Shselasky "USB data payload"); 672221604Shselasky } 673233039Shselasky if (uf_minor == 2) 674233039Shselasky fix_packets(data, datalen); 675233039Shselasky 676215651Sweongyo print_packets(data, datalen); 677215651Sweongyo free(data); 678215651Sweongyo } 679215651Sweongyo} 680215651Sweongyo 681215651Sweongyostatic void 682215651Sweongyodo_loop(struct usbcap *p) 683215651Sweongyo{ 684215651Sweongyo int cc; 685215651Sweongyo 686215651Sweongyo while (doexit == 0) { 687220301Shselasky cc = read(p->fd, (uint8_t *)p->buffer, p->bufsize); 688215651Sweongyo if (cc < 0) { 689215651Sweongyo switch (errno) { 690215651Sweongyo case EINTR: 691215651Sweongyo break; 692215651Sweongyo default: 693215651Sweongyo fprintf(stderr, "read: %s\n", strerror(errno)); 694215651Sweongyo return; 695215651Sweongyo } 696215651Sweongyo continue; 697215651Sweongyo } 698215651Sweongyo if (cc == 0) 699215651Sweongyo continue; 700233039Shselasky 701233039Shselasky fix_packets(p->buffer, cc); 702233039Shselasky 703215651Sweongyo if (w_arg != NULL) 704215651Sweongyo write_packets(p, p->buffer, cc); 705215651Sweongyo print_packets(p->buffer, cc); 706215651Sweongyo } 707215651Sweongyo} 708215651Sweongyo 709215651Sweongyostatic void 710215651Sweongyoinit_rfile(struct usbcap *p) 711215651Sweongyo{ 712215651Sweongyo struct usbcap_filehdr uf; 713215651Sweongyo int ret; 714215651Sweongyo 715215651Sweongyo p->rfd = open(r_arg, O_RDONLY); 716215651Sweongyo if (p->rfd < 0) { 717221604Shselasky err(EXIT_FAILURE, "Could not open " 718221604Shselasky "'%s' for read", r_arg); 719215651Sweongyo } 720215651Sweongyo ret = read(p->rfd, &uf, sizeof(uf)); 721221604Shselasky if (ret != sizeof(uf)) { 722221604Shselasky err(EXIT_FAILURE, "Could not read USB capture " 723221604Shselasky "file header"); 724221604Shselasky } 725221604Shselasky if (le32toh(uf.magic) != USBCAP_FILEHDR_MAGIC) { 726221604Shselasky errx(EX_SOFTWARE, "Invalid magic field(0x%08x) " 727221604Shselasky "in USB capture file header.", 728221604Shselasky (unsigned int)le32toh(uf.magic)); 729221604Shselasky } 730221604Shselasky if (uf.major != 0) { 731221604Shselasky errx(EX_SOFTWARE, "Invalid major version(%d) " 732221604Shselasky "field in USB capture file header.", (int)uf.major); 733221604Shselasky } 734233039Shselasky 735233039Shselasky uf_minor = uf.minor; 736233039Shselasky 737233039Shselasky if (uf.minor != 3 && uf.minor != 2) { 738221604Shselasky errx(EX_SOFTWARE, "Invalid minor version(%d) " 739221604Shselasky "field in USB capture file header.", (int)uf.minor); 740221604Shselasky } 741215651Sweongyo} 742215651Sweongyo 743215651Sweongyostatic void 744215651Sweongyoinit_wfile(struct usbcap *p) 745215651Sweongyo{ 746215651Sweongyo struct usbcap_filehdr uf; 747215651Sweongyo int ret; 748215651Sweongyo 749215651Sweongyo p->wfd = open(w_arg, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); 750215651Sweongyo if (p->wfd < 0) { 751221604Shselasky err(EXIT_FAILURE, "Could not open " 752233039Shselasky "'%s' for write", w_arg); 753215651Sweongyo } 754221604Shselasky memset(&uf, 0, sizeof(uf)); 755215651Sweongyo uf.magic = htole32(USBCAP_FILEHDR_MAGIC); 756215651Sweongyo uf.major = 0; 757233039Shselasky uf.minor = 3; 758215651Sweongyo ret = write(p->wfd, (const void *)&uf, sizeof(uf)); 759221604Shselasky if (ret != sizeof(uf)) { 760221604Shselasky err(EXIT_FAILURE, "Could not write " 761221604Shselasky "USB capture header"); 762221604Shselasky } 763215651Sweongyo} 764215651Sweongyo 765215651Sweongyostatic void 766215651Sweongyousage(void) 767215651Sweongyo{ 768215651Sweongyo 769215651Sweongyo#define FMT " %-14s %s\n" 770215651Sweongyo fprintf(stderr, "usage: usbdump [options]\n"); 771221604Shselasky fprintf(stderr, FMT, "-i <usbusX>", "Listen on USB bus interface"); 772231835Shselasky fprintf(stderr, FMT, "-f <unit[.endpoint]>", "Specify a device and endpoint filter"); 773221604Shselasky fprintf(stderr, FMT, "-r <file>", "Read the raw packets from file"); 774221604Shselasky fprintf(stderr, FMT, "-s <snaplen>", "Snapshot bytes from each packet"); 775221604Shselasky fprintf(stderr, FMT, "-v", "Increase the verbose level"); 776234636Shselasky fprintf(stderr, FMT, "-b <file>", "Save raw version of all recorded data to file"); 777221604Shselasky fprintf(stderr, FMT, "-w <file>", "Write the raw packets to file"); 778234655Shselasky fprintf(stderr, FMT, "-h", "Display summary of command line options"); 779215651Sweongyo#undef FMT 780221604Shselasky exit(EX_USAGE); 781215651Sweongyo} 782215651Sweongyo 783215651Sweongyoint 784215651Sweongyomain(int argc, char *argv[]) 785215651Sweongyo{ 786215651Sweongyo struct timeval tv; 787215803Sweongyo struct bpf_program total_prog; 788215803Sweongyo struct bpf_stat us; 789215803Sweongyo struct bpf_version bv; 790234636Shselasky struct usbcap *p = &uc; 791215803Sweongyo struct ifreq ifr; 792215651Sweongyo long snapshot = 192; 793220301Shselasky uint32_t v; 794231835Shselasky int fd; 795231835Shselasky int o; 796231835Shselasky int filt_unit; 797231835Shselasky int filt_ep; 798238279Shrs int s; 799238279Shrs int ifindex; 800215651Sweongyo const char *optstring; 801231835Shselasky char *pp; 802215651Sweongyo 803234655Shselasky optstring = "b:hi:r:s:vw:f:"; 804215651Sweongyo while ((o = getopt(argc, argv, optstring)) != -1) { 805215651Sweongyo switch (o) { 806215651Sweongyo case 'i': 807215651Sweongyo i_arg = optarg; 808215651Sweongyo break; 809215651Sweongyo case 'r': 810215651Sweongyo r_arg = optarg; 811215651Sweongyo init_rfile(p); 812215651Sweongyo break; 813215651Sweongyo case 's': 814231835Shselasky snapshot = strtol(optarg, &pp, 10); 815215651Sweongyo errno = 0; 816231835Shselasky if (pp != NULL && *pp != 0) 817231835Shselasky usage(); 818215651Sweongyo if (snapshot == 0 && errno == EINVAL) 819215651Sweongyo usage(); 820215651Sweongyo /* snapeshot == 0 is special */ 821215651Sweongyo if (snapshot == 0) 822215651Sweongyo snapshot = -1; 823215651Sweongyo break; 824234636Shselasky case 'b': 825234636Shselasky b_arg = optarg; 826234636Shselasky break; 827215651Sweongyo case 'v': 828215651Sweongyo verbose++; 829215651Sweongyo break; 830215651Sweongyo case 'w': 831215651Sweongyo w_arg = optarg; 832215651Sweongyo init_wfile(p); 833215651Sweongyo break; 834231835Shselasky case 'f': 835231835Shselasky filt_unit = strtol(optarg, &pp, 10); 836231835Shselasky filt_ep = -1; 837231835Shselasky if (pp != NULL) { 838231835Shselasky if (*pp == '.') { 839231835Shselasky filt_ep = strtol(pp + 1, &pp, 10); 840231835Shselasky if (pp != NULL && *pp != 0) 841231835Shselasky usage(); 842231835Shselasky } else if (*pp != 0) { 843231835Shselasky usage(); 844231835Shselasky } 845231835Shselasky } 846231835Shselasky add_filter(filt_unit, filt_ep); 847231835Shselasky break; 848215651Sweongyo default: 849215651Sweongyo usage(); 850215651Sweongyo /* NOTREACHED */ 851215651Sweongyo } 852215651Sweongyo } 853215651Sweongyo 854234636Shselasky if (b_arg != NULL) { 855234636Shselasky p->bfd = open(b_arg, O_CREAT | O_TRUNC | 856234636Shselasky O_WRONLY, S_IRUSR | S_IWUSR); 857234636Shselasky if (p->bfd < 0) { 858234636Shselasky err(EXIT_FAILURE, "Could not open " 859234636Shselasky "'%s' for write", b_arg); 860234636Shselasky } 861234636Shselasky } 862234636Shselasky 863234636Shselasky /* 864234636Shselasky * Require more verbosity to print anything when -w or -b is 865234636Shselasky * specified on the command line: 866234636Shselasky */ 867234636Shselasky if (w_arg != NULL || b_arg != NULL) 868234636Shselasky verbose--; 869234636Shselasky 870215651Sweongyo if (r_arg != NULL) { 871215651Sweongyo read_file(p); 872215651Sweongyo exit(EXIT_SUCCESS); 873215651Sweongyo } 874215651Sweongyo 875215803Sweongyo p->fd = fd = open("/dev/bpf", O_RDONLY); 876221604Shselasky if (p->fd < 0) 877221604Shselasky err(EXIT_FAILURE, "Could not open BPF device"); 878215651Sweongyo 879221604Shselasky if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) 880221604Shselasky err(EXIT_FAILURE, "BIOCVERSION ioctl failed"); 881221604Shselasky 882215803Sweongyo if (bv.bv_major != BPF_MAJOR_VERSION || 883221604Shselasky bv.bv_minor < BPF_MINOR_VERSION) 884221604Shselasky errx(EXIT_FAILURE, "Kernel BPF filter out of date"); 885215651Sweongyo 886220301Shselasky /* USB transfers can be greater than 64KByte */ 887220301Shselasky v = 1U << 16; 888220301Shselasky 889220301Shselasky /* clear ifr structure */ 890220301Shselasky memset(&ifr, 0, sizeof(ifr)); 891220301Shselasky 892238279Shrs /* Try to create usbusN interface if it is not available. */ 893238279Shrs s = socket(AF_LOCAL, SOCK_DGRAM, 0); 894238279Shrs if (s < 0) 895238279Shrs errx(EXIT_FAILURE, "Could not open a socket"); 896238279Shrs ifindex = if_nametoindex(i_arg); 897238279Shrs if (ifindex == 0) { 898238279Shrs (void)strlcpy(ifr.ifr_name, i_arg, sizeof(ifr.ifr_name)); 899238279Shrs if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) 900238279Shrs errx(EXIT_FAILURE, "Invalid bus interface: %s", i_arg); 901238279Shrs } 902238279Shrs 903220301Shselasky for ( ; v >= USBPF_HDR_LEN; v >>= 1) { 904215803Sweongyo (void)ioctl(fd, BIOCSBLEN, (caddr_t)&v); 905238279Shrs (void)strlcpy(ifr.ifr_name, i_arg, sizeof(ifr.ifr_name)); 906215803Sweongyo if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0) 907215651Sweongyo break; 908215651Sweongyo } 909221604Shselasky if (v == 0) 910221604Shselasky errx(EXIT_FAILURE, "No buffer size worked."); 911215651Sweongyo 912221604Shselasky if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) 913221604Shselasky err(EXIT_FAILURE, "BIOCGBLEN ioctl failed"); 914215651Sweongyo 915215651Sweongyo p->bufsize = v; 916220301Shselasky p->buffer = (uint8_t *)malloc(p->bufsize); 917221604Shselasky if (p->buffer == NULL) 918221604Shselasky errx(EX_SOFTWARE, "Out of memory."); 919215651Sweongyo 920231835Shselasky make_filter(&total_prog, snapshot); 921215651Sweongyo 922221604Shselasky if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) 923221604Shselasky err(EXIT_FAILURE, "BIOCSETF ioctl failed"); 924215651Sweongyo 925231835Shselasky free_filter(&total_prog); 926231835Shselasky 927215651Sweongyo /* 1 second read timeout */ 928215651Sweongyo tv.tv_sec = 1; 929215651Sweongyo tv.tv_usec = 0; 930221604Shselasky if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&tv) < 0) 931221604Shselasky err(EXIT_FAILURE, "BIOCSRTIMEOUT ioctl failed"); 932215651Sweongyo 933215651Sweongyo (void)signal(SIGINT, handle_sigint); 934215651Sweongyo 935215651Sweongyo do_loop(p); 936215651Sweongyo 937221604Shselasky if (ioctl(fd, BIOCGSTATS, (caddr_t)&us) < 0) 938221604Shselasky err(EXIT_FAILURE, "BIOCGSTATS ioctl failed"); 939215651Sweongyo 940215651Sweongyo /* XXX what's difference between pkt_captured and us.us_recv? */ 941215651Sweongyo printf("\n"); 942215651Sweongyo printf("%d packets captured\n", pkt_captured); 943215803Sweongyo printf("%d packets received by filter\n", us.bs_recv); 944215803Sweongyo printf("%d packets dropped by kernel\n", us.bs_drop); 945215651Sweongyo 946238279Shrs /* 947238279Shrs * Destroy the usbusN interface only if it was created by 948238279Shrs * usbdump(8). Ignore when it was already destroyed. 949238279Shrs */ 950238279Shrs if (ifindex == 0 && if_nametoindex(i_arg) > 0) { 951238279Shrs (void)strlcpy(ifr.ifr_name, i_arg, sizeof(ifr.ifr_name)); 952238279Shrs if (ioctl(s, SIOCIFDESTROY, &ifr) < 0) 953238279Shrs warn("SIOCIFDESTROY ioctl failed"); 954238279Shrs } 955238279Shrs close(s); 956238279Shrs 957215651Sweongyo if (p->fd > 0) 958215651Sweongyo close(p->fd); 959215651Sweongyo if (p->rfd > 0) 960215651Sweongyo close(p->rfd); 961215651Sweongyo if (p->wfd > 0) 962215651Sweongyo close(p->wfd); 963234636Shselasky if (p->bfd > 0) 964234636Shselasky close(p->bfd); 965215651Sweongyo 966215651Sweongyo return (EXIT_SUCCESS); 967215651Sweongyo} 968