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