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