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