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