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