xref: /freebsd/sys/net/if_vlan.c (revision aad0be7a3b47f163041ef843f43b185c725af522)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32cc2df49SGarrett Wollman  *
42cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
52cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
62cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
72cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
82cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
92cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
102cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
112cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
122cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
132cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
142cc2df49SGarrett Wollman  * warranty.
152cc2df49SGarrett Wollman  *
162cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
172cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
182cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
192cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
202cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
232cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
242cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
252cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
262cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272cc2df49SGarrett Wollman  * SUCH DAMAGE.
282cc2df49SGarrett Wollman  *
29c3aac50fSPeter Wemm  * $FreeBSD$
302cc2df49SGarrett Wollman  */
312cc2df49SGarrett Wollman 
322cc2df49SGarrett Wollman /*
332cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
342cc2df49SGarrett Wollman  * Might be extended some day to also handle IEEE 802.1p priority
352cc2df49SGarrett Wollman  * tagging.  This is sort of sneaky in the implementation, since
362cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
372cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
382cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
3946a32e61SPhilippe Charnier  * ether_output() left on our output queue when it calls
402cc2df49SGarrett Wollman  * if_start(), rewrite them for use by the real outgoing interface,
412cc2df49SGarrett Wollman  * and ask it to send them.
422cc2df49SGarrett Wollman  */
432cc2df49SGarrett Wollman 
442cc2df49SGarrett Wollman #include "opt_inet.h"
4575ee267cSGleb Smirnoff #include "opt_vlan.h"
462cc2df49SGarrett Wollman 
472cc2df49SGarrett Wollman #include <sys/param.h>
482cc2df49SGarrett Wollman #include <sys/kernel.h>
4975ee267cSGleb Smirnoff #include <sys/lock.h>
50f731f104SBill Paul #include <sys/malloc.h>
512cc2df49SGarrett Wollman #include <sys/mbuf.h>
522b120974SPeter Wemm #include <sys/module.h>
5375ee267cSGleb Smirnoff #include <sys/rwlock.h>
54f731f104SBill Paul #include <sys/queue.h>
552cc2df49SGarrett Wollman #include <sys/socket.h>
562cc2df49SGarrett Wollman #include <sys/sockio.h>
572cc2df49SGarrett Wollman #include <sys/sysctl.h>
582cc2df49SGarrett Wollman #include <sys/systm.h>
592cc2df49SGarrett Wollman 
602cc2df49SGarrett Wollman #include <net/bpf.h>
612cc2df49SGarrett Wollman #include <net/ethernet.h>
622cc2df49SGarrett Wollman #include <net/if.h>
63f889d2efSBrooks Davis #include <net/if_clone.h>
642cc2df49SGarrett Wollman #include <net/if_arp.h>
652cc2df49SGarrett Wollman #include <net/if_dl.h>
662cc2df49SGarrett Wollman #include <net/if_types.h>
672cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
682cc2df49SGarrett Wollman 
692cc2df49SGarrett Wollman #ifdef INET
702cc2df49SGarrett Wollman #include <netinet/in.h>
712cc2df49SGarrett Wollman #include <netinet/if_ether.h>
722cc2df49SGarrett Wollman #endif
732cc2df49SGarrett Wollman 
749d4fe4b2SBrooks Davis #define VLANNAME	"vlan"
7575ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
7664a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
7775ee267cSGleb Smirnoff 
7875ee267cSGleb Smirnoff LIST_HEAD(ifvlanhead, ifvlan);
7975ee267cSGleb Smirnoff 
8075ee267cSGleb Smirnoff struct ifvlantrunk {
8175ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
8275ee267cSGleb Smirnoff 	struct	rwlock	rw;
8375ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
845cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
855cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
8675ee267cSGleb Smirnoff #else
8775ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
8875ee267cSGleb Smirnoff 	uint16_t	hmask;
8975ee267cSGleb Smirnoff 	uint16_t	hwidth;
9075ee267cSGleb Smirnoff #endif
9175ee267cSGleb Smirnoff 	int		refcnt;
9275ee267cSGleb Smirnoff 	LIST_ENTRY(ifvlantrunk) trunk_entry;
9375ee267cSGleb Smirnoff };
9475ee267cSGleb Smirnoff static LIST_HEAD(, ifvlantrunk) trunk_list;
959d4fe4b2SBrooks Davis 
96a3814acfSSam Leffler struct vlan_mc_entry {
97a3814acfSSam Leffler 	struct ether_addr		mc_addr;
98a3814acfSSam Leffler 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
99a3814acfSSam Leffler };
100a3814acfSSam Leffler 
101a3814acfSSam Leffler struct	ifvlan {
10275ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
103fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
10475ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
10575ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
1061cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
107a3814acfSSam Leffler 	struct	ifv_linkmib {
108a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
109a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
110a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
11173f2233dSYaroslav Tykhiy 		uint16_t ifvm_proto;	/* encapsulation ethertype */
11275ee267cSGleb Smirnoff 		uint16_t ifvm_tag;	/* tag to apply on packets leaving if */
113a3814acfSSam Leffler 	}	ifv_mib;
114114c608cSYaroslav Tykhiy 	SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
115a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
116a3814acfSSam Leffler };
11773f2233dSYaroslav Tykhiy #define	ifv_proto	ifv_mib.ifvm_proto
118a3814acfSSam Leffler #define	ifv_tag		ifv_mib.ifvm_tag
119a3814acfSSam Leffler #define	ifv_encaplen	ifv_mib.ifvm_encaplen
120a3814acfSSam Leffler #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
121a3814acfSSam Leffler #define	ifv_mintu	ifv_mib.ifvm_mintu
122a3814acfSSam Leffler 
12375ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
1241cf236fbSYaroslav Tykhiy static struct {
1251cf236fbSYaroslav Tykhiy 	int flag;
1261cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
1271cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
1281cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
1291cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
1301cf236fbSYaroslav Tykhiy 	{0, NULL}
1311cf236fbSYaroslav Tykhiy };
132a3814acfSSam Leffler 
1334a408dcbSBill Paul SYSCTL_DECL(_net_link);
134d2a75853SBill Fenner SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
1352cc2df49SGarrett Wollman SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
1362cc2df49SGarrett Wollman 
137f6e5e0adSYaroslav Tykhiy static int soft_pad = 0;
138f6e5e0adSYaroslav Tykhiy SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
139f6e5e0adSYaroslav Tykhiy 	   "pad short frames before tagging");
140f6e5e0adSYaroslav Tykhiy 
1415e17543aSBrooks Davis static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
1422cc2df49SGarrett Wollman 
1435cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
1445cb8c31aSYaroslav Tykhiy 
1454faedfe8SSam Leffler /*
14675ee267cSGleb Smirnoff  * We have a global mutex, that is used to serialize configuration
14775ee267cSGleb Smirnoff  * changes and isn't used in normal packet delivery.
14875ee267cSGleb Smirnoff  *
14975ee267cSGleb Smirnoff  * We also have a per-trunk rwlock, that is locked shared on packet
15075ee267cSGleb Smirnoff  * processing and exclusive when configuration is changed.
15175ee267cSGleb Smirnoff  *
15275ee267cSGleb Smirnoff  * The VLAN_ARRAY substitutes the dynamic hash with a static array
153ad387028SAndrew Thompson  * with 4096 entries. In theory this can give a boost in processing,
15475ee267cSGleb Smirnoff  * however on practice it does not. Probably this is because array
15575ee267cSGleb Smirnoff  * is too big to fit into CPU cache.
1564faedfe8SSam Leffler  */
1574faedfe8SSam Leffler static struct mtx ifv_mtx;
15875ee267cSGleb Smirnoff #define	VLAN_LOCK_INIT()	mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
1594faedfe8SSam Leffler #define	VLAN_LOCK_DESTROY()	mtx_destroy(&ifv_mtx)
1604faedfe8SSam Leffler #define	VLAN_LOCK_ASSERT()	mtx_assert(&ifv_mtx, MA_OWNED)
1614faedfe8SSam Leffler #define	VLAN_LOCK()		mtx_lock(&ifv_mtx)
1624faedfe8SSam Leffler #define	VLAN_UNLOCK()		mtx_unlock(&ifv_mtx)
16375ee267cSGleb Smirnoff #define	TRUNK_LOCK_INIT(trunk)	rw_init(&(trunk)->rw, VLANNAME)
16475ee267cSGleb Smirnoff #define	TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
16575ee267cSGleb Smirnoff #define	TRUNK_LOCK(trunk)	rw_wlock(&(trunk)->rw)
16675ee267cSGleb Smirnoff #define	TRUNK_UNLOCK(trunk)	rw_wunlock(&(trunk)->rw)
16775ee267cSGleb Smirnoff #define	TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
16875ee267cSGleb Smirnoff #define	TRUNK_RLOCK(trunk)	rw_rlock(&(trunk)->rw)
16975ee267cSGleb Smirnoff #define	TRUNK_RUNLOCK(trunk)	rw_runlock(&(trunk)->rw)
17075ee267cSGleb Smirnoff #define	TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
17175ee267cSGleb Smirnoff 
17275ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
17375ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
17475ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
17575ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
17675ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
17775ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
17875ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
17975ee267cSGleb Smirnoff 	uint16_t tag);
18075ee267cSGleb Smirnoff #endif
18175ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
1824faedfe8SSam Leffler 
1832cc2df49SGarrett Wollman static	void vlan_start(struct ifnet *ifp);
184114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
185a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
186cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
1871cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
1881cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
1891cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
190f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
191f731f104SBill Paul static	int vlan_unconfig(struct ifnet *ifp);
1925cb8c31aSYaroslav Tykhiy static	int vlan_unconfig_locked(struct ifnet *ifp);
19375ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
194127d7b2dSAndre Oppermann static	void vlan_link_state(struct ifnet *ifp, int link);
19575ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
19675ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
197f731f104SBill Paul 
198f889d2efSBrooks Davis static	struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
199f889d2efSBrooks Davis     const char *, int *);
200f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
2016b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
202f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
203f889d2efSBrooks Davis 
2045cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(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 
31175ee267cSGleb Smirnoff 	struct ifvlan *ifv;
31275ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
31375ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
31475ee267cSGleb Smirnoff 
31575ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
31675ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
31775ee267cSGleb Smirnoff 
31875ee267cSGleb Smirnoff 	if (howmuch == 0) {
31975ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
32075ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
32175ee267cSGleb Smirnoff 		return;
32275ee267cSGleb Smirnoff 	}
32375ee267cSGleb Smirnoff 
32475ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
32575ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
32675ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
32775ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
32875ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
32975ee267cSGleb Smirnoff 		return;
33075ee267cSGleb Smirnoff 
33175ee267cSGleb Smirnoff 	/* M_NOWAIT because we're called with trunk mutex held */
33275ee267cSGleb Smirnoff 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
33375ee267cSGleb Smirnoff 	if (hash2 == NULL) {
33475ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
33575ee267cSGleb Smirnoff 		    __func__);
33675ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
33775ee267cSGleb Smirnoff 	}
33875ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
33975ee267cSGleb Smirnoff 		LIST_INIT(&hash2[j]);
34075ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
34175ee267cSGleb Smirnoff 		while (!LIST_EMPTY(&trunk->hash[i])) {
34275ee267cSGleb Smirnoff 			ifv = LIST_FIRST(&trunk->hash[i]);
34375ee267cSGleb Smirnoff 			LIST_REMOVE(ifv, ifv_list);
34475ee267cSGleb Smirnoff 			j = HASH(ifv->ifv_tag, n2 - 1);
34575ee267cSGleb Smirnoff 			LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
34675ee267cSGleb Smirnoff 		}
34775ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
34875ee267cSGleb Smirnoff 	trunk->hash = hash2;
34975ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
35075ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
35175ee267cSGleb Smirnoff }
35275ee267cSGleb Smirnoff 
35375ee267cSGleb Smirnoff static __inline struct ifvlan *
35475ee267cSGleb Smirnoff vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
35575ee267cSGleb Smirnoff {
35675ee267cSGleb Smirnoff 	struct ifvlan *ifv;
35775ee267cSGleb Smirnoff 
35875ee267cSGleb Smirnoff 	TRUNK_LOCK_RASSERT(trunk);
35975ee267cSGleb Smirnoff 
36075ee267cSGleb Smirnoff 	LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
36175ee267cSGleb Smirnoff 		if (ifv->ifv_tag == tag)
36275ee267cSGleb Smirnoff 			return (ifv);
36375ee267cSGleb Smirnoff 	return (NULL);
36475ee267cSGleb Smirnoff }
36575ee267cSGleb Smirnoff 
36675ee267cSGleb Smirnoff #if 0
36775ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
36875ee267cSGleb Smirnoff static void
36975ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
37075ee267cSGleb Smirnoff {
37175ee267cSGleb Smirnoff 	int i;
37275ee267cSGleb Smirnoff 	struct ifvlan *ifv;
37375ee267cSGleb Smirnoff 
37475ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
37575ee267cSGleb Smirnoff 		printf("%d: ", i);
37675ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
37775ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
37875ee267cSGleb Smirnoff 		printf("\n");
37975ee267cSGleb Smirnoff 	}
38075ee267cSGleb Smirnoff }
38175ee267cSGleb Smirnoff #endif /* 0 */
38275ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
38375ee267cSGleb Smirnoff 
38475ee267cSGleb Smirnoff static void
38575ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
38675ee267cSGleb Smirnoff {
38775ee267cSGleb Smirnoff 	VLAN_LOCK_ASSERT();
38875ee267cSGleb Smirnoff 
38975ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
39075ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
39175ee267cSGleb Smirnoff 	vlan_freehash(trunk);
39275ee267cSGleb Smirnoff #endif
39375ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
39433499e2aSYaroslav Tykhiy 	LIST_REMOVE(trunk, trunk_entry);
39533499e2aSYaroslav Tykhiy 	TRUNK_UNLOCK(trunk);
39633499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
39775ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
39875ee267cSGleb Smirnoff }
39975ee267cSGleb Smirnoff 
400f731f104SBill Paul /*
401f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
402f731f104SBill Paul  * programming the multicast filter of the parent. This has the
403f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
404f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
405f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
406f731f104SBill Paul  * to avoid this: there really is only one physical interface.
40729c2dfbeSBruce M Simpson  *
40829c2dfbeSBruce M Simpson  * XXX: There is a possible race here if more than one thread is
40929c2dfbeSBruce M Simpson  *      modifying the multicast state of the vlan interface at the same time.
410f731f104SBill Paul  */
4112b120974SPeter Wemm static int
4122b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
413f731f104SBill Paul {
414f731f104SBill Paul 	struct ifnet		*ifp_p;
415f731f104SBill Paul 	struct ifmultiaddr	*ifma, *rifma = NULL;
416f731f104SBill Paul 	struct ifvlan		*sc;
417f731f104SBill Paul 	struct vlan_mc_entry	*mc = NULL;
418f731f104SBill Paul 	struct sockaddr_dl	sdl;
419f731f104SBill Paul 	int			error;
420f731f104SBill Paul 
42129c2dfbeSBruce M Simpson 	/*VLAN_LOCK_ASSERT();*/
42229c2dfbeSBruce M Simpson 
423f731f104SBill Paul 	/* Find the parent. */
424f731f104SBill Paul 	sc = ifp->if_softc;
42575ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
4261b2a4f7aSBill Fenner 
42715a66c21SBruce M Simpson 	bzero((char *)&sdl, sizeof(sdl));
42815a66c21SBruce M Simpson 	sdl.sdl_len = sizeof(sdl);
429f731f104SBill Paul 	sdl.sdl_family = AF_LINK;
43026e30963SBill Fenner 	sdl.sdl_index = ifp_p->if_index;
43126e30963SBill Fenner 	sdl.sdl_type = IFT_ETHER;
43224993214SYaroslav Tykhiy 	sdl.sdl_alen = ETHER_ADDR_LEN;
433f731f104SBill Paul 
434f731f104SBill Paul 	/* First, remove any existing filter entries. */
43522f29826SPoul-Henning Kamp 	while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
43622f29826SPoul-Henning Kamp 		mc = SLIST_FIRST(&sc->vlan_mc_listhead);
437f731f104SBill Paul 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
438f731f104SBill Paul 		error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
439f731f104SBill Paul 		if (error)
440f731f104SBill Paul 			return (error);
441f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
4429d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
443f731f104SBill Paul 	}
444f731f104SBill Paul 
445f731f104SBill Paul 	/* Now program new ones. */
4466817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
447f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
448f731f104SBill Paul 			continue;
44929c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
45029c2dfbeSBruce M Simpson 		if (mc == NULL)
45129c2dfbeSBruce M Simpson 			return (ENOMEM);
452f731f104SBill Paul 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
453f731f104SBill Paul 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
454f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
45526e30963SBill Fenner 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
45626e30963SBill Fenner 		    LLADDR(&sdl), ETHER_ADDR_LEN);
457f731f104SBill Paul 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
458f731f104SBill Paul 		if (error)
459f731f104SBill Paul 			return (error);
460f731f104SBill Paul 	}
461f731f104SBill Paul 
462f731f104SBill Paul 	return (0);
463f731f104SBill Paul }
4642cc2df49SGarrett Wollman 
465a3814acfSSam Leffler /*
4665cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
4675cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
4685cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
4695cb8c31aSYaroslav Tykhiy  * by ejecting its hot-plug card.
4705cb8c31aSYaroslav Tykhiy  */
4715cb8c31aSYaroslav Tykhiy static void
4725cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
4735cb8c31aSYaroslav Tykhiy {
4745cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
4755cb8c31aSYaroslav Tykhiy 	int i;
4765cb8c31aSYaroslav Tykhiy 
4775cb8c31aSYaroslav Tykhiy 	/*
4785cb8c31aSYaroslav Tykhiy 	 * Check if it's a trunk interface first of all
4795cb8c31aSYaroslav Tykhiy 	 * to avoid needless locking.
4805cb8c31aSYaroslav Tykhiy 	 */
4815cb8c31aSYaroslav Tykhiy 	if (ifp->if_vlantrunk == NULL)
4825cb8c31aSYaroslav Tykhiy 		return;
4835cb8c31aSYaroslav Tykhiy 
4845cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
4855cb8c31aSYaroslav Tykhiy 	/*
4865cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
4875cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
4885cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
4895cb8c31aSYaroslav Tykhiy 	 */
4905cb8c31aSYaroslav Tykhiy #ifdef VLAN_ARRAY
4915cb8c31aSYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
4925cb8c31aSYaroslav Tykhiy 		if ((ifv = ifp->if_vlantrunk->vlans[i])) {
4935cb8c31aSYaroslav Tykhiy 			vlan_unconfig_locked(ifv->ifv_ifp);
4945cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk == NULL)
4955cb8c31aSYaroslav Tykhiy 				break;
4965cb8c31aSYaroslav Tykhiy 		}
4975cb8c31aSYaroslav Tykhiy #else /* VLAN_ARRAY */
4985cb8c31aSYaroslav Tykhiy restart:
4995cb8c31aSYaroslav Tykhiy 	for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
5005cb8c31aSYaroslav Tykhiy 		if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
5015cb8c31aSYaroslav Tykhiy 			vlan_unconfig_locked(ifv->ifv_ifp);
5025cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk)
5035cb8c31aSYaroslav Tykhiy 				goto restart;	/* trunk->hwidth can change */
5045cb8c31aSYaroslav Tykhiy 			else
5055cb8c31aSYaroslav Tykhiy 				break;
5065cb8c31aSYaroslav Tykhiy 		}
5075cb8c31aSYaroslav Tykhiy #endif /* VLAN_ARRAY */
5085cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
5095cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
5105cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
5115cb8c31aSYaroslav Tykhiy }
5125cb8c31aSYaroslav Tykhiy 
5135cb8c31aSYaroslav Tykhiy /*
514a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
515a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
516a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
517a3814acfSSam Leffler  * set here.  Noone else in the system should be aware of this so
518a3814acfSSam Leffler  * we use an explicit reference here.
519a3814acfSSam Leffler  */
520a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
521a3814acfSSam Leffler 
522984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
523127d7b2dSAndre Oppermann extern	void (*vlan_link_state_p)(struct ifnet *, int);
524127d7b2dSAndre Oppermann 
5252b120974SPeter Wemm static int
5262b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
5272b120974SPeter Wemm {
5289d4fe4b2SBrooks Davis 
5292b120974SPeter Wemm 	switch (type) {
5302b120974SPeter Wemm 	case MOD_LOAD:
5315cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
5325cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
5335cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
5345cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
53575ee267cSGleb Smirnoff 		LIST_INIT(&trunk_list);
5364faedfe8SSam Leffler 		VLAN_LOCK_INIT();
5379d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
538127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
53975ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
5409d4fe4b2SBrooks Davis 		if_clone_attach(&vlan_cloner);
5412b120974SPeter Wemm 		break;
5422b120974SPeter Wemm 	case MOD_UNLOAD:
54375ee267cSGleb Smirnoff 	    {
54475ee267cSGleb Smirnoff 		struct ifvlantrunk *trunk, *trunk1;
54575ee267cSGleb Smirnoff 
5469d4fe4b2SBrooks Davis 		if_clone_detach(&vlan_cloner);
5475cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
5489d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
549127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
55075ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
55175ee267cSGleb Smirnoff 		VLAN_LOCK();
55275ee267cSGleb Smirnoff 		LIST_FOREACH_SAFE(trunk, &trunk_list, trunk_entry, trunk1)
55375ee267cSGleb Smirnoff 			trunk_destroy(trunk);
55475ee267cSGleb Smirnoff 		VLAN_UNLOCK();
5554faedfe8SSam Leffler 		VLAN_LOCK_DESTROY();
5569d4fe4b2SBrooks Davis 		break;
55775ee267cSGleb Smirnoff 	    }
5583e019deaSPoul-Henning Kamp 	default:
5593e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
5602b120974SPeter Wemm 	}
56115a66c21SBruce M Simpson 	return (0);
5622b120974SPeter Wemm }
5632b120974SPeter Wemm 
5642b120974SPeter Wemm static moduledata_t vlan_mod = {
5652b120974SPeter Wemm 	"if_vlan",
5662b120974SPeter Wemm 	vlan_modevent,
5672b120974SPeter Wemm 	0
5682b120974SPeter Wemm };
5692b120974SPeter Wemm 
5702b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
57111edc477SEd Maste MODULE_VERSION(if_vlan, 3);
572d35bcd3bSRuslan Ermilov MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
5732cc2df49SGarrett Wollman 
574f889d2efSBrooks Davis static struct ifnet *
575f889d2efSBrooks Davis vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
5769d4fe4b2SBrooks Davis {
577f889d2efSBrooks Davis 	const char *cp;
578f889d2efSBrooks Davis 	struct ifnet *ifp;
57915a66c21SBruce M Simpson 	int t = 0;
580f889d2efSBrooks Davis 
581f889d2efSBrooks Davis 	/* Check for <etherif>.<vlan> style interface names. */
582f889d2efSBrooks Davis 	IFNET_RLOCK();
583f889d2efSBrooks Davis 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
584f889d2efSBrooks Davis 		if (ifp->if_type != IFT_ETHER)
585f889d2efSBrooks Davis 			continue;
586f889d2efSBrooks Davis 		if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
587f889d2efSBrooks Davis 			continue;
588f889d2efSBrooks Davis 		cp = name + strlen(ifp->if_xname);
589f889d2efSBrooks Davis 		if (*cp != '.')
590f889d2efSBrooks Davis 			continue;
591f889d2efSBrooks Davis 		for(; *cp != '\0'; cp++) {
592f889d2efSBrooks Davis 			if (*cp < '0' || *cp > '9')
593f889d2efSBrooks Davis 				continue;
594f889d2efSBrooks Davis 			t = (t * 10) + (*cp - '0');
595f889d2efSBrooks Davis 		}
596f889d2efSBrooks Davis 		if (tag != NULL)
597f889d2efSBrooks Davis 			*tag = t;
598f889d2efSBrooks Davis 		break;
599f889d2efSBrooks Davis 	}
600f889d2efSBrooks Davis 	IFNET_RUNLOCK();
601f889d2efSBrooks Davis 
60215a66c21SBruce M Simpson 	return (ifp);
603f889d2efSBrooks Davis }
604f889d2efSBrooks Davis 
605f889d2efSBrooks Davis static int
606f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
607f889d2efSBrooks Davis {
608f889d2efSBrooks Davis 	const char *cp;
609f889d2efSBrooks Davis 
610f889d2efSBrooks Davis 	if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
611f889d2efSBrooks Davis 		return (1);
612f889d2efSBrooks Davis 
613f889d2efSBrooks Davis 	if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
614f889d2efSBrooks Davis 		return (0);
615f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
616f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
617f889d2efSBrooks Davis 			return (0);
618f889d2efSBrooks Davis 	}
619f889d2efSBrooks Davis 
620f889d2efSBrooks Davis 	return (1);
621f889d2efSBrooks Davis }
622f889d2efSBrooks Davis 
623f889d2efSBrooks Davis static int
6246b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
625f889d2efSBrooks Davis {
626f889d2efSBrooks Davis 	char *dp;
627f889d2efSBrooks Davis 	int wildcard;
628f889d2efSBrooks Davis 	int unit;
629f889d2efSBrooks Davis 	int error;
630f889d2efSBrooks Davis 	int tag;
631f889d2efSBrooks Davis 	int ethertag;
6329d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
6339d4fe4b2SBrooks Davis 	struct ifnet *ifp;
634f889d2efSBrooks Davis 	struct ifnet *p;
6356b7330e2SSam Leffler 	struct vlanreq vlr;
636114c608cSYaroslav Tykhiy 	static const u_char eaddr[6];	/* 00:00:00:00:00:00 */
637f889d2efSBrooks Davis 
6386b7330e2SSam Leffler 	/*
6396b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
6406b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
6416b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
6426b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
6436b7330e2SSam Leffler 	 *   must be configured separately.
6446b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
6456b7330e2SSam Leffler 	 * supported for backwards compatibilty.
6466b7330e2SSam Leffler 	 */
6476b7330e2SSam Leffler 	if (params) {
6486b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
6496b7330e2SSam Leffler 		if (error)
6506b7330e2SSam Leffler 			return error;
6516b7330e2SSam Leffler 		p = ifunit(vlr.vlr_parent);
6526b7330e2SSam Leffler 		if (p == NULL)
6536b7330e2SSam Leffler 			return ENXIO;
6546b7330e2SSam Leffler 		/*
6556b7330e2SSam Leffler 		 * Don't let the caller set up a VLAN tag with
6566b7330e2SSam Leffler 		 * anything except VLID bits.
6576b7330e2SSam Leffler 		 */
6586b7330e2SSam Leffler 		if (vlr.vlr_tag & ~EVL_VLID_MASK)
6596b7330e2SSam Leffler 			return (EINVAL);
6606b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
6616b7330e2SSam Leffler 		if (error != 0)
6626b7330e2SSam Leffler 			return (error);
6636b7330e2SSam Leffler 
6646b7330e2SSam Leffler 		ethertag = 1;
6656b7330e2SSam Leffler 		tag = vlr.vlr_tag;
6666b7330e2SSam Leffler 		wildcard = (unit < 0);
6676b7330e2SSam Leffler 	} else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
668f889d2efSBrooks Davis 		ethertag = 1;
669f889d2efSBrooks Davis 		unit = -1;
670f889d2efSBrooks Davis 		wildcard = 0;
671f889d2efSBrooks Davis 
672f889d2efSBrooks Davis 		/*
673f889d2efSBrooks Davis 		 * Don't let the caller set up a VLAN tag with
674f889d2efSBrooks Davis 		 * anything except VLID bits.
675f889d2efSBrooks Davis 		 */
67615a66c21SBruce M Simpson 		if (tag & ~EVL_VLID_MASK)
677f889d2efSBrooks Davis 			return (EINVAL);
678f889d2efSBrooks Davis 	} else {
679f889d2efSBrooks Davis 		ethertag = 0;
680f889d2efSBrooks Davis 
681f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
682f889d2efSBrooks Davis 		if (error != 0)
683f889d2efSBrooks Davis 			return (error);
684f889d2efSBrooks Davis 
685f889d2efSBrooks Davis 		wildcard = (unit < 0);
686f889d2efSBrooks Davis 	}
687f889d2efSBrooks Davis 
688f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
689f889d2efSBrooks Davis 	if (error != 0)
690f889d2efSBrooks Davis 		return (error);
691f889d2efSBrooks Davis 
692f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
693f889d2efSBrooks Davis 	if (wildcard) {
694f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
695f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
696f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
697f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
698f889d2efSBrooks Davis 		}
699f889d2efSBrooks Davis 	}
7009d4fe4b2SBrooks Davis 
701a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
702fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
703fc74a9f9SBrooks Davis 	if (ifp == NULL) {
704fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
705fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
706fc74a9f9SBrooks Davis 		return (ENOSPC);
707fc74a9f9SBrooks Davis 	}
7089d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
7099d4fe4b2SBrooks Davis 
7109d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
711f889d2efSBrooks Davis 	/*
712cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
713f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
714f889d2efSBrooks Davis 	 */
715f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
716f889d2efSBrooks Davis 	ifp->if_dname = ifc->ifc_name;
717f889d2efSBrooks Davis 	ifp->if_dunit = unit;
7189d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
7199d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
72015a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
7219d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
7229d4fe4b2SBrooks Davis 
723114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
7249d4fe4b2SBrooks Davis 	ifp->if_start = vlan_start;
7259d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
7269d4fe4b2SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
72764a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
728fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
7299d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
730211f625aSBill Fenner 	ifp->if_baudrate = 0;
731a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
732a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
7339d4fe4b2SBrooks Davis 
734f889d2efSBrooks Davis 	if (ethertag) {
73575ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, tag);
736f889d2efSBrooks Davis 		if (error != 0) {
737f889d2efSBrooks Davis 			/*
738f889d2efSBrooks Davis 			 * Since we've partialy failed, we need to back
739f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
740f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
741f889d2efSBrooks Davis 			 */
742f889d2efSBrooks Davis 			ether_ifdetach(ifp);
743249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
744fc74a9f9SBrooks Davis 			if_free_type(ifp, IFT_ETHER);
745f889d2efSBrooks Davis 			free(ifv, M_VLAN);
746f889d2efSBrooks Davis 
747f889d2efSBrooks Davis 			return (error);
748f889d2efSBrooks Davis 		}
749f889d2efSBrooks Davis 
7501cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
7511cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
752f889d2efSBrooks Davis 	}
753f889d2efSBrooks Davis 
7549d4fe4b2SBrooks Davis 	return (0);
7559d4fe4b2SBrooks Davis }
7569d4fe4b2SBrooks Davis 
757f889d2efSBrooks Davis static int
758f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
7599d4fe4b2SBrooks Davis {
7609d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
761114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
762b4e9f837SBrooks Davis 
763249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
764249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
765f3447eb4SBrooks Davis 	if_free_type(ifp, IFT_ETHER);
7669d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
767b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
768b4e9f837SBrooks Davis 
769f889d2efSBrooks Davis 	return (0);
7709d4fe4b2SBrooks Davis }
7719d4fe4b2SBrooks Davis 
77215a66c21SBruce M Simpson /*
77315a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
77415a66c21SBruce M Simpson  */
7752cc2df49SGarrett Wollman static void
776114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
7772cc2df49SGarrett Wollman {
7782cc2df49SGarrett Wollman }
7792cc2df49SGarrett Wollman 
7806d3a3ab7SGleb Smirnoff /*
7816d3a3ab7SGleb Smirnoff  * The if_start method for vlan(4) interface. It doesn't
7826d3a3ab7SGleb Smirnoff  * raises the IFF_DRV_OACTIVE flag, since it is called
7836d3a3ab7SGleb Smirnoff  * only from IFQ_HANDOFF() macro in ether_output_frame().
7846d3a3ab7SGleb Smirnoff  * If the interface queue is full, and vlan_start() is
7856d3a3ab7SGleb Smirnoff  * not called, the queue would never get emptied and
7866d3a3ab7SGleb Smirnoff  * interface would stall forever.
7876d3a3ab7SGleb Smirnoff  */
7882cc2df49SGarrett Wollman static void
7892cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp)
7902cc2df49SGarrett Wollman {
7912cc2df49SGarrett Wollman 	struct ifvlan *ifv;
7922cc2df49SGarrett Wollman 	struct ifnet *p;
7935326b23cSMatthew N. Dodd 	struct mbuf *m;
794affc907dSMax Laier 	int error;
7952cc2df49SGarrett Wollman 
7962cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
79775ee267cSGleb Smirnoff 	p = PARENT(ifv);
7982cc2df49SGarrett Wollman 
7992cc2df49SGarrett Wollman 	for (;;) {
8002cc2df49SGarrett Wollman 		IF_DEQUEUE(&ifp->if_snd, m);
8012cc2df49SGarrett Wollman 		if (m == 0)
8022cc2df49SGarrett Wollman 			break;
803a3814acfSSam Leffler 		BPF_MTAP(ifp, m);
8042cc2df49SGarrett Wollman 
805f731f104SBill Paul 		/*
80624993214SYaroslav Tykhiy 		 * Do not run parent's if_start() if the parent is not up,
80724993214SYaroslav Tykhiy 		 * or parent's driver will cause a system crash.
80824993214SYaroslav Tykhiy 		 */
80913f4c340SRobert Watson 		if (!((p->if_flags & IFF_UP) &&
81013f4c340SRobert Watson 		    (p->if_drv_flags & IFF_DRV_RUNNING))) {
81124993214SYaroslav Tykhiy 			m_freem(m);
812a3814acfSSam Leffler 			ifp->if_collisions++;
81324993214SYaroslav Tykhiy 			continue;
81424993214SYaroslav Tykhiy 		}
81524993214SYaroslav Tykhiy 
81624993214SYaroslav Tykhiy 		/*
817f6e5e0adSYaroslav Tykhiy 		 * Pad the frame to the minimum size allowed if told to.
818f6e5e0adSYaroslav Tykhiy 		 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
819f6e5e0adSYaroslav Tykhiy 		 * paragraph C.4.4.3.b.  It can help to work around buggy
820f6e5e0adSYaroslav Tykhiy 		 * bridges that violate paragraph C.4.4.3.a from the same
821f6e5e0adSYaroslav Tykhiy 		 * document, i.e., fail to pad short frames after untagging.
822f6e5e0adSYaroslav Tykhiy 		 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
823f6e5e0adSYaroslav Tykhiy 		 * untagging it will produce a 62-byte frame, which is a runt
824f6e5e0adSYaroslav Tykhiy 		 * and requires padding.  There are VLAN-enabled network
825f6e5e0adSYaroslav Tykhiy 		 * devices that just discard such runts instead or mishandle
826f6e5e0adSYaroslav Tykhiy 		 * them somehow.
827f6e5e0adSYaroslav Tykhiy 		 */
828f6e5e0adSYaroslav Tykhiy 		if (soft_pad) {
829f6e5e0adSYaroslav Tykhiy 			static char pad[8];	/* just zeros */
830f6e5e0adSYaroslav Tykhiy 			int n;
831f6e5e0adSYaroslav Tykhiy 
832f6e5e0adSYaroslav Tykhiy 			for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
833f6e5e0adSYaroslav Tykhiy 			     n > 0; n -= sizeof(pad))
834f6e5e0adSYaroslav Tykhiy 				if (!m_append(m, min(n, sizeof(pad)), pad))
835f6e5e0adSYaroslav Tykhiy 					break;
836f6e5e0adSYaroslav Tykhiy 
837f6e5e0adSYaroslav Tykhiy 			if (n > 0) {
838f6e5e0adSYaroslav Tykhiy 				if_printf(ifp, "cannot pad short frame\n");
839f6e5e0adSYaroslav Tykhiy 				ifp->if_oerrors++;
840f6e5e0adSYaroslav Tykhiy 				m_freem(m);
841f6e5e0adSYaroslav Tykhiy 				continue;
842f6e5e0adSYaroslav Tykhiy 			}
843f6e5e0adSYaroslav Tykhiy 		}
844f6e5e0adSYaroslav Tykhiy 
845f6e5e0adSYaroslav Tykhiy 		/*
846a3814acfSSam Leffler 		 * If underlying interface can do VLAN tag insertion itself,
847a3814acfSSam Leffler 		 * just pass the packet along. However, we need some way to
848a3814acfSSam Leffler 		 * tell the interface where the packet came from so that it
849a3814acfSSam Leffler 		 * knows how to find the VLAN tag to use, so we attach a
850a3814acfSSam Leffler 		 * packet tag that holds it.
851f731f104SBill Paul 		 */
852b08347a0SYaroslav Tykhiy 		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
85378ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = ifv->ifv_tag;
854ba26134bSGleb Smirnoff 			m->m_flags |= M_VLANTAG;
855f731f104SBill Paul 		} else {
85675ee267cSGleb Smirnoff 			struct ether_vlan_header *evl;
85775ee267cSGleb Smirnoff 
858a163d034SWarner Losh 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
8594af90a4dSMatthew N. Dodd 			if (m == NULL) {
8606cbd3e99SYaroslav Tykhiy 				if_printf(ifp,
8616cbd3e99SYaroslav Tykhiy 				    "unable to prepend VLAN header\n");
86226f9b263SRuslan Ermilov 				ifp->if_oerrors++;
8632cc2df49SGarrett Wollman 				continue;
8644af90a4dSMatthew N. Dodd 			}
8652cc2df49SGarrett Wollman 			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
8662cc2df49SGarrett Wollman 
867a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl)) {
868a3814acfSSam Leffler 				m = m_pullup(m, sizeof(*evl));
8695326b23cSMatthew N. Dodd 				if (m == NULL) {
870a3814acfSSam Leffler 					if_printf(ifp,
8716cbd3e99SYaroslav Tykhiy 					    "cannot pullup VLAN header\n");
87226f9b263SRuslan Ermilov 					ifp->if_oerrors++;
8734af90a4dSMatthew N. Dodd 					continue;
8744af90a4dSMatthew N. Dodd 				}
875a3814acfSSam Leffler 			}
8764af90a4dSMatthew N. Dodd 
8772cc2df49SGarrett Wollman 			/*
8782cc2df49SGarrett Wollman 			 * Transform the Ethernet header into an Ethernet header
8792cc2df49SGarrett Wollman 			 * with 802.1Q encapsulation.
8802cc2df49SGarrett Wollman 			 */
881a3814acfSSam Leffler 			bcopy(mtod(m, char *) + ifv->ifv_encaplen,
882797f247bSMatthew N. Dodd 			      mtod(m, char *), ETHER_HDR_LEN);
8832cc2df49SGarrett Wollman 			evl = mtod(m, struct ether_vlan_header *);
8842cc2df49SGarrett Wollman 			evl->evl_proto = evl->evl_encap_proto;
88573f2233dSYaroslav Tykhiy 			evl->evl_encap_proto = htons(ifv->ifv_proto);
8862cc2df49SGarrett Wollman 			evl->evl_tag = htons(ifv->ifv_tag);
887f731f104SBill Paul #ifdef DEBUG
88883908c65SRuslan Ermilov 			printf("%s: %*D\n", __func__, (int)sizeof(*evl),
88983908c65SRuslan Ermilov 			    (unsigned char *)evl, ":");
890f731f104SBill Paul #endif
891f731f104SBill Paul 		}
8922cc2df49SGarrett Wollman 
8932cc2df49SGarrett Wollman 		/*
8942cc2df49SGarrett Wollman 		 * Send it, precisely as ether_output() would have.
8952cc2df49SGarrett Wollman 		 * We are already running at splimp.
8962cc2df49SGarrett Wollman 		 */
897affc907dSMax Laier 		IFQ_HANDOFF(p, m, error);
898affc907dSMax Laier 		if (!error)
899f731f104SBill Paul 			ifp->if_opackets++;
900df5e1987SJonathan Lemon 		else
901df5e1987SJonathan Lemon 			ifp->if_oerrors++;
9022cc2df49SGarrett Wollman 	}
903f731f104SBill Paul }
904f731f104SBill Paul 
905a3814acfSSam Leffler static void
906a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
907f731f104SBill Paul {
90875ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
909f731f104SBill Paul 	struct ifvlan *ifv;
91078ba57b9SAndre Oppermann 	int inenc = 0;
91175ee267cSGleb Smirnoff 	uint16_t tag;
91275ee267cSGleb Smirnoff 
91375ee267cSGleb Smirnoff 	KASSERT(trunk != NULL, ("%s: no trunk", __func__));
914a3814acfSSam Leffler 
915f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
916a3814acfSSam Leffler 		/*
91714e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
918a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
919a3814acfSSam Leffler 		 */
92078ba57b9SAndre Oppermann 		tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
9216ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
922a3814acfSSam Leffler 	} else {
92375ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
92475ee267cSGleb Smirnoff 
92514e98256SYaroslav Tykhiy 		/*
92614e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
92714e98256SYaroslav Tykhiy 		 */
92878ba57b9SAndre Oppermann 		inenc = 1;
929a3814acfSSam Leffler 		switch (ifp->if_type) {
930a3814acfSSam Leffler 		case IFT_ETHER:
931a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
932a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
933a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
934a3814acfSSam Leffler 				return;
935a3814acfSSam Leffler 			}
936a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
937fb88a3e0SBill Paul 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
938db8b5973SYaroslav Tykhiy 
939db8b5973SYaroslav Tykhiy 			/*
940db8b5973SYaroslav Tykhiy 			 * Restore the original ethertype.  We'll remove
941db8b5973SYaroslav Tykhiy 			 * the encapsulation after we've found the vlan
942db8b5973SYaroslav Tykhiy 			 * interface corresponding to the tag.
943db8b5973SYaroslav Tykhiy 			 */
944db8b5973SYaroslav Tykhiy 			evl->evl_encap_proto = evl->evl_proto;
945a3814acfSSam Leffler 			break;
946a3814acfSSam Leffler 		default:
947db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
94860c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
94960c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
950a3814acfSSam Leffler #endif
95160c60618SYaroslav Tykhiy 			m_freem(m);
95260c60618SYaroslav Tykhiy 			ifp->if_noproto++;
95360c60618SYaroslav Tykhiy 			return;
954a3814acfSSam Leffler 		}
9557a46ec8fSBrooks Davis 	}
9567a46ec8fSBrooks Davis 
95715ed2fa1SYaroslav Tykhiy 	TRUNK_RLOCK(trunk);
95875ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
95975ee267cSGleb Smirnoff 	ifv = trunk->vlans[tag];
96075ee267cSGleb Smirnoff #else
96175ee267cSGleb Smirnoff 	ifv = vlan_gethash(trunk, tag);
96215ed2fa1SYaroslav Tykhiy #endif
96375ee267cSGleb Smirnoff 	if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
96475ee267cSGleb Smirnoff 		TRUNK_RUNLOCK(trunk);
96575ee267cSGleb Smirnoff 		m_freem(m);
96675ee267cSGleb Smirnoff 		ifp->if_noproto++;
96775ee267cSGleb Smirnoff 		return;
96875ee267cSGleb Smirnoff 	}
96975ee267cSGleb Smirnoff 	TRUNK_RUNLOCK(trunk);
970f731f104SBill Paul 
97178ba57b9SAndre Oppermann 	if (inenc) {
972f731f104SBill Paul 		/*
973a3814acfSSam Leffler 		 * Packet had an in-line encapsulation header;
974db8b5973SYaroslav Tykhiy 		 * remove it.  The original header has already
975db8b5973SYaroslav Tykhiy 		 * been fixed up above.
976f731f104SBill Paul 		 */
977a3814acfSSam Leffler 		bcopy(mtod(m, caddr_t),
978a3814acfSSam Leffler 		      mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
979db8b5973SYaroslav Tykhiy 		      ETHER_HDR_LEN);
980a3814acfSSam Leffler 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
981a3814acfSSam Leffler 	}
982a3814acfSSam Leffler 
983fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
984fc74a9f9SBrooks Davis 	ifv->ifv_ifp->if_ipackets++;
9852cc2df49SGarrett Wollman 
986a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
987fc74a9f9SBrooks Davis 	(*ifp->if_input)(ifv->ifv_ifp, m);
9882cc2df49SGarrett Wollman }
9892cc2df49SGarrett Wollman 
9902cc2df49SGarrett Wollman static int
99175ee267cSGleb Smirnoff vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
9922cc2df49SGarrett Wollman {
99375ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
9941cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
99575ee267cSGleb Smirnoff 	int error = 0;
9962cc2df49SGarrett Wollman 
99775ee267cSGleb Smirnoff 	/* VID numbers 0x0 and 0xFFF are reserved */
99875ee267cSGleb Smirnoff 	if (tag == 0 || tag == 0xFFF)
99975ee267cSGleb Smirnoff 		return (EINVAL);
10001cf236fbSYaroslav Tykhiy 	if (p->if_type != IFT_ETHER)
100115a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
100264a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
100364a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
100475ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
100515a66c21SBruce M Simpson 		return (EBUSY);
10062cc2df49SGarrett Wollman 
100775ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
100875ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
100975ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
101075ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
101175ee267cSGleb Smirnoff 		vlan_inithash(trunk);
101275ee267cSGleb Smirnoff #endif
101305a2398fSGleb Smirnoff 		VLAN_LOCK();
101405a2398fSGleb Smirnoff 		if (p->if_vlantrunk != NULL) {
101505a2398fSGleb Smirnoff 			/* A race that that is very unlikely to be hit. */
101605a2398fSGleb Smirnoff #ifndef VLAN_ARRAY
101705a2398fSGleb Smirnoff 			vlan_freehash(trunk);
101805a2398fSGleb Smirnoff #endif
101905a2398fSGleb Smirnoff 			free(trunk, M_VLAN);
102005a2398fSGleb Smirnoff 			goto exists;
102105a2398fSGleb Smirnoff 		}
102275ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
102375ee267cSGleb Smirnoff 		LIST_INSERT_HEAD(&trunk_list, trunk, trunk_entry);
102475ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
102575ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
102675ee267cSGleb Smirnoff 		trunk->parent = p;
102775ee267cSGleb Smirnoff 	} else {
102875ee267cSGleb Smirnoff 		VLAN_LOCK();
102975ee267cSGleb Smirnoff exists:
103075ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
103175ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
103275ee267cSGleb Smirnoff 	}
103375ee267cSGleb Smirnoff 
103415ed2fa1SYaroslav Tykhiy 	ifv->ifv_tag = tag;	/* must set this before vlan_inshash() */
103575ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
103615ed2fa1SYaroslav Tykhiy 	if (trunk->vlans[tag] != NULL) {
103775ee267cSGleb Smirnoff 		error = EEXIST;
103815ed2fa1SYaroslav Tykhiy 		goto done;
103915ed2fa1SYaroslav Tykhiy 	}
104015ed2fa1SYaroslav Tykhiy 	trunk->vlans[tag] = ifv;
104115ed2fa1SYaroslav Tykhiy 	trunk->refcnt++;
104275ee267cSGleb Smirnoff #else
104375ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
104475ee267cSGleb Smirnoff 	if (error)
104575ee267cSGleb Smirnoff 		goto done;
104615ed2fa1SYaroslav Tykhiy #endif
104773f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1048a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1049a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
10501cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1051a3814acfSSam Leffler 
1052a3814acfSSam Leffler 	/*
1053a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1054a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1055656acce4SYaroslav Tykhiy 	 * use it.
1056a3814acfSSam Leffler 	 */
1057656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1058656acce4SYaroslav Tykhiy 		/*
1059656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1060656acce4SYaroslav Tykhiy 		 * handle extended frames.
1061656acce4SYaroslav Tykhiy 		 */
1062a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1063656acce4SYaroslav Tykhiy 	} else {
1064a3814acfSSam Leffler 		/*
1065a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1066a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1067a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1068a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1069a3814acfSSam Leffler 		 * which might still be useful.
1070a3814acfSSam Leffler 		 */
1071a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1072a3814acfSSam Leffler 	}
1073a3814acfSSam Leffler 
107475ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
10751cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
10761cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
107775ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
10782cc2df49SGarrett Wollman 	/*
107924993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
108024993214SYaroslav Tykhiy 	 * Other flags are none of our business.
10812cc2df49SGarrett Wollman 	 */
108264a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
10831cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
10841cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
10851cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
10861cf236fbSYaroslav Tykhiy 
10871cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
10882cc2df49SGarrett Wollman 
108975ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1090a3814acfSSam Leffler 
1091a3814acfSSam Leffler 	/*
10922cc2df49SGarrett Wollman 	 * Set up our ``Ethernet address'' to reflect the underlying
10932cc2df49SGarrett Wollman 	 * physical interface's.
10942cc2df49SGarrett Wollman 	 */
1095d09ed26fSRuslan Ermilov 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
10961b2a4f7aSBill Fenner 
10971b2a4f7aSBill Fenner 	/*
10981b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
10991b2a4f7aSBill Fenner 	 * joined on the vlan device.
11001b2a4f7aSBill Fenner 	 */
11011cf236fbSYaroslav Tykhiy 	(void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
11022ada9747SYaroslav Tykhiy 
11032ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
11042ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
110575ee267cSGleb Smirnoff done:
110675ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
110775ee267cSGleb Smirnoff 	VLAN_UNLOCK();
110875ee267cSGleb Smirnoff 
110975ee267cSGleb Smirnoff 	return (error);
11102cc2df49SGarrett Wollman }
11112cc2df49SGarrett Wollman 
11122cc2df49SGarrett Wollman static int
1113f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1114f731f104SBill Paul {
11155cb8c31aSYaroslav Tykhiy 	int ret;
11165cb8c31aSYaroslav Tykhiy 
11175cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
11185cb8c31aSYaroslav Tykhiy 	ret = vlan_unconfig_locked(ifp);
11195cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
11205cb8c31aSYaroslav Tykhiy 	return (ret);
11215cb8c31aSYaroslav Tykhiy }
11225cb8c31aSYaroslav Tykhiy 
11235cb8c31aSYaroslav Tykhiy static int
11245cb8c31aSYaroslav Tykhiy vlan_unconfig_locked(struct ifnet *ifp)
11255cb8c31aSYaroslav Tykhiy {
112675ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1127f731f104SBill Paul 	struct vlan_mc_entry *mc;
1128f731f104SBill Paul 	struct ifvlan *ifv;
1129f731f104SBill Paul 	int error;
1130f731f104SBill Paul 
11315cb8c31aSYaroslav Tykhiy 	VLAN_LOCK_ASSERT();
11324faedfe8SSam Leffler 
1133f731f104SBill Paul 	ifv = ifp->if_softc;
113475ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
1135f731f104SBill Paul 
113675ee267cSGleb Smirnoff 	if (trunk) {
11371b2a4f7aSBill Fenner 		struct sockaddr_dl sdl;
113875ee267cSGleb Smirnoff 		struct ifnet *p = trunk->parent;
113975ee267cSGleb Smirnoff 
114075ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
11411b2a4f7aSBill Fenner 
1142f731f104SBill Paul 		/*
1143f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1144f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
11451b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1146f731f104SBill Paul 		 */
114715a66c21SBruce M Simpson 		bzero((char *)&sdl, sizeof(sdl));
114815a66c21SBruce M Simpson 		sdl.sdl_len = sizeof(sdl);
1149f731f104SBill Paul 		sdl.sdl_family = AF_LINK;
11501b2a4f7aSBill Fenner 		sdl.sdl_index = p->if_index;
11511b2a4f7aSBill Fenner 		sdl.sdl_type = IFT_ETHER;
11521b2a4f7aSBill Fenner 		sdl.sdl_alen = ETHER_ADDR_LEN;
11531b2a4f7aSBill Fenner 
11541b2a4f7aSBill Fenner 		while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
115522f29826SPoul-Henning Kamp 			mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
115615a66c21SBruce M Simpson 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
115715a66c21SBruce M Simpson 			    ETHER_ADDR_LEN);
1158f731f104SBill Paul 			error = if_delmulti(p, (struct sockaddr *)&sdl);
1159f731f104SBill Paul 			if (error)
1160f731f104SBill Paul 				return (error);
1161f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
11629d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
1163f731f104SBill Paul 		}
1164a3814acfSSam Leffler 
11651cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
116615ed2fa1SYaroslav Tykhiy #ifdef VLAN_ARRAY
116715ed2fa1SYaroslav Tykhiy 		trunk->vlans[ifv->ifv_tag] = NULL;
116815ed2fa1SYaroslav Tykhiy 		trunk->refcnt--;
116915ed2fa1SYaroslav Tykhiy #else
117075ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
117175ee267cSGleb Smirnoff #endif
117275ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
117375ee267cSGleb Smirnoff 
117475ee267cSGleb Smirnoff 		/*
117575ee267cSGleb Smirnoff 		 * Check if we were the last.
117675ee267cSGleb Smirnoff 		 */
117775ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
117815ed2fa1SYaroslav Tykhiy 			trunk->parent->if_vlantrunk = NULL;
117975ee267cSGleb Smirnoff 			/*
118075ee267cSGleb Smirnoff 			 * XXXGL: If some ithread has already entered
118175ee267cSGleb Smirnoff 			 * vlan_input() and is now blocked on the trunk
118275ee267cSGleb Smirnoff 			 * lock, then it should preempt us right after
118375ee267cSGleb Smirnoff 			 * unlock and finish its work. Then we will acquire
118475ee267cSGleb Smirnoff 			 * lock again in trunk_destroy().
118575ee267cSGleb Smirnoff 			 */
118675ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
118775ee267cSGleb Smirnoff 			trunk_destroy(trunk);
118875ee267cSGleb Smirnoff 		} else
118975ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
11901b2a4f7aSBill Fenner 	}
1191f731f104SBill Paul 
1192f731f104SBill Paul 	/* Disconnect from parent. */
11931cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
11941cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
11955cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
11965cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
11975cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1198f731f104SBill Paul 
119915a66c21SBruce M Simpson 	return (0);
1200f731f104SBill Paul }
1201f731f104SBill Paul 
12021cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1203f731f104SBill Paul static int
12041cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
12051cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1206a3814acfSSam Leffler {
12071cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
12081cf236fbSYaroslav Tykhiy 	int error;
1209a3814acfSSam Leffler 
12101cf236fbSYaroslav Tykhiy 	/* XXX VLAN_LOCK_ASSERT(); */
1211a3814acfSSam Leffler 
12121cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
12131cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
12141cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
12151cf236fbSYaroslav Tykhiy 
12161cf236fbSYaroslav Tykhiy 	/*
12171cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
12181cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
12191cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
12201cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
12211cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
12221cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
12231cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
12241cf236fbSYaroslav Tykhiy 	 */
12251cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
122675ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
12271cf236fbSYaroslav Tykhiy 		if (error)
1228a3814acfSSam Leffler 			return (error);
12291cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
12301cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
12311cf236fbSYaroslav Tykhiy 	}
12321cf236fbSYaroslav Tykhiy 	return (0);
12331cf236fbSYaroslav Tykhiy }
12341cf236fbSYaroslav Tykhiy 
12351cf236fbSYaroslav Tykhiy /*
12361cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
12371cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
12381cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
12391cf236fbSYaroslav Tykhiy  */
12401cf236fbSYaroslav Tykhiy static int
12411cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
12421cf236fbSYaroslav Tykhiy {
12431cf236fbSYaroslav Tykhiy 	int error, i;
12441cf236fbSYaroslav Tykhiy 
12451cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
12461cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
12471cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
12481cf236fbSYaroslav Tykhiy 		if (error)
12491cf236fbSYaroslav Tykhiy 			return (error);
12501cf236fbSYaroslav Tykhiy 	}
12511cf236fbSYaroslav Tykhiy 	return (0);
1252a3814acfSSam Leffler }
1253a3814acfSSam Leffler 
1254127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1255127d7b2dSAndre Oppermann static void
1256127d7b2dSAndre Oppermann vlan_link_state(struct ifnet *ifp, int link)
1257127d7b2dSAndre Oppermann {
125875ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1259127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
126075ee267cSGleb Smirnoff 	int i;
1261127d7b2dSAndre Oppermann 
126275ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
126375ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
126415ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
126575ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
126675ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
126775ee267cSGleb Smirnoff #else
1268aad0be7aSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
1269aad0be7aSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
127075ee267cSGleb Smirnoff #endif
1271aad0be7aSGleb Smirnoff 			ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1272fc74a9f9SBrooks Davis 			if_link_state_change(ifv->ifv_ifp,
127375ee267cSGleb Smirnoff 			    trunk->parent->if_link_state);
1274127d7b2dSAndre Oppermann 		}
127575ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
127675ee267cSGleb Smirnoff }
127775ee267cSGleb Smirnoff 
127875ee267cSGleb Smirnoff static void
127975ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
128075ee267cSGleb Smirnoff {
128175ee267cSGleb Smirnoff 	struct ifnet *p = PARENT(ifv);
128275ee267cSGleb Smirnoff 	struct ifnet *ifp = ifv->ifv_ifp;
128375ee267cSGleb Smirnoff 
128475ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(TRUNK(ifv));
128575ee267cSGleb Smirnoff 
128675ee267cSGleb Smirnoff 	/*
128775ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
128875ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
128975ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
129075ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
129175ee267cSGleb Smirnoff 	 */
129275ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
129375ee267cSGleb Smirnoff 		ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
129475ee267cSGleb Smirnoff 
129575ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
129675ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
129775ee267cSGleb Smirnoff 		ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
129875ee267cSGleb Smirnoff 		ifp->if_hwassist = p->if_hwassist;
129975ee267cSGleb Smirnoff 	} else {
130075ee267cSGleb Smirnoff 		ifp->if_capenable = 0;
130175ee267cSGleb Smirnoff 		ifp->if_hwassist = 0;
130275ee267cSGleb Smirnoff 	}
130375ee267cSGleb Smirnoff }
130475ee267cSGleb Smirnoff 
130575ee267cSGleb Smirnoff static void
130675ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
130775ee267cSGleb Smirnoff {
130875ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
130975ee267cSGleb Smirnoff 	struct ifvlan *ifv;
131075ee267cSGleb Smirnoff 	int i;
131175ee267cSGleb Smirnoff 
131275ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
131375ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
131415ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
131575ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
131675ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
131775ee267cSGleb Smirnoff #else
131875ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
131975ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
132075ee267cSGleb Smirnoff #endif
132175ee267cSGleb Smirnoff 			vlan_capabilities(ifv);
132275ee267cSGleb Smirnoff 	}
132375ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
1324127d7b2dSAndre Oppermann }
1325127d7b2dSAndre Oppermann 
1326a3814acfSSam Leffler static int
1327cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
13282cc2df49SGarrett Wollman {
13292cc2df49SGarrett Wollman 	struct ifaddr *ifa;
13302cc2df49SGarrett Wollman 	struct ifnet *p;
13312cc2df49SGarrett Wollman 	struct ifreq *ifr;
13322cc2df49SGarrett Wollman 	struct ifvlan *ifv;
13332cc2df49SGarrett Wollman 	struct vlanreq vlr;
13342cc2df49SGarrett Wollman 	int error = 0;
13352cc2df49SGarrett Wollman 
13362cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
13372cc2df49SGarrett Wollman 	ifa = (struct ifaddr *)data;
13382cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
13392cc2df49SGarrett Wollman 
13402cc2df49SGarrett Wollman 	switch (cmd) {
13412cc2df49SGarrett Wollman 	case SIOCSIFADDR:
13422cc2df49SGarrett Wollman 		ifp->if_flags |= IFF_UP;
13432cc2df49SGarrett Wollman 
13442cc2df49SGarrett Wollman 		switch (ifa->ifa_addr->sa_family) {
13452cc2df49SGarrett Wollman #ifdef INET
13462cc2df49SGarrett Wollman 		case AF_INET:
1347fc74a9f9SBrooks Davis 			arp_ifinit(ifv->ifv_ifp, ifa);
13482cc2df49SGarrett Wollman 			break;
13492cc2df49SGarrett Wollman #endif
13502cc2df49SGarrett Wollman 		default:
13512cc2df49SGarrett Wollman 			break;
13522cc2df49SGarrett Wollman 		}
13532cc2df49SGarrett Wollman 		break;
13542cc2df49SGarrett Wollman 
13552cc2df49SGarrett Wollman 	case SIOCGIFADDR:
13562cc2df49SGarrett Wollman 		{
13572cc2df49SGarrett Wollman 			struct sockaddr *sa;
13582cc2df49SGarrett Wollman 
13592cc2df49SGarrett Wollman 			sa = (struct sockaddr *) &ifr->ifr_data;
13604a0d6638SRuslan Ermilov 			bcopy(IF_LLADDR(ifp), (caddr_t)sa->sa_data,
136115a66c21SBruce M Simpson 			    ETHER_ADDR_LEN);
13622cc2df49SGarrett Wollman 		}
13632cc2df49SGarrett Wollman 		break;
13642cc2df49SGarrett Wollman 
1365b3cca108SBill Fenner 	case SIOCGIFMEDIA:
13664faedfe8SSam Leffler 		VLAN_LOCK();
136775ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
136875ee267cSGleb Smirnoff 			error = (*PARENT(ifv)->if_ioctl)(PARENT(ifv),
13694faedfe8SSam Leffler 					SIOCGIFMEDIA, data);
13704faedfe8SSam Leffler 			VLAN_UNLOCK();
1371b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1372b3cca108SBill Fenner 			if (error == 0) {
1373b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1374b3cca108SBill Fenner 
1375b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1376b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1377b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1378b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1379b3cca108SBill Fenner 						ifmr->ifm_ulist,
1380b3cca108SBill Fenner 						sizeof(int));
1381b3cca108SBill Fenner 				}
1382b3cca108SBill Fenner 			}
13834faedfe8SSam Leffler 		} else {
13844faedfe8SSam Leffler 			VLAN_UNLOCK();
1385b3cca108SBill Fenner 			error = EINVAL;
13864faedfe8SSam Leffler 		}
1387b3cca108SBill Fenner 		break;
1388b3cca108SBill Fenner 
1389b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1390b3cca108SBill Fenner 		error = EINVAL;
1391b3cca108SBill Fenner 		break;
1392b3cca108SBill Fenner 
13932cc2df49SGarrett Wollman 	case SIOCSIFMTU:
13942cc2df49SGarrett Wollman 		/*
13952cc2df49SGarrett Wollman 		 * Set the interface MTU.
13962cc2df49SGarrett Wollman 		 */
13974faedfe8SSam Leffler 		VLAN_LOCK();
139875ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1399a3814acfSSam Leffler 			if (ifr->ifr_mtu >
140075ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1401a3814acfSSam Leffler 			    ifr->ifr_mtu <
1402a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
14032cc2df49SGarrett Wollman 				error = EINVAL;
1404a3814acfSSam Leffler 			else
14052cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1406a3814acfSSam Leffler 		} else
1407a3814acfSSam Leffler 			error = EINVAL;
14084faedfe8SSam Leffler 		VLAN_UNLOCK();
14092cc2df49SGarrett Wollman 		break;
14102cc2df49SGarrett Wollman 
14112cc2df49SGarrett Wollman 	case SIOCSETVLAN:
141215a66c21SBruce M Simpson 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
14132cc2df49SGarrett Wollman 		if (error)
14142cc2df49SGarrett Wollman 			break;
14152cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1416f731f104SBill Paul 			vlan_unconfig(ifp);
14172cc2df49SGarrett Wollman 			break;
14182cc2df49SGarrett Wollman 		}
14192cc2df49SGarrett Wollman 		p = ifunit(vlr.vlr_parent);
14202cc2df49SGarrett Wollman 		if (p == 0) {
14212cc2df49SGarrett Wollman 			error = ENOENT;
14222cc2df49SGarrett Wollman 			break;
14232cc2df49SGarrett Wollman 		}
1424fb88a3e0SBill Paul 		/*
1425fb88a3e0SBill Paul 		 * Don't let the caller set up a VLAN tag with
1426fb88a3e0SBill Paul 		 * anything except VLID bits.
1427fb88a3e0SBill Paul 		 */
1428fb88a3e0SBill Paul 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1429fb88a3e0SBill Paul 			error = EINVAL;
1430fb88a3e0SBill Paul 			break;
1431fb88a3e0SBill Paul 		}
143275ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
143375ee267cSGleb Smirnoff 		if (error)
14342cc2df49SGarrett Wollman 			break;
1435a3814acfSSam Leffler 
14361cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
14371cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
14382cc2df49SGarrett Wollman 		break;
14392cc2df49SGarrett Wollman 
14402cc2df49SGarrett Wollman 	case SIOCGETVLAN:
144115a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
14424faedfe8SSam Leffler 		VLAN_LOCK();
144375ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
144475ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
14459bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
14462cc2df49SGarrett Wollman 			vlr.vlr_tag = ifv->ifv_tag;
14472cc2df49SGarrett Wollman 		}
14484faedfe8SSam Leffler 		VLAN_UNLOCK();
144915a66c21SBruce M Simpson 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
14502cc2df49SGarrett Wollman 		break;
14512cc2df49SGarrett Wollman 
14522cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
14532cc2df49SGarrett Wollman 		/*
14541cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
14551cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
14562cc2df49SGarrett Wollman 		 */
145775ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
14581cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
14592cc2df49SGarrett Wollman 		break;
1460a3814acfSSam Leffler 
1461f731f104SBill Paul 	case SIOCADDMULTI:
1462f731f104SBill Paul 	case SIOCDELMULTI:
146375ee267cSGleb Smirnoff 		/*
146475ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
146575ee267cSGleb Smirnoff 		 * when we do.
146675ee267cSGleb Smirnoff 		 */
146775ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
1468f731f104SBill Paul 			error = vlan_setmulti(ifp);
1469f731f104SBill Paul 		break;
147075ee267cSGleb Smirnoff 
14712cc2df49SGarrett Wollman 	default:
14722cc2df49SGarrett Wollman 		error = EINVAL;
14732cc2df49SGarrett Wollman 	}
147415a66c21SBruce M Simpson 
147515a66c21SBruce M Simpson 	return (error);
14762cc2df49SGarrett Wollman }
1477