xref: /freebsd/sys/dev/usb/usb_pf.c (revision 145992504973bd16cf3518af9ba5ce185fefa82a)
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 static void usbpf_init(void);
64 static void usbpf_uninit(void);
65 static int usbpf_ioctl(struct ifnet *, u_long, caddr_t);
66 static int usbpf_clone_match(struct if_clone *, const char *);
67 static int usbpf_clone_create(struct if_clone *, char *, size_t, caddr_t);
68 static int usbpf_clone_destroy(struct if_clone *, struct ifnet *);
69 static struct usb_bus *usbpf_ifname2ubus(const char *);
70 static uint32_t usbpf_aggregate_xferflags(struct usb_xfer_flags *);
71 static uint32_t usbpf_aggregate_status(struct usb_xfer_flags_int *);
72 static int usbpf_xfer_frame_is_read(struct usb_xfer *, uint32_t);
73 static uint32_t usbpf_xfer_precompute_size(struct usb_xfer *, int);
74 
75 static struct if_clone *usbpf_cloner;
76 static const char usbusname[] = "usbus";
77 
78 SYSINIT(usbpf_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_init, NULL);
79 SYSUNINIT(usbpf_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, usbpf_uninit, NULL);
80 
81 static void
82 usbpf_init(void)
83 {
84 
85 	usbpf_cloner = if_clone_advanced(usbusname, 0, usbpf_clone_match,
86 	    usbpf_clone_create, usbpf_clone_destroy);
87 }
88 
89 static void
90 usbpf_uninit(void)
91 {
92 	int devlcnt;
93 	device_t *devlp;
94 	devclass_t dc;
95 	struct usb_bus *ubus;
96 	int error;
97 	int i;
98 
99 	if_clone_detach(usbpf_cloner);
100 
101 	dc = devclass_find(usbusname);
102 	if (dc == NULL)
103 		return;
104 	error = devclass_get_devices(dc, &devlp, &devlcnt);
105 	if (error)
106 		return;
107 	for (i = 0; i < devlcnt; i++) {
108 		ubus = device_get_softc(devlp[i]);
109 		if (ubus != NULL && ubus->ifp != NULL)
110 			usbpf_clone_destroy(usbpf_cloner, ubus->ifp);
111 	}
112 	free(devlp, M_TEMP);
113 }
114 
115 static int
116 usbpf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
117 {
118 
119 	/* No configuration allowed. */
120 	return (EINVAL);
121 }
122 
123 static struct usb_bus *
124 usbpf_ifname2ubus(const char *ifname)
125 {
126 	device_t dev;
127 	devclass_t dc;
128 	int unit;
129 	int error;
130 
131 	if (strncmp(ifname, usbusname, sizeof(usbusname) - 1) != 0)
132 		return (NULL);
133 	error = ifc_name2unit(ifname, &unit);
134 	if (error || unit < 0)
135 		return (NULL);
136 	dc = devclass_find(usbusname);
137 	if (dc == NULL)
138 		return (NULL);
139 	dev = devclass_get_device(dc, unit);
140 	if (dev == NULL)
141 		return (NULL);
142 
143 	return (device_get_softc(dev));
144 }
145 
146 static int
147 usbpf_clone_match(struct if_clone *ifc, const char *name)
148 {
149 	struct usb_bus *ubus;
150 
151 	ubus = usbpf_ifname2ubus(name);
152 	if (ubus == NULL)
153 		return (0);
154 	if (ubus->ifp != NULL)
155 		return (0);
156 
157 	return (1);
158 }
159 
160 static int
161 usbpf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
162 {
163 	int error;
164 	int unit;
165 	struct ifnet *ifp;
166 	struct usb_bus *ubus;
167 
168 	error = ifc_name2unit(name, &unit);
169 	if (error)
170 		return (error);
171  	if (unit < 0)
172 		return (EINVAL);
173 
174 	ubus = usbpf_ifname2ubus(name);
175 	if (ubus == NULL)
176 		return (1);
177 	if (ubus->ifp != NULL)
178 		return (1);
179 
180 	error = ifc_alloc_unit(ifc, &unit);
181 	if (error) {
182 		ifc_free_unit(ifc, unit);
183 		device_printf(ubus->parent, "usbpf: Could not allocate "
184 		    "instance\n");
185 		return (error);
186 	}
187 	ifp = ubus->ifp = if_alloc(IFT_USB);
188 	if (ifp == NULL) {
189 		ifc_free_unit(ifc, unit);
190 		device_printf(ubus->parent, "usbpf: Could not allocate "
191 		    "instance\n");
192 		return (ENOSPC);
193 	}
194 	strlcpy(ifp->if_xname, name, sizeof(ifp->if_xname));
195 	ifp->if_softc = ubus;
196 	ifp->if_dname = usbusname;
197 	ifp->if_dunit = unit;
198 	ifp->if_ioctl = usbpf_ioctl;
199 	if_attach(ifp);
200 	ifp->if_flags |= IFF_UP;
201 	rt_ifmsg(ifp);
202 	/*
203 	 * XXX According to the specification of DLT_USB, it indicates
204 	 * packets beginning with USB setup header. But not sure all
205 	 * packets would be.
206 	 */
207 	bpfattach(ifp, DLT_USB, USBPF_HDR_LEN);
208 
209 	return (0);
210 }
211 
212 static int
213 usbpf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
214 {
215 	struct usb_bus *ubus;
216 	int unit;
217 
218 	ubus = ifp->if_softc;
219 	unit = ifp->if_dunit;
220 
221 	ubus->ifp = NULL;
222 	bpfdetach(ifp);
223 	if_detach(ifp);
224 	if_free(ifp);
225 	ifc_free_unit(ifc, unit);
226 
227 	return (0);
228 }
229 
230 void
231 usbpf_attach(struct usb_bus *ubus)
232 {
233 
234 	if (bootverbose)
235 		device_printf(ubus->parent, "usbpf: Attached\n");
236 }
237 
238 void
239 usbpf_detach(struct usb_bus *ubus)
240 {
241 
242 	if (ubus->ifp != NULL)
243 		usbpf_clone_destroy(usbpf_cloner, ubus->ifp);
244 	if (bootverbose)
245 		device_printf(ubus->parent, "usbpf: Detached\n");
246 }
247 
248 static uint32_t
249 usbpf_aggregate_xferflags(struct usb_xfer_flags *flags)
250 {
251 	uint32_t val = 0;
252 
253 	if (flags->force_short_xfer == 1)
254 		val |= USBPF_FLAG_FORCE_SHORT_XFER;
255 	if (flags->short_xfer_ok == 1)
256 		val |= USBPF_FLAG_SHORT_XFER_OK;
257 	if (flags->short_frames_ok == 1)
258 		val |= USBPF_FLAG_SHORT_FRAMES_OK;
259 	if (flags->pipe_bof == 1)
260 		val |= USBPF_FLAG_PIPE_BOF;
261 	if (flags->proxy_buffer == 1)
262 		val |= USBPF_FLAG_PROXY_BUFFER;
263 	if (flags->ext_buffer == 1)
264 		val |= USBPF_FLAG_EXT_BUFFER;
265 	if (flags->manual_status == 1)
266 		val |= USBPF_FLAG_MANUAL_STATUS;
267 	if (flags->no_pipe_ok == 1)
268 		val |= USBPF_FLAG_NO_PIPE_OK;
269 	if (flags->stall_pipe == 1)
270 		val |= USBPF_FLAG_STALL_PIPE;
271 	return (val);
272 }
273 
274 static uint32_t
275 usbpf_aggregate_status(struct usb_xfer_flags_int *flags)
276 {
277 	uint32_t val = 0;
278 
279 	if (flags->open == 1)
280 		val |= USBPF_STATUS_OPEN;
281 	if (flags->transferring == 1)
282 		val |= USBPF_STATUS_TRANSFERRING;
283 	if (flags->did_dma_delay == 1)
284 		val |= USBPF_STATUS_DID_DMA_DELAY;
285 	if (flags->did_close == 1)
286 		val |= USBPF_STATUS_DID_CLOSE;
287 	if (flags->draining == 1)
288 		val |= USBPF_STATUS_DRAINING;
289 	if (flags->started == 1)
290 		val |= USBPF_STATUS_STARTED;
291 	if (flags->bandwidth_reclaimed == 1)
292 		val |= USBPF_STATUS_BW_RECLAIMED;
293 	if (flags->control_xfr == 1)
294 		val |= USBPF_STATUS_CONTROL_XFR;
295 	if (flags->control_hdr == 1)
296 		val |= USBPF_STATUS_CONTROL_HDR;
297 	if (flags->control_act == 1)
298 		val |= USBPF_STATUS_CONTROL_ACT;
299 	if (flags->control_stall == 1)
300 		val |= USBPF_STATUS_CONTROL_STALL;
301 	if (flags->short_frames_ok == 1)
302 		val |= USBPF_STATUS_SHORT_FRAMES_OK;
303 	if (flags->short_xfer_ok == 1)
304 		val |= USBPF_STATUS_SHORT_XFER_OK;
305 #if USB_HAVE_BUSDMA
306 	if (flags->bdma_enable == 1)
307 		val |= USBPF_STATUS_BDMA_ENABLE;
308 	if (flags->bdma_no_post_sync == 1)
309 		val |= USBPF_STATUS_BDMA_NO_POST_SYNC;
310 	if (flags->bdma_setup == 1)
311 		val |= USBPF_STATUS_BDMA_SETUP;
312 #endif
313 	if (flags->isochronous_xfr == 1)
314 		val |= USBPF_STATUS_ISOCHRONOUS_XFR;
315 	if (flags->curr_dma_set == 1)
316 		val |= USBPF_STATUS_CURR_DMA_SET;
317 	if (flags->can_cancel_immed == 1)
318 		val |= USBPF_STATUS_CAN_CANCEL_IMMED;
319 	if (flags->doing_callback == 1)
320 		val |= USBPF_STATUS_DOING_CALLBACK;
321 
322 	return (val);
323 }
324 
325 static int
326 usbpf_xfer_frame_is_read(struct usb_xfer *xfer, uint32_t frame)
327 {
328 	int isread;
329 
330 	if ((frame == 0) && (xfer->flags_int.control_xfr != 0) &&
331 	    (xfer->flags_int.control_hdr != 0)) {
332 		/* special case */
333 		if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
334 			/* The device controller writes to memory */
335 			isread = 1;
336 		} else {
337 			/* The host controller reads from memory */
338 			isread = 0;
339 		}
340 	} else {
341 		isread = USB_GET_DATA_ISREAD(xfer);
342 	}
343 	return (isread);
344 }
345 
346 static uint32_t
347 usbpf_xfer_precompute_size(struct usb_xfer *xfer, int type)
348 {
349 	uint32_t totlen;
350 	uint32_t x;
351 	uint32_t nframes;
352 
353 	if (type == USBPF_XFERTAP_SUBMIT)
354 		nframes = xfer->nframes;
355 	else
356 		nframes = xfer->aframes;
357 
358 	totlen = USBPF_HDR_LEN + (USBPF_FRAME_HDR_LEN * nframes);
359 
360 	/* precompute all trace lengths */
361 	for (x = 0; x != nframes; x++) {
362 		if (usbpf_xfer_frame_is_read(xfer, x)) {
363 			if (type != USBPF_XFERTAP_SUBMIT) {
364 				totlen += USBPF_FRAME_ALIGN(
365 				    xfer->frlengths[x]);
366 			}
367 		} else {
368 			if (type == USBPF_XFERTAP_SUBMIT) {
369 				totlen += USBPF_FRAME_ALIGN(
370 				    xfer->frlengths[x]);
371 			}
372 		}
373 	}
374 	return (totlen);
375 }
376 
377 void
378 usbpf_xfertap(struct usb_xfer *xfer, int type)
379 {
380 	struct usb_bus *bus;
381 	struct usbpf_pkthdr *up;
382 	struct usbpf_framehdr *uf;
383 	usb_frlength_t offset;
384 	uint32_t totlen;
385 	uint32_t frame;
386 	uint32_t temp;
387 	uint32_t nframes;
388 	uint32_t x;
389 	uint8_t *buf;
390 	uint8_t *ptr;
391 
392 	bus = xfer->xroot->bus;
393 
394 	/* sanity checks */
395 	if (bus->ifp == NULL)
396 		return;
397 	if (!bpf_peers_present(bus->ifp->if_bpf))
398 		return;
399 
400 	totlen = usbpf_xfer_precompute_size(xfer, type);
401 
402 	if (type == USBPF_XFERTAP_SUBMIT)
403 		nframes = xfer->nframes;
404 	else
405 		nframes = xfer->aframes;
406 
407 	/*
408 	 * XXX TODO XXX
409 	 *
410 	 * When BPF supports it we could pass a fragmented array of
411 	 * buffers avoiding the data copy operation here.
412 	 */
413 	buf = ptr = malloc(totlen, M_TEMP, M_NOWAIT);
414 	if (buf == NULL) {
415 		device_printf(bus->parent, "usbpf: Out of memory\n");
416 		return;
417 	}
418 
419 	up = (struct usbpf_pkthdr *)ptr;
420 	ptr += USBPF_HDR_LEN;
421 
422 	/* fill out header */
423 	temp = device_get_unit(bus->bdev);
424 	up->up_totlen = htole32(totlen);
425 	up->up_busunit = htole32(temp);
426 	up->up_address = xfer->xroot->udev->device_index;
427 	if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
428 		up->up_mode = USBPF_MODE_DEVICE;
429 	else
430 		up->up_mode = USBPF_MODE_HOST;
431 	up->up_type = type;
432 	up->up_xfertype = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
433 	temp = usbpf_aggregate_xferflags(&xfer->flags);
434 	up->up_flags = htole32(temp);
435 	temp = usbpf_aggregate_status(&xfer->flags_int);
436 	up->up_status = htole32(temp);
437 	temp = xfer->error;
438 	up->up_error = htole32(temp);
439 	temp = xfer->interval;
440 	up->up_interval = htole32(temp);
441 	up->up_frames = htole32(nframes);
442 	temp = xfer->max_packet_size;
443 	up->up_packet_size = htole32(temp);
444 	temp = xfer->max_packet_count;
445 	up->up_packet_count = htole32(temp);
446 	temp = xfer->endpointno;
447 	up->up_endpoint = htole32(temp);
448 	up->up_speed = xfer->xroot->udev->speed;
449 
450 	/* clear reserved area */
451 	memset(up->up_reserved, 0, sizeof(up->up_reserved));
452 
453 	/* init offset and frame */
454 	offset = 0;
455 	frame = 0;
456 
457 	/* iterate all the USB frames and copy data, if any */
458 	for (x = 0; x != nframes; x++) {
459 		uint32_t length;
460 		int isread;
461 
462 		/* get length */
463 		length = xfer->frlengths[x];
464 
465 		/* get frame header pointer */
466 		uf = (struct usbpf_framehdr *)ptr;
467 		ptr += USBPF_FRAME_HDR_LEN;
468 
469 		/* fill out packet header */
470 		uf->length = htole32(length);
471 		uf->flags = 0;
472 
473 		/* get information about data read/write */
474 		isread = usbpf_xfer_frame_is_read(xfer, x);
475 
476 		/* check if we need to copy any data */
477 		if (isread) {
478 			if (type == USBPF_XFERTAP_SUBMIT)
479 				length = 0;
480 			else {
481 				uf->flags |= htole32(
482 				    USBPF_FRAMEFLAG_DATA_FOLLOWS);
483 			}
484 		} else {
485 			if (type != USBPF_XFERTAP_SUBMIT)
486 				length = 0;
487 			else {
488 				uf->flags |= htole32(
489 				    USBPF_FRAMEFLAG_DATA_FOLLOWS);
490 			}
491 		}
492 
493 		/* check if data is read direction */
494 		if (isread)
495 			uf->flags |= htole32(USBPF_FRAMEFLAG_READ);
496 
497 		/* copy USB data, if any */
498 		if (length != 0) {
499 			/* copy data */
500 			usbd_copy_out(&xfer->frbuffers[frame],
501 			    offset, ptr, length);
502 
503 			/* align length */
504 			temp = USBPF_FRAME_ALIGN(length);
505 
506 			/* zero pad */
507 			if (temp != length)
508 				memset(ptr + length, 0, temp - length);
509 
510 			ptr += temp;
511 		}
512 
513 		if (xfer->flags_int.isochronous_xfr) {
514 			offset += usbd_xfer_old_frame_length(xfer, x);
515 		} else {
516 			frame ++;
517 		}
518 	}
519 
520 	bpf_tap(bus->ifp->if_bpf, buf, totlen);
521 
522 	free(buf, M_TEMP);
523 }
524