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