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