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