From c904e20cc903d28c1090c531c907cdd6865455ff Mon Sep 17 00:00:00 2001 From: Hans Petter Selasky Date: Fri, 14 May 2021 17:12:57 +0200 Subject: [PATCH] Implement own command line option for the frequently used -i usbusX -f Y syntax, -d ugenX.Y, similar to what usbconfig(8) does. MFC after: 1 week Sponsored by: Mellanox Technologies // NVIDIA Networking --- usr.sbin/usbdump/usbdump.8 | 30 +++++++++++++++++++- usr.sbin/usbdump/usbdump.c | 56 ++++++++++++++++++++++++++++++++------ 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/usr.sbin/usbdump/usbdump.8 b/usr.sbin/usbdump/usbdump.8 index 475e832cd03b..6b5ad1920e94 100644 --- a/usr.sbin/usbdump/usbdump.8 +++ b/usr.sbin/usbdump/usbdump.8 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd April 24, 2012 +.Dd May 14, 2021 .Dt USBDUMP 8 .Os .Sh NAME @@ -33,6 +33,9 @@ .Nd "dump traffic on USB host controller" .Sh SYNOPSIS .Nm +.Op Fl d Ar [ugen]B +.Op Fl d Ar [ugen]B.D +.Op Fl d Ar [ugen]B.D.E .Op Fl i Ar ifname .Op Fl r Ar file .Op Fl s Ar snaplen @@ -48,6 +51,31 @@ utility provides a way to dump USB packets on host controllers. .Pp The following options are accepted: .Bl -tag -width ".Fl f Ar file" +.It Fl d Ar [ugen]bus +Shortcut for +.Fl i +option. +The argument may be prefixed by "ugen". +The option may be specified multiple times, but the bus specified must +be the same. +.It Fl d Ar [ugen]bus.device +Shortcut for +.Fl i +and +.Fl f +options. +The argument may be prefixed by "ugen". +The option may be specified multiple times, but the bus specified must +be the same. +.It Fl d Ar [ugen]bus.device.endpoint +Shortcut for +.Fl i +and +.Fl f +options. +The argument may be prefixed by "ugen". +The option may be specified multiple times, but the bus specified must +be the same. .It Fl b Ar file Store data part of the USB trace in binary format to the given .Ar file . diff --git a/usr.sbin/usbdump/usbdump.c b/usr.sbin/usbdump/usbdump.c index a38d08a252af..00d3b8e8913b 100644 --- a/usr.sbin/usbdump/usbdump.c +++ b/usr.sbin/usbdump/usbdump.c @@ -108,14 +108,14 @@ struct header_32 { uint8_t align; } __packed; -static int doexit = 0; -static int pkt_captured = 0; -static int verbose = 0; +static int doexit; +static int pkt_captured; +static int verbose; static int uf_minor; -static const char *i_arg = "usbus0"; -static const char *r_arg = NULL; -static const char *w_arg = NULL; -static const char *b_arg = NULL; +static char *i_arg; +static char *r_arg; +static char *w_arg; +static char *b_arg; static struct usbcap uc; static const char *errstr_table[USB_ERR_MAX] = { [USB_ERR_NORMAL_COMPLETION] = "0", @@ -779,7 +779,10 @@ usage(void) #define FMT " %-14s %s\n" fprintf(stderr, "usage: usbdump [options]\n"); - fprintf(stderr, FMT, "-i ", "Listen on USB bus interface"); + fprintf(stderr, FMT, "-d [ugen]B", "Listen on bus, B"); + fprintf(stderr, FMT, "-d [ugen]B.D", "Listen on bus, B and device, D"); + fprintf(stderr, FMT, "-d [ugen]B.D.E", "Listen on bus, B, device, D, and endpoint E"); + fprintf(stderr, FMT, "-i ", "Listen on this bus interface"); fprintf(stderr, FMT, "-f ", "Specify a device and endpoint filter"); fprintf(stderr, FMT, "-r ", "Read the raw packets from file"); fprintf(stderr, FMT, "-s ", "Snapshot bytes from each packet"); @@ -828,9 +831,41 @@ main(int argc, char *argv[]) const char *optstring; char *pp; - optstring = "b:hi:r:s:vw:f:"; + optstring = "b:d:hi:r:s:vw:f:"; while ((o = getopt(argc, argv, optstring)) != -1) { switch (o) { + case 'd': + pp = optarg; + if (pp[0] == 'u' && pp[1] == 'g' && pp[2] == 'e' && pp[3] == 'n') + pp += 4; + ifindex = strtol(pp, &pp, 10); + /* Must be same bus when using -d option. */ + if (i_arg != NULL) { + if (atoi(i_arg + 5) != ifindex) + usage(); + } else { + asprintf(&i_arg, "usbus%d", ifindex); + } + /* Parse unit and endpoint, if any. */ + if (pp != NULL) { + if (*pp == '.') { + filt_unit = strtol(pp + 1, &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); + } else if (*pp != 0) { + usage(); + } + } + break; case 'i': i_arg = optarg; break; @@ -879,6 +914,9 @@ main(int argc, char *argv[]) } } + if (i_arg == NULL) + i_arg = "usbus0"; + if (b_arg != NULL) { p->bfd = open(b_arg, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);