xref: /freebsd/sys/net/if_vlan.c (revision f941c31aaec70b667b4aa81e951996de26ccaf09)
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.
325d99eb59SMarcelo Araujo  * Might be extended some day to also handle IEEE 802.1p priority
335d99eb59SMarcelo Araujo  * 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
37d9b1d615SJohn Baldwin  * ether_output() sends to us via if_transmit(), rewrite them for
38d9b1d615SJohn Baldwin  * use by the real outgoing interface, and ask it to send them.
392cc2df49SGarrett Wollman  */
402cc2df49SGarrett Wollman 
418b2d9181SPyun YongHyeon #include <sys/cdefs.h>
428b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$");
438b2d9181SPyun YongHyeon 
442c5b403eSOleg Bulyzhin #include "opt_inet.h"
4575ee267cSGleb Smirnoff #include "opt_vlan.h"
462cc2df49SGarrett Wollman 
472cc2df49SGarrett Wollman #include <sys/param.h>
48c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
492cc2df49SGarrett Wollman #include <sys/kernel.h>
5075ee267cSGleb Smirnoff #include <sys/lock.h>
51f731f104SBill Paul #include <sys/malloc.h>
522cc2df49SGarrett Wollman #include <sys/mbuf.h>
532b120974SPeter Wemm #include <sys/module.h>
54772b000fSAlexander V. Chernikov #include <sys/rmlock.h>
55f731f104SBill Paul #include <sys/queue.h>
562cc2df49SGarrett Wollman #include <sys/socket.h>
572cc2df49SGarrett Wollman #include <sys/sockio.h>
582cc2df49SGarrett Wollman #include <sys/sysctl.h>
592cc2df49SGarrett Wollman #include <sys/systm.h>
60e4cd31ddSJeff Roberson #include <sys/sx.h>
612cc2df49SGarrett Wollman 
622cc2df49SGarrett Wollman #include <net/bpf.h>
632cc2df49SGarrett Wollman #include <net/ethernet.h>
642cc2df49SGarrett Wollman #include <net/if.h>
6576039bc8SGleb Smirnoff #include <net/if_var.h>
66f889d2efSBrooks Davis #include <net/if_clone.h>
672cc2df49SGarrett Wollman #include <net/if_dl.h>
682cc2df49SGarrett Wollman #include <net/if_types.h>
692cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
704b79449eSBjoern A. Zeeb #include <net/vnet.h>
712cc2df49SGarrett Wollman 
722c5b403eSOleg Bulyzhin #ifdef INET
732c5b403eSOleg Bulyzhin #include <netinet/in.h>
742c5b403eSOleg Bulyzhin #include <netinet/if_ether.h>
752c5b403eSOleg Bulyzhin #endif
762c5b403eSOleg Bulyzhin 
7775ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
7864a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
7975ee267cSGleb Smirnoff 
802dc879b3SYaroslav Tykhiy #define	UP_AND_RUNNING(ifp) \
812dc879b3SYaroslav Tykhiy     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
822dc879b3SYaroslav Tykhiy 
8375ee267cSGleb Smirnoff LIST_HEAD(ifvlanhead, ifvlan);
8475ee267cSGleb Smirnoff 
8575ee267cSGleb Smirnoff struct ifvlantrunk {
8675ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
87772b000fSAlexander V. Chernikov 	struct	rmlock	lock;
8875ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
895cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
905cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
9175ee267cSGleb Smirnoff #else
9275ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
9375ee267cSGleb Smirnoff 	uint16_t	hmask;
9475ee267cSGleb Smirnoff 	uint16_t	hwidth;
9575ee267cSGleb Smirnoff #endif
9675ee267cSGleb Smirnoff 	int		refcnt;
9775ee267cSGleb Smirnoff };
989d4fe4b2SBrooks Davis 
99a3814acfSSam Leffler struct vlan_mc_entry {
100e4cd31ddSJeff Roberson 	struct sockaddr_dl		mc_addr;
101a3814acfSSam Leffler 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
102a3814acfSSam Leffler };
103a3814acfSSam Leffler 
104a3814acfSSam Leffler struct	ifvlan {
10575ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
106fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
10775ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
10875ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
1096667db31SAlexander V. Chernikov 	void	*ifv_cookie;
1101cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
111a3814acfSSam Leffler 	struct	ifv_linkmib {
112a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
113a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
114a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
11573f2233dSYaroslav Tykhiy 		uint16_t ifvm_proto;	/* encapsulation ethertype */
11675ee267cSGleb Smirnoff 		uint16_t ifvm_tag;	/* tag to apply on packets leaving if */
117a3814acfSSam Leffler 	}	ifv_mib;
118114c608cSYaroslav Tykhiy 	SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
119c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY
120a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
121c0cb022bSYaroslav Tykhiy #endif
122a3814acfSSam Leffler };
12373f2233dSYaroslav Tykhiy #define	ifv_proto	ifv_mib.ifvm_proto
1247983103aSRobert Watson #define	ifv_vid		ifv_mib.ifvm_tag
125a3814acfSSam Leffler #define	ifv_encaplen	ifv_mib.ifvm_encaplen
126a3814acfSSam Leffler #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
127a3814acfSSam Leffler #define	ifv_mintu	ifv_mib.ifvm_mintu
128a3814acfSSam Leffler 
12975ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
1301cf236fbSYaroslav Tykhiy static struct {
1311cf236fbSYaroslav Tykhiy 	int flag;
1321cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
1331cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
1341cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
1351cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
1361cf236fbSYaroslav Tykhiy 	{0, NULL}
1371cf236fbSYaroslav Tykhiy };
138a3814acfSSam Leffler 
1394a408dcbSBill Paul SYSCTL_DECL(_net_link);
1406472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0,
1416472ac3dSEd Schouten     "IEEE 802.1Q VLAN");
1426472ac3dSEd Schouten static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0,
1436472ac3dSEd Schouten     "for consistency");
1442cc2df49SGarrett Wollman 
145478e0520SHiroki Sato static VNET_DEFINE(int, soft_pad);
146478e0520SHiroki Sato #define	V_soft_pad	VNET(soft_pad)
147478e0520SHiroki Sato SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET,
148478e0520SHiroki Sato     &VNET_NAME(soft_pad), 0, "pad short frames before tagging");
149f6e5e0adSYaroslav Tykhiy 
15042a58907SGleb Smirnoff static const char vlanname[] = "vlan";
15142a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
1522cc2df49SGarrett Wollman 
1535cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
154ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag;
1555cb8c31aSYaroslav Tykhiy 
1564faedfe8SSam Leffler /*
15775ee267cSGleb Smirnoff  * We have a global mutex, that is used to serialize configuration
15875ee267cSGleb Smirnoff  * changes and isn't used in normal packet delivery.
15975ee267cSGleb Smirnoff  *
16067975c79SGleb Smirnoff  * We also have a per-trunk rmlock(9), that is locked shared on packet
16175ee267cSGleb Smirnoff  * processing and exclusive when configuration is changed.
16275ee267cSGleb Smirnoff  *
16375ee267cSGleb Smirnoff  * The VLAN_ARRAY substitutes the dynamic hash with a static array
164ad387028SAndrew Thompson  * with 4096 entries. In theory this can give a boost in processing,
16575ee267cSGleb Smirnoff  * however on practice it does not. Probably this is because array
16675ee267cSGleb Smirnoff  * is too big to fit into CPU cache.
1674faedfe8SSam Leffler  */
168e4cd31ddSJeff Roberson static struct sx ifv_lock;
169e4cd31ddSJeff Roberson #define	VLAN_LOCK_INIT()	sx_init(&ifv_lock, "vlan_global")
170e4cd31ddSJeff Roberson #define	VLAN_LOCK_DESTROY()	sx_destroy(&ifv_lock)
171e4cd31ddSJeff Roberson #define	VLAN_LOCK_ASSERT()	sx_assert(&ifv_lock, SA_LOCKED)
172e4cd31ddSJeff Roberson #define	VLAN_LOCK()		sx_xlock(&ifv_lock)
173e4cd31ddSJeff Roberson #define	VLAN_UNLOCK()		sx_xunlock(&ifv_lock)
174772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_INIT(trunk)	rm_init(&(trunk)->lock, vlanname)
175772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_DESTROY(trunk) rm_destroy(&(trunk)->lock)
176772b000fSAlexander V. Chernikov #define	TRUNK_LOCK(trunk)	rm_wlock(&(trunk)->lock)
177772b000fSAlexander V. Chernikov #define	TRUNK_UNLOCK(trunk)	rm_wunlock(&(trunk)->lock)
178772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_WLOCKED)
179772b000fSAlexander V. Chernikov #define	TRUNK_RLOCK(trunk)	rm_rlock(&(trunk)->lock, &tracker)
180772b000fSAlexander V. Chernikov #define	TRUNK_RUNLOCK(trunk)	rm_runlock(&(trunk)->lock, &tracker)
181772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_RASSERT(trunk) rm_assert(&(trunk)->lock, RA_RLOCKED)
182772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_READER	struct rm_priotracker tracker
18375ee267cSGleb Smirnoff 
18475ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
18575ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
18675ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
18775ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
18875ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
18975ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
19075ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
1917983103aSRobert Watson 	uint16_t vid);
19275ee267cSGleb Smirnoff #endif
19375ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
1944faedfe8SSam Leffler 
195114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
196a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
197cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
198d9b1d615SJohn Baldwin static	void vlan_qflush(struct ifnet *ifp);
1991cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
2001cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
2011cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
202f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
203d9b1d615SJohn Baldwin static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
2046f359e28SJohn Baldwin static	void vlan_unconfig(struct ifnet *ifp);
20528cc4d37SJohn Baldwin static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
20675ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
207a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
20875ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
20975ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
210f731f104SBill Paul 
211*f941c31aSGleb Smirnoff static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
212f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
2136b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
214f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
215f889d2efSBrooks Davis 
2165cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
217ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
2185cb8c31aSYaroslav Tykhiy 
21942a58907SGleb Smirnoff static struct if_clone *vlan_cloner;
2209d4fe4b2SBrooks Davis 
221ccf7ba97SMarko Zec #ifdef VIMAGE
22242a58907SGleb Smirnoff static VNET_DEFINE(struct if_clone *, vlan_cloner);
223ccf7ba97SMarko Zec #define	V_vlan_cloner	VNET(vlan_cloner)
224ccf7ba97SMarko Zec #endif
225ccf7ba97SMarko Zec 
22675ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
22775ee267cSGleb Smirnoff #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
228114c608cSYaroslav Tykhiy 
22975ee267cSGleb Smirnoff static void
23075ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
23175ee267cSGleb Smirnoff {
23275ee267cSGleb Smirnoff 	int i, n;
23375ee267cSGleb Smirnoff 
23475ee267cSGleb Smirnoff 	/*
23575ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
23675ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
23775ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
23875ee267cSGleb Smirnoff 	 */
23975ee267cSGleb Smirnoff 
24075ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
24175ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
24275ee267cSGleb Smirnoff 
24375ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
24475ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
24575ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
24675ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
24775ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
24875ee267cSGleb Smirnoff 		LIST_INIT(&trunk->hash[i]);
24975ee267cSGleb Smirnoff }
25075ee267cSGleb Smirnoff 
25175ee267cSGleb Smirnoff static void
25275ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
25375ee267cSGleb Smirnoff {
25475ee267cSGleb Smirnoff #ifdef INVARIANTS
25575ee267cSGleb Smirnoff 	int i;
25675ee267cSGleb Smirnoff 
25775ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
25875ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
25975ee267cSGleb Smirnoff 		KASSERT(LIST_EMPTY(&trunk->hash[i]),
26075ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
26175ee267cSGleb Smirnoff #endif
26275ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
26375ee267cSGleb Smirnoff 	trunk->hash = NULL;
26475ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
26575ee267cSGleb Smirnoff }
26675ee267cSGleb Smirnoff 
26775ee267cSGleb Smirnoff static int
26875ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
26975ee267cSGleb Smirnoff {
27075ee267cSGleb Smirnoff 	int i, b;
27175ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
27275ee267cSGleb Smirnoff 
27375ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
27475ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
27575ee267cSGleb Smirnoff 
27675ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
2777983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
27875ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
2797983103aSRobert Watson 		if (ifv->ifv_vid == ifv2->ifv_vid)
28075ee267cSGleb Smirnoff 			return (EEXIST);
28175ee267cSGleb Smirnoff 
28275ee267cSGleb Smirnoff 	/*
28375ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
28475ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
28575ee267cSGleb Smirnoff 	 * buckets/2.
28675ee267cSGleb Smirnoff 	 */
28775ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
28875ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
2897983103aSRobert Watson 		i = HASH(ifv->ifv_vid, trunk->hmask);
29075ee267cSGleb Smirnoff 	}
29175ee267cSGleb Smirnoff 	LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
29275ee267cSGleb Smirnoff 	trunk->refcnt++;
29375ee267cSGleb Smirnoff 
29475ee267cSGleb Smirnoff 	return (0);
29575ee267cSGleb Smirnoff }
29675ee267cSGleb Smirnoff 
29775ee267cSGleb Smirnoff static int
29875ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
29975ee267cSGleb Smirnoff {
30075ee267cSGleb Smirnoff 	int i, b;
30175ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
30275ee267cSGleb Smirnoff 
30375ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
30475ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
30575ee267cSGleb Smirnoff 
30675ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
3077983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
30875ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
30975ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
31075ee267cSGleb Smirnoff 			trunk->refcnt--;
31175ee267cSGleb Smirnoff 			LIST_REMOVE(ifv2, ifv_list);
31275ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
31375ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
31475ee267cSGleb Smirnoff 			return (0);
31575ee267cSGleb Smirnoff 		}
31675ee267cSGleb Smirnoff 
31775ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
31875ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
31975ee267cSGleb Smirnoff }
32075ee267cSGleb Smirnoff 
32175ee267cSGleb Smirnoff /*
32275ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
32375ee267cSGleb Smirnoff  */
32475ee267cSGleb Smirnoff static void
32575ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
32675ee267cSGleb Smirnoff {
32775ee267cSGleb Smirnoff 	struct ifvlan *ifv;
32875ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
32975ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
33075ee267cSGleb Smirnoff 
33175ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
33275ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
33375ee267cSGleb Smirnoff 
33475ee267cSGleb Smirnoff 	if (howmuch == 0) {
33575ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
33675ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
33775ee267cSGleb Smirnoff 		return;
33875ee267cSGleb Smirnoff 	}
33975ee267cSGleb Smirnoff 
34075ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
34175ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
34275ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
34375ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
34475ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
34575ee267cSGleb Smirnoff 		return;
34675ee267cSGleb Smirnoff 
34775ee267cSGleb Smirnoff 	/* M_NOWAIT because we're called with trunk mutex held */
34875ee267cSGleb Smirnoff 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
34975ee267cSGleb Smirnoff 	if (hash2 == NULL) {
35075ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
35175ee267cSGleb Smirnoff 		    __func__);
35275ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
35375ee267cSGleb Smirnoff 	}
35475ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
35575ee267cSGleb Smirnoff 		LIST_INIT(&hash2[j]);
35675ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
357c0cb022bSYaroslav Tykhiy 		while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
35875ee267cSGleb Smirnoff 			LIST_REMOVE(ifv, ifv_list);
3597983103aSRobert Watson 			j = HASH(ifv->ifv_vid, n2 - 1);
36075ee267cSGleb Smirnoff 			LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
36175ee267cSGleb Smirnoff 		}
36275ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
36375ee267cSGleb Smirnoff 	trunk->hash = hash2;
36475ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
36575ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
366f84b2d69SYaroslav Tykhiy 
367f84b2d69SYaroslav Tykhiy 	if (bootverbose)
368f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
369f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
37075ee267cSGleb Smirnoff }
37175ee267cSGleb Smirnoff 
37275ee267cSGleb Smirnoff static __inline struct ifvlan *
3737983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
37475ee267cSGleb Smirnoff {
37575ee267cSGleb Smirnoff 	struct ifvlan *ifv;
37675ee267cSGleb Smirnoff 
37775ee267cSGleb Smirnoff 	TRUNK_LOCK_RASSERT(trunk);
37875ee267cSGleb Smirnoff 
3797983103aSRobert Watson 	LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
3807983103aSRobert Watson 		if (ifv->ifv_vid == vid)
38175ee267cSGleb Smirnoff 			return (ifv);
38275ee267cSGleb Smirnoff 	return (NULL);
38375ee267cSGleb Smirnoff }
38475ee267cSGleb Smirnoff 
38575ee267cSGleb Smirnoff #if 0
38675ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
38775ee267cSGleb Smirnoff static void
38875ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
38975ee267cSGleb Smirnoff {
39075ee267cSGleb Smirnoff 	int i;
39175ee267cSGleb Smirnoff 	struct ifvlan *ifv;
39275ee267cSGleb Smirnoff 
39375ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
39475ee267cSGleb Smirnoff 		printf("%d: ", i);
39575ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
39675ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
39775ee267cSGleb Smirnoff 		printf("\n");
39875ee267cSGleb Smirnoff 	}
39975ee267cSGleb Smirnoff }
40075ee267cSGleb Smirnoff #endif /* 0 */
401e4cd31ddSJeff Roberson #else
402e4cd31ddSJeff Roberson 
403e4cd31ddSJeff Roberson static __inline struct ifvlan *
4047983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
405e4cd31ddSJeff Roberson {
406e4cd31ddSJeff Roberson 
4077983103aSRobert Watson 	return trunk->vlans[vid];
408e4cd31ddSJeff Roberson }
409e4cd31ddSJeff Roberson 
410e4cd31ddSJeff Roberson static __inline int
411e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
412e4cd31ddSJeff Roberson {
413e4cd31ddSJeff Roberson 
4147983103aSRobert Watson 	if (trunk->vlans[ifv->ifv_vid] != NULL)
415e4cd31ddSJeff Roberson 		return EEXIST;
4167983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = ifv;
417e4cd31ddSJeff Roberson 	trunk->refcnt++;
418e4cd31ddSJeff Roberson 
419e4cd31ddSJeff Roberson 	return (0);
420e4cd31ddSJeff Roberson }
421e4cd31ddSJeff Roberson 
422e4cd31ddSJeff Roberson static __inline int
423e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
424e4cd31ddSJeff Roberson {
425e4cd31ddSJeff Roberson 
4267983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = NULL;
427e4cd31ddSJeff Roberson 	trunk->refcnt--;
428e4cd31ddSJeff Roberson 
429e4cd31ddSJeff Roberson 	return (0);
430e4cd31ddSJeff Roberson }
431e4cd31ddSJeff Roberson 
432e4cd31ddSJeff Roberson static __inline void
433e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk)
434e4cd31ddSJeff Roberson {
435e4cd31ddSJeff Roberson }
436e4cd31ddSJeff Roberson 
437e4cd31ddSJeff Roberson static __inline void
438e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk)
439e4cd31ddSJeff Roberson {
440e4cd31ddSJeff Roberson }
441e4cd31ddSJeff Roberson 
44275ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
44375ee267cSGleb Smirnoff 
44475ee267cSGleb Smirnoff static void
44575ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
44675ee267cSGleb Smirnoff {
44775ee267cSGleb Smirnoff 	VLAN_LOCK_ASSERT();
44875ee267cSGleb Smirnoff 
44975ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
45075ee267cSGleb Smirnoff 	vlan_freehash(trunk);
45175ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
45233499e2aSYaroslav Tykhiy 	TRUNK_UNLOCK(trunk);
45333499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
45475ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
45575ee267cSGleb Smirnoff }
45675ee267cSGleb Smirnoff 
457f731f104SBill Paul /*
458f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
459f731f104SBill Paul  * programming the multicast filter of the parent. This has the
460f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
461f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
462f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
463f731f104SBill Paul  * to avoid this: there really is only one physical interface.
464f731f104SBill Paul  */
4652b120974SPeter Wemm static int
4662b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
467f731f104SBill Paul {
468f731f104SBill Paul 	struct ifnet		*ifp_p;
4692d222cb7SAlexander Motin 	struct ifmultiaddr	*ifma;
470f731f104SBill Paul 	struct ifvlan		*sc;
471c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
472f731f104SBill Paul 	int			error;
473f731f104SBill Paul 
474f731f104SBill Paul 	/* Find the parent. */
475f731f104SBill Paul 	sc = ifp->if_softc;
4762d222cb7SAlexander Motin 	TRUNK_LOCK_ASSERT(TRUNK(sc));
47775ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
4781b2a4f7aSBill Fenner 
4798b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
4808b615593SMarko Zec 
481f731f104SBill Paul 	/* First, remove any existing filter entries. */
482c0cb022bSYaroslav Tykhiy 	while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
483f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
4842d222cb7SAlexander Motin 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
4859d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
486f731f104SBill Paul 	}
487f731f104SBill Paul 
488f731f104SBill Paul 	/* Now program new ones. */
4892d222cb7SAlexander Motin 	IF_ADDR_WLOCK(ifp);
4906817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
491f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
492f731f104SBill Paul 			continue;
49329c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
4942d222cb7SAlexander Motin 		if (mc == NULL) {
4952d222cb7SAlexander Motin 			IF_ADDR_WUNLOCK(ifp);
49629c2dfbeSBruce M Simpson 			return (ENOMEM);
4972d222cb7SAlexander Motin 		}
498e4cd31ddSJeff Roberson 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
499e4cd31ddSJeff Roberson 		mc->mc_addr.sdl_index = ifp_p->if_index;
500f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
5012d222cb7SAlexander Motin 	}
5022d222cb7SAlexander Motin 	IF_ADDR_WUNLOCK(ifp);
5032d222cb7SAlexander Motin 	SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
504e4cd31ddSJeff Roberson 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
5052d222cb7SAlexander Motin 		    NULL);
506f731f104SBill Paul 		if (error)
507f731f104SBill Paul 			return (error);
508f731f104SBill Paul 	}
509f731f104SBill Paul 
5108b615593SMarko Zec 	CURVNET_RESTORE();
511f731f104SBill Paul 	return (0);
512f731f104SBill Paul }
5132cc2df49SGarrett Wollman 
514a3814acfSSam Leffler /*
515ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
516ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
517ea4ca115SAndrew Thompson  * should also change it on all children vlans.
518ea4ca115SAndrew Thompson  */
519ea4ca115SAndrew Thompson static void
520ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
521ea4ca115SAndrew Thompson {
522ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
5236117727bSAndrew Thompson #ifndef VLAN_ARRAY
5246117727bSAndrew Thompson 	struct ifvlan *next;
5256117727bSAndrew Thompson #endif
526ea4ca115SAndrew Thompson 	int i;
527ea4ca115SAndrew Thompson 
528ea4ca115SAndrew Thompson 	/*
529ea4ca115SAndrew Thompson 	 * Check if it's a trunk interface first of all
530ea4ca115SAndrew Thompson 	 * to avoid needless locking.
531ea4ca115SAndrew Thompson 	 */
532ea4ca115SAndrew Thompson 	if (ifp->if_vlantrunk == NULL)
533ea4ca115SAndrew Thompson 		return;
534ea4ca115SAndrew Thompson 
535ea4ca115SAndrew Thompson 	VLAN_LOCK();
536ea4ca115SAndrew Thompson 	/*
537ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
538ea4ca115SAndrew Thompson 	 */
539ea4ca115SAndrew Thompson #ifdef VLAN_ARRAY
540ea4ca115SAndrew Thompson 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
5416117727bSAndrew Thompson 		if ((ifv = ifp->if_vlantrunk->vlans[i])) {
542ea4ca115SAndrew Thompson #else /* VLAN_ARRAY */
543ea4ca115SAndrew Thompson 	for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
5446117727bSAndrew Thompson 		LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
545ea4ca115SAndrew Thompson #endif /* VLAN_ARRAY */
546ea4ca115SAndrew Thompson 			VLAN_UNLOCK();
547e4cd31ddSJeff Roberson 			if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp),
548e4cd31ddSJeff Roberson 			    ifp->if_addrlen);
5496117727bSAndrew Thompson 			VLAN_LOCK();
5506117727bSAndrew Thompson 		}
5516117727bSAndrew Thompson 	VLAN_UNLOCK();
552ea4ca115SAndrew Thompson 
553ea4ca115SAndrew Thompson }
554ea4ca115SAndrew Thompson 
555ea4ca115SAndrew Thompson /*
5565cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
5575cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
5585cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
5595428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
5605428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
5615cb8c31aSYaroslav Tykhiy  */
5625cb8c31aSYaroslav Tykhiy static void
5635cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
5645cb8c31aSYaroslav Tykhiy {
5655cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
5665cb8c31aSYaroslav Tykhiy 	int i;
5675cb8c31aSYaroslav Tykhiy 
5685cb8c31aSYaroslav Tykhiy 	/*
5695cb8c31aSYaroslav Tykhiy 	 * Check if it's a trunk interface first of all
5705cb8c31aSYaroslav Tykhiy 	 * to avoid needless locking.
5715cb8c31aSYaroslav Tykhiy 	 */
5725cb8c31aSYaroslav Tykhiy 	if (ifp->if_vlantrunk == NULL)
5735cb8c31aSYaroslav Tykhiy 		return;
5745cb8c31aSYaroslav Tykhiy 
5755428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
5765428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
5775428776eSJohn Baldwin 		return;
5785428776eSJohn Baldwin 
5795cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
5805cb8c31aSYaroslav Tykhiy 	/*
5815cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
5825cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
5835cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
5845cb8c31aSYaroslav Tykhiy 	 */
5855cb8c31aSYaroslav Tykhiy #ifdef VLAN_ARRAY
5865cb8c31aSYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
5875cb8c31aSYaroslav Tykhiy 		if ((ifv = ifp->if_vlantrunk->vlans[i])) {
58828cc4d37SJohn Baldwin 			vlan_unconfig_locked(ifv->ifv_ifp, 1);
5895cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk == NULL)
5905cb8c31aSYaroslav Tykhiy 				break;
5915cb8c31aSYaroslav Tykhiy 		}
5925cb8c31aSYaroslav Tykhiy #else /* VLAN_ARRAY */
5935cb8c31aSYaroslav Tykhiy restart:
5945cb8c31aSYaroslav Tykhiy 	for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
5955cb8c31aSYaroslav Tykhiy 		if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
59628cc4d37SJohn Baldwin 			vlan_unconfig_locked(ifv->ifv_ifp, 1);
5975cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk)
5985cb8c31aSYaroslav Tykhiy 				goto restart;	/* trunk->hwidth can change */
5995cb8c31aSYaroslav Tykhiy 			else
6005cb8c31aSYaroslav Tykhiy 				break;
6015cb8c31aSYaroslav Tykhiy 		}
6025cb8c31aSYaroslav Tykhiy #endif /* VLAN_ARRAY */
6035cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
6045cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
6055cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
6065cb8c31aSYaroslav Tykhiy }
6075cb8c31aSYaroslav Tykhiy 
6085cb8c31aSYaroslav Tykhiy /*
609e4cd31ddSJeff Roberson  * Return the trunk device for a virtual interface.
610e4cd31ddSJeff Roberson  */
611e4cd31ddSJeff Roberson static struct ifnet  *
612e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp)
613e4cd31ddSJeff Roberson {
614e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
615e4cd31ddSJeff Roberson 
616e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
617e4cd31ddSJeff Roberson 		return (NULL);
618e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
619e4cd31ddSJeff Roberson 	ifp = NULL;
620e4cd31ddSJeff Roberson 	VLAN_LOCK();
621e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
622e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
623e4cd31ddSJeff Roberson 	VLAN_UNLOCK();
624e4cd31ddSJeff Roberson 	return (ifp);
625e4cd31ddSJeff Roberson }
626e4cd31ddSJeff Roberson 
627e4cd31ddSJeff Roberson /*
6287983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
6297983103aSRobert Watson  * components such as Infiniband.
6307983103aSRobert Watson  *
6317983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
6327983103aSRobert Watson  * vlan_vid().
633e4cd31ddSJeff Roberson  */
634e4cd31ddSJeff Roberson static int
6357983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
636e4cd31ddSJeff Roberson {
637e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
638e4cd31ddSJeff Roberson 
639e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
640e4cd31ddSJeff Roberson 		return (EINVAL);
641e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
6427983103aSRobert Watson 	*vidp = ifv->ifv_vid;
643e4cd31ddSJeff Roberson 	return (0);
644e4cd31ddSJeff Roberson }
645e4cd31ddSJeff Roberson 
646e4cd31ddSJeff Roberson /*
647e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
648e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
649e4cd31ddSJeff Roberson  */
650e4cd31ddSJeff Roberson static void *
651e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
652e4cd31ddSJeff Roberson {
653e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
654e4cd31ddSJeff Roberson 
655e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
656e4cd31ddSJeff Roberson 		return (NULL);
657e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
658e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
659e4cd31ddSJeff Roberson }
660e4cd31ddSJeff Roberson 
661e4cd31ddSJeff Roberson /*
662e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
663e4cd31ddSJeff Roberson  * private per-instance data in.
664e4cd31ddSJeff Roberson  */
665e4cd31ddSJeff Roberson static int
666e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
667e4cd31ddSJeff Roberson {
668e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
669e4cd31ddSJeff Roberson 
670e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
671e4cd31ddSJeff Roberson 		return (EINVAL);
672e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
673e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
674e4cd31ddSJeff Roberson 	return (0);
675e4cd31ddSJeff Roberson }
676e4cd31ddSJeff Roberson 
677e4cd31ddSJeff Roberson /*
6787983103aSRobert Watson  * Return the vlan device present at the specific VID.
679e4cd31ddSJeff Roberson  */
680e4cd31ddSJeff Roberson static struct ifnet *
6817983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
682e4cd31ddSJeff Roberson {
683e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
684e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
685772b000fSAlexander V. Chernikov 	TRUNK_LOCK_READER;
686e4cd31ddSJeff Roberson 
687e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
688e4cd31ddSJeff Roberson 	if (trunk == NULL)
689e4cd31ddSJeff Roberson 		return (NULL);
690e4cd31ddSJeff Roberson 	ifp = NULL;
691e4cd31ddSJeff Roberson 	TRUNK_RLOCK(trunk);
6927983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
693e4cd31ddSJeff Roberson 	if (ifv)
694e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
695e4cd31ddSJeff Roberson 	TRUNK_RUNLOCK(trunk);
696e4cd31ddSJeff Roberson 	return (ifp);
697e4cd31ddSJeff Roberson }
698e4cd31ddSJeff Roberson 
699e4cd31ddSJeff Roberson /*
700a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
701a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
702a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
703a3814acfSSam Leffler  * set here.  Noone else in the system should be aware of this so
704a3814acfSSam Leffler  * we use an explicit reference here.
705a3814acfSSam Leffler  */
706a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
707a3814acfSSam Leffler 
708984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
709a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
710127d7b2dSAndre Oppermann 
7112b120974SPeter Wemm static int
7122b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
7132b120974SPeter Wemm {
7149d4fe4b2SBrooks Davis 
7152b120974SPeter Wemm 	switch (type) {
7162b120974SPeter Wemm 	case MOD_LOAD:
7175cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
7185cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
7195cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
7205cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
721ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
722ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
723ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
724ea4ca115SAndrew Thompson 			return (ENOMEM);
7254faedfe8SSam Leffler 		VLAN_LOCK_INIT();
7269d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
727127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
72875ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
729e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
730e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
731e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
732e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
733e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
734ccf7ba97SMarko Zec #ifndef VIMAGE
73542a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
73642a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
737ccf7ba97SMarko Zec #endif
73825c0f7b3SYaroslav Tykhiy 		if (bootverbose)
73925c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
74025c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
74125c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
74225c0f7b3SYaroslav Tykhiy #else
74325c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
74425c0f7b3SYaroslav Tykhiy #endif
74525c0f7b3SYaroslav Tykhiy 
74625c0f7b3SYaroslav Tykhiy 			       "\n");
7472b120974SPeter Wemm 		break;
7482b120974SPeter Wemm 	case MOD_UNLOAD:
749ccf7ba97SMarko Zec #ifndef VIMAGE
75042a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
751ccf7ba97SMarko Zec #endif
7525cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
753ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
7549d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
755127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
75675ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
757e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
758e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
75909fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
76009fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
761e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
7624faedfe8SSam Leffler 		VLAN_LOCK_DESTROY();
76325c0f7b3SYaroslav Tykhiy 		if (bootverbose)
76425c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
7659d4fe4b2SBrooks Davis 		break;
7663e019deaSPoul-Henning Kamp 	default:
7673e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
7682b120974SPeter Wemm 	}
76915a66c21SBruce M Simpson 	return (0);
7702b120974SPeter Wemm }
7712b120974SPeter Wemm 
7722b120974SPeter Wemm static moduledata_t vlan_mod = {
7732b120974SPeter Wemm 	"if_vlan",
7742b120974SPeter Wemm 	vlan_modevent,
7759823d527SKevin Lo 	0
7762b120974SPeter Wemm };
7772b120974SPeter Wemm 
7782b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
77911edc477SEd Maste MODULE_VERSION(if_vlan, 3);
7802cc2df49SGarrett Wollman 
781ccf7ba97SMarko Zec #ifdef VIMAGE
782ccf7ba97SMarko Zec static void
783ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
784ccf7ba97SMarko Zec {
785ccf7ba97SMarko Zec 
78642a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
78742a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
788ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
789ccf7ba97SMarko Zec }
790ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
791ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
792ccf7ba97SMarko Zec 
793ccf7ba97SMarko Zec static void
794ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
795ccf7ba97SMarko Zec {
796ccf7ba97SMarko Zec 
79742a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
798ccf7ba97SMarko Zec }
799ccf7ba97SMarko Zec VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
800ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
801ccf7ba97SMarko Zec #endif
802ccf7ba97SMarko Zec 
803*f941c31aSGleb Smirnoff /*
804*f941c31aSGleb Smirnoff  * Check for <etherif>.<vlan> style interface names.
805*f941c31aSGleb Smirnoff  */
806f889d2efSBrooks Davis static struct ifnet *
807*f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp)
8089d4fe4b2SBrooks Davis {
809*f941c31aSGleb Smirnoff 	char ifname[IFNAMSIZ];
810*f941c31aSGleb Smirnoff 	char *cp;
811f889d2efSBrooks Davis 	struct ifnet *ifp;
8127983103aSRobert Watson 	int vid;
813f889d2efSBrooks Davis 
814*f941c31aSGleb Smirnoff 	strlcpy(ifname, name, IFNAMSIZ);
815*f941c31aSGleb Smirnoff 	if ((cp = strchr(ifname, '.')) == NULL)
816*f941c31aSGleb Smirnoff 		return (NULL);
817*f941c31aSGleb Smirnoff 	*cp = '\0';
818*f941c31aSGleb Smirnoff 	if ((ifp = ifunit(ifname)) == NULL)
819*f941c31aSGleb Smirnoff 		return (NULL);
820e4cd31ddSJeff Roberson 	/*
821e4cd31ddSJeff Roberson 	 * We can handle non-ethernet hardware types as long as
822e4cd31ddSJeff Roberson 	 * they handle the tagging and headers themselves.
823e4cd31ddSJeff Roberson 	 */
824e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_ETHER &&
825e4cd31ddSJeff Roberson 	    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
826*f941c31aSGleb Smirnoff 		return (NULL);
827*f941c31aSGleb Smirnoff 
828*f941c31aSGleb Smirnoff 	/* Parse VID. */
829*f941c31aSGleb Smirnoff 	if (*++cp == '\0')
830*f941c31aSGleb Smirnoff 		return (NULL);
8317983103aSRobert Watson 	vid = 0;
832fb92ad4aSJohn Baldwin 	for(; *cp >= '0' && *cp <= '9'; cp++)
8337983103aSRobert Watson 		vid = (vid * 10) + (*cp - '0');
834fb92ad4aSJohn Baldwin 	if (*cp != '\0')
835*f941c31aSGleb Smirnoff 		return (NULL);
8367983103aSRobert Watson 	if (vidp != NULL)
8377983103aSRobert Watson 		*vidp = vid;
838f889d2efSBrooks Davis 
83915a66c21SBruce M Simpson 	return (ifp);
840f889d2efSBrooks Davis }
841f889d2efSBrooks Davis 
842f889d2efSBrooks Davis static int
843f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
844f889d2efSBrooks Davis {
845f889d2efSBrooks Davis 	const char *cp;
846f889d2efSBrooks Davis 
847*f941c31aSGleb Smirnoff 	if (vlan_clone_match_ethervid(name, NULL) != NULL)
848f889d2efSBrooks Davis 		return (1);
849f889d2efSBrooks Davis 
85042a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
851f889d2efSBrooks Davis 		return (0);
852f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
853f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
854f889d2efSBrooks Davis 			return (0);
855f889d2efSBrooks Davis 	}
856f889d2efSBrooks Davis 
857f889d2efSBrooks Davis 	return (1);
858f889d2efSBrooks Davis }
859f889d2efSBrooks Davis 
860f889d2efSBrooks Davis static int
8616b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
862f889d2efSBrooks Davis {
863f889d2efSBrooks Davis 	char *dp;
864f889d2efSBrooks Davis 	int wildcard;
865f889d2efSBrooks Davis 	int unit;
866f889d2efSBrooks Davis 	int error;
8677983103aSRobert Watson 	int vid;
868f889d2efSBrooks Davis 	int ethertag;
8699d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
8709d4fe4b2SBrooks Davis 	struct ifnet *ifp;
871f889d2efSBrooks Davis 	struct ifnet *p;
8723ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
8733ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
8746b7330e2SSam Leffler 	struct vlanreq vlr;
87565239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
876f889d2efSBrooks Davis 
8776b7330e2SSam Leffler 	/*
8786b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
8796b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
8806b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
8816b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
8826b7330e2SSam Leffler 	 *   must be configured separately.
8836b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
8846b7330e2SSam Leffler 	 * supported for backwards compatibilty.
8857983103aSRobert Watson 	 *
8867983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
8877983103aSRobert Watson 	 * called for.
8886b7330e2SSam Leffler 	 */
8896b7330e2SSam Leffler 	if (params) {
8906b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
8916b7330e2SSam Leffler 		if (error)
8926b7330e2SSam Leffler 			return error;
8936b7330e2SSam Leffler 		p = ifunit(vlr.vlr_parent);
8946b7330e2SSam Leffler 		if (p == NULL)
8956b7330e2SSam Leffler 			return ENXIO;
8966b7330e2SSam Leffler 		/*
8977983103aSRobert Watson 		 * Don't let the caller set up a VLAN VID with
8986b7330e2SSam Leffler 		 * anything except VLID bits.
8996b7330e2SSam Leffler 		 */
9006b7330e2SSam Leffler 		if (vlr.vlr_tag & ~EVL_VLID_MASK)
9016b7330e2SSam Leffler 			return (EINVAL);
9026b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
9036b7330e2SSam Leffler 		if (error != 0)
9046b7330e2SSam Leffler 			return (error);
9056b7330e2SSam Leffler 
9066b7330e2SSam Leffler 		ethertag = 1;
9077983103aSRobert Watson 		vid = vlr.vlr_tag;
9086b7330e2SSam Leffler 		wildcard = (unit < 0);
909*f941c31aSGleb Smirnoff 	} else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
910f889d2efSBrooks Davis 		ethertag = 1;
911f889d2efSBrooks Davis 		unit = -1;
912f889d2efSBrooks Davis 		wildcard = 0;
913f889d2efSBrooks Davis 
914f889d2efSBrooks Davis 		/*
9157983103aSRobert Watson 		 * Don't let the caller set up a VLAN VID with
916f889d2efSBrooks Davis 		 * anything except VLID bits.
917f889d2efSBrooks Davis 		 */
9187983103aSRobert Watson 		if (vid & ~EVL_VLID_MASK)
919f889d2efSBrooks Davis 			return (EINVAL);
920f889d2efSBrooks Davis 	} else {
921f889d2efSBrooks Davis 		ethertag = 0;
922f889d2efSBrooks Davis 
923f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
924f889d2efSBrooks Davis 		if (error != 0)
925f889d2efSBrooks Davis 			return (error);
926f889d2efSBrooks Davis 
927f889d2efSBrooks Davis 		wildcard = (unit < 0);
928f889d2efSBrooks Davis 	}
929f889d2efSBrooks Davis 
930f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
931f889d2efSBrooks Davis 	if (error != 0)
932f889d2efSBrooks Davis 		return (error);
933f889d2efSBrooks Davis 
934f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
935f889d2efSBrooks Davis 	if (wildcard) {
936f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
937f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
938f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
939f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
940f889d2efSBrooks Davis 		}
941f889d2efSBrooks Davis 	}
9429d4fe4b2SBrooks Davis 
943a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
944fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
945fc74a9f9SBrooks Davis 	if (ifp == NULL) {
946fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
947fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
948fc74a9f9SBrooks Davis 		return (ENOSPC);
949fc74a9f9SBrooks Davis 	}
9509d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
9519d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
952f889d2efSBrooks Davis 	/*
953cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
954f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
955f889d2efSBrooks Davis 	 */
956f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
95742a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
958f889d2efSBrooks Davis 	ifp->if_dunit = unit;
9599d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
9609d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
96115a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
9629d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
9639d4fe4b2SBrooks Davis 
964114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
965d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
966d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
9679d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
96864a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
969fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
9709d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
971211f625aSBill Fenner 	ifp->if_baudrate = 0;
972a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
973a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
9743ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
9753ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
9763ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
9779d4fe4b2SBrooks Davis 
978f889d2efSBrooks Davis 	if (ethertag) {
9797983103aSRobert Watson 		error = vlan_config(ifv, p, vid);
980f889d2efSBrooks Davis 		if (error != 0) {
981f889d2efSBrooks Davis 			/*
98228cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
983f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
984f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
985f889d2efSBrooks Davis 			 */
986f889d2efSBrooks Davis 			ether_ifdetach(ifp);
987249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
9884b22573aSBrooks Davis 			if_free(ifp);
98996c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
990f889d2efSBrooks Davis 			free(ifv, M_VLAN);
991f889d2efSBrooks Davis 
992f889d2efSBrooks Davis 			return (error);
993f889d2efSBrooks Davis 		}
994f889d2efSBrooks Davis 
9951cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
9961cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
997f889d2efSBrooks Davis 	}
998f889d2efSBrooks Davis 
9999d4fe4b2SBrooks Davis 	return (0);
10009d4fe4b2SBrooks Davis }
10019d4fe4b2SBrooks Davis 
1002f889d2efSBrooks Davis static int
1003f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
10049d4fe4b2SBrooks Davis {
10059d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1006114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
1007b4e9f837SBrooks Davis 
1008249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1009249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
10104b22573aSBrooks Davis 	if_free(ifp);
10119d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1012b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
1013b4e9f837SBrooks Davis 
1014f889d2efSBrooks Davis 	return (0);
10159d4fe4b2SBrooks Davis }
10169d4fe4b2SBrooks Davis 
101715a66c21SBruce M Simpson /*
101815a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
101915a66c21SBruce M Simpson  */
10202cc2df49SGarrett Wollman static void
1021114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
10222cc2df49SGarrett Wollman {
10232cc2df49SGarrett Wollman }
10242cc2df49SGarrett Wollman 
10256d3a3ab7SGleb Smirnoff /*
1026d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
10276d3a3ab7SGleb Smirnoff  */
1028d9b1d615SJohn Baldwin static int
1029d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
10302cc2df49SGarrett Wollman {
10312cc2df49SGarrett Wollman 	struct ifvlan *ifv;
10322cc2df49SGarrett Wollman 	struct ifnet *p;
10331ad7a257SPyun YongHyeon 	int error, len, mcast;
10342cc2df49SGarrett Wollman 
10352cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
103675ee267cSGleb Smirnoff 	p = PARENT(ifv);
10371ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
10381ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
10392cc2df49SGarrett Wollman 
1040a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
10412cc2df49SGarrett Wollman 
1042f731f104SBill Paul 	/*
1043d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
104424993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
104524993214SYaroslav Tykhiy 	 */
10462dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
104724993214SYaroslav Tykhiy 		m_freem(m);
1048a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
10498b20f6cfSHiroki Sato 		return (ENETDOWN);
105024993214SYaroslav Tykhiy 	}
105124993214SYaroslav Tykhiy 
105224993214SYaroslav Tykhiy 	/*
1053f6e5e0adSYaroslav Tykhiy 	 * Pad the frame to the minimum size allowed if told to.
1054f6e5e0adSYaroslav Tykhiy 	 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
1055f6e5e0adSYaroslav Tykhiy 	 * paragraph C.4.4.3.b.  It can help to work around buggy
1056f6e5e0adSYaroslav Tykhiy 	 * bridges that violate paragraph C.4.4.3.a from the same
1057f6e5e0adSYaroslav Tykhiy 	 * document, i.e., fail to pad short frames after untagging.
1058f6e5e0adSYaroslav Tykhiy 	 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
1059f6e5e0adSYaroslav Tykhiy 	 * untagging it will produce a 62-byte frame, which is a runt
1060f6e5e0adSYaroslav Tykhiy 	 * and requires padding.  There are VLAN-enabled network
1061f6e5e0adSYaroslav Tykhiy 	 * devices that just discard such runts instead or mishandle
1062f6e5e0adSYaroslav Tykhiy 	 * them somehow.
1063f6e5e0adSYaroslav Tykhiy 	 */
1064478e0520SHiroki Sato 	if (V_soft_pad && p->if_type == IFT_ETHER) {
1065f6e5e0adSYaroslav Tykhiy 		static char pad[8];	/* just zeros */
1066f6e5e0adSYaroslav Tykhiy 		int n;
1067f6e5e0adSYaroslav Tykhiy 
1068f6e5e0adSYaroslav Tykhiy 		for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
1069f6e5e0adSYaroslav Tykhiy 		     n > 0; n -= sizeof(pad))
1070f6e5e0adSYaroslav Tykhiy 			if (!m_append(m, min(n, sizeof(pad)), pad))
1071f6e5e0adSYaroslav Tykhiy 				break;
1072f6e5e0adSYaroslav Tykhiy 
1073f6e5e0adSYaroslav Tykhiy 		if (n > 0) {
1074f6e5e0adSYaroslav Tykhiy 			if_printf(ifp, "cannot pad short frame\n");
1075a58ea6b1SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1076f6e5e0adSYaroslav Tykhiy 			m_freem(m);
1077d9b1d615SJohn Baldwin 			return (0);
1078f6e5e0adSYaroslav Tykhiy 		}
1079f6e5e0adSYaroslav Tykhiy 	}
1080f6e5e0adSYaroslav Tykhiy 
1081f6e5e0adSYaroslav Tykhiy 	/*
1082a3814acfSSam Leffler 	 * If underlying interface can do VLAN tag insertion itself,
1083a3814acfSSam Leffler 	 * just pass the packet along. However, we need some way to
1084a3814acfSSam Leffler 	 * tell the interface where the packet came from so that it
1085a3814acfSSam Leffler 	 * knows how to find the VLAN tag to use, so we attach a
1086a3814acfSSam Leffler 	 * packet tag that holds it.
1087f731f104SBill Paul 	 */
1088b08347a0SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
10897983103aSRobert Watson 		m->m_pkthdr.ether_vtag = ifv->ifv_vid;
1090ba26134bSGleb Smirnoff 		m->m_flags |= M_VLANTAG;
1091f731f104SBill Paul 	} else {
10927983103aSRobert Watson 		m = ether_vlanencap(m, ifv->ifv_vid);
10934af90a4dSMatthew N. Dodd 		if (m == NULL) {
1094d9b1d615SJohn Baldwin 			if_printf(ifp, "unable to prepend VLAN header\n");
1095a58ea6b1SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1096d9b1d615SJohn Baldwin 			return (0);
10974af90a4dSMatthew N. Dodd 		}
1098f731f104SBill Paul 	}
10992cc2df49SGarrett Wollman 
11002cc2df49SGarrett Wollman 	/*
11012cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
11022cc2df49SGarrett Wollman 	 */
1103aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1104299153b5SAlexander V. Chernikov 	if (error == 0) {
1105a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1106a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1107a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
11081ad7a257SPyun YongHyeon 	} else
1109a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1110d9b1d615SJohn Baldwin 	return (error);
11112cc2df49SGarrett Wollman }
1112d9b1d615SJohn Baldwin 
1113d9b1d615SJohn Baldwin /*
1114d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1115d9b1d615SJohn Baldwin  */
1116d9b1d615SJohn Baldwin static void
1117d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1118d9b1d615SJohn Baldwin {
1119f731f104SBill Paul }
1120f731f104SBill Paul 
1121a3814acfSSam Leffler static void
1122a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1123f731f104SBill Paul {
112475ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1125f731f104SBill Paul 	struct ifvlan *ifv;
1126772b000fSAlexander V. Chernikov 	TRUNK_LOCK_READER;
11277983103aSRobert Watson 	uint16_t vid;
112875ee267cSGleb Smirnoff 
112975ee267cSGleb Smirnoff 	KASSERT(trunk != NULL, ("%s: no trunk", __func__));
1130a3814acfSSam Leffler 
1131f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1132a3814acfSSam Leffler 		/*
113314e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1134a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1135a3814acfSSam Leffler 		 */
11367983103aSRobert Watson 		vid = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
11376ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1138a3814acfSSam Leffler 	} else {
113975ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
114075ee267cSGleb Smirnoff 
114114e98256SYaroslav Tykhiy 		/*
114214e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
114314e98256SYaroslav Tykhiy 		 */
1144a3814acfSSam Leffler 		switch (ifp->if_type) {
1145a3814acfSSam Leffler 		case IFT_ETHER:
1146a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1147a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1148a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1149a3814acfSSam Leffler 				return;
1150a3814acfSSam Leffler 			}
1151a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
11527983103aSRobert Watson 			vid = EVL_VLANOFTAG(ntohs(evl->evl_tag));
1153db8b5973SYaroslav Tykhiy 
1154db8b5973SYaroslav Tykhiy 			/*
11552dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
11562dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
11572dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
11582dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1159db8b5973SYaroslav Tykhiy 			 */
11602dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
11612dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
11622dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1163a3814acfSSam Leffler 			break;
11642dc879b3SYaroslav Tykhiy 
1165a3814acfSSam Leffler 		default:
1166db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
116760c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
116860c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1169a3814acfSSam Leffler #endif
117060c60618SYaroslav Tykhiy 			m_freem(m);
11713751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
117260c60618SYaroslav Tykhiy 			return;
1173a3814acfSSam Leffler 		}
11747a46ec8fSBrooks Davis 	}
11757a46ec8fSBrooks Davis 
117615ed2fa1SYaroslav Tykhiy 	TRUNK_RLOCK(trunk);
11777983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
11782dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
117975ee267cSGleb Smirnoff 		TRUNK_RUNLOCK(trunk);
118075ee267cSGleb Smirnoff 		m_freem(m);
11813751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
118275ee267cSGleb Smirnoff 		return;
118375ee267cSGleb Smirnoff 	}
118475ee267cSGleb Smirnoff 	TRUNK_RUNLOCK(trunk);
1185f731f104SBill Paul 
1186fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1187a58ea6b1SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1188a58ea6b1SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
11892cc2df49SGarrett Wollman 
1190a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1191fc74a9f9SBrooks Davis 	(*ifp->if_input)(ifv->ifv_ifp, m);
11922cc2df49SGarrett Wollman }
11932cc2df49SGarrett Wollman 
11942cc2df49SGarrett Wollman static int
11957983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
11962cc2df49SGarrett Wollman {
119775ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
11981cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
119975ee267cSGleb Smirnoff 	int error = 0;
12002cc2df49SGarrett Wollman 
120175ee267cSGleb Smirnoff 	/* VID numbers 0x0 and 0xFFF are reserved */
12027983103aSRobert Watson 	if (vid == 0 || vid == 0xFFF)
120375ee267cSGleb Smirnoff 		return (EINVAL);
1204e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1205e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
120615a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
120764a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
120864a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
120975ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
121015a66c21SBruce M Simpson 		return (EBUSY);
12112cc2df49SGarrett Wollman 
121275ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
121375ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
121475ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
121575ee267cSGleb Smirnoff 		vlan_inithash(trunk);
121605a2398fSGleb Smirnoff 		VLAN_LOCK();
121705a2398fSGleb Smirnoff 		if (p->if_vlantrunk != NULL) {
121805a2398fSGleb Smirnoff 			/* A race that that is very unlikely to be hit. */
121905a2398fSGleb Smirnoff 			vlan_freehash(trunk);
122005a2398fSGleb Smirnoff 			free(trunk, M_VLAN);
122105a2398fSGleb Smirnoff 			goto exists;
122205a2398fSGleb Smirnoff 		}
122375ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
122475ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
122575ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
122675ee267cSGleb Smirnoff 		trunk->parent = p;
122775ee267cSGleb Smirnoff 	} else {
122875ee267cSGleb Smirnoff 		VLAN_LOCK();
122975ee267cSGleb Smirnoff exists:
123075ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
123175ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
123275ee267cSGleb Smirnoff 	}
123375ee267cSGleb Smirnoff 
12347983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
123575ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
123675ee267cSGleb Smirnoff 	if (error)
123775ee267cSGleb Smirnoff 		goto done;
123873f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1239a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1240a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
12411cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1242a3814acfSSam Leffler 
1243a3814acfSSam Leffler 	/*
1244a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1245a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1246656acce4SYaroslav Tykhiy 	 * use it.
1247a3814acfSSam Leffler 	 */
1248656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1249656acce4SYaroslav Tykhiy 		/*
1250656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1251656acce4SYaroslav Tykhiy 		 * handle extended frames.
1252656acce4SYaroslav Tykhiy 		 */
1253a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1254656acce4SYaroslav Tykhiy 	} else {
1255a3814acfSSam Leffler 		/*
1256a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1257a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1258a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1259a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1260a3814acfSSam Leffler 		 * which might still be useful.
1261a3814acfSSam Leffler 		 */
1262a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1263a3814acfSSam Leffler 	}
1264a3814acfSSam Leffler 
126575ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
12661cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1267e4cd31ddSJeff Roberson 	/*
1268e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1269e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1270e4cd31ddSJeff Roberson 	 * interfaces to also work.
1271e4cd31ddSJeff Roberson 	 */
12721cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
127375ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1274e4cd31ddSJeff Roberson 	ifp->if_output = p->if_output;
1275e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1276e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1277e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1278e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
1279e4cd31ddSJeff Roberson 
12802cc2df49SGarrett Wollman 	/*
128124993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
128224993214SYaroslav Tykhiy 	 * Other flags are none of our business.
12832cc2df49SGarrett Wollman 	 */
128464a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
12851cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
12861cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
12871cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
12881cf236fbSYaroslav Tykhiy 
12891cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
12902cc2df49SGarrett Wollman 
129175ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1292a3814acfSSam Leffler 
1293a3814acfSSam Leffler 	/*
1294e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
12952cc2df49SGarrett Wollman 	 * physical interface's.
12962cc2df49SGarrett Wollman 	 */
1297e4cd31ddSJeff Roberson 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1298e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1299e4cd31ddSJeff Roberson 	    p->if_addrlen;
13001b2a4f7aSBill Fenner 
13011b2a4f7aSBill Fenner 	/*
13021b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
13031b2a4f7aSBill Fenner 	 * joined on the vlan device.
13041b2a4f7aSBill Fenner 	 */
13051cf236fbSYaroslav Tykhiy 	(void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
13062ada9747SYaroslav Tykhiy 
13072ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
13082ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
130975ee267cSGleb Smirnoff done:
131075ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
1311c725524cSJack F Vogel 	if (error == 0)
13127983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
131375ee267cSGleb Smirnoff 	VLAN_UNLOCK();
131475ee267cSGleb Smirnoff 
131575ee267cSGleb Smirnoff 	return (error);
13162cc2df49SGarrett Wollman }
13172cc2df49SGarrett Wollman 
13186f359e28SJohn Baldwin static void
1319f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1320f731f104SBill Paul {
13215cb8c31aSYaroslav Tykhiy 
13225cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
132328cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
13245cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
13255cb8c31aSYaroslav Tykhiy }
13265cb8c31aSYaroslav Tykhiy 
13276f359e28SJohn Baldwin static void
132828cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
13295cb8c31aSYaroslav Tykhiy {
133075ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1331f731f104SBill Paul 	struct vlan_mc_entry *mc;
1332f731f104SBill Paul 	struct ifvlan *ifv;
1333c725524cSJack F Vogel 	struct ifnet  *parent;
133428cc4d37SJohn Baldwin 	int error;
1335f731f104SBill Paul 
13365cb8c31aSYaroslav Tykhiy 	VLAN_LOCK_ASSERT();
13374faedfe8SSam Leffler 
1338f731f104SBill Paul 	ifv = ifp->if_softc;
133975ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
134022893351SJack F Vogel 	parent = NULL;
1341f731f104SBill Paul 
134222893351SJack F Vogel 	if (trunk != NULL) {
134375ee267cSGleb Smirnoff 
134475ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
134522893351SJack F Vogel 		parent = trunk->parent;
13461b2a4f7aSBill Fenner 
1347f731f104SBill Paul 		/*
1348f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1349f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
13501b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1351f731f104SBill Paul 		 */
1352c0cb022bSYaroslav Tykhiy 		while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
13536f359e28SJohn Baldwin 			/*
135428cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1355b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
135628cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
135728cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
135828cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
135928cc4d37SJohn Baldwin 			 * succeed.
13606f359e28SJohn Baldwin 			 */
136128cc4d37SJohn Baldwin 			if (!departing) {
136228cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1363e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
136428cc4d37SJohn Baldwin 				if (error)
136528cc4d37SJohn Baldwin 					if_printf(ifp,
136628cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
136728cc4d37SJohn Baldwin 					    error);
136828cc4d37SJohn Baldwin 			}
1369f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
13709d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
1371f731f104SBill Paul 		}
1372a3814acfSSam Leffler 
13731cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
137475ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
137575ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
137675ee267cSGleb Smirnoff 
137775ee267cSGleb Smirnoff 		/*
137875ee267cSGleb Smirnoff 		 * Check if we were the last.
137975ee267cSGleb Smirnoff 		 */
138075ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
13812d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
138275ee267cSGleb Smirnoff 			/*
138375ee267cSGleb Smirnoff 			 * XXXGL: If some ithread has already entered
138475ee267cSGleb Smirnoff 			 * vlan_input() and is now blocked on the trunk
138575ee267cSGleb Smirnoff 			 * lock, then it should preempt us right after
138675ee267cSGleb Smirnoff 			 * unlock and finish its work. Then we will acquire
138775ee267cSGleb Smirnoff 			 * lock again in trunk_destroy().
138875ee267cSGleb Smirnoff 			 */
138975ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
139075ee267cSGleb Smirnoff 			trunk_destroy(trunk);
139175ee267cSGleb Smirnoff 		} else
139275ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
13931b2a4f7aSBill Fenner 	}
1394f731f104SBill Paul 
1395f731f104SBill Paul 	/* Disconnect from parent. */
13961cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
13971cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
13985cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
13995cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
14005cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1401f731f104SBill Paul 
140222893351SJack F Vogel 	/*
140322893351SJack F Vogel 	 * Only dispatch an event if vlan was
140422893351SJack F Vogel 	 * attached, otherwise there is nothing
140522893351SJack F Vogel 	 * to cleanup anyway.
140622893351SJack F Vogel 	 */
140722893351SJack F Vogel 	if (parent != NULL)
14087983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1409f731f104SBill Paul }
1410f731f104SBill Paul 
14111cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1412f731f104SBill Paul static int
14131cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
14141cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1415a3814acfSSam Leffler {
14161cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
14171cf236fbSYaroslav Tykhiy 	int error;
1418a3814acfSSam Leffler 
14191cf236fbSYaroslav Tykhiy 	/* XXX VLAN_LOCK_ASSERT(); */
1420a3814acfSSam Leffler 
14211cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
14221cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
14231cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
14241cf236fbSYaroslav Tykhiy 
14251cf236fbSYaroslav Tykhiy 	/*
14261cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
14271cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
14281cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
14291cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
14301cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
14311cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
14321cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
14331cf236fbSYaroslav Tykhiy 	 */
14341cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
143575ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
14361cf236fbSYaroslav Tykhiy 		if (error)
1437a3814acfSSam Leffler 			return (error);
14381cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
14391cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
14401cf236fbSYaroslav Tykhiy 	}
14411cf236fbSYaroslav Tykhiy 	return (0);
14421cf236fbSYaroslav Tykhiy }
14431cf236fbSYaroslav Tykhiy 
14441cf236fbSYaroslav Tykhiy /*
14451cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
14461cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
14471cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
14481cf236fbSYaroslav Tykhiy  */
14491cf236fbSYaroslav Tykhiy static int
14501cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
14511cf236fbSYaroslav Tykhiy {
14521cf236fbSYaroslav Tykhiy 	int error, i;
14531cf236fbSYaroslav Tykhiy 
14541cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
14551cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
14561cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
14571cf236fbSYaroslav Tykhiy 		if (error)
14581cf236fbSYaroslav Tykhiy 			return (error);
14591cf236fbSYaroslav Tykhiy 	}
14601cf236fbSYaroslav Tykhiy 	return (0);
1461a3814acfSSam Leffler }
1462a3814acfSSam Leffler 
1463127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1464127d7b2dSAndre Oppermann static void
1465a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1466127d7b2dSAndre Oppermann {
146775ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1468127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
146975ee267cSGleb Smirnoff 	int i;
1470127d7b2dSAndre Oppermann 
147175ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
147275ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
147315ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
147475ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
147575ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
147675ee267cSGleb Smirnoff #else
1477aad0be7aSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
1478aad0be7aSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
147975ee267cSGleb Smirnoff #endif
1480aad0be7aSGleb Smirnoff 			ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1481fc74a9f9SBrooks Davis 			if_link_state_change(ifv->ifv_ifp,
148275ee267cSGleb Smirnoff 			    trunk->parent->if_link_state);
1483127d7b2dSAndre Oppermann 		}
148475ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
148575ee267cSGleb Smirnoff }
148675ee267cSGleb Smirnoff 
148775ee267cSGleb Smirnoff static void
148875ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
148975ee267cSGleb Smirnoff {
149075ee267cSGleb Smirnoff 	struct ifnet *p = PARENT(ifv);
149175ee267cSGleb Smirnoff 	struct ifnet *ifp = ifv->ifv_ifp;
14929fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
149375ee267cSGleb Smirnoff 
149475ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(TRUNK(ifv));
149575ee267cSGleb Smirnoff 
149675ee267cSGleb Smirnoff 	/*
149775ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
149875ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
149975ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
150075ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
150175ee267cSGleb Smirnoff 	 */
150275ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
150375ee267cSGleb Smirnoff 		ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
150475ee267cSGleb Smirnoff 
150575ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
150675ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
150775ee267cSGleb Smirnoff 		ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
15089b76d9cbSPyun YongHyeon 		ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
1509bf7dcda3SGleb Smirnoff 		    CSUM_UDP | CSUM_SCTP);
151075ee267cSGleb Smirnoff 	} else {
151175ee267cSGleb Smirnoff 		ifp->if_capenable = 0;
151275ee267cSGleb Smirnoff 		ifp->if_hwassist = 0;
151375ee267cSGleb Smirnoff 	}
15149b76d9cbSPyun YongHyeon 	/*
15159b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
15169b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
15179b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
15189b76d9cbSPyun YongHyeon 	 */
15199fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
15209fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
15219fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
15229b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
15239b76d9cbSPyun YongHyeon 		ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
15249b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
15259b76d9cbSPyun YongHyeon 		ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
15269b76d9cbSPyun YongHyeon 		ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
15279b76d9cbSPyun YongHyeon 	} else {
15289b76d9cbSPyun YongHyeon 		ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
15299b76d9cbSPyun YongHyeon 		ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
15309b76d9cbSPyun YongHyeon 	}
153109fe6320SNavdeep Parhar 
153209fe6320SNavdeep Parhar 	/*
153309fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
153409fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
153509fe6320SNavdeep Parhar 	 *
153609fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
153709fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
153809fe6320SNavdeep Parhar 	 * with its own bit.
153909fe6320SNavdeep Parhar 	 */
154009fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
154109fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
154209fe6320SNavdeep Parhar 		ifp->if_capabilities |= p->if_capabilities & IFCAP_TOE;
154309fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
154409fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
154509fe6320SNavdeep Parhar 		ifp->if_capenable |= p->if_capenable & IFCAP_TOE;
154609fe6320SNavdeep Parhar 	}
154775ee267cSGleb Smirnoff }
154875ee267cSGleb Smirnoff 
154975ee267cSGleb Smirnoff static void
155075ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
155175ee267cSGleb Smirnoff {
155275ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
155375ee267cSGleb Smirnoff 	struct ifvlan *ifv;
155475ee267cSGleb Smirnoff 	int i;
155575ee267cSGleb Smirnoff 
155675ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
155775ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
155815ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
155975ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
156075ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
156175ee267cSGleb Smirnoff #else
156275ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
156375ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
156475ee267cSGleb Smirnoff #endif
156575ee267cSGleb Smirnoff 			vlan_capabilities(ifv);
156675ee267cSGleb Smirnoff 	}
156775ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
1568127d7b2dSAndre Oppermann }
1569127d7b2dSAndre Oppermann 
1570a3814acfSSam Leffler static int
1571cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
15722cc2df49SGarrett Wollman {
15732cc2df49SGarrett Wollman 	struct ifnet *p;
15742cc2df49SGarrett Wollman 	struct ifreq *ifr;
1575e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
15762cc2df49SGarrett Wollman 	struct ifvlan *ifv;
15772d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
15782cc2df49SGarrett Wollman 	struct vlanreq vlr;
15792cc2df49SGarrett Wollman 	int error = 0;
15802cc2df49SGarrett Wollman 
15812cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1582e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
15832cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
15842cc2df49SGarrett Wollman 
15852cc2df49SGarrett Wollman 	switch (cmd) {
1586e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1587e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1588e4cd31ddSJeff Roberson #ifdef INET
1589e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1590e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1591e4cd31ddSJeff Roberson #endif
1592e4cd31ddSJeff Roberson 		break;
1593e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
1594e4cd31ddSJeff Roberson                 {
1595e4cd31ddSJeff Roberson 			struct sockaddr *sa;
1596e4cd31ddSJeff Roberson 
1597e4cd31ddSJeff Roberson 			sa = (struct sockaddr *)&ifr->ifr_data;
1598e4cd31ddSJeff Roberson 			bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen);
1599e4cd31ddSJeff Roberson                 }
1600e4cd31ddSJeff Roberson 		break;
1601b3cca108SBill Fenner 	case SIOCGIFMEDIA:
16024faedfe8SSam Leffler 		VLAN_LOCK();
160375ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1604d8564efdSEd Maste 			p = PARENT(ifv);
16054faedfe8SSam Leffler 			VLAN_UNLOCK();
1606d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1607b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1608b3cca108SBill Fenner 			if (error == 0) {
1609b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1610b3cca108SBill Fenner 
1611b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1612b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1613b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1614b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1615b3cca108SBill Fenner 						ifmr->ifm_ulist,
1616b3cca108SBill Fenner 						sizeof(int));
1617b3cca108SBill Fenner 				}
1618b3cca108SBill Fenner 			}
16194faedfe8SSam Leffler 		} else {
16204faedfe8SSam Leffler 			VLAN_UNLOCK();
1621b3cca108SBill Fenner 			error = EINVAL;
16224faedfe8SSam Leffler 		}
1623b3cca108SBill Fenner 		break;
1624b3cca108SBill Fenner 
1625b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1626b3cca108SBill Fenner 		error = EINVAL;
1627b3cca108SBill Fenner 		break;
1628b3cca108SBill Fenner 
16292cc2df49SGarrett Wollman 	case SIOCSIFMTU:
16302cc2df49SGarrett Wollman 		/*
16312cc2df49SGarrett Wollman 		 * Set the interface MTU.
16322cc2df49SGarrett Wollman 		 */
16334faedfe8SSam Leffler 		VLAN_LOCK();
163475ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1635a3814acfSSam Leffler 			if (ifr->ifr_mtu >
163675ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1637a3814acfSSam Leffler 			    ifr->ifr_mtu <
1638a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
16392cc2df49SGarrett Wollman 				error = EINVAL;
1640a3814acfSSam Leffler 			else
16412cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1642a3814acfSSam Leffler 		} else
1643a3814acfSSam Leffler 			error = EINVAL;
16444faedfe8SSam Leffler 		VLAN_UNLOCK();
16452cc2df49SGarrett Wollman 		break;
16462cc2df49SGarrett Wollman 
16472cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1648ccf7ba97SMarko Zec #ifdef VIMAGE
164915f6780eSRobert Watson 		/*
165015f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
165115f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
165215f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
165315f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
16545a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
165515f6780eSRobert Watson 		 */
1656ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1657ccf7ba97SMarko Zec 			error = EPERM;
1658ccf7ba97SMarko Zec 			break;
1659ccf7ba97SMarko Zec 		}
1660ccf7ba97SMarko Zec #endif
166115a66c21SBruce M Simpson 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
16622cc2df49SGarrett Wollman 		if (error)
16632cc2df49SGarrett Wollman 			break;
16642cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1665f731f104SBill Paul 			vlan_unconfig(ifp);
16662cc2df49SGarrett Wollman 			break;
16672cc2df49SGarrett Wollman 		}
16682cc2df49SGarrett Wollman 		p = ifunit(vlr.vlr_parent);
16691bdc73d3SEd Maste 		if (p == NULL) {
16702cc2df49SGarrett Wollman 			error = ENOENT;
16712cc2df49SGarrett Wollman 			break;
16722cc2df49SGarrett Wollman 		}
1673fb88a3e0SBill Paul 		/*
16747983103aSRobert Watson 		 * Don't let the caller set up a VLAN VID with
1675fb88a3e0SBill Paul 		 * anything except VLID bits.
1676fb88a3e0SBill Paul 		 */
1677fb88a3e0SBill Paul 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1678fb88a3e0SBill Paul 			error = EINVAL;
1679fb88a3e0SBill Paul 			break;
1680fb88a3e0SBill Paul 		}
168175ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
168275ee267cSGleb Smirnoff 		if (error)
16832cc2df49SGarrett Wollman 			break;
1684a3814acfSSam Leffler 
16851cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
16861cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
16872cc2df49SGarrett Wollman 		break;
16882cc2df49SGarrett Wollman 
16892cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1690ccf7ba97SMarko Zec #ifdef VIMAGE
1691ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1692ccf7ba97SMarko Zec 			error = EPERM;
1693ccf7ba97SMarko Zec 			break;
1694ccf7ba97SMarko Zec 		}
1695ccf7ba97SMarko Zec #endif
169615a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
16974faedfe8SSam Leffler 		VLAN_LOCK();
169875ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
169975ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
17009bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
17017983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
17022cc2df49SGarrett Wollman 		}
17034faedfe8SSam Leffler 		VLAN_UNLOCK();
170415a66c21SBruce M Simpson 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
17052cc2df49SGarrett Wollman 		break;
17062cc2df49SGarrett Wollman 
17072cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
17082cc2df49SGarrett Wollman 		/*
17091cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
17101cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
17112cc2df49SGarrett Wollman 		 */
171275ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
17131cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
17142cc2df49SGarrett Wollman 		break;
1715a3814acfSSam Leffler 
1716f731f104SBill Paul 	case SIOCADDMULTI:
1717f731f104SBill Paul 	case SIOCDELMULTI:
171875ee267cSGleb Smirnoff 		/*
171975ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
172075ee267cSGleb Smirnoff 		 * when we do.
172175ee267cSGleb Smirnoff 		 */
17222d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
17232d222cb7SAlexander Motin 		if (trunk != NULL) {
17242d222cb7SAlexander Motin 			TRUNK_LOCK(trunk);
1725f731f104SBill Paul 			error = vlan_setmulti(ifp);
17262d222cb7SAlexander Motin 			TRUNK_UNLOCK(trunk);
17272d222cb7SAlexander Motin 		}
1728f731f104SBill Paul 		break;
172975ee267cSGleb Smirnoff 
173008e57366SXin LI 	case SIOCSIFCAP:
173108e57366SXin LI 		VLAN_LOCK();
173208e57366SXin LI 		if (TRUNK(ifv) != NULL) {
173308e57366SXin LI 			p = PARENT(ifv);
173408e57366SXin LI 			VLAN_UNLOCK();
173508e57366SXin LI 			if ((p->if_type != IFT_ETHER) &&
173608e57366SXin LI 			    (ifr->ifr_reqcap & IFCAP_VLAN_HWTAGGING) == 0) {
173708e57366SXin LI 				error = EINVAL;
173808e57366SXin LI 				break;
173908e57366SXin LI 			}
174008e57366SXin LI 			error = (*p->if_ioctl)(p, cmd, data);
174108e57366SXin LI 			if (error)
174208e57366SXin LI 				break;
174308e57366SXin LI 			/* Propogate vlan interface capabilities */
174408e57366SXin LI 			vlan_trunk_capabilities(p);
174508e57366SXin LI 		} else {
174608e57366SXin LI 			VLAN_UNLOCK();
174708e57366SXin LI 			error = EINVAL;
174808e57366SXin LI 		}
174908e57366SXin LI 		break;
175008e57366SXin LI 
17512cc2df49SGarrett Wollman 	default:
1752e4cd31ddSJeff Roberson 		error = EINVAL;
1753e4cd31ddSJeff Roberson 		break;
17542cc2df49SGarrett Wollman 	}
175515a66c21SBruce M Simpson 
175615a66c21SBruce M Simpson 	return (error);
17572cc2df49SGarrett Wollman }
1758