Wed Jul 14 06:50:22 2021 UTC ()
unset IFF_PROMISC at bpf_detach()

Doing "d->bd_promisc = 0" is that bpf_detach() does not call
ifpromisc(ifp, 0). Currently, there is no reason for
this behavior so that it is removed.
In addition to the change, the workaround for it in vlan(4)
is also removed.


(yamaguchi)
diff -r1.240 -r1.241 src/sys/net/bpf.c
diff -r1.158 -r1.159 src/sys/net/if_vlan.c

cvs diff -r1.240 -r1.241 src/sys/net/bpf.c (switch to unified diff)

--- src/sys/net/bpf.c 2021/06/09 15:44:15 1.240
+++ src/sys/net/bpf.c 2021/07/14 06:50:22 1.241
@@ -1,1041 +1,1041 @@ @@ -1,1041 +1,1041 @@
1/* $NetBSD: bpf.c,v 1.240 2021/06/09 15:44:15 martin Exp $ */ 1/* $NetBSD: bpf.c,v 1.241 2021/07/14 06:50:22 yamaguchi 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:
15 * 1. Redistributions of source code must retain the above copyright 15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer. 16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright 17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the 18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution. 19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors 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 21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission. 22 * without specific prior written permission.
23 * 23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 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 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
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.240 2021/06/09 15:44:15 martin Exp $"); 42__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.241 2021/07/14 06:50:22 yamaguchi 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 "opt_net_mpsafe.h" 47#include "opt_net_mpsafe.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/atomic.h> 62#include <sys/atomic.h>
63#include <sys/cpu.h> 63#include <sys/cpu.h>
64 64
65#include <sys/file.h> 65#include <sys/file.h>
66#include <sys/filedesc.h> 66#include <sys/filedesc.h>
67#include <sys/tty.h> 67#include <sys/tty.h>
68#include <sys/uio.h> 68#include <sys/uio.h>
69 69
70#include <sys/protosw.h> 70#include <sys/protosw.h>
71#include <sys/socket.h> 71#include <sys/socket.h>
72#include <sys/errno.h> 72#include <sys/errno.h>
73#include <sys/kernel.h> 73#include <sys/kernel.h>
74#include <sys/poll.h> 74#include <sys/poll.h>
75#include <sys/sysctl.h> 75#include <sys/sysctl.h>
76#include <sys/kauth.h> 76#include <sys/kauth.h>
77#include <sys/syslog.h> 77#include <sys/syslog.h>
78#include <sys/percpu.h> 78#include <sys/percpu.h>
79#include <sys/pserialize.h> 79#include <sys/pserialize.h>
80#include <sys/lwp.h> 80#include <sys/lwp.h>
81#include <sys/xcall.h> 81#include <sys/xcall.h>
82 82
83#include <net/if.h> 83#include <net/if.h>
84#include <net/slip.h> 84#include <net/slip.h>
85 85
86#include <net/bpf.h> 86#include <net/bpf.h>
87#include <net/bpfdesc.h> 87#include <net/bpfdesc.h>
88#include <net/bpfjit.h> 88#include <net/bpfjit.h>
89 89
90#include <net/if_arc.h> 90#include <net/if_arc.h>
91#include <net/if_ether.h> 91#include <net/if_ether.h>
92 92
93#include <netinet/in.h> 93#include <netinet/in.h>
94#include <netinet/if_inarp.h> 94#include <netinet/if_inarp.h>
95 95
96 96
97#include <compat/sys/sockio.h> 97#include <compat/sys/sockio.h>
98 98
99#ifndef BPF_BUFSIZE 99#ifndef BPF_BUFSIZE
100/* 100/*
101 * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet 101 * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet
102 * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k). 102 * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k).
103 */ 103 */
104# define BPF_BUFSIZE 32768 104# define BPF_BUFSIZE 32768
105#endif 105#endif
106 106
107#define PRINET 26 /* interruptible */ 107#define PRINET 26 /* interruptible */
108 108
109/* 109/*
110 * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able. 110 * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able.
111 * XXX the default values should be computed dynamically based 111 * XXX the default values should be computed dynamically based
112 * on available memory size and available mbuf clusters. 112 * on available memory size and available mbuf clusters.
113 */ 113 */
114static int bpf_bufsize = BPF_BUFSIZE; 114static int bpf_bufsize = BPF_BUFSIZE;
115static int bpf_maxbufsize = BPF_DFLTBUFSIZE; /* XXX set dynamically, see above */ 115static int bpf_maxbufsize = BPF_DFLTBUFSIZE; /* XXX set dynamically, see above */
116static bool bpf_jit = false; 116static bool bpf_jit = false;
117 117
118struct bpfjit_ops bpfjit_module_ops = { 118struct bpfjit_ops bpfjit_module_ops = {
119 .bj_generate_code = NULL, 119 .bj_generate_code = NULL,
120 .bj_free_code = NULL 120 .bj_free_code = NULL
121}; 121};
122 122
123/* 123/*
124 * Global BPF statistics returned by net.bpf.stats sysctl. 124 * Global BPF statistics returned by net.bpf.stats sysctl.
125 */ 125 */
126static struct percpu *bpf_gstats_percpu; /* struct bpf_stat */ 126static struct percpu *bpf_gstats_percpu; /* struct bpf_stat */
127 127
128#define BPF_STATINC(id) \ 128#define BPF_STATINC(id) \
129 { \ 129 { \
130 struct bpf_stat *__stats = \ 130 struct bpf_stat *__stats = \
131 percpu_getref(bpf_gstats_percpu); \ 131 percpu_getref(bpf_gstats_percpu); \
132 __stats->bs_##id++; \ 132 __stats->bs_##id++; \
133 percpu_putref(bpf_gstats_percpu); \ 133 percpu_putref(bpf_gstats_percpu); \
134 } 134 }
135 135
136/* 136/*
137 * Locking notes: 137 * Locking notes:
138 * - bpf_mtx (adaptive mutex) protects: 138 * - bpf_mtx (adaptive mutex) protects:
139 * - Gobal lists: bpf_iflist and bpf_dlist 139 * - Gobal lists: bpf_iflist and bpf_dlist
140 * - struct bpf_if 140 * - struct bpf_if
141 * - bpf_close 141 * - bpf_close
142 * - bpf_psz (pserialize) 142 * - bpf_psz (pserialize)
143 * - struct bpf_d has two mutexes: 143 * - struct bpf_d has two mutexes:
144 * - bd_buf_mtx (spin mutex) protects the buffers that can be accessed 144 * - bd_buf_mtx (spin mutex) protects the buffers that can be accessed
145 * on packet tapping 145 * on packet tapping
146 * - bd_mtx (adaptive mutex) protects member variables other than the buffers 146 * - bd_mtx (adaptive mutex) protects member variables other than the buffers
147 * - Locking order: bpf_mtx => bpf_d#bd_mtx => bpf_d#bd_buf_mtx 147 * - Locking order: bpf_mtx => bpf_d#bd_mtx => bpf_d#bd_buf_mtx
148 * - struct bpf_d obtained via fp->f_bpf in bpf_read and bpf_write is 148 * - struct bpf_d obtained via fp->f_bpf in bpf_read and bpf_write is
149 * never freed because struct bpf_d is only freed in bpf_close and 149 * never freed because struct bpf_d is only freed in bpf_close and
150 * bpf_close never be called while executing bpf_read and bpf_write 150 * bpf_close never be called while executing bpf_read and bpf_write
151 * - A filter that is assigned to bpf_d can be replaced with another filter 151 * - A filter that is assigned to bpf_d can be replaced with another filter
152 * while tapping packets, so it needs to be done atomically 152 * while tapping packets, so it needs to be done atomically
153 * - struct bpf_d is iterated on bpf_dlist with psz 153 * - struct bpf_d is iterated on bpf_dlist with psz
154 * - struct bpf_if is iterated on bpf_iflist with psz or psref 154 * - struct bpf_if is iterated on bpf_iflist with psz or psref
155 */ 155 */
156/* 156/*
157 * Use a mutex to avoid a race condition between gathering the stats/peers 157 * Use a mutex to avoid a race condition between gathering the stats/peers
158 * and opening/closing the device. 158 * and opening/closing the device.
159 */ 159 */
160static kmutex_t bpf_mtx; 160static kmutex_t bpf_mtx;
161 161
162static struct psref_class *bpf_psref_class __read_mostly; 162static struct psref_class *bpf_psref_class __read_mostly;
163static pserialize_t bpf_psz; 163static pserialize_t bpf_psz;
164 164
165static inline void 165static inline void
166bpf_if_acquire(struct bpf_if *bp, struct psref *psref) 166bpf_if_acquire(struct bpf_if *bp, struct psref *psref)
167{ 167{
168 168
169 psref_acquire(psref, &bp->bif_psref, bpf_psref_class); 169 psref_acquire(psref, &bp->bif_psref, bpf_psref_class);
170} 170}
171 171
172static inline void 172static inline void
173bpf_if_release(struct bpf_if *bp, struct psref *psref) 173bpf_if_release(struct bpf_if *bp, struct psref *psref)
174{ 174{
175 175
176 psref_release(psref, &bp->bif_psref, bpf_psref_class); 176 psref_release(psref, &bp->bif_psref, bpf_psref_class);
177} 177}
178 178
179/* 179/*
180 * bpf_iflist is the list of interfaces; each corresponds to an ifnet 180 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
181 * bpf_dtab holds the descriptors, indexed by minor device # 181 * bpf_dtab holds the descriptors, indexed by minor device #
182 */ 182 */
183static struct pslist_head bpf_iflist; 183static struct pslist_head bpf_iflist;
184static struct pslist_head bpf_dlist; 184static struct pslist_head bpf_dlist;
185 185
186/* Macros for bpf_d on bpf_dlist */ 186/* Macros for bpf_d on bpf_dlist */
187#define BPF_DLIST_WRITER_INSERT_HEAD(__d) \ 187#define BPF_DLIST_WRITER_INSERT_HEAD(__d) \
188 PSLIST_WRITER_INSERT_HEAD(&bpf_dlist, (__d), bd_bpf_dlist_entry) 188 PSLIST_WRITER_INSERT_HEAD(&bpf_dlist, (__d), bd_bpf_dlist_entry)
189#define BPF_DLIST_READER_FOREACH(__d) \ 189#define BPF_DLIST_READER_FOREACH(__d) \
190 PSLIST_READER_FOREACH((__d), &bpf_dlist, struct bpf_d, \ 190 PSLIST_READER_FOREACH((__d), &bpf_dlist, struct bpf_d, \
191 bd_bpf_dlist_entry) 191 bd_bpf_dlist_entry)
192#define BPF_DLIST_WRITER_FOREACH(__d) \ 192#define BPF_DLIST_WRITER_FOREACH(__d) \
193 PSLIST_WRITER_FOREACH((__d), &bpf_dlist, struct bpf_d, \ 193 PSLIST_WRITER_FOREACH((__d), &bpf_dlist, struct bpf_d, \
194 bd_bpf_dlist_entry) 194 bd_bpf_dlist_entry)
195#define BPF_DLIST_ENTRY_INIT(__d) \ 195#define BPF_DLIST_ENTRY_INIT(__d) \
196 PSLIST_ENTRY_INIT((__d), bd_bpf_dlist_entry) 196 PSLIST_ENTRY_INIT((__d), bd_bpf_dlist_entry)
197#define BPF_DLIST_WRITER_REMOVE(__d) \ 197#define BPF_DLIST_WRITER_REMOVE(__d) \
198 PSLIST_WRITER_REMOVE((__d), bd_bpf_dlist_entry) 198 PSLIST_WRITER_REMOVE((__d), bd_bpf_dlist_entry)
199#define BPF_DLIST_ENTRY_DESTROY(__d) \ 199#define BPF_DLIST_ENTRY_DESTROY(__d) \
200 PSLIST_ENTRY_DESTROY((__d), bd_bpf_dlist_entry) 200 PSLIST_ENTRY_DESTROY((__d), bd_bpf_dlist_entry)
201 201
202/* Macros for bpf_if on bpf_iflist */ 202/* Macros for bpf_if on bpf_iflist */
203#define BPF_IFLIST_WRITER_INSERT_HEAD(__bp) \ 203#define BPF_IFLIST_WRITER_INSERT_HEAD(__bp) \
204 PSLIST_WRITER_INSERT_HEAD(&bpf_iflist, (__bp), bif_iflist_entry) 204 PSLIST_WRITER_INSERT_HEAD(&bpf_iflist, (__bp), bif_iflist_entry)
205#define BPF_IFLIST_READER_FOREACH(__bp) \ 205#define BPF_IFLIST_READER_FOREACH(__bp) \
206 PSLIST_READER_FOREACH((__bp), &bpf_iflist, struct bpf_if, \ 206 PSLIST_READER_FOREACH((__bp), &bpf_iflist, struct bpf_if, \
207 bif_iflist_entry) 207 bif_iflist_entry)
208#define BPF_IFLIST_WRITER_FOREACH(__bp) \ 208#define BPF_IFLIST_WRITER_FOREACH(__bp) \
209 PSLIST_WRITER_FOREACH((__bp), &bpf_iflist, struct bpf_if, \ 209 PSLIST_WRITER_FOREACH((__bp), &bpf_iflist, struct bpf_if, \
210 bif_iflist_entry) 210 bif_iflist_entry)
211#define BPF_IFLIST_WRITER_REMOVE(__bp) \ 211#define BPF_IFLIST_WRITER_REMOVE(__bp) \
212 PSLIST_WRITER_REMOVE((__bp), bif_iflist_entry) 212 PSLIST_WRITER_REMOVE((__bp), bif_iflist_entry)
213#define BPF_IFLIST_ENTRY_INIT(__bp) \ 213#define BPF_IFLIST_ENTRY_INIT(__bp) \
214 PSLIST_ENTRY_INIT((__bp), bif_iflist_entry) 214 PSLIST_ENTRY_INIT((__bp), bif_iflist_entry)
215#define BPF_IFLIST_ENTRY_DESTROY(__bp) \ 215#define BPF_IFLIST_ENTRY_DESTROY(__bp) \
216 PSLIST_ENTRY_DESTROY((__bp), bif_iflist_entry) 216 PSLIST_ENTRY_DESTROY((__bp), bif_iflist_entry)
217 217
218/* Macros for bpf_d on bpf_if#bif_dlist_pslist */ 218/* Macros for bpf_d on bpf_if#bif_dlist_pslist */
219#define BPFIF_DLIST_READER_FOREACH(__d, __bp) \ 219#define BPFIF_DLIST_READER_FOREACH(__d, __bp) \
220 PSLIST_READER_FOREACH((__d), &(__bp)->bif_dlist_head, struct bpf_d, \ 220 PSLIST_READER_FOREACH((__d), &(__bp)->bif_dlist_head, struct bpf_d, \
221 bd_bif_dlist_entry) 221 bd_bif_dlist_entry)
222#define BPFIF_DLIST_WRITER_INSERT_HEAD(__bp, __d) \ 222#define BPFIF_DLIST_WRITER_INSERT_HEAD(__bp, __d) \
223 PSLIST_WRITER_INSERT_HEAD(&(__bp)->bif_dlist_head, (__d), \ 223 PSLIST_WRITER_INSERT_HEAD(&(__bp)->bif_dlist_head, (__d), \
224 bd_bif_dlist_entry) 224 bd_bif_dlist_entry)
225#define BPFIF_DLIST_WRITER_REMOVE(__d) \ 225#define BPFIF_DLIST_WRITER_REMOVE(__d) \
226 PSLIST_WRITER_REMOVE((__d), bd_bif_dlist_entry) 226 PSLIST_WRITER_REMOVE((__d), bd_bif_dlist_entry)
227#define BPFIF_DLIST_ENTRY_INIT(__d) \ 227#define BPFIF_DLIST_ENTRY_INIT(__d) \
228 PSLIST_ENTRY_INIT((__d), bd_bif_dlist_entry) 228 PSLIST_ENTRY_INIT((__d), bd_bif_dlist_entry)
229#define BPFIF_DLIST_READER_EMPTY(__bp) \ 229#define BPFIF_DLIST_READER_EMPTY(__bp) \
230 (PSLIST_READER_FIRST(&(__bp)->bif_dlist_head, struct bpf_d, \ 230 (PSLIST_READER_FIRST(&(__bp)->bif_dlist_head, struct bpf_d, \
231 bd_bif_dlist_entry) == NULL) 231 bd_bif_dlist_entry) == NULL)
232#define BPFIF_DLIST_WRITER_EMPTY(__bp) \ 232#define BPFIF_DLIST_WRITER_EMPTY(__bp) \
233 (PSLIST_WRITER_FIRST(&(__bp)->bif_dlist_head, struct bpf_d, \ 233 (PSLIST_WRITER_FIRST(&(__bp)->bif_dlist_head, struct bpf_d, \
234 bd_bif_dlist_entry) == NULL) 234 bd_bif_dlist_entry) == NULL)
235#define BPFIF_DLIST_ENTRY_DESTROY(__d) \ 235#define BPFIF_DLIST_ENTRY_DESTROY(__d) \
236 PSLIST_ENTRY_DESTROY((__d), bd_bif_dlist_entry) 236 PSLIST_ENTRY_DESTROY((__d), bd_bif_dlist_entry)
237 237
238static int bpf_allocbufs(struct bpf_d *); 238static int bpf_allocbufs(struct bpf_d *);
239static u_int bpf_xfilter(struct bpf_filter **, void *, u_int, u_int); 239static u_int bpf_xfilter(struct bpf_filter **, void *, u_int, u_int);
240static void bpf_deliver(struct bpf_if *, 240static void bpf_deliver(struct bpf_if *,
241 void *(*cpfn)(void *, const void *, size_t), 241 void *(*cpfn)(void *, const void *, size_t),
242 void *, u_int, u_int, const u_int); 242 void *, u_int, u_int, const u_int);
243static void bpf_freed(struct bpf_d *); 243static void bpf_freed(struct bpf_d *);
244static void bpf_free_filter(struct bpf_filter *); 244static void bpf_free_filter(struct bpf_filter *);
245static void bpf_ifname(struct ifnet *, struct ifreq *); 245static void bpf_ifname(struct ifnet *, struct ifreq *);
246static void *bpf_mcpy(void *, const void *, size_t); 246static void *bpf_mcpy(void *, const void *, size_t);
247static int bpf_movein(struct uio *, int, uint64_t, 247static int bpf_movein(struct uio *, int, uint64_t,
248 struct mbuf **, struct sockaddr *, 248 struct mbuf **, struct sockaddr *,
249 struct bpf_filter **); 249 struct bpf_filter **);
250static void bpf_attachd(struct bpf_d *, struct bpf_if *); 250static void bpf_attachd(struct bpf_d *, struct bpf_if *);
251static void bpf_detachd(struct bpf_d *); 251static void bpf_detachd(struct bpf_d *);
252static int bpf_setif(struct bpf_d *, struct ifreq *); 252static int bpf_setif(struct bpf_d *, struct ifreq *);
253static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long); 253static int bpf_setf(struct bpf_d *, struct bpf_program *, u_long);
254static void bpf_timed_out(void *); 254static void bpf_timed_out(void *);
255static inline void 255static inline void
256 bpf_wakeup(struct bpf_d *); 256 bpf_wakeup(struct bpf_d *);
257static int bpf_hdrlen(struct bpf_d *); 257static int bpf_hdrlen(struct bpf_d *);
258static void catchpacket(struct bpf_d *, u_char *, u_int, u_int, 258static void catchpacket(struct bpf_d *, u_char *, u_int, u_int,
259 void *(*)(void *, const void *, size_t), struct timespec *); 259 void *(*)(void *, const void *, size_t), struct timespec *);
260static void reset_d(struct bpf_d *); 260static void reset_d(struct bpf_d *);
261static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *); 261static int bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
262static int bpf_setdlt(struct bpf_d *, u_int); 262static int bpf_setdlt(struct bpf_d *, u_int);
263 263
264static int bpf_read(struct file *, off_t *, struct uio *, kauth_cred_t, 264static int bpf_read(struct file *, off_t *, struct uio *, kauth_cred_t,
265 int); 265 int);
266static int bpf_write(struct file *, off_t *, struct uio *, kauth_cred_t, 266static int bpf_write(struct file *, off_t *, struct uio *, kauth_cred_t,
267 int); 267 int);
268static int bpf_ioctl(struct file *, u_long, void *); 268static int bpf_ioctl(struct file *, u_long, void *);
269static int bpf_poll(struct file *, int); 269static int bpf_poll(struct file *, int);
270static int bpf_stat(struct file *, struct stat *); 270static int bpf_stat(struct file *, struct stat *);
271static int bpf_close(struct file *); 271static int bpf_close(struct file *);
272static int bpf_kqfilter(struct file *, struct knote *); 272static int bpf_kqfilter(struct file *, struct knote *);
273 273
274static const struct fileops bpf_fileops = { 274static const struct fileops bpf_fileops = {
275 .fo_name = "bpf", 275 .fo_name = "bpf",
276 .fo_read = bpf_read, 276 .fo_read = bpf_read,
277 .fo_write = bpf_write, 277 .fo_write = bpf_write,
278 .fo_ioctl = bpf_ioctl, 278 .fo_ioctl = bpf_ioctl,
279 .fo_fcntl = fnullop_fcntl, 279 .fo_fcntl = fnullop_fcntl,
280 .fo_poll = bpf_poll, 280 .fo_poll = bpf_poll,
281 .fo_stat = bpf_stat, 281 .fo_stat = bpf_stat,
282 .fo_close = bpf_close, 282 .fo_close = bpf_close,
283 .fo_kqfilter = bpf_kqfilter, 283 .fo_kqfilter = bpf_kqfilter,
284 .fo_restart = fnullop_restart, 284 .fo_restart = fnullop_restart,
285}; 285};
286 286
287dev_type_open(bpfopen); 287dev_type_open(bpfopen);
288 288
289const struct cdevsw bpf_cdevsw = { 289const struct cdevsw bpf_cdevsw = {
290 .d_open = bpfopen, 290 .d_open = bpfopen,
291 .d_close = noclose, 291 .d_close = noclose,
292 .d_read = noread, 292 .d_read = noread,
293 .d_write = nowrite, 293 .d_write = nowrite,
294 .d_ioctl = noioctl, 294 .d_ioctl = noioctl,
295 .d_stop = nostop, 295 .d_stop = nostop,
296 .d_tty = notty, 296 .d_tty = notty,
297 .d_poll = nopoll, 297 .d_poll = nopoll,
298 .d_mmap = nommap, 298 .d_mmap = nommap,
299 .d_kqfilter = nokqfilter, 299 .d_kqfilter = nokqfilter,
300 .d_discard = nodiscard, 300 .d_discard = nodiscard,
301 .d_flag = D_OTHER | D_MPSAFE 301 .d_flag = D_OTHER | D_MPSAFE
302}; 302};
303 303
304bpfjit_func_t 304bpfjit_func_t
305bpf_jit_generate(bpf_ctx_t *bc, void *code, size_t size) 305bpf_jit_generate(bpf_ctx_t *bc, void *code, size_t size)
306{ 306{
307 struct bpfjit_ops *ops = &bpfjit_module_ops; 307 struct bpfjit_ops *ops = &bpfjit_module_ops;
308 bpfjit_func_t (*generate_code)(const bpf_ctx_t *, 308 bpfjit_func_t (*generate_code)(const bpf_ctx_t *,
309 const struct bpf_insn *, size_t); 309 const struct bpf_insn *, size_t);
310 310
311 generate_code = atomic_load_acquire(&ops->bj_generate_code); 311 generate_code = atomic_load_acquire(&ops->bj_generate_code);
312 if (generate_code != NULL) { 312 if (generate_code != NULL) {
313 return generate_code(bc, code, size); 313 return generate_code(bc, code, size);
314 } 314 }
315 return NULL; 315 return NULL;
316} 316}
317 317
318void 318void
319bpf_jit_freecode(bpfjit_func_t jcode) 319bpf_jit_freecode(bpfjit_func_t jcode)
320{ 320{
321 KASSERT(bpfjit_module_ops.bj_free_code != NULL); 321 KASSERT(bpfjit_module_ops.bj_free_code != NULL);
322 bpfjit_module_ops.bj_free_code(jcode); 322 bpfjit_module_ops.bj_free_code(jcode);
323} 323}
324 324
325static int 325static int
326bpf_movein(struct uio *uio, int linktype, uint64_t mtu, struct mbuf **mp, 326bpf_movein(struct uio *uio, int linktype, uint64_t mtu, struct mbuf **mp,
327 struct sockaddr *sockp, struct bpf_filter **wfilter) 327 struct sockaddr *sockp, struct bpf_filter **wfilter)
328{ 328{
329 struct mbuf *m, *m0, *n; 329 struct mbuf *m, *m0, *n;
330 int error; 330 int error;
331 size_t len; 331 size_t len;
332 size_t hlen; 332 size_t hlen;
333 size_t align; 333 size_t align;
334 u_int slen; 334 u_int slen;
335 335
336 /* 336 /*
337 * Build a sockaddr based on the data link layer type. 337 * Build a sockaddr based on the data link layer type.
338 * We do this at this level because the ethernet header 338 * We do this at this level because the ethernet header
339 * is copied directly into the data field of the sockaddr. 339 * is copied directly into the data field of the sockaddr.
340 * In the case of SLIP, there is no header and the packet 340 * In the case of SLIP, there is no header and the packet
341 * is forwarded as is. 341 * is forwarded as is.
342 * Also, we are careful to leave room at the front of the mbuf 342 * Also, we are careful to leave room at the front of the mbuf
343 * for the link level header. 343 * for the link level header.
344 */ 344 */
345 switch (linktype) { 345 switch (linktype) {
346 346
347 case DLT_SLIP: 347 case DLT_SLIP:
348 sockp->sa_family = AF_INET; 348 sockp->sa_family = AF_INET;
349 hlen = 0; 349 hlen = 0;
350 align = 0; 350 align = 0;
351 break; 351 break;
352 352
353 case DLT_PPP: 353 case DLT_PPP:
354 sockp->sa_family = AF_UNSPEC; 354 sockp->sa_family = AF_UNSPEC;
355 hlen = 0; 355 hlen = 0;
356 align = 0; 356 align = 0;
357 break; 357 break;
358 358
359 case DLT_EN10MB: 359 case DLT_EN10MB:
360 sockp->sa_family = AF_UNSPEC; 360 sockp->sa_family = AF_UNSPEC;
361 /* XXX Would MAXLINKHDR be better? */ 361 /* XXX Would MAXLINKHDR be better? */
362 /* 6(dst)+6(src)+2(type) */ 362 /* 6(dst)+6(src)+2(type) */
363 hlen = sizeof(struct ether_header); 363 hlen = sizeof(struct ether_header);
364 align = 2; 364 align = 2;
365 break; 365 break;
366 366
367 case DLT_ARCNET: 367 case DLT_ARCNET:
368 sockp->sa_family = AF_UNSPEC; 368 sockp->sa_family = AF_UNSPEC;
369 hlen = ARC_HDRLEN; 369 hlen = ARC_HDRLEN;
370 align = 5; 370 align = 5;
371 break; 371 break;
372 372
373 case DLT_FDDI: 373 case DLT_FDDI:
374 sockp->sa_family = AF_LINK; 374 sockp->sa_family = AF_LINK;
375 /* XXX 4(FORMAC)+6(dst)+6(src) */ 375 /* XXX 4(FORMAC)+6(dst)+6(src) */
376 hlen = 16; 376 hlen = 16;
377 align = 0; 377 align = 0;
378 break; 378 break;
379 379
380 case DLT_ECONET: 380 case DLT_ECONET:
381 sockp->sa_family = AF_UNSPEC; 381 sockp->sa_family = AF_UNSPEC;
382 hlen = 6; 382 hlen = 6;
383 align = 2; 383 align = 2;
384 break; 384 break;
385 385
386 case DLT_NULL: 386 case DLT_NULL:
387 sockp->sa_family = AF_UNSPEC; 387 sockp->sa_family = AF_UNSPEC;
388 hlen = 0; 388 hlen = 0;
389 align = 0; 389 align = 0;
390 break; 390 break;
391 391
392 default: 392 default:
393 return (EIO); 393 return (EIO);
394 } 394 }
395 395
396 len = uio->uio_resid; 396 len = uio->uio_resid;
397 /* 397 /*
398 * If there aren't enough bytes for a link level header or the 398 * If there aren't enough bytes for a link level header or the
399 * packet length exceeds the interface mtu, return an error. 399 * packet length exceeds the interface mtu, return an error.
400 */ 400 */
401 if (len - hlen > mtu) 401 if (len - hlen > mtu)
402 return (EMSGSIZE); 402 return (EMSGSIZE);
403 403
404 m0 = m = m_gethdr(M_WAIT, MT_DATA); 404 m0 = m = m_gethdr(M_WAIT, MT_DATA);
405 m_reset_rcvif(m); 405 m_reset_rcvif(m);
406 m->m_pkthdr.len = (int)(len - hlen); 406 m->m_pkthdr.len = (int)(len - hlen);
407 if (len + align > MHLEN) { 407 if (len + align > MHLEN) {
408 m_clget(m, M_WAIT); 408 m_clget(m, M_WAIT);
409 if ((m->m_flags & M_EXT) == 0) { 409 if ((m->m_flags & M_EXT) == 0) {
410 error = ENOBUFS; 410 error = ENOBUFS;
411 goto bad; 411 goto bad;
412 } 412 }
413 } 413 }
414 414
415 /* Insure the data is properly aligned */ 415 /* Insure the data is properly aligned */
416 if (align > 0) 416 if (align > 0)
417 m->m_data += align; 417 m->m_data += align;
418 418
419 for (;;) { 419 for (;;) {
420 len = M_TRAILINGSPACE(m); 420 len = M_TRAILINGSPACE(m);
421 if (len > uio->uio_resid) 421 if (len > uio->uio_resid)
422 len = uio->uio_resid; 422 len = uio->uio_resid;
423 error = uiomove(mtod(m, void *), len, uio); 423 error = uiomove(mtod(m, void *), len, uio);
424 if (error) 424 if (error)
425 goto bad; 425 goto bad;
426 m->m_len = len; 426 m->m_len = len;
427 427
428 if (uio->uio_resid == 0) 428 if (uio->uio_resid == 0)
429 break; 429 break;
430 430
431 n = m_get(M_WAIT, MT_DATA); 431 n = m_get(M_WAIT, MT_DATA);
432 m_clget(n, M_WAIT); /* if fails, there is no problem */ 432 m_clget(n, M_WAIT); /* if fails, there is no problem */
433 m->m_next = n; 433 m->m_next = n;
434 m = n; 434 m = n;
435 } 435 }
436 436
437 slen = bpf_xfilter(wfilter, mtod(m, u_char *), len, len); 437 slen = bpf_xfilter(wfilter, mtod(m, u_char *), len, len);
438 if (slen == 0) { 438 if (slen == 0) {
439 error = EPERM; 439 error = EPERM;
440 goto bad; 440 goto bad;
441 } 441 }
442 442
443 if (hlen != 0) { 443 if (hlen != 0) {
444 /* move link level header in the top of mbuf to sa_data */ 444 /* move link level header in the top of mbuf to sa_data */
445 memcpy(sockp->sa_data, mtod(m0, void *), hlen); 445 memcpy(sockp->sa_data, mtod(m0, void *), hlen);
446 m0->m_data += hlen; 446 m0->m_data += hlen;
447 m0->m_len -= hlen; 447 m0->m_len -= hlen;
448 } 448 }
449 449
450 *mp = m0; 450 *mp = m0;
451 return (0); 451 return (0);
452 452
453bad: 453bad:
454 m_freem(m0); 454 m_freem(m0);
455 return (error); 455 return (error);
456} 456}
457 457
458/* 458/*
459 * Attach file to the bpf interface, i.e. make d listen on bp. 459 * Attach file to the bpf interface, i.e. make d listen on bp.
460 */ 460 */
461static void 461static void
462bpf_attachd(struct bpf_d *d, struct bpf_if *bp) 462bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
463{ 463{
464 struct bpf_event_tracker *t; 464 struct bpf_event_tracker *t;
465 465
466 KASSERT(mutex_owned(&bpf_mtx)); 466 KASSERT(mutex_owned(&bpf_mtx));
467 KASSERT(mutex_owned(d->bd_mtx)); 467 KASSERT(mutex_owned(d->bd_mtx));
468 /* 468 /*
469 * Point d at bp, and add d to the interface's list of listeners. 469 * Point d at bp, and add d to the interface's list of listeners.
470 * Finally, point the driver's bpf cookie at the interface so 470 * Finally, point the driver's bpf cookie at the interface so
471 * it will divert packets to bpf. 471 * it will divert packets to bpf.
472 */ 472 */
473 d->bd_bif = bp; 473 d->bd_bif = bp;
474 BPFIF_DLIST_WRITER_INSERT_HEAD(bp, d); 474 BPFIF_DLIST_WRITER_INSERT_HEAD(bp, d);
475 475
476 *bp->bif_driverp = bp; 476 *bp->bif_driverp = bp;
477 477
478 SLIST_FOREACH(t, &bp->bif_trackers, bet_entries) { 478 SLIST_FOREACH(t, &bp->bif_trackers, bet_entries) {
479 t->bet_notify(bp, bp->bif_ifp, bp->bif_dlt, 479 t->bet_notify(bp, bp->bif_ifp, bp->bif_dlt,
480 BPF_TRACK_EVENT_ATTACH); 480 BPF_TRACK_EVENT_ATTACH);
481 } 481 }
482} 482}
483 483
484/* 484/*
485 * Detach a file from its interface. 485 * Detach a file from its interface.
486 */ 486 */
487static void 487static void
488bpf_detachd(struct bpf_d *d) 488bpf_detachd(struct bpf_d *d)
489{ 489{
490 struct bpf_if *bp; 490 struct bpf_if *bp;
491 struct bpf_event_tracker *t; 491 struct bpf_event_tracker *t;
492 492
493 KASSERT(mutex_owned(&bpf_mtx)); 493 KASSERT(mutex_owned(&bpf_mtx));
494 KASSERT(mutex_owned(d->bd_mtx)); 494 KASSERT(mutex_owned(d->bd_mtx));
495 495
496 bp = d->bd_bif; 496 bp = d->bd_bif;
497 /* 497 /*
498 * Check if this descriptor had requested promiscuous mode. 498 * Check if this descriptor had requested promiscuous mode.
499 * If so, turn it off. 499 * If so, turn it off.
500 */ 500 */
501 if (d->bd_promisc) { 501 if (d->bd_promisc) {
502 int error __diagused; 502 int error __diagused;
503 503
504 d->bd_promisc = 0; 504 d->bd_promisc = 0;
505 /* 505 /*
506 * Take device out of promiscuous mode. Since we were 506 * Take device out of promiscuous mode. Since we were
507 * able to enter promiscuous mode, we should be able 507 * able to enter promiscuous mode, we should be able
508 * to turn it off. But we can get an error if 508 * to turn it off. But we can get an error if
509 * the interface was configured down, so only panic 509 * the interface was configured down, so only panic
510 * if we don't get an unexpected error. 510 * if we don't get an unexpected error.
511 */ 511 */
512 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 512 KERNEL_LOCK_UNLESS_NET_MPSAFE();
513 error = ifpromisc(bp->bif_ifp, 0); 513 error = ifpromisc(bp->bif_ifp, 0);
514 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 514 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
515#ifdef DIAGNOSTIC 515#ifdef DIAGNOSTIC
516 if (error) 516 if (error)
517 printf("%s: ifpromisc failed: %d", __func__, error); 517 printf("%s: ifpromisc failed: %d", __func__, error);
518#endif 518#endif
519 } 519 }
520 520
521 /* Remove d from the interface's descriptor list. */ 521 /* Remove d from the interface's descriptor list. */
522 BPFIF_DLIST_WRITER_REMOVE(d); 522 BPFIF_DLIST_WRITER_REMOVE(d);
523 523
524 pserialize_perform(bpf_psz); 524 pserialize_perform(bpf_psz);
525 525
526 if (BPFIF_DLIST_WRITER_EMPTY(bp)) { 526 if (BPFIF_DLIST_WRITER_EMPTY(bp)) {
527 /* 527 /*
528 * Let the driver know that there are no more listeners. 528 * Let the driver know that there are no more listeners.
529 */ 529 */
530 *d->bd_bif->bif_driverp = NULL; 530 *d->bd_bif->bif_driverp = NULL;
531 } 531 }
532 532
533 d->bd_bif = NULL; 533 d->bd_bif = NULL;
534 534
535 SLIST_FOREACH(t, &bp->bif_trackers, bet_entries) { 535 SLIST_FOREACH(t, &bp->bif_trackers, bet_entries) {
536 t->bet_notify(bp, bp->bif_ifp, bp->bif_dlt, 536 t->bet_notify(bp, bp->bif_ifp, bp->bif_dlt,
537 BPF_TRACK_EVENT_DETACH); 537 BPF_TRACK_EVENT_DETACH);
538 } 538 }
539} 539}
540 540
541static void 541static void
542bpf_init(void) 542bpf_init(void)
543{ 543{
544 544
545 mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE); 545 mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE);
546 bpf_psz = pserialize_create(); 546 bpf_psz = pserialize_create();
547 bpf_psref_class = psref_class_create("bpf", IPL_SOFTNET); 547 bpf_psref_class = psref_class_create("bpf", IPL_SOFTNET);
548 548
549 PSLIST_INIT(&bpf_iflist); 549 PSLIST_INIT(&bpf_iflist);
550 PSLIST_INIT(&bpf_dlist); 550 PSLIST_INIT(&bpf_dlist);
551 551
552 bpf_gstats_percpu = percpu_alloc(sizeof(struct bpf_stat)); 552 bpf_gstats_percpu = percpu_alloc(sizeof(struct bpf_stat));
553 553
554 return; 554 return;
555} 555}
556 556
557/* 557/*
558 * bpfilterattach() is called at boot time. We don't need to do anything 558 * bpfilterattach() is called at boot time. We don't need to do anything
559 * here, since any initialization will happen as part of module init code. 559 * here, since any initialization will happen as part of module init code.
560 */ 560 */
561/* ARGSUSED */ 561/* ARGSUSED */
562void 562void
563bpfilterattach(int n) 563bpfilterattach(int n)
564{ 564{
565 565
566} 566}
567 567
568/* 568/*
569 * Open ethernet device. Clones. 569 * Open ethernet device. Clones.
570 */ 570 */
571/* ARGSUSED */ 571/* ARGSUSED */
572int 572int
573bpfopen(dev_t dev, int flag, int mode, struct lwp *l) 573bpfopen(dev_t dev, int flag, int mode, struct lwp *l)
574{ 574{
575 struct bpf_d *d; 575 struct bpf_d *d;
576 struct file *fp; 576 struct file *fp;
577 int error, fd; 577 int error, fd;
578 578
579 /* falloc() will fill in the descriptor for us. */ 579 /* falloc() will fill in the descriptor for us. */
580 if ((error = fd_allocfile(&fp, &fd)) != 0) 580 if ((error = fd_allocfile(&fp, &fd)) != 0)
581 return error; 581 return error;
582 582
583 d = kmem_zalloc(sizeof(*d), KM_SLEEP); 583 d = kmem_zalloc(sizeof(*d), KM_SLEEP);
584 d->bd_bufsize = bpf_bufsize; 584 d->bd_bufsize = bpf_bufsize;
585 d->bd_direction = BPF_D_INOUT; 585 d->bd_direction = BPF_D_INOUT;
586 d->bd_feedback = 0; 586 d->bd_feedback = 0;
587 d->bd_pid = l->l_proc->p_pid; 587 d->bd_pid = l->l_proc->p_pid;
588#ifdef _LP64 588#ifdef _LP64
589 if (curproc->p_flag & PK_32) 589 if (curproc->p_flag & PK_32)
590 d->bd_compat32 = 1; 590 d->bd_compat32 = 1;
591#endif 591#endif
592 getnanotime(&d->bd_btime); 592 getnanotime(&d->bd_btime);
593 d->bd_atime = d->bd_mtime = d->bd_btime; 593 d->bd_atime = d->bd_mtime = d->bd_btime;
594 callout_init(&d->bd_callout, CALLOUT_MPSAFE); 594 callout_init(&d->bd_callout, CALLOUT_MPSAFE);
595 selinit(&d->bd_sel); 595 selinit(&d->bd_sel);
596 d->bd_jitcode = NULL; 596 d->bd_jitcode = NULL;
597 d->bd_rfilter = NULL; 597 d->bd_rfilter = NULL;
598 d->bd_wfilter = NULL; 598 d->bd_wfilter = NULL;
599 d->bd_locked = 0; 599 d->bd_locked = 0;
600 BPF_DLIST_ENTRY_INIT(d); 600 BPF_DLIST_ENTRY_INIT(d);
601 BPFIF_DLIST_ENTRY_INIT(d); 601 BPFIF_DLIST_ENTRY_INIT(d);
602 d->bd_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET); 602 d->bd_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
603 d->bd_buf_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET); 603 d->bd_buf_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
604 cv_init(&d->bd_cv, "bpf"); 604 cv_init(&d->bd_cv, "bpf");
605 605
606 mutex_enter(&bpf_mtx); 606 mutex_enter(&bpf_mtx);
607 BPF_DLIST_WRITER_INSERT_HEAD(d); 607 BPF_DLIST_WRITER_INSERT_HEAD(d);
608 mutex_exit(&bpf_mtx); 608 mutex_exit(&bpf_mtx);
609 609
610 return fd_clone(fp, fd, flag, &bpf_fileops, d); 610 return fd_clone(fp, fd, flag, &bpf_fileops, d);
611} 611}
612 612
613/* 613/*
614 * Close the descriptor by detaching it from its interface, 614 * Close the descriptor by detaching it from its interface,
615 * deallocating its buffers, and marking it free. 615 * deallocating its buffers, and marking it free.
616 */ 616 */
617/* ARGSUSED */ 617/* ARGSUSED */
618static int 618static int
619bpf_close(struct file *fp) 619bpf_close(struct file *fp)
620{ 620{
621 struct bpf_d *d; 621 struct bpf_d *d;
622 622
623 mutex_enter(&bpf_mtx); 623 mutex_enter(&bpf_mtx);
624 624
625 if ((d = fp->f_bpf) == NULL) { 625 if ((d = fp->f_bpf) == NULL) {
626 mutex_exit(&bpf_mtx); 626 mutex_exit(&bpf_mtx);
627 return 0; 627 return 0;
628 } 628 }
629 629
630 /* 630 /*
631 * Refresh the PID associated with this bpf file. 631 * Refresh the PID associated with this bpf file.
632 */ 632 */
633 d->bd_pid = curproc->p_pid; 633 d->bd_pid = curproc->p_pid;
634 634
635 mutex_enter(d->bd_mtx); 635 mutex_enter(d->bd_mtx);
636 if (d->bd_state == BPF_WAITING) 636 if (d->bd_state == BPF_WAITING)
637 callout_halt(&d->bd_callout, d->bd_mtx); 637 callout_halt(&d->bd_callout, d->bd_mtx);
638 d->bd_state = BPF_IDLE; 638 d->bd_state = BPF_IDLE;
639 if (d->bd_bif) 639 if (d->bd_bif)
640 bpf_detachd(d); 640 bpf_detachd(d);
641 mutex_exit(d->bd_mtx); 641 mutex_exit(d->bd_mtx);
642 642
643 BPF_DLIST_WRITER_REMOVE(d); 643 BPF_DLIST_WRITER_REMOVE(d);
644 644
645 pserialize_perform(bpf_psz); 645 pserialize_perform(bpf_psz);
646 mutex_exit(&bpf_mtx); 646 mutex_exit(&bpf_mtx);
647 647
648 BPFIF_DLIST_ENTRY_DESTROY(d); 648 BPFIF_DLIST_ENTRY_DESTROY(d);
649 BPF_DLIST_ENTRY_DESTROY(d); 649 BPF_DLIST_ENTRY_DESTROY(d);
650 fp->f_bpf = NULL; 650 fp->f_bpf = NULL;
651 bpf_freed(d); 651 bpf_freed(d);
652 callout_destroy(&d->bd_callout); 652 callout_destroy(&d->bd_callout);
653 seldestroy(&d->bd_sel); 653 seldestroy(&d->bd_sel);
654 mutex_obj_free(d->bd_mtx); 654 mutex_obj_free(d->bd_mtx);
655 mutex_obj_free(d->bd_buf_mtx); 655 mutex_obj_free(d->bd_buf_mtx);
656 cv_destroy(&d->bd_cv); 656 cv_destroy(&d->bd_cv);
657 657
658 kmem_free(d, sizeof(*d)); 658 kmem_free(d, sizeof(*d));
659 659
660 return (0); 660 return (0);
661} 661}
662 662
663/* 663/*
664 * Rotate the packet buffers in descriptor d. Move the store buffer 664 * Rotate the packet buffers in descriptor d. Move the store buffer
665 * into the hold slot, and the free buffer into the store slot. 665 * into the hold slot, and the free buffer into the store slot.
666 * Zero the length of the new store buffer. 666 * Zero the length of the new store buffer.
667 */ 667 */
668#define ROTATE_BUFFERS(d) \ 668#define ROTATE_BUFFERS(d) \
669 (d)->bd_hbuf = (d)->bd_sbuf; \ 669 (d)->bd_hbuf = (d)->bd_sbuf; \
670 (d)->bd_hlen = (d)->bd_slen; \ 670 (d)->bd_hlen = (d)->bd_slen; \
671 (d)->bd_sbuf = (d)->bd_fbuf; \ 671 (d)->bd_sbuf = (d)->bd_fbuf; \
672 (d)->bd_slen = 0; \ 672 (d)->bd_slen = 0; \
673 (d)->bd_fbuf = NULL; 673 (d)->bd_fbuf = NULL;
674/* 674/*
675 * bpfread - read next chunk of packets from buffers 675 * bpfread - read next chunk of packets from buffers
676 */ 676 */
677static int 677static int
678bpf_read(struct file *fp, off_t *offp, struct uio *uio, 678bpf_read(struct file *fp, off_t *offp, struct uio *uio,
679 kauth_cred_t cred, int flags) 679 kauth_cred_t cred, int flags)
680{ 680{
681 struct bpf_d *d = fp->f_bpf; 681 struct bpf_d *d = fp->f_bpf;
682 int timed_out; 682 int timed_out;
683 int error; 683 int error;
684 684
685 getnanotime(&d->bd_atime); 685 getnanotime(&d->bd_atime);
686 /* 686 /*
687 * Restrict application to use a buffer the same size as 687 * Restrict application to use a buffer the same size as
688 * the kernel buffers. 688 * the kernel buffers.
689 */ 689 */
690 if (uio->uio_resid != d->bd_bufsize) 690 if (uio->uio_resid != d->bd_bufsize)
691 return (EINVAL); 691 return (EINVAL);
692 692
693 mutex_enter(d->bd_mtx); 693 mutex_enter(d->bd_mtx);
694 if (d->bd_state == BPF_WAITING) 694 if (d->bd_state == BPF_WAITING)
695 callout_halt(&d->bd_callout, d->bd_mtx); 695 callout_halt(&d->bd_callout, d->bd_mtx);
696 timed_out = (d->bd_state == BPF_TIMED_OUT); 696 timed_out = (d->bd_state == BPF_TIMED_OUT);
697 d->bd_state = BPF_IDLE; 697 d->bd_state = BPF_IDLE;
698 mutex_exit(d->bd_mtx); 698 mutex_exit(d->bd_mtx);
699 /* 699 /*
700 * If the hold buffer is empty, then do a timed sleep, which 700 * If the hold buffer is empty, then do a timed sleep, which
701 * ends when the timeout expires or when enough packets 701 * ends when the timeout expires or when enough packets
702 * have arrived to fill the store buffer. 702 * have arrived to fill the store buffer.
703 */ 703 */
704 mutex_enter(d->bd_buf_mtx); 704 mutex_enter(d->bd_buf_mtx);
705 while (d->bd_hbuf == NULL) { 705 while (d->bd_hbuf == NULL) {
706 if (fp->f_flag & FNONBLOCK) { 706 if (fp->f_flag & FNONBLOCK) {
707 if (d->bd_slen == 0) { 707 if (d->bd_slen == 0) {
708 error = EWOULDBLOCK; 708 error = EWOULDBLOCK;
709 goto out; 709 goto out;
710 } 710 }
711 ROTATE_BUFFERS(d); 711 ROTATE_BUFFERS(d);
712 break; 712 break;
713 } 713 }
714 714
715 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) { 715 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
716 /* 716 /*
717 * A packet(s) either arrived since the previous 717 * A packet(s) either arrived since the previous
718 * read or arrived while we were asleep. 718 * read or arrived while we were asleep.
719 * Rotate the buffers and return what's here. 719 * Rotate the buffers and return what's here.
720 */ 720 */
721 ROTATE_BUFFERS(d); 721 ROTATE_BUFFERS(d);
722 break; 722 break;
723 } 723 }
724 724
725 error = cv_timedwait_sig(&d->bd_cv, d->bd_buf_mtx, d->bd_rtout); 725 error = cv_timedwait_sig(&d->bd_cv, d->bd_buf_mtx, d->bd_rtout);
726 726
727 if (error == EINTR || error == ERESTART) 727 if (error == EINTR || error == ERESTART)
728 goto out; 728 goto out;
729 729
730 if (error == EWOULDBLOCK) { 730 if (error == EWOULDBLOCK) {
731 /* 731 /*
732 * On a timeout, return what's in the buffer, 732 * On a timeout, return what's in the buffer,
733 * which may be nothing. If there is something 733 * which may be nothing. If there is something
734 * in the store buffer, we can rotate the buffers. 734 * in the store buffer, we can rotate the buffers.
735 */ 735 */
736 if (d->bd_hbuf) 736 if (d->bd_hbuf)
737 /* 737 /*
738 * We filled up the buffer in between 738 * We filled up the buffer in between
739 * getting the timeout and arriving 739 * getting the timeout and arriving
740 * here, so we don't need to rotate. 740 * here, so we don't need to rotate.
741 */ 741 */
742 break; 742 break;
743 743
744 if (d->bd_slen == 0) { 744 if (d->bd_slen == 0) {
745 error = 0; 745 error = 0;
746 goto out; 746 goto out;
747 } 747 }
748 ROTATE_BUFFERS(d); 748 ROTATE_BUFFERS(d);
749 break; 749 break;
750 } 750 }
751 if (error != 0) 751 if (error != 0)
752 goto out; 752 goto out;
753 } 753 }
754 /* 754 /*
755 * At this point, we know we have something in the hold slot. 755 * At this point, we know we have something in the hold slot.
756 */ 756 */
757 mutex_exit(d->bd_buf_mtx); 757 mutex_exit(d->bd_buf_mtx);
758 758
759 /* 759 /*
760 * Move data from hold buffer into user space. 760 * Move data from hold buffer into user space.
761 * We know the entire buffer is transferred since 761 * We know the entire buffer is transferred since
762 * we checked above that the read buffer is bpf_bufsize bytes. 762 * we checked above that the read buffer is bpf_bufsize bytes.
763 */ 763 */
764 error = uiomove(d->bd_hbuf, d->bd_hlen, uio); 764 error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
765 765
766 mutex_enter(d->bd_buf_mtx); 766 mutex_enter(d->bd_buf_mtx);
767 d->bd_fbuf = d->bd_hbuf; 767 d->bd_fbuf = d->bd_hbuf;
768 d->bd_hbuf = NULL; 768 d->bd_hbuf = NULL;
769 d->bd_hlen = 0; 769 d->bd_hlen = 0;
770out: 770out:
771 mutex_exit(d->bd_buf_mtx); 771 mutex_exit(d->bd_buf_mtx);
772 return (error); 772 return (error);
773} 773}
774 774
775 775
776/* 776/*
777 * If there are processes sleeping on this descriptor, wake them up. 777 * If there are processes sleeping on this descriptor, wake them up.
778 */ 778 */
779static inline void 779static inline void
780bpf_wakeup(struct bpf_d *d) 780bpf_wakeup(struct bpf_d *d)
781{ 781{
782 782
783 mutex_enter(d->bd_buf_mtx); 783 mutex_enter(d->bd_buf_mtx);
784 cv_broadcast(&d->bd_cv); 784 cv_broadcast(&d->bd_cv);
785 mutex_exit(d->bd_buf_mtx); 785 mutex_exit(d->bd_buf_mtx);
786 786
787 if (d->bd_async) 787 if (d->bd_async)
788 fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL); 788 fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL);
789 selnotify(&d->bd_sel, 0, 0); 789 selnotify(&d->bd_sel, 0, 0);
790} 790}
791 791
792static void 792static void
793bpf_timed_out(void *arg) 793bpf_timed_out(void *arg)
794{ 794{
795 struct bpf_d *d = arg; 795 struct bpf_d *d = arg;
796 796
797 mutex_enter(d->bd_mtx); 797 mutex_enter(d->bd_mtx);
798 if (d->bd_state == BPF_WAITING) { 798 if (d->bd_state == BPF_WAITING) {
799 d->bd_state = BPF_TIMED_OUT; 799 d->bd_state = BPF_TIMED_OUT;
800 if (d->bd_slen != 0) 800 if (d->bd_slen != 0)
801 bpf_wakeup(d); 801 bpf_wakeup(d);
802 } 802 }
803 mutex_exit(d->bd_mtx); 803 mutex_exit(d->bd_mtx);
804} 804}
805 805
806 806
807static int 807static int
808bpf_write(struct file *fp, off_t *offp, struct uio *uio, 808bpf_write(struct file *fp, off_t *offp, struct uio *uio,
809 kauth_cred_t cred, int flags) 809 kauth_cred_t cred, int flags)
810{ 810{
811 struct bpf_d *d = fp->f_bpf; 811 struct bpf_d *d = fp->f_bpf;
812 struct bpf_if *bp; 812 struct bpf_if *bp;
813 struct ifnet *ifp; 813 struct ifnet *ifp;
814 struct mbuf *m, *mc; 814 struct mbuf *m, *mc;
815 int error; 815 int error;
816 static struct sockaddr_storage dst; 816 static struct sockaddr_storage dst;
817 struct psref psref; 817 struct psref psref;
818 int bound; 818 int bound;
819 819
820 m = NULL; /* XXX gcc */ 820 m = NULL; /* XXX gcc */
821 821
822 bound = curlwp_bind(); 822 bound = curlwp_bind();
823 mutex_enter(d->bd_mtx); 823 mutex_enter(d->bd_mtx);
824 bp = d->bd_bif; 824 bp = d->bd_bif;
825 if (bp == NULL) { 825 if (bp == NULL) {
826 mutex_exit(d->bd_mtx); 826 mutex_exit(d->bd_mtx);
827 error = ENXIO; 827 error = ENXIO;
828 goto out_bindx; 828 goto out_bindx;
829 } 829 }
830 bpf_if_acquire(bp, &psref); 830 bpf_if_acquire(bp, &psref);
831 mutex_exit(d->bd_mtx); 831 mutex_exit(d->bd_mtx);
832 832
833 getnanotime(&d->bd_mtime); 833 getnanotime(&d->bd_mtime);
834 834
835 ifp = bp->bif_ifp; 835 ifp = bp->bif_ifp;
836 if (if_is_deactivated(ifp)) { 836 if (if_is_deactivated(ifp)) {
837 error = ENXIO; 837 error = ENXIO;
838 goto out; 838 goto out;
839 } 839 }
840 840
841 if (uio->uio_resid == 0) { 841 if (uio->uio_resid == 0) {
842 error = 0; 842 error = 0;
843 goto out; 843 goto out;
844 } 844 }
845 845
846 error = bpf_movein(uio, (int)bp->bif_dlt, ifp->if_mtu, &m, 846 error = bpf_movein(uio, (int)bp->bif_dlt, ifp->if_mtu, &m,
847 (struct sockaddr *) &dst, &d->bd_wfilter); 847 (struct sockaddr *) &dst, &d->bd_wfilter);
848 if (error) 848 if (error)
849 goto out; 849 goto out;
850 850
851 if (m->m_pkthdr.len > ifp->if_mtu) { 851 if (m->m_pkthdr.len > ifp->if_mtu) {
852 m_freem(m); 852 m_freem(m);
853 error = EMSGSIZE; 853 error = EMSGSIZE;
854 goto out; 854 goto out;
855 } 855 }
856 856
857 if (d->bd_hdrcmplt) 857 if (d->bd_hdrcmplt)
858 dst.ss_family = pseudo_AF_HDRCMPLT; 858 dst.ss_family = pseudo_AF_HDRCMPLT;
859 859
860 if (d->bd_feedback) { 860 if (d->bd_feedback) {
861 mc = m_dup(m, 0, M_COPYALL, M_NOWAIT); 861 mc = m_dup(m, 0, M_COPYALL, M_NOWAIT);
862 if (mc != NULL) 862 if (mc != NULL)
863 m_set_rcvif(mc, ifp); 863 m_set_rcvif(mc, ifp);
864 /* Set M_PROMISC for outgoing packets to be discarded. */ 864 /* Set M_PROMISC for outgoing packets to be discarded. */
865 if (1 /*d->bd_direction == BPF_D_INOUT*/) 865 if (1 /*d->bd_direction == BPF_D_INOUT*/)
866 m->m_flags |= M_PROMISC; 866 m->m_flags |= M_PROMISC;
867 } else  867 } else
868 mc = NULL; 868 mc = NULL;
869 869
870 error = if_output_lock(ifp, ifp, m, (struct sockaddr *) &dst, NULL); 870 error = if_output_lock(ifp, ifp, m, (struct sockaddr *) &dst, NULL);
871 871
872 if (mc != NULL) { 872 if (mc != NULL) {
873 if (error == 0) { 873 if (error == 0) {
874 int s = splsoftnet(); 874 int s = splsoftnet();
875 KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp); 875 KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp);
876 ifp->_if_input(ifp, mc); 876 ifp->_if_input(ifp, mc);
877 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp); 877 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp);
878 splx(s); 878 splx(s);
879 } else 879 } else
880 m_freem(mc); 880 m_freem(mc);
881 } 881 }
882 /* 882 /*
883 * The driver frees the mbuf. 883 * The driver frees the mbuf.
884 */ 884 */
885out: 885out:
886 bpf_if_release(bp, &psref); 886 bpf_if_release(bp, &psref);
887out_bindx: 887out_bindx:
888 curlwp_bindx(bound); 888 curlwp_bindx(bound);
889 return error; 889 return error;
890} 890}
891 891
892/* 892/*
893 * Reset a descriptor by flushing its packet buffer and clearing the 893 * Reset a descriptor by flushing its packet buffer and clearing the
894 * receive and drop counts. 894 * receive and drop counts.
895 */ 895 */
896static void 896static void
897reset_d(struct bpf_d *d) 897reset_d(struct bpf_d *d)
898{ 898{
899 899
900 KASSERT(mutex_owned(d->bd_mtx)); 900 KASSERT(mutex_owned(d->bd_mtx));
901 901
902 mutex_enter(d->bd_buf_mtx); 902 mutex_enter(d->bd_buf_mtx);
903 if (d->bd_hbuf) { 903 if (d->bd_hbuf) {
904 /* Free the hold buffer. */ 904 /* Free the hold buffer. */
905 d->bd_fbuf = d->bd_hbuf; 905 d->bd_fbuf = d->bd_hbuf;
906 d->bd_hbuf = NULL; 906 d->bd_hbuf = NULL;
907 } 907 }
908 d->bd_slen = 0; 908 d->bd_slen = 0;
909 d->bd_hlen = 0; 909 d->bd_hlen = 0;
910 d->bd_rcount = 0; 910 d->bd_rcount = 0;
911 d->bd_dcount = 0; 911 d->bd_dcount = 0;
912 d->bd_ccount = 0; 912 d->bd_ccount = 0;
913 mutex_exit(d->bd_buf_mtx); 913 mutex_exit(d->bd_buf_mtx);
914} 914}
915 915
916/* 916/*
917 * FIONREAD Check for read packet available. 917 * FIONREAD Check for read packet available.
918 * BIOCGBLEN Get buffer len [for read()]. 918 * BIOCGBLEN Get buffer len [for read()].
919 * BIOCSETF Set ethernet read filter. 919 * BIOCSETF Set ethernet read filter.
920 * BIOCFLUSH Flush read packet buffer. 920 * BIOCFLUSH Flush read packet buffer.
921 * BIOCPROMISC Put interface into promiscuous mode. 921 * BIOCPROMISC Put interface into promiscuous mode.
922 * BIOCGDLT Get link layer type. 922 * BIOCGDLT Get link layer type.
923 * BIOCGETIF Get interface name. 923 * BIOCGETIF Get interface name.
924 * BIOCSETIF Set interface. 924 * BIOCSETIF Set interface.
925 * BIOCSRTIMEOUT Set read timeout. 925 * BIOCSRTIMEOUT Set read timeout.
926 * BIOCGRTIMEOUT Get read timeout. 926 * BIOCGRTIMEOUT Get read timeout.
927 * BIOCGSTATS Get packet stats. 927 * BIOCGSTATS Get packet stats.
928 * BIOCIMMEDIATE Set immediate mode. 928 * BIOCIMMEDIATE Set immediate mode.
929 * BIOCVERSION Get filter language version. 929 * BIOCVERSION Get filter language version.
930 * BIOCGHDRCMPLT Get "header already complete" flag. 930 * BIOCGHDRCMPLT Get "header already complete" flag.
931 * BIOCSHDRCMPLT Set "header already complete" flag. 931 * BIOCSHDRCMPLT Set "header already complete" flag.
932 * BIOCSFEEDBACK Set packet feedback mode. 932 * BIOCSFEEDBACK Set packet feedback mode.
933 * BIOCGFEEDBACK Get packet feedback mode. 933 * BIOCGFEEDBACK Get packet feedback mode.
934 * BIOCGDIRECTION Get packet direction flag 934 * BIOCGDIRECTION Get packet direction flag
935 * BIOCSDIRECTION Set packet direction flag 935 * BIOCSDIRECTION Set packet direction flag
936 */ 936 */
937/* ARGSUSED */ 937/* ARGSUSED */
938static int 938static int
939bpf_ioctl(struct file *fp, u_long cmd, void *addr) 939bpf_ioctl(struct file *fp, u_long cmd, void *addr)
940{ 940{
941 struct bpf_d *d = fp->f_bpf; 941 struct bpf_d *d = fp->f_bpf;
942 int error = 0; 942 int error = 0;
943 943
944 /* 944 /*
945 * Refresh the PID associated with this bpf file. 945 * Refresh the PID associated with this bpf file.
946 */ 946 */
947 d->bd_pid = curproc->p_pid; 947 d->bd_pid = curproc->p_pid;
948#ifdef _LP64 948#ifdef _LP64
949 if (curproc->p_flag & PK_32) 949 if (curproc->p_flag & PK_32)
950 d->bd_compat32 = 1; 950 d->bd_compat32 = 1;
951 else 951 else
952 d->bd_compat32 = 0; 952 d->bd_compat32 = 0;
953#endif 953#endif
954 954
955 mutex_enter(d->bd_mtx); 955 mutex_enter(d->bd_mtx);
956 if (d->bd_state == BPF_WAITING) 956 if (d->bd_state == BPF_WAITING)
957 callout_halt(&d->bd_callout, d->bd_mtx); 957 callout_halt(&d->bd_callout, d->bd_mtx);
958 d->bd_state = BPF_IDLE; 958 d->bd_state = BPF_IDLE;
959 mutex_exit(d->bd_mtx); 959 mutex_exit(d->bd_mtx);
960 960
961 if (d->bd_locked) { 961 if (d->bd_locked) {
962 switch (cmd) { 962 switch (cmd) {
963 case BIOCGBLEN: /* FALLTHROUGH */ 963 case BIOCGBLEN: /* FALLTHROUGH */
964 case BIOCFLUSH: /* FALLTHROUGH */ 964 case BIOCFLUSH: /* FALLTHROUGH */
965 case BIOCGDLT: /* FALLTHROUGH */ 965 case BIOCGDLT: /* FALLTHROUGH */
966 case BIOCGDLTLIST: /* FALLTHROUGH */ 966 case BIOCGDLTLIST: /* FALLTHROUGH */
967 case BIOCGETIF: /* FALLTHROUGH */ 967 case BIOCGETIF: /* FALLTHROUGH */
968 case BIOCGRTIMEOUT: /* FALLTHROUGH */ 968 case BIOCGRTIMEOUT: /* FALLTHROUGH */
969 case BIOCGSTATS: /* FALLTHROUGH */ 969 case BIOCGSTATS: /* FALLTHROUGH */
970 case BIOCVERSION: /* FALLTHROUGH */ 970 case BIOCVERSION: /* FALLTHROUGH */
971 case BIOCGHDRCMPLT: /* FALLTHROUGH */ 971 case BIOCGHDRCMPLT: /* FALLTHROUGH */
972 case FIONREAD: /* FALLTHROUGH */ 972 case FIONREAD: /* FALLTHROUGH */
973 case BIOCLOCK: /* FALLTHROUGH */ 973 case BIOCLOCK: /* FALLTHROUGH */
974 case BIOCSRTIMEOUT: /* FALLTHROUGH */ 974 case BIOCSRTIMEOUT: /* FALLTHROUGH */
975 case BIOCIMMEDIATE: /* FALLTHROUGH */ 975 case BIOCIMMEDIATE: /* FALLTHROUGH */
976 case TIOCGPGRP: 976 case TIOCGPGRP:
977 break; 977 break;
978 default: 978 default:
979 return EPERM; 979 return EPERM;
980 } 980 }
981 } 981 }
982 982
983 switch (cmd) { 983 switch (cmd) {
984 984
985 default: 985 default:
986 error = EINVAL; 986 error = EINVAL;
987 break; 987 break;
988 988
989 /* 989 /*
990 * Check for read packet available. 990 * Check for read packet available.
991 */ 991 */
992 case FIONREAD: 992 case FIONREAD:
993 { 993 {
994 int n; 994 int n;
995 995
996 mutex_enter(d->bd_buf_mtx); 996 mutex_enter(d->bd_buf_mtx);
997 n = d->bd_slen; 997 n = d->bd_slen;
998 if (d->bd_hbuf) 998 if (d->bd_hbuf)
999 n += d->bd_hlen; 999 n += d->bd_hlen;
1000 mutex_exit(d->bd_buf_mtx); 1000 mutex_exit(d->bd_buf_mtx);
1001 1001
1002 *(int *)addr = n; 1002 *(int *)addr = n;
1003 break; 1003 break;
1004 } 1004 }
1005 1005
1006 /* 1006 /*
1007 * Get buffer len [for read()]. 1007 * Get buffer len [for read()].
1008 */ 1008 */
1009 case BIOCGBLEN: 1009 case BIOCGBLEN:
1010 *(u_int *)addr = d->bd_bufsize; 1010 *(u_int *)addr = d->bd_bufsize;
1011 break; 1011 break;
1012 1012
1013 /* 1013 /*
1014 * Set buffer length. 1014 * Set buffer length.
1015 */ 1015 */
1016 case BIOCSBLEN: 1016 case BIOCSBLEN:
1017 /* 1017 /*
1018 * Forbid to change the buffer length if buffers are already 1018 * Forbid to change the buffer length if buffers are already
1019 * allocated. 1019 * allocated.
1020 */ 1020 */
1021 mutex_enter(d->bd_mtx); 1021 mutex_enter(d->bd_mtx);
1022 mutex_enter(d->bd_buf_mtx); 1022 mutex_enter(d->bd_buf_mtx);
1023 if (d->bd_bif != NULL || d->bd_sbuf != NULL) 1023 if (d->bd_bif != NULL || d->bd_sbuf != NULL)
1024 error = EINVAL; 1024 error = EINVAL;
1025 else { 1025 else {
1026 u_int size = *(u_int *)addr; 1026 u_int size = *(u_int *)addr;
1027 1027
1028 if (size > bpf_maxbufsize) 1028 if (size > bpf_maxbufsize)
1029 *(u_int *)addr = size = bpf_maxbufsize; 1029 *(u_int *)addr = size = bpf_maxbufsize;
1030 else if (size < BPF_MINBUFSIZE) 1030 else if (size < BPF_MINBUFSIZE)
1031 *(u_int *)addr = size = BPF_MINBUFSIZE; 1031 *(u_int *)addr = size = BPF_MINBUFSIZE;
1032 d->bd_bufsize = size; 1032 d->bd_bufsize = size;
1033 } 1033 }
1034 mutex_exit(d->bd_buf_mtx); 1034 mutex_exit(d->bd_buf_mtx);
1035 mutex_exit(d->bd_mtx); 1035 mutex_exit(d->bd_mtx);
1036 break; 1036 break;
1037 1037
1038 /* 1038 /*
1039 * Set link layer read filter. 1039 * Set link layer read filter.
1040 */ 1040 */
1041 case BIOCSETF: /* FALLTHROUGH */ 1041 case BIOCSETF: /* FALLTHROUGH */
@@ -1198,1481 +1198,1480 @@ bpf_ioctl(struct file *fp, u_long cmd, v @@ -1198,1481 +1198,1480 @@ bpf_ioctl(struct file *fp, u_long cmd, v
1198 tv->tv_sec = d->bd_rtout / hz; 1198 tv->tv_sec = d->bd_rtout / hz;
1199 tv->tv_usec = (d->bd_rtout % hz) * tick; 1199 tv->tv_usec = (d->bd_rtout % hz) * tick;
1200 break; 1200 break;
1201 } 1201 }
1202 /* 1202 /*
1203 * Get packet stats. 1203 * Get packet stats.
1204 */ 1204 */
1205 case BIOCGSTATS: 1205 case BIOCGSTATS:
1206 { 1206 {
1207 struct bpf_stat *bs = addr; 1207 struct bpf_stat *bs = addr;
1208 1208
1209 bs->bs_recv = d->bd_rcount; 1209 bs->bs_recv = d->bd_rcount;
1210 bs->bs_drop = d->bd_dcount; 1210 bs->bs_drop = d->bd_dcount;
1211 bs->bs_capt = d->bd_ccount; 1211 bs->bs_capt = d->bd_ccount;
1212 break; 1212 break;
1213 } 1213 }
1214 1214
1215 case BIOCGSTATSOLD: 1215 case BIOCGSTATSOLD:
1216 { 1216 {
1217 struct bpf_stat_old *bs = addr; 1217 struct bpf_stat_old *bs = addr;
1218 1218
1219 bs->bs_recv = d->bd_rcount; 1219 bs->bs_recv = d->bd_rcount;
1220 bs->bs_drop = d->bd_dcount; 1220 bs->bs_drop = d->bd_dcount;
1221 break; 1221 break;
1222 } 1222 }
1223 1223
1224 /* 1224 /*
1225 * Set immediate mode. 1225 * Set immediate mode.
1226 */ 1226 */
1227 case BIOCIMMEDIATE: 1227 case BIOCIMMEDIATE:
1228 d->bd_immediate = *(u_int *)addr; 1228 d->bd_immediate = *(u_int *)addr;
1229 break; 1229 break;
1230 1230
1231 case BIOCVERSION: 1231 case BIOCVERSION:
1232 { 1232 {
1233 struct bpf_version *bv = addr; 1233 struct bpf_version *bv = addr;
1234 1234
1235 bv->bv_major = BPF_MAJOR_VERSION; 1235 bv->bv_major = BPF_MAJOR_VERSION;
1236 bv->bv_minor = BPF_MINOR_VERSION; 1236 bv->bv_minor = BPF_MINOR_VERSION;
1237 break; 1237 break;
1238 } 1238 }
1239 1239
1240 case BIOCGHDRCMPLT: /* get "header already complete" flag */ 1240 case BIOCGHDRCMPLT: /* get "header already complete" flag */
1241 *(u_int *)addr = d->bd_hdrcmplt; 1241 *(u_int *)addr = d->bd_hdrcmplt;
1242 break; 1242 break;
1243 1243
1244 case BIOCSHDRCMPLT: /* set "header already complete" flag */ 1244 case BIOCSHDRCMPLT: /* set "header already complete" flag */
1245 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0; 1245 d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1246 break; 1246 break;
1247 1247
1248 /* 1248 /*
1249 * Get packet direction flag 1249 * Get packet direction flag
1250 */ 1250 */
1251 case BIOCGDIRECTION: 1251 case BIOCGDIRECTION:
1252 *(u_int *)addr = d->bd_direction; 1252 *(u_int *)addr = d->bd_direction;
1253 break; 1253 break;
1254 1254
1255 /* 1255 /*
1256 * Set packet direction flag 1256 * Set packet direction flag
1257 */ 1257 */
1258 case BIOCSDIRECTION: 1258 case BIOCSDIRECTION:
1259 { 1259 {
1260 u_int direction; 1260 u_int direction;
1261 1261
1262 direction = *(u_int *)addr; 1262 direction = *(u_int *)addr;
1263 switch (direction) { 1263 switch (direction) {
1264 case BPF_D_IN: 1264 case BPF_D_IN:
1265 case BPF_D_INOUT: 1265 case BPF_D_INOUT:
1266 case BPF_D_OUT: 1266 case BPF_D_OUT:
1267 d->bd_direction = direction; 1267 d->bd_direction = direction;
1268 break; 1268 break;
1269 default: 1269 default:
1270 error = EINVAL; 1270 error = EINVAL;
1271 } 1271 }
1272 } 1272 }
1273 break; 1273 break;
1274 1274
1275 /* 1275 /*
1276 * Set "feed packets from bpf back to input" mode 1276 * Set "feed packets from bpf back to input" mode
1277 */ 1277 */
1278 case BIOCSFEEDBACK: 1278 case BIOCSFEEDBACK:
1279 d->bd_feedback = *(u_int *)addr; 1279 d->bd_feedback = *(u_int *)addr;
1280 break; 1280 break;
1281 1281
1282 /* 1282 /*
1283 * Get "feed packets from bpf back to input" mode 1283 * Get "feed packets from bpf back to input" mode
1284 */ 1284 */
1285 case BIOCGFEEDBACK: 1285 case BIOCGFEEDBACK:
1286 *(u_int *)addr = d->bd_feedback; 1286 *(u_int *)addr = d->bd_feedback;
1287 break; 1287 break;
1288 1288
1289 case FIONBIO: /* Non-blocking I/O */ 1289 case FIONBIO: /* Non-blocking I/O */
1290 /* 1290 /*
1291 * No need to do anything special as we use IO_NDELAY in 1291 * No need to do anything special as we use IO_NDELAY in
1292 * bpfread() as an indication of whether or not to block 1292 * bpfread() as an indication of whether or not to block
1293 * the read. 1293 * the read.
1294 */ 1294 */
1295 break; 1295 break;
1296 1296
1297 case FIOASYNC: /* Send signal on receive packets */ 1297 case FIOASYNC: /* Send signal on receive packets */
1298 mutex_enter(d->bd_mtx); 1298 mutex_enter(d->bd_mtx);
1299 d->bd_async = *(int *)addr; 1299 d->bd_async = *(int *)addr;
1300 mutex_exit(d->bd_mtx); 1300 mutex_exit(d->bd_mtx);
1301 break; 1301 break;
1302 1302
1303 case TIOCSPGRP: /* Process or group to send signals to */ 1303 case TIOCSPGRP: /* Process or group to send signals to */
1304 case FIOSETOWN: 1304 case FIOSETOWN:
1305 error = fsetown(&d->bd_pgid, cmd, addr); 1305 error = fsetown(&d->bd_pgid, cmd, addr);
1306 break; 1306 break;
1307 1307
1308 case TIOCGPGRP: 1308 case TIOCGPGRP:
1309 case FIOGETOWN: 1309 case FIOGETOWN:
1310 error = fgetown(d->bd_pgid, cmd, addr); 1310 error = fgetown(d->bd_pgid, cmd, addr);
1311 break; 1311 break;
1312 } 1312 }
1313 return (error); 1313 return (error);
1314} 1314}
1315 1315
1316/* 1316/*
1317 * Set d's packet filter program to fp. If this file already has a filter, 1317 * Set d's packet filter program to fp. If this file already has a filter,
1318 * free it and replace it. Returns EINVAL for bogus requests. 1318 * free it and replace it. Returns EINVAL for bogus requests.
1319 */ 1319 */
1320static int 1320static int
1321bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd) 1321bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1322{ 1322{
1323 struct bpf_insn *fcode; 1323 struct bpf_insn *fcode;
1324 bpfjit_func_t jcode; 1324 bpfjit_func_t jcode;
1325 size_t flen, size = 0; 1325 size_t flen, size = 0;
1326 struct bpf_filter *oldf, *newf, **storef; 1326 struct bpf_filter *oldf, *newf, **storef;
1327 1327
1328 jcode = NULL; 1328 jcode = NULL;
1329 flen = fp->bf_len; 1329 flen = fp->bf_len;
1330 1330
1331 if ((fp->bf_insns == NULL && flen) || flen > BPF_MAXINSNS) { 1331 if ((fp->bf_insns == NULL && flen) || flen > BPF_MAXINSNS) {
1332 return EINVAL; 1332 return EINVAL;
1333 } 1333 }
1334 1334
1335 if (flen) { 1335 if (flen) {
1336 /* 1336 /*
1337 * Allocate the buffer, copy the byte-code from 1337 * Allocate the buffer, copy the byte-code from
1338 * userspace and validate it. 1338 * userspace and validate it.
1339 */ 1339 */
1340 size = flen * sizeof(*fp->bf_insns); 1340 size = flen * sizeof(*fp->bf_insns);
1341 fcode = kmem_alloc(size, KM_SLEEP); 1341 fcode = kmem_alloc(size, KM_SLEEP);
1342 if (copyin(fp->bf_insns, fcode, size) != 0 || 1342 if (copyin(fp->bf_insns, fcode, size) != 0 ||
1343 !bpf_validate(fcode, (int)flen)) { 1343 !bpf_validate(fcode, (int)flen)) {
1344 kmem_free(fcode, size); 1344 kmem_free(fcode, size);
1345 return EINVAL; 1345 return EINVAL;
1346 } 1346 }
1347 if (bpf_jit) 1347 if (bpf_jit)
1348 jcode = bpf_jit_generate(NULL, fcode, flen); 1348 jcode = bpf_jit_generate(NULL, fcode, flen);
1349 } else { 1349 } else {
1350 fcode = NULL; 1350 fcode = NULL;
1351 } 1351 }
1352 1352
1353 newf = kmem_alloc(sizeof(*newf), KM_SLEEP); 1353 newf = kmem_alloc(sizeof(*newf), KM_SLEEP);
1354 newf->bf_insn = fcode; 1354 newf->bf_insn = fcode;
1355 newf->bf_size = size; 1355 newf->bf_size = size;
1356 newf->bf_jitcode = jcode; 1356 newf->bf_jitcode = jcode;
1357 if (cmd == BIOCSETF) 1357 if (cmd == BIOCSETF)
1358 d->bd_jitcode = jcode; /* XXX just for kvm(3) users */ 1358 d->bd_jitcode = jcode; /* XXX just for kvm(3) users */
1359 1359
1360 /* Need to hold bpf_mtx for pserialize_perform */ 1360 /* Need to hold bpf_mtx for pserialize_perform */
1361 mutex_enter(&bpf_mtx); 1361 mutex_enter(&bpf_mtx);
1362 mutex_enter(d->bd_mtx); 1362 mutex_enter(d->bd_mtx);
1363 if (cmd == BIOCSETWF) { 1363 if (cmd == BIOCSETWF) {
1364 oldf = d->bd_wfilter; 1364 oldf = d->bd_wfilter;
1365 storef = &d->bd_wfilter; 1365 storef = &d->bd_wfilter;
1366 } else { 1366 } else {
1367 oldf = d->bd_rfilter; 1367 oldf = d->bd_rfilter;
1368 storef = &d->bd_rfilter; 1368 storef = &d->bd_rfilter;
1369 } 1369 }
1370 atomic_store_release(storef, newf); 1370 atomic_store_release(storef, newf);
1371 reset_d(d); 1371 reset_d(d);
1372 pserialize_perform(bpf_psz); 1372 pserialize_perform(bpf_psz);
1373 mutex_exit(d->bd_mtx); 1373 mutex_exit(d->bd_mtx);
1374 mutex_exit(&bpf_mtx); 1374 mutex_exit(&bpf_mtx);
1375 1375
1376 if (oldf != NULL) 1376 if (oldf != NULL)
1377 bpf_free_filter(oldf); 1377 bpf_free_filter(oldf);
1378 1378
1379 return 0; 1379 return 0;
1380} 1380}
1381 1381
1382/* 1382/*
1383 * Detach a file from its current interface (if attached at all) and attach 1383 * Detach a file from its current interface (if attached at all) and attach
1384 * to the interface indicated by the name stored in ifr. 1384 * to the interface indicated by the name stored in ifr.
1385 * Return an errno or 0. 1385 * Return an errno or 0.
1386 */ 1386 */
1387static int 1387static int
1388bpf_setif(struct bpf_d *d, struct ifreq *ifr) 1388bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1389{ 1389{
1390 struct bpf_if *bp; 1390 struct bpf_if *bp;
1391 char *cp; 1391 char *cp;
1392 int unit_seen, i, error; 1392 int unit_seen, i, error;
1393 1393
1394 KASSERT(mutex_owned(&bpf_mtx)); 1394 KASSERT(mutex_owned(&bpf_mtx));
1395 /* 1395 /*
1396 * Make sure the provided name has a unit number, and default 1396 * Make sure the provided name has a unit number, and default
1397 * it to '0' if not specified. 1397 * it to '0' if not specified.
1398 * XXX This is ugly ... do this differently? 1398 * XXX This is ugly ... do this differently?
1399 */ 1399 */
1400 unit_seen = 0; 1400 unit_seen = 0;
1401 cp = ifr->ifr_name; 1401 cp = ifr->ifr_name;
1402 cp[sizeof(ifr->ifr_name) - 1] = '\0'; /* sanity */ 1402 cp[sizeof(ifr->ifr_name) - 1] = '\0'; /* sanity */
1403 while (*cp++) 1403 while (*cp++)
1404 if (*cp >= '0' && *cp <= '9') 1404 if (*cp >= '0' && *cp <= '9')
1405 unit_seen = 1; 1405 unit_seen = 1;
1406 if (!unit_seen) { 1406 if (!unit_seen) {
1407 /* Make sure to leave room for the '\0'. */ 1407 /* Make sure to leave room for the '\0'. */
1408 for (i = 0; i < (IFNAMSIZ - 1); ++i) { 1408 for (i = 0; i < (IFNAMSIZ - 1); ++i) {
1409 if ((ifr->ifr_name[i] >= 'a' && 1409 if ((ifr->ifr_name[i] >= 'a' &&
1410 ifr->ifr_name[i] <= 'z') || 1410 ifr->ifr_name[i] <= 'z') ||
1411 (ifr->ifr_name[i] >= 'A' && 1411 (ifr->ifr_name[i] >= 'A' &&
1412 ifr->ifr_name[i] <= 'Z')) 1412 ifr->ifr_name[i] <= 'Z'))
1413 continue; 1413 continue;
1414 ifr->ifr_name[i] = '0'; 1414 ifr->ifr_name[i] = '0';
1415 } 1415 }
1416 } 1416 }
1417 1417
1418 /* 1418 /*
1419 * Look through attached interfaces for the named one. 1419 * Look through attached interfaces for the named one.
1420 */ 1420 */
1421 BPF_IFLIST_WRITER_FOREACH(bp) { 1421 BPF_IFLIST_WRITER_FOREACH(bp) {
1422 struct ifnet *ifp = bp->bif_ifp; 1422 struct ifnet *ifp = bp->bif_ifp;
1423 1423
1424 if (ifp == NULL || 1424 if (ifp == NULL ||
1425 strcmp(ifp->if_xname, ifr->ifr_name) != 0) 1425 strcmp(ifp->if_xname, ifr->ifr_name) != 0)
1426 continue; 1426 continue;
1427 /* skip additional entry */ 1427 /* skip additional entry */
1428 if (bp->bif_driverp != &ifp->if_bpf) 1428 if (bp->bif_driverp != &ifp->if_bpf)
1429 continue; 1429 continue;
1430 /* 1430 /*
1431 * We found the requested interface. 1431 * We found the requested interface.
1432 * Allocate the packet buffers if we need to. 1432 * Allocate the packet buffers if we need to.
1433 * If we're already attached to requested interface, 1433 * If we're already attached to requested interface,
1434 * just flush the buffer. 1434 * just flush the buffer.
1435 */ 1435 */
1436 /* 1436 /*
1437 * bpf_allocbufs is called only here. bpf_mtx ensures that 1437 * bpf_allocbufs is called only here. bpf_mtx ensures that
1438 * no race condition happen on d->bd_sbuf. 1438 * no race condition happen on d->bd_sbuf.
1439 */ 1439 */
1440 if (d->bd_sbuf == NULL) { 1440 if (d->bd_sbuf == NULL) {
1441 error = bpf_allocbufs(d); 1441 error = bpf_allocbufs(d);
1442 if (error != 0) 1442 if (error != 0)
1443 return (error); 1443 return (error);
1444 } 1444 }
1445 mutex_enter(d->bd_mtx); 1445 mutex_enter(d->bd_mtx);
1446 if (bp != d->bd_bif) { 1446 if (bp != d->bd_bif) {
1447 if (d->bd_bif) { 1447 if (d->bd_bif) {
1448 /* 1448 /*
1449 * Detach if attached to something else. 1449 * Detach if attached to something else.
1450 */ 1450 */
1451 bpf_detachd(d); 1451 bpf_detachd(d);
1452 BPFIF_DLIST_ENTRY_INIT(d); 1452 BPFIF_DLIST_ENTRY_INIT(d);
1453 } 1453 }
1454 1454
1455 bpf_attachd(d, bp); 1455 bpf_attachd(d, bp);
1456 } 1456 }
1457 reset_d(d); 1457 reset_d(d);
1458 mutex_exit(d->bd_mtx); 1458 mutex_exit(d->bd_mtx);
1459 return (0); 1459 return (0);
1460 } 1460 }
1461 /* Not found. */ 1461 /* Not found. */
1462 return (ENXIO); 1462 return (ENXIO);
1463} 1463}
1464 1464
1465/* 1465/*
1466 * Copy the interface name to the ifreq. 1466 * Copy the interface name to the ifreq.
1467 */ 1467 */
1468static void 1468static void
1469bpf_ifname(struct ifnet *ifp, struct ifreq *ifr) 1469bpf_ifname(struct ifnet *ifp, struct ifreq *ifr)
1470{ 1470{
1471 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ); 1471 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
1472} 1472}
1473 1473
1474static int 1474static int
1475bpf_stat(struct file *fp, struct stat *st) 1475bpf_stat(struct file *fp, struct stat *st)
1476{ 1476{
1477 struct bpf_d *d = fp->f_bpf; 1477 struct bpf_d *d = fp->f_bpf;
1478 1478
1479 (void)memset(st, 0, sizeof(*st)); 1479 (void)memset(st, 0, sizeof(*st));
1480 mutex_enter(d->bd_mtx); 1480 mutex_enter(d->bd_mtx);
1481 st->st_dev = makedev(cdevsw_lookup_major(&bpf_cdevsw), d->bd_pid); 1481 st->st_dev = makedev(cdevsw_lookup_major(&bpf_cdevsw), d->bd_pid);
1482 st->st_atimespec = d->bd_atime; 1482 st->st_atimespec = d->bd_atime;
1483 st->st_mtimespec = d->bd_mtime; 1483 st->st_mtimespec = d->bd_mtime;
1484 st->st_ctimespec = st->st_birthtimespec = d->bd_btime; 1484 st->st_ctimespec = st->st_birthtimespec = d->bd_btime;
1485 st->st_uid = kauth_cred_geteuid(fp->f_cred); 1485 st->st_uid = kauth_cred_geteuid(fp->f_cred);
1486 st->st_gid = kauth_cred_getegid(fp->f_cred); 1486 st->st_gid = kauth_cred_getegid(fp->f_cred);
1487 st->st_mode = S_IFCHR; 1487 st->st_mode = S_IFCHR;
1488 mutex_exit(d->bd_mtx); 1488 mutex_exit(d->bd_mtx);
1489 return 0; 1489 return 0;
1490} 1490}
1491 1491
1492/* 1492/*
1493 * Support for poll() system call 1493 * Support for poll() system call
1494 * 1494 *
1495 * Return true iff the specific operation will not block indefinitely - with 1495 * Return true iff the specific operation will not block indefinitely - with
1496 * the assumption that it is safe to positively acknowledge a request for the 1496 * the assumption that it is safe to positively acknowledge a request for the
1497 * ability to write to the BPF device. 1497 * ability to write to the BPF device.
1498 * Otherwise, return false but make a note that a selnotify() must be done. 1498 * Otherwise, return false but make a note that a selnotify() must be done.
1499 */ 1499 */
1500static int 1500static int
1501bpf_poll(struct file *fp, int events) 1501bpf_poll(struct file *fp, int events)
1502{ 1502{
1503 struct bpf_d *d = fp->f_bpf; 1503 struct bpf_d *d = fp->f_bpf;
1504 int revents; 1504 int revents;
1505 1505
1506 /* 1506 /*
1507 * Refresh the PID associated with this bpf file. 1507 * Refresh the PID associated with this bpf file.
1508 */ 1508 */
1509 mutex_enter(&bpf_mtx); 1509 mutex_enter(&bpf_mtx);
1510 d->bd_pid = curproc->p_pid; 1510 d->bd_pid = curproc->p_pid;
1511 1511
1512 revents = events & (POLLOUT | POLLWRNORM); 1512 revents = events & (POLLOUT | POLLWRNORM);
1513 if (events & (POLLIN | POLLRDNORM)) { 1513 if (events & (POLLIN | POLLRDNORM)) {
1514 /* 1514 /*
1515 * An imitation of the FIONREAD ioctl code. 1515 * An imitation of the FIONREAD ioctl code.
1516 */ 1516 */
1517 mutex_enter(d->bd_mtx); 1517 mutex_enter(d->bd_mtx);
1518 if (d->bd_hlen != 0 || 1518 if (d->bd_hlen != 0 ||
1519 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) && 1519 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1520 d->bd_slen != 0)) { 1520 d->bd_slen != 0)) {
1521 revents |= events & (POLLIN | POLLRDNORM); 1521 revents |= events & (POLLIN | POLLRDNORM);
1522 } else { 1522 } else {
1523 selrecord(curlwp, &d->bd_sel); 1523 selrecord(curlwp, &d->bd_sel);
1524 /* Start the read timeout if necessary */ 1524 /* Start the read timeout if necessary */
1525 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) { 1525 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1526 callout_reset(&d->bd_callout, d->bd_rtout, 1526 callout_reset(&d->bd_callout, d->bd_rtout,
1527 bpf_timed_out, d); 1527 bpf_timed_out, d);
1528 d->bd_state = BPF_WAITING; 1528 d->bd_state = BPF_WAITING;
1529 } 1529 }
1530 } 1530 }
1531 mutex_exit(d->bd_mtx); 1531 mutex_exit(d->bd_mtx);
1532 } 1532 }
1533 1533
1534 mutex_exit(&bpf_mtx); 1534 mutex_exit(&bpf_mtx);
1535 return (revents); 1535 return (revents);
1536} 1536}
1537 1537
1538static void 1538static void
1539filt_bpfrdetach(struct knote *kn) 1539filt_bpfrdetach(struct knote *kn)
1540{ 1540{
1541 struct bpf_d *d = kn->kn_hook; 1541 struct bpf_d *d = kn->kn_hook;
1542 1542
1543 mutex_enter(d->bd_buf_mtx); 1543 mutex_enter(d->bd_buf_mtx);
1544 selremove_knote(&d->bd_sel, kn); 1544 selremove_knote(&d->bd_sel, kn);
1545 mutex_exit(d->bd_buf_mtx); 1545 mutex_exit(d->bd_buf_mtx);
1546} 1546}
1547 1547
1548static int 1548static int
1549filt_bpfread(struct knote *kn, long hint) 1549filt_bpfread(struct knote *kn, long hint)
1550{ 1550{
1551 struct bpf_d *d = kn->kn_hook; 1551 struct bpf_d *d = kn->kn_hook;
1552 int rv; 1552 int rv;
1553 1553
1554 mutex_enter(d->bd_buf_mtx); 1554 mutex_enter(d->bd_buf_mtx);
1555 kn->kn_data = d->bd_hlen; 1555 kn->kn_data = d->bd_hlen;
1556 if (d->bd_immediate) 1556 if (d->bd_immediate)
1557 kn->kn_data += d->bd_slen; 1557 kn->kn_data += d->bd_slen;
1558 rv = (kn->kn_data > 0); 1558 rv = (kn->kn_data > 0);
1559 mutex_exit(d->bd_buf_mtx); 1559 mutex_exit(d->bd_buf_mtx);
1560 return rv; 1560 return rv;
1561} 1561}
1562 1562
1563static const struct filterops bpfread_filtops = { 1563static const struct filterops bpfread_filtops = {
1564 .f_isfd = 1, 1564 .f_isfd = 1,
1565 .f_attach = NULL, 1565 .f_attach = NULL,
1566 .f_detach = filt_bpfrdetach, 1566 .f_detach = filt_bpfrdetach,
1567 .f_event = filt_bpfread, 1567 .f_event = filt_bpfread,
1568}; 1568};
1569 1569
1570static int 1570static int
1571bpf_kqfilter(struct file *fp, struct knote *kn) 1571bpf_kqfilter(struct file *fp, struct knote *kn)
1572{ 1572{
1573 struct bpf_d *d = fp->f_bpf; 1573 struct bpf_d *d = fp->f_bpf;
1574 1574
1575 switch (kn->kn_filter) { 1575 switch (kn->kn_filter) {
1576 case EVFILT_READ: 1576 case EVFILT_READ:
1577 kn->kn_fop = &bpfread_filtops; 1577 kn->kn_fop = &bpfread_filtops;
1578 break; 1578 break;
1579 1579
1580 default: 1580 default:
1581 return (EINVAL); 1581 return (EINVAL);
1582 } 1582 }
1583 1583
1584 kn->kn_hook = d; 1584 kn->kn_hook = d;
1585 1585
1586 mutex_enter(d->bd_buf_mtx); 1586 mutex_enter(d->bd_buf_mtx);
1587 selrecord_knote(&d->bd_sel, kn); 1587 selrecord_knote(&d->bd_sel, kn);
1588 mutex_exit(d->bd_buf_mtx); 1588 mutex_exit(d->bd_buf_mtx);
1589 1589
1590 return (0); 1590 return (0);
1591} 1591}
1592 1592
1593/* 1593/*
1594 * Copy data from an mbuf chain into a buffer. This code is derived 1594 * Copy data from an mbuf chain into a buffer. This code is derived
1595 * from m_copydata in sys/uipc_mbuf.c. 1595 * from m_copydata in sys/uipc_mbuf.c.
1596 */ 1596 */
1597static void * 1597static void *
1598bpf_mcpy(void *dst_arg, const void *src_arg, size_t len) 1598bpf_mcpy(void *dst_arg, const void *src_arg, size_t len)
1599{ 1599{
1600 const struct mbuf *m; 1600 const struct mbuf *m;
1601 u_int count; 1601 u_int count;
1602 u_char *dst; 1602 u_char *dst;
1603 1603
1604 m = src_arg; 1604 m = src_arg;
1605 dst = dst_arg; 1605 dst = dst_arg;
1606 while (len > 0) { 1606 while (len > 0) {
1607 if (m == NULL) 1607 if (m == NULL)
1608 panic("bpf_mcpy"); 1608 panic("bpf_mcpy");
1609 count = uimin(m->m_len, len); 1609 count = uimin(m->m_len, len);
1610 memcpy(dst, mtod(m, const void *), count); 1610 memcpy(dst, mtod(m, const void *), count);
1611 m = m->m_next; 1611 m = m->m_next;
1612 dst += count; 1612 dst += count;
1613 len -= count; 1613 len -= count;
1614 } 1614 }
1615 return dst_arg; 1615 return dst_arg;
1616} 1616}
1617 1617
1618static inline u_int 1618static inline u_int
1619bpf_xfilter(struct bpf_filter **filter, void *pkt, u_int pktlen, u_int buflen) 1619bpf_xfilter(struct bpf_filter **filter, void *pkt, u_int pktlen, u_int buflen)
1620{ 1620{
1621 struct bpf_filter *filt; 1621 struct bpf_filter *filt;
1622 uint32_t mem[BPF_MEMWORDS]; 1622 uint32_t mem[BPF_MEMWORDS];
1623 bpf_args_t args = { 1623 bpf_args_t args = {
1624 .pkt = (const uint8_t *)pkt, 1624 .pkt = (const uint8_t *)pkt,
1625 .wirelen = pktlen, 1625 .wirelen = pktlen,
1626 .buflen = buflen, 1626 .buflen = buflen,
1627 .mem = mem, 1627 .mem = mem,
1628 .arg = NULL 1628 .arg = NULL
1629 }; 1629 };
1630 u_int slen; 1630 u_int slen;
1631 1631
1632 filt = atomic_load_consume(filter); 1632 filt = atomic_load_consume(filter);
1633 if (filt == NULL) /* No filter means accept all. */ 1633 if (filt == NULL) /* No filter means accept all. */
1634 return (u_int)-1; 1634 return (u_int)-1;
1635 1635
1636 if (filt->bf_jitcode != NULL) 1636 if (filt->bf_jitcode != NULL)
1637 slen = filt->bf_jitcode(NULL, &args); 1637 slen = filt->bf_jitcode(NULL, &args);
1638 else 1638 else
1639 slen = bpf_filter_ext(NULL, filt->bf_insn, &args); 1639 slen = bpf_filter_ext(NULL, filt->bf_insn, &args);
1640 return slen; 1640 return slen;
1641} 1641}
1642 1642
1643/* 1643/*
1644 * Dispatch a packet to all the listeners on interface bp. 1644 * Dispatch a packet to all the listeners on interface bp.
1645 * 1645 *
1646 * pkt pointer to the packet, either a data buffer or an mbuf chain 1646 * pkt pointer to the packet, either a data buffer or an mbuf chain
1647 * buflen buffer length, if pkt is a data buffer 1647 * buflen buffer length, if pkt is a data buffer
1648 * cpfn a function that can copy pkt into the listener's buffer 1648 * cpfn a function that can copy pkt into the listener's buffer
1649 * pktlen length of the packet 1649 * pktlen length of the packet
1650 * direction BPF_D_IN or BPF_D_OUT 1650 * direction BPF_D_IN or BPF_D_OUT
1651 */ 1651 */
1652static inline void 1652static inline void
1653bpf_deliver(struct bpf_if *bp, void *(*cpfn)(void *, const void *, size_t), 1653bpf_deliver(struct bpf_if *bp, void *(*cpfn)(void *, const void *, size_t),
1654 void *pkt, u_int pktlen, u_int buflen, const u_int direction) 1654 void *pkt, u_int pktlen, u_int buflen, const u_int direction)
1655{ 1655{
1656 bool gottime = false; 1656 bool gottime = false;
1657 struct timespec ts; 1657 struct timespec ts;
1658 struct bpf_d *d; 1658 struct bpf_d *d;
1659 int s; 1659 int s;
1660 u_int slen; 1660 u_int slen;
1661 1661
1662 KASSERT(!cpu_intr_p()); 1662 KASSERT(!cpu_intr_p());
1663 1663
1664 /* 1664 /*
1665 * Note that the IPL does not have to be raised at this point. 1665 * Note that the IPL does not have to be raised at this point.
1666 * The only problem that could arise here is that if two different 1666 * The only problem that could arise here is that if two different
1667 * interfaces shared any data. This is not the case. 1667 * interfaces shared any data. This is not the case.
1668 */ 1668 */
1669 s = pserialize_read_enter(); 1669 s = pserialize_read_enter();
1670 BPFIF_DLIST_READER_FOREACH(d, bp) { 1670 BPFIF_DLIST_READER_FOREACH(d, bp) {
1671 if (direction == BPF_D_IN) { 1671 if (direction == BPF_D_IN) {
1672 if (d->bd_direction == BPF_D_OUT) 1672 if (d->bd_direction == BPF_D_OUT)
1673 continue; 1673 continue;
1674 } else { /* BPF_D_OUT */ 1674 } else { /* BPF_D_OUT */
1675 if (d->bd_direction == BPF_D_IN) 1675 if (d->bd_direction == BPF_D_IN)
1676 continue; 1676 continue;
1677 } 1677 }
1678 1678
1679 atomic_inc_ulong(&d->bd_rcount); 1679 atomic_inc_ulong(&d->bd_rcount);
1680 BPF_STATINC(recv); 1680 BPF_STATINC(recv);
1681 1681
1682 slen = bpf_xfilter(&d->bd_rfilter, pkt, pktlen, buflen); 1682 slen = bpf_xfilter(&d->bd_rfilter, pkt, pktlen, buflen);
1683 if (slen == 0) 1683 if (slen == 0)
1684 continue; 1684 continue;
1685 1685
1686 if (!gottime) { 1686 if (!gottime) {
1687 gottime = true; 1687 gottime = true;
1688 nanotime(&ts); 1688 nanotime(&ts);
1689 } 1689 }
1690 /* Assume catchpacket doesn't sleep */ 1690 /* Assume catchpacket doesn't sleep */
1691 catchpacket(d, pkt, pktlen, slen, cpfn, &ts); 1691 catchpacket(d, pkt, pktlen, slen, cpfn, &ts);
1692 } 1692 }
1693 pserialize_read_exit(s); 1693 pserialize_read_exit(s);
1694} 1694}
1695 1695
1696/* 1696/*
1697 * Incoming linkage from device drivers, when the head of the packet is in 1697 * Incoming linkage from device drivers, when the head of the packet is in
1698 * a buffer, and the tail is in an mbuf chain. 1698 * a buffer, and the tail is in an mbuf chain.
1699 */ 1699 */
1700static void 1700static void
1701_bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m, 1701_bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m,
1702 u_int direction) 1702 u_int direction)
1703{ 1703{
1704 u_int pktlen; 1704 u_int pktlen;
1705 struct mbuf mb; 1705 struct mbuf mb;
1706 1706
1707 /* Skip outgoing duplicate packets. */ 1707 /* Skip outgoing duplicate packets. */
1708 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif_index == 0) { 1708 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif_index == 0) {
1709 m->m_flags &= ~M_PROMISC; 1709 m->m_flags &= ~M_PROMISC;
1710 return; 1710 return;
1711 } 1711 }
1712 1712
1713 pktlen = m_length(m) + dlen; 1713 pktlen = m_length(m) + dlen;
1714 1714
1715 /* 1715 /*
1716 * Craft on-stack mbuf suitable for passing to bpf_filter. 1716 * Craft on-stack mbuf suitable for passing to bpf_filter.
1717 * Note that we cut corners here; we only setup what's 1717 * Note that we cut corners here; we only setup what's
1718 * absolutely needed--this mbuf should never go anywhere else. 1718 * absolutely needed--this mbuf should never go anywhere else.
1719 */ 1719 */
1720 (void)memset(&mb, 0, sizeof(mb)); 1720 (void)memset(&mb, 0, sizeof(mb));
1721 mb.m_type = MT_DATA; 1721 mb.m_type = MT_DATA;
1722 mb.m_next = m; 1722 mb.m_next = m;
1723 mb.m_data = data; 1723 mb.m_data = data;
1724 mb.m_len = dlen; 1724 mb.m_len = dlen;
1725 1725
1726 bpf_deliver(bp, bpf_mcpy, &mb, pktlen, 0, direction); 1726 bpf_deliver(bp, bpf_mcpy, &mb, pktlen, 0, direction);
1727} 1727}
1728 1728
1729/* 1729/*
1730 * Incoming linkage from device drivers, when packet is in an mbuf chain. 1730 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1731 */ 1731 */
1732static void 1732static void
1733_bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction) 1733_bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction)
1734{ 1734{
1735 void *(*cpfn)(void *, const void *, size_t); 1735 void *(*cpfn)(void *, const void *, size_t);
1736 u_int pktlen, buflen; 1736 u_int pktlen, buflen;
1737 void *marg; 1737 void *marg;
1738 1738
1739 /* Skip outgoing duplicate packets. */ 1739 /* Skip outgoing duplicate packets. */
1740 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif_index == 0) { 1740 if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif_index == 0) {
1741 m->m_flags &= ~M_PROMISC; 1741 m->m_flags &= ~M_PROMISC;
1742 return; 1742 return;
1743 } 1743 }
1744 1744
1745 pktlen = m_length(m); 1745 pktlen = m_length(m);
1746 1746
1747 /* Skip zero-sized packets. */ 1747 /* Skip zero-sized packets. */
1748 if (__predict_false(pktlen == 0)) { 1748 if (__predict_false(pktlen == 0)) {
1749 return; 1749 return;
1750 } 1750 }
1751 1751
1752 if (pktlen == m->m_len) { 1752 if (pktlen == m->m_len) {
1753 cpfn = (void *)memcpy; 1753 cpfn = (void *)memcpy;
1754 marg = mtod(m, void *); 1754 marg = mtod(m, void *);
1755 buflen = pktlen; 1755 buflen = pktlen;
1756 KASSERT(buflen != 0); 1756 KASSERT(buflen != 0);
1757 } else { 1757 } else {
1758 cpfn = bpf_mcpy; 1758 cpfn = bpf_mcpy;
1759 marg = m; 1759 marg = m;
1760 buflen = 0; 1760 buflen = 0;
1761 } 1761 }
1762 1762
1763 bpf_deliver(bp, cpfn, marg, pktlen, buflen, direction); 1763 bpf_deliver(bp, cpfn, marg, pktlen, buflen, direction);
1764} 1764}
1765 1765
1766/* 1766/*
1767 * We need to prepend the address family as 1767 * We need to prepend the address family as
1768 * a four byte field. Cons up a dummy header 1768 * a four byte field. Cons up a dummy header
1769 * to pacify bpf. This is safe because bpf 1769 * to pacify bpf. This is safe because bpf
1770 * will only read from the mbuf (i.e., it won't 1770 * will only read from the mbuf (i.e., it won't
1771 * try to free it or keep a pointer a to it). 1771 * try to free it or keep a pointer a to it).
1772 */ 1772 */
1773static void 1773static void
1774_bpf_mtap_af(struct bpf_if *bp, uint32_t af, struct mbuf *m, u_int direction) 1774_bpf_mtap_af(struct bpf_if *bp, uint32_t af, struct mbuf *m, u_int direction)
1775{ 1775{
1776 struct mbuf m0; 1776 struct mbuf m0;
1777 1777
1778 m0.m_type = MT_DATA; 1778 m0.m_type = MT_DATA;
1779 m0.m_flags = 0; 1779 m0.m_flags = 0;
1780 m0.m_next = m; 1780 m0.m_next = m;
1781 m0.m_nextpkt = NULL; 1781 m0.m_nextpkt = NULL;
1782 m0.m_owner = NULL; 1782 m0.m_owner = NULL;
1783 m0.m_len = 4; 1783 m0.m_len = 4;
1784 m0.m_data = (char *)&af; 1784 m0.m_data = (char *)&af;
1785 1785
1786 _bpf_mtap(bp, &m0, direction); 1786 _bpf_mtap(bp, &m0, direction);
1787} 1787}
1788 1788
1789/* 1789/*
1790 * Put the SLIP pseudo-"link header" in place. 1790 * Put the SLIP pseudo-"link header" in place.
1791 * Note this M_PREPEND() should never fail, 1791 * Note this M_PREPEND() should never fail,
1792 * swince we know we always have enough space 1792 * swince we know we always have enough space
1793 * in the input buffer. 1793 * in the input buffer.
1794 */ 1794 */
1795static void 1795static void
1796_bpf_mtap_sl_in(struct bpf_if *bp, u_char *chdr, struct mbuf **m) 1796_bpf_mtap_sl_in(struct bpf_if *bp, u_char *chdr, struct mbuf **m)
1797{ 1797{
1798 u_char *hp; 1798 u_char *hp;
1799 1799
1800 M_PREPEND(*m, SLIP_HDRLEN, M_DONTWAIT); 1800 M_PREPEND(*m, SLIP_HDRLEN, M_DONTWAIT);
1801 if (*m == NULL) 1801 if (*m == NULL)
1802 return; 1802 return;
1803 1803
1804 hp = mtod(*m, u_char *); 1804 hp = mtod(*m, u_char *);
1805 hp[SLX_DIR] = SLIPDIR_IN; 1805 hp[SLX_DIR] = SLIPDIR_IN;
1806 (void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN); 1806 (void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
1807 1807
1808 _bpf_mtap(bp, *m, BPF_D_IN); 1808 _bpf_mtap(bp, *m, BPF_D_IN);
1809 1809
1810 m_adj(*m, SLIP_HDRLEN); 1810 m_adj(*m, SLIP_HDRLEN);
1811} 1811}
1812 1812
1813/* 1813/*
1814 * Put the SLIP pseudo-"link header" in 1814 * Put the SLIP pseudo-"link header" in
1815 * place. The compressed header is now 1815 * place. The compressed header is now
1816 * at the beginning of the mbuf. 1816 * at the beginning of the mbuf.
1817 */ 1817 */
1818static void 1818static void
1819_bpf_mtap_sl_out(struct bpf_if *bp, u_char *chdr, struct mbuf *m) 1819_bpf_mtap_sl_out(struct bpf_if *bp, u_char *chdr, struct mbuf *m)
1820{ 1820{
1821 struct mbuf m0; 1821 struct mbuf m0;
1822 u_char *hp; 1822 u_char *hp;
1823 1823
1824 m0.m_type = MT_DATA; 1824 m0.m_type = MT_DATA;
1825 m0.m_flags = 0; 1825 m0.m_flags = 0;
1826 m0.m_next = m; 1826 m0.m_next = m;
1827 m0.m_nextpkt = NULL; 1827 m0.m_nextpkt = NULL;
1828 m0.m_owner = NULL; 1828 m0.m_owner = NULL;
1829 m0.m_data = m0.m_dat; 1829 m0.m_data = m0.m_dat;
1830 m0.m_len = SLIP_HDRLEN; 1830 m0.m_len = SLIP_HDRLEN;
1831 1831
1832 hp = mtod(&m0, u_char *); 1832 hp = mtod(&m0, u_char *);
1833 1833
1834 hp[SLX_DIR] = SLIPDIR_OUT; 1834 hp[SLX_DIR] = SLIPDIR_OUT;
1835 (void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN); 1835 (void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
1836 1836
1837 _bpf_mtap(bp, &m0, BPF_D_OUT); 1837 _bpf_mtap(bp, &m0, BPF_D_OUT);
1838 m_freem(m); 1838 m_freem(m);
1839} 1839}
1840 1840
1841static struct mbuf * 1841static struct mbuf *
1842bpf_mbuf_enqueue(struct bpf_if *bp, struct mbuf *m) 1842bpf_mbuf_enqueue(struct bpf_if *bp, struct mbuf *m)
1843{ 1843{
1844 struct mbuf *dup; 1844 struct mbuf *dup;
1845 1845
1846 dup = m_dup(m, 0, M_COPYALL, M_NOWAIT); 1846 dup = m_dup(m, 0, M_COPYALL, M_NOWAIT);
1847 if (dup == NULL) 1847 if (dup == NULL)
1848 return NULL; 1848 return NULL;
1849 1849
1850 if (bp->bif_mbuf_tail != NULL) { 1850 if (bp->bif_mbuf_tail != NULL) {
1851 bp->bif_mbuf_tail->m_nextpkt = dup; 1851 bp->bif_mbuf_tail->m_nextpkt = dup;
1852 } else { 1852 } else {
1853 bp->bif_mbuf_head = dup; 1853 bp->bif_mbuf_head = dup;
1854 } 1854 }
1855 bp->bif_mbuf_tail = dup; 1855 bp->bif_mbuf_tail = dup;
1856#ifdef BPF_MTAP_SOFTINT_DEBUG 1856#ifdef BPF_MTAP_SOFTINT_DEBUG
1857 log(LOG_DEBUG, "%s: enqueued mbuf=%p to %s\n", 1857 log(LOG_DEBUG, "%s: enqueued mbuf=%p to %s\n",
1858 __func__, dup, bp->bif_ifp->if_xname); 1858 __func__, dup, bp->bif_ifp->if_xname);
1859#endif 1859#endif
1860 1860
1861 return dup; 1861 return dup;
1862} 1862}
1863 1863
1864static struct mbuf * 1864static struct mbuf *
1865bpf_mbuf_dequeue(struct bpf_if *bp) 1865bpf_mbuf_dequeue(struct bpf_if *bp)
1866{ 1866{
1867 struct mbuf *m; 1867 struct mbuf *m;
1868 int s; 1868 int s;
1869 1869
1870 /* XXX NOMPSAFE: assumed running on one CPU */ 1870 /* XXX NOMPSAFE: assumed running on one CPU */
1871 s = splnet(); 1871 s = splnet();
1872 m = bp->bif_mbuf_head; 1872 m = bp->bif_mbuf_head;
1873 if (m != NULL) { 1873 if (m != NULL) {
1874 bp->bif_mbuf_head = m->m_nextpkt; 1874 bp->bif_mbuf_head = m->m_nextpkt;
1875 m->m_nextpkt = NULL; 1875 m->m_nextpkt = NULL;
1876 1876
1877 if (bp->bif_mbuf_head == NULL) 1877 if (bp->bif_mbuf_head == NULL)
1878 bp->bif_mbuf_tail = NULL; 1878 bp->bif_mbuf_tail = NULL;
1879#ifdef BPF_MTAP_SOFTINT_DEBUG 1879#ifdef BPF_MTAP_SOFTINT_DEBUG
1880 log(LOG_DEBUG, "%s: dequeued mbuf=%p from %s\n", 1880 log(LOG_DEBUG, "%s: dequeued mbuf=%p from %s\n",
1881 __func__, m, bp->bif_ifp->if_xname); 1881 __func__, m, bp->bif_ifp->if_xname);
1882#endif 1882#endif
1883 } 1883 }
1884 splx(s); 1884 splx(s);
1885 1885
1886 return m; 1886 return m;
1887} 1887}
1888 1888
1889static void 1889static void
1890bpf_mtap_si(void *arg) 1890bpf_mtap_si(void *arg)
1891{ 1891{
1892 struct bpf_if *bp = arg; 1892 struct bpf_if *bp = arg;
1893 struct mbuf *m; 1893 struct mbuf *m;
1894 1894
1895 while ((m = bpf_mbuf_dequeue(bp)) != NULL) { 1895 while ((m = bpf_mbuf_dequeue(bp)) != NULL) {
1896#ifdef BPF_MTAP_SOFTINT_DEBUG 1896#ifdef BPF_MTAP_SOFTINT_DEBUG
1897 log(LOG_DEBUG, "%s: tapping mbuf=%p on %s\n", 1897 log(LOG_DEBUG, "%s: tapping mbuf=%p on %s\n",
1898 __func__, m, bp->bif_ifp->if_xname); 1898 __func__, m, bp->bif_ifp->if_xname);
1899#endif 1899#endif
1900 bpf_ops->bpf_mtap(bp, m, BPF_D_IN); 1900 bpf_ops->bpf_mtap(bp, m, BPF_D_IN);
1901 m_freem(m); 1901 m_freem(m);
1902 } 1902 }
1903} 1903}
1904 1904
1905static void 1905static void
1906_bpf_mtap_softint(struct ifnet *ifp, struct mbuf *m) 1906_bpf_mtap_softint(struct ifnet *ifp, struct mbuf *m)
1907{ 1907{
1908 struct bpf_if *bp = ifp->if_bpf; 1908 struct bpf_if *bp = ifp->if_bpf;
1909 struct mbuf *dup; 1909 struct mbuf *dup;
1910 1910
1911 KASSERT(cpu_intr_p()); 1911 KASSERT(cpu_intr_p());
1912 1912
1913 /* To avoid extra invocations of the softint */ 1913 /* To avoid extra invocations of the softint */
1914 if (BPFIF_DLIST_READER_EMPTY(bp)) 1914 if (BPFIF_DLIST_READER_EMPTY(bp))
1915 return; 1915 return;
1916 KASSERT(bp->bif_si != NULL); 1916 KASSERT(bp->bif_si != NULL);
1917 1917
1918 dup = bpf_mbuf_enqueue(bp, m); 1918 dup = bpf_mbuf_enqueue(bp, m);
1919 if (dup != NULL) 1919 if (dup != NULL)
1920 softint_schedule(bp->bif_si); 1920 softint_schedule(bp->bif_si);
1921} 1921}
1922 1922
1923static int 1923static int
1924bpf_hdrlen(struct bpf_d *d) 1924bpf_hdrlen(struct bpf_d *d)
1925{ 1925{
1926 int hdrlen = d->bd_bif->bif_hdrlen; 1926 int hdrlen = d->bd_bif->bif_hdrlen;
1927 /* 1927 /*
1928 * Compute the length of the bpf header. This is not necessarily 1928 * Compute the length of the bpf header. This is not necessarily
1929 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1929 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1930 * that the network layer header begins on a longword boundary (for 1930 * that the network layer header begins on a longword boundary (for
1931 * performance reasons and to alleviate alignment restrictions). 1931 * performance reasons and to alleviate alignment restrictions).
1932 */ 1932 */
1933#ifdef _LP64 1933#ifdef _LP64
1934 if (d->bd_compat32) 1934 if (d->bd_compat32)
1935 return (BPF_WORDALIGN32(hdrlen + SIZEOF_BPF_HDR32) - hdrlen); 1935 return (BPF_WORDALIGN32(hdrlen + SIZEOF_BPF_HDR32) - hdrlen);
1936 else 1936 else
1937#endif 1937#endif
1938 return (BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen); 1938 return (BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen);
1939} 1939}
1940 1940
1941/* 1941/*
1942 * Move the packet data from interface memory (pkt) into the 1942 * Move the packet data from interface memory (pkt) into the
1943 * store buffer. Call the wakeup functions if it's time to wakeup 1943 * store buffer. Call the wakeup functions if it's time to wakeup
1944 * a listener (buffer full), "cpfn" is the routine called to do the 1944 * a listener (buffer full), "cpfn" is the routine called to do the
1945 * actual data transfer. memcpy is passed in to copy contiguous chunks, 1945 * actual data transfer. memcpy is passed in to copy contiguous chunks,
1946 * while bpf_mcpy is passed in to copy mbuf chains. In the latter case, 1946 * while bpf_mcpy is passed in to copy mbuf chains. In the latter case,
1947 * pkt is really an mbuf. 1947 * pkt is really an mbuf.
1948 */ 1948 */
1949static void 1949static void
1950catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen, 1950catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1951 void *(*cpfn)(void *, const void *, size_t), struct timespec *ts) 1951 void *(*cpfn)(void *, const void *, size_t), struct timespec *ts)
1952{ 1952{
1953 char *h; 1953 char *h;
1954 int totlen, curlen, caplen; 1954 int totlen, curlen, caplen;
1955 int hdrlen = bpf_hdrlen(d); 1955 int hdrlen = bpf_hdrlen(d);
1956 int do_wakeup = 0; 1956 int do_wakeup = 0;
1957 1957
1958 atomic_inc_ulong(&d->bd_ccount); 1958 atomic_inc_ulong(&d->bd_ccount);
1959 BPF_STATINC(capt); 1959 BPF_STATINC(capt);
1960 /* 1960 /*
1961 * Figure out how many bytes to move. If the packet is 1961 * Figure out how many bytes to move. If the packet is
1962 * greater or equal to the snapshot length, transfer that 1962 * greater or equal to the snapshot length, transfer that
1963 * much. Otherwise, transfer the whole packet (unless 1963 * much. Otherwise, transfer the whole packet (unless
1964 * we hit the buffer size limit). 1964 * we hit the buffer size limit).
1965 */ 1965 */
1966 totlen = hdrlen + uimin(snaplen, pktlen); 1966 totlen = hdrlen + uimin(snaplen, pktlen);
1967 if (totlen > d->bd_bufsize) 1967 if (totlen > d->bd_bufsize)
1968 totlen = d->bd_bufsize; 1968 totlen = d->bd_bufsize;
1969 /* 1969 /*
1970 * If we adjusted totlen to fit the bufsize, it could be that 1970 * If we adjusted totlen to fit the bufsize, it could be that
1971 * totlen is smaller than hdrlen because of the link layer header. 1971 * totlen is smaller than hdrlen because of the link layer header.
1972 */ 1972 */
1973 caplen = totlen - hdrlen; 1973 caplen = totlen - hdrlen;
1974 if (caplen < 0) 1974 if (caplen < 0)
1975 caplen = 0; 1975 caplen = 0;
1976 1976
1977 mutex_enter(d->bd_buf_mtx); 1977 mutex_enter(d->bd_buf_mtx);
1978 /* 1978 /*
1979 * Round up the end of the previous packet to the next longword. 1979 * Round up the end of the previous packet to the next longword.
1980 */ 1980 */
1981#ifdef _LP64 1981#ifdef _LP64
1982 if (d->bd_compat32) 1982 if (d->bd_compat32)
1983 curlen = BPF_WORDALIGN32(d->bd_slen); 1983 curlen = BPF_WORDALIGN32(d->bd_slen);
1984 else 1984 else
1985#endif 1985#endif
1986 curlen = BPF_WORDALIGN(d->bd_slen); 1986 curlen = BPF_WORDALIGN(d->bd_slen);
1987 if (curlen + totlen > d->bd_bufsize) { 1987 if (curlen + totlen > d->bd_bufsize) {
1988 /* 1988 /*
1989 * This packet will overflow the storage buffer. 1989 * This packet will overflow the storage buffer.
1990 * Rotate the buffers if we can, then wakeup any 1990 * Rotate the buffers if we can, then wakeup any
1991 * pending reads. 1991 * pending reads.
1992 */ 1992 */
1993 if (d->bd_fbuf == NULL) { 1993 if (d->bd_fbuf == NULL) {
1994 mutex_exit(d->bd_buf_mtx); 1994 mutex_exit(d->bd_buf_mtx);
1995 /* 1995 /*
1996 * We haven't completed the previous read yet, 1996 * We haven't completed the previous read yet,
1997 * so drop the packet. 1997 * so drop the packet.
1998 */ 1998 */
1999 atomic_inc_ulong(&d->bd_dcount); 1999 atomic_inc_ulong(&d->bd_dcount);
2000 BPF_STATINC(drop); 2000 BPF_STATINC(drop);
2001 return; 2001 return;
2002 } 2002 }
2003 ROTATE_BUFFERS(d); 2003 ROTATE_BUFFERS(d);
2004 do_wakeup = 1; 2004 do_wakeup = 1;
2005 curlen = 0; 2005 curlen = 0;
2006 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) { 2006 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) {
2007 /* 2007 /*
2008 * Immediate mode is set, or the read timeout has 2008 * Immediate mode is set, or the read timeout has
2009 * already expired during a select call. A packet 2009 * already expired during a select call. A packet
2010 * arrived, so the reader should be woken up. 2010 * arrived, so the reader should be woken up.
2011 */ 2011 */
2012 do_wakeup = 1; 2012 do_wakeup = 1;
2013 } 2013 }
2014 2014
2015 /* 2015 /*
2016 * Append the bpf header. 2016 * Append the bpf header.
2017 */ 2017 */
2018 h = (char *)d->bd_sbuf + curlen; 2018 h = (char *)d->bd_sbuf + curlen;
2019#ifdef _LP64 2019#ifdef _LP64
2020 if (d->bd_compat32) { 2020 if (d->bd_compat32) {
2021 struct bpf_hdr32 *hp32; 2021 struct bpf_hdr32 *hp32;
2022 2022
2023 hp32 = (struct bpf_hdr32 *)h; 2023 hp32 = (struct bpf_hdr32 *)h;
2024 hp32->bh_tstamp.tv_sec = ts->tv_sec; 2024 hp32->bh_tstamp.tv_sec = ts->tv_sec;
2025 hp32->bh_tstamp.tv_usec = ts->tv_nsec / 1000; 2025 hp32->bh_tstamp.tv_usec = ts->tv_nsec / 1000;
2026 hp32->bh_datalen = pktlen; 2026 hp32->bh_datalen = pktlen;
2027 hp32->bh_hdrlen = hdrlen; 2027 hp32->bh_hdrlen = hdrlen;
2028 hp32->bh_caplen = caplen; 2028 hp32->bh_caplen = caplen;
2029 } else 2029 } else
2030#endif 2030#endif
2031 { 2031 {
2032 struct bpf_hdr *hp; 2032 struct bpf_hdr *hp;
2033 2033
2034 hp = (struct bpf_hdr *)h; 2034 hp = (struct bpf_hdr *)h;
2035 hp->bh_tstamp.tv_sec = ts->tv_sec; 2035 hp->bh_tstamp.tv_sec = ts->tv_sec;
2036 hp->bh_tstamp.tv_usec = ts->tv_nsec / 1000; 2036 hp->bh_tstamp.tv_usec = ts->tv_nsec / 1000;
2037 hp->bh_datalen = pktlen; 2037 hp->bh_datalen = pktlen;
2038 hp->bh_hdrlen = hdrlen; 2038 hp->bh_hdrlen = hdrlen;
2039 hp->bh_caplen = caplen; 2039 hp->bh_caplen = caplen;
2040 } 2040 }
2041 2041
2042 /* 2042 /*
2043 * Copy the packet data into the store buffer and update its length. 2043 * Copy the packet data into the store buffer and update its length.
2044 */ 2044 */
2045 (*cpfn)(h + hdrlen, pkt, caplen); 2045 (*cpfn)(h + hdrlen, pkt, caplen);
2046 d->bd_slen = curlen + totlen; 2046 d->bd_slen = curlen + totlen;
2047 mutex_exit(d->bd_buf_mtx); 2047 mutex_exit(d->bd_buf_mtx);
2048 2048
2049 /* 2049 /*
2050 * Call bpf_wakeup after bd_slen has been updated so that kevent(2) 2050 * Call bpf_wakeup after bd_slen has been updated so that kevent(2)
2051 * will cause filt_bpfread() to be called with it adjusted. 2051 * will cause filt_bpfread() to be called with it adjusted.
2052 */ 2052 */
2053 if (do_wakeup) 2053 if (do_wakeup)
2054 bpf_wakeup(d); 2054 bpf_wakeup(d);
2055} 2055}
2056 2056
2057/* 2057/*
2058 * Initialize all nonzero fields of a descriptor. 2058 * Initialize all nonzero fields of a descriptor.
2059 */ 2059 */
2060static int 2060static int
2061bpf_allocbufs(struct bpf_d *d) 2061bpf_allocbufs(struct bpf_d *d)
2062{ 2062{
2063 2063
2064 d->bd_fbuf = kmem_zalloc(d->bd_bufsize, KM_NOSLEEP); 2064 d->bd_fbuf = kmem_zalloc(d->bd_bufsize, KM_NOSLEEP);
2065 if (!d->bd_fbuf) 2065 if (!d->bd_fbuf)
2066 return (ENOBUFS); 2066 return (ENOBUFS);
2067 d->bd_sbuf = kmem_zalloc(d->bd_bufsize, KM_NOSLEEP); 2067 d->bd_sbuf = kmem_zalloc(d->bd_bufsize, KM_NOSLEEP);
2068 if (!d->bd_sbuf) { 2068 if (!d->bd_sbuf) {
2069 kmem_free(d->bd_fbuf, d->bd_bufsize); 2069 kmem_free(d->bd_fbuf, d->bd_bufsize);
2070 return (ENOBUFS); 2070 return (ENOBUFS);
2071 } 2071 }
2072 d->bd_slen = 0; 2072 d->bd_slen = 0;
2073 d->bd_hlen = 0; 2073 d->bd_hlen = 0;
2074 return (0); 2074 return (0);
2075} 2075}
2076 2076
2077static void 2077static void
2078bpf_free_filter(struct bpf_filter *filter) 2078bpf_free_filter(struct bpf_filter *filter)
2079{ 2079{
2080 2080
2081 KASSERT(filter != NULL); 2081 KASSERT(filter != NULL);
2082 KASSERT(filter->bf_insn != NULL); 2082 KASSERT(filter->bf_insn != NULL);
2083 2083
2084 kmem_free(filter->bf_insn, filter->bf_size); 2084 kmem_free(filter->bf_insn, filter->bf_size);
2085 if (filter->bf_jitcode != NULL) 2085 if (filter->bf_jitcode != NULL)
2086 bpf_jit_freecode(filter->bf_jitcode); 2086 bpf_jit_freecode(filter->bf_jitcode);
2087 kmem_free(filter, sizeof(*filter)); 2087 kmem_free(filter, sizeof(*filter));
2088} 2088}
2089 2089
2090/* 2090/*
2091 * Free buffers currently in use by a descriptor. 2091 * Free buffers currently in use by a descriptor.
2092 * Called on close. 2092 * Called on close.
2093 */ 2093 */
2094static void 2094static void
2095bpf_freed(struct bpf_d *d) 2095bpf_freed(struct bpf_d *d)
2096{ 2096{
2097 /* 2097 /*
2098 * We don't need to lock out interrupts since this descriptor has 2098 * We don't need to lock out interrupts since this descriptor has
2099 * been detached from its interface and it yet hasn't been marked 2099 * been detached from its interface and it yet hasn't been marked
2100 * free. 2100 * free.
2101 */ 2101 */
2102 if (d->bd_sbuf != NULL) { 2102 if (d->bd_sbuf != NULL) {
2103 kmem_free(d->bd_sbuf, d->bd_bufsize); 2103 kmem_free(d->bd_sbuf, d->bd_bufsize);
2104 if (d->bd_hbuf != NULL) 2104 if (d->bd_hbuf != NULL)
2105 kmem_free(d->bd_hbuf, d->bd_bufsize); 2105 kmem_free(d->bd_hbuf, d->bd_bufsize);
2106 if (d->bd_fbuf != NULL) 2106 if (d->bd_fbuf != NULL)
2107 kmem_free(d->bd_fbuf, d->bd_bufsize); 2107 kmem_free(d->bd_fbuf, d->bd_bufsize);
2108 } 2108 }
2109 if (d->bd_rfilter != NULL) { 2109 if (d->bd_rfilter != NULL) {
2110 bpf_free_filter(d->bd_rfilter); 2110 bpf_free_filter(d->bd_rfilter);
2111 d->bd_rfilter = NULL; 2111 d->bd_rfilter = NULL;
2112 } 2112 }
2113 if (d->bd_wfilter != NULL) { 2113 if (d->bd_wfilter != NULL) {
2114 bpf_free_filter(d->bd_wfilter); 2114 bpf_free_filter(d->bd_wfilter);
2115 d->bd_wfilter = NULL; 2115 d->bd_wfilter = NULL;
2116 } 2116 }
2117 d->bd_jitcode = NULL; 2117 d->bd_jitcode = NULL;
2118} 2118}
2119 2119
2120/* 2120/*
2121 * Attach an interface to bpf. dlt is the link layer type; 2121 * Attach an interface to bpf. dlt is the link layer type;
2122 * hdrlen is the fixed size of the link header for the specified dlt 2122 * hdrlen is the fixed size of the link header for the specified dlt
2123 * (variable length headers not yet supported). 2123 * (variable length headers not yet supported).
2124 */ 2124 */
2125static void 2125static void
2126_bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp) 2126_bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
2127{ 2127{
2128 struct bpf_if *bp; 2128 struct bpf_if *bp;
2129 bp = kmem_alloc(sizeof(*bp), KM_NOSLEEP); 2129 bp = kmem_alloc(sizeof(*bp), KM_NOSLEEP);
2130 if (bp == NULL) 2130 if (bp == NULL)
2131 panic("%s: out of memory", __func__); 2131 panic("%s: out of memory", __func__);
2132 2132
2133 mutex_enter(&bpf_mtx); 2133 mutex_enter(&bpf_mtx);
2134 bp->bif_driverp = driverp; 2134 bp->bif_driverp = driverp;
2135 bp->bif_ifp = ifp; 2135 bp->bif_ifp = ifp;
2136 bp->bif_dlt = dlt; 2136 bp->bif_dlt = dlt;
2137 bp->bif_si = NULL; 2137 bp->bif_si = NULL;
2138 BPF_IFLIST_ENTRY_INIT(bp); 2138 BPF_IFLIST_ENTRY_INIT(bp);
2139 PSLIST_INIT(&bp->bif_dlist_head); 2139 PSLIST_INIT(&bp->bif_dlist_head);
2140 psref_target_init(&bp->bif_psref, bpf_psref_class); 2140 psref_target_init(&bp->bif_psref, bpf_psref_class);
2141 SLIST_INIT(&bp->bif_trackers); 2141 SLIST_INIT(&bp->bif_trackers);
2142 2142
2143 BPF_IFLIST_WRITER_INSERT_HEAD(bp); 2143 BPF_IFLIST_WRITER_INSERT_HEAD(bp);
2144 2144
2145 *bp->bif_driverp = NULL; 2145 *bp->bif_driverp = NULL;
2146 2146
2147 bp->bif_hdrlen = hdrlen; 2147 bp->bif_hdrlen = hdrlen;
2148 mutex_exit(&bpf_mtx); 2148 mutex_exit(&bpf_mtx);
2149#if 0 2149#if 0
2150 printf("bpf: %s attached with dlt %x\n", ifp->if_xname, dlt); 2150 printf("bpf: %s attached with dlt %x\n", ifp->if_xname, dlt);
2151#endif 2151#endif
2152} 2152}
2153 2153
2154static void 2154static void
2155_bpf_mtap_softint_init(struct ifnet *ifp) 2155_bpf_mtap_softint_init(struct ifnet *ifp)
2156{ 2156{
2157 struct bpf_if *bp; 2157 struct bpf_if *bp;
2158 2158
2159 mutex_enter(&bpf_mtx); 2159 mutex_enter(&bpf_mtx);
2160 BPF_IFLIST_WRITER_FOREACH(bp) { 2160 BPF_IFLIST_WRITER_FOREACH(bp) {
2161 if (bp->bif_ifp != ifp) 2161 if (bp->bif_ifp != ifp)
2162 continue; 2162 continue;
2163 2163
2164 bp->bif_mbuf_head = NULL; 2164 bp->bif_mbuf_head = NULL;
2165 bp->bif_mbuf_tail = NULL; 2165 bp->bif_mbuf_tail = NULL;
2166 bp->bif_si = softint_establish(SOFTINT_NET, bpf_mtap_si, bp); 2166 bp->bif_si = softint_establish(SOFTINT_NET, bpf_mtap_si, bp);
2167 if (bp->bif_si == NULL) 2167 if (bp->bif_si == NULL)
2168 panic("%s: softint_establish() failed", __func__); 2168 panic("%s: softint_establish() failed", __func__);
2169 break; 2169 break;
2170 } 2170 }
2171 mutex_exit(&bpf_mtx); 2171 mutex_exit(&bpf_mtx);
2172 2172
2173 if (bp == NULL) 2173 if (bp == NULL)
2174 panic("%s: no bpf_if found for %s", __func__, ifp->if_xname); 2174 panic("%s: no bpf_if found for %s", __func__, ifp->if_xname);
2175} 2175}
2176 2176
2177/* 2177/*
2178 * Remove an interface from bpf. 2178 * Remove an interface from bpf.
2179 */ 2179 */
2180static void 2180static void
2181_bpfdetach(struct ifnet *ifp) 2181_bpfdetach(struct ifnet *ifp)
2182{ 2182{
2183 struct bpf_if *bp; 2183 struct bpf_if *bp;
2184 struct bpf_d *d; 2184 struct bpf_d *d;
2185 int s; 2185 int s;
2186 2186
2187 mutex_enter(&bpf_mtx); 2187 mutex_enter(&bpf_mtx);
2188 /* Nuke the vnodes for any open instances */ 2188 /* Nuke the vnodes for any open instances */
2189 again_d: 2189 again_d:
2190 BPF_DLIST_WRITER_FOREACH(d) { 2190 BPF_DLIST_WRITER_FOREACH(d) {
2191 mutex_enter(d->bd_mtx); 2191 mutex_enter(d->bd_mtx);
2192 if (d->bd_bif != NULL && d->bd_bif->bif_ifp == ifp) { 2192 if (d->bd_bif != NULL && d->bd_bif->bif_ifp == ifp) {
2193 /* 2193 /*
2194 * Detach the descriptor from an interface now. 2194 * Detach the descriptor from an interface now.
2195 * It will be free'ed later by close routine. 2195 * It will be free'ed later by close routine.
2196 */ 2196 */
2197 d->bd_promisc = 0; /* we can't touch device. */ 
2198 bpf_detachd(d); 2197 bpf_detachd(d);
2199 mutex_exit(d->bd_mtx); 2198 mutex_exit(d->bd_mtx);
2200 goto again_d; 2199 goto again_d;
2201 } 2200 }
2202 mutex_exit(d->bd_mtx); 2201 mutex_exit(d->bd_mtx);
2203 } 2202 }
2204 2203
2205 again: 2204 again:
2206 BPF_IFLIST_WRITER_FOREACH(bp) { 2205 BPF_IFLIST_WRITER_FOREACH(bp) {
2207 if (bp->bif_ifp == ifp) { 2206 if (bp->bif_ifp == ifp) {
2208 BPF_IFLIST_WRITER_REMOVE(bp); 2207 BPF_IFLIST_WRITER_REMOVE(bp);
2209 2208
2210 pserialize_perform(bpf_psz); 2209 pserialize_perform(bpf_psz);
2211 psref_target_destroy(&bp->bif_psref, bpf_psref_class); 2210 psref_target_destroy(&bp->bif_psref, bpf_psref_class);
2212 2211
2213 while (!SLIST_EMPTY(&bp->bif_trackers)) { 2212 while (!SLIST_EMPTY(&bp->bif_trackers)) {
2214 struct bpf_event_tracker *t = 2213 struct bpf_event_tracker *t =
2215 SLIST_FIRST(&bp->bif_trackers); 2214 SLIST_FIRST(&bp->bif_trackers);
2216 SLIST_REMOVE_HEAD(&bp->bif_trackers, 2215 SLIST_REMOVE_HEAD(&bp->bif_trackers,
2217 bet_entries); 2216 bet_entries);
2218 kmem_free(t, sizeof(*t)); 2217 kmem_free(t, sizeof(*t));
2219 } 2218 }
2220 2219
2221 BPF_IFLIST_ENTRY_DESTROY(bp); 2220 BPF_IFLIST_ENTRY_DESTROY(bp);
2222 if (bp->bif_si != NULL) { 2221 if (bp->bif_si != NULL) {
2223 /* XXX NOMPSAFE: assumed running on one CPU */ 2222 /* XXX NOMPSAFE: assumed running on one CPU */
2224 s = splnet(); 2223 s = splnet();
2225 while (bp->bif_mbuf_head != NULL) { 2224 while (bp->bif_mbuf_head != NULL) {
2226 struct mbuf *m = bp->bif_mbuf_head; 2225 struct mbuf *m = bp->bif_mbuf_head;
2227 bp->bif_mbuf_head = m->m_nextpkt; 2226 bp->bif_mbuf_head = m->m_nextpkt;
2228 m_freem(m); 2227 m_freem(m);
2229 } 2228 }
2230 splx(s); 2229 splx(s);
2231 softint_disestablish(bp->bif_si); 2230 softint_disestablish(bp->bif_si);
2232 } 2231 }
2233 kmem_free(bp, sizeof(*bp)); 2232 kmem_free(bp, sizeof(*bp));
2234 goto again; 2233 goto again;
2235 } 2234 }
2236 } 2235 }
2237 mutex_exit(&bpf_mtx); 2236 mutex_exit(&bpf_mtx);
2238} 2237}
2239 2238
2240/* 2239/*
2241 * Change the data link type of a interface. 2240 * Change the data link type of a interface.
2242 */ 2241 */
2243static void 2242static void
2244_bpf_change_type(struct ifnet *ifp, u_int dlt, u_int hdrlen) 2243_bpf_change_type(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2245{ 2244{
2246 struct bpf_if *bp; 2245 struct bpf_if *bp;
2247 2246
2248 mutex_enter(&bpf_mtx); 2247 mutex_enter(&bpf_mtx);
2249 BPF_IFLIST_WRITER_FOREACH(bp) { 2248 BPF_IFLIST_WRITER_FOREACH(bp) {
2250 if (bp->bif_driverp == &ifp->if_bpf) 2249 if (bp->bif_driverp == &ifp->if_bpf)
2251 break; 2250 break;
2252 } 2251 }
2253 if (bp == NULL) 2252 if (bp == NULL)
2254 panic("bpf_change_type"); 2253 panic("bpf_change_type");
2255 2254
2256 bp->bif_dlt = dlt; 2255 bp->bif_dlt = dlt;
2257 2256
2258 bp->bif_hdrlen = hdrlen; 2257 bp->bif_hdrlen = hdrlen;
2259 mutex_exit(&bpf_mtx); 2258 mutex_exit(&bpf_mtx);
2260} 2259}
2261 2260
2262/* 2261/*
2263 * Get a list of available data link type of the interface. 2262 * Get a list of available data link type of the interface.
2264 */ 2263 */
2265static int 2264static int
2266bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl) 2265bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
2267{ 2266{
2268 int n, error; 2267 int n, error;
2269 struct ifnet *ifp; 2268 struct ifnet *ifp;
2270 struct bpf_if *bp; 2269 struct bpf_if *bp;
2271 int s, bound; 2270 int s, bound;
2272 2271
2273 KASSERT(mutex_owned(d->bd_mtx)); 2272 KASSERT(mutex_owned(d->bd_mtx));
2274 2273
2275 ifp = d->bd_bif->bif_ifp; 2274 ifp = d->bd_bif->bif_ifp;
2276 n = 0; 2275 n = 0;
2277 error = 0; 2276 error = 0;
2278 2277
2279 bound = curlwp_bind(); 2278 bound = curlwp_bind();
2280 s = pserialize_read_enter(); 2279 s = pserialize_read_enter();
2281 BPF_IFLIST_READER_FOREACH(bp) { 2280 BPF_IFLIST_READER_FOREACH(bp) {
2282 if (bp->bif_ifp != ifp) 2281 if (bp->bif_ifp != ifp)
2283 continue; 2282 continue;
2284 if (bfl->bfl_list != NULL) { 2283 if (bfl->bfl_list != NULL) {
2285 struct psref psref; 2284 struct psref psref;
2286 2285
2287 if (n >= bfl->bfl_len) { 2286 if (n >= bfl->bfl_len) {
2288 pserialize_read_exit(s); 2287 pserialize_read_exit(s);
2289 return ENOMEM; 2288 return ENOMEM;
2290 } 2289 }
2291 2290
2292 bpf_if_acquire(bp, &psref); 2291 bpf_if_acquire(bp, &psref);
2293 pserialize_read_exit(s); 2292 pserialize_read_exit(s);
2294 2293
2295 error = copyout(&bp->bif_dlt, 2294 error = copyout(&bp->bif_dlt,
2296 bfl->bfl_list + n, sizeof(u_int)); 2295 bfl->bfl_list + n, sizeof(u_int));
2297 2296
2298 s = pserialize_read_enter(); 2297 s = pserialize_read_enter();
2299 bpf_if_release(bp, &psref); 2298 bpf_if_release(bp, &psref);
2300 } 2299 }
2301 n++; 2300 n++;
2302 } 2301 }
2303 pserialize_read_exit(s); 2302 pserialize_read_exit(s);
2304 curlwp_bindx(bound); 2303 curlwp_bindx(bound);
2305 2304
2306 bfl->bfl_len = n; 2305 bfl->bfl_len = n;
2307 return error; 2306 return error;
2308} 2307}
2309 2308
2310/* 2309/*
2311 * Set the data link type of a BPF instance. 2310 * Set the data link type of a BPF instance.
2312 */ 2311 */
2313static int 2312static int
2314bpf_setdlt(struct bpf_d *d, u_int dlt) 2313bpf_setdlt(struct bpf_d *d, u_int dlt)
2315{ 2314{
2316 int error, opromisc; 2315 int error, opromisc;
2317 struct ifnet *ifp; 2316 struct ifnet *ifp;
2318 struct bpf_if *bp; 2317 struct bpf_if *bp;
2319 2318
2320 KASSERT(mutex_owned(&bpf_mtx)); 2319 KASSERT(mutex_owned(&bpf_mtx));
2321 KASSERT(mutex_owned(d->bd_mtx)); 2320 KASSERT(mutex_owned(d->bd_mtx));
2322 2321
2323 if (d->bd_bif->bif_dlt == dlt) 2322 if (d->bd_bif->bif_dlt == dlt)
2324 return 0; 2323 return 0;
2325 ifp = d->bd_bif->bif_ifp; 2324 ifp = d->bd_bif->bif_ifp;
2326 BPF_IFLIST_WRITER_FOREACH(bp) { 2325 BPF_IFLIST_WRITER_FOREACH(bp) {
2327 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt) 2326 if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
2328 break; 2327 break;
2329 } 2328 }
2330 if (bp == NULL) 2329 if (bp == NULL)
2331 return EINVAL; 2330 return EINVAL;
2332 opromisc = d->bd_promisc; 2331 opromisc = d->bd_promisc;
2333 bpf_detachd(d); 2332 bpf_detachd(d);
2334 BPFIF_DLIST_ENTRY_INIT(d); 2333 BPFIF_DLIST_ENTRY_INIT(d);
2335 bpf_attachd(d, bp); 2334 bpf_attachd(d, bp);
2336 reset_d(d); 2335 reset_d(d);
2337 if (opromisc) { 2336 if (opromisc) {
2338 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2337 KERNEL_LOCK_UNLESS_NET_MPSAFE();
2339 error = ifpromisc(bp->bif_ifp, 1); 2338 error = ifpromisc(bp->bif_ifp, 1);
2340 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 2339 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2341 if (error) 2340 if (error)
2342 printf("%s: bpf_setdlt: ifpromisc failed (%d)\n", 2341 printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
2343 bp->bif_ifp->if_xname, error); 2342 bp->bif_ifp->if_xname, error);
2344 else 2343 else
2345 d->bd_promisc = 1; 2344 d->bd_promisc = 1;
2346 } 2345 }
2347 return 0; 2346 return 0;
2348} 2347}
2349 2348
2350static int 2349static int
2351sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS) 2350sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS)
2352{ 2351{
2353 int newsize, error; 2352 int newsize, error;
2354 struct sysctlnode node; 2353 struct sysctlnode node;
2355 2354
2356 node = *rnode; 2355 node = *rnode;
2357 node.sysctl_data = &newsize; 2356 node.sysctl_data = &newsize;
2358 newsize = bpf_maxbufsize; 2357 newsize = bpf_maxbufsize;
2359 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2358 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2360 if (error || newp == NULL) 2359 if (error || newp == NULL)
2361 return (error); 2360 return (error);
2362 2361
2363 if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE) 2362 if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE)
2364 return (EINVAL); 2363 return (EINVAL);
2365 2364
2366 bpf_maxbufsize = newsize; 2365 bpf_maxbufsize = newsize;
2367 2366
2368 return (0); 2367 return (0);
2369} 2368}
2370 2369
2371#if defined(MODULAR) || defined(BPFJIT) 2370#if defined(MODULAR) || defined(BPFJIT)
2372static int 2371static int
2373sysctl_net_bpf_jit(SYSCTLFN_ARGS) 2372sysctl_net_bpf_jit(SYSCTLFN_ARGS)
2374{ 2373{
2375 bool newval; 2374 bool newval;
2376 int error; 2375 int error;
2377 struct sysctlnode node; 2376 struct sysctlnode node;
2378 2377
2379 node = *rnode; 2378 node = *rnode;
2380 node.sysctl_data = &newval; 2379 node.sysctl_data = &newval;
2381 newval = bpf_jit; 2380 newval = bpf_jit;
2382 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2381 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2383 if (error != 0 || newp == NULL) 2382 if (error != 0 || newp == NULL)
2384 return error; 2383 return error;
2385 2384
2386 bpf_jit = newval; 2385 bpf_jit = newval;
2387 if (newval && bpfjit_module_ops.bj_generate_code == NULL) { 2386 if (newval && bpfjit_module_ops.bj_generate_code == NULL) {
2388 printf("JIT compilation is postponed " 2387 printf("JIT compilation is postponed "
2389 "until after bpfjit module is loaded\n"); 2388 "until after bpfjit module is loaded\n");
2390 } 2389 }
2391 2390
2392 return 0; 2391 return 0;
2393} 2392}
2394#endif 2393#endif
2395 2394
2396static int 2395static int
2397sysctl_net_bpf_peers(SYSCTLFN_ARGS) 2396sysctl_net_bpf_peers(SYSCTLFN_ARGS)
2398{ 2397{
2399 int error, elem_count; 2398 int error, elem_count;
2400 struct bpf_d *dp; 2399 struct bpf_d *dp;
2401 struct bpf_d_ext dpe; 2400 struct bpf_d_ext dpe;
2402 size_t len, needed, elem_size, out_size; 2401 size_t len, needed, elem_size, out_size;
2403 char *sp; 2402 char *sp;
2404 2403
2405 if (namelen == 1 && name[0] == CTL_QUERY) 2404 if (namelen == 1 && name[0] == CTL_QUERY)
2406 return (sysctl_query(SYSCTLFN_CALL(rnode))); 2405 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2407 2406
2408 if (namelen != 2) 2407 if (namelen != 2)
2409 return (EINVAL); 2408 return (EINVAL);
2410 2409
2411 /* BPF peers is privileged information. */ 2410 /* BPF peers is privileged information. */
2412 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE, 2411 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
2413 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, NULL, NULL, NULL); 2412 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, NULL, NULL, NULL);
2414 if (error) 2413 if (error)
2415 return (EPERM); 2414 return (EPERM);
2416 2415
2417 len = (oldp != NULL) ? *oldlenp : 0; 2416 len = (oldp != NULL) ? *oldlenp : 0;
2418 sp = oldp; 2417 sp = oldp;
2419 elem_size = name[0]; 2418 elem_size = name[0];
2420 elem_count = name[1]; 2419 elem_count = name[1];
2421 out_size = MIN(sizeof(dpe), elem_size); 2420 out_size = MIN(sizeof(dpe), elem_size);
2422 needed = 0; 2421 needed = 0;
2423 2422
2424 if (elem_size < 1 || elem_count < 0) 2423 if (elem_size < 1 || elem_count < 0)
2425 return (EINVAL); 2424 return (EINVAL);
2426 2425
2427 mutex_enter(&bpf_mtx); 2426 mutex_enter(&bpf_mtx);
2428 BPF_DLIST_WRITER_FOREACH(dp) { 2427 BPF_DLIST_WRITER_FOREACH(dp) {
2429 if (len >= elem_size && elem_count > 0) { 2428 if (len >= elem_size && elem_count > 0) {
2430#define BPF_EXT(field) dpe.bde_ ## field = dp->bd_ ## field 2429#define BPF_EXT(field) dpe.bde_ ## field = dp->bd_ ## field
2431 BPF_EXT(bufsize); 2430 BPF_EXT(bufsize);
2432 BPF_EXT(promisc); 2431 BPF_EXT(promisc);
2433 BPF_EXT(state); 2432 BPF_EXT(state);
2434 BPF_EXT(immediate); 2433 BPF_EXT(immediate);
2435 BPF_EXT(hdrcmplt); 2434 BPF_EXT(hdrcmplt);
2436 BPF_EXT(direction); 2435 BPF_EXT(direction);
2437 BPF_EXT(pid); 2436 BPF_EXT(pid);
2438 BPF_EXT(rcount); 2437 BPF_EXT(rcount);
2439 BPF_EXT(dcount); 2438 BPF_EXT(dcount);
2440 BPF_EXT(ccount); 2439 BPF_EXT(ccount);
2441#undef BPF_EXT 2440#undef BPF_EXT
2442 mutex_enter(dp->bd_mtx); 2441 mutex_enter(dp->bd_mtx);
2443 if (dp->bd_bif) 2442 if (dp->bd_bif)
2444 (void)strlcpy(dpe.bde_ifname, 2443 (void)strlcpy(dpe.bde_ifname,
2445 dp->bd_bif->bif_ifp->if_xname, 2444 dp->bd_bif->bif_ifp->if_xname,
2446 IFNAMSIZ - 1); 2445 IFNAMSIZ - 1);
2447 else 2446 else
2448 dpe.bde_ifname[0] = '\0'; 2447 dpe.bde_ifname[0] = '\0';
2449 dpe.bde_locked = dp->bd_locked; 2448 dpe.bde_locked = dp->bd_locked;
2450 mutex_exit(dp->bd_mtx); 2449 mutex_exit(dp->bd_mtx);
2451 2450
2452 error = copyout(&dpe, sp, out_size); 2451 error = copyout(&dpe, sp, out_size);
2453 if (error) 2452 if (error)
2454 break; 2453 break;
2455 sp += elem_size; 2454 sp += elem_size;
2456 len -= elem_size; 2455 len -= elem_size;
2457 } 2456 }
2458 needed += elem_size; 2457 needed += elem_size;
2459 if (elem_count > 0 && elem_count != INT_MAX) 2458 if (elem_count > 0 && elem_count != INT_MAX)
2460 elem_count--; 2459 elem_count--;
2461 } 2460 }
2462 mutex_exit(&bpf_mtx); 2461 mutex_exit(&bpf_mtx);
2463 2462
2464 *oldlenp = needed; 2463 *oldlenp = needed;
2465 2464
2466 return (error); 2465 return (error);
2467} 2466}
2468 2467
2469static void 2468static void
2470bpf_stats(void *p, void *arg, struct cpu_info *ci __unused) 2469bpf_stats(void *p, void *arg, struct cpu_info *ci __unused)
2471{ 2470{
2472 struct bpf_stat *const stats = p; 2471 struct bpf_stat *const stats = p;
2473 struct bpf_stat *sum = arg; 2472 struct bpf_stat *sum = arg;
2474 2473
2475 int s = splnet(); 2474 int s = splnet();
2476 2475
2477 sum->bs_recv += stats->bs_recv; 2476 sum->bs_recv += stats->bs_recv;
2478 sum->bs_drop += stats->bs_drop; 2477 sum->bs_drop += stats->bs_drop;
2479 sum->bs_capt += stats->bs_capt; 2478 sum->bs_capt += stats->bs_capt;
2480 2479
2481 splx(s); 2480 splx(s);
2482} 2481}
2483 2482
2484static int 2483static int
2485bpf_sysctl_gstats_handler(SYSCTLFN_ARGS) 2484bpf_sysctl_gstats_handler(SYSCTLFN_ARGS)
2486{ 2485{
2487 struct sysctlnode node; 2486 struct sysctlnode node;
2488 int error; 2487 int error;
2489 struct bpf_stat sum; 2488 struct bpf_stat sum;
2490 2489
2491 memset(&sum, 0, sizeof(sum)); 2490 memset(&sum, 0, sizeof(sum));
2492 node = *rnode; 2491 node = *rnode;
2493 2492
2494 percpu_foreach_xcall(bpf_gstats_percpu, XC_HIGHPRI_IPL(IPL_SOFTNET), 2493 percpu_foreach_xcall(bpf_gstats_percpu, XC_HIGHPRI_IPL(IPL_SOFTNET),
2495 bpf_stats, &sum); 2494 bpf_stats, &sum);
2496 2495
2497 node.sysctl_data = &sum; 2496 node.sysctl_data = &sum;
2498 node.sysctl_size = sizeof(sum); 2497 node.sysctl_size = sizeof(sum);
2499 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2498 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2500 if (error != 0 || newp == NULL) 2499 if (error != 0 || newp == NULL)
2501 return error; 2500 return error;
2502 2501
2503 return 0; 2502 return 0;
2504} 2503}
2505 2504
2506SYSCTL_SETUP(sysctl_net_bpf_setup, "bpf sysctls") 2505SYSCTL_SETUP(sysctl_net_bpf_setup, "bpf sysctls")
2507{ 2506{
2508 const struct sysctlnode *node; 2507 const struct sysctlnode *node;
2509 2508
2510 node = NULL; 2509 node = NULL;
2511 sysctl_createv(clog, 0, NULL, &node, 2510 sysctl_createv(clog, 0, NULL, &node,
2512 CTLFLAG_PERMANENT, 2511 CTLFLAG_PERMANENT,
2513 CTLTYPE_NODE, "bpf", 2512 CTLTYPE_NODE, "bpf",
2514 SYSCTL_DESCR("BPF options"), 2513 SYSCTL_DESCR("BPF options"),
2515 NULL, 0, NULL, 0, 2514 NULL, 0, NULL, 0,
2516 CTL_NET, CTL_CREATE, CTL_EOL); 2515 CTL_NET, CTL_CREATE, CTL_EOL);
2517 if (node != NULL) { 2516 if (node != NULL) {
2518#if defined(MODULAR) || defined(BPFJIT) 2517#if defined(MODULAR) || defined(BPFJIT)
2519 sysctl_createv(clog, 0, NULL, NULL, 2518 sysctl_createv(clog, 0, NULL, NULL,
2520 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2519 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2521 CTLTYPE_BOOL, "jit", 2520 CTLTYPE_BOOL, "jit",
2522 SYSCTL_DESCR("Toggle Just-In-Time compilation"), 2521 SYSCTL_DESCR("Toggle Just-In-Time compilation"),
2523 sysctl_net_bpf_jit, 0, &bpf_jit, 0, 2522 sysctl_net_bpf_jit, 0, &bpf_jit, 0,
2524 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2523 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2525#endif 2524#endif
2526 sysctl_createv(clog, 0, NULL, NULL, 2525 sysctl_createv(clog, 0, NULL, NULL,
2527 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2526 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2528 CTLTYPE_INT, "maxbufsize", 2527 CTLTYPE_INT, "maxbufsize",
2529 SYSCTL_DESCR("Maximum size for data capture buffer"), 2528 SYSCTL_DESCR("Maximum size for data capture buffer"),
2530 sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0, 2529 sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0,
2531 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2530 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2532 sysctl_createv(clog, 0, NULL, NULL, 2531 sysctl_createv(clog, 0, NULL, NULL,
2533 CTLFLAG_PERMANENT, 2532 CTLFLAG_PERMANENT,
2534 CTLTYPE_STRUCT, "stats", 2533 CTLTYPE_STRUCT, "stats",
2535 SYSCTL_DESCR("BPF stats"), 2534 SYSCTL_DESCR("BPF stats"),
2536 bpf_sysctl_gstats_handler, 0, NULL, 0, 2535 bpf_sysctl_gstats_handler, 0, NULL, 0,
2537 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2536 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2538 sysctl_createv(clog, 0, NULL, NULL, 2537 sysctl_createv(clog, 0, NULL, NULL,
2539 CTLFLAG_PERMANENT, 2538 CTLFLAG_PERMANENT,
2540 CTLTYPE_STRUCT, "peers", 2539 CTLTYPE_STRUCT, "peers",
2541 SYSCTL_DESCR("BPF peers"), 2540 SYSCTL_DESCR("BPF peers"),
2542 sysctl_net_bpf_peers, 0, NULL, 0, 2541 sysctl_net_bpf_peers, 0, NULL, 0,
2543 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL); 2542 CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2544 } 2543 }
2545 2544
2546} 2545}
2547 2546
2548static int 2547static int
2549_bpf_register_track_event(struct bpf_if **driverp, 2548_bpf_register_track_event(struct bpf_if **driverp,
2550 void (*_fun)(struct bpf_if *, struct ifnet *, int, int)) 2549 void (*_fun)(struct bpf_if *, struct ifnet *, int, int))
2551{ 2550{
2552 struct bpf_if *bp; 2551 struct bpf_if *bp;
2553 struct bpf_event_tracker *t; 2552 struct bpf_event_tracker *t;
2554 int ret = ENOENT; 2553 int ret = ENOENT;
2555 2554
2556 t = kmem_zalloc(sizeof(*t), KM_SLEEP); 2555 t = kmem_zalloc(sizeof(*t), KM_SLEEP);
2557 if (!t) 2556 if (!t)
2558 return ENOMEM; 2557 return ENOMEM;
2559 t->bet_notify = _fun; 2558 t->bet_notify = _fun;
2560 2559
2561 mutex_enter(&bpf_mtx); 2560 mutex_enter(&bpf_mtx);
2562 BPF_IFLIST_WRITER_FOREACH(bp) { 2561 BPF_IFLIST_WRITER_FOREACH(bp) {
2563 if (bp->bif_driverp != driverp) 2562 if (bp->bif_driverp != driverp)
2564 continue; 2563 continue;
2565 SLIST_INSERT_HEAD(&bp->bif_trackers, t, bet_entries); 2564 SLIST_INSERT_HEAD(&bp->bif_trackers, t, bet_entries);
2566 ret = 0; 2565 ret = 0;
2567 break; 2566 break;
2568 } 2567 }
2569 mutex_exit(&bpf_mtx); 2568 mutex_exit(&bpf_mtx);
2570 2569
2571 return ret; 2570 return ret;
2572} 2571}
2573 2572
2574static int 2573static int
2575_bpf_deregister_track_event(struct bpf_if **driverp, 2574_bpf_deregister_track_event(struct bpf_if **driverp,
2576 void (*_fun)(struct bpf_if *, struct ifnet *, int, int)) 2575 void (*_fun)(struct bpf_if *, struct ifnet *, int, int))
2577{ 2576{
2578 struct bpf_if *bp; 2577 struct bpf_if *bp;
2579 struct bpf_event_tracker *t = NULL; 2578 struct bpf_event_tracker *t = NULL;
2580 int ret = ENOENT; 2579 int ret = ENOENT;
2581 2580
2582 mutex_enter(&bpf_mtx); 2581 mutex_enter(&bpf_mtx);
2583 BPF_IFLIST_WRITER_FOREACH(bp) { 2582 BPF_IFLIST_WRITER_FOREACH(bp) {
2584 if (bp->bif_driverp != driverp) 2583 if (bp->bif_driverp != driverp)
2585 continue; 2584 continue;
2586 SLIST_FOREACH(t, &bp->bif_trackers, bet_entries) { 2585 SLIST_FOREACH(t, &bp->bif_trackers, bet_entries) {
2587 if (t->bet_notify == _fun) { 2586 if (t->bet_notify == _fun) {
2588 ret = 0; 2587 ret = 0;
2589 break; 2588 break;
2590 } 2589 }
2591 } 2590 }
2592 if (ret == 0) 2591 if (ret == 0)
2593 break; 2592 break;
2594 } 2593 }
2595 if (ret == 0 && t && t->bet_notify == _fun) { 2594 if (ret == 0 && t && t->bet_notify == _fun) {
2596 SLIST_REMOVE(&bp->bif_trackers, t, bpf_event_tracker, 2595 SLIST_REMOVE(&bp->bif_trackers, t, bpf_event_tracker,
2597 bet_entries); 2596 bet_entries);
2598 } 2597 }
2599 mutex_exit(&bpf_mtx); 2598 mutex_exit(&bpf_mtx);
2600 if (ret == 0) 2599 if (ret == 0)
2601 kmem_free(t, sizeof(*t)); 2600 kmem_free(t, sizeof(*t));
2602 return ret; 2601 return ret;
2603} 2602}
2604 2603
2605struct bpf_ops bpf_ops_kernel = { 2604struct bpf_ops bpf_ops_kernel = {
2606 .bpf_attach = _bpfattach, 2605 .bpf_attach = _bpfattach,
2607 .bpf_detach = _bpfdetach, 2606 .bpf_detach = _bpfdetach,
2608 .bpf_change_type = _bpf_change_type, 2607 .bpf_change_type = _bpf_change_type,
2609 .bpf_register_track_event = _bpf_register_track_event, 2608 .bpf_register_track_event = _bpf_register_track_event,
2610 .bpf_deregister_track_event = _bpf_deregister_track_event, 2609 .bpf_deregister_track_event = _bpf_deregister_track_event,
2611 2610
2612 .bpf_mtap = _bpf_mtap, 2611 .bpf_mtap = _bpf_mtap,
2613 .bpf_mtap2 = _bpf_mtap2, 2612 .bpf_mtap2 = _bpf_mtap2,
2614 .bpf_mtap_af = _bpf_mtap_af, 2613 .bpf_mtap_af = _bpf_mtap_af,
2615 .bpf_mtap_sl_in = _bpf_mtap_sl_in, 2614 .bpf_mtap_sl_in = _bpf_mtap_sl_in,
2616 .bpf_mtap_sl_out = _bpf_mtap_sl_out, 2615 .bpf_mtap_sl_out = _bpf_mtap_sl_out,
2617 2616
2618 .bpf_mtap_softint = _bpf_mtap_softint, 2617 .bpf_mtap_softint = _bpf_mtap_softint,
2619 .bpf_mtap_softint_init = _bpf_mtap_softint_init, 2618 .bpf_mtap_softint_init = _bpf_mtap_softint_init,
2620}; 2619};
2621 2620
2622MODULE(MODULE_CLASS_DRIVER, bpf, "bpf_filter"); 2621MODULE(MODULE_CLASS_DRIVER, bpf, "bpf_filter");
2623 2622
2624static int 2623static int
2625bpf_modcmd(modcmd_t cmd, void *arg) 2624bpf_modcmd(modcmd_t cmd, void *arg)
2626{ 2625{
2627#ifdef _MODULE 2626#ifdef _MODULE
2628 devmajor_t bmajor, cmajor; 2627 devmajor_t bmajor, cmajor;
2629#endif 2628#endif
2630 int error = 0; 2629 int error = 0;
2631 2630
2632 switch (cmd) { 2631 switch (cmd) {
2633 case MODULE_CMD_INIT: 2632 case MODULE_CMD_INIT:
2634 bpf_init(); 2633 bpf_init();
2635#ifdef _MODULE 2634#ifdef _MODULE
2636 bmajor = cmajor = NODEVMAJOR; 2635 bmajor = cmajor = NODEVMAJOR;
2637 error = devsw_attach("bpf", NULL, &bmajor, 2636 error = devsw_attach("bpf", NULL, &bmajor,
2638 &bpf_cdevsw, &cmajor); 2637 &bpf_cdevsw, &cmajor);
2639 if (error) 2638 if (error)
2640 break; 2639 break;
2641#endif 2640#endif
2642 2641
2643 bpf_ops_handover_enter(&bpf_ops_kernel); 2642 bpf_ops_handover_enter(&bpf_ops_kernel);
2644 atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel); 2643 atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel);
2645 bpf_ops_handover_exit(); 2644 bpf_ops_handover_exit();
2646 break; 2645 break;
2647 2646
2648 case MODULE_CMD_FINI: 2647 case MODULE_CMD_FINI:
2649 /* 2648 /*
2650 * While there is no reference counting for bpf callers, 2649 * While there is no reference counting for bpf callers,
2651 * unload could at least in theory be done similarly to  2650 * unload could at least in theory be done similarly to
2652 * system call disestablishment. This should even be 2651 * system call disestablishment. This should even be
2653 * a little simpler: 2652 * a little simpler:
2654 *  2653 *
2655 * 1) replace op vector with stubs 2654 * 1) replace op vector with stubs
2656 * 2) post update to all cpus with xc 2655 * 2) post update to all cpus with xc
2657 * 3) check that nobody is in bpf anymore 2656 * 3) check that nobody is in bpf anymore
2658 * (it's doubtful we'd want something like l_sysent, 2657 * (it's doubtful we'd want something like l_sysent,
2659 * but we could do something like *signed* percpu 2658 * but we could do something like *signed* percpu
2660 * counters. if the sum is 0, we're good). 2659 * counters. if the sum is 0, we're good).
2661 * 4) if fail, unroll changes 2660 * 4) if fail, unroll changes
2662 * 2661 *
2663 * NOTE: change won't be atomic to the outside. some 2662 * NOTE: change won't be atomic to the outside. some
2664 * packets may be not captured even if unload is 2663 * packets may be not captured even if unload is
2665 * not succesful. I think packet capture not working 2664 * not succesful. I think packet capture not working
2666 * is a perfectly logical consequence of trying to 2665 * is a perfectly logical consequence of trying to
2667 * disable packet capture. 2666 * disable packet capture.
2668 */ 2667 */
2669 error = EOPNOTSUPP; 2668 error = EOPNOTSUPP;
2670 break; 2669 break;
2671 2670
2672 default: 2671 default:
2673 error = ENOTTY; 2672 error = ENOTTY;
2674 break; 2673 break;
2675 } 2674 }
2676 2675
2677 return error; 2676 return error;
2678} 2677}

cvs diff -r1.158 -r1.159 src/sys/net/if_vlan.c (switch to unified diff)

--- src/sys/net/if_vlan.c 2021/07/14 06:23:06 1.158
+++ src/sys/net/if_vlan.c 2021/07/14 06:50:22 1.159
@@ -1,1704 +1,1702 @@ @@ -1,1704 +1,1702 @@
1/* $NetBSD: if_vlan.c,v 1.158 2021/07/14 06:23:06 yamaguchi Exp $ */ 1/* $NetBSD: if_vlan.c,v 1.159 2021/07/14 06:50:22 yamaguchi Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc. 4 * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc. 8 * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32/* 32/*
33 * Copyright 1998 Massachusetts Institute of Technology 33 * Copyright 1998 Massachusetts Institute of Technology
34 * 34 *
35 * Permission to use, copy, modify, and distribute this software and 35 * Permission to use, copy, modify, and distribute this software and
36 * its documentation for any purpose and without fee is hereby 36 * its documentation for any purpose and without fee is hereby
37 * granted, provided that both the above copyright notice and this 37 * granted, provided that both the above copyright notice and this
38 * permission notice appear in all copies, that both the above 38 * permission notice appear in all copies, that both the above
39 * copyright notice and this permission notice appear in all 39 * copyright notice and this permission notice appear in all
40 * supporting documentation, and that the name of M.I.T. not be used 40 * supporting documentation, and that the name of M.I.T. not be used
41 * in advertising or publicity pertaining to distribution of the 41 * in advertising or publicity pertaining to distribution of the
42 * software without specific, written prior permission. M.I.T. makes 42 * software without specific, written prior permission. M.I.T. makes
43 * no representations about the suitability of this software for any 43 * no representations about the suitability of this software for any
44 * purpose. It is provided "as is" without express or implied 44 * purpose. It is provided "as is" without express or implied
45 * warranty. 45 * warranty.
46 * 46 *
47 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 47 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
48 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 48 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
49 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 49 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 50 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
51 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 51 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 54 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE. 58 * SUCH DAMAGE.
59 * 59 *
60 * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp 60 * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
61 * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp 61 * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
62 */ 62 */
63 63
64/* 64/*
65 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be 65 * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs. Might be
66 * extended some day to also handle IEEE 802.1P priority tagging. This is 66 * extended some day to also handle IEEE 802.1P priority tagging. This is
67 * sort of sneaky in the implementation, since we need to pretend to be 67 * sort of sneaky in the implementation, since we need to pretend to be
68 * enough of an Ethernet implementation to make ARP work. The way we do 68 * enough of an Ethernet implementation to make ARP work. The way we do
69 * this is by telling everyone that we are an Ethernet interface, and then 69 * this is by telling everyone that we are an Ethernet interface, and then
70 * catch the packets that ether_output() left on our output queue when it 70 * catch the packets that ether_output() left on our output queue when it
71 * calls if_start(), rewrite them for use by the real outgoing interface, 71 * calls if_start(), rewrite them for use by the real outgoing interface,
72 * and ask it to send them. 72 * and ask it to send them.
73 * 73 *
74 * TODO: 74 * TODO:
75 * 75 *
76 * - Need some way to notify vlan interfaces when the parent 76 * - Need some way to notify vlan interfaces when the parent
77 * interface changes MTU. 77 * interface changes MTU.
78 */ 78 */
79 79
80#include <sys/cdefs.h> 80#include <sys/cdefs.h>
81__KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.158 2021/07/14 06:23:06 yamaguchi Exp $"); 81__KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.159 2021/07/14 06:50:22 yamaguchi Exp $");
82 82
83#ifdef _KERNEL_OPT 83#ifdef _KERNEL_OPT
84#include "opt_inet.h" 84#include "opt_inet.h"
85#include "opt_net_mpsafe.h" 85#include "opt_net_mpsafe.h"
86#endif 86#endif
87 87
88#include <sys/param.h> 88#include <sys/param.h>
89#include <sys/systm.h> 89#include <sys/systm.h>
90#include <sys/kernel.h> 90#include <sys/kernel.h>
91#include <sys/mbuf.h> 91#include <sys/mbuf.h>
92#include <sys/queue.h> 92#include <sys/queue.h>
93#include <sys/socket.h> 93#include <sys/socket.h>
94#include <sys/sockio.h> 94#include <sys/sockio.h>
95#include <sys/systm.h> 95#include <sys/systm.h>
96#include <sys/proc.h> 96#include <sys/proc.h>
97#include <sys/kauth.h> 97#include <sys/kauth.h>
98#include <sys/mutex.h> 98#include <sys/mutex.h>
99#include <sys/kmem.h> 99#include <sys/kmem.h>
100#include <sys/cpu.h> 100#include <sys/cpu.h>
101#include <sys/pserialize.h> 101#include <sys/pserialize.h>
102#include <sys/psref.h> 102#include <sys/psref.h>
103#include <sys/pslist.h> 103#include <sys/pslist.h>
104#include <sys/atomic.h> 104#include <sys/atomic.h>
105#include <sys/device.h> 105#include <sys/device.h>
106#include <sys/module.h> 106#include <sys/module.h>
107 107
108#include <net/bpf.h> 108#include <net/bpf.h>
109#include <net/if.h> 109#include <net/if.h>
110#include <net/if_dl.h> 110#include <net/if_dl.h>
111#include <net/if_types.h> 111#include <net/if_types.h>
112#include <net/if_ether.h> 112#include <net/if_ether.h>
113#include <net/if_vlanvar.h> 113#include <net/if_vlanvar.h>
114 114
115#ifdef INET 115#ifdef INET
116#include <netinet/in.h> 116#include <netinet/in.h>
117#include <netinet/if_inarp.h> 117#include <netinet/if_inarp.h>
118#endif 118#endif
119#ifdef INET6 119#ifdef INET6
120#include <netinet6/in6_ifattach.h> 120#include <netinet6/in6_ifattach.h>
121#include <netinet6/in6_var.h> 121#include <netinet6/in6_var.h>
122#include <netinet6/nd6.h> 122#include <netinet6/nd6.h>
123#endif 123#endif
124 124
125#include "ioconf.h" 125#include "ioconf.h"
126 126
127struct vlan_mc_entry { 127struct vlan_mc_entry {
128 LIST_ENTRY(vlan_mc_entry) mc_entries; 128 LIST_ENTRY(vlan_mc_entry) mc_entries;
129 /* 129 /*
130 * A key to identify this entry. The mc_addr below can't be 130 * A key to identify this entry. The mc_addr below can't be
131 * used since multiple sockaddr may mapped into the same 131 * used since multiple sockaddr may mapped into the same
132 * ether_multi (e.g., AF_UNSPEC). 132 * ether_multi (e.g., AF_UNSPEC).
133 */ 133 */
134 struct ether_multi *mc_enm; 134 struct ether_multi *mc_enm;
135 struct sockaddr_storage mc_addr; 135 struct sockaddr_storage mc_addr;
136}; 136};
137 137
138struct ifvlan_linkmib { 138struct ifvlan_linkmib {
139 struct ifvlan *ifvm_ifvlan; 139 struct ifvlan *ifvm_ifvlan;
140 const struct vlan_multisw *ifvm_msw; 140 const struct vlan_multisw *ifvm_msw;
141 int ifvm_encaplen; /* encapsulation length */ 141 int ifvm_encaplen; /* encapsulation length */
142 int ifvm_mtufudge; /* MTU fudged by this much */ 142 int ifvm_mtufudge; /* MTU fudged by this much */
143 int ifvm_mintu; /* min transmission unit */ 143 int ifvm_mintu; /* min transmission unit */
144 uint16_t ifvm_proto; /* encapsulation ethertype */ 144 uint16_t ifvm_proto; /* encapsulation ethertype */
145 uint16_t ifvm_tag; /* tag to apply on packets */ 145 uint16_t ifvm_tag; /* tag to apply on packets */
146 struct ifnet *ifvm_p; /* parent interface of this vlan */ 146 struct ifnet *ifvm_p; /* parent interface of this vlan */
147 147
148 struct psref_target ifvm_psref; 148 struct psref_target ifvm_psref;
149}; 149};
150 150
151struct ifvlan { 151struct ifvlan {
152 struct ethercom ifv_ec; 152 struct ethercom ifv_ec;
153 struct ifvlan_linkmib *ifv_mib; /* 153 struct ifvlan_linkmib *ifv_mib; /*
154 * reader must use vlan_getref_linkmib() 154 * reader must use vlan_getref_linkmib()
155 * instead of direct dereference 155 * instead of direct dereference
156 */ 156 */
157 kmutex_t ifv_lock; /* writer lock for ifv_mib */ 157 kmutex_t ifv_lock; /* writer lock for ifv_mib */
158 pserialize_t ifv_psz; 158 pserialize_t ifv_psz;
159 159
160 LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead; 160 LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
161 LIST_ENTRY(ifvlan) ifv_list; 161 LIST_ENTRY(ifvlan) ifv_list;
162 struct pslist_entry ifv_hash; 162 struct pslist_entry ifv_hash;
163 int ifv_flags; 163 int ifv_flags;
164}; 164};
165 165
166#define IFVF_PROMISC 0x01 /* promiscuous mode enabled */ 166#define IFVF_PROMISC 0x01 /* promiscuous mode enabled */
167 167
168#define ifv_if ifv_ec.ec_if 168#define ifv_if ifv_ec.ec_if
169 169
170#define ifv_msw ifv_mib.ifvm_msw 170#define ifv_msw ifv_mib.ifvm_msw
171#define ifv_encaplen ifv_mib.ifvm_encaplen 171#define ifv_encaplen ifv_mib.ifvm_encaplen
172#define ifv_mtufudge ifv_mib.ifvm_mtufudge 172#define ifv_mtufudge ifv_mib.ifvm_mtufudge
173#define ifv_mintu ifv_mib.ifvm_mintu 173#define ifv_mintu ifv_mib.ifvm_mintu
174#define ifv_tag ifv_mib.ifvm_tag 174#define ifv_tag ifv_mib.ifvm_tag
175 175
176struct vlan_multisw { 176struct vlan_multisw {
177 int (*vmsw_addmulti)(struct ifvlan *, struct ifreq *); 177 int (*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
178 int (*vmsw_delmulti)(struct ifvlan *, struct ifreq *); 178 int (*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
179 void (*vmsw_purgemulti)(struct ifvlan *); 179 void (*vmsw_purgemulti)(struct ifvlan *);
180}; 180};
181 181
182static int vlan_ether_addmulti(struct ifvlan *, struct ifreq *); 182static int vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
183static int vlan_ether_delmulti(struct ifvlan *, struct ifreq *); 183static int vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
184static void vlan_ether_purgemulti(struct ifvlan *); 184static void vlan_ether_purgemulti(struct ifvlan *);
185 185
186const struct vlan_multisw vlan_ether_multisw = { 186const struct vlan_multisw vlan_ether_multisw = {
187 .vmsw_addmulti = vlan_ether_addmulti, 187 .vmsw_addmulti = vlan_ether_addmulti,
188 .vmsw_delmulti = vlan_ether_delmulti, 188 .vmsw_delmulti = vlan_ether_delmulti,
189 .vmsw_purgemulti = vlan_ether_purgemulti, 189 .vmsw_purgemulti = vlan_ether_purgemulti,
190}; 190};
191 191
192static int vlan_clone_create(struct if_clone *, int); 192static int vlan_clone_create(struct if_clone *, int);
193static int vlan_clone_destroy(struct ifnet *); 193static int vlan_clone_destroy(struct ifnet *);
194static int vlan_config(struct ifvlan *, struct ifnet *, uint16_t); 194static int vlan_config(struct ifvlan *, struct ifnet *, uint16_t);
195static int vlan_ioctl(struct ifnet *, u_long, void *); 195static int vlan_ioctl(struct ifnet *, u_long, void *);
196static void vlan_start(struct ifnet *); 196static void vlan_start(struct ifnet *);
197static int vlan_transmit(struct ifnet *, struct mbuf *); 197static int vlan_transmit(struct ifnet *, struct mbuf *);
198static void vlan_unconfig(struct ifnet *); 198static void vlan_unconfig(struct ifnet *);
199static int vlan_unconfig_locked(struct ifvlan *, struct ifvlan_linkmib *); 199static int vlan_unconfig_locked(struct ifvlan *, struct ifvlan_linkmib *);
200static void vlan_hash_init(void); 200static void vlan_hash_init(void);
201static int vlan_hash_fini(void); 201static int vlan_hash_fini(void);
202static int vlan_tag_hash(uint16_t, u_long); 202static int vlan_tag_hash(uint16_t, u_long);
203static struct ifvlan_linkmib* vlan_getref_linkmib(struct ifvlan *, 203static struct ifvlan_linkmib* vlan_getref_linkmib(struct ifvlan *,
204 struct psref *); 204 struct psref *);
205static void vlan_putref_linkmib(struct ifvlan_linkmib *, struct psref *); 205static void vlan_putref_linkmib(struct ifvlan_linkmib *, struct psref *);
206static void vlan_linkmib_update(struct ifvlan *, struct ifvlan_linkmib *); 206static void vlan_linkmib_update(struct ifvlan *, struct ifvlan_linkmib *);
207static struct ifvlan_linkmib* vlan_lookup_tag_psref(struct ifnet *, 207static struct ifvlan_linkmib* vlan_lookup_tag_psref(struct ifnet *,
208 uint16_t, struct psref *); 208 uint16_t, struct psref *);
209 209
210static struct { 210static struct {
211 kmutex_t lock; 211 kmutex_t lock;
212 LIST_HEAD(vlan_ifvlist, ifvlan) list; 212 LIST_HEAD(vlan_ifvlist, ifvlan) list;
213} ifv_list __cacheline_aligned; 213} ifv_list __cacheline_aligned;
214 214
215 215
216#if !defined(VLAN_TAG_HASH_SIZE) 216#if !defined(VLAN_TAG_HASH_SIZE)
217#define VLAN_TAG_HASH_SIZE 32 217#define VLAN_TAG_HASH_SIZE 32
218#endif 218#endif
219static struct { 219static struct {
220 kmutex_t lock; 220 kmutex_t lock;
221 struct pslist_head *lists; 221 struct pslist_head *lists;
222 u_long mask; 222 u_long mask;
223} ifv_hash __cacheline_aligned = { 223} ifv_hash __cacheline_aligned = {
224 .lists = NULL, 224 .lists = NULL,
225 .mask = 0, 225 .mask = 0,
226}; 226};
227 227
228pserialize_t vlan_psz __read_mostly; 228pserialize_t vlan_psz __read_mostly;
229static struct psref_class *ifvm_psref_class __read_mostly; 229static struct psref_class *ifvm_psref_class __read_mostly;
230 230
231struct if_clone vlan_cloner = 231struct if_clone vlan_cloner =
232 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy); 232 IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
233 233
234/* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */ 234/* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
235static char vlan_zero_pad_buff[ETHER_MIN_LEN]; 235static char vlan_zero_pad_buff[ETHER_MIN_LEN];
236 236
237static inline int 237static inline int
238vlan_safe_ifpromisc(struct ifnet *ifp, int pswitch) 238vlan_safe_ifpromisc(struct ifnet *ifp, int pswitch)
239{ 239{
240 int e; 240 int e;
241 241
242 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 242 KERNEL_LOCK_UNLESS_NET_MPSAFE();
243 e = ifpromisc(ifp, pswitch); 243 e = ifpromisc(ifp, pswitch);
244 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 244 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
245 245
246 return e; 246 return e;
247} 247}
248 248
249static inline int 249static inline int
250vlan_safe_ifpromisc_locked(struct ifnet *ifp, int pswitch) 250vlan_safe_ifpromisc_locked(struct ifnet *ifp, int pswitch)
251{ 251{
252 int e; 252 int e;
253 253
254 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 254 KERNEL_LOCK_UNLESS_NET_MPSAFE();
255 e = ifpromisc_locked(ifp, pswitch); 255 e = ifpromisc_locked(ifp, pswitch);
256 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 256 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
257 257
258 return e; 258 return e;
259} 259}
260 260
261void 261void
262vlanattach(int n) 262vlanattach(int n)
263{ 263{
264 264
265 /* 265 /*
266 * Nothing to do here, initialization is handled by the 266 * Nothing to do here, initialization is handled by the
267 * module initialization code in vlaninit() below. 267 * module initialization code in vlaninit() below.
268 */ 268 */
269} 269}
270 270
271static void 271static void
272vlaninit(void) 272vlaninit(void)
273{ 273{
274 mutex_init(&ifv_list.lock, MUTEX_DEFAULT, IPL_NONE); 274 mutex_init(&ifv_list.lock, MUTEX_DEFAULT, IPL_NONE);
275 LIST_INIT(&ifv_list.list); 275 LIST_INIT(&ifv_list.list);
276 276
277 mutex_init(&ifv_hash.lock, MUTEX_DEFAULT, IPL_NONE); 277 mutex_init(&ifv_hash.lock, MUTEX_DEFAULT, IPL_NONE);
278 vlan_psz = pserialize_create(); 278 vlan_psz = pserialize_create();
279 ifvm_psref_class = psref_class_create("vlanlinkmib", IPL_SOFTNET); 279 ifvm_psref_class = psref_class_create("vlanlinkmib", IPL_SOFTNET);
280 if_clone_attach(&vlan_cloner); 280 if_clone_attach(&vlan_cloner);
281 281
282 vlan_hash_init(); 282 vlan_hash_init();
283 MODULE_HOOK_SET(if_vlan_vlan_input_hook, vlan_input); 283 MODULE_HOOK_SET(if_vlan_vlan_input_hook, vlan_input);
284} 284}
285 285
286static int 286static int
287vlandetach(void) 287vlandetach(void)
288{ 288{
289 bool is_empty; 289 bool is_empty;
290 int error; 290 int error;
291 291
292 mutex_enter(&ifv_list.lock); 292 mutex_enter(&ifv_list.lock);
293 is_empty = LIST_EMPTY(&ifv_list.list); 293 is_empty = LIST_EMPTY(&ifv_list.list);
294 mutex_exit(&ifv_list.lock); 294 mutex_exit(&ifv_list.lock);
295 295
296 if (!is_empty) 296 if (!is_empty)
297 return EBUSY; 297 return EBUSY;
298 298
299 error = vlan_hash_fini(); 299 error = vlan_hash_fini();
300 if (error != 0) 300 if (error != 0)
301 return error; 301 return error;
302 302
303 if_clone_detach(&vlan_cloner); 303 if_clone_detach(&vlan_cloner);
304 psref_class_destroy(ifvm_psref_class); 304 psref_class_destroy(ifvm_psref_class);
305 pserialize_destroy(vlan_psz); 305 pserialize_destroy(vlan_psz);
306 mutex_destroy(&ifv_hash.lock); 306 mutex_destroy(&ifv_hash.lock);
307 mutex_destroy(&ifv_list.lock); 307 mutex_destroy(&ifv_list.lock);
308 308
309 MODULE_HOOK_UNSET(if_vlan_vlan_input_hook); 309 MODULE_HOOK_UNSET(if_vlan_vlan_input_hook);
310 return 0; 310 return 0;
311} 311}
312 312
313static void 313static void
314vlan_reset_linkname(struct ifnet *ifp) 314vlan_reset_linkname(struct ifnet *ifp)
315{ 315{
316 316
317 /* 317 /*
318 * We start out with a "802.1Q VLAN" type and zero-length 318 * We start out with a "802.1Q VLAN" type and zero-length
319 * addresses. When we attach to a parent interface, we 319 * addresses. When we attach to a parent interface, we
320 * inherit its type, address length, address, and data link 320 * inherit its type, address length, address, and data link
321 * type. 321 * type.
322 */ 322 */
323 323
324 ifp->if_type = IFT_L2VLAN; 324 ifp->if_type = IFT_L2VLAN;
325 ifp->if_addrlen = 0; 325 ifp->if_addrlen = 0;
326 ifp->if_dlt = DLT_NULL; 326 ifp->if_dlt = DLT_NULL;
327 if_alloc_sadl(ifp); 327 if_alloc_sadl(ifp);
328} 328}
329 329
330static int 330static int
331vlan_clone_create(struct if_clone *ifc, int unit) 331vlan_clone_create(struct if_clone *ifc, int unit)
332{ 332{
333 struct ifvlan *ifv; 333 struct ifvlan *ifv;
334 struct ifnet *ifp; 334 struct ifnet *ifp;
335 struct ifvlan_linkmib *mib; 335 struct ifvlan_linkmib *mib;
336 336
337 ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK | M_ZERO); 337 ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK | M_ZERO);
338 mib = kmem_zalloc(sizeof(struct ifvlan_linkmib), KM_SLEEP); 338 mib = kmem_zalloc(sizeof(struct ifvlan_linkmib), KM_SLEEP);
339 ifp = &ifv->ifv_if; 339 ifp = &ifv->ifv_if;
340 LIST_INIT(&ifv->ifv_mc_listhead); 340 LIST_INIT(&ifv->ifv_mc_listhead);
341 341
342 mib->ifvm_ifvlan = ifv; 342 mib->ifvm_ifvlan = ifv;
343 mib->ifvm_p = NULL; 343 mib->ifvm_p = NULL;
344 psref_target_init(&mib->ifvm_psref, ifvm_psref_class); 344 psref_target_init(&mib->ifvm_psref, ifvm_psref_class);
345 345
346 mutex_init(&ifv->ifv_lock, MUTEX_DEFAULT, IPL_NONE); 346 mutex_init(&ifv->ifv_lock, MUTEX_DEFAULT, IPL_NONE);
347 ifv->ifv_psz = pserialize_create(); 347 ifv->ifv_psz = pserialize_create();
348 ifv->ifv_mib = mib; 348 ifv->ifv_mib = mib;
349 349
350 mutex_enter(&ifv_list.lock); 350 mutex_enter(&ifv_list.lock);
351 LIST_INSERT_HEAD(&ifv_list.list, ifv, ifv_list); 351 LIST_INSERT_HEAD(&ifv_list.list, ifv, ifv_list);
352 mutex_exit(&ifv_list.lock); 352 mutex_exit(&ifv_list.lock);
353 353
354 if_initname(ifp, ifc->ifc_name, unit); 354 if_initname(ifp, ifc->ifc_name, unit);
355 ifp->if_softc = ifv; 355 ifp->if_softc = ifv;
356 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 356 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
357#ifdef NET_MPSAFE 357#ifdef NET_MPSAFE
358 ifp->if_extflags = IFEF_MPSAFE; 358 ifp->if_extflags = IFEF_MPSAFE;
359#endif 359#endif
360 ifp->if_start = vlan_start; 360 ifp->if_start = vlan_start;
361 ifp->if_transmit = vlan_transmit; 361 ifp->if_transmit = vlan_transmit;
362 ifp->if_ioctl = vlan_ioctl; 362 ifp->if_ioctl = vlan_ioctl;
363 IFQ_SET_READY(&ifp->if_snd); 363 IFQ_SET_READY(&ifp->if_snd);
364 if_initialize(ifp); 364 if_initialize(ifp);
365 /* 365 /*
366 * Set the link state to down. 366 * Set the link state to down.
367 * When the parent interface attaches we will use that link state. 367 * When the parent interface attaches we will use that link state.
368 * When the parent interface link state changes, so will ours. 368 * When the parent interface link state changes, so will ours.
369 * When the parent interface detaches, set the link state to down. 369 * When the parent interface detaches, set the link state to down.
370 */ 370 */
371 ifp->if_link_state = LINK_STATE_DOWN; 371 ifp->if_link_state = LINK_STATE_DOWN;
372 372
373 vlan_reset_linkname(ifp); 373 vlan_reset_linkname(ifp);
374 if_register(ifp); 374 if_register(ifp);
375 return 0; 375 return 0;
376} 376}
377 377
378static int 378static int
379vlan_clone_destroy(struct ifnet *ifp) 379vlan_clone_destroy(struct ifnet *ifp)
380{ 380{
381 struct ifvlan *ifv = ifp->if_softc; 381 struct ifvlan *ifv = ifp->if_softc;
382 382
383 mutex_enter(&ifv_list.lock); 383 mutex_enter(&ifv_list.lock);
384 LIST_REMOVE(ifv, ifv_list); 384 LIST_REMOVE(ifv, ifv_list);
385 mutex_exit(&ifv_list.lock); 385 mutex_exit(&ifv_list.lock);
386 386
387 IFNET_LOCK(ifp); 387 IFNET_LOCK(ifp);
388 vlan_unconfig(ifp); 388 vlan_unconfig(ifp);
389 IFNET_UNLOCK(ifp); 389 IFNET_UNLOCK(ifp);
390 if_detach(ifp); 390 if_detach(ifp);
391 391
392 psref_target_destroy(&ifv->ifv_mib->ifvm_psref, ifvm_psref_class); 392 psref_target_destroy(&ifv->ifv_mib->ifvm_psref, ifvm_psref_class);
393 kmem_free(ifv->ifv_mib, sizeof(struct ifvlan_linkmib)); 393 kmem_free(ifv->ifv_mib, sizeof(struct ifvlan_linkmib));
394 pserialize_destroy(ifv->ifv_psz); 394 pserialize_destroy(ifv->ifv_psz);
395 mutex_destroy(&ifv->ifv_lock); 395 mutex_destroy(&ifv->ifv_lock);
396 free(ifv, M_DEVBUF); 396 free(ifv, M_DEVBUF);
397 397
398 return 0; 398 return 0;
399} 399}
400 400
401/* 401/*
402 * Configure a VLAN interface. 402 * Configure a VLAN interface.
403 */ 403 */
404static int 404static int
405vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag) 405vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
406{ 406{
407 struct ifnet *ifp = &ifv->ifv_if; 407 struct ifnet *ifp = &ifv->ifv_if;
408 struct ifvlan_linkmib *nmib = NULL; 408 struct ifvlan_linkmib *nmib = NULL;
409 struct ifvlan_linkmib *omib = NULL; 409 struct ifvlan_linkmib *omib = NULL;
410 struct ifvlan_linkmib *checkmib; 410 struct ifvlan_linkmib *checkmib;
411 struct psref_target *nmib_psref = NULL; 411 struct psref_target *nmib_psref = NULL;
412 const uint16_t vid = EVL_VLANOFTAG(tag); 412 const uint16_t vid = EVL_VLANOFTAG(tag);
413 int error = 0; 413 int error = 0;
414 int idx; 414 int idx;
415 bool omib_cleanup = false; 415 bool omib_cleanup = false;
416 struct psref psref; 416 struct psref psref;
417 417
418 /* VLAN ID 0 and 4095 are reserved in the spec */ 418 /* VLAN ID 0 and 4095 are reserved in the spec */
419 if ((vid == 0) || (vid == 0xfff)) 419 if ((vid == 0) || (vid == 0xfff))
420 return EINVAL; 420 return EINVAL;
421 421
422 nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP); 422 nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
423 mutex_enter(&ifv->ifv_lock); 423 mutex_enter(&ifv->ifv_lock);
424 omib = ifv->ifv_mib; 424 omib = ifv->ifv_mib;
425 425
426 if (omib->ifvm_p != NULL) { 426 if (omib->ifvm_p != NULL) {
427 error = EBUSY; 427 error = EBUSY;
428 goto done; 428 goto done;
429 } 429 }
430 430
431 /* Duplicate check */ 431 /* Duplicate check */
432 checkmib = vlan_lookup_tag_psref(p, vid, &psref); 432 checkmib = vlan_lookup_tag_psref(p, vid, &psref);
433 if (checkmib != NULL) { 433 if (checkmib != NULL) {
434 vlan_putref_linkmib(checkmib, &psref); 434 vlan_putref_linkmib(checkmib, &psref);
435 error = EEXIST; 435 error = EEXIST;
436 goto done; 436 goto done;
437 } 437 }
438 438
439 *nmib = *omib; 439 *nmib = *omib;
440 nmib_psref = &nmib->ifvm_psref; 440 nmib_psref = &nmib->ifvm_psref;
441 441
442 psref_target_init(nmib_psref, ifvm_psref_class); 442 psref_target_init(nmib_psref, ifvm_psref_class);
443 443
444 switch (p->if_type) { 444 switch (p->if_type) {
445 case IFT_ETHER: 445 case IFT_ETHER:
446 { 446 {
447 struct ethercom *ec = (void *)p; 447 struct ethercom *ec = (void *)p;
448 struct vlanid_list *vidmem; 448 struct vlanid_list *vidmem;
449 449
450 nmib->ifvm_msw = &vlan_ether_multisw; 450 nmib->ifvm_msw = &vlan_ether_multisw;
451 nmib->ifvm_encaplen = ETHER_VLAN_ENCAP_LEN; 451 nmib->ifvm_encaplen = ETHER_VLAN_ENCAP_LEN;
452 nmib->ifvm_mintu = ETHERMIN; 452 nmib->ifvm_mintu = ETHERMIN;
453 453
454 if (ec->ec_nvlans++ == 0) { 454 if (ec->ec_nvlans++ == 0) {
455 IFNET_LOCK(p); 455 IFNET_LOCK(p);
456 error = ether_enable_vlan_mtu(p); 456 error = ether_enable_vlan_mtu(p);
457 IFNET_UNLOCK(p); 457 IFNET_UNLOCK(p);
458 if (error >= 0) { 458 if (error >= 0) {
459 if (error) { 459 if (error) {
460 ec->ec_nvlans--; 460 ec->ec_nvlans--;
461 goto done; 461 goto done;
462 } 462 }
463 nmib->ifvm_mtufudge = 0; 463 nmib->ifvm_mtufudge = 0;
464 } else { 464 } else {
465 /* 465 /*
466 * Fudge the MTU by the encapsulation size. This 466 * Fudge the MTU by the encapsulation size. This
467 * makes us incompatible with strictly compliant 467 * makes us incompatible with strictly compliant
468 * 802.1Q implementations, but allows us to use 468 * 802.1Q implementations, but allows us to use
469 * the feature with other NetBSD 469 * the feature with other NetBSD
470 * implementations, which might still be useful. 470 * implementations, which might still be useful.
471 */ 471 */
472 nmib->ifvm_mtufudge = nmib->ifvm_encaplen; 472 nmib->ifvm_mtufudge = nmib->ifvm_encaplen;
473 } 473 }
474 error = 0; 474 error = 0;
475 } 475 }
476 /* Add a vid to the list */ 476 /* Add a vid to the list */
477 vidmem = kmem_alloc(sizeof(struct vlanid_list), KM_SLEEP); 477 vidmem = kmem_alloc(sizeof(struct vlanid_list), KM_SLEEP);
478 vidmem->vid = vid; 478 vidmem->vid = vid;
479 ETHER_LOCK(ec); 479 ETHER_LOCK(ec);
480 SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidmem, vid_list); 480 SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidmem, vid_list);
481 ETHER_UNLOCK(ec); 481 ETHER_UNLOCK(ec);
482 482
483 if (ec->ec_vlan_cb != NULL) { 483 if (ec->ec_vlan_cb != NULL) {
484 /* 484 /*
485 * Call ec_vlan_cb(). It will setup VLAN HW filter or 485 * Call ec_vlan_cb(). It will setup VLAN HW filter or
486 * HW tagging function. 486 * HW tagging function.
487 */ 487 */
488 error = (*ec->ec_vlan_cb)(ec, vid, true); 488 error = (*ec->ec_vlan_cb)(ec, vid, true);
489 if (error) { 489 if (error) {
490 ec->ec_nvlans--; 490 ec->ec_nvlans--;
491 if (ec->ec_nvlans == 0) { 491 if (ec->ec_nvlans == 0) {
492 IFNET_LOCK(p); 492 IFNET_LOCK(p);
493 (void)ether_disable_vlan_mtu(p); 493 (void)ether_disable_vlan_mtu(p);
494 IFNET_UNLOCK(p); 494 IFNET_UNLOCK(p);
495 } 495 }
496 goto done; 496 goto done;
497 } 497 }
498 } 498 }
499 /* 499 /*
500 * If the parent interface can do hardware-assisted 500 * If the parent interface can do hardware-assisted
501 * VLAN encapsulation, then propagate its hardware- 501 * VLAN encapsulation, then propagate its hardware-
502 * assisted checksumming flags and tcp segmentation 502 * assisted checksumming flags and tcp segmentation
503 * offload. 503 * offload.
504 */ 504 */
505 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) { 505 if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
506 ifp->if_capabilities = p->if_capabilities & 506 ifp->if_capabilities = p->if_capabilities &
507 (IFCAP_TSOv4 | IFCAP_TSOv6 | 507 (IFCAP_TSOv4 | IFCAP_TSOv6 |
508 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx | 508 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
509 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx | 509 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
510 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx | 510 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
511 IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx | 511 IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx |
512 IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx); 512 IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx);
513 } 513 }
514 514
515 /* 515 /*
516 * We inherit the parent's Ethernet address. 516 * We inherit the parent's Ethernet address.
517 */ 517 */
518 ether_ifattach(ifp, CLLADDR(p->if_sadl)); 518 ether_ifattach(ifp, CLLADDR(p->if_sadl));
519 ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */ 519 ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
520 break; 520 break;
521 } 521 }
522 522
523 default: 523 default:
524 error = EPROTONOSUPPORT; 524 error = EPROTONOSUPPORT;
525 goto done; 525 goto done;
526 } 526 }
527 527
528 nmib->ifvm_p = p; 528 nmib->ifvm_p = p;
529 nmib->ifvm_tag = vid; 529 nmib->ifvm_tag = vid;
530 ifv->ifv_if.if_mtu = p->if_mtu - nmib->ifvm_mtufudge; 530 ifv->ifv_if.if_mtu = p->if_mtu - nmib->ifvm_mtufudge;
531 ifv->ifv_if.if_flags = p->if_flags & 531 ifv->ifv_if.if_flags = p->if_flags &
532 (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 532 (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
533 533
534 /* 534 /*
535 * Inherit the if_type from the parent. This allows us 535 * Inherit the if_type from the parent. This allows us
536 * to participate in bridges of that type. 536 * to participate in bridges of that type.
537 */ 537 */
538 ifv->ifv_if.if_type = p->if_type; 538 ifv->ifv_if.if_type = p->if_type;
539 539
540 PSLIST_ENTRY_INIT(ifv, ifv_hash); 540 PSLIST_ENTRY_INIT(ifv, ifv_hash);
541 idx = vlan_tag_hash(vid, ifv_hash.mask); 541 idx = vlan_tag_hash(vid, ifv_hash.mask);
542 542
543 mutex_enter(&ifv_hash.lock); 543 mutex_enter(&ifv_hash.lock);
544 PSLIST_WRITER_INSERT_HEAD(&ifv_hash.lists[idx], ifv, ifv_hash); 544 PSLIST_WRITER_INSERT_HEAD(&ifv_hash.lists[idx], ifv, ifv_hash);
545 mutex_exit(&ifv_hash.lock); 545 mutex_exit(&ifv_hash.lock);
546 546
547 vlan_linkmib_update(ifv, nmib); 547 vlan_linkmib_update(ifv, nmib);
548 nmib = NULL; 548 nmib = NULL;
549 nmib_psref = NULL; 549 nmib_psref = NULL;
550 omib_cleanup = true; 550 omib_cleanup = true;
551 551
552 552
553 /* 553 /*
554 * We inherit the parents link state. 554 * We inherit the parents link state.
555 */ 555 */
556 if_link_state_change(&ifv->ifv_if, p->if_link_state); 556 if_link_state_change(&ifv->ifv_if, p->if_link_state);
557 557
558done: 558done:
559 mutex_exit(&ifv->ifv_lock); 559 mutex_exit(&ifv->ifv_lock);
560 560
561 if (nmib_psref) 561 if (nmib_psref)
562 psref_target_destroy(nmib_psref, ifvm_psref_class); 562 psref_target_destroy(nmib_psref, ifvm_psref_class);
563 if (nmib) 563 if (nmib)
564 kmem_free(nmib, sizeof(*nmib)); 564 kmem_free(nmib, sizeof(*nmib));
565 if (omib_cleanup) 565 if (omib_cleanup)
566 kmem_free(omib, sizeof(*omib)); 566 kmem_free(omib, sizeof(*omib));
567 567
568 return error; 568 return error;
569} 569}
570 570
571/* 571/*
572 * Unconfigure a VLAN interface. 572 * Unconfigure a VLAN interface.
573 */ 573 */
574static void 574static void
575vlan_unconfig(struct ifnet *ifp) 575vlan_unconfig(struct ifnet *ifp)
576{ 576{
577 struct ifvlan *ifv = ifp->if_softc; 577 struct ifvlan *ifv = ifp->if_softc;
578 struct ifvlan_linkmib *nmib = NULL; 578 struct ifvlan_linkmib *nmib = NULL;
579 int error; 579 int error;
580 580
581 KASSERT(IFNET_LOCKED(ifp)); 581 KASSERT(IFNET_LOCKED(ifp));
582 582
583 nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP); 583 nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
584 584
585 mutex_enter(&ifv->ifv_lock); 585 mutex_enter(&ifv->ifv_lock);
586 error = vlan_unconfig_locked(ifv, nmib); 586 error = vlan_unconfig_locked(ifv, nmib);
587 mutex_exit(&ifv->ifv_lock); 587 mutex_exit(&ifv->ifv_lock);
588 588
589 if (error) 589 if (error)
590 kmem_free(nmib, sizeof(*nmib)); 590 kmem_free(nmib, sizeof(*nmib));
591} 591}
592static int 592static int
593vlan_unconfig_locked(struct ifvlan *ifv, struct ifvlan_linkmib *nmib) 593vlan_unconfig_locked(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
594{ 594{
595 struct ifnet *p; 595 struct ifnet *p;
596 struct ifnet *ifp = &ifv->ifv_if; 596 struct ifnet *ifp = &ifv->ifv_if;
597 struct psref_target *nmib_psref = NULL; 597 struct psref_target *nmib_psref = NULL;
598 struct ifvlan_linkmib *omib; 598 struct ifvlan_linkmib *omib;
599 int error = 0; 599 int error = 0;
600 600
601 KASSERT(IFNET_LOCKED(ifp)); 601 KASSERT(IFNET_LOCKED(ifp));
602 KASSERT(mutex_owned(&ifv->ifv_lock)); 602 KASSERT(mutex_owned(&ifv->ifv_lock));
603 603
604 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING); 604 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
605 605
606 omib = ifv->ifv_mib; 606 omib = ifv->ifv_mib;
607 p = omib->ifvm_p; 607 p = omib->ifvm_p;
608 608
609 if (p == NULL) { 609 if (p == NULL) {
610 error = -1; 610 error = -1;
611 goto done; 611 goto done;
612 } 612 }
613 613
614 *nmib = *omib; 614 *nmib = *omib;
615 nmib_psref = &nmib->ifvm_psref; 615 nmib_psref = &nmib->ifvm_psref;
616 psref_target_init(nmib_psref, ifvm_psref_class); 616 psref_target_init(nmib_psref, ifvm_psref_class);
617 617
618 /* 618 /*
619 * Since the interface is being unconfigured, we need to empty the 619 * Since the interface is being unconfigured, we need to empty the
620 * list of multicast groups that we may have joined while we were 620 * list of multicast groups that we may have joined while we were
621 * alive and remove them from the parent's list also. 621 * alive and remove them from the parent's list also.
622 */ 622 */
623 (*nmib->ifvm_msw->vmsw_purgemulti)(ifv); 623 (*nmib->ifvm_msw->vmsw_purgemulti)(ifv);
624 624
625 /* Disconnect from parent. */ 625 /* Disconnect from parent. */
626 switch (p->if_type) { 626 switch (p->if_type) {
627 case IFT_ETHER: 627 case IFT_ETHER:
628 { 628 {
629 struct ethercom *ec = (void *)p; 629 struct ethercom *ec = (void *)p;
630 struct vlanid_list *vlanidp; 630 struct vlanid_list *vlanidp;
631 uint16_t vid = EVL_VLANOFTAG(nmib->ifvm_tag); 631 uint16_t vid = EVL_VLANOFTAG(nmib->ifvm_tag);
632 632
633 ETHER_LOCK(ec); 633 ETHER_LOCK(ec);
634 SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) { 634 SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
635 if (vlanidp->vid == vid) { 635 if (vlanidp->vid == vid) {
636 SIMPLEQ_REMOVE(&ec->ec_vids, vlanidp, 636 SIMPLEQ_REMOVE(&ec->ec_vids, vlanidp,
637 vlanid_list, vid_list); 637 vlanid_list, vid_list);
638 break; 638 break;
639 } 639 }
640 } 640 }
641 ETHER_UNLOCK(ec); 641 ETHER_UNLOCK(ec);
642 if (vlanidp != NULL) 642 if (vlanidp != NULL)
643 kmem_free(vlanidp, sizeof(*vlanidp)); 643 kmem_free(vlanidp, sizeof(*vlanidp));
644 644
645 if (ec->ec_vlan_cb != NULL) { 645 if (ec->ec_vlan_cb != NULL) {
646 /* 646 /*
647 * Call ec_vlan_cb(). It will setup VLAN HW filter or 647 * Call ec_vlan_cb(). It will setup VLAN HW filter or
648 * HW tagging function. 648 * HW tagging function.
649 */ 649 */
650 (void)(*ec->ec_vlan_cb)(ec, vid, false); 650 (void)(*ec->ec_vlan_cb)(ec, vid, false);
651 } 651 }
652 if (--ec->ec_nvlans == 0) { 652 if (--ec->ec_nvlans == 0) {
653 IFNET_LOCK(p); 653 IFNET_LOCK(p);
654 (void)ether_disable_vlan_mtu(p); 654 (void)ether_disable_vlan_mtu(p);
655 IFNET_UNLOCK(p); 655 IFNET_UNLOCK(p);
656 } 656 }
657 657
658 /* XXX ether_ifdetach must not be called with IFNET_LOCK */ 658 /* XXX ether_ifdetach must not be called with IFNET_LOCK */
659 mutex_exit(&ifv->ifv_lock); 659 mutex_exit(&ifv->ifv_lock);
660 IFNET_UNLOCK(ifp); 660 IFNET_UNLOCK(ifp);
661 ether_ifdetach(ifp); 661 ether_ifdetach(ifp);
662 IFNET_LOCK(ifp); 662 IFNET_LOCK(ifp);
663 mutex_enter(&ifv->ifv_lock); 663 mutex_enter(&ifv->ifv_lock);
664 664
665 /* if_free_sadl must be called with IFNET_LOCK */ 665 /* if_free_sadl must be called with IFNET_LOCK */
666 if_free_sadl(ifp, 1); 666 if_free_sadl(ifp, 1);
667 667
668 /* Restore vlan_ioctl overwritten by ether_ifdetach */ 668 /* Restore vlan_ioctl overwritten by ether_ifdetach */
669 ifp->if_ioctl = vlan_ioctl; 669 ifp->if_ioctl = vlan_ioctl;
670 vlan_reset_linkname(ifp); 670 vlan_reset_linkname(ifp);
671 break; 671 break;
672 } 672 }
673 673
674 default: 674 default:
675 panic("%s: impossible", __func__); 675 panic("%s: impossible", __func__);
676 } 676 }
677 677
678 nmib->ifvm_p = NULL; 678 nmib->ifvm_p = NULL;
679 ifv->ifv_if.if_mtu = 0; 679 ifv->ifv_if.if_mtu = 0;
680 ifv->ifv_flags = 0; 680 ifv->ifv_flags = 0;
681 681
682 mutex_enter(&ifv_hash.lock); 682 mutex_enter(&ifv_hash.lock);
683 PSLIST_WRITER_REMOVE(ifv, ifv_hash); 683 PSLIST_WRITER_REMOVE(ifv, ifv_hash);
684 pserialize_perform(vlan_psz); 684 pserialize_perform(vlan_psz);
685 mutex_exit(&ifv_hash.lock); 685 mutex_exit(&ifv_hash.lock);
686 PSLIST_ENTRY_DESTROY(ifv, ifv_hash); 686 PSLIST_ENTRY_DESTROY(ifv, ifv_hash);
687 687
688 vlan_linkmib_update(ifv, nmib); 688 vlan_linkmib_update(ifv, nmib);
689 if_link_state_change(ifp, LINK_STATE_DOWN); 689 if_link_state_change(ifp, LINK_STATE_DOWN);
690 690
691 mutex_exit(&ifv->ifv_lock); 691 mutex_exit(&ifv->ifv_lock);
692 692
693 nmib_psref = NULL; 693 nmib_psref = NULL;
694 kmem_free(omib, sizeof(*omib)); 694 kmem_free(omib, sizeof(*omib));
695 695
696#ifdef INET6 696#ifdef INET6
697 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 697 KERNEL_LOCK_UNLESS_NET_MPSAFE();
698 /* To delete v6 link local addresses */ 698 /* To delete v6 link local addresses */
699 if (in6_present) 699 if (in6_present)
700 in6_ifdetach(ifp); 700 in6_ifdetach(ifp);
701 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 701 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
702#endif 702#endif
703 703
704 if ((ifp->if_flags & IFF_PROMISC) != 0) 
705 vlan_safe_ifpromisc_locked(ifp, 0); 
706 if_down_locked(ifp); 704 if_down_locked(ifp);
707 ifp->if_capabilities = 0; 705 ifp->if_capabilities = 0;
708 mutex_enter(&ifv->ifv_lock); 706 mutex_enter(&ifv->ifv_lock);
709done: 707done:
710 708
711 if (nmib_psref) 709 if (nmib_psref)
712 psref_target_destroy(nmib_psref, ifvm_psref_class); 710 psref_target_destroy(nmib_psref, ifvm_psref_class);
713 711
714 return error; 712 return error;
715} 713}
716 714
717static void 715static void
718vlan_hash_init(void) 716vlan_hash_init(void)
719{ 717{
720 718
721 ifv_hash.lists = hashinit(VLAN_TAG_HASH_SIZE, HASH_PSLIST, true, 719 ifv_hash.lists = hashinit(VLAN_TAG_HASH_SIZE, HASH_PSLIST, true,
722 &ifv_hash.mask); 720 &ifv_hash.mask);
723} 721}
724 722
725static int 723static int
726vlan_hash_fini(void) 724vlan_hash_fini(void)
727{ 725{
728 int i; 726 int i;
729 727
730 mutex_enter(&ifv_hash.lock); 728 mutex_enter(&ifv_hash.lock);
731 729
732 for (i = 0; i < ifv_hash.mask + 1; i++) { 730 for (i = 0; i < ifv_hash.mask + 1; i++) {
733 if (PSLIST_WRITER_FIRST(&ifv_hash.lists[i], struct ifvlan, 731 if (PSLIST_WRITER_FIRST(&ifv_hash.lists[i], struct ifvlan,
734 ifv_hash) != NULL) { 732 ifv_hash) != NULL) {
735 mutex_exit(&ifv_hash.lock); 733 mutex_exit(&ifv_hash.lock);
736 return EBUSY; 734 return EBUSY;
737 } 735 }
738 } 736 }
739 737
740 for (i = 0; i < ifv_hash.mask + 1; i++) 738 for (i = 0; i < ifv_hash.mask + 1; i++)
741 PSLIST_DESTROY(&ifv_hash.lists[i]); 739 PSLIST_DESTROY(&ifv_hash.lists[i]);
742 740
743 mutex_exit(&ifv_hash.lock); 741 mutex_exit(&ifv_hash.lock);
744 742
745 hashdone(ifv_hash.lists, HASH_PSLIST, ifv_hash.mask); 743 hashdone(ifv_hash.lists, HASH_PSLIST, ifv_hash.mask);
746 744
747 ifv_hash.lists = NULL; 745 ifv_hash.lists = NULL;
748 ifv_hash.mask = 0; 746 ifv_hash.mask = 0;
749 747
750 return 0; 748 return 0;
751} 749}
752 750
753static int 751static int
754vlan_tag_hash(uint16_t tag, u_long mask) 752vlan_tag_hash(uint16_t tag, u_long mask)
755{ 753{
756 uint32_t hash; 754 uint32_t hash;
757 755
758 hash = (tag >> 8) ^ tag; 756 hash = (tag >> 8) ^ tag;
759 hash = (hash >> 2) ^ hash; 757 hash = (hash >> 2) ^ hash;
760 758
761 return hash & mask; 759 return hash & mask;
762} 760}
763 761
764static struct ifvlan_linkmib * 762static struct ifvlan_linkmib *
765vlan_getref_linkmib(struct ifvlan *sc, struct psref *psref) 763vlan_getref_linkmib(struct ifvlan *sc, struct psref *psref)
766{ 764{
767 struct ifvlan_linkmib *mib; 765 struct ifvlan_linkmib *mib;
768 int s; 766 int s;
769 767
770 s = pserialize_read_enter(); 768 s = pserialize_read_enter();
771 mib = atomic_load_consume(&sc->ifv_mib); 769 mib = atomic_load_consume(&sc->ifv_mib);
772 if (mib == NULL) { 770 if (mib == NULL) {
773 pserialize_read_exit(s); 771 pserialize_read_exit(s);
774 return NULL; 772 return NULL;
775 } 773 }
776 psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class); 774 psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
777 pserialize_read_exit(s); 775 pserialize_read_exit(s);
778 776
779 return mib; 777 return mib;
780} 778}
781 779
782static void 780static void
783vlan_putref_linkmib(struct ifvlan_linkmib *mib, struct psref *psref) 781vlan_putref_linkmib(struct ifvlan_linkmib *mib, struct psref *psref)
784{ 782{
785 if (mib == NULL) 783 if (mib == NULL)
786 return; 784 return;
787 psref_release(psref, &mib->ifvm_psref, ifvm_psref_class); 785 psref_release(psref, &mib->ifvm_psref, ifvm_psref_class);
788} 786}
789 787
790static struct ifvlan_linkmib * 788static struct ifvlan_linkmib *
791vlan_lookup_tag_psref(struct ifnet *ifp, uint16_t tag, struct psref *psref) 789vlan_lookup_tag_psref(struct ifnet *ifp, uint16_t tag, struct psref *psref)
792{ 790{
793 int idx; 791 int idx;
794 int s; 792 int s;
795 struct ifvlan *sc; 793 struct ifvlan *sc;
796 794
797 idx = vlan_tag_hash(tag, ifv_hash.mask); 795 idx = vlan_tag_hash(tag, ifv_hash.mask);
798 796
799 s = pserialize_read_enter(); 797 s = pserialize_read_enter();
800 PSLIST_READER_FOREACH(sc, &ifv_hash.lists[idx], struct ifvlan, 798 PSLIST_READER_FOREACH(sc, &ifv_hash.lists[idx], struct ifvlan,
801 ifv_hash) { 799 ifv_hash) {
802 struct ifvlan_linkmib *mib = atomic_load_consume(&sc->ifv_mib); 800 struct ifvlan_linkmib *mib = atomic_load_consume(&sc->ifv_mib);
803 if (mib == NULL) 801 if (mib == NULL)
804 continue; 802 continue;
805 if (mib->ifvm_tag != tag) 803 if (mib->ifvm_tag != tag)
806 continue; 804 continue;
807 if (mib->ifvm_p != ifp) 805 if (mib->ifvm_p != ifp)
808 continue; 806 continue;
809 807
810 psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class); 808 psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
811 pserialize_read_exit(s); 809 pserialize_read_exit(s);
812 return mib; 810 return mib;
813 } 811 }
814 pserialize_read_exit(s); 812 pserialize_read_exit(s);
815 return NULL; 813 return NULL;
816} 814}
817 815
818static void 816static void
819vlan_linkmib_update(struct ifvlan *ifv, struct ifvlan_linkmib *nmib) 817vlan_linkmib_update(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
820{ 818{
821 struct ifvlan_linkmib *omib = ifv->ifv_mib; 819 struct ifvlan_linkmib *omib = ifv->ifv_mib;
822 820
823 KASSERT(mutex_owned(&ifv->ifv_lock)); 821 KASSERT(mutex_owned(&ifv->ifv_lock));
824 822
825 atomic_store_release(&ifv->ifv_mib, nmib); 823 atomic_store_release(&ifv->ifv_mib, nmib);
826 824
827 pserialize_perform(ifv->ifv_psz); 825 pserialize_perform(ifv->ifv_psz);
828 psref_target_destroy(&omib->ifvm_psref, ifvm_psref_class); 826 psref_target_destroy(&omib->ifvm_psref, ifvm_psref_class);
829} 827}
830 828
831/* 829/*
832 * Called when a parent interface is detaching; destroy any VLAN 830 * Called when a parent interface is detaching; destroy any VLAN
833 * configuration for the parent interface. 831 * configuration for the parent interface.
834 */ 832 */
835void 833void
836vlan_ifdetach(struct ifnet *p) 834vlan_ifdetach(struct ifnet *p)
837{ 835{
838 struct ifvlan *ifv; 836 struct ifvlan *ifv;
839 struct ifvlan_linkmib *mib, **nmibs; 837 struct ifvlan_linkmib *mib, **nmibs;
840 struct psref psref; 838 struct psref psref;
841 int error; 839 int error;
842 int bound; 840 int bound;
843 int i, cnt = 0; 841 int i, cnt = 0;
844 842
845 bound = curlwp_bind(); 843 bound = curlwp_bind();
846 844
847 mutex_enter(&ifv_list.lock); 845 mutex_enter(&ifv_list.lock);
848 LIST_FOREACH(ifv, &ifv_list.list, ifv_list) { 846 LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
849 mib = vlan_getref_linkmib(ifv, &psref); 847 mib = vlan_getref_linkmib(ifv, &psref);
850 if (mib == NULL) 848 if (mib == NULL)
851 continue; 849 continue;
852 850
853 if (mib->ifvm_p == p) 851 if (mib->ifvm_p == p)
854 cnt++; 852 cnt++;
855 853
856 vlan_putref_linkmib(mib, &psref); 854 vlan_putref_linkmib(mib, &psref);
857 } 855 }
858 mutex_exit(&ifv_list.lock); 856 mutex_exit(&ifv_list.lock);
859 857
860 if (cnt == 0) { 858 if (cnt == 0) {
861 curlwp_bindx(bound); 859 curlwp_bindx(bound);
862 return; 860 return;
863 } 861 }
864 862
865 /* 863 /*
866 * The value of "cnt" does not increase while ifv_list.lock 864 * The value of "cnt" does not increase while ifv_list.lock
867 * and ifv->ifv_lock are released here, because the parent 865 * and ifv->ifv_lock are released here, because the parent
868 * interface is detaching. 866 * interface is detaching.
869 */ 867 */
870 nmibs = kmem_alloc(sizeof(*nmibs) * cnt, KM_SLEEP); 868 nmibs = kmem_alloc(sizeof(*nmibs) * cnt, KM_SLEEP);
871 for (i = 0; i < cnt; i++) { 869 for (i = 0; i < cnt; i++) {
872 nmibs[i] = kmem_alloc(sizeof(*nmibs[i]), KM_SLEEP); 870 nmibs[i] = kmem_alloc(sizeof(*nmibs[i]), KM_SLEEP);
873 } 871 }
874 872
875 mutex_enter(&ifv_list.lock); 873 mutex_enter(&ifv_list.lock);
876 874
877 i = 0; 875 i = 0;
878 LIST_FOREACH(ifv, &ifv_list.list, ifv_list) { 876 LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
879 struct ifnet *ifp = &ifv->ifv_if; 877 struct ifnet *ifp = &ifv->ifv_if;
880 878
881 /* IFNET_LOCK must be held before ifv_lock. */ 879 /* IFNET_LOCK must be held before ifv_lock. */
882 IFNET_LOCK(ifp); 880 IFNET_LOCK(ifp);
883 mutex_enter(&ifv->ifv_lock); 881 mutex_enter(&ifv->ifv_lock);
884 882
885 /* XXX ifv_mib = NULL? */ 883 /* XXX ifv_mib = NULL? */
886 if (ifv->ifv_mib->ifvm_p == p) { 884 if (ifv->ifv_mib->ifvm_p == p) {
887 KASSERTMSG(i < cnt, 885 KASSERTMSG(i < cnt,
888 "no memory for unconfig, parent=%s", p->if_xname); 886 "no memory for unconfig, parent=%s", p->if_xname);
889 error = vlan_unconfig_locked(ifv, nmibs[i]); 887 error = vlan_unconfig_locked(ifv, nmibs[i]);
890 if (!error) { 888 if (!error) {
891 nmibs[i] = NULL; 889 nmibs[i] = NULL;
892 i++; 890 i++;
893 } 891 }
894 892
895 } 893 }
896 894
897 mutex_exit(&ifv->ifv_lock); 895 mutex_exit(&ifv->ifv_lock);
898 IFNET_UNLOCK(ifp); 896 IFNET_UNLOCK(ifp);
899 } 897 }
900 898
901 mutex_exit(&ifv_list.lock); 899 mutex_exit(&ifv_list.lock);
902 900
903 curlwp_bindx(bound); 901 curlwp_bindx(bound);
904 902
905 for (i = 0; i < cnt; i++) { 903 for (i = 0; i < cnt; i++) {
906 if (nmibs[i]) 904 if (nmibs[i])
907 kmem_free(nmibs[i], sizeof(*nmibs[i])); 905 kmem_free(nmibs[i], sizeof(*nmibs[i]));
908 } 906 }
909 907
910 kmem_free(nmibs, sizeof(*nmibs) * cnt); 908 kmem_free(nmibs, sizeof(*nmibs) * cnt);
911 909
912 return; 910 return;
913} 911}
914 912
915static int 913static int
916vlan_set_promisc(struct ifnet *ifp) 914vlan_set_promisc(struct ifnet *ifp)
917{ 915{
918 struct ifvlan *ifv = ifp->if_softc; 916 struct ifvlan *ifv = ifp->if_softc;
919 struct ifvlan_linkmib *mib; 917 struct ifvlan_linkmib *mib;
920 struct psref psref; 918 struct psref psref;
921 int error = 0; 919 int error = 0;
922 int bound; 920 int bound;
923 921
924 bound = curlwp_bind(); 922 bound = curlwp_bind();
925 mib = vlan_getref_linkmib(ifv, &psref); 923 mib = vlan_getref_linkmib(ifv, &psref);
926 if (mib == NULL) { 924 if (mib == NULL) {
927 curlwp_bindx(bound); 925 curlwp_bindx(bound);
928 return EBUSY; 926 return EBUSY;
929 } 927 }
930 928
931 if ((ifp->if_flags & IFF_PROMISC) != 0) { 929 if ((ifp->if_flags & IFF_PROMISC) != 0) {
932 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) { 930 if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
933 error = vlan_safe_ifpromisc(mib->ifvm_p, 1); 931 error = vlan_safe_ifpromisc(mib->ifvm_p, 1);
934 if (error == 0) 932 if (error == 0)
935 ifv->ifv_flags |= IFVF_PROMISC; 933 ifv->ifv_flags |= IFVF_PROMISC;
936 } 934 }
937 } else { 935 } else {
938 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) { 936 if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
939 error = vlan_safe_ifpromisc(mib->ifvm_p, 0); 937 error = vlan_safe_ifpromisc(mib->ifvm_p, 0);
940 if (error == 0) 938 if (error == 0)
941 ifv->ifv_flags &= ~IFVF_PROMISC; 939 ifv->ifv_flags &= ~IFVF_PROMISC;
942 } 940 }
943 } 941 }
944 vlan_putref_linkmib(mib, &psref); 942 vlan_putref_linkmib(mib, &psref);
945 curlwp_bindx(bound); 943 curlwp_bindx(bound);
946 944
947 return error; 945 return error;
948} 946}
949 947
950static int 948static int
951vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data) 949vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data)
952{ 950{
953 struct lwp *l = curlwp; 951 struct lwp *l = curlwp;
954 struct ifvlan *ifv = ifp->if_softc; 952 struct ifvlan *ifv = ifp->if_softc;
955 struct ifaddr *ifa = (struct ifaddr *) data; 953 struct ifaddr *ifa = (struct ifaddr *) data;
956 struct ifreq *ifr = (struct ifreq *) data; 954 struct ifreq *ifr = (struct ifreq *) data;
957 struct ifnet *pr; 955 struct ifnet *pr;
958 struct ifcapreq *ifcr; 956 struct ifcapreq *ifcr;
959 struct vlanreq vlr; 957 struct vlanreq vlr;
960 struct ifvlan_linkmib *mib; 958 struct ifvlan_linkmib *mib;
961 struct psref psref; 959 struct psref psref;
962 int error = 0; 960 int error = 0;
963 int bound; 961 int bound;
964 962
965 switch (cmd) { 963 switch (cmd) {
966 case SIOCSIFMTU: 964 case SIOCSIFMTU:
967 bound = curlwp_bind(); 965 bound = curlwp_bind();
968 mib = vlan_getref_linkmib(ifv, &psref); 966 mib = vlan_getref_linkmib(ifv, &psref);
969 if (mib == NULL) { 967 if (mib == NULL) {
970 curlwp_bindx(bound); 968 curlwp_bindx(bound);
971 error = EBUSY; 969 error = EBUSY;
972 break; 970 break;
973 } 971 }
974 972
975 if (mib->ifvm_p == NULL) { 973 if (mib->ifvm_p == NULL) {
976 vlan_putref_linkmib(mib, &psref); 974 vlan_putref_linkmib(mib, &psref);
977 curlwp_bindx(bound); 975 curlwp_bindx(bound);
978 error = EINVAL; 976 error = EINVAL;
979 } else if ( 977 } else if (
980 ifr->ifr_mtu > (mib->ifvm_p->if_mtu - mib->ifvm_mtufudge) || 978 ifr->ifr_mtu > (mib->ifvm_p->if_mtu - mib->ifvm_mtufudge) ||
981 ifr->ifr_mtu < (mib->ifvm_mintu - mib->ifvm_mtufudge)) { 979 ifr->ifr_mtu < (mib->ifvm_mintu - mib->ifvm_mtufudge)) {
982 vlan_putref_linkmib(mib, &psref); 980 vlan_putref_linkmib(mib, &psref);
983 curlwp_bindx(bound); 981 curlwp_bindx(bound);
984 error = EINVAL; 982 error = EINVAL;
985 } else { 983 } else {
986 vlan_putref_linkmib(mib, &psref); 984 vlan_putref_linkmib(mib, &psref);
987 curlwp_bindx(bound); 985 curlwp_bindx(bound);
988 986
989 error = ifioctl_common(ifp, cmd, data); 987 error = ifioctl_common(ifp, cmd, data);
990 if (error == ENETRESET) 988 if (error == ENETRESET)
991 error = 0; 989 error = 0;
992 } 990 }
993 991
994 break; 992 break;
995 993
996 case SIOCSETVLAN: 994 case SIOCSETVLAN:
997 if ((error = kauth_authorize_network(l->l_cred, 995 if ((error = kauth_authorize_network(l->l_cred,
998 KAUTH_NETWORK_INTERFACE, 996 KAUTH_NETWORK_INTERFACE,
999 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 997 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1000 NULL)) != 0) 998 NULL)) != 0)
1001 break; 999 break;
1002 if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0) 1000 if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
1003 break; 1001 break;
1004 1002
1005 if (vlr.vlr_parent[0] == '\0') { 1003 if (vlr.vlr_parent[0] == '\0') {
1006 bound = curlwp_bind(); 1004 bound = curlwp_bind();
1007 mib = vlan_getref_linkmib(ifv, &psref); 1005 mib = vlan_getref_linkmib(ifv, &psref);
1008 if (mib == NULL) { 1006 if (mib == NULL) {
1009 curlwp_bindx(bound); 1007 curlwp_bindx(bound);
1010 error = EBUSY; 1008 error = EBUSY;
1011 break; 1009 break;
1012 } 1010 }
1013 1011
1014 if (mib->ifvm_p != NULL && 1012 if (mib->ifvm_p != NULL &&
1015 (ifp->if_flags & IFF_PROMISC) != 0) 1013 (ifp->if_flags & IFF_PROMISC) != 0)
1016 error = vlan_safe_ifpromisc(mib->ifvm_p, 0); 1014 error = vlan_safe_ifpromisc(mib->ifvm_p, 0);
1017 1015
1018 vlan_putref_linkmib(mib, &psref); 1016 vlan_putref_linkmib(mib, &psref);
1019 curlwp_bindx(bound); 1017 curlwp_bindx(bound);
1020 1018
1021 vlan_unconfig(ifp); 1019 vlan_unconfig(ifp);
1022 break; 1020 break;
1023 } 1021 }
1024 if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) { 1022 if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
1025 error = EINVAL; /* check for valid tag */ 1023 error = EINVAL; /* check for valid tag */
1026 break; 1024 break;
1027 } 1025 }
1028 if ((pr = ifunit(vlr.vlr_parent)) == NULL) { 1026 if ((pr = ifunit(vlr.vlr_parent)) == NULL) {
1029 error = ENOENT; 1027 error = ENOENT;
1030 break; 1028 break;
1031 } 1029 }
1032 1030
1033 error = vlan_config(ifv, pr, vlr.vlr_tag); 1031 error = vlan_config(ifv, pr, vlr.vlr_tag);
1034 if (error != 0) 1032 if (error != 0)
1035 break; 1033 break;
1036 1034
1037 /* Update promiscuous mode, if necessary. */ 1035 /* Update promiscuous mode, if necessary. */
1038 vlan_set_promisc(ifp); 1036 vlan_set_promisc(ifp);
1039 1037
1040 ifp->if_flags |= IFF_RUNNING; 1038 ifp->if_flags |= IFF_RUNNING;
1041 break; 1039 break;
1042 1040
1043 case SIOCGETVLAN: 1041 case SIOCGETVLAN:
1044 memset(&vlr, 0, sizeof(vlr)); 1042 memset(&vlr, 0, sizeof(vlr));
1045 bound = curlwp_bind(); 1043 bound = curlwp_bind();
1046 mib = vlan_getref_linkmib(ifv, &psref); 1044 mib = vlan_getref_linkmib(ifv, &psref);
1047 if (mib == NULL) { 1045 if (mib == NULL) {
1048 curlwp_bindx(bound); 1046 curlwp_bindx(bound);
1049 error = EBUSY; 1047 error = EBUSY;
1050 break; 1048 break;
1051 } 1049 }
1052 if (mib->ifvm_p != NULL) { 1050 if (mib->ifvm_p != NULL) {
1053 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s", 1051 snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
1054 mib->ifvm_p->if_xname); 1052 mib->ifvm_p->if_xname);
1055 vlr.vlr_tag = mib->ifvm_tag; 1053 vlr.vlr_tag = mib->ifvm_tag;
1056 } 1054 }
1057 vlan_putref_linkmib(mib, &psref); 1055 vlan_putref_linkmib(mib, &psref);
1058 curlwp_bindx(bound); 1056 curlwp_bindx(bound);
1059 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr)); 1057 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1060 break; 1058 break;
1061 1059
1062 case SIOCSIFFLAGS: 1060 case SIOCSIFFLAGS:
1063 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1061 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1064 break; 1062 break;
1065 /* 1063 /*
1066 * For promiscuous mode, we enable promiscuous mode on 1064 * For promiscuous mode, we enable promiscuous mode on
1067 * the parent if we need promiscuous on the VLAN interface. 1065 * the parent if we need promiscuous on the VLAN interface.
1068 */ 1066 */
1069 bound = curlwp_bind(); 1067 bound = curlwp_bind();
1070 mib = vlan_getref_linkmib(ifv, &psref); 1068 mib = vlan_getref_linkmib(ifv, &psref);
1071 if (mib == NULL) { 1069 if (mib == NULL) {
1072 curlwp_bindx(bound); 1070 curlwp_bindx(bound);
1073 error = EBUSY; 1071 error = EBUSY;
1074 break; 1072 break;
1075 } 1073 }
1076 1074
1077 if (mib->ifvm_p != NULL) 1075 if (mib->ifvm_p != NULL)
1078 error = vlan_set_promisc(ifp); 1076 error = vlan_set_promisc(ifp);
1079 vlan_putref_linkmib(mib, &psref); 1077 vlan_putref_linkmib(mib, &psref);
1080 curlwp_bindx(bound); 1078 curlwp_bindx(bound);
1081 break; 1079 break;
1082 1080
1083 case SIOCADDMULTI: 1081 case SIOCADDMULTI:
1084 mutex_enter(&ifv->ifv_lock); 1082 mutex_enter(&ifv->ifv_lock);
1085 mib = ifv->ifv_mib; 1083 mib = ifv->ifv_mib;
1086 if (mib == NULL) { 1084 if (mib == NULL) {
1087 error = EBUSY; 1085 error = EBUSY;
1088 mutex_exit(&ifv->ifv_lock); 1086 mutex_exit(&ifv->ifv_lock);
1089 break; 1087 break;
1090 } 1088 }
1091 1089
1092 error = (mib->ifvm_p != NULL) ? 1090 error = (mib->ifvm_p != NULL) ?
1093 (*mib->ifvm_msw->vmsw_addmulti)(ifv, ifr) : EINVAL; 1091 (*mib->ifvm_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
1094 mib = NULL; 1092 mib = NULL;
1095 mutex_exit(&ifv->ifv_lock); 1093 mutex_exit(&ifv->ifv_lock);
1096 break; 1094 break;
1097 1095
1098 case SIOCDELMULTI: 1096 case SIOCDELMULTI:
1099 mutex_enter(&ifv->ifv_lock); 1097 mutex_enter(&ifv->ifv_lock);
1100 mib = ifv->ifv_mib; 1098 mib = ifv->ifv_mib;
1101 if (mib == NULL) { 1099 if (mib == NULL) {
1102 error = EBUSY; 1100 error = EBUSY;
1103 mutex_exit(&ifv->ifv_lock); 1101 mutex_exit(&ifv->ifv_lock);
1104 break; 1102 break;
1105 } 1103 }
1106 error = (mib->ifvm_p != NULL) ? 1104 error = (mib->ifvm_p != NULL) ?
1107 (*mib->ifvm_msw->vmsw_delmulti)(ifv, ifr) : EINVAL; 1105 (*mib->ifvm_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
1108 mib = NULL; 1106 mib = NULL;
1109 mutex_exit(&ifv->ifv_lock); 1107 mutex_exit(&ifv->ifv_lock);
1110 break; 1108 break;
1111 1109
1112 case SIOCSIFCAP: 1110 case SIOCSIFCAP:
1113 ifcr = data; 1111 ifcr = data;
1114 /* make sure caps are enabled on parent */ 1112 /* make sure caps are enabled on parent */
1115 bound = curlwp_bind(); 1113 bound = curlwp_bind();
1116 mib = vlan_getref_linkmib(ifv, &psref); 1114 mib = vlan_getref_linkmib(ifv, &psref);
1117 if (mib == NULL) { 1115 if (mib == NULL) {
1118 curlwp_bindx(bound); 1116 curlwp_bindx(bound);
1119 error = EBUSY; 1117 error = EBUSY;
1120 break; 1118 break;
1121 } 1119 }
1122 1120
1123 if (mib->ifvm_p == NULL) { 1121 if (mib->ifvm_p == NULL) {
1124 vlan_putref_linkmib(mib, &psref); 1122 vlan_putref_linkmib(mib, &psref);
1125 curlwp_bindx(bound); 1123 curlwp_bindx(bound);
1126 error = EINVAL; 1124 error = EINVAL;
1127 break; 1125 break;
1128 } 1126 }
1129 if ((mib->ifvm_p->if_capenable & ifcr->ifcr_capenable) != 1127 if ((mib->ifvm_p->if_capenable & ifcr->ifcr_capenable) !=
1130 ifcr->ifcr_capenable) { 1128 ifcr->ifcr_capenable) {
1131 vlan_putref_linkmib(mib, &psref); 1129 vlan_putref_linkmib(mib, &psref);
1132 curlwp_bindx(bound); 1130 curlwp_bindx(bound);
1133 error = EINVAL; 1131 error = EINVAL;
1134 break; 1132 break;
1135 } 1133 }
1136 1134
1137 vlan_putref_linkmib(mib, &psref); 1135 vlan_putref_linkmib(mib, &psref);
1138 curlwp_bindx(bound); 1136 curlwp_bindx(bound);
1139 1137
1140 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET) 1138 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1141 error = 0; 1139 error = 0;
1142 break; 1140 break;
1143 case SIOCINITIFADDR: 1141 case SIOCINITIFADDR:
1144 bound = curlwp_bind(); 1142 bound = curlwp_bind();
1145 mib = vlan_getref_linkmib(ifv, &psref); 1143 mib = vlan_getref_linkmib(ifv, &psref);
1146 if (mib == NULL) { 1144 if (mib == NULL) {
1147 curlwp_bindx(bound); 1145 curlwp_bindx(bound);
1148 error = EBUSY; 1146 error = EBUSY;
1149 break; 1147 break;
1150 } 1148 }
1151 1149
1152 if (mib->ifvm_p == NULL) { 1150 if (mib->ifvm_p == NULL) {
1153 error = EINVAL; 1151 error = EINVAL;
1154 vlan_putref_linkmib(mib, &psref); 1152 vlan_putref_linkmib(mib, &psref);
1155 curlwp_bindx(bound); 1153 curlwp_bindx(bound);
1156 break; 1154 break;
1157 } 1155 }
1158 vlan_putref_linkmib(mib, &psref); 1156 vlan_putref_linkmib(mib, &psref);
1159 curlwp_bindx(bound); 1157 curlwp_bindx(bound);
1160 1158
1161 ifp->if_flags |= IFF_UP; 1159 ifp->if_flags |= IFF_UP;
1162#ifdef INET 1160#ifdef INET
1163 if (ifa->ifa_addr->sa_family == AF_INET) 1161 if (ifa->ifa_addr->sa_family == AF_INET)
1164 arp_ifinit(ifp, ifa); 1162 arp_ifinit(ifp, ifa);
1165#endif 1163#endif
1166 break; 1164 break;
1167 1165
1168 default: 1166 default:
1169 error = ether_ioctl(ifp, cmd, data); 1167 error = ether_ioctl(ifp, cmd, data);
1170 } 1168 }
1171 1169
1172 return error; 1170 return error;
1173} 1171}
1174 1172
1175static int 1173static int
1176vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr) 1174vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
1177{ 1175{
1178 const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr); 1176 const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
1179 struct vlan_mc_entry *mc; 1177 struct vlan_mc_entry *mc;
1180 uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN]; 1178 uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
1181 struct ifvlan_linkmib *mib; 1179 struct ifvlan_linkmib *mib;
1182 int error; 1180 int error;
1183 1181
1184 KASSERT(mutex_owned(&ifv->ifv_lock)); 1182 KASSERT(mutex_owned(&ifv->ifv_lock));
1185 1183
1186 if (sa->sa_len > sizeof(struct sockaddr_storage)) 1184 if (sa->sa_len > sizeof(struct sockaddr_storage))
1187 return EINVAL; 1185 return EINVAL;
1188 1186
1189 error = ether_addmulti(sa, &ifv->ifv_ec); 1187 error = ether_addmulti(sa, &ifv->ifv_ec);
1190 if (error != ENETRESET) 1188 if (error != ENETRESET)
1191 return error; 1189 return error;
1192 1190
1193 /* 1191 /*
1194 * This is a new multicast address. We have to tell parent 1192 * This is a new multicast address. We have to tell parent
1195 * about it. Also, remember this multicast address so that 1193 * about it. Also, remember this multicast address so that
1196 * we can delete it on unconfigure. 1194 * we can delete it on unconfigure.
1197 */ 1195 */
1198 mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT); 1196 mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
1199 if (mc == NULL) { 1197 if (mc == NULL) {
1200 error = ENOMEM; 1198 error = ENOMEM;
1201 goto alloc_failed; 1199 goto alloc_failed;
1202 } 1200 }
1203 1201
1204 /* 1202 /*
1205 * Since ether_addmulti() returned ENETRESET, the following two 1203 * Since ether_addmulti() returned ENETRESET, the following two
1206 * statements shouldn't fail. Here ifv_ec is implicitly protected 1204 * statements shouldn't fail. Here ifv_ec is implicitly protected
1207 * by the ifv_lock lock. 1205 * by the ifv_lock lock.
1208 */ 1206 */
1209 error = ether_multiaddr(sa, addrlo, addrhi); 1207 error = ether_multiaddr(sa, addrlo, addrhi);
1210 KASSERT(error == 0); 1208 KASSERT(error == 0);
1211 1209
1212 ETHER_LOCK(&ifv->ifv_ec); 1210 ETHER_LOCK(&ifv->ifv_ec);
1213 mc->mc_enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec); 1211 mc->mc_enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec);
1214 ETHER_UNLOCK(&ifv->ifv_ec); 1212 ETHER_UNLOCK(&ifv->ifv_ec);
1215 1213
1216 KASSERT(mc->mc_enm != NULL); 1214 KASSERT(mc->mc_enm != NULL);
1217 1215
1218 memcpy(&mc->mc_addr, sa, sa->sa_len); 1216 memcpy(&mc->mc_addr, sa, sa->sa_len);
1219 LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries); 1217 LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
1220 1218
1221 mib = ifv->ifv_mib; 1219 mib = ifv->ifv_mib;
1222 1220
1223 KERNEL_LOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p); 1221 KERNEL_LOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p);
1224 error = if_mcast_op(mib->ifvm_p, SIOCADDMULTI, sa); 1222 error = if_mcast_op(mib->ifvm_p, SIOCADDMULTI, sa);
1225 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p); 1223 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p);
1226 1224
1227 if (error != 0) 1225 if (error != 0)
1228 goto ioctl_failed; 1226 goto ioctl_failed;
1229 return error; 1227 return error;
1230 1228
1231ioctl_failed: 1229ioctl_failed:
1232 LIST_REMOVE(mc, mc_entries); 1230 LIST_REMOVE(mc, mc_entries);
1233 free(mc, M_DEVBUF); 1231 free(mc, M_DEVBUF);
1234 1232
1235alloc_failed: 1233alloc_failed:
1236 (void)ether_delmulti(sa, &ifv->ifv_ec); 1234 (void)ether_delmulti(sa, &ifv->ifv_ec);
1237 return error; 1235 return error;
1238} 1236}
1239 1237
1240static int 1238static int
1241vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr) 1239vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
1242{ 1240{
1243 const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr); 1241 const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
1244 struct ether_multi *enm; 1242 struct ether_multi *enm;
1245 struct vlan_mc_entry *mc; 1243 struct vlan_mc_entry *mc;
1246 struct ifvlan_linkmib *mib; 1244 struct ifvlan_linkmib *mib;
1247 uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN]; 1245 uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
1248 int error; 1246 int error;
1249 1247
1250 KASSERT(mutex_owned(&ifv->ifv_lock)); 1248 KASSERT(mutex_owned(&ifv->ifv_lock));
1251 1249
1252 /* 1250 /*
1253 * Find a key to lookup vlan_mc_entry. We have to do this 1251 * Find a key to lookup vlan_mc_entry. We have to do this
1254 * before calling ether_delmulti for obvious reasons. 1252 * before calling ether_delmulti for obvious reasons.
1255 */ 1253 */
1256 if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0) 1254 if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
1257 return error; 1255 return error;
1258 1256
1259 ETHER_LOCK(&ifv->ifv_ec); 1257 ETHER_LOCK(&ifv->ifv_ec);
1260 enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec); 1258 enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec);
1261 ETHER_UNLOCK(&ifv->ifv_ec); 1259 ETHER_UNLOCK(&ifv->ifv_ec);
1262 if (enm == NULL) 1260 if (enm == NULL)
1263 return EINVAL; 1261 return EINVAL;
1264 1262
1265 LIST_FOREACH(mc, &ifv->ifv_mc_listhead, mc_entries) { 1263 LIST_FOREACH(mc, &ifv->ifv_mc_listhead, mc_entries) {
1266 if (mc->mc_enm == enm) 1264 if (mc->mc_enm == enm)
1267 break; 1265 break;
1268 } 1266 }
1269 1267
1270 /* We woun't delete entries we didn't add */ 1268 /* We woun't delete entries we didn't add */
1271 if (mc == NULL) 1269 if (mc == NULL)
1272 return EINVAL; 1270 return EINVAL;
1273 1271
1274 error = ether_delmulti(sa, &ifv->ifv_ec); 1272 error = ether_delmulti(sa, &ifv->ifv_ec);
1275 if (error != ENETRESET) 1273 if (error != ENETRESET)
1276 return error; 1274 return error;
1277 1275
1278 /* We no longer use this multicast address. Tell parent so. */ 1276 /* We no longer use this multicast address. Tell parent so. */
1279 mib = ifv->ifv_mib; 1277 mib = ifv->ifv_mib;
1280 error = if_mcast_op(mib->ifvm_p, SIOCDELMULTI, sa); 1278 error = if_mcast_op(mib->ifvm_p, SIOCDELMULTI, sa);
1281 1279
1282 if (error == 0) { 1280 if (error == 0) {
1283 /* And forget about this address. */ 1281 /* And forget about this address. */
1284 LIST_REMOVE(mc, mc_entries); 1282 LIST_REMOVE(mc, mc_entries);
1285 free(mc, M_DEVBUF); 1283 free(mc, M_DEVBUF);
1286 } else { 1284 } else {
1287 (void)ether_addmulti(sa, &ifv->ifv_ec); 1285 (void)ether_addmulti(sa, &ifv->ifv_ec);
1288 } 1286 }
1289 1287
1290 return error; 1288 return error;
1291} 1289}
1292 1290
1293/* 1291/*
1294 * Delete any multicast address we have asked to add from parent 1292 * Delete any multicast address we have asked to add from parent
1295 * interface. Called when the vlan is being unconfigured. 1293 * interface. Called when the vlan is being unconfigured.
1296 */ 1294 */
1297static void 1295static void
1298vlan_ether_purgemulti(struct ifvlan *ifv) 1296vlan_ether_purgemulti(struct ifvlan *ifv)
1299{ 1297{
1300 struct vlan_mc_entry *mc; 1298 struct vlan_mc_entry *mc;
1301 struct ifvlan_linkmib *mib; 1299 struct ifvlan_linkmib *mib;
1302 1300
1303 KASSERT(mutex_owned(&ifv->ifv_lock)); 1301 KASSERT(mutex_owned(&ifv->ifv_lock));
1304 mib = ifv->ifv_mib; 1302 mib = ifv->ifv_mib;
1305 if (mib == NULL) { 1303 if (mib == NULL) {
1306 return; 1304 return;
1307 } 1305 }
1308 1306
1309 while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) { 1307 while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
1310 (void)if_mcast_op(mib->ifvm_p, SIOCDELMULTI, 1308 (void)if_mcast_op(mib->ifvm_p, SIOCDELMULTI,
1311 sstocsa(&mc->mc_addr)); 1309 sstocsa(&mc->mc_addr));
1312 LIST_REMOVE(mc, mc_entries); 1310 LIST_REMOVE(mc, mc_entries);
1313 free(mc, M_DEVBUF); 1311 free(mc, M_DEVBUF);
1314 } 1312 }
1315} 1313}
1316 1314
1317static void 1315static void
1318vlan_start(struct ifnet *ifp) 1316vlan_start(struct ifnet *ifp)
1319{ 1317{
1320 struct ifvlan *ifv = ifp->if_softc; 1318 struct ifvlan *ifv = ifp->if_softc;
1321 struct ifnet *p; 1319 struct ifnet *p;
1322 struct ethercom *ec; 1320 struct ethercom *ec;
1323 struct mbuf *m; 1321 struct mbuf *m;
1324 struct ifvlan_linkmib *mib; 1322 struct ifvlan_linkmib *mib;
1325 struct psref psref; 1323 struct psref psref;
1326 int error; 1324 int error;
1327 1325
1328 mib = vlan_getref_linkmib(ifv, &psref); 1326 mib = vlan_getref_linkmib(ifv, &psref);
1329 if (mib == NULL) 1327 if (mib == NULL)
1330 return; 1328 return;
1331 1329
1332 if (__predict_false(mib->ifvm_p == NULL)) { 1330 if (__predict_false(mib->ifvm_p == NULL)) {
1333 vlan_putref_linkmib(mib, &psref); 1331 vlan_putref_linkmib(mib, &psref);
1334 return; 1332 return;
1335 } 1333 }
1336 1334
1337 p = mib->ifvm_p; 1335 p = mib->ifvm_p;
1338 ec = (void *)mib->ifvm_p; 1336 ec = (void *)mib->ifvm_p;
1339 1337
1340 ifp->if_flags |= IFF_OACTIVE; 1338 ifp->if_flags |= IFF_OACTIVE;
1341 1339
1342 for (;;) { 1340 for (;;) {
1343 IFQ_DEQUEUE(&ifp->if_snd, m); 1341 IFQ_DEQUEUE(&ifp->if_snd, m);
1344 if (m == NULL) 1342 if (m == NULL)
1345 break; 1343 break;
1346 1344
1347#ifdef ALTQ 1345#ifdef ALTQ
1348 /* 1346 /*
1349 * KERNEL_LOCK is required for ALTQ even if NET_MPSAFE is 1347 * KERNEL_LOCK is required for ALTQ even if NET_MPSAFE is
1350 * defined. 1348 * defined.
1351 */ 1349 */
1352 KERNEL_LOCK(1, NULL); 1350 KERNEL_LOCK(1, NULL);
1353 /* 1351 /*
1354 * If ALTQ is enabled on the parent interface, do 1352 * If ALTQ is enabled on the parent interface, do
1355 * classification; the queueing discipline might 1353 * classification; the queueing discipline might
1356 * not require classification, but might require 1354 * not require classification, but might require
1357 * the address family/header pointer in the pktattr. 1355 * the address family/header pointer in the pktattr.
1358 */ 1356 */
1359 if (ALTQ_IS_ENABLED(&p->if_snd)) { 1357 if (ALTQ_IS_ENABLED(&p->if_snd)) {
1360 switch (p->if_type) { 1358 switch (p->if_type) {
1361 case IFT_ETHER: 1359 case IFT_ETHER:
1362 altq_etherclassify(&p->if_snd, m); 1360 altq_etherclassify(&p->if_snd, m);
1363 break; 1361 break;
1364 default: 1362 default:
1365 panic("%s: impossible (altq)", __func__); 1363 panic("%s: impossible (altq)", __func__);
1366 } 1364 }
1367 } 1365 }
1368 KERNEL_UNLOCK_ONE(NULL); 1366 KERNEL_UNLOCK_ONE(NULL);
1369#endif /* ALTQ */ 1367#endif /* ALTQ */
1370 1368
1371 bpf_mtap(ifp, m, BPF_D_OUT); 1369 bpf_mtap(ifp, m, BPF_D_OUT);
1372 /* 1370 /*
1373 * If the parent can insert the tag itself, just mark 1371 * If the parent can insert the tag itself, just mark
1374 * the tag in the mbuf header. 1372 * the tag in the mbuf header.
1375 */ 1373 */
1376 if (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) { 1374 if (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) {
1377 vlan_set_tag(m, mib->ifvm_tag); 1375 vlan_set_tag(m, mib->ifvm_tag);
1378 } else { 1376 } else {
1379 /* 1377 /*
1380 * insert the tag ourselves 1378 * insert the tag ourselves
1381 */ 1379 */
1382 M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT); 1380 M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
1383 if (m == NULL) { 1381 if (m == NULL) {
1384 printf("%s: unable to prepend encap header", 1382 printf("%s: unable to prepend encap header",
1385 p->if_xname); 1383 p->if_xname);
1386 if_statinc(ifp, if_oerrors); 1384 if_statinc(ifp, if_oerrors);
1387 continue; 1385 continue;
1388 } 1386 }
1389 1387
1390 switch (p->if_type) { 1388 switch (p->if_type) {
1391 case IFT_ETHER: 1389 case IFT_ETHER:
1392 { 1390 {
1393 struct ether_vlan_header *evl; 1391 struct ether_vlan_header *evl;
1394 1392
1395 if (m->m_len < sizeof(struct ether_vlan_header)) 1393 if (m->m_len < sizeof(struct ether_vlan_header))
1396 m = m_pullup(m, 1394 m = m_pullup(m,
1397 sizeof(struct ether_vlan_header)); 1395 sizeof(struct ether_vlan_header));
1398 if (m == NULL) { 1396 if (m == NULL) {
1399 printf("%s: unable to pullup encap " 1397 printf("%s: unable to pullup encap "
1400 "header", p->if_xname); 1398 "header", p->if_xname);
1401 if_statinc(ifp, if_oerrors); 1399 if_statinc(ifp, if_oerrors);
1402 continue; 1400 continue;
1403 } 1401 }
1404 1402
1405 /* 1403 /*
1406 * Transform the Ethernet header into an 1404 * Transform the Ethernet header into an
1407 * Ethernet header with 802.1Q encapsulation. 1405 * Ethernet header with 802.1Q encapsulation.
1408 */ 1406 */
1409 memmove(mtod(m, void *), 1407 memmove(mtod(m, void *),
1410 mtod(m, char *) + mib->ifvm_encaplen, 1408 mtod(m, char *) + mib->ifvm_encaplen,
1411 sizeof(struct ether_header)); 1409 sizeof(struct ether_header));
1412 evl = mtod(m, struct ether_vlan_header *); 1410 evl = mtod(m, struct ether_vlan_header *);
1413 evl->evl_proto = evl->evl_encap_proto; 1411 evl->evl_proto = evl->evl_encap_proto;
1414 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 1412 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1415 evl->evl_tag = htons(mib->ifvm_tag); 1413 evl->evl_tag = htons(mib->ifvm_tag);
1416 1414
1417 /* 1415 /*
1418 * To cater for VLAN-aware layer 2 ethernet 1416 * To cater for VLAN-aware layer 2 ethernet
1419 * switches which may need to strip the tag 1417 * switches which may need to strip the tag
1420 * before forwarding the packet, make sure 1418 * before forwarding the packet, make sure
1421 * the packet+tag is at least 68 bytes long. 1419 * the packet+tag is at least 68 bytes long.
1422 * This is necessary because our parent will 1420 * This is necessary because our parent will
1423 * only pad to 64 bytes (ETHER_MIN_LEN) and 1421 * only pad to 64 bytes (ETHER_MIN_LEN) and
1424 * some switches will not pad by themselves 1422 * some switches will not pad by themselves
1425 * after deleting a tag. 1423 * after deleting a tag.
1426 */ 1424 */
1427 const size_t min_data_len = ETHER_MIN_LEN - 1425 const size_t min_data_len = ETHER_MIN_LEN -
1428 ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN; 1426 ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
1429 if (m->m_pkthdr.len < min_data_len) { 1427 if (m->m_pkthdr.len < min_data_len) {
1430 m_copyback(m, m->m_pkthdr.len, 1428 m_copyback(m, m->m_pkthdr.len,
1431 min_data_len - m->m_pkthdr.len, 1429 min_data_len - m->m_pkthdr.len,
1432 vlan_zero_pad_buff); 1430 vlan_zero_pad_buff);
1433 } 1431 }
1434 break; 1432 break;
1435 } 1433 }
1436 1434
1437 default: 1435 default:
1438 panic("%s: impossible", __func__); 1436 panic("%s: impossible", __func__);
1439 } 1437 }
1440 } 1438 }
1441 1439
1442 if ((p->if_flags & IFF_RUNNING) == 0) { 1440 if ((p->if_flags & IFF_RUNNING) == 0) {
1443 m_freem(m); 1441 m_freem(m);
1444 continue; 1442 continue;
1445 } 1443 }
1446 1444
1447 error = if_transmit_lock(p, m); 1445 error = if_transmit_lock(p, m);
1448 if (error) { 1446 if (error) {
1449 /* mbuf is already freed */ 1447 /* mbuf is already freed */
1450 if_statinc(ifp, if_oerrors); 1448 if_statinc(ifp, if_oerrors);
1451 continue; 1449 continue;
1452 } 1450 }
1453 if_statinc(ifp, if_opackets); 1451 if_statinc(ifp, if_opackets);
1454 } 1452 }
1455 1453
1456 ifp->if_flags &= ~IFF_OACTIVE; 1454 ifp->if_flags &= ~IFF_OACTIVE;
1457 1455
1458 /* Remove reference to mib before release */ 1456 /* Remove reference to mib before release */
1459 vlan_putref_linkmib(mib, &psref); 1457 vlan_putref_linkmib(mib, &psref);
1460} 1458}
1461 1459
1462static int 1460static int
1463vlan_transmit(struct ifnet *ifp, struct mbuf *m) 1461vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1464{ 1462{
1465 struct ifvlan *ifv = ifp->if_softc; 1463 struct ifvlan *ifv = ifp->if_softc;
1466 struct ifnet *p; 1464 struct ifnet *p;
1467 struct ethercom *ec; 1465 struct ethercom *ec;
1468 struct ifvlan_linkmib *mib; 1466 struct ifvlan_linkmib *mib;
1469 struct psref psref; 1467 struct psref psref;
1470 int error; 1468 int error;
1471 size_t pktlen = m->m_pkthdr.len; 1469 size_t pktlen = m->m_pkthdr.len;
1472 bool mcast = (m->m_flags & M_MCAST) != 0; 1470 bool mcast = (m->m_flags & M_MCAST) != 0;
1473 1471
1474 mib = vlan_getref_linkmib(ifv, &psref); 1472 mib = vlan_getref_linkmib(ifv, &psref);
1475 if (mib == NULL) { 1473 if (mib == NULL) {
1476 m_freem(m); 1474 m_freem(m);
1477 return ENETDOWN; 1475 return ENETDOWN;
1478 } 1476 }
1479 1477
1480 if (__predict_false(mib->ifvm_p == NULL)) { 1478 if (__predict_false(mib->ifvm_p == NULL)) {
1481 vlan_putref_linkmib(mib, &psref); 1479 vlan_putref_linkmib(mib, &psref);
1482 m_freem(m); 1480 m_freem(m);
1483 return ENETDOWN; 1481 return ENETDOWN;
1484 } 1482 }
1485 1483
1486 p = mib->ifvm_p; 1484 p = mib->ifvm_p;
1487 ec = (void *)mib->ifvm_p; 1485 ec = (void *)mib->ifvm_p;
1488 1486
1489 bpf_mtap(ifp, m, BPF_D_OUT); 1487 bpf_mtap(ifp, m, BPF_D_OUT);
1490 1488
1491 if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0) 1489 if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
1492 goto out; 1490 goto out;
1493 if (m == NULL) 1491 if (m == NULL)
1494 goto out; 1492 goto out;
1495 1493
1496 /* 1494 /*
1497 * If the parent can insert the tag itself, just mark 1495 * If the parent can insert the tag itself, just mark
1498 * the tag in the mbuf header. 1496 * the tag in the mbuf header.
1499 */ 1497 */
1500 if (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) { 1498 if (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) {
1501 vlan_set_tag(m, mib->ifvm_tag); 1499 vlan_set_tag(m, mib->ifvm_tag);
1502 } else { 1500 } else {
1503 /* 1501 /*
1504 * insert the tag ourselves 1502 * insert the tag ourselves
1505 */ 1503 */
1506 M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT); 1504 M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
1507 if (m == NULL) { 1505 if (m == NULL) {
1508 printf("%s: unable to prepend encap header", 1506 printf("%s: unable to prepend encap header",
1509 p->if_xname); 1507 p->if_xname);
1510 if_statinc(ifp, if_oerrors); 1508 if_statinc(ifp, if_oerrors);
1511 error = ENOBUFS; 1509 error = ENOBUFS;
1512 goto out; 1510 goto out;
1513 } 1511 }
1514 1512
1515 switch (p->if_type) { 1513 switch (p->if_type) {
1516 case IFT_ETHER: 1514 case IFT_ETHER:
1517 { 1515 {
1518 struct ether_vlan_header *evl; 1516 struct ether_vlan_header *evl;
1519 1517
1520 if (m->m_len < sizeof(struct ether_vlan_header)) 1518 if (m->m_len < sizeof(struct ether_vlan_header))
1521 m = m_pullup(m, 1519 m = m_pullup(m,
1522 sizeof(struct ether_vlan_header)); 1520 sizeof(struct ether_vlan_header));
1523 if (m == NULL) { 1521 if (m == NULL) {
1524 printf("%s: unable to pullup encap " 1522 printf("%s: unable to pullup encap "
1525 "header", p->if_xname); 1523 "header", p->if_xname);
1526 if_statinc(ifp, if_oerrors); 1524 if_statinc(ifp, if_oerrors);
1527 error = ENOBUFS; 1525 error = ENOBUFS;
1528 goto out; 1526 goto out;
1529 } 1527 }
1530 1528
1531 /* 1529 /*
1532 * Transform the Ethernet header into an 1530 * Transform the Ethernet header into an
1533 * Ethernet header with 802.1Q encapsulation. 1531 * Ethernet header with 802.1Q encapsulation.
1534 */ 1532 */
1535 memmove(mtod(m, void *), 1533 memmove(mtod(m, void *),
1536 mtod(m, char *) + mib->ifvm_encaplen, 1534 mtod(m, char *) + mib->ifvm_encaplen,
1537 sizeof(struct ether_header)); 1535 sizeof(struct ether_header));
1538 evl = mtod(m, struct ether_vlan_header *); 1536 evl = mtod(m, struct ether_vlan_header *);
1539 evl->evl_proto = evl->evl_encap_proto; 1537 evl->evl_proto = evl->evl_encap_proto;
1540 evl->evl_encap_proto = htons(ETHERTYPE_VLAN); 1538 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1541 evl->evl_tag = htons(mib->ifvm_tag); 1539 evl->evl_tag = htons(mib->ifvm_tag);
1542 1540
1543 /* 1541 /*
1544 * To cater for VLAN-aware layer 2 ethernet 1542 * To cater for VLAN-aware layer 2 ethernet
1545 * switches which may need to strip the tag 1543 * switches which may need to strip the tag
1546 * before forwarding the packet, make sure 1544 * before forwarding the packet, make sure
1547 * the packet+tag is at least 68 bytes long. 1545 * the packet+tag is at least 68 bytes long.
1548 * This is necessary because our parent will 1546 * This is necessary because our parent will
1549 * only pad to 64 bytes (ETHER_MIN_LEN) and 1547 * only pad to 64 bytes (ETHER_MIN_LEN) and
1550 * some switches will not pad by themselves 1548 * some switches will not pad by themselves
1551 * after deleting a tag. 1549 * after deleting a tag.
1552 */ 1550 */
1553 const size_t min_data_len = ETHER_MIN_LEN - 1551 const size_t min_data_len = ETHER_MIN_LEN -
1554 ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN; 1552 ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
1555 if (m->m_pkthdr.len < min_data_len) { 1553 if (m->m_pkthdr.len < min_data_len) {
1556 m_copyback(m, m->m_pkthdr.len, 1554 m_copyback(m, m->m_pkthdr.len,
1557 min_data_len - m->m_pkthdr.len, 1555 min_data_len - m->m_pkthdr.len,
1558 vlan_zero_pad_buff); 1556 vlan_zero_pad_buff);
1559 } 1557 }
1560 break; 1558 break;
1561 } 1559 }
1562 1560
1563 default: 1561 default:
1564 panic("%s: impossible", __func__); 1562 panic("%s: impossible", __func__);
1565 } 1563 }
1566 } 1564 }
1567 1565
1568 if ((p->if_flags & IFF_RUNNING) == 0) { 1566 if ((p->if_flags & IFF_RUNNING) == 0) {
1569 m_freem(m); 1567 m_freem(m);
1570 error = ENETDOWN; 1568 error = ENETDOWN;
1571 goto out; 1569 goto out;
1572 } 1570 }
1573 1571
1574 error = if_transmit_lock(p, m); 1572 error = if_transmit_lock(p, m);
1575 net_stat_ref_t nsr = IF_STAT_GETREF(ifp); 1573 net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
1576 if (error) { 1574 if (error) {
1577 /* mbuf is already freed */ 1575 /* mbuf is already freed */
1578 if_statinc_ref(nsr, if_oerrors); 1576 if_statinc_ref(nsr, if_oerrors);
1579 } else { 1577 } else {
1580 if_statinc_ref(nsr, if_opackets); 1578 if_statinc_ref(nsr, if_opackets);
1581 if_statadd_ref(nsr, if_obytes, pktlen); 1579 if_statadd_ref(nsr, if_obytes, pktlen);
1582 if (mcast) 1580 if (mcast)
1583 if_statinc_ref(nsr, if_omcasts); 1581 if_statinc_ref(nsr, if_omcasts);
1584 } 1582 }
1585 IF_STAT_PUTREF(ifp); 1583 IF_STAT_PUTREF(ifp);
1586 1584
1587out: 1585out:
1588 /* Remove reference to mib before release */ 1586 /* Remove reference to mib before release */
1589 vlan_putref_linkmib(mib, &psref); 1587 vlan_putref_linkmib(mib, &psref);
1590 return error; 1588 return error;
1591} 1589}
1592 1590
1593/* 1591/*
1594 * Given an Ethernet frame, find a valid vlan interface corresponding to the 1592 * Given an Ethernet frame, find a valid vlan interface corresponding to the
1595 * given source interface and tag, then run the real packet through the 1593 * given source interface and tag, then run the real packet through the
1596 * parent's input routine. 1594 * parent's input routine.
1597 */ 1595 */
1598void 1596void
1599vlan_input(struct ifnet *ifp, struct mbuf *m) 1597vlan_input(struct ifnet *ifp, struct mbuf *m)
1600{ 1598{
1601 struct ifvlan *ifv; 1599 struct ifvlan *ifv;
1602 uint16_t vid; 1600 uint16_t vid;
1603 struct ifvlan_linkmib *mib; 1601 struct ifvlan_linkmib *mib;
1604 struct psref psref; 1602 struct psref psref;
1605 bool have_vtag; 1603 bool have_vtag;
1606 1604
1607 have_vtag = vlan_has_tag(m); 1605 have_vtag = vlan_has_tag(m);
1608 if (have_vtag) { 1606 if (have_vtag) {
1609 vid = EVL_VLANOFTAG(vlan_get_tag(m)); 1607 vid = EVL_VLANOFTAG(vlan_get_tag(m));
1610 m->m_flags &= ~M_VLANTAG; 1608 m->m_flags &= ~M_VLANTAG;
1611 } else { 1609 } else {
1612 struct ether_vlan_header *evl; 1610 struct ether_vlan_header *evl;
1613 1611
1614 if (ifp->if_type != IFT_ETHER) { 1612 if (ifp->if_type != IFT_ETHER) {
1615 panic("%s: impossible", __func__); 1613 panic("%s: impossible", __func__);
1616 } 1614 }
1617 1615
1618 if (m->m_len < sizeof(struct ether_vlan_header) && 1616 if (m->m_len < sizeof(struct ether_vlan_header) &&
1619 (m = m_pullup(m, 1617 (m = m_pullup(m,
1620 sizeof(struct ether_vlan_header))) == NULL) { 1618 sizeof(struct ether_vlan_header))) == NULL) {
1621 printf("%s: no memory for VLAN header, " 1619 printf("%s: no memory for VLAN header, "
1622 "dropping packet.\n", ifp->if_xname); 1620 "dropping packet.\n", ifp->if_xname);
1623 return; 1621 return;
1624 } 1622 }
1625 1623
1626 if (m_makewritable(&m, 0, 1624 if (m_makewritable(&m, 0,
1627 sizeof(struct ether_vlan_header), M_DONTWAIT)) { 1625 sizeof(struct ether_vlan_header), M_DONTWAIT)) {
1628 m_freem(m); 1626 m_freem(m);
1629 if_statinc(ifp, if_ierrors); 1627 if_statinc(ifp, if_ierrors);
1630 return; 1628 return;
1631 } 1629 }
1632 1630
1633 evl = mtod(m, struct ether_vlan_header *); 1631 evl = mtod(m, struct ether_vlan_header *);
1634 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN); 1632 KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
1635 1633
1636 vid = EVL_VLANOFTAG(ntohs(evl->evl_tag)); 1634 vid = EVL_VLANOFTAG(ntohs(evl->evl_tag));
1637 1635
1638 /* 1636 /*
1639 * Restore the original ethertype. We'll remove 1637 * Restore the original ethertype. We'll remove
1640 * the encapsulation after we've found the vlan 1638 * the encapsulation after we've found the vlan
1641 * interface corresponding to the tag. 1639 * interface corresponding to the tag.
1642 */ 1640 */
1643 evl->evl_encap_proto = evl->evl_proto; 1641 evl->evl_encap_proto = evl->evl_proto;
1644 } 1642 }
1645 1643
1646 mib = vlan_lookup_tag_psref(ifp, vid, &psref); 1644 mib = vlan_lookup_tag_psref(ifp, vid, &psref);
1647 if (mib == NULL) { 1645 if (mib == NULL) {
1648 m_freem(m); 1646 m_freem(m);
1649 if_statinc(ifp, if_noproto); 1647 if_statinc(ifp, if_noproto);
1650 return; 1648 return;
1651 } 1649 }
1652 KASSERT(mib->ifvm_encaplen == ETHER_VLAN_ENCAP_LEN); 1650 KASSERT(mib->ifvm_encaplen == ETHER_VLAN_ENCAP_LEN);
1653 1651
1654 ifv = mib->ifvm_ifvlan; 1652 ifv = mib->ifvm_ifvlan;
1655 if ((ifv->ifv_if.if_flags & (IFF_UP | IFF_RUNNING)) != 1653 if ((ifv->ifv_if.if_flags & (IFF_UP | IFF_RUNNING)) !=
1656 (IFF_UP | IFF_RUNNING)) { 1654 (IFF_UP | IFF_RUNNING)) {
1657 m_freem(m); 1655 m_freem(m);
1658 if_statinc(ifp, if_noproto); 1656 if_statinc(ifp, if_noproto);
1659 goto out; 1657 goto out;
1660 } 1658 }
1661 1659
1662 /* 1660 /*
1663 * Now, remove the encapsulation header. The original 1661 * Now, remove the encapsulation header. The original
1664 * header has already been fixed up above. 1662 * header has already been fixed up above.
1665 */ 1663 */
1666 if (!have_vtag) { 1664 if (!have_vtag) {
1667 memmove(mtod(m, char *) + mib->ifvm_encaplen, 1665 memmove(mtod(m, char *) + mib->ifvm_encaplen,
1668 mtod(m, void *), sizeof(struct ether_header)); 1666 mtod(m, void *), sizeof(struct ether_header));
1669 m_adj(m, mib->ifvm_encaplen); 1667 m_adj(m, mib->ifvm_encaplen);
1670 } 1668 }
1671 1669
1672 /* 1670 /*
1673 * Drop promiscuously received packets if we are not in 1671 * Drop promiscuously received packets if we are not in
1674 * promiscuous mode 1672 * promiscuous mode
1675 */ 1673 */
1676 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0 && 1674 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0 &&
1677 (ifp->if_flags & IFF_PROMISC) && 1675 (ifp->if_flags & IFF_PROMISC) &&
1678 (ifv->ifv_if.if_flags & IFF_PROMISC) == 0) { 1676 (ifv->ifv_if.if_flags & IFF_PROMISC) == 0) {
1679 struct ether_header *eh; 1677 struct ether_header *eh;
1680 1678
1681 eh = mtod(m, struct ether_header *); 1679 eh = mtod(m, struct ether_header *);
1682 if (memcmp(CLLADDR(ifv->ifv_if.if_sadl), 1680 if (memcmp(CLLADDR(ifv->ifv_if.if_sadl),
1683 eh->ether_dhost, ETHER_ADDR_LEN) != 0) { 1681 eh->ether_dhost, ETHER_ADDR_LEN) != 0) {
1684 m_freem(m); 1682 m_freem(m);
1685 if_statinc(&ifv->ifv_if, if_ierrors); 1683 if_statinc(&ifv->ifv_if, if_ierrors);
1686 goto out; 1684 goto out;
1687 } 1685 }
1688 } 1686 }
1689 1687
1690 m_set_rcvif(m, &ifv->ifv_if); 1688 m_set_rcvif(m, &ifv->ifv_if);
1691 1689
1692 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0) 1690 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
1693 goto out; 1691 goto out;
1694 if (m == NULL) 1692 if (m == NULL)
1695 goto out; 1693 goto out;
1696 1694
1697 m->m_flags &= ~M_PROMISC; 1695 m->m_flags &= ~M_PROMISC;
1698 if_input(&ifv->ifv_if, m); 1696 if_input(&ifv->ifv_if, m);
1699out: 1697out:
1700 vlan_putref_linkmib(mib, &psref); 1698 vlan_putref_linkmib(mib, &psref);
1701} 1699}
1702 1700
1703/* 1701/*
1704 * If the parent link state changed, the vlan link state should change also. 1702 * If the parent link state changed, the vlan link state should change also.