xref: /freebsd/sys/net/if_vlan.c (revision 4be465ab468ad37003826f11ad3465db5168c95d)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32ccbbd06SMarcelo Araujo  * Copyright 2012 ADARA Networks, Inc.
4d148c2a2SMatt Joras  * Copyright 2017 Dell EMC Isilon
52ccbbd06SMarcelo Araujo  *
62ccbbd06SMarcelo Araujo  * Portions of this software were developed by Robert N. M. Watson under
72ccbbd06SMarcelo Araujo  * contract to ADARA Networks, Inc.
82cc2df49SGarrett Wollman  *
92cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
102cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
112cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
122cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
132cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
142cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
152cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
162cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
172cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
182cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
192cc2df49SGarrett Wollman  * warranty.
202cc2df49SGarrett Wollman  *
212cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
222cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
232cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
242cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
252cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
262cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
272cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
282cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
292cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
302cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
312cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322cc2df49SGarrett Wollman  * SUCH DAMAGE.
332cc2df49SGarrett Wollman  */
342cc2df49SGarrett Wollman 
352cc2df49SGarrett Wollman /*
362cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
372ccbbd06SMarcelo Araujo  * This is sort of sneaky in the implementation, since
382cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
392cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
402cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
41d9b1d615SJohn Baldwin  * ether_output() sends to us via if_transmit(), rewrite them for
42d9b1d615SJohn Baldwin  * use by the real outgoing interface, and ask it to send them.
432cc2df49SGarrett Wollman  */
442cc2df49SGarrett Wollman 
458b2d9181SPyun YongHyeon #include <sys/cdefs.h>
468b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$");
478b2d9181SPyun YongHyeon 
482c5b403eSOleg Bulyzhin #include "opt_inet.h"
4984becee1SAlexander Motin #include "opt_inet6.h"
50b2e60773SJohn Baldwin #include "opt_kern_tls.h"
5175ee267cSGleb Smirnoff #include "opt_vlan.h"
52f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
532cc2df49SGarrett Wollman 
542cc2df49SGarrett Wollman #include <sys/param.h>
55c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
562cc2df49SGarrett Wollman #include <sys/kernel.h>
5775ee267cSGleb Smirnoff #include <sys/lock.h>
58f731f104SBill Paul #include <sys/malloc.h>
592cc2df49SGarrett Wollman #include <sys/mbuf.h>
602b120974SPeter Wemm #include <sys/module.h>
61772b000fSAlexander V. Chernikov #include <sys/rmlock.h>
622ccbbd06SMarcelo Araujo #include <sys/priv.h>
63f731f104SBill Paul #include <sys/queue.h>
642cc2df49SGarrett Wollman #include <sys/socket.h>
652cc2df49SGarrett Wollman #include <sys/sockio.h>
662cc2df49SGarrett Wollman #include <sys/sysctl.h>
672cc2df49SGarrett Wollman #include <sys/systm.h>
68e4cd31ddSJeff Roberson #include <sys/sx.h>
69d148c2a2SMatt Joras #include <sys/taskqueue.h>
702cc2df49SGarrett Wollman 
712cc2df49SGarrett Wollman #include <net/bpf.h>
722cc2df49SGarrett Wollman #include <net/ethernet.h>
732cc2df49SGarrett Wollman #include <net/if.h>
7476039bc8SGleb Smirnoff #include <net/if_var.h>
75f889d2efSBrooks Davis #include <net/if_clone.h>
762cc2df49SGarrett Wollman #include <net/if_dl.h>
772cc2df49SGarrett Wollman #include <net/if_types.h>
782cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
7984becee1SAlexander Motin #include <net/route.h>
804b79449eSBjoern A. Zeeb #include <net/vnet.h>
812cc2df49SGarrett Wollman 
822c5b403eSOleg Bulyzhin #ifdef INET
832c5b403eSOleg Bulyzhin #include <netinet/in.h>
842c5b403eSOleg Bulyzhin #include <netinet/if_ether.h>
852c5b403eSOleg Bulyzhin #endif
862c5b403eSOleg Bulyzhin 
8784becee1SAlexander Motin #ifdef INET6
8884becee1SAlexander Motin /*
8984becee1SAlexander Motin  * XXX: declare here to avoid to include many inet6 related files..
9084becee1SAlexander Motin  * should be more generalized?
9184becee1SAlexander Motin  */
9284becee1SAlexander Motin extern void	nd6_setmtu(struct ifnet *);
9384becee1SAlexander Motin #endif
9484becee1SAlexander Motin 
9575ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
9664a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
9775ee267cSGleb Smirnoff 
982dc879b3SYaroslav Tykhiy #define	UP_AND_RUNNING(ifp) \
992dc879b3SYaroslav Tykhiy     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
1002dc879b3SYaroslav Tykhiy 
101b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan);
10275ee267cSGleb Smirnoff 
10375ee267cSGleb Smirnoff struct ifvlantrunk {
10475ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
105b08d611dSMatt Macy 	struct	mtx	lock;
10675ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
1075cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
1085cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
10975ee267cSGleb Smirnoff #else
11075ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
11175ee267cSGleb Smirnoff 	uint16_t	hmask;
11275ee267cSGleb Smirnoff 	uint16_t	hwidth;
11375ee267cSGleb Smirnoff #endif
11475ee267cSGleb Smirnoff 	int		refcnt;
11575ee267cSGleb Smirnoff };
1169d4fe4b2SBrooks Davis 
117b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
118fb3bc596SJohn Baldwin struct vlan_snd_tag {
119fb3bc596SJohn Baldwin 	struct m_snd_tag com;
120fb3bc596SJohn Baldwin 	struct m_snd_tag *tag;
121fb3bc596SJohn Baldwin };
122fb3bc596SJohn Baldwin 
123fb3bc596SJohn Baldwin static inline struct vlan_snd_tag *
124fb3bc596SJohn Baldwin mst_to_vst(struct m_snd_tag *mst)
125fb3bc596SJohn Baldwin {
126fb3bc596SJohn Baldwin 
127fb3bc596SJohn Baldwin 	return (__containerof(mst, struct vlan_snd_tag, com));
128fb3bc596SJohn Baldwin }
129fb3bc596SJohn Baldwin #endif
130fb3bc596SJohn Baldwin 
131d148c2a2SMatt Joras /*
132d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk with
133d148c2a2SMatt Joras  * the assumption that none will be added/removed during iteration.
134d148c2a2SMatt Joras  */
135d148c2a2SMatt Joras #ifdef VLAN_ARRAY
136d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
137d148c2a2SMatt Joras 	size_t _i; \
138d148c2a2SMatt Joras 	for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
139d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
140d148c2a2SMatt Joras #else /* VLAN_ARRAY */
141d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
142d148c2a2SMatt Joras 	struct ifvlan *_next; \
143d148c2a2SMatt Joras 	size_t _i; \
144d148c2a2SMatt Joras 	for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
145b08d611dSMatt Macy 		CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
146d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
147d148c2a2SMatt Joras 
148d148c2a2SMatt Joras /*
149d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk while
150d148c2a2SMatt Joras  * also modifying the number of vlans on the trunk. The iteration continues
151d148c2a2SMatt Joras  * until some condition is met or there are no more vlans on the trunk.
152d148c2a2SMatt Joras  */
153d148c2a2SMatt Joras #ifdef VLAN_ARRAY
154d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
155d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
156d148c2a2SMatt Joras 	size_t _i; \
157d148c2a2SMatt Joras 	for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
158d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]))
159d148c2a2SMatt Joras #else /* VLAN_ARRAY */
160d148c2a2SMatt Joras /*
161d148c2a2SMatt Joras  * The hash table case is more complicated. We allow for the hash table to be
162d148c2a2SMatt Joras  * modified (i.e. vlans removed) while we are iterating over it. To allow for
163d148c2a2SMatt Joras  * this we must restart the iteration every time we "touch" something during
164d148c2a2SMatt Joras  * the iteration, since removal will resize the hash table and invalidate our
165d148c2a2SMatt Joras  * current position. If acting on the touched element causes the trunk to be
166d148c2a2SMatt Joras  * emptied, then iteration also stops.
167d148c2a2SMatt Joras  */
168d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
169d148c2a2SMatt Joras 	size_t _i; \
170d148c2a2SMatt Joras 	bool _touch = false; \
171d148c2a2SMatt Joras 	for (_i = 0; \
172d148c2a2SMatt Joras 	    !(_cond) && _i < (1 << (_trunk)->hwidth); \
173d148c2a2SMatt Joras 	    _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
174b08d611dSMatt Macy 		if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
175d148c2a2SMatt Joras 		    (_touch = true))
176d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
177d148c2a2SMatt Joras 
178a3814acfSSam Leffler struct vlan_mc_entry {
179e4cd31ddSJeff Roberson 	struct sockaddr_dl		mc_addr;
180b08d611dSMatt Macy 	CK_SLIST_ENTRY(vlan_mc_entry)	mc_entries;
181c32a9d66SHans Petter Selasky 	struct epoch_context		mc_epoch_ctx;
182a3814acfSSam Leffler };
183a3814acfSSam Leffler 
184a3814acfSSam Leffler struct ifvlan {
18575ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
186fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
18775ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
18875ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
1896667db31SAlexander V. Chernikov 	void	*ifv_cookie;
1901cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
191d89baa5aSAlexander Motin 	int	ifv_capenable;
19272755d28SMark Johnston 	int	ifv_encaplen;	/* encapsulation length */
19372755d28SMark Johnston 	int	ifv_mtufudge;	/* MTU fudged by this much */
19472755d28SMark Johnston 	int	ifv_mintu;	/* min transmission unit */
19572755d28SMark Johnston 	uint16_t ifv_proto;	/* encapsulation ethertype */
19672755d28SMark Johnston 	uint16_t ifv_tag;	/* tag to apply on packets leaving if */
19772755d28SMark Johnston 	uint16_t ifv_vid;	/* VLAN ID */
19872755d28SMark Johnston 	uint8_t	ifv_pcp;	/* Priority Code Point (PCP). */
199d148c2a2SMatt Joras 	struct task lladdr_task;
200b08d611dSMatt Macy 	CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
201c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY
202b08d611dSMatt Macy 	CK_SLIST_ENTRY(ifvlan) ifv_list;
203c0cb022bSYaroslav Tykhiy #endif
204a3814acfSSam Leffler };
205a3814acfSSam Leffler 
20675ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
2071cf236fbSYaroslav Tykhiy static struct {
2081cf236fbSYaroslav Tykhiy 	int flag;
2091cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
2101cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
2111cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
2121cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
2131cf236fbSYaroslav Tykhiy 	{0, NULL}
2141cf236fbSYaroslav Tykhiy };
215a3814acfSSam Leffler 
216f1379734SKonstantin Belousov extern int vlan_mtag_pcp;
2172ccbbd06SMarcelo Araujo 
21842a58907SGleb Smirnoff static const char vlanname[] = "vlan";
21942a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
2202cc2df49SGarrett Wollman 
2215cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
222ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag;
2235cb8c31aSYaroslav Tykhiy 
2244faedfe8SSam Leffler /*
225b08d611dSMatt Macy  * if_vlan uses two module-level synchronizations primitives to allow concurrent
226b08d611dSMatt Macy  * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
227b08d611dSMatt Macy  * while they are being used for tx/rx. To accomplish this in a way that has
228b08d611dSMatt Macy  * acceptable performance and cooperation with other parts of the network stack
229b08d611dSMatt Macy  * there is a non-sleepable epoch(9) and an sx(9).
23075ee267cSGleb Smirnoff  *
231b08d611dSMatt Macy  * The performance-sensitive paths that warrant using the epoch(9) are
232d148c2a2SMatt Joras  * vlan_transmit and vlan_input. Both have to check for the vlan interface's
233d148c2a2SMatt Joras  * existence using if_vlantrunk, and being in the network tx/rx paths the use
234b08d611dSMatt Macy  * of an epoch(9) gives a measureable improvement in performance.
23575ee267cSGleb Smirnoff  *
236d148c2a2SMatt Joras  * The reason for having an sx(9) is mostly because there are still areas that
237d148c2a2SMatt Joras  * must be sleepable and also have safe concurrent access to a vlan interface.
238d148c2a2SMatt Joras  * Since the sx(9) exists, it is used by default in most paths unless sleeping
239d148c2a2SMatt Joras  * is not permitted, or if it is not clear whether sleeping is permitted.
240d148c2a2SMatt Joras  *
2414faedfe8SSam Leffler  */
242d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx
243d148c2a2SMatt Joras 
244d148c2a2SMatt Joras static struct sx _VLAN_SX_ID;
245d148c2a2SMatt Joras 
246d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \
247d148c2a2SMatt Joras 	sx_init(&_VLAN_SX_ID, "vlan_sx")
248d148c2a2SMatt Joras 
249d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \
250d148c2a2SMatt Joras 	sx_destroy(&_VLAN_SX_ID)
251d148c2a2SMatt Joras 
252d148c2a2SMatt Joras #define	VLAN_SLOCK()			sx_slock(&_VLAN_SX_ID)
253d148c2a2SMatt Joras #define	VLAN_SUNLOCK()			sx_sunlock(&_VLAN_SX_ID)
254d148c2a2SMatt Joras #define	VLAN_XLOCK()			sx_xlock(&_VLAN_SX_ID)
255d148c2a2SMatt Joras #define	VLAN_XUNLOCK()			sx_xunlock(&_VLAN_SX_ID)
256d148c2a2SMatt Joras #define	VLAN_SLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
257d148c2a2SMatt Joras #define	VLAN_XLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
258d148c2a2SMatt Joras #define	VLAN_SXLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_LOCKED)
259d148c2a2SMatt Joras 
260d148c2a2SMatt Joras /*
261b08d611dSMatt Macy  * We also have a per-trunk mutex that should be acquired when changing
262b08d611dSMatt Macy  * its state.
263d148c2a2SMatt Joras  */
264b08d611dSMatt Macy #define	TRUNK_LOCK_INIT(trunk)		mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
265b08d611dSMatt Macy #define	TRUNK_LOCK_DESTROY(trunk)	mtx_destroy(&(trunk)->lock)
266b08d611dSMatt Macy #define	TRUNK_WLOCK(trunk)		mtx_lock(&(trunk)->lock)
267b08d611dSMatt Macy #define	TRUNK_WUNLOCK(trunk)		mtx_unlock(&(trunk)->lock)
268b08d611dSMatt Macy #define	TRUNK_WLOCK_ASSERT(trunk)	mtx_assert(&(trunk)->lock, MA_OWNED);
26975ee267cSGleb Smirnoff 
270d148c2a2SMatt Joras /*
271d148c2a2SMatt Joras  * The VLAN_ARRAY substitutes the dynamic hash with a static array
272d148c2a2SMatt Joras  * with 4096 entries. In theory this can give a boost in processing,
273d148c2a2SMatt Joras  * however in practice it does not. Probably this is because the array
274d148c2a2SMatt Joras  * is too big to fit into CPU cache.
275d148c2a2SMatt Joras  */
27675ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
27775ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
27875ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
27975ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
28075ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
28175ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
28275ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
2837983103aSRobert Watson 	uint16_t vid);
28475ee267cSGleb Smirnoff #endif
28575ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
2864faedfe8SSam Leffler 
287114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
288a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
289cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
290b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
291f3e7afe2SHans Petter Selasky static	int vlan_snd_tag_alloc(struct ifnet *,
292f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *, struct m_snd_tag **);
293fb3bc596SJohn Baldwin static	int vlan_snd_tag_modify(struct m_snd_tag *,
294fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *);
295fb3bc596SJohn Baldwin static	int vlan_snd_tag_query(struct m_snd_tag *,
296fb3bc596SJohn Baldwin     union if_snd_tag_query_params *);
297fa91f845SRandall Stewart static	void vlan_snd_tag_free(struct m_snd_tag *);
298f3e7afe2SHans Petter Selasky #endif
299d9b1d615SJohn Baldwin static	void vlan_qflush(struct ifnet *ifp);
3001cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
3011cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
3021cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
303f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
304d9b1d615SJohn Baldwin static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
30516cf6bdbSMatt Joras static	int vlan_output(struct ifnet *ifp, struct mbuf *m,
30616cf6bdbSMatt Joras     const struct sockaddr *dst, struct route *ro);
3076f359e28SJohn Baldwin static	void vlan_unconfig(struct ifnet *ifp);
30828cc4d37SJohn Baldwin static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
30975ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
310a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
31175ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
31275ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
313f731f104SBill Paul 
314f941c31aSGleb Smirnoff static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
315f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
3166b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
317f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
318f889d2efSBrooks Davis 
3195cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
320ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
3215cb8c31aSYaroslav Tykhiy 
322d148c2a2SMatt Joras static  void vlan_lladdr_fn(void *arg, int pending);
323d148c2a2SMatt Joras 
32442a58907SGleb Smirnoff static struct if_clone *vlan_cloner;
3259d4fe4b2SBrooks Davis 
326ccf7ba97SMarko Zec #ifdef VIMAGE
3275f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
328ccf7ba97SMarko Zec #define	V_vlan_cloner	VNET(vlan_cloner)
329ccf7ba97SMarko Zec #endif
330ccf7ba97SMarko Zec 
33175ee267cSGleb Smirnoff static void
332c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx)
333c32a9d66SHans Petter Selasky {
334c32a9d66SHans Petter Selasky 	struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
335c32a9d66SHans Petter Selasky 	free(mc, M_VLAN);
336c32a9d66SHans Petter Selasky }
337c32a9d66SHans Petter Selasky 
338cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY
339cac30248SOleg Bulyzhin #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
340cac30248SOleg Bulyzhin 
341c32a9d66SHans Petter Selasky static void
34275ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
34375ee267cSGleb Smirnoff {
34475ee267cSGleb Smirnoff 	int i, n;
34575ee267cSGleb Smirnoff 
34675ee267cSGleb Smirnoff 	/*
34775ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
34875ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
34975ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
35075ee267cSGleb Smirnoff 	 */
35175ee267cSGleb Smirnoff 
35275ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
35375ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
35475ee267cSGleb Smirnoff 
35575ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
35675ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
35775ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
35875ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
35975ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
360b08d611dSMatt Macy 		CK_SLIST_INIT(&trunk->hash[i]);
36175ee267cSGleb Smirnoff }
36275ee267cSGleb Smirnoff 
36375ee267cSGleb Smirnoff static void
36475ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
36575ee267cSGleb Smirnoff {
36675ee267cSGleb Smirnoff #ifdef INVARIANTS
36775ee267cSGleb Smirnoff 	int i;
36875ee267cSGleb Smirnoff 
36975ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
37075ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
371b08d611dSMatt Macy 		KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
37275ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
37375ee267cSGleb Smirnoff #endif
37475ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
37575ee267cSGleb Smirnoff 	trunk->hash = NULL;
37675ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
37775ee267cSGleb Smirnoff }
37875ee267cSGleb Smirnoff 
37975ee267cSGleb Smirnoff static int
38075ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
38175ee267cSGleb Smirnoff {
38275ee267cSGleb Smirnoff 	int i, b;
38375ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
38475ee267cSGleb Smirnoff 
385b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
38675ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
38775ee267cSGleb Smirnoff 
38875ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
3897983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
390b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
3917983103aSRobert Watson 		if (ifv->ifv_vid == ifv2->ifv_vid)
39275ee267cSGleb Smirnoff 			return (EEXIST);
39375ee267cSGleb Smirnoff 
39475ee267cSGleb Smirnoff 	/*
39575ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
39675ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
39775ee267cSGleb Smirnoff 	 * buckets/2.
39875ee267cSGleb Smirnoff 	 */
39975ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
40075ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
4017983103aSRobert Watson 		i = HASH(ifv->ifv_vid, trunk->hmask);
40275ee267cSGleb Smirnoff 	}
403b08d611dSMatt Macy 	CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
40475ee267cSGleb Smirnoff 	trunk->refcnt++;
40575ee267cSGleb Smirnoff 
40675ee267cSGleb Smirnoff 	return (0);
40775ee267cSGleb Smirnoff }
40875ee267cSGleb Smirnoff 
40975ee267cSGleb Smirnoff static int
41075ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
41175ee267cSGleb Smirnoff {
41275ee267cSGleb Smirnoff 	int i, b;
41375ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
41475ee267cSGleb Smirnoff 
415b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
41675ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
41775ee267cSGleb Smirnoff 
41875ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
4197983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
420b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
42175ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
42275ee267cSGleb Smirnoff 			trunk->refcnt--;
423b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
42475ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
42575ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
42675ee267cSGleb Smirnoff 			return (0);
42775ee267cSGleb Smirnoff 		}
42875ee267cSGleb Smirnoff 
42975ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
43075ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
43175ee267cSGleb Smirnoff }
43275ee267cSGleb Smirnoff 
43375ee267cSGleb Smirnoff /*
43475ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
43575ee267cSGleb Smirnoff  */
43675ee267cSGleb Smirnoff static void
43775ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
43875ee267cSGleb Smirnoff {
43975ee267cSGleb Smirnoff 	struct ifvlan *ifv;
44075ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
44175ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
44275ee267cSGleb Smirnoff 
443b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
44475ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
44575ee267cSGleb Smirnoff 
44675ee267cSGleb Smirnoff 	if (howmuch == 0) {
44775ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
44875ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
44975ee267cSGleb Smirnoff 		return;
45075ee267cSGleb Smirnoff 	}
45175ee267cSGleb Smirnoff 
45275ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
45375ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
45475ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
45575ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
45675ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
45775ee267cSGleb Smirnoff 		return;
45875ee267cSGleb Smirnoff 
459b08d611dSMatt Macy 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
46075ee267cSGleb Smirnoff 	if (hash2 == NULL) {
46175ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
46275ee267cSGleb Smirnoff 		    __func__);
46375ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
46475ee267cSGleb Smirnoff 	}
46575ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
466b08d611dSMatt Macy 		CK_SLIST_INIT(&hash2[j]);
46775ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
468b08d611dSMatt Macy 		while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
469b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
4707983103aSRobert Watson 			j = HASH(ifv->ifv_vid, n2 - 1);
471b08d611dSMatt Macy 			CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
47275ee267cSGleb Smirnoff 		}
473b08d611dSMatt Macy 	NET_EPOCH_WAIT();
47475ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
47575ee267cSGleb Smirnoff 	trunk->hash = hash2;
47675ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
47775ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
478f84b2d69SYaroslav Tykhiy 
479f84b2d69SYaroslav Tykhiy 	if (bootverbose)
480f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
481f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
48275ee267cSGleb Smirnoff }
48375ee267cSGleb Smirnoff 
48475ee267cSGleb Smirnoff static __inline struct ifvlan *
4857983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
48675ee267cSGleb Smirnoff {
48775ee267cSGleb Smirnoff 	struct ifvlan *ifv;
48875ee267cSGleb Smirnoff 
489a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
49075ee267cSGleb Smirnoff 
491b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
4927983103aSRobert Watson 		if (ifv->ifv_vid == vid)
49375ee267cSGleb Smirnoff 			return (ifv);
49475ee267cSGleb Smirnoff 	return (NULL);
49575ee267cSGleb Smirnoff }
49675ee267cSGleb Smirnoff 
49775ee267cSGleb Smirnoff #if 0
49875ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
49975ee267cSGleb Smirnoff static void
50075ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
50175ee267cSGleb Smirnoff {
50275ee267cSGleb Smirnoff 	int i;
50375ee267cSGleb Smirnoff 	struct ifvlan *ifv;
50475ee267cSGleb Smirnoff 
50575ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
50675ee267cSGleb Smirnoff 		printf("%d: ", i);
507b08d611dSMatt Macy 		CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
50875ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
50975ee267cSGleb Smirnoff 		printf("\n");
51075ee267cSGleb Smirnoff 	}
51175ee267cSGleb Smirnoff }
51275ee267cSGleb Smirnoff #endif /* 0 */
513e4cd31ddSJeff Roberson #else
514e4cd31ddSJeff Roberson 
515e4cd31ddSJeff Roberson static __inline struct ifvlan *
5167983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
517e4cd31ddSJeff Roberson {
518e4cd31ddSJeff Roberson 
5197983103aSRobert Watson 	return trunk->vlans[vid];
520e4cd31ddSJeff Roberson }
521e4cd31ddSJeff Roberson 
522e4cd31ddSJeff Roberson static __inline int
523e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
524e4cd31ddSJeff Roberson {
525e4cd31ddSJeff Roberson 
5267983103aSRobert Watson 	if (trunk->vlans[ifv->ifv_vid] != NULL)
527e4cd31ddSJeff Roberson 		return EEXIST;
5287983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = ifv;
529e4cd31ddSJeff Roberson 	trunk->refcnt++;
530e4cd31ddSJeff Roberson 
531e4cd31ddSJeff Roberson 	return (0);
532e4cd31ddSJeff Roberson }
533e4cd31ddSJeff Roberson 
534e4cd31ddSJeff Roberson static __inline int
535e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
536e4cd31ddSJeff Roberson {
537e4cd31ddSJeff Roberson 
5387983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = NULL;
539e4cd31ddSJeff Roberson 	trunk->refcnt--;
540e4cd31ddSJeff Roberson 
541e4cd31ddSJeff Roberson 	return (0);
542e4cd31ddSJeff Roberson }
543e4cd31ddSJeff Roberson 
544e4cd31ddSJeff Roberson static __inline void
545e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk)
546e4cd31ddSJeff Roberson {
547e4cd31ddSJeff Roberson }
548e4cd31ddSJeff Roberson 
549e4cd31ddSJeff Roberson static __inline void
550e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk)
551e4cd31ddSJeff Roberson {
552e4cd31ddSJeff Roberson }
553e4cd31ddSJeff Roberson 
55475ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
55575ee267cSGleb Smirnoff 
55675ee267cSGleb Smirnoff static void
55775ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
55875ee267cSGleb Smirnoff {
559d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
56075ee267cSGleb Smirnoff 
56175ee267cSGleb Smirnoff 	vlan_freehash(trunk);
56275ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
56333499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
5649bcf3ae4SAlexander Motin 	if_rele(trunk->parent);
56575ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
56675ee267cSGleb Smirnoff }
56775ee267cSGleb Smirnoff 
568f731f104SBill Paul /*
569f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
570f731f104SBill Paul  * programming the multicast filter of the parent. This has the
571f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
572f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
573f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
574f731f104SBill Paul  * to avoid this: there really is only one physical interface.
575f731f104SBill Paul  */
5762b120974SPeter Wemm static int
5772b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
578f731f104SBill Paul {
579f731f104SBill Paul 	struct ifnet		*ifp_p;
5802d222cb7SAlexander Motin 	struct ifmultiaddr	*ifma;
581f731f104SBill Paul 	struct ifvlan		*sc;
582c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
583f731f104SBill Paul 	int			error;
584f731f104SBill Paul 
585b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
586d148c2a2SMatt Joras 
587f731f104SBill Paul 	/* Find the parent. */
588f731f104SBill Paul 	sc = ifp->if_softc;
58975ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
5901b2a4f7aSBill Fenner 
5918b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
5928b615593SMarko Zec 
593f731f104SBill Paul 	/* First, remove any existing filter entries. */
594b08d611dSMatt Macy 	while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
595b08d611dSMatt Macy 		CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
5962d222cb7SAlexander Motin 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
5972a4bd982SGleb Smirnoff 		NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
598f731f104SBill Paul 	}
599f731f104SBill Paul 
600f731f104SBill Paul 	/* Now program new ones. */
6012d222cb7SAlexander Motin 	IF_ADDR_WLOCK(ifp);
602d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
603f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
604f731f104SBill Paul 			continue;
60529c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
6062d222cb7SAlexander Motin 		if (mc == NULL) {
6072d222cb7SAlexander Motin 			IF_ADDR_WUNLOCK(ifp);
60829c2dfbeSBruce M Simpson 			return (ENOMEM);
6092d222cb7SAlexander Motin 		}
610e4cd31ddSJeff Roberson 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
611e4cd31ddSJeff Roberson 		mc->mc_addr.sdl_index = ifp_p->if_index;
612b08d611dSMatt Macy 		CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
6132d222cb7SAlexander Motin 	}
6142d222cb7SAlexander Motin 	IF_ADDR_WUNLOCK(ifp);
615b08d611dSMatt Macy 	CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
616e4cd31ddSJeff Roberson 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
6172d222cb7SAlexander Motin 		    NULL);
618f731f104SBill Paul 		if (error)
619f731f104SBill Paul 			return (error);
620f731f104SBill Paul 	}
621f731f104SBill Paul 
6228b615593SMarko Zec 	CURVNET_RESTORE();
623f731f104SBill Paul 	return (0);
624f731f104SBill Paul }
6252cc2df49SGarrett Wollman 
626a3814acfSSam Leffler /*
627ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
628ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
629ea4ca115SAndrew Thompson  * should also change it on all children vlans.
630ea4ca115SAndrew Thompson  */
631ea4ca115SAndrew Thompson static void
632ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
633ea4ca115SAndrew Thompson {
634a68cc388SGleb Smirnoff 	struct epoch_tracker et;
635ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
636d148c2a2SMatt Joras 	struct ifnet *ifv_ifp;
637d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
638d148c2a2SMatt Joras 	struct sockaddr_dl *sdl;
639ea4ca115SAndrew Thompson 
6407b7f772fSGleb Smirnoff 	/* Need the epoch since this is run on taskqueue_swi. */
641a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
642d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
643d148c2a2SMatt Joras 	if (trunk == NULL) {
644a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
645ea4ca115SAndrew Thompson 		return;
646d148c2a2SMatt Joras 	}
647ea4ca115SAndrew Thompson 
648ea4ca115SAndrew Thompson 	/*
649ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
650d148c2a2SMatt Joras 	 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
651d148c2a2SMatt Joras 	 * ioctl calls on the parent garbling the lladdr of the child vlan.
652ea4ca115SAndrew Thompson 	 */
653d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
654d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
655d148c2a2SMatt Joras 		/*
656d148c2a2SMatt Joras 		 * Copy new new lladdr into the ifv_ifp, enqueue a task
657d148c2a2SMatt Joras 		 * to actually call if_setlladdr. if_setlladdr needs to
658d148c2a2SMatt Joras 		 * be deferred to a taskqueue because it will call into
659d148c2a2SMatt Joras 		 * the if_vlan ioctl path and try to acquire the global
660d148c2a2SMatt Joras 		 * lock.
661d148c2a2SMatt Joras 		 */
662d148c2a2SMatt Joras 		ifv_ifp = ifv->ifv_ifp;
663d148c2a2SMatt Joras 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
664e4cd31ddSJeff Roberson 		    ifp->if_addrlen);
665d148c2a2SMatt Joras 		sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
666d148c2a2SMatt Joras 		sdl->sdl_alen = ifp->if_addrlen;
667d148c2a2SMatt Joras 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
6686117727bSAndrew Thompson 	}
669d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
670a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
671ea4ca115SAndrew Thompson }
672ea4ca115SAndrew Thompson 
673ea4ca115SAndrew Thompson /*
6745cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
6755cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
6765cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
6775428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
6785428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
6795cb8c31aSYaroslav Tykhiy  */
6805cb8c31aSYaroslav Tykhiy static void
6815cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
6825cb8c31aSYaroslav Tykhiy {
6835cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
684d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
6855cb8c31aSYaroslav Tykhiy 
6865428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
6875428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
6885428776eSJohn Baldwin 		return;
689d148c2a2SMatt Joras 	VLAN_XLOCK();
690d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
691d148c2a2SMatt Joras 	if (trunk == NULL) {
692d148c2a2SMatt Joras 		VLAN_XUNLOCK();
693d148c2a2SMatt Joras 		return;
694d148c2a2SMatt Joras 	}
6955428776eSJohn Baldwin 
6965cb8c31aSYaroslav Tykhiy 	/*
6975cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
6985cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
6995cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
7005cb8c31aSYaroslav Tykhiy 	 */
701d148c2a2SMatt Joras 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
702d148c2a2SMatt Joras 	    ifp->if_vlantrunk == NULL)
70328cc4d37SJohn Baldwin 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
704d148c2a2SMatt Joras 
7055cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
7065cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
707d148c2a2SMatt Joras 	VLAN_XUNLOCK();
7085cb8c31aSYaroslav Tykhiy }
7095cb8c31aSYaroslav Tykhiy 
7105cb8c31aSYaroslav Tykhiy /*
711e4cd31ddSJeff Roberson  * Return the trunk device for a virtual interface.
712e4cd31ddSJeff Roberson  */
713e4cd31ddSJeff Roberson static struct ifnet  *
714e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp)
715e4cd31ddSJeff Roberson {
716e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
717e4cd31ddSJeff Roberson 
718b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
719b8a6e03fSGleb Smirnoff 
720e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
721e4cd31ddSJeff Roberson 		return (NULL);
722d148c2a2SMatt Joras 
723e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
724e4cd31ddSJeff Roberson 	ifp = NULL;
725e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
726e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
727e4cd31ddSJeff Roberson 	return (ifp);
728e4cd31ddSJeff Roberson }
729e4cd31ddSJeff Roberson 
730e4cd31ddSJeff Roberson /*
7317983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
7327983103aSRobert Watson  * components such as Infiniband.
7337983103aSRobert Watson  *
7347983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
7357983103aSRobert Watson  * vlan_vid().
736e4cd31ddSJeff Roberson  */
737e4cd31ddSJeff Roberson static int
7387983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
739e4cd31ddSJeff Roberson {
740e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
741e4cd31ddSJeff Roberson 
742e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
743e4cd31ddSJeff Roberson 		return (EINVAL);
744e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
7457983103aSRobert Watson 	*vidp = ifv->ifv_vid;
746e4cd31ddSJeff Roberson 	return (0);
747e4cd31ddSJeff Roberson }
748e4cd31ddSJeff Roberson 
74932d2623aSNavdeep Parhar static int
75032d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
75132d2623aSNavdeep Parhar {
75232d2623aSNavdeep Parhar 	struct ifvlan *ifv;
75332d2623aSNavdeep Parhar 
75432d2623aSNavdeep Parhar 	if (ifp->if_type != IFT_L2VLAN)
75532d2623aSNavdeep Parhar 		return (EINVAL);
75632d2623aSNavdeep Parhar 	ifv = ifp->if_softc;
75732d2623aSNavdeep Parhar 	*pcpp = ifv->ifv_pcp;
75832d2623aSNavdeep Parhar 	return (0);
75932d2623aSNavdeep Parhar }
76032d2623aSNavdeep Parhar 
761e4cd31ddSJeff Roberson /*
762e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
763e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
764e4cd31ddSJeff Roberson  */
765e4cd31ddSJeff Roberson static void *
766e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
767e4cd31ddSJeff Roberson {
768e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
769e4cd31ddSJeff Roberson 
770e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
771e4cd31ddSJeff Roberson 		return (NULL);
772e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
773e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
774e4cd31ddSJeff Roberson }
775e4cd31ddSJeff Roberson 
776e4cd31ddSJeff Roberson /*
777e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
778e4cd31ddSJeff Roberson  * private per-instance data in.
779e4cd31ddSJeff Roberson  */
780e4cd31ddSJeff Roberson static int
781e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
782e4cd31ddSJeff Roberson {
783e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
784e4cd31ddSJeff Roberson 
785e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
786e4cd31ddSJeff Roberson 		return (EINVAL);
787e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
788e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
789e4cd31ddSJeff Roberson 	return (0);
790e4cd31ddSJeff Roberson }
791e4cd31ddSJeff Roberson 
792e4cd31ddSJeff Roberson /*
7937983103aSRobert Watson  * Return the vlan device present at the specific VID.
794e4cd31ddSJeff Roberson  */
795e4cd31ddSJeff Roberson static struct ifnet *
7967983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
797e4cd31ddSJeff Roberson {
798e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
799e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
800e4cd31ddSJeff Roberson 
801b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
802b8a6e03fSGleb Smirnoff 
803e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
804b8a6e03fSGleb Smirnoff 	if (trunk == NULL)
805e4cd31ddSJeff Roberson 		return (NULL);
806e4cd31ddSJeff Roberson 	ifp = NULL;
8077983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
808e4cd31ddSJeff Roberson 	if (ifv)
809e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
810e4cd31ddSJeff Roberson 	return (ifp);
811e4cd31ddSJeff Roberson }
812e4cd31ddSJeff Roberson 
813e4cd31ddSJeff Roberson /*
8142ccbbd06SMarcelo Araujo  * Recalculate the cached VLAN tag exposed via the MIB.
8152ccbbd06SMarcelo Araujo  */
8162ccbbd06SMarcelo Araujo static void
8172ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv)
8182ccbbd06SMarcelo Araujo {
8192ccbbd06SMarcelo Araujo 
8202ccbbd06SMarcelo Araujo        ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
8212ccbbd06SMarcelo Araujo }
8222ccbbd06SMarcelo Araujo 
8232ccbbd06SMarcelo Araujo /*
824a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
825a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
826a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
827a3814acfSSam Leffler  * set here.  No one else in the system should be aware of this so
828a3814acfSSam Leffler  * we use an explicit reference here.
829a3814acfSSam Leffler  */
830a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
831a3814acfSSam Leffler 
832984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
833a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
834127d7b2dSAndre Oppermann 
8352b120974SPeter Wemm static int
8362b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
8372b120974SPeter Wemm {
8389d4fe4b2SBrooks Davis 
8392b120974SPeter Wemm 	switch (type) {
8402b120974SPeter Wemm 	case MOD_LOAD:
8415cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
8425cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
8435cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
8445cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
845ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
846ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
847ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
848ea4ca115SAndrew Thompson 			return (ENOMEM);
849d148c2a2SMatt Joras 		VLAN_LOCKING_INIT();
8509d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
851127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
85275ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
853e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
854e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
855e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
856e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
85732d2623aSNavdeep Parhar 		vlan_pcp_p = vlan_pcp;
858e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
859ccf7ba97SMarko Zec #ifndef VIMAGE
86042a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
86142a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
862ccf7ba97SMarko Zec #endif
86325c0f7b3SYaroslav Tykhiy 		if (bootverbose)
86425c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
86525c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
86625c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
86725c0f7b3SYaroslav Tykhiy #else
86825c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
86925c0f7b3SYaroslav Tykhiy #endif
87025c0f7b3SYaroslav Tykhiy 
87125c0f7b3SYaroslav Tykhiy 			       "\n");
8722b120974SPeter Wemm 		break;
8732b120974SPeter Wemm 	case MOD_UNLOAD:
874ccf7ba97SMarko Zec #ifndef VIMAGE
87542a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
876ccf7ba97SMarko Zec #endif
8775cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
878ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
8799d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
880127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
88175ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
882e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
883e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
88409fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
88509fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
886e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
887d148c2a2SMatt Joras 		VLAN_LOCKING_DESTROY();
88825c0f7b3SYaroslav Tykhiy 		if (bootverbose)
88925c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
8909d4fe4b2SBrooks Davis 		break;
8913e019deaSPoul-Henning Kamp 	default:
8923e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
8932b120974SPeter Wemm 	}
89415a66c21SBruce M Simpson 	return (0);
8952b120974SPeter Wemm }
8962b120974SPeter Wemm 
8972b120974SPeter Wemm static moduledata_t vlan_mod = {
8982b120974SPeter Wemm 	"if_vlan",
8992b120974SPeter Wemm 	vlan_modevent,
9009823d527SKevin Lo 	0
9012b120974SPeter Wemm };
9022b120974SPeter Wemm 
9032b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
90411edc477SEd Maste MODULE_VERSION(if_vlan, 3);
9052cc2df49SGarrett Wollman 
906ccf7ba97SMarko Zec #ifdef VIMAGE
907ccf7ba97SMarko Zec static void
908ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
909ccf7ba97SMarko Zec {
910ccf7ba97SMarko Zec 
91142a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
91242a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
913ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
914ccf7ba97SMarko Zec }
915ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
916ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
917ccf7ba97SMarko Zec 
918ccf7ba97SMarko Zec static void
919ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
920ccf7ba97SMarko Zec {
921ccf7ba97SMarko Zec 
92242a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
923ccf7ba97SMarko Zec }
92489856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
925ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
926ccf7ba97SMarko Zec #endif
927ccf7ba97SMarko Zec 
928f941c31aSGleb Smirnoff /*
929f941c31aSGleb Smirnoff  * Check for <etherif>.<vlan> style interface names.
930f941c31aSGleb Smirnoff  */
931f889d2efSBrooks Davis static struct ifnet *
932f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp)
9339d4fe4b2SBrooks Davis {
934f941c31aSGleb Smirnoff 	char ifname[IFNAMSIZ];
935f941c31aSGleb Smirnoff 	char *cp;
936f889d2efSBrooks Davis 	struct ifnet *ifp;
9377983103aSRobert Watson 	int vid;
938f889d2efSBrooks Davis 
939f941c31aSGleb Smirnoff 	strlcpy(ifname, name, IFNAMSIZ);
940f941c31aSGleb Smirnoff 	if ((cp = strchr(ifname, '.')) == NULL)
941f941c31aSGleb Smirnoff 		return (NULL);
942f941c31aSGleb Smirnoff 	*cp = '\0';
9439bcf3ae4SAlexander Motin 	if ((ifp = ifunit_ref(ifname)) == NULL)
944f941c31aSGleb Smirnoff 		return (NULL);
945f941c31aSGleb Smirnoff 	/* Parse VID. */
9469bcf3ae4SAlexander Motin 	if (*++cp == '\0') {
9479bcf3ae4SAlexander Motin 		if_rele(ifp);
948f941c31aSGleb Smirnoff 		return (NULL);
9499bcf3ae4SAlexander Motin 	}
9507983103aSRobert Watson 	vid = 0;
951fb92ad4aSJohn Baldwin 	for(; *cp >= '0' && *cp <= '9'; cp++)
9527983103aSRobert Watson 		vid = (vid * 10) + (*cp - '0');
9539bcf3ae4SAlexander Motin 	if (*cp != '\0') {
9549bcf3ae4SAlexander Motin 		if_rele(ifp);
955f941c31aSGleb Smirnoff 		return (NULL);
9569bcf3ae4SAlexander Motin 	}
9577983103aSRobert Watson 	if (vidp != NULL)
9587983103aSRobert Watson 		*vidp = vid;
959f889d2efSBrooks Davis 
96015a66c21SBruce M Simpson 	return (ifp);
961f889d2efSBrooks Davis }
962f889d2efSBrooks Davis 
963f889d2efSBrooks Davis static int
964f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
965f889d2efSBrooks Davis {
966*4be465abSAlexander V. Chernikov 	struct ifnet *ifp;
967f889d2efSBrooks Davis 	const char *cp;
968f889d2efSBrooks Davis 
969*4be465abSAlexander V. Chernikov 	ifp = vlan_clone_match_ethervid(name, NULL);
970*4be465abSAlexander V. Chernikov 	if (ifp != NULL) {
971*4be465abSAlexander V. Chernikov 		if_rele(ifp);
972f889d2efSBrooks Davis 		return (1);
973*4be465abSAlexander V. Chernikov 	}
974f889d2efSBrooks Davis 
97542a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
976f889d2efSBrooks Davis 		return (0);
977f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
978f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
979f889d2efSBrooks Davis 			return (0);
980f889d2efSBrooks Davis 	}
981f889d2efSBrooks Davis 
982f889d2efSBrooks Davis 	return (1);
983f889d2efSBrooks Davis }
984f889d2efSBrooks Davis 
985f889d2efSBrooks Davis static int
9866b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
987f889d2efSBrooks Davis {
988f889d2efSBrooks Davis 	char *dp;
989f889d2efSBrooks Davis 	int wildcard;
990f889d2efSBrooks Davis 	int unit;
991f889d2efSBrooks Davis 	int error;
9927983103aSRobert Watson 	int vid;
9939d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
9949d4fe4b2SBrooks Davis 	struct ifnet *ifp;
995f889d2efSBrooks Davis 	struct ifnet *p;
9963ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
9973ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
9986b7330e2SSam Leffler 	struct vlanreq vlr;
99965239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
1000f889d2efSBrooks Davis 
10016b7330e2SSam Leffler 	/*
10026b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
10036b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
10046b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
10056b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
10066b7330e2SSam Leffler 	 *   must be configured separately.
10076b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
1008a4641f4eSPedro F. Giffuni 	 * supported for backwards compatibility.
10097983103aSRobert Watson 	 *
10107983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
10117983103aSRobert Watson 	 * called for.
10126b7330e2SSam Leffler 	 */
10136b7330e2SSam Leffler 	if (params) {
10146b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
10156b7330e2SSam Leffler 		if (error)
10166b7330e2SSam Leffler 			return error;
10179bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
10186b7330e2SSam Leffler 		if (p == NULL)
1019b1828acfSGleb Smirnoff 			return (ENXIO);
10206b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
10219bcf3ae4SAlexander Motin 		if (error != 0) {
10229bcf3ae4SAlexander Motin 			if_rele(p);
10236b7330e2SSam Leffler 			return (error);
10249bcf3ae4SAlexander Motin 		}
10257983103aSRobert Watson 		vid = vlr.vlr_tag;
10266b7330e2SSam Leffler 		wildcard = (unit < 0);
1027f941c31aSGleb Smirnoff 	} else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1028f889d2efSBrooks Davis 		unit = -1;
1029f889d2efSBrooks Davis 		wildcard = 0;
1030f889d2efSBrooks Davis 	} else {
10319bcf3ae4SAlexander Motin 		p = NULL;
1032f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
1033f889d2efSBrooks Davis 		if (error != 0)
1034f889d2efSBrooks Davis 			return (error);
1035f889d2efSBrooks Davis 
1036f889d2efSBrooks Davis 		wildcard = (unit < 0);
1037f889d2efSBrooks Davis 	}
1038f889d2efSBrooks Davis 
1039f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
10409bcf3ae4SAlexander Motin 	if (error != 0) {
10419bcf3ae4SAlexander Motin 		if (p != NULL)
10429bcf3ae4SAlexander Motin 			if_rele(p);
1043f889d2efSBrooks Davis 		return (error);
10449bcf3ae4SAlexander Motin 	}
1045f889d2efSBrooks Davis 
1046f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
1047f889d2efSBrooks Davis 	if (wildcard) {
1048f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
1049f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1050f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
1051f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
1052f889d2efSBrooks Davis 		}
1053f889d2efSBrooks Davis 	}
10549d4fe4b2SBrooks Davis 
1055a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1056fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1057fc74a9f9SBrooks Davis 	if (ifp == NULL) {
1058fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
1059fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
10609bcf3ae4SAlexander Motin 		if (p != NULL)
10619bcf3ae4SAlexander Motin 			if_rele(p);
1062fc74a9f9SBrooks Davis 		return (ENOSPC);
1063fc74a9f9SBrooks Davis 	}
1064b08d611dSMatt Macy 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
10659d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
1066f889d2efSBrooks Davis 	/*
1067cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
1068f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
1069f889d2efSBrooks Davis 	 */
1070f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
107142a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
1072f889d2efSBrooks Davis 	ifp->if_dunit = unit;
10739d4fe4b2SBrooks Davis 
1074114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
1075d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
1076d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
10779d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
1078b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1079f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1080fb3bc596SJohn Baldwin 	ifp->if_snd_tag_modify = vlan_snd_tag_modify;
1081fb3bc596SJohn Baldwin 	ifp->if_snd_tag_query = vlan_snd_tag_query;
1082fa91f845SRandall Stewart 	ifp->if_snd_tag_free = vlan_snd_tag_free;
1083f3e7afe2SHans Petter Selasky #endif
108464a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
1085fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
10869d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
1087211f625aSBill Fenner 	ifp->if_baudrate = 0;
1088a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
1089a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
10903ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
10913ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
10923ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
10939d4fe4b2SBrooks Davis 
10949bcf3ae4SAlexander Motin 	if (p != NULL) {
10957983103aSRobert Watson 		error = vlan_config(ifv, p, vid);
10969bcf3ae4SAlexander Motin 		if_rele(p);
1097f889d2efSBrooks Davis 		if (error != 0) {
1098f889d2efSBrooks Davis 			/*
109928cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
1100f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
1101f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1102f889d2efSBrooks Davis 			 */
1103f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1104249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
11054b22573aSBrooks Davis 			if_free(ifp);
110696c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
1107f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1108f889d2efSBrooks Davis 
1109f889d2efSBrooks Davis 			return (error);
1110f889d2efSBrooks Davis 		}
1111f889d2efSBrooks Davis 	}
1112f889d2efSBrooks Davis 
11139d4fe4b2SBrooks Davis 	return (0);
11149d4fe4b2SBrooks Davis }
11159d4fe4b2SBrooks Davis 
1116f889d2efSBrooks Davis static int
1117f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
11189d4fe4b2SBrooks Davis {
11199d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1120114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
1121b4e9f837SBrooks Davis 
1122249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1123249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1124d148c2a2SMatt Joras 	/*
1125d148c2a2SMatt Joras 	 * We should have the only reference to the ifv now, so we can now
1126d148c2a2SMatt Joras 	 * drain any remaining lladdr task before freeing the ifnet and the
1127d148c2a2SMatt Joras 	 * ifvlan.
1128d148c2a2SMatt Joras 	 */
1129d148c2a2SMatt Joras 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1130b08d611dSMatt Macy 	NET_EPOCH_WAIT();
11314b22573aSBrooks Davis 	if_free(ifp);
11329d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1133b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
1134b4e9f837SBrooks Davis 
1135f889d2efSBrooks Davis 	return (0);
11369d4fe4b2SBrooks Davis }
11379d4fe4b2SBrooks Davis 
113815a66c21SBruce M Simpson /*
113915a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
114015a66c21SBruce M Simpson  */
11412cc2df49SGarrett Wollman static void
1142114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
11432cc2df49SGarrett Wollman {
11442cc2df49SGarrett Wollman }
11452cc2df49SGarrett Wollman 
11466d3a3ab7SGleb Smirnoff /*
1147d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
11486d3a3ab7SGleb Smirnoff  */
1149d9b1d615SJohn Baldwin static int
1150d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
11512cc2df49SGarrett Wollman {
11522cc2df49SGarrett Wollman 	struct ifvlan *ifv;
11532cc2df49SGarrett Wollman 	struct ifnet *p;
11541ad7a257SPyun YongHyeon 	int error, len, mcast;
11552cc2df49SGarrett Wollman 
1156b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1157b8a6e03fSGleb Smirnoff 
11582cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
1159d148c2a2SMatt Joras 	if (TRUNK(ifv) == NULL) {
1160d148c2a2SMatt Joras 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1161d148c2a2SMatt Joras 		m_freem(m);
1162d148c2a2SMatt Joras 		return (ENETDOWN);
1163d148c2a2SMatt Joras 	}
116475ee267cSGleb Smirnoff 	p = PARENT(ifv);
11651ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
11661ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
11672cc2df49SGarrett Wollman 
1168a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
11692cc2df49SGarrett Wollman 
1170b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1171fb3bc596SJohn Baldwin 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1172fb3bc596SJohn Baldwin 		struct vlan_snd_tag *vst;
1173fb3bc596SJohn Baldwin 		struct m_snd_tag *mst;
1174fb3bc596SJohn Baldwin 
1175fb3bc596SJohn Baldwin 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1176fb3bc596SJohn Baldwin 		mst = m->m_pkthdr.snd_tag;
1177fb3bc596SJohn Baldwin 		vst = mst_to_vst(mst);
1178fb3bc596SJohn Baldwin 		if (vst->tag->ifp != p) {
1179fb3bc596SJohn Baldwin 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1180fb3bc596SJohn Baldwin 			m_freem(m);
1181fb3bc596SJohn Baldwin 			return (EAGAIN);
1182fb3bc596SJohn Baldwin 		}
1183fb3bc596SJohn Baldwin 
1184fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1185fb3bc596SJohn Baldwin 		m_snd_tag_rele(mst);
1186fb3bc596SJohn Baldwin 	}
1187fb3bc596SJohn Baldwin #endif
1188fb3bc596SJohn Baldwin 
1189f731f104SBill Paul 	/*
1190d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
119124993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
119224993214SYaroslav Tykhiy 	 */
11932dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
1194a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1195d148c2a2SMatt Joras 		m_freem(m);
11968b20f6cfSHiroki Sato 		return (ENETDOWN);
119724993214SYaroslav Tykhiy 	}
119824993214SYaroslav Tykhiy 
1199f1379734SKonstantin Belousov 	if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1200a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1201d9b1d615SJohn Baldwin 		return (0);
12024af90a4dSMatthew N. Dodd 	}
12032cc2df49SGarrett Wollman 
12042cc2df49SGarrett Wollman 	/*
12052cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
12062cc2df49SGarrett Wollman 	 */
1207aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1208299153b5SAlexander V. Chernikov 	if (error == 0) {
1209a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1210a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1211a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
12121ad7a257SPyun YongHyeon 	} else
1213a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1214d9b1d615SJohn Baldwin 	return (error);
12152cc2df49SGarrett Wollman }
1216d9b1d615SJohn Baldwin 
121716cf6bdbSMatt Joras static int
121816cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
121916cf6bdbSMatt Joras     struct route *ro)
122016cf6bdbSMatt Joras {
122116cf6bdbSMatt Joras 	struct ifvlan *ifv;
122216cf6bdbSMatt Joras 	struct ifnet *p;
122316cf6bdbSMatt Joras 
1224b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1225b8a6e03fSGleb Smirnoff 
122616cf6bdbSMatt Joras 	ifv = ifp->if_softc;
122716cf6bdbSMatt Joras 	if (TRUNK(ifv) == NULL) {
122816cf6bdbSMatt Joras 		m_freem(m);
122916cf6bdbSMatt Joras 		return (ENETDOWN);
123016cf6bdbSMatt Joras 	}
123116cf6bdbSMatt Joras 	p = PARENT(ifv);
123216cf6bdbSMatt Joras 	return p->if_output(ifp, m, dst, ro);
123316cf6bdbSMatt Joras }
123416cf6bdbSMatt Joras 
123516cf6bdbSMatt Joras 
1236d9b1d615SJohn Baldwin /*
1237d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1238d9b1d615SJohn Baldwin  */
1239d9b1d615SJohn Baldwin static void
1240d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1241d9b1d615SJohn Baldwin {
1242f731f104SBill Paul }
1243f731f104SBill Paul 
1244a3814acfSSam Leffler static void
1245a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1246f731f104SBill Paul {
1247d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1248f731f104SBill Paul 	struct ifvlan *ifv;
12492ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
12502ccbbd06SMarcelo Araujo 	uint16_t vid, tag;
125175ee267cSGleb Smirnoff 
1252b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1253b8a6e03fSGleb Smirnoff 
1254d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1255d148c2a2SMatt Joras 	if (trunk == NULL) {
1256d148c2a2SMatt Joras 		m_freem(m);
1257d148c2a2SMatt Joras 		return;
1258d148c2a2SMatt Joras 	}
1259a3814acfSSam Leffler 
1260f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1261a3814acfSSam Leffler 		/*
126214e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1263a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1264a3814acfSSam Leffler 		 */
12652ccbbd06SMarcelo Araujo 		tag = m->m_pkthdr.ether_vtag;
12666ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1267a3814acfSSam Leffler 	} else {
126875ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
126975ee267cSGleb Smirnoff 
127014e98256SYaroslav Tykhiy 		/*
127114e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
127214e98256SYaroslav Tykhiy 		 */
1273a3814acfSSam Leffler 		switch (ifp->if_type) {
1274a3814acfSSam Leffler 		case IFT_ETHER:
1275a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1276a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1277a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1278a3814acfSSam Leffler 				return;
1279a3814acfSSam Leffler 			}
1280a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
12812ccbbd06SMarcelo Araujo 			tag = ntohs(evl->evl_tag);
1282db8b5973SYaroslav Tykhiy 
1283db8b5973SYaroslav Tykhiy 			/*
12842dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
12852dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
12862dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
12872dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1288db8b5973SYaroslav Tykhiy 			 */
12892dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
12902dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
12912dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1292a3814acfSSam Leffler 			break;
12932dc879b3SYaroslav Tykhiy 
1294a3814acfSSam Leffler 		default:
1295db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
129660c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
129760c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1298a3814acfSSam Leffler #endif
12993751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1300d148c2a2SMatt Joras 			m_freem(m);
130160c60618SYaroslav Tykhiy 			return;
1302a3814acfSSam Leffler 		}
13037a46ec8fSBrooks Davis 	}
13047a46ec8fSBrooks Davis 
13052ccbbd06SMarcelo Araujo 	vid = EVL_VLANOFTAG(tag);
13062ccbbd06SMarcelo Araujo 
13077983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
13082dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1309b08d611dSMatt Macy 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1310d148c2a2SMatt Joras 		m_freem(m);
131175ee267cSGleb Smirnoff 		return;
131275ee267cSGleb Smirnoff 	}
1313f731f104SBill Paul 
13142ccbbd06SMarcelo Araujo 	if (vlan_mtag_pcp) {
13152ccbbd06SMarcelo Araujo 		/*
13162ccbbd06SMarcelo Araujo 		 * While uncommon, it is possible that we will find a 802.1q
13172ccbbd06SMarcelo Araujo 		 * packet encapsulated inside another packet that also had an
13182ccbbd06SMarcelo Araujo 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
13192ccbbd06SMarcelo Araujo 		 * arriving over ethernet.  In that case, we replace the
13202ccbbd06SMarcelo Araujo 		 * existing 802.1q PCP m_tag value.
13212ccbbd06SMarcelo Araujo 		 */
13222ccbbd06SMarcelo Araujo 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
13232ccbbd06SMarcelo Araujo 		if (mtag == NULL) {
13242ccbbd06SMarcelo Araujo 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
13252ccbbd06SMarcelo Araujo 			    sizeof(uint8_t), M_NOWAIT);
13262ccbbd06SMarcelo Araujo 			if (mtag == NULL) {
13272ccbbd06SMarcelo Araujo 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1328d148c2a2SMatt Joras 				m_freem(m);
13292ccbbd06SMarcelo Araujo 				return;
13302ccbbd06SMarcelo Araujo 			}
13312ccbbd06SMarcelo Araujo 			m_tag_prepend(m, mtag);
13322ccbbd06SMarcelo Araujo 		}
13332ccbbd06SMarcelo Araujo 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
13342ccbbd06SMarcelo Araujo 	}
13352ccbbd06SMarcelo Araujo 
1336fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1337c0304424SGleb Smirnoff 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
13382cc2df49SGarrett Wollman 
1339a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1340fdbf1174SMatt Joras 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
13412cc2df49SGarrett Wollman }
13422cc2df49SGarrett Wollman 
1343d148c2a2SMatt Joras static void
1344d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused)
1345d148c2a2SMatt Joras {
1346d148c2a2SMatt Joras 	struct ifvlan *ifv;
1347d148c2a2SMatt Joras 	struct ifnet *ifp;
1348d148c2a2SMatt Joras 
1349d148c2a2SMatt Joras 	ifv = (struct ifvlan *)arg;
1350d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
13515191a3aeSKristof Provost 
13525191a3aeSKristof Provost 	CURVNET_SET(ifp->if_vnet);
13535191a3aeSKristof Provost 
1354d148c2a2SMatt Joras 	/* The ifv_ifp already has the lladdr copied in. */
1355d148c2a2SMatt Joras 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
13565191a3aeSKristof Provost 
13575191a3aeSKristof Provost 	CURVNET_RESTORE();
1358d148c2a2SMatt Joras }
1359d148c2a2SMatt Joras 
13602cc2df49SGarrett Wollman static int
13617983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
13622cc2df49SGarrett Wollman {
13636dcec895SGleb Smirnoff 	struct epoch_tracker et;
136475ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
13651cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
136675ee267cSGleb Smirnoff 	int error = 0;
13672cc2df49SGarrett Wollman 
1368b1828acfSGleb Smirnoff 	/*
1369b1828acfSGleb Smirnoff 	 * We can handle non-ethernet hardware types as long as
1370b1828acfSGleb Smirnoff 	 * they handle the tagging and headers themselves.
1371b1828acfSGleb Smirnoff 	 */
1372e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1373e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
137415a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
137564a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
137664a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
1377b1828acfSGleb Smirnoff 	/*
1378b1828acfSGleb Smirnoff 	 * Don't let the caller set up a VLAN VID with
1379b1828acfSGleb Smirnoff 	 * anything except VLID bits.
1380b1828acfSGleb Smirnoff 	 * VID numbers 0x0 and 0xFFF are reserved.
1381b1828acfSGleb Smirnoff 	 */
1382b1828acfSGleb Smirnoff 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1383b1828acfSGleb Smirnoff 		return (EINVAL);
138475ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
138515a66c21SBruce M Simpson 		return (EBUSY);
13862cc2df49SGarrett Wollman 
1387d148c2a2SMatt Joras 	VLAN_XLOCK();
138875ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
138975ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
139075ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
139175ee267cSGleb Smirnoff 		vlan_inithash(trunk);
139275ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
1393d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
139475ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
139575ee267cSGleb Smirnoff 		trunk->parent = p;
13969bcf3ae4SAlexander Motin 		if_ref(trunk->parent);
1397b08d611dSMatt Macy 		TRUNK_WUNLOCK(trunk);
139875ee267cSGleb Smirnoff 	} else {
139975ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
140075ee267cSGleb Smirnoff 	}
140175ee267cSGleb Smirnoff 
14027983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
14032ccbbd06SMarcelo Araujo 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
14042ccbbd06SMarcelo Araujo 	vlan_tag_recalculate(ifv);
140575ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
140675ee267cSGleb Smirnoff 	if (error)
140775ee267cSGleb Smirnoff 		goto done;
140873f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1409a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1410a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
14111cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1412d89baa5aSAlexander Motin 	ifv->ifv_capenable = -1;
1413a3814acfSSam Leffler 
1414a3814acfSSam Leffler 	/*
1415a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1416a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1417656acce4SYaroslav Tykhiy 	 * use it.
1418a3814acfSSam Leffler 	 */
1419656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1420656acce4SYaroslav Tykhiy 		/*
1421656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1422656acce4SYaroslav Tykhiy 		 * handle extended frames.
1423656acce4SYaroslav Tykhiy 		 */
1424a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1425656acce4SYaroslav Tykhiy 	} else {
1426a3814acfSSam Leffler 		/*
1427a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1428a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1429a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1430a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1431a3814acfSSam Leffler 		 * which might still be useful.
1432a3814acfSSam Leffler 		 */
1433a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1434a3814acfSSam Leffler 	}
1435a3814acfSSam Leffler 
143675ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
14371cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1438e4cd31ddSJeff Roberson 	/*
1439e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1440e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1441e4cd31ddSJeff Roberson 	 * interfaces to also work.
1442e4cd31ddSJeff Roberson 	 */
14431cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
144475ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1445e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1446e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1447e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1448e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
144932a52e9eSNavdeep Parhar 	ifp->if_pcp = ifv->ifv_pcp;
1450e4cd31ddSJeff Roberson 
14512cc2df49SGarrett Wollman 	/*
145216cf6bdbSMatt Joras 	 * We wrap the parent's if_output using vlan_output to ensure that it
145316cf6bdbSMatt Joras 	 * can't become stale.
145416cf6bdbSMatt Joras 	 */
145516cf6bdbSMatt Joras 	ifp->if_output = vlan_output;
145616cf6bdbSMatt Joras 
145716cf6bdbSMatt Joras 	/*
145824993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
145924993214SYaroslav Tykhiy 	 * Other flags are none of our business.
14602cc2df49SGarrett Wollman 	 */
146164a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
14621cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
14631cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
14641cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
14651cf236fbSYaroslav Tykhiy 
14661cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
14672cc2df49SGarrett Wollman 
14686dcec895SGleb Smirnoff 	NET_EPOCH_ENTER(et);
146975ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
14706dcec895SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1471a3814acfSSam Leffler 
1472a3814acfSSam Leffler 	/*
1473e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
14742cc2df49SGarrett Wollman 	 * physical interface's.
14752cc2df49SGarrett Wollman 	 */
1476a961401eSAndrey V. Elsukov 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1477e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1478e4cd31ddSJeff Roberson 	    p->if_addrlen;
14791b2a4f7aSBill Fenner 
1480a961401eSAndrey V. Elsukov 	/*
1481a961401eSAndrey V. Elsukov 	 * Do not schedule link address update if it was the same
1482a961401eSAndrey V. Elsukov 	 * as previous parent's. This helps avoid updating for each
1483a961401eSAndrey V. Elsukov 	 * associated llentry.
1484a961401eSAndrey V. Elsukov 	 */
1485a961401eSAndrey V. Elsukov 	if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1486a961401eSAndrey V. Elsukov 		bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1487a961401eSAndrey V. Elsukov 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1488a961401eSAndrey V. Elsukov 	}
14892ada9747SYaroslav Tykhiy 
14902ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
14912ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1492d148c2a2SMatt Joras 
1493d148c2a2SMatt Joras 	/* Update flags on the parent, if necessary. */
1494d148c2a2SMatt Joras 	vlan_setflags(ifp, 1);
1495b08d611dSMatt Macy 
1496d148c2a2SMatt Joras 	/*
1497b08d611dSMatt Macy 	 * Configure multicast addresses that may already be
1498b08d611dSMatt Macy 	 * joined on the vlan device.
1499d148c2a2SMatt Joras 	 */
1500b08d611dSMatt Macy 	(void)vlan_setmulti(ifp);
1501b08d611dSMatt Macy 
1502b08d611dSMatt Macy done:
1503c725524cSJack F Vogel 	if (error == 0)
15047983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1505d148c2a2SMatt Joras 	VLAN_XUNLOCK();
150675ee267cSGleb Smirnoff 
150775ee267cSGleb Smirnoff 	return (error);
15082cc2df49SGarrett Wollman }
15092cc2df49SGarrett Wollman 
15106f359e28SJohn Baldwin static void
1511f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1512f731f104SBill Paul {
15135cb8c31aSYaroslav Tykhiy 
1514d148c2a2SMatt Joras 	VLAN_XLOCK();
151528cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
1516d148c2a2SMatt Joras 	VLAN_XUNLOCK();
15175cb8c31aSYaroslav Tykhiy }
15185cb8c31aSYaroslav Tykhiy 
15196f359e28SJohn Baldwin static void
152028cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
15215cb8c31aSYaroslav Tykhiy {
152275ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1523f731f104SBill Paul 	struct vlan_mc_entry *mc;
1524f731f104SBill Paul 	struct ifvlan *ifv;
1525c725524cSJack F Vogel 	struct ifnet  *parent;
152628cc4d37SJohn Baldwin 	int error;
1527f731f104SBill Paul 
1528d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
15294faedfe8SSam Leffler 
1530f731f104SBill Paul 	ifv = ifp->if_softc;
153175ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
153222893351SJack F Vogel 	parent = NULL;
1533f731f104SBill Paul 
153422893351SJack F Vogel 	if (trunk != NULL) {
153522893351SJack F Vogel 		parent = trunk->parent;
15361b2a4f7aSBill Fenner 
1537f731f104SBill Paul 		/*
1538f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1539f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
15401b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1541f731f104SBill Paul 		 */
1542b08d611dSMatt Macy 		while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
15436f359e28SJohn Baldwin 			/*
154428cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1545b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
154628cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
154728cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
154828cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
154928cc4d37SJohn Baldwin 			 * succeed.
15506f359e28SJohn Baldwin 			 */
155128cc4d37SJohn Baldwin 			if (!departing) {
155228cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1553e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
155428cc4d37SJohn Baldwin 				if (error)
155528cc4d37SJohn Baldwin 					if_printf(ifp,
155628cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
155728cc4d37SJohn Baldwin 					    error);
155828cc4d37SJohn Baldwin 			}
1559b08d611dSMatt Macy 			CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
15602a4bd982SGleb Smirnoff 			NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
1561f731f104SBill Paul 		}
1562a3814acfSSam Leffler 
15631cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1564d148c2a2SMatt Joras 
156575ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
156675ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
156775ee267cSGleb Smirnoff 
156875ee267cSGleb Smirnoff 		/*
156975ee267cSGleb Smirnoff 		 * Check if we were the last.
157075ee267cSGleb Smirnoff 		 */
157175ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
15722d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
1573b08d611dSMatt Macy 			NET_EPOCH_WAIT();
157475ee267cSGleb Smirnoff 			trunk_destroy(trunk);
1575d148c2a2SMatt Joras 		}
15761b2a4f7aSBill Fenner 	}
1577f731f104SBill Paul 
1578f731f104SBill Paul 	/* Disconnect from parent. */
15791cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
15801cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
15815cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
15825cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
15835cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1584f731f104SBill Paul 
158522893351SJack F Vogel 	/*
158622893351SJack F Vogel 	 * Only dispatch an event if vlan was
158722893351SJack F Vogel 	 * attached, otherwise there is nothing
158822893351SJack F Vogel 	 * to cleanup anyway.
158922893351SJack F Vogel 	 */
159022893351SJack F Vogel 	if (parent != NULL)
15917983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1592f731f104SBill Paul }
1593f731f104SBill Paul 
15941cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1595f731f104SBill Paul static int
15961cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
15971cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1598a3814acfSSam Leffler {
15991cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
16001cf236fbSYaroslav Tykhiy 	int error;
1601a3814acfSSam Leffler 
1602d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1603a3814acfSSam Leffler 
16041cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
16051cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
16061cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
16071cf236fbSYaroslav Tykhiy 
16081cf236fbSYaroslav Tykhiy 	/*
16091cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
16101cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
16111cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
16121cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
16131cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
16141cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
16151cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
16161cf236fbSYaroslav Tykhiy 	 */
16171cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
161875ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
16191cf236fbSYaroslav Tykhiy 		if (error)
1620a3814acfSSam Leffler 			return (error);
16211cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
16221cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
16231cf236fbSYaroslav Tykhiy 	}
16241cf236fbSYaroslav Tykhiy 	return (0);
16251cf236fbSYaroslav Tykhiy }
16261cf236fbSYaroslav Tykhiy 
16271cf236fbSYaroslav Tykhiy /*
16281cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
16291cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
16301cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
16311cf236fbSYaroslav Tykhiy  */
16321cf236fbSYaroslav Tykhiy static int
16331cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
16341cf236fbSYaroslav Tykhiy {
16351cf236fbSYaroslav Tykhiy 	int error, i;
16361cf236fbSYaroslav Tykhiy 
16371cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
16381cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
16391cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
16401cf236fbSYaroslav Tykhiy 		if (error)
16411cf236fbSYaroslav Tykhiy 			return (error);
16421cf236fbSYaroslav Tykhiy 	}
16431cf236fbSYaroslav Tykhiy 	return (0);
1644a3814acfSSam Leffler }
1645a3814acfSSam Leffler 
1646127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1647127d7b2dSAndre Oppermann static void
1648a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1649127d7b2dSAndre Oppermann {
1650b2807792SGleb Smirnoff 	struct epoch_tracker et;
1651d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1652127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
1653127d7b2dSAndre Oppermann 
1654b2807792SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1655d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1656b2807792SGleb Smirnoff 	if (trunk == NULL) {
1657b2807792SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1658d148c2a2SMatt Joras 		return;
1659b2807792SGleb Smirnoff 	}
1660d148c2a2SMatt Joras 
1661d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1662d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
1663aad0be7aSGleb Smirnoff 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1664fc74a9f9SBrooks Davis 		if_link_state_change(ifv->ifv_ifp,
166575ee267cSGleb Smirnoff 		    trunk->parent->if_link_state);
1666127d7b2dSAndre Oppermann 	}
1667d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1668b2807792SGleb Smirnoff 	NET_EPOCH_EXIT(et);
166975ee267cSGleb Smirnoff }
167075ee267cSGleb Smirnoff 
167175ee267cSGleb Smirnoff static void
167275ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
167375ee267cSGleb Smirnoff {
1674d148c2a2SMatt Joras 	struct ifnet *p;
1675d148c2a2SMatt Joras 	struct ifnet *ifp;
16769fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
1677d89baa5aSAlexander Motin 	int cap = 0, ena = 0, mena;
1678d89baa5aSAlexander Motin 	u_long hwa = 0;
167975ee267cSGleb Smirnoff 
1680a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
1681b8a6e03fSGleb Smirnoff 	VLAN_SXLOCK_ASSERT();
1682b8a6e03fSGleb Smirnoff 
1683d148c2a2SMatt Joras 	p = PARENT(ifv);
1684d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
168575ee267cSGleb Smirnoff 
1686d89baa5aSAlexander Motin 	/* Mask parent interface enabled capabilities disabled by user. */
1687d89baa5aSAlexander Motin 	mena = p->if_capenable & ifv->ifv_capenable;
1688d89baa5aSAlexander Motin 
168975ee267cSGleb Smirnoff 	/*
169075ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
169175ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
169275ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
169375ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
169475ee267cSGleb Smirnoff 	 */
169575ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1696d89baa5aSAlexander Motin 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
169775ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
169875ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1699d89baa5aSAlexander Motin 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1700d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM)
1701d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1702d89baa5aSAlexander Motin 			    CSUM_UDP | CSUM_SCTP);
1703d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM_IPV6)
1704d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1705d89baa5aSAlexander Motin 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
170675ee267cSGleb Smirnoff 	}
1707d89baa5aSAlexander Motin 
17089b76d9cbSPyun YongHyeon 	/*
17099b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
17109b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
17119b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
17129b76d9cbSPyun YongHyeon 	 */
17139fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
17149fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
17159fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
17169b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1717d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TSO;
17189b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1719d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TSO;
1720d89baa5aSAlexander Motin 		if (ena & IFCAP_TSO)
1721d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & CSUM_TSO;
17229b76d9cbSPyun YongHyeon 	}
172309fe6320SNavdeep Parhar 
172409fe6320SNavdeep Parhar 	/*
172559150e91SAlexander Motin 	 * If the parent interface can do LRO and checksum offloading on
172659150e91SAlexander Motin 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
172759150e91SAlexander Motin 	 * cost nothing, while false negative may lead to some confusions.
172859150e91SAlexander Motin 	 */
172959150e91SAlexander Motin 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
173059150e91SAlexander Motin 		cap |= p->if_capabilities & IFCAP_LRO;
173159150e91SAlexander Motin 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
173259150e91SAlexander Motin 		ena |= p->if_capenable & IFCAP_LRO;
173359150e91SAlexander Motin 
173459150e91SAlexander Motin 	/*
173509fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
173609fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
173709fe6320SNavdeep Parhar 	 *
173809fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
173909fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
174009fe6320SNavdeep Parhar 	 * with its own bit.
174109fe6320SNavdeep Parhar 	 */
174209fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
174309fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1744d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TOE;
174509fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
174609fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
1747d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TOE;
174809fe6320SNavdeep Parhar 	}
1749f3e7afe2SHans Petter Selasky 
1750d89baa5aSAlexander Motin 	/*
1751d89baa5aSAlexander Motin 	 * If the parent interface supports dynamic link state, so does the
1752d89baa5aSAlexander Motin 	 * VLAN interface.
1753d89baa5aSAlexander Motin 	 */
1754d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1755d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_LINKSTATE);
1756d89baa5aSAlexander Motin 
1757f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1758f3e7afe2SHans Petter Selasky 	/*
1759f3e7afe2SHans Petter Selasky 	 * If the parent interface supports ratelimiting, so does the
1760f3e7afe2SHans Petter Selasky 	 * VLAN interface.
1761f3e7afe2SHans Petter Selasky 	 */
1762d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1763d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_TXRTLMT);
1764f3e7afe2SHans Petter Selasky #endif
1765d89baa5aSAlexander Motin 
176666d0c056SJohn Baldwin 	/*
176766d0c056SJohn Baldwin 	 * If the parent interface supports unmapped mbufs, so does
176866d0c056SJohn Baldwin 	 * the VLAN interface.  Note that this should be fine even for
176966d0c056SJohn Baldwin 	 * interfaces that don't support hardware tagging as headers
177066d0c056SJohn Baldwin 	 * are prepended in normal mbufs to unmapped mbufs holding
177166d0c056SJohn Baldwin 	 * payload data.
177266d0c056SJohn Baldwin 	 */
177366d0c056SJohn Baldwin 	cap |= (p->if_capabilities & IFCAP_NOMAP);
177466d0c056SJohn Baldwin 	ena |= (mena & IFCAP_NOMAP);
177566d0c056SJohn Baldwin 
1776b2e60773SJohn Baldwin 	/*
1777b2e60773SJohn Baldwin 	 * If the parent interface can offload encryption and segmentation
1778b2e60773SJohn Baldwin 	 * of TLS records over TCP, propagate it's capability to the VLAN
1779b2e60773SJohn Baldwin 	 * interface.
1780b2e60773SJohn Baldwin 	 *
1781b2e60773SJohn Baldwin 	 * All TLS drivers in the tree today can deal with VLANs.  If
1782b2e60773SJohn Baldwin 	 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1783b2e60773SJohn Baldwin 	 * defined.
1784b2e60773SJohn Baldwin 	 */
1785b2e60773SJohn Baldwin 	if (p->if_capabilities & IFCAP_TXTLS)
1786b2e60773SJohn Baldwin 		cap |= p->if_capabilities & IFCAP_TXTLS;
1787b2e60773SJohn Baldwin 	if (p->if_capenable & IFCAP_TXTLS)
1788b2e60773SJohn Baldwin 		ena |= mena & IFCAP_TXTLS;
1789b2e60773SJohn Baldwin 
1790d89baa5aSAlexander Motin 	ifp->if_capabilities = cap;
1791d89baa5aSAlexander Motin 	ifp->if_capenable = ena;
1792d89baa5aSAlexander Motin 	ifp->if_hwassist = hwa;
179375ee267cSGleb Smirnoff }
179475ee267cSGleb Smirnoff 
179575ee267cSGleb Smirnoff static void
179675ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
179775ee267cSGleb Smirnoff {
1798b2807792SGleb Smirnoff 	struct epoch_tracker et;
1799d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
180075ee267cSGleb Smirnoff 	struct ifvlan *ifv;
180175ee267cSGleb Smirnoff 
1802d148c2a2SMatt Joras 	VLAN_SLOCK();
1803d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1804d148c2a2SMatt Joras 	if (trunk == NULL) {
1805d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1806d148c2a2SMatt Joras 		return;
1807d148c2a2SMatt Joras 	}
1808b2807792SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1809b8a6e03fSGleb Smirnoff 	VLAN_FOREACH(ifv, trunk)
181075ee267cSGleb Smirnoff 		vlan_capabilities(ifv);
1811b2807792SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1812d148c2a2SMatt Joras 	VLAN_SUNLOCK();
1813127d7b2dSAndre Oppermann }
1814127d7b2dSAndre Oppermann 
1815a3814acfSSam Leffler static int
1816cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
18172cc2df49SGarrett Wollman {
18182cc2df49SGarrett Wollman 	struct ifnet *p;
18192cc2df49SGarrett Wollman 	struct ifreq *ifr;
1820e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
18212cc2df49SGarrett Wollman 	struct ifvlan *ifv;
18222d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
18232cc2df49SGarrett Wollman 	struct vlanreq vlr;
182484becee1SAlexander Motin 	int error = 0, oldmtu;
18252cc2df49SGarrett Wollman 
18262cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1827e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
18282cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
18292cc2df49SGarrett Wollman 
18302cc2df49SGarrett Wollman 	switch (cmd) {
1831e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1832e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1833e4cd31ddSJeff Roberson #ifdef INET
1834e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1835e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1836e4cd31ddSJeff Roberson #endif
1837e4cd31ddSJeff Roberson 		break;
1838e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
183938d958a6SBrooks Davis 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
184038d958a6SBrooks Davis 		    ifp->if_addrlen);
1841e4cd31ddSJeff Roberson 		break;
1842b3cca108SBill Fenner 	case SIOCGIFMEDIA:
1843d148c2a2SMatt Joras 		VLAN_SLOCK();
184475ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1845d8564efdSEd Maste 			p = PARENT(ifv);
18469bcf3ae4SAlexander Motin 			if_ref(p);
1847d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
18489bcf3ae4SAlexander Motin 			if_rele(p);
1849b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1850b3cca108SBill Fenner 			if (error == 0) {
1851b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1852b3cca108SBill Fenner 
1853b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1854b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1855b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1856b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1857b3cca108SBill Fenner 						ifmr->ifm_ulist,
1858b3cca108SBill Fenner 						sizeof(int));
1859b3cca108SBill Fenner 				}
1860b3cca108SBill Fenner 			}
18614faedfe8SSam Leffler 		} else {
1862b3cca108SBill Fenner 			error = EINVAL;
18634faedfe8SSam Leffler 		}
1864d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1865b3cca108SBill Fenner 		break;
1866b3cca108SBill Fenner 
1867b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1868b3cca108SBill Fenner 		error = EINVAL;
1869b3cca108SBill Fenner 		break;
1870b3cca108SBill Fenner 
18712cc2df49SGarrett Wollman 	case SIOCSIFMTU:
18722cc2df49SGarrett Wollman 		/*
18732cc2df49SGarrett Wollman 		 * Set the interface MTU.
18742cc2df49SGarrett Wollman 		 */
1875d148c2a2SMatt Joras 		VLAN_SLOCK();
1876d148c2a2SMatt Joras 		trunk = TRUNK(ifv);
1877d148c2a2SMatt Joras 		if (trunk != NULL) {
1878d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1879a3814acfSSam Leffler 			if (ifr->ifr_mtu >
188075ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1881a3814acfSSam Leffler 			    ifr->ifr_mtu <
1882a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
18832cc2df49SGarrett Wollman 				error = EINVAL;
1884a3814acfSSam Leffler 			else
18852cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1886d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
1887a3814acfSSam Leffler 		} else
1888a3814acfSSam Leffler 			error = EINVAL;
1889d148c2a2SMatt Joras 		VLAN_SUNLOCK();
18902cc2df49SGarrett Wollman 		break;
18912cc2df49SGarrett Wollman 
18922cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1893ccf7ba97SMarko Zec #ifdef VIMAGE
189415f6780eSRobert Watson 		/*
189515f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
189615f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
189715f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
189815f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
18995a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
190015f6780eSRobert Watson 		 */
1901ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1902ccf7ba97SMarko Zec 			error = EPERM;
1903ccf7ba97SMarko Zec 			break;
1904ccf7ba97SMarko Zec 		}
1905ccf7ba97SMarko Zec #endif
1906541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
19072cc2df49SGarrett Wollman 		if (error)
19082cc2df49SGarrett Wollman 			break;
19092cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1910f731f104SBill Paul 			vlan_unconfig(ifp);
19112cc2df49SGarrett Wollman 			break;
19122cc2df49SGarrett Wollman 		}
19139bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
19141bdc73d3SEd Maste 		if (p == NULL) {
19152cc2df49SGarrett Wollman 			error = ENOENT;
19162cc2df49SGarrett Wollman 			break;
19172cc2df49SGarrett Wollman 		}
191884becee1SAlexander Motin 		oldmtu = ifp->if_mtu;
191975ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
19209bcf3ae4SAlexander Motin 		if_rele(p);
192184becee1SAlexander Motin 
192284becee1SAlexander Motin 		/*
192384becee1SAlexander Motin 		 * VLAN MTU may change during addition of the vlandev.
192484becee1SAlexander Motin 		 * If it did, do network layer specific procedure.
192584becee1SAlexander Motin 		 */
192684becee1SAlexander Motin 		if (ifp->if_mtu != oldmtu) {
192784becee1SAlexander Motin #ifdef INET6
192884becee1SAlexander Motin 			nd6_setmtu(ifp);
192984becee1SAlexander Motin #endif
193084becee1SAlexander Motin 			rt_updatemtu(ifp);
193184becee1SAlexander Motin 		}
19322cc2df49SGarrett Wollman 		break;
19332cc2df49SGarrett Wollman 
19342cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1935ccf7ba97SMarko Zec #ifdef VIMAGE
1936ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1937ccf7ba97SMarko Zec 			error = EPERM;
1938ccf7ba97SMarko Zec 			break;
1939ccf7ba97SMarko Zec 		}
1940ccf7ba97SMarko Zec #endif
194115a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
1942d148c2a2SMatt Joras 		VLAN_SLOCK();
194375ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
194475ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
19459bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
19467983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
19472cc2df49SGarrett Wollman 		}
1948d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1949541d96aaSBrooks Davis 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
19502cc2df49SGarrett Wollman 		break;
19512cc2df49SGarrett Wollman 
19522cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
19532cc2df49SGarrett Wollman 		/*
19541cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
19551cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
19562cc2df49SGarrett Wollman 		 */
1957d148c2a2SMatt Joras 		VLAN_XLOCK();
195875ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
19591cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
1960d148c2a2SMatt Joras 		VLAN_XUNLOCK();
19612cc2df49SGarrett Wollman 		break;
1962a3814acfSSam Leffler 
1963f731f104SBill Paul 	case SIOCADDMULTI:
1964f731f104SBill Paul 	case SIOCDELMULTI:
196575ee267cSGleb Smirnoff 		/*
196675ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
196775ee267cSGleb Smirnoff 		 * when we do.
1968d148c2a2SMatt Joras 		 *
1969d148c2a2SMatt Joras 		 * XXX We need the rmlock here to avoid sleeping while
1970d148c2a2SMatt Joras 		 * holding in6_multi_mtx.
197175ee267cSGleb Smirnoff 		 */
1972b08d611dSMatt Macy 		VLAN_XLOCK();
19732d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
1974b08d611dSMatt Macy 		if (trunk != NULL)
1975f731f104SBill Paul 			error = vlan_setmulti(ifp);
1976b08d611dSMatt Macy 		VLAN_XUNLOCK();
197775ee267cSGleb Smirnoff 
1978b08d611dSMatt Macy 		break;
19792ccbbd06SMarcelo Araujo 	case SIOCGVLANPCP:
19802ccbbd06SMarcelo Araujo #ifdef VIMAGE
19812ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19822ccbbd06SMarcelo Araujo 			error = EPERM;
19832ccbbd06SMarcelo Araujo 			break;
19842ccbbd06SMarcelo Araujo 		}
19852ccbbd06SMarcelo Araujo #endif
19862ccbbd06SMarcelo Araujo 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
19872ccbbd06SMarcelo Araujo 		break;
19882ccbbd06SMarcelo Araujo 
19892ccbbd06SMarcelo Araujo 	case SIOCSVLANPCP:
19902ccbbd06SMarcelo Araujo #ifdef VIMAGE
19912ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19922ccbbd06SMarcelo Araujo 			error = EPERM;
19932ccbbd06SMarcelo Araujo 			break;
19942ccbbd06SMarcelo Araujo 		}
19952ccbbd06SMarcelo Araujo #endif
19962ccbbd06SMarcelo Araujo 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
19972ccbbd06SMarcelo Araujo 		if (error)
19982ccbbd06SMarcelo Araujo 			break;
19992ccbbd06SMarcelo Araujo 		if (ifr->ifr_vlan_pcp > 7) {
20002ccbbd06SMarcelo Araujo 			error = EINVAL;
20012ccbbd06SMarcelo Araujo 			break;
20022ccbbd06SMarcelo Araujo 		}
20032ccbbd06SMarcelo Araujo 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
200432a52e9eSNavdeep Parhar 		ifp->if_pcp = ifv->ifv_pcp;
20052ccbbd06SMarcelo Araujo 		vlan_tag_recalculate(ifv);
20064a381a9eSHans Petter Selasky 		/* broadcast event about PCP change */
20074a381a9eSHans Petter Selasky 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
20082ccbbd06SMarcelo Araujo 		break;
20092ccbbd06SMarcelo Araujo 
2010d89baa5aSAlexander Motin 	case SIOCSIFCAP:
2011d148c2a2SMatt Joras 		VLAN_SLOCK();
2012d89baa5aSAlexander Motin 		ifv->ifv_capenable = ifr->ifr_reqcap;
2013d89baa5aSAlexander Motin 		trunk = TRUNK(ifv);
20146dcec895SGleb Smirnoff 		if (trunk != NULL) {
20156dcec895SGleb Smirnoff 			struct epoch_tracker et;
20166dcec895SGleb Smirnoff 
20176dcec895SGleb Smirnoff 			NET_EPOCH_ENTER(et);
2018d89baa5aSAlexander Motin 			vlan_capabilities(ifv);
20196dcec895SGleb Smirnoff 			NET_EPOCH_EXIT(et);
20206dcec895SGleb Smirnoff 		}
2021d148c2a2SMatt Joras 		VLAN_SUNLOCK();
2022d89baa5aSAlexander Motin 		break;
2023d89baa5aSAlexander Motin 
20242cc2df49SGarrett Wollman 	default:
2025e4cd31ddSJeff Roberson 		error = EINVAL;
2026e4cd31ddSJeff Roberson 		break;
20272cc2df49SGarrett Wollman 	}
202815a66c21SBruce M Simpson 
202915a66c21SBruce M Simpson 	return (error);
20302cc2df49SGarrett Wollman }
2031f3e7afe2SHans Petter Selasky 
2032b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
2033f3e7afe2SHans Petter Selasky static int
2034f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp,
2035f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *params,
2036f3e7afe2SHans Petter Selasky     struct m_snd_tag **ppmt)
2037f3e7afe2SHans Petter Selasky {
2038fb3bc596SJohn Baldwin 	struct epoch_tracker et;
2039fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2040fb3bc596SJohn Baldwin 	struct ifvlan *ifv;
2041fb3bc596SJohn Baldwin 	struct ifnet *parent;
2042fb3bc596SJohn Baldwin 	int error;
2043f3e7afe2SHans Petter Selasky 
2044fb3bc596SJohn Baldwin 	NET_EPOCH_ENTER(et);
2045fb3bc596SJohn Baldwin 	ifv = ifp->if_softc;
2046fb3bc596SJohn Baldwin 	if (ifv->ifv_trunk != NULL)
2047fb3bc596SJohn Baldwin 		parent = PARENT(ifv);
2048fb3bc596SJohn Baldwin 	else
2049fb3bc596SJohn Baldwin 		parent = NULL;
2050fb3bc596SJohn Baldwin 	if (parent == NULL || parent->if_snd_tag_alloc == NULL) {
2051fb3bc596SJohn Baldwin 		NET_EPOCH_EXIT(et);
2052f3e7afe2SHans Petter Selasky 		return (EOPNOTSUPP);
2053fb3bc596SJohn Baldwin 	}
2054fb3bc596SJohn Baldwin 	if_ref(parent);
2055fb3bc596SJohn Baldwin 	NET_EPOCH_EXIT(et);
2056fb3bc596SJohn Baldwin 
2057fb3bc596SJohn Baldwin 	vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2058fb3bc596SJohn Baldwin 	if (vst == NULL) {
2059fb3bc596SJohn Baldwin 		if_rele(parent);
2060fb3bc596SJohn Baldwin 		return (ENOMEM);
2061fb3bc596SJohn Baldwin 	}
2062fb3bc596SJohn Baldwin 
2063fb3bc596SJohn Baldwin 	error = parent->if_snd_tag_alloc(parent, params, &vst->tag);
2064fb3bc596SJohn Baldwin 	if_rele(parent);
2065fb3bc596SJohn Baldwin 	if (error) {
2066fb3bc596SJohn Baldwin 		free(vst, M_VLAN);
2067fb3bc596SJohn Baldwin 		return (error);
2068fb3bc596SJohn Baldwin 	}
2069fb3bc596SJohn Baldwin 
2070fb3bc596SJohn Baldwin 	m_snd_tag_init(&vst->com, ifp);
2071fb3bc596SJohn Baldwin 
2072fb3bc596SJohn Baldwin 	*ppmt = &vst->com;
2073fb3bc596SJohn Baldwin 	return (0);
2074fb3bc596SJohn Baldwin }
2075fb3bc596SJohn Baldwin 
2076fb3bc596SJohn Baldwin static int
2077fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst,
2078fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *params)
2079fb3bc596SJohn Baldwin {
2080fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2081fb3bc596SJohn Baldwin 
2082fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2083fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params));
2084fb3bc596SJohn Baldwin }
2085fb3bc596SJohn Baldwin 
2086fb3bc596SJohn Baldwin static int
2087fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst,
2088fb3bc596SJohn Baldwin     union if_snd_tag_query_params *params)
2089fb3bc596SJohn Baldwin {
2090fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2091fb3bc596SJohn Baldwin 
2092fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2093fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_query(vst->tag, params));
2094f3e7afe2SHans Petter Selasky }
2095fa91f845SRandall Stewart 
2096fa91f845SRandall Stewart static void
2097fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst)
2098fa91f845SRandall Stewart {
2099fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2100fb3bc596SJohn Baldwin 
2101fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2102fb3bc596SJohn Baldwin 	m_snd_tag_rele(vst->tag);
2103fb3bc596SJohn Baldwin 	free(vst, M_VLAN);
2104fa91f845SRandall Stewart }
2105f3e7afe2SHans Petter Selasky #endif
2106