xref: /freebsd/sys/net/if_vlan.c (revision 3ba24fde11ab407fc492f5d99216ace74c9cde2b)
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  */
292cc2df49SGarrett Wollman 
302cc2df49SGarrett Wollman /*
312cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
322cc2df49SGarrett Wollman  * Might be extended some day to also handle IEEE 802.1p priority
332cc2df49SGarrett Wollman  * tagging.  This is sort of sneaky in the implementation, since
342cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
352cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
362cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
3746a32e61SPhilippe Charnier  * ether_output() left on our output queue when it calls
382cc2df49SGarrett Wollman  * if_start(), rewrite them for use by the real outgoing interface,
392cc2df49SGarrett Wollman  * and ask it to send them.
402cc2df49SGarrett Wollman  */
412cc2df49SGarrett Wollman 
428b2d9181SPyun YongHyeon #include <sys/cdefs.h>
438b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$");
448b2d9181SPyun YongHyeon 
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_dl.h>
652cc2df49SGarrett Wollman #include <net/if_types.h>
662cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
674b79449eSBjoern A. Zeeb #include <net/vnet.h>
682cc2df49SGarrett Wollman 
699d4fe4b2SBrooks Davis #define VLANNAME	"vlan"
7075ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
7164a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
7275ee267cSGleb Smirnoff 
732dc879b3SYaroslav Tykhiy #define	UP_AND_RUNNING(ifp) \
742dc879b3SYaroslav Tykhiy     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
752dc879b3SYaroslav Tykhiy 
7675ee267cSGleb Smirnoff LIST_HEAD(ifvlanhead, ifvlan);
7775ee267cSGleb Smirnoff 
7875ee267cSGleb Smirnoff struct ifvlantrunk {
7975ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
8075ee267cSGleb Smirnoff 	struct	rwlock	rw;
8175ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
825cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
835cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
8475ee267cSGleb Smirnoff #else
8575ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
8675ee267cSGleb Smirnoff 	uint16_t	hmask;
8775ee267cSGleb Smirnoff 	uint16_t	hwidth;
8875ee267cSGleb Smirnoff #endif
8975ee267cSGleb Smirnoff 	int		refcnt;
9075ee267cSGleb Smirnoff };
919d4fe4b2SBrooks Davis 
92a3814acfSSam Leffler struct vlan_mc_entry {
93a3814acfSSam Leffler 	struct ether_addr		mc_addr;
94a3814acfSSam Leffler 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
95a3814acfSSam Leffler };
96a3814acfSSam Leffler 
97a3814acfSSam Leffler struct	ifvlan {
9875ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
99fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
10075ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
10175ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
1021cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
103a3814acfSSam Leffler 	struct	ifv_linkmib {
104a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
105a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
106a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
10773f2233dSYaroslav Tykhiy 		uint16_t ifvm_proto;	/* encapsulation ethertype */
10875ee267cSGleb Smirnoff 		uint16_t ifvm_tag;	/* tag to apply on packets leaving if */
109a3814acfSSam Leffler 	}	ifv_mib;
110114c608cSYaroslav Tykhiy 	SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
111c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY
112a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
113c0cb022bSYaroslav Tykhiy #endif
114a3814acfSSam Leffler };
11573f2233dSYaroslav Tykhiy #define	ifv_proto	ifv_mib.ifvm_proto
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 
135f6e5e0adSYaroslav Tykhiy static int soft_pad = 0;
136f6e5e0adSYaroslav Tykhiy SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
137f6e5e0adSYaroslav Tykhiy 	   "pad short frames before tagging");
138f6e5e0adSYaroslav Tykhiy 
1395e17543aSBrooks Davis static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
1402cc2df49SGarrett Wollman 
1415cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
142ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag;
1435cb8c31aSYaroslav Tykhiy 
1444faedfe8SSam Leffler /*
14575ee267cSGleb Smirnoff  * We have a global mutex, that is used to serialize configuration
14675ee267cSGleb Smirnoff  * changes and isn't used in normal packet delivery.
14775ee267cSGleb Smirnoff  *
14875ee267cSGleb Smirnoff  * We also have a per-trunk rwlock, that is locked shared on packet
14975ee267cSGleb Smirnoff  * processing and exclusive when configuration is changed.
15075ee267cSGleb Smirnoff  *
15175ee267cSGleb Smirnoff  * The VLAN_ARRAY substitutes the dynamic hash with a static array
152ad387028SAndrew Thompson  * with 4096 entries. In theory this can give a boost in processing,
15375ee267cSGleb Smirnoff  * however on practice it does not. Probably this is because array
15475ee267cSGleb Smirnoff  * is too big to fit into CPU cache.
1554faedfe8SSam Leffler  */
1564faedfe8SSam Leffler static struct mtx ifv_mtx;
15775ee267cSGleb Smirnoff #define	VLAN_LOCK_INIT()	mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
1584faedfe8SSam Leffler #define	VLAN_LOCK_DESTROY()	mtx_destroy(&ifv_mtx)
1594faedfe8SSam Leffler #define	VLAN_LOCK_ASSERT()	mtx_assert(&ifv_mtx, MA_OWNED)
1604faedfe8SSam Leffler #define	VLAN_LOCK()		mtx_lock(&ifv_mtx)
1614faedfe8SSam Leffler #define	VLAN_UNLOCK()		mtx_unlock(&ifv_mtx)
16275ee267cSGleb Smirnoff #define	TRUNK_LOCK_INIT(trunk)	rw_init(&(trunk)->rw, VLANNAME)
16375ee267cSGleb Smirnoff #define	TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
16475ee267cSGleb Smirnoff #define	TRUNK_LOCK(trunk)	rw_wlock(&(trunk)->rw)
16575ee267cSGleb Smirnoff #define	TRUNK_UNLOCK(trunk)	rw_wunlock(&(trunk)->rw)
16675ee267cSGleb Smirnoff #define	TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
16775ee267cSGleb Smirnoff #define	TRUNK_RLOCK(trunk)	rw_rlock(&(trunk)->rw)
16875ee267cSGleb Smirnoff #define	TRUNK_RUNLOCK(trunk)	rw_runlock(&(trunk)->rw)
16975ee267cSGleb Smirnoff #define	TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
17075ee267cSGleb Smirnoff 
17175ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
17275ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
17375ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
17475ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
17575ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
17675ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
17775ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
17875ee267cSGleb Smirnoff 	uint16_t tag);
17975ee267cSGleb Smirnoff #endif
18075ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
1814faedfe8SSam Leffler 
1822cc2df49SGarrett Wollman static	void vlan_start(struct ifnet *ifp);
183114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
184a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
185cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
1861cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
1871cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
1881cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
189f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
1906f359e28SJohn Baldwin static	void vlan_unconfig(struct ifnet *ifp);
1916f359e28SJohn Baldwin static	void vlan_unconfig_locked(struct ifnet *ifp);
19275ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
193a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
19475ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
19575ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
196f731f104SBill Paul 
197f889d2efSBrooks Davis static	struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
198f889d2efSBrooks Davis     const char *, int *);
199f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
2006b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
201f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
202f889d2efSBrooks Davis 
2035cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
204ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
2055cb8c31aSYaroslav Tykhiy 
206c6e6ca3eSYaroslav Tykhiy static	struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
207c6e6ca3eSYaroslav Tykhiy     IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
2089d4fe4b2SBrooks Davis 
20975ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
21075ee267cSGleb Smirnoff #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
211114c608cSYaroslav Tykhiy 
21275ee267cSGleb Smirnoff static void
21375ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
21475ee267cSGleb Smirnoff {
21575ee267cSGleb Smirnoff 	int i, n;
21675ee267cSGleb Smirnoff 
21775ee267cSGleb Smirnoff 	/*
21875ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
21975ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
22075ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
22175ee267cSGleb Smirnoff 	 */
22275ee267cSGleb Smirnoff 
22375ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
22475ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
22575ee267cSGleb Smirnoff 
22675ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
22775ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
22875ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
22975ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
23075ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
23175ee267cSGleb Smirnoff 		LIST_INIT(&trunk->hash[i]);
23275ee267cSGleb Smirnoff }
23375ee267cSGleb Smirnoff 
23475ee267cSGleb Smirnoff static void
23575ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
23675ee267cSGleb Smirnoff {
23775ee267cSGleb Smirnoff #ifdef INVARIANTS
23875ee267cSGleb Smirnoff 	int i;
23975ee267cSGleb Smirnoff 
24075ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
24175ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
24275ee267cSGleb Smirnoff 		KASSERT(LIST_EMPTY(&trunk->hash[i]),
24375ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
24475ee267cSGleb Smirnoff #endif
24575ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
24675ee267cSGleb Smirnoff 	trunk->hash = NULL;
24775ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
24875ee267cSGleb Smirnoff }
24975ee267cSGleb Smirnoff 
25075ee267cSGleb Smirnoff static int
25175ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
25275ee267cSGleb Smirnoff {
25375ee267cSGleb Smirnoff 	int i, b;
25475ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
25575ee267cSGleb Smirnoff 
25675ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
25775ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
25875ee267cSGleb Smirnoff 
25975ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
26075ee267cSGleb Smirnoff 	i = HASH(ifv->ifv_tag, trunk->hmask);
26175ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
26275ee267cSGleb Smirnoff 		if (ifv->ifv_tag == ifv2->ifv_tag)
26375ee267cSGleb Smirnoff 			return (EEXIST);
26475ee267cSGleb Smirnoff 
26575ee267cSGleb Smirnoff 	/*
26675ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
26775ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
26875ee267cSGleb Smirnoff 	 * buckets/2.
26975ee267cSGleb Smirnoff 	 */
27075ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
27175ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
27275ee267cSGleb Smirnoff 		i = HASH(ifv->ifv_tag, trunk->hmask);
27375ee267cSGleb Smirnoff 	}
27475ee267cSGleb Smirnoff 	LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
27575ee267cSGleb Smirnoff 	trunk->refcnt++;
27675ee267cSGleb Smirnoff 
27775ee267cSGleb Smirnoff 	return (0);
27875ee267cSGleb Smirnoff }
27975ee267cSGleb Smirnoff 
28075ee267cSGleb Smirnoff static int
28175ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
28275ee267cSGleb Smirnoff {
28375ee267cSGleb Smirnoff 	int i, b;
28475ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
28575ee267cSGleb Smirnoff 
28675ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
28775ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
28875ee267cSGleb Smirnoff 
28975ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
29075ee267cSGleb Smirnoff 	i = HASH(ifv->ifv_tag, trunk->hmask);
29175ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
29275ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
29375ee267cSGleb Smirnoff 			trunk->refcnt--;
29475ee267cSGleb Smirnoff 			LIST_REMOVE(ifv2, ifv_list);
29575ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
29675ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
29775ee267cSGleb Smirnoff 			return (0);
29875ee267cSGleb Smirnoff 		}
29975ee267cSGleb Smirnoff 
30075ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
30175ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
30275ee267cSGleb Smirnoff }
30375ee267cSGleb Smirnoff 
30475ee267cSGleb Smirnoff /*
30575ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
30675ee267cSGleb Smirnoff  */
30775ee267cSGleb Smirnoff static void
30875ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
30975ee267cSGleb Smirnoff {
31075ee267cSGleb Smirnoff 	struct ifvlan *ifv;
31175ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
31275ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
31375ee267cSGleb Smirnoff 
31475ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
31575ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
31675ee267cSGleb Smirnoff 
31775ee267cSGleb Smirnoff 	if (howmuch == 0) {
31875ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
31975ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
32075ee267cSGleb Smirnoff 		return;
32175ee267cSGleb Smirnoff 	}
32275ee267cSGleb Smirnoff 
32375ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
32475ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
32575ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
32675ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
32775ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
32875ee267cSGleb Smirnoff 		return;
32975ee267cSGleb Smirnoff 
33075ee267cSGleb Smirnoff 	/* M_NOWAIT because we're called with trunk mutex held */
33175ee267cSGleb Smirnoff 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
33275ee267cSGleb Smirnoff 	if (hash2 == NULL) {
33375ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
33475ee267cSGleb Smirnoff 		    __func__);
33575ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
33675ee267cSGleb Smirnoff 	}
33775ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
33875ee267cSGleb Smirnoff 		LIST_INIT(&hash2[j]);
33975ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
340c0cb022bSYaroslav Tykhiy 		while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
34175ee267cSGleb Smirnoff 			LIST_REMOVE(ifv, ifv_list);
34275ee267cSGleb Smirnoff 			j = HASH(ifv->ifv_tag, n2 - 1);
34375ee267cSGleb Smirnoff 			LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
34475ee267cSGleb Smirnoff 		}
34575ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
34675ee267cSGleb Smirnoff 	trunk->hash = hash2;
34775ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
34875ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
349f84b2d69SYaroslav Tykhiy 
350f84b2d69SYaroslav Tykhiy 	if (bootverbose)
351f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
352f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
35375ee267cSGleb Smirnoff }
35475ee267cSGleb Smirnoff 
35575ee267cSGleb Smirnoff static __inline struct ifvlan *
35675ee267cSGleb Smirnoff vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
35775ee267cSGleb Smirnoff {
35875ee267cSGleb Smirnoff 	struct ifvlan *ifv;
35975ee267cSGleb Smirnoff 
36075ee267cSGleb Smirnoff 	TRUNK_LOCK_RASSERT(trunk);
36175ee267cSGleb Smirnoff 
36275ee267cSGleb Smirnoff 	LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
36375ee267cSGleb Smirnoff 		if (ifv->ifv_tag == tag)
36475ee267cSGleb Smirnoff 			return (ifv);
36575ee267cSGleb Smirnoff 	return (NULL);
36675ee267cSGleb Smirnoff }
36775ee267cSGleb Smirnoff 
36875ee267cSGleb Smirnoff #if 0
36975ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
37075ee267cSGleb Smirnoff static void
37175ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
37275ee267cSGleb Smirnoff {
37375ee267cSGleb Smirnoff 	int i;
37475ee267cSGleb Smirnoff 	struct ifvlan *ifv;
37575ee267cSGleb Smirnoff 
37675ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
37775ee267cSGleb Smirnoff 		printf("%d: ", i);
37875ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
37975ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
38075ee267cSGleb Smirnoff 		printf("\n");
38175ee267cSGleb Smirnoff 	}
38275ee267cSGleb Smirnoff }
38375ee267cSGleb Smirnoff #endif /* 0 */
38475ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
38575ee267cSGleb Smirnoff 
38675ee267cSGleb Smirnoff static void
38775ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
38875ee267cSGleb Smirnoff {
38975ee267cSGleb Smirnoff 	VLAN_LOCK_ASSERT();
39075ee267cSGleb Smirnoff 
39175ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
39275ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
39375ee267cSGleb Smirnoff 	vlan_freehash(trunk);
39475ee267cSGleb Smirnoff #endif
39575ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
39633499e2aSYaroslav Tykhiy 	TRUNK_UNLOCK(trunk);
39733499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
39875ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
39975ee267cSGleb Smirnoff }
40075ee267cSGleb Smirnoff 
401f731f104SBill Paul /*
402f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
403f731f104SBill Paul  * programming the multicast filter of the parent. This has the
404f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
405f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
406f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
407f731f104SBill Paul  * to avoid this: there really is only one physical interface.
40829c2dfbeSBruce M Simpson  *
40929c2dfbeSBruce M Simpson  * XXX: There is a possible race here if more than one thread is
41029c2dfbeSBruce M Simpson  *      modifying the multicast state of the vlan interface at the same time.
411f731f104SBill Paul  */
4122b120974SPeter Wemm static int
4132b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
414f731f104SBill Paul {
415f731f104SBill Paul 	struct ifnet		*ifp_p;
416f731f104SBill Paul 	struct ifmultiaddr	*ifma, *rifma = NULL;
417f731f104SBill Paul 	struct ifvlan		*sc;
418c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
419f731f104SBill Paul 	struct sockaddr_dl	sdl;
420f731f104SBill Paul 	int			error;
421f731f104SBill Paul 
42229c2dfbeSBruce M Simpson 	/*VLAN_LOCK_ASSERT();*/
42329c2dfbeSBruce M Simpson 
424f731f104SBill Paul 	/* Find the parent. */
425f731f104SBill Paul 	sc = ifp->if_softc;
42675ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
4271b2a4f7aSBill Fenner 
4288b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
4298b615593SMarko Zec 
43015a66c21SBruce M Simpson 	bzero((char *)&sdl, sizeof(sdl));
43115a66c21SBruce M Simpson 	sdl.sdl_len = sizeof(sdl);
432f731f104SBill Paul 	sdl.sdl_family = AF_LINK;
43326e30963SBill Fenner 	sdl.sdl_index = ifp_p->if_index;
43426e30963SBill Fenner 	sdl.sdl_type = IFT_ETHER;
43524993214SYaroslav Tykhiy 	sdl.sdl_alen = ETHER_ADDR_LEN;
436f731f104SBill Paul 
437f731f104SBill Paul 	/* First, remove any existing filter entries. */
438c0cb022bSYaroslav Tykhiy 	while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
439f731f104SBill Paul 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
440f731f104SBill Paul 		error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
441f731f104SBill Paul 		if (error)
442f731f104SBill Paul 			return (error);
443f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
4449d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
445f731f104SBill Paul 	}
446f731f104SBill Paul 
447f731f104SBill Paul 	/* Now program new ones. */
4486817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
449f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
450f731f104SBill Paul 			continue;
45129c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
45229c2dfbeSBruce M Simpson 		if (mc == NULL)
45329c2dfbeSBruce M Simpson 			return (ENOMEM);
454f731f104SBill Paul 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
455f731f104SBill Paul 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
456f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
45726e30963SBill Fenner 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
45826e30963SBill Fenner 		    LLADDR(&sdl), ETHER_ADDR_LEN);
459f731f104SBill Paul 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
460f731f104SBill Paul 		if (error)
461f731f104SBill Paul 			return (error);
462f731f104SBill Paul 	}
463f731f104SBill Paul 
4648b615593SMarko Zec 	CURVNET_RESTORE();
465f731f104SBill Paul 	return (0);
466f731f104SBill Paul }
4672cc2df49SGarrett Wollman 
468a3814acfSSam Leffler /*
469ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
470ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
471ea4ca115SAndrew Thompson  * should also change it on all children vlans.
472ea4ca115SAndrew Thompson  */
473ea4ca115SAndrew Thompson static void
474ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
475ea4ca115SAndrew Thompson {
476ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
4776117727bSAndrew Thompson #ifndef VLAN_ARRAY
4786117727bSAndrew Thompson 	struct ifvlan *next;
4796117727bSAndrew Thompson #endif
480ea4ca115SAndrew Thompson 	int i;
481ea4ca115SAndrew Thompson 
482ea4ca115SAndrew Thompson 	/*
483ea4ca115SAndrew Thompson 	 * Check if it's a trunk interface first of all
484ea4ca115SAndrew Thompson 	 * to avoid needless locking.
485ea4ca115SAndrew Thompson 	 */
486ea4ca115SAndrew Thompson 	if (ifp->if_vlantrunk == NULL)
487ea4ca115SAndrew Thompson 		return;
488ea4ca115SAndrew Thompson 
489ea4ca115SAndrew Thompson 	VLAN_LOCK();
490ea4ca115SAndrew Thompson 	/*
491ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
492ea4ca115SAndrew Thompson 	 */
493ea4ca115SAndrew Thompson #ifdef VLAN_ARRAY
494ea4ca115SAndrew Thompson 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
4956117727bSAndrew Thompson 		if ((ifv = ifp->if_vlantrunk->vlans[i])) {
496ea4ca115SAndrew Thompson #else /* VLAN_ARRAY */
497ea4ca115SAndrew Thompson 	for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
4986117727bSAndrew Thompson 		LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
499ea4ca115SAndrew Thompson #endif /* VLAN_ARRAY */
500ea4ca115SAndrew Thompson 			VLAN_UNLOCK();
5016117727bSAndrew Thompson 			if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
5026117727bSAndrew Thompson 			VLAN_LOCK();
5036117727bSAndrew Thompson 		}
5046117727bSAndrew Thompson 	VLAN_UNLOCK();
505ea4ca115SAndrew Thompson 
506ea4ca115SAndrew Thompson }
507ea4ca115SAndrew Thompson 
508ea4ca115SAndrew Thompson /*
5095cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
5105cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
5115cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
5125428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
5135428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
5145cb8c31aSYaroslav Tykhiy  */
5155cb8c31aSYaroslav Tykhiy static void
5165cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
5175cb8c31aSYaroslav Tykhiy {
5185cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
5195cb8c31aSYaroslav Tykhiy 	int i;
5205cb8c31aSYaroslav Tykhiy 
5215cb8c31aSYaroslav Tykhiy 	/*
5225cb8c31aSYaroslav Tykhiy 	 * Check if it's a trunk interface first of all
5235cb8c31aSYaroslav Tykhiy 	 * to avoid needless locking.
5245cb8c31aSYaroslav Tykhiy 	 */
5255cb8c31aSYaroslav Tykhiy 	if (ifp->if_vlantrunk == NULL)
5265cb8c31aSYaroslav Tykhiy 		return;
5275cb8c31aSYaroslav Tykhiy 
5285428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
5295428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
5305428776eSJohn Baldwin 		return;
5315428776eSJohn Baldwin 
5325cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
5335cb8c31aSYaroslav Tykhiy 	/*
5345cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
5355cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
5365cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
5375cb8c31aSYaroslav Tykhiy 	 */
5385cb8c31aSYaroslav Tykhiy #ifdef VLAN_ARRAY
5395cb8c31aSYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
5405cb8c31aSYaroslav Tykhiy 		if ((ifv = ifp->if_vlantrunk->vlans[i])) {
5415cb8c31aSYaroslav Tykhiy 			vlan_unconfig_locked(ifv->ifv_ifp);
5425cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk == NULL)
5435cb8c31aSYaroslav Tykhiy 				break;
5445cb8c31aSYaroslav Tykhiy 		}
5455cb8c31aSYaroslav Tykhiy #else /* VLAN_ARRAY */
5465cb8c31aSYaroslav Tykhiy restart:
5475cb8c31aSYaroslav Tykhiy 	for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
5485cb8c31aSYaroslav Tykhiy 		if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
5495cb8c31aSYaroslav Tykhiy 			vlan_unconfig_locked(ifv->ifv_ifp);
5505cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk)
5515cb8c31aSYaroslav Tykhiy 				goto restart;	/* trunk->hwidth can change */
5525cb8c31aSYaroslav Tykhiy 			else
5535cb8c31aSYaroslav Tykhiy 				break;
5545cb8c31aSYaroslav Tykhiy 		}
5555cb8c31aSYaroslav Tykhiy #endif /* VLAN_ARRAY */
5565cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
5575cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
5585cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
5595cb8c31aSYaroslav Tykhiy }
5605cb8c31aSYaroslav Tykhiy 
5615cb8c31aSYaroslav Tykhiy /*
562a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
563a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
564a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
565a3814acfSSam Leffler  * set here.  Noone else in the system should be aware of this so
566a3814acfSSam Leffler  * we use an explicit reference here.
567a3814acfSSam Leffler  */
568a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
569a3814acfSSam Leffler 
570984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
571a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
572127d7b2dSAndre Oppermann 
5732b120974SPeter Wemm static int
5742b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
5752b120974SPeter Wemm {
5769d4fe4b2SBrooks Davis 
5772b120974SPeter Wemm 	switch (type) {
5782b120974SPeter Wemm 	case MOD_LOAD:
5795cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
5805cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
5815cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
5825cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
583ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
584ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
585ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
586ea4ca115SAndrew Thompson 			return (ENOMEM);
5874faedfe8SSam Leffler 		VLAN_LOCK_INIT();
5889d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
589127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
59075ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
5919d4fe4b2SBrooks Davis 		if_clone_attach(&vlan_cloner);
59225c0f7b3SYaroslav Tykhiy 		if (bootverbose)
59325c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
59425c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
59525c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
59625c0f7b3SYaroslav Tykhiy #else
59725c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
59825c0f7b3SYaroslav Tykhiy #endif
59925c0f7b3SYaroslav Tykhiy 
60025c0f7b3SYaroslav Tykhiy 			       "\n");
6012b120974SPeter Wemm 		break;
6022b120974SPeter Wemm 	case MOD_UNLOAD:
6039d4fe4b2SBrooks Davis 		if_clone_detach(&vlan_cloner);
6045cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
605ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
6069d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
607127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
60875ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
6094faedfe8SSam Leffler 		VLAN_LOCK_DESTROY();
61025c0f7b3SYaroslav Tykhiy 		if (bootverbose)
61125c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
6129d4fe4b2SBrooks Davis 		break;
6133e019deaSPoul-Henning Kamp 	default:
6143e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
6152b120974SPeter Wemm 	}
61615a66c21SBruce M Simpson 	return (0);
6172b120974SPeter Wemm }
6182b120974SPeter Wemm 
6192b120974SPeter Wemm static moduledata_t vlan_mod = {
6202b120974SPeter Wemm 	"if_vlan",
6212b120974SPeter Wemm 	vlan_modevent,
6222b120974SPeter Wemm 	0
6232b120974SPeter Wemm };
6242b120974SPeter Wemm 
6252b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
62611edc477SEd Maste MODULE_VERSION(if_vlan, 3);
6272cc2df49SGarrett Wollman 
628f889d2efSBrooks Davis static struct ifnet *
629f889d2efSBrooks Davis vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
6309d4fe4b2SBrooks Davis {
631f889d2efSBrooks Davis 	const char *cp;
632f889d2efSBrooks Davis 	struct ifnet *ifp;
633fb92ad4aSJohn Baldwin 	int t;
634f889d2efSBrooks Davis 
635f889d2efSBrooks Davis 	/* Check for <etherif>.<vlan> style interface names. */
63677dfcdc4SRobert Watson 	IFNET_RLOCK_NOSLEEP();
637603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
638f889d2efSBrooks Davis 		if (ifp->if_type != IFT_ETHER)
639f889d2efSBrooks Davis 			continue;
640f889d2efSBrooks Davis 		if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
641f889d2efSBrooks Davis 			continue;
642f889d2efSBrooks Davis 		cp = name + strlen(ifp->if_xname);
643fb92ad4aSJohn Baldwin 		if (*cp++ != '.')
644f889d2efSBrooks Davis 			continue;
645fb92ad4aSJohn Baldwin 		if (*cp == '\0')
646f889d2efSBrooks Davis 			continue;
647fb92ad4aSJohn Baldwin 		t = 0;
648fb92ad4aSJohn Baldwin 		for(; *cp >= '0' && *cp <= '9'; cp++)
649f889d2efSBrooks Davis 			t = (t * 10) + (*cp - '0');
650fb92ad4aSJohn Baldwin 		if (*cp != '\0')
651fb92ad4aSJohn Baldwin 			continue;
652f889d2efSBrooks Davis 		if (tag != NULL)
653f889d2efSBrooks Davis 			*tag = t;
654f889d2efSBrooks Davis 		break;
655f889d2efSBrooks Davis 	}
65677dfcdc4SRobert Watson 	IFNET_RUNLOCK_NOSLEEP();
657f889d2efSBrooks Davis 
65815a66c21SBruce M Simpson 	return (ifp);
659f889d2efSBrooks Davis }
660f889d2efSBrooks Davis 
661f889d2efSBrooks Davis static int
662f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
663f889d2efSBrooks Davis {
664f889d2efSBrooks Davis 	const char *cp;
665f889d2efSBrooks Davis 
666f889d2efSBrooks Davis 	if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
667f889d2efSBrooks Davis 		return (1);
668f889d2efSBrooks Davis 
669f889d2efSBrooks Davis 	if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
670f889d2efSBrooks Davis 		return (0);
671f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
672f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
673f889d2efSBrooks Davis 			return (0);
674f889d2efSBrooks Davis 	}
675f889d2efSBrooks Davis 
676f889d2efSBrooks Davis 	return (1);
677f889d2efSBrooks Davis }
678f889d2efSBrooks Davis 
679f889d2efSBrooks Davis static int
6806b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
681f889d2efSBrooks Davis {
682f889d2efSBrooks Davis 	char *dp;
683f889d2efSBrooks Davis 	int wildcard;
684f889d2efSBrooks Davis 	int unit;
685f889d2efSBrooks Davis 	int error;
686f889d2efSBrooks Davis 	int tag;
687f889d2efSBrooks Davis 	int ethertag;
6889d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
6899d4fe4b2SBrooks Davis 	struct ifnet *ifp;
690f889d2efSBrooks Davis 	struct ifnet *p;
691*3ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
692*3ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
6936b7330e2SSam Leffler 	struct vlanreq vlr;
69465239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
695f889d2efSBrooks Davis 
6966b7330e2SSam Leffler 	/*
6976b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
6986b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
6996b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
7006b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
7016b7330e2SSam Leffler 	 *   must be configured separately.
7026b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
7036b7330e2SSam Leffler 	 * supported for backwards compatibilty.
7046b7330e2SSam Leffler 	 */
7056b7330e2SSam Leffler 	if (params) {
7066b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
7076b7330e2SSam Leffler 		if (error)
7086b7330e2SSam Leffler 			return error;
7096b7330e2SSam Leffler 		p = ifunit(vlr.vlr_parent);
7106b7330e2SSam Leffler 		if (p == NULL)
7116b7330e2SSam Leffler 			return ENXIO;
7126b7330e2SSam Leffler 		/*
7136b7330e2SSam Leffler 		 * Don't let the caller set up a VLAN tag with
7146b7330e2SSam Leffler 		 * anything except VLID bits.
7156b7330e2SSam Leffler 		 */
7166b7330e2SSam Leffler 		if (vlr.vlr_tag & ~EVL_VLID_MASK)
7176b7330e2SSam Leffler 			return (EINVAL);
7186b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
7196b7330e2SSam Leffler 		if (error != 0)
7206b7330e2SSam Leffler 			return (error);
7216b7330e2SSam Leffler 
7226b7330e2SSam Leffler 		ethertag = 1;
7236b7330e2SSam Leffler 		tag = vlr.vlr_tag;
7246b7330e2SSam Leffler 		wildcard = (unit < 0);
7256b7330e2SSam Leffler 	} else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
726f889d2efSBrooks Davis 		ethertag = 1;
727f889d2efSBrooks Davis 		unit = -1;
728f889d2efSBrooks Davis 		wildcard = 0;
729f889d2efSBrooks Davis 
730f889d2efSBrooks Davis 		/*
731f889d2efSBrooks Davis 		 * Don't let the caller set up a VLAN tag with
732f889d2efSBrooks Davis 		 * anything except VLID bits.
733f889d2efSBrooks Davis 		 */
73415a66c21SBruce M Simpson 		if (tag & ~EVL_VLID_MASK)
735f889d2efSBrooks Davis 			return (EINVAL);
736f889d2efSBrooks Davis 	} else {
737f889d2efSBrooks Davis 		ethertag = 0;
738f889d2efSBrooks Davis 
739f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
740f889d2efSBrooks Davis 		if (error != 0)
741f889d2efSBrooks Davis 			return (error);
742f889d2efSBrooks Davis 
743f889d2efSBrooks Davis 		wildcard = (unit < 0);
744f889d2efSBrooks Davis 	}
745f889d2efSBrooks Davis 
746f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
747f889d2efSBrooks Davis 	if (error != 0)
748f889d2efSBrooks Davis 		return (error);
749f889d2efSBrooks Davis 
750f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
751f889d2efSBrooks Davis 	if (wildcard) {
752f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
753f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
754f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
755f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
756f889d2efSBrooks Davis 		}
757f889d2efSBrooks Davis 	}
7589d4fe4b2SBrooks Davis 
759a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
760fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
761fc74a9f9SBrooks Davis 	if (ifp == NULL) {
762fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
763fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
764fc74a9f9SBrooks Davis 		return (ENOSPC);
765fc74a9f9SBrooks Davis 	}
7669d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
7679d4fe4b2SBrooks Davis 
7689d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
769f889d2efSBrooks Davis 	/*
770cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
771f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
772f889d2efSBrooks Davis 	 */
773f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
774f889d2efSBrooks Davis 	ifp->if_dname = ifc->ifc_name;
775f889d2efSBrooks Davis 	ifp->if_dunit = unit;
7769d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
7779d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
77815a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
7799d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
7809d4fe4b2SBrooks Davis 
781114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
7829d4fe4b2SBrooks Davis 	ifp->if_start = vlan_start;
7839d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
7849d4fe4b2SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
78564a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
786fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
7879d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
788211f625aSBill Fenner 	ifp->if_baudrate = 0;
789a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
790a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
791*3ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
792*3ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
793*3ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
7949d4fe4b2SBrooks Davis 
795f889d2efSBrooks Davis 	if (ethertag) {
79675ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, tag);
797f889d2efSBrooks Davis 		if (error != 0) {
798f889d2efSBrooks Davis 			/*
799f889d2efSBrooks Davis 			 * Since we've partialy failed, we need to back
800f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
801f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
802f889d2efSBrooks Davis 			 */
803f889d2efSBrooks Davis 			ether_ifdetach(ifp);
804249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
805fc74a9f9SBrooks Davis 			if_free_type(ifp, IFT_ETHER);
80696c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
807f889d2efSBrooks Davis 			free(ifv, M_VLAN);
808f889d2efSBrooks Davis 
809f889d2efSBrooks Davis 			return (error);
810f889d2efSBrooks Davis 		}
811f889d2efSBrooks Davis 
8121cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
8131cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
814f889d2efSBrooks Davis 	}
815f889d2efSBrooks Davis 
8169d4fe4b2SBrooks Davis 	return (0);
8179d4fe4b2SBrooks Davis }
8189d4fe4b2SBrooks Davis 
819f889d2efSBrooks Davis static int
820f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
8219d4fe4b2SBrooks Davis {
8229d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
823114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
824b4e9f837SBrooks Davis 
825249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
826249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
827f3447eb4SBrooks Davis 	if_free_type(ifp, IFT_ETHER);
8289d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
829b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
830b4e9f837SBrooks Davis 
831f889d2efSBrooks Davis 	return (0);
8329d4fe4b2SBrooks Davis }
8339d4fe4b2SBrooks Davis 
83415a66c21SBruce M Simpson /*
83515a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
83615a66c21SBruce M Simpson  */
8372cc2df49SGarrett Wollman static void
838114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
8392cc2df49SGarrett Wollman {
8402cc2df49SGarrett Wollman }
8412cc2df49SGarrett Wollman 
8426d3a3ab7SGleb Smirnoff /*
8436d3a3ab7SGleb Smirnoff  * The if_start method for vlan(4) interface. It doesn't
8446d3a3ab7SGleb Smirnoff  * raises the IFF_DRV_OACTIVE flag, since it is called
8456d3a3ab7SGleb Smirnoff  * only from IFQ_HANDOFF() macro in ether_output_frame().
8466d3a3ab7SGleb Smirnoff  * If the interface queue is full, and vlan_start() is
8476d3a3ab7SGleb Smirnoff  * not called, the queue would never get emptied and
8486d3a3ab7SGleb Smirnoff  * interface would stall forever.
8496d3a3ab7SGleb Smirnoff  */
8502cc2df49SGarrett Wollman static void
8512cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp)
8522cc2df49SGarrett Wollman {
8532cc2df49SGarrett Wollman 	struct ifvlan *ifv;
8542cc2df49SGarrett Wollman 	struct ifnet *p;
8555326b23cSMatthew N. Dodd 	struct mbuf *m;
856affc907dSMax Laier 	int error;
8572cc2df49SGarrett Wollman 
8582cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
85975ee267cSGleb Smirnoff 	p = PARENT(ifv);
8602cc2df49SGarrett Wollman 
8612cc2df49SGarrett Wollman 	for (;;) {
8622cc2df49SGarrett Wollman 		IF_DEQUEUE(&ifp->if_snd, m);
8632dc879b3SYaroslav Tykhiy 		if (m == NULL)
8642cc2df49SGarrett Wollman 			break;
865a3814acfSSam Leffler 		BPF_MTAP(ifp, m);
8662cc2df49SGarrett Wollman 
867f731f104SBill Paul 		/*
86824993214SYaroslav Tykhiy 		 * Do not run parent's if_start() if the parent is not up,
86924993214SYaroslav Tykhiy 		 * or parent's driver will cause a system crash.
87024993214SYaroslav Tykhiy 		 */
8712dc879b3SYaroslav Tykhiy 		if (!UP_AND_RUNNING(p)) {
87224993214SYaroslav Tykhiy 			m_freem(m);
873a3814acfSSam Leffler 			ifp->if_collisions++;
87424993214SYaroslav Tykhiy 			continue;
87524993214SYaroslav Tykhiy 		}
87624993214SYaroslav Tykhiy 
87724993214SYaroslav Tykhiy 		/*
878f6e5e0adSYaroslav Tykhiy 		 * Pad the frame to the minimum size allowed if told to.
879f6e5e0adSYaroslav Tykhiy 		 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
880f6e5e0adSYaroslav Tykhiy 		 * paragraph C.4.4.3.b.  It can help to work around buggy
881f6e5e0adSYaroslav Tykhiy 		 * bridges that violate paragraph C.4.4.3.a from the same
882f6e5e0adSYaroslav Tykhiy 		 * document, i.e., fail to pad short frames after untagging.
883f6e5e0adSYaroslav Tykhiy 		 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
884f6e5e0adSYaroslav Tykhiy 		 * untagging it will produce a 62-byte frame, which is a runt
885f6e5e0adSYaroslav Tykhiy 		 * and requires padding.  There are VLAN-enabled network
886f6e5e0adSYaroslav Tykhiy 		 * devices that just discard such runts instead or mishandle
887f6e5e0adSYaroslav Tykhiy 		 * them somehow.
888f6e5e0adSYaroslav Tykhiy 		 */
889f6e5e0adSYaroslav Tykhiy 		if (soft_pad) {
890f6e5e0adSYaroslav Tykhiy 			static char pad[8];	/* just zeros */
891f6e5e0adSYaroslav Tykhiy 			int n;
892f6e5e0adSYaroslav Tykhiy 
893f6e5e0adSYaroslav Tykhiy 			for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
894f6e5e0adSYaroslav Tykhiy 			     n > 0; n -= sizeof(pad))
895f6e5e0adSYaroslav Tykhiy 				if (!m_append(m, min(n, sizeof(pad)), pad))
896f6e5e0adSYaroslav Tykhiy 					break;
897f6e5e0adSYaroslav Tykhiy 
898f6e5e0adSYaroslav Tykhiy 			if (n > 0) {
899f6e5e0adSYaroslav Tykhiy 				if_printf(ifp, "cannot pad short frame\n");
900f6e5e0adSYaroslav Tykhiy 				ifp->if_oerrors++;
901f6e5e0adSYaroslav Tykhiy 				m_freem(m);
902f6e5e0adSYaroslav Tykhiy 				continue;
903f6e5e0adSYaroslav Tykhiy 			}
904f6e5e0adSYaroslav Tykhiy 		}
905f6e5e0adSYaroslav Tykhiy 
906f6e5e0adSYaroslav Tykhiy 		/*
907a3814acfSSam Leffler 		 * If underlying interface can do VLAN tag insertion itself,
908a3814acfSSam Leffler 		 * just pass the packet along. However, we need some way to
909a3814acfSSam Leffler 		 * tell the interface where the packet came from so that it
910a3814acfSSam Leffler 		 * knows how to find the VLAN tag to use, so we attach a
911a3814acfSSam Leffler 		 * packet tag that holds it.
912f731f104SBill Paul 		 */
913b08347a0SYaroslav Tykhiy 		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
91478ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = ifv->ifv_tag;
915ba26134bSGleb Smirnoff 			m->m_flags |= M_VLANTAG;
916f731f104SBill Paul 		} else {
91760e87ca8SAndrew Thompson 			m = ether_vlanencap(m, ifv->ifv_tag);
9184af90a4dSMatthew N. Dodd 			if (m == NULL) {
9196cbd3e99SYaroslav Tykhiy 				if_printf(ifp,
9206cbd3e99SYaroslav Tykhiy 				    "unable to prepend VLAN header\n");
92126f9b263SRuslan Ermilov 				ifp->if_oerrors++;
9222cc2df49SGarrett Wollman 				continue;
9234af90a4dSMatthew N. Dodd 			}
924f731f104SBill Paul 		}
9252cc2df49SGarrett Wollman 
9262cc2df49SGarrett Wollman 		/*
9272cc2df49SGarrett Wollman 		 * Send it, precisely as ether_output() would have.
9282cc2df49SGarrett Wollman 		 * We are already running at splimp.
9292cc2df49SGarrett Wollman 		 */
930aea78d20SKip Macy 		error = (p->if_transmit)(p, m);
931affc907dSMax Laier 		if (!error)
932f731f104SBill Paul 			ifp->if_opackets++;
933df5e1987SJonathan Lemon 		else
934df5e1987SJonathan Lemon 			ifp->if_oerrors++;
9352cc2df49SGarrett Wollman 	}
936f731f104SBill Paul }
937f731f104SBill Paul 
938a3814acfSSam Leffler static void
939a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
940f731f104SBill Paul {
94175ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
942f731f104SBill Paul 	struct ifvlan *ifv;
94375ee267cSGleb Smirnoff 	uint16_t tag;
94475ee267cSGleb Smirnoff 
94575ee267cSGleb Smirnoff 	KASSERT(trunk != NULL, ("%s: no trunk", __func__));
946a3814acfSSam Leffler 
947f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
948a3814acfSSam Leffler 		/*
94914e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
950a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
951a3814acfSSam Leffler 		 */
95278ba57b9SAndre Oppermann 		tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
9536ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
954a3814acfSSam Leffler 	} else {
95575ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
95675ee267cSGleb Smirnoff 
95714e98256SYaroslav Tykhiy 		/*
95814e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
95914e98256SYaroslav Tykhiy 		 */
960a3814acfSSam Leffler 		switch (ifp->if_type) {
961a3814acfSSam Leffler 		case IFT_ETHER:
962a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
963a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
964a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
965a3814acfSSam Leffler 				return;
966a3814acfSSam Leffler 			}
967a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
968fb88a3e0SBill Paul 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
969db8b5973SYaroslav Tykhiy 
970db8b5973SYaroslav Tykhiy 			/*
9712dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
9722dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
9732dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
9742dc879b3SYaroslav Tykhiy 			 * type field is already in place.
975db8b5973SYaroslav Tykhiy 			 */
9762dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
9772dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
9782dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
979a3814acfSSam Leffler 			break;
9802dc879b3SYaroslav Tykhiy 
981a3814acfSSam Leffler 		default:
982db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
98360c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
98460c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
985a3814acfSSam Leffler #endif
98660c60618SYaroslav Tykhiy 			m_freem(m);
98760c60618SYaroslav Tykhiy 			ifp->if_noproto++;
98860c60618SYaroslav Tykhiy 			return;
989a3814acfSSam Leffler 		}
9907a46ec8fSBrooks Davis 	}
9917a46ec8fSBrooks Davis 
99215ed2fa1SYaroslav Tykhiy 	TRUNK_RLOCK(trunk);
99375ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
99475ee267cSGleb Smirnoff 	ifv = trunk->vlans[tag];
99575ee267cSGleb Smirnoff #else
99675ee267cSGleb Smirnoff 	ifv = vlan_gethash(trunk, tag);
99715ed2fa1SYaroslav Tykhiy #endif
9982dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
99975ee267cSGleb Smirnoff 		TRUNK_RUNLOCK(trunk);
100075ee267cSGleb Smirnoff 		m_freem(m);
100175ee267cSGleb Smirnoff 		ifp->if_noproto++;
100275ee267cSGleb Smirnoff 		return;
100375ee267cSGleb Smirnoff 	}
100475ee267cSGleb Smirnoff 	TRUNK_RUNLOCK(trunk);
1005f731f104SBill Paul 
1006fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1007fc74a9f9SBrooks Davis 	ifv->ifv_ifp->if_ipackets++;
10082cc2df49SGarrett Wollman 
1009a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1010fc74a9f9SBrooks Davis 	(*ifp->if_input)(ifv->ifv_ifp, m);
10112cc2df49SGarrett Wollman }
10122cc2df49SGarrett Wollman 
10132cc2df49SGarrett Wollman static int
101475ee267cSGleb Smirnoff vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
10152cc2df49SGarrett Wollman {
101675ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
10171cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
101875ee267cSGleb Smirnoff 	int error = 0;
10192cc2df49SGarrett Wollman 
102075ee267cSGleb Smirnoff 	/* VID numbers 0x0 and 0xFFF are reserved */
102175ee267cSGleb Smirnoff 	if (tag == 0 || tag == 0xFFF)
102275ee267cSGleb Smirnoff 		return (EINVAL);
10231cf236fbSYaroslav Tykhiy 	if (p->if_type != IFT_ETHER)
102415a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
102564a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
102664a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
102775ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
102815a66c21SBruce M Simpson 		return (EBUSY);
10292cc2df49SGarrett Wollman 
103075ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
103175ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
103275ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
103375ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
103475ee267cSGleb Smirnoff 		vlan_inithash(trunk);
103575ee267cSGleb Smirnoff #endif
103605a2398fSGleb Smirnoff 		VLAN_LOCK();
103705a2398fSGleb Smirnoff 		if (p->if_vlantrunk != NULL) {
103805a2398fSGleb Smirnoff 			/* A race that that is very unlikely to be hit. */
103905a2398fSGleb Smirnoff #ifndef VLAN_ARRAY
104005a2398fSGleb Smirnoff 			vlan_freehash(trunk);
104105a2398fSGleb Smirnoff #endif
104205a2398fSGleb Smirnoff 			free(trunk, M_VLAN);
104305a2398fSGleb Smirnoff 			goto exists;
104405a2398fSGleb Smirnoff 		}
104575ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
104675ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
104775ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
104875ee267cSGleb Smirnoff 		trunk->parent = p;
104975ee267cSGleb Smirnoff 	} else {
105075ee267cSGleb Smirnoff 		VLAN_LOCK();
105175ee267cSGleb Smirnoff exists:
105275ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
105375ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
105475ee267cSGleb Smirnoff 	}
105575ee267cSGleb Smirnoff 
105615ed2fa1SYaroslav Tykhiy 	ifv->ifv_tag = tag;	/* must set this before vlan_inshash() */
105775ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
105815ed2fa1SYaroslav Tykhiy 	if (trunk->vlans[tag] != NULL) {
105975ee267cSGleb Smirnoff 		error = EEXIST;
106015ed2fa1SYaroslav Tykhiy 		goto done;
106115ed2fa1SYaroslav Tykhiy 	}
106215ed2fa1SYaroslav Tykhiy 	trunk->vlans[tag] = ifv;
106315ed2fa1SYaroslav Tykhiy 	trunk->refcnt++;
106475ee267cSGleb Smirnoff #else
106575ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
106675ee267cSGleb Smirnoff 	if (error)
106775ee267cSGleb Smirnoff 		goto done;
106815ed2fa1SYaroslav Tykhiy #endif
106973f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1070a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1071a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
10721cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1073a3814acfSSam Leffler 
1074a3814acfSSam Leffler 	/*
1075a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1076a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1077656acce4SYaroslav Tykhiy 	 * use it.
1078a3814acfSSam Leffler 	 */
1079656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1080656acce4SYaroslav Tykhiy 		/*
1081656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1082656acce4SYaroslav Tykhiy 		 * handle extended frames.
1083656acce4SYaroslav Tykhiy 		 */
1084a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1085656acce4SYaroslav Tykhiy 	} else {
1086a3814acfSSam Leffler 		/*
1087a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1088a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1089a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1090a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1091a3814acfSSam Leffler 		 * which might still be useful.
1092a3814acfSSam Leffler 		 */
1093a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1094a3814acfSSam Leffler 	}
1095a3814acfSSam Leffler 
109675ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
10971cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
10981cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
109975ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
11002cc2df49SGarrett Wollman 	/*
110124993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
110224993214SYaroslav Tykhiy 	 * Other flags are none of our business.
11032cc2df49SGarrett Wollman 	 */
110464a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
11051cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
11061cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
11071cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
11081cf236fbSYaroslav Tykhiy 
11091cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
11102cc2df49SGarrett Wollman 
111175ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1112a3814acfSSam Leffler 
1113a3814acfSSam Leffler 	/*
11142cc2df49SGarrett Wollman 	 * Set up our ``Ethernet address'' to reflect the underlying
11152cc2df49SGarrett Wollman 	 * physical interface's.
11162cc2df49SGarrett Wollman 	 */
1117d09ed26fSRuslan Ermilov 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
11181b2a4f7aSBill Fenner 
11191b2a4f7aSBill Fenner 	/*
11201b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
11211b2a4f7aSBill Fenner 	 * joined on the vlan device.
11221b2a4f7aSBill Fenner 	 */
11231cf236fbSYaroslav Tykhiy 	(void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
11242ada9747SYaroslav Tykhiy 
11252ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
11262ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
112775ee267cSGleb Smirnoff done:
112875ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
1129c725524cSJack F Vogel 	if (error == 0)
1130c725524cSJack F Vogel 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
113175ee267cSGleb Smirnoff 	VLAN_UNLOCK();
113275ee267cSGleb Smirnoff 
113375ee267cSGleb Smirnoff 	return (error);
11342cc2df49SGarrett Wollman }
11352cc2df49SGarrett Wollman 
11366f359e28SJohn Baldwin static void
1137f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1138f731f104SBill Paul {
11395cb8c31aSYaroslav Tykhiy 
11405cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
11416f359e28SJohn Baldwin 	vlan_unconfig_locked(ifp);
11425cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
11435cb8c31aSYaroslav Tykhiy }
11445cb8c31aSYaroslav Tykhiy 
11456f359e28SJohn Baldwin static void
11465cb8c31aSYaroslav Tykhiy vlan_unconfig_locked(struct ifnet *ifp)
11475cb8c31aSYaroslav Tykhiy {
114875ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1149f731f104SBill Paul 	struct vlan_mc_entry *mc;
1150f731f104SBill Paul 	struct ifvlan *ifv;
1151c725524cSJack F Vogel 	struct ifnet  *parent;
1152f731f104SBill Paul 
11535cb8c31aSYaroslav Tykhiy 	VLAN_LOCK_ASSERT();
11544faedfe8SSam Leffler 
1155f731f104SBill Paul 	ifv = ifp->if_softc;
115675ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
115722893351SJack F Vogel 	parent = NULL;
1158f731f104SBill Paul 
115922893351SJack F Vogel 	if (trunk != NULL) {
11601b2a4f7aSBill Fenner 		struct sockaddr_dl sdl;
116175ee267cSGleb Smirnoff 
116275ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
116322893351SJack F Vogel 		parent = trunk->parent;
11641b2a4f7aSBill Fenner 
1165f731f104SBill Paul 		/*
1166f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1167f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
11681b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1169f731f104SBill Paul 		 */
117015a66c21SBruce M Simpson 		bzero((char *)&sdl, sizeof(sdl));
117115a66c21SBruce M Simpson 		sdl.sdl_len = sizeof(sdl);
1172f731f104SBill Paul 		sdl.sdl_family = AF_LINK;
117322893351SJack F Vogel 		sdl.sdl_index = parent->if_index;
11741b2a4f7aSBill Fenner 		sdl.sdl_type = IFT_ETHER;
11751b2a4f7aSBill Fenner 		sdl.sdl_alen = ETHER_ADDR_LEN;
11761b2a4f7aSBill Fenner 
1177c0cb022bSYaroslav Tykhiy 		while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
117815a66c21SBruce M Simpson 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
117915a66c21SBruce M Simpson 			    ETHER_ADDR_LEN);
11806f359e28SJohn Baldwin 
11816f359e28SJohn Baldwin 			/*
11826f359e28SJohn Baldwin 			 * This may fail if the parent interface is
11836f359e28SJohn Baldwin 			 * being detached.  Regardless, we should do a
11846f359e28SJohn Baldwin 			 * best effort to free this interface as much
11856f359e28SJohn Baldwin 			 * as possible as all callers expect vlan
11866f359e28SJohn Baldwin 			 * destruction to succeed.
11876f359e28SJohn Baldwin 			 */
11886f359e28SJohn Baldwin 			(void)if_delmulti(parent, (struct sockaddr *)&sdl);
1189f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
11909d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
1191f731f104SBill Paul 		}
1192a3814acfSSam Leffler 
11931cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
119415ed2fa1SYaroslav Tykhiy #ifdef VLAN_ARRAY
119515ed2fa1SYaroslav Tykhiy 		trunk->vlans[ifv->ifv_tag] = NULL;
119615ed2fa1SYaroslav Tykhiy 		trunk->refcnt--;
119715ed2fa1SYaroslav Tykhiy #else
119875ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
119975ee267cSGleb Smirnoff #endif
120075ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
120175ee267cSGleb Smirnoff 
120275ee267cSGleb Smirnoff 		/*
120375ee267cSGleb Smirnoff 		 * Check if we were the last.
120475ee267cSGleb Smirnoff 		 */
120575ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
120615ed2fa1SYaroslav Tykhiy 			trunk->parent->if_vlantrunk = NULL;
120775ee267cSGleb Smirnoff 			/*
120875ee267cSGleb Smirnoff 			 * XXXGL: If some ithread has already entered
120975ee267cSGleb Smirnoff 			 * vlan_input() and is now blocked on the trunk
121075ee267cSGleb Smirnoff 			 * lock, then it should preempt us right after
121175ee267cSGleb Smirnoff 			 * unlock and finish its work. Then we will acquire
121275ee267cSGleb Smirnoff 			 * lock again in trunk_destroy().
121375ee267cSGleb Smirnoff 			 */
121475ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
121575ee267cSGleb Smirnoff 			trunk_destroy(trunk);
121675ee267cSGleb Smirnoff 		} else
121775ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
12181b2a4f7aSBill Fenner 	}
1219f731f104SBill Paul 
1220f731f104SBill Paul 	/* Disconnect from parent. */
12211cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
12221cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
12235cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
12245cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
12255cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1226f731f104SBill Paul 
122722893351SJack F Vogel 	/*
122822893351SJack F Vogel 	 * Only dispatch an event if vlan was
122922893351SJack F Vogel 	 * attached, otherwise there is nothing
123022893351SJack F Vogel 	 * to cleanup anyway.
123122893351SJack F Vogel 	 */
123222893351SJack F Vogel 	if (parent != NULL)
1233c725524cSJack F Vogel 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
1234f731f104SBill Paul }
1235f731f104SBill Paul 
12361cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1237f731f104SBill Paul static int
12381cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
12391cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1240a3814acfSSam Leffler {
12411cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
12421cf236fbSYaroslav Tykhiy 	int error;
1243a3814acfSSam Leffler 
12441cf236fbSYaroslav Tykhiy 	/* XXX VLAN_LOCK_ASSERT(); */
1245a3814acfSSam Leffler 
12461cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
12471cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
12481cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
12491cf236fbSYaroslav Tykhiy 
12501cf236fbSYaroslav Tykhiy 	/*
12511cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
12521cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
12531cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
12541cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
12551cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
12561cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
12571cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
12581cf236fbSYaroslav Tykhiy 	 */
12591cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
126075ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
12611cf236fbSYaroslav Tykhiy 		if (error)
1262a3814acfSSam Leffler 			return (error);
12631cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
12641cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
12651cf236fbSYaroslav Tykhiy 	}
12661cf236fbSYaroslav Tykhiy 	return (0);
12671cf236fbSYaroslav Tykhiy }
12681cf236fbSYaroslav Tykhiy 
12691cf236fbSYaroslav Tykhiy /*
12701cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
12711cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
12721cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
12731cf236fbSYaroslav Tykhiy  */
12741cf236fbSYaroslav Tykhiy static int
12751cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
12761cf236fbSYaroslav Tykhiy {
12771cf236fbSYaroslav Tykhiy 	int error, i;
12781cf236fbSYaroslav Tykhiy 
12791cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
12801cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
12811cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
12821cf236fbSYaroslav Tykhiy 		if (error)
12831cf236fbSYaroslav Tykhiy 			return (error);
12841cf236fbSYaroslav Tykhiy 	}
12851cf236fbSYaroslav Tykhiy 	return (0);
1286a3814acfSSam Leffler }
1287a3814acfSSam Leffler 
1288127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1289127d7b2dSAndre Oppermann static void
1290a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1291127d7b2dSAndre Oppermann {
129275ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1293127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
129475ee267cSGleb Smirnoff 	int i;
1295127d7b2dSAndre Oppermann 
129675ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
129775ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
129815ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
129975ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
130075ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
130175ee267cSGleb Smirnoff #else
1302aad0be7aSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
1303aad0be7aSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
130475ee267cSGleb Smirnoff #endif
1305aad0be7aSGleb Smirnoff 			ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1306fc74a9f9SBrooks Davis 			if_link_state_change(ifv->ifv_ifp,
130775ee267cSGleb Smirnoff 			    trunk->parent->if_link_state);
1308127d7b2dSAndre Oppermann 		}
130975ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
131075ee267cSGleb Smirnoff }
131175ee267cSGleb Smirnoff 
131275ee267cSGleb Smirnoff static void
131375ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
131475ee267cSGleb Smirnoff {
131575ee267cSGleb Smirnoff 	struct ifnet *p = PARENT(ifv);
131675ee267cSGleb Smirnoff 	struct ifnet *ifp = ifv->ifv_ifp;
131775ee267cSGleb Smirnoff 
131875ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(TRUNK(ifv));
131975ee267cSGleb Smirnoff 
132075ee267cSGleb Smirnoff 	/*
132175ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
132275ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
132375ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
132475ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
132575ee267cSGleb Smirnoff 	 */
132675ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
132775ee267cSGleb Smirnoff 		ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
132875ee267cSGleb Smirnoff 
132975ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
133075ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
133175ee267cSGleb Smirnoff 		ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
13329b76d9cbSPyun YongHyeon 		ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
13339b76d9cbSPyun YongHyeon 		    CSUM_UDP | CSUM_SCTP | CSUM_IP_FRAGS | CSUM_FRAGMENT);
133475ee267cSGleb Smirnoff 	} else {
133575ee267cSGleb Smirnoff 		ifp->if_capenable = 0;
133675ee267cSGleb Smirnoff 		ifp->if_hwassist = 0;
133775ee267cSGleb Smirnoff 	}
13389b76d9cbSPyun YongHyeon 	/*
13399b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
13409b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
13419b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
13429b76d9cbSPyun YongHyeon 	 */
13439b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
13449b76d9cbSPyun YongHyeon 		ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
13459b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
13469b76d9cbSPyun YongHyeon 		ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
13479b76d9cbSPyun YongHyeon 		ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
13489b76d9cbSPyun YongHyeon 	} else {
13499b76d9cbSPyun YongHyeon 		ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
13509b76d9cbSPyun YongHyeon 		ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
13519b76d9cbSPyun YongHyeon 	}
135275ee267cSGleb Smirnoff }
135375ee267cSGleb Smirnoff 
135475ee267cSGleb Smirnoff static void
135575ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
135675ee267cSGleb Smirnoff {
135775ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
135875ee267cSGleb Smirnoff 	struct ifvlan *ifv;
135975ee267cSGleb Smirnoff 	int i;
136075ee267cSGleb Smirnoff 
136175ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
136275ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
136315ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
136475ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
136575ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
136675ee267cSGleb Smirnoff #else
136775ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
136875ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
136975ee267cSGleb Smirnoff #endif
137075ee267cSGleb Smirnoff 			vlan_capabilities(ifv);
137175ee267cSGleb Smirnoff 	}
137275ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
1373127d7b2dSAndre Oppermann }
1374127d7b2dSAndre Oppermann 
1375a3814acfSSam Leffler static int
1376cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
13772cc2df49SGarrett Wollman {
13782cc2df49SGarrett Wollman 	struct ifnet *p;
13792cc2df49SGarrett Wollman 	struct ifreq *ifr;
13802cc2df49SGarrett Wollman 	struct ifvlan *ifv;
13812cc2df49SGarrett Wollman 	struct vlanreq vlr;
13822cc2df49SGarrett Wollman 	int error = 0;
13832cc2df49SGarrett Wollman 
13842cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
13852cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
13862cc2df49SGarrett Wollman 
13872cc2df49SGarrett Wollman 	switch (cmd) {
1388b3cca108SBill Fenner 	case SIOCGIFMEDIA:
13894faedfe8SSam Leffler 		VLAN_LOCK();
139075ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1391d8564efdSEd Maste 			p = PARENT(ifv);
13924faedfe8SSam Leffler 			VLAN_UNLOCK();
1393d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1394b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1395b3cca108SBill Fenner 			if (error == 0) {
1396b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1397b3cca108SBill Fenner 
1398b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1399b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1400b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1401b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1402b3cca108SBill Fenner 						ifmr->ifm_ulist,
1403b3cca108SBill Fenner 						sizeof(int));
1404b3cca108SBill Fenner 				}
1405b3cca108SBill Fenner 			}
14064faedfe8SSam Leffler 		} else {
14074faedfe8SSam Leffler 			VLAN_UNLOCK();
1408b3cca108SBill Fenner 			error = EINVAL;
14094faedfe8SSam Leffler 		}
1410b3cca108SBill Fenner 		break;
1411b3cca108SBill Fenner 
1412b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1413b3cca108SBill Fenner 		error = EINVAL;
1414b3cca108SBill Fenner 		break;
1415b3cca108SBill Fenner 
14162cc2df49SGarrett Wollman 	case SIOCSIFMTU:
14172cc2df49SGarrett Wollman 		/*
14182cc2df49SGarrett Wollman 		 * Set the interface MTU.
14192cc2df49SGarrett Wollman 		 */
14204faedfe8SSam Leffler 		VLAN_LOCK();
142175ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1422a3814acfSSam Leffler 			if (ifr->ifr_mtu >
142375ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1424a3814acfSSam Leffler 			    ifr->ifr_mtu <
1425a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
14262cc2df49SGarrett Wollman 				error = EINVAL;
1427a3814acfSSam Leffler 			else
14282cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1429a3814acfSSam Leffler 		} else
1430a3814acfSSam Leffler 			error = EINVAL;
14314faedfe8SSam Leffler 		VLAN_UNLOCK();
14322cc2df49SGarrett Wollman 		break;
14332cc2df49SGarrett Wollman 
14342cc2df49SGarrett Wollman 	case SIOCSETVLAN:
143515a66c21SBruce M Simpson 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
14362cc2df49SGarrett Wollman 		if (error)
14372cc2df49SGarrett Wollman 			break;
14382cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1439f731f104SBill Paul 			vlan_unconfig(ifp);
14402cc2df49SGarrett Wollman 			break;
14412cc2df49SGarrett Wollman 		}
14422cc2df49SGarrett Wollman 		p = ifunit(vlr.vlr_parent);
14431bdc73d3SEd Maste 		if (p == NULL) {
14442cc2df49SGarrett Wollman 			error = ENOENT;
14452cc2df49SGarrett Wollman 			break;
14462cc2df49SGarrett Wollman 		}
1447fb88a3e0SBill Paul 		/*
1448fb88a3e0SBill Paul 		 * Don't let the caller set up a VLAN tag with
1449fb88a3e0SBill Paul 		 * anything except VLID bits.
1450fb88a3e0SBill Paul 		 */
1451fb88a3e0SBill Paul 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1452fb88a3e0SBill Paul 			error = EINVAL;
1453fb88a3e0SBill Paul 			break;
1454fb88a3e0SBill Paul 		}
145575ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
145675ee267cSGleb Smirnoff 		if (error)
14572cc2df49SGarrett Wollman 			break;
1458a3814acfSSam Leffler 
14591cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
14601cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
14612cc2df49SGarrett Wollman 		break;
14622cc2df49SGarrett Wollman 
14632cc2df49SGarrett Wollman 	case SIOCGETVLAN:
146415a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
14654faedfe8SSam Leffler 		VLAN_LOCK();
146675ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
146775ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
14689bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
14692cc2df49SGarrett Wollman 			vlr.vlr_tag = ifv->ifv_tag;
14702cc2df49SGarrett Wollman 		}
14714faedfe8SSam Leffler 		VLAN_UNLOCK();
147215a66c21SBruce M Simpson 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
14732cc2df49SGarrett Wollman 		break;
14742cc2df49SGarrett Wollman 
14752cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
14762cc2df49SGarrett Wollman 		/*
14771cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
14781cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
14792cc2df49SGarrett Wollman 		 */
148075ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
14811cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
14822cc2df49SGarrett Wollman 		break;
1483a3814acfSSam Leffler 
1484f731f104SBill Paul 	case SIOCADDMULTI:
1485f731f104SBill Paul 	case SIOCDELMULTI:
148675ee267cSGleb Smirnoff 		/*
148775ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
148875ee267cSGleb Smirnoff 		 * when we do.
148975ee267cSGleb Smirnoff 		 */
149075ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
1491f731f104SBill Paul 			error = vlan_setmulti(ifp);
1492f731f104SBill Paul 		break;
149375ee267cSGleb Smirnoff 
14942cc2df49SGarrett Wollman 	default:
14959c6dee24SYaroslav Tykhiy 		error = ether_ioctl(ifp, cmd, data);
14962cc2df49SGarrett Wollman 	}
149715a66c21SBruce M Simpson 
149815a66c21SBruce M Simpson 	return (error);
14992cc2df49SGarrett Wollman }
1500