The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/usb_pf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 01cf9a52aa7986a115c7f55d909eab89


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.