1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1990, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from the Stanford/CMU enet packet filter,
8 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10 * Berkeley Laboratory.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifdef USB_GLOBAL_INCLUDE_FILE
38 #include USB_GLOBAL_INCLUDE_FILE
39 #else
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/fcntl.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/epoch.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <net/bpf.h>
50 #include <sys/sysctl.h>
51
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usb_busdma.h>
55 #include <dev/usb/usb_controller.h>
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_device.h>
59 #include <dev/usb/usb_bus.h>
60 #include <dev/usb/usb_pf.h>
61 #include <dev/usb/usb_transfer.h>
62 #endif /* USB_GLOBAL_INCLUDE_FILE */
63
64 static uint32_t usbpf_aggregate_xferflags(struct usb_xfer_flags *);
65 static uint32_t usbpf_aggregate_status(struct usb_xfer_flags_int *);
66 static int usbpf_xfer_frame_is_read(struct usb_xfer *, uint32_t);
67 static uint32_t usbpf_xfer_precompute_size(struct usb_xfer *, int);
68
69 static const struct bif_methods bpf_usb_methods = {
70 /*
71 * XXXGL: bpf_tap() doesn't check direction, but we actually can
72 * report it and make USB dumping able to match direction.
73 */
74 .bif_chkdir = NULL
75 };
76
77 void
usbpf_attach(struct usb_bus * ubus)78 usbpf_attach(struct usb_bus *ubus)
79 {
80
81 ubus->bpf = bpf_attach(device_get_nameunit(ubus->bdev), DLT_USB,
82 USBPF_HDR_LEN, &bpf_usb_methods, ubus);
83 if (bootverbose)
84 device_printf(ubus->parent, "usbpf: Attached\n");
85 }
86
87 void
usbpf_detach(struct usb_bus * ubus)88 usbpf_detach(struct usb_bus *ubus)
89 {
90
91 bpf_detach(ubus->bpf);
92 if (bootverbose)
93 device_printf(ubus->parent, "usbpf: Detached\n");
94 }
95
96 static uint32_t
usbpf_aggregate_xferflags(struct usb_xfer_flags * flags)97 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
98 {
99 uint32_t val = 0;
100
101 if (flags->force_short_xfer == 1)
102 val |= USBPF_FLAG_FORCE_SHORT_XFER;
103 if (flags->short_xfer_ok == 1)
104 val |= USBPF_FLAG_SHORT_XFER_OK;
105 if (flags->short_frames_ok == 1)
106 val |= USBPF_FLAG_SHORT_FRAMES_OK;
107 if (flags->pipe_bof == 1)
108 val |= USBPF_FLAG_PIPE_BOF;
109 if (flags->proxy_buffer == 1)
110 val |= USBPF_FLAG_PROXY_BUFFER;
111 if (flags->ext_buffer == 1)
112 val |= USBPF_FLAG_EXT_BUFFER;
113 if (flags->manual_status == 1)
114 val |= USBPF_FLAG_MANUAL_STATUS;
115 if (flags->no_pipe_ok == 1)
116 val |= USBPF_FLAG_NO_PIPE_OK;
117 if (flags->stall_pipe == 1)
118 val |= USBPF_FLAG_STALL_PIPE;
119 return (val);
120 }
121
122 static uint32_t
usbpf_aggregate_status(struct usb_xfer_flags_int * flags)123 usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
124 {
125 uint32_t val = 0;
126
127 if (flags->open == 1)
128 val |= USBPF_STATUS_OPEN;
129 if (flags->transferring == 1)
130 val |= USBPF_STATUS_TRANSFERRING;
131 if (flags->did_dma_delay == 1)
132 val |= USBPF_STATUS_DID_DMA_DELAY;
133 if (flags->did_close == 1)
134 val |= USBPF_STATUS_DID_CLOSE;
135 if (flags->draining == 1)
136 val |= USBPF_STATUS_DRAINING;
137 if (flags->started == 1)
138 val |= USBPF_STATUS_STARTED;
139 if (flags->bandwidth_reclaimed == 1)
140 val |= USBPF_STATUS_BW_RECLAIMED;
141 if (flags->control_xfr == 1)
142 val |= USBPF_STATUS_CONTROL_XFR;
143 if (flags->control_hdr == 1)
144 val |= USBPF_STATUS_CONTROL_HDR;
145 if (flags->control_act == 1)
146 val |= USBPF_STATUS_CONTROL_ACT;
147 if (flags->control_stall == 1)
148 val |= USBPF_STATUS_CONTROL_STALL;
149 if (flags->short_frames_ok == 1)
150 val |= USBPF_STATUS_SHORT_FRAMES_OK;
151 if (flags->short_xfer_ok == 1)
152 val |= USBPF_STATUS_SHORT_XFER_OK;
153 #if USB_HAVE_BUSDMA
154 if (flags->bdma_enable == 1)
155 val |= USBPF_STATUS_BDMA_ENABLE;
156 if (flags->bdma_no_post_sync == 1)
157 val |= USBPF_STATUS_BDMA_NO_POST_SYNC;
158 if (flags->bdma_setup == 1)
159 val |= USBPF_STATUS_BDMA_SETUP;
160 #endif
161 if (flags->isochronous_xfr == 1)
162 val |= USBPF_STATUS_ISOCHRONOUS_XFR;
163 if (flags->curr_dma_set == 1)
164 val |= USBPF_STATUS_CURR_DMA_SET;
165 if (flags->can_cancel_immed == 1)
166 val |= USBPF_STATUS_CAN_CANCEL_IMMED;
167 if (flags->doing_callback == 1)
168 val |= USBPF_STATUS_DOING_CALLBACK;
169
170 return (val);
171 }
172
173 static int
usbpf_xfer_frame_is_read(struct usb_xfer * xfer,uint32_t frame)174 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame)
175 {
176 int isread;
177
178 if ((frame == 0) && (xfer->flags_int.control_xfr != 0) &&
179 (xfer->flags_int.control_hdr != 0)) {
180 /* special case */
181 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
182 /* The device controller writes to memory */
183 isread = 1;
184 } else {
185 /* The host controller reads from memory */
186 isread = 0;
187 }
188 } else {
189 isread = USB_GET_DATA_ISREAD(xfer);
190 }
191 return (isread);
192 }
193
194 static uint32_t
usbpf_xfer_precompute_size(struct usb_xfer * xfer,int type)195 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type)
196 {
197 uint32_t totlen;
198 uint32_t x;
199 uint32_t nframes;
200
201 if (type == USBPF_XFERTAP_SUBMIT)
202 nframes = xfer->nframes;
203 else
204 nframes = xfer->aframes;
205
206 totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes);
207
208 /* precompute all trace lengths */
209 for (x = 0; x != nframes; x++) {
210 if (usbpf_xfer_frame_is_read(xfer, x)) {
211 if (type != USBPF_XFERTAP_SUBMIT) {
212 totlen += USBPF_FRAME_ALIGN(
213 xfer->frlengths[x]);
214 }
215 } else {
216 if (type == USBPF_XFERTAP_SUBMIT) {
217 totlen += USBPF_FRAME_ALIGN(
218 xfer->frlengths[x]);
219 }
220 }
221 }
222 return (totlen);
223 }
224
225 void
usbpf_xfertap(struct usb_xfer * xfer,int type)226 usbpf_xfertap(struct usb_xfer *xfer, int type)
227 {
228 struct epoch_tracker et;
229 struct usb_bus *bus;
230 struct usbpf_pkthdr *up;
231 struct usbpf_framehdr *uf;
232 usb_frlength_t offset;
233 uint32_t totlen;
234 uint32_t frame;
235 uint32_t temp;
236 uint32_t nframes;
237 uint32_t x;
238 uint8_t *buf;
239 uint8_t *ptr;
240
241 bus = xfer->xroot->bus;
242
243 totlen = usbpf_xfer_precompute_size(xfer, type);
244
245 if (type == USBPF_XFERTAP_SUBMIT)
246 nframes = xfer->nframes;
247 else
248 nframes = xfer->aframes;
249
250 /*
251 * XXX TODO XXX
252 *
253 * When BPF supports it we could pass a fragmented array of
254 * buffers avoiding the data copy operation here.
255 */
256 buf = ptr = malloc(totlen, M_TEMP, M_NOWAIT);
257 if (buf == NULL) {
258 device_printf(bus->parent, "usbpf: Out of memory\n");
259 return;
260 }
261
262 up = (struct usbpf_pkthdr *)ptr;
263 ptr += USBPF_HDR_LEN;
264
265 /* fill out header */
266 temp = device_get_unit(bus->bdev);
267 up->up_totlen = htole32(totlen);
268 up->up_busunit = htole32(temp);
269 up->up_address = xfer->xroot->udev->device_index;
270 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
271 up->up_mode = USBPF_MODE_DEVICE;
272 else
273 up->up_mode = USBPF_MODE_HOST;
274 up->up_type = type;
275 up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
276 temp = usbpf_aggregate_xferflags(&xfer->flags);
277 up->up_flags = htole32(temp);
278 temp = usbpf_aggregate_status(&xfer->flags_int);
279 up->up_status = htole32(temp);
280 temp = xfer->error;
281 up->up_error = htole32(temp);
282 temp = xfer->interval;
283 up->up_interval = htole32(temp);
284 up->up_frames = htole32(nframes);
285 temp = xfer->max_packet_size;
286 up->up_packet_size = htole32(temp);
287 temp = xfer->max_packet_count;
288 up->up_packet_count = htole32(temp);
289 temp = xfer->endpointno;
290 up->up_endpoint = htole32(temp);
291 up->up_speed = xfer->xroot->udev->speed;
292
293 /* clear reserved area */
294 memset(up->up_reserved, 0, sizeof(up->up_reserved));
295
296 /* init offset and frame */
297 offset = 0;
298 frame = 0;
299
300 /* iterate all the USB frames and copy data, if any */
301 for (x = 0; x != nframes; x++) {
302 uint32_t length;
303 int isread;
304
305 /* get length */
306 length = xfer->frlengths[x];
307
308 /* get frame header pointer */
309 uf = (struct usbpf_framehdr *)ptr;
310 ptr += USBPF_FRAME_HDR_LEN;
311
312 /* fill out packet header */
313 uf->length = htole32(length);
314 uf->flags = 0;
315
316 /* get information about data read/write */
317 isread = usbpf_xfer_frame_is_read(xfer, x);
318
319 /* check if we need to copy any data */
320 if (isread) {
321 if (type == USBPF_XFERTAP_SUBMIT)
322 length = 0;
323 else {
324 uf->flags |= htole32(
325 USBPF_FRAMEFLAG_DATA_FOLLOWS);
326 }
327 } else {
328 if (type != USBPF_XFERTAP_SUBMIT)
329 length = 0;
330 else {
331 uf->flags |= htole32(
332 USBPF_FRAMEFLAG_DATA_FOLLOWS);
333 }
334 }
335
336 /* check if data is read direction */
337 if (isread)
338 uf->flags |= htole32(USBPF_FRAMEFLAG_READ);
339
340 /* copy USB data, if any */
341 if (length != 0) {
342 /* copy data */
343 usbd_copy_out(&xfer->frbuffers[frame],
344 offset, ptr, length);
345
346 /* align length */
347 temp = USBPF_FRAME_ALIGN(length);
348
349 /* zero pad */
350 if (temp != length)
351 memset(ptr + length, 0, temp - length);
352
353 ptr += temp;
354 }
355
356 if (xfer->flags_int.isochronous_xfr) {
357 offset += usbd_xfer_old_frame_length(xfer, x);
358 } else {
359 frame ++;
360 }
361 }
362
363 NET_EPOCH_ENTER(et);
364 bpf_tap(bus->bpf, buf, totlen);
365 NET_EPOCH_EXIT(et);
366
367 free(buf, M_TEMP);
368 }
369