1127668Sbms/*
2127668Sbms * Copyright 2009 Bert Vermeulen <bert@biot.com>
3127668Sbms *
4127668Sbms * Redistribution and use in source and binary forms, with or without
5127668Sbms * modification, are permitted provided that: (1) source code distributions
6127668Sbms * retain the above copyright notice and this paragraph in its entirety, (2)
7127668Sbms * distributions including binary code include the above copyright notice and
8127668Sbms * this paragraph in its entirety in the documentation or other materials
9127668Sbms * provided with the distribution, and (3) all advertising materials mentioning
10127668Sbms * features or use of this software display the following acknowledgement:
11127668Sbms * ``This product includes software developed by Paolo Abeni.''
12127668Sbms * The name of author may not be used to endorse or promote products derived
13127668Sbms * from this software without specific prior written permission.
14127668Sbms * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
15127668Sbms * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
16127668Sbms * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17127668Sbms *
18127668Sbms * Support for USB packets
19127668Sbms *
20127668Sbms */
21127668Sbms
22127668Sbms#ifdef HAVE_CONFIG_H
23190207Srpaulo#include "config.h"
24127668Sbms#endif
25127668Sbms
26127668Sbms#include <tcpdump-stdinc.h>
27127668Sbms
28127668Sbms#include <pcap.h>
29127668Sbms#include <stdio.h>
30127668Sbms#include <string.h>
31127668Sbms
32127668Sbms#include "interface.h"
33127668Sbms
34127668Sbms
35127668Sbms#if defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX)
36214478Srpaulo#include <pcap/usb.h>
37127668Sbms
38127668Sbms/* returns direction: 1=inbound 2=outbound -1=invalid */
39127668Sbmsstatic int
40127668Sbmsget_direction(int transfer_type, int event_type)
41127668Sbms{
42127668Sbms	int direction;
43127668Sbms
44127668Sbms	direction = -1;
45127668Sbms	switch(transfer_type){
46127668Sbms	case URB_BULK:
47127668Sbms	case URB_CONTROL:
48127668Sbms	case URB_ISOCHRONOUS:
49127668Sbms		switch(event_type)
50127668Sbms		{
51127668Sbms		case URB_SUBMIT:
52127668Sbms			direction = 2;
53127668Sbms			break;
54127668Sbms		case URB_COMPLETE:
55127668Sbms		case URB_ERROR:
56127668Sbms			direction = 1;
57127668Sbms			break;
58127668Sbms		default:
59127668Sbms			direction = -1;
60127668Sbms		}
61214478Srpaulo		break;
62214478Srpaulo	case URB_INTERRUPT:
63127668Sbms		switch(event_type)
64127668Sbms		{
65127668Sbms		case URB_SUBMIT:
66190207Srpaulo			direction = 1;
67190207Srpaulo			break;
68127668Sbms		case URB_COMPLETE:
69214478Srpaulo		case URB_ERROR:
70127668Sbms			direction = 2;
71127668Sbms			break;
72214478Srpaulo		default:
73214478Srpaulo			direction = -1;
74127668Sbms		}
75214478Srpaulo		break;
76127668Sbms	 default:
77127668Sbms		direction = -1;
78127668Sbms	}
79127668Sbms
80127668Sbms	return direction;
81127668Sbms}
82127668Sbms
83127668Sbmsstatic void
84146773Ssamusb_header_print(const pcap_usb_header *uh)
85127668Sbms{
86127668Sbms	int direction;
87127668Sbms
88127668Sbms	switch(uh->transfer_type)
89127668Sbms	{
90127668Sbms		case URB_ISOCHRONOUS:
91127668Sbms			printf("ISOCHRONOUS");
92127668Sbms			break;
93127668Sbms		case URB_INTERRUPT:
94127668Sbms			printf("INTERRUPT");
95127668Sbms			break;
96127668Sbms		case URB_CONTROL:
97127668Sbms			printf("CONTROL");
98127668Sbms			break;
99127668Sbms		case URB_BULK:
100127668Sbms			printf("BULK");
101127668Sbms			break;
102127668Sbms		default:
103127668Sbms			printf(" ?");
104127668Sbms	}
105127668Sbms
106127668Sbms	switch(uh->event_type)
107127668Sbms	{
108214478Srpaulo		case URB_SUBMIT:
109236192Sdelphij			printf(" SUBMIT");
110127668Sbms			break;
111127668Sbms		case URB_COMPLETE:
112127668Sbms			printf(" COMPLETE");
113127668Sbms			break;
114162017Ssam		case URB_ERROR:
115127668Sbms			printf(" ERROR");
116127668Sbms			break;
117127668Sbms		default:
118127668Sbms			printf(" ?");
119127668Sbms	}
120
121	direction = get_direction(uh->transfer_type, uh->event_type);
122	if(direction == 1)
123		printf(" from");
124	else if(direction == 2)
125		printf(" to");
126	printf(" %d:%d:%d", uh->bus_id, uh->device_address, uh->endpoint_number & 0x7f);
127}
128
129/*
130 * This is the top level routine of the printer for captures with a
131 * 48-byte header.
132 *
133 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
134 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
135 * is the number of bytes actually captured.
136 */
137u_int
138usb_linux_48_byte_print(const struct pcap_pkthdr *h, register const u_char *p)
139{
140	if (h->caplen < sizeof(pcap_usb_header)) {
141		printf("[|usb]");
142		return(sizeof(pcap_usb_header));
143	}
144
145	usb_header_print((const pcap_usb_header *) p);
146
147	return(sizeof(pcap_usb_header));
148}
149
150#ifdef DLT_USB_LINUX_MMAPPED
151/*
152 * This is the top level routine of the printer for captures with a
153 * 64-byte header.
154 *
155 * 'p' points to the header of the packet, 'h->ts' is the timestamp,
156 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
157 * is the number of bytes actually captured.
158 */
159u_int
160usb_linux_64_byte_print(const struct pcap_pkthdr *h, register const u_char *p)
161{
162	if (h->caplen < sizeof(pcap_usb_header_mmapped)) {
163		printf("[|usb]");
164		return(sizeof(pcap_usb_header_mmapped));
165	}
166
167	usb_header_print((const pcap_usb_header *) p);
168
169	return(sizeof(pcap_usb_header_mmapped));
170}
171#endif /* DLT_USB_LINUX_MMAPPED */
172
173#endif /* defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX) */
174
175