3 * SPDX-License-Identifier: BSD-3-Clause
5 * Copyright (c) 1990, 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from the Stanford/CMU enet packet filter,
9 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
10 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
11 * Berkeley Laboratory.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifdef USB_GLOBAL_INCLUDE_FILE
39 #include USB_GLOBAL_INCLUDE_FILE
41 #include <sys/param.h>
42 #include <sys/kernel.h>
44 #include <sys/fcntl.h>
45 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
50 #include <net/if_var.h>
51 #include <net/if_types.h>
52 #include <net/if_clone.h>
54 #include <sys/sysctl.h>
55 #include <net/route.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usb_busdma.h>
60 #include <dev/usb/usb_controller.h>
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_bus.h>
65 #include <dev/usb/usb_pf.h>
66 #include <dev/usb/usb_transfer.h>
67 #endif /* USB_GLOBAL_INCLUDE_FILE */
69 static void usbpf_init(void *);
70 static void usbpf_uninit(void *);
71 static int usbpf_ioctl(struct ifnet *, u_long, caddr_t);
72 static int usbpf_clone_match(struct if_clone *, const char *);
73 static int usbpf_clone_create(struct if_clone *, char *, size_t, caddr_t);
74 static int usbpf_clone_destroy(struct if_clone *, struct ifnet *);
75 static struct usb_bus *usbpf_ifname2ubus(const char *);
76 static uint32_t usbpf_aggregate_xferflags(struct usb_xfer_flags *);
77 static uint32_t usbpf_aggregate_status(struct usb_xfer_flags_int *);
78 static int usbpf_xfer_frame_is_read(struct usb_xfer *, uint32_t);
79 static uint32_t usbpf_xfer_precompute_size(struct usb_xfer *, int);
81 static struct if_clone *usbpf_cloner;
82 static const char usbusname[] = "usbus";
84 SYSINIT(usbpf_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_init, NULL);
85 SYSUNINIT(usbpf_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_uninit, NULL);
91 usbpf_cloner = if_clone_advanced(usbusname, 0, usbpf_clone_match,
92 usbpf_clone_create, usbpf_clone_destroy);
96 usbpf_uninit(void *arg)
101 struct usb_bus *ubus;
105 if_clone_detach(usbpf_cloner);
107 dc = devclass_find(usbusname);
110 error = devclass_get_devices(dc, &devlp, &devlcnt);
113 for (i = 0; i < devlcnt; i++) {
114 ubus = device_get_softc(devlp[i]);
115 if (ubus != NULL && ubus->ifp != NULL)
116 usbpf_clone_destroy(usbpf_cloner, ubus->ifp);
122 usbpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
125 /* No configuration allowed. */
129 static struct usb_bus *
130 usbpf_ifname2ubus(const char *ifname)
137 if (strncmp(ifname, usbusname, sizeof(usbusname) - 1) != 0)
139 error = ifc_name2unit(ifname, &unit);
140 if (error || unit < 0)
142 dc = devclass_find(usbusname);
145 dev = devclass_get_device(dc, unit);
149 return (device_get_softc(dev));
153 usbpf_clone_match(struct if_clone *ifc, const char *name)
155 struct usb_bus *ubus;
157 ubus = usbpf_ifname2ubus(name);
160 if (ubus->ifp != NULL)
167 usbpf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
172 struct usb_bus *ubus;
174 error = ifc_name2unit(name, &unit);
180 ubus = usbpf_ifname2ubus(name);
183 if (ubus->ifp != NULL)
186 error = ifc_alloc_unit(ifc, &unit);
188 device_printf(ubus->parent, "usbpf: Could not allocate "
192 ifp = ubus->ifp = if_alloc(IFT_USB);
194 ifc_free_unit(ifc, unit);
195 device_printf(ubus->parent, "usbpf: Could not allocate "
199 strlcpy(ifp->if_xname, name, sizeof(ifp->if_xname));
200 ifp->if_softc = ubus;
201 ifp->if_dname = usbusname;
202 ifp->if_dunit = unit;
203 ifp->if_ioctl = usbpf_ioctl;
205 ifp->if_flags |= IFF_UP;
208 * XXX According to the specification of DLT_USB, it indicates
209 * packets beginning with USB setup header. But not sure all
212 bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
218 usbpf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
220 struct usb_bus *ubus;
223 ubus = ifp->if_softc;
224 unit = ifp->if_dunit;
227 * Lock USB before clearing the "ifp" pointer, to avoid
228 * clearing the pointer in the middle of a TAP operation:
232 USB_BUS_UNLOCK(ubus);
236 ifc_free_unit(ifc, unit);
242 usbpf_attach(struct usb_bus *ubus)
246 device_printf(ubus->parent, "usbpf: Attached\n");
250 usbpf_detach(struct usb_bus *ubus)
253 if (ubus->ifp != NULL)
254 usbpf_clone_destroy(usbpf_cloner, ubus->ifp);
256 device_printf(ubus->parent, "usbpf: Detached\n");
260 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
264 if (flags->force_short_xfer == 1)
265 val |= USBPF_FLAG_FORCE_SHORT_XFER;
266 if (flags->short_xfer_ok == 1)
267 val |= USBPF_FLAG_SHORT_XFER_OK;
268 if (flags->short_frames_ok == 1)
269 val |= USBPF_FLAG_SHORT_FRAMES_OK;
270 if (flags->pipe_bof == 1)
271 val |= USBPF_FLAG_PIPE_BOF;
272 if (flags->proxy_buffer == 1)
273 val |= USBPF_FLAG_PROXY_BUFFER;
274 if (flags->ext_buffer == 1)
275 val |= USBPF_FLAG_EXT_BUFFER;
276 if (flags->manual_status == 1)
277 val |= USBPF_FLAG_MANUAL_STATUS;
278 if (flags->no_pipe_ok == 1)
279 val |= USBPF_FLAG_NO_PIPE_OK;
280 if (flags->stall_pipe == 1)
281 val |= USBPF_FLAG_STALL_PIPE;
286 usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
290 if (flags->open == 1)
291 val |= USBPF_STATUS_OPEN;
292 if (flags->transferring == 1)
293 val |= USBPF_STATUS_TRANSFERRING;
294 if (flags->did_dma_delay == 1)
295 val |= USBPF_STATUS_DID_DMA_DELAY;
296 if (flags->did_close == 1)
297 val |= USBPF_STATUS_DID_CLOSE;
298 if (flags->draining == 1)
299 val |= USBPF_STATUS_DRAINING;
300 if (flags->started == 1)
301 val |= USBPF_STATUS_STARTED;
302 if (flags->bandwidth_reclaimed == 1)
303 val |= USBPF_STATUS_BW_RECLAIMED;
304 if (flags->control_xfr == 1)
305 val |= USBPF_STATUS_CONTROL_XFR;
306 if (flags->control_hdr == 1)
307 val |= USBPF_STATUS_CONTROL_HDR;
308 if (flags->control_act == 1)
309 val |= USBPF_STATUS_CONTROL_ACT;
310 if (flags->control_stall == 1)
311 val |= USBPF_STATUS_CONTROL_STALL;
312 if (flags->short_frames_ok == 1)
313 val |= USBPF_STATUS_SHORT_FRAMES_OK;
314 if (flags->short_xfer_ok == 1)
315 val |= USBPF_STATUS_SHORT_XFER_OK;
317 if (flags->bdma_enable == 1)
318 val |= USBPF_STATUS_BDMA_ENABLE;
319 if (flags->bdma_no_post_sync == 1)
320 val |= USBPF_STATUS_BDMA_NO_POST_SYNC;
321 if (flags->bdma_setup == 1)
322 val |= USBPF_STATUS_BDMA_SETUP;
324 if (flags->isochronous_xfr == 1)
325 val |= USBPF_STATUS_ISOCHRONOUS_XFR;
326 if (flags->curr_dma_set == 1)
327 val |= USBPF_STATUS_CURR_DMA_SET;
328 if (flags->can_cancel_immed == 1)
329 val |= USBPF_STATUS_CAN_CANCEL_IMMED;
330 if (flags->doing_callback == 1)
331 val |= USBPF_STATUS_DOING_CALLBACK;
337 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame)
341 if ((frame == 0) && (xfer->flags_int.control_xfr != 0) &&
342 (xfer->flags_int.control_hdr != 0)) {
344 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
345 /* The device controller writes to memory */
348 /* The host controller reads from memory */
352 isread = USB_GET_DATA_ISREAD(xfer);
358 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type)
364 if (type == USBPF_XFERTAP_SUBMIT)
365 nframes = xfer->nframes;
367 nframes = xfer->aframes;
369 totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes);
371 /* precompute all trace lengths */
372 for (x = 0; x != nframes; x++) {
373 if (usbpf_xfer_frame_is_read(xfer, x)) {
374 if (type != USBPF_XFERTAP_SUBMIT) {
375 totlen += USBPF_FRAME_ALIGN(
379 if (type == USBPF_XFERTAP_SUBMIT) {
380 totlen += USBPF_FRAME_ALIGN(
389 usbpf_xfertap(struct usb_xfer *xfer, int type)
392 struct usbpf_pkthdr *up;
393 struct usbpf_framehdr *uf;
394 usb_frlength_t offset;
403 bus = xfer->xroot->bus;
406 if (bus->ifp == NULL || bus->ifp->if_bpf == NULL)
408 if (!bpf_peers_present(bus->ifp->if_bpf))
411 totlen = usbpf_xfer_precompute_size(xfer, type);
413 if (type == USBPF_XFERTAP_SUBMIT)
414 nframes = xfer->nframes;
416 nframes = xfer->aframes;
421 * When BPF supports it we could pass a fragmented array of
422 * buffers avoiding the data copy operation here.
424 buf = ptr = malloc(totlen, M_TEMP, M_NOWAIT);
426 device_printf(bus->parent, "usbpf: Out of memory\n");
430 up = (struct usbpf_pkthdr *)ptr;
431 ptr += USBPF_HDR_LEN;
433 /* fill out header */
434 temp = device_get_unit(bus->bdev);
435 up->up_totlen = htole32(totlen);
436 up->up_busunit = htole32(temp);
437 up->up_address = xfer->xroot->udev->device_index;
438 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
439 up->up_mode = USBPF_MODE_DEVICE;
441 up->up_mode = USBPF_MODE_HOST;
443 up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
444 temp = usbpf_aggregate_xferflags(&xfer->flags);
445 up->up_flags = htole32(temp);
446 temp = usbpf_aggregate_status(&xfer->flags_int);
447 up->up_status = htole32(temp);
449 up->up_error = htole32(temp);
450 temp = xfer->interval;
451 up->up_interval = htole32(temp);
452 up->up_frames = htole32(nframes);
453 temp = xfer->max_packet_size;
454 up->up_packet_size = htole32(temp);
455 temp = xfer->max_packet_count;
456 up->up_packet_count = htole32(temp);
457 temp = xfer->endpointno;
458 up->up_endpoint = htole32(temp);
459 up->up_speed = xfer->xroot->udev->speed;
461 /* clear reserved area */
462 memset(up->up_reserved, 0, sizeof(up->up_reserved));
464 /* init offset and frame */
468 /* iterate all the USB frames and copy data, if any */
469 for (x = 0; x != nframes; x++) {
474 length = xfer->frlengths[x];
476 /* get frame header pointer */
477 uf = (struct usbpf_framehdr *)ptr;
478 ptr += USBPF_FRAME_HDR_LEN;
480 /* fill out packet header */
481 uf->length = htole32(length);
484 /* get information about data read/write */
485 isread = usbpf_xfer_frame_is_read(xfer, x);
487 /* check if we need to copy any data */
489 if (type == USBPF_XFERTAP_SUBMIT)
492 uf->flags |= htole32(
493 USBPF_FRAMEFLAG_DATA_FOLLOWS);
496 if (type != USBPF_XFERTAP_SUBMIT)
499 uf->flags |= htole32(
500 USBPF_FRAMEFLAG_DATA_FOLLOWS);
504 /* check if data is read direction */
506 uf->flags |= htole32(USBPF_FRAMEFLAG_READ);
508 /* copy USB data, if any */
511 usbd_copy_out(&xfer->frbuffers[frame],
512 offset, ptr, length);
515 temp = USBPF_FRAME_ALIGN(length);
519 memset(ptr + length, 0, temp - length);
524 if (xfer->flags_int.isochronous_xfr) {
525 offset += usbd_xfer_old_frame_length(xfer, x);
531 bpf_tap(bus->ifp->if_bpf, buf, totlen);