xref: /freebsd/sys/net/if_vlan.c (revision 6dcec895d97f4a68b257e3683d638636602a3261)
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"
49b2e60773SJohn 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 
107b2e60773SJohn 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 /*
251b08d611dSMatt Macy  * We also have a per-trunk mutex that should be acquired when changing
252b08d611dSMatt Macy  * its state.
253d148c2a2SMatt Joras  */
254b08d611dSMatt Macy #define	TRUNK_LOCK_INIT(trunk)		mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
255b08d611dSMatt Macy #define	TRUNK_LOCK_DESTROY(trunk)	mtx_destroy(&(trunk)->lock)
256b08d611dSMatt Macy #define	TRUNK_WLOCK(trunk)		mtx_lock(&(trunk)->lock)
257b08d611dSMatt Macy #define	TRUNK_WUNLOCK(trunk)		mtx_unlock(&(trunk)->lock)
258b08d611dSMatt Macy #define	TRUNK_WLOCK_ASSERT(trunk)	mtx_assert(&(trunk)->lock, MA_OWNED);
25975ee267cSGleb Smirnoff 
260d148c2a2SMatt Joras /*
261d148c2a2SMatt Joras  * The VLAN_ARRAY substitutes the dynamic hash with a static array
262d148c2a2SMatt Joras  * with 4096 entries. In theory this can give a boost in processing,
263d148c2a2SMatt Joras  * however in practice it does not. Probably this is because the array
264d148c2a2SMatt Joras  * is too big to fit into CPU cache.
265d148c2a2SMatt Joras  */
26675ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
26775ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
26875ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
26975ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
27075ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
27175ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
27275ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
2737983103aSRobert Watson 	uint16_t vid);
27475ee267cSGleb Smirnoff #endif
27575ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
2764faedfe8SSam Leffler 
277114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
278a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
279cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
280b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
281f3e7afe2SHans Petter Selasky static	int vlan_snd_tag_alloc(struct ifnet *,
282f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *, struct m_snd_tag **);
283fb3bc596SJohn Baldwin static	int vlan_snd_tag_modify(struct m_snd_tag *,
284fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *);
285fb3bc596SJohn Baldwin static	int vlan_snd_tag_query(struct m_snd_tag *,
286fb3bc596SJohn Baldwin     union if_snd_tag_query_params *);
287fa91f845SRandall Stewart static	void vlan_snd_tag_free(struct m_snd_tag *);
288f3e7afe2SHans Petter Selasky #endif
289d9b1d615SJohn Baldwin static	void vlan_qflush(struct ifnet *ifp);
2901cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
2911cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
2921cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
293f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
294d9b1d615SJohn Baldwin static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
29516cf6bdbSMatt Joras static	int vlan_output(struct ifnet *ifp, struct mbuf *m,
29616cf6bdbSMatt Joras     const struct sockaddr *dst, struct route *ro);
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 {
706e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
707e4cd31ddSJeff Roberson 
708b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
709b8a6e03fSGleb Smirnoff 
710e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
711e4cd31ddSJeff Roberson 		return (NULL);
712d148c2a2SMatt Joras 
713e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
714e4cd31ddSJeff Roberson 	ifp = NULL;
715e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
716e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
717e4cd31ddSJeff Roberson 	return (ifp);
718e4cd31ddSJeff Roberson }
719e4cd31ddSJeff Roberson 
720e4cd31ddSJeff Roberson /*
7217983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
7227983103aSRobert Watson  * components such as Infiniband.
7237983103aSRobert Watson  *
7247983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
7257983103aSRobert Watson  * vlan_vid().
726e4cd31ddSJeff Roberson  */
727e4cd31ddSJeff Roberson static int
7287983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
729e4cd31ddSJeff Roberson {
730e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
731e4cd31ddSJeff Roberson 
732e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
733e4cd31ddSJeff Roberson 		return (EINVAL);
734e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
7357983103aSRobert Watson 	*vidp = ifv->ifv_vid;
736e4cd31ddSJeff Roberson 	return (0);
737e4cd31ddSJeff Roberson }
738e4cd31ddSJeff Roberson 
73932d2623aSNavdeep Parhar static int
74032d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
74132d2623aSNavdeep Parhar {
74232d2623aSNavdeep Parhar 	struct ifvlan *ifv;
74332d2623aSNavdeep Parhar 
74432d2623aSNavdeep Parhar 	if (ifp->if_type != IFT_L2VLAN)
74532d2623aSNavdeep Parhar 		return (EINVAL);
74632d2623aSNavdeep Parhar 	ifv = ifp->if_softc;
74732d2623aSNavdeep Parhar 	*pcpp = ifv->ifv_pcp;
74832d2623aSNavdeep Parhar 	return (0);
74932d2623aSNavdeep Parhar }
75032d2623aSNavdeep Parhar 
751e4cd31ddSJeff Roberson /*
752e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
753e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
754e4cd31ddSJeff Roberson  */
755e4cd31ddSJeff Roberson static void *
756e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
757e4cd31ddSJeff Roberson {
758e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
759e4cd31ddSJeff Roberson 
760e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
761e4cd31ddSJeff Roberson 		return (NULL);
762e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
763e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
764e4cd31ddSJeff Roberson }
765e4cd31ddSJeff Roberson 
766e4cd31ddSJeff Roberson /*
767e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
768e4cd31ddSJeff Roberson  * private per-instance data in.
769e4cd31ddSJeff Roberson  */
770e4cd31ddSJeff Roberson static int
771e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
772e4cd31ddSJeff Roberson {
773e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
774e4cd31ddSJeff Roberson 
775e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
776e4cd31ddSJeff Roberson 		return (EINVAL);
777e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
778e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
779e4cd31ddSJeff Roberson 	return (0);
780e4cd31ddSJeff Roberson }
781e4cd31ddSJeff Roberson 
782e4cd31ddSJeff Roberson /*
7837983103aSRobert Watson  * Return the vlan device present at the specific VID.
784e4cd31ddSJeff Roberson  */
785e4cd31ddSJeff Roberson static struct ifnet *
7867983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
787e4cd31ddSJeff Roberson {
788e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
789e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
790e4cd31ddSJeff Roberson 
791b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
792b8a6e03fSGleb Smirnoff 
793e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
794b8a6e03fSGleb Smirnoff 	if (trunk == NULL)
795e4cd31ddSJeff Roberson 		return (NULL);
796e4cd31ddSJeff Roberson 	ifp = NULL;
7977983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
798e4cd31ddSJeff Roberson 	if (ifv)
799e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
800e4cd31ddSJeff Roberson 	return (ifp);
801e4cd31ddSJeff Roberson }
802e4cd31ddSJeff Roberson 
803e4cd31ddSJeff Roberson /*
8042ccbbd06SMarcelo Araujo  * Recalculate the cached VLAN tag exposed via the MIB.
8052ccbbd06SMarcelo Araujo  */
8062ccbbd06SMarcelo Araujo static void
8072ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv)
8082ccbbd06SMarcelo Araujo {
8092ccbbd06SMarcelo Araujo 
8102ccbbd06SMarcelo Araujo        ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
8112ccbbd06SMarcelo Araujo }
8122ccbbd06SMarcelo Araujo 
8132ccbbd06SMarcelo Araujo /*
814a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
815a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
816a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
817a3814acfSSam Leffler  * set here.  No one else in the system should be aware of this so
818a3814acfSSam Leffler  * we use an explicit reference here.
819a3814acfSSam Leffler  */
820a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
821a3814acfSSam Leffler 
822984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
823a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
824127d7b2dSAndre Oppermann 
8252b120974SPeter Wemm static int
8262b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
8272b120974SPeter Wemm {
8289d4fe4b2SBrooks Davis 
8292b120974SPeter Wemm 	switch (type) {
8302b120974SPeter Wemm 	case MOD_LOAD:
8315cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
8325cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
8335cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
8345cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
835ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
836ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
837ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
838ea4ca115SAndrew Thompson 			return (ENOMEM);
839d148c2a2SMatt Joras 		VLAN_LOCKING_INIT();
8409d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
841127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
84275ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
843e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
844e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
845e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
846e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
84732d2623aSNavdeep Parhar 		vlan_pcp_p = vlan_pcp;
848e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
849ccf7ba97SMarko Zec #ifndef VIMAGE
85042a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
85142a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
852ccf7ba97SMarko Zec #endif
85325c0f7b3SYaroslav Tykhiy 		if (bootverbose)
85425c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
85525c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
85625c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
85725c0f7b3SYaroslav Tykhiy #else
85825c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
85925c0f7b3SYaroslav Tykhiy #endif
86025c0f7b3SYaroslav Tykhiy 
86125c0f7b3SYaroslav Tykhiy 			       "\n");
8622b120974SPeter Wemm 		break;
8632b120974SPeter Wemm 	case MOD_UNLOAD:
864ccf7ba97SMarko Zec #ifndef VIMAGE
86542a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
866ccf7ba97SMarko Zec #endif
8675cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
868ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
8699d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
870127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
87175ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
872e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
873e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
87409fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
87509fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
876e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
877d148c2a2SMatt Joras 		VLAN_LOCKING_DESTROY();
87825c0f7b3SYaroslav Tykhiy 		if (bootverbose)
87925c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
8809d4fe4b2SBrooks Davis 		break;
8813e019deaSPoul-Henning Kamp 	default:
8823e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
8832b120974SPeter Wemm 	}
88415a66c21SBruce M Simpson 	return (0);
8852b120974SPeter Wemm }
8862b120974SPeter Wemm 
8872b120974SPeter Wemm static moduledata_t vlan_mod = {
8882b120974SPeter Wemm 	"if_vlan",
8892b120974SPeter Wemm 	vlan_modevent,
8909823d527SKevin Lo 	0
8912b120974SPeter Wemm };
8922b120974SPeter Wemm 
8932b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
89411edc477SEd Maste MODULE_VERSION(if_vlan, 3);
8952cc2df49SGarrett Wollman 
896ccf7ba97SMarko Zec #ifdef VIMAGE
897ccf7ba97SMarko Zec static void
898ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
899ccf7ba97SMarko Zec {
900ccf7ba97SMarko Zec 
90142a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
90242a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
903ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
904ccf7ba97SMarko Zec }
905ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
906ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
907ccf7ba97SMarko Zec 
908ccf7ba97SMarko Zec static void
909ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
910ccf7ba97SMarko Zec {
911ccf7ba97SMarko Zec 
91242a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
913ccf7ba97SMarko Zec }
91489856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
915ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
916ccf7ba97SMarko Zec #endif
917ccf7ba97SMarko Zec 
918f941c31aSGleb Smirnoff /*
919f941c31aSGleb Smirnoff  * Check for <etherif>.<vlan> style interface names.
920f941c31aSGleb Smirnoff  */
921f889d2efSBrooks Davis static struct ifnet *
922f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp)
9239d4fe4b2SBrooks Davis {
924f941c31aSGleb Smirnoff 	char ifname[IFNAMSIZ];
925f941c31aSGleb Smirnoff 	char *cp;
926f889d2efSBrooks Davis 	struct ifnet *ifp;
9277983103aSRobert Watson 	int vid;
928f889d2efSBrooks Davis 
929f941c31aSGleb Smirnoff 	strlcpy(ifname, name, IFNAMSIZ);
930f941c31aSGleb Smirnoff 	if ((cp = strchr(ifname, '.')) == NULL)
931f941c31aSGleb Smirnoff 		return (NULL);
932f941c31aSGleb Smirnoff 	*cp = '\0';
9339bcf3ae4SAlexander Motin 	if ((ifp = ifunit_ref(ifname)) == NULL)
934f941c31aSGleb Smirnoff 		return (NULL);
935f941c31aSGleb Smirnoff 	/* Parse VID. */
9369bcf3ae4SAlexander Motin 	if (*++cp == '\0') {
9379bcf3ae4SAlexander Motin 		if_rele(ifp);
938f941c31aSGleb Smirnoff 		return (NULL);
9399bcf3ae4SAlexander Motin 	}
9407983103aSRobert Watson 	vid = 0;
941fb92ad4aSJohn Baldwin 	for(; *cp >= '0' && *cp <= '9'; cp++)
9427983103aSRobert Watson 		vid = (vid * 10) + (*cp - '0');
9439bcf3ae4SAlexander Motin 	if (*cp != '\0') {
9449bcf3ae4SAlexander Motin 		if_rele(ifp);
945f941c31aSGleb Smirnoff 		return (NULL);
9469bcf3ae4SAlexander Motin 	}
9477983103aSRobert Watson 	if (vidp != NULL)
9487983103aSRobert Watson 		*vidp = vid;
949f889d2efSBrooks Davis 
95015a66c21SBruce M Simpson 	return (ifp);
951f889d2efSBrooks Davis }
952f889d2efSBrooks Davis 
953f889d2efSBrooks Davis static int
954f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
955f889d2efSBrooks Davis {
956f889d2efSBrooks Davis 	const char *cp;
957f889d2efSBrooks Davis 
958f941c31aSGleb Smirnoff 	if (vlan_clone_match_ethervid(name, NULL) != NULL)
959f889d2efSBrooks Davis 		return (1);
960f889d2efSBrooks Davis 
96142a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
962f889d2efSBrooks Davis 		return (0);
963f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
964f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
965f889d2efSBrooks Davis 			return (0);
966f889d2efSBrooks Davis 	}
967f889d2efSBrooks Davis 
968f889d2efSBrooks Davis 	return (1);
969f889d2efSBrooks Davis }
970f889d2efSBrooks Davis 
971f889d2efSBrooks Davis static int
9726b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
973f889d2efSBrooks Davis {
974f889d2efSBrooks Davis 	char *dp;
975f889d2efSBrooks Davis 	int wildcard;
976f889d2efSBrooks Davis 	int unit;
977f889d2efSBrooks Davis 	int error;
9787983103aSRobert Watson 	int vid;
9799d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
9809d4fe4b2SBrooks Davis 	struct ifnet *ifp;
981f889d2efSBrooks Davis 	struct ifnet *p;
9823ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
9833ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
9846b7330e2SSam Leffler 	struct vlanreq vlr;
98565239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
986f889d2efSBrooks Davis 
9876b7330e2SSam Leffler 	/*
9886b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
9896b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
9906b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
9916b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
9926b7330e2SSam Leffler 	 *   must be configured separately.
9936b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
994a4641f4eSPedro F. Giffuni 	 * supported for backwards compatibility.
9957983103aSRobert Watson 	 *
9967983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
9977983103aSRobert Watson 	 * called for.
9986b7330e2SSam Leffler 	 */
9996b7330e2SSam Leffler 	if (params) {
10006b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
10016b7330e2SSam Leffler 		if (error)
10026b7330e2SSam Leffler 			return error;
10039bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
10046b7330e2SSam Leffler 		if (p == NULL)
1005b1828acfSGleb Smirnoff 			return (ENXIO);
10066b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
10079bcf3ae4SAlexander Motin 		if (error != 0) {
10089bcf3ae4SAlexander Motin 			if_rele(p);
10096b7330e2SSam Leffler 			return (error);
10109bcf3ae4SAlexander Motin 		}
10117983103aSRobert Watson 		vid = vlr.vlr_tag;
10126b7330e2SSam Leffler 		wildcard = (unit < 0);
1013f941c31aSGleb Smirnoff 	} else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1014f889d2efSBrooks Davis 		unit = -1;
1015f889d2efSBrooks Davis 		wildcard = 0;
1016f889d2efSBrooks Davis 	} else {
10179bcf3ae4SAlexander Motin 		p = NULL;
1018f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
1019f889d2efSBrooks Davis 		if (error != 0)
1020f889d2efSBrooks Davis 			return (error);
1021f889d2efSBrooks Davis 
1022f889d2efSBrooks Davis 		wildcard = (unit < 0);
1023f889d2efSBrooks Davis 	}
1024f889d2efSBrooks Davis 
1025f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
10269bcf3ae4SAlexander Motin 	if (error != 0) {
10279bcf3ae4SAlexander Motin 		if (p != NULL)
10289bcf3ae4SAlexander Motin 			if_rele(p);
1029f889d2efSBrooks Davis 		return (error);
10309bcf3ae4SAlexander Motin 	}
1031f889d2efSBrooks Davis 
1032f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
1033f889d2efSBrooks Davis 	if (wildcard) {
1034f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
1035f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1036f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
1037f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
1038f889d2efSBrooks Davis 		}
1039f889d2efSBrooks Davis 	}
10409d4fe4b2SBrooks Davis 
1041a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1042fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1043fc74a9f9SBrooks Davis 	if (ifp == NULL) {
1044fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
1045fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
10469bcf3ae4SAlexander Motin 		if (p != NULL)
10479bcf3ae4SAlexander Motin 			if_rele(p);
1048fc74a9f9SBrooks Davis 		return (ENOSPC);
1049fc74a9f9SBrooks Davis 	}
1050b08d611dSMatt Macy 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
10519d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
1052f889d2efSBrooks Davis 	/*
1053cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
1054f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
1055f889d2efSBrooks Davis 	 */
1056f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
105742a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
1058f889d2efSBrooks Davis 	ifp->if_dunit = unit;
10599d4fe4b2SBrooks Davis 
1060114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
1061d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
1062d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
10639d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
1064b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1065f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1066fb3bc596SJohn Baldwin 	ifp->if_snd_tag_modify = vlan_snd_tag_modify;
1067fb3bc596SJohn Baldwin 	ifp->if_snd_tag_query = vlan_snd_tag_query;
1068fa91f845SRandall Stewart 	ifp->if_snd_tag_free = vlan_snd_tag_free;
1069f3e7afe2SHans Petter Selasky #endif
107064a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
1071fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
10729d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
1073211f625aSBill Fenner 	ifp->if_baudrate = 0;
1074a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
1075a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
10763ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
10773ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
10783ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
10799d4fe4b2SBrooks Davis 
10809bcf3ae4SAlexander Motin 	if (p != NULL) {
10817983103aSRobert Watson 		error = vlan_config(ifv, p, vid);
10829bcf3ae4SAlexander Motin 		if_rele(p);
1083f889d2efSBrooks Davis 		if (error != 0) {
1084f889d2efSBrooks Davis 			/*
108528cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
1086f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
1087f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1088f889d2efSBrooks Davis 			 */
1089f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1090249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
10914b22573aSBrooks Davis 			if_free(ifp);
109296c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
1093f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1094f889d2efSBrooks Davis 
1095f889d2efSBrooks Davis 			return (error);
1096f889d2efSBrooks Davis 		}
1097f889d2efSBrooks Davis 	}
1098f889d2efSBrooks Davis 
10999d4fe4b2SBrooks Davis 	return (0);
11009d4fe4b2SBrooks Davis }
11019d4fe4b2SBrooks Davis 
1102f889d2efSBrooks Davis static int
1103f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
11049d4fe4b2SBrooks Davis {
11059d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1106114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
1107b4e9f837SBrooks Davis 
1108249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1109249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1110d148c2a2SMatt Joras 	/*
1111d148c2a2SMatt Joras 	 * We should have the only reference to the ifv now, so we can now
1112d148c2a2SMatt Joras 	 * drain any remaining lladdr task before freeing the ifnet and the
1113d148c2a2SMatt Joras 	 * ifvlan.
1114d148c2a2SMatt Joras 	 */
1115d148c2a2SMatt Joras 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1116b08d611dSMatt Macy 	NET_EPOCH_WAIT();
11174b22573aSBrooks Davis 	if_free(ifp);
11189d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1119b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
1120b4e9f837SBrooks Davis 
1121f889d2efSBrooks Davis 	return (0);
11229d4fe4b2SBrooks Davis }
11239d4fe4b2SBrooks Davis 
112415a66c21SBruce M Simpson /*
112515a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
112615a66c21SBruce M Simpson  */
11272cc2df49SGarrett Wollman static void
1128114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
11292cc2df49SGarrett Wollman {
11302cc2df49SGarrett Wollman }
11312cc2df49SGarrett Wollman 
11326d3a3ab7SGleb Smirnoff /*
1133d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
11346d3a3ab7SGleb Smirnoff  */
1135d9b1d615SJohn Baldwin static int
1136d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
11372cc2df49SGarrett Wollman {
11382cc2df49SGarrett Wollman 	struct ifvlan *ifv;
11392cc2df49SGarrett Wollman 	struct ifnet *p;
11401ad7a257SPyun YongHyeon 	int error, len, mcast;
11412cc2df49SGarrett Wollman 
1142b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1143b8a6e03fSGleb Smirnoff 
11442cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
1145d148c2a2SMatt Joras 	if (TRUNK(ifv) == NULL) {
1146d148c2a2SMatt Joras 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1147d148c2a2SMatt Joras 		m_freem(m);
1148d148c2a2SMatt Joras 		return (ENETDOWN);
1149d148c2a2SMatt Joras 	}
115075ee267cSGleb Smirnoff 	p = PARENT(ifv);
11511ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
11521ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
11532cc2df49SGarrett Wollman 
1154a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
11552cc2df49SGarrett Wollman 
1156b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1157fb3bc596SJohn Baldwin 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1158fb3bc596SJohn Baldwin 		struct vlan_snd_tag *vst;
1159fb3bc596SJohn Baldwin 		struct m_snd_tag *mst;
1160fb3bc596SJohn Baldwin 
1161fb3bc596SJohn Baldwin 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1162fb3bc596SJohn Baldwin 		mst = m->m_pkthdr.snd_tag;
1163fb3bc596SJohn Baldwin 		vst = mst_to_vst(mst);
1164fb3bc596SJohn Baldwin 		if (vst->tag->ifp != p) {
1165fb3bc596SJohn Baldwin 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1166fb3bc596SJohn Baldwin 			m_freem(m);
1167fb3bc596SJohn Baldwin 			return (EAGAIN);
1168fb3bc596SJohn Baldwin 		}
1169fb3bc596SJohn Baldwin 
1170fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1171fb3bc596SJohn Baldwin 		m_snd_tag_rele(mst);
1172fb3bc596SJohn Baldwin 	}
1173fb3bc596SJohn Baldwin #endif
1174fb3bc596SJohn Baldwin 
1175f731f104SBill Paul 	/*
1176d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
117724993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
117824993214SYaroslav Tykhiy 	 */
11792dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
1180a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1181d148c2a2SMatt Joras 		m_freem(m);
11828b20f6cfSHiroki Sato 		return (ENETDOWN);
118324993214SYaroslav Tykhiy 	}
118424993214SYaroslav Tykhiy 
1185f1379734SKonstantin Belousov 	if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1186a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1187d9b1d615SJohn Baldwin 		return (0);
11884af90a4dSMatthew N. Dodd 	}
11892cc2df49SGarrett Wollman 
11902cc2df49SGarrett Wollman 	/*
11912cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
11922cc2df49SGarrett Wollman 	 */
1193aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1194299153b5SAlexander V. Chernikov 	if (error == 0) {
1195a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1196a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1197a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
11981ad7a257SPyun YongHyeon 	} else
1199a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1200d9b1d615SJohn Baldwin 	return (error);
12012cc2df49SGarrett Wollman }
1202d9b1d615SJohn Baldwin 
120316cf6bdbSMatt Joras static int
120416cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
120516cf6bdbSMatt Joras     struct route *ro)
120616cf6bdbSMatt Joras {
120716cf6bdbSMatt Joras 	struct ifvlan *ifv;
120816cf6bdbSMatt Joras 	struct ifnet *p;
120916cf6bdbSMatt Joras 
1210b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1211b8a6e03fSGleb Smirnoff 
121216cf6bdbSMatt Joras 	ifv = ifp->if_softc;
121316cf6bdbSMatt Joras 	if (TRUNK(ifv) == NULL) {
121416cf6bdbSMatt Joras 		m_freem(m);
121516cf6bdbSMatt Joras 		return (ENETDOWN);
121616cf6bdbSMatt Joras 	}
121716cf6bdbSMatt Joras 	p = PARENT(ifv);
121816cf6bdbSMatt Joras 	return p->if_output(ifp, m, dst, ro);
121916cf6bdbSMatt Joras }
122016cf6bdbSMatt Joras 
122116cf6bdbSMatt Joras 
1222d9b1d615SJohn Baldwin /*
1223d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1224d9b1d615SJohn Baldwin  */
1225d9b1d615SJohn Baldwin static void
1226d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1227d9b1d615SJohn Baldwin {
1228f731f104SBill Paul }
1229f731f104SBill Paul 
1230a3814acfSSam Leffler static void
1231a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1232f731f104SBill Paul {
1233d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1234f731f104SBill Paul 	struct ifvlan *ifv;
12352ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
12362ccbbd06SMarcelo Araujo 	uint16_t vid, tag;
123775ee267cSGleb Smirnoff 
1238b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1239b8a6e03fSGleb Smirnoff 
1240d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1241d148c2a2SMatt Joras 	if (trunk == NULL) {
1242d148c2a2SMatt Joras 		m_freem(m);
1243d148c2a2SMatt Joras 		return;
1244d148c2a2SMatt Joras 	}
1245a3814acfSSam Leffler 
1246f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1247a3814acfSSam Leffler 		/*
124814e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1249a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1250a3814acfSSam Leffler 		 */
12512ccbbd06SMarcelo Araujo 		tag = m->m_pkthdr.ether_vtag;
12526ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1253a3814acfSSam Leffler 	} else {
125475ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
125575ee267cSGleb Smirnoff 
125614e98256SYaroslav Tykhiy 		/*
125714e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
125814e98256SYaroslav Tykhiy 		 */
1259a3814acfSSam Leffler 		switch (ifp->if_type) {
1260a3814acfSSam Leffler 		case IFT_ETHER:
1261a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1262a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1263a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1264a3814acfSSam Leffler 				return;
1265a3814acfSSam Leffler 			}
1266a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
12672ccbbd06SMarcelo Araujo 			tag = ntohs(evl->evl_tag);
1268db8b5973SYaroslav Tykhiy 
1269db8b5973SYaroslav Tykhiy 			/*
12702dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
12712dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
12722dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
12732dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1274db8b5973SYaroslav Tykhiy 			 */
12752dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
12762dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
12772dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1278a3814acfSSam Leffler 			break;
12792dc879b3SYaroslav Tykhiy 
1280a3814acfSSam Leffler 		default:
1281db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
128260c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
128360c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1284a3814acfSSam Leffler #endif
12853751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1286d148c2a2SMatt Joras 			m_freem(m);
128760c60618SYaroslav Tykhiy 			return;
1288a3814acfSSam Leffler 		}
12897a46ec8fSBrooks Davis 	}
12907a46ec8fSBrooks Davis 
12912ccbbd06SMarcelo Araujo 	vid = EVL_VLANOFTAG(tag);
12922ccbbd06SMarcelo Araujo 
12937983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
12942dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1295b08d611dSMatt Macy 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1296d148c2a2SMatt Joras 		m_freem(m);
129775ee267cSGleb Smirnoff 		return;
129875ee267cSGleb Smirnoff 	}
1299f731f104SBill Paul 
13002ccbbd06SMarcelo Araujo 	if (vlan_mtag_pcp) {
13012ccbbd06SMarcelo Araujo 		/*
13022ccbbd06SMarcelo Araujo 		 * While uncommon, it is possible that we will find a 802.1q
13032ccbbd06SMarcelo Araujo 		 * packet encapsulated inside another packet that also had an
13042ccbbd06SMarcelo Araujo 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
13052ccbbd06SMarcelo Araujo 		 * arriving over ethernet.  In that case, we replace the
13062ccbbd06SMarcelo Araujo 		 * existing 802.1q PCP m_tag value.
13072ccbbd06SMarcelo Araujo 		 */
13082ccbbd06SMarcelo Araujo 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
13092ccbbd06SMarcelo Araujo 		if (mtag == NULL) {
13102ccbbd06SMarcelo Araujo 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
13112ccbbd06SMarcelo Araujo 			    sizeof(uint8_t), M_NOWAIT);
13122ccbbd06SMarcelo Araujo 			if (mtag == NULL) {
13132ccbbd06SMarcelo Araujo 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1314d148c2a2SMatt Joras 				m_freem(m);
13152ccbbd06SMarcelo Araujo 				return;
13162ccbbd06SMarcelo Araujo 			}
13172ccbbd06SMarcelo Araujo 			m_tag_prepend(m, mtag);
13182ccbbd06SMarcelo Araujo 		}
13192ccbbd06SMarcelo Araujo 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
13202ccbbd06SMarcelo Araujo 	}
13212ccbbd06SMarcelo Araujo 
1322fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1323c0304424SGleb Smirnoff 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
13242cc2df49SGarrett Wollman 
1325a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1326fdbf1174SMatt Joras 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
13272cc2df49SGarrett Wollman }
13282cc2df49SGarrett Wollman 
1329d148c2a2SMatt Joras static void
1330d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused)
1331d148c2a2SMatt Joras {
1332d148c2a2SMatt Joras 	struct ifvlan *ifv;
1333d148c2a2SMatt Joras 	struct ifnet *ifp;
1334d148c2a2SMatt Joras 
1335d148c2a2SMatt Joras 	ifv = (struct ifvlan *)arg;
1336d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
13375191a3aeSKristof Provost 
13385191a3aeSKristof Provost 	CURVNET_SET(ifp->if_vnet);
13395191a3aeSKristof Provost 
1340d148c2a2SMatt Joras 	/* The ifv_ifp already has the lladdr copied in. */
1341d148c2a2SMatt Joras 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
13425191a3aeSKristof Provost 
13435191a3aeSKristof Provost 	CURVNET_RESTORE();
1344d148c2a2SMatt Joras }
1345d148c2a2SMatt Joras 
13462cc2df49SGarrett Wollman static int
13477983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
13482cc2df49SGarrett Wollman {
1349*6dcec895SGleb Smirnoff 	struct epoch_tracker et;
135075ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
13511cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
135275ee267cSGleb Smirnoff 	int error = 0;
13532cc2df49SGarrett Wollman 
1354b1828acfSGleb Smirnoff 	/*
1355b1828acfSGleb Smirnoff 	 * We can handle non-ethernet hardware types as long as
1356b1828acfSGleb Smirnoff 	 * they handle the tagging and headers themselves.
1357b1828acfSGleb Smirnoff 	 */
1358e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1359e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
136015a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
136164a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
136264a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
1363b1828acfSGleb Smirnoff 	/*
1364b1828acfSGleb Smirnoff 	 * Don't let the caller set up a VLAN VID with
1365b1828acfSGleb Smirnoff 	 * anything except VLID bits.
1366b1828acfSGleb Smirnoff 	 * VID numbers 0x0 and 0xFFF are reserved.
1367b1828acfSGleb Smirnoff 	 */
1368b1828acfSGleb Smirnoff 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1369b1828acfSGleb Smirnoff 		return (EINVAL);
137075ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
137115a66c21SBruce M Simpson 		return (EBUSY);
13722cc2df49SGarrett Wollman 
1373d148c2a2SMatt Joras 	VLAN_XLOCK();
137475ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
137575ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
137675ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
137775ee267cSGleb Smirnoff 		vlan_inithash(trunk);
137875ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
1379d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
138075ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
138175ee267cSGleb Smirnoff 		trunk->parent = p;
13829bcf3ae4SAlexander Motin 		if_ref(trunk->parent);
1383b08d611dSMatt Macy 		TRUNK_WUNLOCK(trunk);
138475ee267cSGleb Smirnoff 	} else {
138575ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
138675ee267cSGleb Smirnoff 	}
138775ee267cSGleb Smirnoff 
13887983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
13892ccbbd06SMarcelo Araujo 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
13902ccbbd06SMarcelo Araujo 	vlan_tag_recalculate(ifv);
139175ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
139275ee267cSGleb Smirnoff 	if (error)
139375ee267cSGleb Smirnoff 		goto done;
139473f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1395a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1396a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
13971cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1398d89baa5aSAlexander Motin 	ifv->ifv_capenable = -1;
1399a3814acfSSam Leffler 
1400a3814acfSSam Leffler 	/*
1401a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1402a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1403656acce4SYaroslav Tykhiy 	 * use it.
1404a3814acfSSam Leffler 	 */
1405656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1406656acce4SYaroslav Tykhiy 		/*
1407656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1408656acce4SYaroslav Tykhiy 		 * handle extended frames.
1409656acce4SYaroslav Tykhiy 		 */
1410a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1411656acce4SYaroslav Tykhiy 	} else {
1412a3814acfSSam Leffler 		/*
1413a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1414a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1415a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1416a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1417a3814acfSSam Leffler 		 * which might still be useful.
1418a3814acfSSam Leffler 		 */
1419a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1420a3814acfSSam Leffler 	}
1421a3814acfSSam Leffler 
142275ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
14231cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1424e4cd31ddSJeff Roberson 	/*
1425e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1426e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1427e4cd31ddSJeff Roberson 	 * interfaces to also work.
1428e4cd31ddSJeff Roberson 	 */
14291cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
143075ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1431e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1432e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1433e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1434e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
143532a52e9eSNavdeep Parhar 	ifp->if_pcp = ifv->ifv_pcp;
1436e4cd31ddSJeff Roberson 
14372cc2df49SGarrett Wollman 	/*
143816cf6bdbSMatt Joras 	 * We wrap the parent's if_output using vlan_output to ensure that it
143916cf6bdbSMatt Joras 	 * can't become stale.
144016cf6bdbSMatt Joras 	 */
144116cf6bdbSMatt Joras 	ifp->if_output = vlan_output;
144216cf6bdbSMatt Joras 
144316cf6bdbSMatt Joras 	/*
144424993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
144524993214SYaroslav Tykhiy 	 * Other flags are none of our business.
14462cc2df49SGarrett Wollman 	 */
144764a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
14481cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
14491cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
14501cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
14511cf236fbSYaroslav Tykhiy 
14521cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
14532cc2df49SGarrett Wollman 
1454*6dcec895SGleb Smirnoff 	NET_EPOCH_ENTER(et);
145575ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1456*6dcec895SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1457a3814acfSSam Leffler 
1458a3814acfSSam Leffler 	/*
1459e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
14602cc2df49SGarrett Wollman 	 * physical interface's.
14612cc2df49SGarrett Wollman 	 */
1462e4cd31ddSJeff Roberson 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1463e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1464e4cd31ddSJeff Roberson 	    p->if_addrlen;
14651b2a4f7aSBill Fenner 
1466d148c2a2SMatt Joras 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
14672ada9747SYaroslav Tykhiy 
14682ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
14692ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1470d148c2a2SMatt Joras 
1471d148c2a2SMatt Joras 	/* Update flags on the parent, if necessary. */
1472d148c2a2SMatt Joras 	vlan_setflags(ifp, 1);
1473b08d611dSMatt Macy 
1474d148c2a2SMatt Joras 	/*
1475b08d611dSMatt Macy 	 * Configure multicast addresses that may already be
1476b08d611dSMatt Macy 	 * joined on the vlan device.
1477d148c2a2SMatt Joras 	 */
1478b08d611dSMatt Macy 	(void)vlan_setmulti(ifp);
1479b08d611dSMatt Macy 
1480b08d611dSMatt Macy done:
1481c725524cSJack F Vogel 	if (error == 0)
14827983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1483d148c2a2SMatt Joras 	VLAN_XUNLOCK();
148475ee267cSGleb Smirnoff 
148575ee267cSGleb Smirnoff 	return (error);
14862cc2df49SGarrett Wollman }
14872cc2df49SGarrett Wollman 
14886f359e28SJohn Baldwin static void
1489f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1490f731f104SBill Paul {
14915cb8c31aSYaroslav Tykhiy 
1492d148c2a2SMatt Joras 	VLAN_XLOCK();
149328cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
1494d148c2a2SMatt Joras 	VLAN_XUNLOCK();
14955cb8c31aSYaroslav Tykhiy }
14965cb8c31aSYaroslav Tykhiy 
14976f359e28SJohn Baldwin static void
149828cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
14995cb8c31aSYaroslav Tykhiy {
150075ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1501f731f104SBill Paul 	struct vlan_mc_entry *mc;
1502f731f104SBill Paul 	struct ifvlan *ifv;
1503c725524cSJack F Vogel 	struct ifnet  *parent;
150428cc4d37SJohn Baldwin 	int error;
1505f731f104SBill Paul 
1506d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
15074faedfe8SSam Leffler 
1508f731f104SBill Paul 	ifv = ifp->if_softc;
150975ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
151022893351SJack F Vogel 	parent = NULL;
1511f731f104SBill Paul 
151222893351SJack F Vogel 	if (trunk != NULL) {
151322893351SJack F Vogel 		parent = trunk->parent;
15141b2a4f7aSBill Fenner 
1515f731f104SBill Paul 		/*
1516f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1517f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
15181b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1519f731f104SBill Paul 		 */
1520b08d611dSMatt Macy 		while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
15216f359e28SJohn Baldwin 			/*
152228cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1523b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
152428cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
152528cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
152628cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
152728cc4d37SJohn Baldwin 			 * succeed.
15286f359e28SJohn Baldwin 			 */
152928cc4d37SJohn Baldwin 			if (!departing) {
153028cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1531e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
153228cc4d37SJohn Baldwin 				if (error)
153328cc4d37SJohn Baldwin 					if_printf(ifp,
153428cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
153528cc4d37SJohn Baldwin 					    error);
153628cc4d37SJohn Baldwin 			}
1537b08d611dSMatt Macy 			CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1538c32a9d66SHans Petter Selasky 			epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
1539f731f104SBill Paul 		}
1540a3814acfSSam Leffler 
15411cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1542d148c2a2SMatt Joras 
154375ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
154475ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
154575ee267cSGleb Smirnoff 
154675ee267cSGleb Smirnoff 		/*
154775ee267cSGleb Smirnoff 		 * Check if we were the last.
154875ee267cSGleb Smirnoff 		 */
154975ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
15502d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
1551b08d611dSMatt Macy 			NET_EPOCH_WAIT();
155275ee267cSGleb Smirnoff 			trunk_destroy(trunk);
1553d148c2a2SMatt Joras 		}
15541b2a4f7aSBill Fenner 	}
1555f731f104SBill Paul 
1556f731f104SBill Paul 	/* Disconnect from parent. */
15571cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
15581cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
15595cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
15605cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
15615cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1562f731f104SBill Paul 
156322893351SJack F Vogel 	/*
156422893351SJack F Vogel 	 * Only dispatch an event if vlan was
156522893351SJack F Vogel 	 * attached, otherwise there is nothing
156622893351SJack F Vogel 	 * to cleanup anyway.
156722893351SJack F Vogel 	 */
156822893351SJack F Vogel 	if (parent != NULL)
15697983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1570f731f104SBill Paul }
1571f731f104SBill Paul 
15721cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1573f731f104SBill Paul static int
15741cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
15751cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1576a3814acfSSam Leffler {
15771cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
15781cf236fbSYaroslav Tykhiy 	int error;
1579a3814acfSSam Leffler 
1580d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1581a3814acfSSam Leffler 
15821cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
15831cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
15841cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
15851cf236fbSYaroslav Tykhiy 
15861cf236fbSYaroslav Tykhiy 	/*
15871cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
15881cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
15891cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
15901cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
15911cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
15921cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
15931cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
15941cf236fbSYaroslav Tykhiy 	 */
15951cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
159675ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
15971cf236fbSYaroslav Tykhiy 		if (error)
1598a3814acfSSam Leffler 			return (error);
15991cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
16001cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
16011cf236fbSYaroslav Tykhiy 	}
16021cf236fbSYaroslav Tykhiy 	return (0);
16031cf236fbSYaroslav Tykhiy }
16041cf236fbSYaroslav Tykhiy 
16051cf236fbSYaroslav Tykhiy /*
16061cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
16071cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
16081cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
16091cf236fbSYaroslav Tykhiy  */
16101cf236fbSYaroslav Tykhiy static int
16111cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
16121cf236fbSYaroslav Tykhiy {
16131cf236fbSYaroslav Tykhiy 	int error, i;
16141cf236fbSYaroslav Tykhiy 
16151cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
16161cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
16171cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
16181cf236fbSYaroslav Tykhiy 		if (error)
16191cf236fbSYaroslav Tykhiy 			return (error);
16201cf236fbSYaroslav Tykhiy 	}
16211cf236fbSYaroslav Tykhiy 	return (0);
1622a3814acfSSam Leffler }
1623a3814acfSSam Leffler 
1624127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1625127d7b2dSAndre Oppermann static void
1626a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1627127d7b2dSAndre Oppermann {
1628d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1629127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
1630127d7b2dSAndre Oppermann 
1631b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1632b8a6e03fSGleb Smirnoff 
1633d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1634b8a6e03fSGleb Smirnoff 	if (trunk == NULL)
1635d148c2a2SMatt Joras 		return;
1636d148c2a2SMatt Joras 
1637d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1638d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
1639aad0be7aSGleb Smirnoff 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1640fc74a9f9SBrooks Davis 		if_link_state_change(ifv->ifv_ifp,
164175ee267cSGleb Smirnoff 		    trunk->parent->if_link_state);
1642127d7b2dSAndre Oppermann 	}
1643d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
164475ee267cSGleb Smirnoff }
164575ee267cSGleb Smirnoff 
164675ee267cSGleb Smirnoff static void
164775ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
164875ee267cSGleb Smirnoff {
1649d148c2a2SMatt Joras 	struct ifnet *p;
1650d148c2a2SMatt Joras 	struct ifnet *ifp;
16519fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
1652d89baa5aSAlexander Motin 	int cap = 0, ena = 0, mena;
1653d89baa5aSAlexander Motin 	u_long hwa = 0;
165475ee267cSGleb Smirnoff 
1655a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
1656b8a6e03fSGleb Smirnoff 	VLAN_SXLOCK_ASSERT();
1657b8a6e03fSGleb Smirnoff 
1658d148c2a2SMatt Joras 	p = PARENT(ifv);
1659d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
166075ee267cSGleb Smirnoff 
1661d89baa5aSAlexander Motin 	/* Mask parent interface enabled capabilities disabled by user. */
1662d89baa5aSAlexander Motin 	mena = p->if_capenable & ifv->ifv_capenable;
1663d89baa5aSAlexander Motin 
166475ee267cSGleb Smirnoff 	/*
166575ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
166675ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
166775ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
166875ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
166975ee267cSGleb Smirnoff 	 */
167075ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1671d89baa5aSAlexander Motin 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
167275ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
167375ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1674d89baa5aSAlexander Motin 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1675d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM)
1676d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1677d89baa5aSAlexander Motin 			    CSUM_UDP | CSUM_SCTP);
1678d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM_IPV6)
1679d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1680d89baa5aSAlexander Motin 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
168175ee267cSGleb Smirnoff 	}
1682d89baa5aSAlexander Motin 
16839b76d9cbSPyun YongHyeon 	/*
16849b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
16859b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
16869b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
16879b76d9cbSPyun YongHyeon 	 */
16889fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
16899fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
16909fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
16919b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1692d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TSO;
16939b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1694d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TSO;
1695d89baa5aSAlexander Motin 		if (ena & IFCAP_TSO)
1696d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & CSUM_TSO;
16979b76d9cbSPyun YongHyeon 	}
169809fe6320SNavdeep Parhar 
169909fe6320SNavdeep Parhar 	/*
170059150e91SAlexander Motin 	 * If the parent interface can do LRO and checksum offloading on
170159150e91SAlexander Motin 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
170259150e91SAlexander Motin 	 * cost nothing, while false negative may lead to some confusions.
170359150e91SAlexander Motin 	 */
170459150e91SAlexander Motin 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
170559150e91SAlexander Motin 		cap |= p->if_capabilities & IFCAP_LRO;
170659150e91SAlexander Motin 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
170759150e91SAlexander Motin 		ena |= p->if_capenable & IFCAP_LRO;
170859150e91SAlexander Motin 
170959150e91SAlexander Motin 	/*
171009fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
171109fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
171209fe6320SNavdeep Parhar 	 *
171309fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
171409fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
171509fe6320SNavdeep Parhar 	 * with its own bit.
171609fe6320SNavdeep Parhar 	 */
171709fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
171809fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1719d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TOE;
172009fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
172109fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
1722d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TOE;
172309fe6320SNavdeep Parhar 	}
1724f3e7afe2SHans Petter Selasky 
1725d89baa5aSAlexander Motin 	/*
1726d89baa5aSAlexander Motin 	 * If the parent interface supports dynamic link state, so does the
1727d89baa5aSAlexander Motin 	 * VLAN interface.
1728d89baa5aSAlexander Motin 	 */
1729d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1730d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_LINKSTATE);
1731d89baa5aSAlexander Motin 
1732f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1733f3e7afe2SHans Petter Selasky 	/*
1734f3e7afe2SHans Petter Selasky 	 * If the parent interface supports ratelimiting, so does the
1735f3e7afe2SHans Petter Selasky 	 * VLAN interface.
1736f3e7afe2SHans Petter Selasky 	 */
1737d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1738d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_TXRTLMT);
1739f3e7afe2SHans Petter Selasky #endif
1740d89baa5aSAlexander Motin 
174166d0c056SJohn Baldwin 	/*
174266d0c056SJohn Baldwin 	 * If the parent interface supports unmapped mbufs, so does
174366d0c056SJohn Baldwin 	 * the VLAN interface.  Note that this should be fine even for
174466d0c056SJohn Baldwin 	 * interfaces that don't support hardware tagging as headers
174566d0c056SJohn Baldwin 	 * are prepended in normal mbufs to unmapped mbufs holding
174666d0c056SJohn Baldwin 	 * payload data.
174766d0c056SJohn Baldwin 	 */
174866d0c056SJohn Baldwin 	cap |= (p->if_capabilities & IFCAP_NOMAP);
174966d0c056SJohn Baldwin 	ena |= (mena & IFCAP_NOMAP);
175066d0c056SJohn Baldwin 
1751b2e60773SJohn Baldwin 	/*
1752b2e60773SJohn Baldwin 	 * If the parent interface can offload encryption and segmentation
1753b2e60773SJohn Baldwin 	 * of TLS records over TCP, propagate it's capability to the VLAN
1754b2e60773SJohn Baldwin 	 * interface.
1755b2e60773SJohn Baldwin 	 *
1756b2e60773SJohn Baldwin 	 * All TLS drivers in the tree today can deal with VLANs.  If
1757b2e60773SJohn Baldwin 	 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1758b2e60773SJohn Baldwin 	 * defined.
1759b2e60773SJohn Baldwin 	 */
1760b2e60773SJohn Baldwin 	if (p->if_capabilities & IFCAP_TXTLS)
1761b2e60773SJohn Baldwin 		cap |= p->if_capabilities & IFCAP_TXTLS;
1762b2e60773SJohn Baldwin 	if (p->if_capenable & IFCAP_TXTLS)
1763b2e60773SJohn Baldwin 		ena |= mena & IFCAP_TXTLS;
1764b2e60773SJohn Baldwin 
1765d89baa5aSAlexander Motin 	ifp->if_capabilities = cap;
1766d89baa5aSAlexander Motin 	ifp->if_capenable = ena;
1767d89baa5aSAlexander Motin 	ifp->if_hwassist = hwa;
176875ee267cSGleb Smirnoff }
176975ee267cSGleb Smirnoff 
177075ee267cSGleb Smirnoff static void
177175ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
177275ee267cSGleb Smirnoff {
1773d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
177475ee267cSGleb Smirnoff 	struct ifvlan *ifv;
177575ee267cSGleb Smirnoff 
1776d148c2a2SMatt Joras 	VLAN_SLOCK();
1777d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1778d148c2a2SMatt Joras 	if (trunk == NULL) {
1779d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1780d148c2a2SMatt Joras 		return;
1781d148c2a2SMatt Joras 	}
1782b8a6e03fSGleb Smirnoff 	VLAN_FOREACH(ifv, trunk)
178375ee267cSGleb Smirnoff 		vlan_capabilities(ifv);
1784d148c2a2SMatt Joras 	VLAN_SUNLOCK();
1785127d7b2dSAndre Oppermann }
1786127d7b2dSAndre Oppermann 
1787a3814acfSSam Leffler static int
1788cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
17892cc2df49SGarrett Wollman {
17902cc2df49SGarrett Wollman 	struct ifnet *p;
17912cc2df49SGarrett Wollman 	struct ifreq *ifr;
1792e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
17932cc2df49SGarrett Wollman 	struct ifvlan *ifv;
17942d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
17952cc2df49SGarrett Wollman 	struct vlanreq vlr;
17962cc2df49SGarrett Wollman 	int error = 0;
17972cc2df49SGarrett Wollman 
17982cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1799e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
18002cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
18012cc2df49SGarrett Wollman 
18022cc2df49SGarrett Wollman 	switch (cmd) {
1803e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1804e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1805e4cd31ddSJeff Roberson #ifdef INET
1806e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1807e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1808e4cd31ddSJeff Roberson #endif
1809e4cd31ddSJeff Roberson 		break;
1810e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
181138d958a6SBrooks Davis 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
181238d958a6SBrooks Davis 		    ifp->if_addrlen);
1813e4cd31ddSJeff Roberson 		break;
1814b3cca108SBill Fenner 	case SIOCGIFMEDIA:
1815d148c2a2SMatt Joras 		VLAN_SLOCK();
181675ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1817d8564efdSEd Maste 			p = PARENT(ifv);
18189bcf3ae4SAlexander Motin 			if_ref(p);
1819d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
18209bcf3ae4SAlexander Motin 			if_rele(p);
1821b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1822b3cca108SBill Fenner 			if (error == 0) {
1823b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1824b3cca108SBill Fenner 
1825b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1826b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1827b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1828b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1829b3cca108SBill Fenner 						ifmr->ifm_ulist,
1830b3cca108SBill Fenner 						sizeof(int));
1831b3cca108SBill Fenner 				}
1832b3cca108SBill Fenner 			}
18334faedfe8SSam Leffler 		} else {
1834b3cca108SBill Fenner 			error = EINVAL;
18354faedfe8SSam Leffler 		}
1836d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1837b3cca108SBill Fenner 		break;
1838b3cca108SBill Fenner 
1839b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1840b3cca108SBill Fenner 		error = EINVAL;
1841b3cca108SBill Fenner 		break;
1842b3cca108SBill Fenner 
18432cc2df49SGarrett Wollman 	case SIOCSIFMTU:
18442cc2df49SGarrett Wollman 		/*
18452cc2df49SGarrett Wollman 		 * Set the interface MTU.
18462cc2df49SGarrett Wollman 		 */
1847d148c2a2SMatt Joras 		VLAN_SLOCK();
1848d148c2a2SMatt Joras 		trunk = TRUNK(ifv);
1849d148c2a2SMatt Joras 		if (trunk != NULL) {
1850d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1851a3814acfSSam Leffler 			if (ifr->ifr_mtu >
185275ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1853a3814acfSSam Leffler 			    ifr->ifr_mtu <
1854a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
18552cc2df49SGarrett Wollman 				error = EINVAL;
1856a3814acfSSam Leffler 			else
18572cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1858d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
1859a3814acfSSam Leffler 		} else
1860a3814acfSSam Leffler 			error = EINVAL;
1861d148c2a2SMatt Joras 		VLAN_SUNLOCK();
18622cc2df49SGarrett Wollman 		break;
18632cc2df49SGarrett Wollman 
18642cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1865ccf7ba97SMarko Zec #ifdef VIMAGE
186615f6780eSRobert Watson 		/*
186715f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
186815f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
186915f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
187015f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
18715a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
187215f6780eSRobert Watson 		 */
1873ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1874ccf7ba97SMarko Zec 			error = EPERM;
1875ccf7ba97SMarko Zec 			break;
1876ccf7ba97SMarko Zec 		}
1877ccf7ba97SMarko Zec #endif
1878541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
18792cc2df49SGarrett Wollman 		if (error)
18802cc2df49SGarrett Wollman 			break;
18812cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1882f731f104SBill Paul 			vlan_unconfig(ifp);
18832cc2df49SGarrett Wollman 			break;
18842cc2df49SGarrett Wollman 		}
18859bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
18861bdc73d3SEd Maste 		if (p == NULL) {
18872cc2df49SGarrett Wollman 			error = ENOENT;
18882cc2df49SGarrett Wollman 			break;
18892cc2df49SGarrett Wollman 		}
189075ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
18919bcf3ae4SAlexander Motin 		if_rele(p);
18922cc2df49SGarrett Wollman 		break;
18932cc2df49SGarrett Wollman 
18942cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1895ccf7ba97SMarko Zec #ifdef VIMAGE
1896ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1897ccf7ba97SMarko Zec 			error = EPERM;
1898ccf7ba97SMarko Zec 			break;
1899ccf7ba97SMarko Zec 		}
1900ccf7ba97SMarko Zec #endif
190115a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
1902d148c2a2SMatt Joras 		VLAN_SLOCK();
190375ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
190475ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
19059bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
19067983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
19072cc2df49SGarrett Wollman 		}
1908d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1909541d96aaSBrooks Davis 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
19102cc2df49SGarrett Wollman 		break;
19112cc2df49SGarrett Wollman 
19122cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
19132cc2df49SGarrett Wollman 		/*
19141cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
19151cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
19162cc2df49SGarrett Wollman 		 */
1917d148c2a2SMatt Joras 		VLAN_XLOCK();
191875ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
19191cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
1920d148c2a2SMatt Joras 		VLAN_XUNLOCK();
19212cc2df49SGarrett Wollman 		break;
1922a3814acfSSam Leffler 
1923f731f104SBill Paul 	case SIOCADDMULTI:
1924f731f104SBill Paul 	case SIOCDELMULTI:
192575ee267cSGleb Smirnoff 		/*
192675ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
192775ee267cSGleb Smirnoff 		 * when we do.
1928d148c2a2SMatt Joras 		 *
1929d148c2a2SMatt Joras 		 * XXX We need the rmlock here to avoid sleeping while
1930d148c2a2SMatt Joras 		 * holding in6_multi_mtx.
193175ee267cSGleb Smirnoff 		 */
1932b08d611dSMatt Macy 		VLAN_XLOCK();
19332d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
1934b08d611dSMatt Macy 		if (trunk != NULL)
1935f731f104SBill Paul 			error = vlan_setmulti(ifp);
1936b08d611dSMatt Macy 		VLAN_XUNLOCK();
193775ee267cSGleb Smirnoff 
1938b08d611dSMatt Macy 		break;
19392ccbbd06SMarcelo Araujo 	case SIOCGVLANPCP:
19402ccbbd06SMarcelo Araujo #ifdef VIMAGE
19412ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19422ccbbd06SMarcelo Araujo 			error = EPERM;
19432ccbbd06SMarcelo Araujo 			break;
19442ccbbd06SMarcelo Araujo 		}
19452ccbbd06SMarcelo Araujo #endif
19462ccbbd06SMarcelo Araujo 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
19472ccbbd06SMarcelo Araujo 		break;
19482ccbbd06SMarcelo Araujo 
19492ccbbd06SMarcelo Araujo 	case SIOCSVLANPCP:
19502ccbbd06SMarcelo Araujo #ifdef VIMAGE
19512ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19522ccbbd06SMarcelo Araujo 			error = EPERM;
19532ccbbd06SMarcelo Araujo 			break;
19542ccbbd06SMarcelo Araujo 		}
19552ccbbd06SMarcelo Araujo #endif
19562ccbbd06SMarcelo Araujo 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
19572ccbbd06SMarcelo Araujo 		if (error)
19582ccbbd06SMarcelo Araujo 			break;
19592ccbbd06SMarcelo Araujo 		if (ifr->ifr_vlan_pcp > 7) {
19602ccbbd06SMarcelo Araujo 			error = EINVAL;
19612ccbbd06SMarcelo Araujo 			break;
19622ccbbd06SMarcelo Araujo 		}
19632ccbbd06SMarcelo Araujo 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
196432a52e9eSNavdeep Parhar 		ifp->if_pcp = ifv->ifv_pcp;
19652ccbbd06SMarcelo Araujo 		vlan_tag_recalculate(ifv);
19664a381a9eSHans Petter Selasky 		/* broadcast event about PCP change */
19674a381a9eSHans Petter Selasky 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
19682ccbbd06SMarcelo Araujo 		break;
19692ccbbd06SMarcelo Araujo 
1970d89baa5aSAlexander Motin 	case SIOCSIFCAP:
1971d148c2a2SMatt Joras 		VLAN_SLOCK();
1972d89baa5aSAlexander Motin 		ifv->ifv_capenable = ifr->ifr_reqcap;
1973d89baa5aSAlexander Motin 		trunk = TRUNK(ifv);
1974*6dcec895SGleb Smirnoff 		if (trunk != NULL) {
1975*6dcec895SGleb Smirnoff 			struct epoch_tracker et;
1976*6dcec895SGleb Smirnoff 
1977*6dcec895SGleb Smirnoff 			NET_EPOCH_ENTER(et);
1978d89baa5aSAlexander Motin 			vlan_capabilities(ifv);
1979*6dcec895SGleb Smirnoff 			NET_EPOCH_EXIT(et);
1980*6dcec895SGleb Smirnoff 		}
1981d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1982d89baa5aSAlexander Motin 		break;
1983d89baa5aSAlexander Motin 
19842cc2df49SGarrett Wollman 	default:
1985e4cd31ddSJeff Roberson 		error = EINVAL;
1986e4cd31ddSJeff Roberson 		break;
19872cc2df49SGarrett Wollman 	}
198815a66c21SBruce M Simpson 
198915a66c21SBruce M Simpson 	return (error);
19902cc2df49SGarrett Wollman }
1991f3e7afe2SHans Petter Selasky 
1992b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1993f3e7afe2SHans Petter Selasky static int
1994f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp,
1995f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *params,
1996f3e7afe2SHans Petter Selasky     struct m_snd_tag **ppmt)
1997f3e7afe2SHans Petter Selasky {
1998fb3bc596SJohn Baldwin 	struct epoch_tracker et;
1999fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2000fb3bc596SJohn Baldwin 	struct ifvlan *ifv;
2001fb3bc596SJohn Baldwin 	struct ifnet *parent;
2002fb3bc596SJohn Baldwin 	int error;
2003f3e7afe2SHans Petter Selasky 
2004fb3bc596SJohn Baldwin 	NET_EPOCH_ENTER(et);
2005fb3bc596SJohn Baldwin 	ifv = ifp->if_softc;
2006fb3bc596SJohn Baldwin 	if (ifv->ifv_trunk != NULL)
2007fb3bc596SJohn Baldwin 		parent = PARENT(ifv);
2008fb3bc596SJohn Baldwin 	else
2009fb3bc596SJohn Baldwin 		parent = NULL;
2010fb3bc596SJohn Baldwin 	if (parent == NULL || parent->if_snd_tag_alloc == NULL) {
2011fb3bc596SJohn Baldwin 		NET_EPOCH_EXIT(et);
2012f3e7afe2SHans Petter Selasky 		return (EOPNOTSUPP);
2013fb3bc596SJohn Baldwin 	}
2014fb3bc596SJohn Baldwin 	if_ref(parent);
2015fb3bc596SJohn Baldwin 	NET_EPOCH_EXIT(et);
2016fb3bc596SJohn Baldwin 
2017fb3bc596SJohn Baldwin 	vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2018fb3bc596SJohn Baldwin 	if (vst == NULL) {
2019fb3bc596SJohn Baldwin 		if_rele(parent);
2020fb3bc596SJohn Baldwin 		return (ENOMEM);
2021fb3bc596SJohn Baldwin 	}
2022fb3bc596SJohn Baldwin 
2023fb3bc596SJohn Baldwin 	error = parent->if_snd_tag_alloc(parent, params, &vst->tag);
2024fb3bc596SJohn Baldwin 	if_rele(parent);
2025fb3bc596SJohn Baldwin 	if (error) {
2026fb3bc596SJohn Baldwin 		free(vst, M_VLAN);
2027fb3bc596SJohn Baldwin 		return (error);
2028fb3bc596SJohn Baldwin 	}
2029fb3bc596SJohn Baldwin 
2030fb3bc596SJohn Baldwin 	m_snd_tag_init(&vst->com, ifp);
2031fb3bc596SJohn Baldwin 
2032fb3bc596SJohn Baldwin 	*ppmt = &vst->com;
2033fb3bc596SJohn Baldwin 	return (0);
2034fb3bc596SJohn Baldwin }
2035fb3bc596SJohn Baldwin 
2036fb3bc596SJohn Baldwin static int
2037fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst,
2038fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *params)
2039fb3bc596SJohn Baldwin {
2040fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2041fb3bc596SJohn Baldwin 
2042fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2043fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_modify(vst->tag, params));
2044fb3bc596SJohn Baldwin }
2045fb3bc596SJohn Baldwin 
2046fb3bc596SJohn Baldwin static int
2047fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst,
2048fb3bc596SJohn Baldwin     union if_snd_tag_query_params *params)
2049fb3bc596SJohn Baldwin {
2050fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2051fb3bc596SJohn Baldwin 
2052fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2053fb3bc596SJohn Baldwin 	return (vst->tag->ifp->if_snd_tag_query(vst->tag, params));
2054f3e7afe2SHans Petter Selasky }
2055fa91f845SRandall Stewart 
2056fa91f845SRandall Stewart static void
2057fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst)
2058fa91f845SRandall Stewart {
2059fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2060fb3bc596SJohn Baldwin 
2061fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2062fb3bc596SJohn Baldwin 	m_snd_tag_rele(vst->tag);
2063fb3bc596SJohn Baldwin 	free(vst, M_VLAN);
2064fa91f845SRandall Stewart }
2065f3e7afe2SHans Petter Selasky #endif
2066