xref: /freebsd/sys/net/if_vlan.c (revision 6667db3130b236ae6c2de05770faec29f9d7d7a6)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32cc2df49SGarrett Wollman  *
42cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
52cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
62cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
72cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
82cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
92cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
102cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
112cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
122cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
132cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
142cc2df49SGarrett Wollman  * warranty.
152cc2df49SGarrett Wollman  *
162cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
172cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
182cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
192cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
202cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
232cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
242cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
252cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
262cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272cc2df49SGarrett Wollman  * SUCH DAMAGE.
282cc2df49SGarrett Wollman  */
292cc2df49SGarrett Wollman 
302cc2df49SGarrett Wollman /*
312cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
322cc2df49SGarrett Wollman  * Might be extended some day to also handle IEEE 802.1p priority
332cc2df49SGarrett Wollman  * tagging.  This is sort of sneaky in the implementation, since
342cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
352cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
362cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
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;
107299153b5SAlexander V. Chernikov 	counter_u64_t	ifv_ipackets;
108299153b5SAlexander V. Chernikov 	counter_u64_t	ifv_ibytes;
109299153b5SAlexander V. Chernikov 	counter_u64_t	ifv_opackets;
110299153b5SAlexander V. Chernikov 	counter_u64_t	ifv_obytes;
111299153b5SAlexander V. Chernikov 	counter_u64_t	ifv_omcasts;
112*6667db31SAlexander V. Chernikov 	counter_u64_t	ifv_oerrors;
11375ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
11475ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
115*6667db31SAlexander V. Chernikov 	void	*ifv_cookie;
1161cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
117a3814acfSSam Leffler 	struct	ifv_linkmib {
118a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
119a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
120a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
12173f2233dSYaroslav Tykhiy 		uint16_t ifvm_proto;	/* encapsulation ethertype */
12275ee267cSGleb Smirnoff 		uint16_t ifvm_tag;	/* tag to apply on packets leaving if */
123a3814acfSSam Leffler 	}	ifv_mib;
124114c608cSYaroslav Tykhiy 	SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
125c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY
126a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
127c0cb022bSYaroslav Tykhiy #endif
128a3814acfSSam Leffler };
12973f2233dSYaroslav Tykhiy #define	ifv_proto	ifv_mib.ifvm_proto
1307983103aSRobert Watson #define	ifv_vid		ifv_mib.ifvm_tag
131a3814acfSSam Leffler #define	ifv_encaplen	ifv_mib.ifvm_encaplen
132a3814acfSSam Leffler #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
133a3814acfSSam Leffler #define	ifv_mintu	ifv_mib.ifvm_mintu
134a3814acfSSam Leffler 
13575ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
1361cf236fbSYaroslav Tykhiy static struct {
1371cf236fbSYaroslav Tykhiy 	int flag;
1381cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
1391cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
1401cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
1411cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
1421cf236fbSYaroslav Tykhiy 	{0, NULL}
1431cf236fbSYaroslav Tykhiy };
144a3814acfSSam Leffler 
1454a408dcbSBill Paul SYSCTL_DECL(_net_link);
1466472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0,
1476472ac3dSEd Schouten     "IEEE 802.1Q VLAN");
1486472ac3dSEd Schouten static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0,
1496472ac3dSEd Schouten     "for consistency");
1502cc2df49SGarrett Wollman 
151f6e5e0adSYaroslav Tykhiy static int soft_pad = 0;
152f6e5e0adSYaroslav Tykhiy SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
153f6e5e0adSYaroslav Tykhiy 	   "pad short frames before tagging");
154f6e5e0adSYaroslav Tykhiy 
15542a58907SGleb Smirnoff static const char vlanname[] = "vlan";
15642a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
1572cc2df49SGarrett Wollman 
1585cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
159ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag;
1605cb8c31aSYaroslav Tykhiy 
1614faedfe8SSam Leffler /*
16275ee267cSGleb Smirnoff  * We have a global mutex, that is used to serialize configuration
16375ee267cSGleb Smirnoff  * changes and isn't used in normal packet delivery.
16475ee267cSGleb Smirnoff  *
16575ee267cSGleb Smirnoff  * We also have a per-trunk rwlock, that is locked shared on packet
16675ee267cSGleb Smirnoff  * processing and exclusive when configuration is changed.
16775ee267cSGleb Smirnoff  *
16875ee267cSGleb Smirnoff  * The VLAN_ARRAY substitutes the dynamic hash with a static array
169ad387028SAndrew Thompson  * with 4096 entries. In theory this can give a boost in processing,
17075ee267cSGleb Smirnoff  * however on practice it does not. Probably this is because array
17175ee267cSGleb Smirnoff  * is too big to fit into CPU cache.
1724faedfe8SSam Leffler  */
173e4cd31ddSJeff Roberson static struct sx ifv_lock;
174e4cd31ddSJeff Roberson #define	VLAN_LOCK_INIT()	sx_init(&ifv_lock, "vlan_global")
175e4cd31ddSJeff Roberson #define	VLAN_LOCK_DESTROY()	sx_destroy(&ifv_lock)
176e4cd31ddSJeff Roberson #define	VLAN_LOCK_ASSERT()	sx_assert(&ifv_lock, SA_LOCKED)
177e4cd31ddSJeff Roberson #define	VLAN_LOCK()		sx_xlock(&ifv_lock)
178e4cd31ddSJeff Roberson #define	VLAN_UNLOCK()		sx_xunlock(&ifv_lock)
179772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_INIT(trunk)	rm_init(&(trunk)->lock, vlanname)
180772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_DESTROY(trunk) rm_destroy(&(trunk)->lock)
181772b000fSAlexander V. Chernikov #define	TRUNK_LOCK(trunk)	rm_wlock(&(trunk)->lock)
182772b000fSAlexander V. Chernikov #define	TRUNK_UNLOCK(trunk)	rm_wunlock(&(trunk)->lock)
183772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_ASSERT(trunk) rm_assert(&(trunk)->lock, RA_WLOCKED)
184772b000fSAlexander V. Chernikov #define	TRUNK_RLOCK(trunk)	rm_rlock(&(trunk)->lock, &tracker)
185772b000fSAlexander V. Chernikov #define	TRUNK_RUNLOCK(trunk)	rm_runlock(&(trunk)->lock, &tracker)
186772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_RASSERT(trunk) rm_assert(&(trunk)->lock, RA_RLOCKED)
187772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_READER	struct rm_priotracker tracker
18875ee267cSGleb Smirnoff 
18975ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
19075ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
19175ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
19275ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
19375ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
19475ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
19575ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
1967983103aSRobert Watson 	uint16_t vid);
19775ee267cSGleb Smirnoff #endif
19875ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
1994faedfe8SSam Leffler 
200114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
201a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
202cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
203d9b1d615SJohn Baldwin static	void vlan_qflush(struct ifnet *ifp);
204299153b5SAlexander V. Chernikov static uint64_t vlan_get_counter(struct ifnet *ifp, ifnet_counter cnt);
2051cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
2061cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
2071cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
208f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
209d9b1d615SJohn Baldwin static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
2106f359e28SJohn Baldwin static	void vlan_unconfig(struct ifnet *ifp);
21128cc4d37SJohn Baldwin static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
21275ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
213a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
21475ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
21575ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
216f731f104SBill Paul 
2177983103aSRobert Watson static	struct ifnet *vlan_clone_match_ethervid(struct if_clone *,
218f889d2efSBrooks Davis     const char *, int *);
219f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
2206b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
221f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
222f889d2efSBrooks Davis 
2235cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
224ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
2255cb8c31aSYaroslav Tykhiy 
22642a58907SGleb Smirnoff static struct if_clone *vlan_cloner;
2279d4fe4b2SBrooks Davis 
228ccf7ba97SMarko Zec #ifdef VIMAGE
22942a58907SGleb Smirnoff static VNET_DEFINE(struct if_clone *, vlan_cloner);
230ccf7ba97SMarko Zec #define	V_vlan_cloner	VNET(vlan_cloner)
231ccf7ba97SMarko Zec #endif
232ccf7ba97SMarko Zec 
23375ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
23475ee267cSGleb Smirnoff #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
235114c608cSYaroslav Tykhiy 
23675ee267cSGleb Smirnoff static void
23775ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
23875ee267cSGleb Smirnoff {
23975ee267cSGleb Smirnoff 	int i, n;
24075ee267cSGleb Smirnoff 
24175ee267cSGleb Smirnoff 	/*
24275ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
24375ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
24475ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
24575ee267cSGleb Smirnoff 	 */
24675ee267cSGleb Smirnoff 
24775ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
24875ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
24975ee267cSGleb Smirnoff 
25075ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
25175ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
25275ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
25375ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
25475ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
25575ee267cSGleb Smirnoff 		LIST_INIT(&trunk->hash[i]);
25675ee267cSGleb Smirnoff }
25775ee267cSGleb Smirnoff 
25875ee267cSGleb Smirnoff static void
25975ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
26075ee267cSGleb Smirnoff {
26175ee267cSGleb Smirnoff #ifdef INVARIANTS
26275ee267cSGleb Smirnoff 	int i;
26375ee267cSGleb Smirnoff 
26475ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
26575ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
26675ee267cSGleb Smirnoff 		KASSERT(LIST_EMPTY(&trunk->hash[i]),
26775ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
26875ee267cSGleb Smirnoff #endif
26975ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
27075ee267cSGleb Smirnoff 	trunk->hash = NULL;
27175ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
27275ee267cSGleb Smirnoff }
27375ee267cSGleb Smirnoff 
27475ee267cSGleb Smirnoff static int
27575ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
27675ee267cSGleb Smirnoff {
27775ee267cSGleb Smirnoff 	int i, b;
27875ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
27975ee267cSGleb Smirnoff 
28075ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
28175ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
28275ee267cSGleb Smirnoff 
28375ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
2847983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
28575ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
2867983103aSRobert Watson 		if (ifv->ifv_vid == ifv2->ifv_vid)
28775ee267cSGleb Smirnoff 			return (EEXIST);
28875ee267cSGleb Smirnoff 
28975ee267cSGleb Smirnoff 	/*
29075ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
29175ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
29275ee267cSGleb Smirnoff 	 * buckets/2.
29375ee267cSGleb Smirnoff 	 */
29475ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
29575ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
2967983103aSRobert Watson 		i = HASH(ifv->ifv_vid, trunk->hmask);
29775ee267cSGleb Smirnoff 	}
29875ee267cSGleb Smirnoff 	LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
29975ee267cSGleb Smirnoff 	trunk->refcnt++;
30075ee267cSGleb Smirnoff 
30175ee267cSGleb Smirnoff 	return (0);
30275ee267cSGleb Smirnoff }
30375ee267cSGleb Smirnoff 
30475ee267cSGleb Smirnoff static int
30575ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
30675ee267cSGleb Smirnoff {
30775ee267cSGleb Smirnoff 	int i, b;
30875ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
30975ee267cSGleb Smirnoff 
31075ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
31175ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
31275ee267cSGleb Smirnoff 
31375ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
3147983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
31575ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
31675ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
31775ee267cSGleb Smirnoff 			trunk->refcnt--;
31875ee267cSGleb Smirnoff 			LIST_REMOVE(ifv2, ifv_list);
31975ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
32075ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
32175ee267cSGleb Smirnoff 			return (0);
32275ee267cSGleb Smirnoff 		}
32375ee267cSGleb Smirnoff 
32475ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
32575ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
32675ee267cSGleb Smirnoff }
32775ee267cSGleb Smirnoff 
32875ee267cSGleb Smirnoff /*
32975ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
33075ee267cSGleb Smirnoff  */
33175ee267cSGleb Smirnoff static void
33275ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
33375ee267cSGleb Smirnoff {
33475ee267cSGleb Smirnoff 	struct ifvlan *ifv;
33575ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
33675ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
33775ee267cSGleb Smirnoff 
33875ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(trunk);
33975ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
34075ee267cSGleb Smirnoff 
34175ee267cSGleb Smirnoff 	if (howmuch == 0) {
34275ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
34375ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
34475ee267cSGleb Smirnoff 		return;
34575ee267cSGleb Smirnoff 	}
34675ee267cSGleb Smirnoff 
34775ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
34875ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
34975ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
35075ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
35175ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
35275ee267cSGleb Smirnoff 		return;
35375ee267cSGleb Smirnoff 
35475ee267cSGleb Smirnoff 	/* M_NOWAIT because we're called with trunk mutex held */
35575ee267cSGleb Smirnoff 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
35675ee267cSGleb Smirnoff 	if (hash2 == NULL) {
35775ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
35875ee267cSGleb Smirnoff 		    __func__);
35975ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
36075ee267cSGleb Smirnoff 	}
36175ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
36275ee267cSGleb Smirnoff 		LIST_INIT(&hash2[j]);
36375ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
364c0cb022bSYaroslav Tykhiy 		while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
36575ee267cSGleb Smirnoff 			LIST_REMOVE(ifv, ifv_list);
3667983103aSRobert Watson 			j = HASH(ifv->ifv_vid, n2 - 1);
36775ee267cSGleb Smirnoff 			LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
36875ee267cSGleb Smirnoff 		}
36975ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
37075ee267cSGleb Smirnoff 	trunk->hash = hash2;
37175ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
37275ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
373f84b2d69SYaroslav Tykhiy 
374f84b2d69SYaroslav Tykhiy 	if (bootverbose)
375f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
376f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
37775ee267cSGleb Smirnoff }
37875ee267cSGleb Smirnoff 
37975ee267cSGleb Smirnoff static __inline struct ifvlan *
3807983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
38175ee267cSGleb Smirnoff {
38275ee267cSGleb Smirnoff 	struct ifvlan *ifv;
38375ee267cSGleb Smirnoff 
38475ee267cSGleb Smirnoff 	TRUNK_LOCK_RASSERT(trunk);
38575ee267cSGleb Smirnoff 
3867983103aSRobert Watson 	LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
3877983103aSRobert Watson 		if (ifv->ifv_vid == vid)
38875ee267cSGleb Smirnoff 			return (ifv);
38975ee267cSGleb Smirnoff 	return (NULL);
39075ee267cSGleb Smirnoff }
39175ee267cSGleb Smirnoff 
39275ee267cSGleb Smirnoff #if 0
39375ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
39475ee267cSGleb Smirnoff static void
39575ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
39675ee267cSGleb Smirnoff {
39775ee267cSGleb Smirnoff 	int i;
39875ee267cSGleb Smirnoff 	struct ifvlan *ifv;
39975ee267cSGleb Smirnoff 
40075ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
40175ee267cSGleb Smirnoff 		printf("%d: ", i);
40275ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
40375ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
40475ee267cSGleb Smirnoff 		printf("\n");
40575ee267cSGleb Smirnoff 	}
40675ee267cSGleb Smirnoff }
40775ee267cSGleb Smirnoff #endif /* 0 */
408e4cd31ddSJeff Roberson #else
409e4cd31ddSJeff Roberson 
410e4cd31ddSJeff Roberson static __inline struct ifvlan *
4117983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
412e4cd31ddSJeff Roberson {
413e4cd31ddSJeff Roberson 
4147983103aSRobert Watson 	return trunk->vlans[vid];
415e4cd31ddSJeff Roberson }
416e4cd31ddSJeff Roberson 
417e4cd31ddSJeff Roberson static __inline int
418e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
419e4cd31ddSJeff Roberson {
420e4cd31ddSJeff Roberson 
4217983103aSRobert Watson 	if (trunk->vlans[ifv->ifv_vid] != NULL)
422e4cd31ddSJeff Roberson 		return EEXIST;
4237983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = ifv;
424e4cd31ddSJeff Roberson 	trunk->refcnt++;
425e4cd31ddSJeff Roberson 
426e4cd31ddSJeff Roberson 	return (0);
427e4cd31ddSJeff Roberson }
428e4cd31ddSJeff Roberson 
429e4cd31ddSJeff Roberson static __inline int
430e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
431e4cd31ddSJeff Roberson {
432e4cd31ddSJeff Roberson 
4337983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = NULL;
434e4cd31ddSJeff Roberson 	trunk->refcnt--;
435e4cd31ddSJeff Roberson 
436e4cd31ddSJeff Roberson 	return (0);
437e4cd31ddSJeff Roberson }
438e4cd31ddSJeff Roberson 
439e4cd31ddSJeff Roberson static __inline void
440e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk)
441e4cd31ddSJeff Roberson {
442e4cd31ddSJeff Roberson }
443e4cd31ddSJeff Roberson 
444e4cd31ddSJeff Roberson static __inline void
445e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk)
446e4cd31ddSJeff Roberson {
447e4cd31ddSJeff Roberson }
448e4cd31ddSJeff Roberson 
44975ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
45075ee267cSGleb Smirnoff 
45175ee267cSGleb Smirnoff static void
45275ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
45375ee267cSGleb Smirnoff {
45475ee267cSGleb Smirnoff 	VLAN_LOCK_ASSERT();
45575ee267cSGleb Smirnoff 
45675ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
45775ee267cSGleb Smirnoff 	vlan_freehash(trunk);
45875ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
45933499e2aSYaroslav Tykhiy 	TRUNK_UNLOCK(trunk);
46033499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
46175ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
46275ee267cSGleb Smirnoff }
46375ee267cSGleb Smirnoff 
464f731f104SBill Paul /*
465f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
466f731f104SBill Paul  * programming the multicast filter of the parent. This has the
467f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
468f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
469f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
470f731f104SBill Paul  * to avoid this: there really is only one physical interface.
471f731f104SBill Paul  */
4722b120974SPeter Wemm static int
4732b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
474f731f104SBill Paul {
475f731f104SBill Paul 	struct ifnet		*ifp_p;
4762d222cb7SAlexander Motin 	struct ifmultiaddr	*ifma;
477f731f104SBill Paul 	struct ifvlan		*sc;
478c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
479f731f104SBill Paul 	int			error;
480f731f104SBill Paul 
481f731f104SBill Paul 	/* Find the parent. */
482f731f104SBill Paul 	sc = ifp->if_softc;
4832d222cb7SAlexander Motin 	TRUNK_LOCK_ASSERT(TRUNK(sc));
48475ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
4851b2a4f7aSBill Fenner 
4868b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
4878b615593SMarko Zec 
488f731f104SBill Paul 	/* First, remove any existing filter entries. */
489c0cb022bSYaroslav Tykhiy 	while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
490f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
4912d222cb7SAlexander Motin 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
4929d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
493f731f104SBill Paul 	}
494f731f104SBill Paul 
495f731f104SBill Paul 	/* Now program new ones. */
4962d222cb7SAlexander Motin 	IF_ADDR_WLOCK(ifp);
4976817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
498f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
499f731f104SBill Paul 			continue;
50029c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
5012d222cb7SAlexander Motin 		if (mc == NULL) {
5022d222cb7SAlexander Motin 			IF_ADDR_WUNLOCK(ifp);
50329c2dfbeSBruce M Simpson 			return (ENOMEM);
5042d222cb7SAlexander Motin 		}
505e4cd31ddSJeff Roberson 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
506e4cd31ddSJeff Roberson 		mc->mc_addr.sdl_index = ifp_p->if_index;
507f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
5082d222cb7SAlexander Motin 	}
5092d222cb7SAlexander Motin 	IF_ADDR_WUNLOCK(ifp);
5102d222cb7SAlexander Motin 	SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
511e4cd31ddSJeff Roberson 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
5122d222cb7SAlexander Motin 		    NULL);
513f731f104SBill Paul 		if (error)
514f731f104SBill Paul 			return (error);
515f731f104SBill Paul 	}
516f731f104SBill Paul 
5178b615593SMarko Zec 	CURVNET_RESTORE();
518f731f104SBill Paul 	return (0);
519f731f104SBill Paul }
5202cc2df49SGarrett Wollman 
521a3814acfSSam Leffler /*
522ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
523ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
524ea4ca115SAndrew Thompson  * should also change it on all children vlans.
525ea4ca115SAndrew Thompson  */
526ea4ca115SAndrew Thompson static void
527ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
528ea4ca115SAndrew Thompson {
529ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
5306117727bSAndrew Thompson #ifndef VLAN_ARRAY
5316117727bSAndrew Thompson 	struct ifvlan *next;
5326117727bSAndrew Thompson #endif
533ea4ca115SAndrew Thompson 	int i;
534ea4ca115SAndrew Thompson 
535ea4ca115SAndrew Thompson 	/*
536ea4ca115SAndrew Thompson 	 * Check if it's a trunk interface first of all
537ea4ca115SAndrew Thompson 	 * to avoid needless locking.
538ea4ca115SAndrew Thompson 	 */
539ea4ca115SAndrew Thompson 	if (ifp->if_vlantrunk == NULL)
540ea4ca115SAndrew Thompson 		return;
541ea4ca115SAndrew Thompson 
542ea4ca115SAndrew Thompson 	VLAN_LOCK();
543ea4ca115SAndrew Thompson 	/*
544ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
545ea4ca115SAndrew Thompson 	 */
546ea4ca115SAndrew Thompson #ifdef VLAN_ARRAY
547ea4ca115SAndrew Thompson 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
5486117727bSAndrew Thompson 		if ((ifv = ifp->if_vlantrunk->vlans[i])) {
549ea4ca115SAndrew Thompson #else /* VLAN_ARRAY */
550ea4ca115SAndrew Thompson 	for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
5516117727bSAndrew Thompson 		LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
552ea4ca115SAndrew Thompson #endif /* VLAN_ARRAY */
553ea4ca115SAndrew Thompson 			VLAN_UNLOCK();
554e4cd31ddSJeff Roberson 			if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp),
555e4cd31ddSJeff Roberson 			    ifp->if_addrlen);
5566117727bSAndrew Thompson 			VLAN_LOCK();
5576117727bSAndrew Thompson 		}
5586117727bSAndrew Thompson 	VLAN_UNLOCK();
559ea4ca115SAndrew Thompson 
560ea4ca115SAndrew Thompson }
561ea4ca115SAndrew Thompson 
562ea4ca115SAndrew Thompson /*
5635cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
5645cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
5655cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
5665428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
5675428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
5685cb8c31aSYaroslav Tykhiy  */
5695cb8c31aSYaroslav Tykhiy static void
5705cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
5715cb8c31aSYaroslav Tykhiy {
5725cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
5735cb8c31aSYaroslav Tykhiy 	int i;
5745cb8c31aSYaroslav Tykhiy 
5755cb8c31aSYaroslav Tykhiy 	/*
5765cb8c31aSYaroslav Tykhiy 	 * Check if it's a trunk interface first of all
5775cb8c31aSYaroslav Tykhiy 	 * to avoid needless locking.
5785cb8c31aSYaroslav Tykhiy 	 */
5795cb8c31aSYaroslav Tykhiy 	if (ifp->if_vlantrunk == NULL)
5805cb8c31aSYaroslav Tykhiy 		return;
5815cb8c31aSYaroslav Tykhiy 
5825428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
5835428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
5845428776eSJohn Baldwin 		return;
5855428776eSJohn Baldwin 
5865cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
5875cb8c31aSYaroslav Tykhiy 	/*
5885cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
5895cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
5905cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
5915cb8c31aSYaroslav Tykhiy 	 */
5925cb8c31aSYaroslav Tykhiy #ifdef VLAN_ARRAY
5935cb8c31aSYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
5945cb8c31aSYaroslav Tykhiy 		if ((ifv = ifp->if_vlantrunk->vlans[i])) {
59528cc4d37SJohn Baldwin 			vlan_unconfig_locked(ifv->ifv_ifp, 1);
5965cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk == NULL)
5975cb8c31aSYaroslav Tykhiy 				break;
5985cb8c31aSYaroslav Tykhiy 		}
5995cb8c31aSYaroslav Tykhiy #else /* VLAN_ARRAY */
6005cb8c31aSYaroslav Tykhiy restart:
6015cb8c31aSYaroslav Tykhiy 	for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
6025cb8c31aSYaroslav Tykhiy 		if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
60328cc4d37SJohn Baldwin 			vlan_unconfig_locked(ifv->ifv_ifp, 1);
6045cb8c31aSYaroslav Tykhiy 			if (ifp->if_vlantrunk)
6055cb8c31aSYaroslav Tykhiy 				goto restart;	/* trunk->hwidth can change */
6065cb8c31aSYaroslav Tykhiy 			else
6075cb8c31aSYaroslav Tykhiy 				break;
6085cb8c31aSYaroslav Tykhiy 		}
6095cb8c31aSYaroslav Tykhiy #endif /* VLAN_ARRAY */
6105cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
6115cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
6125cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
6135cb8c31aSYaroslav Tykhiy }
6145cb8c31aSYaroslav Tykhiy 
6155cb8c31aSYaroslav Tykhiy /*
616e4cd31ddSJeff Roberson  * Return the trunk device for a virtual interface.
617e4cd31ddSJeff Roberson  */
618e4cd31ddSJeff Roberson static struct ifnet  *
619e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp)
620e4cd31ddSJeff Roberson {
621e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
622e4cd31ddSJeff Roberson 
623e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
624e4cd31ddSJeff Roberson 		return (NULL);
625e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
626e4cd31ddSJeff Roberson 	ifp = NULL;
627e4cd31ddSJeff Roberson 	VLAN_LOCK();
628e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
629e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
630e4cd31ddSJeff Roberson 	VLAN_UNLOCK();
631e4cd31ddSJeff Roberson 	return (ifp);
632e4cd31ddSJeff Roberson }
633e4cd31ddSJeff Roberson 
634e4cd31ddSJeff Roberson /*
6357983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
6367983103aSRobert Watson  * components such as Infiniband.
6377983103aSRobert Watson  *
6387983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
6397983103aSRobert Watson  * vlan_vid().
640e4cd31ddSJeff Roberson  */
641e4cd31ddSJeff Roberson static int
6427983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
643e4cd31ddSJeff Roberson {
644e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
645e4cd31ddSJeff Roberson 
646e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
647e4cd31ddSJeff Roberson 		return (EINVAL);
648e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
6497983103aSRobert Watson 	*vidp = ifv->ifv_vid;
650e4cd31ddSJeff Roberson 	return (0);
651e4cd31ddSJeff Roberson }
652e4cd31ddSJeff Roberson 
653e4cd31ddSJeff Roberson /*
654e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
655e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
656e4cd31ddSJeff Roberson  */
657e4cd31ddSJeff Roberson static void *
658e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
659e4cd31ddSJeff Roberson {
660e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
661e4cd31ddSJeff Roberson 
662e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
663e4cd31ddSJeff Roberson 		return (NULL);
664e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
665e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
666e4cd31ddSJeff Roberson }
667e4cd31ddSJeff Roberson 
668e4cd31ddSJeff Roberson /*
669e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
670e4cd31ddSJeff Roberson  * private per-instance data in.
671e4cd31ddSJeff Roberson  */
672e4cd31ddSJeff Roberson static int
673e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
674e4cd31ddSJeff Roberson {
675e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
676e4cd31ddSJeff Roberson 
677e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
678e4cd31ddSJeff Roberson 		return (EINVAL);
679e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
680e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
681e4cd31ddSJeff Roberson 	return (0);
682e4cd31ddSJeff Roberson }
683e4cd31ddSJeff Roberson 
684e4cd31ddSJeff Roberson /*
6857983103aSRobert Watson  * Return the vlan device present at the specific VID.
686e4cd31ddSJeff Roberson  */
687e4cd31ddSJeff Roberson static struct ifnet *
6887983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
689e4cd31ddSJeff Roberson {
690e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
691e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
692772b000fSAlexander V. Chernikov 	TRUNK_LOCK_READER;
693e4cd31ddSJeff Roberson 
694e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
695e4cd31ddSJeff Roberson 	if (trunk == NULL)
696e4cd31ddSJeff Roberson 		return (NULL);
697e4cd31ddSJeff Roberson 	ifp = NULL;
698e4cd31ddSJeff Roberson 	TRUNK_RLOCK(trunk);
6997983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
700e4cd31ddSJeff Roberson 	if (ifv)
701e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
702e4cd31ddSJeff Roberson 	TRUNK_RUNLOCK(trunk);
703e4cd31ddSJeff Roberson 	return (ifp);
704e4cd31ddSJeff Roberson }
705e4cd31ddSJeff Roberson 
706e4cd31ddSJeff Roberson /*
707a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
708a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
709a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
710a3814acfSSam Leffler  * set here.  Noone else in the system should be aware of this so
711a3814acfSSam Leffler  * we use an explicit reference here.
712a3814acfSSam Leffler  */
713a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
714a3814acfSSam Leffler 
715984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
716a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
717127d7b2dSAndre Oppermann 
7182b120974SPeter Wemm static int
7192b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
7202b120974SPeter Wemm {
7219d4fe4b2SBrooks Davis 
7222b120974SPeter Wemm 	switch (type) {
7232b120974SPeter Wemm 	case MOD_LOAD:
7245cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
7255cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
7265cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
7275cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
728ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
729ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
730ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
731ea4ca115SAndrew Thompson 			return (ENOMEM);
7324faedfe8SSam Leffler 		VLAN_LOCK_INIT();
7339d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
734127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
73575ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
736e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
737e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
738e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
739e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
740e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
741ccf7ba97SMarko Zec #ifndef VIMAGE
74242a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
74342a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
744ccf7ba97SMarko Zec #endif
74525c0f7b3SYaroslav Tykhiy 		if (bootverbose)
74625c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
74725c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
74825c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
74925c0f7b3SYaroslav Tykhiy #else
75025c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
75125c0f7b3SYaroslav Tykhiy #endif
75225c0f7b3SYaroslav Tykhiy 
75325c0f7b3SYaroslav Tykhiy 			       "\n");
7542b120974SPeter Wemm 		break;
7552b120974SPeter Wemm 	case MOD_UNLOAD:
756ccf7ba97SMarko Zec #ifndef VIMAGE
75742a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
758ccf7ba97SMarko Zec #endif
7595cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
760ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
7619d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
762127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
76375ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
764e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
765e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
76609fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
76709fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
768e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
7694faedfe8SSam Leffler 		VLAN_LOCK_DESTROY();
77025c0f7b3SYaroslav Tykhiy 		if (bootverbose)
77125c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
7729d4fe4b2SBrooks Davis 		break;
7733e019deaSPoul-Henning Kamp 	default:
7743e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
7752b120974SPeter Wemm 	}
77615a66c21SBruce M Simpson 	return (0);
7772b120974SPeter Wemm }
7782b120974SPeter Wemm 
7792b120974SPeter Wemm static moduledata_t vlan_mod = {
7802b120974SPeter Wemm 	"if_vlan",
7812b120974SPeter Wemm 	vlan_modevent,
7829823d527SKevin Lo 	0
7832b120974SPeter Wemm };
7842b120974SPeter Wemm 
7852b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
78611edc477SEd Maste MODULE_VERSION(if_vlan, 3);
7872cc2df49SGarrett Wollman 
788ccf7ba97SMarko Zec #ifdef VIMAGE
789ccf7ba97SMarko Zec static void
790ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
791ccf7ba97SMarko Zec {
792ccf7ba97SMarko Zec 
79342a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
79442a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
795ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
796ccf7ba97SMarko Zec }
797ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
798ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
799ccf7ba97SMarko Zec 
800ccf7ba97SMarko Zec static void
801ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
802ccf7ba97SMarko Zec {
803ccf7ba97SMarko Zec 
80442a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
805ccf7ba97SMarko Zec }
806ccf7ba97SMarko Zec VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
807ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
808ccf7ba97SMarko Zec #endif
809ccf7ba97SMarko Zec 
810f889d2efSBrooks Davis static struct ifnet *
8117983103aSRobert Watson vlan_clone_match_ethervid(struct if_clone *ifc, const char *name, int *vidp)
8129d4fe4b2SBrooks Davis {
813f889d2efSBrooks Davis 	const char *cp;
814f889d2efSBrooks Davis 	struct ifnet *ifp;
8157983103aSRobert Watson 	int vid;
816f889d2efSBrooks Davis 
817f889d2efSBrooks Davis 	/* Check for <etherif>.<vlan> style interface names. */
81877dfcdc4SRobert Watson 	IFNET_RLOCK_NOSLEEP();
819603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
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)
826f889d2efSBrooks Davis 			continue;
827f889d2efSBrooks Davis 		if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
828f889d2efSBrooks Davis 			continue;
829f889d2efSBrooks Davis 		cp = name + strlen(ifp->if_xname);
830fb92ad4aSJohn Baldwin 		if (*cp++ != '.')
831f889d2efSBrooks Davis 			continue;
832fb92ad4aSJohn Baldwin 		if (*cp == '\0')
833f889d2efSBrooks Davis 			continue;
8347983103aSRobert Watson 		vid = 0;
835fb92ad4aSJohn Baldwin 		for(; *cp >= '0' && *cp <= '9'; cp++)
8367983103aSRobert Watson 			vid = (vid * 10) + (*cp - '0');
837fb92ad4aSJohn Baldwin 		if (*cp != '\0')
838fb92ad4aSJohn Baldwin 			continue;
8397983103aSRobert Watson 		if (vidp != NULL)
8407983103aSRobert Watson 			*vidp = vid;
841f889d2efSBrooks Davis 		break;
842f889d2efSBrooks Davis 	}
84377dfcdc4SRobert Watson 	IFNET_RUNLOCK_NOSLEEP();
844f889d2efSBrooks Davis 
84515a66c21SBruce M Simpson 	return (ifp);
846f889d2efSBrooks Davis }
847f889d2efSBrooks Davis 
848f889d2efSBrooks Davis static int
849f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
850f889d2efSBrooks Davis {
851f889d2efSBrooks Davis 	const char *cp;
852f889d2efSBrooks Davis 
8537983103aSRobert Watson 	if (vlan_clone_match_ethervid(ifc, name, NULL) != NULL)
854f889d2efSBrooks Davis 		return (1);
855f889d2efSBrooks Davis 
85642a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
857f889d2efSBrooks Davis 		return (0);
858f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
859f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
860f889d2efSBrooks Davis 			return (0);
861f889d2efSBrooks Davis 	}
862f889d2efSBrooks Davis 
863f889d2efSBrooks Davis 	return (1);
864f889d2efSBrooks Davis }
865f889d2efSBrooks Davis 
866f889d2efSBrooks Davis static int
8676b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
868f889d2efSBrooks Davis {
869f889d2efSBrooks Davis 	char *dp;
870f889d2efSBrooks Davis 	int wildcard;
871f889d2efSBrooks Davis 	int unit;
872f889d2efSBrooks Davis 	int error;
8737983103aSRobert Watson 	int vid;
874f889d2efSBrooks Davis 	int ethertag;
8759d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
8769d4fe4b2SBrooks Davis 	struct ifnet *ifp;
877f889d2efSBrooks Davis 	struct ifnet *p;
8783ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
8793ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
8806b7330e2SSam Leffler 	struct vlanreq vlr;
88165239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
882f889d2efSBrooks Davis 
8836b7330e2SSam Leffler 	/*
8846b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
8856b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
8866b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
8876b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
8886b7330e2SSam Leffler 	 *   must be configured separately.
8896b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
8906b7330e2SSam Leffler 	 * supported for backwards compatibilty.
8917983103aSRobert Watson 	 *
8927983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
8937983103aSRobert Watson 	 * called for.
8946b7330e2SSam Leffler 	 */
8956b7330e2SSam Leffler 	if (params) {
8966b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
8976b7330e2SSam Leffler 		if (error)
8986b7330e2SSam Leffler 			return error;
8996b7330e2SSam Leffler 		p = ifunit(vlr.vlr_parent);
9006b7330e2SSam Leffler 		if (p == NULL)
9016b7330e2SSam Leffler 			return ENXIO;
9026b7330e2SSam Leffler 		/*
9037983103aSRobert Watson 		 * Don't let the caller set up a VLAN VID with
9046b7330e2SSam Leffler 		 * anything except VLID bits.
9056b7330e2SSam Leffler 		 */
9066b7330e2SSam Leffler 		if (vlr.vlr_tag & ~EVL_VLID_MASK)
9076b7330e2SSam Leffler 			return (EINVAL);
9086b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
9096b7330e2SSam Leffler 		if (error != 0)
9106b7330e2SSam Leffler 			return (error);
9116b7330e2SSam Leffler 
9126b7330e2SSam Leffler 		ethertag = 1;
9137983103aSRobert Watson 		vid = vlr.vlr_tag;
9146b7330e2SSam Leffler 		wildcard = (unit < 0);
9157983103aSRobert Watson 	} else if ((p = vlan_clone_match_ethervid(ifc, name, &vid)) != NULL) {
916f889d2efSBrooks Davis 		ethertag = 1;
917f889d2efSBrooks Davis 		unit = -1;
918f889d2efSBrooks Davis 		wildcard = 0;
919f889d2efSBrooks Davis 
920f889d2efSBrooks Davis 		/*
9217983103aSRobert Watson 		 * Don't let the caller set up a VLAN VID with
922f889d2efSBrooks Davis 		 * anything except VLID bits.
923f889d2efSBrooks Davis 		 */
9247983103aSRobert Watson 		if (vid & ~EVL_VLID_MASK)
925f889d2efSBrooks Davis 			return (EINVAL);
926f889d2efSBrooks Davis 	} else {
927f889d2efSBrooks Davis 		ethertag = 0;
928f889d2efSBrooks Davis 
929f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
930f889d2efSBrooks Davis 		if (error != 0)
931f889d2efSBrooks Davis 			return (error);
932f889d2efSBrooks Davis 
933f889d2efSBrooks Davis 		wildcard = (unit < 0);
934f889d2efSBrooks Davis 	}
935f889d2efSBrooks Davis 
936f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
937f889d2efSBrooks Davis 	if (error != 0)
938f889d2efSBrooks Davis 		return (error);
939f889d2efSBrooks Davis 
940f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
941f889d2efSBrooks Davis 	if (wildcard) {
942f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
943f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
944f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
945f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
946f889d2efSBrooks Davis 		}
947f889d2efSBrooks Davis 	}
9489d4fe4b2SBrooks Davis 
949a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
950fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
951fc74a9f9SBrooks Davis 	if (ifp == NULL) {
952fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
953fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
954fc74a9f9SBrooks Davis 		return (ENOSPC);
955fc74a9f9SBrooks Davis 	}
9569d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
957299153b5SAlexander V. Chernikov 	/* Prepare pcpu counters */
958299153b5SAlexander V. Chernikov 	ifv->ifv_ipackets = counter_u64_alloc(M_WAITOK);
959299153b5SAlexander V. Chernikov 	ifv->ifv_opackets = counter_u64_alloc(M_WAITOK);
960299153b5SAlexander V. Chernikov 	ifv->ifv_ibytes = counter_u64_alloc(M_WAITOK);
961299153b5SAlexander V. Chernikov 	ifv->ifv_obytes = counter_u64_alloc(M_WAITOK);
962299153b5SAlexander V. Chernikov 	ifv->ifv_omcasts = counter_u64_alloc(M_WAITOK);
963*6667db31SAlexander V. Chernikov 	ifv->ifv_oerrors = counter_u64_alloc(M_WAITOK);
9649d4fe4b2SBrooks Davis 
9659d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
966f889d2efSBrooks Davis 	/*
967cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
968f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
969f889d2efSBrooks Davis 	 */
970f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
97142a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
972f889d2efSBrooks Davis 	ifp->if_dunit = unit;
9739d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
9749d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
97515a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
9769d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
9779d4fe4b2SBrooks Davis 
978114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
979d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
980d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
9819d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
98264a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
983299153b5SAlexander V. Chernikov 	ifp->if_get_counter = vlan_get_counter;
984fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
9859d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
986211f625aSBill Fenner 	ifp->if_baudrate = 0;
987a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
988a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
9893ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
9903ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
9913ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
9929d4fe4b2SBrooks Davis 
993f889d2efSBrooks Davis 	if (ethertag) {
9947983103aSRobert Watson 		error = vlan_config(ifv, p, vid);
995f889d2efSBrooks Davis 		if (error != 0) {
996f889d2efSBrooks Davis 			/*
99728cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
998f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
999f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1000f889d2efSBrooks Davis 			 */
1001f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1002249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
10034b22573aSBrooks Davis 			if_free(ifp);
100496c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
1005f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1006f889d2efSBrooks Davis 
1007f889d2efSBrooks Davis 			return (error);
1008f889d2efSBrooks Davis 		}
1009f889d2efSBrooks Davis 
10101cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
10111cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
1012f889d2efSBrooks Davis 	}
1013f889d2efSBrooks Davis 
10149d4fe4b2SBrooks Davis 	return (0);
10159d4fe4b2SBrooks Davis }
10169d4fe4b2SBrooks Davis 
1017f889d2efSBrooks Davis static int
1018f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
10199d4fe4b2SBrooks Davis {
10209d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1021114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
1022b4e9f837SBrooks Davis 
1023249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1024249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
10254b22573aSBrooks Davis 	if_free(ifp);
1026299153b5SAlexander V. Chernikov 	counter_u64_free(ifv->ifv_ipackets);
1027299153b5SAlexander V. Chernikov 	counter_u64_free(ifv->ifv_opackets);
1028299153b5SAlexander V. Chernikov 	counter_u64_free(ifv->ifv_ibytes);
1029299153b5SAlexander V. Chernikov 	counter_u64_free(ifv->ifv_obytes);
1030299153b5SAlexander V. Chernikov 	counter_u64_free(ifv->ifv_omcasts);
1031*6667db31SAlexander V. Chernikov 	counter_u64_free(ifv->ifv_oerrors);
10329d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1033b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
1034b4e9f837SBrooks Davis 
1035f889d2efSBrooks Davis 	return (0);
10369d4fe4b2SBrooks Davis }
10379d4fe4b2SBrooks Davis 
103815a66c21SBruce M Simpson /*
103915a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
104015a66c21SBruce M Simpson  */
10412cc2df49SGarrett Wollman static void
1042114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
10432cc2df49SGarrett Wollman {
10442cc2df49SGarrett Wollman }
10452cc2df49SGarrett Wollman 
10466d3a3ab7SGleb Smirnoff /*
1047d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
10486d3a3ab7SGleb Smirnoff  */
1049d9b1d615SJohn Baldwin static int
1050d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
10512cc2df49SGarrett Wollman {
10522cc2df49SGarrett Wollman 	struct ifvlan *ifv;
10532cc2df49SGarrett Wollman 	struct ifnet *p;
10541ad7a257SPyun YongHyeon 	int error, len, mcast;
10552cc2df49SGarrett Wollman 
10562cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
105775ee267cSGleb Smirnoff 	p = PARENT(ifv);
10581ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
10591ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
10602cc2df49SGarrett Wollman 
1061a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
10622cc2df49SGarrett Wollman 
1063f731f104SBill Paul 	/*
1064d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
106524993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
106624993214SYaroslav Tykhiy 	 */
10672dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
106824993214SYaroslav Tykhiy 		m_freem(m);
1069*6667db31SAlexander V. Chernikov 		counter_u64_add(ifv->ifv_oerrors, 1);
10708b20f6cfSHiroki Sato 		return (ENETDOWN);
107124993214SYaroslav Tykhiy 	}
107224993214SYaroslav Tykhiy 
107324993214SYaroslav Tykhiy 	/*
1074f6e5e0adSYaroslav Tykhiy 	 * Pad the frame to the minimum size allowed if told to.
1075f6e5e0adSYaroslav Tykhiy 	 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
1076f6e5e0adSYaroslav Tykhiy 	 * paragraph C.4.4.3.b.  It can help to work around buggy
1077f6e5e0adSYaroslav Tykhiy 	 * bridges that violate paragraph C.4.4.3.a from the same
1078f6e5e0adSYaroslav Tykhiy 	 * document, i.e., fail to pad short frames after untagging.
1079f6e5e0adSYaroslav Tykhiy 	 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
1080f6e5e0adSYaroslav Tykhiy 	 * untagging it will produce a 62-byte frame, which is a runt
1081f6e5e0adSYaroslav Tykhiy 	 * and requires padding.  There are VLAN-enabled network
1082f6e5e0adSYaroslav Tykhiy 	 * devices that just discard such runts instead or mishandle
1083f6e5e0adSYaroslav Tykhiy 	 * them somehow.
1084f6e5e0adSYaroslav Tykhiy 	 */
1085e4cd31ddSJeff Roberson 	if (soft_pad && p->if_type == IFT_ETHER) {
1086f6e5e0adSYaroslav Tykhiy 		static char pad[8];	/* just zeros */
1087f6e5e0adSYaroslav Tykhiy 		int n;
1088f6e5e0adSYaroslav Tykhiy 
1089f6e5e0adSYaroslav Tykhiy 		for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
1090f6e5e0adSYaroslav Tykhiy 		     n > 0; n -= sizeof(pad))
1091f6e5e0adSYaroslav Tykhiy 			if (!m_append(m, min(n, sizeof(pad)), pad))
1092f6e5e0adSYaroslav Tykhiy 				break;
1093f6e5e0adSYaroslav Tykhiy 
1094f6e5e0adSYaroslav Tykhiy 		if (n > 0) {
1095f6e5e0adSYaroslav Tykhiy 			if_printf(ifp, "cannot pad short frame\n");
1096*6667db31SAlexander V. Chernikov 			counter_u64_add(ifv->ifv_oerrors, 1);
1097f6e5e0adSYaroslav Tykhiy 			m_freem(m);
1098d9b1d615SJohn Baldwin 			return (0);
1099f6e5e0adSYaroslav Tykhiy 		}
1100f6e5e0adSYaroslav Tykhiy 	}
1101f6e5e0adSYaroslav Tykhiy 
1102f6e5e0adSYaroslav Tykhiy 	/*
1103a3814acfSSam Leffler 	 * If underlying interface can do VLAN tag insertion itself,
1104a3814acfSSam Leffler 	 * just pass the packet along. However, we need some way to
1105a3814acfSSam Leffler 	 * tell the interface where the packet came from so that it
1106a3814acfSSam Leffler 	 * knows how to find the VLAN tag to use, so we attach a
1107a3814acfSSam Leffler 	 * packet tag that holds it.
1108f731f104SBill Paul 	 */
1109b08347a0SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
11107983103aSRobert Watson 		m->m_pkthdr.ether_vtag = ifv->ifv_vid;
1111ba26134bSGleb Smirnoff 		m->m_flags |= M_VLANTAG;
1112f731f104SBill Paul 	} else {
11137983103aSRobert Watson 		m = ether_vlanencap(m, ifv->ifv_vid);
11144af90a4dSMatthew N. Dodd 		if (m == NULL) {
1115d9b1d615SJohn Baldwin 			if_printf(ifp, "unable to prepend VLAN header\n");
1116*6667db31SAlexander V. Chernikov 			counter_u64_add(ifv->ifv_oerrors, 1);
1117d9b1d615SJohn Baldwin 			return (0);
11184af90a4dSMatthew N. Dodd 		}
1119f731f104SBill Paul 	}
11202cc2df49SGarrett Wollman 
11212cc2df49SGarrett Wollman 	/*
11222cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
11232cc2df49SGarrett Wollman 	 */
1124aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1125299153b5SAlexander V. Chernikov 	if (error == 0) {
1126299153b5SAlexander V. Chernikov 		counter_u64_add(ifv->ifv_opackets, 1);
1127299153b5SAlexander V. Chernikov 		counter_u64_add(ifv->ifv_obytes, len);
1128*6667db31SAlexander V. Chernikov 		counter_u64_add(ifv->ifv_omcasts, mcast);
11291ad7a257SPyun YongHyeon 	} else
1130*6667db31SAlexander V. Chernikov 		counter_u64_add(ifv->ifv_oerrors, 1);
1131d9b1d615SJohn Baldwin 	return (error);
11322cc2df49SGarrett Wollman }
1133d9b1d615SJohn Baldwin 
1134299153b5SAlexander V. Chernikov static uint64_t
1135299153b5SAlexander V. Chernikov vlan_get_counter(struct ifnet *ifp, ifnet_counter cnt)
1136299153b5SAlexander V. Chernikov {
1137299153b5SAlexander V. Chernikov 	struct ifvlan *ifv;
1138299153b5SAlexander V. Chernikov 
1139299153b5SAlexander V. Chernikov 	ifv = ifp->if_softc;
1140299153b5SAlexander V. Chernikov 
1141299153b5SAlexander V. Chernikov 	switch (cnt) {
1142299153b5SAlexander V. Chernikov 		case IFCOUNTER_IPACKETS:
1143299153b5SAlexander V. Chernikov 			return (counter_u64_fetch(ifv->ifv_ipackets));
1144299153b5SAlexander V. Chernikov 		case IFCOUNTER_OPACKETS:
1145299153b5SAlexander V. Chernikov 			return (counter_u64_fetch(ifv->ifv_opackets));
1146299153b5SAlexander V. Chernikov 		case IFCOUNTER_IBYTES:
1147299153b5SAlexander V. Chernikov 			return (counter_u64_fetch(ifv->ifv_ibytes));
1148299153b5SAlexander V. Chernikov 		case IFCOUNTER_OBYTES:
1149299153b5SAlexander V. Chernikov 			return (counter_u64_fetch(ifv->ifv_obytes));
1150299153b5SAlexander V. Chernikov 		case IFCOUNTER_OMCASTS:
1151299153b5SAlexander V. Chernikov 			return (counter_u64_fetch(ifv->ifv_omcasts));
1152*6667db31SAlexander V. Chernikov 		case IFCOUNTER_OERRORS:
1153*6667db31SAlexander V. Chernikov 			return (counter_u64_fetch(ifv->ifv_oerrors));
1154299153b5SAlexander V. Chernikov 		default:
1155299153b5SAlexander V. Chernikov 			return (if_get_counter_compat(ifp, cnt));
1156299153b5SAlexander V. Chernikov 	}
1157299153b5SAlexander V. Chernikov 	/* NOTREACHED */
1158299153b5SAlexander V. Chernikov }
1159299153b5SAlexander V. Chernikov 
1160d9b1d615SJohn Baldwin /*
1161d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1162d9b1d615SJohn Baldwin  */
1163d9b1d615SJohn Baldwin static void
1164d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1165d9b1d615SJohn Baldwin {
1166f731f104SBill Paul }
1167f731f104SBill Paul 
1168a3814acfSSam Leffler static void
1169a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1170f731f104SBill Paul {
117175ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1172f731f104SBill Paul 	struct ifvlan *ifv;
1173772b000fSAlexander V. Chernikov 	TRUNK_LOCK_READER;
11747983103aSRobert Watson 	uint16_t vid;
117575ee267cSGleb Smirnoff 
117675ee267cSGleb Smirnoff 	KASSERT(trunk != NULL, ("%s: no trunk", __func__));
1177a3814acfSSam Leffler 
1178f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1179a3814acfSSam Leffler 		/*
118014e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1181a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1182a3814acfSSam Leffler 		 */
11837983103aSRobert Watson 		vid = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
11846ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1185a3814acfSSam Leffler 	} else {
118675ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
118775ee267cSGleb Smirnoff 
118814e98256SYaroslav Tykhiy 		/*
118914e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
119014e98256SYaroslav Tykhiy 		 */
1191a3814acfSSam Leffler 		switch (ifp->if_type) {
1192a3814acfSSam Leffler 		case IFT_ETHER:
1193a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1194a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1195a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1196a3814acfSSam Leffler 				return;
1197a3814acfSSam Leffler 			}
1198a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
11997983103aSRobert Watson 			vid = EVL_VLANOFTAG(ntohs(evl->evl_tag));
1200db8b5973SYaroslav Tykhiy 
1201db8b5973SYaroslav Tykhiy 			/*
12022dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
12032dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
12042dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
12052dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1206db8b5973SYaroslav Tykhiy 			 */
12072dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
12082dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
12092dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1210a3814acfSSam Leffler 			break;
12112dc879b3SYaroslav Tykhiy 
1212a3814acfSSam Leffler 		default:
1213db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
121460c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
121560c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1216a3814acfSSam Leffler #endif
121760c60618SYaroslav Tykhiy 			m_freem(m);
121860c60618SYaroslav Tykhiy 			ifp->if_noproto++;
121960c60618SYaroslav Tykhiy 			return;
1220a3814acfSSam Leffler 		}
12217a46ec8fSBrooks Davis 	}
12227a46ec8fSBrooks Davis 
122315ed2fa1SYaroslav Tykhiy 	TRUNK_RLOCK(trunk);
12247983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
12252dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
122675ee267cSGleb Smirnoff 		TRUNK_RUNLOCK(trunk);
122775ee267cSGleb Smirnoff 		m_freem(m);
122875ee267cSGleb Smirnoff 		ifp->if_noproto++;
122975ee267cSGleb Smirnoff 		return;
123075ee267cSGleb Smirnoff 	}
123175ee267cSGleb Smirnoff 	TRUNK_RUNLOCK(trunk);
1232f731f104SBill Paul 
1233fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1234299153b5SAlexander V. Chernikov 	counter_u64_add(ifv->ifv_ipackets, 1);
1235299153b5SAlexander V. Chernikov 	counter_u64_add(ifv->ifv_ibytes, m->m_pkthdr.len);
12362cc2df49SGarrett Wollman 
1237a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1238fc74a9f9SBrooks Davis 	(*ifp->if_input)(ifv->ifv_ifp, m);
12392cc2df49SGarrett Wollman }
12402cc2df49SGarrett Wollman 
12412cc2df49SGarrett Wollman static int
12427983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
12432cc2df49SGarrett Wollman {
124475ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
12451cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
124675ee267cSGleb Smirnoff 	int error = 0;
12472cc2df49SGarrett Wollman 
124875ee267cSGleb Smirnoff 	/* VID numbers 0x0 and 0xFFF are reserved */
12497983103aSRobert Watson 	if (vid == 0 || vid == 0xFFF)
125075ee267cSGleb Smirnoff 		return (EINVAL);
1251e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1252e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
125315a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
125464a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
125564a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
125675ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
125715a66c21SBruce M Simpson 		return (EBUSY);
12582cc2df49SGarrett Wollman 
125975ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
126075ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
126175ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
126275ee267cSGleb Smirnoff 		vlan_inithash(trunk);
126305a2398fSGleb Smirnoff 		VLAN_LOCK();
126405a2398fSGleb Smirnoff 		if (p->if_vlantrunk != NULL) {
126505a2398fSGleb Smirnoff 			/* A race that that is very unlikely to be hit. */
126605a2398fSGleb Smirnoff 			vlan_freehash(trunk);
126705a2398fSGleb Smirnoff 			free(trunk, M_VLAN);
126805a2398fSGleb Smirnoff 			goto exists;
126905a2398fSGleb Smirnoff 		}
127075ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
127175ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
127275ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
127375ee267cSGleb Smirnoff 		trunk->parent = p;
127475ee267cSGleb Smirnoff 	} else {
127575ee267cSGleb Smirnoff 		VLAN_LOCK();
127675ee267cSGleb Smirnoff exists:
127775ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
127875ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
127975ee267cSGleb Smirnoff 	}
128075ee267cSGleb Smirnoff 
12817983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
128275ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
128375ee267cSGleb Smirnoff 	if (error)
128475ee267cSGleb Smirnoff 		goto done;
128573f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1286a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1287a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
12881cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1289a3814acfSSam Leffler 
1290a3814acfSSam Leffler 	/*
1291a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1292a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1293656acce4SYaroslav Tykhiy 	 * use it.
1294a3814acfSSam Leffler 	 */
1295656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1296656acce4SYaroslav Tykhiy 		/*
1297656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1298656acce4SYaroslav Tykhiy 		 * handle extended frames.
1299656acce4SYaroslav Tykhiy 		 */
1300a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1301656acce4SYaroslav Tykhiy 	} else {
1302a3814acfSSam Leffler 		/*
1303a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1304a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1305a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1306a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1307a3814acfSSam Leffler 		 * which might still be useful.
1308a3814acfSSam Leffler 		 */
1309a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1310a3814acfSSam Leffler 	}
1311a3814acfSSam Leffler 
131275ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
13131cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1314e4cd31ddSJeff Roberson 	/*
1315e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1316e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1317e4cd31ddSJeff Roberson 	 * interfaces to also work.
1318e4cd31ddSJeff Roberson 	 */
13191cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
132075ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1321e4cd31ddSJeff Roberson 	ifp->if_output = p->if_output;
1322e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1323e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1324e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1325e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
1326e4cd31ddSJeff Roberson 
13272cc2df49SGarrett Wollman 	/*
132824993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
132924993214SYaroslav Tykhiy 	 * Other flags are none of our business.
13302cc2df49SGarrett Wollman 	 */
133164a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
13321cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
13331cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
13341cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
13351cf236fbSYaroslav Tykhiy 
13361cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
13372cc2df49SGarrett Wollman 
133875ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1339a3814acfSSam Leffler 
1340a3814acfSSam Leffler 	/*
1341e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
13422cc2df49SGarrett Wollman 	 * physical interface's.
13432cc2df49SGarrett Wollman 	 */
1344e4cd31ddSJeff Roberson 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1345e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1346e4cd31ddSJeff Roberson 	    p->if_addrlen;
13471b2a4f7aSBill Fenner 
13481b2a4f7aSBill Fenner 	/*
13491b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
13501b2a4f7aSBill Fenner 	 * joined on the vlan device.
13511b2a4f7aSBill Fenner 	 */
13521cf236fbSYaroslav Tykhiy 	(void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
13532ada9747SYaroslav Tykhiy 
13542ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
13552ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
135675ee267cSGleb Smirnoff done:
135775ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
1358c725524cSJack F Vogel 	if (error == 0)
13597983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
136075ee267cSGleb Smirnoff 	VLAN_UNLOCK();
136175ee267cSGleb Smirnoff 
136275ee267cSGleb Smirnoff 	return (error);
13632cc2df49SGarrett Wollman }
13642cc2df49SGarrett Wollman 
13656f359e28SJohn Baldwin static void
1366f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1367f731f104SBill Paul {
13685cb8c31aSYaroslav Tykhiy 
13695cb8c31aSYaroslav Tykhiy 	VLAN_LOCK();
137028cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
13715cb8c31aSYaroslav Tykhiy 	VLAN_UNLOCK();
13725cb8c31aSYaroslav Tykhiy }
13735cb8c31aSYaroslav Tykhiy 
13746f359e28SJohn Baldwin static void
137528cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
13765cb8c31aSYaroslav Tykhiy {
137775ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1378f731f104SBill Paul 	struct vlan_mc_entry *mc;
1379f731f104SBill Paul 	struct ifvlan *ifv;
1380c725524cSJack F Vogel 	struct ifnet  *parent;
138128cc4d37SJohn Baldwin 	int error;
1382f731f104SBill Paul 
13835cb8c31aSYaroslav Tykhiy 	VLAN_LOCK_ASSERT();
13844faedfe8SSam Leffler 
1385f731f104SBill Paul 	ifv = ifp->if_softc;
138675ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
138722893351SJack F Vogel 	parent = NULL;
1388f731f104SBill Paul 
138922893351SJack F Vogel 	if (trunk != NULL) {
139075ee267cSGleb Smirnoff 
139175ee267cSGleb Smirnoff 		TRUNK_LOCK(trunk);
139222893351SJack F Vogel 		parent = trunk->parent;
13931b2a4f7aSBill Fenner 
1394f731f104SBill Paul 		/*
1395f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1396f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
13971b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1398f731f104SBill Paul 		 */
1399c0cb022bSYaroslav Tykhiy 		while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
14006f359e28SJohn Baldwin 			/*
140128cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1402b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
140328cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
140428cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
140528cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
140628cc4d37SJohn Baldwin 			 * succeed.
14076f359e28SJohn Baldwin 			 */
140828cc4d37SJohn Baldwin 			if (!departing) {
140928cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1410e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
141128cc4d37SJohn Baldwin 				if (error)
141228cc4d37SJohn Baldwin 					if_printf(ifp,
141328cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
141428cc4d37SJohn Baldwin 					    error);
141528cc4d37SJohn Baldwin 			}
1416f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
14179d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
1418f731f104SBill Paul 		}
1419a3814acfSSam Leffler 
14201cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
142175ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
142275ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
142375ee267cSGleb Smirnoff 
142475ee267cSGleb Smirnoff 		/*
142575ee267cSGleb Smirnoff 		 * Check if we were the last.
142675ee267cSGleb Smirnoff 		 */
142775ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
14282d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
142975ee267cSGleb Smirnoff 			/*
143075ee267cSGleb Smirnoff 			 * XXXGL: If some ithread has already entered
143175ee267cSGleb Smirnoff 			 * vlan_input() and is now blocked on the trunk
143275ee267cSGleb Smirnoff 			 * lock, then it should preempt us right after
143375ee267cSGleb Smirnoff 			 * unlock and finish its work. Then we will acquire
143475ee267cSGleb Smirnoff 			 * lock again in trunk_destroy().
143575ee267cSGleb Smirnoff 			 */
143675ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
143775ee267cSGleb Smirnoff 			trunk_destroy(trunk);
143875ee267cSGleb Smirnoff 		} else
143975ee267cSGleb Smirnoff 			TRUNK_UNLOCK(trunk);
14401b2a4f7aSBill Fenner 	}
1441f731f104SBill Paul 
1442f731f104SBill Paul 	/* Disconnect from parent. */
14431cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
14441cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
14455cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
14465cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
14475cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1448f731f104SBill Paul 
144922893351SJack F Vogel 	/*
145022893351SJack F Vogel 	 * Only dispatch an event if vlan was
145122893351SJack F Vogel 	 * attached, otherwise there is nothing
145222893351SJack F Vogel 	 * to cleanup anyway.
145322893351SJack F Vogel 	 */
145422893351SJack F Vogel 	if (parent != NULL)
14557983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1456f731f104SBill Paul }
1457f731f104SBill Paul 
14581cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1459f731f104SBill Paul static int
14601cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
14611cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1462a3814acfSSam Leffler {
14631cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
14641cf236fbSYaroslav Tykhiy 	int error;
1465a3814acfSSam Leffler 
14661cf236fbSYaroslav Tykhiy 	/* XXX VLAN_LOCK_ASSERT(); */
1467a3814acfSSam Leffler 
14681cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
14691cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
14701cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
14711cf236fbSYaroslav Tykhiy 
14721cf236fbSYaroslav Tykhiy 	/*
14731cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
14741cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
14751cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
14761cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
14771cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
14781cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
14791cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
14801cf236fbSYaroslav Tykhiy 	 */
14811cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
148275ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
14831cf236fbSYaroslav Tykhiy 		if (error)
1484a3814acfSSam Leffler 			return (error);
14851cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
14861cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
14871cf236fbSYaroslav Tykhiy 	}
14881cf236fbSYaroslav Tykhiy 	return (0);
14891cf236fbSYaroslav Tykhiy }
14901cf236fbSYaroslav Tykhiy 
14911cf236fbSYaroslav Tykhiy /*
14921cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
14931cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
14941cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
14951cf236fbSYaroslav Tykhiy  */
14961cf236fbSYaroslav Tykhiy static int
14971cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
14981cf236fbSYaroslav Tykhiy {
14991cf236fbSYaroslav Tykhiy 	int error, i;
15001cf236fbSYaroslav Tykhiy 
15011cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
15021cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
15031cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
15041cf236fbSYaroslav Tykhiy 		if (error)
15051cf236fbSYaroslav Tykhiy 			return (error);
15061cf236fbSYaroslav Tykhiy 	}
15071cf236fbSYaroslav Tykhiy 	return (0);
1508a3814acfSSam Leffler }
1509a3814acfSSam Leffler 
1510127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1511127d7b2dSAndre Oppermann static void
1512a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1513127d7b2dSAndre Oppermann {
151475ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
1515127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
151675ee267cSGleb Smirnoff 	int i;
1517127d7b2dSAndre Oppermann 
151875ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
151975ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
152015ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
152175ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
152275ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
152375ee267cSGleb Smirnoff #else
1524aad0be7aSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
1525aad0be7aSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
152675ee267cSGleb Smirnoff #endif
1527aad0be7aSGleb Smirnoff 			ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1528fc74a9f9SBrooks Davis 			if_link_state_change(ifv->ifv_ifp,
152975ee267cSGleb Smirnoff 			    trunk->parent->if_link_state);
1530127d7b2dSAndre Oppermann 		}
153175ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
153275ee267cSGleb Smirnoff }
153375ee267cSGleb Smirnoff 
153475ee267cSGleb Smirnoff static void
153575ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
153675ee267cSGleb Smirnoff {
153775ee267cSGleb Smirnoff 	struct ifnet *p = PARENT(ifv);
153875ee267cSGleb Smirnoff 	struct ifnet *ifp = ifv->ifv_ifp;
153975ee267cSGleb Smirnoff 
154075ee267cSGleb Smirnoff 	TRUNK_LOCK_ASSERT(TRUNK(ifv));
154175ee267cSGleb Smirnoff 
154275ee267cSGleb Smirnoff 	/*
154375ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
154475ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
154575ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
154675ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
154775ee267cSGleb Smirnoff 	 */
154875ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
154975ee267cSGleb Smirnoff 		ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
155075ee267cSGleb Smirnoff 
155175ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
155275ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
155375ee267cSGleb Smirnoff 		ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
15549b76d9cbSPyun YongHyeon 		ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
1555bf7dcda3SGleb Smirnoff 		    CSUM_UDP | CSUM_SCTP);
155675ee267cSGleb Smirnoff 	} else {
155775ee267cSGleb Smirnoff 		ifp->if_capenable = 0;
155875ee267cSGleb Smirnoff 		ifp->if_hwassist = 0;
155975ee267cSGleb Smirnoff 	}
15609b76d9cbSPyun YongHyeon 	/*
15619b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
15629b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
15639b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
15649b76d9cbSPyun YongHyeon 	 */
156572f31000SHans Petter Selasky 	if (p->if_hw_tsomax > 0)
156672f31000SHans Petter Selasky 		ifp->if_hw_tsomax = p->if_hw_tsomax;
15679b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
15689b76d9cbSPyun YongHyeon 		ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
15699b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
15709b76d9cbSPyun YongHyeon 		ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
15719b76d9cbSPyun YongHyeon 		ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
15729b76d9cbSPyun YongHyeon 	} else {
15739b76d9cbSPyun YongHyeon 		ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
15749b76d9cbSPyun YongHyeon 		ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
15759b76d9cbSPyun YongHyeon 	}
157609fe6320SNavdeep Parhar 
157709fe6320SNavdeep Parhar 	/*
157809fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
157909fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
158009fe6320SNavdeep Parhar 	 *
158109fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
158209fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
158309fe6320SNavdeep Parhar 	 * with its own bit.
158409fe6320SNavdeep Parhar 	 */
158509fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
158609fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
158709fe6320SNavdeep Parhar 		ifp->if_capabilities |= p->if_capabilities & IFCAP_TOE;
158809fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
158909fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
159009fe6320SNavdeep Parhar 		ifp->if_capenable |= p->if_capenable & IFCAP_TOE;
159109fe6320SNavdeep Parhar 	}
159275ee267cSGleb Smirnoff }
159375ee267cSGleb Smirnoff 
159475ee267cSGleb Smirnoff static void
159575ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
159675ee267cSGleb Smirnoff {
159775ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk = ifp->if_vlantrunk;
159875ee267cSGleb Smirnoff 	struct ifvlan *ifv;
159975ee267cSGleb Smirnoff 	int i;
160075ee267cSGleb Smirnoff 
160175ee267cSGleb Smirnoff 	TRUNK_LOCK(trunk);
160275ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
160315ed2fa1SYaroslav Tykhiy 	for (i = 0; i < VLAN_ARRAY_SIZE; i++)
160475ee267cSGleb Smirnoff 		if (trunk->vlans[i] != NULL) {
160575ee267cSGleb Smirnoff 			ifv = trunk->vlans[i];
160675ee267cSGleb Smirnoff #else
160775ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
160875ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
160975ee267cSGleb Smirnoff #endif
161075ee267cSGleb Smirnoff 			vlan_capabilities(ifv);
161175ee267cSGleb Smirnoff 	}
161275ee267cSGleb Smirnoff 	TRUNK_UNLOCK(trunk);
1613127d7b2dSAndre Oppermann }
1614127d7b2dSAndre Oppermann 
1615a3814acfSSam Leffler static int
1616cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
16172cc2df49SGarrett Wollman {
16182cc2df49SGarrett Wollman 	struct ifnet *p;
16192cc2df49SGarrett Wollman 	struct ifreq *ifr;
1620e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
16212cc2df49SGarrett Wollman 	struct ifvlan *ifv;
16222d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
16232cc2df49SGarrett Wollman 	struct vlanreq vlr;
16242cc2df49SGarrett Wollman 	int error = 0;
16252cc2df49SGarrett Wollman 
16262cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1627e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
16282cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
16292cc2df49SGarrett Wollman 
16302cc2df49SGarrett Wollman 	switch (cmd) {
1631e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1632e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1633e4cd31ddSJeff Roberson #ifdef INET
1634e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1635e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1636e4cd31ddSJeff Roberson #endif
1637e4cd31ddSJeff Roberson 		break;
1638e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
1639e4cd31ddSJeff Roberson                 {
1640e4cd31ddSJeff Roberson 			struct sockaddr *sa;
1641e4cd31ddSJeff Roberson 
1642e4cd31ddSJeff Roberson 			sa = (struct sockaddr *)&ifr->ifr_data;
1643e4cd31ddSJeff Roberson 			bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen);
1644e4cd31ddSJeff Roberson                 }
1645e4cd31ddSJeff Roberson 		break;
1646b3cca108SBill Fenner 	case SIOCGIFMEDIA:
16474faedfe8SSam Leffler 		VLAN_LOCK();
164875ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1649d8564efdSEd Maste 			p = PARENT(ifv);
16504faedfe8SSam Leffler 			VLAN_UNLOCK();
1651d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
1652b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1653b3cca108SBill Fenner 			if (error == 0) {
1654b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1655b3cca108SBill Fenner 
1656b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1657b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1658b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1659b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1660b3cca108SBill Fenner 						ifmr->ifm_ulist,
1661b3cca108SBill Fenner 						sizeof(int));
1662b3cca108SBill Fenner 				}
1663b3cca108SBill Fenner 			}
16644faedfe8SSam Leffler 		} else {
16654faedfe8SSam Leffler 			VLAN_UNLOCK();
1666b3cca108SBill Fenner 			error = EINVAL;
16674faedfe8SSam Leffler 		}
1668b3cca108SBill Fenner 		break;
1669b3cca108SBill Fenner 
1670b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1671b3cca108SBill Fenner 		error = EINVAL;
1672b3cca108SBill Fenner 		break;
1673b3cca108SBill Fenner 
16742cc2df49SGarrett Wollman 	case SIOCSIFMTU:
16752cc2df49SGarrett Wollman 		/*
16762cc2df49SGarrett Wollman 		 * Set the interface MTU.
16772cc2df49SGarrett Wollman 		 */
16784faedfe8SSam Leffler 		VLAN_LOCK();
167975ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1680a3814acfSSam Leffler 			if (ifr->ifr_mtu >
168175ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1682a3814acfSSam Leffler 			    ifr->ifr_mtu <
1683a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
16842cc2df49SGarrett Wollman 				error = EINVAL;
1685a3814acfSSam Leffler 			else
16862cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1687a3814acfSSam Leffler 		} else
1688a3814acfSSam Leffler 			error = EINVAL;
16894faedfe8SSam Leffler 		VLAN_UNLOCK();
16902cc2df49SGarrett Wollman 		break;
16912cc2df49SGarrett Wollman 
16922cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1693ccf7ba97SMarko Zec #ifdef VIMAGE
169415f6780eSRobert Watson 		/*
169515f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
169615f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
169715f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
169815f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
16995a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
170015f6780eSRobert Watson 		 */
1701ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1702ccf7ba97SMarko Zec 			error = EPERM;
1703ccf7ba97SMarko Zec 			break;
1704ccf7ba97SMarko Zec 		}
1705ccf7ba97SMarko Zec #endif
170615a66c21SBruce M Simpson 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
17072cc2df49SGarrett Wollman 		if (error)
17082cc2df49SGarrett Wollman 			break;
17092cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1710f731f104SBill Paul 			vlan_unconfig(ifp);
17112cc2df49SGarrett Wollman 			break;
17122cc2df49SGarrett Wollman 		}
17132cc2df49SGarrett Wollman 		p = ifunit(vlr.vlr_parent);
17141bdc73d3SEd Maste 		if (p == NULL) {
17152cc2df49SGarrett Wollman 			error = ENOENT;
17162cc2df49SGarrett Wollman 			break;
17172cc2df49SGarrett Wollman 		}
1718fb88a3e0SBill Paul 		/*
17197983103aSRobert Watson 		 * Don't let the caller set up a VLAN VID with
1720fb88a3e0SBill Paul 		 * anything except VLID bits.
1721fb88a3e0SBill Paul 		 */
1722fb88a3e0SBill Paul 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
1723fb88a3e0SBill Paul 			error = EINVAL;
1724fb88a3e0SBill Paul 			break;
1725fb88a3e0SBill Paul 		}
172675ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
172775ee267cSGleb Smirnoff 		if (error)
17282cc2df49SGarrett Wollman 			break;
1729a3814acfSSam Leffler 
17301cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
17311cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
17322cc2df49SGarrett Wollman 		break;
17332cc2df49SGarrett Wollman 
17342cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1735ccf7ba97SMarko Zec #ifdef VIMAGE
1736ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1737ccf7ba97SMarko Zec 			error = EPERM;
1738ccf7ba97SMarko Zec 			break;
1739ccf7ba97SMarko Zec 		}
1740ccf7ba97SMarko Zec #endif
174115a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
17424faedfe8SSam Leffler 		VLAN_LOCK();
174375ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
174475ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
17459bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
17467983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
17472cc2df49SGarrett Wollman 		}
17484faedfe8SSam Leffler 		VLAN_UNLOCK();
174915a66c21SBruce M Simpson 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
17502cc2df49SGarrett Wollman 		break;
17512cc2df49SGarrett Wollman 
17522cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
17532cc2df49SGarrett Wollman 		/*
17541cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
17551cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
17562cc2df49SGarrett Wollman 		 */
175775ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
17581cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
17592cc2df49SGarrett Wollman 		break;
1760a3814acfSSam Leffler 
1761f731f104SBill Paul 	case SIOCADDMULTI:
1762f731f104SBill Paul 	case SIOCDELMULTI:
176375ee267cSGleb Smirnoff 		/*
176475ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
176575ee267cSGleb Smirnoff 		 * when we do.
176675ee267cSGleb Smirnoff 		 */
17672d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
17682d222cb7SAlexander Motin 		if (trunk != NULL) {
17692d222cb7SAlexander Motin 			TRUNK_LOCK(trunk);
1770f731f104SBill Paul 			error = vlan_setmulti(ifp);
17712d222cb7SAlexander Motin 			TRUNK_UNLOCK(trunk);
17722d222cb7SAlexander Motin 		}
1773f731f104SBill Paul 		break;
177475ee267cSGleb Smirnoff 
17752cc2df49SGarrett Wollman 	default:
1776e4cd31ddSJeff Roberson 		error = EINVAL;
1777e4cd31ddSJeff Roberson 		break;
17782cc2df49SGarrett Wollman 	}
177915a66c21SBruce M Simpson 
178015a66c21SBruce M Simpson 	return (error);
17812cc2df49SGarrett Wollman }
1782