xref: /freebsd/sys/net/if_vlan.c (revision 64a17d2e869a58367ed93aaa5d216f3e684b88dd)
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
8475ee267cSGleb Smirnoff 	struct	ifvlan	*vlans[EVL_VLID_MASK+1]; /* static table */
8575ee267cSGleb Smirnoff #else
8675ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
8775ee267cSGleb Smirnoff 	uint16_t	hmask;
8875ee267cSGleb Smirnoff 	uint16_t	hwidth;
8975ee267cSGleb Smirnoff #endif
9075ee267cSGleb Smirnoff 	int		refcnt;
9175ee267cSGleb Smirnoff 	LIST_ENTRY(ifvlantrunk) trunk_entry;
9275ee267cSGleb Smirnoff };
9375ee267cSGleb Smirnoff static LIST_HEAD(, ifvlantrunk) trunk_list;
949d4fe4b2SBrooks Davis 
95a3814acfSSam Leffler struct vlan_mc_entry {
96a3814acfSSam Leffler 	struct ether_addr		mc_addr;
97a3814acfSSam Leffler 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
98a3814acfSSam Leffler };
99a3814acfSSam Leffler 
100a3814acfSSam Leffler struct	ifvlan {
10175ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
102fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
10375ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
10475ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
1051cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
106a3814acfSSam Leffler 	struct	ifv_linkmib {
107a3814acfSSam Leffler 		int	ifvm_parent;
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_proto;	/* encapsulation ethertype */
11275ee267cSGleb Smirnoff 		uint16_t ifvm_tag;	/* tag to apply on packets leaving if */
113a3814acfSSam Leffler 	}	ifv_mib;
114a3814acfSSam Leffler 	SLIST_HEAD(__vlan_mchead, vlan_mc_entry) vlan_mc_listhead;
115a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
116a3814acfSSam Leffler };
117a3814acfSSam Leffler #define	ifv_tag	ifv_mib.ifvm_tag
118a3814acfSSam Leffler #define	ifv_encaplen	ifv_mib.ifvm_encaplen
119a3814acfSSam Leffler #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
120a3814acfSSam Leffler #define	ifv_mintu	ifv_mib.ifvm_mintu
121a3814acfSSam Leffler 
12275ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
1231cf236fbSYaroslav Tykhiy static struct {
1241cf236fbSYaroslav Tykhiy 	int flag;
1251cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
1261cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
1271cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
1281cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
1291cf236fbSYaroslav Tykhiy 	{0, NULL}
1301cf236fbSYaroslav Tykhiy };
131a3814acfSSam Leffler 
1324a408dcbSBill Paul SYSCTL_DECL(_net_link);
133d2a75853SBill Fenner SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
1342cc2df49SGarrett Wollman SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
1352cc2df49SGarrett Wollman 
1365e17543aSBrooks Davis static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
1372cc2df49SGarrett Wollman 
1384faedfe8SSam Leffler /*
13975ee267cSGleb Smirnoff  * We have a global mutex, that is used to serialize configuration
14075ee267cSGleb Smirnoff  * changes and isn't used in normal packet delivery.
14175ee267cSGleb Smirnoff  *
14275ee267cSGleb Smirnoff  * We also have a per-trunk rwlock, that is locked shared on packet
14375ee267cSGleb Smirnoff  * processing and exclusive when configuration is changed.
14475ee267cSGleb Smirnoff  *
14575ee267cSGleb Smirnoff  * The VLAN_ARRAY substitutes the dynamic hash with a static array
14675ee267cSGleb Smirnoff  * with 4096 entries. In theory this can give a boots in processing,
14775ee267cSGleb Smirnoff  * however on practice it does not. Probably this is because array
14875ee267cSGleb Smirnoff  * is too big to fit into CPU cache.
1494faedfe8SSam Leffler  */
1504faedfe8SSam Leffler static struct mtx ifv_mtx;
15175ee267cSGleb Smirnoff #define	VLAN_LOCK_INIT()	mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
1524faedfe8SSam Leffler #define	VLAN_LOCK_DESTROY()	mtx_destroy(&ifv_mtx)
1534faedfe8SSam Leffler #define	VLAN_LOCK_ASSERT()	mtx_assert(&ifv_mtx, MA_OWNED)
1544faedfe8SSam Leffler #define	VLAN_LOCK()		mtx_lock(&ifv_mtx)
1554faedfe8SSam Leffler #define	VLAN_UNLOCK()		mtx_unlock(&ifv_mtx)
15675ee267cSGleb Smirnoff #define	TRUNK_LOCK_INIT(trunk)	rw_init(&(trunk)->rw, VLANNAME)
15775ee267cSGleb Smirnoff #define	TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
15875ee267cSGleb Smirnoff #define	TRUNK_LOCK(trunk)	rw_wlock(&(trunk)->rw)
15975ee267cSGleb Smirnoff #define	TRUNK_UNLOCK(trunk)	rw_wunlock(&(trunk)->rw)
16075ee267cSGleb Smirnoff #define	TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
16175ee267cSGleb Smirnoff #define	TRUNK_RLOCK(trunk)	rw_rlock(&(trunk)->rw)
16275ee267cSGleb Smirnoff #define	TRUNK_RUNLOCK(trunk)	rw_runlock(&(trunk)->rw)
16375ee267cSGleb Smirnoff #define	TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
16475ee267cSGleb Smirnoff 
16575ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
16675ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
16775ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
16875ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
16975ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
17075ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
17175ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
17275ee267cSGleb Smirnoff 	uint16_t tag);
17375ee267cSGleb Smirnoff #endif
17475ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
1754faedfe8SSam Leffler 
1762cc2df49SGarrett Wollman static	void vlan_start(struct ifnet *ifp);
1772cc2df49SGarrett Wollman static	void vlan_ifinit(void *foo);
178a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
179cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
1801cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
1811cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
1821cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
183f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
184f731f104SBill Paul static	int vlan_unconfig(struct ifnet *ifp);
18575ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
186127d7b2dSAndre Oppermann static	void vlan_link_state(struct ifnet *ifp, int link);
18775ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
18875ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
189f731f104SBill Paul 
190f889d2efSBrooks Davis static	struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
191f889d2efSBrooks Davis     const char *, int *);
192f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
193f889d2efSBrooks Davis static	int vlan_clone_create(struct if_clone *, char *, size_t);
194f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
195f889d2efSBrooks Davis 
196c6e6ca3eSYaroslav Tykhiy static	struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
197c6e6ca3eSYaroslav Tykhiy     IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
1989d4fe4b2SBrooks Davis 
19975ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
20075ee267cSGleb Smirnoff #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
20175ee267cSGleb Smirnoff static void
20275ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
20375ee267cSGleb Smirnoff {
20475ee267cSGleb Smirnoff 	int i, n;
20575ee267cSGleb Smirnoff 
20675ee267cSGleb Smirnoff 	/*
20775ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
20875ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
20975ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
21075ee267cSGleb Smirnoff 	 */
21175ee267cSGleb Smirnoff 
21275ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
21375ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
21475ee267cSGleb Smirnoff 
21575ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
21675ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
21775ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
21875ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
21975ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
22075ee267cSGleb Smirnoff 		LIST_INIT(&trunk->hash[i]);
22175ee267cSGleb Smirnoff }
22275ee267cSGleb Smirnoff 
22375ee267cSGleb Smirnoff static void
22475ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
22575ee267cSGleb Smirnoff {
22675ee267cSGleb Smirnoff #ifdef INVARIANTS
22775ee267cSGleb Smirnoff 	int i;
22875ee267cSGleb Smirnoff 
22975ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);	/* XXX just unhook trunk first? */
23075ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
23175ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
23275ee267cSGleb Smirnoff 		KASSERT(LIST_EMPTY(&trunk->hash[i]),
23375ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
23475ee267cSGleb Smirnoff #endif
23575ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
23675ee267cSGleb Smirnoff 	trunk->hash = NULL;
23775ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
23875ee267cSGleb Smirnoff }
23975ee267cSGleb Smirnoff 
24075ee267cSGleb Smirnoff static int
24175ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
24275ee267cSGleb Smirnoff {
24375ee267cSGleb Smirnoff 	int i, b;
24475ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
24575ee267cSGleb Smirnoff 
24675ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
24775ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
24875ee267cSGleb Smirnoff 
24975ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
25075ee267cSGleb Smirnoff 	i = HASH(ifv->ifv_tag, trunk->hmask);
25175ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
25275ee267cSGleb Smirnoff 		if (ifv->ifv_tag == ifv2->ifv_tag)
25375ee267cSGleb Smirnoff 			return (EEXIST);
25475ee267cSGleb Smirnoff 
25575ee267cSGleb Smirnoff 	/*
25675ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
25775ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
25875ee267cSGleb Smirnoff 	 * buckets/2.
25975ee267cSGleb Smirnoff 	 */
26075ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
26175ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
26275ee267cSGleb Smirnoff 		i = HASH(ifv->ifv_tag, trunk->hmask);
26375ee267cSGleb Smirnoff 	}
26475ee267cSGleb Smirnoff 	LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
26575ee267cSGleb Smirnoff 	trunk->refcnt++;
26675ee267cSGleb Smirnoff 
26775ee267cSGleb Smirnoff 	return (0);
26875ee267cSGleb Smirnoff }
26975ee267cSGleb Smirnoff 
27075ee267cSGleb Smirnoff static int
27175ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
27275ee267cSGleb Smirnoff {
27375ee267cSGleb Smirnoff 	int i, b;
27475ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
27575ee267cSGleb Smirnoff 
27675ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
27775ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
27875ee267cSGleb Smirnoff 
27975ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
28075ee267cSGleb Smirnoff 	i = HASH(ifv->ifv_tag, trunk->hmask);
28175ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
28275ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
28375ee267cSGleb Smirnoff 			trunk->refcnt--;
28475ee267cSGleb Smirnoff 			LIST_REMOVE(ifv2, ifv_list);
28575ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
28675ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
28775ee267cSGleb Smirnoff 			return (0);
28875ee267cSGleb Smirnoff 		}
28975ee267cSGleb Smirnoff 
29075ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
29175ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
29275ee267cSGleb Smirnoff }
29375ee267cSGleb Smirnoff 
29475ee267cSGleb Smirnoff /*
29575ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
29675ee267cSGleb Smirnoff  */
29775ee267cSGleb Smirnoff static void
29875ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
29975ee267cSGleb Smirnoff {
30075ee267cSGleb Smirnoff 
30175ee267cSGleb Smirnoff 	struct ifvlan *ifv;
30275ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
30375ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
30475ee267cSGleb Smirnoff 
30575ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
30675ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
30775ee267cSGleb Smirnoff 
30875ee267cSGleb Smirnoff 	if (howmuch == 0) {
30975ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
31075ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
31175ee267cSGleb Smirnoff 		return;
31275ee267cSGleb Smirnoff 	}
31375ee267cSGleb Smirnoff 
31475ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
31575ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
31675ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
31775ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
31875ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
31975ee267cSGleb Smirnoff 		return;
32075ee267cSGleb Smirnoff 
32175ee267cSGleb Smirnoff 	/* M_NOWAIT because we're called with trunk mutex held */
32275ee267cSGleb Smirnoff 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
32375ee267cSGleb Smirnoff 	if (hash2 == NULL) {
32475ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
32575ee267cSGleb Smirnoff 		    __func__);
32675ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
32775ee267cSGleb Smirnoff 	}
32875ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
32975ee267cSGleb Smirnoff 		LIST_INIT(&hash2[j]);
33075ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
33175ee267cSGleb Smirnoff 		while (!LIST_EMPTY(&trunk->hash[i])) {
33275ee267cSGleb Smirnoff 			ifv = LIST_FIRST(&trunk->hash[i]);
33375ee267cSGleb Smirnoff 			LIST_REMOVE(ifv, ifv_list);
33475ee267cSGleb Smirnoff 			j = HASH(ifv->ifv_tag, n2 - 1);
33575ee267cSGleb Smirnoff 			LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
33675ee267cSGleb Smirnoff 		}
33775ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
33875ee267cSGleb Smirnoff 	trunk->hash = hash2;
33975ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
34075ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
34175ee267cSGleb Smirnoff }
34275ee267cSGleb Smirnoff 
34375ee267cSGleb Smirnoff static __inline struct ifvlan *
34475ee267cSGleb Smirnoff vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
34575ee267cSGleb Smirnoff {
34675ee267cSGleb Smirnoff 	struct ifvlan *ifv;
34775ee267cSGleb Smirnoff 
34875ee267cSGleb Smirnoff 	TRUNK_LOCK_RASSERT(trunk);
34975ee267cSGleb Smirnoff 
35075ee267cSGleb Smirnoff 	LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
35175ee267cSGleb Smirnoff 		if (ifv->ifv_tag == tag)
35275ee267cSGleb Smirnoff 			return (ifv);
35375ee267cSGleb Smirnoff 	return (NULL);
35475ee267cSGleb Smirnoff }
35575ee267cSGleb Smirnoff 
35675ee267cSGleb Smirnoff #if 0
35775ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
35875ee267cSGleb Smirnoff static void
35975ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
36075ee267cSGleb Smirnoff {
36175ee267cSGleb Smirnoff 	int i;
36275ee267cSGleb Smirnoff 	struct ifvlan *ifv;
36375ee267cSGleb Smirnoff 
36475ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
36575ee267cSGleb Smirnoff 		printf("%d: ", i);
36675ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
36775ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
36875ee267cSGleb Smirnoff 		printf("\n");
36975ee267cSGleb Smirnoff 	}
37075ee267cSGleb Smirnoff }
37175ee267cSGleb Smirnoff #endif /* 0 */
37275ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
37375ee267cSGleb Smirnoff 
37475ee267cSGleb Smirnoff static void
37575ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
37675ee267cSGleb Smirnoff {
37775ee267cSGleb Smirnoff 	VLAN_LOCK_ASSERT();
37875ee267cSGleb Smirnoff 
37975ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
38075ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
38175ee267cSGleb Smirnoff 	vlan_freehash(trunk);
38275ee267cSGleb Smirnoff #endif
38375ee267cSGleb Smirnoff 	TRUNK_LOCK_DESTROY(trunk);
38475ee267cSGleb Smirnoff 	LIST_REMOVE(trunk, trunk_entry);
38575ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
38675ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
38775ee267cSGleb Smirnoff }
38875ee267cSGleb Smirnoff 
389f731f104SBill Paul /*
390f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
391f731f104SBill Paul  * programming the multicast filter of the parent. This has the
392f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
393f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
394f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
395f731f104SBill Paul  * to avoid this: there really is only one physical interface.
39629c2dfbeSBruce M Simpson  *
39729c2dfbeSBruce M Simpson  * XXX: There is a possible race here if more than one thread is
39829c2dfbeSBruce M Simpson  *      modifying the multicast state of the vlan interface at the same time.
399f731f104SBill Paul  */
4002b120974SPeter Wemm static int
4012b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
402f731f104SBill Paul {
403f731f104SBill Paul 	struct ifnet		*ifp_p;
404f731f104SBill Paul 	struct ifmultiaddr	*ifma, *rifma = NULL;
405f731f104SBill Paul 	struct ifvlan		*sc;
406f731f104SBill Paul 	struct vlan_mc_entry	*mc = NULL;
407f731f104SBill Paul 	struct sockaddr_dl	sdl;
408f731f104SBill Paul 	int			error;
409f731f104SBill Paul 
41029c2dfbeSBruce M Simpson 	/*VLAN_LOCK_ASSERT();*/
41129c2dfbeSBruce M Simpson 
412f731f104SBill Paul 	/* Find the parent. */
413f731f104SBill Paul 	sc = ifp->if_softc;
41475ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
4151b2a4f7aSBill Fenner 
41615a66c21SBruce M Simpson 	bzero((char *)&sdl, sizeof(sdl));
41715a66c21SBruce M Simpson 	sdl.sdl_len = sizeof(sdl);
418f731f104SBill Paul 	sdl.sdl_family = AF_LINK;
41926e30963SBill Fenner 	sdl.sdl_index = ifp_p->if_index;
42026e30963SBill Fenner 	sdl.sdl_type = IFT_ETHER;
42124993214SYaroslav Tykhiy 	sdl.sdl_alen = ETHER_ADDR_LEN;
422f731f104SBill Paul 
423f731f104SBill Paul 	/* First, remove any existing filter entries. */
42422f29826SPoul-Henning Kamp 	while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
42522f29826SPoul-Henning Kamp 		mc = SLIST_FIRST(&sc->vlan_mc_listhead);
426f731f104SBill Paul 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
427f731f104SBill Paul 		error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
428f731f104SBill Paul 		if (error)
429f731f104SBill Paul 			return (error);
430f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
4319d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
432f731f104SBill Paul 	}
433f731f104SBill Paul 
434f731f104SBill Paul 	/* Now program new ones. */
4356817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
436f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
437f731f104SBill Paul 			continue;
43829c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
43929c2dfbeSBruce M Simpson 		if (mc == NULL)
44029c2dfbeSBruce M Simpson 			return (ENOMEM);
441f731f104SBill Paul 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
442f731f104SBill Paul 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
443f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
44426e30963SBill Fenner 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
44526e30963SBill Fenner 		    LLADDR(&sdl), ETHER_ADDR_LEN);
446f731f104SBill Paul 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
447f731f104SBill Paul 		if (error)
448f731f104SBill Paul 			return (error);
449f731f104SBill Paul 	}
450f731f104SBill Paul 
451f731f104SBill Paul 	return (0);
452f731f104SBill Paul }
4532cc2df49SGarrett Wollman 
454a3814acfSSam Leffler /*
455a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
456a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
457a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
458a3814acfSSam Leffler  * set here.  Noone else in the system should be aware of this so
459a3814acfSSam Leffler  * we use an explicit reference here.
460a3814acfSSam Leffler  */
461a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
462a3814acfSSam Leffler 
463984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
464127d7b2dSAndre Oppermann extern	void (*vlan_link_state_p)(struct ifnet *, int);
465127d7b2dSAndre Oppermann 
4662b120974SPeter Wemm static int
4672b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
4682b120974SPeter Wemm {
4699d4fe4b2SBrooks Davis 
4702b120974SPeter Wemm 	switch (type) {
4712b120974SPeter Wemm 	case MOD_LOAD:
47275ee267cSGleb Smirnoff 		LIST_INIT(&trunk_list);
4734faedfe8SSam Leffler 		VLAN_LOCK_INIT();
4749d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
475127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
47675ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
4779d4fe4b2SBrooks Davis 		if_clone_attach(&vlan_cloner);
4782b120974SPeter Wemm 		break;
4792b120974SPeter Wemm 	case MOD_UNLOAD:
48075ee267cSGleb Smirnoff 	    {
48175ee267cSGleb Smirnoff 		struct ifvlantrunk *trunk, *trunk1;
48275ee267cSGleb Smirnoff 
4839d4fe4b2SBrooks Davis 		if_clone_detach(&vlan_cloner);
4849d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
485127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
48675ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
48775ee267cSGleb Smirnoff 		VLAN_LOCK();
48875ee267cSGleb Smirnoff 		LIST_FOREACH_SAFE(trunk, &trunk_list, trunk_entry, trunk1)
48975ee267cSGleb Smirnoff 			trunk_destroy(trunk);
49075ee267cSGleb Smirnoff 		VLAN_UNLOCK();
4914faedfe8SSam Leffler 		VLAN_LOCK_DESTROY();
4929d4fe4b2SBrooks Davis 		break;
49375ee267cSGleb Smirnoff 	    }
4943e019deaSPoul-Henning Kamp 	default:
4953e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
4962b120974SPeter Wemm 	}
49715a66c21SBruce M Simpson 	return (0);
4982b120974SPeter Wemm }
4992b120974SPeter Wemm 
5002b120974SPeter Wemm static moduledata_t vlan_mod = {
5012b120974SPeter Wemm 	"if_vlan",
5022b120974SPeter Wemm 	vlan_modevent,
5032b120974SPeter Wemm 	0
5042b120974SPeter Wemm };
5052b120974SPeter Wemm 
5062b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
507d35bcd3bSRuslan Ermilov MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
5082cc2df49SGarrett Wollman 
509f889d2efSBrooks Davis static struct ifnet *
510f889d2efSBrooks Davis vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
5119d4fe4b2SBrooks Davis {
512f889d2efSBrooks Davis 	const char *cp;
513f889d2efSBrooks Davis 	struct ifnet *ifp;
51415a66c21SBruce M Simpson 	int t = 0;
515f889d2efSBrooks Davis 
516f889d2efSBrooks Davis 	/* Check for <etherif>.<vlan> style interface names. */
517f889d2efSBrooks Davis 	IFNET_RLOCK();
518f889d2efSBrooks Davis 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
519f889d2efSBrooks Davis 		if (ifp->if_type != IFT_ETHER)
520f889d2efSBrooks Davis 			continue;
521f889d2efSBrooks Davis 		if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
522f889d2efSBrooks Davis 			continue;
523f889d2efSBrooks Davis 		cp = name + strlen(ifp->if_xname);
524f889d2efSBrooks Davis 		if (*cp != '.')
525f889d2efSBrooks Davis 			continue;
526f889d2efSBrooks Davis 		for(; *cp != '\0'; cp++) {
527f889d2efSBrooks Davis 			if (*cp < '0' || *cp > '9')
528f889d2efSBrooks Davis 				continue;
529f889d2efSBrooks Davis 			t = (t * 10) + (*cp - '0');
530f889d2efSBrooks Davis 		}
531f889d2efSBrooks Davis 		if (tag != NULL)
532f889d2efSBrooks Davis 			*tag = t;
533f889d2efSBrooks Davis 		break;
534f889d2efSBrooks Davis 	}
535f889d2efSBrooks Davis 	IFNET_RUNLOCK();
536f889d2efSBrooks Davis 
53715a66c21SBruce M Simpson 	return (ifp);
538f889d2efSBrooks Davis }
539f889d2efSBrooks Davis 
540f889d2efSBrooks Davis static int
541f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
542f889d2efSBrooks Davis {
543f889d2efSBrooks Davis 	const char *cp;
544f889d2efSBrooks Davis 
545f889d2efSBrooks Davis 	if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
546f889d2efSBrooks Davis 		return (1);
547f889d2efSBrooks Davis 
548f889d2efSBrooks Davis 	if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
549f889d2efSBrooks Davis 		return (0);
550f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
551f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
552f889d2efSBrooks Davis 			return (0);
553f889d2efSBrooks Davis 	}
554f889d2efSBrooks Davis 
555f889d2efSBrooks Davis 	return (1);
556f889d2efSBrooks Davis }
557f889d2efSBrooks Davis 
558f889d2efSBrooks Davis static int
559f889d2efSBrooks Davis vlan_clone_create(struct if_clone *ifc, char *name, size_t len)
560f889d2efSBrooks Davis {
561f889d2efSBrooks Davis 	char *dp;
562f889d2efSBrooks Davis 	int wildcard;
563f889d2efSBrooks Davis 	int unit;
564f889d2efSBrooks Davis 	int error;
565f889d2efSBrooks Davis 	int tag;
566f889d2efSBrooks Davis 	int ethertag;
5679d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
5689d4fe4b2SBrooks Davis 	struct ifnet *ifp;
569f889d2efSBrooks Davis 	struct ifnet *p;
570fc74a9f9SBrooks Davis 	u_char eaddr[6] = {0,0,0,0,0,0};
571f889d2efSBrooks Davis 
572f889d2efSBrooks Davis 	if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
573f889d2efSBrooks Davis 		ethertag = 1;
574f889d2efSBrooks Davis 		unit = -1;
575f889d2efSBrooks Davis 		wildcard = 0;
576f889d2efSBrooks Davis 
577f889d2efSBrooks Davis 		/*
578f889d2efSBrooks Davis 		 * Don't let the caller set up a VLAN tag with
579f889d2efSBrooks Davis 		 * anything except VLID bits.
580f889d2efSBrooks Davis 		 */
58115a66c21SBruce M Simpson 		if (tag & ~EVL_VLID_MASK)
582f889d2efSBrooks Davis 			return (EINVAL);
583f889d2efSBrooks Davis 	} else {
584f889d2efSBrooks Davis 		ethertag = 0;
585f889d2efSBrooks Davis 
586f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
587f889d2efSBrooks Davis 		if (error != 0)
588f889d2efSBrooks Davis 			return (error);
589f889d2efSBrooks Davis 
590f889d2efSBrooks Davis 		wildcard = (unit < 0);
591f889d2efSBrooks Davis 	}
592f889d2efSBrooks Davis 
593f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
594f889d2efSBrooks Davis 	if (error != 0)
595f889d2efSBrooks Davis 		return (error);
596f889d2efSBrooks Davis 
597f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
598f889d2efSBrooks Davis 	if (wildcard) {
599f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
600f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
601f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
602f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
603f889d2efSBrooks Davis 		}
604f889d2efSBrooks Davis 	}
6059d4fe4b2SBrooks Davis 
606a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
607fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
608fc74a9f9SBrooks Davis 	if (ifp == NULL) {
609fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
610fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
611fc74a9f9SBrooks Davis 		return (ENOSPC);
612fc74a9f9SBrooks Davis 	}
6139d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
6149d4fe4b2SBrooks Davis 
6159d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
616f889d2efSBrooks Davis 	/*
617cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
618f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
619f889d2efSBrooks Davis 	 */
620f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
621f889d2efSBrooks Davis 	ifp->if_dname = ifc->ifc_name;
622f889d2efSBrooks Davis 	ifp->if_dunit = unit;
6239d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
6249d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
62515a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
6269d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
6279d4fe4b2SBrooks Davis 
6289d4fe4b2SBrooks Davis 	ifp->if_init = vlan_ifinit;
6299d4fe4b2SBrooks Davis 	ifp->if_start = vlan_start;
6309d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
6319d4fe4b2SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
63264a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
633fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
6349d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
635211f625aSBill Fenner 	ifp->if_baudrate = 0;
636a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
637a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
6389d4fe4b2SBrooks Davis 
639f889d2efSBrooks Davis 	if (ethertag) {
64075ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, tag);
641f889d2efSBrooks Davis 		if (error != 0) {
642f889d2efSBrooks Davis 			/*
643f889d2efSBrooks Davis 			 * Since we've partialy failed, we need to back
644f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
645f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
646f889d2efSBrooks Davis 			 */
647f889d2efSBrooks Davis 			vlan_unconfig(ifp);
648f889d2efSBrooks Davis 			ether_ifdetach(ifp);
649fc74a9f9SBrooks Davis 			if_free_type(ifp, IFT_ETHER);
650f889d2efSBrooks Davis 			free(ifv, M_VLAN);
651f889d2efSBrooks Davis 
652f889d2efSBrooks Davis 			return (error);
653f889d2efSBrooks Davis 		}
65413f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
655f889d2efSBrooks Davis 
6561cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
6571cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
658f889d2efSBrooks Davis 	}
659f889d2efSBrooks Davis 
6609d4fe4b2SBrooks Davis 	return (0);
6619d4fe4b2SBrooks Davis }
6629d4fe4b2SBrooks Davis 
663f889d2efSBrooks Davis static int
664f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
6659d4fe4b2SBrooks Davis {
666b4e9f837SBrooks Davis 	int unit;
6679d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
6689d4fe4b2SBrooks Davis 
669b4e9f837SBrooks Davis 	unit = ifp->if_dunit;
670b4e9f837SBrooks Davis 
6719d4fe4b2SBrooks Davis 	vlan_unconfig(ifp);
6729d4fe4b2SBrooks Davis 
673a3814acfSSam Leffler 	ether_ifdetach(ifp);
674f3447eb4SBrooks Davis 	if_free_type(ifp, IFT_ETHER);
6759d4fe4b2SBrooks Davis 
6769d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
677f889d2efSBrooks Davis 
678b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
679b4e9f837SBrooks Davis 
680f889d2efSBrooks Davis 	return (0);
6819d4fe4b2SBrooks Davis }
6829d4fe4b2SBrooks Davis 
68315a66c21SBruce M Simpson /*
68415a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
68515a66c21SBruce M Simpson  */
6862cc2df49SGarrett Wollman static void
6872cc2df49SGarrett Wollman vlan_ifinit(void *foo)
6882cc2df49SGarrett Wollman {
68915a66c21SBruce M Simpson 
6902cc2df49SGarrett Wollman }
6912cc2df49SGarrett Wollman 
6926d3a3ab7SGleb Smirnoff /*
6936d3a3ab7SGleb Smirnoff  * The if_start method for vlan(4) interface. It doesn't
6946d3a3ab7SGleb Smirnoff  * raises the IFF_DRV_OACTIVE flag, since it is called
6956d3a3ab7SGleb Smirnoff  * only from IFQ_HANDOFF() macro in ether_output_frame().
6966d3a3ab7SGleb Smirnoff  * If the interface queue is full, and vlan_start() is
6976d3a3ab7SGleb Smirnoff  * not called, the queue would never get emptied and
6986d3a3ab7SGleb Smirnoff  * interface would stall forever.
6996d3a3ab7SGleb Smirnoff  */
7002cc2df49SGarrett Wollman static void
7012cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp)
7022cc2df49SGarrett Wollman {
7032cc2df49SGarrett Wollman 	struct ifvlan *ifv;
7042cc2df49SGarrett Wollman 	struct ifnet *p;
7055326b23cSMatthew N. Dodd 	struct mbuf *m;
706affc907dSMax Laier 	int error;
7072cc2df49SGarrett Wollman 
7082cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
70975ee267cSGleb Smirnoff 	p = PARENT(ifv);
7102cc2df49SGarrett Wollman 
7112cc2df49SGarrett Wollman 	for (;;) {
7122cc2df49SGarrett Wollman 		IF_DEQUEUE(&ifp->if_snd, m);
7132cc2df49SGarrett Wollman 		if (m == 0)
7142cc2df49SGarrett Wollman 			break;
715a3814acfSSam Leffler 		BPF_MTAP(ifp, m);
7162cc2df49SGarrett Wollman 
717f731f104SBill Paul 		/*
71824993214SYaroslav Tykhiy 		 * Do not run parent's if_start() if the parent is not up,
71924993214SYaroslav Tykhiy 		 * or parent's driver will cause a system crash.
72024993214SYaroslav Tykhiy 		 */
72113f4c340SRobert Watson 		if (!((p->if_flags & IFF_UP) &&
72213f4c340SRobert Watson 		    (p->if_drv_flags & IFF_DRV_RUNNING))) {
72324993214SYaroslav Tykhiy 			m_freem(m);
724a3814acfSSam Leffler 			ifp->if_collisions++;
72524993214SYaroslav Tykhiy 			continue;
72624993214SYaroslav Tykhiy 		}
72724993214SYaroslav Tykhiy 
72824993214SYaroslav Tykhiy 		/*
729a3814acfSSam Leffler 		 * If underlying interface can do VLAN tag insertion itself,
730a3814acfSSam Leffler 		 * just pass the packet along. However, we need some way to
731a3814acfSSam Leffler 		 * tell the interface where the packet came from so that it
732a3814acfSSam Leffler 		 * knows how to find the VLAN tag to use, so we attach a
733a3814acfSSam Leffler 		 * packet tag that holds it.
734f731f104SBill Paul 		 */
735b08347a0SYaroslav Tykhiy 		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
73675ee267cSGleb Smirnoff 			struct m_tag *mtag = (struct m_tag *)
73775ee267cSGleb Smirnoff 			    uma_zalloc(zone_mtag_vlan, M_NOWAIT);
738a3814acfSSam Leffler 			if (mtag == NULL) {
739a3814acfSSam Leffler 				ifp->if_oerrors++;
740a3814acfSSam Leffler 				m_freem(m);
741a3814acfSSam Leffler 				continue;
742a3814acfSSam Leffler 			}
743eefbcf0eSYaroslav Tykhiy 			VLAN_TAG_VALUE(mtag) = ifv->ifv_tag;
744a3814acfSSam Leffler 			m_tag_prepend(m, mtag);
745ba26134bSGleb Smirnoff 			m->m_flags |= M_VLANTAG;
746f731f104SBill Paul 		} else {
74775ee267cSGleb Smirnoff 			struct ether_vlan_header *evl;
74875ee267cSGleb Smirnoff 
749a163d034SWarner Losh 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
7504af90a4dSMatthew N. Dodd 			if (m == NULL) {
7516cbd3e99SYaroslav Tykhiy 				if_printf(ifp,
7526cbd3e99SYaroslav Tykhiy 				    "unable to prepend VLAN header\n");
75326f9b263SRuslan Ermilov 				ifp->if_oerrors++;
7542cc2df49SGarrett Wollman 				continue;
7554af90a4dSMatthew N. Dodd 			}
7562cc2df49SGarrett Wollman 			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
7572cc2df49SGarrett Wollman 
758a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl)) {
759a3814acfSSam Leffler 				m = m_pullup(m, sizeof(*evl));
7605326b23cSMatthew N. Dodd 				if (m == NULL) {
761a3814acfSSam Leffler 					if_printf(ifp,
7626cbd3e99SYaroslav Tykhiy 					    "cannot pullup VLAN header\n");
76326f9b263SRuslan Ermilov 					ifp->if_oerrors++;
7644af90a4dSMatthew N. Dodd 					continue;
7654af90a4dSMatthew N. Dodd 				}
766a3814acfSSam Leffler 			}
7674af90a4dSMatthew N. Dodd 
7682cc2df49SGarrett Wollman 			/*
7692cc2df49SGarrett Wollman 			 * Transform the Ethernet header into an Ethernet header
7702cc2df49SGarrett Wollman 			 * with 802.1Q encapsulation.
7712cc2df49SGarrett Wollman 			 */
772a3814acfSSam Leffler 			bcopy(mtod(m, char *) + ifv->ifv_encaplen,
773797f247bSMatthew N. Dodd 			      mtod(m, char *), ETHER_HDR_LEN);
7742cc2df49SGarrett Wollman 			evl = mtod(m, struct ether_vlan_header *);
7752cc2df49SGarrett Wollman 			evl->evl_proto = evl->evl_encap_proto;
7769d4fe4b2SBrooks Davis 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
7772cc2df49SGarrett Wollman 			evl->evl_tag = htons(ifv->ifv_tag);
778f731f104SBill Paul #ifdef DEBUG
77983908c65SRuslan Ermilov 			printf("%s: %*D\n", __func__, (int)sizeof(*evl),
78083908c65SRuslan Ermilov 			    (unsigned char *)evl, ":");
781f731f104SBill Paul #endif
782f731f104SBill Paul 		}
7832cc2df49SGarrett Wollman 
7842cc2df49SGarrett Wollman 		/*
7852cc2df49SGarrett Wollman 		 * Send it, precisely as ether_output() would have.
7862cc2df49SGarrett Wollman 		 * We are already running at splimp.
7872cc2df49SGarrett Wollman 		 */
788affc907dSMax Laier 		IFQ_HANDOFF(p, m, error);
789affc907dSMax Laier 		if (!error)
790f731f104SBill Paul 			ifp->if_opackets++;
791df5e1987SJonathan Lemon 		else
792df5e1987SJonathan Lemon 			ifp->if_oerrors++;
7932cc2df49SGarrett Wollman 	}
794f731f104SBill Paul }
795f731f104SBill Paul 
796a3814acfSSam Leffler static void
797a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
798f731f104SBill Paul {
79975ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
800f731f104SBill Paul 	struct ifvlan *ifv;
801a3814acfSSam Leffler 	struct m_tag *mtag;
80275ee267cSGleb Smirnoff 	uint16_t tag;
80375ee267cSGleb Smirnoff 
80475ee267cSGleb Smirnoff 	KASSERT(trunk != NULL, ("%s: no trunk", __func__));
805a3814acfSSam Leffler 
806f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
807a3814acfSSam Leffler 		/*
80814e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
809a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
810a3814acfSSam Leffler 		 */
811f4ec4126SYaroslav Tykhiy 		mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
812f4ec4126SYaroslav Tykhiy 		KASSERT(mtag != NULL,
813f4ec4126SYaroslav Tykhiy 			("%s: M_VLANTAG without m_tag", __func__));
81426f9b263SRuslan Ermilov 		tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
815a3814acfSSam Leffler 		m_tag_delete(m, mtag);
8166ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
817a3814acfSSam Leffler 	} else {
81875ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
81975ee267cSGleb Smirnoff 
82014e98256SYaroslav Tykhiy 		/*
82114e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
82214e98256SYaroslav Tykhiy 		 */
823f4ec4126SYaroslav Tykhiy 		mtag = NULL;
824a3814acfSSam Leffler 		switch (ifp->if_type) {
825a3814acfSSam Leffler 		case IFT_ETHER:
826a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
827a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
828a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
829a3814acfSSam Leffler 				return;
830a3814acfSSam Leffler 			}
831a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
832a3814acfSSam Leffler 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
833ffdd61c3SYaroslav Tykhiy 				("%s: bad encapsulation protocol (%u)",
834ffdd61c3SYaroslav Tykhiy 				 __func__, ntohs(evl->evl_encap_proto)));
835a3814acfSSam Leffler 
836fb88a3e0SBill Paul 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
837f731f104SBill Paul 
8387a46ec8fSBrooks Davis 			/*
839a3814acfSSam Leffler 			 * Restore the original ethertype.  We'll remove
840a3814acfSSam Leffler 			 * the encapsulation after we've found the vlan
841a3814acfSSam Leffler 			 * interface corresponding to the tag.
8427a46ec8fSBrooks Davis 			 */
843a3814acfSSam Leffler 			evl->evl_encap_proto = evl->evl_proto;
844a3814acfSSam Leffler 			break;
845a3814acfSSam Leffler 		default:
84675ee267cSGleb Smirnoff 			tag = (uint16_t) -1;
847ffdd61c3SYaroslav Tykhiy #ifdef INVARIANTS
848ffdd61c3SYaroslav Tykhiy 			panic("%s: unsupported if_type (%u)",
849ffdd61c3SYaroslav Tykhiy 			      __func__, ifp->if_type);
850a3814acfSSam Leffler #endif
851a3814acfSSam Leffler 			break;
852a3814acfSSam Leffler 		}
8537a46ec8fSBrooks Davis 	}
8547a46ec8fSBrooks Davis 
85575ee267cSGleb Smirnoff 	/*
85675ee267cSGleb Smirnoff 	 * In VLAN_ARRAY case we proceed completely lockless.
85775ee267cSGleb Smirnoff 	 */
85875ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
85975ee267cSGleb Smirnoff 	ifv = trunk->vlans[tag];
860fc74a9f9SBrooks Davis 	if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
8617d3e4c6eSLuigi Rizzo 		m_freem(m);
862a3814acfSSam Leffler 		ifp->if_noproto++;
863a3814acfSSam Leffler 		return;
864f731f104SBill Paul 	}
86575ee267cSGleb Smirnoff #else
86675ee267cSGleb Smirnoff 	TRUNK_RLOCK(trunk);
86775ee267cSGleb Smirnoff 	ifv = vlan_gethash(trunk, tag);
86875ee267cSGleb Smirnoff 	if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
86975ee267cSGleb Smirnoff 		TRUNK_RUNLOCK(trunk);
87075ee267cSGleb Smirnoff 		m_freem(m);
87175ee267cSGleb Smirnoff 		ifp->if_noproto++;
87275ee267cSGleb Smirnoff 		return;
87375ee267cSGleb Smirnoff 	}
87475ee267cSGleb Smirnoff 	TRUNK_RUNLOCK(trunk);
875b46f884bSJoerg Wunsch #endif
876f731f104SBill Paul 
877a3814acfSSam Leffler 	if (mtag == NULL) {
878f731f104SBill Paul 		/*
879a3814acfSSam Leffler 		 * Packet had an in-line encapsulation header;
880a3814acfSSam Leffler 		 * remove it.  The original header has already
881a3814acfSSam Leffler 		 * been fixed up above.
882f731f104SBill Paul 		 */
883a3814acfSSam Leffler 		bcopy(mtod(m, caddr_t),
884a3814acfSSam Leffler 		      mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
885797f247bSMatthew N. Dodd 		      ETHER_HDR_LEN);
886a3814acfSSam Leffler 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
887a3814acfSSam Leffler 	}
888a3814acfSSam Leffler 
889fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
890fc74a9f9SBrooks Davis 	ifv->ifv_ifp->if_ipackets++;
8912cc2df49SGarrett Wollman 
892a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
893fc74a9f9SBrooks Davis 	(*ifp->if_input)(ifv->ifv_ifp, m);
8942cc2df49SGarrett Wollman }
8952cc2df49SGarrett Wollman 
8962cc2df49SGarrett Wollman static int
89775ee267cSGleb Smirnoff vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
8982cc2df49SGarrett Wollman {
89975ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
9001cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
90175ee267cSGleb Smirnoff 	int error = 0;
9022cc2df49SGarrett Wollman 
90375ee267cSGleb Smirnoff 	/* VID numbers 0x0 and 0xFFF are reserved */
90475ee267cSGleb Smirnoff 	if (tag == 0 || tag == 0xFFF)
90575ee267cSGleb Smirnoff 		return (EINVAL);
9061cf236fbSYaroslav Tykhiy 	if (p->if_type != IFT_ETHER)
90715a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
90864a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
90964a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
91075ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
91115a66c21SBruce M Simpson 		return (EBUSY);
9122cc2df49SGarrett Wollman 
91375ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
91475ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
91575ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
91675ee267cSGleb Smirnoff 		VLAN_LOCK();
91775ee267cSGleb Smirnoff 		if (p->if_vlantrunk != NULL) {
91875ee267cSGleb Smirnoff 			/* A race that that is very unlikely to be hit. */
91975ee267cSGleb Smirnoff 			free(trunk, M_VLAN);
92075ee267cSGleb Smirnoff 			goto exists;
92175ee267cSGleb Smirnoff 		}
92275ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
92375ee267cSGleb Smirnoff 		vlan_inithash(trunk);
92475ee267cSGleb Smirnoff #endif
92575ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
92675ee267cSGleb Smirnoff 		LIST_INSERT_HEAD(&trunk_list, trunk, trunk_entry);
92775ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
92875ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
92975ee267cSGleb Smirnoff 		trunk->parent = p;
93075ee267cSGleb Smirnoff 	} else {
93175ee267cSGleb Smirnoff 		VLAN_LOCK();
93275ee267cSGleb Smirnoff exists:
93375ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
93475ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
93575ee267cSGleb Smirnoff 	}
93675ee267cSGleb Smirnoff 
93775ee267cSGleb Smirnoff 	ifv->ifv_tag = tag;
93875ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
93975ee267cSGleb Smirnoff 	if (trunk->vlans[tag] != NULL)
94075ee267cSGleb Smirnoff 		error = EEXIST;
94175ee267cSGleb Smirnoff #else
94275ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
94375ee267cSGleb Smirnoff #endif
94475ee267cSGleb Smirnoff 	if (error)
94575ee267cSGleb Smirnoff 		goto done;
94675ee267cSGleb Smirnoff 
947a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
948a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
9491cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
950a3814acfSSam Leffler 
951a3814acfSSam Leffler 	/*
952a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
953a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
954656acce4SYaroslav Tykhiy 	 * use it.
955a3814acfSSam Leffler 	 */
956656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
957656acce4SYaroslav Tykhiy 		/*
958656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
959656acce4SYaroslav Tykhiy 		 * handle extended frames.
960656acce4SYaroslav Tykhiy 		 */
961a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
962656acce4SYaroslav Tykhiy 	} else {
963a3814acfSSam Leffler 		/*
964a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
965a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
966a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
967a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
968a3814acfSSam Leffler 		 * which might still be useful.
969a3814acfSSam Leffler 		 */
970a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
971a3814acfSSam Leffler 	}
972a3814acfSSam Leffler 
97375ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
9741cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
9751cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
97675ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
9772cc2df49SGarrett Wollman 	/*
97824993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
97924993214SYaroslav Tykhiy 	 * Other flags are none of our business.
9802cc2df49SGarrett Wollman 	 */
98164a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
9821cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
9831cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
9841cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
9851cf236fbSYaroslav Tykhiy 
9861cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
9872cc2df49SGarrett Wollman 
98875ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
989a3814acfSSam Leffler 
990a3814acfSSam Leffler 	/*
9912cc2df49SGarrett Wollman 	 * Set up our ``Ethernet address'' to reflect the underlying
9922cc2df49SGarrett Wollman 	 * physical interface's.
9932cc2df49SGarrett Wollman 	 */
994d09ed26fSRuslan Ermilov 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
9951b2a4f7aSBill Fenner 
9961b2a4f7aSBill Fenner 	/*
9971b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
9981b2a4f7aSBill Fenner 	 * joined on the vlan device.
9991b2a4f7aSBill Fenner 	 */
10001cf236fbSYaroslav Tykhiy 	(void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
10011b2a4f7aSBill Fenner 
100275ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
100375ee267cSGleb Smirnoff 	atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[tag], (uintptr_t)ifv);
100475ee267cSGleb Smirnoff 	trunk->refcnt++;
100575ee267cSGleb Smirnoff #endif
100675ee267cSGleb Smirnoff done:
100775ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
100875ee267cSGleb Smirnoff 	VLAN_UNLOCK();
100975ee267cSGleb Smirnoff 
101075ee267cSGleb Smirnoff 	return (error);
10112cc2df49SGarrett Wollman }
10122cc2df49SGarrett Wollman 
10132cc2df49SGarrett Wollman static int
1014f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1015f731f104SBill Paul {
101675ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1017f731f104SBill Paul 	struct vlan_mc_entry *mc;
1018f731f104SBill Paul 	struct ifvlan *ifv;
1019f731f104SBill Paul 	int error;
1020f731f104SBill Paul 
102175ee267cSGleb Smirnoff 	VLAN_LOCK();
10224faedfe8SSam Leffler 
1023f731f104SBill Paul 	ifv = ifp->if_softc;
102475ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
1025f731f104SBill Paul 
102675ee267cSGleb Smirnoff 	if (trunk) {
10271b2a4f7aSBill Fenner 		struct sockaddr_dl sdl;
102875ee267cSGleb Smirnoff 		struct ifnet *p = trunk->parent;
102975ee267cSGleb Smirnoff 
103075ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
103175ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
103275ee267cSGleb Smirnoff 		atomic_store_rel_ptr((uintptr_t *)&trunk->vlans[ifv->ifv_tag],
103375ee267cSGleb Smirnoff 		    (uintptr_t)NULL);
103475ee267cSGleb Smirnoff 		trunk->refcnt--;
103575ee267cSGleb Smirnoff #endif
10361b2a4f7aSBill Fenner 
1037f731f104SBill Paul 		/*
1038f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1039f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
10401b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1041f731f104SBill Paul 		 */
104215a66c21SBruce M Simpson 		bzero((char *)&sdl, sizeof(sdl));
104315a66c21SBruce M Simpson 		sdl.sdl_len = sizeof(sdl);
1044f731f104SBill Paul 		sdl.sdl_family = AF_LINK;
10451b2a4f7aSBill Fenner 		sdl.sdl_index = p->if_index;
10461b2a4f7aSBill Fenner 		sdl.sdl_type = IFT_ETHER;
10471b2a4f7aSBill Fenner 		sdl.sdl_alen = ETHER_ADDR_LEN;
10481b2a4f7aSBill Fenner 
10491b2a4f7aSBill Fenner 		while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
105022f29826SPoul-Henning Kamp 			mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
105115a66c21SBruce M Simpson 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
105215a66c21SBruce M Simpson 			    ETHER_ADDR_LEN);
1053f731f104SBill Paul 			error = if_delmulti(p, (struct sockaddr *)&sdl);
1054f731f104SBill Paul 			if (error)
1055f731f104SBill Paul 				return (error);
1056f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
10579d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
1058f731f104SBill Paul 		}
1059a3814acfSSam Leffler 
10601cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
106175ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
106275ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
106375ee267cSGleb Smirnoff #endif
106475ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
106575ee267cSGleb Smirnoff 
106675ee267cSGleb Smirnoff 		/*
106775ee267cSGleb Smirnoff 		 * Check if we were the last.
106875ee267cSGleb Smirnoff 		 */
106975ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
107075ee267cSGleb Smirnoff 			atomic_store_rel_ptr((uintptr_t *)
107175ee267cSGleb Smirnoff 			    &trunk->parent->if_vlantrunk,
107275ee267cSGleb Smirnoff 			    (uintptr_t)NULL);
107375ee267cSGleb Smirnoff 			/*
107475ee267cSGleb Smirnoff 			 * XXXGL: If some ithread has already entered
107575ee267cSGleb Smirnoff 			 * vlan_input() and is now blocked on the trunk
107675ee267cSGleb Smirnoff 			 * lock, then it should preempt us right after
107775ee267cSGleb Smirnoff 			 * unlock and finish its work. Then we will acquire
107875ee267cSGleb Smirnoff 			 * lock again in trunk_destroy().
107975ee267cSGleb Smirnoff 			 * XXX: not true in case of VLAN_ARRAY
108075ee267cSGleb Smirnoff 			 */
108175ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
108275ee267cSGleb Smirnoff 			trunk_destroy(trunk);
108375ee267cSGleb Smirnoff 		} else
108475ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
10851b2a4f7aSBill Fenner 	}
1086f731f104SBill Paul 
1087f731f104SBill Paul 	/* Disconnect from parent. */
10881cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
10891cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
1090fc74a9f9SBrooks Davis 	ifv->ifv_ifp->if_mtu = ETHERMTU;		/* XXX why not 0? */
1091fc74a9f9SBrooks Davis 	ifv->ifv_ifp->if_link_state = LINK_STATE_UNKNOWN;
1092f731f104SBill Paul 
1093f731f104SBill Paul 	/* Clear our MAC address. */
1094d09ed26fSRuslan Ermilov 	bzero(IF_LLADDR(ifv->ifv_ifp), ETHER_ADDR_LEN);
1095f731f104SBill Paul 
109675ee267cSGleb Smirnoff 	VLAN_UNLOCK();
109775ee267cSGleb Smirnoff 
109815a66c21SBruce M Simpson 	return (0);
1099f731f104SBill Paul }
1100f731f104SBill Paul 
11011cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1102f731f104SBill Paul static int
11031cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
11041cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1105a3814acfSSam Leffler {
11061cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
11071cf236fbSYaroslav Tykhiy 	int error;
1108a3814acfSSam Leffler 
11091cf236fbSYaroslav Tykhiy 	/* XXX VLAN_LOCK_ASSERT(); */
1110a3814acfSSam Leffler 
11111cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
11121cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
11131cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
11141cf236fbSYaroslav Tykhiy 
11151cf236fbSYaroslav Tykhiy 	/*
11161cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
11171cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
11181cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
11191cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
11201cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
11211cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
11221cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
11231cf236fbSYaroslav Tykhiy 	 */
11241cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
112575ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
11261cf236fbSYaroslav Tykhiy 		if (error)
1127a3814acfSSam Leffler 			return (error);
11281cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
11291cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
11301cf236fbSYaroslav Tykhiy 	}
11311cf236fbSYaroslav Tykhiy 	return (0);
11321cf236fbSYaroslav Tykhiy }
11331cf236fbSYaroslav Tykhiy 
11341cf236fbSYaroslav Tykhiy /*
11351cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
11361cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
11371cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
11381cf236fbSYaroslav Tykhiy  */
11391cf236fbSYaroslav Tykhiy static int
11401cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
11411cf236fbSYaroslav Tykhiy {
11421cf236fbSYaroslav Tykhiy 	int error, i;
11431cf236fbSYaroslav Tykhiy 
11441cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
11451cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
11461cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
11471cf236fbSYaroslav Tykhiy 		if (error)
11481cf236fbSYaroslav Tykhiy 			return (error);
11491cf236fbSYaroslav Tykhiy 	}
11501cf236fbSYaroslav Tykhiy 	return (0);
1151a3814acfSSam Leffler }
1152a3814acfSSam Leffler 
1153127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1154127d7b2dSAndre Oppermann static void
1155127d7b2dSAndre Oppermann vlan_link_state(struct ifnet *ifp, int link)
1156127d7b2dSAndre Oppermann {
115775ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1158127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
115975ee267cSGleb Smirnoff 	int i;
1160127d7b2dSAndre Oppermann 
116175ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
116275ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
116375ee267cSGleb Smirnoff 	for (i = 0; i < EVL_VLID_MASK+1; i++)
116475ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
116575ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
116675ee267cSGleb Smirnoff #else
116775ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
116875ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
116975ee267cSGleb Smirnoff #endif
1170fc74a9f9SBrooks Davis 			if_link_state_change(ifv->ifv_ifp,
117175ee267cSGleb Smirnoff 			    trunk->parent->if_link_state);
1172127d7b2dSAndre Oppermann 	}
117375ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
117475ee267cSGleb Smirnoff }
117575ee267cSGleb Smirnoff 
117675ee267cSGleb Smirnoff static void
117775ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
117875ee267cSGleb Smirnoff {
117975ee267cSGleb Smirnoff 	struct ifnet *p = PARENT(ifv);
118075ee267cSGleb Smirnoff 	struct ifnet *ifp = ifv->ifv_ifp;
118175ee267cSGleb Smirnoff 
118275ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(TRUNK(ifv));
118375ee267cSGleb Smirnoff 
118475ee267cSGleb Smirnoff 	/*
118575ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
118675ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
118775ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
118875ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
118975ee267cSGleb Smirnoff 	 */
119075ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
119175ee267cSGleb Smirnoff 		ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
119275ee267cSGleb Smirnoff 
119375ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
119475ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
119575ee267cSGleb Smirnoff 		ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
119675ee267cSGleb Smirnoff 		ifp->if_hwassist = p->if_hwassist;
119775ee267cSGleb Smirnoff 	} else {
119875ee267cSGleb Smirnoff 		ifp->if_capenable = 0;
119975ee267cSGleb Smirnoff 		ifp->if_hwassist = 0;
120075ee267cSGleb Smirnoff 	}
120175ee267cSGleb Smirnoff }
120275ee267cSGleb Smirnoff 
120375ee267cSGleb Smirnoff static void
120475ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
120575ee267cSGleb Smirnoff {
120675ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
120775ee267cSGleb Smirnoff 	struct ifvlan *ifv;
120875ee267cSGleb Smirnoff 	int i;
120975ee267cSGleb Smirnoff 
121075ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
121175ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
121275ee267cSGleb Smirnoff 	for (i = 0; i < EVL_VLID_MASK+1; i++)
121375ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
121475ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
121575ee267cSGleb Smirnoff #else
121675ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
121775ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
121875ee267cSGleb Smirnoff #endif
121975ee267cSGleb Smirnoff 			vlan_capabilities(ifv);
122075ee267cSGleb Smirnoff 	}
122175ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
1222127d7b2dSAndre Oppermann }
1223127d7b2dSAndre Oppermann 
1224a3814acfSSam Leffler static int
1225cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
12262cc2df49SGarrett Wollman {
12272cc2df49SGarrett Wollman 	struct ifaddr *ifa;
12282cc2df49SGarrett Wollman 	struct ifnet *p;
12292cc2df49SGarrett Wollman 	struct ifreq *ifr;
12302cc2df49SGarrett Wollman 	struct ifvlan *ifv;
12312cc2df49SGarrett Wollman 	struct vlanreq vlr;
12322cc2df49SGarrett Wollman 	int error = 0;
12332cc2df49SGarrett Wollman 
12342cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
12352cc2df49SGarrett Wollman 	ifa = (struct ifaddr *)data;
12362cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
12372cc2df49SGarrett Wollman 
12382cc2df49SGarrett Wollman 	switch (cmd) {
12392cc2df49SGarrett Wollman 	case SIOCSIFADDR:
12402cc2df49SGarrett Wollman 		ifp->if_flags |= IFF_UP;
12412cc2df49SGarrett Wollman 
12422cc2df49SGarrett Wollman 		switch (ifa->ifa_addr->sa_family) {
12432cc2df49SGarrett Wollman #ifdef INET
12442cc2df49SGarrett Wollman 		case AF_INET:
1245fc74a9f9SBrooks Davis 			arp_ifinit(ifv->ifv_ifp, ifa);
12462cc2df49SGarrett Wollman 			break;
12472cc2df49SGarrett Wollman #endif
12482cc2df49SGarrett Wollman 		default:
12492cc2df49SGarrett Wollman 			break;
12502cc2df49SGarrett Wollman 		}
12512cc2df49SGarrett Wollman 		break;
12522cc2df49SGarrett Wollman 
12532cc2df49SGarrett Wollman 	case SIOCGIFADDR:
12542cc2df49SGarrett Wollman 		{
12552cc2df49SGarrett Wollman 			struct sockaddr *sa;
12562cc2df49SGarrett Wollman 
12572cc2df49SGarrett Wollman 			sa = (struct sockaddr *) &ifr->ifr_data;
12584a0d6638SRuslan Ermilov 			bcopy(IF_LLADDR(ifp), (caddr_t)sa->sa_data,
125915a66c21SBruce M Simpson 			    ETHER_ADDR_LEN);
12602cc2df49SGarrett Wollman 		}
12612cc2df49SGarrett Wollman 		break;
12622cc2df49SGarrett Wollman 
1263b3cca108SBill Fenner 	case SIOCGIFMEDIA:
12644faedfe8SSam Leffler 		VLAN_LOCK();
126575ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
126675ee267cSGleb Smirnoff 			error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
12674faedfe8SSam Leffler 					SIOCGIFMEDIA, data);
12684faedfe8SSam Leffler 			VLAN_UNLOCK();
1269b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1270b3cca108SBill Fenner 			if (error == 0) {
1271b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1272b3cca108SBill Fenner 
1273b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1274b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1275b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1276b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1277b3cca108SBill Fenner 						ifmr->ifm_ulist,
1278b3cca108SBill Fenner 						sizeof(int));
1279b3cca108SBill Fenner 				}
1280b3cca108SBill Fenner 			}
12814faedfe8SSam Leffler 		} else {
12824faedfe8SSam Leffler 			VLAN_UNLOCK();
1283b3cca108SBill Fenner 			error = EINVAL;
12844faedfe8SSam Leffler 		}
1285b3cca108SBill Fenner 		break;
1286b3cca108SBill Fenner 
1287b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1288b3cca108SBill Fenner 		error = EINVAL;
1289b3cca108SBill Fenner 		break;
1290b3cca108SBill Fenner 
12912cc2df49SGarrett Wollman 	case SIOCSIFMTU:
12922cc2df49SGarrett Wollman 		/*
12932cc2df49SGarrett Wollman 		 * Set the interface MTU.
12942cc2df49SGarrett Wollman 		 */
12954faedfe8SSam Leffler 		VLAN_LOCK();
129675ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1297a3814acfSSam Leffler 			if (ifr->ifr_mtu >
129875ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1299a3814acfSSam Leffler 			    ifr->ifr_mtu <
1300a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
13012cc2df49SGarrett Wollman 				error = EINVAL;
1302a3814acfSSam Leffler 			else
13032cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1304a3814acfSSam Leffler 		} else
1305a3814acfSSam Leffler 			error = EINVAL;
13064faedfe8SSam Leffler 		VLAN_UNLOCK();
13072cc2df49SGarrett Wollman 		break;
13082cc2df49SGarrett Wollman 
13092cc2df49SGarrett Wollman 	case SIOCSETVLAN:
131015a66c21SBruce M Simpson 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
13112cc2df49SGarrett Wollman 		if (error)
13122cc2df49SGarrett Wollman 			break;
13132cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
13144faedfe8SSam Leffler 			VLAN_LOCK();
1315f731f104SBill Paul 			vlan_unconfig(ifp);
13164faedfe8SSam Leffler 			if (ifp->if_flags & IFF_UP)
13172cc2df49SGarrett Wollman 				if_down(ifp);
131813f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
13194faedfe8SSam Leffler 			VLAN_UNLOCK();
13202cc2df49SGarrett Wollman 			break;
13212cc2df49SGarrett Wollman 		}
13222cc2df49SGarrett Wollman 		p = ifunit(vlr.vlr_parent);
13232cc2df49SGarrett Wollman 		if (p == 0) {
13242cc2df49SGarrett Wollman 			error = ENOENT;
13252cc2df49SGarrett Wollman 			break;
13262cc2df49SGarrett Wollman 		}
1327fb88a3e0SBill Paul 		/*
1328fb88a3e0SBill Paul 		 * Don't let the caller set up a VLAN tag with
1329fb88a3e0SBill Paul 		 * anything except VLID bits.
1330fb88a3e0SBill Paul 		 */
1331fb88a3e0SBill Paul 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1332fb88a3e0SBill Paul 			error = EINVAL;
1333fb88a3e0SBill Paul 			break;
1334fb88a3e0SBill Paul 		}
133575ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
133675ee267cSGleb Smirnoff 		if (error)
13372cc2df49SGarrett Wollman 			break;
133813f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1339a3814acfSSam Leffler 
13401cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
13411cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
13422cc2df49SGarrett Wollman 		break;
13432cc2df49SGarrett Wollman 
13442cc2df49SGarrett Wollman 	case SIOCGETVLAN:
134515a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
13464faedfe8SSam Leffler 		VLAN_LOCK();
134775ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
134875ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
13499bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
13502cc2df49SGarrett Wollman 			vlr.vlr_tag = ifv->ifv_tag;
13512cc2df49SGarrett Wollman 		}
13524faedfe8SSam Leffler 		VLAN_UNLOCK();
135315a66c21SBruce M Simpson 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
13542cc2df49SGarrett Wollman 		break;
13552cc2df49SGarrett Wollman 
13562cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
13572cc2df49SGarrett Wollman 		/*
13581cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
13591cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
13602cc2df49SGarrett Wollman 		 */
136175ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
13621cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
13632cc2df49SGarrett Wollman 		break;
1364a3814acfSSam Leffler 
1365f731f104SBill Paul 	case SIOCADDMULTI:
1366f731f104SBill Paul 	case SIOCDELMULTI:
136775ee267cSGleb Smirnoff 		/*
136875ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
136975ee267cSGleb Smirnoff 		 * when we do.
137075ee267cSGleb Smirnoff 		 */
137175ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
1372f731f104SBill Paul 			error = vlan_setmulti(ifp);
1373f731f104SBill Paul 		break;
137475ee267cSGleb Smirnoff 
13752cc2df49SGarrett Wollman 	default:
13762cc2df49SGarrett Wollman 		error = EINVAL;
13772cc2df49SGarrett Wollman 	}
137815a66c21SBruce M Simpson 
137915a66c21SBruce M Simpson 	return (error);
13802cc2df49SGarrett Wollman }
1381