xref: /freebsd/sys/net/if_vlan.c (revision 84becee1ace1cccfc359fa40ff0688715a7604a4)
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"
49*84becee1SAlexander 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>
79*84becee1SAlexander 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 
87*84becee1SAlexander Motin #ifdef INET6
88*84becee1SAlexander Motin /*
89*84becee1SAlexander Motin  * XXX: declare here to avoid to include many inet6 related files..
90*84becee1SAlexander Motin  * should be more generalized?
91*84becee1SAlexander Motin  */
92*84becee1SAlexander Motin extern void	nd6_setmtu(struct ifnet *);
93*84becee1SAlexander Motin #endif
94*84becee1SAlexander 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 {
966f889d2efSBrooks Davis 	const char *cp;
967f889d2efSBrooks Davis 
968f941c31aSGleb Smirnoff 	if (vlan_clone_match_ethervid(name, NULL) != NULL)
969f889d2efSBrooks Davis 		return (1);
970f889d2efSBrooks Davis 
97142a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
972f889d2efSBrooks Davis 		return (0);
973f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
974f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
975f889d2efSBrooks Davis 			return (0);
976f889d2efSBrooks Davis 	}
977f889d2efSBrooks Davis 
978f889d2efSBrooks Davis 	return (1);
979f889d2efSBrooks Davis }
980f889d2efSBrooks Davis 
981f889d2efSBrooks Davis static int
9826b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
983f889d2efSBrooks Davis {
984f889d2efSBrooks Davis 	char *dp;
985f889d2efSBrooks Davis 	int wildcard;
986f889d2efSBrooks Davis 	int unit;
987f889d2efSBrooks Davis 	int error;
9887983103aSRobert Watson 	int vid;
9899d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
9909d4fe4b2SBrooks Davis 	struct ifnet *ifp;
991f889d2efSBrooks Davis 	struct ifnet *p;
9923ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
9933ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
9946b7330e2SSam Leffler 	struct vlanreq vlr;
99565239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
996f889d2efSBrooks Davis 
9976b7330e2SSam Leffler 	/*
9986b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
9996b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
10006b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
10016b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
10026b7330e2SSam Leffler 	 *   must be configured separately.
10036b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
1004a4641f4eSPedro F. Giffuni 	 * supported for backwards compatibility.
10057983103aSRobert Watson 	 *
10067983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
10077983103aSRobert Watson 	 * called for.
10086b7330e2SSam Leffler 	 */
10096b7330e2SSam Leffler 	if (params) {
10106b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
10116b7330e2SSam Leffler 		if (error)
10126b7330e2SSam Leffler 			return error;
10139bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
10146b7330e2SSam Leffler 		if (p == NULL)
1015b1828acfSGleb Smirnoff 			return (ENXIO);
10166b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
10179bcf3ae4SAlexander Motin 		if (error != 0) {
10189bcf3ae4SAlexander Motin 			if_rele(p);
10196b7330e2SSam Leffler 			return (error);
10209bcf3ae4SAlexander Motin 		}
10217983103aSRobert Watson 		vid = vlr.vlr_tag;
10226b7330e2SSam Leffler 		wildcard = (unit < 0);
1023f941c31aSGleb Smirnoff 	} else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1024f889d2efSBrooks Davis 		unit = -1;
1025f889d2efSBrooks Davis 		wildcard = 0;
1026f889d2efSBrooks Davis 	} else {
10279bcf3ae4SAlexander Motin 		p = NULL;
1028f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
1029f889d2efSBrooks Davis 		if (error != 0)
1030f889d2efSBrooks Davis 			return (error);
1031f889d2efSBrooks Davis 
1032f889d2efSBrooks Davis 		wildcard = (unit < 0);
1033f889d2efSBrooks Davis 	}
1034f889d2efSBrooks Davis 
1035f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
10369bcf3ae4SAlexander Motin 	if (error != 0) {
10379bcf3ae4SAlexander Motin 		if (p != NULL)
10389bcf3ae4SAlexander Motin 			if_rele(p);
1039f889d2efSBrooks Davis 		return (error);
10409bcf3ae4SAlexander Motin 	}
1041f889d2efSBrooks Davis 
1042f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
1043f889d2efSBrooks Davis 	if (wildcard) {
1044f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
1045f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1046f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
1047f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
1048f889d2efSBrooks Davis 		}
1049f889d2efSBrooks Davis 	}
10509d4fe4b2SBrooks Davis 
1051a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1052fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1053fc74a9f9SBrooks Davis 	if (ifp == NULL) {
1054fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
1055fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
10569bcf3ae4SAlexander Motin 		if (p != NULL)
10579bcf3ae4SAlexander Motin 			if_rele(p);
1058fc74a9f9SBrooks Davis 		return (ENOSPC);
1059fc74a9f9SBrooks Davis 	}
1060b08d611dSMatt Macy 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
10619d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
1062f889d2efSBrooks Davis 	/*
1063cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
1064f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
1065f889d2efSBrooks Davis 	 */
1066f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
106742a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
1068f889d2efSBrooks Davis 	ifp->if_dunit = unit;
10699d4fe4b2SBrooks Davis 
1070114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
1071d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
1072d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
10739d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
1074b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1075f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1076fb3bc596SJohn Baldwin 	ifp->if_snd_tag_modify = vlan_snd_tag_modify;
1077fb3bc596SJohn Baldwin 	ifp->if_snd_tag_query = vlan_snd_tag_query;
1078fa91f845SRandall Stewart 	ifp->if_snd_tag_free = vlan_snd_tag_free;
1079f3e7afe2SHans Petter Selasky #endif
108064a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
1081fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
10829d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
1083211f625aSBill Fenner 	ifp->if_baudrate = 0;
1084a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
1085a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
10863ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
10873ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
10883ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
10899d4fe4b2SBrooks Davis 
10909bcf3ae4SAlexander Motin 	if (p != NULL) {
10917983103aSRobert Watson 		error = vlan_config(ifv, p, vid);
10929bcf3ae4SAlexander Motin 		if_rele(p);
1093f889d2efSBrooks Davis 		if (error != 0) {
1094f889d2efSBrooks Davis 			/*
109528cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
1096f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
1097f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1098f889d2efSBrooks Davis 			 */
1099f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1100249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
11014b22573aSBrooks Davis 			if_free(ifp);
110296c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
1103f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1104f889d2efSBrooks Davis 
1105f889d2efSBrooks Davis 			return (error);
1106f889d2efSBrooks Davis 		}
1107f889d2efSBrooks Davis 	}
1108f889d2efSBrooks Davis 
11099d4fe4b2SBrooks Davis 	return (0);
11109d4fe4b2SBrooks Davis }
11119d4fe4b2SBrooks Davis 
1112f889d2efSBrooks Davis static int
1113f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
11149d4fe4b2SBrooks Davis {
11159d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1116114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
1117b4e9f837SBrooks Davis 
1118249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1119249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1120d148c2a2SMatt Joras 	/*
1121d148c2a2SMatt Joras 	 * We should have the only reference to the ifv now, so we can now
1122d148c2a2SMatt Joras 	 * drain any remaining lladdr task before freeing the ifnet and the
1123d148c2a2SMatt Joras 	 * ifvlan.
1124d148c2a2SMatt Joras 	 */
1125d148c2a2SMatt Joras 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1126b08d611dSMatt Macy 	NET_EPOCH_WAIT();
11274b22573aSBrooks Davis 	if_free(ifp);
11289d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1129b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
1130b4e9f837SBrooks Davis 
1131f889d2efSBrooks Davis 	return (0);
11329d4fe4b2SBrooks Davis }
11339d4fe4b2SBrooks Davis 
113415a66c21SBruce M Simpson /*
113515a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
113615a66c21SBruce M Simpson  */
11372cc2df49SGarrett Wollman static void
1138114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
11392cc2df49SGarrett Wollman {
11402cc2df49SGarrett Wollman }
11412cc2df49SGarrett Wollman 
11426d3a3ab7SGleb Smirnoff /*
1143d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
11446d3a3ab7SGleb Smirnoff  */
1145d9b1d615SJohn Baldwin static int
1146d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
11472cc2df49SGarrett Wollman {
11482cc2df49SGarrett Wollman 	struct ifvlan *ifv;
11492cc2df49SGarrett Wollman 	struct ifnet *p;
11501ad7a257SPyun YongHyeon 	int error, len, mcast;
11512cc2df49SGarrett Wollman 
1152b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1153b8a6e03fSGleb Smirnoff 
11542cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
1155d148c2a2SMatt Joras 	if (TRUNK(ifv) == NULL) {
1156d148c2a2SMatt Joras 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1157d148c2a2SMatt Joras 		m_freem(m);
1158d148c2a2SMatt Joras 		return (ENETDOWN);
1159d148c2a2SMatt Joras 	}
116075ee267cSGleb Smirnoff 	p = PARENT(ifv);
11611ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
11621ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
11632cc2df49SGarrett Wollman 
1164a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
11652cc2df49SGarrett Wollman 
1166b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1167fb3bc596SJohn Baldwin 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1168fb3bc596SJohn Baldwin 		struct vlan_snd_tag *vst;
1169fb3bc596SJohn Baldwin 		struct m_snd_tag *mst;
1170fb3bc596SJohn Baldwin 
1171fb3bc596SJohn Baldwin 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1172fb3bc596SJohn Baldwin 		mst = m->m_pkthdr.snd_tag;
1173fb3bc596SJohn Baldwin 		vst = mst_to_vst(mst);
1174fb3bc596SJohn Baldwin 		if (vst->tag->ifp != p) {
1175fb3bc596SJohn Baldwin 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1176fb3bc596SJohn Baldwin 			m_freem(m);
1177fb3bc596SJohn Baldwin 			return (EAGAIN);
1178fb3bc596SJohn Baldwin 		}
1179fb3bc596SJohn Baldwin 
1180fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1181fb3bc596SJohn Baldwin 		m_snd_tag_rele(mst);
1182fb3bc596SJohn Baldwin 	}
1183fb3bc596SJohn Baldwin #endif
1184fb3bc596SJohn Baldwin 
1185f731f104SBill Paul 	/*
1186d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
118724993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
118824993214SYaroslav Tykhiy 	 */
11892dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
1190a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1191d148c2a2SMatt Joras 		m_freem(m);
11928b20f6cfSHiroki Sato 		return (ENETDOWN);
119324993214SYaroslav Tykhiy 	}
119424993214SYaroslav Tykhiy 
1195f1379734SKonstantin Belousov 	if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1196a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1197d9b1d615SJohn Baldwin 		return (0);
11984af90a4dSMatthew N. Dodd 	}
11992cc2df49SGarrett Wollman 
12002cc2df49SGarrett Wollman 	/*
12012cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
12022cc2df49SGarrett Wollman 	 */
1203aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1204299153b5SAlexander V. Chernikov 	if (error == 0) {
1205a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1206a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1207a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
12081ad7a257SPyun YongHyeon 	} else
1209a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1210d9b1d615SJohn Baldwin 	return (error);
12112cc2df49SGarrett Wollman }
1212d9b1d615SJohn Baldwin 
121316cf6bdbSMatt Joras static int
121416cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
121516cf6bdbSMatt Joras     struct route *ro)
121616cf6bdbSMatt Joras {
121716cf6bdbSMatt Joras 	struct ifvlan *ifv;
121816cf6bdbSMatt Joras 	struct ifnet *p;
121916cf6bdbSMatt Joras 
1220b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1221b8a6e03fSGleb Smirnoff 
122216cf6bdbSMatt Joras 	ifv = ifp->if_softc;
122316cf6bdbSMatt Joras 	if (TRUNK(ifv) == NULL) {
122416cf6bdbSMatt Joras 		m_freem(m);
122516cf6bdbSMatt Joras 		return (ENETDOWN);
122616cf6bdbSMatt Joras 	}
122716cf6bdbSMatt Joras 	p = PARENT(ifv);
122816cf6bdbSMatt Joras 	return p->if_output(ifp, m, dst, ro);
122916cf6bdbSMatt Joras }
123016cf6bdbSMatt Joras 
123116cf6bdbSMatt Joras 
1232d9b1d615SJohn Baldwin /*
1233d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1234d9b1d615SJohn Baldwin  */
1235d9b1d615SJohn Baldwin static void
1236d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1237d9b1d615SJohn Baldwin {
1238f731f104SBill Paul }
1239f731f104SBill Paul 
1240a3814acfSSam Leffler static void
1241a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1242f731f104SBill Paul {
1243d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1244f731f104SBill Paul 	struct ifvlan *ifv;
12452ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
12462ccbbd06SMarcelo Araujo 	uint16_t vid, tag;
124775ee267cSGleb Smirnoff 
1248b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1249b8a6e03fSGleb Smirnoff 
1250d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1251d148c2a2SMatt Joras 	if (trunk == NULL) {
1252d148c2a2SMatt Joras 		m_freem(m);
1253d148c2a2SMatt Joras 		return;
1254d148c2a2SMatt Joras 	}
1255a3814acfSSam Leffler 
1256f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1257a3814acfSSam Leffler 		/*
125814e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1259a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1260a3814acfSSam Leffler 		 */
12612ccbbd06SMarcelo Araujo 		tag = m->m_pkthdr.ether_vtag;
12626ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1263a3814acfSSam Leffler 	} else {
126475ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
126575ee267cSGleb Smirnoff 
126614e98256SYaroslav Tykhiy 		/*
126714e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
126814e98256SYaroslav Tykhiy 		 */
1269a3814acfSSam Leffler 		switch (ifp->if_type) {
1270a3814acfSSam Leffler 		case IFT_ETHER:
1271a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1272a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1273a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1274a3814acfSSam Leffler 				return;
1275a3814acfSSam Leffler 			}
1276a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
12772ccbbd06SMarcelo Araujo 			tag = ntohs(evl->evl_tag);
1278db8b5973SYaroslav Tykhiy 
1279db8b5973SYaroslav Tykhiy 			/*
12802dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
12812dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
12822dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
12832dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1284db8b5973SYaroslav Tykhiy 			 */
12852dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
12862dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
12872dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1288a3814acfSSam Leffler 			break;
12892dc879b3SYaroslav Tykhiy 
1290a3814acfSSam Leffler 		default:
1291db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
129260c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
129360c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1294a3814acfSSam Leffler #endif
12953751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1296d148c2a2SMatt Joras 			m_freem(m);
129760c60618SYaroslav Tykhiy 			return;
1298a3814acfSSam Leffler 		}
12997a46ec8fSBrooks Davis 	}
13007a46ec8fSBrooks Davis 
13012ccbbd06SMarcelo Araujo 	vid = EVL_VLANOFTAG(tag);
13022ccbbd06SMarcelo Araujo 
13037983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
13042dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1305b08d611dSMatt Macy 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1306d148c2a2SMatt Joras 		m_freem(m);
130775ee267cSGleb Smirnoff 		return;
130875ee267cSGleb Smirnoff 	}
1309f731f104SBill Paul 
13102ccbbd06SMarcelo Araujo 	if (vlan_mtag_pcp) {
13112ccbbd06SMarcelo Araujo 		/*
13122ccbbd06SMarcelo Araujo 		 * While uncommon, it is possible that we will find a 802.1q
13132ccbbd06SMarcelo Araujo 		 * packet encapsulated inside another packet that also had an
13142ccbbd06SMarcelo Araujo 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
13152ccbbd06SMarcelo Araujo 		 * arriving over ethernet.  In that case, we replace the
13162ccbbd06SMarcelo Araujo 		 * existing 802.1q PCP m_tag value.
13172ccbbd06SMarcelo Araujo 		 */
13182ccbbd06SMarcelo Araujo 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
13192ccbbd06SMarcelo Araujo 		if (mtag == NULL) {
13202ccbbd06SMarcelo Araujo 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
13212ccbbd06SMarcelo Araujo 			    sizeof(uint8_t), M_NOWAIT);
13222ccbbd06SMarcelo Araujo 			if (mtag == NULL) {
13232ccbbd06SMarcelo Araujo 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1324d148c2a2SMatt Joras 				m_freem(m);
13252ccbbd06SMarcelo Araujo 				return;
13262ccbbd06SMarcelo Araujo 			}
13272ccbbd06SMarcelo Araujo 			m_tag_prepend(m, mtag);
13282ccbbd06SMarcelo Araujo 		}
13292ccbbd06SMarcelo Araujo 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
13302ccbbd06SMarcelo Araujo 	}
13312ccbbd06SMarcelo Araujo 
1332fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1333c0304424SGleb Smirnoff 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
13342cc2df49SGarrett Wollman 
1335a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1336fdbf1174SMatt Joras 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
13372cc2df49SGarrett Wollman }
13382cc2df49SGarrett Wollman 
1339d148c2a2SMatt Joras static void
1340d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused)
1341d148c2a2SMatt Joras {
1342d148c2a2SMatt Joras 	struct ifvlan *ifv;
1343d148c2a2SMatt Joras 	struct ifnet *ifp;
1344d148c2a2SMatt Joras 
1345d148c2a2SMatt Joras 	ifv = (struct ifvlan *)arg;
1346d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
13475191a3aeSKristof Provost 
13485191a3aeSKristof Provost 	CURVNET_SET(ifp->if_vnet);
13495191a3aeSKristof Provost 
1350d148c2a2SMatt Joras 	/* The ifv_ifp already has the lladdr copied in. */
1351d148c2a2SMatt Joras 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
13525191a3aeSKristof Provost 
13535191a3aeSKristof Provost 	CURVNET_RESTORE();
1354d148c2a2SMatt Joras }
1355d148c2a2SMatt Joras 
13562cc2df49SGarrett Wollman static int
13577983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
13582cc2df49SGarrett Wollman {
13596dcec895SGleb Smirnoff 	struct epoch_tracker et;
136075ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
13611cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
136275ee267cSGleb Smirnoff 	int error = 0;
13632cc2df49SGarrett Wollman 
1364b1828acfSGleb Smirnoff 	/*
1365b1828acfSGleb Smirnoff 	 * We can handle non-ethernet hardware types as long as
1366b1828acfSGleb Smirnoff 	 * they handle the tagging and headers themselves.
1367b1828acfSGleb Smirnoff 	 */
1368e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1369e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
137015a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
137164a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
137264a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
1373b1828acfSGleb Smirnoff 	/*
1374b1828acfSGleb Smirnoff 	 * Don't let the caller set up a VLAN VID with
1375b1828acfSGleb Smirnoff 	 * anything except VLID bits.
1376b1828acfSGleb Smirnoff 	 * VID numbers 0x0 and 0xFFF are reserved.
1377b1828acfSGleb Smirnoff 	 */
1378b1828acfSGleb Smirnoff 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1379b1828acfSGleb Smirnoff 		return (EINVAL);
138075ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
138115a66c21SBruce M Simpson 		return (EBUSY);
13822cc2df49SGarrett Wollman 
1383d148c2a2SMatt Joras 	VLAN_XLOCK();
138475ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
138575ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
138675ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
138775ee267cSGleb Smirnoff 		vlan_inithash(trunk);
138875ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
1389d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
139075ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
139175ee267cSGleb Smirnoff 		trunk->parent = p;
13929bcf3ae4SAlexander Motin 		if_ref(trunk->parent);
1393b08d611dSMatt Macy 		TRUNK_WUNLOCK(trunk);
139475ee267cSGleb Smirnoff 	} else {
139575ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
139675ee267cSGleb Smirnoff 	}
139775ee267cSGleb Smirnoff 
13987983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
13992ccbbd06SMarcelo Araujo 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
14002ccbbd06SMarcelo Araujo 	vlan_tag_recalculate(ifv);
140175ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
140275ee267cSGleb Smirnoff 	if (error)
140375ee267cSGleb Smirnoff 		goto done;
140473f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1405a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1406a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
14071cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1408d89baa5aSAlexander Motin 	ifv->ifv_capenable = -1;
1409a3814acfSSam Leffler 
1410a3814acfSSam Leffler 	/*
1411a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1412a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1413656acce4SYaroslav Tykhiy 	 * use it.
1414a3814acfSSam Leffler 	 */
1415656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1416656acce4SYaroslav Tykhiy 		/*
1417656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1418656acce4SYaroslav Tykhiy 		 * handle extended frames.
1419656acce4SYaroslav Tykhiy 		 */
1420a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1421656acce4SYaroslav Tykhiy 	} else {
1422a3814acfSSam Leffler 		/*
1423a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1424a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1425a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1426a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1427a3814acfSSam Leffler 		 * which might still be useful.
1428a3814acfSSam Leffler 		 */
1429a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1430a3814acfSSam Leffler 	}
1431a3814acfSSam Leffler 
143275ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
14331cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1434e4cd31ddSJeff Roberson 	/*
1435e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1436e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1437e4cd31ddSJeff Roberson 	 * interfaces to also work.
1438e4cd31ddSJeff Roberson 	 */
14391cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
144075ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1441e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1442e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1443e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1444e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
144532a52e9eSNavdeep Parhar 	ifp->if_pcp = ifv->ifv_pcp;
1446e4cd31ddSJeff Roberson 
14472cc2df49SGarrett Wollman 	/*
144816cf6bdbSMatt Joras 	 * We wrap the parent's if_output using vlan_output to ensure that it
144916cf6bdbSMatt Joras 	 * can't become stale.
145016cf6bdbSMatt Joras 	 */
145116cf6bdbSMatt Joras 	ifp->if_output = vlan_output;
145216cf6bdbSMatt Joras 
145316cf6bdbSMatt Joras 	/*
145424993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
145524993214SYaroslav Tykhiy 	 * Other flags are none of our business.
14562cc2df49SGarrett Wollman 	 */
145764a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
14581cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
14591cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
14601cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
14611cf236fbSYaroslav Tykhiy 
14621cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
14632cc2df49SGarrett Wollman 
14646dcec895SGleb Smirnoff 	NET_EPOCH_ENTER(et);
146575ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
14666dcec895SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1467a3814acfSSam Leffler 
1468a3814acfSSam Leffler 	/*
1469e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
14702cc2df49SGarrett Wollman 	 * physical interface's.
14712cc2df49SGarrett Wollman 	 */
1472a961401eSAndrey V. Elsukov 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1473e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1474e4cd31ddSJeff Roberson 	    p->if_addrlen;
14751b2a4f7aSBill Fenner 
1476a961401eSAndrey V. Elsukov 	/*
1477a961401eSAndrey V. Elsukov 	 * Do not schedule link address update if it was the same
1478a961401eSAndrey V. Elsukov 	 * as previous parent's. This helps avoid updating for each
1479a961401eSAndrey V. Elsukov 	 * associated llentry.
1480a961401eSAndrey V. Elsukov 	 */
1481a961401eSAndrey V. Elsukov 	if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1482a961401eSAndrey V. Elsukov 		bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1483a961401eSAndrey V. Elsukov 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1484a961401eSAndrey V. Elsukov 	}
14852ada9747SYaroslav Tykhiy 
14862ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
14872ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1488d148c2a2SMatt Joras 
1489d148c2a2SMatt Joras 	/* Update flags on the parent, if necessary. */
1490d148c2a2SMatt Joras 	vlan_setflags(ifp, 1);
1491b08d611dSMatt Macy 
1492d148c2a2SMatt Joras 	/*
1493b08d611dSMatt Macy 	 * Configure multicast addresses that may already be
1494b08d611dSMatt Macy 	 * joined on the vlan device.
1495d148c2a2SMatt Joras 	 */
1496b08d611dSMatt Macy 	(void)vlan_setmulti(ifp);
1497b08d611dSMatt Macy 
1498b08d611dSMatt Macy done:
1499c725524cSJack F Vogel 	if (error == 0)
15007983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1501d148c2a2SMatt Joras 	VLAN_XUNLOCK();
150275ee267cSGleb Smirnoff 
150375ee267cSGleb Smirnoff 	return (error);
15042cc2df49SGarrett Wollman }
15052cc2df49SGarrett Wollman 
15066f359e28SJohn Baldwin static void
1507f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1508f731f104SBill Paul {
15095cb8c31aSYaroslav Tykhiy 
1510d148c2a2SMatt Joras 	VLAN_XLOCK();
151128cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
1512d148c2a2SMatt Joras 	VLAN_XUNLOCK();
15135cb8c31aSYaroslav Tykhiy }
15145cb8c31aSYaroslav Tykhiy 
15156f359e28SJohn Baldwin static void
151628cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
15175cb8c31aSYaroslav Tykhiy {
151875ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1519f731f104SBill Paul 	struct vlan_mc_entry *mc;
1520f731f104SBill Paul 	struct ifvlan *ifv;
1521c725524cSJack F Vogel 	struct ifnet  *parent;
152228cc4d37SJohn Baldwin 	int error;
1523f731f104SBill Paul 
1524d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
15254faedfe8SSam Leffler 
1526f731f104SBill Paul 	ifv = ifp->if_softc;
152775ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
152822893351SJack F Vogel 	parent = NULL;
1529f731f104SBill Paul 
153022893351SJack F Vogel 	if (trunk != NULL) {
153122893351SJack F Vogel 		parent = trunk->parent;
15321b2a4f7aSBill Fenner 
1533f731f104SBill Paul 		/*
1534f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1535f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
15361b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1537f731f104SBill Paul 		 */
1538b08d611dSMatt Macy 		while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
15396f359e28SJohn Baldwin 			/*
154028cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1541b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
154228cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
154328cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
154428cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
154528cc4d37SJohn Baldwin 			 * succeed.
15466f359e28SJohn Baldwin 			 */
154728cc4d37SJohn Baldwin 			if (!departing) {
154828cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1549e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
155028cc4d37SJohn Baldwin 				if (error)
155128cc4d37SJohn Baldwin 					if_printf(ifp,
155228cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
155328cc4d37SJohn Baldwin 					    error);
155428cc4d37SJohn Baldwin 			}
1555b08d611dSMatt Macy 			CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
15562a4bd982SGleb Smirnoff 			NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
1557f731f104SBill Paul 		}
1558a3814acfSSam Leffler 
15591cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1560d148c2a2SMatt Joras 
156175ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
156275ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
156375ee267cSGleb Smirnoff 
156475ee267cSGleb Smirnoff 		/*
156575ee267cSGleb Smirnoff 		 * Check if we were the last.
156675ee267cSGleb Smirnoff 		 */
156775ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
15682d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
1569b08d611dSMatt Macy 			NET_EPOCH_WAIT();
157075ee267cSGleb Smirnoff 			trunk_destroy(trunk);
1571d148c2a2SMatt Joras 		}
15721b2a4f7aSBill Fenner 	}
1573f731f104SBill Paul 
1574f731f104SBill Paul 	/* Disconnect from parent. */
15751cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
15761cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
15775cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
15785cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
15795cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1580f731f104SBill Paul 
158122893351SJack F Vogel 	/*
158222893351SJack F Vogel 	 * Only dispatch an event if vlan was
158322893351SJack F Vogel 	 * attached, otherwise there is nothing
158422893351SJack F Vogel 	 * to cleanup anyway.
158522893351SJack F Vogel 	 */
158622893351SJack F Vogel 	if (parent != NULL)
15877983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1588f731f104SBill Paul }
1589f731f104SBill Paul 
15901cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1591f731f104SBill Paul static int
15921cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
15931cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1594a3814acfSSam Leffler {
15951cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
15961cf236fbSYaroslav Tykhiy 	int error;
1597a3814acfSSam Leffler 
1598d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1599a3814acfSSam Leffler 
16001cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
16011cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
16021cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
16031cf236fbSYaroslav Tykhiy 
16041cf236fbSYaroslav Tykhiy 	/*
16051cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
16061cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
16071cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
16081cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
16091cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
16101cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
16111cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
16121cf236fbSYaroslav Tykhiy 	 */
16131cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
161475ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
16151cf236fbSYaroslav Tykhiy 		if (error)
1616a3814acfSSam Leffler 			return (error);
16171cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
16181cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
16191cf236fbSYaroslav Tykhiy 	}
16201cf236fbSYaroslav Tykhiy 	return (0);
16211cf236fbSYaroslav Tykhiy }
16221cf236fbSYaroslav Tykhiy 
16231cf236fbSYaroslav Tykhiy /*
16241cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
16251cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
16261cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
16271cf236fbSYaroslav Tykhiy  */
16281cf236fbSYaroslav Tykhiy static int
16291cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
16301cf236fbSYaroslav Tykhiy {
16311cf236fbSYaroslav Tykhiy 	int error, i;
16321cf236fbSYaroslav Tykhiy 
16331cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
16341cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
16351cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
16361cf236fbSYaroslav Tykhiy 		if (error)
16371cf236fbSYaroslav Tykhiy 			return (error);
16381cf236fbSYaroslav Tykhiy 	}
16391cf236fbSYaroslav Tykhiy 	return (0);
1640a3814acfSSam Leffler }
1641a3814acfSSam Leffler 
1642127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1643127d7b2dSAndre Oppermann static void
1644a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1645127d7b2dSAndre Oppermann {
1646b2807792SGleb Smirnoff 	struct epoch_tracker et;
1647d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1648127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
1649127d7b2dSAndre Oppermann 
1650b2807792SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1651d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1652b2807792SGleb Smirnoff 	if (trunk == NULL) {
1653b2807792SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1654d148c2a2SMatt Joras 		return;
1655b2807792SGleb Smirnoff 	}
1656d148c2a2SMatt Joras 
1657d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1658d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
1659aad0be7aSGleb Smirnoff 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1660fc74a9f9SBrooks Davis 		if_link_state_change(ifv->ifv_ifp,
166175ee267cSGleb Smirnoff 		    trunk->parent->if_link_state);
1662127d7b2dSAndre Oppermann 	}
1663d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1664b2807792SGleb Smirnoff 	NET_EPOCH_EXIT(et);
166575ee267cSGleb Smirnoff }
166675ee267cSGleb Smirnoff 
166775ee267cSGleb Smirnoff static void
166875ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
166975ee267cSGleb Smirnoff {
1670d148c2a2SMatt Joras 	struct ifnet *p;
1671d148c2a2SMatt Joras 	struct ifnet *ifp;
16729fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
1673d89baa5aSAlexander Motin 	int cap = 0, ena = 0, mena;
1674d89baa5aSAlexander Motin 	u_long hwa = 0;
167575ee267cSGleb Smirnoff 
1676a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
1677b8a6e03fSGleb Smirnoff 	VLAN_SXLOCK_ASSERT();
1678b8a6e03fSGleb Smirnoff 
1679d148c2a2SMatt Joras 	p = PARENT(ifv);
1680d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
168175ee267cSGleb Smirnoff 
1682d89baa5aSAlexander Motin 	/* Mask parent interface enabled capabilities disabled by user. */
1683d89baa5aSAlexander Motin 	mena = p->if_capenable & ifv->ifv_capenable;
1684d89baa5aSAlexander Motin 
168575ee267cSGleb Smirnoff 	/*
168675ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
168775ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
168875ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
168975ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
169075ee267cSGleb Smirnoff 	 */
169175ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1692d89baa5aSAlexander Motin 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
169375ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
169475ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1695d89baa5aSAlexander Motin 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1696d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM)
1697d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1698d89baa5aSAlexander Motin 			    CSUM_UDP | CSUM_SCTP);
1699d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM_IPV6)
1700d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1701d89baa5aSAlexander Motin 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
170275ee267cSGleb Smirnoff 	}
1703d89baa5aSAlexander Motin 
17049b76d9cbSPyun YongHyeon 	/*
17059b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
17069b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
17079b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
17089b76d9cbSPyun YongHyeon 	 */
17099fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
17109fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
17119fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
17129b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1713d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TSO;
17149b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1715d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TSO;
1716d89baa5aSAlexander Motin 		if (ena & IFCAP_TSO)
1717d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & CSUM_TSO;
17189b76d9cbSPyun YongHyeon 	}
171909fe6320SNavdeep Parhar 
172009fe6320SNavdeep Parhar 	/*
172159150e91SAlexander Motin 	 * If the parent interface can do LRO and checksum offloading on
172259150e91SAlexander Motin 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
172359150e91SAlexander Motin 	 * cost nothing, while false negative may lead to some confusions.
172459150e91SAlexander Motin 	 */
172559150e91SAlexander Motin 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
172659150e91SAlexander Motin 		cap |= p->if_capabilities & IFCAP_LRO;
172759150e91SAlexander Motin 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
172859150e91SAlexander Motin 		ena |= p->if_capenable & IFCAP_LRO;
172959150e91SAlexander Motin 
173059150e91SAlexander Motin 	/*
173109fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
173209fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
173309fe6320SNavdeep Parhar 	 *
173409fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
173509fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
173609fe6320SNavdeep Parhar 	 * with its own bit.
173709fe6320SNavdeep Parhar 	 */
173809fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
173909fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1740d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TOE;
174109fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
174209fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
1743d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TOE;
174409fe6320SNavdeep Parhar 	}
1745f3e7afe2SHans Petter Selasky 
1746d89baa5aSAlexander Motin 	/*
1747d89baa5aSAlexander Motin 	 * If the parent interface supports dynamic link state, so does the
1748d89baa5aSAlexander Motin 	 * VLAN interface.
1749d89baa5aSAlexander Motin 	 */
1750d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1751d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_LINKSTATE);
1752d89baa5aSAlexander Motin 
1753f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1754f3e7afe2SHans Petter Selasky 	/*
1755f3e7afe2SHans Petter Selasky 	 * If the parent interface supports ratelimiting, so does the
1756f3e7afe2SHans Petter Selasky 	 * VLAN interface.
1757f3e7afe2SHans Petter Selasky 	 */
1758d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1759d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_TXRTLMT);
1760f3e7afe2SHans Petter Selasky #endif
1761d89baa5aSAlexander Motin 
176266d0c056SJohn Baldwin 	/*
176366d0c056SJohn Baldwin 	 * If the parent interface supports unmapped mbufs, so does
176466d0c056SJohn Baldwin 	 * the VLAN interface.  Note that this should be fine even for
176566d0c056SJohn Baldwin 	 * interfaces that don't support hardware tagging as headers
176666d0c056SJohn Baldwin 	 * are prepended in normal mbufs to unmapped mbufs holding
176766d0c056SJohn Baldwin 	 * payload data.
176866d0c056SJohn Baldwin 	 */
176966d0c056SJohn Baldwin 	cap |= (p->if_capabilities & IFCAP_NOMAP);
177066d0c056SJohn Baldwin 	ena |= (mena & IFCAP_NOMAP);
177166d0c056SJohn Baldwin 
1772b2e60773SJohn Baldwin 	/*
1773b2e60773SJohn Baldwin 	 * If the parent interface can offload encryption and segmentation
1774b2e60773SJohn Baldwin 	 * of TLS records over TCP, propagate it's capability to the VLAN
1775b2e60773SJohn Baldwin 	 * interface.
1776b2e60773SJohn Baldwin 	 *
1777b2e60773SJohn Baldwin 	 * All TLS drivers in the tree today can deal with VLANs.  If
1778b2e60773SJohn Baldwin 	 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1779b2e60773SJohn Baldwin 	 * defined.
1780b2e60773SJohn Baldwin 	 */
1781b2e60773SJohn Baldwin 	if (p->if_capabilities & IFCAP_TXTLS)
1782b2e60773SJohn Baldwin 		cap |= p->if_capabilities & IFCAP_TXTLS;
1783b2e60773SJohn Baldwin 	if (p->if_capenable & IFCAP_TXTLS)
1784b2e60773SJohn Baldwin 		ena |= mena & IFCAP_TXTLS;
1785b2e60773SJohn Baldwin 
1786d89baa5aSAlexander Motin 	ifp->if_capabilities = cap;
1787d89baa5aSAlexander Motin 	ifp->if_capenable = ena;
1788d89baa5aSAlexander Motin 	ifp->if_hwassist = hwa;
178975ee267cSGleb Smirnoff }
179075ee267cSGleb Smirnoff 
179175ee267cSGleb Smirnoff static void
179275ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
179375ee267cSGleb Smirnoff {
1794b2807792SGleb Smirnoff 	struct epoch_tracker et;
1795d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
179675ee267cSGleb Smirnoff 	struct ifvlan *ifv;
179775ee267cSGleb Smirnoff 
1798d148c2a2SMatt Joras 	VLAN_SLOCK();
1799d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1800d148c2a2SMatt Joras 	if (trunk == NULL) {
1801d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1802d148c2a2SMatt Joras 		return;
1803d148c2a2SMatt Joras 	}
1804b2807792SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1805b8a6e03fSGleb Smirnoff 	VLAN_FOREACH(ifv, trunk)
180675ee267cSGleb Smirnoff 		vlan_capabilities(ifv);
1807b2807792SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1808d148c2a2SMatt Joras 	VLAN_SUNLOCK();
1809127d7b2dSAndre Oppermann }
1810127d7b2dSAndre Oppermann 
1811a3814acfSSam Leffler static int
1812cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
18132cc2df49SGarrett Wollman {
18142cc2df49SGarrett Wollman 	struct ifnet *p;
18152cc2df49SGarrett Wollman 	struct ifreq *ifr;
1816e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
18172cc2df49SGarrett Wollman 	struct ifvlan *ifv;
18182d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
18192cc2df49SGarrett Wollman 	struct vlanreq vlr;
1820*84becee1SAlexander Motin 	int error = 0, oldmtu;
18212cc2df49SGarrett Wollman 
18222cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1823e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
18242cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
18252cc2df49SGarrett Wollman 
18262cc2df49SGarrett Wollman 	switch (cmd) {
1827e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1828e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1829e4cd31ddSJeff Roberson #ifdef INET
1830e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1831e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1832e4cd31ddSJeff Roberson #endif
1833e4cd31ddSJeff Roberson 		break;
1834e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
183538d958a6SBrooks Davis 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
183638d958a6SBrooks Davis 		    ifp->if_addrlen);
1837e4cd31ddSJeff Roberson 		break;
1838b3cca108SBill Fenner 	case SIOCGIFMEDIA:
1839d148c2a2SMatt Joras 		VLAN_SLOCK();
184075ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1841d8564efdSEd Maste 			p = PARENT(ifv);
18429bcf3ae4SAlexander Motin 			if_ref(p);
1843d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
18449bcf3ae4SAlexander Motin 			if_rele(p);
1845b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1846b3cca108SBill Fenner 			if (error == 0) {
1847b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1848b3cca108SBill Fenner 
1849b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1850b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1851b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1852b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1853b3cca108SBill Fenner 						ifmr->ifm_ulist,
1854b3cca108SBill Fenner 						sizeof(int));
1855b3cca108SBill Fenner 				}
1856b3cca108SBill Fenner 			}
18574faedfe8SSam Leffler 		} else {
1858b3cca108SBill Fenner 			error = EINVAL;
18594faedfe8SSam Leffler 		}
1860d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1861b3cca108SBill Fenner 		break;
1862b3cca108SBill Fenner 
1863b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1864b3cca108SBill Fenner 		error = EINVAL;
1865b3cca108SBill Fenner 		break;
1866b3cca108SBill Fenner 
18672cc2df49SGarrett Wollman 	case SIOCSIFMTU:
18682cc2df49SGarrett Wollman 		/*
18692cc2df49SGarrett Wollman 		 * Set the interface MTU.
18702cc2df49SGarrett Wollman 		 */
1871d148c2a2SMatt Joras 		VLAN_SLOCK();
1872d148c2a2SMatt Joras 		trunk = TRUNK(ifv);
1873d148c2a2SMatt Joras 		if (trunk != NULL) {
1874d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1875a3814acfSSam Leffler 			if (ifr->ifr_mtu >
187675ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1877a3814acfSSam Leffler 			    ifr->ifr_mtu <
1878a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
18792cc2df49SGarrett Wollman 				error = EINVAL;
1880a3814acfSSam Leffler 			else
18812cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1882d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
1883a3814acfSSam Leffler 		} else
1884a3814acfSSam Leffler 			error = EINVAL;
1885d148c2a2SMatt Joras 		VLAN_SUNLOCK();
18862cc2df49SGarrett Wollman 		break;
18872cc2df49SGarrett Wollman 
18882cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1889ccf7ba97SMarko Zec #ifdef VIMAGE
189015f6780eSRobert Watson 		/*
189115f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
189215f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
189315f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
189415f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
18955a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
189615f6780eSRobert Watson 		 */
1897ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1898ccf7ba97SMarko Zec 			error = EPERM;
1899ccf7ba97SMarko Zec 			break;
1900ccf7ba97SMarko Zec 		}
1901ccf7ba97SMarko Zec #endif
1902541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
19032cc2df49SGarrett Wollman 		if (error)
19042cc2df49SGarrett Wollman 			break;
19052cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1906f731f104SBill Paul 			vlan_unconfig(ifp);
19072cc2df49SGarrett Wollman 			break;
19082cc2df49SGarrett Wollman 		}
19099bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
19101bdc73d3SEd Maste 		if (p == NULL) {
19112cc2df49SGarrett Wollman 			error = ENOENT;
19122cc2df49SGarrett Wollman 			break;
19132cc2df49SGarrett Wollman 		}
1914*84becee1SAlexander Motin 		oldmtu = ifp->if_mtu;
191575ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
19169bcf3ae4SAlexander Motin 		if_rele(p);
1917*84becee1SAlexander Motin 
1918*84becee1SAlexander Motin 		/*
1919*84becee1SAlexander Motin 		 * VLAN MTU may change during addition of the vlandev.
1920*84becee1SAlexander Motin 		 * If it did, do network layer specific procedure.
1921*84becee1SAlexander Motin 		 */
1922*84becee1SAlexander Motin 		if (ifp->if_mtu != oldmtu) {
1923*84becee1SAlexander Motin #ifdef INET6
1924*84becee1SAlexander Motin 			nd6_setmtu(ifp);
1925*84becee1SAlexander Motin #endif
1926*84becee1SAlexander Motin 			rt_updatemtu(ifp);
1927*84becee1SAlexander Motin 		}
19282cc2df49SGarrett Wollman 		break;
19292cc2df49SGarrett Wollman 
19302cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1931ccf7ba97SMarko Zec #ifdef VIMAGE
1932ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1933ccf7ba97SMarko Zec 			error = EPERM;
1934ccf7ba97SMarko Zec 			break;
1935ccf7ba97SMarko Zec 		}
1936ccf7ba97SMarko Zec #endif
193715a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
1938d148c2a2SMatt Joras 		VLAN_SLOCK();
193975ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
194075ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
19419bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
19427983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
19432cc2df49SGarrett Wollman 		}
1944d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1945541d96aaSBrooks Davis 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
19462cc2df49SGarrett Wollman 		break;
19472cc2df49SGarrett Wollman 
19482cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
19492cc2df49SGarrett Wollman 		/*
19501cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
19511cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
19522cc2df49SGarrett Wollman 		 */
1953d148c2a2SMatt Joras 		VLAN_XLOCK();
195475ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
19551cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
1956d148c2a2SMatt Joras 		VLAN_XUNLOCK();
19572cc2df49SGarrett Wollman 		break;
1958a3814acfSSam Leffler 
1959f731f104SBill Paul 	case SIOCADDMULTI:
1960f731f104SBill Paul 	case SIOCDELMULTI:
196175ee267cSGleb Smirnoff 		/*
196275ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
196375ee267cSGleb Smirnoff 		 * when we do.
1964d148c2a2SMatt Joras 		 *
1965d148c2a2SMatt Joras 		 * XXX We need the rmlock here to avoid sleeping while
1966d148c2a2SMatt Joras 		 * holding in6_multi_mtx.
196775ee267cSGleb Smirnoff 		 */
1968b08d611dSMatt Macy 		VLAN_XLOCK();
19692d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
1970b08d611dSMatt Macy 		if (trunk != NULL)
1971f731f104SBill Paul 			error = vlan_setmulti(ifp);
1972b08d611dSMatt Macy 		VLAN_XUNLOCK();
197375ee267cSGleb Smirnoff 
1974b08d611dSMatt Macy 		break;
19752ccbbd06SMarcelo Araujo 	case SIOCGVLANPCP:
19762ccbbd06SMarcelo Araujo #ifdef VIMAGE
19772ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19782ccbbd06SMarcelo Araujo 			error = EPERM;
19792ccbbd06SMarcelo Araujo 			break;
19802ccbbd06SMarcelo Araujo 		}
19812ccbbd06SMarcelo Araujo #endif
19822ccbbd06SMarcelo Araujo 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
19832ccbbd06SMarcelo Araujo 		break;
19842ccbbd06SMarcelo Araujo 
19852ccbbd06SMarcelo Araujo 	case SIOCSVLANPCP:
19862ccbbd06SMarcelo Araujo #ifdef VIMAGE
19872ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19882ccbbd06SMarcelo Araujo 			error = EPERM;
19892ccbbd06SMarcelo Araujo 			break;
19902ccbbd06SMarcelo Araujo 		}
19912ccbbd06SMarcelo Araujo #endif
19922ccbbd06SMarcelo Araujo 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
19932ccbbd06SMarcelo Araujo 		if (error)
19942ccbbd06SMarcelo Araujo 			break;
19952ccbbd06SMarcelo Araujo 		if (ifr->ifr_vlan_pcp > 7) {
19962ccbbd06SMarcelo Araujo 			error = EINVAL;
19972ccbbd06SMarcelo Araujo 			break;
19982ccbbd06SMarcelo Araujo 		}
19992ccbbd06SMarcelo Araujo 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
200032a52e9eSNavdeep Parhar 		ifp->if_pcp = ifv->ifv_pcp;
20012ccbbd06SMarcelo Araujo 		vlan_tag_recalculate(ifv);
20024a381a9eSHans Petter Selasky 		/* broadcast event about PCP change */
20034a381a9eSHans Petter Selasky 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
20042ccbbd06SMarcelo Araujo 		break;
20052ccbbd06SMarcelo Araujo 
2006d89baa5aSAlexander Motin 	case SIOCSIFCAP:
2007d148c2a2SMatt Joras 		VLAN_SLOCK();
2008d89baa5aSAlexander Motin 		ifv->ifv_capenable = ifr->ifr_reqcap;
2009d89baa5aSAlexander Motin 		trunk = TRUNK(ifv);
20106dcec895SGleb Smirnoff 		if (trunk != NULL) {
20116dcec895SGleb Smirnoff 			struct epoch_tracker et;
20126dcec895SGleb Smirnoff 
20136dcec895SGleb Smirnoff 			NET_EPOCH_ENTER(et);
2014d89baa5aSAlexander Motin 			vlan_capabilities(ifv);
20156dcec895SGleb Smirnoff 			NET_EPOCH_EXIT(et);
20166dcec895SGleb Smirnoff 		}
2017d148c2a2SMatt Joras 		VLAN_SUNLOCK();
2018d89baa5aSAlexander Motin 		break;
2019d89baa5aSAlexander Motin 
20202cc2df49SGarrett Wollman 	default:
2021e4cd31ddSJeff Roberson 		error = EINVAL;
2022e4cd31ddSJeff Roberson 		break;
20232cc2df49SGarrett Wollman 	}
202415a66c21SBruce M Simpson 
202515a66c21SBruce M Simpson 	return (error);
20262cc2df49SGarrett Wollman }
2027f3e7afe2SHans Petter Selasky 
2028b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
2029f3e7afe2SHans Petter Selasky static int
2030f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp,
2031f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *params,
2032f3e7afe2SHans Petter Selasky     struct m_snd_tag **ppmt)
2033f3e7afe2SHans Petter Selasky {
2034fb3bc596SJohn Baldwin 	struct epoch_tracker et;
2035fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2036fb3bc596SJohn Baldwin 	struct ifvlan *ifv;
2037fb3bc596SJohn Baldwin 	struct ifnet *parent;
2038fb3bc596SJohn Baldwin 	int error;
2039f3e7afe2SHans Petter Selasky 
2040fb3bc596SJohn Baldwin 	NET_EPOCH_ENTER(et);
2041fb3bc596SJohn Baldwin 	ifv = ifp->if_softc;
2042fb3bc596SJohn Baldwin 	if (ifv->ifv_trunk != NULL)
2043fb3bc596SJohn Baldwin 		parent = PARENT(ifv);
2044fb3bc596SJohn Baldwin 	else
2045fb3bc596SJohn Baldwin 		parent = NULL;
2046fb3bc596SJohn Baldwin 	if (parent == NULL || parent->if_snd_tag_alloc == NULL) {
2047fb3bc596SJohn Baldwin 		NET_EPOCH_EXIT(et);
2048f3e7afe2SHans Petter Selasky 		return (EOPNOTSUPP);
2049fb3bc596SJohn Baldwin 	}
2050fb3bc596SJohn Baldwin 	if_ref(parent);
2051fb3bc596SJohn Baldwin 	NET_EPOCH_EXIT(et);
2052fb3bc596SJohn Baldwin 
2053fb3bc596SJohn Baldwin 	vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2054fb3bc596SJohn Baldwin 	if (vst == NULL) {
2055fb3bc596SJohn Baldwin 		if_rele(parent);
2056fb3bc596SJohn Baldwin 		return (ENOMEM);
2057fb3bc596SJohn Baldwin 	}
2058fb3bc596SJohn Baldwin 
2059fb3bc596SJohn Baldwin 	error = parent->if_snd_tag_alloc(parent, params, &vst->tag);
2060fb3bc596SJohn Baldwin 	if_rele(parent);
2061fb3bc596SJohn Baldwin 	if (error) {
2062fb3bc596SJohn Baldwin 		free(vst, M_VLAN);
2063fb3bc596SJohn Baldwin 		return (error);
2064fb3bc596SJohn Baldwin 	}
2065fb3bc596SJohn Baldwin 
2066fb3bc596SJohn Baldwin 	m_snd_tag_init(&vst->com, ifp);
2067fb3bc596SJohn Baldwin 
2068fb3bc596SJohn Baldwin 	*ppmt = &vst->com;
2069fb3bc596SJohn Baldwin 	return (0);
2070fb3bc596SJohn Baldwin }
2071fb3bc596SJohn Baldwin 
2072fb3bc596SJohn Baldwin static int
2073fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst,
2074fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *params)
2075fb3bc596SJohn Baldwin {
2076fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2077fb3bc596SJohn Baldwin 
2078fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2079fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params));
2080fb3bc596SJohn Baldwin }
2081fb3bc596SJohn Baldwin 
2082fb3bc596SJohn Baldwin static int
2083fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst,
2084fb3bc596SJohn Baldwin     union if_snd_tag_query_params *params)
2085fb3bc596SJohn Baldwin {
2086fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2087fb3bc596SJohn Baldwin 
2088fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2089fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_query(vst->tag, params));
2090f3e7afe2SHans Petter Selasky }
2091fa91f845SRandall Stewart 
2092fa91f845SRandall Stewart static void
2093fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst)
2094fa91f845SRandall Stewart {
2095fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2096fb3bc596SJohn Baldwin 
2097fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2098fb3bc596SJohn Baldwin 	m_snd_tag_rele(vst->tag);
2099fb3bc596SJohn Baldwin 	free(vst, M_VLAN);
2100fa91f845SRandall Stewart }
2101f3e7afe2SHans Petter Selasky #endif
2102