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