2010-11-22 01:28:29 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2010 Weongyo Jeong <weongyo@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer,
|
|
|
|
* without modification.
|
|
|
|
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
|
|
|
|
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
|
|
|
|
* redistribution must be conditioned upon including a substantially
|
|
|
|
* similar Disclaimer requirement for further binary redistribution.
|
|
|
|
*
|
|
|
|
* NO WARRANTY
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
|
|
|
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
|
|
|
|
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
|
|
|
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGES.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/ioctl.h>
|
2010-11-24 19:15:26 +00:00
|
|
|
#include <sys/socket.h>
|
2010-11-22 01:28:29 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/utsname.h>
|
2012-02-16 21:18:36 +00:00
|
|
|
#include <sys/queue.h>
|
2010-11-24 19:15:26 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/bpf.h>
|
2010-11-22 01:28:29 +00:00
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usb_pf.h>
|
|
|
|
#include <dev/usb/usbdi.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-02-16 21:18:36 +00:00
|
|
|
#include <stdint.h>
|
2010-11-22 01:28:29 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2011-05-07 16:28:23 +00:00
|
|
|
#include <sysexits.h>
|
|
|
|
#include <err.h>
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2012-02-16 21:18:36 +00:00
|
|
|
#define BPF_STORE_JUMP(x,_c,_k,_jt,_jf) do { \
|
|
|
|
(x).code = (_c); \
|
|
|
|
(x).k = (_k); \
|
|
|
|
(x).jt = (_jt); \
|
|
|
|
(x).jf = (_jf); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define BPF_STORE_STMT(x,_c,_k) do { \
|
|
|
|
(x).code = (_c); \
|
|
|
|
(x).k = (_k); \
|
|
|
|
(x).jt = 0; \
|
|
|
|
(x).jf = 0; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
struct usb_filt {
|
|
|
|
STAILQ_ENTRY(usb_filt) entry;
|
|
|
|
int unit;
|
|
|
|
int endpoint;
|
|
|
|
};
|
|
|
|
|
2010-11-22 01:28:29 +00:00
|
|
|
struct usbcap {
|
|
|
|
int fd; /* fd for /dev/usbpf */
|
2011-04-03 20:03:45 +00:00
|
|
|
uint32_t bufsize;
|
|
|
|
uint8_t *buffer;
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
/* for -w option */
|
|
|
|
int wfd;
|
|
|
|
/* for -r option */
|
|
|
|
int rfd;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct usbcap_filehdr {
|
2011-04-03 20:03:45 +00:00
|
|
|
uint32_t magic;
|
2010-11-22 01:28:29 +00:00
|
|
|
#define USBCAP_FILEHDR_MAGIC 0x9a90000e
|
2011-04-03 20:03:45 +00:00
|
|
|
uint8_t major;
|
|
|
|
uint8_t minor;
|
|
|
|
uint8_t reserved[26];
|
2010-11-22 01:28:29 +00:00
|
|
|
} __packed;
|
|
|
|
|
2012-03-16 16:29:21 +00:00
|
|
|
#define HEADER_ALIGN(x,a) (((x) + (a) - 1) & ~((a) - 1))
|
|
|
|
|
|
|
|
struct header_32 {
|
2012-03-16 17:30:22 +00:00
|
|
|
/* capture timestamp */
|
2012-03-16 16:29:21 +00:00
|
|
|
uint32_t ts_sec;
|
|
|
|
uint32_t ts_usec;
|
2012-03-16 17:30:22 +00:00
|
|
|
/* data length and alignment information */
|
2012-03-16 16:29:21 +00:00
|
|
|
uint32_t caplen;
|
|
|
|
uint32_t datalen;
|
2012-03-16 17:30:22 +00:00
|
|
|
uint8_t hdrlen;
|
|
|
|
uint8_t align;
|
2012-03-16 16:29:21 +00:00
|
|
|
} __packed;
|
|
|
|
|
2010-11-22 01:28:29 +00:00
|
|
|
static int doexit = 0;
|
|
|
|
static int pkt_captured = 0;
|
|
|
|
static int verbose = 0;
|
2012-03-16 17:30:22 +00:00
|
|
|
static int uf_minor;
|
2011-01-28 08:00:57 +00:00
|
|
|
static const char *i_arg = "usbus0";
|
2010-11-22 01:28:29 +00:00
|
|
|
static const char *r_arg = NULL;
|
|
|
|
static const char *w_arg = NULL;
|
|
|
|
static const char *errstr_table[USB_ERR_MAX] = {
|
2011-04-03 20:03:45 +00:00
|
|
|
[USB_ERR_NORMAL_COMPLETION] = "0",
|
2010-11-22 01:28:29 +00:00
|
|
|
[USB_ERR_PENDING_REQUESTS] = "PENDING_REQUESTS",
|
|
|
|
[USB_ERR_NOT_STARTED] = "NOT_STARTED",
|
|
|
|
[USB_ERR_INVAL] = "INVAL",
|
|
|
|
[USB_ERR_NOMEM] = "NOMEM",
|
|
|
|
[USB_ERR_CANCELLED] = "CANCELLED",
|
|
|
|
[USB_ERR_BAD_ADDRESS] = "BAD_ADDRESS",
|
|
|
|
[USB_ERR_BAD_BUFSIZE] = "BAD_BUFSIZE",
|
|
|
|
[USB_ERR_BAD_FLAG] = "BAD_FLAG",
|
|
|
|
[USB_ERR_NO_CALLBACK] = "NO_CALLBACK",
|
|
|
|
[USB_ERR_IN_USE] = "IN_USE",
|
|
|
|
[USB_ERR_NO_ADDR] = "NO_ADDR",
|
|
|
|
[USB_ERR_NO_PIPE] = "NO_PIPE",
|
|
|
|
[USB_ERR_ZERO_NFRAMES] = "ZERO_NFRAMES",
|
|
|
|
[USB_ERR_ZERO_MAXP] = "ZERO_MAXP",
|
|
|
|
[USB_ERR_SET_ADDR_FAILED] = "SET_ADDR_FAILED",
|
|
|
|
[USB_ERR_NO_POWER] = "NO_POWER",
|
|
|
|
[USB_ERR_TOO_DEEP] = "TOO_DEEP",
|
|
|
|
[USB_ERR_IOERROR] = "IOERROR",
|
|
|
|
[USB_ERR_NOT_CONFIGURED] = "NOT_CONFIGURED",
|
|
|
|
[USB_ERR_TIMEOUT] = "TIMEOUT",
|
|
|
|
[USB_ERR_SHORT_XFER] = "SHORT_XFER",
|
|
|
|
[USB_ERR_STALLED] = "STALLED",
|
|
|
|
[USB_ERR_INTERRUPTED] = "INTERRUPTED",
|
|
|
|
[USB_ERR_DMA_LOAD_FAILED] = "DMA_LOAD_FAILED",
|
|
|
|
[USB_ERR_BAD_CONTEXT] = "BAD_CONTEXT",
|
|
|
|
[USB_ERR_NO_ROOT_HUB] = "NO_ROOT_HUB",
|
|
|
|
[USB_ERR_NO_INTR_THREAD] = "NO_INTR_THREAD",
|
|
|
|
[USB_ERR_NOT_LOCKED] = "NOT_LOCKED",
|
|
|
|
};
|
|
|
|
|
2011-04-03 20:03:45 +00:00
|
|
|
static const char *xfertype_table[4] = {
|
2010-11-22 01:28:29 +00:00
|
|
|
[UE_CONTROL] = "CTRL",
|
|
|
|
[UE_ISOCHRONOUS] = "ISOC",
|
|
|
|
[UE_BULK] = "BULK",
|
|
|
|
[UE_INTERRUPT] = "INTR"
|
|
|
|
};
|
|
|
|
|
2011-04-03 20:03:45 +00:00
|
|
|
static const char *speed_table[USB_SPEED_MAX] = {
|
|
|
|
[USB_SPEED_FULL] = "FULL",
|
|
|
|
[USB_SPEED_HIGH] = "HIGH",
|
|
|
|
[USB_SPEED_LOW] = "LOW",
|
|
|
|
[USB_SPEED_VARIABLE] = "VARI",
|
|
|
|
[USB_SPEED_SUPER] = "SUPER",
|
|
|
|
};
|
|
|
|
|
2012-02-16 21:18:36 +00:00
|
|
|
static STAILQ_HEAD(,usb_filt) usb_filt_head =
|
|
|
|
STAILQ_HEAD_INITIALIZER(usb_filt_head);
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_filter(int usb_filt_unit, int usb_filt_ep)
|
|
|
|
{
|
|
|
|
struct usb_filt *puf;
|
|
|
|
|
|
|
|
puf = malloc(sizeof(struct usb_filt));
|
|
|
|
if (puf == NULL)
|
|
|
|
errx(EX_SOFTWARE, "Out of memory.");
|
|
|
|
|
|
|
|
puf->unit = usb_filt_unit;
|
|
|
|
puf->endpoint = usb_filt_ep;
|
|
|
|
|
|
|
|
STAILQ_INSERT_TAIL(&usb_filt_head, puf, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
make_filter(struct bpf_program *pprog, int snapshot)
|
|
|
|
{
|
|
|
|
struct usb_filt *puf;
|
|
|
|
struct bpf_insn *dynamic_insn;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
STAILQ_FOREACH(puf, &usb_filt_head, entry)
|
|
|
|
len++;
|
|
|
|
|
|
|
|
dynamic_insn = malloc(((len * 5) + 1) * sizeof(struct bpf_insn));
|
|
|
|
|
|
|
|
if (dynamic_insn == NULL)
|
|
|
|
errx(EX_SOFTWARE, "Out of memory.");
|
|
|
|
|
|
|
|
len++;
|
|
|
|
|
|
|
|
if (len == 1) {
|
|
|
|
/* accept all packets */
|
|
|
|
|
|
|
|
BPF_STORE_STMT(dynamic_insn[0], BPF_RET | BPF_K, snapshot);
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
STAILQ_FOREACH(puf, &usb_filt_head, entry) {
|
|
|
|
const int addr_off = (uintptr_t)&((struct usbpf_pkthdr *)0)->up_address;
|
|
|
|
const int addr_ep = (uintptr_t)&((struct usbpf_pkthdr *)0)->up_endpoint;
|
|
|
|
|
|
|
|
if (puf->unit != -1) {
|
|
|
|
if (puf->endpoint != -1) {
|
|
|
|
BPF_STORE_STMT(dynamic_insn[len],
|
|
|
|
BPF_LD | BPF_B | BPF_ABS, addr_off);
|
|
|
|
len++;
|
|
|
|
BPF_STORE_JUMP(dynamic_insn[len],
|
|
|
|
BPF_JMP | BPF_JEQ | BPF_K, (uint8_t)puf->unit, 0, 3);
|
|
|
|
len++;
|
|
|
|
BPF_STORE_STMT(dynamic_insn[len],
|
|
|
|
BPF_LD | BPF_W | BPF_ABS, addr_ep);
|
|
|
|
len++;
|
|
|
|
BPF_STORE_JUMP(dynamic_insn[len],
|
|
|
|
BPF_JMP | BPF_JEQ | BPF_K, htobe32(puf->endpoint), 0, 1);
|
|
|
|
len++;
|
|
|
|
} else {
|
|
|
|
BPF_STORE_STMT(dynamic_insn[len],
|
|
|
|
BPF_LD | BPF_B | BPF_ABS, addr_off);
|
|
|
|
len++;
|
|
|
|
BPF_STORE_JUMP(dynamic_insn[len],
|
|
|
|
BPF_JMP | BPF_JEQ | BPF_K, (uint8_t)puf->unit, 0, 1);
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (puf->endpoint != -1) {
|
|
|
|
BPF_STORE_STMT(dynamic_insn[len],
|
|
|
|
BPF_LD | BPF_W | BPF_ABS, addr_ep);
|
|
|
|
len++;
|
|
|
|
BPF_STORE_JUMP(dynamic_insn[len],
|
|
|
|
BPF_JMP | BPF_JEQ | BPF_K, htobe32(puf->endpoint), 0, 1);
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BPF_STORE_STMT(dynamic_insn[len],
|
|
|
|
BPF_RET | BPF_K, snapshot);
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
BPF_STORE_STMT(dynamic_insn[len], BPF_RET | BPF_K, 0);
|
|
|
|
len++;
|
|
|
|
|
|
|
|
done:
|
|
|
|
pprog->bf_len = len;
|
|
|
|
pprog->bf_insns = dynamic_insn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_filter(struct bpf_program *pprog)
|
|
|
|
{
|
|
|
|
struct usb_filt *puf;
|
|
|
|
|
|
|
|
while ((puf = STAILQ_FIRST(&usb_filt_head)) != NULL) {
|
|
|
|
STAILQ_REMOVE_HEAD(&usb_filt_head, entry);
|
|
|
|
free(puf);
|
|
|
|
}
|
|
|
|
free(pprog->bf_insns);
|
|
|
|
}
|
|
|
|
|
2010-11-22 01:28:29 +00:00
|
|
|
static void
|
|
|
|
handle_sigint(int sig)
|
|
|
|
{
|
|
|
|
|
|
|
|
(void)sig;
|
|
|
|
doexit = 1;
|
|
|
|
}
|
|
|
|
|
2011-04-03 20:03:45 +00:00
|
|
|
#define FLAGS(x, name) \
|
|
|
|
(((x) & USBPF_FLAG_##name) ? #name "|" : "")
|
|
|
|
|
|
|
|
#define STATUS(x, name) \
|
|
|
|
(((x) & USBPF_STATUS_##name) ? #name "|" : "")
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
usb_errstr(uint32_t error)
|
|
|
|
{
|
|
|
|
if (error >= USB_ERR_MAX || errstr_table[error] == NULL)
|
|
|
|
return ("UNKNOWN");
|
|
|
|
else
|
|
|
|
return (errstr_table[error]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
usb_speedstr(uint8_t speed)
|
|
|
|
{
|
|
|
|
if (speed >= USB_SPEED_MAX || speed_table[speed] == NULL)
|
|
|
|
return ("UNKNOWN");
|
|
|
|
else
|
|
|
|
return (speed_table[speed]);
|
|
|
|
}
|
|
|
|
|
2010-11-22 01:28:29 +00:00
|
|
|
static void
|
2011-04-03 20:03:45 +00:00
|
|
|
print_flags(uint32_t flags)
|
2010-11-22 01:28:29 +00:00
|
|
|
{
|
2011-04-03 20:03:45 +00:00
|
|
|
printf(" flags %#x <%s%s%s%s%s%s%s%s%s0>\n",
|
|
|
|
flags,
|
|
|
|
FLAGS(flags, FORCE_SHORT_XFER),
|
|
|
|
FLAGS(flags, SHORT_XFER_OK),
|
|
|
|
FLAGS(flags, SHORT_FRAMES_OK),
|
|
|
|
FLAGS(flags, PIPE_BOF),
|
|
|
|
FLAGS(flags, PROXY_BUFFER),
|
|
|
|
FLAGS(flags, EXT_BUFFER),
|
|
|
|
FLAGS(flags, MANUAL_STATUS),
|
|
|
|
FLAGS(flags, NO_PIPE_OK),
|
|
|
|
FLAGS(flags, STALL_PIPE));
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-03 20:03:45 +00:00
|
|
|
print_status(uint32_t status)
|
2010-11-22 01:28:29 +00:00
|
|
|
{
|
2011-04-03 20:03:45 +00:00
|
|
|
printf(" status %#x <%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s0>\n",
|
|
|
|
status,
|
|
|
|
STATUS(status, OPEN),
|
|
|
|
STATUS(status, TRANSFERRING),
|
|
|
|
STATUS(status, DID_DMA_DELAY),
|
|
|
|
STATUS(status, DID_CLOSE),
|
|
|
|
STATUS(status, DRAINING),
|
|
|
|
STATUS(status, STARTED),
|
|
|
|
STATUS(status, BW_RECLAIMED),
|
|
|
|
STATUS(status, CONTROL_XFR),
|
|
|
|
STATUS(status, CONTROL_HDR),
|
|
|
|
STATUS(status, CONTROL_ACT),
|
|
|
|
STATUS(status, CONTROL_STALL),
|
|
|
|
STATUS(status, SHORT_FRAMES_OK),
|
|
|
|
STATUS(status, SHORT_XFER_OK),
|
|
|
|
STATUS(status, BDMA_ENABLE),
|
|
|
|
STATUS(status, BDMA_NO_POST_SYNC),
|
|
|
|
STATUS(status, BDMA_SETUP),
|
|
|
|
STATUS(status, ISOCHRONOUS_XFR),
|
|
|
|
STATUS(status, CURR_DMA_SET),
|
|
|
|
STATUS(status, CAN_CANCEL_IMMED),
|
|
|
|
STATUS(status, DOING_CALLBACK));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump a byte into hex format.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
hexbyte(char *buf, uint8_t temp)
|
|
|
|
{
|
|
|
|
uint8_t lo;
|
|
|
|
uint8_t hi;
|
|
|
|
|
|
|
|
lo = temp & 0xF;
|
|
|
|
hi = temp >> 4;
|
|
|
|
|
|
|
|
if (hi < 10)
|
|
|
|
buf[0] = '0' + hi;
|
|
|
|
else
|
|
|
|
buf[0] = 'A' + hi - 10;
|
|
|
|
|
|
|
|
if (lo < 10)
|
|
|
|
buf[1] = '0' + lo;
|
|
|
|
else
|
|
|
|
buf[1] = 'A' + lo - 10;
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display a region in traditional hexdump format.
|
|
|
|
*/
|
|
|
|
static void
|
2011-04-03 20:03:45 +00:00
|
|
|
hexdump(const uint8_t *region, uint32_t len)
|
2010-11-22 01:28:29 +00:00
|
|
|
{
|
2011-04-03 20:03:45 +00:00
|
|
|
const uint8_t *line;
|
|
|
|
char linebuf[128];
|
|
|
|
int i;
|
2011-01-28 08:00:57 +00:00
|
|
|
int x;
|
|
|
|
int c;
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
for (line = region; line < (region + len); line += 16) {
|
2011-04-03 20:03:45 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
linebuf[i] = ' ';
|
|
|
|
hexbyte(linebuf + i + 1, ((line - region) >> 8) & 0xFF);
|
|
|
|
hexbyte(linebuf + i + 3, (line - region) & 0xFF);
|
|
|
|
linebuf[i + 5] = ' ';
|
|
|
|
linebuf[i + 6] = ' ';
|
|
|
|
i += 7;
|
|
|
|
|
2010-11-22 01:28:29 +00:00
|
|
|
for (x = 0; x < 16; x++) {
|
2011-04-03 20:03:45 +00:00
|
|
|
if ((line + x) < (region + len)) {
|
|
|
|
hexbyte(linebuf + i,
|
|
|
|
*(const u_int8_t *)(line + x));
|
|
|
|
} else {
|
|
|
|
linebuf[i] = '-';
|
|
|
|
linebuf[i + 1] = '-';
|
|
|
|
}
|
|
|
|
linebuf[i + 2] = ' ';
|
|
|
|
if (x == 7) {
|
|
|
|
linebuf[i + 3] = ' ';
|
|
|
|
i += 4;
|
|
|
|
} else {
|
|
|
|
i += 3;
|
|
|
|
}
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
2011-04-03 20:03:45 +00:00
|
|
|
linebuf[i] = ' ';
|
|
|
|
linebuf[i + 1] = '|';
|
|
|
|
i += 2;
|
2010-11-22 01:28:29 +00:00
|
|
|
for (x = 0; x < 16; x++) {
|
|
|
|
if ((line + x) < (region + len)) {
|
|
|
|
c = *(const u_int8_t *)(line + x);
|
|
|
|
/* !isprint(c) */
|
|
|
|
if ((c < ' ') || (c > '~'))
|
|
|
|
c = '.';
|
2011-04-03 20:03:45 +00:00
|
|
|
linebuf[i] = c;
|
|
|
|
} else {
|
|
|
|
linebuf[i] = ' ';
|
|
|
|
}
|
|
|
|
i++;
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
2011-04-03 20:03:45 +00:00
|
|
|
linebuf[i] = '|';
|
|
|
|
linebuf[i + 1] = 0;
|
|
|
|
i += 2;
|
|
|
|
puts(linebuf);
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-16 16:29:21 +00:00
|
|
|
print_apacket(const struct header_32 *hdr, const uint8_t *ptr, int ptr_len)
|
2010-11-22 01:28:29 +00:00
|
|
|
{
|
|
|
|
struct tm *tm;
|
2011-04-03 20:03:45 +00:00
|
|
|
struct usbpf_pkthdr up_temp;
|
|
|
|
struct usbpf_pkthdr *up;
|
2010-11-22 01:28:29 +00:00
|
|
|
struct timeval tv;
|
|
|
|
size_t len;
|
2011-04-03 20:03:45 +00:00
|
|
|
uint32_t x;
|
2010-11-22 01:28:29 +00:00
|
|
|
char buf[64];
|
|
|
|
|
2011-04-03 20:03:45 +00:00
|
|
|
ptr += USBPF_HDR_LEN;
|
|
|
|
ptr_len -= USBPF_HDR_LEN;
|
|
|
|
if (ptr_len < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* make sure we don't change the source buffer */
|
|
|
|
memcpy(&up_temp, ptr - USBPF_HDR_LEN, sizeof(up_temp));
|
|
|
|
up = &up_temp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A packet from the kernel is based on little endian byte
|
|
|
|
* order.
|
|
|
|
*/
|
|
|
|
up->up_totlen = le32toh(up->up_totlen);
|
2010-11-22 01:28:29 +00:00
|
|
|
up->up_busunit = le32toh(up->up_busunit);
|
2011-04-03 20:03:45 +00:00
|
|
|
up->up_address = le32toh(up->up_address);
|
2010-11-22 01:28:29 +00:00
|
|
|
up->up_flags = le32toh(up->up_flags);
|
|
|
|
up->up_status = le32toh(up->up_status);
|
|
|
|
up->up_error = le32toh(up->up_error);
|
|
|
|
up->up_interval = le32toh(up->up_interval);
|
2011-04-03 20:03:45 +00:00
|
|
|
up->up_frames = le32toh(up->up_frames);
|
|
|
|
up->up_packet_size = le32toh(up->up_packet_size);
|
|
|
|
up->up_packet_count = le32toh(up->up_packet_count);
|
|
|
|
up->up_endpoint = le32toh(up->up_endpoint);
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2012-03-16 16:29:21 +00:00
|
|
|
tv.tv_sec = hdr->ts_sec;
|
|
|
|
tv.tv_usec = hdr->ts_usec;
|
2010-11-22 01:28:29 +00:00
|
|
|
tm = localtime(&tv.tv_sec);
|
|
|
|
|
|
|
|
len = strftime(buf, sizeof(buf), "%H:%M:%S", tm);
|
2011-04-03 20:03:45 +00:00
|
|
|
|
2011-04-04 02:57:19 +00:00
|
|
|
printf("%.*s.%06ld usbus%d.%d %s-%s-EP=%08x,SPD=%s,NFR=%d,SLEN=%d,IVAL=%d%s%s\n",
|
2011-04-03 20:03:45 +00:00
|
|
|
(int)len, buf, tv.tv_usec,
|
|
|
|
(int)up->up_busunit, (int)up->up_address,
|
|
|
|
(up->up_type == USBPF_XFERTAP_SUBMIT) ? "SUBM" : "DONE",
|
2010-11-22 01:28:29 +00:00
|
|
|
xfertype_table[up->up_xfertype],
|
2011-04-03 20:03:45 +00:00
|
|
|
(unsigned int)up->up_endpoint,
|
|
|
|
usb_speedstr(up->up_speed),
|
|
|
|
(int)up->up_frames,
|
|
|
|
(int)(up->up_totlen - USBPF_HDR_LEN -
|
|
|
|
(USBPF_FRAME_HDR_LEN * up->up_frames)),
|
|
|
|
(int)up->up_interval,
|
|
|
|
(up->up_type == USBPF_XFERTAP_DONE) ? ",ERR=" : "",
|
|
|
|
(up->up_type == USBPF_XFERTAP_DONE) ?
|
|
|
|
usb_errstr(up->up_error) : "");
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
if (verbose >= 1) {
|
2011-04-03 20:03:45 +00:00
|
|
|
for (x = 0; x != up->up_frames; x++) {
|
|
|
|
const struct usbpf_framehdr *uf;
|
|
|
|
uint32_t framelen;
|
|
|
|
uint32_t flags;
|
|
|
|
|
|
|
|
uf = (const struct usbpf_framehdr *)ptr;
|
|
|
|
ptr += USBPF_FRAME_HDR_LEN;
|
|
|
|
ptr_len -= USBPF_FRAME_HDR_LEN;
|
|
|
|
if (ptr_len < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
framelen = le32toh(uf->length);
|
|
|
|
flags = le32toh(uf->flags);
|
|
|
|
|
|
|
|
printf(" frame[%u] %s %d bytes\n",
|
|
|
|
(unsigned int)x,
|
|
|
|
(flags & USBPF_FRAMEFLAG_READ) ? "READ" : "WRITE",
|
|
|
|
(int)framelen);
|
|
|
|
|
|
|
|
if (flags & USBPF_FRAMEFLAG_DATA_FOLLOWS) {
|
|
|
|
|
|
|
|
int tot_frame_len;
|
|
|
|
|
|
|
|
tot_frame_len = USBPF_FRAME_ALIGN(framelen);
|
|
|
|
|
|
|
|
ptr_len -= tot_frame_len;
|
|
|
|
|
|
|
|
if (tot_frame_len < 0 ||
|
|
|
|
(int)framelen < 0 || (int)ptr_len < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
hexdump(ptr, framelen);
|
|
|
|
|
|
|
|
ptr += tot_frame_len;
|
|
|
|
}
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-03 20:03:45 +00:00
|
|
|
if (verbose >= 2)
|
2010-11-22 01:28:29 +00:00
|
|
|
print_flags(up->up_flags);
|
2011-04-03 20:03:45 +00:00
|
|
|
if (verbose >= 3)
|
2010-11-22 01:28:29 +00:00
|
|
|
print_status(up->up_status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-16 17:30:22 +00:00
|
|
|
fix_packets(uint8_t *data, const int datalen)
|
2010-11-22 01:28:29 +00:00
|
|
|
{
|
2012-03-16 16:29:21 +00:00
|
|
|
struct header_32 temp;
|
2011-04-03 20:03:45 +00:00
|
|
|
uint8_t *ptr;
|
|
|
|
uint8_t *next;
|
2012-03-16 17:30:22 +00:00
|
|
|
uint32_t hdrlen;
|
|
|
|
uint32_t caplen;
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
for (ptr = data; ptr < (data + datalen); ptr = next) {
|
2012-03-16 16:29:21 +00:00
|
|
|
|
2012-03-16 17:30:22 +00:00
|
|
|
const struct bpf_hdr *hdr;
|
|
|
|
|
|
|
|
hdr = (const struct bpf_hdr *)ptr;
|
|
|
|
|
|
|
|
temp.ts_sec = htole32(hdr->bh_tstamp.tv_sec);
|
|
|
|
temp.ts_usec = htole32(hdr->bh_tstamp.tv_usec);
|
|
|
|
temp.caplen = htole32(hdr->bh_caplen);
|
|
|
|
temp.datalen = htole32(hdr->bh_datalen);
|
|
|
|
temp.hdrlen = hdr->bh_hdrlen;
|
|
|
|
temp.align = BPF_WORDALIGN(1);
|
|
|
|
|
|
|
|
hdrlen = hdr->bh_hdrlen;
|
|
|
|
caplen = hdr->bh_caplen;
|
|
|
|
|
|
|
|
if ((hdrlen >= sizeof(temp)) && (hdrlen <= 255) &&
|
|
|
|
((ptr + hdrlen) <= (data + datalen))) {
|
|
|
|
memcpy(ptr, &temp, sizeof(temp));
|
|
|
|
memset(ptr + sizeof(temp), 0, hdrlen - sizeof(temp));
|
2012-03-16 16:29:21 +00:00
|
|
|
} else {
|
2012-03-16 17:30:22 +00:00
|
|
|
err(EXIT_FAILURE, "Invalid header length %d", hdrlen);
|
2012-03-16 16:29:21 +00:00
|
|
|
}
|
|
|
|
|
2012-03-16 17:30:22 +00:00
|
|
|
next = ptr + BPF_WORDALIGN(hdrlen + caplen);
|
|
|
|
|
2012-03-16 16:29:21 +00:00
|
|
|
if (next <= ptr)
|
2012-03-16 17:30:22 +00:00
|
|
|
err(EXIT_FAILURE, "Invalid length");
|
|
|
|
}
|
|
|
|
}
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2012-03-16 17:30:22 +00:00
|
|
|
static void
|
|
|
|
print_packets(uint8_t *data, const int datalen)
|
|
|
|
{
|
|
|
|
struct header_32 temp;
|
|
|
|
uint8_t *ptr;
|
|
|
|
uint8_t *next;
|
|
|
|
|
|
|
|
for (ptr = data; ptr < (data + datalen); ptr = next) {
|
|
|
|
|
|
|
|
const struct header_32 *hdr32;
|
|
|
|
|
|
|
|
hdr32 = (const struct header_32 *)ptr;
|
|
|
|
|
|
|
|
temp.ts_sec = le32toh(hdr32->ts_sec);
|
|
|
|
temp.ts_usec = le32toh(hdr32->ts_usec);
|
|
|
|
temp.caplen = le32toh(hdr32->caplen);
|
|
|
|
temp.datalen = le32toh(hdr32->datalen);
|
|
|
|
temp.hdrlen = hdr32->hdrlen;
|
|
|
|
temp.align = hdr32->align;
|
|
|
|
|
|
|
|
next = ptr + HEADER_ALIGN(temp.hdrlen + temp.caplen, temp.align);
|
|
|
|
|
|
|
|
if (next <= ptr)
|
|
|
|
err(EXIT_FAILURE, "Invalid length");
|
|
|
|
|
|
|
|
if (w_arg == NULL || r_arg != NULL) {
|
2012-03-16 16:29:21 +00:00
|
|
|
print_apacket(&temp, ptr +
|
|
|
|
temp.hdrlen, temp.caplen);
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
2011-04-03 20:03:45 +00:00
|
|
|
pkt_captured++;
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-03 20:03:45 +00:00
|
|
|
write_packets(struct usbcap *p, const uint8_t *data, const int datalen)
|
2010-11-22 01:28:29 +00:00
|
|
|
{
|
2011-04-03 20:03:45 +00:00
|
|
|
int len = htole32(datalen);
|
|
|
|
int ret;
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
ret = write(p->wfd, &len, sizeof(int));
|
2011-05-07 16:28:23 +00:00
|
|
|
if (ret != sizeof(int)) {
|
|
|
|
err(EXIT_FAILURE, "Could not write length "
|
|
|
|
"field of USB data payload");
|
|
|
|
}
|
2010-11-22 01:28:29 +00:00
|
|
|
ret = write(p->wfd, data, datalen);
|
2011-05-07 16:28:23 +00:00
|
|
|
if (ret != datalen) {
|
|
|
|
err(EXIT_FAILURE, "Could not write "
|
|
|
|
"complete USB data payload");
|
|
|
|
}
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_file(struct usbcap *p)
|
|
|
|
{
|
2011-04-03 20:03:45 +00:00
|
|
|
int datalen;
|
|
|
|
int ret;
|
|
|
|
uint8_t *data;
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
while ((ret = read(p->rfd, &datalen, sizeof(int))) == sizeof(int)) {
|
|
|
|
datalen = le32toh(datalen);
|
|
|
|
data = malloc(datalen);
|
2011-05-07 16:28:23 +00:00
|
|
|
if (data == NULL)
|
|
|
|
errx(EX_SOFTWARE, "Out of memory.");
|
2010-11-22 01:28:29 +00:00
|
|
|
ret = read(p->rfd, data, datalen);
|
2011-05-07 16:28:23 +00:00
|
|
|
if (ret != datalen) {
|
|
|
|
err(EXIT_FAILURE, "Could not read complete "
|
|
|
|
"USB data payload");
|
|
|
|
}
|
2012-03-16 17:30:22 +00:00
|
|
|
if (uf_minor == 2)
|
|
|
|
fix_packets(data, datalen);
|
|
|
|
|
2010-11-22 01:28:29 +00:00
|
|
|
print_packets(data, datalen);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_loop(struct usbcap *p)
|
|
|
|
{
|
|
|
|
int cc;
|
|
|
|
|
|
|
|
while (doexit == 0) {
|
2011-04-03 20:03:45 +00:00
|
|
|
cc = read(p->fd, (uint8_t *)p->buffer, p->bufsize);
|
2010-11-22 01:28:29 +00:00
|
|
|
if (cc < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "read: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cc == 0)
|
|
|
|
continue;
|
2012-03-16 17:30:22 +00:00
|
|
|
|
|
|
|
fix_packets(p->buffer, cc);
|
|
|
|
|
2010-11-22 01:28:29 +00:00
|
|
|
if (w_arg != NULL)
|
|
|
|
write_packets(p, p->buffer, cc);
|
|
|
|
print_packets(p->buffer, cc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_rfile(struct usbcap *p)
|
|
|
|
{
|
|
|
|
struct usbcap_filehdr uf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
p->rfd = open(r_arg, O_RDONLY);
|
|
|
|
if (p->rfd < 0) {
|
2011-05-07 16:28:23 +00:00
|
|
|
err(EXIT_FAILURE, "Could not open "
|
|
|
|
"'%s' for read", r_arg);
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
ret = read(p->rfd, &uf, sizeof(uf));
|
2011-05-07 16:28:23 +00:00
|
|
|
if (ret != sizeof(uf)) {
|
|
|
|
err(EXIT_FAILURE, "Could not read USB capture "
|
|
|
|
"file header");
|
|
|
|
}
|
|
|
|
if (le32toh(uf.magic) != USBCAP_FILEHDR_MAGIC) {
|
|
|
|
errx(EX_SOFTWARE, "Invalid magic field(0x%08x) "
|
|
|
|
"in USB capture file header.",
|
|
|
|
(unsigned int)le32toh(uf.magic));
|
|
|
|
}
|
|
|
|
if (uf.major != 0) {
|
|
|
|
errx(EX_SOFTWARE, "Invalid major version(%d) "
|
|
|
|
"field in USB capture file header.", (int)uf.major);
|
|
|
|
}
|
2012-03-16 17:30:22 +00:00
|
|
|
|
|
|
|
uf_minor = uf.minor;
|
|
|
|
|
|
|
|
if (uf.minor != 3 && uf.minor != 2) {
|
2011-05-07 16:28:23 +00:00
|
|
|
errx(EX_SOFTWARE, "Invalid minor version(%d) "
|
|
|
|
"field in USB capture file header.", (int)uf.minor);
|
|
|
|
}
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_wfile(struct usbcap *p)
|
|
|
|
{
|
|
|
|
struct usbcap_filehdr uf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
p->wfd = open(w_arg, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
|
|
|
|
if (p->wfd < 0) {
|
2011-05-07 16:28:23 +00:00
|
|
|
err(EXIT_FAILURE, "Could not open "
|
2012-03-16 17:30:22 +00:00
|
|
|
"'%s' for write", w_arg);
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
2011-05-07 16:28:23 +00:00
|
|
|
memset(&uf, 0, sizeof(uf));
|
2010-11-22 01:28:29 +00:00
|
|
|
uf.magic = htole32(USBCAP_FILEHDR_MAGIC);
|
|
|
|
uf.major = 0;
|
2012-03-16 17:30:22 +00:00
|
|
|
uf.minor = 3;
|
2010-11-22 01:28:29 +00:00
|
|
|
ret = write(p->wfd, (const void *)&uf, sizeof(uf));
|
2011-05-07 16:28:23 +00:00
|
|
|
if (ret != sizeof(uf)) {
|
|
|
|
err(EXIT_FAILURE, "Could not write "
|
|
|
|
"USB capture header");
|
|
|
|
}
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
#define FMT " %-14s %s\n"
|
|
|
|
fprintf(stderr, "usage: usbdump [options]\n");
|
2011-05-07 16:28:23 +00:00
|
|
|
fprintf(stderr, FMT, "-i <usbusX>", "Listen on USB bus interface");
|
2012-02-16 21:18:36 +00:00
|
|
|
fprintf(stderr, FMT, "-f <unit[.endpoint]>", "Specify a device and endpoint filter");
|
2011-05-07 16:28:23 +00:00
|
|
|
fprintf(stderr, FMT, "-r <file>", "Read the raw packets from file");
|
|
|
|
fprintf(stderr, FMT, "-s <snaplen>", "Snapshot bytes from each packet");
|
|
|
|
fprintf(stderr, FMT, "-v", "Increase the verbose level");
|
|
|
|
fprintf(stderr, FMT, "-w <file>", "Write the raw packets to file");
|
2010-11-22 01:28:29 +00:00
|
|
|
#undef FMT
|
2011-05-07 16:28:23 +00:00
|
|
|
exit(EX_USAGE);
|
2010-11-22 01:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2010-11-24 19:15:26 +00:00
|
|
|
struct bpf_program total_prog;
|
|
|
|
struct bpf_stat us;
|
|
|
|
struct bpf_version bv;
|
2010-11-22 01:28:29 +00:00
|
|
|
struct usbcap uc, *p = &uc;
|
2010-11-24 19:15:26 +00:00
|
|
|
struct ifreq ifr;
|
2010-11-22 01:28:29 +00:00
|
|
|
long snapshot = 192;
|
2011-04-03 20:03:45 +00:00
|
|
|
uint32_t v;
|
2012-02-16 21:18:36 +00:00
|
|
|
int fd;
|
|
|
|
int o;
|
|
|
|
int filt_unit;
|
|
|
|
int filt_ep;
|
2010-11-22 01:28:29 +00:00
|
|
|
const char *optstring;
|
2012-02-16 21:18:36 +00:00
|
|
|
char *pp;
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2011-05-07 16:28:23 +00:00
|
|
|
memset(&uc, 0, sizeof(struct usbcap));
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2012-02-16 21:18:36 +00:00
|
|
|
optstring = "i:r:s:vw:f:";
|
2010-11-22 01:28:29 +00:00
|
|
|
while ((o = getopt(argc, argv, optstring)) != -1) {
|
|
|
|
switch (o) {
|
|
|
|
case 'i':
|
|
|
|
i_arg = optarg;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
r_arg = optarg;
|
|
|
|
init_rfile(p);
|
|
|
|
break;
|
|
|
|
case 's':
|
2012-02-16 21:18:36 +00:00
|
|
|
snapshot = strtol(optarg, &pp, 10);
|
2010-11-22 01:28:29 +00:00
|
|
|
errno = 0;
|
2012-02-16 21:18:36 +00:00
|
|
|
if (pp != NULL && *pp != 0)
|
|
|
|
usage();
|
2010-11-22 01:28:29 +00:00
|
|
|
if (snapshot == 0 && errno == EINVAL)
|
|
|
|
usage();
|
|
|
|
/* snapeshot == 0 is special */
|
|
|
|
if (snapshot == 0)
|
|
|
|
snapshot = -1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
w_arg = optarg;
|
|
|
|
init_wfile(p);
|
|
|
|
break;
|
2012-02-16 21:18:36 +00:00
|
|
|
case 'f':
|
|
|
|
filt_unit = strtol(optarg, &pp, 10);
|
|
|
|
filt_ep = -1;
|
|
|
|
if (pp != NULL) {
|
|
|
|
if (*pp == '.') {
|
|
|
|
filt_ep = strtol(pp + 1, &pp, 10);
|
|
|
|
if (pp != NULL && *pp != 0)
|
|
|
|
usage();
|
|
|
|
} else if (*pp != 0) {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_filter(filt_unit, filt_ep);
|
|
|
|
break;
|
2010-11-22 01:28:29 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_arg != NULL) {
|
|
|
|
read_file(p);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2010-11-24 19:15:26 +00:00
|
|
|
p->fd = fd = open("/dev/bpf", O_RDONLY);
|
2011-05-07 16:28:23 +00:00
|
|
|
if (p->fd < 0)
|
|
|
|
err(EXIT_FAILURE, "Could not open BPF device");
|
|
|
|
|
|
|
|
if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
|
|
|
|
err(EXIT_FAILURE, "BIOCVERSION ioctl failed");
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2010-11-24 19:15:26 +00:00
|
|
|
if (bv.bv_major != BPF_MAJOR_VERSION ||
|
2011-05-07 16:28:23 +00:00
|
|
|
bv.bv_minor < BPF_MINOR_VERSION)
|
|
|
|
errx(EXIT_FAILURE, "Kernel BPF filter out of date");
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2011-04-03 20:03:45 +00:00
|
|
|
/* USB transfers can be greater than 64KByte */
|
|
|
|
v = 1U << 16;
|
|
|
|
|
|
|
|
/* clear ifr structure */
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
|
|
|
|
for ( ; v >= USBPF_HDR_LEN; v >>= 1) {
|
2010-11-24 19:15:26 +00:00
|
|
|
(void)ioctl(fd, BIOCSBLEN, (caddr_t)&v);
|
|
|
|
(void)strncpy(ifr.ifr_name, i_arg, sizeof(ifr.ifr_name));
|
|
|
|
if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
|
2010-11-22 01:28:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-05-07 16:28:23 +00:00
|
|
|
if (v == 0)
|
|
|
|
errx(EXIT_FAILURE, "No buffer size worked.");
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2011-05-07 16:28:23 +00:00
|
|
|
if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0)
|
|
|
|
err(EXIT_FAILURE, "BIOCGBLEN ioctl failed");
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
p->bufsize = v;
|
2011-04-03 20:03:45 +00:00
|
|
|
p->buffer = (uint8_t *)malloc(p->bufsize);
|
2011-05-07 16:28:23 +00:00
|
|
|
if (p->buffer == NULL)
|
|
|
|
errx(EX_SOFTWARE, "Out of memory.");
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2012-02-16 21:18:36 +00:00
|
|
|
make_filter(&total_prog, snapshot);
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2011-05-07 16:28:23 +00:00
|
|
|
if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0)
|
|
|
|
err(EXIT_FAILURE, "BIOCSETF ioctl failed");
|
2010-11-22 01:28:29 +00:00
|
|
|
|
2012-02-16 21:18:36 +00:00
|
|
|
free_filter(&total_prog);
|
|
|
|
|
2010-11-22 01:28:29 +00:00
|
|
|
/* 1 second read timeout */
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
2011-05-07 16:28:23 +00:00
|
|
|
if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&tv) < 0)
|
|
|
|
err(EXIT_FAILURE, "BIOCSRTIMEOUT ioctl failed");
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
(void)signal(SIGINT, handle_sigint);
|
|
|
|
|
|
|
|
do_loop(p);
|
|
|
|
|
2011-05-07 16:28:23 +00:00
|
|
|
if (ioctl(fd, BIOCGSTATS, (caddr_t)&us) < 0)
|
|
|
|
err(EXIT_FAILURE, "BIOCGSTATS ioctl failed");
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
/* XXX what's difference between pkt_captured and us.us_recv? */
|
|
|
|
printf("\n");
|
|
|
|
printf("%d packets captured\n", pkt_captured);
|
2010-11-24 19:15:26 +00:00
|
|
|
printf("%d packets received by filter\n", us.bs_recv);
|
|
|
|
printf("%d packets dropped by kernel\n", us.bs_drop);
|
2010-11-22 01:28:29 +00:00
|
|
|
|
|
|
|
if (p->fd > 0)
|
|
|
|
close(p->fd);
|
|
|
|
if (p->rfd > 0)
|
|
|
|
close(p->rfd);
|
|
|
|
if (p->wfd > 0)
|
|
|
|
close(p->wfd);
|
|
|
|
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|