xref: /freebsd/sys/net/if_vlan.c (revision 409a390c3341fb4f162cd7de1fd595a323ebbfd8)
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
34  * Might be extended some day to also handle IEEE 802.1p priority
35  * tagging.  This is sort of sneaky in the implementation, since
36  * we need to pretend to be enough of an Ethernet implementation
37  * to make arp work.  The way we do this is by telling everyone
38  * that we are an Ethernet, and then catch the packets that
39  * ether_output() left on our output queue when it calls
40  * if_start(), rewrite them for use by the real outgoing interface,
41  * and ask it to send them.
42  */
43 
44 #include "opt_vlan.h"
45 
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/module.h>
52 #include <sys/rwlock.h>
53 #include <sys/queue.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
58 
59 #include <net/bpf.h>
60 #include <net/ethernet.h>
61 #include <net/if.h>
62 #include <net/if_clone.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/if_vlan_var.h>
66 #include <net/vnet.h>
67 
68 #define VLANNAME	"vlan"
69 #define	VLAN_DEF_HWIDTH	4
70 #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
71 
72 #define	UP_AND_RUNNING(ifp) \
73     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
74 
75 LIST_HEAD(ifvlanhead, ifvlan);
76 
77 struct ifvlantrunk {
78 	struct	ifnet   *parent;	/* parent interface of this trunk */
79 	struct	rwlock	rw;
80 #ifdef VLAN_ARRAY
81 #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
82 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
83 #else
84 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
85 	uint16_t	hmask;
86 	uint16_t	hwidth;
87 #endif
88 	int		refcnt;
89 };
90 
91 struct vlan_mc_entry {
92 	struct ether_addr		mc_addr;
93 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
94 };
95 
96 struct	ifvlan {
97 	struct	ifvlantrunk *ifv_trunk;
98 	struct	ifnet *ifv_ifp;
99 #define	TRUNK(ifv)	((ifv)->ifv_trunk)
100 #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
101 	int	ifv_pflags;	/* special flags we have set on parent */
102 	struct	ifv_linkmib {
103 		int	ifvm_encaplen;	/* encapsulation length */
104 		int	ifvm_mtufudge;	/* MTU fudged by this much */
105 		int	ifvm_mintu;	/* min transmission unit */
106 		uint16_t ifvm_proto;	/* encapsulation ethertype */
107 		uint16_t ifvm_tag;	/* tag to apply on packets leaving if */
108 	}	ifv_mib;
109 	SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
110 #ifndef VLAN_ARRAY
111 	LIST_ENTRY(ifvlan) ifv_list;
112 #endif
113 };
114 #define	ifv_proto	ifv_mib.ifvm_proto
115 #define	ifv_tag		ifv_mib.ifvm_tag
116 #define	ifv_encaplen	ifv_mib.ifvm_encaplen
117 #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
118 #define	ifv_mintu	ifv_mib.ifvm_mintu
119 
120 /* Special flags we should propagate to parent. */
121 static struct {
122 	int flag;
123 	int (*func)(struct ifnet *, int);
124 } vlan_pflags[] = {
125 	{IFF_PROMISC, ifpromisc},
126 	{IFF_ALLMULTI, if_allmulti},
127 	{0, NULL}
128 };
129 
130 SYSCTL_DECL(_net_link);
131 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
132 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
133 
134 static int soft_pad = 0;
135 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
136 	   "pad short frames before tagging");
137 
138 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
139 
140 static eventhandler_tag ifdetach_tag;
141 
142 /*
143  * We have a global mutex, that is used to serialize configuration
144  * changes and isn't used in normal packet delivery.
145  *
146  * We also have a per-trunk rwlock, that is locked shared on packet
147  * processing and exclusive when configuration is changed.
148  *
149  * The VLAN_ARRAY substitutes the dynamic hash with a static array
150  * with 4096 entries. In theory this can give a boost in processing,
151  * however on practice it does not. Probably this is because array
152  * is too big to fit into CPU cache.
153  */
154 static struct mtx ifv_mtx;
155 #define	VLAN_LOCK_INIT()	mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
156 #define	VLAN_LOCK_DESTROY()	mtx_destroy(&ifv_mtx)
157 #define	VLAN_LOCK_ASSERT()	mtx_assert(&ifv_mtx, MA_OWNED)
158 #define	VLAN_LOCK()		mtx_lock(&ifv_mtx)
159 #define	VLAN_UNLOCK()		mtx_unlock(&ifv_mtx)
160 #define	TRUNK_LOCK_INIT(trunk)	rw_init(&(trunk)->rw, VLANNAME)
161 #define	TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
162 #define	TRUNK_LOCK(trunk)	rw_wlock(&(trunk)->rw)
163 #define	TRUNK_UNLOCK(trunk)	rw_wunlock(&(trunk)->rw)
164 #define	TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
165 #define	TRUNK_RLOCK(trunk)	rw_rlock(&(trunk)->rw)
166 #define	TRUNK_RUNLOCK(trunk)	rw_runlock(&(trunk)->rw)
167 #define	TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
168 
169 #ifndef VLAN_ARRAY
170 static	void vlan_inithash(struct ifvlantrunk *trunk);
171 static	void vlan_freehash(struct ifvlantrunk *trunk);
172 static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
173 static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
174 static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
175 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
176 	uint16_t tag);
177 #endif
178 static	void trunk_destroy(struct ifvlantrunk *trunk);
179 
180 static	void vlan_start(struct ifnet *ifp);
181 static	void vlan_init(void *foo);
182 static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
183 static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
184 static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
185     int (*func)(struct ifnet *, int));
186 static	int vlan_setflags(struct ifnet *ifp, int status);
187 static	int vlan_setmulti(struct ifnet *ifp);
188 static	int vlan_unconfig(struct ifnet *ifp);
189 static	int vlan_unconfig_locked(struct ifnet *ifp);
190 static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
191 static	void vlan_link_state(struct ifnet *ifp);
192 static	void vlan_capabilities(struct ifvlan *ifv);
193 static	void vlan_trunk_capabilities(struct ifnet *ifp);
194 
195 static	struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
196     const char *, int *);
197 static	int vlan_clone_match(struct if_clone *, const char *);
198 static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
199 static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
200 
201 static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
202 
203 static	struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
204     IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
205 
206 #ifndef VLAN_ARRAY
207 #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
208 
209 static void
210 vlan_inithash(struct ifvlantrunk *trunk)
211 {
212 	int i, n;
213 
214 	/*
215 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
216 	 * It is OK in case this function is called before the trunk struct
217 	 * gets hooked up and becomes visible from other threads.
218 	 */
219 
220 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
221 	    ("%s: hash already initialized", __func__));
222 
223 	trunk->hwidth = VLAN_DEF_HWIDTH;
224 	n = 1 << trunk->hwidth;
225 	trunk->hmask = n - 1;
226 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
227 	for (i = 0; i < n; i++)
228 		LIST_INIT(&trunk->hash[i]);
229 }
230 
231 static void
232 vlan_freehash(struct ifvlantrunk *trunk)
233 {
234 #ifdef INVARIANTS
235 	int i;
236 
237 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
238 	for (i = 0; i < (1 << trunk->hwidth); i++)
239 		KASSERT(LIST_EMPTY(&trunk->hash[i]),
240 		    ("%s: hash table not empty", __func__));
241 #endif
242 	free(trunk->hash, M_VLAN);
243 	trunk->hash = NULL;
244 	trunk->hwidth = trunk->hmask = 0;
245 }
246 
247 static int
248 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
249 {
250 	int i, b;
251 	struct ifvlan *ifv2;
252 
253 	TRUNK_LOCK_ASSERT(trunk);
254 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
255 
256 	b = 1 << trunk->hwidth;
257 	i = HASH(ifv->ifv_tag, trunk->hmask);
258 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
259 		if (ifv->ifv_tag == ifv2->ifv_tag)
260 			return (EEXIST);
261 
262 	/*
263 	 * Grow the hash when the number of vlans exceeds half of the number of
264 	 * hash buckets squared. This will make the average linked-list length
265 	 * buckets/2.
266 	 */
267 	if (trunk->refcnt > (b * b) / 2) {
268 		vlan_growhash(trunk, 1);
269 		i = HASH(ifv->ifv_tag, trunk->hmask);
270 	}
271 	LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
272 	trunk->refcnt++;
273 
274 	return (0);
275 }
276 
277 static int
278 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
279 {
280 	int i, b;
281 	struct ifvlan *ifv2;
282 
283 	TRUNK_LOCK_ASSERT(trunk);
284 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
285 
286 	b = 1 << trunk->hwidth;
287 	i = HASH(ifv->ifv_tag, trunk->hmask);
288 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
289 		if (ifv2 == ifv) {
290 			trunk->refcnt--;
291 			LIST_REMOVE(ifv2, ifv_list);
292 			if (trunk->refcnt < (b * b) / 2)
293 				vlan_growhash(trunk, -1);
294 			return (0);
295 		}
296 
297 	panic("%s: vlan not found\n", __func__);
298 	return (ENOENT); /*NOTREACHED*/
299 }
300 
301 /*
302  * Grow the hash larger or smaller if memory permits.
303  */
304 static void
305 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
306 {
307 	struct ifvlan *ifv;
308 	struct ifvlanhead *hash2;
309 	int hwidth2, i, j, n, n2;
310 
311 	TRUNK_LOCK_ASSERT(trunk);
312 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
313 
314 	if (howmuch == 0) {
315 		/* Harmless yet obvious coding error */
316 		printf("%s: howmuch is 0\n", __func__);
317 		return;
318 	}
319 
320 	hwidth2 = trunk->hwidth + howmuch;
321 	n = 1 << trunk->hwidth;
322 	n2 = 1 << hwidth2;
323 	/* Do not shrink the table below the default */
324 	if (hwidth2 < VLAN_DEF_HWIDTH)
325 		return;
326 
327 	/* M_NOWAIT because we're called with trunk mutex held */
328 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
329 	if (hash2 == NULL) {
330 		printf("%s: out of memory -- hash size not changed\n",
331 		    __func__);
332 		return;		/* We can live with the old hash table */
333 	}
334 	for (j = 0; j < n2; j++)
335 		LIST_INIT(&hash2[j]);
336 	for (i = 0; i < n; i++)
337 		while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
338 			LIST_REMOVE(ifv, ifv_list);
339 			j = HASH(ifv->ifv_tag, n2 - 1);
340 			LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
341 		}
342 	free(trunk->hash, M_VLAN);
343 	trunk->hash = hash2;
344 	trunk->hwidth = hwidth2;
345 	trunk->hmask = n2 - 1;
346 
347 	if (bootverbose)
348 		if_printf(trunk->parent,
349 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
350 }
351 
352 static __inline struct ifvlan *
353 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
354 {
355 	struct ifvlan *ifv;
356 
357 	TRUNK_LOCK_RASSERT(trunk);
358 
359 	LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
360 		if (ifv->ifv_tag == tag)
361 			return (ifv);
362 	return (NULL);
363 }
364 
365 #if 0
366 /* Debugging code to view the hashtables. */
367 static void
368 vlan_dumphash(struct ifvlantrunk *trunk)
369 {
370 	int i;
371 	struct ifvlan *ifv;
372 
373 	for (i = 0; i < (1 << trunk->hwidth); i++) {
374 		printf("%d: ", i);
375 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
376 			printf("%s ", ifv->ifv_ifp->if_xname);
377 		printf("\n");
378 	}
379 }
380 #endif /* 0 */
381 #endif /* !VLAN_ARRAY */
382 
383 static void
384 trunk_destroy(struct ifvlantrunk *trunk)
385 {
386 	VLAN_LOCK_ASSERT();
387 
388 	TRUNK_LOCK(trunk);
389 #ifndef VLAN_ARRAY
390 	vlan_freehash(trunk);
391 #endif
392 	trunk->parent->if_vlantrunk = NULL;
393 	TRUNK_UNLOCK(trunk);
394 	TRUNK_LOCK_DESTROY(trunk);
395 	free(trunk, M_VLAN);
396 }
397 
398 /*
399  * Program our multicast filter. What we're actually doing is
400  * programming the multicast filter of the parent. This has the
401  * side effect of causing the parent interface to receive multicast
402  * traffic that it doesn't really want, which ends up being discarded
403  * later by the upper protocol layers. Unfortunately, there's no way
404  * to avoid this: there really is only one physical interface.
405  *
406  * XXX: There is a possible race here if more than one thread is
407  *      modifying the multicast state of the vlan interface at the same time.
408  */
409 static int
410 vlan_setmulti(struct ifnet *ifp)
411 {
412 	struct ifnet		*ifp_p;
413 	struct ifmultiaddr	*ifma, *rifma = NULL;
414 	struct ifvlan		*sc;
415 	struct vlan_mc_entry	*mc;
416 	struct sockaddr_dl	sdl;
417 	int			error;
418 
419 	/*VLAN_LOCK_ASSERT();*/
420 
421 	/* Find the parent. */
422 	sc = ifp->if_softc;
423 	ifp_p = PARENT(sc);
424 
425 	CURVNET_SET_QUIET(ifp_p->if_vnet);
426 
427 	bzero((char *)&sdl, sizeof(sdl));
428 	sdl.sdl_len = sizeof(sdl);
429 	sdl.sdl_family = AF_LINK;
430 	sdl.sdl_index = ifp_p->if_index;
431 	sdl.sdl_type = IFT_ETHER;
432 	sdl.sdl_alen = ETHER_ADDR_LEN;
433 
434 	/* First, remove any existing filter entries. */
435 	while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
436 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
437 		error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
438 		if (error)
439 			return (error);
440 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
441 		free(mc, M_VLAN);
442 	}
443 
444 	/* Now program new ones. */
445 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
446 		if (ifma->ifma_addr->sa_family != AF_LINK)
447 			continue;
448 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
449 		if (mc == NULL)
450 			return (ENOMEM);
451 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
452 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
453 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
454 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
455 		    LLADDR(&sdl), ETHER_ADDR_LEN);
456 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
457 		if (error)
458 			return (error);
459 	}
460 
461 	CURVNET_RESTORE();
462 	return (0);
463 }
464 
465 /*
466  * A handler for network interface departure events.
467  * Track departure of trunks here so that we don't access invalid
468  * pointers or whatever if a trunk is ripped from under us, e.g.,
469  * by ejecting its hot-plug card.  However, if an ifnet is simply
470  * being renamed, then there's no need to tear down the state.
471  */
472 static void
473 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
474 {
475 	struct ifvlan *ifv;
476 	int i;
477 
478 	/*
479 	 * Check if it's a trunk interface first of all
480 	 * to avoid needless locking.
481 	 */
482 	if (ifp->if_vlantrunk == NULL)
483 		return;
484 
485 	/* If the ifnet is just being renamed, don't do anything. */
486 	if (ifp->if_flags & IFF_RENAMING)
487 		return;
488 
489 	VLAN_LOCK();
490 	/*
491 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
492 	 * Check trunk pointer after each vlan_unconfig() as it will
493 	 * free it and set to NULL after the last vlan was detached.
494 	 */
495 #ifdef VLAN_ARRAY
496 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
497 		if ((ifv = ifp->if_vlantrunk->vlans[i])) {
498 			vlan_unconfig_locked(ifv->ifv_ifp);
499 			if (ifp->if_vlantrunk == NULL)
500 				break;
501 		}
502 #else /* VLAN_ARRAY */
503 restart:
504 	for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
505 		if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
506 			vlan_unconfig_locked(ifv->ifv_ifp);
507 			if (ifp->if_vlantrunk)
508 				goto restart;	/* trunk->hwidth can change */
509 			else
510 				break;
511 		}
512 #endif /* VLAN_ARRAY */
513 	/* Trunk should have been destroyed in vlan_unconfig(). */
514 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
515 	VLAN_UNLOCK();
516 }
517 
518 /*
519  * VLAN support can be loaded as a module.  The only place in the
520  * system that's intimately aware of this is ether_input.  We hook
521  * into this code through vlan_input_p which is defined there and
522  * set here.  Noone else in the system should be aware of this so
523  * we use an explicit reference here.
524  */
525 extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
526 
527 /* For if_link_state_change() eyes only... */
528 extern	void (*vlan_link_state_p)(struct ifnet *);
529 
530 static int
531 vlan_modevent(module_t mod, int type, void *data)
532 {
533 
534 	switch (type) {
535 	case MOD_LOAD:
536 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
537 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
538 		if (ifdetach_tag == NULL)
539 			return (ENOMEM);
540 		VLAN_LOCK_INIT();
541 		vlan_input_p = vlan_input;
542 		vlan_link_state_p = vlan_link_state;
543 		vlan_trunk_cap_p = vlan_trunk_capabilities;
544 		if_clone_attach(&vlan_cloner);
545 		if (bootverbose)
546 			printf("vlan: initialized, using "
547 #ifdef VLAN_ARRAY
548 			       "full-size arrays"
549 #else
550 			       "hash tables with chaining"
551 #endif
552 
553 			       "\n");
554 		break;
555 	case MOD_UNLOAD:
556 		if_clone_detach(&vlan_cloner);
557 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
558 		vlan_input_p = NULL;
559 		vlan_link_state_p = NULL;
560 		vlan_trunk_cap_p = NULL;
561 		VLAN_LOCK_DESTROY();
562 		if (bootverbose)
563 			printf("vlan: unloaded\n");
564 		break;
565 	default:
566 		return (EOPNOTSUPP);
567 	}
568 	return (0);
569 }
570 
571 static moduledata_t vlan_mod = {
572 	"if_vlan",
573 	vlan_modevent,
574 	0
575 };
576 
577 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
578 MODULE_VERSION(if_vlan, 3);
579 
580 static struct ifnet *
581 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
582 {
583 	const char *cp;
584 	struct ifnet *ifp;
585 	int t;
586 
587 	/* Check for <etherif>.<vlan> style interface names. */
588 	IFNET_RLOCK_NOSLEEP();
589 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
590 		if (ifp->if_type != IFT_ETHER)
591 			continue;
592 		if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
593 			continue;
594 		cp = name + strlen(ifp->if_xname);
595 		if (*cp++ != '.')
596 			continue;
597 		if (*cp == '\0')
598 			continue;
599 		t = 0;
600 		for(; *cp >= '0' && *cp <= '9'; cp++)
601 			t = (t * 10) + (*cp - '0');
602 		if (*cp != '\0')
603 			continue;
604 		if (tag != NULL)
605 			*tag = t;
606 		break;
607 	}
608 	IFNET_RUNLOCK_NOSLEEP();
609 
610 	return (ifp);
611 }
612 
613 static int
614 vlan_clone_match(struct if_clone *ifc, const char *name)
615 {
616 	const char *cp;
617 
618 	if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
619 		return (1);
620 
621 	if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
622 		return (0);
623 	for (cp = name + 4; *cp != '\0'; cp++) {
624 		if (*cp < '0' || *cp > '9')
625 			return (0);
626 	}
627 
628 	return (1);
629 }
630 
631 static int
632 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
633 {
634 	char *dp;
635 	int wildcard;
636 	int unit;
637 	int error;
638 	int tag;
639 	int ethertag;
640 	struct ifvlan *ifv;
641 	struct ifnet *ifp;
642 	struct ifnet *p;
643 	struct vlanreq vlr;
644 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
645 
646 	/*
647 	 * There are 3 (ugh) ways to specify the cloned device:
648 	 * o pass a parameter block with the clone request.
649 	 * o specify parameters in the text of the clone device name
650 	 * o specify no parameters and get an unattached device that
651 	 *   must be configured separately.
652 	 * The first technique is preferred; the latter two are
653 	 * supported for backwards compatibilty.
654 	 */
655 	if (params) {
656 		error = copyin(params, &vlr, sizeof(vlr));
657 		if (error)
658 			return error;
659 		p = ifunit(vlr.vlr_parent);
660 		if (p == NULL)
661 			return ENXIO;
662 		/*
663 		 * Don't let the caller set up a VLAN tag with
664 		 * anything except VLID bits.
665 		 */
666 		if (vlr.vlr_tag & ~EVL_VLID_MASK)
667 			return (EINVAL);
668 		error = ifc_name2unit(name, &unit);
669 		if (error != 0)
670 			return (error);
671 
672 		ethertag = 1;
673 		tag = vlr.vlr_tag;
674 		wildcard = (unit < 0);
675 	} else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
676 		ethertag = 1;
677 		unit = -1;
678 		wildcard = 0;
679 
680 		/*
681 		 * Don't let the caller set up a VLAN tag with
682 		 * anything except VLID bits.
683 		 */
684 		if (tag & ~EVL_VLID_MASK)
685 			return (EINVAL);
686 	} else {
687 		ethertag = 0;
688 
689 		error = ifc_name2unit(name, &unit);
690 		if (error != 0)
691 			return (error);
692 
693 		wildcard = (unit < 0);
694 	}
695 
696 	error = ifc_alloc_unit(ifc, &unit);
697 	if (error != 0)
698 		return (error);
699 
700 	/* In the wildcard case, we need to update the name. */
701 	if (wildcard) {
702 		for (dp = name; *dp != '\0'; dp++);
703 		if (snprintf(dp, len - (dp-name), "%d", unit) >
704 		    len - (dp-name) - 1) {
705 			panic("%s: interface name too long", __func__);
706 		}
707 	}
708 
709 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
710 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
711 	if (ifp == NULL) {
712 		ifc_free_unit(ifc, unit);
713 		free(ifv, M_VLAN);
714 		return (ENOSPC);
715 	}
716 	SLIST_INIT(&ifv->vlan_mc_listhead);
717 
718 	ifp->if_softc = ifv;
719 	/*
720 	 * Set the name manually rather than using if_initname because
721 	 * we don't conform to the default naming convention for interfaces.
722 	 */
723 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
724 	ifp->if_dname = ifc->ifc_name;
725 	ifp->if_dunit = unit;
726 	/* NB: flags are not set here */
727 	ifp->if_linkmib = &ifv->ifv_mib;
728 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
729 	/* NB: mtu is not set here */
730 
731 	ifp->if_init = vlan_init;
732 	ifp->if_start = vlan_start;
733 	ifp->if_ioctl = vlan_ioctl;
734 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
735 	ifp->if_flags = VLAN_IFFLAGS;
736 	ether_ifattach(ifp, eaddr);
737 	/* Now undo some of the damage... */
738 	ifp->if_baudrate = 0;
739 	ifp->if_type = IFT_L2VLAN;
740 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
741 
742 	if (ethertag) {
743 		error = vlan_config(ifv, p, tag);
744 		if (error != 0) {
745 			/*
746 			 * Since we've partialy failed, we need to back
747 			 * out all the way, otherwise userland could get
748 			 * confused.  Thus, we destroy the interface.
749 			 */
750 			ether_ifdetach(ifp);
751 			vlan_unconfig(ifp);
752 			if_free_type(ifp, IFT_ETHER);
753 			ifc_free_unit(ifc, unit);
754 			free(ifv, M_VLAN);
755 
756 			return (error);
757 		}
758 
759 		/* Update flags on the parent, if necessary. */
760 		vlan_setflags(ifp, 1);
761 	}
762 
763 	return (0);
764 }
765 
766 static int
767 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
768 {
769 	struct ifvlan *ifv = ifp->if_softc;
770 	int unit = ifp->if_dunit;
771 
772 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
773 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
774 	if_free_type(ifp, IFT_ETHER);
775 	free(ifv, M_VLAN);
776 	ifc_free_unit(ifc, unit);
777 
778 	return (0);
779 }
780 
781 /*
782  * The ifp->if_init entry point for vlan(4) is a no-op.
783  */
784 static void
785 vlan_init(void *foo __unused)
786 {
787 }
788 
789 /*
790  * The if_start method for vlan(4) interface. It doesn't
791  * raises the IFF_DRV_OACTIVE flag, since it is called
792  * only from IFQ_HANDOFF() macro in ether_output_frame().
793  * If the interface queue is full, and vlan_start() is
794  * not called, the queue would never get emptied and
795  * interface would stall forever.
796  */
797 static void
798 vlan_start(struct ifnet *ifp)
799 {
800 	struct ifvlan *ifv;
801 	struct ifnet *p;
802 	struct mbuf *m;
803 	int error;
804 
805 	ifv = ifp->if_softc;
806 	p = PARENT(ifv);
807 
808 	for (;;) {
809 		IF_DEQUEUE(&ifp->if_snd, m);
810 		if (m == NULL)
811 			break;
812 		BPF_MTAP(ifp, m);
813 
814 		/*
815 		 * Do not run parent's if_start() if the parent is not up,
816 		 * or parent's driver will cause a system crash.
817 		 */
818 		if (!UP_AND_RUNNING(p)) {
819 			m_freem(m);
820 			ifp->if_collisions++;
821 			continue;
822 		}
823 
824 		/*
825 		 * Pad the frame to the minimum size allowed if told to.
826 		 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
827 		 * paragraph C.4.4.3.b.  It can help to work around buggy
828 		 * bridges that violate paragraph C.4.4.3.a from the same
829 		 * document, i.e., fail to pad short frames after untagging.
830 		 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
831 		 * untagging it will produce a 62-byte frame, which is a runt
832 		 * and requires padding.  There are VLAN-enabled network
833 		 * devices that just discard such runts instead or mishandle
834 		 * them somehow.
835 		 */
836 		if (soft_pad) {
837 			static char pad[8];	/* just zeros */
838 			int n;
839 
840 			for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
841 			     n > 0; n -= sizeof(pad))
842 				if (!m_append(m, min(n, sizeof(pad)), pad))
843 					break;
844 
845 			if (n > 0) {
846 				if_printf(ifp, "cannot pad short frame\n");
847 				ifp->if_oerrors++;
848 				m_freem(m);
849 				continue;
850 			}
851 		}
852 
853 		/*
854 		 * If underlying interface can do VLAN tag insertion itself,
855 		 * just pass the packet along. However, we need some way to
856 		 * tell the interface where the packet came from so that it
857 		 * knows how to find the VLAN tag to use, so we attach a
858 		 * packet tag that holds it.
859 		 */
860 		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
861 			m->m_pkthdr.ether_vtag = ifv->ifv_tag;
862 			m->m_flags |= M_VLANTAG;
863 		} else {
864 			m = ether_vlanencap(m, ifv->ifv_tag);
865 			if (m == NULL) {
866 				if_printf(ifp,
867 				    "unable to prepend VLAN header\n");
868 				ifp->if_oerrors++;
869 				continue;
870 			}
871 		}
872 
873 		/*
874 		 * Send it, precisely as ether_output() would have.
875 		 * We are already running at splimp.
876 		 */
877 		error = (p->if_transmit)(p, m);
878 		if (!error)
879 			ifp->if_opackets++;
880 		else
881 			ifp->if_oerrors++;
882 	}
883 }
884 
885 static void
886 vlan_input(struct ifnet *ifp, struct mbuf *m)
887 {
888 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
889 	struct ifvlan *ifv;
890 	uint16_t tag;
891 
892 	KASSERT(trunk != NULL, ("%s: no trunk", __func__));
893 
894 	if (m->m_flags & M_VLANTAG) {
895 		/*
896 		 * Packet is tagged, but m contains a normal
897 		 * Ethernet frame; the tag is stored out-of-band.
898 		 */
899 		tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
900 		m->m_flags &= ~M_VLANTAG;
901 	} else {
902 		struct ether_vlan_header *evl;
903 
904 		/*
905 		 * Packet is tagged in-band as specified by 802.1q.
906 		 */
907 		switch (ifp->if_type) {
908 		case IFT_ETHER:
909 			if (m->m_len < sizeof(*evl) &&
910 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
911 				if_printf(ifp, "cannot pullup VLAN header\n");
912 				return;
913 			}
914 			evl = mtod(m, struct ether_vlan_header *);
915 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
916 
917 			/*
918 			 * Remove the 802.1q header by copying the Ethernet
919 			 * addresses over it and adjusting the beginning of
920 			 * the data in the mbuf.  The encapsulated Ethernet
921 			 * type field is already in place.
922 			 */
923 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
924 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
925 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
926 			break;
927 
928 		default:
929 #ifdef INVARIANTS
930 			panic("%s: %s has unsupported if_type %u",
931 			      __func__, ifp->if_xname, ifp->if_type);
932 #endif
933 			m_freem(m);
934 			ifp->if_noproto++;
935 			return;
936 		}
937 	}
938 
939 	TRUNK_RLOCK(trunk);
940 #ifdef VLAN_ARRAY
941 	ifv = trunk->vlans[tag];
942 #else
943 	ifv = vlan_gethash(trunk, tag);
944 #endif
945 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
946 		TRUNK_RUNLOCK(trunk);
947 		m_freem(m);
948 		ifp->if_noproto++;
949 		return;
950 	}
951 	TRUNK_RUNLOCK(trunk);
952 
953 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
954 	ifv->ifv_ifp->if_ipackets++;
955 
956 	/* Pass it back through the parent's input routine. */
957 	(*ifp->if_input)(ifv->ifv_ifp, m);
958 }
959 
960 static int
961 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
962 {
963 	struct ifvlantrunk *trunk;
964 	struct ifnet *ifp;
965 	int error = 0;
966 
967 	/* VID numbers 0x0 and 0xFFF are reserved */
968 	if (tag == 0 || tag == 0xFFF)
969 		return (EINVAL);
970 	if (p->if_type != IFT_ETHER)
971 		return (EPROTONOSUPPORT);
972 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
973 		return (EPROTONOSUPPORT);
974 	if (ifv->ifv_trunk)
975 		return (EBUSY);
976 
977 	if (p->if_vlantrunk == NULL) {
978 		trunk = malloc(sizeof(struct ifvlantrunk),
979 		    M_VLAN, M_WAITOK | M_ZERO);
980 #ifndef VLAN_ARRAY
981 		vlan_inithash(trunk);
982 #endif
983 		VLAN_LOCK();
984 		if (p->if_vlantrunk != NULL) {
985 			/* A race that that is very unlikely to be hit. */
986 #ifndef VLAN_ARRAY
987 			vlan_freehash(trunk);
988 #endif
989 			free(trunk, M_VLAN);
990 			goto exists;
991 		}
992 		TRUNK_LOCK_INIT(trunk);
993 		TRUNK_LOCK(trunk);
994 		p->if_vlantrunk = trunk;
995 		trunk->parent = p;
996 	} else {
997 		VLAN_LOCK();
998 exists:
999 		trunk = p->if_vlantrunk;
1000 		TRUNK_LOCK(trunk);
1001 	}
1002 
1003 	ifv->ifv_tag = tag;	/* must set this before vlan_inshash() */
1004 #ifdef VLAN_ARRAY
1005 	if (trunk->vlans[tag] != NULL) {
1006 		error = EEXIST;
1007 		goto done;
1008 	}
1009 	trunk->vlans[tag] = ifv;
1010 	trunk->refcnt++;
1011 #else
1012 	error = vlan_inshash(trunk, ifv);
1013 	if (error)
1014 		goto done;
1015 #endif
1016 	ifv->ifv_proto = ETHERTYPE_VLAN;
1017 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1018 	ifv->ifv_mintu = ETHERMIN;
1019 	ifv->ifv_pflags = 0;
1020 
1021 	/*
1022 	 * If the parent supports the VLAN_MTU capability,
1023 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1024 	 * use it.
1025 	 */
1026 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1027 		/*
1028 		 * No need to fudge the MTU since the parent can
1029 		 * handle extended frames.
1030 		 */
1031 		ifv->ifv_mtufudge = 0;
1032 	} else {
1033 		/*
1034 		 * Fudge the MTU by the encapsulation size.  This
1035 		 * makes us incompatible with strictly compliant
1036 		 * 802.1Q implementations, but allows us to use
1037 		 * the feature with other NetBSD implementations,
1038 		 * which might still be useful.
1039 		 */
1040 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1041 	}
1042 
1043 	ifv->ifv_trunk = trunk;
1044 	ifp = ifv->ifv_ifp;
1045 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1046 	ifp->if_baudrate = p->if_baudrate;
1047 	/*
1048 	 * Copy only a selected subset of flags from the parent.
1049 	 * Other flags are none of our business.
1050 	 */
1051 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1052 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
1053 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1054 #undef VLAN_COPY_FLAGS
1055 
1056 	ifp->if_link_state = p->if_link_state;
1057 
1058 	vlan_capabilities(ifv);
1059 
1060 	/*
1061 	 * Set up our ``Ethernet address'' to reflect the underlying
1062 	 * physical interface's.
1063 	 */
1064 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1065 
1066 	/*
1067 	 * Configure multicast addresses that may already be
1068 	 * joined on the vlan device.
1069 	 */
1070 	(void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1071 
1072 	/* We are ready for operation now. */
1073 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1074 done:
1075 	TRUNK_UNLOCK(trunk);
1076 	if (error == 0)
1077 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
1078 	VLAN_UNLOCK();
1079 
1080 	return (error);
1081 }
1082 
1083 static int
1084 vlan_unconfig(struct ifnet *ifp)
1085 {
1086 	int ret;
1087 
1088 	VLAN_LOCK();
1089 	ret = vlan_unconfig_locked(ifp);
1090 	VLAN_UNLOCK();
1091 	return (ret);
1092 }
1093 
1094 static int
1095 vlan_unconfig_locked(struct ifnet *ifp)
1096 {
1097 	struct ifvlantrunk *trunk;
1098 	struct vlan_mc_entry *mc;
1099 	struct ifvlan *ifv;
1100 	struct ifnet  *parent;
1101 	int error;
1102 
1103 	VLAN_LOCK_ASSERT();
1104 
1105 	ifv = ifp->if_softc;
1106 	trunk = ifv->ifv_trunk;
1107 	parent = NULL;
1108 
1109 	if (trunk != NULL) {
1110 		struct sockaddr_dl sdl;
1111 
1112 		TRUNK_LOCK(trunk);
1113 		parent = trunk->parent;
1114 
1115 		/*
1116 		 * Since the interface is being unconfigured, we need to
1117 		 * empty the list of multicast groups that we may have joined
1118 		 * while we were alive from the parent's list.
1119 		 */
1120 		bzero((char *)&sdl, sizeof(sdl));
1121 		sdl.sdl_len = sizeof(sdl);
1122 		sdl.sdl_family = AF_LINK;
1123 		sdl.sdl_index = parent->if_index;
1124 		sdl.sdl_type = IFT_ETHER;
1125 		sdl.sdl_alen = ETHER_ADDR_LEN;
1126 
1127 		while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1128 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1129 			    ETHER_ADDR_LEN);
1130 			error = if_delmulti(parent, (struct sockaddr *)&sdl);
1131 			if (error)
1132 				return (error);
1133 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1134 			free(mc, M_VLAN);
1135 		}
1136 
1137 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1138 #ifdef VLAN_ARRAY
1139 		trunk->vlans[ifv->ifv_tag] = NULL;
1140 		trunk->refcnt--;
1141 #else
1142 		vlan_remhash(trunk, ifv);
1143 #endif
1144 		ifv->ifv_trunk = NULL;
1145 
1146 		/*
1147 		 * Check if we were the last.
1148 		 */
1149 		if (trunk->refcnt == 0) {
1150 			trunk->parent->if_vlantrunk = NULL;
1151 			/*
1152 			 * XXXGL: If some ithread has already entered
1153 			 * vlan_input() and is now blocked on the trunk
1154 			 * lock, then it should preempt us right after
1155 			 * unlock and finish its work. Then we will acquire
1156 			 * lock again in trunk_destroy().
1157 			 */
1158 			TRUNK_UNLOCK(trunk);
1159 			trunk_destroy(trunk);
1160 		} else
1161 			TRUNK_UNLOCK(trunk);
1162 	}
1163 
1164 	/* Disconnect from parent. */
1165 	if (ifv->ifv_pflags)
1166 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1167 	ifp->if_mtu = ETHERMTU;
1168 	ifp->if_link_state = LINK_STATE_UNKNOWN;
1169 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1170 
1171 	/*
1172 	 * Only dispatch an event if vlan was
1173 	 * attached, otherwise there is nothing
1174 	 * to cleanup anyway.
1175 	 */
1176 	if (parent != NULL)
1177 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
1178 
1179 	return (0);
1180 }
1181 
1182 /* Handle a reference counted flag that should be set on the parent as well */
1183 static int
1184 vlan_setflag(struct ifnet *ifp, int flag, int status,
1185 	     int (*func)(struct ifnet *, int))
1186 {
1187 	struct ifvlan *ifv;
1188 	int error;
1189 
1190 	/* XXX VLAN_LOCK_ASSERT(); */
1191 
1192 	ifv = ifp->if_softc;
1193 	status = status ? (ifp->if_flags & flag) : 0;
1194 	/* Now "status" contains the flag value or 0 */
1195 
1196 	/*
1197 	 * See if recorded parent's status is different from what
1198 	 * we want it to be.  If it is, flip it.  We record parent's
1199 	 * status in ifv_pflags so that we won't clear parent's flag
1200 	 * we haven't set.  In fact, we don't clear or set parent's
1201 	 * flags directly, but get or release references to them.
1202 	 * That's why we can be sure that recorded flags still are
1203 	 * in accord with actual parent's flags.
1204 	 */
1205 	if (status != (ifv->ifv_pflags & flag)) {
1206 		error = (*func)(PARENT(ifv), status);
1207 		if (error)
1208 			return (error);
1209 		ifv->ifv_pflags &= ~flag;
1210 		ifv->ifv_pflags |= status;
1211 	}
1212 	return (0);
1213 }
1214 
1215 /*
1216  * Handle IFF_* flags that require certain changes on the parent:
1217  * if "status" is true, update parent's flags respective to our if_flags;
1218  * if "status" is false, forcedly clear the flags set on parent.
1219  */
1220 static int
1221 vlan_setflags(struct ifnet *ifp, int status)
1222 {
1223 	int error, i;
1224 
1225 	for (i = 0; vlan_pflags[i].flag; i++) {
1226 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
1227 				     status, vlan_pflags[i].func);
1228 		if (error)
1229 			return (error);
1230 	}
1231 	return (0);
1232 }
1233 
1234 /* Inform all vlans that their parent has changed link state */
1235 static void
1236 vlan_link_state(struct ifnet *ifp)
1237 {
1238 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1239 	struct ifvlan *ifv;
1240 	int i;
1241 
1242 	TRUNK_LOCK(trunk);
1243 #ifdef VLAN_ARRAY
1244 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1245 		if (trunk->vlans[i] != NULL) {
1246 			ifv = trunk->vlans[i];
1247 #else
1248 	for (i = 0; i < (1 << trunk->hwidth); i++)
1249 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1250 #endif
1251 			ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1252 			if_link_state_change(ifv->ifv_ifp,
1253 			    trunk->parent->if_link_state);
1254 		}
1255 	TRUNK_UNLOCK(trunk);
1256 }
1257 
1258 static void
1259 vlan_capabilities(struct ifvlan *ifv)
1260 {
1261 	struct ifnet *p = PARENT(ifv);
1262 	struct ifnet *ifp = ifv->ifv_ifp;
1263 
1264 	TRUNK_LOCK_ASSERT(TRUNK(ifv));
1265 
1266 	/*
1267 	 * If the parent interface can do checksum offloading
1268 	 * on VLANs, then propagate its hardware-assisted
1269 	 * checksumming flags. Also assert that checksum
1270 	 * offloading requires hardware VLAN tagging.
1271 	 */
1272 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1273 		ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1274 
1275 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1276 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1277 		ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1278 		ifp->if_hwassist = p->if_hwassist;
1279 	} else {
1280 		ifp->if_capenable = 0;
1281 		ifp->if_hwassist = 0;
1282 	}
1283 }
1284 
1285 static void
1286 vlan_trunk_capabilities(struct ifnet *ifp)
1287 {
1288 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1289 	struct ifvlan *ifv;
1290 	int i;
1291 
1292 	TRUNK_LOCK(trunk);
1293 #ifdef VLAN_ARRAY
1294 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1295 		if (trunk->vlans[i] != NULL) {
1296 			ifv = trunk->vlans[i];
1297 #else
1298 	for (i = 0; i < (1 << trunk->hwidth); i++) {
1299 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1300 #endif
1301 			vlan_capabilities(ifv);
1302 	}
1303 	TRUNK_UNLOCK(trunk);
1304 }
1305 
1306 static int
1307 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1308 {
1309 	struct ifnet *p;
1310 	struct ifreq *ifr;
1311 	struct ifvlan *ifv;
1312 	struct vlanreq vlr;
1313 	int error = 0;
1314 
1315 	ifr = (struct ifreq *)data;
1316 	ifv = ifp->if_softc;
1317 
1318 	switch (cmd) {
1319 	case SIOCGIFMEDIA:
1320 		VLAN_LOCK();
1321 		if (TRUNK(ifv) != NULL) {
1322 			error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
1323 					SIOCGIFMEDIA, data);
1324 			VLAN_UNLOCK();
1325 			/* Limit the result to the parent's current config. */
1326 			if (error == 0) {
1327 				struct ifmediareq *ifmr;
1328 
1329 				ifmr = (struct ifmediareq *)data;
1330 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1331 					ifmr->ifm_count = 1;
1332 					error = copyout(&ifmr->ifm_current,
1333 						ifmr->ifm_ulist,
1334 						sizeof(int));
1335 				}
1336 			}
1337 		} else {
1338 			VLAN_UNLOCK();
1339 			error = EINVAL;
1340 		}
1341 		break;
1342 
1343 	case SIOCSIFMEDIA:
1344 		error = EINVAL;
1345 		break;
1346 
1347 	case SIOCSIFMTU:
1348 		/*
1349 		 * Set the interface MTU.
1350 		 */
1351 		VLAN_LOCK();
1352 		if (TRUNK(ifv) != NULL) {
1353 			if (ifr->ifr_mtu >
1354 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1355 			    ifr->ifr_mtu <
1356 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
1357 				error = EINVAL;
1358 			else
1359 				ifp->if_mtu = ifr->ifr_mtu;
1360 		} else
1361 			error = EINVAL;
1362 		VLAN_UNLOCK();
1363 		break;
1364 
1365 	case SIOCSETVLAN:
1366 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1367 		if (error)
1368 			break;
1369 		if (vlr.vlr_parent[0] == '\0') {
1370 			vlan_unconfig(ifp);
1371 			break;
1372 		}
1373 		p = ifunit(vlr.vlr_parent);
1374 		if (p == NULL) {
1375 			error = ENOENT;
1376 			break;
1377 		}
1378 		/*
1379 		 * Don't let the caller set up a VLAN tag with
1380 		 * anything except VLID bits.
1381 		 */
1382 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1383 			error = EINVAL;
1384 			break;
1385 		}
1386 		error = vlan_config(ifv, p, vlr.vlr_tag);
1387 		if (error)
1388 			break;
1389 
1390 		/* Update flags on the parent, if necessary. */
1391 		vlan_setflags(ifp, 1);
1392 		break;
1393 
1394 	case SIOCGETVLAN:
1395 		bzero(&vlr, sizeof(vlr));
1396 		VLAN_LOCK();
1397 		if (TRUNK(ifv) != NULL) {
1398 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1399 			    sizeof(vlr.vlr_parent));
1400 			vlr.vlr_tag = ifv->ifv_tag;
1401 		}
1402 		VLAN_UNLOCK();
1403 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1404 		break;
1405 
1406 	case SIOCSIFFLAGS:
1407 		/*
1408 		 * We should propagate selected flags to the parent,
1409 		 * e.g., promiscuous mode.
1410 		 */
1411 		if (TRUNK(ifv) != NULL)
1412 			error = vlan_setflags(ifp, 1);
1413 		break;
1414 
1415 	case SIOCADDMULTI:
1416 	case SIOCDELMULTI:
1417 		/*
1418 		 * If we don't have a parent, just remember the membership for
1419 		 * when we do.
1420 		 */
1421 		if (TRUNK(ifv) != NULL)
1422 			error = vlan_setmulti(ifp);
1423 		break;
1424 
1425 	default:
1426 		error = ether_ioctl(ifp, cmd, data);
1427 	}
1428 
1429 	return (error);
1430 }
1431