if_vlan.c revision 167484
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 167484 2007-03-12 12:42:14Z 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		if (bootverbose)
541			printf("vlan: initialized, using "
542#ifdef VLAN_ARRAY
543			       "full-size arrays"
544#else
545			       "hash tables with chaining"
546#endif
547
548			       "\n");
549		break;
550	case MOD_UNLOAD:
551		if_clone_detach(&vlan_cloner);
552		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
553		vlan_input_p = NULL;
554		vlan_link_state_p = NULL;
555		vlan_trunk_cap_p = NULL;
556		VLAN_LOCK_DESTROY();
557		if (bootverbose)
558			printf("vlan: unloaded\n");
559		break;
560	default:
561		return (EOPNOTSUPP);
562	}
563	return (0);
564}
565
566static moduledata_t vlan_mod = {
567	"if_vlan",
568	vlan_modevent,
569	0
570};
571
572DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
573MODULE_VERSION(if_vlan, 3);
574MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
575
576static struct ifnet *
577vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
578{
579	const char *cp;
580	struct ifnet *ifp;
581	int t = 0;
582
583	/* Check for <etherif>.<vlan> style interface names. */
584	IFNET_RLOCK();
585	TAILQ_FOREACH(ifp, &ifnet, if_link) {
586		if (ifp->if_type != IFT_ETHER)
587			continue;
588		if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
589			continue;
590		cp = name + strlen(ifp->if_xname);
591		if (*cp != '.')
592			continue;
593		for(; *cp != '\0'; cp++) {
594			if (*cp < '0' || *cp > '9')
595				continue;
596			t = (t * 10) + (*cp - '0');
597		}
598		if (tag != NULL)
599			*tag = t;
600		break;
601	}
602	IFNET_RUNLOCK();
603
604	return (ifp);
605}
606
607static int
608vlan_clone_match(struct if_clone *ifc, const char *name)
609{
610	const char *cp;
611
612	if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
613		return (1);
614
615	if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
616		return (0);
617	for (cp = name + 4; *cp != '\0'; cp++) {
618		if (*cp < '0' || *cp > '9')
619			return (0);
620	}
621
622	return (1);
623}
624
625static int
626vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
627{
628	char *dp;
629	int wildcard;
630	int unit;
631	int error;
632	int tag;
633	int ethertag;
634	struct ifvlan *ifv;
635	struct ifnet *ifp;
636	struct ifnet *p;
637	struct vlanreq vlr;
638	static const u_char eaddr[6];	/* 00:00:00:00:00:00 */
639
640	/*
641	 * There are 3 (ugh) ways to specify the cloned device:
642	 * o pass a parameter block with the clone request.
643	 * o specify parameters in the text of the clone device name
644	 * o specify no parameters and get an unattached device that
645	 *   must be configured separately.
646	 * The first technique is preferred; the latter two are
647	 * supported for backwards compatibilty.
648	 */
649	if (params) {
650		error = copyin(params, &vlr, sizeof(vlr));
651		if (error)
652			return error;
653		p = ifunit(vlr.vlr_parent);
654		if (p == NULL)
655			return ENXIO;
656		/*
657		 * Don't let the caller set up a VLAN tag with
658		 * anything except VLID bits.
659		 */
660		if (vlr.vlr_tag & ~EVL_VLID_MASK)
661			return (EINVAL);
662		error = ifc_name2unit(name, &unit);
663		if (error != 0)
664			return (error);
665
666		ethertag = 1;
667		tag = vlr.vlr_tag;
668		wildcard = (unit < 0);
669	} else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
670		ethertag = 1;
671		unit = -1;
672		wildcard = 0;
673
674		/*
675		 * Don't let the caller set up a VLAN tag with
676		 * anything except VLID bits.
677		 */
678		if (tag & ~EVL_VLID_MASK)
679			return (EINVAL);
680	} else {
681		ethertag = 0;
682
683		error = ifc_name2unit(name, &unit);
684		if (error != 0)
685			return (error);
686
687		wildcard = (unit < 0);
688	}
689
690	error = ifc_alloc_unit(ifc, &unit);
691	if (error != 0)
692		return (error);
693
694	/* In the wildcard case, we need to update the name. */
695	if (wildcard) {
696		for (dp = name; *dp != '\0'; dp++);
697		if (snprintf(dp, len - (dp-name), "%d", unit) >
698		    len - (dp-name) - 1) {
699			panic("%s: interface name too long", __func__);
700		}
701	}
702
703	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
704	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
705	if (ifp == NULL) {
706		ifc_free_unit(ifc, unit);
707		free(ifv, M_VLAN);
708		return (ENOSPC);
709	}
710	SLIST_INIT(&ifv->vlan_mc_listhead);
711
712	ifp->if_softc = ifv;
713	/*
714	 * Set the name manually rather than using if_initname because
715	 * we don't conform to the default naming convention for interfaces.
716	 */
717	strlcpy(ifp->if_xname, name, IFNAMSIZ);
718	ifp->if_dname = ifc->ifc_name;
719	ifp->if_dunit = unit;
720	/* NB: flags are not set here */
721	ifp->if_linkmib = &ifv->ifv_mib;
722	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
723	/* NB: mtu is not set here */
724
725	ifp->if_init = vlan_init;
726	ifp->if_start = vlan_start;
727	ifp->if_ioctl = vlan_ioctl;
728	ifp->if_snd.ifq_maxlen = ifqmaxlen;
729	ifp->if_flags = VLAN_IFFLAGS;
730	ether_ifattach(ifp, eaddr);
731	/* Now undo some of the damage... */
732	ifp->if_baudrate = 0;
733	ifp->if_type = IFT_L2VLAN;
734	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
735
736	if (ethertag) {
737		error = vlan_config(ifv, p, tag);
738		if (error != 0) {
739			/*
740			 * Since we've partialy failed, we need to back
741			 * out all the way, otherwise userland could get
742			 * confused.  Thus, we destroy the interface.
743			 */
744			ether_ifdetach(ifp);
745			vlan_unconfig(ifp);
746			if_free_type(ifp, IFT_ETHER);
747			free(ifv, M_VLAN);
748
749			return (error);
750		}
751
752		/* Update flags on the parent, if necessary. */
753		vlan_setflags(ifp, 1);
754	}
755
756	return (0);
757}
758
759static int
760vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
761{
762	struct ifvlan *ifv = ifp->if_softc;
763	int unit = ifp->if_dunit;
764
765	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
766	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
767	if_free_type(ifp, IFT_ETHER);
768	free(ifv, M_VLAN);
769	ifc_free_unit(ifc, unit);
770
771	return (0);
772}
773
774/*
775 * The ifp->if_init entry point for vlan(4) is a no-op.
776 */
777static void
778vlan_init(void *foo __unused)
779{
780}
781
782/*
783 * The if_start method for vlan(4) interface. It doesn't
784 * raises the IFF_DRV_OACTIVE flag, since it is called
785 * only from IFQ_HANDOFF() macro in ether_output_frame().
786 * If the interface queue is full, and vlan_start() is
787 * not called, the queue would never get emptied and
788 * interface would stall forever.
789 */
790static void
791vlan_start(struct ifnet *ifp)
792{
793	struct ifvlan *ifv;
794	struct ifnet *p;
795	struct mbuf *m;
796	int error;
797
798	ifv = ifp->if_softc;
799	p = PARENT(ifv);
800
801	for (;;) {
802		IF_DEQUEUE(&ifp->if_snd, m);
803		if (m == NULL)
804			break;
805		BPF_MTAP(ifp, m);
806
807		/*
808		 * Do not run parent's if_start() if the parent is not up,
809		 * or parent's driver will cause a system crash.
810		 */
811		if (!UP_AND_RUNNING(p)) {
812			m_freem(m);
813			ifp->if_collisions++;
814			continue;
815		}
816
817		/*
818		 * Pad the frame to the minimum size allowed if told to.
819		 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
820		 * paragraph C.4.4.3.b.  It can help to work around buggy
821		 * bridges that violate paragraph C.4.4.3.a from the same
822		 * document, i.e., fail to pad short frames after untagging.
823		 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
824		 * untagging it will produce a 62-byte frame, which is a runt
825		 * and requires padding.  There are VLAN-enabled network
826		 * devices that just discard such runts instead or mishandle
827		 * them somehow.
828		 */
829		if (soft_pad) {
830			static char pad[8];	/* just zeros */
831			int n;
832
833			for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
834			     n > 0; n -= sizeof(pad))
835				if (!m_append(m, min(n, sizeof(pad)), pad))
836					break;
837
838			if (n > 0) {
839				if_printf(ifp, "cannot pad short frame\n");
840				ifp->if_oerrors++;
841				m_freem(m);
842				continue;
843			}
844		}
845
846		/*
847		 * If underlying interface can do VLAN tag insertion itself,
848		 * just pass the packet along. However, we need some way to
849		 * tell the interface where the packet came from so that it
850		 * knows how to find the VLAN tag to use, so we attach a
851		 * packet tag that holds it.
852		 */
853		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
854			m->m_pkthdr.ether_vtag = ifv->ifv_tag;
855			m->m_flags |= M_VLANTAG;
856		} else {
857			struct ether_vlan_header *evl;
858
859			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
860			if (m == NULL) {
861				if_printf(ifp,
862				    "unable to prepend VLAN header\n");
863				ifp->if_oerrors++;
864				continue;
865			}
866			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
867
868			if (m->m_len < sizeof(*evl)) {
869				m = m_pullup(m, sizeof(*evl));
870				if (m == NULL) {
871					if_printf(ifp,
872					    "cannot pullup VLAN header\n");
873					ifp->if_oerrors++;
874					continue;
875				}
876			}
877
878			/*
879			 * Transform the Ethernet header into an Ethernet header
880			 * with 802.1Q encapsulation.
881			 */
882			evl = mtod(m, struct ether_vlan_header *);
883			bcopy((char *)evl + ifv->ifv_encaplen,
884			      (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
885			evl->evl_encap_proto = htons(ifv->ifv_proto);
886			evl->evl_tag = htons(ifv->ifv_tag);
887#ifdef DEBUG
888			printf("%s: %*D\n", __func__, (int)sizeof(*evl),
889			    (unsigned char *)evl, ":");
890#endif
891		}
892
893		/*
894		 * Send it, precisely as ether_output() would have.
895		 * We are already running at splimp.
896		 */
897		IFQ_HANDOFF(p, m, error);
898		if (!error)
899			ifp->if_opackets++;
900		else
901			ifp->if_oerrors++;
902	}
903}
904
905static void
906vlan_input(struct ifnet *ifp, struct mbuf *m)
907{
908	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
909	struct ifvlan *ifv;
910	uint16_t tag;
911
912	KASSERT(trunk != NULL, ("%s: no trunk", __func__));
913
914	if (m->m_flags & M_VLANTAG) {
915		/*
916		 * Packet is tagged, but m contains a normal
917		 * Ethernet frame; the tag is stored out-of-band.
918		 */
919		tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
920		m->m_flags &= ~M_VLANTAG;
921	} else {
922		struct ether_vlan_header *evl;
923
924		/*
925		 * Packet is tagged in-band as specified by 802.1q.
926		 */
927		switch (ifp->if_type) {
928		case IFT_ETHER:
929			if (m->m_len < sizeof(*evl) &&
930			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
931				if_printf(ifp, "cannot pullup VLAN header\n");
932				return;
933			}
934			evl = mtod(m, struct ether_vlan_header *);
935			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
936
937			/*
938			 * Remove the 802.1q header by copying the Ethernet
939			 * addresses over it and adjusting the beginning of
940			 * the data in the mbuf.  The encapsulated Ethernet
941			 * type field is already in place.
942			 */
943			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
944			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
945			m_adj(m, ETHER_VLAN_ENCAP_LEN);
946			break;
947
948		default:
949#ifdef INVARIANTS
950			panic("%s: %s has unsupported if_type %u",
951			      __func__, ifp->if_xname, ifp->if_type);
952#endif
953			m_freem(m);
954			ifp->if_noproto++;
955			return;
956		}
957	}
958
959	TRUNK_RLOCK(trunk);
960#ifdef VLAN_ARRAY
961	ifv = trunk->vlans[tag];
962#else
963	ifv = vlan_gethash(trunk, tag);
964#endif
965	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
966		TRUNK_RUNLOCK(trunk);
967		m_freem(m);
968		ifp->if_noproto++;
969		return;
970	}
971	TRUNK_RUNLOCK(trunk);
972
973	m->m_pkthdr.rcvif = ifv->ifv_ifp;
974	ifv->ifv_ifp->if_ipackets++;
975
976	/* Pass it back through the parent's input routine. */
977	(*ifp->if_input)(ifv->ifv_ifp, m);
978}
979
980static int
981vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
982{
983	struct ifvlantrunk *trunk;
984	struct ifnet *ifp;
985	int error = 0;
986
987	/* VID numbers 0x0 and 0xFFF are reserved */
988	if (tag == 0 || tag == 0xFFF)
989		return (EINVAL);
990	if (p->if_type != IFT_ETHER)
991		return (EPROTONOSUPPORT);
992	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
993		return (EPROTONOSUPPORT);
994	if (ifv->ifv_trunk)
995		return (EBUSY);
996
997	if (p->if_vlantrunk == NULL) {
998		trunk = malloc(sizeof(struct ifvlantrunk),
999		    M_VLAN, M_WAITOK | M_ZERO);
1000#ifndef VLAN_ARRAY
1001		vlan_inithash(trunk);
1002#endif
1003		VLAN_LOCK();
1004		if (p->if_vlantrunk != NULL) {
1005			/* A race that that is very unlikely to be hit. */
1006#ifndef VLAN_ARRAY
1007			vlan_freehash(trunk);
1008#endif
1009			free(trunk, M_VLAN);
1010			goto exists;
1011		}
1012		TRUNK_LOCK_INIT(trunk);
1013		TRUNK_LOCK(trunk);
1014		p->if_vlantrunk = trunk;
1015		trunk->parent = p;
1016	} else {
1017		VLAN_LOCK();
1018exists:
1019		trunk = p->if_vlantrunk;
1020		TRUNK_LOCK(trunk);
1021	}
1022
1023	ifv->ifv_tag = tag;	/* must set this before vlan_inshash() */
1024#ifdef VLAN_ARRAY
1025	if (trunk->vlans[tag] != NULL) {
1026		error = EEXIST;
1027		goto done;
1028	}
1029	trunk->vlans[tag] = ifv;
1030	trunk->refcnt++;
1031#else
1032	error = vlan_inshash(trunk, ifv);
1033	if (error)
1034		goto done;
1035#endif
1036	ifv->ifv_proto = ETHERTYPE_VLAN;
1037	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1038	ifv->ifv_mintu = ETHERMIN;
1039	ifv->ifv_pflags = 0;
1040
1041	/*
1042	 * If the parent supports the VLAN_MTU capability,
1043	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1044	 * use it.
1045	 */
1046	if (p->if_capenable & IFCAP_VLAN_MTU) {
1047		/*
1048		 * No need to fudge the MTU since the parent can
1049		 * handle extended frames.
1050		 */
1051		ifv->ifv_mtufudge = 0;
1052	} else {
1053		/*
1054		 * Fudge the MTU by the encapsulation size.  This
1055		 * makes us incompatible with strictly compliant
1056		 * 802.1Q implementations, but allows us to use
1057		 * the feature with other NetBSD implementations,
1058		 * which might still be useful.
1059		 */
1060		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1061	}
1062
1063	ifv->ifv_trunk = trunk;
1064	ifp = ifv->ifv_ifp;
1065	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
1066	ifp->if_baudrate = p->if_baudrate;
1067	/*
1068	 * Copy only a selected subset of flags from the parent.
1069	 * Other flags are none of our business.
1070	 */
1071#define VLAN_COPY_FLAGS (IFF_SIMPLEX)
1072	ifp->if_flags &= ~VLAN_COPY_FLAGS;
1073	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
1074#undef VLAN_COPY_FLAGS
1075
1076	ifp->if_link_state = p->if_link_state;
1077
1078	vlan_capabilities(ifv);
1079
1080	/*
1081	 * Set up our ``Ethernet address'' to reflect the underlying
1082	 * physical interface's.
1083	 */
1084	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
1085
1086	/*
1087	 * Configure multicast addresses that may already be
1088	 * joined on the vlan device.
1089	 */
1090	(void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
1091
1092	/* We are ready for operation now. */
1093	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1094done:
1095	TRUNK_UNLOCK(trunk);
1096	VLAN_UNLOCK();
1097
1098	return (error);
1099}
1100
1101static int
1102vlan_unconfig(struct ifnet *ifp)
1103{
1104	int ret;
1105
1106	VLAN_LOCK();
1107	ret = vlan_unconfig_locked(ifp);
1108	VLAN_UNLOCK();
1109	return (ret);
1110}
1111
1112static int
1113vlan_unconfig_locked(struct ifnet *ifp)
1114{
1115	struct ifvlantrunk *trunk;
1116	struct vlan_mc_entry *mc;
1117	struct ifvlan *ifv;
1118	int error;
1119
1120	VLAN_LOCK_ASSERT();
1121
1122	ifv = ifp->if_softc;
1123	trunk = ifv->ifv_trunk;
1124
1125	if (trunk) {
1126		struct sockaddr_dl sdl;
1127		struct ifnet *p = trunk->parent;
1128
1129		TRUNK_LOCK(trunk);
1130
1131		/*
1132		 * Since the interface is being unconfigured, we need to
1133		 * empty the list of multicast groups that we may have joined
1134		 * while we were alive from the parent's list.
1135		 */
1136		bzero((char *)&sdl, sizeof(sdl));
1137		sdl.sdl_len = sizeof(sdl);
1138		sdl.sdl_family = AF_LINK;
1139		sdl.sdl_index = p->if_index;
1140		sdl.sdl_type = IFT_ETHER;
1141		sdl.sdl_alen = ETHER_ADDR_LEN;
1142
1143		while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
1144			bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
1145			    ETHER_ADDR_LEN);
1146			error = if_delmulti(p, (struct sockaddr *)&sdl);
1147			if (error)
1148				return (error);
1149			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1150			free(mc, M_VLAN);
1151		}
1152
1153		vlan_setflags(ifp, 0); /* clear special flags on parent */
1154#ifdef VLAN_ARRAY
1155		trunk->vlans[ifv->ifv_tag] = NULL;
1156		trunk->refcnt--;
1157#else
1158		vlan_remhash(trunk, ifv);
1159#endif
1160		ifv->ifv_trunk = NULL;
1161
1162		/*
1163		 * Check if we were the last.
1164		 */
1165		if (trunk->refcnt == 0) {
1166			trunk->parent->if_vlantrunk = NULL;
1167			/*
1168			 * XXXGL: If some ithread has already entered
1169			 * vlan_input() and is now blocked on the trunk
1170			 * lock, then it should preempt us right after
1171			 * unlock and finish its work. Then we will acquire
1172			 * lock again in trunk_destroy().
1173			 */
1174			TRUNK_UNLOCK(trunk);
1175			trunk_destroy(trunk);
1176		} else
1177			TRUNK_UNLOCK(trunk);
1178	}
1179
1180	/* Disconnect from parent. */
1181	if (ifv->ifv_pflags)
1182		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1183	ifp->if_mtu = ETHERMTU;
1184	ifp->if_link_state = LINK_STATE_UNKNOWN;
1185	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1186
1187	return (0);
1188}
1189
1190/* Handle a reference counted flag that should be set on the parent as well */
1191static int
1192vlan_setflag(struct ifnet *ifp, int flag, int status,
1193	     int (*func)(struct ifnet *, int))
1194{
1195	struct ifvlan *ifv;
1196	int error;
1197
1198	/* XXX VLAN_LOCK_ASSERT(); */
1199
1200	ifv = ifp->if_softc;
1201	status = status ? (ifp->if_flags & flag) : 0;
1202	/* Now "status" contains the flag value or 0 */
1203
1204	/*
1205	 * See if recorded parent's status is different from what
1206	 * we want it to be.  If it is, flip it.  We record parent's
1207	 * status in ifv_pflags so that we won't clear parent's flag
1208	 * we haven't set.  In fact, we don't clear or set parent's
1209	 * flags directly, but get or release references to them.
1210	 * That's why we can be sure that recorded flags still are
1211	 * in accord with actual parent's flags.
1212	 */
1213	if (status != (ifv->ifv_pflags & flag)) {
1214		error = (*func)(PARENT(ifv), status);
1215		if (error)
1216			return (error);
1217		ifv->ifv_pflags &= ~flag;
1218		ifv->ifv_pflags |= status;
1219	}
1220	return (0);
1221}
1222
1223/*
1224 * Handle IFF_* flags that require certain changes on the parent:
1225 * if "status" is true, update parent's flags respective to our if_flags;
1226 * if "status" is false, forcedly clear the flags set on parent.
1227 */
1228static int
1229vlan_setflags(struct ifnet *ifp, int status)
1230{
1231	int error, i;
1232
1233	for (i = 0; vlan_pflags[i].flag; i++) {
1234		error = vlan_setflag(ifp, vlan_pflags[i].flag,
1235				     status, vlan_pflags[i].func);
1236		if (error)
1237			return (error);
1238	}
1239	return (0);
1240}
1241
1242/* Inform all vlans that their parent has changed link state */
1243static void
1244vlan_link_state(struct ifnet *ifp, int link)
1245{
1246	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1247	struct ifvlan *ifv;
1248	int i;
1249
1250	TRUNK_LOCK(trunk);
1251#ifdef VLAN_ARRAY
1252	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1253		if (trunk->vlans[i] != NULL) {
1254			ifv = trunk->vlans[i];
1255#else
1256	for (i = 0; i < (1 << trunk->hwidth); i++)
1257		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
1258#endif
1259			ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1260			if_link_state_change(ifv->ifv_ifp,
1261			    trunk->parent->if_link_state);
1262		}
1263	TRUNK_UNLOCK(trunk);
1264}
1265
1266static void
1267vlan_capabilities(struct ifvlan *ifv)
1268{
1269	struct ifnet *p = PARENT(ifv);
1270	struct ifnet *ifp = ifv->ifv_ifp;
1271
1272	TRUNK_LOCK_ASSERT(TRUNK(ifv));
1273
1274	/*
1275	 * If the parent interface can do checksum offloading
1276	 * on VLANs, then propagate its hardware-assisted
1277	 * checksumming flags. Also assert that checksum
1278	 * offloading requires hardware VLAN tagging.
1279	 */
1280	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1281		ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
1282
1283	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
1284	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1285		ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
1286		ifp->if_hwassist = p->if_hwassist;
1287	} else {
1288		ifp->if_capenable = 0;
1289		ifp->if_hwassist = 0;
1290	}
1291}
1292
1293static void
1294vlan_trunk_capabilities(struct ifnet *ifp)
1295{
1296	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1297	struct ifvlan *ifv;
1298	int i;
1299
1300	TRUNK_LOCK(trunk);
1301#ifdef VLAN_ARRAY
1302	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
1303		if (trunk->vlans[i] != NULL) {
1304			ifv = trunk->vlans[i];
1305#else
1306	for (i = 0; i < (1 << trunk->hwidth); i++) {
1307		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
1308#endif
1309			vlan_capabilities(ifv);
1310	}
1311	TRUNK_UNLOCK(trunk);
1312}
1313
1314static int
1315vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1316{
1317	struct ifaddr *ifa;
1318	struct ifnet *p;
1319	struct ifreq *ifr;
1320	struct ifvlan *ifv;
1321	struct vlanreq vlr;
1322	int error = 0;
1323
1324	ifr = (struct ifreq *)data;
1325	ifa = (struct ifaddr *)data;
1326	ifv = ifp->if_softc;
1327
1328	switch (cmd) {
1329	case SIOCSIFADDR:
1330		ifp->if_flags |= IFF_UP;
1331
1332		switch (ifa->ifa_addr->sa_family) {
1333#ifdef INET
1334		case AF_INET:
1335			arp_ifinit(ifv->ifv_ifp, ifa);
1336			break;
1337#endif
1338		default:
1339			break;
1340		}
1341		break;
1342
1343	case SIOCGIFADDR:
1344		{
1345			struct sockaddr *sa;
1346
1347			sa = (struct sockaddr *) &ifr->ifr_data;
1348			bcopy(IF_LLADDR(ifp), (caddr_t)sa->sa_data,
1349			    ETHER_ADDR_LEN);
1350		}
1351		break;
1352
1353	case SIOCGIFMEDIA:
1354		VLAN_LOCK();
1355		if (TRUNK(ifv) != NULL) {
1356			error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
1357					SIOCGIFMEDIA, data);
1358			VLAN_UNLOCK();
1359			/* Limit the result to the parent's current config. */
1360			if (error == 0) {
1361				struct ifmediareq *ifmr;
1362
1363				ifmr = (struct ifmediareq *)data;
1364				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1365					ifmr->ifm_count = 1;
1366					error = copyout(&ifmr->ifm_current,
1367						ifmr->ifm_ulist,
1368						sizeof(int));
1369				}
1370			}
1371		} else {
1372			VLAN_UNLOCK();
1373			error = EINVAL;
1374		}
1375		break;
1376
1377	case SIOCSIFMEDIA:
1378		error = EINVAL;
1379		break;
1380
1381	case SIOCSIFMTU:
1382		/*
1383		 * Set the interface MTU.
1384		 */
1385		VLAN_LOCK();
1386		if (TRUNK(ifv) != NULL) {
1387			if (ifr->ifr_mtu >
1388			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1389			    ifr->ifr_mtu <
1390			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
1391				error = EINVAL;
1392			else
1393				ifp->if_mtu = ifr->ifr_mtu;
1394		} else
1395			error = EINVAL;
1396		VLAN_UNLOCK();
1397		break;
1398
1399	case SIOCSETVLAN:
1400		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
1401		if (error)
1402			break;
1403		if (vlr.vlr_parent[0] == '\0') {
1404			vlan_unconfig(ifp);
1405			break;
1406		}
1407		p = ifunit(vlr.vlr_parent);
1408		if (p == 0) {
1409			error = ENOENT;
1410			break;
1411		}
1412		/*
1413		 * Don't let the caller set up a VLAN tag with
1414		 * anything except VLID bits.
1415		 */
1416		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1417			error = EINVAL;
1418			break;
1419		}
1420		error = vlan_config(ifv, p, vlr.vlr_tag);
1421		if (error)
1422			break;
1423
1424		/* Update flags on the parent, if necessary. */
1425		vlan_setflags(ifp, 1);
1426		break;
1427
1428	case SIOCGETVLAN:
1429		bzero(&vlr, sizeof(vlr));
1430		VLAN_LOCK();
1431		if (TRUNK(ifv) != NULL) {
1432			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
1433			    sizeof(vlr.vlr_parent));
1434			vlr.vlr_tag = ifv->ifv_tag;
1435		}
1436		VLAN_UNLOCK();
1437		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
1438		break;
1439
1440	case SIOCSIFFLAGS:
1441		/*
1442		 * We should propagate selected flags to the parent,
1443		 * e.g., promiscuous mode.
1444		 */
1445		if (TRUNK(ifv) != NULL)
1446			error = vlan_setflags(ifp, 1);
1447		break;
1448
1449	case SIOCADDMULTI:
1450	case SIOCDELMULTI:
1451		/*
1452		 * If we don't have a parent, just remember the membership for
1453		 * when we do.
1454		 */
1455		if (TRUNK(ifv) != NULL)
1456			error = vlan_setmulti(ifp);
1457		break;
1458
1459	default:
1460		error = EINVAL;
1461	}
1462
1463	return (error);
1464}
1465