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