| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: bpf.c,v 1.202 2016/07/17 02:49:52 pgoyette Exp $ */ | | 1 | /* $NetBSD: bpf.c,v 1.203 2016/07/19 02:47:45 pgoyette Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1990, 1991, 1993 | | 4 | * Copyright (c) 1990, 1991, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from the Stanford/CMU enet packet filter, | | 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 | | 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 | | 9 | * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence |
10 | * Berkeley Laboratory. | | 10 | * Berkeley Laboratory. |
11 | * | | 11 | * |
12 | * Redistribution and use in source and binary forms, with or without | | 12 | * Redistribution and use in source and binary forms, with or without |
13 | * modification, are permitted provided that the following conditions | | 13 | * modification, are permitted provided that the following conditions |
14 | * are met: | | 14 | * are met: |
| @@ -29,47 +29,46 @@ | | | @@ -29,47 +29,46 @@ |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 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 | | 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 | | 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 | | 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | * SUCH DAMAGE. | | 34 | * SUCH DAMAGE. |
35 | * | | 35 | * |
36 | * @(#)bpf.c 8.4 (Berkeley) 1/9/95 | | 36 | * @(#)bpf.c 8.4 (Berkeley) 1/9/95 |
37 | * static char rcsid[] = | | 37 | * static char rcsid[] = |
38 | * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp "; | | 38 | * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp "; |
39 | */ | | 39 | */ |
40 | | | 40 | |
41 | #include <sys/cdefs.h> | | 41 | #include <sys/cdefs.h> |
42 | __KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.202 2016/07/17 02:49:52 pgoyette Exp $"); | | 42 | __KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.203 2016/07/19 02:47:45 pgoyette Exp $"); |
43 | | | 43 | |
44 | #if defined(_KERNEL_OPT) | | 44 | #if defined(_KERNEL_OPT) |
45 | #include "opt_bpf.h" | | 45 | #include "opt_bpf.h" |
46 | #include "sl.h" | | 46 | #include "sl.h" |
47 | #include "strip.h" | | 47 | #include "strip.h" |
48 | #endif | | 48 | #endif |
49 | | | 49 | |
50 | #include <sys/param.h> | | 50 | #include <sys/param.h> |
51 | #include <sys/systm.h> | | 51 | #include <sys/systm.h> |
52 | #include <sys/mbuf.h> | | 52 | #include <sys/mbuf.h> |
53 | #include <sys/buf.h> | | 53 | #include <sys/buf.h> |
54 | #include <sys/time.h> | | 54 | #include <sys/time.h> |
55 | #include <sys/proc.h> | | 55 | #include <sys/proc.h> |
56 | #include <sys/ioctl.h> | | 56 | #include <sys/ioctl.h> |
57 | #include <sys/conf.h> | | 57 | #include <sys/conf.h> |
58 | #include <sys/vnode.h> | | 58 | #include <sys/vnode.h> |
59 | #include <sys/queue.h> | | 59 | #include <sys/queue.h> |
60 | #include <sys/stat.h> | | 60 | #include <sys/stat.h> |
61 | #include <sys/module.h> | | 61 | #include <sys/module.h> |
62 | #include <sys/once.h> | | | |
63 | #include <sys/atomic.h> | | 62 | #include <sys/atomic.h> |
64 | | | 63 | |
65 | #include <sys/file.h> | | 64 | #include <sys/file.h> |
66 | #include <sys/filedesc.h> | | 65 | #include <sys/filedesc.h> |
67 | #include <sys/tty.h> | | 66 | #include <sys/tty.h> |
68 | #include <sys/uio.h> | | 67 | #include <sys/uio.h> |
69 | | | 68 | |
70 | #include <sys/protosw.h> | | 69 | #include <sys/protosw.h> |
71 | #include <sys/socket.h> | | 70 | #include <sys/socket.h> |
72 | #include <sys/errno.h> | | 71 | #include <sys/errno.h> |
73 | #include <sys/kernel.h> | | 72 | #include <sys/kernel.h> |
74 | #include <sys/poll.h> | | 73 | #include <sys/poll.h> |
75 | #include <sys/sysctl.h> | | 74 | #include <sys/sysctl.h> |
| @@ -392,51 +391,50 @@ bpf_detachd(struct bpf_d *d) | | | @@ -392,51 +391,50 @@ bpf_detachd(struct bpf_d *d) |
392 | p = &(*p)->bd_next; | | 391 | p = &(*p)->bd_next; |
393 | if (*p == NULL) | | 392 | if (*p == NULL) |
394 | panic("%s: descriptor not in list", __func__); | | 393 | panic("%s: descriptor not in list", __func__); |
395 | } | | 394 | } |
396 | *p = (*p)->bd_next; | | 395 | *p = (*p)->bd_next; |
397 | if (bp->bif_dlist == NULL) | | 396 | if (bp->bif_dlist == NULL) |
398 | /* | | 397 | /* |
399 | * Let the driver know that there are no more listeners. | | 398 | * Let the driver know that there are no more listeners. |
400 | */ | | 399 | */ |
401 | *d->bd_bif->bif_driverp = NULL; | | 400 | *d->bd_bif->bif_driverp = NULL; |
402 | d->bd_bif = NULL; | | 401 | d->bd_bif = NULL; |
403 | } | | 402 | } |
404 | | | 403 | |
405 | static int | | 404 | static void |
406 | doinit(void) | | 405 | bpf_init(void) |
407 | { | | 406 | { |
408 | | | 407 | |
409 | mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE); | | 408 | mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE); |
410 | | | 409 | |
411 | LIST_INIT(&bpf_list); | | 410 | LIST_INIT(&bpf_list); |
412 | | | 411 | |
413 | bpf_gstats.bs_recv = 0; | | 412 | bpf_gstats.bs_recv = 0; |
414 | bpf_gstats.bs_drop = 0; | | 413 | bpf_gstats.bs_drop = 0; |
415 | bpf_gstats.bs_capt = 0; | | 414 | bpf_gstats.bs_capt = 0; |
416 | | | 415 | |
417 | return 0; | | 416 | return; |
418 | } | | 417 | } |
419 | | | 418 | |
420 | /* | | 419 | /* |
421 | * bpfilterattach() is called at boot time. | | 420 | * bpfilterattach() is called at boot time. We don't need to do anything |
| | | 421 | * here, since any initialization will happen as part of module init code. |
422 | */ | | 422 | */ |
423 | /* ARGSUSED */ | | 423 | /* ARGSUSED */ |
424 | void | | 424 | void |
425 | bpfilterattach(int n) | | 425 | bpfilterattach(int n) |
426 | { | | 426 | { |
427 | static ONCE_DECL(control); | | | |
428 | | | 427 | |
429 | RUN_ONCE(&control, doinit); | | | |
430 | } | | 428 | } |
431 | | | 429 | |
432 | /* | | 430 | /* |
433 | * Open ethernet device. Clones. | | 431 | * Open ethernet device. Clones. |
434 | */ | | 432 | */ |
435 | /* ARGSUSED */ | | 433 | /* ARGSUSED */ |
436 | int | | 434 | int |
437 | bpfopen(dev_t dev, int flag, int mode, struct lwp *l) | | 435 | bpfopen(dev_t dev, int flag, int mode, struct lwp *l) |
438 | { | | 436 | { |
439 | struct bpf_d *d; | | 437 | struct bpf_d *d; |
440 | struct file *fp; | | 438 | struct file *fp; |
441 | int error, fd; | | 439 | int error, fd; |
442 | | | 440 | |
| @@ -2107,37 +2105,36 @@ struct bpf_ops bpf_ops_kernel = { | | | @@ -2107,37 +2105,36 @@ struct bpf_ops bpf_ops_kernel = { |
2107 | .bpf_mtap_sl_out = _bpf_mtap_sl_out, | | 2105 | .bpf_mtap_sl_out = _bpf_mtap_sl_out, |
2108 | }; | | 2106 | }; |
2109 | | | 2107 | |
2110 | MODULE(MODULE_CLASS_DRIVER, bpf, "bpf_filter"); | | 2108 | MODULE(MODULE_CLASS_DRIVER, bpf, "bpf_filter"); |
2111 | | | 2109 | |
2112 | static int | | 2110 | static int |
2113 | bpf_modcmd(modcmd_t cmd, void *arg) | | 2111 | bpf_modcmd(modcmd_t cmd, void *arg) |
2114 | { | | 2112 | { |
2115 | #ifdef _MODULE | | 2113 | #ifdef _MODULE |
2116 | devmajor_t bmajor, cmajor; | | 2114 | devmajor_t bmajor, cmajor; |
2117 | #endif | | 2115 | #endif |
2118 | int error = 0; | | 2116 | int error = 0; |
2119 | | | 2117 | |
2120 | | | | |
2121 | switch (cmd) { | | 2118 | switch (cmd) { |
2122 | case MODULE_CMD_INIT: | | 2119 | case MODULE_CMD_INIT: |
2123 | bpfilterattach(0); | | 2120 | bpf_init(); |
2124 | #ifdef _MODULE | | 2121 | #ifdef _MODULE |
2125 | bmajor = cmajor = NODEVMAJOR; | | 2122 | bmajor = cmajor = NODEVMAJOR; |
2126 | error = devsw_attach("bpf", NULL, &bmajor, | | 2123 | error = devsw_attach("bpf", NULL, &bmajor, |
2127 | &bpf_cdevsw, &cmajor); | | 2124 | &bpf_cdevsw, &cmajor); |
2128 | #endif | | | |
2129 | if (error) | | 2125 | if (error) |
2130 | break; | | 2126 | break; |
| | | 2127 | #endif |
2131 | | | 2128 | |
2132 | bpf_ops_handover_enter(&bpf_ops_kernel); | | 2129 | bpf_ops_handover_enter(&bpf_ops_kernel); |
2133 | atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel); | | 2130 | atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel); |
2134 | bpf_ops_handover_exit(); | | 2131 | bpf_ops_handover_exit(); |
2135 | sysctl_net_bpf_setup(); | | 2132 | sysctl_net_bpf_setup(); |
2136 | break; | | 2133 | break; |
2137 | | | 2134 | |
2138 | case MODULE_CMD_FINI: | | 2135 | case MODULE_CMD_FINI: |
2139 | /* | | 2136 | /* |
2140 | * While there is no reference counting for bpf callers, | | 2137 | * While there is no reference counting for bpf callers, |
2141 | * unload could at least in theory be done similarly to | | 2138 | * unload could at least in theory be done similarly to |
2142 | * system call disestablishment. This should even be | | 2139 | * system call disestablishment. This should even be |
2143 | * a little simpler: | | 2140 | * a little simpler: |