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