xref: /openbsd-src/usr.sbin/tcpdump/print-usbpcap.c (revision 5e3c7963eb248119b7dfd4b0defad58a7d9cd306)
1 /*
2  * Copyright (c) 2018 Martin Pieuchot <mpi@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <dev/usb/usb.h>
18 #include <dev/usb/usbpcap.h>
19 
20 #include <pcap.h>
21 
22 #include "interface.h"
23 
24 #ifndef nitems
25 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
26 #endif
27 
28 const char *usbpcap_xfer_type[] = {"isoc", "intr", "ctrl", "bulk"};
29 
30 void
31 usbpcap_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
32 {
33 	u_int length = h->len;
34 	u_int caplen = h->caplen;
35 	const struct usbpcap_pkt_hdr *uph;
36 	u_int16_t hdrlen;
37 
38 	ts_print(&h->ts);
39 
40 	/* check length */
41 	if (caplen < sizeof(uint16_t)) {
42 		printf("[|usb]");
43 		goto out;
44 	}
45 	uph = (struct usbpcap_pkt_hdr *)p;
46 	hdrlen = letoh16(uph->uph_hlen);
47 	if (hdrlen < sizeof(*uph)) {
48 		printf("[usb: invalid header length %u!]", hdrlen);
49 		goto out;
50 	}
51 
52 	if (caplen < hdrlen) {
53 		printf("[|usb]");
54 		goto out;
55 	}
56 
57 	printf("bus %u %c addr %u: ep%u",
58 	    letoh16(uph->uph_bus),
59 	     ((uph->uph_info & USBPCAP_INFO_DIRECTION_IN) ? '<' : '>'),
60 	    letoh16(uph->uph_devaddr), UE_GET_ADDR(uph->uph_epaddr));
61 
62 	if (uph->uph_xfertype < nitems(usbpcap_xfer_type))
63 		printf(" %s", usbpcap_xfer_type[uph->uph_xfertype]);
64 	else
65 		printf(" ??");
66 
67 	printf(" %u", letoh32(uph->uph_dlen));
68 
69 	if (xflag)
70 		default_print(p + sizeof(*uph), length - sizeof(*uph));
71 out:
72 	putchar('\n');
73 }
74