xref: /freebsd/sys/net/if_vlan.c (revision c7cffd65c5d858425e90b847d2e8e583e3b13bf7)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32ccbbd06SMarcelo Araujo  * Copyright 2012 ADARA Networks, Inc.
4d148c2a2SMatt Joras  * Copyright 2017 Dell EMC Isilon
52ccbbd06SMarcelo Araujo  *
62ccbbd06SMarcelo Araujo  * Portions of this software were developed by Robert N. M. Watson under
72ccbbd06SMarcelo Araujo  * contract to ADARA Networks, Inc.
82cc2df49SGarrett Wollman  *
92cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
102cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
112cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
122cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
132cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
142cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
152cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
162cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
172cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
182cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
192cc2df49SGarrett Wollman  * warranty.
202cc2df49SGarrett Wollman  *
212cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
222cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
232cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
242cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
252cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
262cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
272cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
282cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
292cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
302cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
312cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322cc2df49SGarrett Wollman  * SUCH DAMAGE.
332cc2df49SGarrett Wollman  */
342cc2df49SGarrett Wollman 
352cc2df49SGarrett Wollman /*
362cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
372ccbbd06SMarcelo Araujo  * This is sort of sneaky in the implementation, since
382cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
392cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
402cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
41d9b1d615SJohn Baldwin  * ether_output() sends to us via if_transmit(), rewrite them for
42d9b1d615SJohn Baldwin  * use by the real outgoing interface, and ask it to send them.
432cc2df49SGarrett Wollman  */
442cc2df49SGarrett Wollman 
458b2d9181SPyun YongHyeon #include <sys/cdefs.h>
468b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$");
478b2d9181SPyun YongHyeon 
482c5b403eSOleg Bulyzhin #include "opt_inet.h"
4984becee1SAlexander Motin #include "opt_inet6.h"
50b2e60773SJohn Baldwin #include "opt_kern_tls.h"
5175ee267cSGleb Smirnoff #include "opt_vlan.h"
52f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
532cc2df49SGarrett Wollman 
542cc2df49SGarrett Wollman #include <sys/param.h>
55c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
562cc2df49SGarrett Wollman #include <sys/kernel.h>
5775ee267cSGleb Smirnoff #include <sys/lock.h>
58f731f104SBill Paul #include <sys/malloc.h>
592cc2df49SGarrett Wollman #include <sys/mbuf.h>
602b120974SPeter Wemm #include <sys/module.h>
61772b000fSAlexander V. Chernikov #include <sys/rmlock.h>
622ccbbd06SMarcelo Araujo #include <sys/priv.h>
63f731f104SBill Paul #include <sys/queue.h>
642cc2df49SGarrett Wollman #include <sys/socket.h>
652cc2df49SGarrett Wollman #include <sys/sockio.h>
662cc2df49SGarrett Wollman #include <sys/sysctl.h>
672cc2df49SGarrett Wollman #include <sys/systm.h>
68e4cd31ddSJeff Roberson #include <sys/sx.h>
69d148c2a2SMatt Joras #include <sys/taskqueue.h>
702cc2df49SGarrett Wollman 
712cc2df49SGarrett Wollman #include <net/bpf.h>
722cc2df49SGarrett Wollman #include <net/ethernet.h>
732cc2df49SGarrett Wollman #include <net/if.h>
7476039bc8SGleb Smirnoff #include <net/if_var.h>
75f889d2efSBrooks Davis #include <net/if_clone.h>
762cc2df49SGarrett Wollman #include <net/if_dl.h>
772cc2df49SGarrett Wollman #include <net/if_types.h>
782cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
7984becee1SAlexander Motin #include <net/route.h>
804b79449eSBjoern A. Zeeb #include <net/vnet.h>
812cc2df49SGarrett Wollman 
822c5b403eSOleg Bulyzhin #ifdef INET
832c5b403eSOleg Bulyzhin #include <netinet/in.h>
842c5b403eSOleg Bulyzhin #include <netinet/if_ether.h>
852c5b403eSOleg Bulyzhin #endif
862c5b403eSOleg Bulyzhin 
8784becee1SAlexander Motin #ifdef INET6
8884becee1SAlexander Motin /*
8984becee1SAlexander Motin  * XXX: declare here to avoid to include many inet6 related files..
9084becee1SAlexander Motin  * should be more generalized?
9184becee1SAlexander Motin  */
9284becee1SAlexander Motin extern void	nd6_setmtu(struct ifnet *);
9384becee1SAlexander Motin #endif
9484becee1SAlexander Motin 
9575ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
9664a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
9775ee267cSGleb Smirnoff 
982dc879b3SYaroslav Tykhiy #define	UP_AND_RUNNING(ifp) \
992dc879b3SYaroslav Tykhiy     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
1002dc879b3SYaroslav Tykhiy 
101b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan);
10275ee267cSGleb Smirnoff 
10375ee267cSGleb Smirnoff struct ifvlantrunk {
10475ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
105b08d611dSMatt Macy 	struct	mtx	lock;
10675ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
1075cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
1085cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
10975ee267cSGleb Smirnoff #else
11075ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
11175ee267cSGleb Smirnoff 	uint16_t	hmask;
11275ee267cSGleb Smirnoff 	uint16_t	hwidth;
11375ee267cSGleb Smirnoff #endif
11475ee267cSGleb Smirnoff 	int		refcnt;
11575ee267cSGleb Smirnoff };
1169d4fe4b2SBrooks Davis 
117b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
118fb3bc596SJohn Baldwin struct vlan_snd_tag {
119fb3bc596SJohn Baldwin 	struct m_snd_tag com;
120fb3bc596SJohn Baldwin 	struct m_snd_tag *tag;
121fb3bc596SJohn Baldwin };
122fb3bc596SJohn Baldwin 
123fb3bc596SJohn Baldwin static inline struct vlan_snd_tag *
124fb3bc596SJohn Baldwin mst_to_vst(struct m_snd_tag *mst)
125fb3bc596SJohn Baldwin {
126fb3bc596SJohn Baldwin 
127fb3bc596SJohn Baldwin 	return (__containerof(mst, struct vlan_snd_tag, com));
128fb3bc596SJohn Baldwin }
129fb3bc596SJohn Baldwin #endif
130fb3bc596SJohn Baldwin 
131d148c2a2SMatt Joras /*
132d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk with
133d148c2a2SMatt Joras  * the assumption that none will be added/removed during iteration.
134d148c2a2SMatt Joras  */
135d148c2a2SMatt Joras #ifdef VLAN_ARRAY
136d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
137d148c2a2SMatt Joras 	size_t _i; \
138d148c2a2SMatt Joras 	for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
139d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
140d148c2a2SMatt Joras #else /* VLAN_ARRAY */
141d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
142d148c2a2SMatt Joras 	struct ifvlan *_next; \
143d148c2a2SMatt Joras 	size_t _i; \
144d148c2a2SMatt Joras 	for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
145b08d611dSMatt Macy 		CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
146d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
147d148c2a2SMatt Joras 
148d148c2a2SMatt Joras /*
149d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk while
150d148c2a2SMatt Joras  * also modifying the number of vlans on the trunk. The iteration continues
151d148c2a2SMatt Joras  * until some condition is met or there are no more vlans on the trunk.
152d148c2a2SMatt Joras  */
153d148c2a2SMatt Joras #ifdef VLAN_ARRAY
154d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
155d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
156d148c2a2SMatt Joras 	size_t _i; \
157d148c2a2SMatt Joras 	for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
158d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]))
159d148c2a2SMatt Joras #else /* VLAN_ARRAY */
160d148c2a2SMatt Joras /*
161d148c2a2SMatt Joras  * The hash table case is more complicated. We allow for the hash table to be
162d148c2a2SMatt Joras  * modified (i.e. vlans removed) while we are iterating over it. To allow for
163d148c2a2SMatt Joras  * this we must restart the iteration every time we "touch" something during
164d148c2a2SMatt Joras  * the iteration, since removal will resize the hash table and invalidate our
165d148c2a2SMatt Joras  * current position. If acting on the touched element causes the trunk to be
166d148c2a2SMatt Joras  * emptied, then iteration also stops.
167d148c2a2SMatt Joras  */
168d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
169d148c2a2SMatt Joras 	size_t _i; \
170d148c2a2SMatt Joras 	bool _touch = false; \
171d148c2a2SMatt Joras 	for (_i = 0; \
172d148c2a2SMatt Joras 	    !(_cond) && _i < (1 << (_trunk)->hwidth); \
173d148c2a2SMatt Joras 	    _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
174b08d611dSMatt Macy 		if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
175d148c2a2SMatt Joras 		    (_touch = true))
176d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
177d148c2a2SMatt Joras 
178a3814acfSSam Leffler struct vlan_mc_entry {
179e4cd31ddSJeff Roberson 	struct sockaddr_dl		mc_addr;
180b08d611dSMatt Macy 	CK_SLIST_ENTRY(vlan_mc_entry)	mc_entries;
181c32a9d66SHans Petter Selasky 	struct epoch_context		mc_epoch_ctx;
182a3814acfSSam Leffler };
183a3814acfSSam Leffler 
184a3814acfSSam Leffler struct ifvlan {
18575ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
186fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
18775ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
188*c7cffd65SAlexander V. Chernikov #define	PARENT(ifv)	(TRUNK(ifv)->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 */
195*c7cffd65SAlexander V. Chernikov 	struct  ether_8021q_tag ifv_qtag;
196*c7cffd65SAlexander V. Chernikov #define ifv_proto	ifv_qtag.proto
197*c7cffd65SAlexander V. Chernikov #define ifv_vid		ifv_qtag.vid
198*c7cffd65SAlexander V. Chernikov #define ifv_pcp		ifv_qtag.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() \
247*c7cffd65SAlexander V. Chernikov 	sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE)
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);
309*c7cffd65SAlexander V. Chernikov static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag,
310*c7cffd65SAlexander V. Chernikov 	uint16_t proto);
311a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
31275ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
31375ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
314f731f104SBill Paul 
315f941c31aSGleb Smirnoff static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
316f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
3176b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
318f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
319f889d2efSBrooks Davis 
3205cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
321ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
3225cb8c31aSYaroslav Tykhiy 
323d148c2a2SMatt Joras static  void vlan_lladdr_fn(void *arg, int pending);
324d148c2a2SMatt Joras 
32542a58907SGleb Smirnoff static struct if_clone *vlan_cloner;
3269d4fe4b2SBrooks Davis 
327ccf7ba97SMarko Zec #ifdef VIMAGE
3285f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
329ccf7ba97SMarko Zec #define	V_vlan_cloner	VNET(vlan_cloner)
330ccf7ba97SMarko Zec #endif
331ccf7ba97SMarko Zec 
33275ee267cSGleb Smirnoff static void
333c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx)
334c32a9d66SHans Petter Selasky {
335c32a9d66SHans Petter Selasky 	struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
336c32a9d66SHans Petter Selasky 	free(mc, M_VLAN);
337c32a9d66SHans Petter Selasky }
338c32a9d66SHans Petter Selasky 
339cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY
340cac30248SOleg Bulyzhin #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
341cac30248SOleg Bulyzhin 
342c32a9d66SHans Petter Selasky static void
34375ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
34475ee267cSGleb Smirnoff {
34575ee267cSGleb Smirnoff 	int i, n;
34675ee267cSGleb Smirnoff 
34775ee267cSGleb Smirnoff 	/*
34875ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
34975ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
35075ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
35175ee267cSGleb Smirnoff 	 */
35275ee267cSGleb Smirnoff 
35375ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
35475ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
35575ee267cSGleb Smirnoff 
35675ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
35775ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
35875ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
35975ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
36075ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
361b08d611dSMatt Macy 		CK_SLIST_INIT(&trunk->hash[i]);
36275ee267cSGleb Smirnoff }
36375ee267cSGleb Smirnoff 
36475ee267cSGleb Smirnoff static void
36575ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
36675ee267cSGleb Smirnoff {
36775ee267cSGleb Smirnoff #ifdef INVARIANTS
36875ee267cSGleb Smirnoff 	int i;
36975ee267cSGleb Smirnoff 
37075ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
37175ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
372b08d611dSMatt Macy 		KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
37375ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
37475ee267cSGleb Smirnoff #endif
37575ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
37675ee267cSGleb Smirnoff 	trunk->hash = NULL;
37775ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
37875ee267cSGleb Smirnoff }
37975ee267cSGleb Smirnoff 
38075ee267cSGleb Smirnoff static int
38175ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
38275ee267cSGleb Smirnoff {
38375ee267cSGleb Smirnoff 	int i, b;
38475ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
38575ee267cSGleb Smirnoff 
386b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
38775ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
38875ee267cSGleb Smirnoff 
38975ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
3907983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
391b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
3927983103aSRobert Watson 		if (ifv->ifv_vid == ifv2->ifv_vid)
39375ee267cSGleb Smirnoff 			return (EEXIST);
39475ee267cSGleb Smirnoff 
39575ee267cSGleb Smirnoff 	/*
39675ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
39775ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
39875ee267cSGleb Smirnoff 	 * buckets/2.
39975ee267cSGleb Smirnoff 	 */
40075ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
40175ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
4027983103aSRobert Watson 		i = HASH(ifv->ifv_vid, trunk->hmask);
40375ee267cSGleb Smirnoff 	}
404b08d611dSMatt Macy 	CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
40575ee267cSGleb Smirnoff 	trunk->refcnt++;
40675ee267cSGleb Smirnoff 
40775ee267cSGleb Smirnoff 	return (0);
40875ee267cSGleb Smirnoff }
40975ee267cSGleb Smirnoff 
41075ee267cSGleb Smirnoff static int
41175ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
41275ee267cSGleb Smirnoff {
41375ee267cSGleb Smirnoff 	int i, b;
41475ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
41575ee267cSGleb Smirnoff 
416b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
41775ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
41875ee267cSGleb Smirnoff 
41975ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
4207983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
421b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
42275ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
42375ee267cSGleb Smirnoff 			trunk->refcnt--;
424b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
42575ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
42675ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
42775ee267cSGleb Smirnoff 			return (0);
42875ee267cSGleb Smirnoff 		}
42975ee267cSGleb Smirnoff 
43075ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
43175ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
43275ee267cSGleb Smirnoff }
43375ee267cSGleb Smirnoff 
43475ee267cSGleb Smirnoff /*
43575ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
43675ee267cSGleb Smirnoff  */
43775ee267cSGleb Smirnoff static void
43875ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
43975ee267cSGleb Smirnoff {
44075ee267cSGleb Smirnoff 	struct ifvlan *ifv;
44175ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
44275ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
44375ee267cSGleb Smirnoff 
444b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
44575ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
44675ee267cSGleb Smirnoff 
44775ee267cSGleb Smirnoff 	if (howmuch == 0) {
44875ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
44975ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
45075ee267cSGleb Smirnoff 		return;
45175ee267cSGleb Smirnoff 	}
45275ee267cSGleb Smirnoff 
45375ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
45475ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
45575ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
45675ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
45775ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
45875ee267cSGleb Smirnoff 		return;
45975ee267cSGleb Smirnoff 
460b08d611dSMatt Macy 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
46175ee267cSGleb Smirnoff 	if (hash2 == NULL) {
46275ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
46375ee267cSGleb Smirnoff 		    __func__);
46475ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
46575ee267cSGleb Smirnoff 	}
46675ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
467b08d611dSMatt Macy 		CK_SLIST_INIT(&hash2[j]);
46875ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
469b08d611dSMatt Macy 		while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
470b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
4717983103aSRobert Watson 			j = HASH(ifv->ifv_vid, n2 - 1);
472b08d611dSMatt Macy 			CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
47375ee267cSGleb Smirnoff 		}
474b08d611dSMatt Macy 	NET_EPOCH_WAIT();
47575ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
47675ee267cSGleb Smirnoff 	trunk->hash = hash2;
47775ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
47875ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
479f84b2d69SYaroslav Tykhiy 
480f84b2d69SYaroslav Tykhiy 	if (bootverbose)
481f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
482f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
48375ee267cSGleb Smirnoff }
48475ee267cSGleb Smirnoff 
48575ee267cSGleb Smirnoff static __inline struct ifvlan *
4867983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
48775ee267cSGleb Smirnoff {
48875ee267cSGleb Smirnoff 	struct ifvlan *ifv;
48975ee267cSGleb Smirnoff 
490a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
49175ee267cSGleb Smirnoff 
492b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
4937983103aSRobert Watson 		if (ifv->ifv_vid == vid)
49475ee267cSGleb Smirnoff 			return (ifv);
49575ee267cSGleb Smirnoff 	return (NULL);
49675ee267cSGleb Smirnoff }
49775ee267cSGleb Smirnoff 
49875ee267cSGleb Smirnoff #if 0
49975ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
50075ee267cSGleb Smirnoff static void
50175ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
50275ee267cSGleb Smirnoff {
50375ee267cSGleb Smirnoff 	int i;
50475ee267cSGleb Smirnoff 	struct ifvlan *ifv;
50575ee267cSGleb Smirnoff 
50675ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
50775ee267cSGleb Smirnoff 		printf("%d: ", i);
508b08d611dSMatt Macy 		CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
50975ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
51075ee267cSGleb Smirnoff 		printf("\n");
51175ee267cSGleb Smirnoff 	}
51275ee267cSGleb Smirnoff }
51375ee267cSGleb Smirnoff #endif /* 0 */
514e4cd31ddSJeff Roberson #else
515e4cd31ddSJeff Roberson 
516e4cd31ddSJeff Roberson static __inline struct ifvlan *
5177983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
518e4cd31ddSJeff Roberson {
519e4cd31ddSJeff Roberson 
5207983103aSRobert Watson 	return trunk->vlans[vid];
521e4cd31ddSJeff Roberson }
522e4cd31ddSJeff Roberson 
523e4cd31ddSJeff Roberson static __inline int
524e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
525e4cd31ddSJeff Roberson {
526e4cd31ddSJeff Roberson 
5277983103aSRobert Watson 	if (trunk->vlans[ifv->ifv_vid] != NULL)
528e4cd31ddSJeff Roberson 		return EEXIST;
5297983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = ifv;
530e4cd31ddSJeff Roberson 	trunk->refcnt++;
531e4cd31ddSJeff Roberson 
532e4cd31ddSJeff Roberson 	return (0);
533e4cd31ddSJeff Roberson }
534e4cd31ddSJeff Roberson 
535e4cd31ddSJeff Roberson static __inline int
536e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
537e4cd31ddSJeff Roberson {
538e4cd31ddSJeff Roberson 
5397983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = NULL;
540e4cd31ddSJeff Roberson 	trunk->refcnt--;
541e4cd31ddSJeff Roberson 
542e4cd31ddSJeff Roberson 	return (0);
543e4cd31ddSJeff Roberson }
544e4cd31ddSJeff Roberson 
545e4cd31ddSJeff Roberson static __inline void
546e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk)
547e4cd31ddSJeff Roberson {
548e4cd31ddSJeff Roberson }
549e4cd31ddSJeff Roberson 
550e4cd31ddSJeff Roberson static __inline void
551e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk)
552e4cd31ddSJeff Roberson {
553e4cd31ddSJeff Roberson }
554e4cd31ddSJeff Roberson 
55575ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
55675ee267cSGleb Smirnoff 
55775ee267cSGleb Smirnoff static void
55875ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
55975ee267cSGleb Smirnoff {
560d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
56175ee267cSGleb Smirnoff 
56275ee267cSGleb Smirnoff 	vlan_freehash(trunk);
56375ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
56433499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
5659bcf3ae4SAlexander Motin 	if_rele(trunk->parent);
56675ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
56775ee267cSGleb Smirnoff }
56875ee267cSGleb Smirnoff 
569f731f104SBill Paul /*
570f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
571f731f104SBill Paul  * programming the multicast filter of the parent. This has the
572f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
573f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
574f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
575f731f104SBill Paul  * to avoid this: there really is only one physical interface.
576f731f104SBill Paul  */
5772b120974SPeter Wemm static int
5782b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
579f731f104SBill Paul {
580f731f104SBill Paul 	struct ifnet		*ifp_p;
5812d222cb7SAlexander Motin 	struct ifmultiaddr	*ifma;
582f731f104SBill Paul 	struct ifvlan		*sc;
583c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
584f731f104SBill Paul 	int			error;
585f731f104SBill Paul 
586b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
587d148c2a2SMatt Joras 
588f731f104SBill Paul 	/* Find the parent. */
589f731f104SBill Paul 	sc = ifp->if_softc;
59075ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
5911b2a4f7aSBill Fenner 
5928b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
5938b615593SMarko Zec 
594f731f104SBill Paul 	/* First, remove any existing filter entries. */
595b08d611dSMatt Macy 	while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
596b08d611dSMatt Macy 		CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
5972d222cb7SAlexander Motin 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
5982a4bd982SGleb Smirnoff 		NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
599f731f104SBill Paul 	}
600f731f104SBill Paul 
601f731f104SBill Paul 	/* Now program new ones. */
6022d222cb7SAlexander Motin 	IF_ADDR_WLOCK(ifp);
603d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
604f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
605f731f104SBill Paul 			continue;
60629c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
6072d222cb7SAlexander Motin 		if (mc == NULL) {
6082d222cb7SAlexander Motin 			IF_ADDR_WUNLOCK(ifp);
60929c2dfbeSBruce M Simpson 			return (ENOMEM);
6102d222cb7SAlexander Motin 		}
611e4cd31ddSJeff Roberson 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
612e4cd31ddSJeff Roberson 		mc->mc_addr.sdl_index = ifp_p->if_index;
613b08d611dSMatt Macy 		CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
6142d222cb7SAlexander Motin 	}
6152d222cb7SAlexander Motin 	IF_ADDR_WUNLOCK(ifp);
616b08d611dSMatt Macy 	CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
617e4cd31ddSJeff Roberson 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
6182d222cb7SAlexander Motin 		    NULL);
619f731f104SBill Paul 		if (error)
620f731f104SBill Paul 			return (error);
621f731f104SBill Paul 	}
622f731f104SBill Paul 
6238b615593SMarko Zec 	CURVNET_RESTORE();
624f731f104SBill Paul 	return (0);
625f731f104SBill Paul }
6262cc2df49SGarrett Wollman 
627a3814acfSSam Leffler /*
628ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
629ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
630ea4ca115SAndrew Thompson  * should also change it on all children vlans.
631ea4ca115SAndrew Thompson  */
632ea4ca115SAndrew Thompson static void
633ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
634ea4ca115SAndrew Thompson {
635a68cc388SGleb Smirnoff 	struct epoch_tracker et;
636ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
637d148c2a2SMatt Joras 	struct ifnet *ifv_ifp;
638d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
639d148c2a2SMatt Joras 	struct sockaddr_dl *sdl;
640ea4ca115SAndrew Thompson 
6417b7f772fSGleb Smirnoff 	/* Need the epoch since this is run on taskqueue_swi. */
642a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
643d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
644d148c2a2SMatt Joras 	if (trunk == NULL) {
645a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
646ea4ca115SAndrew Thompson 		return;
647d148c2a2SMatt Joras 	}
648ea4ca115SAndrew Thompson 
649ea4ca115SAndrew Thompson 	/*
650ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
651d148c2a2SMatt Joras 	 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
652d148c2a2SMatt Joras 	 * ioctl calls on the parent garbling the lladdr of the child vlan.
653ea4ca115SAndrew Thompson 	 */
654d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
655d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
656d148c2a2SMatt Joras 		/*
657d148c2a2SMatt Joras 		 * Copy new new lladdr into the ifv_ifp, enqueue a task
658d148c2a2SMatt Joras 		 * to actually call if_setlladdr. if_setlladdr needs to
659d148c2a2SMatt Joras 		 * be deferred to a taskqueue because it will call into
660d148c2a2SMatt Joras 		 * the if_vlan ioctl path and try to acquire the global
661d148c2a2SMatt Joras 		 * lock.
662d148c2a2SMatt Joras 		 */
663d148c2a2SMatt Joras 		ifv_ifp = ifv->ifv_ifp;
664d148c2a2SMatt Joras 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
665e4cd31ddSJeff Roberson 		    ifp->if_addrlen);
666d148c2a2SMatt Joras 		sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
667d148c2a2SMatt Joras 		sdl->sdl_alen = ifp->if_addrlen;
668d148c2a2SMatt Joras 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
6696117727bSAndrew Thompson 	}
670d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
671a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
672ea4ca115SAndrew Thompson }
673ea4ca115SAndrew Thompson 
674ea4ca115SAndrew Thompson /*
6755cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
6765cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
6775cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
6785428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
6795428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
6805cb8c31aSYaroslav Tykhiy  */
6815cb8c31aSYaroslav Tykhiy static void
6825cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
6835cb8c31aSYaroslav Tykhiy {
6845cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
685d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
6865cb8c31aSYaroslav Tykhiy 
6875428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
6885428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
6895428776eSJohn Baldwin 		return;
690d148c2a2SMatt Joras 	VLAN_XLOCK();
691d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
692d148c2a2SMatt Joras 	if (trunk == NULL) {
693d148c2a2SMatt Joras 		VLAN_XUNLOCK();
694d148c2a2SMatt Joras 		return;
695d148c2a2SMatt Joras 	}
6965428776eSJohn Baldwin 
6975cb8c31aSYaroslav Tykhiy 	/*
6985cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
6995cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
7005cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
7015cb8c31aSYaroslav Tykhiy 	 */
702d148c2a2SMatt Joras 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
703d148c2a2SMatt Joras 	    ifp->if_vlantrunk == NULL)
70428cc4d37SJohn Baldwin 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
705d148c2a2SMatt Joras 
7065cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
7075cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
708d148c2a2SMatt Joras 	VLAN_XUNLOCK();
7095cb8c31aSYaroslav Tykhiy }
7105cb8c31aSYaroslav Tykhiy 
7115cb8c31aSYaroslav Tykhiy /*
712e4cd31ddSJeff Roberson  * Return the trunk device for a virtual interface.
713e4cd31ddSJeff Roberson  */
714e4cd31ddSJeff Roberson static struct ifnet  *
715e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp)
716e4cd31ddSJeff Roberson {
717e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
718e4cd31ddSJeff Roberson 
719b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
720b8a6e03fSGleb Smirnoff 
721e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
722e4cd31ddSJeff Roberson 		return (NULL);
723d148c2a2SMatt Joras 
724e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
725e4cd31ddSJeff Roberson 	ifp = NULL;
726e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
727e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
728e4cd31ddSJeff Roberson 	return (ifp);
729e4cd31ddSJeff Roberson }
730e4cd31ddSJeff Roberson 
731e4cd31ddSJeff Roberson /*
7327983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
7337983103aSRobert Watson  * components such as Infiniband.
7347983103aSRobert Watson  *
7357983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
7367983103aSRobert Watson  * vlan_vid().
737e4cd31ddSJeff Roberson  */
738e4cd31ddSJeff Roberson static int
7397983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
740e4cd31ddSJeff Roberson {
741e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
742e4cd31ddSJeff Roberson 
743e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
744e4cd31ddSJeff Roberson 		return (EINVAL);
745e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
7467983103aSRobert Watson 	*vidp = ifv->ifv_vid;
747e4cd31ddSJeff Roberson 	return (0);
748e4cd31ddSJeff Roberson }
749e4cd31ddSJeff Roberson 
75032d2623aSNavdeep Parhar static int
75132d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
75232d2623aSNavdeep Parhar {
75332d2623aSNavdeep Parhar 	struct ifvlan *ifv;
75432d2623aSNavdeep Parhar 
75532d2623aSNavdeep Parhar 	if (ifp->if_type != IFT_L2VLAN)
75632d2623aSNavdeep Parhar 		return (EINVAL);
75732d2623aSNavdeep Parhar 	ifv = ifp->if_softc;
75832d2623aSNavdeep Parhar 	*pcpp = ifv->ifv_pcp;
75932d2623aSNavdeep Parhar 	return (0);
76032d2623aSNavdeep Parhar }
76132d2623aSNavdeep Parhar 
762e4cd31ddSJeff Roberson /*
763e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
764e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
765e4cd31ddSJeff Roberson  */
766e4cd31ddSJeff Roberson static void *
767e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
768e4cd31ddSJeff Roberson {
769e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
770e4cd31ddSJeff Roberson 
771e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
772e4cd31ddSJeff Roberson 		return (NULL);
773e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
774e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
775e4cd31ddSJeff Roberson }
776e4cd31ddSJeff Roberson 
777e4cd31ddSJeff Roberson /*
778e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
779e4cd31ddSJeff Roberson  * private per-instance data in.
780e4cd31ddSJeff Roberson  */
781e4cd31ddSJeff Roberson static int
782e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
783e4cd31ddSJeff Roberson {
784e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
785e4cd31ddSJeff Roberson 
786e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
787e4cd31ddSJeff Roberson 		return (EINVAL);
788e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
789e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
790e4cd31ddSJeff Roberson 	return (0);
791e4cd31ddSJeff Roberson }
792e4cd31ddSJeff Roberson 
793e4cd31ddSJeff Roberson /*
7947983103aSRobert Watson  * Return the vlan device present at the specific VID.
795e4cd31ddSJeff Roberson  */
796e4cd31ddSJeff Roberson static struct ifnet *
7977983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
798e4cd31ddSJeff Roberson {
799e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
800e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
801e4cd31ddSJeff Roberson 
802b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
803b8a6e03fSGleb Smirnoff 
804e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
805b8a6e03fSGleb Smirnoff 	if (trunk == NULL)
806e4cd31ddSJeff Roberson 		return (NULL);
807e4cd31ddSJeff Roberson 	ifp = NULL;
8087983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
809e4cd31ddSJeff Roberson 	if (ifv)
810e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
811e4cd31ddSJeff Roberson 	return (ifp);
812e4cd31ddSJeff Roberson }
813e4cd31ddSJeff Roberson 
814e4cd31ddSJeff Roberson /*
815a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
816a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
817a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
818a3814acfSSam Leffler  * set here.  No one else in the system should be aware of this so
819a3814acfSSam Leffler  * we use an explicit reference here.
820a3814acfSSam Leffler  */
821a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
822a3814acfSSam Leffler 
823984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
824a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
825127d7b2dSAndre Oppermann 
8262b120974SPeter Wemm static int
8272b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
8282b120974SPeter Wemm {
8299d4fe4b2SBrooks Davis 
8302b120974SPeter Wemm 	switch (type) {
8312b120974SPeter Wemm 	case MOD_LOAD:
8325cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
8335cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
8345cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
8355cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
836ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
837ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
838ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
839ea4ca115SAndrew Thompson 			return (ENOMEM);
840d148c2a2SMatt Joras 		VLAN_LOCKING_INIT();
8419d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
842127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
84375ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
844e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
845e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
846e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
847e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
84832d2623aSNavdeep Parhar 		vlan_pcp_p = vlan_pcp;
849e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
850ccf7ba97SMarko Zec #ifndef VIMAGE
85142a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
85242a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
853ccf7ba97SMarko Zec #endif
85425c0f7b3SYaroslav Tykhiy 		if (bootverbose)
85525c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
85625c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
85725c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
85825c0f7b3SYaroslav Tykhiy #else
85925c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
86025c0f7b3SYaroslav Tykhiy #endif
86125c0f7b3SYaroslav Tykhiy 
86225c0f7b3SYaroslav Tykhiy 			       "\n");
8632b120974SPeter Wemm 		break;
8642b120974SPeter Wemm 	case MOD_UNLOAD:
865ccf7ba97SMarko Zec #ifndef VIMAGE
86642a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
867ccf7ba97SMarko Zec #endif
8685cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
869ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
8709d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
871127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
87275ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
873e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
874e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
87509fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
87609fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
877e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
878d148c2a2SMatt Joras 		VLAN_LOCKING_DESTROY();
87925c0f7b3SYaroslav Tykhiy 		if (bootverbose)
88025c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
8819d4fe4b2SBrooks Davis 		break;
8823e019deaSPoul-Henning Kamp 	default:
8833e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
8842b120974SPeter Wemm 	}
88515a66c21SBruce M Simpson 	return (0);
8862b120974SPeter Wemm }
8872b120974SPeter Wemm 
8882b120974SPeter Wemm static moduledata_t vlan_mod = {
8892b120974SPeter Wemm 	"if_vlan",
8902b120974SPeter Wemm 	vlan_modevent,
8919823d527SKevin Lo 	0
8922b120974SPeter Wemm };
8932b120974SPeter Wemm 
8942b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
89511edc477SEd Maste MODULE_VERSION(if_vlan, 3);
8962cc2df49SGarrett Wollman 
897ccf7ba97SMarko Zec #ifdef VIMAGE
898ccf7ba97SMarko Zec static void
899ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
900ccf7ba97SMarko Zec {
901ccf7ba97SMarko Zec 
90242a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
90342a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
904ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
905ccf7ba97SMarko Zec }
906ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
907ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
908ccf7ba97SMarko Zec 
909ccf7ba97SMarko Zec static void
910ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
911ccf7ba97SMarko Zec {
912ccf7ba97SMarko Zec 
91342a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
914ccf7ba97SMarko Zec }
915eb03a443SKristof Provost VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
916ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
917ccf7ba97SMarko Zec #endif
918ccf7ba97SMarko Zec 
919f941c31aSGleb Smirnoff /*
920*c7cffd65SAlexander V. Chernikov  * Check for <etherif>.<vlan>[.<vlan> ...] style interface names.
921f941c31aSGleb Smirnoff  */
922f889d2efSBrooks Davis static struct ifnet *
923f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp)
9249d4fe4b2SBrooks Davis {
925f941c31aSGleb Smirnoff 	char ifname[IFNAMSIZ];
926f941c31aSGleb Smirnoff 	char *cp;
927f889d2efSBrooks Davis 	struct ifnet *ifp;
9287983103aSRobert Watson 	int vid;
929f889d2efSBrooks Davis 
930f941c31aSGleb Smirnoff 	strlcpy(ifname, name, IFNAMSIZ);
931*c7cffd65SAlexander V. Chernikov 	if ((cp = strrchr(ifname, '.')) == NULL)
932f941c31aSGleb Smirnoff 		return (NULL);
933f941c31aSGleb Smirnoff 	*cp = '\0';
9349bcf3ae4SAlexander Motin 	if ((ifp = ifunit_ref(ifname)) == NULL)
935f941c31aSGleb Smirnoff 		return (NULL);
936f941c31aSGleb Smirnoff 	/* Parse VID. */
9379bcf3ae4SAlexander Motin 	if (*++cp == '\0') {
9389bcf3ae4SAlexander Motin 		if_rele(ifp);
939f941c31aSGleb Smirnoff 		return (NULL);
9409bcf3ae4SAlexander Motin 	}
9417983103aSRobert Watson 	vid = 0;
942fb92ad4aSJohn Baldwin 	for(; *cp >= '0' && *cp <= '9'; cp++)
9437983103aSRobert Watson 		vid = (vid * 10) + (*cp - '0');
9449bcf3ae4SAlexander Motin 	if (*cp != '\0') {
9459bcf3ae4SAlexander Motin 		if_rele(ifp);
946f941c31aSGleb Smirnoff 		return (NULL);
9479bcf3ae4SAlexander Motin 	}
9487983103aSRobert Watson 	if (vidp != NULL)
9497983103aSRobert Watson 		*vidp = vid;
950f889d2efSBrooks Davis 
95115a66c21SBruce M Simpson 	return (ifp);
952f889d2efSBrooks Davis }
953f889d2efSBrooks Davis 
954f889d2efSBrooks Davis static int
955f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
956f889d2efSBrooks Davis {
9574be465abSAlexander V. Chernikov 	struct ifnet *ifp;
958f889d2efSBrooks Davis 	const char *cp;
959f889d2efSBrooks Davis 
9604be465abSAlexander V. Chernikov 	ifp = vlan_clone_match_ethervid(name, NULL);
9614be465abSAlexander V. Chernikov 	if (ifp != NULL) {
9624be465abSAlexander V. Chernikov 		if_rele(ifp);
963f889d2efSBrooks Davis 		return (1);
9644be465abSAlexander V. Chernikov 	}
965f889d2efSBrooks Davis 
96642a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
967f889d2efSBrooks Davis 		return (0);
968f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
969f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
970f889d2efSBrooks Davis 			return (0);
971f889d2efSBrooks Davis 	}
972f889d2efSBrooks Davis 
973f889d2efSBrooks Davis 	return (1);
974f889d2efSBrooks Davis }
975f889d2efSBrooks Davis 
976f889d2efSBrooks Davis static int
9776b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
978f889d2efSBrooks Davis {
979f889d2efSBrooks Davis 	char *dp;
980f889d2efSBrooks Davis 	int wildcard;
981f889d2efSBrooks Davis 	int unit;
982f889d2efSBrooks Davis 	int error;
9837983103aSRobert Watson 	int vid;
984*c7cffd65SAlexander V. Chernikov 	uint16_t proto;
9859d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
9869d4fe4b2SBrooks Davis 	struct ifnet *ifp;
987f889d2efSBrooks Davis 	struct ifnet *p;
9883ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
9893ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
9906b7330e2SSam Leffler 	struct vlanreq vlr;
99165239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
992f889d2efSBrooks Davis 
993*c7cffd65SAlexander V. Chernikov 	proto = ETHERTYPE_VLAN;
994*c7cffd65SAlexander V. Chernikov 
9956b7330e2SSam Leffler 	/*
996*c7cffd65SAlexander V. Chernikov 	 * There are two ways to specify the cloned device:
9976b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
9986b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
9996b7330e2SSam Leffler 	 *   must be configured separately.
1000*c7cffd65SAlexander V. Chernikov 	 * The first technique is preferred; the latter is supported
1001*c7cffd65SAlexander V. Chernikov 	 * for backwards compatibility.
10027983103aSRobert Watson 	 *
10037983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
10047983103aSRobert Watson 	 * called for.
10056b7330e2SSam Leffler 	 */
10066b7330e2SSam Leffler 	if (params) {
10076b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
10086b7330e2SSam Leffler 		if (error)
10096b7330e2SSam Leffler 			return error;
10109bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
10116b7330e2SSam Leffler 		if (p == NULL)
1012b1828acfSGleb Smirnoff 			return (ENXIO);
10136b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
10149bcf3ae4SAlexander Motin 		if (error != 0) {
10159bcf3ae4SAlexander Motin 			if_rele(p);
10166b7330e2SSam Leffler 			return (error);
10179bcf3ae4SAlexander Motin 		}
10187983103aSRobert Watson 		vid = vlr.vlr_tag;
1019*c7cffd65SAlexander V. Chernikov 		proto = vlr.vlr_proto;
10206b7330e2SSam Leffler 		wildcard = (unit < 0);
1021f889d2efSBrooks Davis 	} else {
10229bcf3ae4SAlexander Motin 		p = NULL;
1023f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
1024f889d2efSBrooks Davis 		if (error != 0)
1025f889d2efSBrooks Davis 			return (error);
1026f889d2efSBrooks Davis 
1027f889d2efSBrooks Davis 		wildcard = (unit < 0);
1028f889d2efSBrooks Davis 	}
1029f889d2efSBrooks Davis 
1030f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
10319bcf3ae4SAlexander Motin 	if (error != 0) {
10329bcf3ae4SAlexander Motin 		if (p != NULL)
10339bcf3ae4SAlexander Motin 			if_rele(p);
1034f889d2efSBrooks Davis 		return (error);
10359bcf3ae4SAlexander Motin 	}
1036f889d2efSBrooks Davis 
1037f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
1038f889d2efSBrooks Davis 	if (wildcard) {
1039f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
1040f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1041f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
1042f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
1043f889d2efSBrooks Davis 		}
1044f889d2efSBrooks Davis 	}
10459d4fe4b2SBrooks Davis 
1046a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1047fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1048fc74a9f9SBrooks Davis 	if (ifp == NULL) {
1049fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
1050fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
10519bcf3ae4SAlexander Motin 		if (p != NULL)
10529bcf3ae4SAlexander Motin 			if_rele(p);
1053fc74a9f9SBrooks Davis 		return (ENOSPC);
1054fc74a9f9SBrooks Davis 	}
1055b08d611dSMatt Macy 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
10569d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
1057f889d2efSBrooks Davis 	/*
1058cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
1059f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
1060f889d2efSBrooks Davis 	 */
1061f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
106242a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
1063f889d2efSBrooks Davis 	ifp->if_dunit = unit;
10649d4fe4b2SBrooks Davis 
1065114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
1066d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
1067d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
10689d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
1069b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1070f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1071fb3bc596SJohn Baldwin 	ifp->if_snd_tag_modify = vlan_snd_tag_modify;
1072fb3bc596SJohn Baldwin 	ifp->if_snd_tag_query = vlan_snd_tag_query;
1073fa91f845SRandall Stewart 	ifp->if_snd_tag_free = vlan_snd_tag_free;
1074f3e7afe2SHans Petter Selasky #endif
107564a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
1076fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
10779d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
1078211f625aSBill Fenner 	ifp->if_baudrate = 0;
1079a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
1080a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
10813ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
10823ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
10833ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
10849d4fe4b2SBrooks Davis 
10859bcf3ae4SAlexander Motin 	if (p != NULL) {
1086*c7cffd65SAlexander V. Chernikov 		error = vlan_config(ifv, p, vid, proto);
10879bcf3ae4SAlexander Motin 		if_rele(p);
1088f889d2efSBrooks Davis 		if (error != 0) {
1089f889d2efSBrooks Davis 			/*
109028cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
1091f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
1092f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1093f889d2efSBrooks Davis 			 */
1094f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1095249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
10964b22573aSBrooks Davis 			if_free(ifp);
109796c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
1098f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1099f889d2efSBrooks Davis 
1100f889d2efSBrooks Davis 			return (error);
1101f889d2efSBrooks Davis 		}
1102f889d2efSBrooks Davis 	}
1103f889d2efSBrooks Davis 
11049d4fe4b2SBrooks Davis 	return (0);
11059d4fe4b2SBrooks Davis }
11069d4fe4b2SBrooks Davis 
1107f889d2efSBrooks Davis static int
1108f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
11099d4fe4b2SBrooks Davis {
11109d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1111*c7cffd65SAlexander V. Chernikov 
1112*c7cffd65SAlexander V. Chernikov 	if (ifp->if_vlantrunk)
1113*c7cffd65SAlexander V. Chernikov 		return (EBUSY);
1114b4e9f837SBrooks Davis 
1115249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1116249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1117d148c2a2SMatt Joras 	/*
1118d148c2a2SMatt Joras 	 * We should have the only reference to the ifv now, so we can now
1119d148c2a2SMatt Joras 	 * drain any remaining lladdr task before freeing the ifnet and the
1120d148c2a2SMatt Joras 	 * ifvlan.
1121d148c2a2SMatt Joras 	 */
1122d148c2a2SMatt Joras 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1123b08d611dSMatt Macy 	NET_EPOCH_WAIT();
11244b22573aSBrooks Davis 	if_free(ifp);
11259d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1126*c7cffd65SAlexander V. Chernikov 	ifc_free_unit(ifc, ifp->if_dunit);
1127b4e9f837SBrooks Davis 
1128f889d2efSBrooks Davis 	return (0);
11299d4fe4b2SBrooks Davis }
11309d4fe4b2SBrooks Davis 
113115a66c21SBruce M Simpson /*
113215a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
113315a66c21SBruce M Simpson  */
11342cc2df49SGarrett Wollman static void
1135114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
11362cc2df49SGarrett Wollman {
11372cc2df49SGarrett Wollman }
11382cc2df49SGarrett Wollman 
11396d3a3ab7SGleb Smirnoff /*
1140d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
11416d3a3ab7SGleb Smirnoff  */
1142d9b1d615SJohn Baldwin static int
1143d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
11442cc2df49SGarrett Wollman {
11452cc2df49SGarrett Wollman 	struct ifvlan *ifv;
11462cc2df49SGarrett Wollman 	struct ifnet *p;
11471ad7a257SPyun YongHyeon 	int error, len, mcast;
11482cc2df49SGarrett Wollman 
1149b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1150b8a6e03fSGleb Smirnoff 
11512cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
1152d148c2a2SMatt Joras 	if (TRUNK(ifv) == NULL) {
1153d148c2a2SMatt Joras 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1154d148c2a2SMatt Joras 		m_freem(m);
1155d148c2a2SMatt Joras 		return (ENETDOWN);
1156d148c2a2SMatt Joras 	}
115775ee267cSGleb Smirnoff 	p = PARENT(ifv);
11581ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
11591ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
11602cc2df49SGarrett Wollman 
1161a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
11622cc2df49SGarrett Wollman 
1163b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1164fb3bc596SJohn Baldwin 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1165fb3bc596SJohn Baldwin 		struct vlan_snd_tag *vst;
1166fb3bc596SJohn Baldwin 		struct m_snd_tag *mst;
1167fb3bc596SJohn Baldwin 
1168fb3bc596SJohn Baldwin 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1169fb3bc596SJohn Baldwin 		mst = m->m_pkthdr.snd_tag;
1170fb3bc596SJohn Baldwin 		vst = mst_to_vst(mst);
1171fb3bc596SJohn Baldwin 		if (vst->tag->ifp != p) {
1172fb3bc596SJohn Baldwin 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1173fb3bc596SJohn Baldwin 			m_freem(m);
1174fb3bc596SJohn Baldwin 			return (EAGAIN);
1175fb3bc596SJohn Baldwin 		}
1176fb3bc596SJohn Baldwin 
1177fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1178fb3bc596SJohn Baldwin 		m_snd_tag_rele(mst);
1179fb3bc596SJohn Baldwin 	}
1180fb3bc596SJohn Baldwin #endif
1181fb3bc596SJohn Baldwin 
1182f731f104SBill Paul 	/*
1183d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
118424993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
118524993214SYaroslav Tykhiy 	 */
11862dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
1187a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1188d148c2a2SMatt Joras 		m_freem(m);
11898b20f6cfSHiroki Sato 		return (ENETDOWN);
119024993214SYaroslav Tykhiy 	}
119124993214SYaroslav Tykhiy 
1192*c7cffd65SAlexander V. Chernikov 	if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) {
1193a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1194d9b1d615SJohn Baldwin 		return (0);
11954af90a4dSMatthew N. Dodd 	}
11962cc2df49SGarrett Wollman 
11972cc2df49SGarrett Wollman 	/*
11982cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
11992cc2df49SGarrett Wollman 	 */
1200aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1201299153b5SAlexander V. Chernikov 	if (error == 0) {
1202a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1203a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1204a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
12051ad7a257SPyun YongHyeon 	} else
1206a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1207d9b1d615SJohn Baldwin 	return (error);
12082cc2df49SGarrett Wollman }
1209d9b1d615SJohn Baldwin 
121016cf6bdbSMatt Joras static int
121116cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
121216cf6bdbSMatt Joras     struct route *ro)
121316cf6bdbSMatt Joras {
121416cf6bdbSMatt Joras 	struct ifvlan *ifv;
121516cf6bdbSMatt Joras 	struct ifnet *p;
121616cf6bdbSMatt Joras 
1217b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1218b8a6e03fSGleb Smirnoff 
1219*c7cffd65SAlexander V. Chernikov 	/*
1220*c7cffd65SAlexander V. Chernikov 	 * Find the first non-VLAN parent interface.
1221*c7cffd65SAlexander V. Chernikov 	 */
122216cf6bdbSMatt Joras 	ifv = ifp->if_softc;
1223*c7cffd65SAlexander V. Chernikov 	do {
122416cf6bdbSMatt Joras 		if (TRUNK(ifv) == NULL) {
122516cf6bdbSMatt Joras 			m_freem(m);
122616cf6bdbSMatt Joras 			return (ENETDOWN);
122716cf6bdbSMatt Joras 		}
122816cf6bdbSMatt Joras 		p = PARENT(ifv);
1229*c7cffd65SAlexander V. Chernikov 		ifv = p->if_softc;
1230*c7cffd65SAlexander V. Chernikov 	} while (p->if_type == IFT_L2VLAN);
1231*c7cffd65SAlexander V. Chernikov 
123216cf6bdbSMatt Joras 	return p->if_output(ifp, m, dst, ro);
123316cf6bdbSMatt Joras }
123416cf6bdbSMatt Joras 
1235d9b1d615SJohn Baldwin /*
1236d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1237d9b1d615SJohn Baldwin  */
1238d9b1d615SJohn Baldwin static void
1239d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1240d9b1d615SJohn Baldwin {
1241f731f104SBill Paul }
1242f731f104SBill Paul 
1243a3814acfSSam Leffler static void
1244a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1245f731f104SBill Paul {
1246d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1247f731f104SBill Paul 	struct ifvlan *ifv;
12482ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
12492ccbbd06SMarcelo Araujo 	uint16_t vid, tag;
125075ee267cSGleb Smirnoff 
1251b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1252b8a6e03fSGleb Smirnoff 
1253d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1254d148c2a2SMatt Joras 	if (trunk == NULL) {
1255d148c2a2SMatt Joras 		m_freem(m);
1256d148c2a2SMatt Joras 		return;
1257d148c2a2SMatt Joras 	}
1258a3814acfSSam Leffler 
1259f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1260a3814acfSSam Leffler 		/*
126114e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1262a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1263a3814acfSSam Leffler 		 */
12642ccbbd06SMarcelo Araujo 		tag = m->m_pkthdr.ether_vtag;
12656ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1266a3814acfSSam Leffler 	} else {
126775ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
126875ee267cSGleb Smirnoff 
126914e98256SYaroslav Tykhiy 		/*
127014e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
127114e98256SYaroslav Tykhiy 		 */
1272a3814acfSSam Leffler 		switch (ifp->if_type) {
1273a3814acfSSam Leffler 		case IFT_ETHER:
1274a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1275a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1276a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1277a3814acfSSam Leffler 				return;
1278a3814acfSSam Leffler 			}
1279a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
12802ccbbd06SMarcelo Araujo 			tag = ntohs(evl->evl_tag);
1281db8b5973SYaroslav Tykhiy 
1282db8b5973SYaroslav Tykhiy 			/*
12832dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
12842dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
12852dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
12862dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1287db8b5973SYaroslav Tykhiy 			 */
12882dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
12892dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
12902dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1291a3814acfSSam Leffler 			break;
12922dc879b3SYaroslav Tykhiy 
1293a3814acfSSam Leffler 		default:
1294db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
129560c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
129660c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1297a3814acfSSam Leffler #endif
12983751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1299d148c2a2SMatt Joras 			m_freem(m);
130060c60618SYaroslav Tykhiy 			return;
1301a3814acfSSam Leffler 		}
13027a46ec8fSBrooks Davis 	}
13037a46ec8fSBrooks Davis 
13042ccbbd06SMarcelo Araujo 	vid = EVL_VLANOFTAG(tag);
13052ccbbd06SMarcelo Araujo 
13067983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
13072dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1308b08d611dSMatt Macy 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1309d148c2a2SMatt Joras 		m_freem(m);
131075ee267cSGleb Smirnoff 		return;
131175ee267cSGleb Smirnoff 	}
1312f731f104SBill Paul 
13132ccbbd06SMarcelo Araujo 	if (vlan_mtag_pcp) {
13142ccbbd06SMarcelo Araujo 		/*
13152ccbbd06SMarcelo Araujo 		 * While uncommon, it is possible that we will find a 802.1q
13162ccbbd06SMarcelo Araujo 		 * packet encapsulated inside another packet that also had an
13172ccbbd06SMarcelo Araujo 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
13182ccbbd06SMarcelo Araujo 		 * arriving over ethernet.  In that case, we replace the
13192ccbbd06SMarcelo Araujo 		 * existing 802.1q PCP m_tag value.
13202ccbbd06SMarcelo Araujo 		 */
13212ccbbd06SMarcelo Araujo 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
13222ccbbd06SMarcelo Araujo 		if (mtag == NULL) {
13232ccbbd06SMarcelo Araujo 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
13242ccbbd06SMarcelo Araujo 			    sizeof(uint8_t), M_NOWAIT);
13252ccbbd06SMarcelo Araujo 			if (mtag == NULL) {
13262ccbbd06SMarcelo Araujo 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1327d148c2a2SMatt Joras 				m_freem(m);
13282ccbbd06SMarcelo Araujo 				return;
13292ccbbd06SMarcelo Araujo 			}
13302ccbbd06SMarcelo Araujo 			m_tag_prepend(m, mtag);
13312ccbbd06SMarcelo Araujo 		}
13322ccbbd06SMarcelo Araujo 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
13332ccbbd06SMarcelo Araujo 	}
13342ccbbd06SMarcelo Araujo 
1335fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1336c0304424SGleb Smirnoff 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
13372cc2df49SGarrett Wollman 
1338a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1339fdbf1174SMatt Joras 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
13402cc2df49SGarrett Wollman }
13412cc2df49SGarrett Wollman 
1342d148c2a2SMatt Joras static void
1343d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused)
1344d148c2a2SMatt Joras {
1345d148c2a2SMatt Joras 	struct ifvlan *ifv;
1346d148c2a2SMatt Joras 	struct ifnet *ifp;
1347d148c2a2SMatt Joras 
1348d148c2a2SMatt Joras 	ifv = (struct ifvlan *)arg;
1349d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
13505191a3aeSKristof Provost 
13515191a3aeSKristof Provost 	CURVNET_SET(ifp->if_vnet);
13525191a3aeSKristof Provost 
1353d148c2a2SMatt Joras 	/* The ifv_ifp already has the lladdr copied in. */
1354d148c2a2SMatt Joras 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
13555191a3aeSKristof Provost 
13565191a3aeSKristof Provost 	CURVNET_RESTORE();
1357d148c2a2SMatt Joras }
1358d148c2a2SMatt Joras 
13592cc2df49SGarrett Wollman static int
1360*c7cffd65SAlexander V. Chernikov vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid,
1361*c7cffd65SAlexander V. Chernikov 	uint16_t proto)
13622cc2df49SGarrett Wollman {
13636dcec895SGleb Smirnoff 	struct epoch_tracker et;
136475ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
13651cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
136675ee267cSGleb Smirnoff 	int error = 0;
13672cc2df49SGarrett Wollman 
1368b1828acfSGleb Smirnoff 	/*
1369b1828acfSGleb Smirnoff 	 * We can handle non-ethernet hardware types as long as
1370b1828acfSGleb Smirnoff 	 * they handle the tagging and headers themselves.
1371b1828acfSGleb Smirnoff 	 */
1372e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1373*c7cffd65SAlexander V. Chernikov 	    p->if_type != IFT_L2VLAN &&
1374e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
137515a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
137664a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
137764a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
1378b1828acfSGleb Smirnoff 	/*
1379b1828acfSGleb Smirnoff 	 * Don't let the caller set up a VLAN VID with
1380b1828acfSGleb Smirnoff 	 * anything except VLID bits.
1381b1828acfSGleb Smirnoff 	 * VID numbers 0x0 and 0xFFF are reserved.
1382b1828acfSGleb Smirnoff 	 */
1383b1828acfSGleb Smirnoff 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1384b1828acfSGleb Smirnoff 		return (EINVAL);
138575ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
138615a66c21SBruce M Simpson 		return (EBUSY);
13872cc2df49SGarrett Wollman 
1388d148c2a2SMatt Joras 	VLAN_XLOCK();
138975ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
139075ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
139175ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
139275ee267cSGleb Smirnoff 		vlan_inithash(trunk);
139375ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
1394d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
139575ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
139675ee267cSGleb Smirnoff 		trunk->parent = p;
13979bcf3ae4SAlexander Motin 		if_ref(trunk->parent);
1398b08d611dSMatt Macy 		TRUNK_WUNLOCK(trunk);
139975ee267cSGleb Smirnoff 	} else {
140075ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
140175ee267cSGleb Smirnoff 	}
140275ee267cSGleb Smirnoff 
14037983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
14042ccbbd06SMarcelo Araujo 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
140575ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
140675ee267cSGleb Smirnoff 	if (error)
140775ee267cSGleb Smirnoff 		goto done;
1408*c7cffd65SAlexander V. Chernikov 	ifv->ifv_proto = proto;
1409a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1410a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
14111cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1412d89baa5aSAlexander Motin 	ifv->ifv_capenable = -1;
1413a3814acfSSam Leffler 
1414a3814acfSSam Leffler 	/*
1415a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1416a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1417656acce4SYaroslav Tykhiy 	 * use it.
1418a3814acfSSam Leffler 	 */
1419656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1420656acce4SYaroslav Tykhiy 		/*
1421656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1422656acce4SYaroslav Tykhiy 		 * handle extended frames.
1423656acce4SYaroslav Tykhiy 		 */
1424a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1425656acce4SYaroslav Tykhiy 	} else {
1426a3814acfSSam Leffler 		/*
1427a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1428a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1429a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1430a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1431a3814acfSSam Leffler 		 * which might still be useful.
1432a3814acfSSam Leffler 		 */
1433a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1434a3814acfSSam Leffler 	}
1435a3814acfSSam Leffler 
143675ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
14371cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1438e4cd31ddSJeff Roberson 	/*
1439e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1440e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1441e4cd31ddSJeff Roberson 	 * interfaces to also work.
1442e4cd31ddSJeff Roberson 	 */
14431cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
144475ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1445e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1446e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1447e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1448e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
144932a52e9eSNavdeep Parhar 	ifp->if_pcp = ifv->ifv_pcp;
1450e4cd31ddSJeff Roberson 
14512cc2df49SGarrett Wollman 	/*
145216cf6bdbSMatt Joras 	 * We wrap the parent's if_output using vlan_output to ensure that it
145316cf6bdbSMatt Joras 	 * can't become stale.
145416cf6bdbSMatt Joras 	 */
145516cf6bdbSMatt Joras 	ifp->if_output = vlan_output;
145616cf6bdbSMatt Joras 
145716cf6bdbSMatt Joras 	/*
145824993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
145924993214SYaroslav Tykhiy 	 * Other flags are none of our business.
14602cc2df49SGarrett Wollman 	 */
146164a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
14621cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
14631cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
14641cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
14651cf236fbSYaroslav Tykhiy 
14661cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
14672cc2df49SGarrett Wollman 
14686dcec895SGleb Smirnoff 	NET_EPOCH_ENTER(et);
146975ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
14706dcec895SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1471a3814acfSSam Leffler 
1472a3814acfSSam Leffler 	/*
1473e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
14742cc2df49SGarrett Wollman 	 * physical interface's.
14752cc2df49SGarrett Wollman 	 */
1476a961401eSAndrey V. Elsukov 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1477e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1478e4cd31ddSJeff Roberson 	    p->if_addrlen;
14791b2a4f7aSBill Fenner 
1480a961401eSAndrey V. Elsukov 	/*
1481a961401eSAndrey V. Elsukov 	 * Do not schedule link address update if it was the same
1482a961401eSAndrey V. Elsukov 	 * as previous parent's. This helps avoid updating for each
1483a961401eSAndrey V. Elsukov 	 * associated llentry.
1484a961401eSAndrey V. Elsukov 	 */
1485a961401eSAndrey V. Elsukov 	if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1486a961401eSAndrey V. Elsukov 		bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1487a961401eSAndrey V. Elsukov 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1488a961401eSAndrey V. Elsukov 	}
14892ada9747SYaroslav Tykhiy 
14902ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
14912ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1492d148c2a2SMatt Joras 
1493d148c2a2SMatt Joras 	/* Update flags on the parent, if necessary. */
1494d148c2a2SMatt Joras 	vlan_setflags(ifp, 1);
1495b08d611dSMatt Macy 
1496d148c2a2SMatt Joras 	/*
1497b08d611dSMatt Macy 	 * Configure multicast addresses that may already be
1498b08d611dSMatt Macy 	 * joined on the vlan device.
1499d148c2a2SMatt Joras 	 */
1500b08d611dSMatt Macy 	(void)vlan_setmulti(ifp);
1501b08d611dSMatt Macy 
1502b08d611dSMatt Macy done:
1503c725524cSJack F Vogel 	if (error == 0)
15047983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1505d148c2a2SMatt Joras 	VLAN_XUNLOCK();
150675ee267cSGleb Smirnoff 
150775ee267cSGleb Smirnoff 	return (error);
15082cc2df49SGarrett Wollman }
15092cc2df49SGarrett Wollman 
15106f359e28SJohn Baldwin static void
1511f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1512f731f104SBill Paul {
15135cb8c31aSYaroslav Tykhiy 
1514d148c2a2SMatt Joras 	VLAN_XLOCK();
151528cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
1516d148c2a2SMatt Joras 	VLAN_XUNLOCK();
15175cb8c31aSYaroslav Tykhiy }
15185cb8c31aSYaroslav Tykhiy 
15196f359e28SJohn Baldwin static void
152028cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
15215cb8c31aSYaroslav Tykhiy {
152275ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1523f731f104SBill Paul 	struct vlan_mc_entry *mc;
1524f731f104SBill Paul 	struct ifvlan *ifv;
1525c725524cSJack F Vogel 	struct ifnet  *parent;
152628cc4d37SJohn Baldwin 	int error;
1527f731f104SBill Paul 
1528d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
15294faedfe8SSam Leffler 
1530f731f104SBill Paul 	ifv = ifp->if_softc;
153175ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
153222893351SJack F Vogel 	parent = NULL;
1533f731f104SBill Paul 
153422893351SJack F Vogel 	if (trunk != NULL) {
153522893351SJack F Vogel 		parent = trunk->parent;
15361b2a4f7aSBill Fenner 
1537f731f104SBill Paul 		/*
1538f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1539f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
15401b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1541f731f104SBill Paul 		 */
1542b08d611dSMatt Macy 		while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
15436f359e28SJohn Baldwin 			/*
154428cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1545b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
154628cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
154728cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
154828cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
154928cc4d37SJohn Baldwin 			 * succeed.
15506f359e28SJohn Baldwin 			 */
155128cc4d37SJohn Baldwin 			if (!departing) {
155228cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1553e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
155428cc4d37SJohn Baldwin 				if (error)
155528cc4d37SJohn Baldwin 					if_printf(ifp,
155628cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
155728cc4d37SJohn Baldwin 					    error);
155828cc4d37SJohn Baldwin 			}
1559b08d611dSMatt Macy 			CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
15602a4bd982SGleb Smirnoff 			NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
1561f731f104SBill Paul 		}
1562a3814acfSSam Leffler 
15631cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1564d148c2a2SMatt Joras 
156575ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
156675ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
156775ee267cSGleb Smirnoff 
156875ee267cSGleb Smirnoff 		/*
156975ee267cSGleb Smirnoff 		 * Check if we were the last.
157075ee267cSGleb Smirnoff 		 */
157175ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
15722d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
1573b08d611dSMatt Macy 			NET_EPOCH_WAIT();
157475ee267cSGleb Smirnoff 			trunk_destroy(trunk);
1575d148c2a2SMatt Joras 		}
15761b2a4f7aSBill Fenner 	}
1577f731f104SBill Paul 
1578f731f104SBill Paul 	/* Disconnect from parent. */
15791cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
15801cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
15815cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
15825cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
15835cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1584f731f104SBill Paul 
158522893351SJack F Vogel 	/*
158622893351SJack F Vogel 	 * Only dispatch an event if vlan was
158722893351SJack F Vogel 	 * attached, otherwise there is nothing
158822893351SJack F Vogel 	 * to cleanup anyway.
158922893351SJack F Vogel 	 */
159022893351SJack F Vogel 	if (parent != NULL)
15917983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1592f731f104SBill Paul }
1593f731f104SBill Paul 
15941cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1595f731f104SBill Paul static int
15961cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
15971cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1598a3814acfSSam Leffler {
15991cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
16001cf236fbSYaroslav Tykhiy 	int error;
1601a3814acfSSam Leffler 
1602d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1603a3814acfSSam Leffler 
16041cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
16051cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
16061cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
16071cf236fbSYaroslav Tykhiy 
16081cf236fbSYaroslav Tykhiy 	/*
16091cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
16101cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
16111cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
16121cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
16131cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
16141cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
16151cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
16161cf236fbSYaroslav Tykhiy 	 */
16171cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
161875ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
16191cf236fbSYaroslav Tykhiy 		if (error)
1620a3814acfSSam Leffler 			return (error);
16211cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
16221cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
16231cf236fbSYaroslav Tykhiy 	}
16241cf236fbSYaroslav Tykhiy 	return (0);
16251cf236fbSYaroslav Tykhiy }
16261cf236fbSYaroslav Tykhiy 
16271cf236fbSYaroslav Tykhiy /*
16281cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
16291cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
16301cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
16311cf236fbSYaroslav Tykhiy  */
16321cf236fbSYaroslav Tykhiy static int
16331cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
16341cf236fbSYaroslav Tykhiy {
16351cf236fbSYaroslav Tykhiy 	int error, i;
16361cf236fbSYaroslav Tykhiy 
16371cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
16381cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
16391cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
16401cf236fbSYaroslav Tykhiy 		if (error)
16411cf236fbSYaroslav Tykhiy 			return (error);
16421cf236fbSYaroslav Tykhiy 	}
16431cf236fbSYaroslav Tykhiy 	return (0);
1644a3814acfSSam Leffler }
1645a3814acfSSam Leffler 
1646127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1647127d7b2dSAndre Oppermann static void
1648a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1649127d7b2dSAndre Oppermann {
1650b2807792SGleb Smirnoff 	struct epoch_tracker et;
1651d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1652127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
1653127d7b2dSAndre Oppermann 
1654b2807792SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1655d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1656b2807792SGleb Smirnoff 	if (trunk == NULL) {
1657b2807792SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1658d148c2a2SMatt Joras 		return;
1659b2807792SGleb Smirnoff 	}
1660d148c2a2SMatt Joras 
1661d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1662d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
1663aad0be7aSGleb Smirnoff 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1664fc74a9f9SBrooks Davis 		if_link_state_change(ifv->ifv_ifp,
166575ee267cSGleb Smirnoff 		    trunk->parent->if_link_state);
1666127d7b2dSAndre Oppermann 	}
1667d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1668b2807792SGleb Smirnoff 	NET_EPOCH_EXIT(et);
166975ee267cSGleb Smirnoff }
167075ee267cSGleb Smirnoff 
167175ee267cSGleb Smirnoff static void
167275ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
167375ee267cSGleb Smirnoff {
1674d148c2a2SMatt Joras 	struct ifnet *p;
1675d148c2a2SMatt Joras 	struct ifnet *ifp;
16769fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
1677d89baa5aSAlexander Motin 	int cap = 0, ena = 0, mena;
1678d89baa5aSAlexander Motin 	u_long hwa = 0;
167975ee267cSGleb Smirnoff 
1680a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
1681b8a6e03fSGleb Smirnoff 	VLAN_SXLOCK_ASSERT();
1682b8a6e03fSGleb Smirnoff 
1683d148c2a2SMatt Joras 	p = PARENT(ifv);
1684d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
168575ee267cSGleb Smirnoff 
1686d89baa5aSAlexander Motin 	/* Mask parent interface enabled capabilities disabled by user. */
1687d89baa5aSAlexander Motin 	mena = p->if_capenable & ifv->ifv_capenable;
1688d89baa5aSAlexander Motin 
168975ee267cSGleb Smirnoff 	/*
169075ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
169175ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
169275ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
169375ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
169475ee267cSGleb Smirnoff 	 */
169575ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1696d89baa5aSAlexander Motin 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
169775ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
169875ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1699d89baa5aSAlexander Motin 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1700d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM)
1701d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1702d89baa5aSAlexander Motin 			    CSUM_UDP | CSUM_SCTP);
1703d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM_IPV6)
1704d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1705d89baa5aSAlexander Motin 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
170675ee267cSGleb Smirnoff 	}
1707d89baa5aSAlexander Motin 
17089b76d9cbSPyun YongHyeon 	/*
17099b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
17109b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
17119b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
17129b76d9cbSPyun YongHyeon 	 */
17139fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
17149fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
17159fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
17169b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1717d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TSO;
17189b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1719d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TSO;
1720d89baa5aSAlexander Motin 		if (ena & IFCAP_TSO)
1721d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & CSUM_TSO;
17229b76d9cbSPyun YongHyeon 	}
172309fe6320SNavdeep Parhar 
172409fe6320SNavdeep Parhar 	/*
172559150e91SAlexander Motin 	 * If the parent interface can do LRO and checksum offloading on
172659150e91SAlexander Motin 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
172759150e91SAlexander Motin 	 * cost nothing, while false negative may lead to some confusions.
172859150e91SAlexander Motin 	 */
172959150e91SAlexander Motin 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
173059150e91SAlexander Motin 		cap |= p->if_capabilities & IFCAP_LRO;
173159150e91SAlexander Motin 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
173259150e91SAlexander Motin 		ena |= p->if_capenable & IFCAP_LRO;
173359150e91SAlexander Motin 
173459150e91SAlexander Motin 	/*
173509fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
173609fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
173709fe6320SNavdeep Parhar 	 *
173809fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
173909fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
174009fe6320SNavdeep Parhar 	 * with its own bit.
174109fe6320SNavdeep Parhar 	 */
174209fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
174309fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1744d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TOE;
174509fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
174609fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
1747d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TOE;
174809fe6320SNavdeep Parhar 	}
1749f3e7afe2SHans Petter Selasky 
1750d89baa5aSAlexander Motin 	/*
1751d89baa5aSAlexander Motin 	 * If the parent interface supports dynamic link state, so does the
1752d89baa5aSAlexander Motin 	 * VLAN interface.
1753d89baa5aSAlexander Motin 	 */
1754d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1755d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_LINKSTATE);
1756d89baa5aSAlexander Motin 
1757f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1758f3e7afe2SHans Petter Selasky 	/*
1759f3e7afe2SHans Petter Selasky 	 * If the parent interface supports ratelimiting, so does the
1760f3e7afe2SHans Petter Selasky 	 * VLAN interface.
1761f3e7afe2SHans Petter Selasky 	 */
1762d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1763d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_TXRTLMT);
1764f3e7afe2SHans Petter Selasky #endif
1765d89baa5aSAlexander Motin 
176666d0c056SJohn Baldwin 	/*
176766d0c056SJohn Baldwin 	 * If the parent interface supports unmapped mbufs, so does
176866d0c056SJohn Baldwin 	 * the VLAN interface.  Note that this should be fine even for
176966d0c056SJohn Baldwin 	 * interfaces that don't support hardware tagging as headers
177066d0c056SJohn Baldwin 	 * are prepended in normal mbufs to unmapped mbufs holding
177166d0c056SJohn Baldwin 	 * payload data.
177266d0c056SJohn Baldwin 	 */
177366d0c056SJohn Baldwin 	cap |= (p->if_capabilities & IFCAP_NOMAP);
177466d0c056SJohn Baldwin 	ena |= (mena & IFCAP_NOMAP);
177566d0c056SJohn Baldwin 
1776b2e60773SJohn Baldwin 	/*
1777b2e60773SJohn Baldwin 	 * If the parent interface can offload encryption and segmentation
1778b2e60773SJohn Baldwin 	 * of TLS records over TCP, propagate it's capability to the VLAN
1779b2e60773SJohn Baldwin 	 * interface.
1780b2e60773SJohn Baldwin 	 *
1781b2e60773SJohn Baldwin 	 * All TLS drivers in the tree today can deal with VLANs.  If
1782b2e60773SJohn Baldwin 	 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1783b2e60773SJohn Baldwin 	 * defined.
1784b2e60773SJohn Baldwin 	 */
1785b2e60773SJohn Baldwin 	if (p->if_capabilities & IFCAP_TXTLS)
1786b2e60773SJohn Baldwin 		cap |= p->if_capabilities & IFCAP_TXTLS;
1787b2e60773SJohn Baldwin 	if (p->if_capenable & IFCAP_TXTLS)
1788b2e60773SJohn Baldwin 		ena |= mena & IFCAP_TXTLS;
1789b2e60773SJohn Baldwin 
1790d89baa5aSAlexander Motin 	ifp->if_capabilities = cap;
1791d89baa5aSAlexander Motin 	ifp->if_capenable = ena;
1792d89baa5aSAlexander Motin 	ifp->if_hwassist = hwa;
179375ee267cSGleb Smirnoff }
179475ee267cSGleb Smirnoff 
179575ee267cSGleb Smirnoff static void
179675ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
179775ee267cSGleb Smirnoff {
1798b2807792SGleb Smirnoff 	struct epoch_tracker et;
1799d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
180075ee267cSGleb Smirnoff 	struct ifvlan *ifv;
180175ee267cSGleb Smirnoff 
1802d148c2a2SMatt Joras 	VLAN_SLOCK();
1803d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1804d148c2a2SMatt Joras 	if (trunk == NULL) {
1805d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1806d148c2a2SMatt Joras 		return;
1807d148c2a2SMatt Joras 	}
1808b2807792SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1809b8a6e03fSGleb Smirnoff 	VLAN_FOREACH(ifv, trunk)
181075ee267cSGleb Smirnoff 		vlan_capabilities(ifv);
1811b2807792SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1812d148c2a2SMatt Joras 	VLAN_SUNLOCK();
1813127d7b2dSAndre Oppermann }
1814127d7b2dSAndre Oppermann 
1815a3814acfSSam Leffler static int
1816cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
18172cc2df49SGarrett Wollman {
18182cc2df49SGarrett Wollman 	struct ifnet *p;
18192cc2df49SGarrett Wollman 	struct ifreq *ifr;
1820e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
18212cc2df49SGarrett Wollman 	struct ifvlan *ifv;
18222d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
18232cc2df49SGarrett Wollman 	struct vlanreq vlr;
182484becee1SAlexander Motin 	int error = 0, oldmtu;
18252cc2df49SGarrett Wollman 
18262cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1827e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
18282cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
18292cc2df49SGarrett Wollman 
18302cc2df49SGarrett Wollman 	switch (cmd) {
1831e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1832e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1833e4cd31ddSJeff Roberson #ifdef INET
1834e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1835e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1836e4cd31ddSJeff Roberson #endif
1837e4cd31ddSJeff Roberson 		break;
1838e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
183938d958a6SBrooks Davis 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
184038d958a6SBrooks Davis 		    ifp->if_addrlen);
1841e4cd31ddSJeff Roberson 		break;
1842b3cca108SBill Fenner 	case SIOCGIFMEDIA:
1843d148c2a2SMatt Joras 		VLAN_SLOCK();
184475ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1845d8564efdSEd Maste 			p = PARENT(ifv);
18469bcf3ae4SAlexander Motin 			if_ref(p);
1847d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
18489bcf3ae4SAlexander Motin 			if_rele(p);
1849b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1850b3cca108SBill Fenner 			if (error == 0) {
1851b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1852b3cca108SBill Fenner 
1853b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1854b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1855b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1856b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1857b3cca108SBill Fenner 						ifmr->ifm_ulist,
1858b3cca108SBill Fenner 						sizeof(int));
1859b3cca108SBill Fenner 				}
1860b3cca108SBill Fenner 			}
18614faedfe8SSam Leffler 		} else {
1862b3cca108SBill Fenner 			error = EINVAL;
18634faedfe8SSam Leffler 		}
1864d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1865b3cca108SBill Fenner 		break;
1866b3cca108SBill Fenner 
1867b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1868b3cca108SBill Fenner 		error = EINVAL;
1869b3cca108SBill Fenner 		break;
1870b3cca108SBill Fenner 
18712cc2df49SGarrett Wollman 	case SIOCSIFMTU:
18722cc2df49SGarrett Wollman 		/*
18732cc2df49SGarrett Wollman 		 * Set the interface MTU.
18742cc2df49SGarrett Wollman 		 */
1875d148c2a2SMatt Joras 		VLAN_SLOCK();
1876d148c2a2SMatt Joras 		trunk = TRUNK(ifv);
1877d148c2a2SMatt Joras 		if (trunk != NULL) {
1878d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1879a3814acfSSam Leffler 			if (ifr->ifr_mtu >
188075ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1881a3814acfSSam Leffler 			    ifr->ifr_mtu <
1882a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
18832cc2df49SGarrett Wollman 				error = EINVAL;
1884a3814acfSSam Leffler 			else
18852cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1886d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
1887a3814acfSSam Leffler 		} else
1888a3814acfSSam Leffler 			error = EINVAL;
1889d148c2a2SMatt Joras 		VLAN_SUNLOCK();
18902cc2df49SGarrett Wollman 		break;
18912cc2df49SGarrett Wollman 
18922cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1893ccf7ba97SMarko Zec #ifdef VIMAGE
189415f6780eSRobert Watson 		/*
189515f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
189615f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
189715f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
189815f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
18995a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
190015f6780eSRobert Watson 		 */
1901ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1902ccf7ba97SMarko Zec 			error = EPERM;
1903ccf7ba97SMarko Zec 			break;
1904ccf7ba97SMarko Zec 		}
1905ccf7ba97SMarko Zec #endif
1906541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
19072cc2df49SGarrett Wollman 		if (error)
19082cc2df49SGarrett Wollman 			break;
19092cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1910f731f104SBill Paul 			vlan_unconfig(ifp);
19112cc2df49SGarrett Wollman 			break;
19122cc2df49SGarrett Wollman 		}
19139bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
19141bdc73d3SEd Maste 		if (p == NULL) {
19152cc2df49SGarrett Wollman 			error = ENOENT;
19162cc2df49SGarrett Wollman 			break;
19172cc2df49SGarrett Wollman 		}
191884becee1SAlexander Motin 		oldmtu = ifp->if_mtu;
1919*c7cffd65SAlexander V. Chernikov 		error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto);
19209bcf3ae4SAlexander Motin 		if_rele(p);
192184becee1SAlexander Motin 
192284becee1SAlexander Motin 		/*
192384becee1SAlexander Motin 		 * VLAN MTU may change during addition of the vlandev.
192484becee1SAlexander Motin 		 * If it did, do network layer specific procedure.
192584becee1SAlexander Motin 		 */
192684becee1SAlexander Motin 		if (ifp->if_mtu != oldmtu) {
192784becee1SAlexander Motin #ifdef INET6
192884becee1SAlexander Motin 			nd6_setmtu(ifp);
192984becee1SAlexander Motin #endif
193084becee1SAlexander Motin 			rt_updatemtu(ifp);
193184becee1SAlexander Motin 		}
19322cc2df49SGarrett Wollman 		break;
19332cc2df49SGarrett Wollman 
19342cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1935ccf7ba97SMarko Zec #ifdef VIMAGE
1936ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1937ccf7ba97SMarko Zec 			error = EPERM;
1938ccf7ba97SMarko Zec 			break;
1939ccf7ba97SMarko Zec 		}
1940ccf7ba97SMarko Zec #endif
194115a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
1942d148c2a2SMatt Joras 		VLAN_SLOCK();
194375ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
194475ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
19459bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
19467983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
1947*c7cffd65SAlexander V. Chernikov 			vlr.vlr_proto = ifv->ifv_proto;
19482cc2df49SGarrett Wollman 		}
1949d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1950541d96aaSBrooks Davis 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
19512cc2df49SGarrett Wollman 		break;
19522cc2df49SGarrett Wollman 
19532cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
19542cc2df49SGarrett Wollman 		/*
19551cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
19561cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
19572cc2df49SGarrett Wollman 		 */
1958d148c2a2SMatt Joras 		VLAN_XLOCK();
195975ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
19601cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
1961d148c2a2SMatt Joras 		VLAN_XUNLOCK();
19622cc2df49SGarrett Wollman 		break;
1963a3814acfSSam Leffler 
1964f731f104SBill Paul 	case SIOCADDMULTI:
1965f731f104SBill Paul 	case SIOCDELMULTI:
196675ee267cSGleb Smirnoff 		/*
196775ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
196875ee267cSGleb Smirnoff 		 * when we do.
1969d148c2a2SMatt Joras 		 *
1970d148c2a2SMatt Joras 		 * XXX We need the rmlock here to avoid sleeping while
1971d148c2a2SMatt Joras 		 * holding in6_multi_mtx.
197275ee267cSGleb Smirnoff 		 */
1973b08d611dSMatt Macy 		VLAN_XLOCK();
19742d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
1975b08d611dSMatt Macy 		if (trunk != NULL)
1976f731f104SBill Paul 			error = vlan_setmulti(ifp);
1977b08d611dSMatt Macy 		VLAN_XUNLOCK();
197875ee267cSGleb Smirnoff 
1979b08d611dSMatt Macy 		break;
19802ccbbd06SMarcelo Araujo 	case SIOCGVLANPCP:
19812ccbbd06SMarcelo Araujo #ifdef VIMAGE
19822ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19832ccbbd06SMarcelo Araujo 			error = EPERM;
19842ccbbd06SMarcelo Araujo 			break;
19852ccbbd06SMarcelo Araujo 		}
19862ccbbd06SMarcelo Araujo #endif
19872ccbbd06SMarcelo Araujo 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
19882ccbbd06SMarcelo Araujo 		break;
19892ccbbd06SMarcelo Araujo 
19902ccbbd06SMarcelo Araujo 	case SIOCSVLANPCP:
19912ccbbd06SMarcelo Araujo #ifdef VIMAGE
19922ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19932ccbbd06SMarcelo Araujo 			error = EPERM;
19942ccbbd06SMarcelo Araujo 			break;
19952ccbbd06SMarcelo Araujo 		}
19962ccbbd06SMarcelo Araujo #endif
19972ccbbd06SMarcelo Araujo 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
19982ccbbd06SMarcelo Araujo 		if (error)
19992ccbbd06SMarcelo Araujo 			break;
20002ccbbd06SMarcelo Araujo 		if (ifr->ifr_vlan_pcp > 7) {
20012ccbbd06SMarcelo Araujo 			error = EINVAL;
20022ccbbd06SMarcelo Araujo 			break;
20032ccbbd06SMarcelo Araujo 		}
20042ccbbd06SMarcelo Araujo 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
200532a52e9eSNavdeep Parhar 		ifp->if_pcp = ifv->ifv_pcp;
20064a381a9eSHans Petter Selasky 		/* broadcast event about PCP change */
20074a381a9eSHans Petter Selasky 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
20082ccbbd06SMarcelo Araujo 		break;
20092ccbbd06SMarcelo Araujo 
2010d89baa5aSAlexander Motin 	case SIOCSIFCAP:
2011d148c2a2SMatt Joras 		VLAN_SLOCK();
2012d89baa5aSAlexander Motin 		ifv->ifv_capenable = ifr->ifr_reqcap;
2013d89baa5aSAlexander Motin 		trunk = TRUNK(ifv);
20146dcec895SGleb Smirnoff 		if (trunk != NULL) {
20156dcec895SGleb Smirnoff 			struct epoch_tracker et;
20166dcec895SGleb Smirnoff 
20176dcec895SGleb Smirnoff 			NET_EPOCH_ENTER(et);
2018d89baa5aSAlexander Motin 			vlan_capabilities(ifv);
20196dcec895SGleb Smirnoff 			NET_EPOCH_EXIT(et);
20206dcec895SGleb Smirnoff 		}
2021d148c2a2SMatt Joras 		VLAN_SUNLOCK();
2022d89baa5aSAlexander Motin 		break;
2023d89baa5aSAlexander Motin 
20242cc2df49SGarrett Wollman 	default:
2025e4cd31ddSJeff Roberson 		error = EINVAL;
2026e4cd31ddSJeff Roberson 		break;
20272cc2df49SGarrett Wollman 	}
202815a66c21SBruce M Simpson 
202915a66c21SBruce M Simpson 	return (error);
20302cc2df49SGarrett Wollman }
2031f3e7afe2SHans Petter Selasky 
2032b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
2033f3e7afe2SHans Petter Selasky static int
2034f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp,
2035f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *params,
2036f3e7afe2SHans Petter Selasky     struct m_snd_tag **ppmt)
2037f3e7afe2SHans Petter Selasky {
2038fb3bc596SJohn Baldwin 	struct epoch_tracker et;
2039fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2040fb3bc596SJohn Baldwin 	struct ifvlan *ifv;
2041fb3bc596SJohn Baldwin 	struct ifnet *parent;
2042fb3bc596SJohn Baldwin 	int error;
2043f3e7afe2SHans Petter Selasky 
2044fb3bc596SJohn Baldwin 	NET_EPOCH_ENTER(et);
2045fb3bc596SJohn Baldwin 	ifv = ifp->if_softc;
2046fb3bc596SJohn Baldwin 	if (ifv->ifv_trunk != NULL)
2047fb3bc596SJohn Baldwin 		parent = PARENT(ifv);
2048fb3bc596SJohn Baldwin 	else
2049fb3bc596SJohn Baldwin 		parent = NULL;
2050fb3bc596SJohn Baldwin 	if (parent == NULL || parent->if_snd_tag_alloc == NULL) {
2051fb3bc596SJohn Baldwin 		NET_EPOCH_EXIT(et);
2052f3e7afe2SHans Petter Selasky 		return (EOPNOTSUPP);
2053fb3bc596SJohn Baldwin 	}
2054fb3bc596SJohn Baldwin 	if_ref(parent);
2055fb3bc596SJohn Baldwin 	NET_EPOCH_EXIT(et);
2056fb3bc596SJohn Baldwin 
2057fb3bc596SJohn Baldwin 	vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2058fb3bc596SJohn Baldwin 	if (vst == NULL) {
2059fb3bc596SJohn Baldwin 		if_rele(parent);
2060fb3bc596SJohn Baldwin 		return (ENOMEM);
2061fb3bc596SJohn Baldwin 	}
2062fb3bc596SJohn Baldwin 
2063fb3bc596SJohn Baldwin 	error = parent->if_snd_tag_alloc(parent, params, &vst->tag);
2064fb3bc596SJohn Baldwin 	if_rele(parent);
2065fb3bc596SJohn Baldwin 	if (error) {
2066fb3bc596SJohn Baldwin 		free(vst, M_VLAN);
2067fb3bc596SJohn Baldwin 		return (error);
2068fb3bc596SJohn Baldwin 	}
2069fb3bc596SJohn Baldwin 
207056fb710fSJohn Baldwin 	m_snd_tag_init(&vst->com, ifp, vst->tag->type);
2071fb3bc596SJohn Baldwin 
2072fb3bc596SJohn Baldwin 	*ppmt = &vst->com;
2073fb3bc596SJohn Baldwin 	return (0);
2074fb3bc596SJohn Baldwin }
2075fb3bc596SJohn Baldwin 
2076fb3bc596SJohn Baldwin static int
2077fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst,
2078fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *params)
2079fb3bc596SJohn Baldwin {
2080fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2081fb3bc596SJohn Baldwin 
2082fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2083fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params));
2084fb3bc596SJohn Baldwin }
2085fb3bc596SJohn Baldwin 
2086fb3bc596SJohn Baldwin static int
2087fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst,
2088fb3bc596SJohn Baldwin     union if_snd_tag_query_params *params)
2089fb3bc596SJohn Baldwin {
2090fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2091fb3bc596SJohn Baldwin 
2092fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2093fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_query(vst->tag, params));
2094f3e7afe2SHans Petter Selasky }
2095fa91f845SRandall Stewart 
2096fa91f845SRandall Stewart static void
2097fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst)
2098fa91f845SRandall Stewart {
2099fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2100fb3bc596SJohn Baldwin 
2101fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2102fb3bc596SJohn Baldwin 	m_snd_tag_rele(vst->tag);
2103fb3bc596SJohn Baldwin 	free(vst, M_VLAN);
2104fa91f845SRandall Stewart }
2105f3e7afe2SHans Petter Selasky #endif
2106