xref: /freebsd/sys/net/if_vlan.c (revision 114c608c71849cf875852a393335470a39bbde5d)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32cc2df49SGarrett Wollman  *
42cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
52cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
62cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
72cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
82cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
92cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
102cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
112cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
122cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
132cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
142cc2df49SGarrett Wollman  * warranty.
152cc2df49SGarrett Wollman  *
162cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
172cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
182cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
192cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
202cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
232cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
242cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
252cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
262cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272cc2df49SGarrett Wollman  * SUCH DAMAGE.
282cc2df49SGarrett Wollman  *
29c3aac50fSPeter Wemm  * $FreeBSD$
302cc2df49SGarrett Wollman  */
312cc2df49SGarrett Wollman 
322cc2df49SGarrett Wollman /*
332cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
342cc2df49SGarrett Wollman  * Might be extended some day to also handle IEEE 802.1p priority
352cc2df49SGarrett Wollman  * tagging.  This is sort of sneaky in the implementation, since
362cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
372cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
382cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
3946a32e61SPhilippe Charnier  * ether_output() left on our output queue when it calls
402cc2df49SGarrett Wollman  * if_start(), rewrite them for use by the real outgoing interface,
412cc2df49SGarrett Wollman  * and ask it to send them.
422cc2df49SGarrett Wollman  */
432cc2df49SGarrett Wollman 
442cc2df49SGarrett Wollman #include "opt_inet.h"
4575ee267cSGleb Smirnoff #include "opt_vlan.h"
462cc2df49SGarrett Wollman 
472cc2df49SGarrett Wollman #include <sys/param.h>
482cc2df49SGarrett Wollman #include <sys/kernel.h>
4975ee267cSGleb Smirnoff #include <sys/lock.h>
50f731f104SBill Paul #include <sys/malloc.h>
512cc2df49SGarrett Wollman #include <sys/mbuf.h>
522b120974SPeter Wemm #include <sys/module.h>
5375ee267cSGleb Smirnoff #include <sys/rwlock.h>
54f731f104SBill Paul #include <sys/queue.h>
552cc2df49SGarrett Wollman #include <sys/socket.h>
562cc2df49SGarrett Wollman #include <sys/sockio.h>
572cc2df49SGarrett Wollman #include <sys/sysctl.h>
582cc2df49SGarrett Wollman #include <sys/systm.h>
592cc2df49SGarrett Wollman 
602cc2df49SGarrett Wollman #include <net/bpf.h>
612cc2df49SGarrett Wollman #include <net/ethernet.h>
622cc2df49SGarrett Wollman #include <net/if.h>
63f889d2efSBrooks Davis #include <net/if_clone.h>
642cc2df49SGarrett Wollman #include <net/if_arp.h>
652cc2df49SGarrett Wollman #include <net/if_dl.h>
662cc2df49SGarrett Wollman #include <net/if_types.h>
672cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
682cc2df49SGarrett Wollman 
692cc2df49SGarrett Wollman #ifdef INET
702cc2df49SGarrett Wollman #include <netinet/in.h>
712cc2df49SGarrett Wollman #include <netinet/if_ether.h>
722cc2df49SGarrett Wollman #endif
732cc2df49SGarrett Wollman 
749d4fe4b2SBrooks Davis #define VLANNAME	"vlan"
7575ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
7664a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
7775ee267cSGleb Smirnoff 
7875ee267cSGleb Smirnoff LIST_HEAD(ifvlanhead, ifvlan);
7975ee267cSGleb Smirnoff 
8075ee267cSGleb Smirnoff struct ifvlantrunk {
8175ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
8275ee267cSGleb Smirnoff 	struct	rwlock	rw;
8375ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
845cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
855cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
8675ee267cSGleb Smirnoff #else
8775ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
8875ee267cSGleb Smirnoff 	uint16_t	hmask;
8975ee267cSGleb Smirnoff 	uint16_t	hwidth;
9075ee267cSGleb Smirnoff #endif
9175ee267cSGleb Smirnoff 	int		refcnt;
9275ee267cSGleb Smirnoff 	LIST_ENTRY(ifvlantrunk) trunk_entry;
9375ee267cSGleb Smirnoff };
9475ee267cSGleb Smirnoff static LIST_HEAD(, ifvlantrunk) trunk_list;
959d4fe4b2SBrooks Davis 
96a3814acfSSam Leffler struct vlan_mc_entry {
97a3814acfSSam Leffler 	struct ether_addr		mc_addr;
98a3814acfSSam Leffler 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
99a3814acfSSam Leffler };
100a3814acfSSam Leffler 
101a3814acfSSam Leffler struct	ifvlan {
10275ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
103fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
10475ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
10575ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
1061cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
107a3814acfSSam Leffler 	struct	ifv_linkmib {
108a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
109a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
110a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
11175ee267cSGleb Smirnoff 		uint16_t ifvm_tag;	/* tag to apply on packets leaving if */
112a3814acfSSam Leffler 	}	ifv_mib;
113114c608cSYaroslav Tykhiy 	SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
114a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
115a3814acfSSam Leffler };
116a3814acfSSam Leffler #define	ifv_tag		ifv_mib.ifvm_tag
117a3814acfSSam Leffler #define	ifv_encaplen	ifv_mib.ifvm_encaplen
118a3814acfSSam Leffler #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
119a3814acfSSam Leffler #define	ifv_mintu	ifv_mib.ifvm_mintu
120a3814acfSSam Leffler 
12175ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
1221cf236fbSYaroslav Tykhiy static struct {
1231cf236fbSYaroslav Tykhiy 	int flag;
1241cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
1251cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
1261cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
1271cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
1281cf236fbSYaroslav Tykhiy 	{0, NULL}
1291cf236fbSYaroslav Tykhiy };
130a3814acfSSam Leffler 
1314a408dcbSBill Paul SYSCTL_DECL(_net_link);
132d2a75853SBill Fenner SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
1332cc2df49SGarrett Wollman SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
1342cc2df49SGarrett Wollman 
1355e17543aSBrooks Davis static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
1362cc2df49SGarrett Wollman 
1375cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
1385cb8c31aSYaroslav Tykhiy 
1394faedfe8SSam Leffler /*
14075ee267cSGleb Smirnoff  * We have a global mutex, that is used to serialize configuration
14175ee267cSGleb Smirnoff  * changes and isn't used in normal packet delivery.
14275ee267cSGleb Smirnoff  *
14375ee267cSGleb Smirnoff  * We also have a per-trunk rwlock, that is locked shared on packet
14475ee267cSGleb Smirnoff  * processing and exclusive when configuration is changed.
14575ee267cSGleb Smirnoff  *
14675ee267cSGleb Smirnoff  * The VLAN_ARRAY substitutes the dynamic hash with a static array
14775ee267cSGleb Smirnoff  * with 4096 entries. In theory this can give a boots in processing,
14875ee267cSGleb Smirnoff  * however on practice it does not. Probably this is because array
14975ee267cSGleb Smirnoff  * is too big to fit into CPU cache.
1504faedfe8SSam Leffler  */
1514faedfe8SSam Leffler static struct mtx ifv_mtx;
15275ee267cSGleb Smirnoff #define	VLAN_LOCK_INIT()	mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
1534faedfe8SSam Leffler #define	VLAN_LOCK_DESTROY()	mtx_destroy(&ifv_mtx)
1544faedfe8SSam Leffler #define	VLAN_LOCK_ASSERT()	mtx_assert(&ifv_mtx, MA_OWNED)
1554faedfe8SSam Leffler #define	VLAN_LOCK()		mtx_lock(&ifv_mtx)
1564faedfe8SSam Leffler #define	VLAN_UNLOCK()		mtx_unlock(&ifv_mtx)
15775ee267cSGleb Smirnoff #define	TRUNK_LOCK_INIT(trunk)	rw_init(&(trunk)->rw, VLANNAME)
15875ee267cSGleb Smirnoff #define	TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
15975ee267cSGleb Smirnoff #define	TRUNK_LOCK(trunk)	rw_wlock(&(trunk)->rw)
16075ee267cSGleb Smirnoff #define	TRUNK_UNLOCK(trunk)	rw_wunlock(&(trunk)->rw)
16175ee267cSGleb Smirnoff #define	TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
16275ee267cSGleb Smirnoff #define	TRUNK_RLOCK(trunk)	rw_rlock(&(trunk)->rw)
16375ee267cSGleb Smirnoff #define	TRUNK_RUNLOCK(trunk)	rw_runlock(&(trunk)->rw)
16475ee267cSGleb Smirnoff #define	TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
16575ee267cSGleb Smirnoff 
16675ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
16775ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
16875ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
16975ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
17075ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
17175ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
17275ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
17375ee267cSGleb Smirnoff 	uint16_t tag);
17475ee267cSGleb Smirnoff #endif
17575ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
1764faedfe8SSam Leffler 
1772cc2df49SGarrett Wollman static	void vlan_start(struct ifnet *ifp);
178114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
179a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
180cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
1811cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
1821cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
1831cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
184f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
185f731f104SBill Paul static	int vlan_unconfig(struct ifnet *ifp);
1865cb8c31aSYaroslav Tykhiy static	int vlan_unconfig_locked(struct ifnet *ifp);
18775ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
188127d7b2dSAndre Oppermann static	void vlan_link_state(struct ifnet *ifp, int link);
18975ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
19075ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
191f731f104SBill Paul 
192f889d2efSBrooks Davis static	struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
193f889d2efSBrooks Davis     const char *, int *);
194f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
195f889d2efSBrooks Davis static	int vlan_clone_create(struct if_clone *, char *, size_t);
196f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
197f889d2efSBrooks Davis 
1985cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
1995cb8c31aSYaroslav Tykhiy 
200c6e6ca3eSYaroslav Tykhiy static	struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
201c6e6ca3eSYaroslav Tykhiy     IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
2029d4fe4b2SBrooks Davis 
20375ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
20475ee267cSGleb Smirnoff #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
205114c608cSYaroslav Tykhiy 
20675ee267cSGleb Smirnoff static void
20775ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
20875ee267cSGleb Smirnoff {
20975ee267cSGleb Smirnoff 	int i, n;
21075ee267cSGleb Smirnoff 
21175ee267cSGleb Smirnoff 	/*
21275ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
21375ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
21475ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
21575ee267cSGleb Smirnoff 	 */
21675ee267cSGleb Smirnoff 
21775ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
21875ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
21975ee267cSGleb Smirnoff 
22075ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
22175ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
22275ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
22375ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
22475ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
22575ee267cSGleb Smirnoff 		LIST_INIT(&trunk->hash[i]);
22675ee267cSGleb Smirnoff }
22775ee267cSGleb Smirnoff 
22875ee267cSGleb Smirnoff static void
22975ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
23075ee267cSGleb Smirnoff {
23175ee267cSGleb Smirnoff #ifdef INVARIANTS
23275ee267cSGleb Smirnoff 	int i;
23375ee267cSGleb Smirnoff 
23475ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
23575ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
23675ee267cSGleb Smirnoff 		KASSERT(LIST_EMPTY(&trunk->hash[i]),
23775ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
23875ee267cSGleb Smirnoff #endif
23975ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
24075ee267cSGleb Smirnoff 	trunk->hash = NULL;
24175ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
24275ee267cSGleb Smirnoff }
24375ee267cSGleb Smirnoff 
24475ee267cSGleb Smirnoff static int
24575ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
24675ee267cSGleb Smirnoff {
24775ee267cSGleb Smirnoff 	int i, b;
24875ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
24975ee267cSGleb Smirnoff 
25075ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
25175ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
25275ee267cSGleb Smirnoff 
25375ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
25475ee267cSGleb Smirnoff 	i = HASH(ifv->ifv_tag, trunk->hmask);
25575ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
25675ee267cSGleb Smirnoff 		if (ifv->ifv_tag == ifv2->ifv_tag)
25775ee267cSGleb Smirnoff 			return (EEXIST);
25875ee267cSGleb Smirnoff 
25975ee267cSGleb Smirnoff 	/*
26075ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
26175ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
26275ee267cSGleb Smirnoff 	 * buckets/2.
26375ee267cSGleb Smirnoff 	 */
26475ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
26575ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
26675ee267cSGleb Smirnoff 		i = HASH(ifv->ifv_tag, trunk->hmask);
26775ee267cSGleb Smirnoff 	}
26875ee267cSGleb Smirnoff 	LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
26975ee267cSGleb Smirnoff 	trunk->refcnt++;
27075ee267cSGleb Smirnoff 
27175ee267cSGleb Smirnoff 	return (0);
27275ee267cSGleb Smirnoff }
27375ee267cSGleb Smirnoff 
27475ee267cSGleb Smirnoff static int
27575ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
27675ee267cSGleb Smirnoff {
27775ee267cSGleb Smirnoff 	int i, b;
27875ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
27975ee267cSGleb Smirnoff 
28075ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
28175ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
28275ee267cSGleb Smirnoff 
28375ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
28475ee267cSGleb Smirnoff 	i = HASH(ifv->ifv_tag, trunk->hmask);
28575ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
28675ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
28775ee267cSGleb Smirnoff 			trunk->refcnt--;
28875ee267cSGleb Smirnoff 			LIST_REMOVE(ifv2, ifv_list);
28975ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
29075ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
29175ee267cSGleb Smirnoff 			return (0);
29275ee267cSGleb Smirnoff 		}
29375ee267cSGleb Smirnoff 
29475ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
29575ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
29675ee267cSGleb Smirnoff }
29775ee267cSGleb Smirnoff 
29875ee267cSGleb Smirnoff /*
29975ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
30075ee267cSGleb Smirnoff  */
30175ee267cSGleb Smirnoff static void
30275ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
30375ee267cSGleb Smirnoff {
30475ee267cSGleb Smirnoff 
30575ee267cSGleb Smirnoff 	struct ifvlan *ifv;
30675ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
30775ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
30875ee267cSGleb Smirnoff 
30975ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
31075ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
31175ee267cSGleb Smirnoff 
31275ee267cSGleb Smirnoff 	if (howmuch == 0) {
31375ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
31475ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
31575ee267cSGleb Smirnoff 		return;
31675ee267cSGleb Smirnoff 	}
31775ee267cSGleb Smirnoff 
31875ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
31975ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
32075ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
32175ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
32275ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
32375ee267cSGleb Smirnoff 		return;
32475ee267cSGleb Smirnoff 
32575ee267cSGleb Smirnoff 	/* M_NOWAIT because we're called with trunk mutex held */
32675ee267cSGleb Smirnoff 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
32775ee267cSGleb Smirnoff 	if (hash2 == NULL) {
32875ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
32975ee267cSGleb Smirnoff 		    __func__);
33075ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
33175ee267cSGleb Smirnoff 	}
33275ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
33375ee267cSGleb Smirnoff 		LIST_INIT(&hash2[j]);
33475ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
33575ee267cSGleb Smirnoff 		while (!LIST_EMPTY(&trunk->hash[i])) {
33675ee267cSGleb Smirnoff 			ifv = LIST_FIRST(&trunk->hash[i]);
33775ee267cSGleb Smirnoff 			LIST_REMOVE(ifv, ifv_list);
33875ee267cSGleb Smirnoff 			j = HASH(ifv->ifv_tag, n2 - 1);
33975ee267cSGleb Smirnoff 			LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
34075ee267cSGleb Smirnoff 		}
34175ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
34275ee267cSGleb Smirnoff 	trunk->hash = hash2;
34375ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
34475ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
34575ee267cSGleb Smirnoff }
34675ee267cSGleb Smirnoff 
34775ee267cSGleb Smirnoff static __inline struct ifvlan *
34875ee267cSGleb Smirnoff vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
34975ee267cSGleb Smirnoff {
35075ee267cSGleb Smirnoff 	struct ifvlan *ifv;
35175ee267cSGleb Smirnoff 
35275ee267cSGleb Smirnoff 	TRUNK_LOCK_RASSERT(trunk);
35375ee267cSGleb Smirnoff 
35475ee267cSGleb Smirnoff 	LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
35575ee267cSGleb Smirnoff 		if (ifv->ifv_tag == tag)
35675ee267cSGleb Smirnoff 			return (ifv);
35775ee267cSGleb Smirnoff 	return (NULL);
35875ee267cSGleb Smirnoff }
35975ee267cSGleb Smirnoff 
36075ee267cSGleb Smirnoff #if 0
36175ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
36275ee267cSGleb Smirnoff static void
36375ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
36475ee267cSGleb Smirnoff {
36575ee267cSGleb Smirnoff 	int i;
36675ee267cSGleb Smirnoff 	struct ifvlan *ifv;
36775ee267cSGleb Smirnoff 
36875ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
36975ee267cSGleb Smirnoff 		printf("%d: ", i);
37075ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
37175ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
37275ee267cSGleb Smirnoff 		printf("\n");
37375ee267cSGleb Smirnoff 	}
37475ee267cSGleb Smirnoff }
37575ee267cSGleb Smirnoff #endif /* 0 */
37675ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
37775ee267cSGleb Smirnoff 
37875ee267cSGleb Smirnoff static void
37975ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
38075ee267cSGleb Smirnoff {
38175ee267cSGleb Smirnoff 	VLAN_LOCK_ASSERT();
38275ee267cSGleb Smirnoff 
38375ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
38475ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
38575ee267cSGleb Smirnoff 	vlan_freehash(trunk);
38675ee267cSGleb Smirnoff #endif
38775ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
38833499e2aSYaroslav Tykhiy 	LIST_REMOVE(trunk, trunk_entry);
38933499e2aSYaroslav Tykhiy 	TRUNK_UNLOCK(trunk);
39033499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
39175ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
39275ee267cSGleb Smirnoff }
39375ee267cSGleb Smirnoff 
394f731f104SBill Paul /*
395f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
396f731f104SBill Paul  * programming the multicast filter of the parent. This has the
397f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
398f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
399f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
400f731f104SBill Paul  * to avoid this: there really is only one physical interface.
40129c2dfbeSBruce M Simpson  *
40229c2dfbeSBruce M Simpson  * XXX: There is a possible race here if more than one thread is
40329c2dfbeSBruce M Simpson  *      modifying the multicast state of the vlan interface at the same time.
404f731f104SBill Paul  */
4052b120974SPeter Wemm static int
4062b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
407f731f104SBill Paul {
408f731f104SBill Paul 	struct ifnet		*ifp_p;
409f731f104SBill Paul 	struct ifmultiaddr	*ifma, *rifma = NULL;
410f731f104SBill Paul 	struct ifvlan		*sc;
411f731f104SBill Paul 	struct vlan_mc_entry	*mc = NULL;
412f731f104SBill Paul 	struct sockaddr_dl	sdl;
413f731f104SBill Paul 	int			error;
414f731f104SBill Paul 
41529c2dfbeSBruce M Simpson 	/*VLAN_LOCK_ASSERT();*/
41629c2dfbeSBruce M Simpson 
417f731f104SBill Paul 	/* Find the parent. */
418f731f104SBill Paul 	sc = ifp->if_softc;
41975ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
4201b2a4f7aSBill Fenner 
42115a66c21SBruce M Simpson 	bzero((char *)&sdl, sizeof(sdl));
42215a66c21SBruce M Simpson 	sdl.sdl_len = sizeof(sdl);
423f731f104SBill Paul 	sdl.sdl_family = AF_LINK;
42426e30963SBill Fenner 	sdl.sdl_index = ifp_p->if_index;
42526e30963SBill Fenner 	sdl.sdl_type = IFT_ETHER;
42624993214SYaroslav Tykhiy 	sdl.sdl_alen = ETHER_ADDR_LEN;
427f731f104SBill Paul 
428f731f104SBill Paul 	/* First, remove any existing filter entries. */
42922f29826SPoul-Henning Kamp 	while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
43022f29826SPoul-Henning Kamp 		mc = SLIST_FIRST(&sc->vlan_mc_listhead);
431f731f104SBill Paul 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
432f731f104SBill Paul 		error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
433f731f104SBill Paul 		if (error)
434f731f104SBill Paul 			return (error);
435f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
4369d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
437f731f104SBill Paul 	}
438f731f104SBill Paul 
439f731f104SBill Paul 	/* Now program new ones. */
4406817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
441f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
442f731f104SBill Paul 			continue;
44329c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
44429c2dfbeSBruce M Simpson 		if (mc == NULL)
44529c2dfbeSBruce M Simpson 			return (ENOMEM);
446f731f104SBill Paul 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
447f731f104SBill Paul 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
448f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
44926e30963SBill Fenner 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
45026e30963SBill Fenner 		    LLADDR(&sdl), ETHER_ADDR_LEN);
451f731f104SBill Paul 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
452f731f104SBill Paul 		if (error)
453f731f104SBill Paul 			return (error);
454f731f104SBill Paul 	}
455f731f104SBill Paul 
456f731f104SBill Paul 	return (0);
457f731f104SBill Paul }
4582cc2df49SGarrett Wollman 
459a3814acfSSam Leffler /*
4605cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
4615cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
4625cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
4635cb8c31aSYaroslav Tykhiy  * by ejecting its hot-plug card.
4645cb8c31aSYaroslav Tykhiy  */
4655cb8c31aSYaroslav Tykhiy static void
4665cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
4675cb8c31aSYaroslav Tykhiy {
4685cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
4695cb8c31aSYaroslav Tykhiy 	int i;
4705cb8c31aSYaroslav Tykhiy 
4715cb8c31aSYaroslav Tykhiy 	/*
4725cb8c31aSYaroslav Tykhiy 	 * Check if it's a trunk interface first of all
4735cb8c31aSYaroslav Tykhiy 	 * to avoid needless locking.
4745cb8c31aSYaroslav Tykhiy 	 */
4755cb8c31aSYaroslav Tykhiy 	if (ifp->if_vlantrunk == NULL)
4765cb8c31aSYaroslav Tykhiy 		return;
4775cb8c31aSYaroslav Tykhiy 
4785cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
4795cb8c31aSYaroslav Tykhiy 	/*
4805cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
4815cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
4825cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
4835cb8c31aSYaroslav Tykhiy 	 */
4845cb8c31aSYaroslav Tykhiy #ifdef VLAN_ARRAY
4855cb8c31aSYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
4865cb8c31aSYaroslav Tykhiy 		if ((ifv = ifp->if_vlantrunk->vlans[i])) {
4875cb8c31aSYaroslav Tykhiy 			vlan_unconfig_locked(ifv->ifv_ifp);
4885cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk == NULL)
4895cb8c31aSYaroslav Tykhiy 				break;
4905cb8c31aSYaroslav Tykhiy 		}
4915cb8c31aSYaroslav Tykhiy #else /* VLAN_ARRAY */
4925cb8c31aSYaroslav Tykhiy restart:
4935cb8c31aSYaroslav Tykhiy 	for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
4945cb8c31aSYaroslav Tykhiy 		if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
4955cb8c31aSYaroslav Tykhiy 			vlan_unconfig_locked(ifv->ifv_ifp);
4965cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk)
4975cb8c31aSYaroslav Tykhiy 				goto restart;	/* trunk->hwidth can change */
4985cb8c31aSYaroslav Tykhiy 			else
4995cb8c31aSYaroslav Tykhiy 				break;
5005cb8c31aSYaroslav Tykhiy 		}
5015cb8c31aSYaroslav Tykhiy #endif /* VLAN_ARRAY */
5025cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
5035cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
5045cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
5055cb8c31aSYaroslav Tykhiy }
5065cb8c31aSYaroslav Tykhiy 
5075cb8c31aSYaroslav Tykhiy /*
508a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
509a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
510a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
511a3814acfSSam Leffler  * set here.  Noone else in the system should be aware of this so
512a3814acfSSam Leffler  * we use an explicit reference here.
513a3814acfSSam Leffler  */
514a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
515a3814acfSSam Leffler 
516984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
517127d7b2dSAndre Oppermann extern	void (*vlan_link_state_p)(struct ifnet *, int);
518127d7b2dSAndre Oppermann 
5192b120974SPeter Wemm static int
5202b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
5212b120974SPeter Wemm {
5229d4fe4b2SBrooks Davis 
5232b120974SPeter Wemm 	switch (type) {
5242b120974SPeter Wemm 	case MOD_LOAD:
5255cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
5265cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
5275cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
5285cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
52975ee267cSGleb Smirnoff 		LIST_INIT(&trunk_list);
5304faedfe8SSam Leffler 		VLAN_LOCK_INIT();
5319d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
532127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
53375ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
5349d4fe4b2SBrooks Davis 		if_clone_attach(&vlan_cloner);
5352b120974SPeter Wemm 		break;
5362b120974SPeter Wemm 	case MOD_UNLOAD:
53775ee267cSGleb Smirnoff 	    {
53875ee267cSGleb Smirnoff 		struct ifvlantrunk *trunk, *trunk1;
53975ee267cSGleb Smirnoff 
5409d4fe4b2SBrooks Davis 		if_clone_detach(&vlan_cloner);
5415cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
5429d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
543127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
54475ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
54575ee267cSGleb Smirnoff 		VLAN_LOCK();
54675ee267cSGleb Smirnoff 		LIST_FOREACH_SAFE(trunk, &trunk_list, trunk_entry, trunk1)
54775ee267cSGleb Smirnoff 			trunk_destroy(trunk);
54875ee267cSGleb Smirnoff 		VLAN_UNLOCK();
5494faedfe8SSam Leffler 		VLAN_LOCK_DESTROY();
5509d4fe4b2SBrooks Davis 		break;
55175ee267cSGleb Smirnoff 	    }
5523e019deaSPoul-Henning Kamp 	default:
5533e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
5542b120974SPeter Wemm 	}
55515a66c21SBruce M Simpson 	return (0);
5562b120974SPeter Wemm }
5572b120974SPeter Wemm 
5582b120974SPeter Wemm static moduledata_t vlan_mod = {
5592b120974SPeter Wemm 	"if_vlan",
5602b120974SPeter Wemm 	vlan_modevent,
5612b120974SPeter Wemm 	0
5622b120974SPeter Wemm };
5632b120974SPeter Wemm 
5642b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
56511edc477SEd Maste MODULE_VERSION(if_vlan, 3);
566d35bcd3bSRuslan Ermilov MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
5672cc2df49SGarrett Wollman 
568f889d2efSBrooks Davis static struct ifnet *
569f889d2efSBrooks Davis vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
5709d4fe4b2SBrooks Davis {
571f889d2efSBrooks Davis 	const char *cp;
572f889d2efSBrooks Davis 	struct ifnet *ifp;
57315a66c21SBruce M Simpson 	int t = 0;
574f889d2efSBrooks Davis 
575f889d2efSBrooks Davis 	/* Check for <etherif>.<vlan> style interface names. */
576f889d2efSBrooks Davis 	IFNET_RLOCK();
577f889d2efSBrooks Davis 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
578f889d2efSBrooks Davis 		if (ifp->if_type != IFT_ETHER)
579f889d2efSBrooks Davis 			continue;
580f889d2efSBrooks Davis 		if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
581f889d2efSBrooks Davis 			continue;
582f889d2efSBrooks Davis 		cp = name + strlen(ifp->if_xname);
583f889d2efSBrooks Davis 		if (*cp != '.')
584f889d2efSBrooks Davis 			continue;
585f889d2efSBrooks Davis 		for(; *cp != '\0'; cp++) {
586f889d2efSBrooks Davis 			if (*cp < '0' || *cp > '9')
587f889d2efSBrooks Davis 				continue;
588f889d2efSBrooks Davis 			t = (t * 10) + (*cp - '0');
589f889d2efSBrooks Davis 		}
590f889d2efSBrooks Davis 		if (tag != NULL)
591f889d2efSBrooks Davis 			*tag = t;
592f889d2efSBrooks Davis 		break;
593f889d2efSBrooks Davis 	}
594f889d2efSBrooks Davis 	IFNET_RUNLOCK();
595f889d2efSBrooks Davis 
59615a66c21SBruce M Simpson 	return (ifp);
597f889d2efSBrooks Davis }
598f889d2efSBrooks Davis 
599f889d2efSBrooks Davis static int
600f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
601f889d2efSBrooks Davis {
602f889d2efSBrooks Davis 	const char *cp;
603f889d2efSBrooks Davis 
604f889d2efSBrooks Davis 	if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
605f889d2efSBrooks Davis 		return (1);
606f889d2efSBrooks Davis 
607f889d2efSBrooks Davis 	if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
608f889d2efSBrooks Davis 		return (0);
609f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
610f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
611f889d2efSBrooks Davis 			return (0);
612f889d2efSBrooks Davis 	}
613f889d2efSBrooks Davis 
614f889d2efSBrooks Davis 	return (1);
615f889d2efSBrooks Davis }
616f889d2efSBrooks Davis 
617f889d2efSBrooks Davis static int
618f889d2efSBrooks Davis vlan_clone_create(struct if_clone *ifc, char *name, size_t len)
619f889d2efSBrooks Davis {
620f889d2efSBrooks Davis 	char *dp;
621f889d2efSBrooks Davis 	int wildcard;
622f889d2efSBrooks Davis 	int unit;
623f889d2efSBrooks Davis 	int error;
624f889d2efSBrooks Davis 	int tag;
625f889d2efSBrooks Davis 	int ethertag;
6269d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
6279d4fe4b2SBrooks Davis 	struct ifnet *ifp;
628f889d2efSBrooks Davis 	struct ifnet *p;
629114c608cSYaroslav Tykhiy 	static const u_char eaddr[6];	/* 00:00:00:00:00:00 */
630f889d2efSBrooks Davis 
631f889d2efSBrooks Davis 	if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
632f889d2efSBrooks Davis 		ethertag = 1;
633f889d2efSBrooks Davis 		unit = -1;
634f889d2efSBrooks Davis 		wildcard = 0;
635f889d2efSBrooks Davis 
636f889d2efSBrooks Davis 		/*
637f889d2efSBrooks Davis 		 * Don't let the caller set up a VLAN tag with
638f889d2efSBrooks Davis 		 * anything except VLID bits.
639f889d2efSBrooks Davis 		 */
64015a66c21SBruce M Simpson 		if (tag & ~EVL_VLID_MASK)
641f889d2efSBrooks Davis 			return (EINVAL);
642f889d2efSBrooks Davis 	} else {
643f889d2efSBrooks Davis 		ethertag = 0;
644f889d2efSBrooks Davis 
645f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
646f889d2efSBrooks Davis 		if (error != 0)
647f889d2efSBrooks Davis 			return (error);
648f889d2efSBrooks Davis 
649f889d2efSBrooks Davis 		wildcard = (unit < 0);
650f889d2efSBrooks Davis 	}
651f889d2efSBrooks Davis 
652f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
653f889d2efSBrooks Davis 	if (error != 0)
654f889d2efSBrooks Davis 		return (error);
655f889d2efSBrooks Davis 
656f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
657f889d2efSBrooks Davis 	if (wildcard) {
658f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
659f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
660f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
661f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
662f889d2efSBrooks Davis 		}
663f889d2efSBrooks Davis 	}
6649d4fe4b2SBrooks Davis 
665a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
666fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
667fc74a9f9SBrooks Davis 	if (ifp == NULL) {
668fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
669fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
670fc74a9f9SBrooks Davis 		return (ENOSPC);
671fc74a9f9SBrooks Davis 	}
6729d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
6739d4fe4b2SBrooks Davis 
6749d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
675f889d2efSBrooks Davis 	/*
676cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
677f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
678f889d2efSBrooks Davis 	 */
679f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
680f889d2efSBrooks Davis 	ifp->if_dname = ifc->ifc_name;
681f889d2efSBrooks Davis 	ifp->if_dunit = unit;
6829d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
6839d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
68415a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
6859d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
6869d4fe4b2SBrooks Davis 
687114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
6889d4fe4b2SBrooks Davis 	ifp->if_start = vlan_start;
6899d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
6909d4fe4b2SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
69164a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
692fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
6939d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
694211f625aSBill Fenner 	ifp->if_baudrate = 0;
695a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
696a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
6979d4fe4b2SBrooks Davis 
698f889d2efSBrooks Davis 	if (ethertag) {
69975ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, tag);
700f889d2efSBrooks Davis 		if (error != 0) {
701f889d2efSBrooks Davis 			/*
702f889d2efSBrooks Davis 			 * Since we've partialy failed, we need to back
703f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
704f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
705f889d2efSBrooks Davis 			 */
706f889d2efSBrooks Davis 			vlan_unconfig(ifp);
707f889d2efSBrooks Davis 			ether_ifdetach(ifp);
708fc74a9f9SBrooks Davis 			if_free_type(ifp, IFT_ETHER);
709f889d2efSBrooks Davis 			free(ifv, M_VLAN);
710f889d2efSBrooks Davis 
711f889d2efSBrooks Davis 			return (error);
712f889d2efSBrooks Davis 		}
71313f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
714f889d2efSBrooks Davis 
7151cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
7161cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
717f889d2efSBrooks Davis 	}
718f889d2efSBrooks Davis 
7199d4fe4b2SBrooks Davis 	return (0);
7209d4fe4b2SBrooks Davis }
7219d4fe4b2SBrooks Davis 
722f889d2efSBrooks Davis static int
723f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
7249d4fe4b2SBrooks Davis {
7259d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
726114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
727b4e9f837SBrooks Davis 
7289d4fe4b2SBrooks Davis 	vlan_unconfig(ifp);
7299d4fe4b2SBrooks Davis 
730a3814acfSSam Leffler 	ether_ifdetach(ifp);
731f3447eb4SBrooks Davis 	if_free_type(ifp, IFT_ETHER);
7329d4fe4b2SBrooks Davis 
7339d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
734f889d2efSBrooks Davis 
735b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
736b4e9f837SBrooks Davis 
737f889d2efSBrooks Davis 	return (0);
7389d4fe4b2SBrooks Davis }
7399d4fe4b2SBrooks Davis 
74015a66c21SBruce M Simpson /*
74115a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
74215a66c21SBruce M Simpson  */
7432cc2df49SGarrett Wollman static void
744114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
7452cc2df49SGarrett Wollman {
7462cc2df49SGarrett Wollman }
7472cc2df49SGarrett Wollman 
7486d3a3ab7SGleb Smirnoff /*
7496d3a3ab7SGleb Smirnoff  * The if_start method for vlan(4) interface. It doesn't
7506d3a3ab7SGleb Smirnoff  * raises the IFF_DRV_OACTIVE flag, since it is called
7516d3a3ab7SGleb Smirnoff  * only from IFQ_HANDOFF() macro in ether_output_frame().
7526d3a3ab7SGleb Smirnoff  * If the interface queue is full, and vlan_start() is
7536d3a3ab7SGleb Smirnoff  * not called, the queue would never get emptied and
7546d3a3ab7SGleb Smirnoff  * interface would stall forever.
7556d3a3ab7SGleb Smirnoff  */
7562cc2df49SGarrett Wollman static void
7572cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp)
7582cc2df49SGarrett Wollman {
7592cc2df49SGarrett Wollman 	struct ifvlan *ifv;
7602cc2df49SGarrett Wollman 	struct ifnet *p;
7615326b23cSMatthew N. Dodd 	struct mbuf *m;
762affc907dSMax Laier 	int error;
7632cc2df49SGarrett Wollman 
7642cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
76575ee267cSGleb Smirnoff 	p = PARENT(ifv);
7662cc2df49SGarrett Wollman 
7672cc2df49SGarrett Wollman 	for (;;) {
7682cc2df49SGarrett Wollman 		IF_DEQUEUE(&ifp->if_snd, m);
7692cc2df49SGarrett Wollman 		if (m == 0)
7702cc2df49SGarrett Wollman 			break;
771a3814acfSSam Leffler 		BPF_MTAP(ifp, m);
7722cc2df49SGarrett Wollman 
773f731f104SBill Paul 		/*
77424993214SYaroslav Tykhiy 		 * Do not run parent's if_start() if the parent is not up,
77524993214SYaroslav Tykhiy 		 * or parent's driver will cause a system crash.
77624993214SYaroslav Tykhiy 		 */
77713f4c340SRobert Watson 		if (!((p->if_flags & IFF_UP) &&
77813f4c340SRobert Watson 		    (p->if_drv_flags & IFF_DRV_RUNNING))) {
77924993214SYaroslav Tykhiy 			m_freem(m);
780a3814acfSSam Leffler 			ifp->if_collisions++;
78124993214SYaroslav Tykhiy 			continue;
78224993214SYaroslav Tykhiy 		}
78324993214SYaroslav Tykhiy 
78424993214SYaroslav Tykhiy 		/*
785a3814acfSSam Leffler 		 * If underlying interface can do VLAN tag insertion itself,
786a3814acfSSam Leffler 		 * just pass the packet along. However, we need some way to
787a3814acfSSam Leffler 		 * tell the interface where the packet came from so that it
788a3814acfSSam Leffler 		 * knows how to find the VLAN tag to use, so we attach a
789a3814acfSSam Leffler 		 * packet tag that holds it.
790f731f104SBill Paul 		 */
791b08347a0SYaroslav Tykhiy 		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
79275ee267cSGleb Smirnoff 			struct m_tag *mtag = (struct m_tag *)
79375ee267cSGleb Smirnoff 			    uma_zalloc(zone_mtag_vlan, M_NOWAIT);
794a3814acfSSam Leffler 			if (mtag == NULL) {
795a3814acfSSam Leffler 				ifp->if_oerrors++;
796a3814acfSSam Leffler 				m_freem(m);
797a3814acfSSam Leffler 				continue;
798a3814acfSSam Leffler 			}
799eefbcf0eSYaroslav Tykhiy 			VLAN_TAG_VALUE(mtag) = ifv->ifv_tag;
800a3814acfSSam Leffler 			m_tag_prepend(m, mtag);
801ba26134bSGleb Smirnoff 			m->m_flags |= M_VLANTAG;
802f731f104SBill Paul 		} else {
80375ee267cSGleb Smirnoff 			struct ether_vlan_header *evl;
80475ee267cSGleb Smirnoff 
805a163d034SWarner Losh 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
8064af90a4dSMatthew N. Dodd 			if (m == NULL) {
8076cbd3e99SYaroslav Tykhiy 				if_printf(ifp,
8086cbd3e99SYaroslav Tykhiy 				    "unable to prepend VLAN header\n");
80926f9b263SRuslan Ermilov 				ifp->if_oerrors++;
8102cc2df49SGarrett Wollman 				continue;
8114af90a4dSMatthew N. Dodd 			}
8122cc2df49SGarrett Wollman 			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
8132cc2df49SGarrett Wollman 
814a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl)) {
815a3814acfSSam Leffler 				m = m_pullup(m, sizeof(*evl));
8165326b23cSMatthew N. Dodd 				if (m == NULL) {
817a3814acfSSam Leffler 					if_printf(ifp,
8186cbd3e99SYaroslav Tykhiy 					    "cannot pullup VLAN header\n");
81926f9b263SRuslan Ermilov 					ifp->if_oerrors++;
8204af90a4dSMatthew N. Dodd 					continue;
8214af90a4dSMatthew N. Dodd 				}
822a3814acfSSam Leffler 			}
8234af90a4dSMatthew N. Dodd 
8242cc2df49SGarrett Wollman 			/*
8252cc2df49SGarrett Wollman 			 * Transform the Ethernet header into an Ethernet header
8262cc2df49SGarrett Wollman 			 * with 802.1Q encapsulation.
8272cc2df49SGarrett Wollman 			 */
828a3814acfSSam Leffler 			bcopy(mtod(m, char *) + ifv->ifv_encaplen,
829797f247bSMatthew N. Dodd 			      mtod(m, char *), ETHER_HDR_LEN);
8302cc2df49SGarrett Wollman 			evl = mtod(m, struct ether_vlan_header *);
8312cc2df49SGarrett Wollman 			evl->evl_proto = evl->evl_encap_proto;
8329d4fe4b2SBrooks Davis 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
8332cc2df49SGarrett Wollman 			evl->evl_tag = htons(ifv->ifv_tag);
834f731f104SBill Paul #ifdef DEBUG
83583908c65SRuslan Ermilov 			printf("%s: %*D\n", __func__, (int)sizeof(*evl),
83683908c65SRuslan Ermilov 			    (unsigned char *)evl, ":");
837f731f104SBill Paul #endif
838f731f104SBill Paul 		}
8392cc2df49SGarrett Wollman 
8402cc2df49SGarrett Wollman 		/*
8412cc2df49SGarrett Wollman 		 * Send it, precisely as ether_output() would have.
8422cc2df49SGarrett Wollman 		 * We are already running at splimp.
8432cc2df49SGarrett Wollman 		 */
844affc907dSMax Laier 		IFQ_HANDOFF(p, m, error);
845affc907dSMax Laier 		if (!error)
846f731f104SBill Paul 			ifp->if_opackets++;
847df5e1987SJonathan Lemon 		else
848df5e1987SJonathan Lemon 			ifp->if_oerrors++;
8492cc2df49SGarrett Wollman 	}
850f731f104SBill Paul }
851f731f104SBill Paul 
852a3814acfSSam Leffler static void
853a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
854f731f104SBill Paul {
85575ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
856f731f104SBill Paul 	struct ifvlan *ifv;
857a3814acfSSam Leffler 	struct m_tag *mtag;
85875ee267cSGleb Smirnoff 	uint16_t tag;
85975ee267cSGleb Smirnoff 
86075ee267cSGleb Smirnoff 	KASSERT(trunk != NULL, ("%s: no trunk", __func__));
861a3814acfSSam Leffler 
862f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
863a3814acfSSam Leffler 		/*
86414e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
865a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
866a3814acfSSam Leffler 		 */
867f4ec4126SYaroslav Tykhiy 		mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
868f4ec4126SYaroslav Tykhiy 		KASSERT(mtag != NULL,
869f4ec4126SYaroslav Tykhiy 			("%s: M_VLANTAG without m_tag", __func__));
87026f9b263SRuslan Ermilov 		tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
871a3814acfSSam Leffler 		m_tag_delete(m, mtag);
8726ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
873a3814acfSSam Leffler 	} else {
87475ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
87575ee267cSGleb Smirnoff 
87614e98256SYaroslav Tykhiy 		/*
87714e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
87814e98256SYaroslav Tykhiy 		 */
879f4ec4126SYaroslav Tykhiy 		mtag = NULL;
880a3814acfSSam Leffler 		switch (ifp->if_type) {
881a3814acfSSam Leffler 		case IFT_ETHER:
882a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
883a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
884a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
885a3814acfSSam Leffler 				return;
886a3814acfSSam Leffler 			}
887a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
888a3814acfSSam Leffler 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
889ffdd61c3SYaroslav Tykhiy 				("%s: bad encapsulation protocol (%u)",
890ffdd61c3SYaroslav Tykhiy 				 __func__, ntohs(evl->evl_encap_proto)));
891a3814acfSSam Leffler 
892fb88a3e0SBill Paul 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
893f731f104SBill Paul 
8947a46ec8fSBrooks Davis 			/*
895a3814acfSSam Leffler 			 * Restore the original ethertype.  We'll remove
896a3814acfSSam Leffler 			 * the encapsulation after we've found the vlan
897a3814acfSSam Leffler 			 * interface corresponding to the tag.
8987a46ec8fSBrooks Davis 			 */
899a3814acfSSam Leffler 			evl->evl_encap_proto = evl->evl_proto;
900a3814acfSSam Leffler 			break;
901a3814acfSSam Leffler 		default:
90275ee267cSGleb Smirnoff 			tag = (uint16_t) -1;
903ffdd61c3SYaroslav Tykhiy #ifdef INVARIANTS
904ffdd61c3SYaroslav Tykhiy 			panic("%s: unsupported if_type (%u)",
905ffdd61c3SYaroslav Tykhiy 			      __func__, ifp->if_type);
906a3814acfSSam Leffler #endif
907a3814acfSSam Leffler 			break;
908a3814acfSSam Leffler 		}
9097a46ec8fSBrooks Davis 	}
9107a46ec8fSBrooks Davis 
91115ed2fa1SYaroslav Tykhiy 	TRUNK_RLOCK(trunk);
91275ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
91375ee267cSGleb Smirnoff 	ifv = trunk->vlans[tag];
91475ee267cSGleb Smirnoff #else
91575ee267cSGleb Smirnoff 	ifv = vlan_gethash(trunk, tag);
91615ed2fa1SYaroslav Tykhiy #endif
91775ee267cSGleb Smirnoff 	if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
91875ee267cSGleb Smirnoff 		TRUNK_RUNLOCK(trunk);
91975ee267cSGleb Smirnoff 		m_freem(m);
92075ee267cSGleb Smirnoff 		ifp->if_noproto++;
92175ee267cSGleb Smirnoff 		return;
92275ee267cSGleb Smirnoff 	}
92375ee267cSGleb Smirnoff 	TRUNK_RUNLOCK(trunk);
924f731f104SBill Paul 
925a3814acfSSam Leffler 	if (mtag == NULL) {
926f731f104SBill Paul 		/*
927a3814acfSSam Leffler 		 * Packet had an in-line encapsulation header;
928a3814acfSSam Leffler 		 * remove it.  The original header has already
929a3814acfSSam Leffler 		 * been fixed up above.
930f731f104SBill Paul 		 */
931a3814acfSSam Leffler 		bcopy(mtod(m, caddr_t),
932a3814acfSSam Leffler 		      mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
933797f247bSMatthew N. Dodd 		      ETHER_HDR_LEN);
934a3814acfSSam Leffler 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
935a3814acfSSam Leffler 	}
936a3814acfSSam Leffler 
937fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
938fc74a9f9SBrooks Davis 	ifv->ifv_ifp->if_ipackets++;
9392cc2df49SGarrett Wollman 
940a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
941fc74a9f9SBrooks Davis 	(*ifp->if_input)(ifv->ifv_ifp, m);
9422cc2df49SGarrett Wollman }
9432cc2df49SGarrett Wollman 
9442cc2df49SGarrett Wollman static int
94575ee267cSGleb Smirnoff vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
9462cc2df49SGarrett Wollman {
94775ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
9481cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
94975ee267cSGleb Smirnoff 	int error = 0;
9502cc2df49SGarrett Wollman 
95175ee267cSGleb Smirnoff 	/* VID numbers 0x0 and 0xFFF are reserved */
95275ee267cSGleb Smirnoff 	if (tag == 0 || tag == 0xFFF)
95375ee267cSGleb Smirnoff 		return (EINVAL);
9541cf236fbSYaroslav Tykhiy 	if (p->if_type != IFT_ETHER)
95515a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
95664a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
95764a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
95875ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
95915a66c21SBruce M Simpson 		return (EBUSY);
9602cc2df49SGarrett Wollman 
96175ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
96275ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
96375ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
96475ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
96575ee267cSGleb Smirnoff 		vlan_inithash(trunk);
96675ee267cSGleb Smirnoff #endif
96705a2398fSGleb Smirnoff 		VLAN_LOCK();
96805a2398fSGleb Smirnoff 		if (p->if_vlantrunk != NULL) {
96905a2398fSGleb Smirnoff 			/* A race that that is very unlikely to be hit. */
97005a2398fSGleb Smirnoff #ifndef VLAN_ARRAY
97105a2398fSGleb Smirnoff 			vlan_freehash(trunk);
97205a2398fSGleb Smirnoff #endif
97305a2398fSGleb Smirnoff 			free(trunk, M_VLAN);
97405a2398fSGleb Smirnoff 			goto exists;
97505a2398fSGleb Smirnoff 		}
97675ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
97775ee267cSGleb Smirnoff 		LIST_INSERT_HEAD(&trunk_list, trunk, trunk_entry);
97875ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
97975ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
98075ee267cSGleb Smirnoff 		trunk->parent = p;
98175ee267cSGleb Smirnoff 	} else {
98275ee267cSGleb Smirnoff 		VLAN_LOCK();
98375ee267cSGleb Smirnoff exists:
98475ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
98575ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
98675ee267cSGleb Smirnoff 	}
98775ee267cSGleb Smirnoff 
98815ed2fa1SYaroslav Tykhiy 	ifv->ifv_tag = tag;	/* must set this before vlan_inshash() */
98975ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
99015ed2fa1SYaroslav Tykhiy 	if (trunk->vlans[tag] != NULL) {
99175ee267cSGleb Smirnoff 		error = EEXIST;
99215ed2fa1SYaroslav Tykhiy 		goto done;
99315ed2fa1SYaroslav Tykhiy 	}
99415ed2fa1SYaroslav Tykhiy 	trunk->vlans[tag] = ifv;
99515ed2fa1SYaroslav Tykhiy 	trunk->refcnt++;
99675ee267cSGleb Smirnoff #else
99775ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
99875ee267cSGleb Smirnoff 	if (error)
99975ee267cSGleb Smirnoff 		goto done;
100015ed2fa1SYaroslav Tykhiy #endif
1001a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1002a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
10031cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1004a3814acfSSam Leffler 
1005a3814acfSSam Leffler 	/*
1006a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1007a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1008656acce4SYaroslav Tykhiy 	 * use it.
1009a3814acfSSam Leffler 	 */
1010656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1011656acce4SYaroslav Tykhiy 		/*
1012656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1013656acce4SYaroslav Tykhiy 		 * handle extended frames.
1014656acce4SYaroslav Tykhiy 		 */
1015a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1016656acce4SYaroslav Tykhiy 	} else {
1017a3814acfSSam Leffler 		/*
1018a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1019a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1020a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1021a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1022a3814acfSSam Leffler 		 * which might still be useful.
1023a3814acfSSam Leffler 		 */
1024a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1025a3814acfSSam Leffler 	}
1026a3814acfSSam Leffler 
102775ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
10281cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
10291cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
103075ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
10312cc2df49SGarrett Wollman 	/*
103224993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
103324993214SYaroslav Tykhiy 	 * Other flags are none of our business.
10342cc2df49SGarrett Wollman 	 */
103564a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
10361cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
10371cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
10381cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
10391cf236fbSYaroslav Tykhiy 
10401cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
10412cc2df49SGarrett Wollman 
104275ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1043a3814acfSSam Leffler 
1044a3814acfSSam Leffler 	/*
10452cc2df49SGarrett Wollman 	 * Set up our ``Ethernet address'' to reflect the underlying
10462cc2df49SGarrett Wollman 	 * physical interface's.
10472cc2df49SGarrett Wollman 	 */
1048d09ed26fSRuslan Ermilov 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
10491b2a4f7aSBill Fenner 
10501b2a4f7aSBill Fenner 	/*
10511b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
10521b2a4f7aSBill Fenner 	 * joined on the vlan device.
10531b2a4f7aSBill Fenner 	 */
10541cf236fbSYaroslav Tykhiy 	(void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
105575ee267cSGleb Smirnoff done:
105675ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
105775ee267cSGleb Smirnoff 	VLAN_UNLOCK();
105875ee267cSGleb Smirnoff 
105975ee267cSGleb Smirnoff 	return (error);
10602cc2df49SGarrett Wollman }
10612cc2df49SGarrett Wollman 
10622cc2df49SGarrett Wollman static int
1063f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1064f731f104SBill Paul {
10655cb8c31aSYaroslav Tykhiy 	int ret;
10665cb8c31aSYaroslav Tykhiy 
10675cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
10685cb8c31aSYaroslav Tykhiy 	ret = vlan_unconfig_locked(ifp);
10695cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
10705cb8c31aSYaroslav Tykhiy 	return (ret);
10715cb8c31aSYaroslav Tykhiy }
10725cb8c31aSYaroslav Tykhiy 
10735cb8c31aSYaroslav Tykhiy static int
10745cb8c31aSYaroslav Tykhiy vlan_unconfig_locked(struct ifnet *ifp)
10755cb8c31aSYaroslav Tykhiy {
107675ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1077f731f104SBill Paul 	struct vlan_mc_entry *mc;
1078f731f104SBill Paul 	struct ifvlan *ifv;
1079f731f104SBill Paul 	int error;
1080f731f104SBill Paul 
10815cb8c31aSYaroslav Tykhiy 	VLAN_LOCK_ASSERT();
10824faedfe8SSam Leffler 
1083f731f104SBill Paul 	ifv = ifp->if_softc;
108475ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
1085f731f104SBill Paul 
108675ee267cSGleb Smirnoff 	if (trunk) {
10871b2a4f7aSBill Fenner 		struct sockaddr_dl sdl;
108875ee267cSGleb Smirnoff 		struct ifnet *p = trunk->parent;
108975ee267cSGleb Smirnoff 
109075ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
10911b2a4f7aSBill Fenner 
1092f731f104SBill Paul 		/*
1093f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1094f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
10951b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1096f731f104SBill Paul 		 */
109715a66c21SBruce M Simpson 		bzero((char *)&sdl, sizeof(sdl));
109815a66c21SBruce M Simpson 		sdl.sdl_len = sizeof(sdl);
1099f731f104SBill Paul 		sdl.sdl_family = AF_LINK;
11001b2a4f7aSBill Fenner 		sdl.sdl_index = p->if_index;
11011b2a4f7aSBill Fenner 		sdl.sdl_type = IFT_ETHER;
11021b2a4f7aSBill Fenner 		sdl.sdl_alen = ETHER_ADDR_LEN;
11031b2a4f7aSBill Fenner 
11041b2a4f7aSBill Fenner 		while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
110522f29826SPoul-Henning Kamp 			mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
110615a66c21SBruce M Simpson 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
110715a66c21SBruce M Simpson 			    ETHER_ADDR_LEN);
1108f731f104SBill Paul 			error = if_delmulti(p, (struct sockaddr *)&sdl);
1109f731f104SBill Paul 			if (error)
1110f731f104SBill Paul 				return (error);
1111f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
11129d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
1113f731f104SBill Paul 		}
1114a3814acfSSam Leffler 
11151cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
111615ed2fa1SYaroslav Tykhiy #ifdef VLAN_ARRAY
111715ed2fa1SYaroslav Tykhiy 		trunk->vlans[ifv->ifv_tag] = NULL;
111815ed2fa1SYaroslav Tykhiy 		trunk->refcnt--;
111915ed2fa1SYaroslav Tykhiy #else
112075ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
112175ee267cSGleb Smirnoff #endif
112275ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
112375ee267cSGleb Smirnoff 
112475ee267cSGleb Smirnoff 		/*
112575ee267cSGleb Smirnoff 		 * Check if we were the last.
112675ee267cSGleb Smirnoff 		 */
112775ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
112815ed2fa1SYaroslav Tykhiy 			trunk->parent->if_vlantrunk = NULL;
112975ee267cSGleb Smirnoff 			/*
113075ee267cSGleb Smirnoff 			 * XXXGL: If some ithread has already entered
113175ee267cSGleb Smirnoff 			 * vlan_input() and is now blocked on the trunk
113275ee267cSGleb Smirnoff 			 * lock, then it should preempt us right after
113375ee267cSGleb Smirnoff 			 * unlock and finish its work. Then we will acquire
113475ee267cSGleb Smirnoff 			 * lock again in trunk_destroy().
113575ee267cSGleb Smirnoff 			 * XXX: not true in case of VLAN_ARRAY
113675ee267cSGleb Smirnoff 			 */
113775ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
113875ee267cSGleb Smirnoff 			trunk_destroy(trunk);
113975ee267cSGleb Smirnoff 		} else
114075ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
11411b2a4f7aSBill Fenner 	}
1142f731f104SBill Paul 
1143f731f104SBill Paul 	/* Disconnect from parent. */
11441cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
11451cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
11465cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
11475cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
11485cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1149f731f104SBill Paul 
1150f731f104SBill Paul 	/* Clear our MAC address. */
11515cb8c31aSYaroslav Tykhiy 	bzero(IF_LLADDR(ifp), ETHER_ADDR_LEN);
115275ee267cSGleb Smirnoff 
115315a66c21SBruce M Simpson 	return (0);
1154f731f104SBill Paul }
1155f731f104SBill Paul 
11561cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1157f731f104SBill Paul static int
11581cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
11591cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1160a3814acfSSam Leffler {
11611cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
11621cf236fbSYaroslav Tykhiy 	int error;
1163a3814acfSSam Leffler 
11641cf236fbSYaroslav Tykhiy 	/* XXX VLAN_LOCK_ASSERT(); */
1165a3814acfSSam Leffler 
11661cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
11671cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
11681cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
11691cf236fbSYaroslav Tykhiy 
11701cf236fbSYaroslav Tykhiy 	/*
11711cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
11721cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
11731cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
11741cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
11751cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
11761cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
11771cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
11781cf236fbSYaroslav Tykhiy 	 */
11791cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
118075ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
11811cf236fbSYaroslav Tykhiy 		if (error)
1182a3814acfSSam Leffler 			return (error);
11831cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
11841cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
11851cf236fbSYaroslav Tykhiy 	}
11861cf236fbSYaroslav Tykhiy 	return (0);
11871cf236fbSYaroslav Tykhiy }
11881cf236fbSYaroslav Tykhiy 
11891cf236fbSYaroslav Tykhiy /*
11901cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
11911cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
11921cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
11931cf236fbSYaroslav Tykhiy  */
11941cf236fbSYaroslav Tykhiy static int
11951cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
11961cf236fbSYaroslav Tykhiy {
11971cf236fbSYaroslav Tykhiy 	int error, i;
11981cf236fbSYaroslav Tykhiy 
11991cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
12001cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
12011cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
12021cf236fbSYaroslav Tykhiy 		if (error)
12031cf236fbSYaroslav Tykhiy 			return (error);
12041cf236fbSYaroslav Tykhiy 	}
12051cf236fbSYaroslav Tykhiy 	return (0);
1206a3814acfSSam Leffler }
1207a3814acfSSam Leffler 
1208127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1209127d7b2dSAndre Oppermann static void
1210127d7b2dSAndre Oppermann vlan_link_state(struct ifnet *ifp, int link)
1211127d7b2dSAndre Oppermann {
121275ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1213127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
121475ee267cSGleb Smirnoff 	int i;
1215127d7b2dSAndre Oppermann 
121675ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
121775ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
121815ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
121975ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
122075ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
122175ee267cSGleb Smirnoff #else
122275ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
122375ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
122475ee267cSGleb Smirnoff #endif
1225fc74a9f9SBrooks Davis 			if_link_state_change(ifv->ifv_ifp,
122675ee267cSGleb Smirnoff 			    trunk->parent->if_link_state);
1227127d7b2dSAndre Oppermann 	}
122875ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
122975ee267cSGleb Smirnoff }
123075ee267cSGleb Smirnoff 
123175ee267cSGleb Smirnoff static void
123275ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
123375ee267cSGleb Smirnoff {
123475ee267cSGleb Smirnoff 	struct ifnet *p = PARENT(ifv);
123575ee267cSGleb Smirnoff 	struct ifnet *ifp = ifv->ifv_ifp;
123675ee267cSGleb Smirnoff 
123775ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(TRUNK(ifv));
123875ee267cSGleb Smirnoff 
123975ee267cSGleb Smirnoff 	/*
124075ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
124175ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
124275ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
124375ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
124475ee267cSGleb Smirnoff 	 */
124575ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
124675ee267cSGleb Smirnoff 		ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
124775ee267cSGleb Smirnoff 
124875ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
124975ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
125075ee267cSGleb Smirnoff 		ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
125175ee267cSGleb Smirnoff 		ifp->if_hwassist = p->if_hwassist;
125275ee267cSGleb Smirnoff 	} else {
125375ee267cSGleb Smirnoff 		ifp->if_capenable = 0;
125475ee267cSGleb Smirnoff 		ifp->if_hwassist = 0;
125575ee267cSGleb Smirnoff 	}
125675ee267cSGleb Smirnoff }
125775ee267cSGleb Smirnoff 
125875ee267cSGleb Smirnoff static void
125975ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
126075ee267cSGleb Smirnoff {
126175ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
126275ee267cSGleb Smirnoff 	struct ifvlan *ifv;
126375ee267cSGleb Smirnoff 	int i;
126475ee267cSGleb Smirnoff 
126575ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
126675ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
126715ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
126875ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
126975ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
127075ee267cSGleb Smirnoff #else
127175ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
127275ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
127375ee267cSGleb Smirnoff #endif
127475ee267cSGleb Smirnoff 			vlan_capabilities(ifv);
127575ee267cSGleb Smirnoff 	}
127675ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
1277127d7b2dSAndre Oppermann }
1278127d7b2dSAndre Oppermann 
1279a3814acfSSam Leffler static int
1280cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
12812cc2df49SGarrett Wollman {
12822cc2df49SGarrett Wollman 	struct ifaddr *ifa;
12832cc2df49SGarrett Wollman 	struct ifnet *p;
12842cc2df49SGarrett Wollman 	struct ifreq *ifr;
12852cc2df49SGarrett Wollman 	struct ifvlan *ifv;
12862cc2df49SGarrett Wollman 	struct vlanreq vlr;
12872cc2df49SGarrett Wollman 	int error = 0;
12882cc2df49SGarrett Wollman 
12892cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
12902cc2df49SGarrett Wollman 	ifa = (struct ifaddr *)data;
12912cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
12922cc2df49SGarrett Wollman 
12932cc2df49SGarrett Wollman 	switch (cmd) {
12942cc2df49SGarrett Wollman 	case SIOCSIFADDR:
12952cc2df49SGarrett Wollman 		ifp->if_flags |= IFF_UP;
12962cc2df49SGarrett Wollman 
12972cc2df49SGarrett Wollman 		switch (ifa->ifa_addr->sa_family) {
12982cc2df49SGarrett Wollman #ifdef INET
12992cc2df49SGarrett Wollman 		case AF_INET:
1300fc74a9f9SBrooks Davis 			arp_ifinit(ifv->ifv_ifp, ifa);
13012cc2df49SGarrett Wollman 			break;
13022cc2df49SGarrett Wollman #endif
13032cc2df49SGarrett Wollman 		default:
13042cc2df49SGarrett Wollman 			break;
13052cc2df49SGarrett Wollman 		}
13062cc2df49SGarrett Wollman 		break;
13072cc2df49SGarrett Wollman 
13082cc2df49SGarrett Wollman 	case SIOCGIFADDR:
13092cc2df49SGarrett Wollman 		{
13102cc2df49SGarrett Wollman 			struct sockaddr *sa;
13112cc2df49SGarrett Wollman 
13122cc2df49SGarrett Wollman 			sa = (struct sockaddr *) &ifr->ifr_data;
13134a0d6638SRuslan Ermilov 			bcopy(IF_LLADDR(ifp), (caddr_t)sa->sa_data,
131415a66c21SBruce M Simpson 			    ETHER_ADDR_LEN);
13152cc2df49SGarrett Wollman 		}
13162cc2df49SGarrett Wollman 		break;
13172cc2df49SGarrett Wollman 
1318b3cca108SBill Fenner 	case SIOCGIFMEDIA:
13194faedfe8SSam Leffler 		VLAN_LOCK();
132075ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
132175ee267cSGleb Smirnoff 			error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
13224faedfe8SSam Leffler 					SIOCGIFMEDIA, data);
13234faedfe8SSam Leffler 			VLAN_UNLOCK();
1324b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1325b3cca108SBill Fenner 			if (error == 0) {
1326b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1327b3cca108SBill Fenner 
1328b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1329b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1330b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1331b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1332b3cca108SBill Fenner 						ifmr->ifm_ulist,
1333b3cca108SBill Fenner 						sizeof(int));
1334b3cca108SBill Fenner 				}
1335b3cca108SBill Fenner 			}
13364faedfe8SSam Leffler 		} else {
13374faedfe8SSam Leffler 			VLAN_UNLOCK();
1338b3cca108SBill Fenner 			error = EINVAL;
13394faedfe8SSam Leffler 		}
1340b3cca108SBill Fenner 		break;
1341b3cca108SBill Fenner 
1342b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1343b3cca108SBill Fenner 		error = EINVAL;
1344b3cca108SBill Fenner 		break;
1345b3cca108SBill Fenner 
13462cc2df49SGarrett Wollman 	case SIOCSIFMTU:
13472cc2df49SGarrett Wollman 		/*
13482cc2df49SGarrett Wollman 		 * Set the interface MTU.
13492cc2df49SGarrett Wollman 		 */
13504faedfe8SSam Leffler 		VLAN_LOCK();
135175ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1352a3814acfSSam Leffler 			if (ifr->ifr_mtu >
135375ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1354a3814acfSSam Leffler 			    ifr->ifr_mtu <
1355a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
13562cc2df49SGarrett Wollman 				error = EINVAL;
1357a3814acfSSam Leffler 			else
13582cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1359a3814acfSSam Leffler 		} else
1360a3814acfSSam Leffler 			error = EINVAL;
13614faedfe8SSam Leffler 		VLAN_UNLOCK();
13622cc2df49SGarrett Wollman 		break;
13632cc2df49SGarrett Wollman 
13642cc2df49SGarrett Wollman 	case SIOCSETVLAN:
136515a66c21SBruce M Simpson 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
13662cc2df49SGarrett Wollman 		if (error)
13672cc2df49SGarrett Wollman 			break;
13682cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1369f731f104SBill Paul 			vlan_unconfig(ifp);
13702cc2df49SGarrett Wollman 			break;
13712cc2df49SGarrett Wollman 		}
13722cc2df49SGarrett Wollman 		p = ifunit(vlr.vlr_parent);
13732cc2df49SGarrett Wollman 		if (p == 0) {
13742cc2df49SGarrett Wollman 			error = ENOENT;
13752cc2df49SGarrett Wollman 			break;
13762cc2df49SGarrett Wollman 		}
1377fb88a3e0SBill Paul 		/*
1378fb88a3e0SBill Paul 		 * Don't let the caller set up a VLAN tag with
1379fb88a3e0SBill Paul 		 * anything except VLID bits.
1380fb88a3e0SBill Paul 		 */
1381fb88a3e0SBill Paul 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1382fb88a3e0SBill Paul 			error = EINVAL;
1383fb88a3e0SBill Paul 			break;
1384fb88a3e0SBill Paul 		}
138575ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
138675ee267cSGleb Smirnoff 		if (error)
13872cc2df49SGarrett Wollman 			break;
138813f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1389a3814acfSSam Leffler 
13901cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
13911cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
13922cc2df49SGarrett Wollman 		break;
13932cc2df49SGarrett Wollman 
13942cc2df49SGarrett Wollman 	case SIOCGETVLAN:
139515a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
13964faedfe8SSam Leffler 		VLAN_LOCK();
139775ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
139875ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
13999bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
14002cc2df49SGarrett Wollman 			vlr.vlr_tag = ifv->ifv_tag;
14012cc2df49SGarrett Wollman 		}
14024faedfe8SSam Leffler 		VLAN_UNLOCK();
140315a66c21SBruce M Simpson 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
14042cc2df49SGarrett Wollman 		break;
14052cc2df49SGarrett Wollman 
14062cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
14072cc2df49SGarrett Wollman 		/*
14081cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
14091cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
14102cc2df49SGarrett Wollman 		 */
141175ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
14121cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
14132cc2df49SGarrett Wollman 		break;
1414a3814acfSSam Leffler 
1415f731f104SBill Paul 	case SIOCADDMULTI:
1416f731f104SBill Paul 	case SIOCDELMULTI:
141775ee267cSGleb Smirnoff 		/*
141875ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
141975ee267cSGleb Smirnoff 		 * when we do.
142075ee267cSGleb Smirnoff 		 */
142175ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
1422f731f104SBill Paul 			error = vlan_setmulti(ifp);
1423f731f104SBill Paul 		break;
142475ee267cSGleb Smirnoff 
14252cc2df49SGarrett Wollman 	default:
14262cc2df49SGarrett Wollman 		error = EINVAL;
14272cc2df49SGarrett Wollman 	}
142815a66c21SBruce M Simpson 
142915a66c21SBruce M Simpson 	return (error);
14302cc2df49SGarrett Wollman }
1431