]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/usb_pf.c
MFC r286799:
[FreeBSD/stable/8.git] / sys / dev / usb / usb_pf.c
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/bpf.h>
48 #include <sys/sysctl.h>
49
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usb_busdma.h>
53 #include <dev/usb/usb_controller.h>
54 #include <dev/usb/usb_core.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_device.h>
57 #include <dev/usb/usb_bus.h>
58 #include <dev/usb/usb_pf.h>
59 #include <dev/usb/usb_transfer.h>
60
61 static int usb_no_pf;
62
63 SYSCTL_INT(_hw_usb, OID_AUTO, no_pf, CTLFLAG_RW,
64     &usb_no_pf, 0, "Set to disable USB packet filtering");
65
66 TUNABLE_INT("hw.usb.no_pf", &usb_no_pf);
67
68 void
69 usbpf_attach(struct usb_bus *ubus)
70 {
71         struct ifnet *ifp;
72
73         if (usb_no_pf != 0) {
74                 ubus->ifp = NULL;
75                 return;
76         }
77
78         ifp = ubus->ifp = if_alloc(IFT_USB);
79         if (ifp == NULL) {
80                 device_printf(ubus->parent, "usbpf: Could not allocate "
81                     "instance\n");
82                 return;
83         }
84
85         if_initname(ifp, "usbus", device_get_unit(ubus->bdev));
86         ifp->if_flags = IFF_CANTCONFIG;
87         if_attach(ifp);
88         if_up(ifp);
89
90         /*
91          * XXX According to the specification of DLT_USB, it indicates
92          * packets beginning with USB setup header. But not sure all
93          * packets would be.
94          */
95         bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
96
97         if (bootverbose)
98                 device_printf(ubus->parent, "usbpf: Attached\n");
99 }
100
101 void
102 usbpf_detach(struct usb_bus *ubus)
103 {
104         struct ifnet *ifp = ubus->ifp;
105
106         USB_BUS_LOCK(ubus);
107         ubus->ifp = NULL;
108         USB_BUS_UNLOCK(ubus);
109
110         if (ifp != NULL) {
111                 bpfdetach(ifp);
112                 if_down(ifp);
113                 if_detach(ifp);
114                 if_free(ifp);
115         }
116 }
117
118 static uint32_t
119 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
120 {
121         uint32_t val = 0;
122
123         if (flags->force_short_xfer == 1)
124                 val |= USBPF_FLAG_FORCE_SHORT_XFER;
125         if (flags->short_xfer_ok == 1)
126                 val |= USBPF_FLAG_SHORT_XFER_OK;
127         if (flags->short_frames_ok == 1)
128                 val |= USBPF_FLAG_SHORT_FRAMES_OK;
129         if (flags->pipe_bof == 1)
130                 val |= USBPF_FLAG_PIPE_BOF;
131         if (flags->proxy_buffer == 1)
132                 val |= USBPF_FLAG_PROXY_BUFFER;
133         if (flags->ext_buffer == 1)
134                 val |= USBPF_FLAG_EXT_BUFFER;
135         if (flags->manual_status == 1)
136                 val |= USBPF_FLAG_MANUAL_STATUS;
137         if (flags->no_pipe_ok == 1)
138                 val |= USBPF_FLAG_NO_PIPE_OK;
139         if (flags->stall_pipe == 1)
140                 val |= USBPF_FLAG_STALL_PIPE;
141         return (val);
142 }
143
144 static uint32_t
145 usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
146 {
147         uint32_t val = 0;
148
149         if (flags->open == 1)
150                 val |= USBPF_STATUS_OPEN;
151         if (flags->transferring == 1)
152                 val |= USBPF_STATUS_TRANSFERRING;
153         if (flags->did_dma_delay == 1)
154                 val |= USBPF_STATUS_DID_DMA_DELAY;
155         if (flags->did_close == 1)
156                 val |= USBPF_STATUS_DID_CLOSE;
157         if (flags->draining == 1)
158                 val |= USBPF_STATUS_DRAINING;
159         if (flags->started == 1)
160                 val |= USBPF_STATUS_STARTED;
161         if (flags->bandwidth_reclaimed == 1)
162                 val |= USBPF_STATUS_BW_RECLAIMED;
163         if (flags->control_xfr == 1)
164                 val |= USBPF_STATUS_CONTROL_XFR;
165         if (flags->control_hdr == 1)
166                 val |= USBPF_STATUS_CONTROL_HDR;
167         if (flags->control_act == 1)
168                 val |= USBPF_STATUS_CONTROL_ACT;
169         if (flags->control_stall == 1)
170                 val |= USBPF_STATUS_CONTROL_STALL;
171         if (flags->short_frames_ok == 1)
172                 val |= USBPF_STATUS_SHORT_FRAMES_OK;
173         if (flags->short_xfer_ok == 1)
174                 val |= USBPF_STATUS_SHORT_XFER_OK;
175 #if USB_HAVE_BUSDMA
176         if (flags->bdma_enable == 1)
177                 val |= USBPF_STATUS_BDMA_ENABLE;
178         if (flags->bdma_no_post_sync == 1)
179                 val |= USBPF_STATUS_BDMA_NO_POST_SYNC;
180         if (flags->bdma_setup == 1)
181                 val |= USBPF_STATUS_BDMA_SETUP;
182 #endif
183         if (flags->isochronous_xfr == 1)
184                 val |= USBPF_STATUS_ISOCHRONOUS_XFR;
185         if (flags->curr_dma_set == 1)
186                 val |= USBPF_STATUS_CURR_DMA_SET;
187         if (flags->can_cancel_immed == 1)
188                 val |= USBPF_STATUS_CAN_CANCEL_IMMED;
189         if (flags->doing_callback == 1)
190                 val |= USBPF_STATUS_DOING_CALLBACK;
191
192         return (val);
193 }
194
195 static int
196 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame)
197 {
198         int isread;
199
200         if ((frame == 0) && (xfer->flags_int.control_xfr != 0) &&
201             (xfer->flags_int.control_hdr != 0)) {
202                 /* special case */
203                 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
204                         /* The device controller writes to memory */
205                         isread = 1;
206                 } else {
207                         /* The host controller reads from memory */
208                         isread = 0;
209                 }
210         } else {
211                 isread = USB_GET_DATA_ISREAD(xfer);
212         }
213         return (isread);
214 }
215
216 static uint32_t
217 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type)
218 {
219         uint32_t totlen;
220         uint32_t x;
221         uint32_t nframes;
222
223         if (type == USBPF_XFERTAP_SUBMIT)
224                 nframes = xfer->nframes;
225         else
226                 nframes = xfer->aframes;
227
228         totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes);
229
230         /* precompute all trace lengths */
231         for (x = 0; x != nframes; x++) {
232                 if (usbpf_xfer_frame_is_read(xfer, x)) {
233                         if (type != USBPF_XFERTAP_SUBMIT) {
234                                 totlen += USBPF_FRAME_ALIGN(
235                                     xfer->frlengths[x]);
236                         }
237                 } else {
238                         if (type == USBPF_XFERTAP_SUBMIT) {
239                                 totlen += USBPF_FRAME_ALIGN(
240                                     xfer->frlengths[x]);
241                         }
242                 }
243         }
244         return (totlen);
245 }
246
247 void
248 usbpf_xfertap(struct usb_xfer *xfer, int type)
249 {
250         struct usb_bus *bus;
251         struct usbpf_pkthdr *up;
252         struct usbpf_framehdr *uf;
253         usb_frlength_t offset;
254         uint32_t totlen;
255         uint32_t frame;
256         uint32_t temp;
257         uint32_t nframes;
258         uint32_t x;
259         uint8_t *buf;
260         uint8_t *ptr;
261
262         bus = xfer->xroot->bus;
263
264         /* sanity checks */
265         if (usb_no_pf != 0)
266                 return;
267         if (bus->ifp == NULL || bus->ifp->if_bpf == NULL)
268                 return;
269         if (!bpf_peers_present(bus->ifp->if_bpf))
270                 return;
271
272         totlen = usbpf_xfer_precompute_size(xfer, type);
273
274         if (type == USBPF_XFERTAP_SUBMIT)
275                 nframes = xfer->nframes;
276         else
277                 nframes = xfer->aframes;
278
279         /*
280          * XXX TODO XXX
281          *
282          * When BPF supports it we could pass a fragmented array of
283          * buffers avoiding the data copy operation here.
284          */
285         buf = ptr = malloc(totlen, M_TEMP, M_NOWAIT);
286         if (buf == NULL) {
287                 device_printf(bus->parent, "usbpf: Out of memory\n");
288                 return;
289         }
290
291         up = (struct usbpf_pkthdr *)ptr;
292         ptr += USBPF_HDR_LEN;
293
294         /* fill out header */
295         temp = device_get_unit(bus->bdev);
296         up->up_totlen = htole32(totlen);
297         up->up_busunit = htole32(temp);
298         up->up_address = xfer->xroot->udev->device_index;
299         if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
300                 up->up_mode = USBPF_MODE_DEVICE;
301         else
302                 up->up_mode = USBPF_MODE_HOST;
303         up->up_type = type;
304         up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
305         temp = usbpf_aggregate_xferflags(&xfer->flags);
306         up->up_flags = htole32(temp);
307         temp = usbpf_aggregate_status(&xfer->flags_int);
308         up->up_status = htole32(temp);
309         temp = xfer->error;
310         up->up_error = htole32(temp);
311         temp = xfer->interval;
312         up->up_interval = htole32(temp);
313         up->up_frames = htole32(nframes);
314         temp = xfer->max_packet_size;
315         up->up_packet_size = htole32(temp);
316         temp = xfer->max_packet_count;
317         up->up_packet_count = htole32(temp);
318         temp = xfer->endpointno;
319         up->up_endpoint = htole32(temp);
320         up->up_speed = xfer->xroot->udev->speed;
321
322         /* clear reserved area */
323         memset(up->up_reserved, 0, sizeof(up->up_reserved));
324
325         /* init offset and frame */
326         offset = 0;
327         frame = 0;
328
329         /* iterate all the USB frames and copy data, if any */
330         for (x = 0; x != nframes; x++) {
331                 uint32_t length;
332                 int isread;
333
334                 /* get length */
335                 length = xfer->frlengths[x];
336
337                 /* get frame header pointer */
338                 uf = (struct usbpf_framehdr *)ptr;
339                 ptr += USBPF_FRAME_HDR_LEN;
340
341                 /* fill out packet header */
342                 uf->length = htole32(length);
343                 uf->flags = 0;
344
345                 /* get information about data read/write */
346                 isread = usbpf_xfer_frame_is_read(xfer, x);
347
348                 /* check if we need to copy any data */
349                 if (isread) {
350                         if (type == USBPF_XFERTAP_SUBMIT)
351                                 length = 0;
352                         else {
353                                 uf->flags |= htole32(
354                                     USBPF_FRAMEFLAG_DATA_FOLLOWS);
355                         }
356                 } else {
357                         if (type != USBPF_XFERTAP_SUBMIT)
358                                 length = 0;
359                         else {
360                                 uf->flags |= htole32(
361                                     USBPF_FRAMEFLAG_DATA_FOLLOWS);
362                         }
363                 }
364
365                 /* check if data is read direction */
366                 if (isread)
367                         uf->flags |= htole32(USBPF_FRAMEFLAG_READ);
368
369                 /* copy USB data, if any */
370                 if (length != 0) {
371                         /* copy data */
372                         usbd_copy_out(&xfer->frbuffers[frame],
373                             offset, ptr, length);
374
375                         /* align length */
376                         temp = USBPF_FRAME_ALIGN(length);
377
378                         /* zero pad */
379                         if (temp != length)
380                                 memset(ptr + length, 0, temp - length);
381
382                         ptr += temp;
383                 }
384
385                 if (xfer->flags_int.isochronous_xfr) {
386                         offset += usbd_xfer_old_frame_length(xfer, x);
387                 } else {
388                         frame ++;
389                 }
390         }
391
392         bpf_tap(bus->ifp->if_bpf, buf, totlen);
393
394         free(buf, M_TEMP);
395 }