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