1 /*- 2 * Copyright (c) 1990, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from the Stanford/CMU enet packet filter, 6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed 7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 8 * Berkeley Laboratory. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 #include <sys/param.h> 38 #include <sys/kernel.h> 39 #include <sys/bus.h> 40 #include <sys/fcntl.h> 41 #include <sys/malloc.h> 42 #include <sys/proc.h> 43 #include <sys/socket.h> 44 #include <sys/sockio.h> 45 #include <net/if.h> 46 #include <net/if_types.h> 47 #include <net/if_clone.h> 48 #include <net/bpf.h> 49 #include <sys/sysctl.h> 50 #include <net/route.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 63 #define USBUSNAME "usbus" 64 65 static void usbpf_init(void); 66 static void usbpf_uninit(void); 67 static int usbpf_ioctl(struct ifnet *, u_long, caddr_t); 68 static int usbpf_clone_match(struct if_clone *, const char *); 69 static int usbpf_clone_create(struct if_clone *, char *, size_t, caddr_t); 70 static int usbpf_clone_destroy(struct if_clone *, struct ifnet *); 71 static struct usb_bus *usbpf_ifname2ubus(const char *); 72 static uint32_t usbpf_aggregate_xferflags(struct usb_xfer_flags *); 73 static uint32_t usbpf_aggregate_status(struct usb_xfer_flags_int *); 74 static int usbpf_xfer_frame_is_read(struct usb_xfer *, uint32_t); 75 static uint32_t usbpf_xfer_precompute_size(struct usb_xfer *, int); 76 77 static struct if_clone usbpf_cloner = IFC_CLONE_INITIALIZER( 78 USBUSNAME, NULL, IF_MAXUNIT, 79 NULL, usbpf_clone_match, usbpf_clone_create, usbpf_clone_destroy); 80 81 SYSINIT(usbpf_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_init, NULL); 82 SYSUNINIT(usbpf_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_uninit, NULL); 83 84 static void 85 usbpf_init(void) 86 { 87 88 if_clone_attach(&usbpf_cloner); 89 } 90 91 static void 92 usbpf_uninit(void) 93 { 94 int devlcnt; 95 device_t *devlp; 96 devclass_t dc; 97 struct usb_bus *ubus; 98 int error; 99 int i; 100 101 if_clone_detach(&usbpf_cloner); 102 103 dc = devclass_find(USBUSNAME); 104 if (dc == NULL) 105 return; 106 error = devclass_get_devices(dc, &devlp, &devlcnt); 107 if (error) 108 return; 109 for (i = 0; i < devlcnt; i++) { 110 ubus = device_get_softc(devlp[i]); 111 if (ubus != NULL && ubus->ifp != NULL) 112 usbpf_clone_destroy(&usbpf_cloner, ubus->ifp); 113 } 114 } 115 116 static int 117 usbpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 118 { 119 120 /* No configuration allowed. */ 121 return (EINVAL); 122 } 123 124 static struct usb_bus * 125 usbpf_ifname2ubus(const char *ifname) 126 { 127 device_t dev; 128 devclass_t dc; 129 int unit; 130 int error; 131 132 if (strncmp(ifname, USBUSNAME, sizeof(USBUSNAME) - 1) != 0) 133 return (NULL); 134 error = ifc_name2unit(ifname, &unit); 135 if (error || unit < 0) 136 return (NULL); 137 dc = devclass_find(USBUSNAME); 138 if (dc == NULL) 139 return (NULL); 140 dev = devclass_get_device(dc, unit); 141 if (dev == NULL) 142 return (NULL); 143 144 return (device_get_softc(dev)); 145 } 146 147 static int 148 usbpf_clone_match(struct if_clone *ifc, const char *name) 149 { 150 struct usb_bus *ubus; 151 152 ubus = usbpf_ifname2ubus(name); 153 if (ubus == NULL) 154 return (0); 155 if (ubus->ifp != NULL) 156 return (0); 157 158 return (1); 159 } 160 161 static int 162 usbpf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 163 { 164 int error; 165 int unit; 166 struct ifnet *ifp; 167 struct usb_bus *ubus; 168 169 error = ifc_name2unit(name, &unit); 170 if (error) 171 return (error); 172 if (unit < 0) 173 return (EINVAL); 174 175 ubus = usbpf_ifname2ubus(name); 176 if (ubus == NULL) 177 return (1); 178 if (ubus->ifp != NULL) 179 return (1); 180 181 error = ifc_alloc_unit(ifc, &unit); 182 if (error) { 183 ifc_free_unit(ifc, unit); 184 device_printf(ubus->parent, "usbpf: Could not allocate " 185 "instance\n"); 186 return (error); 187 } 188 ifp = ubus->ifp = if_alloc(IFT_USB); 189 if (ifp == NULL) { 190 ifc_free_unit(ifc, unit); 191 device_printf(ubus->parent, "usbpf: Could not allocate " 192 "instance\n"); 193 return (ENOSPC); 194 } 195 strlcpy(ifp->if_xname, name, sizeof(ifp->if_xname)); 196 ifp->if_softc = ubus; 197 ifp->if_dname = ifc->ifc_name; 198 ifp->if_dunit = unit; 199 ifp->if_ioctl = usbpf_ioctl; 200 if_attach(ifp); 201 ifp->if_flags |= IFF_UP; 202 rt_ifmsg(ifp); 203 /* 204 * XXX According to the specification of DLT_USB, it indicates 205 * packets beginning with USB setup header. But not sure all 206 * packets would be. 207 */ 208 bpfattach(ifp, DLT_USB, USBPF_HDR_LEN); 209 210 return (0); 211 } 212 213 static int 214 usbpf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 215 { 216 struct usb_bus *ubus; 217 int unit; 218 219 ubus = ifp->if_softc; 220 unit = ifp->if_dunit; 221 222 ubus->ifp = NULL; 223 bpfdetach(ifp); 224 if_detach(ifp); 225 if_free(ifp); 226 ifc_free_unit(ifc, unit); 227 228 return (0); 229 } 230 231 void 232 usbpf_attach(struct usb_bus *ubus) 233 { 234 235 if (bootverbose) 236 device_printf(ubus->parent, "usbpf: Attached\n"); 237 } 238 239 void 240 usbpf_detach(struct usb_bus *ubus) 241 { 242 243 if (ubus->ifp != NULL) 244 usbpf_clone_destroy(&usbpf_cloner, ubus->ifp); 245 if (bootverbose) 246 device_printf(ubus->parent, "usbpf: Detached\n"); 247 } 248 249 static uint32_t 250 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags) 251 { 252 uint32_t val = 0; 253 254 if (flags->force_short_xfer == 1) 255 val |= USBPF_FLAG_FORCE_SHORT_XFER; 256 if (flags->short_xfer_ok == 1) 257 val |= USBPF_FLAG_SHORT_XFER_OK; 258 if (flags->short_frames_ok == 1) 259 val |= USBPF_FLAG_SHORT_FRAMES_OK; 260 if (flags->pipe_bof == 1) 261 val |= USBPF_FLAG_PIPE_BOF; 262 if (flags->proxy_buffer == 1) 263 val |= USBPF_FLAG_PROXY_BUFFER; 264 if (flags->ext_buffer == 1) 265 val |= USBPF_FLAG_EXT_BUFFER; 266 if (flags->manual_status == 1) 267 val |= USBPF_FLAG_MANUAL_STATUS; 268 if (flags->no_pipe_ok == 1) 269 val |= USBPF_FLAG_NO_PIPE_OK; 270 if (flags->stall_pipe == 1) 271 val |= USBPF_FLAG_STALL_PIPE; 272 return (val); 273 } 274 275 static uint32_t 276 usbpf_aggregate_status(struct usb_xfer_flags_int *flags) 277 { 278 uint32_t val = 0; 279 280 if (flags->open == 1) 281 val |= USBPF_STATUS_OPEN; 282 if (flags->transferring == 1) 283 val |= USBPF_STATUS_TRANSFERRING; 284 if (flags->did_dma_delay == 1) 285 val |= USBPF_STATUS_DID_DMA_DELAY; 286 if (flags->did_close == 1) 287 val |= USBPF_STATUS_DID_CLOSE; 288 if (flags->draining == 1) 289 val |= USBPF_STATUS_DRAINING; 290 if (flags->started == 1) 291 val |= USBPF_STATUS_STARTED; 292 if (flags->bandwidth_reclaimed == 1) 293 val |= USBPF_STATUS_BW_RECLAIMED; 294 if (flags->control_xfr == 1) 295 val |= USBPF_STATUS_CONTROL_XFR; 296 if (flags->control_hdr == 1) 297 val |= USBPF_STATUS_CONTROL_HDR; 298 if (flags->control_act == 1) 299 val |= USBPF_STATUS_CONTROL_ACT; 300 if (flags->control_stall == 1) 301 val |= USBPF_STATUS_CONTROL_STALL; 302 if (flags->short_frames_ok == 1) 303 val |= USBPF_STATUS_SHORT_FRAMES_OK; 304 if (flags->short_xfer_ok == 1) 305 val |= USBPF_STATUS_SHORT_XFER_OK; 306 #if USB_HAVE_BUSDMA 307 if (flags->bdma_enable == 1) 308 val |= USBPF_STATUS_BDMA_ENABLE; 309 if (flags->bdma_no_post_sync == 1) 310 val |= USBPF_STATUS_BDMA_NO_POST_SYNC; 311 if (flags->bdma_setup == 1) 312 val |= USBPF_STATUS_BDMA_SETUP; 313 #endif 314 if (flags->isochronous_xfr == 1) 315 val |= USBPF_STATUS_ISOCHRONOUS_XFR; 316 if (flags->curr_dma_set == 1) 317 val |= USBPF_STATUS_CURR_DMA_SET; 318 if (flags->can_cancel_immed == 1) 319 val |= USBPF_STATUS_CAN_CANCEL_IMMED; 320 if (flags->doing_callback == 1) 321 val |= USBPF_STATUS_DOING_CALLBACK; 322 323 return (val); 324 } 325 326 static int 327 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame) 328 { 329 int isread; 330 331 if ((frame == 0) && (xfer->flags_int.control_xfr != 0) && 332 (xfer->flags_int.control_hdr != 0)) { 333 /* special case */ 334 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 335 /* The device controller writes to memory */ 336 isread = 1; 337 } else { 338 /* The host controller reads from memory */ 339 isread = 0; 340 } 341 } else { 342 isread = USB_GET_DATA_ISREAD(xfer); 343 } 344 return (isread); 345 } 346 347 static uint32_t 348 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type) 349 { 350 uint32_t totlen; 351 uint32_t x; 352 uint32_t nframes; 353 354 if (type == USBPF_XFERTAP_SUBMIT) 355 nframes = xfer->nframes; 356 else 357 nframes = xfer->aframes; 358 359 totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes); 360 361 /* precompute all trace lengths */ 362 for (x = 0; x != nframes; x++) { 363 if (usbpf_xfer_frame_is_read(xfer, x)) { 364 if (type != USBPF_XFERTAP_SUBMIT) { 365 totlen += USBPF_FRAME_ALIGN( 366 xfer->frlengths[x]); 367 } 368 } else { 369 if (type == USBPF_XFERTAP_SUBMIT) { 370 totlen += USBPF_FRAME_ALIGN( 371 xfer->frlengths[x]); 372 } 373 } 374 } 375 return (totlen); 376 } 377 378 void 379 usbpf_xfertap(struct usb_xfer *xfer, int type) 380 { 381 struct usb_bus *bus; 382 struct usbpf_pkthdr *up; 383 struct usbpf_framehdr *uf; 384 usb_frlength_t offset; 385 uint32_t totlen; 386 uint32_t frame; 387 uint32_t temp; 388 uint32_t nframes; 389 uint32_t x; 390 uint8_t *buf; 391 uint8_t *ptr; 392 393 bus = xfer->xroot->bus; 394 395 /* sanity checks */ 396 if (bus->ifp == NULL) 397 return; 398 if (!bpf_peers_present(bus->ifp->if_bpf)) 399 return; 400 401 totlen = usbpf_xfer_precompute_size(xfer, type); 402 403 if (type == USBPF_XFERTAP_SUBMIT) 404 nframes = xfer->nframes; 405 else 406 nframes = xfer->aframes; 407 408 /* 409 * XXX TODO XXX 410 * 411 * When BPF supports it we could pass a fragmented array of 412 * buffers avoiding the data copy operation here. 413 */ 414 buf = ptr = malloc(totlen, M_TEMP, M_NOWAIT); 415 if (buf == NULL) { 416 device_printf(bus->parent, "usbpf: Out of memory\n"); 417 return; 418 } 419 420 up = (struct usbpf_pkthdr *)ptr; 421 ptr += USBPF_HDR_LEN; 422 423 /* fill out header */ 424 temp = device_get_unit(bus->bdev); 425 up->up_totlen = htole32(totlen); 426 up->up_busunit = htole32(temp); 427 up->up_address = xfer->xroot->udev->device_index; 428 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) 429 up->up_mode = USBPF_MODE_DEVICE; 430 else 431 up->up_mode = USBPF_MODE_HOST; 432 up->up_type = type; 433 up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE; 434 temp = usbpf_aggregate_xferflags(&xfer->flags); 435 up->up_flags = htole32(temp); 436 temp = usbpf_aggregate_status(&xfer->flags_int); 437 up->up_status = htole32(temp); 438 temp = xfer->error; 439 up->up_error = htole32(temp); 440 temp = xfer->interval; 441 up->up_interval = htole32(temp); 442 up->up_frames = htole32(nframes); 443 temp = xfer->max_packet_size; 444 up->up_packet_size = htole32(temp); 445 temp = xfer->max_packet_count; 446 up->up_packet_count = htole32(temp); 447 temp = xfer->endpointno; 448 up->up_endpoint = htole32(temp); 449 up->up_speed = xfer->xroot->udev->speed; 450 451 /* clear reserved area */ 452 memset(up->up_reserved, 0, sizeof(up->up_reserved)); 453 454 /* init offset and frame */ 455 offset = 0; 456 frame = 0; 457 458 /* iterate all the USB frames and copy data, if any */ 459 for (x = 0; x != nframes; x++) { 460 uint32_t length; 461 int isread; 462 463 /* get length */ 464 length = xfer->frlengths[x]; 465 466 /* get frame header pointer */ 467 uf = (struct usbpf_framehdr *)ptr; 468 ptr += USBPF_FRAME_HDR_LEN; 469 470 /* fill out packet header */ 471 uf->length = htole32(length); 472 uf->flags = 0; 473 474 /* get information about data read/write */ 475 isread = usbpf_xfer_frame_is_read(xfer, x); 476 477 /* check if we need to copy any data */ 478 if (isread) { 479 if (type == USBPF_XFERTAP_SUBMIT) 480 length = 0; 481 else { 482 uf->flags |= htole32( 483 USBPF_FRAMEFLAG_DATA_FOLLOWS); 484 } 485 } else { 486 if (type != USBPF_XFERTAP_SUBMIT) 487 length = 0; 488 else { 489 uf->flags |= htole32( 490 USBPF_FRAMEFLAG_DATA_FOLLOWS); 491 } 492 } 493 494 /* check if data is read direction */ 495 if (isread) 496 uf->flags |= htole32(USBPF_FRAMEFLAG_READ); 497 498 /* copy USB data, if any */ 499 if (length != 0) { 500 /* copy data */ 501 usbd_copy_out(&xfer->frbuffers[frame], 502 offset, ptr, length); 503 504 /* align length */ 505 temp = USBPF_FRAME_ALIGN(length); 506 507 /* zero pad */ 508 if (temp != length) 509 memset(ptr + length, 0, temp - length); 510 511 ptr += temp; 512 } 513 514 if (xfer->flags_int.isochronous_xfr) { 515 offset += usbd_xfer_old_frame_length(xfer, x); 516 } else { 517 frame ++; 518 } 519 } 520 521 bpf_tap(bus->ifp->if_bpf, buf, totlen); 522 523 free(buf, M_TEMP); 524 } 525