xref: /freebsd/sys/net/if_vlan.c (revision b2e60773c6b015f06fcd71510cd20a91eb43bcaa)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32ccbbd06SMarcelo Araujo  * Copyright 2012 ADARA Networks, Inc.
4d148c2a2SMatt Joras  * Copyright 2017 Dell EMC Isilon
52ccbbd06SMarcelo Araujo  *
62ccbbd06SMarcelo Araujo  * Portions of this software were developed by Robert N. M. Watson under
72ccbbd06SMarcelo Araujo  * contract to ADARA Networks, Inc.
82cc2df49SGarrett Wollman  *
92cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
102cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
112cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
122cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
132cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
142cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
152cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
162cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
172cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
182cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
192cc2df49SGarrett Wollman  * warranty.
202cc2df49SGarrett Wollman  *
212cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
222cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
232cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
242cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
252cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
262cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
272cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
282cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
292cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
302cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
312cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322cc2df49SGarrett Wollman  * SUCH DAMAGE.
332cc2df49SGarrett Wollman  */
342cc2df49SGarrett Wollman 
352cc2df49SGarrett Wollman /*
362cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
372ccbbd06SMarcelo Araujo  * This is sort of sneaky in the implementation, since
382cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
392cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
402cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
41d9b1d615SJohn Baldwin  * ether_output() sends to us via if_transmit(), rewrite them for
42d9b1d615SJohn Baldwin  * use by the real outgoing interface, and ask it to send them.
432cc2df49SGarrett Wollman  */
442cc2df49SGarrett Wollman 
458b2d9181SPyun YongHyeon #include <sys/cdefs.h>
468b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$");
478b2d9181SPyun YongHyeon 
482c5b403eSOleg Bulyzhin #include "opt_inet.h"
49*b2e60773SJohn Baldwin #include "opt_kern_tls.h"
5075ee267cSGleb Smirnoff #include "opt_vlan.h"
51f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
522cc2df49SGarrett Wollman 
532cc2df49SGarrett Wollman #include <sys/param.h>
54c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
552cc2df49SGarrett Wollman #include <sys/kernel.h>
5675ee267cSGleb Smirnoff #include <sys/lock.h>
57f731f104SBill Paul #include <sys/malloc.h>
582cc2df49SGarrett Wollman #include <sys/mbuf.h>
592b120974SPeter Wemm #include <sys/module.h>
60772b000fSAlexander V. Chernikov #include <sys/rmlock.h>
612ccbbd06SMarcelo Araujo #include <sys/priv.h>
62f731f104SBill Paul #include <sys/queue.h>
632cc2df49SGarrett Wollman #include <sys/socket.h>
642cc2df49SGarrett Wollman #include <sys/sockio.h>
652cc2df49SGarrett Wollman #include <sys/sysctl.h>
662cc2df49SGarrett Wollman #include <sys/systm.h>
67e4cd31ddSJeff Roberson #include <sys/sx.h>
68d148c2a2SMatt Joras #include <sys/taskqueue.h>
692cc2df49SGarrett Wollman 
702cc2df49SGarrett Wollman #include <net/bpf.h>
712cc2df49SGarrett Wollman #include <net/ethernet.h>
722cc2df49SGarrett Wollman #include <net/if.h>
7376039bc8SGleb Smirnoff #include <net/if_var.h>
74f889d2efSBrooks Davis #include <net/if_clone.h>
752cc2df49SGarrett Wollman #include <net/if_dl.h>
762cc2df49SGarrett Wollman #include <net/if_types.h>
772cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
784b79449eSBjoern A. Zeeb #include <net/vnet.h>
792cc2df49SGarrett Wollman 
802c5b403eSOleg Bulyzhin #ifdef INET
812c5b403eSOleg Bulyzhin #include <netinet/in.h>
822c5b403eSOleg Bulyzhin #include <netinet/if_ether.h>
832c5b403eSOleg Bulyzhin #endif
842c5b403eSOleg Bulyzhin 
8575ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
8664a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
8775ee267cSGleb Smirnoff 
882dc879b3SYaroslav Tykhiy #define	UP_AND_RUNNING(ifp) \
892dc879b3SYaroslav Tykhiy     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
902dc879b3SYaroslav Tykhiy 
91b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan);
9275ee267cSGleb Smirnoff 
9375ee267cSGleb Smirnoff struct ifvlantrunk {
9475ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
95b08d611dSMatt Macy 	struct	mtx	lock;
9675ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
975cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
985cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
9975ee267cSGleb Smirnoff #else
10075ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
10175ee267cSGleb Smirnoff 	uint16_t	hmask;
10275ee267cSGleb Smirnoff 	uint16_t	hwidth;
10375ee267cSGleb Smirnoff #endif
10475ee267cSGleb Smirnoff 	int		refcnt;
10575ee267cSGleb Smirnoff };
1069d4fe4b2SBrooks Davis 
107*b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
108fb3bc596SJohn Baldwin struct vlan_snd_tag {
109fb3bc596SJohn Baldwin 	struct m_snd_tag com;
110fb3bc596SJohn Baldwin 	struct m_snd_tag *tag;
111fb3bc596SJohn Baldwin };
112fb3bc596SJohn Baldwin 
113fb3bc596SJohn Baldwin static inline struct vlan_snd_tag *
114fb3bc596SJohn Baldwin mst_to_vst(struct m_snd_tag *mst)
115fb3bc596SJohn Baldwin {
116fb3bc596SJohn Baldwin 
117fb3bc596SJohn Baldwin 	return (__containerof(mst, struct vlan_snd_tag, com));
118fb3bc596SJohn Baldwin }
119fb3bc596SJohn Baldwin #endif
120fb3bc596SJohn Baldwin 
121d148c2a2SMatt Joras /*
122d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk with
123d148c2a2SMatt Joras  * the assumption that none will be added/removed during iteration.
124d148c2a2SMatt Joras  */
125d148c2a2SMatt Joras #ifdef VLAN_ARRAY
126d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
127d148c2a2SMatt Joras 	size_t _i; \
128d148c2a2SMatt Joras 	for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
129d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
130d148c2a2SMatt Joras #else /* VLAN_ARRAY */
131d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
132d148c2a2SMatt Joras 	struct ifvlan *_next; \
133d148c2a2SMatt Joras 	size_t _i; \
134d148c2a2SMatt Joras 	for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
135b08d611dSMatt Macy 		CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
136d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
137d148c2a2SMatt Joras 
138d148c2a2SMatt Joras /*
139d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk while
140d148c2a2SMatt Joras  * also modifying the number of vlans on the trunk. The iteration continues
141d148c2a2SMatt Joras  * until some condition is met or there are no more vlans on the trunk.
142d148c2a2SMatt Joras  */
143d148c2a2SMatt Joras #ifdef VLAN_ARRAY
144d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
145d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
146d148c2a2SMatt Joras 	size_t _i; \
147d148c2a2SMatt Joras 	for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
148d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]))
149d148c2a2SMatt Joras #else /* VLAN_ARRAY */
150d148c2a2SMatt Joras /*
151d148c2a2SMatt Joras  * The hash table case is more complicated. We allow for the hash table to be
152d148c2a2SMatt Joras  * modified (i.e. vlans removed) while we are iterating over it. To allow for
153d148c2a2SMatt Joras  * this we must restart the iteration every time we "touch" something during
154d148c2a2SMatt Joras  * the iteration, since removal will resize the hash table and invalidate our
155d148c2a2SMatt Joras  * current position. If acting on the touched element causes the trunk to be
156d148c2a2SMatt Joras  * emptied, then iteration also stops.
157d148c2a2SMatt Joras  */
158d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
159d148c2a2SMatt Joras 	size_t _i; \
160d148c2a2SMatt Joras 	bool _touch = false; \
161d148c2a2SMatt Joras 	for (_i = 0; \
162d148c2a2SMatt Joras 	    !(_cond) && _i < (1 << (_trunk)->hwidth); \
163d148c2a2SMatt Joras 	    _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
164b08d611dSMatt Macy 		if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
165d148c2a2SMatt Joras 		    (_touch = true))
166d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
167d148c2a2SMatt Joras 
168a3814acfSSam Leffler struct vlan_mc_entry {
169e4cd31ddSJeff Roberson 	struct sockaddr_dl		mc_addr;
170b08d611dSMatt Macy 	CK_SLIST_ENTRY(vlan_mc_entry)	mc_entries;
171c32a9d66SHans Petter Selasky 	struct epoch_context		mc_epoch_ctx;
172a3814acfSSam Leffler };
173a3814acfSSam Leffler 
174a3814acfSSam Leffler struct ifvlan {
17575ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
176fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
17775ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
17875ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
1796667db31SAlexander V. Chernikov 	void	*ifv_cookie;
1801cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
181d89baa5aSAlexander Motin 	int	ifv_capenable;
18272755d28SMark Johnston 	int	ifv_encaplen;	/* encapsulation length */
18372755d28SMark Johnston 	int	ifv_mtufudge;	/* MTU fudged by this much */
18472755d28SMark Johnston 	int	ifv_mintu;	/* min transmission unit */
18572755d28SMark Johnston 	uint16_t ifv_proto;	/* encapsulation ethertype */
18672755d28SMark Johnston 	uint16_t ifv_tag;	/* tag to apply on packets leaving if */
18772755d28SMark Johnston 	uint16_t ifv_vid;	/* VLAN ID */
18872755d28SMark Johnston 	uint8_t	ifv_pcp;	/* Priority Code Point (PCP). */
189d148c2a2SMatt Joras 	struct task lladdr_task;
190b08d611dSMatt Macy 	CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
191c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY
192b08d611dSMatt Macy 	CK_SLIST_ENTRY(ifvlan) ifv_list;
193c0cb022bSYaroslav Tykhiy #endif
194a3814acfSSam Leffler };
195a3814acfSSam Leffler 
19675ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
1971cf236fbSYaroslav Tykhiy static struct {
1981cf236fbSYaroslav Tykhiy 	int flag;
1991cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
2001cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
2011cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
2021cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
2031cf236fbSYaroslav Tykhiy 	{0, NULL}
2041cf236fbSYaroslav Tykhiy };
205a3814acfSSam Leffler 
206f1379734SKonstantin Belousov extern int vlan_mtag_pcp;
2072ccbbd06SMarcelo Araujo 
20842a58907SGleb Smirnoff static const char vlanname[] = "vlan";
20942a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
2102cc2df49SGarrett Wollman 
2115cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
212ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag;
2135cb8c31aSYaroslav Tykhiy 
2144faedfe8SSam Leffler /*
215b08d611dSMatt Macy  * if_vlan uses two module-level synchronizations primitives to allow concurrent
216b08d611dSMatt Macy  * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
217b08d611dSMatt Macy  * while they are being used for tx/rx. To accomplish this in a way that has
218b08d611dSMatt Macy  * acceptable performance and cooperation with other parts of the network stack
219b08d611dSMatt Macy  * there is a non-sleepable epoch(9) and an sx(9).
22075ee267cSGleb Smirnoff  *
221b08d611dSMatt Macy  * The performance-sensitive paths that warrant using the epoch(9) are
222d148c2a2SMatt Joras  * vlan_transmit and vlan_input. Both have to check for the vlan interface's
223d148c2a2SMatt Joras  * existence using if_vlantrunk, and being in the network tx/rx paths the use
224b08d611dSMatt Macy  * of an epoch(9) gives a measureable improvement in performance.
22575ee267cSGleb Smirnoff  *
226d148c2a2SMatt Joras  * The reason for having an sx(9) is mostly because there are still areas that
227d148c2a2SMatt Joras  * must be sleepable and also have safe concurrent access to a vlan interface.
228d148c2a2SMatt Joras  * Since the sx(9) exists, it is used by default in most paths unless sleeping
229d148c2a2SMatt Joras  * is not permitted, or if it is not clear whether sleeping is permitted.
230d148c2a2SMatt Joras  *
2314faedfe8SSam Leffler  */
232d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx
233d148c2a2SMatt Joras 
234d148c2a2SMatt Joras static struct sx _VLAN_SX_ID;
235d148c2a2SMatt Joras 
236d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \
237d148c2a2SMatt Joras 	sx_init(&_VLAN_SX_ID, "vlan_sx")
238d148c2a2SMatt Joras 
239d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \
240d148c2a2SMatt Joras 	sx_destroy(&_VLAN_SX_ID)
241d148c2a2SMatt Joras 
242d148c2a2SMatt Joras #define	VLAN_SLOCK()			sx_slock(&_VLAN_SX_ID)
243d148c2a2SMatt Joras #define	VLAN_SUNLOCK()			sx_sunlock(&_VLAN_SX_ID)
244d148c2a2SMatt Joras #define	VLAN_XLOCK()			sx_xlock(&_VLAN_SX_ID)
245d148c2a2SMatt Joras #define	VLAN_XUNLOCK()			sx_xunlock(&_VLAN_SX_ID)
246d148c2a2SMatt Joras #define	VLAN_SLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
247d148c2a2SMatt Joras #define	VLAN_XLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
248d148c2a2SMatt Joras #define	VLAN_SXLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_LOCKED)
249d148c2a2SMatt Joras 
250d148c2a2SMatt Joras 
251d148c2a2SMatt Joras /*
252b08d611dSMatt Macy  * We also have a per-trunk mutex that should be acquired when changing
253b08d611dSMatt Macy  * its state.
254d148c2a2SMatt Joras  */
255b08d611dSMatt Macy #define	TRUNK_LOCK_INIT(trunk)		mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
256b08d611dSMatt Macy #define	TRUNK_LOCK_DESTROY(trunk)	mtx_destroy(&(trunk)->lock)
257b08d611dSMatt Macy #define	TRUNK_WLOCK(trunk)		mtx_lock(&(trunk)->lock)
258b08d611dSMatt Macy #define	TRUNK_WUNLOCK(trunk)		mtx_unlock(&(trunk)->lock)
259b08d611dSMatt Macy #define	TRUNK_LOCK_ASSERT(trunk)	MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(trunk)->lock))
260b08d611dSMatt Macy #define	TRUNK_WLOCK_ASSERT(trunk)	mtx_assert(&(trunk)->lock, MA_OWNED);
26175ee267cSGleb Smirnoff 
262d148c2a2SMatt Joras /*
263d148c2a2SMatt Joras  * The VLAN_ARRAY substitutes the dynamic hash with a static array
264d148c2a2SMatt Joras  * with 4096 entries. In theory this can give a boost in processing,
265d148c2a2SMatt Joras  * however in practice it does not. Probably this is because the array
266d148c2a2SMatt Joras  * is too big to fit into CPU cache.
267d148c2a2SMatt Joras  */
26875ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
26975ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
27075ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
27175ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
27275ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
27375ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
27475ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
2757983103aSRobert Watson 	uint16_t vid);
27675ee267cSGleb Smirnoff #endif
27775ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
2784faedfe8SSam Leffler 
279114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
280a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
281cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
282*b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
283f3e7afe2SHans Petter Selasky static	int vlan_snd_tag_alloc(struct ifnet *,
284f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *, struct m_snd_tag **);
285fb3bc596SJohn Baldwin static	int vlan_snd_tag_modify(struct m_snd_tag *,
286fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *);
287fb3bc596SJohn Baldwin static	int vlan_snd_tag_query(struct m_snd_tag *,
288fb3bc596SJohn Baldwin     union if_snd_tag_query_params *);
289fa91f845SRandall Stewart static	void vlan_snd_tag_free(struct m_snd_tag *);
290f3e7afe2SHans Petter Selasky #endif
291d9b1d615SJohn Baldwin static	void vlan_qflush(struct ifnet *ifp);
2921cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
2931cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
2941cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
295f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
296d9b1d615SJohn Baldwin static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
2976f359e28SJohn Baldwin static	void vlan_unconfig(struct ifnet *ifp);
29828cc4d37SJohn Baldwin static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
29975ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
300a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
30175ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
30275ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
303f731f104SBill Paul 
304f941c31aSGleb Smirnoff static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
305f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
3066b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
307f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
308f889d2efSBrooks Davis 
3095cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
310ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
3115cb8c31aSYaroslav Tykhiy 
312d148c2a2SMatt Joras static  void vlan_lladdr_fn(void *arg, int pending);
313d148c2a2SMatt Joras 
31442a58907SGleb Smirnoff static struct if_clone *vlan_cloner;
3159d4fe4b2SBrooks Davis 
316ccf7ba97SMarko Zec #ifdef VIMAGE
3175f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
318ccf7ba97SMarko Zec #define	V_vlan_cloner	VNET(vlan_cloner)
319ccf7ba97SMarko Zec #endif
320ccf7ba97SMarko Zec 
32175ee267cSGleb Smirnoff static void
322c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx)
323c32a9d66SHans Petter Selasky {
324c32a9d66SHans Petter Selasky 	struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
325c32a9d66SHans Petter Selasky 	free(mc, M_VLAN);
326c32a9d66SHans Petter Selasky }
327c32a9d66SHans Petter Selasky 
328cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY
329cac30248SOleg Bulyzhin #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
330cac30248SOleg Bulyzhin 
331c32a9d66SHans Petter Selasky static void
33275ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
33375ee267cSGleb Smirnoff {
33475ee267cSGleb Smirnoff 	int i, n;
33575ee267cSGleb Smirnoff 
33675ee267cSGleb Smirnoff 	/*
33775ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
33875ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
33975ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
34075ee267cSGleb Smirnoff 	 */
34175ee267cSGleb Smirnoff 
34275ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
34375ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
34475ee267cSGleb Smirnoff 
34575ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
34675ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
34775ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
34875ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
34975ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
350b08d611dSMatt Macy 		CK_SLIST_INIT(&trunk->hash[i]);
35175ee267cSGleb Smirnoff }
35275ee267cSGleb Smirnoff 
35375ee267cSGleb Smirnoff static void
35475ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
35575ee267cSGleb Smirnoff {
35675ee267cSGleb Smirnoff #ifdef INVARIANTS
35775ee267cSGleb Smirnoff 	int i;
35875ee267cSGleb Smirnoff 
35975ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
36075ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
361b08d611dSMatt Macy 		KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
36275ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
36375ee267cSGleb Smirnoff #endif
36475ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
36575ee267cSGleb Smirnoff 	trunk->hash = NULL;
36675ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
36775ee267cSGleb Smirnoff }
36875ee267cSGleb Smirnoff 
36975ee267cSGleb Smirnoff static int
37075ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
37175ee267cSGleb Smirnoff {
37275ee267cSGleb Smirnoff 	int i, b;
37375ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
37475ee267cSGleb Smirnoff 
375b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
37675ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
37775ee267cSGleb Smirnoff 
37875ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
3797983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
380b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
3817983103aSRobert Watson 		if (ifv->ifv_vid == ifv2->ifv_vid)
38275ee267cSGleb Smirnoff 			return (EEXIST);
38375ee267cSGleb Smirnoff 
38475ee267cSGleb Smirnoff 	/*
38575ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
38675ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
38775ee267cSGleb Smirnoff 	 * buckets/2.
38875ee267cSGleb Smirnoff 	 */
38975ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
39075ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
3917983103aSRobert Watson 		i = HASH(ifv->ifv_vid, trunk->hmask);
39275ee267cSGleb Smirnoff 	}
393b08d611dSMatt Macy 	CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
39475ee267cSGleb Smirnoff 	trunk->refcnt++;
39575ee267cSGleb Smirnoff 
39675ee267cSGleb Smirnoff 	return (0);
39775ee267cSGleb Smirnoff }
39875ee267cSGleb Smirnoff 
39975ee267cSGleb Smirnoff static int
40075ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
40175ee267cSGleb Smirnoff {
40275ee267cSGleb Smirnoff 	int i, b;
40375ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
40475ee267cSGleb Smirnoff 
405b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
40675ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
40775ee267cSGleb Smirnoff 
40875ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
4097983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
410b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
41175ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
41275ee267cSGleb Smirnoff 			trunk->refcnt--;
413b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
41475ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
41575ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
41675ee267cSGleb Smirnoff 			return (0);
41775ee267cSGleb Smirnoff 		}
41875ee267cSGleb Smirnoff 
41975ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
42075ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
42175ee267cSGleb Smirnoff }
42275ee267cSGleb Smirnoff 
42375ee267cSGleb Smirnoff /*
42475ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
42575ee267cSGleb Smirnoff  */
42675ee267cSGleb Smirnoff static void
42775ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
42875ee267cSGleb Smirnoff {
42975ee267cSGleb Smirnoff 	struct ifvlan *ifv;
43075ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
43175ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
43275ee267cSGleb Smirnoff 
433b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
43475ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
43575ee267cSGleb Smirnoff 
43675ee267cSGleb Smirnoff 	if (howmuch == 0) {
43775ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
43875ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
43975ee267cSGleb Smirnoff 		return;
44075ee267cSGleb Smirnoff 	}
44175ee267cSGleb Smirnoff 
44275ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
44375ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
44475ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
44575ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
44675ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
44775ee267cSGleb Smirnoff 		return;
44875ee267cSGleb Smirnoff 
449b08d611dSMatt Macy 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
45075ee267cSGleb Smirnoff 	if (hash2 == NULL) {
45175ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
45275ee267cSGleb Smirnoff 		    __func__);
45375ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
45475ee267cSGleb Smirnoff 	}
45575ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
456b08d611dSMatt Macy 		CK_SLIST_INIT(&hash2[j]);
45775ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
458b08d611dSMatt Macy 		while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
459b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
4607983103aSRobert Watson 			j = HASH(ifv->ifv_vid, n2 - 1);
461b08d611dSMatt Macy 			CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
46275ee267cSGleb Smirnoff 		}
463b08d611dSMatt Macy 	NET_EPOCH_WAIT();
46475ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
46575ee267cSGleb Smirnoff 	trunk->hash = hash2;
46675ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
46775ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
468f84b2d69SYaroslav Tykhiy 
469f84b2d69SYaroslav Tykhiy 	if (bootverbose)
470f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
471f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
47275ee267cSGleb Smirnoff }
47375ee267cSGleb Smirnoff 
47475ee267cSGleb Smirnoff static __inline struct ifvlan *
4757983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
47675ee267cSGleb Smirnoff {
47775ee267cSGleb Smirnoff 	struct ifvlan *ifv;
47875ee267cSGleb Smirnoff 
479a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
48075ee267cSGleb Smirnoff 
481b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
4827983103aSRobert Watson 		if (ifv->ifv_vid == vid)
48375ee267cSGleb Smirnoff 			return (ifv);
48475ee267cSGleb Smirnoff 	return (NULL);
48575ee267cSGleb Smirnoff }
48675ee267cSGleb Smirnoff 
48775ee267cSGleb Smirnoff #if 0
48875ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
48975ee267cSGleb Smirnoff static void
49075ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
49175ee267cSGleb Smirnoff {
49275ee267cSGleb Smirnoff 	int i;
49375ee267cSGleb Smirnoff 	struct ifvlan *ifv;
49475ee267cSGleb Smirnoff 
49575ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
49675ee267cSGleb Smirnoff 		printf("%d: ", i);
497b08d611dSMatt Macy 		CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
49875ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
49975ee267cSGleb Smirnoff 		printf("\n");
50075ee267cSGleb Smirnoff 	}
50175ee267cSGleb Smirnoff }
50275ee267cSGleb Smirnoff #endif /* 0 */
503e4cd31ddSJeff Roberson #else
504e4cd31ddSJeff Roberson 
505e4cd31ddSJeff Roberson static __inline struct ifvlan *
5067983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
507e4cd31ddSJeff Roberson {
508e4cd31ddSJeff Roberson 
5097983103aSRobert Watson 	return trunk->vlans[vid];
510e4cd31ddSJeff Roberson }
511e4cd31ddSJeff Roberson 
512e4cd31ddSJeff Roberson static __inline int
513e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
514e4cd31ddSJeff Roberson {
515e4cd31ddSJeff Roberson 
5167983103aSRobert Watson 	if (trunk->vlans[ifv->ifv_vid] != NULL)
517e4cd31ddSJeff Roberson 		return EEXIST;
5187983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = ifv;
519e4cd31ddSJeff Roberson 	trunk->refcnt++;
520e4cd31ddSJeff Roberson 
521e4cd31ddSJeff Roberson 	return (0);
522e4cd31ddSJeff Roberson }
523e4cd31ddSJeff Roberson 
524e4cd31ddSJeff Roberson static __inline int
525e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
526e4cd31ddSJeff Roberson {
527e4cd31ddSJeff Roberson 
5287983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = NULL;
529e4cd31ddSJeff Roberson 	trunk->refcnt--;
530e4cd31ddSJeff Roberson 
531e4cd31ddSJeff Roberson 	return (0);
532e4cd31ddSJeff Roberson }
533e4cd31ddSJeff Roberson 
534e4cd31ddSJeff Roberson static __inline void
535e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk)
536e4cd31ddSJeff Roberson {
537e4cd31ddSJeff Roberson }
538e4cd31ddSJeff Roberson 
539e4cd31ddSJeff Roberson static __inline void
540e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk)
541e4cd31ddSJeff Roberson {
542e4cd31ddSJeff Roberson }
543e4cd31ddSJeff Roberson 
54475ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
54575ee267cSGleb Smirnoff 
54675ee267cSGleb Smirnoff static void
54775ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
54875ee267cSGleb Smirnoff {
549d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
55075ee267cSGleb Smirnoff 
55175ee267cSGleb Smirnoff 	vlan_freehash(trunk);
55275ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
55333499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
5549bcf3ae4SAlexander Motin 	if_rele(trunk->parent);
55575ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
55675ee267cSGleb Smirnoff }
55775ee267cSGleb Smirnoff 
558f731f104SBill Paul /*
559f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
560f731f104SBill Paul  * programming the multicast filter of the parent. This has the
561f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
562f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
563f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
564f731f104SBill Paul  * to avoid this: there really is only one physical interface.
565f731f104SBill Paul  */
5662b120974SPeter Wemm static int
5672b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
568f731f104SBill Paul {
569f731f104SBill Paul 	struct ifnet		*ifp_p;
5702d222cb7SAlexander Motin 	struct ifmultiaddr	*ifma;
571f731f104SBill Paul 	struct ifvlan		*sc;
572c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
573f731f104SBill Paul 	int			error;
574f731f104SBill Paul 
575b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
576d148c2a2SMatt Joras 
577f731f104SBill Paul 	/* Find the parent. */
578f731f104SBill Paul 	sc = ifp->if_softc;
57975ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
5801b2a4f7aSBill Fenner 
5818b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
5828b615593SMarko Zec 
583f731f104SBill Paul 	/* First, remove any existing filter entries. */
584b08d611dSMatt Macy 	while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
585b08d611dSMatt Macy 		CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
5862d222cb7SAlexander Motin 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
587c32a9d66SHans Petter Selasky 		epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
588f731f104SBill Paul 	}
589f731f104SBill Paul 
590f731f104SBill Paul 	/* Now program new ones. */
5912d222cb7SAlexander Motin 	IF_ADDR_WLOCK(ifp);
592d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
593f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
594f731f104SBill Paul 			continue;
59529c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
5962d222cb7SAlexander Motin 		if (mc == NULL) {
5972d222cb7SAlexander Motin 			IF_ADDR_WUNLOCK(ifp);
59829c2dfbeSBruce M Simpson 			return (ENOMEM);
5992d222cb7SAlexander Motin 		}
600e4cd31ddSJeff Roberson 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
601e4cd31ddSJeff Roberson 		mc->mc_addr.sdl_index = ifp_p->if_index;
602b08d611dSMatt Macy 		CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
6032d222cb7SAlexander Motin 	}
6042d222cb7SAlexander Motin 	IF_ADDR_WUNLOCK(ifp);
605b08d611dSMatt Macy 	CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
606e4cd31ddSJeff Roberson 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
6072d222cb7SAlexander Motin 		    NULL);
608f731f104SBill Paul 		if (error)
609f731f104SBill Paul 			return (error);
610f731f104SBill Paul 	}
611f731f104SBill Paul 
6128b615593SMarko Zec 	CURVNET_RESTORE();
613f731f104SBill Paul 	return (0);
614f731f104SBill Paul }
6152cc2df49SGarrett Wollman 
616a3814acfSSam Leffler /*
617ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
618ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
619ea4ca115SAndrew Thompson  * should also change it on all children vlans.
620ea4ca115SAndrew Thompson  */
621ea4ca115SAndrew Thompson static void
622ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
623ea4ca115SAndrew Thompson {
624a68cc388SGleb Smirnoff 	struct epoch_tracker et;
625ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
626d148c2a2SMatt Joras 	struct ifnet *ifv_ifp;
627d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
628d148c2a2SMatt Joras 	struct sockaddr_dl *sdl;
629ea4ca115SAndrew Thompson 
6307b7f772fSGleb Smirnoff 	/* Need the epoch since this is run on taskqueue_swi. */
631a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
632d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
633d148c2a2SMatt Joras 	if (trunk == NULL) {
634a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
635ea4ca115SAndrew Thompson 		return;
636d148c2a2SMatt Joras 	}
637ea4ca115SAndrew Thompson 
638ea4ca115SAndrew Thompson 	/*
639ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
640d148c2a2SMatt Joras 	 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
641d148c2a2SMatt Joras 	 * ioctl calls on the parent garbling the lladdr of the child vlan.
642ea4ca115SAndrew Thompson 	 */
643d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
644d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
645d148c2a2SMatt Joras 		/*
646d148c2a2SMatt Joras 		 * Copy new new lladdr into the ifv_ifp, enqueue a task
647d148c2a2SMatt Joras 		 * to actually call if_setlladdr. if_setlladdr needs to
648d148c2a2SMatt Joras 		 * be deferred to a taskqueue because it will call into
649d148c2a2SMatt Joras 		 * the if_vlan ioctl path and try to acquire the global
650d148c2a2SMatt Joras 		 * lock.
651d148c2a2SMatt Joras 		 */
652d148c2a2SMatt Joras 		ifv_ifp = ifv->ifv_ifp;
653d148c2a2SMatt Joras 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
654e4cd31ddSJeff Roberson 		    ifp->if_addrlen);
655d148c2a2SMatt Joras 		sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
656d148c2a2SMatt Joras 		sdl->sdl_alen = ifp->if_addrlen;
657d148c2a2SMatt Joras 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
6586117727bSAndrew Thompson 	}
659d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
660a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
661ea4ca115SAndrew Thompson }
662ea4ca115SAndrew Thompson 
663ea4ca115SAndrew Thompson /*
6645cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
6655cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
6665cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
6675428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
6685428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
6695cb8c31aSYaroslav Tykhiy  */
6705cb8c31aSYaroslav Tykhiy static void
6715cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
6725cb8c31aSYaroslav Tykhiy {
6735cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
674d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
6755cb8c31aSYaroslav Tykhiy 
6765428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
6775428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
6785428776eSJohn Baldwin 		return;
679d148c2a2SMatt Joras 	VLAN_XLOCK();
680d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
681d148c2a2SMatt Joras 	if (trunk == NULL) {
682d148c2a2SMatt Joras 		VLAN_XUNLOCK();
683d148c2a2SMatt Joras 		return;
684d148c2a2SMatt Joras 	}
6855428776eSJohn Baldwin 
6865cb8c31aSYaroslav Tykhiy 	/*
6875cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
6885cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
6895cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
6905cb8c31aSYaroslav Tykhiy 	 */
691d148c2a2SMatt Joras 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
692d148c2a2SMatt Joras 	    ifp->if_vlantrunk == NULL)
69328cc4d37SJohn Baldwin 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
694d148c2a2SMatt Joras 
6955cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
6965cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
697d148c2a2SMatt Joras 	VLAN_XUNLOCK();
6985cb8c31aSYaroslav Tykhiy }
6995cb8c31aSYaroslav Tykhiy 
7005cb8c31aSYaroslav Tykhiy /*
701e4cd31ddSJeff Roberson  * Return the trunk device for a virtual interface.
702e4cd31ddSJeff Roberson  */
703e4cd31ddSJeff Roberson static struct ifnet  *
704e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp)
705e4cd31ddSJeff Roberson {
706a68cc388SGleb Smirnoff 	struct epoch_tracker et;
707e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
708e4cd31ddSJeff Roberson 
709e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
710e4cd31ddSJeff Roberson 		return (NULL);
711d148c2a2SMatt Joras 
712a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
713e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
714e4cd31ddSJeff Roberson 	ifp = NULL;
715e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
716e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
717a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
718e4cd31ddSJeff Roberson 	return (ifp);
719e4cd31ddSJeff Roberson }
720e4cd31ddSJeff Roberson 
721e4cd31ddSJeff Roberson /*
7227983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
7237983103aSRobert Watson  * components such as Infiniband.
7247983103aSRobert Watson  *
7257983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
7267983103aSRobert Watson  * vlan_vid().
727e4cd31ddSJeff Roberson  */
728e4cd31ddSJeff Roberson static int
7297983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
730e4cd31ddSJeff Roberson {
731e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
732e4cd31ddSJeff Roberson 
733e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
734e4cd31ddSJeff Roberson 		return (EINVAL);
735e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
7367983103aSRobert Watson 	*vidp = ifv->ifv_vid;
737e4cd31ddSJeff Roberson 	return (0);
738e4cd31ddSJeff Roberson }
739e4cd31ddSJeff Roberson 
74032d2623aSNavdeep Parhar static int
74132d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
74232d2623aSNavdeep Parhar {
74332d2623aSNavdeep Parhar 	struct ifvlan *ifv;
74432d2623aSNavdeep Parhar 
74532d2623aSNavdeep Parhar 	if (ifp->if_type != IFT_L2VLAN)
74632d2623aSNavdeep Parhar 		return (EINVAL);
74732d2623aSNavdeep Parhar 	ifv = ifp->if_softc;
74832d2623aSNavdeep Parhar 	*pcpp = ifv->ifv_pcp;
74932d2623aSNavdeep Parhar 	return (0);
75032d2623aSNavdeep Parhar }
75132d2623aSNavdeep Parhar 
752e4cd31ddSJeff Roberson /*
753e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
754e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
755e4cd31ddSJeff Roberson  */
756e4cd31ddSJeff Roberson static void *
757e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
758e4cd31ddSJeff Roberson {
759e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
760e4cd31ddSJeff Roberson 
761e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
762e4cd31ddSJeff Roberson 		return (NULL);
763e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
764e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
765e4cd31ddSJeff Roberson }
766e4cd31ddSJeff Roberson 
767e4cd31ddSJeff Roberson /*
768e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
769e4cd31ddSJeff Roberson  * private per-instance data in.
770e4cd31ddSJeff Roberson  */
771e4cd31ddSJeff Roberson static int
772e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
773e4cd31ddSJeff Roberson {
774e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
775e4cd31ddSJeff Roberson 
776e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
777e4cd31ddSJeff Roberson 		return (EINVAL);
778e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
779e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
780e4cd31ddSJeff Roberson 	return (0);
781e4cd31ddSJeff Roberson }
782e4cd31ddSJeff Roberson 
783e4cd31ddSJeff Roberson /*
7847983103aSRobert Watson  * Return the vlan device present at the specific VID.
785e4cd31ddSJeff Roberson  */
786e4cd31ddSJeff Roberson static struct ifnet *
7877983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
788e4cd31ddSJeff Roberson {
789a68cc388SGleb Smirnoff 	struct epoch_tracker et;
790e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
791e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
792e4cd31ddSJeff Roberson 
793a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
794e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
795d148c2a2SMatt Joras 	if (trunk == NULL) {
796a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
797e4cd31ddSJeff Roberson 		return (NULL);
798d148c2a2SMatt Joras 	}
799e4cd31ddSJeff Roberson 	ifp = NULL;
8007983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
801e4cd31ddSJeff Roberson 	if (ifv)
802e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
803a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
804e4cd31ddSJeff Roberson 	return (ifp);
805e4cd31ddSJeff Roberson }
806e4cd31ddSJeff Roberson 
807e4cd31ddSJeff Roberson /*
8082ccbbd06SMarcelo Araujo  * Recalculate the cached VLAN tag exposed via the MIB.
8092ccbbd06SMarcelo Araujo  */
8102ccbbd06SMarcelo Araujo static void
8112ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv)
8122ccbbd06SMarcelo Araujo {
8132ccbbd06SMarcelo Araujo 
8142ccbbd06SMarcelo Araujo        ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
8152ccbbd06SMarcelo Araujo }
8162ccbbd06SMarcelo Araujo 
8172ccbbd06SMarcelo Araujo /*
818a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
819a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
820a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
821a3814acfSSam Leffler  * set here.  No one else in the system should be aware of this so
822a3814acfSSam Leffler  * we use an explicit reference here.
823a3814acfSSam Leffler  */
824a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
825a3814acfSSam Leffler 
826984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
827a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
828127d7b2dSAndre Oppermann 
8292b120974SPeter Wemm static int
8302b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
8312b120974SPeter Wemm {
8329d4fe4b2SBrooks Davis 
8332b120974SPeter Wemm 	switch (type) {
8342b120974SPeter Wemm 	case MOD_LOAD:
8355cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
8365cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
8375cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
8385cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
839ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
840ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
841ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
842ea4ca115SAndrew Thompson 			return (ENOMEM);
843d148c2a2SMatt Joras 		VLAN_LOCKING_INIT();
8449d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
845127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
84675ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
847e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
848e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
849e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
850e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
85132d2623aSNavdeep Parhar 		vlan_pcp_p = vlan_pcp;
852e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
853ccf7ba97SMarko Zec #ifndef VIMAGE
85442a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
85542a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
856ccf7ba97SMarko Zec #endif
85725c0f7b3SYaroslav Tykhiy 		if (bootverbose)
85825c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
85925c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
86025c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
86125c0f7b3SYaroslav Tykhiy #else
86225c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
86325c0f7b3SYaroslav Tykhiy #endif
86425c0f7b3SYaroslav Tykhiy 
86525c0f7b3SYaroslav Tykhiy 			       "\n");
8662b120974SPeter Wemm 		break;
8672b120974SPeter Wemm 	case MOD_UNLOAD:
868ccf7ba97SMarko Zec #ifndef VIMAGE
86942a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
870ccf7ba97SMarko Zec #endif
8715cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
872ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
8739d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
874127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
87575ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
876e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
877e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
87809fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
87909fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
880e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
881d148c2a2SMatt Joras 		VLAN_LOCKING_DESTROY();
88225c0f7b3SYaroslav Tykhiy 		if (bootverbose)
88325c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
8849d4fe4b2SBrooks Davis 		break;
8853e019deaSPoul-Henning Kamp 	default:
8863e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
8872b120974SPeter Wemm 	}
88815a66c21SBruce M Simpson 	return (0);
8892b120974SPeter Wemm }
8902b120974SPeter Wemm 
8912b120974SPeter Wemm static moduledata_t vlan_mod = {
8922b120974SPeter Wemm 	"if_vlan",
8932b120974SPeter Wemm 	vlan_modevent,
8949823d527SKevin Lo 	0
8952b120974SPeter Wemm };
8962b120974SPeter Wemm 
8972b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
89811edc477SEd Maste MODULE_VERSION(if_vlan, 3);
8992cc2df49SGarrett Wollman 
900ccf7ba97SMarko Zec #ifdef VIMAGE
901ccf7ba97SMarko Zec static void
902ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
903ccf7ba97SMarko Zec {
904ccf7ba97SMarko Zec 
90542a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
90642a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
907ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
908ccf7ba97SMarko Zec }
909ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
910ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
911ccf7ba97SMarko Zec 
912ccf7ba97SMarko Zec static void
913ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
914ccf7ba97SMarko Zec {
915ccf7ba97SMarko Zec 
91642a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
917ccf7ba97SMarko Zec }
91889856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
919ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
920ccf7ba97SMarko Zec #endif
921ccf7ba97SMarko Zec 
922f941c31aSGleb Smirnoff /*
923f941c31aSGleb Smirnoff  * Check for <etherif>.<vlan> style interface names.
924f941c31aSGleb Smirnoff  */
925f889d2efSBrooks Davis static struct ifnet *
926f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp)
9279d4fe4b2SBrooks Davis {
928f941c31aSGleb Smirnoff 	char ifname[IFNAMSIZ];
929f941c31aSGleb Smirnoff 	char *cp;
930f889d2efSBrooks Davis 	struct ifnet *ifp;
9317983103aSRobert Watson 	int vid;
932f889d2efSBrooks Davis 
933f941c31aSGleb Smirnoff 	strlcpy(ifname, name, IFNAMSIZ);
934f941c31aSGleb Smirnoff 	if ((cp = strchr(ifname, '.')) == NULL)
935f941c31aSGleb Smirnoff 		return (NULL);
936f941c31aSGleb Smirnoff 	*cp = '\0';
9379bcf3ae4SAlexander Motin 	if ((ifp = ifunit_ref(ifname)) == NULL)
938f941c31aSGleb Smirnoff 		return (NULL);
939f941c31aSGleb Smirnoff 	/* Parse VID. */
9409bcf3ae4SAlexander Motin 	if (*++cp == '\0') {
9419bcf3ae4SAlexander Motin 		if_rele(ifp);
942f941c31aSGleb Smirnoff 		return (NULL);
9439bcf3ae4SAlexander Motin 	}
9447983103aSRobert Watson 	vid = 0;
945fb92ad4aSJohn Baldwin 	for(; *cp >= '0' && *cp <= '9'; cp++)
9467983103aSRobert Watson 		vid = (vid * 10) + (*cp - '0');
9479bcf3ae4SAlexander Motin 	if (*cp != '\0') {
9489bcf3ae4SAlexander Motin 		if_rele(ifp);
949f941c31aSGleb Smirnoff 		return (NULL);
9509bcf3ae4SAlexander Motin 	}
9517983103aSRobert Watson 	if (vidp != NULL)
9527983103aSRobert Watson 		*vidp = vid;
953f889d2efSBrooks Davis 
95415a66c21SBruce M Simpson 	return (ifp);
955f889d2efSBrooks Davis }
956f889d2efSBrooks Davis 
957f889d2efSBrooks Davis static int
958f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
959f889d2efSBrooks Davis {
960f889d2efSBrooks Davis 	const char *cp;
961f889d2efSBrooks Davis 
962f941c31aSGleb Smirnoff 	if (vlan_clone_match_ethervid(name, NULL) != NULL)
963f889d2efSBrooks Davis 		return (1);
964f889d2efSBrooks Davis 
96542a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
966f889d2efSBrooks Davis 		return (0);
967f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
968f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
969f889d2efSBrooks Davis 			return (0);
970f889d2efSBrooks Davis 	}
971f889d2efSBrooks Davis 
972f889d2efSBrooks Davis 	return (1);
973f889d2efSBrooks Davis }
974f889d2efSBrooks Davis 
975f889d2efSBrooks Davis static int
9766b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
977f889d2efSBrooks Davis {
978f889d2efSBrooks Davis 	char *dp;
979f889d2efSBrooks Davis 	int wildcard;
980f889d2efSBrooks Davis 	int unit;
981f889d2efSBrooks Davis 	int error;
9827983103aSRobert Watson 	int vid;
9839d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
9849d4fe4b2SBrooks Davis 	struct ifnet *ifp;
985f889d2efSBrooks Davis 	struct ifnet *p;
9863ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
9873ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
9886b7330e2SSam Leffler 	struct vlanreq vlr;
98965239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
990f889d2efSBrooks Davis 
9916b7330e2SSam Leffler 	/*
9926b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
9936b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
9946b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
9956b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
9966b7330e2SSam Leffler 	 *   must be configured separately.
9976b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
998a4641f4eSPedro F. Giffuni 	 * supported for backwards compatibility.
9997983103aSRobert Watson 	 *
10007983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
10017983103aSRobert Watson 	 * called for.
10026b7330e2SSam Leffler 	 */
10036b7330e2SSam Leffler 	if (params) {
10046b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
10056b7330e2SSam Leffler 		if (error)
10066b7330e2SSam Leffler 			return error;
10079bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
10086b7330e2SSam Leffler 		if (p == NULL)
1009b1828acfSGleb Smirnoff 			return (ENXIO);
10106b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
10119bcf3ae4SAlexander Motin 		if (error != 0) {
10129bcf3ae4SAlexander Motin 			if_rele(p);
10136b7330e2SSam Leffler 			return (error);
10149bcf3ae4SAlexander Motin 		}
10157983103aSRobert Watson 		vid = vlr.vlr_tag;
10166b7330e2SSam Leffler 		wildcard = (unit < 0);
1017f941c31aSGleb Smirnoff 	} else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1018f889d2efSBrooks Davis 		unit = -1;
1019f889d2efSBrooks Davis 		wildcard = 0;
1020f889d2efSBrooks Davis 	} else {
10219bcf3ae4SAlexander Motin 		p = NULL;
1022f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
1023f889d2efSBrooks Davis 		if (error != 0)
1024f889d2efSBrooks Davis 			return (error);
1025f889d2efSBrooks Davis 
1026f889d2efSBrooks Davis 		wildcard = (unit < 0);
1027f889d2efSBrooks Davis 	}
1028f889d2efSBrooks Davis 
1029f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
10309bcf3ae4SAlexander Motin 	if (error != 0) {
10319bcf3ae4SAlexander Motin 		if (p != NULL)
10329bcf3ae4SAlexander Motin 			if_rele(p);
1033f889d2efSBrooks Davis 		return (error);
10349bcf3ae4SAlexander Motin 	}
1035f889d2efSBrooks Davis 
1036f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
1037f889d2efSBrooks Davis 	if (wildcard) {
1038f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
1039f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1040f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
1041f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
1042f889d2efSBrooks Davis 		}
1043f889d2efSBrooks Davis 	}
10449d4fe4b2SBrooks Davis 
1045a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1046fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1047fc74a9f9SBrooks Davis 	if (ifp == NULL) {
1048fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
1049fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
10509bcf3ae4SAlexander Motin 		if (p != NULL)
10519bcf3ae4SAlexander Motin 			if_rele(p);
1052fc74a9f9SBrooks Davis 		return (ENOSPC);
1053fc74a9f9SBrooks Davis 	}
1054b08d611dSMatt Macy 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
10559d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
1056f889d2efSBrooks Davis 	/*
1057cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
1058f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
1059f889d2efSBrooks Davis 	 */
1060f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
106142a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
1062f889d2efSBrooks Davis 	ifp->if_dunit = unit;
10639d4fe4b2SBrooks Davis 
1064114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
1065d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
1066d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
10679d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
1068*b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1069f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1070fb3bc596SJohn Baldwin 	ifp->if_snd_tag_modify = vlan_snd_tag_modify;
1071fb3bc596SJohn Baldwin 	ifp->if_snd_tag_query = vlan_snd_tag_query;
1072fa91f845SRandall Stewart 	ifp->if_snd_tag_free = vlan_snd_tag_free;
1073f3e7afe2SHans Petter Selasky #endif
107464a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
1075fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
10769d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
1077211f625aSBill Fenner 	ifp->if_baudrate = 0;
1078a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
1079a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
10803ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
10813ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
10823ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
10839d4fe4b2SBrooks Davis 
10849bcf3ae4SAlexander Motin 	if (p != NULL) {
10857983103aSRobert Watson 		error = vlan_config(ifv, p, vid);
10869bcf3ae4SAlexander Motin 		if_rele(p);
1087f889d2efSBrooks Davis 		if (error != 0) {
1088f889d2efSBrooks Davis 			/*
108928cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
1090f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
1091f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1092f889d2efSBrooks Davis 			 */
1093f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1094249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
10954b22573aSBrooks Davis 			if_free(ifp);
109696c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
1097f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1098f889d2efSBrooks Davis 
1099f889d2efSBrooks Davis 			return (error);
1100f889d2efSBrooks Davis 		}
1101f889d2efSBrooks Davis 	}
1102f889d2efSBrooks Davis 
11039d4fe4b2SBrooks Davis 	return (0);
11049d4fe4b2SBrooks Davis }
11059d4fe4b2SBrooks Davis 
1106f889d2efSBrooks Davis static int
1107f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
11089d4fe4b2SBrooks Davis {
11099d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1110114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
1111b4e9f837SBrooks Davis 
1112249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1113249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1114d148c2a2SMatt Joras 	/*
1115d148c2a2SMatt Joras 	 * We should have the only reference to the ifv now, so we can now
1116d148c2a2SMatt Joras 	 * drain any remaining lladdr task before freeing the ifnet and the
1117d148c2a2SMatt Joras 	 * ifvlan.
1118d148c2a2SMatt Joras 	 */
1119d148c2a2SMatt Joras 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1120b08d611dSMatt Macy 	NET_EPOCH_WAIT();
11214b22573aSBrooks Davis 	if_free(ifp);
11229d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1123b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
1124b4e9f837SBrooks Davis 
1125f889d2efSBrooks Davis 	return (0);
11269d4fe4b2SBrooks Davis }
11279d4fe4b2SBrooks Davis 
112815a66c21SBruce M Simpson /*
112915a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
113015a66c21SBruce M Simpson  */
11312cc2df49SGarrett Wollman static void
1132114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
11332cc2df49SGarrett Wollman {
11342cc2df49SGarrett Wollman }
11352cc2df49SGarrett Wollman 
11366d3a3ab7SGleb Smirnoff /*
1137d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
11386d3a3ab7SGleb Smirnoff  */
1139d9b1d615SJohn Baldwin static int
1140d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
11412cc2df49SGarrett Wollman {
1142a68cc388SGleb Smirnoff 	struct epoch_tracker et;
11432cc2df49SGarrett Wollman 	struct ifvlan *ifv;
11442cc2df49SGarrett Wollman 	struct ifnet *p;
11451ad7a257SPyun YongHyeon 	int error, len, mcast;
11462cc2df49SGarrett Wollman 
1147a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
11482cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
1149d148c2a2SMatt Joras 	if (TRUNK(ifv) == NULL) {
1150d148c2a2SMatt Joras 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1151a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1152d148c2a2SMatt Joras 		m_freem(m);
1153d148c2a2SMatt Joras 		return (ENETDOWN);
1154d148c2a2SMatt Joras 	}
115575ee267cSGleb Smirnoff 	p = PARENT(ifv);
11561ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
11571ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
11582cc2df49SGarrett Wollman 
1159a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
11602cc2df49SGarrett Wollman 
1161*b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1162fb3bc596SJohn Baldwin 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1163fb3bc596SJohn Baldwin 		struct vlan_snd_tag *vst;
1164fb3bc596SJohn Baldwin 		struct m_snd_tag *mst;
1165fb3bc596SJohn Baldwin 
1166fb3bc596SJohn Baldwin 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1167fb3bc596SJohn Baldwin 		mst = m->m_pkthdr.snd_tag;
1168fb3bc596SJohn Baldwin 		vst = mst_to_vst(mst);
1169fb3bc596SJohn Baldwin 		if (vst->tag->ifp != p) {
1170fb3bc596SJohn Baldwin 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1171fb3bc596SJohn Baldwin 			NET_EPOCH_EXIT(et);
1172fb3bc596SJohn Baldwin 			m_freem(m);
1173fb3bc596SJohn Baldwin 			return (EAGAIN);
1174fb3bc596SJohn Baldwin 		}
1175fb3bc596SJohn Baldwin 
1176fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1177fb3bc596SJohn Baldwin 		m_snd_tag_rele(mst);
1178fb3bc596SJohn Baldwin 	}
1179fb3bc596SJohn Baldwin #endif
1180fb3bc596SJohn Baldwin 
1181f731f104SBill Paul 	/*
1182d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
118324993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
118424993214SYaroslav Tykhiy 	 */
11852dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
1186a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1187a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1188d148c2a2SMatt Joras 		m_freem(m);
11898b20f6cfSHiroki Sato 		return (ENETDOWN);
119024993214SYaroslav Tykhiy 	}
119124993214SYaroslav Tykhiy 
1192f1379734SKonstantin Belousov 	if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1193a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1194a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1195d9b1d615SJohn Baldwin 		return (0);
11964af90a4dSMatthew N. Dodd 	}
11972cc2df49SGarrett Wollman 
11982cc2df49SGarrett Wollman 	/*
11992cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
12002cc2df49SGarrett Wollman 	 */
1201aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1202299153b5SAlexander V. Chernikov 	if (error == 0) {
1203a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1204a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1205a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
12061ad7a257SPyun YongHyeon 	} else
1207a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1208a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1209d9b1d615SJohn Baldwin 	return (error);
12102cc2df49SGarrett Wollman }
1211d9b1d615SJohn Baldwin 
1212d9b1d615SJohn Baldwin /*
1213d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1214d9b1d615SJohn Baldwin  */
1215d9b1d615SJohn Baldwin static void
1216d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1217d9b1d615SJohn Baldwin {
1218f731f104SBill Paul }
1219f731f104SBill Paul 
1220a3814acfSSam Leffler static void
1221a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1222f731f104SBill Paul {
1223a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1224d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1225f731f104SBill Paul 	struct ifvlan *ifv;
12262ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
12272ccbbd06SMarcelo Araujo 	uint16_t vid, tag;
122875ee267cSGleb Smirnoff 
1229a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1230d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1231d148c2a2SMatt Joras 	if (trunk == NULL) {
1232a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1233d148c2a2SMatt Joras 		m_freem(m);
1234d148c2a2SMatt Joras 		return;
1235d148c2a2SMatt Joras 	}
1236a3814acfSSam Leffler 
1237f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1238a3814acfSSam Leffler 		/*
123914e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1240a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1241a3814acfSSam Leffler 		 */
12422ccbbd06SMarcelo Araujo 		tag = m->m_pkthdr.ether_vtag;
12436ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1244a3814acfSSam Leffler 	} else {
124575ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
124675ee267cSGleb Smirnoff 
124714e98256SYaroslav Tykhiy 		/*
124814e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
124914e98256SYaroslav Tykhiy 		 */
1250a3814acfSSam Leffler 		switch (ifp->if_type) {
1251a3814acfSSam Leffler 		case IFT_ETHER:
1252a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1253a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1254a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1255a68cc388SGleb Smirnoff 				NET_EPOCH_EXIT(et);
1256a3814acfSSam Leffler 				return;
1257a3814acfSSam Leffler 			}
1258a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
12592ccbbd06SMarcelo Araujo 			tag = ntohs(evl->evl_tag);
1260db8b5973SYaroslav Tykhiy 
1261db8b5973SYaroslav Tykhiy 			/*
12622dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
12632dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
12642dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
12652dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1266db8b5973SYaroslav Tykhiy 			 */
12672dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
12682dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
12692dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1270a3814acfSSam Leffler 			break;
12712dc879b3SYaroslav Tykhiy 
1272a3814acfSSam Leffler 		default:
1273db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
127460c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
127560c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1276a3814acfSSam Leffler #endif
12773751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1278a68cc388SGleb Smirnoff 			NET_EPOCH_EXIT(et);
1279d148c2a2SMatt Joras 			m_freem(m);
128060c60618SYaroslav Tykhiy 			return;
1281a3814acfSSam Leffler 		}
12827a46ec8fSBrooks Davis 	}
12837a46ec8fSBrooks Davis 
12842ccbbd06SMarcelo Araujo 	vid = EVL_VLANOFTAG(tag);
12852ccbbd06SMarcelo Araujo 
12867983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
12872dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1288a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1289b08d611dSMatt Macy 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1290d148c2a2SMatt Joras 		m_freem(m);
129175ee267cSGleb Smirnoff 		return;
129275ee267cSGleb Smirnoff 	}
1293f731f104SBill Paul 
12942ccbbd06SMarcelo Araujo 	if (vlan_mtag_pcp) {
12952ccbbd06SMarcelo Araujo 		/*
12962ccbbd06SMarcelo Araujo 		 * While uncommon, it is possible that we will find a 802.1q
12972ccbbd06SMarcelo Araujo 		 * packet encapsulated inside another packet that also had an
12982ccbbd06SMarcelo Araujo 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
12992ccbbd06SMarcelo Araujo 		 * arriving over ethernet.  In that case, we replace the
13002ccbbd06SMarcelo Araujo 		 * existing 802.1q PCP m_tag value.
13012ccbbd06SMarcelo Araujo 		 */
13022ccbbd06SMarcelo Araujo 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
13032ccbbd06SMarcelo Araujo 		if (mtag == NULL) {
13042ccbbd06SMarcelo Araujo 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
13052ccbbd06SMarcelo Araujo 			    sizeof(uint8_t), M_NOWAIT);
13062ccbbd06SMarcelo Araujo 			if (mtag == NULL) {
13072ccbbd06SMarcelo Araujo 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1308a68cc388SGleb Smirnoff 				NET_EPOCH_EXIT(et);
1309d148c2a2SMatt Joras 				m_freem(m);
13102ccbbd06SMarcelo Araujo 				return;
13112ccbbd06SMarcelo Araujo 			}
13122ccbbd06SMarcelo Araujo 			m_tag_prepend(m, mtag);
13132ccbbd06SMarcelo Araujo 		}
13142ccbbd06SMarcelo Araujo 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
13152ccbbd06SMarcelo Araujo 	}
13162ccbbd06SMarcelo Araujo 
1317fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1318c0304424SGleb Smirnoff 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1319a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13202cc2df49SGarrett Wollman 
1321a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1322fdbf1174SMatt Joras 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
13232cc2df49SGarrett Wollman }
13242cc2df49SGarrett Wollman 
1325d148c2a2SMatt Joras static void
1326d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused)
1327d148c2a2SMatt Joras {
1328d148c2a2SMatt Joras 	struct ifvlan *ifv;
1329d148c2a2SMatt Joras 	struct ifnet *ifp;
1330d148c2a2SMatt Joras 
1331d148c2a2SMatt Joras 	ifv = (struct ifvlan *)arg;
1332d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
13335191a3aeSKristof Provost 
13345191a3aeSKristof Provost 	CURVNET_SET(ifp->if_vnet);
13355191a3aeSKristof Provost 
1336d148c2a2SMatt Joras 	/* The ifv_ifp already has the lladdr copied in. */
1337d148c2a2SMatt Joras 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
13385191a3aeSKristof Provost 
13395191a3aeSKristof Provost 	CURVNET_RESTORE();
1340d148c2a2SMatt Joras }
1341d148c2a2SMatt Joras 
13422cc2df49SGarrett Wollman static int
13437983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
13442cc2df49SGarrett Wollman {
1345a68cc388SGleb Smirnoff 	struct epoch_tracker et;
134675ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
13471cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
134875ee267cSGleb Smirnoff 	int error = 0;
13492cc2df49SGarrett Wollman 
1350b1828acfSGleb Smirnoff 	/*
1351b1828acfSGleb Smirnoff 	 * We can handle non-ethernet hardware types as long as
1352b1828acfSGleb Smirnoff 	 * they handle the tagging and headers themselves.
1353b1828acfSGleb Smirnoff 	 */
1354e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1355e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
135615a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
135764a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
135864a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
1359b1828acfSGleb Smirnoff 	/*
1360b1828acfSGleb Smirnoff 	 * Don't let the caller set up a VLAN VID with
1361b1828acfSGleb Smirnoff 	 * anything except VLID bits.
1362b1828acfSGleb Smirnoff 	 * VID numbers 0x0 and 0xFFF are reserved.
1363b1828acfSGleb Smirnoff 	 */
1364b1828acfSGleb Smirnoff 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1365b1828acfSGleb Smirnoff 		return (EINVAL);
136675ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
136715a66c21SBruce M Simpson 		return (EBUSY);
13682cc2df49SGarrett Wollman 
1369d148c2a2SMatt Joras 	VLAN_XLOCK();
137075ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
137175ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
137275ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
137375ee267cSGleb Smirnoff 		vlan_inithash(trunk);
137475ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
1375d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
137675ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
137775ee267cSGleb Smirnoff 		trunk->parent = p;
13789bcf3ae4SAlexander Motin 		if_ref(trunk->parent);
1379b08d611dSMatt Macy 		TRUNK_WUNLOCK(trunk);
138075ee267cSGleb Smirnoff 	} else {
138175ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
138275ee267cSGleb Smirnoff 	}
138375ee267cSGleb Smirnoff 
13847983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
13852ccbbd06SMarcelo Araujo 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
13862ccbbd06SMarcelo Araujo 	vlan_tag_recalculate(ifv);
138775ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
138875ee267cSGleb Smirnoff 	if (error)
138975ee267cSGleb Smirnoff 		goto done;
139073f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1391a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1392a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
13931cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1394d89baa5aSAlexander Motin 	ifv->ifv_capenable = -1;
1395a3814acfSSam Leffler 
1396a3814acfSSam Leffler 	/*
1397a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1398a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1399656acce4SYaroslav Tykhiy 	 * use it.
1400a3814acfSSam Leffler 	 */
1401656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1402656acce4SYaroslav Tykhiy 		/*
1403656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1404656acce4SYaroslav Tykhiy 		 * handle extended frames.
1405656acce4SYaroslav Tykhiy 		 */
1406a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1407656acce4SYaroslav Tykhiy 	} else {
1408a3814acfSSam Leffler 		/*
1409a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1410a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1411a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1412a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1413a3814acfSSam Leffler 		 * which might still be useful.
1414a3814acfSSam Leffler 		 */
1415a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1416a3814acfSSam Leffler 	}
1417a3814acfSSam Leffler 
141875ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
14191cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1420e4cd31ddSJeff Roberson 	/*
1421e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1422e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1423e4cd31ddSJeff Roberson 	 * interfaces to also work.
1424e4cd31ddSJeff Roberson 	 */
14251cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
142675ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1427e4cd31ddSJeff Roberson 	ifp->if_output = p->if_output;
1428e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1429e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1430e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1431e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
143232a52e9eSNavdeep Parhar 	ifp->if_pcp = ifv->ifv_pcp;
1433e4cd31ddSJeff Roberson 
14342cc2df49SGarrett Wollman 	/*
143524993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
143624993214SYaroslav Tykhiy 	 * Other flags are none of our business.
14372cc2df49SGarrett Wollman 	 */
143864a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
14391cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
14401cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
14411cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
14421cf236fbSYaroslav Tykhiy 
14431cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
14442cc2df49SGarrett Wollman 
1445a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
144675ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1447a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1448a3814acfSSam Leffler 
1449a3814acfSSam Leffler 	/*
1450e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
14512cc2df49SGarrett Wollman 	 * physical interface's.
14522cc2df49SGarrett Wollman 	 */
1453e4cd31ddSJeff Roberson 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1454e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1455e4cd31ddSJeff Roberson 	    p->if_addrlen;
14561b2a4f7aSBill Fenner 
1457d148c2a2SMatt Joras 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
14582ada9747SYaroslav Tykhiy 
14592ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
14602ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1461d148c2a2SMatt Joras 
1462d148c2a2SMatt Joras 	/* Update flags on the parent, if necessary. */
1463d148c2a2SMatt Joras 	vlan_setflags(ifp, 1);
1464b08d611dSMatt Macy 
1465d148c2a2SMatt Joras 	/*
1466b08d611dSMatt Macy 	 * Configure multicast addresses that may already be
1467b08d611dSMatt Macy 	 * joined on the vlan device.
1468d148c2a2SMatt Joras 	 */
1469b08d611dSMatt Macy 	(void)vlan_setmulti(ifp);
1470b08d611dSMatt Macy 
1471b08d611dSMatt Macy done:
1472c725524cSJack F Vogel 	if (error == 0)
14737983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1474d148c2a2SMatt Joras 	VLAN_XUNLOCK();
147575ee267cSGleb Smirnoff 
147675ee267cSGleb Smirnoff 	return (error);
14772cc2df49SGarrett Wollman }
14782cc2df49SGarrett Wollman 
14796f359e28SJohn Baldwin static void
1480f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1481f731f104SBill Paul {
14825cb8c31aSYaroslav Tykhiy 
1483d148c2a2SMatt Joras 	VLAN_XLOCK();
148428cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
1485d148c2a2SMatt Joras 	VLAN_XUNLOCK();
14865cb8c31aSYaroslav Tykhiy }
14875cb8c31aSYaroslav Tykhiy 
14886f359e28SJohn Baldwin static void
148928cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
14905cb8c31aSYaroslav Tykhiy {
149175ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1492f731f104SBill Paul 	struct vlan_mc_entry *mc;
1493f731f104SBill Paul 	struct ifvlan *ifv;
1494c725524cSJack F Vogel 	struct ifnet  *parent;
149528cc4d37SJohn Baldwin 	int error;
1496f731f104SBill Paul 
1497d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
14984faedfe8SSam Leffler 
1499f731f104SBill Paul 	ifv = ifp->if_softc;
150075ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
150122893351SJack F Vogel 	parent = NULL;
1502f731f104SBill Paul 
150322893351SJack F Vogel 	if (trunk != NULL) {
150422893351SJack F Vogel 		parent = trunk->parent;
15051b2a4f7aSBill Fenner 
1506f731f104SBill Paul 		/*
1507f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1508f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
15091b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1510f731f104SBill Paul 		 */
1511b08d611dSMatt Macy 		while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
15126f359e28SJohn Baldwin 			/*
151328cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1514b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
151528cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
151628cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
151728cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
151828cc4d37SJohn Baldwin 			 * succeed.
15196f359e28SJohn Baldwin 			 */
152028cc4d37SJohn Baldwin 			if (!departing) {
152128cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1522e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
152328cc4d37SJohn Baldwin 				if (error)
152428cc4d37SJohn Baldwin 					if_printf(ifp,
152528cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
152628cc4d37SJohn Baldwin 					    error);
152728cc4d37SJohn Baldwin 			}
1528b08d611dSMatt Macy 			CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1529c32a9d66SHans Petter Selasky 			epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
1530f731f104SBill Paul 		}
1531a3814acfSSam Leffler 
15321cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1533d148c2a2SMatt Joras 
153475ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
153575ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
153675ee267cSGleb Smirnoff 
153775ee267cSGleb Smirnoff 		/*
153875ee267cSGleb Smirnoff 		 * Check if we were the last.
153975ee267cSGleb Smirnoff 		 */
154075ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
15412d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
1542b08d611dSMatt Macy 			NET_EPOCH_WAIT();
154375ee267cSGleb Smirnoff 			trunk_destroy(trunk);
1544d148c2a2SMatt Joras 		}
15451b2a4f7aSBill Fenner 	}
1546f731f104SBill Paul 
1547f731f104SBill Paul 	/* Disconnect from parent. */
15481cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
15491cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
15505cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
15515cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
15525cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1553f731f104SBill Paul 
155422893351SJack F Vogel 	/*
155522893351SJack F Vogel 	 * Only dispatch an event if vlan was
155622893351SJack F Vogel 	 * attached, otherwise there is nothing
155722893351SJack F Vogel 	 * to cleanup anyway.
155822893351SJack F Vogel 	 */
155922893351SJack F Vogel 	if (parent != NULL)
15607983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1561f731f104SBill Paul }
1562f731f104SBill Paul 
15631cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1564f731f104SBill Paul static int
15651cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
15661cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1567a3814acfSSam Leffler {
15681cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
15691cf236fbSYaroslav Tykhiy 	int error;
1570a3814acfSSam Leffler 
1571d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1572a3814acfSSam Leffler 
15731cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
15741cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
15751cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
15761cf236fbSYaroslav Tykhiy 
15771cf236fbSYaroslav Tykhiy 	/*
15781cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
15791cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
15801cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
15811cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
15821cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
15831cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
15841cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
15851cf236fbSYaroslav Tykhiy 	 */
15861cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
158775ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
15881cf236fbSYaroslav Tykhiy 		if (error)
1589a3814acfSSam Leffler 			return (error);
15901cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
15911cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
15921cf236fbSYaroslav Tykhiy 	}
15931cf236fbSYaroslav Tykhiy 	return (0);
15941cf236fbSYaroslav Tykhiy }
15951cf236fbSYaroslav Tykhiy 
15961cf236fbSYaroslav Tykhiy /*
15971cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
15981cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
15991cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
16001cf236fbSYaroslav Tykhiy  */
16011cf236fbSYaroslav Tykhiy static int
16021cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
16031cf236fbSYaroslav Tykhiy {
16041cf236fbSYaroslav Tykhiy 	int error, i;
16051cf236fbSYaroslav Tykhiy 
16061cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
16071cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
16081cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
16091cf236fbSYaroslav Tykhiy 		if (error)
16101cf236fbSYaroslav Tykhiy 			return (error);
16111cf236fbSYaroslav Tykhiy 	}
16121cf236fbSYaroslav Tykhiy 	return (0);
1613a3814acfSSam Leffler }
1614a3814acfSSam Leffler 
1615127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1616127d7b2dSAndre Oppermann static void
1617a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1618127d7b2dSAndre Oppermann {
1619a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1620d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1621127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
1622127d7b2dSAndre Oppermann 
1623d148c2a2SMatt Joras 	/* Called from a taskqueue_swi task, so we cannot sleep. */
1624a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1625d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1626d148c2a2SMatt Joras 	if (trunk == NULL) {
1627a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1628d148c2a2SMatt Joras 		return;
1629d148c2a2SMatt Joras 	}
1630d148c2a2SMatt Joras 
1631d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1632d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
1633aad0be7aSGleb Smirnoff 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1634fc74a9f9SBrooks Davis 		if_link_state_change(ifv->ifv_ifp,
163575ee267cSGleb Smirnoff 		    trunk->parent->if_link_state);
1636127d7b2dSAndre Oppermann 	}
1637d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1638a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
163975ee267cSGleb Smirnoff }
164075ee267cSGleb Smirnoff 
164175ee267cSGleb Smirnoff static void
164275ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
164375ee267cSGleb Smirnoff {
1644d148c2a2SMatt Joras 	struct ifnet *p;
1645d148c2a2SMatt Joras 	struct ifnet *ifp;
16469fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
1647d89baa5aSAlexander Motin 	int cap = 0, ena = 0, mena;
1648d89baa5aSAlexander Motin 	u_long hwa = 0;
164975ee267cSGleb Smirnoff 
1650d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1651a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
1652d148c2a2SMatt Joras 	p = PARENT(ifv);
1653d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
165475ee267cSGleb Smirnoff 
1655d89baa5aSAlexander Motin 	/* Mask parent interface enabled capabilities disabled by user. */
1656d89baa5aSAlexander Motin 	mena = p->if_capenable & ifv->ifv_capenable;
1657d89baa5aSAlexander Motin 
165875ee267cSGleb Smirnoff 	/*
165975ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
166075ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
166175ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
166275ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
166375ee267cSGleb Smirnoff 	 */
166475ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1665d89baa5aSAlexander Motin 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
166675ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
166775ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1668d89baa5aSAlexander Motin 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1669d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM)
1670d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1671d89baa5aSAlexander Motin 			    CSUM_UDP | CSUM_SCTP);
1672d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM_IPV6)
1673d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1674d89baa5aSAlexander Motin 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
167575ee267cSGleb Smirnoff 	}
1676d89baa5aSAlexander Motin 
16779b76d9cbSPyun YongHyeon 	/*
16789b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
16799b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
16809b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
16819b76d9cbSPyun YongHyeon 	 */
16829fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
16839fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
16849fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
16859b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1686d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TSO;
16879b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1688d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TSO;
1689d89baa5aSAlexander Motin 		if (ena & IFCAP_TSO)
1690d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & CSUM_TSO;
16919b76d9cbSPyun YongHyeon 	}
169209fe6320SNavdeep Parhar 
169309fe6320SNavdeep Parhar 	/*
169459150e91SAlexander Motin 	 * If the parent interface can do LRO and checksum offloading on
169559150e91SAlexander Motin 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
169659150e91SAlexander Motin 	 * cost nothing, while false negative may lead to some confusions.
169759150e91SAlexander Motin 	 */
169859150e91SAlexander Motin 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
169959150e91SAlexander Motin 		cap |= p->if_capabilities & IFCAP_LRO;
170059150e91SAlexander Motin 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
170159150e91SAlexander Motin 		ena |= p->if_capenable & IFCAP_LRO;
170259150e91SAlexander Motin 
170359150e91SAlexander Motin 	/*
170409fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
170509fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
170609fe6320SNavdeep Parhar 	 *
170709fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
170809fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
170909fe6320SNavdeep Parhar 	 * with its own bit.
171009fe6320SNavdeep Parhar 	 */
171109fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
171209fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1713d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TOE;
171409fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
171509fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
1716d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TOE;
171709fe6320SNavdeep Parhar 	}
1718f3e7afe2SHans Petter Selasky 
1719d89baa5aSAlexander Motin 	/*
1720d89baa5aSAlexander Motin 	 * If the parent interface supports dynamic link state, so does the
1721d89baa5aSAlexander Motin 	 * VLAN interface.
1722d89baa5aSAlexander Motin 	 */
1723d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1724d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_LINKSTATE);
1725d89baa5aSAlexander Motin 
1726f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1727f3e7afe2SHans Petter Selasky 	/*
1728f3e7afe2SHans Petter Selasky 	 * If the parent interface supports ratelimiting, so does the
1729f3e7afe2SHans Petter Selasky 	 * VLAN interface.
1730f3e7afe2SHans Petter Selasky 	 */
1731d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1732d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_TXRTLMT);
1733f3e7afe2SHans Petter Selasky #endif
1734d89baa5aSAlexander Motin 
173566d0c056SJohn Baldwin 	/*
173666d0c056SJohn Baldwin 	 * If the parent interface supports unmapped mbufs, so does
173766d0c056SJohn Baldwin 	 * the VLAN interface.  Note that this should be fine even for
173866d0c056SJohn Baldwin 	 * interfaces that don't support hardware tagging as headers
173966d0c056SJohn Baldwin 	 * are prepended in normal mbufs to unmapped mbufs holding
174066d0c056SJohn Baldwin 	 * payload data.
174166d0c056SJohn Baldwin 	 */
174266d0c056SJohn Baldwin 	cap |= (p->if_capabilities & IFCAP_NOMAP);
174366d0c056SJohn Baldwin 	ena |= (mena & IFCAP_NOMAP);
174466d0c056SJohn Baldwin 
1745*b2e60773SJohn Baldwin 	/*
1746*b2e60773SJohn Baldwin 	 * If the parent interface can offload encryption and segmentation
1747*b2e60773SJohn Baldwin 	 * of TLS records over TCP, propagate it's capability to the VLAN
1748*b2e60773SJohn Baldwin 	 * interface.
1749*b2e60773SJohn Baldwin 	 *
1750*b2e60773SJohn Baldwin 	 * All TLS drivers in the tree today can deal with VLANs.  If
1751*b2e60773SJohn Baldwin 	 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1752*b2e60773SJohn Baldwin 	 * defined.
1753*b2e60773SJohn Baldwin 	 */
1754*b2e60773SJohn Baldwin 	if (p->if_capabilities & IFCAP_TXTLS)
1755*b2e60773SJohn Baldwin 		cap |= p->if_capabilities & IFCAP_TXTLS;
1756*b2e60773SJohn Baldwin 	if (p->if_capenable & IFCAP_TXTLS)
1757*b2e60773SJohn Baldwin 		ena |= mena & IFCAP_TXTLS;
1758*b2e60773SJohn Baldwin 
1759d89baa5aSAlexander Motin 	ifp->if_capabilities = cap;
1760d89baa5aSAlexander Motin 	ifp->if_capenable = ena;
1761d89baa5aSAlexander Motin 	ifp->if_hwassist = hwa;
176275ee267cSGleb Smirnoff }
176375ee267cSGleb Smirnoff 
176475ee267cSGleb Smirnoff static void
176575ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
176675ee267cSGleb Smirnoff {
1767a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1768d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
176975ee267cSGleb Smirnoff 	struct ifvlan *ifv;
177075ee267cSGleb Smirnoff 
1771d148c2a2SMatt Joras 	VLAN_SLOCK();
1772d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1773d148c2a2SMatt Joras 	if (trunk == NULL) {
1774d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1775d148c2a2SMatt Joras 		return;
1776d148c2a2SMatt Joras 	}
1777a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1778d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
177975ee267cSGleb Smirnoff 		vlan_capabilities(ifv);
178075ee267cSGleb Smirnoff 	}
1781a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1782d148c2a2SMatt Joras 	VLAN_SUNLOCK();
1783127d7b2dSAndre Oppermann }
1784127d7b2dSAndre Oppermann 
1785a3814acfSSam Leffler static int
1786cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
17872cc2df49SGarrett Wollman {
17882cc2df49SGarrett Wollman 	struct ifnet *p;
17892cc2df49SGarrett Wollman 	struct ifreq *ifr;
1790e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
17912cc2df49SGarrett Wollman 	struct ifvlan *ifv;
17922d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
17932cc2df49SGarrett Wollman 	struct vlanreq vlr;
17942cc2df49SGarrett Wollman 	int error = 0;
17952cc2df49SGarrett Wollman 
17962cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1797e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
17982cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
17992cc2df49SGarrett Wollman 
18002cc2df49SGarrett Wollman 	switch (cmd) {
1801e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1802e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1803e4cd31ddSJeff Roberson #ifdef INET
1804e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1805e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1806e4cd31ddSJeff Roberson #endif
1807e4cd31ddSJeff Roberson 		break;
1808e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
180938d958a6SBrooks Davis 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
181038d958a6SBrooks Davis 		    ifp->if_addrlen);
1811e4cd31ddSJeff Roberson 		break;
1812b3cca108SBill Fenner 	case SIOCGIFMEDIA:
1813d148c2a2SMatt Joras 		VLAN_SLOCK();
181475ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1815d8564efdSEd Maste 			p = PARENT(ifv);
18169bcf3ae4SAlexander Motin 			if_ref(p);
1817d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
18189bcf3ae4SAlexander Motin 			if_rele(p);
1819b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1820b3cca108SBill Fenner 			if (error == 0) {
1821b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1822b3cca108SBill Fenner 
1823b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1824b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1825b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1826b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1827b3cca108SBill Fenner 						ifmr->ifm_ulist,
1828b3cca108SBill Fenner 						sizeof(int));
1829b3cca108SBill Fenner 				}
1830b3cca108SBill Fenner 			}
18314faedfe8SSam Leffler 		} else {
1832b3cca108SBill Fenner 			error = EINVAL;
18334faedfe8SSam Leffler 		}
1834d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1835b3cca108SBill Fenner 		break;
1836b3cca108SBill Fenner 
1837b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1838b3cca108SBill Fenner 		error = EINVAL;
1839b3cca108SBill Fenner 		break;
1840b3cca108SBill Fenner 
18412cc2df49SGarrett Wollman 	case SIOCSIFMTU:
18422cc2df49SGarrett Wollman 		/*
18432cc2df49SGarrett Wollman 		 * Set the interface MTU.
18442cc2df49SGarrett Wollman 		 */
1845d148c2a2SMatt Joras 		VLAN_SLOCK();
1846d148c2a2SMatt Joras 		trunk = TRUNK(ifv);
1847d148c2a2SMatt Joras 		if (trunk != NULL) {
1848d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1849a3814acfSSam Leffler 			if (ifr->ifr_mtu >
185075ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1851a3814acfSSam Leffler 			    ifr->ifr_mtu <
1852a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
18532cc2df49SGarrett Wollman 				error = EINVAL;
1854a3814acfSSam Leffler 			else
18552cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1856d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
1857a3814acfSSam Leffler 		} else
1858a3814acfSSam Leffler 			error = EINVAL;
1859d148c2a2SMatt Joras 		VLAN_SUNLOCK();
18602cc2df49SGarrett Wollman 		break;
18612cc2df49SGarrett Wollman 
18622cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1863ccf7ba97SMarko Zec #ifdef VIMAGE
186415f6780eSRobert Watson 		/*
186515f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
186615f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
186715f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
186815f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
18695a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
187015f6780eSRobert Watson 		 */
1871ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1872ccf7ba97SMarko Zec 			error = EPERM;
1873ccf7ba97SMarko Zec 			break;
1874ccf7ba97SMarko Zec 		}
1875ccf7ba97SMarko Zec #endif
1876541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
18772cc2df49SGarrett Wollman 		if (error)
18782cc2df49SGarrett Wollman 			break;
18792cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1880f731f104SBill Paul 			vlan_unconfig(ifp);
18812cc2df49SGarrett Wollman 			break;
18822cc2df49SGarrett Wollman 		}
18839bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
18841bdc73d3SEd Maste 		if (p == NULL) {
18852cc2df49SGarrett Wollman 			error = ENOENT;
18862cc2df49SGarrett Wollman 			break;
18872cc2df49SGarrett Wollman 		}
188875ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
18899bcf3ae4SAlexander Motin 		if_rele(p);
18902cc2df49SGarrett Wollman 		break;
18912cc2df49SGarrett Wollman 
18922cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1893ccf7ba97SMarko Zec #ifdef VIMAGE
1894ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1895ccf7ba97SMarko Zec 			error = EPERM;
1896ccf7ba97SMarko Zec 			break;
1897ccf7ba97SMarko Zec 		}
1898ccf7ba97SMarko Zec #endif
189915a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
1900d148c2a2SMatt Joras 		VLAN_SLOCK();
190175ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
190275ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
19039bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
19047983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
19052cc2df49SGarrett Wollman 		}
1906d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1907541d96aaSBrooks Davis 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
19082cc2df49SGarrett Wollman 		break;
19092cc2df49SGarrett Wollman 
19102cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
19112cc2df49SGarrett Wollman 		/*
19121cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
19131cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
19142cc2df49SGarrett Wollman 		 */
1915d148c2a2SMatt Joras 		VLAN_XLOCK();
191675ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
19171cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
1918d148c2a2SMatt Joras 		VLAN_XUNLOCK();
19192cc2df49SGarrett Wollman 		break;
1920a3814acfSSam Leffler 
1921f731f104SBill Paul 	case SIOCADDMULTI:
1922f731f104SBill Paul 	case SIOCDELMULTI:
192375ee267cSGleb Smirnoff 		/*
192475ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
192575ee267cSGleb Smirnoff 		 * when we do.
1926d148c2a2SMatt Joras 		 *
1927d148c2a2SMatt Joras 		 * XXX We need the rmlock here to avoid sleeping while
1928d148c2a2SMatt Joras 		 * holding in6_multi_mtx.
192975ee267cSGleb Smirnoff 		 */
1930b08d611dSMatt Macy 		VLAN_XLOCK();
19312d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
1932b08d611dSMatt Macy 		if (trunk != NULL)
1933f731f104SBill Paul 			error = vlan_setmulti(ifp);
1934b08d611dSMatt Macy 		VLAN_XUNLOCK();
193575ee267cSGleb Smirnoff 
1936b08d611dSMatt Macy 		break;
19372ccbbd06SMarcelo Araujo 	case SIOCGVLANPCP:
19382ccbbd06SMarcelo Araujo #ifdef VIMAGE
19392ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19402ccbbd06SMarcelo Araujo 			error = EPERM;
19412ccbbd06SMarcelo Araujo 			break;
19422ccbbd06SMarcelo Araujo 		}
19432ccbbd06SMarcelo Araujo #endif
19442ccbbd06SMarcelo Araujo 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
19452ccbbd06SMarcelo Araujo 		break;
19462ccbbd06SMarcelo Araujo 
19472ccbbd06SMarcelo Araujo 	case SIOCSVLANPCP:
19482ccbbd06SMarcelo Araujo #ifdef VIMAGE
19492ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19502ccbbd06SMarcelo Araujo 			error = EPERM;
19512ccbbd06SMarcelo Araujo 			break;
19522ccbbd06SMarcelo Araujo 		}
19532ccbbd06SMarcelo Araujo #endif
19542ccbbd06SMarcelo Araujo 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
19552ccbbd06SMarcelo Araujo 		if (error)
19562ccbbd06SMarcelo Araujo 			break;
19572ccbbd06SMarcelo Araujo 		if (ifr->ifr_vlan_pcp > 7) {
19582ccbbd06SMarcelo Araujo 			error = EINVAL;
19592ccbbd06SMarcelo Araujo 			break;
19602ccbbd06SMarcelo Araujo 		}
19612ccbbd06SMarcelo Araujo 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
196232a52e9eSNavdeep Parhar 		ifp->if_pcp = ifv->ifv_pcp;
19632ccbbd06SMarcelo Araujo 		vlan_tag_recalculate(ifv);
19644a381a9eSHans Petter Selasky 		/* broadcast event about PCP change */
19654a381a9eSHans Petter Selasky 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
19662ccbbd06SMarcelo Araujo 		break;
19672ccbbd06SMarcelo Araujo 
1968d89baa5aSAlexander Motin 	case SIOCSIFCAP:
1969d148c2a2SMatt Joras 		VLAN_SLOCK();
1970d89baa5aSAlexander Motin 		ifv->ifv_capenable = ifr->ifr_reqcap;
1971d89baa5aSAlexander Motin 		trunk = TRUNK(ifv);
1972d89baa5aSAlexander Motin 		if (trunk != NULL) {
1973a68cc388SGleb Smirnoff 			struct epoch_tracker et;
1974a68cc388SGleb Smirnoff 
1975a68cc388SGleb Smirnoff 			NET_EPOCH_ENTER(et);
1976d89baa5aSAlexander Motin 			vlan_capabilities(ifv);
1977a68cc388SGleb Smirnoff 			NET_EPOCH_EXIT(et);
1978d89baa5aSAlexander Motin 		}
1979d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1980d89baa5aSAlexander Motin 		break;
1981d89baa5aSAlexander Motin 
19822cc2df49SGarrett Wollman 	default:
1983e4cd31ddSJeff Roberson 		error = EINVAL;
1984e4cd31ddSJeff Roberson 		break;
19852cc2df49SGarrett Wollman 	}
198615a66c21SBruce M Simpson 
198715a66c21SBruce M Simpson 	return (error);
19882cc2df49SGarrett Wollman }
1989f3e7afe2SHans Petter Selasky 
1990*b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1991f3e7afe2SHans Petter Selasky static int
1992f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp,
1993f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *params,
1994f3e7afe2SHans Petter Selasky     struct m_snd_tag **ppmt)
1995f3e7afe2SHans Petter Selasky {
1996fb3bc596SJohn Baldwin 	struct epoch_tracker et;
1997fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
1998fb3bc596SJohn Baldwin 	struct ifvlan *ifv;
1999fb3bc596SJohn Baldwin 	struct ifnet *parent;
2000fb3bc596SJohn Baldwin 	int error;
2001f3e7afe2SHans Petter Selasky 
2002fb3bc596SJohn Baldwin 	NET_EPOCH_ENTER(et);
2003fb3bc596SJohn Baldwin 	ifv = ifp->if_softc;
2004fb3bc596SJohn Baldwin 	if (ifv->ifv_trunk != NULL)
2005fb3bc596SJohn Baldwin 		parent = PARENT(ifv);
2006fb3bc596SJohn Baldwin 	else
2007fb3bc596SJohn Baldwin 		parent = NULL;
2008fb3bc596SJohn Baldwin 	if (parent == NULL || parent->if_snd_tag_alloc == NULL) {
2009fb3bc596SJohn Baldwin 		NET_EPOCH_EXIT(et);
2010f3e7afe2SHans Petter Selasky 		return (EOPNOTSUPP);
2011fb3bc596SJohn Baldwin 	}
2012fb3bc596SJohn Baldwin 	if_ref(parent);
2013fb3bc596SJohn Baldwin 	NET_EPOCH_EXIT(et);
2014fb3bc596SJohn Baldwin 
2015fb3bc596SJohn Baldwin 	vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2016fb3bc596SJohn Baldwin 	if (vst == NULL) {
2017fb3bc596SJohn Baldwin 		if_rele(parent);
2018fb3bc596SJohn Baldwin 		return (ENOMEM);
2019fb3bc596SJohn Baldwin 	}
2020fb3bc596SJohn Baldwin 
2021fb3bc596SJohn Baldwin 	error = parent->if_snd_tag_alloc(parent, params, &vst->tag);
2022fb3bc596SJohn Baldwin 	if_rele(parent);
2023fb3bc596SJohn Baldwin 	if (error) {
2024fb3bc596SJohn Baldwin 		free(vst, M_VLAN);
2025fb3bc596SJohn Baldwin 		return (error);
2026fb3bc596SJohn Baldwin 	}
2027fb3bc596SJohn Baldwin 
2028fb3bc596SJohn Baldwin 	m_snd_tag_init(&vst->com, ifp);
2029fb3bc596SJohn Baldwin 
2030fb3bc596SJohn Baldwin 	*ppmt = &vst->com;
2031fb3bc596SJohn Baldwin 	return (0);
2032fb3bc596SJohn Baldwin }
2033fb3bc596SJohn Baldwin 
2034fb3bc596SJohn Baldwin static int
2035fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst,
2036fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *params)
2037fb3bc596SJohn Baldwin {
2038fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2039fb3bc596SJohn Baldwin 
2040fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2041fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params));
2042fb3bc596SJohn Baldwin }
2043fb3bc596SJohn Baldwin 
2044fb3bc596SJohn Baldwin static int
2045fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst,
2046fb3bc596SJohn Baldwin     union if_snd_tag_query_params *params)
2047fb3bc596SJohn Baldwin {
2048fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2049fb3bc596SJohn Baldwin 
2050fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2051fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_query(vst->tag, params));
2052f3e7afe2SHans Petter Selasky }
2053fa91f845SRandall Stewart 
2054fa91f845SRandall Stewart static void
2055fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst)
2056fa91f845SRandall Stewart {
2057fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2058fb3bc596SJohn Baldwin 
2059fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2060fb3bc596SJohn Baldwin 	m_snd_tag_rele(vst->tag);
2061fb3bc596SJohn Baldwin 	free(vst, M_VLAN);
2062fa91f845SRandall Stewart }
2063f3e7afe2SHans Petter Selasky #endif
2064