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