1 /* $FreeBSD$ */ 2 /*- 3 * SPDX-License-Identifier: BSD-3-Clause 4 * 5 * Copyright (c) 1990, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 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. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 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. 24 * 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 35 * SUCH DAMAGE. 36 */ 37 38 #ifdef USB_GLOBAL_INCLUDE_FILE 39 #include USB_GLOBAL_INCLUDE_FILE 40 #else 41 #include <sys/param.h> 42 #include <sys/kernel.h> 43 #include <sys/bus.h> 44 #include <sys/fcntl.h> 45 #include <sys/malloc.h> 46 #include <sys/proc.h> 47 #include <sys/socket.h> 48 #include <sys/sockio.h> 49 #include <net/if.h> 50 #include <net/if_var.h> 51 #include <net/if_types.h> 52 #include <net/if_clone.h> 53 #include <net/bpf.h> 54 #include <sys/sysctl.h> 55 #include <net/route.h> 56 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 */ 68 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); 80 81 static struct if_clone *usbpf_cloner; 82 static const char usbusname[] = "usbus"; 83 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); 86 87 static void 88 usbpf_init(void *arg) 89 { 90 91 usbpf_cloner = if_clone_advanced(usbusname, 0, usbpf_clone_match, 92 usbpf_clone_create, usbpf_clone_destroy); 93 } 94 95 static void 96 usbpf_uninit(void *arg) 97 { 98 int devlcnt; 99 device_t *devlp; 100 devclass_t dc; 101 struct usb_bus *ubus; 102 int error; 103 int i; 104 105 if_clone_detach(usbpf_cloner); 106 107 dc = devclass_find(usbusname); 108 if (dc == NULL) 109 return; 110 error = devclass_get_devices(dc, &devlp, &devlcnt); 111 if (error) 112 return; 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); 117 } 118 free(devlp, M_TEMP); 119 } 120 121 static int 122 usbpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 123 { 124 125 /* No configuration allowed. */ 126 return (EINVAL); 127 } 128 129 static struct usb_bus * 130 usbpf_ifname2ubus(const char *ifname) 131 { 132 device_t dev; 133 devclass_t dc; 134 int unit; 135 int error; 136 137 if (strncmp(ifname, usbusname, sizeof(usbusname) - 1) != 0) 138 return (NULL); 139 error = ifc_name2unit(ifname, &unit); 140 if (error || unit < 0) 141 return (NULL); 142 dc = devclass_find(usbusname); 143 if (dc == NULL) 144 return (NULL); 145 dev = devclass_get_device(dc, unit); 146 if (dev == NULL) 147 return (NULL); 148 149 return (device_get_softc(dev)); 150 } 151 152 static int 153 usbpf_clone_match(struct if_clone *ifc, const char *name) 154 { 155 struct usb_bus *ubus; 156 157 ubus = usbpf_ifname2ubus(name); 158 if (ubus == NULL) 159 return (0); 160 if (ubus->ifp != NULL) 161 return (0); 162 163 return (1); 164 } 165 166 static int 167 usbpf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 168 { 169 int error; 170 int unit; 171 struct ifnet *ifp; 172 struct usb_bus *ubus; 173 174 error = ifc_name2unit(name, &unit); 175 if (error) 176 return (error); 177 if (unit < 0) 178 return (EINVAL); 179 180 ubus = usbpf_ifname2ubus(name); 181 if (ubus == NULL) 182 return (1); 183 if (ubus->ifp != NULL) 184 return (1); 185 186 error = ifc_alloc_unit(ifc, &unit); 187 if (error) { 188 device_printf(ubus->parent, "usbpf: Could not allocate " 189 "instance\n"); 190 return (error); 191 } 192 ifp = ubus->ifp = if_alloc(IFT_USB); 193 if (ifp == NULL) { 194 ifc_free_unit(ifc, unit); 195 device_printf(ubus->parent, "usbpf: Could not allocate " 196 "instance\n"); 197 return (ENOSPC); 198 } 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; 204 if_attach(ifp); 205 ifp->if_flags |= IFF_UP; 206 rt_ifmsg(ifp); 207 /* 208 * XXX According to the specification of DLT_USB, it indicates 209 * packets beginning with USB setup header. But not sure all 210 * packets would be. 211 */ 212 bpfattach(ifp, DLT_USB, USBPF_HDR_LEN); 213 214 return (0); 215 } 216 217 static int 218 usbpf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 219 { 220 struct usb_bus *ubus; 221 int unit; 222 223 ubus = ifp->if_softc; 224 unit = ifp->if_dunit; 225 226 /* 227 * Lock USB before clearing the "ifp" pointer, to avoid 228 * clearing the pointer in the middle of a TAP operation: 229 */ 230 USB_BUS_LOCK(ubus); 231 ubus->ifp = NULL; 232 USB_BUS_UNLOCK(ubus); 233 bpfdetach(ifp); 234 if_detach(ifp); 235 if_free(ifp); 236 ifc_free_unit(ifc, unit); 237 238 return (0); 239 } 240 241 void 242 usbpf_attach(struct usb_bus *ubus) 243 { 244 245 if (bootverbose) 246 device_printf(ubus->parent, "usbpf: Attached\n"); 247 } 248 249 void 250 usbpf_detach(struct usb_bus *ubus) 251 { 252 253 if (ubus->ifp != NULL) 254 usbpf_clone_destroy(usbpf_cloner, ubus->ifp); 255 if (bootverbose) 256 device_printf(ubus->parent, "usbpf: Detached\n"); 257 } 258 259 static uint32_t 260 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags) 261 { 262 uint32_t val = 0; 263 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; 282 return (val); 283 } 284 285 static uint32_t 286 usbpf_aggregate_status(struct usb_xfer_flags_int *flags) 287 { 288 uint32_t val = 0; 289 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; 316 #if USB_HAVE_BUSDMA 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; 323 #endif 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; 332 333 return (val); 334 } 335 336 static int 337 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame) 338 { 339 int isread; 340 341 if ((frame == 0) && (xfer->flags_int.control_xfr != 0) && 342 (xfer->flags_int.control_hdr != 0)) { 343 /* special case */ 344 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 345 /* The device controller writes to memory */ 346 isread = 1; 347 } else { 348 /* The host controller reads from memory */ 349 isread = 0; 350 } 351 } else { 352 isread = USB_GET_DATA_ISREAD(xfer); 353 } 354 return (isread); 355 } 356 357 static uint32_t 358 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type) 359 { 360 uint32_t totlen; 361 uint32_t x; 362 uint32_t nframes; 363 364 if (type == USBPF_XFERTAP_SUBMIT) 365 nframes = xfer->nframes; 366 else 367 nframes = xfer->aframes; 368 369 totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes); 370 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( 376 xfer->frlengths[x]); 377 } 378 } else { 379 if (type == USBPF_XFERTAP_SUBMIT) { 380 totlen += USBPF_FRAME_ALIGN( 381 xfer->frlengths[x]); 382 } 383 } 384 } 385 return (totlen); 386 } 387 388 void 389 usbpf_xfertap(struct usb_xfer *xfer, int type) 390 { 391 struct usb_bus *bus; 392 struct usbpf_pkthdr *up; 393 struct usbpf_framehdr *uf; 394 usb_frlength_t offset; 395 uint32_t totlen; 396 uint32_t frame; 397 uint32_t temp; 398 uint32_t nframes; 399 uint32_t x; 400 uint8_t *buf; 401 uint8_t *ptr; 402 403 bus = xfer->xroot->bus; 404 405 /* sanity checks */ 406 if (bus->ifp == NULL || bus->ifp->if_bpf == NULL) 407 return; 408 if (!bpf_peers_present(bus->ifp->if_bpf)) 409 return; 410 411 totlen = usbpf_xfer_precompute_size(xfer, type); 412 413 if (type == USBPF_XFERTAP_SUBMIT) 414 nframes = xfer->nframes; 415 else 416 nframes = xfer->aframes; 417 418 /* 419 * XXX TODO XXX 420 * 421 * When BPF supports it we could pass a fragmented array of 422 * buffers avoiding the data copy operation here. 423 */ 424 buf = ptr = malloc(totlen, M_TEMP, M_NOWAIT); 425 if (buf == NULL) { 426 device_printf(bus->parent, "usbpf: Out of memory\n"); 427 return; 428 } 429 430 up = (struct usbpf_pkthdr *)ptr; 431 ptr += USBPF_HDR_LEN; 432 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; 440 else 441 up->up_mode = USBPF_MODE_HOST; 442 up->up_type = type; 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); 448 temp = xfer->error; 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; 460 461 /* clear reserved area */ 462 memset(up->up_reserved, 0, sizeof(up->up_reserved)); 463 464 /* init offset and frame */ 465 offset = 0; 466 frame = 0; 467 468 /* iterate all the USB frames and copy data, if any */ 469 for (x = 0; x != nframes; x++) { 470 uint32_t length; 471 int isread; 472 473 /* get length */ 474 length = xfer->frlengths[x]; 475 476 /* get frame header pointer */ 477 uf = (struct usbpf_framehdr *)ptr; 478 ptr += USBPF_FRAME_HDR_LEN; 479 480 /* fill out packet header */ 481 uf->length = htole32(length); 482 uf->flags = 0; 483 484 /* get information about data read/write */ 485 isread = usbpf_xfer_frame_is_read(xfer, x); 486 487 /* check if we need to copy any data */ 488 if (isread) { 489 if (type == USBPF_XFERTAP_SUBMIT) 490 length = 0; 491 else { 492 uf->flags |= htole32( 493 USBPF_FRAMEFLAG_DATA_FOLLOWS); 494 } 495 } else { 496 if (type != USBPF_XFERTAP_SUBMIT) 497 length = 0; 498 else { 499 uf->flags |= htole32( 500 USBPF_FRAMEFLAG_DATA_FOLLOWS); 501 } 502 } 503 504 /* check if data is read direction */ 505 if (isread) 506 uf->flags |= htole32(USBPF_FRAMEFLAG_READ); 507 508 /* copy USB data, if any */ 509 if (length != 0) { 510 /* copy data */ 511 usbd_copy_out(&xfer->frbuffers[frame], 512 offset, ptr, length); 513 514 /* align length */ 515 temp = USBPF_FRAME_ALIGN(length); 516 517 /* zero pad */ 518 if (temp != length) 519 memset(ptr + length, 0, temp - length); 520 521 ptr += temp; 522 } 523 524 if (xfer->flags_int.isochronous_xfr) { 525 offset += usbd_xfer_old_frame_length(xfer, x); 526 } else { 527 frame ++; 528 } 529 } 530 531 bpf_tap(bus->ifp->if_bpf, buf, totlen); 532 533 free(buf, M_TEMP); 534 } 535