xref: /freebsd/sys/net/if_vlan.c (revision c07d6445eb89d9dd3950361b065b7bd110e3a043)
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  * Copyright 2012 ADARA Networks, Inc.
4  * Copyright 2017 Dell EMC Isilon
5  *
6  * Portions of this software were developed by Robert N. M. Watson under
7  * contract to ADARA Networks, Inc.
8  *
9  * Permission to use, copy, modify, and distribute this software and
10  * its documentation for any purpose and without fee is hereby
11  * granted, provided that both the above copyright notice and this
12  * permission notice appear in all copies, that both the above
13  * copyright notice and this permission notice appear in all
14  * supporting documentation, and that the name of M.I.T. not be used
15  * in advertising or publicity pertaining to distribution of the
16  * software without specific, written prior permission.  M.I.T. makes
17  * no representations about the suitability of this software for any
18  * purpose.  It is provided "as is" without express or implied
19  * warranty.
20  *
21  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
22  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
23  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
25  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
37  * This is sort of sneaky in the implementation, since
38  * we need to pretend to be enough of an Ethernet implementation
39  * to make arp work.  The way we do this is by telling everyone
40  * that we are an Ethernet, and then catch the packets that
41  * ether_output() sends to us via if_transmit(), rewrite them for
42  * use by the real outgoing interface, and ask it to send them.
43  */
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include "opt_inet.h"
49 #include "opt_inet6.h"
50 #include "opt_kern_tls.h"
51 #include "opt_vlan.h"
52 #include "opt_ratelimit.h"
53 
54 #include <sys/param.h>
55 #include <sys/eventhandler.h>
56 #include <sys/kernel.h>
57 #include <sys/lock.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/module.h>
61 #include <sys/rmlock.h>
62 #include <sys/priv.h>
63 #include <sys/queue.h>
64 #include <sys/socket.h>
65 #include <sys/sockio.h>
66 #include <sys/sysctl.h>
67 #include <sys/systm.h>
68 #include <sys/sx.h>
69 #include <sys/taskqueue.h>
70 
71 #include <net/bpf.h>
72 #include <net/ethernet.h>
73 #include <net/if.h>
74 #include <net/if_var.h>
75 #include <net/if_clone.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_vlan_var.h>
79 #include <net/route.h>
80 #include <net/vnet.h>
81 
82 #ifdef INET
83 #include <netinet/in.h>
84 #include <netinet/if_ether.h>
85 #endif
86 
87 #ifdef INET6
88 /*
89  * XXX: declare here to avoid to include many inet6 related files..
90  * should be more generalized?
91  */
92 extern void	nd6_setmtu(struct ifnet *);
93 #endif
94 
95 #define	VLAN_DEF_HWIDTH	4
96 #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
97 
98 #define	UP_AND_RUNNING(ifp) \
99     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
100 
101 CK_SLIST_HEAD(ifvlanhead, ifvlan);
102 
103 struct ifvlantrunk {
104 	struct	ifnet   *parent;	/* parent interface of this trunk */
105 	struct	mtx	lock;
106 #ifdef VLAN_ARRAY
107 #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
108 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
109 #else
110 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
111 	uint16_t	hmask;
112 	uint16_t	hwidth;
113 #endif
114 	int		refcnt;
115 };
116 
117 #if defined(KERN_TLS) || defined(RATELIMIT)
118 struct vlan_snd_tag {
119 	struct m_snd_tag com;
120 	struct m_snd_tag *tag;
121 };
122 
123 static inline struct vlan_snd_tag *
124 mst_to_vst(struct m_snd_tag *mst)
125 {
126 
127 	return (__containerof(mst, struct vlan_snd_tag, com));
128 }
129 #endif
130 
131 /*
132  * This macro provides a facility to iterate over every vlan on a trunk with
133  * the assumption that none will be added/removed during iteration.
134  */
135 #ifdef VLAN_ARRAY
136 #define VLAN_FOREACH(_ifv, _trunk) \
137 	size_t _i; \
138 	for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
139 		if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
140 #else /* VLAN_ARRAY */
141 #define VLAN_FOREACH(_ifv, _trunk) \
142 	struct ifvlan *_next; \
143 	size_t _i; \
144 	for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
145 		CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
146 #endif /* VLAN_ARRAY */
147 
148 /*
149  * This macro provides a facility to iterate over every vlan on a trunk while
150  * also modifying the number of vlans on the trunk. The iteration continues
151  * until some condition is met or there are no more vlans on the trunk.
152  */
153 #ifdef VLAN_ARRAY
154 /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
155 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
156 	size_t _i; \
157 	for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
158 		if (((_ifv) = (_trunk)->vlans[_i]))
159 #else /* VLAN_ARRAY */
160 /*
161  * The hash table case is more complicated. We allow for the hash table to be
162  * modified (i.e. vlans removed) while we are iterating over it. To allow for
163  * this we must restart the iteration every time we "touch" something during
164  * the iteration, since removal will resize the hash table and invalidate our
165  * current position. If acting on the touched element causes the trunk to be
166  * emptied, then iteration also stops.
167  */
168 #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
169 	size_t _i; \
170 	bool _touch = false; \
171 	for (_i = 0; \
172 	    !(_cond) && _i < (1 << (_trunk)->hwidth); \
173 	    _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
174 		if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
175 		    (_touch = true))
176 #endif /* VLAN_ARRAY */
177 
178 struct vlan_mc_entry {
179 	struct sockaddr_dl		mc_addr;
180 	CK_SLIST_ENTRY(vlan_mc_entry)	mc_entries;
181 	struct epoch_context		mc_epoch_ctx;
182 };
183 
184 struct ifvlan {
185 	struct	ifvlantrunk *ifv_trunk;
186 	struct	ifnet *ifv_ifp;
187 #define	TRUNK(ifv)	((ifv)->ifv_trunk)
188 #define	PARENT(ifv)	(TRUNK(ifv)->parent)
189 	void	*ifv_cookie;
190 	int	ifv_pflags;	/* special flags we have set on parent */
191 	int	ifv_capenable;
192 	int	ifv_encaplen;	/* encapsulation length */
193 	int	ifv_mtufudge;	/* MTU fudged by this much */
194 	int	ifv_mintu;	/* min transmission unit */
195 	struct  ether_8021q_tag ifv_qtag;
196 #define ifv_proto	ifv_qtag.proto
197 #define ifv_vid		ifv_qtag.vid
198 #define ifv_pcp		ifv_qtag.pcp
199 	struct task lladdr_task;
200 	CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
201 #ifndef VLAN_ARRAY
202 	CK_SLIST_ENTRY(ifvlan) ifv_list;
203 #endif
204 };
205 
206 /* Special flags we should propagate to parent. */
207 static struct {
208 	int flag;
209 	int (*func)(struct ifnet *, int);
210 } vlan_pflags[] = {
211 	{IFF_PROMISC, ifpromisc},
212 	{IFF_ALLMULTI, if_allmulti},
213 	{0, NULL}
214 };
215 
216 VNET_DECLARE(int, vlan_mtag_pcp);
217 #define	V_vlan_mtag_pcp	VNET(vlan_mtag_pcp)
218 
219 static const char vlanname[] = "vlan";
220 static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
221 
222 static eventhandler_tag ifdetach_tag;
223 static eventhandler_tag iflladdr_tag;
224 static eventhandler_tag ifevent_tag;
225 
226 /*
227  * if_vlan uses two module-level synchronizations primitives to allow concurrent
228  * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
229  * while they are being used for tx/rx. To accomplish this in a way that has
230  * acceptable performance and cooperation with other parts of the network stack
231  * there is a non-sleepable epoch(9) and an sx(9).
232  *
233  * The performance-sensitive paths that warrant using the epoch(9) are
234  * vlan_transmit and vlan_input. Both have to check for the vlan interface's
235  * existence using if_vlantrunk, and being in the network tx/rx paths the use
236  * of an epoch(9) gives a measureable improvement in performance.
237  *
238  * The reason for having an sx(9) is mostly because there are still areas that
239  * must be sleepable and also have safe concurrent access to a vlan interface.
240  * Since the sx(9) exists, it is used by default in most paths unless sleeping
241  * is not permitted, or if it is not clear whether sleeping is permitted.
242  *
243  */
244 #define _VLAN_SX_ID ifv_sx
245 
246 static struct sx _VLAN_SX_ID;
247 
248 #define VLAN_LOCKING_INIT() \
249 	sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE)
250 
251 #define VLAN_LOCKING_DESTROY() \
252 	sx_destroy(&_VLAN_SX_ID)
253 
254 #define	VLAN_SLOCK()			sx_slock(&_VLAN_SX_ID)
255 #define	VLAN_SUNLOCK()			sx_sunlock(&_VLAN_SX_ID)
256 #define	VLAN_XLOCK()			sx_xlock(&_VLAN_SX_ID)
257 #define	VLAN_XUNLOCK()			sx_xunlock(&_VLAN_SX_ID)
258 #define	VLAN_SLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
259 #define	VLAN_XLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
260 #define	VLAN_SXLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_LOCKED)
261 
262 /*
263  * We also have a per-trunk mutex that should be acquired when changing
264  * its state.
265  */
266 #define	TRUNK_LOCK_INIT(trunk)		mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
267 #define	TRUNK_LOCK_DESTROY(trunk)	mtx_destroy(&(trunk)->lock)
268 #define	TRUNK_WLOCK(trunk)		mtx_lock(&(trunk)->lock)
269 #define	TRUNK_WUNLOCK(trunk)		mtx_unlock(&(trunk)->lock)
270 #define	TRUNK_WLOCK_ASSERT(trunk)	mtx_assert(&(trunk)->lock, MA_OWNED);
271 
272 /*
273  * The VLAN_ARRAY substitutes the dynamic hash with a static array
274  * with 4096 entries. In theory this can give a boost in processing,
275  * however in practice it does not. Probably this is because the array
276  * is too big to fit into CPU cache.
277  */
278 #ifndef VLAN_ARRAY
279 static	void vlan_inithash(struct ifvlantrunk *trunk);
280 static	void vlan_freehash(struct ifvlantrunk *trunk);
281 static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
282 static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
283 static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
284 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
285 	uint16_t vid);
286 #endif
287 static	void trunk_destroy(struct ifvlantrunk *trunk);
288 
289 static	void vlan_init(void *foo);
290 static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
291 static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
292 #if defined(KERN_TLS) || defined(RATELIMIT)
293 static	int vlan_snd_tag_alloc(struct ifnet *,
294     union if_snd_tag_alloc_params *, struct m_snd_tag **);
295 static	int vlan_snd_tag_modify(struct m_snd_tag *,
296     union if_snd_tag_modify_params *);
297 static	int vlan_snd_tag_query(struct m_snd_tag *,
298     union if_snd_tag_query_params *);
299 static	void vlan_snd_tag_free(struct m_snd_tag *);
300 static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *);
301 static void vlan_ratelimit_query(struct ifnet *,
302     struct if_ratelimit_query_results *);
303 #endif
304 static	void vlan_qflush(struct ifnet *ifp);
305 static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
306     int (*func)(struct ifnet *, int));
307 static	int vlan_setflags(struct ifnet *ifp, int status);
308 static	int vlan_setmulti(struct ifnet *ifp);
309 static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
310 #ifdef ALTQ
311 static void vlan_altq_start(struct ifnet *ifp);
312 static	int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m);
313 #endif
314 static	int vlan_output(struct ifnet *ifp, struct mbuf *m,
315     const struct sockaddr *dst, struct route *ro);
316 static	void vlan_unconfig(struct ifnet *ifp);
317 static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
318 static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag,
319 	uint16_t proto);
320 static	void vlan_link_state(struct ifnet *ifp);
321 static	void vlan_capabilities(struct ifvlan *ifv);
322 static	void vlan_trunk_capabilities(struct ifnet *ifp);
323 
324 static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
325 static	int vlan_clone_match(struct if_clone *, const char *);
326 static	int vlan_clone_create(struct if_clone *, char *, size_t,
327     struct ifc_data *, struct ifnet **);
328 static	int vlan_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
329 
330 static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
331 static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
332 static  void vlan_ifevent(void *arg, struct ifnet *ifp, int event);
333 
334 static  void vlan_lladdr_fn(void *arg, int pending);
335 
336 static struct if_clone *vlan_cloner;
337 
338 #ifdef VIMAGE
339 VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
340 #define	V_vlan_cloner	VNET(vlan_cloner)
341 #endif
342 
343 #ifdef RATELIMIT
344 static const struct if_snd_tag_sw vlan_snd_tag_ul_sw = {
345 	.snd_tag_modify = vlan_snd_tag_modify,
346 	.snd_tag_query = vlan_snd_tag_query,
347 	.snd_tag_free = vlan_snd_tag_free,
348 	.next_snd_tag = vlan_next_snd_tag,
349 	.type = IF_SND_TAG_TYPE_UNLIMITED
350 };
351 
352 static const struct if_snd_tag_sw vlan_snd_tag_rl_sw = {
353 	.snd_tag_modify = vlan_snd_tag_modify,
354 	.snd_tag_query = vlan_snd_tag_query,
355 	.snd_tag_free = vlan_snd_tag_free,
356 	.next_snd_tag = vlan_next_snd_tag,
357 	.type = IF_SND_TAG_TYPE_RATE_LIMIT
358 };
359 #endif
360 
361 #ifdef KERN_TLS
362 static const struct if_snd_tag_sw vlan_snd_tag_tls_sw = {
363 	.snd_tag_modify = vlan_snd_tag_modify,
364 	.snd_tag_query = vlan_snd_tag_query,
365 	.snd_tag_free = vlan_snd_tag_free,
366 	.next_snd_tag = vlan_next_snd_tag,
367 	.type = IF_SND_TAG_TYPE_TLS
368 };
369 
370 #ifdef RATELIMIT
371 static const struct if_snd_tag_sw vlan_snd_tag_tls_rl_sw = {
372 	.snd_tag_modify = vlan_snd_tag_modify,
373 	.snd_tag_query = vlan_snd_tag_query,
374 	.snd_tag_free = vlan_snd_tag_free,
375 	.next_snd_tag = vlan_next_snd_tag,
376 	.type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT
377 };
378 #endif
379 #endif
380 
381 static void
382 vlan_mc_free(struct epoch_context *ctx)
383 {
384 	struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
385 	free(mc, M_VLAN);
386 }
387 
388 #ifndef VLAN_ARRAY
389 #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
390 
391 static void
392 vlan_inithash(struct ifvlantrunk *trunk)
393 {
394 	int i, n;
395 
396 	/*
397 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
398 	 * It is OK in case this function is called before the trunk struct
399 	 * gets hooked up and becomes visible from other threads.
400 	 */
401 
402 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
403 	    ("%s: hash already initialized", __func__));
404 
405 	trunk->hwidth = VLAN_DEF_HWIDTH;
406 	n = 1 << trunk->hwidth;
407 	trunk->hmask = n - 1;
408 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
409 	for (i = 0; i < n; i++)
410 		CK_SLIST_INIT(&trunk->hash[i]);
411 }
412 
413 static void
414 vlan_freehash(struct ifvlantrunk *trunk)
415 {
416 #ifdef INVARIANTS
417 	int i;
418 
419 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
420 	for (i = 0; i < (1 << trunk->hwidth); i++)
421 		KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
422 		    ("%s: hash table not empty", __func__));
423 #endif
424 	free(trunk->hash, M_VLAN);
425 	trunk->hash = NULL;
426 	trunk->hwidth = trunk->hmask = 0;
427 }
428 
429 static int
430 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
431 {
432 	int i, b;
433 	struct ifvlan *ifv2;
434 
435 	VLAN_XLOCK_ASSERT();
436 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
437 
438 	b = 1 << trunk->hwidth;
439 	i = HASH(ifv->ifv_vid, trunk->hmask);
440 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
441 		if (ifv->ifv_vid == ifv2->ifv_vid)
442 			return (EEXIST);
443 
444 	/*
445 	 * Grow the hash when the number of vlans exceeds half of the number of
446 	 * hash buckets squared. This will make the average linked-list length
447 	 * buckets/2.
448 	 */
449 	if (trunk->refcnt > (b * b) / 2) {
450 		vlan_growhash(trunk, 1);
451 		i = HASH(ifv->ifv_vid, trunk->hmask);
452 	}
453 	CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
454 	trunk->refcnt++;
455 
456 	return (0);
457 }
458 
459 static int
460 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
461 {
462 	int i, b;
463 	struct ifvlan *ifv2;
464 
465 	VLAN_XLOCK_ASSERT();
466 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
467 
468 	b = 1 << (trunk->hwidth - 1);
469 	i = HASH(ifv->ifv_vid, trunk->hmask);
470 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
471 		if (ifv2 == ifv) {
472 			trunk->refcnt--;
473 			CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
474 			if (trunk->refcnt < (b * b) / 2)
475 				vlan_growhash(trunk, -1);
476 			return (0);
477 		}
478 
479 	panic("%s: vlan not found\n", __func__);
480 	return (ENOENT); /*NOTREACHED*/
481 }
482 
483 /*
484  * Grow the hash larger or smaller if memory permits.
485  */
486 static void
487 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
488 {
489 	struct ifvlan *ifv;
490 	struct ifvlanhead *hash2;
491 	int hwidth2, i, j, n, n2;
492 
493 	VLAN_XLOCK_ASSERT();
494 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
495 
496 	if (howmuch == 0) {
497 		/* Harmless yet obvious coding error */
498 		printf("%s: howmuch is 0\n", __func__);
499 		return;
500 	}
501 
502 	hwidth2 = trunk->hwidth + howmuch;
503 	n = 1 << trunk->hwidth;
504 	n2 = 1 << hwidth2;
505 	/* Do not shrink the table below the default */
506 	if (hwidth2 < VLAN_DEF_HWIDTH)
507 		return;
508 
509 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
510 	if (hash2 == NULL) {
511 		printf("%s: out of memory -- hash size not changed\n",
512 		    __func__);
513 		return;		/* We can live with the old hash table */
514 	}
515 	for (j = 0; j < n2; j++)
516 		CK_SLIST_INIT(&hash2[j]);
517 	for (i = 0; i < n; i++)
518 		while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
519 			CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
520 			j = HASH(ifv->ifv_vid, n2 - 1);
521 			CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
522 		}
523 	NET_EPOCH_WAIT();
524 	free(trunk->hash, M_VLAN);
525 	trunk->hash = hash2;
526 	trunk->hwidth = hwidth2;
527 	trunk->hmask = n2 - 1;
528 
529 	if (bootverbose)
530 		if_printf(trunk->parent,
531 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
532 }
533 
534 static __inline struct ifvlan *
535 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
536 {
537 	struct ifvlan *ifv;
538 
539 	NET_EPOCH_ASSERT();
540 
541 	CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
542 		if (ifv->ifv_vid == vid)
543 			return (ifv);
544 	return (NULL);
545 }
546 
547 #if 0
548 /* Debugging code to view the hashtables. */
549 static void
550 vlan_dumphash(struct ifvlantrunk *trunk)
551 {
552 	int i;
553 	struct ifvlan *ifv;
554 
555 	for (i = 0; i < (1 << trunk->hwidth); i++) {
556 		printf("%d: ", i);
557 		CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
558 			printf("%s ", ifv->ifv_ifp->if_xname);
559 		printf("\n");
560 	}
561 }
562 #endif /* 0 */
563 #else
564 
565 static __inline struct ifvlan *
566 vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
567 {
568 
569 	return trunk->vlans[vid];
570 }
571 
572 static __inline int
573 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
574 {
575 
576 	if (trunk->vlans[ifv->ifv_vid] != NULL)
577 		return EEXIST;
578 	trunk->vlans[ifv->ifv_vid] = ifv;
579 	trunk->refcnt++;
580 
581 	return (0);
582 }
583 
584 static __inline int
585 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
586 {
587 
588 	trunk->vlans[ifv->ifv_vid] = NULL;
589 	trunk->refcnt--;
590 
591 	return (0);
592 }
593 
594 static __inline void
595 vlan_freehash(struct ifvlantrunk *trunk)
596 {
597 }
598 
599 static __inline void
600 vlan_inithash(struct ifvlantrunk *trunk)
601 {
602 }
603 
604 #endif /* !VLAN_ARRAY */
605 
606 static void
607 trunk_destroy(struct ifvlantrunk *trunk)
608 {
609 	VLAN_XLOCK_ASSERT();
610 
611 	vlan_freehash(trunk);
612 	trunk->parent->if_vlantrunk = NULL;
613 	TRUNK_LOCK_DESTROY(trunk);
614 	if_rele(trunk->parent);
615 	free(trunk, M_VLAN);
616 }
617 
618 /*
619  * Program our multicast filter. What we're actually doing is
620  * programming the multicast filter of the parent. This has the
621  * side effect of causing the parent interface to receive multicast
622  * traffic that it doesn't really want, which ends up being discarded
623  * later by the upper protocol layers. Unfortunately, there's no way
624  * to avoid this: there really is only one physical interface.
625  */
626 static int
627 vlan_setmulti(struct ifnet *ifp)
628 {
629 	struct ifnet		*ifp_p;
630 	struct ifmultiaddr	*ifma;
631 	struct ifvlan		*sc;
632 	struct vlan_mc_entry	*mc;
633 	int			error;
634 
635 	VLAN_XLOCK_ASSERT();
636 
637 	/* Find the parent. */
638 	sc = ifp->if_softc;
639 	ifp_p = PARENT(sc);
640 
641 	CURVNET_SET_QUIET(ifp_p->if_vnet);
642 
643 	/* First, remove any existing filter entries. */
644 	while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
645 		CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
646 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
647 		NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
648 	}
649 
650 	/* Now program new ones. */
651 	IF_ADDR_WLOCK(ifp);
652 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
653 		if (ifma->ifma_addr->sa_family != AF_LINK)
654 			continue;
655 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
656 		if (mc == NULL) {
657 			IF_ADDR_WUNLOCK(ifp);
658 			CURVNET_RESTORE();
659 			return (ENOMEM);
660 		}
661 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
662 		mc->mc_addr.sdl_index = ifp_p->if_index;
663 		CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
664 	}
665 	IF_ADDR_WUNLOCK(ifp);
666 	CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
667 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
668 		    NULL);
669 		if (error) {
670 			CURVNET_RESTORE();
671 			return (error);
672 		}
673 	}
674 
675 	CURVNET_RESTORE();
676 	return (0);
677 }
678 
679 /*
680  * A handler for interface ifnet events.
681  */
682 static void
683 vlan_ifevent(void *arg __unused, struct ifnet *ifp, int event)
684 {
685 	struct epoch_tracker et;
686 	struct ifvlan *ifv;
687 	struct ifvlantrunk *trunk;
688 
689 	if (event != IFNET_EVENT_UPDATE_BAUDRATE)
690 		return;
691 
692 	NET_EPOCH_ENTER(et);
693 	trunk = ifp->if_vlantrunk;
694 	if (trunk == NULL) {
695 		NET_EPOCH_EXIT(et);
696 		return;
697 	}
698 
699 	TRUNK_WLOCK(trunk);
700 	VLAN_FOREACH(ifv, trunk) {
701 		ifv->ifv_ifp->if_baudrate = ifp->if_baudrate;
702 	}
703 	TRUNK_WUNLOCK(trunk);
704 	NET_EPOCH_EXIT(et);
705 }
706 
707 /*
708  * A handler for parent interface link layer address changes.
709  * If the parent interface link layer address is changed we
710  * should also change it on all children vlans.
711  */
712 static void
713 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
714 {
715 	struct epoch_tracker et;
716 	struct ifvlan *ifv;
717 	struct ifnet *ifv_ifp;
718 	struct ifvlantrunk *trunk;
719 	struct sockaddr_dl *sdl;
720 
721 	/* Need the epoch since this is run on taskqueue_swi. */
722 	NET_EPOCH_ENTER(et);
723 	trunk = ifp->if_vlantrunk;
724 	if (trunk == NULL) {
725 		NET_EPOCH_EXIT(et);
726 		return;
727 	}
728 
729 	/*
730 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
731 	 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
732 	 * ioctl calls on the parent garbling the lladdr of the child vlan.
733 	 */
734 	TRUNK_WLOCK(trunk);
735 	VLAN_FOREACH(ifv, trunk) {
736 		/*
737 		 * Copy new new lladdr into the ifv_ifp, enqueue a task
738 		 * to actually call if_setlladdr. if_setlladdr needs to
739 		 * be deferred to a taskqueue because it will call into
740 		 * the if_vlan ioctl path and try to acquire the global
741 		 * lock.
742 		 */
743 		ifv_ifp = ifv->ifv_ifp;
744 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
745 		    ifp->if_addrlen);
746 		sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
747 		sdl->sdl_alen = ifp->if_addrlen;
748 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
749 	}
750 	TRUNK_WUNLOCK(trunk);
751 	NET_EPOCH_EXIT(et);
752 }
753 
754 /*
755  * A handler for network interface departure events.
756  * Track departure of trunks here so that we don't access invalid
757  * pointers or whatever if a trunk is ripped from under us, e.g.,
758  * by ejecting its hot-plug card.  However, if an ifnet is simply
759  * being renamed, then there's no need to tear down the state.
760  */
761 static void
762 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
763 {
764 	struct ifvlan *ifv;
765 	struct ifvlantrunk *trunk;
766 
767 	/* If the ifnet is just being renamed, don't do anything. */
768 	if (ifp->if_flags & IFF_RENAMING)
769 		return;
770 	VLAN_XLOCK();
771 	trunk = ifp->if_vlantrunk;
772 	if (trunk == NULL) {
773 		VLAN_XUNLOCK();
774 		return;
775 	}
776 
777 	/*
778 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
779 	 * Check trunk pointer after each vlan_unconfig() as it will
780 	 * free it and set to NULL after the last vlan was detached.
781 	 */
782 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
783 	    ifp->if_vlantrunk == NULL)
784 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
785 
786 	/* Trunk should have been destroyed in vlan_unconfig(). */
787 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
788 	VLAN_XUNLOCK();
789 }
790 
791 /*
792  * Return the trunk device for a virtual interface.
793  */
794 static struct ifnet  *
795 vlan_trunkdev(struct ifnet *ifp)
796 {
797 	struct ifvlan *ifv;
798 
799 	NET_EPOCH_ASSERT();
800 
801 	if (ifp->if_type != IFT_L2VLAN)
802 		return (NULL);
803 
804 	ifv = ifp->if_softc;
805 	ifp = NULL;
806 	if (ifv->ifv_trunk)
807 		ifp = PARENT(ifv);
808 	return (ifp);
809 }
810 
811 /*
812  * Return the 12-bit VLAN VID for this interface, for use by external
813  * components such as Infiniband.
814  *
815  * XXXRW: Note that the function name here is historical; it should be named
816  * vlan_vid().
817  */
818 static int
819 vlan_tag(struct ifnet *ifp, uint16_t *vidp)
820 {
821 	struct ifvlan *ifv;
822 
823 	if (ifp->if_type != IFT_L2VLAN)
824 		return (EINVAL);
825 	ifv = ifp->if_softc;
826 	*vidp = ifv->ifv_vid;
827 	return (0);
828 }
829 
830 static int
831 vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
832 {
833 	struct ifvlan *ifv;
834 
835 	if (ifp->if_type != IFT_L2VLAN)
836 		return (EINVAL);
837 	ifv = ifp->if_softc;
838 	*pcpp = ifv->ifv_pcp;
839 	return (0);
840 }
841 
842 /*
843  * Return a driver specific cookie for this interface.  Synchronization
844  * with setcookie must be provided by the driver.
845  */
846 static void *
847 vlan_cookie(struct ifnet *ifp)
848 {
849 	struct ifvlan *ifv;
850 
851 	if (ifp->if_type != IFT_L2VLAN)
852 		return (NULL);
853 	ifv = ifp->if_softc;
854 	return (ifv->ifv_cookie);
855 }
856 
857 /*
858  * Store a cookie in our softc that drivers can use to store driver
859  * private per-instance data in.
860  */
861 static int
862 vlan_setcookie(struct ifnet *ifp, void *cookie)
863 {
864 	struct ifvlan *ifv;
865 
866 	if (ifp->if_type != IFT_L2VLAN)
867 		return (EINVAL);
868 	ifv = ifp->if_softc;
869 	ifv->ifv_cookie = cookie;
870 	return (0);
871 }
872 
873 /*
874  * Return the vlan device present at the specific VID.
875  */
876 static struct ifnet *
877 vlan_devat(struct ifnet *ifp, uint16_t vid)
878 {
879 	struct ifvlantrunk *trunk;
880 	struct ifvlan *ifv;
881 
882 	NET_EPOCH_ASSERT();
883 
884 	trunk = ifp->if_vlantrunk;
885 	if (trunk == NULL)
886 		return (NULL);
887 	ifp = NULL;
888 	ifv = vlan_gethash(trunk, vid);
889 	if (ifv)
890 		ifp = ifv->ifv_ifp;
891 	return (ifp);
892 }
893 
894 /*
895  * VLAN support can be loaded as a module.  The only place in the
896  * system that's intimately aware of this is ether_input.  We hook
897  * into this code through vlan_input_p which is defined there and
898  * set here.  No one else in the system should be aware of this so
899  * we use an explicit reference here.
900  */
901 extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
902 
903 /* For if_link_state_change() eyes only... */
904 extern	void (*vlan_link_state_p)(struct ifnet *);
905 
906 static struct if_clone_addreq vlan_addreq = {
907 	.match_f = vlan_clone_match,
908 	.create_f = vlan_clone_create,
909 	.destroy_f = vlan_clone_destroy,
910 };
911 
912 static int
913 vlan_modevent(module_t mod, int type, void *data)
914 {
915 
916 	switch (type) {
917 	case MOD_LOAD:
918 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
919 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
920 		if (ifdetach_tag == NULL)
921 			return (ENOMEM);
922 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
923 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
924 		if (iflladdr_tag == NULL)
925 			return (ENOMEM);
926 		ifevent_tag = EVENTHANDLER_REGISTER(ifnet_event,
927 		    vlan_ifevent, NULL, EVENTHANDLER_PRI_ANY);
928 		if (ifevent_tag == NULL)
929 			return (ENOMEM);
930 		VLAN_LOCKING_INIT();
931 		vlan_input_p = vlan_input;
932 		vlan_link_state_p = vlan_link_state;
933 		vlan_trunk_cap_p = vlan_trunk_capabilities;
934 		vlan_trunkdev_p = vlan_trunkdev;
935 		vlan_cookie_p = vlan_cookie;
936 		vlan_setcookie_p = vlan_setcookie;
937 		vlan_tag_p = vlan_tag;
938 		vlan_pcp_p = vlan_pcp;
939 		vlan_devat_p = vlan_devat;
940 #ifndef VIMAGE
941 		vlan_cloner = ifc_attach_cloner(vlanname, &vlan_addreq);
942 #endif
943 		if (bootverbose)
944 			printf("vlan: initialized, using "
945 #ifdef VLAN_ARRAY
946 			       "full-size arrays"
947 #else
948 			       "hash tables with chaining"
949 #endif
950 
951 			       "\n");
952 		break;
953 	case MOD_UNLOAD:
954 #ifndef VIMAGE
955 		ifc_detach_cloner(vlan_cloner);
956 #endif
957 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
958 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
959 		EVENTHANDLER_DEREGISTER(ifnet_event, ifevent_tag);
960 		vlan_input_p = NULL;
961 		vlan_link_state_p = NULL;
962 		vlan_trunk_cap_p = NULL;
963 		vlan_trunkdev_p = NULL;
964 		vlan_tag_p = NULL;
965 		vlan_cookie_p = NULL;
966 		vlan_setcookie_p = NULL;
967 		vlan_devat_p = NULL;
968 		VLAN_LOCKING_DESTROY();
969 		if (bootverbose)
970 			printf("vlan: unloaded\n");
971 		break;
972 	default:
973 		return (EOPNOTSUPP);
974 	}
975 	return (0);
976 }
977 
978 static moduledata_t vlan_mod = {
979 	"if_vlan",
980 	vlan_modevent,
981 	0
982 };
983 
984 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
985 MODULE_VERSION(if_vlan, 3);
986 
987 #ifdef VIMAGE
988 static void
989 vnet_vlan_init(const void *unused __unused)
990 {
991 	vlan_cloner = ifc_attach_cloner(vlanname, &vlan_addreq);
992 	V_vlan_cloner = vlan_cloner;
993 }
994 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
995     vnet_vlan_init, NULL);
996 
997 static void
998 vnet_vlan_uninit(const void *unused __unused)
999 {
1000 
1001 	ifc_detach_cloner(V_vlan_cloner);
1002 }
1003 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
1004     vnet_vlan_uninit, NULL);
1005 #endif
1006 
1007 /*
1008  * Check for <etherif>.<vlan>[.<vlan> ...] style interface names.
1009  */
1010 static struct ifnet *
1011 vlan_clone_match_ethervid(const char *name, int *vidp)
1012 {
1013 	char ifname[IFNAMSIZ];
1014 	char *cp;
1015 	struct ifnet *ifp;
1016 	int vid;
1017 
1018 	strlcpy(ifname, name, IFNAMSIZ);
1019 	if ((cp = strrchr(ifname, '.')) == NULL)
1020 		return (NULL);
1021 	*cp = '\0';
1022 	if ((ifp = ifunit_ref(ifname)) == NULL)
1023 		return (NULL);
1024 	/* Parse VID. */
1025 	if (*++cp == '\0') {
1026 		if_rele(ifp);
1027 		return (NULL);
1028 	}
1029 	vid = 0;
1030 	for(; *cp >= '0' && *cp <= '9'; cp++)
1031 		vid = (vid * 10) + (*cp - '0');
1032 	if (*cp != '\0') {
1033 		if_rele(ifp);
1034 		return (NULL);
1035 	}
1036 	if (vidp != NULL)
1037 		*vidp = vid;
1038 
1039 	return (ifp);
1040 }
1041 
1042 static int
1043 vlan_clone_match(struct if_clone *ifc, const char *name)
1044 {
1045 	struct ifnet *ifp;
1046 	const char *cp;
1047 
1048 	ifp = vlan_clone_match_ethervid(name, NULL);
1049 	if (ifp != NULL) {
1050 		if_rele(ifp);
1051 		return (1);
1052 	}
1053 
1054 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
1055 		return (0);
1056 	for (cp = name + 4; *cp != '\0'; cp++) {
1057 		if (*cp < '0' || *cp > '9')
1058 			return (0);
1059 	}
1060 
1061 	return (1);
1062 }
1063 
1064 static int
1065 vlan_clone_create(struct if_clone *ifc, char *name, size_t len,
1066     struct ifc_data *ifd, struct ifnet **ifpp)
1067 {
1068 	char *dp;
1069 	bool wildcard = false;
1070 	bool subinterface = false;
1071 	int unit;
1072 	int error;
1073 	int vid = 0;
1074 	uint16_t proto = ETHERTYPE_VLAN;
1075 	struct ifvlan *ifv;
1076 	struct ifnet *ifp;
1077 	struct ifnet *p = NULL;
1078 	struct ifaddr *ifa;
1079 	struct sockaddr_dl *sdl;
1080 	struct vlanreq vlr;
1081 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
1082 
1083 
1084 	/*
1085 	 * There are three ways to specify the cloned device:
1086 	 * o pass a parameter block with the clone request.
1087 	 * o specify parameters in the text of the clone device name
1088 	 * o specify no parameters and get an unattached device that
1089 	 *   must be configured separately.
1090 	 * The first technique is preferred; the latter two are supported
1091 	 * for backwards compatibility.
1092 	 *
1093 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
1094 	 * called for.
1095 	 */
1096 
1097 	if (ifd->params != NULL) {
1098 		error = ifc_copyin(ifd, &vlr, sizeof(vlr));
1099 		if (error)
1100 			return error;
1101 		vid = vlr.vlr_tag;
1102 		proto = vlr.vlr_proto;
1103 
1104 #ifdef COMPAT_FREEBSD12
1105 		if (proto == 0)
1106 			proto = ETHERTYPE_VLAN;
1107 #endif
1108 		p = ifunit_ref(vlr.vlr_parent);
1109 		if (p == NULL)
1110 			return (ENXIO);
1111 	}
1112 
1113 	if ((error = ifc_name2unit(name, &unit)) == 0) {
1114 
1115 		/*
1116 		 * vlanX interface. Set wildcard to true if the unit number
1117 		 * is not fixed (-1)
1118 		 */
1119 		wildcard = (unit < 0);
1120 	} else {
1121 		struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid);
1122 		if (p_tmp != NULL) {
1123 			error = 0;
1124 			subinterface = true;
1125 			unit = IF_DUNIT_NONE;
1126 			wildcard = false;
1127 			if (p != NULL) {
1128 				if_rele(p_tmp);
1129 				if (p != p_tmp)
1130 					error = EINVAL;
1131 			} else
1132 				p = p_tmp;
1133 		} else
1134 			error = ENXIO;
1135 	}
1136 
1137 	if (error != 0) {
1138 		if (p != NULL)
1139 			if_rele(p);
1140 		return (error);
1141 	}
1142 
1143 	if (!subinterface) {
1144 		/* vlanX interface, mark X as busy or allocate new unit # */
1145 		error = ifc_alloc_unit(ifc, &unit);
1146 		if (error != 0) {
1147 			if (p != NULL)
1148 				if_rele(p);
1149 			return (error);
1150 		}
1151 	}
1152 
1153 	/* In the wildcard case, we need to update the name. */
1154 	if (wildcard) {
1155 		for (dp = name; *dp != '\0'; dp++);
1156 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1157 		    len - (dp-name) - 1) {
1158 			panic("%s: interface name too long", __func__);
1159 		}
1160 	}
1161 
1162 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1163 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1164 	if (ifp == NULL) {
1165 		if (!subinterface)
1166 			ifc_free_unit(ifc, unit);
1167 		free(ifv, M_VLAN);
1168 		if (p != NULL)
1169 			if_rele(p);
1170 		return (ENOSPC);
1171 	}
1172 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
1173 	ifp->if_softc = ifv;
1174 	/*
1175 	 * Set the name manually rather than using if_initname because
1176 	 * we don't conform to the default naming convention for interfaces.
1177 	 */
1178 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
1179 	ifp->if_dname = vlanname;
1180 	ifp->if_dunit = unit;
1181 
1182 	ifp->if_init = vlan_init;
1183 #ifdef ALTQ
1184 	ifp->if_start = vlan_altq_start;
1185 	ifp->if_transmit = vlan_altq_transmit;
1186 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1187 	ifp->if_snd.ifq_drv_maxlen = 0;
1188 	IFQ_SET_READY(&ifp->if_snd);
1189 #else
1190 	ifp->if_transmit = vlan_transmit;
1191 #endif
1192 	ifp->if_qflush = vlan_qflush;
1193 	ifp->if_ioctl = vlan_ioctl;
1194 #if defined(KERN_TLS) || defined(RATELIMIT)
1195 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1196 	ifp->if_ratelimit_query = vlan_ratelimit_query;
1197 #endif
1198 	ifp->if_flags = VLAN_IFFLAGS;
1199 	ether_ifattach(ifp, eaddr);
1200 	/* Now undo some of the damage... */
1201 	ifp->if_baudrate = 0;
1202 	ifp->if_type = IFT_L2VLAN;
1203 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
1204 	ifa = ifp->if_addr;
1205 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1206 	sdl->sdl_type = IFT_L2VLAN;
1207 
1208 	if (p != NULL) {
1209 		error = vlan_config(ifv, p, vid, proto);
1210 		if_rele(p);
1211 		if (error != 0) {
1212 			/*
1213 			 * Since we've partially failed, we need to back
1214 			 * out all the way, otherwise userland could get
1215 			 * confused.  Thus, we destroy the interface.
1216 			 */
1217 			ether_ifdetach(ifp);
1218 			vlan_unconfig(ifp);
1219 			if_free(ifp);
1220 			if (!subinterface)
1221 				ifc_free_unit(ifc, unit);
1222 			free(ifv, M_VLAN);
1223 
1224 			return (error);
1225 		}
1226 	}
1227 	*ifpp = ifp;
1228 
1229 	return (0);
1230 }
1231 
1232 static int
1233 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
1234 {
1235 	struct ifvlan *ifv = ifp->if_softc;
1236 	int unit = ifp->if_dunit;
1237 
1238 	if (ifp->if_vlantrunk)
1239 		return (EBUSY);
1240 
1241 #ifdef ALTQ
1242 	IFQ_PURGE(&ifp->if_snd);
1243 #endif
1244 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1245 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1246 	/*
1247 	 * We should have the only reference to the ifv now, so we can now
1248 	 * drain any remaining lladdr task before freeing the ifnet and the
1249 	 * ifvlan.
1250 	 */
1251 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1252 	NET_EPOCH_WAIT();
1253 	if_free(ifp);
1254 	free(ifv, M_VLAN);
1255 	if (unit != IF_DUNIT_NONE)
1256 		ifc_free_unit(ifc, unit);
1257 
1258 	return (0);
1259 }
1260 
1261 /*
1262  * The ifp->if_init entry point for vlan(4) is a no-op.
1263  */
1264 static void
1265 vlan_init(void *foo __unused)
1266 {
1267 }
1268 
1269 /*
1270  * The if_transmit method for vlan(4) interface.
1271  */
1272 static int
1273 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
1274 {
1275 	struct ifvlan *ifv;
1276 	struct ifnet *p;
1277 	int error, len, mcast;
1278 
1279 	NET_EPOCH_ASSERT();
1280 
1281 	ifv = ifp->if_softc;
1282 	if (TRUNK(ifv) == NULL) {
1283 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1284 		m_freem(m);
1285 		return (ENETDOWN);
1286 	}
1287 	p = PARENT(ifv);
1288 	len = m->m_pkthdr.len;
1289 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
1290 
1291 	BPF_MTAP(ifp, m);
1292 
1293 #if defined(KERN_TLS) || defined(RATELIMIT)
1294 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1295 		struct vlan_snd_tag *vst;
1296 		struct m_snd_tag *mst;
1297 
1298 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1299 		mst = m->m_pkthdr.snd_tag;
1300 		vst = mst_to_vst(mst);
1301 		if (vst->tag->ifp != p) {
1302 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1303 			m_freem(m);
1304 			return (EAGAIN);
1305 		}
1306 
1307 		m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1308 		m_snd_tag_rele(mst);
1309 	}
1310 #endif
1311 
1312 	/*
1313 	 * Do not run parent's if_transmit() if the parent is not up,
1314 	 * or parent's driver will cause a system crash.
1315 	 */
1316 	if (!UP_AND_RUNNING(p)) {
1317 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1318 		m_freem(m);
1319 		return (ENETDOWN);
1320 	}
1321 
1322 	if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) {
1323 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1324 		return (0);
1325 	}
1326 
1327 	/*
1328 	 * Send it, precisely as ether_output() would have.
1329 	 */
1330 	error = (p->if_transmit)(p, m);
1331 	if (error == 0) {
1332 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1333 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1334 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
1335 	} else
1336 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1337 	return (error);
1338 }
1339 
1340 static int
1341 vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1342     struct route *ro)
1343 {
1344 	struct ifvlan *ifv;
1345 	struct ifnet *p;
1346 
1347 	NET_EPOCH_ASSERT();
1348 
1349 	/*
1350 	 * Find the first non-VLAN parent interface.
1351 	 */
1352 	ifv = ifp->if_softc;
1353 	do {
1354 		if (TRUNK(ifv) == NULL) {
1355 			m_freem(m);
1356 			return (ENETDOWN);
1357 		}
1358 		p = PARENT(ifv);
1359 		ifv = p->if_softc;
1360 	} while (p->if_type == IFT_L2VLAN);
1361 
1362 	return p->if_output(ifp, m, dst, ro);
1363 }
1364 
1365 #ifdef ALTQ
1366 static void
1367 vlan_altq_start(if_t ifp)
1368 {
1369 	struct ifaltq *ifq = &ifp->if_snd;
1370 	struct mbuf *m;
1371 
1372 	IFQ_LOCK(ifq);
1373 	IFQ_DEQUEUE_NOLOCK(ifq, m);
1374 	while (m != NULL) {
1375 		vlan_transmit(ifp, m);
1376 		IFQ_DEQUEUE_NOLOCK(ifq, m);
1377 	}
1378 	IFQ_UNLOCK(ifq);
1379 }
1380 
1381 static int
1382 vlan_altq_transmit(if_t ifp, struct mbuf *m)
1383 {
1384 	int err;
1385 
1386 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
1387 		IFQ_ENQUEUE(&ifp->if_snd, m, err);
1388 		if (err == 0)
1389 			vlan_altq_start(ifp);
1390 	} else
1391 		err = vlan_transmit(ifp, m);
1392 
1393 	return (err);
1394 }
1395 #endif	/* ALTQ */
1396 
1397 /*
1398  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1399  */
1400 static void
1401 vlan_qflush(struct ifnet *ifp __unused)
1402 {
1403 }
1404 
1405 static void
1406 vlan_input(struct ifnet *ifp, struct mbuf *m)
1407 {
1408 	struct ifvlantrunk *trunk;
1409 	struct ifvlan *ifv;
1410 	struct m_tag *mtag;
1411 	uint16_t vid, tag;
1412 
1413 	NET_EPOCH_ASSERT();
1414 
1415 	trunk = ifp->if_vlantrunk;
1416 	if (trunk == NULL) {
1417 		m_freem(m);
1418 		return;
1419 	}
1420 
1421 	if (m->m_flags & M_VLANTAG) {
1422 		/*
1423 		 * Packet is tagged, but m contains a normal
1424 		 * Ethernet frame; the tag is stored out-of-band.
1425 		 */
1426 		tag = m->m_pkthdr.ether_vtag;
1427 		m->m_flags &= ~M_VLANTAG;
1428 	} else {
1429 		struct ether_vlan_header *evl;
1430 
1431 		/*
1432 		 * Packet is tagged in-band as specified by 802.1q.
1433 		 */
1434 		switch (ifp->if_type) {
1435 		case IFT_ETHER:
1436 			if (m->m_len < sizeof(*evl) &&
1437 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1438 				if_printf(ifp, "cannot pullup VLAN header\n");
1439 				return;
1440 			}
1441 			evl = mtod(m, struct ether_vlan_header *);
1442 			tag = ntohs(evl->evl_tag);
1443 
1444 			/*
1445 			 * Remove the 802.1q header by copying the Ethernet
1446 			 * addresses over it and adjusting the beginning of
1447 			 * the data in the mbuf.  The encapsulated Ethernet
1448 			 * type field is already in place.
1449 			 */
1450 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
1451 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
1452 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1453 			break;
1454 
1455 		default:
1456 #ifdef INVARIANTS
1457 			panic("%s: %s has unsupported if_type %u",
1458 			      __func__, ifp->if_xname, ifp->if_type);
1459 #endif
1460 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1461 			m_freem(m);
1462 			return;
1463 		}
1464 	}
1465 
1466 	vid = EVL_VLANOFTAG(tag);
1467 
1468 	ifv = vlan_gethash(trunk, vid);
1469 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1470 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1471 		m_freem(m);
1472 		return;
1473 	}
1474 
1475 	if (V_vlan_mtag_pcp) {
1476 		/*
1477 		 * While uncommon, it is possible that we will find a 802.1q
1478 		 * packet encapsulated inside another packet that also had an
1479 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
1480 		 * arriving over ethernet.  In that case, we replace the
1481 		 * existing 802.1q PCP m_tag value.
1482 		 */
1483 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
1484 		if (mtag == NULL) {
1485 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
1486 			    sizeof(uint8_t), M_NOWAIT);
1487 			if (mtag == NULL) {
1488 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1489 				m_freem(m);
1490 				return;
1491 			}
1492 			m_tag_prepend(m, mtag);
1493 		}
1494 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
1495 	}
1496 
1497 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1498 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1499 
1500 	/* Pass it back through the parent's input routine. */
1501 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
1502 }
1503 
1504 static void
1505 vlan_lladdr_fn(void *arg, int pending __unused)
1506 {
1507 	struct ifvlan *ifv;
1508 	struct ifnet *ifp;
1509 
1510 	ifv = (struct ifvlan *)arg;
1511 	ifp = ifv->ifv_ifp;
1512 
1513 	CURVNET_SET(ifp->if_vnet);
1514 
1515 	/* The ifv_ifp already has the lladdr copied in. */
1516 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1517 
1518 	CURVNET_RESTORE();
1519 }
1520 
1521 static int
1522 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid,
1523 	uint16_t proto)
1524 {
1525 	struct epoch_tracker et;
1526 	struct ifvlantrunk *trunk;
1527 	struct ifnet *ifp;
1528 	int error = 0;
1529 
1530 	/*
1531 	 * We can handle non-ethernet hardware types as long as
1532 	 * they handle the tagging and headers themselves.
1533 	 */
1534 	if (p->if_type != IFT_ETHER &&
1535 	    p->if_type != IFT_L2VLAN &&
1536 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1537 		return (EPROTONOSUPPORT);
1538 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
1539 		return (EPROTONOSUPPORT);
1540 	/*
1541 	 * Don't let the caller set up a VLAN VID with
1542 	 * anything except VLID bits.
1543 	 * VID numbers 0x0 and 0xFFF are reserved.
1544 	 */
1545 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1546 		return (EINVAL);
1547 	if (ifv->ifv_trunk) {
1548 		trunk = ifv->ifv_trunk;
1549 		if (trunk->parent != p)
1550 			return (EBUSY);
1551 
1552 		VLAN_XLOCK();
1553 
1554 		ifv->ifv_proto = proto;
1555 
1556 		if (ifv->ifv_vid != vid) {
1557 			/* Re-hash */
1558 			vlan_remhash(trunk, ifv);
1559 			ifv->ifv_vid = vid;
1560 			error = vlan_inshash(trunk, ifv);
1561 		}
1562 		/* Will unlock */
1563 		goto done;
1564 	}
1565 
1566 	VLAN_XLOCK();
1567 	if (p->if_vlantrunk == NULL) {
1568 		trunk = malloc(sizeof(struct ifvlantrunk),
1569 		    M_VLAN, M_WAITOK | M_ZERO);
1570 		vlan_inithash(trunk);
1571 		TRUNK_LOCK_INIT(trunk);
1572 		TRUNK_WLOCK(trunk);
1573 		p->if_vlantrunk = trunk;
1574 		trunk->parent = p;
1575 		if_ref(trunk->parent);
1576 		TRUNK_WUNLOCK(trunk);
1577 	} else {
1578 		trunk = p->if_vlantrunk;
1579 	}
1580 
1581 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
1582 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
1583 	error = vlan_inshash(trunk, ifv);
1584 	if (error)
1585 		goto done;
1586 	ifv->ifv_proto = proto;
1587 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1588 	ifv->ifv_mintu = ETHERMIN;
1589 	ifv->ifv_pflags = 0;
1590 	ifv->ifv_capenable = -1;
1591 
1592 	/*
1593 	 * If the parent supports the VLAN_MTU capability,
1594 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1595 	 * use it.
1596 	 */
1597 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1598 		/*
1599 		 * No need to fudge the MTU since the parent can
1600 		 * handle extended frames.
1601 		 */
1602 		ifv->ifv_mtufudge = 0;
1603 	} else {
1604 		/*
1605 		 * Fudge the MTU by the encapsulation size.  This
1606 		 * makes us incompatible with strictly compliant
1607 		 * 802.1Q implementations, but allows us to use
1608 		 * the feature with other NetBSD implementations,
1609 		 * which might still be useful.
1610 		 */
1611 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1612 	}
1613 
1614 	ifv->ifv_trunk = trunk;
1615 	ifp = ifv->ifv_ifp;
1616 	/*
1617 	 * Initialize fields from our parent.  This duplicates some
1618 	 * work with ether_ifattach() but allows for non-ethernet
1619 	 * interfaces to also work.
1620 	 */
1621 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1622 	ifp->if_baudrate = p->if_baudrate;
1623 	ifp->if_input = p->if_input;
1624 	ifp->if_resolvemulti = p->if_resolvemulti;
1625 	ifp->if_addrlen = p->if_addrlen;
1626 	ifp->if_broadcastaddr = p->if_broadcastaddr;
1627 	ifp->if_pcp = ifv->ifv_pcp;
1628 
1629 	/*
1630 	 * We wrap the parent's if_output using vlan_output to ensure that it
1631 	 * can't become stale.
1632 	 */
1633 	ifp->if_output = vlan_output;
1634 
1635 	/*
1636 	 * Copy only a selected subset of flags from the parent.
1637 	 * Other flags are none of our business.
1638 	 */
1639 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1640 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
1641 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1642 #undef VLAN_COPY_FLAGS
1643 
1644 	ifp->if_link_state = p->if_link_state;
1645 
1646 	NET_EPOCH_ENTER(et);
1647 	vlan_capabilities(ifv);
1648 	NET_EPOCH_EXIT(et);
1649 
1650 	/*
1651 	 * Set up our interface address to reflect the underlying
1652 	 * physical interface's.
1653 	 */
1654 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1655 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1656 	    p->if_addrlen;
1657 
1658 	/*
1659 	 * Do not schedule link address update if it was the same
1660 	 * as previous parent's. This helps avoid updating for each
1661 	 * associated llentry.
1662 	 */
1663 	if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1664 		bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1665 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1666 	}
1667 
1668 	/* We are ready for operation now. */
1669 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1670 
1671 	/* Update flags on the parent, if necessary. */
1672 	vlan_setflags(ifp, 1);
1673 
1674 	/*
1675 	 * Configure multicast addresses that may already be
1676 	 * joined on the vlan device.
1677 	 */
1678 	(void)vlan_setmulti(ifp);
1679 
1680 done:
1681 	if (error == 0)
1682 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1683 	VLAN_XUNLOCK();
1684 
1685 	return (error);
1686 }
1687 
1688 static void
1689 vlan_unconfig(struct ifnet *ifp)
1690 {
1691 
1692 	VLAN_XLOCK();
1693 	vlan_unconfig_locked(ifp, 0);
1694 	VLAN_XUNLOCK();
1695 }
1696 
1697 static void
1698 vlan_unconfig_locked(struct ifnet *ifp, int departing)
1699 {
1700 	struct ifvlantrunk *trunk;
1701 	struct vlan_mc_entry *mc;
1702 	struct ifvlan *ifv;
1703 	struct ifnet  *parent;
1704 	int error;
1705 
1706 	VLAN_XLOCK_ASSERT();
1707 
1708 	ifv = ifp->if_softc;
1709 	trunk = ifv->ifv_trunk;
1710 	parent = NULL;
1711 
1712 	if (trunk != NULL) {
1713 		parent = trunk->parent;
1714 
1715 		/*
1716 		 * Since the interface is being unconfigured, we need to
1717 		 * empty the list of multicast groups that we may have joined
1718 		 * while we were alive from the parent's list.
1719 		 */
1720 		while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1721 			/*
1722 			 * If the parent interface is being detached,
1723 			 * all its multicast addresses have already
1724 			 * been removed.  Warn about errors if
1725 			 * if_delmulti() does fail, but don't abort as
1726 			 * all callers expect vlan destruction to
1727 			 * succeed.
1728 			 */
1729 			if (!departing) {
1730 				error = if_delmulti(parent,
1731 				    (struct sockaddr *)&mc->mc_addr);
1732 				if (error)
1733 					if_printf(ifp,
1734 		    "Failed to delete multicast address from parent: %d\n",
1735 					    error);
1736 			}
1737 			CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1738 			NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
1739 		}
1740 
1741 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1742 
1743 		vlan_remhash(trunk, ifv);
1744 		ifv->ifv_trunk = NULL;
1745 
1746 		/*
1747 		 * Check if we were the last.
1748 		 */
1749 		if (trunk->refcnt == 0) {
1750 			parent->if_vlantrunk = NULL;
1751 			NET_EPOCH_WAIT();
1752 			trunk_destroy(trunk);
1753 		}
1754 	}
1755 
1756 	/* Disconnect from parent. */
1757 	if (ifv->ifv_pflags)
1758 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1759 	ifp->if_mtu = ETHERMTU;
1760 	ifp->if_link_state = LINK_STATE_UNKNOWN;
1761 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1762 
1763 	/*
1764 	 * Only dispatch an event if vlan was
1765 	 * attached, otherwise there is nothing
1766 	 * to cleanup anyway.
1767 	 */
1768 	if (parent != NULL)
1769 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1770 }
1771 
1772 /* Handle a reference counted flag that should be set on the parent as well */
1773 static int
1774 vlan_setflag(struct ifnet *ifp, int flag, int status,
1775 	     int (*func)(struct ifnet *, int))
1776 {
1777 	struct ifvlan *ifv;
1778 	int error;
1779 
1780 	VLAN_SXLOCK_ASSERT();
1781 
1782 	ifv = ifp->if_softc;
1783 	status = status ? (ifp->if_flags & flag) : 0;
1784 	/* Now "status" contains the flag value or 0 */
1785 
1786 	/*
1787 	 * See if recorded parent's status is different from what
1788 	 * we want it to be.  If it is, flip it.  We record parent's
1789 	 * status in ifv_pflags so that we won't clear parent's flag
1790 	 * we haven't set.  In fact, we don't clear or set parent's
1791 	 * flags directly, but get or release references to them.
1792 	 * That's why we can be sure that recorded flags still are
1793 	 * in accord with actual parent's flags.
1794 	 */
1795 	if (status != (ifv->ifv_pflags & flag)) {
1796 		error = (*func)(PARENT(ifv), status);
1797 		if (error)
1798 			return (error);
1799 		ifv->ifv_pflags &= ~flag;
1800 		ifv->ifv_pflags |= status;
1801 	}
1802 	return (0);
1803 }
1804 
1805 /*
1806  * Handle IFF_* flags that require certain changes on the parent:
1807  * if "status" is true, update parent's flags respective to our if_flags;
1808  * if "status" is false, forcedly clear the flags set on parent.
1809  */
1810 static int
1811 vlan_setflags(struct ifnet *ifp, int status)
1812 {
1813 	int error, i;
1814 
1815 	for (i = 0; vlan_pflags[i].flag; i++) {
1816 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
1817 				     status, vlan_pflags[i].func);
1818 		if (error)
1819 			return (error);
1820 	}
1821 	return (0);
1822 }
1823 
1824 /* Inform all vlans that their parent has changed link state */
1825 static void
1826 vlan_link_state(struct ifnet *ifp)
1827 {
1828 	struct epoch_tracker et;
1829 	struct ifvlantrunk *trunk;
1830 	struct ifvlan *ifv;
1831 
1832 	NET_EPOCH_ENTER(et);
1833 	trunk = ifp->if_vlantrunk;
1834 	if (trunk == NULL) {
1835 		NET_EPOCH_EXIT(et);
1836 		return;
1837 	}
1838 
1839 	TRUNK_WLOCK(trunk);
1840 	VLAN_FOREACH(ifv, trunk) {
1841 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1842 		if_link_state_change(ifv->ifv_ifp,
1843 		    trunk->parent->if_link_state);
1844 	}
1845 	TRUNK_WUNLOCK(trunk);
1846 	NET_EPOCH_EXIT(et);
1847 }
1848 
1849 static void
1850 vlan_capabilities(struct ifvlan *ifv)
1851 {
1852 	struct ifnet *p;
1853 	struct ifnet *ifp;
1854 	struct ifnet_hw_tsomax hw_tsomax;
1855 	int cap = 0, ena = 0, mena;
1856 	u_long hwa = 0;
1857 
1858 	NET_EPOCH_ASSERT();
1859 	VLAN_SXLOCK_ASSERT();
1860 
1861 	p = PARENT(ifv);
1862 	ifp = ifv->ifv_ifp;
1863 
1864 	/* Mask parent interface enabled capabilities disabled by user. */
1865 	mena = p->if_capenable & ifv->ifv_capenable;
1866 
1867 	/*
1868 	 * If the parent interface can do checksum offloading
1869 	 * on VLANs, then propagate its hardware-assisted
1870 	 * checksumming flags. Also assert that checksum
1871 	 * offloading requires hardware VLAN tagging.
1872 	 */
1873 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1874 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1875 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1876 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1877 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1878 		if (ena & IFCAP_TXCSUM)
1879 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1880 			    CSUM_UDP | CSUM_SCTP);
1881 		if (ena & IFCAP_TXCSUM_IPV6)
1882 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1883 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
1884 	}
1885 
1886 	/*
1887 	 * If the parent interface can do TSO on VLANs then
1888 	 * propagate the hardware-assisted flag. TSO on VLANs
1889 	 * does not necessarily require hardware VLAN tagging.
1890 	 */
1891 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
1892 	if_hw_tsomax_common(p, &hw_tsomax);
1893 	if_hw_tsomax_update(ifp, &hw_tsomax);
1894 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1895 		cap |= p->if_capabilities & IFCAP_TSO;
1896 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1897 		ena |= mena & IFCAP_TSO;
1898 		if (ena & IFCAP_TSO)
1899 			hwa |= p->if_hwassist & CSUM_TSO;
1900 	}
1901 
1902 	/*
1903 	 * If the parent interface can do LRO and checksum offloading on
1904 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
1905 	 * cost nothing, while false negative may lead to some confusions.
1906 	 */
1907 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1908 		cap |= p->if_capabilities & IFCAP_LRO;
1909 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
1910 		ena |= p->if_capenable & IFCAP_LRO;
1911 
1912 	/*
1913 	 * If the parent interface can offload TCP connections over VLANs then
1914 	 * propagate its TOE capability to the VLAN interface.
1915 	 *
1916 	 * All TOE drivers in the tree today can deal with VLANs.  If this
1917 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
1918 	 * with its own bit.
1919 	 */
1920 #define	IFCAP_VLAN_TOE IFCAP_TOE
1921 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1922 		cap |= p->if_capabilities & IFCAP_TOE;
1923 	if (p->if_capenable & IFCAP_VLAN_TOE) {
1924 		TOEDEV(ifp) = TOEDEV(p);
1925 		ena |= mena & IFCAP_TOE;
1926 	}
1927 
1928 	/*
1929 	 * If the parent interface supports dynamic link state, so does the
1930 	 * VLAN interface.
1931 	 */
1932 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1933 	ena |= (mena & IFCAP_LINKSTATE);
1934 
1935 #ifdef RATELIMIT
1936 	/*
1937 	 * If the parent interface supports ratelimiting, so does the
1938 	 * VLAN interface.
1939 	 */
1940 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1941 	ena |= (mena & IFCAP_TXRTLMT);
1942 #endif
1943 
1944 	/*
1945 	 * If the parent interface supports unmapped mbufs, so does
1946 	 * the VLAN interface.  Note that this should be fine even for
1947 	 * interfaces that don't support hardware tagging as headers
1948 	 * are prepended in normal mbufs to unmapped mbufs holding
1949 	 * payload data.
1950 	 */
1951 	cap |= (p->if_capabilities & IFCAP_MEXTPG);
1952 	ena |= (mena & IFCAP_MEXTPG);
1953 
1954 	/*
1955 	 * If the parent interface can offload encryption and segmentation
1956 	 * of TLS records over TCP, propagate it's capability to the VLAN
1957 	 * interface.
1958 	 *
1959 	 * All TLS drivers in the tree today can deal with VLANs.  If
1960 	 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1961 	 * defined.
1962 	 */
1963 	if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
1964 		cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
1965 	if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
1966 		ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
1967 
1968 	ifp->if_capabilities = cap;
1969 	ifp->if_capenable = ena;
1970 	ifp->if_hwassist = hwa;
1971 }
1972 
1973 static void
1974 vlan_trunk_capabilities(struct ifnet *ifp)
1975 {
1976 	struct epoch_tracker et;
1977 	struct ifvlantrunk *trunk;
1978 	struct ifvlan *ifv;
1979 
1980 	VLAN_SLOCK();
1981 	trunk = ifp->if_vlantrunk;
1982 	if (trunk == NULL) {
1983 		VLAN_SUNLOCK();
1984 		return;
1985 	}
1986 	NET_EPOCH_ENTER(et);
1987 	VLAN_FOREACH(ifv, trunk)
1988 		vlan_capabilities(ifv);
1989 	NET_EPOCH_EXIT(et);
1990 	VLAN_SUNLOCK();
1991 }
1992 
1993 static int
1994 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1995 {
1996 	struct ifnet *p;
1997 	struct ifreq *ifr;
1998 #ifdef INET
1999 	struct ifaddr *ifa;
2000 #endif
2001 	struct ifvlan *ifv;
2002 	struct ifvlantrunk *trunk;
2003 	struct vlanreq vlr;
2004 	int error = 0, oldmtu;
2005 
2006 	ifr = (struct ifreq *)data;
2007 #ifdef INET
2008 	ifa = (struct ifaddr *) data;
2009 #endif
2010 	ifv = ifp->if_softc;
2011 
2012 	switch (cmd) {
2013 	case SIOCSIFADDR:
2014 		ifp->if_flags |= IFF_UP;
2015 #ifdef INET
2016 		if (ifa->ifa_addr->sa_family == AF_INET)
2017 			arp_ifinit(ifp, ifa);
2018 #endif
2019 		break;
2020 	case SIOCGIFADDR:
2021 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
2022 		    ifp->if_addrlen);
2023 		break;
2024 	case SIOCGIFMEDIA:
2025 		VLAN_SLOCK();
2026 		if (TRUNK(ifv) != NULL) {
2027 			p = PARENT(ifv);
2028 			if_ref(p);
2029 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
2030 			if_rele(p);
2031 			/* Limit the result to the parent's current config. */
2032 			if (error == 0) {
2033 				struct ifmediareq *ifmr;
2034 
2035 				ifmr = (struct ifmediareq *)data;
2036 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
2037 					ifmr->ifm_count = 1;
2038 					error = copyout(&ifmr->ifm_current,
2039 						ifmr->ifm_ulist,
2040 						sizeof(int));
2041 				}
2042 			}
2043 		} else {
2044 			error = EINVAL;
2045 		}
2046 		VLAN_SUNLOCK();
2047 		break;
2048 
2049 	case SIOCSIFMEDIA:
2050 		error = EINVAL;
2051 		break;
2052 
2053 	case SIOCSIFMTU:
2054 		/*
2055 		 * Set the interface MTU.
2056 		 */
2057 		VLAN_SLOCK();
2058 		trunk = TRUNK(ifv);
2059 		if (trunk != NULL) {
2060 			TRUNK_WLOCK(trunk);
2061 			if (ifr->ifr_mtu >
2062 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
2063 			    ifr->ifr_mtu <
2064 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
2065 				error = EINVAL;
2066 			else
2067 				ifp->if_mtu = ifr->ifr_mtu;
2068 			TRUNK_WUNLOCK(trunk);
2069 		} else
2070 			error = EINVAL;
2071 		VLAN_SUNLOCK();
2072 		break;
2073 
2074 	case SIOCSETVLAN:
2075 #ifdef VIMAGE
2076 		/*
2077 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
2078 		 * interface to be delegated to a jail without allowing the
2079 		 * jail to change what underlying interface/VID it is
2080 		 * associated with.  We are not entirely convinced that this
2081 		 * is the right way to accomplish that policy goal.
2082 		 */
2083 		if (ifp->if_vnet != ifp->if_home_vnet) {
2084 			error = EPERM;
2085 			break;
2086 		}
2087 #endif
2088 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
2089 		if (error)
2090 			break;
2091 		if (vlr.vlr_parent[0] == '\0') {
2092 			vlan_unconfig(ifp);
2093 			break;
2094 		}
2095 		p = ifunit_ref(vlr.vlr_parent);
2096 		if (p == NULL) {
2097 			error = ENOENT;
2098 			break;
2099 		}
2100 #ifdef COMPAT_FREEBSD12
2101 		if (vlr.vlr_proto == 0)
2102 			vlr.vlr_proto = ETHERTYPE_VLAN;
2103 #endif
2104 		oldmtu = ifp->if_mtu;
2105 		error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto);
2106 		if_rele(p);
2107 
2108 		/*
2109 		 * VLAN MTU may change during addition of the vlandev.
2110 		 * If it did, do network layer specific procedure.
2111 		 */
2112 		if (ifp->if_mtu != oldmtu) {
2113 #ifdef INET6
2114 			nd6_setmtu(ifp);
2115 #endif
2116 			rt_updatemtu(ifp);
2117 		}
2118 		break;
2119 
2120 	case SIOCGETVLAN:
2121 #ifdef VIMAGE
2122 		if (ifp->if_vnet != ifp->if_home_vnet) {
2123 			error = EPERM;
2124 			break;
2125 		}
2126 #endif
2127 		bzero(&vlr, sizeof(vlr));
2128 		VLAN_SLOCK();
2129 		if (TRUNK(ifv) != NULL) {
2130 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
2131 			    sizeof(vlr.vlr_parent));
2132 			vlr.vlr_tag = ifv->ifv_vid;
2133 			vlr.vlr_proto = ifv->ifv_proto;
2134 		}
2135 		VLAN_SUNLOCK();
2136 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
2137 		break;
2138 
2139 	case SIOCSIFFLAGS:
2140 		/*
2141 		 * We should propagate selected flags to the parent,
2142 		 * e.g., promiscuous mode.
2143 		 */
2144 		VLAN_XLOCK();
2145 		if (TRUNK(ifv) != NULL)
2146 			error = vlan_setflags(ifp, 1);
2147 		VLAN_XUNLOCK();
2148 		break;
2149 
2150 	case SIOCADDMULTI:
2151 	case SIOCDELMULTI:
2152 		/*
2153 		 * If we don't have a parent, just remember the membership for
2154 		 * when we do.
2155 		 *
2156 		 * XXX We need the rmlock here to avoid sleeping while
2157 		 * holding in6_multi_mtx.
2158 		 */
2159 		VLAN_XLOCK();
2160 		trunk = TRUNK(ifv);
2161 		if (trunk != NULL)
2162 			error = vlan_setmulti(ifp);
2163 		VLAN_XUNLOCK();
2164 
2165 		break;
2166 	case SIOCGVLANPCP:
2167 #ifdef VIMAGE
2168 		if (ifp->if_vnet != ifp->if_home_vnet) {
2169 			error = EPERM;
2170 			break;
2171 		}
2172 #endif
2173 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
2174 		break;
2175 
2176 	case SIOCSVLANPCP:
2177 #ifdef VIMAGE
2178 		if (ifp->if_vnet != ifp->if_home_vnet) {
2179 			error = EPERM;
2180 			break;
2181 		}
2182 #endif
2183 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
2184 		if (error)
2185 			break;
2186 		if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) {
2187 			error = EINVAL;
2188 			break;
2189 		}
2190 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
2191 		ifp->if_pcp = ifv->ifv_pcp;
2192 		/* broadcast event about PCP change */
2193 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
2194 		break;
2195 
2196 	case SIOCSIFCAP:
2197 		VLAN_SLOCK();
2198 		ifv->ifv_capenable = ifr->ifr_reqcap;
2199 		trunk = TRUNK(ifv);
2200 		if (trunk != NULL) {
2201 			struct epoch_tracker et;
2202 
2203 			NET_EPOCH_ENTER(et);
2204 			vlan_capabilities(ifv);
2205 			NET_EPOCH_EXIT(et);
2206 		}
2207 		VLAN_SUNLOCK();
2208 		break;
2209 
2210 	default:
2211 		error = EINVAL;
2212 		break;
2213 	}
2214 
2215 	return (error);
2216 }
2217 
2218 #if defined(KERN_TLS) || defined(RATELIMIT)
2219 static int
2220 vlan_snd_tag_alloc(struct ifnet *ifp,
2221     union if_snd_tag_alloc_params *params,
2222     struct m_snd_tag **ppmt)
2223 {
2224 	struct epoch_tracker et;
2225 	const struct if_snd_tag_sw *sw;
2226 	struct vlan_snd_tag *vst;
2227 	struct ifvlan *ifv;
2228 	struct ifnet *parent;
2229 	struct m_snd_tag *mst;
2230 	int error;
2231 
2232 	NET_EPOCH_ENTER(et);
2233 	ifv = ifp->if_softc;
2234 
2235 	switch (params->hdr.type) {
2236 #ifdef RATELIMIT
2237 	case IF_SND_TAG_TYPE_UNLIMITED:
2238 		sw = &vlan_snd_tag_ul_sw;
2239 		break;
2240 	case IF_SND_TAG_TYPE_RATE_LIMIT:
2241 		sw = &vlan_snd_tag_rl_sw;
2242 		break;
2243 #endif
2244 #ifdef KERN_TLS
2245 	case IF_SND_TAG_TYPE_TLS:
2246 		sw = &vlan_snd_tag_tls_sw;
2247 		break;
2248 	case IF_SND_TAG_TYPE_TLS_RX:
2249 		sw = NULL;
2250 		if (params->tls_rx.vlan_id != 0)
2251 			goto failure;
2252 		params->tls_rx.vlan_id = ifv->ifv_vid;
2253 		break;
2254 #ifdef RATELIMIT
2255 	case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
2256 		sw = &vlan_snd_tag_tls_rl_sw;
2257 		break;
2258 #endif
2259 #endif
2260 	default:
2261 		goto failure;
2262 	}
2263 
2264 	if (ifv->ifv_trunk != NULL)
2265 		parent = PARENT(ifv);
2266 	else
2267 		parent = NULL;
2268 	if (parent == NULL)
2269 		goto failure;
2270 	if_ref(parent);
2271 	NET_EPOCH_EXIT(et);
2272 
2273 	if (sw != NULL) {
2274 		vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2275 		if (vst == NULL) {
2276 			if_rele(parent);
2277 			return (ENOMEM);
2278 		}
2279 	} else
2280 		vst = NULL;
2281 
2282 	error = m_snd_tag_alloc(parent, params, &mst);
2283 	if_rele(parent);
2284 	if (error) {
2285 		free(vst, M_VLAN);
2286 		return (error);
2287 	}
2288 
2289 	if (sw != NULL) {
2290 		m_snd_tag_init(&vst->com, ifp, sw);
2291 		vst->tag = mst;
2292 
2293 		*ppmt = &vst->com;
2294 	} else
2295 		*ppmt = mst;
2296 
2297 	return (0);
2298 failure:
2299 	NET_EPOCH_EXIT(et);
2300 	return (EOPNOTSUPP);
2301 }
2302 
2303 static struct m_snd_tag *
2304 vlan_next_snd_tag(struct m_snd_tag *mst)
2305 {
2306 	struct vlan_snd_tag *vst;
2307 
2308 	vst = mst_to_vst(mst);
2309 	return (vst->tag);
2310 }
2311 
2312 static int
2313 vlan_snd_tag_modify(struct m_snd_tag *mst,
2314     union if_snd_tag_modify_params *params)
2315 {
2316 	struct vlan_snd_tag *vst;
2317 
2318 	vst = mst_to_vst(mst);
2319 	return (vst->tag->sw->snd_tag_modify(vst->tag, params));
2320 }
2321 
2322 static int
2323 vlan_snd_tag_query(struct m_snd_tag *mst,
2324     union if_snd_tag_query_params *params)
2325 {
2326 	struct vlan_snd_tag *vst;
2327 
2328 	vst = mst_to_vst(mst);
2329 	return (vst->tag->sw->snd_tag_query(vst->tag, params));
2330 }
2331 
2332 static void
2333 vlan_snd_tag_free(struct m_snd_tag *mst)
2334 {
2335 	struct vlan_snd_tag *vst;
2336 
2337 	vst = mst_to_vst(mst);
2338 	m_snd_tag_rele(vst->tag);
2339 	free(vst, M_VLAN);
2340 }
2341 
2342 static void
2343 vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
2344 {
2345 	/*
2346 	 * For vlan, we have an indirect
2347 	 * interface. The caller needs to
2348 	 * get a ratelimit tag on the actual
2349 	 * interface the flow will go on.
2350 	 */
2351 	q->rate_table = NULL;
2352 	q->flags = RT_IS_INDIRECT;
2353 	q->max_flows = 0;
2354 	q->number_of_rates = 0;
2355 }
2356 
2357 #endif
2358