xref: /freebsd/sys/net/if_vlan.c (revision 541d96aaaf46cde0f4247c226ed29d635b1915c4)
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"
4975ee267cSGleb Smirnoff #include "opt_vlan.h"
50f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
512cc2df49SGarrett Wollman 
522cc2df49SGarrett Wollman #include <sys/param.h>
53c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
542cc2df49SGarrett Wollman #include <sys/kernel.h>
5575ee267cSGleb Smirnoff #include <sys/lock.h>
56f731f104SBill Paul #include <sys/malloc.h>
572cc2df49SGarrett Wollman #include <sys/mbuf.h>
582b120974SPeter Wemm #include <sys/module.h>
59772b000fSAlexander V. Chernikov #include <sys/rmlock.h>
602ccbbd06SMarcelo Araujo #include <sys/priv.h>
61f731f104SBill Paul #include <sys/queue.h>
622cc2df49SGarrett Wollman #include <sys/socket.h>
632cc2df49SGarrett Wollman #include <sys/sockio.h>
642cc2df49SGarrett Wollman #include <sys/sysctl.h>
652cc2df49SGarrett Wollman #include <sys/systm.h>
66e4cd31ddSJeff Roberson #include <sys/sx.h>
67d148c2a2SMatt Joras #include <sys/taskqueue.h>
682cc2df49SGarrett Wollman 
692cc2df49SGarrett Wollman #include <net/bpf.h>
702cc2df49SGarrett Wollman #include <net/ethernet.h>
712cc2df49SGarrett Wollman #include <net/if.h>
7276039bc8SGleb Smirnoff #include <net/if_var.h>
73f889d2efSBrooks Davis #include <net/if_clone.h>
742cc2df49SGarrett Wollman #include <net/if_dl.h>
752cc2df49SGarrett Wollman #include <net/if_types.h>
762cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
774b79449eSBjoern A. Zeeb #include <net/vnet.h>
782cc2df49SGarrett Wollman 
792c5b403eSOleg Bulyzhin #ifdef INET
802c5b403eSOleg Bulyzhin #include <netinet/in.h>
812c5b403eSOleg Bulyzhin #include <netinet/if_ether.h>
822c5b403eSOleg Bulyzhin #endif
832c5b403eSOleg Bulyzhin 
8475ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
8564a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
8675ee267cSGleb Smirnoff 
872dc879b3SYaroslav Tykhiy #define	UP_AND_RUNNING(ifp) \
882dc879b3SYaroslav Tykhiy     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
892dc879b3SYaroslav Tykhiy 
9075ee267cSGleb Smirnoff LIST_HEAD(ifvlanhead, ifvlan);
9175ee267cSGleb Smirnoff 
9275ee267cSGleb Smirnoff struct ifvlantrunk {
9375ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
94772b000fSAlexander V. Chernikov 	struct	rmlock	lock;
9575ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
965cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
975cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
9875ee267cSGleb Smirnoff #else
9975ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
10075ee267cSGleb Smirnoff 	uint16_t	hmask;
10175ee267cSGleb Smirnoff 	uint16_t	hwidth;
10275ee267cSGleb Smirnoff #endif
10375ee267cSGleb Smirnoff 	int		refcnt;
10475ee267cSGleb Smirnoff };
1059d4fe4b2SBrooks Davis 
106d148c2a2SMatt Joras /*
107d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk with
108d148c2a2SMatt Joras  * the assumption that none will be added/removed during iteration.
109d148c2a2SMatt Joras  */
110d148c2a2SMatt Joras #ifdef VLAN_ARRAY
111d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
112d148c2a2SMatt Joras 	size_t _i; \
113d148c2a2SMatt Joras 	for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
114d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
115d148c2a2SMatt Joras #else /* VLAN_ARRAY */
116d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
117d148c2a2SMatt Joras 	struct ifvlan *_next; \
118d148c2a2SMatt Joras 	size_t _i; \
119d148c2a2SMatt Joras 	for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
120d148c2a2SMatt Joras 		LIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
121d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
122d148c2a2SMatt Joras 
123d148c2a2SMatt Joras /*
124d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk while
125d148c2a2SMatt Joras  * also modifying the number of vlans on the trunk. The iteration continues
126d148c2a2SMatt Joras  * until some condition is met or there are no more vlans on the trunk.
127d148c2a2SMatt Joras  */
128d148c2a2SMatt Joras #ifdef VLAN_ARRAY
129d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
130d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
131d148c2a2SMatt Joras 	size_t _i; \
132d148c2a2SMatt Joras 	for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
133d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]))
134d148c2a2SMatt Joras #else /* VLAN_ARRAY */
135d148c2a2SMatt Joras /*
136d148c2a2SMatt Joras  * The hash table case is more complicated. We allow for the hash table to be
137d148c2a2SMatt Joras  * modified (i.e. vlans removed) while we are iterating over it. To allow for
138d148c2a2SMatt Joras  * this we must restart the iteration every time we "touch" something during
139d148c2a2SMatt Joras  * the iteration, since removal will resize the hash table and invalidate our
140d148c2a2SMatt Joras  * current position. If acting on the touched element causes the trunk to be
141d148c2a2SMatt Joras  * emptied, then iteration also stops.
142d148c2a2SMatt Joras  */
143d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
144d148c2a2SMatt Joras 	size_t _i; \
145d148c2a2SMatt Joras 	bool _touch = false; \
146d148c2a2SMatt Joras 	for (_i = 0; \
147d148c2a2SMatt Joras 	    !(_cond) && _i < (1 << (_trunk)->hwidth); \
148d148c2a2SMatt Joras 	    _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
149d148c2a2SMatt Joras 		if (((_ifv) = LIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
150d148c2a2SMatt Joras 		    (_touch = true))
151d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
152d148c2a2SMatt Joras 
153a3814acfSSam Leffler struct vlan_mc_entry {
154e4cd31ddSJeff Roberson 	struct sockaddr_dl		mc_addr;
155a3814acfSSam Leffler 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
156a3814acfSSam Leffler };
157a3814acfSSam Leffler 
158a3814acfSSam Leffler struct	ifvlan {
15975ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
160fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
16175ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
16275ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
1636667db31SAlexander V. Chernikov 	void	*ifv_cookie;
1641cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
165d89baa5aSAlexander Motin 	int	ifv_capenable;
166a3814acfSSam Leffler 	struct	ifv_linkmib {
167a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
168a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
169a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
17073f2233dSYaroslav Tykhiy 		uint16_t ifvm_proto;	/* encapsulation ethertype */
17175ee267cSGleb Smirnoff 		uint16_t ifvm_tag;	/* tag to apply on packets leaving if */
1722ccbbd06SMarcelo Araujo               	uint16_t ifvm_vid;	/* VLAN ID */
1732ccbbd06SMarcelo Araujo 		uint8_t	ifvm_pcp;	/* Priority Code Point (PCP). */
174a3814acfSSam Leffler 	}	ifv_mib;
175d148c2a2SMatt Joras 	struct task lladdr_task;
176114c608cSYaroslav Tykhiy 	SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
177c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY
178a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
179c0cb022bSYaroslav Tykhiy #endif
180a3814acfSSam Leffler };
18173f2233dSYaroslav Tykhiy #define	ifv_proto	ifv_mib.ifvm_proto
1822ccbbd06SMarcelo Araujo #define	ifv_tag		ifv_mib.ifvm_tag
1832ccbbd06SMarcelo Araujo #define	ifv_vid 	ifv_mib.ifvm_vid
1842ccbbd06SMarcelo Araujo #define	ifv_pcp		ifv_mib.ifvm_pcp
185a3814acfSSam Leffler #define	ifv_encaplen	ifv_mib.ifvm_encaplen
186a3814acfSSam Leffler #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
187a3814acfSSam Leffler #define	ifv_mintu	ifv_mib.ifvm_mintu
188a3814acfSSam Leffler 
18975ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
1901cf236fbSYaroslav Tykhiy static struct {
1911cf236fbSYaroslav Tykhiy 	int flag;
1921cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
1931cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
1941cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
1951cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
1961cf236fbSYaroslav Tykhiy 	{0, NULL}
1971cf236fbSYaroslav Tykhiy };
198a3814acfSSam Leffler 
199f1379734SKonstantin Belousov extern int vlan_mtag_pcp;
2002ccbbd06SMarcelo Araujo 
20142a58907SGleb Smirnoff static const char vlanname[] = "vlan";
20242a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
2032cc2df49SGarrett Wollman 
2045cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
205ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag;
2065cb8c31aSYaroslav Tykhiy 
2074faedfe8SSam Leffler /*
208d148c2a2SMatt Joras  * if_vlan uses two module-level locks to allow concurrent modification of vlan
209d148c2a2SMatt Joras  * interfaces and (mostly) allow for vlans to be destroyed while they are being
210d148c2a2SMatt Joras  * used for tx/rx. To accomplish this in a way that has acceptable performance
211d148c2a2SMatt Joras  * and cooperation with other parts of the network stack there is a
212d148c2a2SMatt Joras  * non-sleepable rmlock(9) and an sx(9). Both locks are exclusively acquired
213d148c2a2SMatt Joras  * when destroying a vlan interface, i.e. when the if_vlantrunk field of struct
214d148c2a2SMatt Joras  * ifnet is de-allocated and NULL'd. Thus a reader holding either lock has a
215d148c2a2SMatt Joras  * guarantee that the struct ifvlantrunk references a valid vlan trunk.
21675ee267cSGleb Smirnoff  *
217d148c2a2SMatt Joras  * The performance-sensitive paths that warrant using the rmlock(9) are
218d148c2a2SMatt Joras  * vlan_transmit and vlan_input. Both have to check for the vlan interface's
219d148c2a2SMatt Joras  * existence using if_vlantrunk, and being in the network tx/rx paths the use
220d148c2a2SMatt Joras  * of an rmlock(9) gives a measureable improvement in performance.
22175ee267cSGleb Smirnoff  *
222d148c2a2SMatt Joras  * The reason for having an sx(9) is mostly because there are still areas that
223d148c2a2SMatt Joras  * must be sleepable and also have safe concurrent access to a vlan interface.
224d148c2a2SMatt Joras  * Since the sx(9) exists, it is used by default in most paths unless sleeping
225d148c2a2SMatt Joras  * is not permitted, or if it is not clear whether sleeping is permitted.
226d148c2a2SMatt Joras  *
227d148c2a2SMatt Joras  * Note that despite these protections, there is still an inherent race in the
228d148c2a2SMatt Joras  * destruction of vlans since there's no guarantee that the ifnet hasn't been
229d148c2a2SMatt Joras  * freed/reused when the tx/rx functions are called by the stack. This can only
230d148c2a2SMatt Joras  * be fixed by addressing ifnet's lifetime issues.
2314faedfe8SSam Leffler  */
232d148c2a2SMatt Joras #define _VLAN_RM_ID ifv_rm_lock
233d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx
234d148c2a2SMatt Joras 
235d148c2a2SMatt Joras static struct rmlock _VLAN_RM_ID;
236d148c2a2SMatt Joras static struct sx _VLAN_SX_ID;
237d148c2a2SMatt Joras 
238d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \
239d148c2a2SMatt Joras 	rm_init(&_VLAN_RM_ID, "vlan_rm"); \
240d148c2a2SMatt Joras 	sx_init(&_VLAN_SX_ID, "vlan_sx")
241d148c2a2SMatt Joras 
242d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \
243d148c2a2SMatt Joras 	rm_destroy(&_VLAN_RM_ID); \
244d148c2a2SMatt Joras 	sx_destroy(&_VLAN_SX_ID)
245d148c2a2SMatt Joras 
246d148c2a2SMatt Joras #define	_VLAN_RM_TRACKER		_vlan_rm_tracker
247d148c2a2SMatt Joras #define	VLAN_RLOCK()			rm_rlock(&_VLAN_RM_ID, \
248d148c2a2SMatt Joras 					    &_VLAN_RM_TRACKER)
249d148c2a2SMatt Joras #define	VLAN_RUNLOCK()			rm_runlock(&_VLAN_RM_ID, \
250d148c2a2SMatt Joras 					    &_VLAN_RM_TRACKER)
251d148c2a2SMatt Joras #define	VLAN_WLOCK()			rm_wlock(&_VLAN_RM_ID)
252d148c2a2SMatt Joras #define	VLAN_WUNLOCK()			rm_wunlock(&_VLAN_RM_ID)
253d148c2a2SMatt Joras #define	VLAN_RLOCK_ASSERT()		rm_assert(&_VLAN_RM_ID, RA_RLOCKED)
254d148c2a2SMatt Joras #define	VLAN_WLOCK_ASSERT()		rm_assert(&_VLAN_RM_ID, RA_WLOCKED)
255d148c2a2SMatt Joras #define	VLAN_RWLOCK_ASSERT()		rm_assert(&_VLAN_RM_ID, RA_LOCKED)
256d148c2a2SMatt Joras #define	VLAN_LOCK_READER		struct rm_priotracker _VLAN_RM_TRACKER
257d148c2a2SMatt Joras 
258d148c2a2SMatt Joras #define	VLAN_SLOCK()			sx_slock(&_VLAN_SX_ID)
259d148c2a2SMatt Joras #define	VLAN_SUNLOCK()			sx_sunlock(&_VLAN_SX_ID)
260d148c2a2SMatt Joras #define	VLAN_XLOCK()			sx_xlock(&_VLAN_SX_ID)
261d148c2a2SMatt Joras #define	VLAN_XUNLOCK()			sx_xunlock(&_VLAN_SX_ID)
262d148c2a2SMatt Joras #define	VLAN_SLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
263d148c2a2SMatt Joras #define	VLAN_XLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
264d148c2a2SMatt Joras #define	VLAN_SXLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_LOCKED)
265d148c2a2SMatt Joras 
266d148c2a2SMatt Joras 
267d148c2a2SMatt Joras /*
268d148c2a2SMatt Joras  * We also have a per-trunk rmlock(9), that is locked shared on packet
269d148c2a2SMatt Joras  * processing and exclusive when configuration is changed. Note: This should
270d148c2a2SMatt Joras  * only be acquired while there is a shared lock on either of the global locks
271d148c2a2SMatt Joras  * via VLAN_SLOCK or VLAN_RLOCK. Thus, an exclusive lock on the global locks
272d148c2a2SMatt Joras  * makes a call to TRUNK_RLOCK/TRUNK_WLOCK technically superfluous.
273d148c2a2SMatt Joras  */
274d148c2a2SMatt Joras #define	_TRUNK_RM_TRACKER		_trunk_rm_tracker
275772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_INIT(trunk)		rm_init(&(trunk)->lock, vlanname)
276772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_DESTROY(trunk)	rm_destroy(&(trunk)->lock)
277d148c2a2SMatt Joras #define	TRUNK_RLOCK(trunk)		rm_rlock(&(trunk)->lock, \
278d148c2a2SMatt Joras     &_TRUNK_RM_TRACKER)
279d148c2a2SMatt Joras #define	TRUNK_WLOCK(trunk)		rm_wlock(&(trunk)->lock)
280d148c2a2SMatt Joras #define	TRUNK_RUNLOCK(trunk)		rm_runlock(&(trunk)->lock, \
281d148c2a2SMatt Joras     &_TRUNK_RM_TRACKER)
282d148c2a2SMatt Joras #define	TRUNK_WUNLOCK(trunk)		rm_wunlock(&(trunk)->lock)
283d148c2a2SMatt Joras #define	TRUNK_RLOCK_ASSERT(trunk)	rm_assert(&(trunk)->lock, RA_RLOCKED)
284d148c2a2SMatt Joras #define	TRUNK_LOCK_ASSERT(trunk)	rm_assert(&(trunk)->lock, RA_LOCKED)
285d148c2a2SMatt Joras #define	TRUNK_WLOCK_ASSERT(trunk)	rm_assert(&(trunk)->lock, RA_WLOCKED)
286d148c2a2SMatt Joras #define	TRUNK_LOCK_READER		struct rm_priotracker _TRUNK_RM_TRACKER
28775ee267cSGleb Smirnoff 
288d148c2a2SMatt Joras /*
289d148c2a2SMatt Joras  * The VLAN_ARRAY substitutes the dynamic hash with a static array
290d148c2a2SMatt Joras  * with 4096 entries. In theory this can give a boost in processing,
291d148c2a2SMatt Joras  * however in practice it does not. Probably this is because the array
292d148c2a2SMatt Joras  * is too big to fit into CPU cache.
293d148c2a2SMatt Joras  */
29475ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
29575ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
29675ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
29775ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
29875ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
29975ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
30075ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
3017983103aSRobert Watson 	uint16_t vid);
30275ee267cSGleb Smirnoff #endif
30375ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
3044faedfe8SSam Leffler 
305114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
306a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
307cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
308f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
309f3e7afe2SHans Petter Selasky static	int vlan_snd_tag_alloc(struct ifnet *,
310f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *, struct m_snd_tag **);
311f3e7afe2SHans Petter Selasky #endif
312d9b1d615SJohn Baldwin static	void vlan_qflush(struct ifnet *ifp);
3131cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
3141cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
3151cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
316f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
317d9b1d615SJohn Baldwin static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
3186f359e28SJohn Baldwin static	void vlan_unconfig(struct ifnet *ifp);
31928cc4d37SJohn Baldwin static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
32075ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
321a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
32275ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
32375ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
324f731f104SBill Paul 
325f941c31aSGleb Smirnoff static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
326f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
3276b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
328f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
329f889d2efSBrooks Davis 
3305cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
331ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
3325cb8c31aSYaroslav Tykhiy 
333d148c2a2SMatt Joras static  void vlan_lladdr_fn(void *arg, int pending);
334d148c2a2SMatt Joras 
33542a58907SGleb Smirnoff static struct if_clone *vlan_cloner;
3369d4fe4b2SBrooks Davis 
337ccf7ba97SMarko Zec #ifdef VIMAGE
33842a58907SGleb Smirnoff static VNET_DEFINE(struct if_clone *, vlan_cloner);
339ccf7ba97SMarko Zec #define	V_vlan_cloner	VNET(vlan_cloner)
340ccf7ba97SMarko Zec #endif
341ccf7ba97SMarko Zec 
34275ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
34375ee267cSGleb Smirnoff #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
344114c608cSYaroslav Tykhiy 
34575ee267cSGleb Smirnoff static void
34675ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
34775ee267cSGleb Smirnoff {
34875ee267cSGleb Smirnoff 	int i, n;
34975ee267cSGleb Smirnoff 
35075ee267cSGleb Smirnoff 	/*
35175ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
35275ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
35375ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
35475ee267cSGleb Smirnoff 	 */
35575ee267cSGleb Smirnoff 
35675ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
35775ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
35875ee267cSGleb Smirnoff 
35975ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
36075ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
36175ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
36275ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
36375ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
36475ee267cSGleb Smirnoff 		LIST_INIT(&trunk->hash[i]);
36575ee267cSGleb Smirnoff }
36675ee267cSGleb Smirnoff 
36775ee267cSGleb Smirnoff static void
36875ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
36975ee267cSGleb Smirnoff {
37075ee267cSGleb Smirnoff #ifdef INVARIANTS
37175ee267cSGleb Smirnoff 	int i;
37275ee267cSGleb Smirnoff 
37375ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
37475ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
37575ee267cSGleb Smirnoff 		KASSERT(LIST_EMPTY(&trunk->hash[i]),
37675ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
37775ee267cSGleb Smirnoff #endif
37875ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
37975ee267cSGleb Smirnoff 	trunk->hash = NULL;
38075ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
38175ee267cSGleb Smirnoff }
38275ee267cSGleb Smirnoff 
38375ee267cSGleb Smirnoff static int
38475ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
38575ee267cSGleb Smirnoff {
38675ee267cSGleb Smirnoff 	int i, b;
38775ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
38875ee267cSGleb Smirnoff 
389d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(trunk);
39075ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
39175ee267cSGleb Smirnoff 
39275ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
3937983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
39475ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
3957983103aSRobert Watson 		if (ifv->ifv_vid == ifv2->ifv_vid)
39675ee267cSGleb Smirnoff 			return (EEXIST);
39775ee267cSGleb Smirnoff 
39875ee267cSGleb Smirnoff 	/*
39975ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
40075ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
40175ee267cSGleb Smirnoff 	 * buckets/2.
40275ee267cSGleb Smirnoff 	 */
40375ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
40475ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
4057983103aSRobert Watson 		i = HASH(ifv->ifv_vid, trunk->hmask);
40675ee267cSGleb Smirnoff 	}
40775ee267cSGleb Smirnoff 	LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
40875ee267cSGleb Smirnoff 	trunk->refcnt++;
40975ee267cSGleb Smirnoff 
41075ee267cSGleb Smirnoff 	return (0);
41175ee267cSGleb Smirnoff }
41275ee267cSGleb Smirnoff 
41375ee267cSGleb Smirnoff static int
41475ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
41575ee267cSGleb Smirnoff {
41675ee267cSGleb Smirnoff 	int i, b;
41775ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
41875ee267cSGleb Smirnoff 
419d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(trunk);
42075ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
42175ee267cSGleb Smirnoff 
42275ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
4237983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
42475ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
42575ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
42675ee267cSGleb Smirnoff 			trunk->refcnt--;
42775ee267cSGleb Smirnoff 			LIST_REMOVE(ifv2, ifv_list);
42875ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
42975ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
43075ee267cSGleb Smirnoff 			return (0);
43175ee267cSGleb Smirnoff 		}
43275ee267cSGleb Smirnoff 
43375ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
43475ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
43575ee267cSGleb Smirnoff }
43675ee267cSGleb Smirnoff 
43775ee267cSGleb Smirnoff /*
43875ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
43975ee267cSGleb Smirnoff  */
44075ee267cSGleb Smirnoff static void
44175ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
44275ee267cSGleb Smirnoff {
44375ee267cSGleb Smirnoff 	struct ifvlan *ifv;
44475ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
44575ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
44675ee267cSGleb Smirnoff 
447d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(trunk);
44875ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
44975ee267cSGleb Smirnoff 
45075ee267cSGleb Smirnoff 	if (howmuch == 0) {
45175ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
45275ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
45375ee267cSGleb Smirnoff 		return;
45475ee267cSGleb Smirnoff 	}
45575ee267cSGleb Smirnoff 
45675ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
45775ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
45875ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
45975ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
46075ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
46175ee267cSGleb Smirnoff 		return;
46275ee267cSGleb Smirnoff 
46375ee267cSGleb Smirnoff 	/* M_NOWAIT because we're called with trunk mutex held */
464ac2fffa4SPedro F. Giffuni 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
46575ee267cSGleb Smirnoff 	if (hash2 == NULL) {
46675ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
46775ee267cSGleb Smirnoff 		    __func__);
46875ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
46975ee267cSGleb Smirnoff 	}
47075ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
47175ee267cSGleb Smirnoff 		LIST_INIT(&hash2[j]);
47275ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
473c0cb022bSYaroslav Tykhiy 		while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
47475ee267cSGleb Smirnoff 			LIST_REMOVE(ifv, ifv_list);
4757983103aSRobert Watson 			j = HASH(ifv->ifv_vid, n2 - 1);
47675ee267cSGleb Smirnoff 			LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
47775ee267cSGleb Smirnoff 		}
47875ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
47975ee267cSGleb Smirnoff 	trunk->hash = hash2;
48075ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
48175ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
482f84b2d69SYaroslav Tykhiy 
483f84b2d69SYaroslav Tykhiy 	if (bootverbose)
484f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
485f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
48675ee267cSGleb Smirnoff }
48775ee267cSGleb Smirnoff 
48875ee267cSGleb Smirnoff static __inline struct ifvlan *
4897983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
49075ee267cSGleb Smirnoff {
49175ee267cSGleb Smirnoff 	struct ifvlan *ifv;
49275ee267cSGleb Smirnoff 
493d148c2a2SMatt Joras 	TRUNK_RLOCK_ASSERT(trunk);
49475ee267cSGleb Smirnoff 
4957983103aSRobert Watson 	LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
4967983103aSRobert Watson 		if (ifv->ifv_vid == vid)
49775ee267cSGleb Smirnoff 			return (ifv);
49875ee267cSGleb Smirnoff 	return (NULL);
49975ee267cSGleb Smirnoff }
50075ee267cSGleb Smirnoff 
50175ee267cSGleb Smirnoff #if 0
50275ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
50375ee267cSGleb Smirnoff static void
50475ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
50575ee267cSGleb Smirnoff {
50675ee267cSGleb Smirnoff 	int i;
50775ee267cSGleb Smirnoff 	struct ifvlan *ifv;
50875ee267cSGleb Smirnoff 
50975ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
51075ee267cSGleb Smirnoff 		printf("%d: ", i);
51175ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
51275ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
51375ee267cSGleb Smirnoff 		printf("\n");
51475ee267cSGleb Smirnoff 	}
51575ee267cSGleb Smirnoff }
51675ee267cSGleb Smirnoff #endif /* 0 */
517e4cd31ddSJeff Roberson #else
518e4cd31ddSJeff Roberson 
519e4cd31ddSJeff Roberson static __inline struct ifvlan *
5207983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
521e4cd31ddSJeff Roberson {
522e4cd31ddSJeff Roberson 
5237983103aSRobert Watson 	return trunk->vlans[vid];
524e4cd31ddSJeff Roberson }
525e4cd31ddSJeff Roberson 
526e4cd31ddSJeff Roberson static __inline int
527e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
528e4cd31ddSJeff Roberson {
529e4cd31ddSJeff Roberson 
5307983103aSRobert Watson 	if (trunk->vlans[ifv->ifv_vid] != NULL)
531e4cd31ddSJeff Roberson 		return EEXIST;
5327983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = ifv;
533e4cd31ddSJeff Roberson 	trunk->refcnt++;
534e4cd31ddSJeff Roberson 
535e4cd31ddSJeff Roberson 	return (0);
536e4cd31ddSJeff Roberson }
537e4cd31ddSJeff Roberson 
538e4cd31ddSJeff Roberson static __inline int
539e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
540e4cd31ddSJeff Roberson {
541e4cd31ddSJeff Roberson 
5427983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = NULL;
543e4cd31ddSJeff Roberson 	trunk->refcnt--;
544e4cd31ddSJeff Roberson 
545e4cd31ddSJeff Roberson 	return (0);
546e4cd31ddSJeff Roberson }
547e4cd31ddSJeff Roberson 
548e4cd31ddSJeff Roberson static __inline void
549e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk)
550e4cd31ddSJeff Roberson {
551e4cd31ddSJeff Roberson }
552e4cd31ddSJeff Roberson 
553e4cd31ddSJeff Roberson static __inline void
554e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk)
555e4cd31ddSJeff Roberson {
556e4cd31ddSJeff Roberson }
557e4cd31ddSJeff Roberson 
55875ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
55975ee267cSGleb Smirnoff 
56075ee267cSGleb Smirnoff static void
56175ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
56275ee267cSGleb Smirnoff {
563d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
564d148c2a2SMatt Joras 	VLAN_WLOCK_ASSERT();
56575ee267cSGleb Smirnoff 
56675ee267cSGleb Smirnoff 	vlan_freehash(trunk);
56775ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
56833499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
5699bcf3ae4SAlexander Motin 	if_rele(trunk->parent);
57075ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
57175ee267cSGleb Smirnoff }
57275ee267cSGleb Smirnoff 
573f731f104SBill Paul /*
574f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
575f731f104SBill Paul  * programming the multicast filter of the parent. This has the
576f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
577f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
578f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
579f731f104SBill Paul  * to avoid this: there really is only one physical interface.
580f731f104SBill Paul  */
5812b120974SPeter Wemm static int
5822b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
583f731f104SBill Paul {
584f731f104SBill Paul 	struct ifnet		*ifp_p;
5852d222cb7SAlexander Motin 	struct ifmultiaddr	*ifma;
586f731f104SBill Paul 	struct ifvlan		*sc;
587c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
588f731f104SBill Paul 	int			error;
589f731f104SBill Paul 
590d148c2a2SMatt Joras 	/*
591d148c2a2SMatt Joras 	 * XXX This stupidly needs the rmlock to avoid sleeping while holding
592d148c2a2SMatt Joras 	 * the in6_multi_mtx (see in6_mc_join_locked).
593d148c2a2SMatt Joras 	 */
594d148c2a2SMatt Joras 	VLAN_RWLOCK_ASSERT();
595d148c2a2SMatt Joras 
596f731f104SBill Paul 	/* Find the parent. */
597f731f104SBill Paul 	sc = ifp->if_softc;
598d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(TRUNK(sc));
59975ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
6001b2a4f7aSBill Fenner 
6018b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
6028b615593SMarko Zec 
603f731f104SBill Paul 	/* First, remove any existing filter entries. */
604c0cb022bSYaroslav Tykhiy 	while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
605f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
6062d222cb7SAlexander Motin 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
6079d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
608f731f104SBill Paul 	}
609f731f104SBill Paul 
610f731f104SBill Paul 	/* Now program new ones. */
6112d222cb7SAlexander Motin 	IF_ADDR_WLOCK(ifp);
6126817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
613f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
614f731f104SBill Paul 			continue;
61529c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
6162d222cb7SAlexander Motin 		if (mc == NULL) {
6172d222cb7SAlexander Motin 			IF_ADDR_WUNLOCK(ifp);
61829c2dfbeSBruce M Simpson 			return (ENOMEM);
6192d222cb7SAlexander Motin 		}
620e4cd31ddSJeff Roberson 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
621e4cd31ddSJeff Roberson 		mc->mc_addr.sdl_index = ifp_p->if_index;
622f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
6232d222cb7SAlexander Motin 	}
6242d222cb7SAlexander Motin 	IF_ADDR_WUNLOCK(ifp);
6252d222cb7SAlexander Motin 	SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
626e4cd31ddSJeff Roberson 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
6272d222cb7SAlexander Motin 		    NULL);
628f731f104SBill Paul 		if (error)
629f731f104SBill Paul 			return (error);
630f731f104SBill Paul 	}
631f731f104SBill Paul 
6328b615593SMarko Zec 	CURVNET_RESTORE();
633f731f104SBill Paul 	return (0);
634f731f104SBill Paul }
6352cc2df49SGarrett Wollman 
636a3814acfSSam Leffler /*
637ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
638ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
639ea4ca115SAndrew Thompson  * should also change it on all children vlans.
640ea4ca115SAndrew Thompson  */
641ea4ca115SAndrew Thompson static void
642ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
643ea4ca115SAndrew Thompson {
644ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
645d148c2a2SMatt Joras 	struct ifnet *ifv_ifp;
646d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
647d148c2a2SMatt Joras 	struct sockaddr_dl *sdl;
648d148c2a2SMatt Joras 	VLAN_LOCK_READER;
649ea4ca115SAndrew Thompson 
650d148c2a2SMatt Joras 	/* Need the rmlock since this is run on taskqueue_swi. */
651d148c2a2SMatt Joras 	VLAN_RLOCK();
652d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
653d148c2a2SMatt Joras 	if (trunk == NULL) {
654d148c2a2SMatt Joras 		VLAN_RUNLOCK();
655ea4ca115SAndrew Thompson 		return;
656d148c2a2SMatt Joras 	}
657ea4ca115SAndrew Thompson 
658ea4ca115SAndrew Thompson 	/*
659ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
660d148c2a2SMatt Joras 	 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
661d148c2a2SMatt Joras 	 * ioctl calls on the parent garbling the lladdr of the child vlan.
662ea4ca115SAndrew Thompson 	 */
663d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
664d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
665d148c2a2SMatt Joras 		/*
666d148c2a2SMatt Joras 		 * Copy new new lladdr into the ifv_ifp, enqueue a task
667d148c2a2SMatt Joras 		 * to actually call if_setlladdr. if_setlladdr needs to
668d148c2a2SMatt Joras 		 * be deferred to a taskqueue because it will call into
669d148c2a2SMatt Joras 		 * the if_vlan ioctl path and try to acquire the global
670d148c2a2SMatt Joras 		 * lock.
671d148c2a2SMatt Joras 		 */
672d148c2a2SMatt Joras 		ifv_ifp = ifv->ifv_ifp;
673d148c2a2SMatt Joras 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
674e4cd31ddSJeff Roberson 		    ifp->if_addrlen);
675d148c2a2SMatt Joras 		sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
676d148c2a2SMatt Joras 		sdl->sdl_alen = ifp->if_addrlen;
677d148c2a2SMatt Joras 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
6786117727bSAndrew Thompson 	}
679d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
680d148c2a2SMatt Joras 	VLAN_RUNLOCK();
681ea4ca115SAndrew Thompson }
682ea4ca115SAndrew Thompson 
683ea4ca115SAndrew Thompson /*
6845cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
6855cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
6865cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
6875428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
6885428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
6895cb8c31aSYaroslav Tykhiy  */
6905cb8c31aSYaroslav Tykhiy static void
6915cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
6925cb8c31aSYaroslav Tykhiy {
6935cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
694d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
6955cb8c31aSYaroslav Tykhiy 
6965428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
6975428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
6985428776eSJohn Baldwin 		return;
699d148c2a2SMatt Joras 	VLAN_XLOCK();
700d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
701d148c2a2SMatt Joras 	if (trunk == NULL) {
702d148c2a2SMatt Joras 		VLAN_XUNLOCK();
703d148c2a2SMatt Joras 		return;
704d148c2a2SMatt Joras 	}
7055428776eSJohn Baldwin 
7065cb8c31aSYaroslav Tykhiy 	/*
7075cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
7085cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
7095cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
7105cb8c31aSYaroslav Tykhiy 	 */
711d148c2a2SMatt Joras 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
712d148c2a2SMatt Joras 	    ifp->if_vlantrunk == NULL)
71328cc4d37SJohn Baldwin 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
714d148c2a2SMatt Joras 
7155cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
7165cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
717d148c2a2SMatt Joras 	VLAN_XUNLOCK();
7185cb8c31aSYaroslav Tykhiy }
7195cb8c31aSYaroslav Tykhiy 
7205cb8c31aSYaroslav Tykhiy /*
721e4cd31ddSJeff Roberson  * Return the trunk device for a virtual interface.
722e4cd31ddSJeff Roberson  */
723e4cd31ddSJeff Roberson static struct ifnet  *
724e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp)
725e4cd31ddSJeff Roberson {
726e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
727d148c2a2SMatt Joras 	VLAN_LOCK_READER;
728e4cd31ddSJeff Roberson 
729e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
730e4cd31ddSJeff Roberson 		return (NULL);
731d148c2a2SMatt Joras 
732d148c2a2SMatt Joras 	/* Not clear if callers are sleepable, so acquire the rmlock. */
733d148c2a2SMatt Joras 	VLAN_RLOCK();
734e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
735e4cd31ddSJeff Roberson 	ifp = NULL;
736e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
737e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
738d148c2a2SMatt Joras 	VLAN_RUNLOCK();
739e4cd31ddSJeff Roberson 	return (ifp);
740e4cd31ddSJeff Roberson }
741e4cd31ddSJeff Roberson 
742e4cd31ddSJeff Roberson /*
7437983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
7447983103aSRobert Watson  * components such as Infiniband.
7457983103aSRobert Watson  *
7467983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
7477983103aSRobert Watson  * vlan_vid().
748e4cd31ddSJeff Roberson  */
749e4cd31ddSJeff Roberson static int
7507983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
751e4cd31ddSJeff Roberson {
752e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
753e4cd31ddSJeff Roberson 
754e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
755e4cd31ddSJeff Roberson 		return (EINVAL);
756e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
7577983103aSRobert Watson 	*vidp = ifv->ifv_vid;
758e4cd31ddSJeff Roberson 	return (0);
759e4cd31ddSJeff Roberson }
760e4cd31ddSJeff Roberson 
761e4cd31ddSJeff Roberson /*
762e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
763e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
764e4cd31ddSJeff Roberson  */
765e4cd31ddSJeff Roberson static void *
766e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
767e4cd31ddSJeff Roberson {
768e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
769e4cd31ddSJeff Roberson 
770e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
771e4cd31ddSJeff Roberson 		return (NULL);
772e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
773e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
774e4cd31ddSJeff Roberson }
775e4cd31ddSJeff Roberson 
776e4cd31ddSJeff Roberson /*
777e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
778e4cd31ddSJeff Roberson  * private per-instance data in.
779e4cd31ddSJeff Roberson  */
780e4cd31ddSJeff Roberson static int
781e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
782e4cd31ddSJeff Roberson {
783e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
784e4cd31ddSJeff Roberson 
785e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
786e4cd31ddSJeff Roberson 		return (EINVAL);
787e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
788e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
789e4cd31ddSJeff Roberson 	return (0);
790e4cd31ddSJeff Roberson }
791e4cd31ddSJeff Roberson 
792e4cd31ddSJeff Roberson /*
7937983103aSRobert Watson  * Return the vlan device present at the specific VID.
794e4cd31ddSJeff Roberson  */
795e4cd31ddSJeff Roberson static struct ifnet *
7967983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
797e4cd31ddSJeff Roberson {
798e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
799e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
800d148c2a2SMatt Joras 	VLAN_LOCK_READER;
801772b000fSAlexander V. Chernikov 	TRUNK_LOCK_READER;
802e4cd31ddSJeff Roberson 
803d148c2a2SMatt Joras 	/* Not clear if callers are sleepable, so acquire the rmlock. */
804d148c2a2SMatt Joras 	VLAN_RLOCK();
805e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
806d148c2a2SMatt Joras 	if (trunk == NULL) {
807d148c2a2SMatt Joras 		VLAN_RUNLOCK();
808e4cd31ddSJeff Roberson 		return (NULL);
809d148c2a2SMatt Joras 	}
810e4cd31ddSJeff Roberson 	ifp = NULL;
811e4cd31ddSJeff Roberson 	TRUNK_RLOCK(trunk);
8127983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
813e4cd31ddSJeff Roberson 	if (ifv)
814e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
815e4cd31ddSJeff Roberson 	TRUNK_RUNLOCK(trunk);
816d148c2a2SMatt Joras 	VLAN_RUNLOCK();
817e4cd31ddSJeff Roberson 	return (ifp);
818e4cd31ddSJeff Roberson }
819e4cd31ddSJeff Roberson 
820e4cd31ddSJeff Roberson /*
8212ccbbd06SMarcelo Araujo  * Recalculate the cached VLAN tag exposed via the MIB.
8222ccbbd06SMarcelo Araujo  */
8232ccbbd06SMarcelo Araujo static void
8242ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv)
8252ccbbd06SMarcelo Araujo {
8262ccbbd06SMarcelo Araujo 
8272ccbbd06SMarcelo Araujo        ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
8282ccbbd06SMarcelo Araujo }
8292ccbbd06SMarcelo Araujo 
8302ccbbd06SMarcelo Araujo /*
831a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
832a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
833a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
834a3814acfSSam Leffler  * set here.  No one else in the system should be aware of this so
835a3814acfSSam Leffler  * we use an explicit reference here.
836a3814acfSSam Leffler  */
837a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
838a3814acfSSam Leffler 
839984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
840a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
841127d7b2dSAndre Oppermann 
8422b120974SPeter Wemm static int
8432b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
8442b120974SPeter Wemm {
8459d4fe4b2SBrooks Davis 
8462b120974SPeter Wemm 	switch (type) {
8472b120974SPeter Wemm 	case MOD_LOAD:
8485cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
8495cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
8505cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
8515cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
852ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
853ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
854ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
855ea4ca115SAndrew Thompson 			return (ENOMEM);
856d148c2a2SMatt Joras 		VLAN_LOCKING_INIT();
8579d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
858127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
85975ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
860e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
861e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
862e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
863e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
864e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
865ccf7ba97SMarko Zec #ifndef VIMAGE
86642a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
86742a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
868ccf7ba97SMarko Zec #endif
86925c0f7b3SYaroslav Tykhiy 		if (bootverbose)
87025c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
87125c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
87225c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
87325c0f7b3SYaroslav Tykhiy #else
87425c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
87525c0f7b3SYaroslav Tykhiy #endif
87625c0f7b3SYaroslav Tykhiy 
87725c0f7b3SYaroslav Tykhiy 			       "\n");
8782b120974SPeter Wemm 		break;
8792b120974SPeter Wemm 	case MOD_UNLOAD:
880ccf7ba97SMarko Zec #ifndef VIMAGE
88142a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
882ccf7ba97SMarko Zec #endif
8835cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
884ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
8859d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
886127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
88775ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
888e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
889e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
89009fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
89109fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
892e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
893d148c2a2SMatt Joras 		VLAN_LOCKING_DESTROY();
89425c0f7b3SYaroslav Tykhiy 		if (bootverbose)
89525c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
8969d4fe4b2SBrooks Davis 		break;
8973e019deaSPoul-Henning Kamp 	default:
8983e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
8992b120974SPeter Wemm 	}
90015a66c21SBruce M Simpson 	return (0);
9012b120974SPeter Wemm }
9022b120974SPeter Wemm 
9032b120974SPeter Wemm static moduledata_t vlan_mod = {
9042b120974SPeter Wemm 	"if_vlan",
9052b120974SPeter Wemm 	vlan_modevent,
9069823d527SKevin Lo 	0
9072b120974SPeter Wemm };
9082b120974SPeter Wemm 
9092b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
91011edc477SEd Maste MODULE_VERSION(if_vlan, 3);
9112cc2df49SGarrett Wollman 
912ccf7ba97SMarko Zec #ifdef VIMAGE
913ccf7ba97SMarko Zec static void
914ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
915ccf7ba97SMarko Zec {
916ccf7ba97SMarko Zec 
91742a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
91842a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
919ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
920ccf7ba97SMarko Zec }
921ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
922ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
923ccf7ba97SMarko Zec 
924ccf7ba97SMarko Zec static void
925ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
926ccf7ba97SMarko Zec {
927ccf7ba97SMarko Zec 
92842a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
929ccf7ba97SMarko Zec }
93089856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
931ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
932ccf7ba97SMarko Zec #endif
933ccf7ba97SMarko Zec 
934f941c31aSGleb Smirnoff /*
935f941c31aSGleb Smirnoff  * Check for <etherif>.<vlan> style interface names.
936f941c31aSGleb Smirnoff  */
937f889d2efSBrooks Davis static struct ifnet *
938f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp)
9399d4fe4b2SBrooks Davis {
940f941c31aSGleb Smirnoff 	char ifname[IFNAMSIZ];
941f941c31aSGleb Smirnoff 	char *cp;
942f889d2efSBrooks Davis 	struct ifnet *ifp;
9437983103aSRobert Watson 	int vid;
944f889d2efSBrooks Davis 
945f941c31aSGleb Smirnoff 	strlcpy(ifname, name, IFNAMSIZ);
946f941c31aSGleb Smirnoff 	if ((cp = strchr(ifname, '.')) == NULL)
947f941c31aSGleb Smirnoff 		return (NULL);
948f941c31aSGleb Smirnoff 	*cp = '\0';
9499bcf3ae4SAlexander Motin 	if ((ifp = ifunit_ref(ifname)) == NULL)
950f941c31aSGleb Smirnoff 		return (NULL);
951f941c31aSGleb Smirnoff 	/* Parse VID. */
9529bcf3ae4SAlexander Motin 	if (*++cp == '\0') {
9539bcf3ae4SAlexander Motin 		if_rele(ifp);
954f941c31aSGleb Smirnoff 		return (NULL);
9559bcf3ae4SAlexander Motin 	}
9567983103aSRobert Watson 	vid = 0;
957fb92ad4aSJohn Baldwin 	for(; *cp >= '0' && *cp <= '9'; cp++)
9587983103aSRobert Watson 		vid = (vid * 10) + (*cp - '0');
9599bcf3ae4SAlexander Motin 	if (*cp != '\0') {
9609bcf3ae4SAlexander Motin 		if_rele(ifp);
961f941c31aSGleb Smirnoff 		return (NULL);
9629bcf3ae4SAlexander Motin 	}
9637983103aSRobert Watson 	if (vidp != NULL)
9647983103aSRobert Watson 		*vidp = vid;
965f889d2efSBrooks Davis 
96615a66c21SBruce M Simpson 	return (ifp);
967f889d2efSBrooks Davis }
968f889d2efSBrooks Davis 
969f889d2efSBrooks Davis static int
970f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
971f889d2efSBrooks Davis {
972f889d2efSBrooks Davis 	const char *cp;
973f889d2efSBrooks Davis 
974f941c31aSGleb Smirnoff 	if (vlan_clone_match_ethervid(name, NULL) != NULL)
975f889d2efSBrooks Davis 		return (1);
976f889d2efSBrooks Davis 
97742a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
978f889d2efSBrooks Davis 		return (0);
979f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
980f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
981f889d2efSBrooks Davis 			return (0);
982f889d2efSBrooks Davis 	}
983f889d2efSBrooks Davis 
984f889d2efSBrooks Davis 	return (1);
985f889d2efSBrooks Davis }
986f889d2efSBrooks Davis 
987f889d2efSBrooks Davis static int
9886b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
989f889d2efSBrooks Davis {
990f889d2efSBrooks Davis 	char *dp;
991f889d2efSBrooks Davis 	int wildcard;
992f889d2efSBrooks Davis 	int unit;
993f889d2efSBrooks Davis 	int error;
9947983103aSRobert Watson 	int vid;
9959d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
9969d4fe4b2SBrooks Davis 	struct ifnet *ifp;
997f889d2efSBrooks Davis 	struct ifnet *p;
9983ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
9993ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
10006b7330e2SSam Leffler 	struct vlanreq vlr;
100165239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
1002f889d2efSBrooks Davis 
10036b7330e2SSam Leffler 	/*
10046b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
10056b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
10066b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
10076b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
10086b7330e2SSam Leffler 	 *   must be configured separately.
10096b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
1010a4641f4eSPedro F. Giffuni 	 * supported for backwards compatibility.
10117983103aSRobert Watson 	 *
10127983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
10137983103aSRobert Watson 	 * called for.
10146b7330e2SSam Leffler 	 */
10156b7330e2SSam Leffler 	if (params) {
10166b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
10176b7330e2SSam Leffler 		if (error)
10186b7330e2SSam Leffler 			return error;
10199bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
10206b7330e2SSam Leffler 		if (p == NULL)
1021b1828acfSGleb Smirnoff 			return (ENXIO);
10226b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
10239bcf3ae4SAlexander Motin 		if (error != 0) {
10249bcf3ae4SAlexander Motin 			if_rele(p);
10256b7330e2SSam Leffler 			return (error);
10269bcf3ae4SAlexander Motin 		}
10277983103aSRobert Watson 		vid = vlr.vlr_tag;
10286b7330e2SSam Leffler 		wildcard = (unit < 0);
1029f941c31aSGleb Smirnoff 	} else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1030f889d2efSBrooks Davis 		unit = -1;
1031f889d2efSBrooks Davis 		wildcard = 0;
1032f889d2efSBrooks Davis 	} else {
10339bcf3ae4SAlexander Motin 		p = NULL;
1034f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
1035f889d2efSBrooks Davis 		if (error != 0)
1036f889d2efSBrooks Davis 			return (error);
1037f889d2efSBrooks Davis 
1038f889d2efSBrooks Davis 		wildcard = (unit < 0);
1039f889d2efSBrooks Davis 	}
1040f889d2efSBrooks Davis 
1041f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
10429bcf3ae4SAlexander Motin 	if (error != 0) {
10439bcf3ae4SAlexander Motin 		if (p != NULL)
10449bcf3ae4SAlexander Motin 			if_rele(p);
1045f889d2efSBrooks Davis 		return (error);
10469bcf3ae4SAlexander Motin 	}
1047f889d2efSBrooks Davis 
1048f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
1049f889d2efSBrooks Davis 	if (wildcard) {
1050f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
1051f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1052f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
1053f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
1054f889d2efSBrooks Davis 		}
1055f889d2efSBrooks Davis 	}
10569d4fe4b2SBrooks Davis 
1057a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1058fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1059fc74a9f9SBrooks Davis 	if (ifp == NULL) {
1060fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
1061fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
10629bcf3ae4SAlexander Motin 		if (p != NULL)
10639bcf3ae4SAlexander Motin 			if_rele(p);
1064fc74a9f9SBrooks Davis 		return (ENOSPC);
1065fc74a9f9SBrooks Davis 	}
10669d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
10679d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
1068f889d2efSBrooks Davis 	/*
1069cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
1070f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
1071f889d2efSBrooks Davis 	 */
1072f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
107342a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
1074f889d2efSBrooks Davis 	ifp->if_dunit = unit;
10759d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
10769d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
107715a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
10789d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
10799d4fe4b2SBrooks Davis 
1080114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
1081d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
1082d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
10839d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
1084f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1085f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1086f3e7afe2SHans Petter Selasky #endif
108764a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
1088fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
10899d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
1090211f625aSBill Fenner 	ifp->if_baudrate = 0;
1091a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
1092a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
10933ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
10943ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
10953ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
10969d4fe4b2SBrooks Davis 
10979bcf3ae4SAlexander Motin 	if (p != NULL) {
10987983103aSRobert Watson 		error = vlan_config(ifv, p, vid);
10999bcf3ae4SAlexander Motin 		if_rele(p);
1100f889d2efSBrooks Davis 		if (error != 0) {
1101f889d2efSBrooks Davis 			/*
110228cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
1103f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
1104f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1105f889d2efSBrooks Davis 			 */
1106f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1107249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
11084b22573aSBrooks Davis 			if_free(ifp);
110996c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
1110f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1111f889d2efSBrooks Davis 
1112f889d2efSBrooks Davis 			return (error);
1113f889d2efSBrooks Davis 		}
1114f889d2efSBrooks Davis 	}
1115f889d2efSBrooks Davis 
11169d4fe4b2SBrooks Davis 	return (0);
11179d4fe4b2SBrooks Davis }
11189d4fe4b2SBrooks Davis 
1119f889d2efSBrooks Davis static int
1120f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
11219d4fe4b2SBrooks Davis {
11229d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1123114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
1124b4e9f837SBrooks Davis 
1125249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1126249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1127d148c2a2SMatt Joras 	/*
1128d148c2a2SMatt Joras 	 * We should have the only reference to the ifv now, so we can now
1129d148c2a2SMatt Joras 	 * drain any remaining lladdr task before freeing the ifnet and the
1130d148c2a2SMatt Joras 	 * ifvlan.
1131d148c2a2SMatt Joras 	 */
1132d148c2a2SMatt Joras 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
11334b22573aSBrooks Davis 	if_free(ifp);
11349d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1135b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
1136b4e9f837SBrooks Davis 
1137f889d2efSBrooks Davis 	return (0);
11389d4fe4b2SBrooks Davis }
11399d4fe4b2SBrooks Davis 
114015a66c21SBruce M Simpson /*
114115a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
114215a66c21SBruce M Simpson  */
11432cc2df49SGarrett Wollman static void
1144114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
11452cc2df49SGarrett Wollman {
11462cc2df49SGarrett Wollman }
11472cc2df49SGarrett Wollman 
11486d3a3ab7SGleb Smirnoff /*
1149d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
11506d3a3ab7SGleb Smirnoff  */
1151d9b1d615SJohn Baldwin static int
1152d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
11532cc2df49SGarrett Wollman {
11542cc2df49SGarrett Wollman 	struct ifvlan *ifv;
11552cc2df49SGarrett Wollman 	struct ifnet *p;
11561ad7a257SPyun YongHyeon 	int error, len, mcast;
1157d148c2a2SMatt Joras 	VLAN_LOCK_READER;
11582cc2df49SGarrett Wollman 
1159d148c2a2SMatt Joras 	VLAN_RLOCK();
11602cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
1161d148c2a2SMatt Joras 	if (TRUNK(ifv) == NULL) {
1162d148c2a2SMatt Joras 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1163d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1164d148c2a2SMatt Joras 		m_freem(m);
1165d148c2a2SMatt Joras 		return (ENETDOWN);
1166d148c2a2SMatt Joras 	}
116775ee267cSGleb Smirnoff 	p = PARENT(ifv);
11681ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
11691ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
11702cc2df49SGarrett Wollman 
1171a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
11722cc2df49SGarrett Wollman 
1173f731f104SBill Paul 	/*
1174d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
117524993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
117624993214SYaroslav Tykhiy 	 */
11772dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
1178a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1179d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1180d148c2a2SMatt Joras 		m_freem(m);
11818b20f6cfSHiroki Sato 		return (ENETDOWN);
118224993214SYaroslav Tykhiy 	}
118324993214SYaroslav Tykhiy 
1184f1379734SKonstantin Belousov 	if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1185a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1186d148c2a2SMatt Joras 		VLAN_RUNLOCK();
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);
1200d148c2a2SMatt Joras 	VLAN_RUNLOCK();
1201d9b1d615SJohn Baldwin 	return (error);
12022cc2df49SGarrett Wollman }
1203d9b1d615SJohn Baldwin 
1204d9b1d615SJohn Baldwin /*
1205d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1206d9b1d615SJohn Baldwin  */
1207d9b1d615SJohn Baldwin static void
1208d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1209d9b1d615SJohn Baldwin {
1210f731f104SBill Paul }
1211f731f104SBill Paul 
1212a3814acfSSam Leffler static void
1213a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1214f731f104SBill Paul {
1215d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1216f731f104SBill Paul 	struct ifvlan *ifv;
1217d148c2a2SMatt Joras 	VLAN_LOCK_READER;
1218772b000fSAlexander V. Chernikov 	TRUNK_LOCK_READER;
12192ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
12202ccbbd06SMarcelo Araujo 	uint16_t vid, tag;
122175ee267cSGleb Smirnoff 
1222d148c2a2SMatt Joras 	VLAN_RLOCK();
1223d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1224d148c2a2SMatt Joras 	if (trunk == NULL) {
1225d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1226d148c2a2SMatt Joras 		m_freem(m);
1227d148c2a2SMatt Joras 		return;
1228d148c2a2SMatt Joras 	}
1229a3814acfSSam Leffler 
1230f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1231a3814acfSSam Leffler 		/*
123214e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1233a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1234a3814acfSSam Leffler 		 */
12352ccbbd06SMarcelo Araujo 		tag = m->m_pkthdr.ether_vtag;
12366ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1237a3814acfSSam Leffler 	} else {
123875ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
123975ee267cSGleb Smirnoff 
124014e98256SYaroslav Tykhiy 		/*
124114e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
124214e98256SYaroslav Tykhiy 		 */
1243a3814acfSSam Leffler 		switch (ifp->if_type) {
1244a3814acfSSam Leffler 		case IFT_ETHER:
1245a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1246a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1247a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1248d148c2a2SMatt Joras 				VLAN_RUNLOCK();
1249a3814acfSSam Leffler 				return;
1250a3814acfSSam Leffler 			}
1251a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
12522ccbbd06SMarcelo Araujo 			tag = ntohs(evl->evl_tag);
1253db8b5973SYaroslav Tykhiy 
1254db8b5973SYaroslav Tykhiy 			/*
12552dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
12562dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
12572dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
12582dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1259db8b5973SYaroslav Tykhiy 			 */
12602dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
12612dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
12622dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1263a3814acfSSam Leffler 			break;
12642dc879b3SYaroslav Tykhiy 
1265a3814acfSSam Leffler 		default:
1266db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
126760c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
126860c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1269a3814acfSSam Leffler #endif
12703751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1271d148c2a2SMatt Joras 			VLAN_RUNLOCK();
1272d148c2a2SMatt Joras 			m_freem(m);
127360c60618SYaroslav Tykhiy 			return;
1274a3814acfSSam Leffler 		}
12757a46ec8fSBrooks Davis 	}
12767a46ec8fSBrooks Davis 
12772ccbbd06SMarcelo Araujo 	vid = EVL_VLANOFTAG(tag);
12782ccbbd06SMarcelo Araujo 
127915ed2fa1SYaroslav Tykhiy 	TRUNK_RLOCK(trunk);
12807983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
12812dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
128275ee267cSGleb Smirnoff 		TRUNK_RUNLOCK(trunk);
12833751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1284d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1285d148c2a2SMatt Joras 		m_freem(m);
128675ee267cSGleb Smirnoff 		return;
128775ee267cSGleb Smirnoff 	}
128875ee267cSGleb Smirnoff 	TRUNK_RUNLOCK(trunk);
1289f731f104SBill Paul 
12902ccbbd06SMarcelo Araujo 	if (vlan_mtag_pcp) {
12912ccbbd06SMarcelo Araujo 		/*
12922ccbbd06SMarcelo Araujo 		 * While uncommon, it is possible that we will find a 802.1q
12932ccbbd06SMarcelo Araujo 		 * packet encapsulated inside another packet that also had an
12942ccbbd06SMarcelo Araujo 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
12952ccbbd06SMarcelo Araujo 		 * arriving over ethernet.  In that case, we replace the
12962ccbbd06SMarcelo Araujo 		 * existing 802.1q PCP m_tag value.
12972ccbbd06SMarcelo Araujo 		 */
12982ccbbd06SMarcelo Araujo 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
12992ccbbd06SMarcelo Araujo 		if (mtag == NULL) {
13002ccbbd06SMarcelo Araujo 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
13012ccbbd06SMarcelo Araujo 			    sizeof(uint8_t), M_NOWAIT);
13022ccbbd06SMarcelo Araujo 			if (mtag == NULL) {
13032ccbbd06SMarcelo Araujo 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1304d148c2a2SMatt Joras 				VLAN_RUNLOCK();
1305d148c2a2SMatt Joras 				m_freem(m);
13062ccbbd06SMarcelo Araujo 				return;
13072ccbbd06SMarcelo Araujo 			}
13082ccbbd06SMarcelo Araujo 			m_tag_prepend(m, mtag);
13092ccbbd06SMarcelo Araujo 		}
13102ccbbd06SMarcelo Araujo 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
13112ccbbd06SMarcelo Araujo 	}
13122ccbbd06SMarcelo Araujo 
1313fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1314c0304424SGleb Smirnoff 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1315d148c2a2SMatt Joras 	VLAN_RUNLOCK();
13162cc2df49SGarrett Wollman 
1317a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1318fdbf1174SMatt Joras 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
13192cc2df49SGarrett Wollman }
13202cc2df49SGarrett Wollman 
1321d148c2a2SMatt Joras static void
1322d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused)
1323d148c2a2SMatt Joras {
1324d148c2a2SMatt Joras 	struct ifvlan *ifv;
1325d148c2a2SMatt Joras 	struct ifnet *ifp;
1326d148c2a2SMatt Joras 
1327d148c2a2SMatt Joras 	ifv = (struct ifvlan *)arg;
1328d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
1329d148c2a2SMatt Joras 	/* The ifv_ifp already has the lladdr copied in. */
1330d148c2a2SMatt Joras 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1331d148c2a2SMatt Joras }
1332d148c2a2SMatt Joras 
13332cc2df49SGarrett Wollman static int
13347983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
13352cc2df49SGarrett Wollman {
133675ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
13371cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
133875ee267cSGleb Smirnoff 	int error = 0;
13392cc2df49SGarrett Wollman 
1340b1828acfSGleb Smirnoff 	/*
1341b1828acfSGleb Smirnoff 	 * We can handle non-ethernet hardware types as long as
1342b1828acfSGleb Smirnoff 	 * they handle the tagging and headers themselves.
1343b1828acfSGleb Smirnoff 	 */
1344e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1345e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
134615a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
134764a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
134864a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
1349b1828acfSGleb Smirnoff 	/*
1350b1828acfSGleb Smirnoff 	 * Don't let the caller set up a VLAN VID with
1351b1828acfSGleb Smirnoff 	 * anything except VLID bits.
1352b1828acfSGleb Smirnoff 	 * VID numbers 0x0 and 0xFFF are reserved.
1353b1828acfSGleb Smirnoff 	 */
1354b1828acfSGleb Smirnoff 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1355b1828acfSGleb Smirnoff 		return (EINVAL);
135675ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
135715a66c21SBruce M Simpson 		return (EBUSY);
13582cc2df49SGarrett Wollman 
1359d148c2a2SMatt Joras 	/* Acquire rmlock after the branch so we can M_WAITOK. */
1360d148c2a2SMatt Joras 	VLAN_XLOCK();
136175ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
136275ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
136375ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
136475ee267cSGleb Smirnoff 		vlan_inithash(trunk);
136575ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
1366d148c2a2SMatt Joras 		VLAN_WLOCK();
1367d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
136875ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
136975ee267cSGleb Smirnoff 		trunk->parent = p;
13709bcf3ae4SAlexander Motin 		if_ref(trunk->parent);
137175ee267cSGleb Smirnoff 	} else {
1372d148c2a2SMatt Joras 		VLAN_WLOCK();
137375ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
1374d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
137575ee267cSGleb Smirnoff 	}
137675ee267cSGleb Smirnoff 
13777983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
13782ccbbd06SMarcelo Araujo 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
13792ccbbd06SMarcelo Araujo 	vlan_tag_recalculate(ifv);
138075ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
138175ee267cSGleb Smirnoff 	if (error)
138275ee267cSGleb Smirnoff 		goto done;
138373f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1384a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1385a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
13861cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1387d89baa5aSAlexander Motin 	ifv->ifv_capenable = -1;
1388a3814acfSSam Leffler 
1389a3814acfSSam Leffler 	/*
1390a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1391a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1392656acce4SYaroslav Tykhiy 	 * use it.
1393a3814acfSSam Leffler 	 */
1394656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1395656acce4SYaroslav Tykhiy 		/*
1396656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1397656acce4SYaroslav Tykhiy 		 * handle extended frames.
1398656acce4SYaroslav Tykhiy 		 */
1399a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1400656acce4SYaroslav Tykhiy 	} else {
1401a3814acfSSam Leffler 		/*
1402a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1403a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1404a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1405a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1406a3814acfSSam Leffler 		 * which might still be useful.
1407a3814acfSSam Leffler 		 */
1408a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1409a3814acfSSam Leffler 	}
1410a3814acfSSam Leffler 
141175ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
14121cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1413e4cd31ddSJeff Roberson 	/*
1414e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1415e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1416e4cd31ddSJeff Roberson 	 * interfaces to also work.
1417e4cd31ddSJeff Roberson 	 */
14181cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
141975ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1420e4cd31ddSJeff Roberson 	ifp->if_output = p->if_output;
1421e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1422e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1423e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1424e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
1425e4cd31ddSJeff Roberson 
14262cc2df49SGarrett Wollman 	/*
142724993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
142824993214SYaroslav Tykhiy 	 * Other flags are none of our business.
14292cc2df49SGarrett Wollman 	 */
143064a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
14311cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
14321cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
14331cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
14341cf236fbSYaroslav Tykhiy 
14351cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
14362cc2df49SGarrett Wollman 
143775ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1438a3814acfSSam Leffler 
1439a3814acfSSam Leffler 	/*
1440e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
14412cc2df49SGarrett Wollman 	 * physical interface's.
14422cc2df49SGarrett Wollman 	 */
1443e4cd31ddSJeff Roberson 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1444e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1445e4cd31ddSJeff Roberson 	    p->if_addrlen;
14461b2a4f7aSBill Fenner 
14471b2a4f7aSBill Fenner 	/*
14481b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
14491b2a4f7aSBill Fenner 	 * joined on the vlan device.
14501b2a4f7aSBill Fenner 	 */
1451d148c2a2SMatt Joras 	(void)vlan_setmulti(ifp);
1452d148c2a2SMatt Joras 
1453d148c2a2SMatt Joras 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
14542ada9747SYaroslav Tykhiy 
14552ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
14562ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1457d148c2a2SMatt Joras 
1458d148c2a2SMatt Joras 	/* Update flags on the parent, if necessary. */
1459d148c2a2SMatt Joras 	vlan_setflags(ifp, 1);
146075ee267cSGleb Smirnoff done:
1461d148c2a2SMatt Joras 	/*
1462d148c2a2SMatt Joras 	 * We need to drop the non-sleepable rmlock so that the underlying
1463d148c2a2SMatt Joras 	 * devices can sleep in their vlan_config hooks.
1464d148c2a2SMatt Joras 	 */
1465d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1466d148c2a2SMatt Joras 	VLAN_WUNLOCK();
1467c725524cSJack F Vogel 	if (error == 0)
14687983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1469d148c2a2SMatt Joras 	VLAN_XUNLOCK();
147075ee267cSGleb Smirnoff 
147175ee267cSGleb Smirnoff 	return (error);
14722cc2df49SGarrett Wollman }
14732cc2df49SGarrett Wollman 
14746f359e28SJohn Baldwin static void
1475f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1476f731f104SBill Paul {
14775cb8c31aSYaroslav Tykhiy 
1478d148c2a2SMatt Joras 	VLAN_XLOCK();
147928cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
1480d148c2a2SMatt Joras 	VLAN_XUNLOCK();
14815cb8c31aSYaroslav Tykhiy }
14825cb8c31aSYaroslav Tykhiy 
14836f359e28SJohn Baldwin static void
148428cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
14855cb8c31aSYaroslav Tykhiy {
148675ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1487f731f104SBill Paul 	struct vlan_mc_entry *mc;
1488f731f104SBill Paul 	struct ifvlan *ifv;
1489c725524cSJack F Vogel 	struct ifnet  *parent;
149028cc4d37SJohn Baldwin 	int error;
1491f731f104SBill Paul 
1492d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
14934faedfe8SSam Leffler 
1494f731f104SBill Paul 	ifv = ifp->if_softc;
149575ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
149622893351SJack F Vogel 	parent = NULL;
1497f731f104SBill Paul 
149822893351SJack F Vogel 	if (trunk != NULL) {
1499d148c2a2SMatt Joras 		/*
1500d148c2a2SMatt Joras 		 * Both vlan_transmit and vlan_input rely on the trunk fields
1501d148c2a2SMatt Joras 		 * being NULL to determine whether to bail, so we need to get
1502d148c2a2SMatt Joras 		 * an exclusive lock here to prevent them from using bad
1503d148c2a2SMatt Joras 		 * ifvlans.
1504d148c2a2SMatt Joras 		 */
1505d148c2a2SMatt Joras 		VLAN_WLOCK();
150622893351SJack F Vogel 		parent = trunk->parent;
15071b2a4f7aSBill Fenner 
1508f731f104SBill Paul 		/*
1509f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1510f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
15111b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1512f731f104SBill Paul 		 */
1513c0cb022bSYaroslav Tykhiy 		while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
15146f359e28SJohn Baldwin 			/*
151528cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1516b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
151728cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
151828cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
151928cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
152028cc4d37SJohn Baldwin 			 * succeed.
15216f359e28SJohn Baldwin 			 */
152228cc4d37SJohn Baldwin 			if (!departing) {
152328cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1524e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
152528cc4d37SJohn Baldwin 				if (error)
152628cc4d37SJohn Baldwin 					if_printf(ifp,
152728cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
152828cc4d37SJohn Baldwin 					    error);
152928cc4d37SJohn Baldwin 			}
1530f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
15319d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
1532f731f104SBill Paul 		}
1533a3814acfSSam Leffler 
15341cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1535d148c2a2SMatt Joras 
1536d148c2a2SMatt Joras 		/*
1537d148c2a2SMatt Joras 		 * The trunk lock isn't actually required here, but
1538d148c2a2SMatt Joras 		 * vlan_remhash expects it.
1539d148c2a2SMatt Joras 		 */
1540d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
154175ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
1542d148c2a2SMatt Joras 		TRUNK_WUNLOCK(trunk);
154375ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
154475ee267cSGleb Smirnoff 
154575ee267cSGleb Smirnoff 		/*
154675ee267cSGleb Smirnoff 		 * Check if we were the last.
154775ee267cSGleb Smirnoff 		 */
154875ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
15492d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
155075ee267cSGleb Smirnoff 			trunk_destroy(trunk);
1551d148c2a2SMatt Joras 		}
1552d148c2a2SMatt Joras 		VLAN_WUNLOCK();
15531b2a4f7aSBill Fenner 	}
1554f731f104SBill Paul 
1555f731f104SBill Paul 	/* Disconnect from parent. */
15561cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
15571cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
15585cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
15595cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
15605cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1561f731f104SBill Paul 
156222893351SJack F Vogel 	/*
156322893351SJack F Vogel 	 * Only dispatch an event if vlan was
156422893351SJack F Vogel 	 * attached, otherwise there is nothing
156522893351SJack F Vogel 	 * to cleanup anyway.
156622893351SJack F Vogel 	 */
156722893351SJack F Vogel 	if (parent != NULL)
15687983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1569f731f104SBill Paul }
1570f731f104SBill Paul 
15711cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1572f731f104SBill Paul static int
15731cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
15741cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1575a3814acfSSam Leffler {
15761cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
15771cf236fbSYaroslav Tykhiy 	int error;
1578a3814acfSSam Leffler 
1579d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1580a3814acfSSam Leffler 
15811cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
15821cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
15831cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
15841cf236fbSYaroslav Tykhiy 
15851cf236fbSYaroslav Tykhiy 	/*
15861cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
15871cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
15881cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
15891cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
15901cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
15911cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
15921cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
15931cf236fbSYaroslav Tykhiy 	 */
15941cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
159575ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
15961cf236fbSYaroslav Tykhiy 		if (error)
1597a3814acfSSam Leffler 			return (error);
15981cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
15991cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
16001cf236fbSYaroslav Tykhiy 	}
16011cf236fbSYaroslav Tykhiy 	return (0);
16021cf236fbSYaroslav Tykhiy }
16031cf236fbSYaroslav Tykhiy 
16041cf236fbSYaroslav Tykhiy /*
16051cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
16061cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
16071cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
16081cf236fbSYaroslav Tykhiy  */
16091cf236fbSYaroslav Tykhiy static int
16101cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
16111cf236fbSYaroslav Tykhiy {
16121cf236fbSYaroslav Tykhiy 	int error, i;
16131cf236fbSYaroslav Tykhiy 
16141cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
16151cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
16161cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
16171cf236fbSYaroslav Tykhiy 		if (error)
16181cf236fbSYaroslav Tykhiy 			return (error);
16191cf236fbSYaroslav Tykhiy 	}
16201cf236fbSYaroslav Tykhiy 	return (0);
1621a3814acfSSam Leffler }
1622a3814acfSSam Leffler 
1623127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1624127d7b2dSAndre Oppermann static void
1625a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1626127d7b2dSAndre Oppermann {
1627d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1628127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
1629d148c2a2SMatt Joras 	VLAN_LOCK_READER;
1630127d7b2dSAndre Oppermann 
1631d148c2a2SMatt Joras 	/* Called from a taskqueue_swi task, so we cannot sleep. */
1632d148c2a2SMatt Joras 	VLAN_RLOCK();
1633d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1634d148c2a2SMatt Joras 	if (trunk == NULL) {
1635d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1636d148c2a2SMatt Joras 		return;
1637d148c2a2SMatt Joras 	}
1638d148c2a2SMatt Joras 
1639d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1640d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
1641aad0be7aSGleb Smirnoff 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1642fc74a9f9SBrooks Davis 		if_link_state_change(ifv->ifv_ifp,
164375ee267cSGleb Smirnoff 		    trunk->parent->if_link_state);
1644127d7b2dSAndre Oppermann 	}
1645d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1646d148c2a2SMatt Joras 	VLAN_RUNLOCK();
164775ee267cSGleb Smirnoff }
164875ee267cSGleb Smirnoff 
164975ee267cSGleb Smirnoff static void
165075ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
165175ee267cSGleb Smirnoff {
1652d148c2a2SMatt Joras 	struct ifnet *p;
1653d148c2a2SMatt Joras 	struct ifnet *ifp;
16549fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
1655d89baa5aSAlexander Motin 	int cap = 0, ena = 0, mena;
1656d89baa5aSAlexander Motin 	u_long hwa = 0;
165775ee267cSGleb Smirnoff 
1658d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1659d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(TRUNK(ifv));
1660d148c2a2SMatt Joras 	p = PARENT(ifv);
1661d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
166275ee267cSGleb Smirnoff 
1663d89baa5aSAlexander Motin 	/* Mask parent interface enabled capabilities disabled by user. */
1664d89baa5aSAlexander Motin 	mena = p->if_capenable & ifv->ifv_capenable;
1665d89baa5aSAlexander Motin 
166675ee267cSGleb Smirnoff 	/*
166775ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
166875ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
166975ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
167075ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
167175ee267cSGleb Smirnoff 	 */
167275ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1673d89baa5aSAlexander Motin 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
167475ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
167575ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1676d89baa5aSAlexander Motin 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1677d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM)
1678d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1679d89baa5aSAlexander Motin 			    CSUM_UDP | CSUM_SCTP);
1680d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM_IPV6)
1681d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1682d89baa5aSAlexander Motin 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
168375ee267cSGleb Smirnoff 	}
1684d89baa5aSAlexander Motin 
16859b76d9cbSPyun YongHyeon 	/*
16869b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
16879b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
16889b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
16899b76d9cbSPyun YongHyeon 	 */
16909fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
16919fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
16929fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
16939b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1694d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TSO;
16959b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1696d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TSO;
1697d89baa5aSAlexander Motin 		if (ena & IFCAP_TSO)
1698d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & CSUM_TSO;
16999b76d9cbSPyun YongHyeon 	}
170009fe6320SNavdeep Parhar 
170109fe6320SNavdeep Parhar 	/*
170259150e91SAlexander Motin 	 * If the parent interface can do LRO and checksum offloading on
170359150e91SAlexander Motin 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
170459150e91SAlexander Motin 	 * cost nothing, while false negative may lead to some confusions.
170559150e91SAlexander Motin 	 */
170659150e91SAlexander Motin 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
170759150e91SAlexander Motin 		cap |= p->if_capabilities & IFCAP_LRO;
170859150e91SAlexander Motin 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
170959150e91SAlexander Motin 		ena |= p->if_capenable & IFCAP_LRO;
171059150e91SAlexander Motin 
171159150e91SAlexander Motin 	/*
171209fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
171309fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
171409fe6320SNavdeep Parhar 	 *
171509fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
171609fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
171709fe6320SNavdeep Parhar 	 * with its own bit.
171809fe6320SNavdeep Parhar 	 */
171909fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
172009fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1721d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TOE;
172209fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
172309fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
1724d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TOE;
172509fe6320SNavdeep Parhar 	}
1726f3e7afe2SHans Petter Selasky 
1727d89baa5aSAlexander Motin 	/*
1728d89baa5aSAlexander Motin 	 * If the parent interface supports dynamic link state, so does the
1729d89baa5aSAlexander Motin 	 * VLAN interface.
1730d89baa5aSAlexander Motin 	 */
1731d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1732d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_LINKSTATE);
1733d89baa5aSAlexander Motin 
1734f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1735f3e7afe2SHans Petter Selasky 	/*
1736f3e7afe2SHans Petter Selasky 	 * If the parent interface supports ratelimiting, so does the
1737f3e7afe2SHans Petter Selasky 	 * VLAN interface.
1738f3e7afe2SHans Petter Selasky 	 */
1739d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1740d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_TXRTLMT);
1741f3e7afe2SHans Petter Selasky #endif
1742d89baa5aSAlexander Motin 
1743d89baa5aSAlexander Motin 	ifp->if_capabilities = cap;
1744d89baa5aSAlexander Motin 	ifp->if_capenable = ena;
1745d89baa5aSAlexander Motin 	ifp->if_hwassist = hwa;
174675ee267cSGleb Smirnoff }
174775ee267cSGleb Smirnoff 
174875ee267cSGleb Smirnoff static void
174975ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
175075ee267cSGleb Smirnoff {
1751d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
175275ee267cSGleb Smirnoff 	struct ifvlan *ifv;
175375ee267cSGleb Smirnoff 
1754d148c2a2SMatt Joras 	VLAN_SLOCK();
1755d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1756d148c2a2SMatt Joras 	if (trunk == NULL) {
1757d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1758d148c2a2SMatt Joras 		return;
1759d148c2a2SMatt Joras 	}
1760d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1761d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
176275ee267cSGleb Smirnoff 		vlan_capabilities(ifv);
176375ee267cSGleb Smirnoff 	}
1764d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1765d148c2a2SMatt Joras 	VLAN_SUNLOCK();
1766127d7b2dSAndre Oppermann }
1767127d7b2dSAndre Oppermann 
1768a3814acfSSam Leffler static int
1769cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
17702cc2df49SGarrett Wollman {
17712cc2df49SGarrett Wollman 	struct ifnet *p;
17722cc2df49SGarrett Wollman 	struct ifreq *ifr;
1773e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
17742cc2df49SGarrett Wollman 	struct ifvlan *ifv;
17752d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
17762cc2df49SGarrett Wollman 	struct vlanreq vlr;
17772cc2df49SGarrett Wollman 	int error = 0;
1778d148c2a2SMatt Joras 	VLAN_LOCK_READER;
17792cc2df49SGarrett Wollman 
17802cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1781e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
17822cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
17832cc2df49SGarrett Wollman 
17842cc2df49SGarrett Wollman 	switch (cmd) {
1785e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1786e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1787e4cd31ddSJeff Roberson #ifdef INET
1788e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1789e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1790e4cd31ddSJeff Roberson #endif
1791e4cd31ddSJeff Roberson 		break;
1792e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
179338d958a6SBrooks Davis 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
179438d958a6SBrooks Davis 		    ifp->if_addrlen);
1795e4cd31ddSJeff Roberson 		break;
1796b3cca108SBill Fenner 	case SIOCGIFMEDIA:
1797d148c2a2SMatt Joras 		VLAN_SLOCK();
179875ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1799d8564efdSEd Maste 			p = PARENT(ifv);
18009bcf3ae4SAlexander Motin 			if_ref(p);
1801d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
18029bcf3ae4SAlexander Motin 			if_rele(p);
1803b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1804b3cca108SBill Fenner 			if (error == 0) {
1805b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1806b3cca108SBill Fenner 
1807b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1808b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1809b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1810b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1811b3cca108SBill Fenner 						ifmr->ifm_ulist,
1812b3cca108SBill Fenner 						sizeof(int));
1813b3cca108SBill Fenner 				}
1814b3cca108SBill Fenner 			}
18154faedfe8SSam Leffler 		} else {
1816b3cca108SBill Fenner 			error = EINVAL;
18174faedfe8SSam Leffler 		}
1818d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1819b3cca108SBill Fenner 		break;
1820b3cca108SBill Fenner 
1821b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1822b3cca108SBill Fenner 		error = EINVAL;
1823b3cca108SBill Fenner 		break;
1824b3cca108SBill Fenner 
18252cc2df49SGarrett Wollman 	case SIOCSIFMTU:
18262cc2df49SGarrett Wollman 		/*
18272cc2df49SGarrett Wollman 		 * Set the interface MTU.
18282cc2df49SGarrett Wollman 		 */
1829d148c2a2SMatt Joras 		VLAN_SLOCK();
1830d148c2a2SMatt Joras 		trunk = TRUNK(ifv);
1831d148c2a2SMatt Joras 		if (trunk != NULL) {
1832d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1833a3814acfSSam Leffler 			if (ifr->ifr_mtu >
183475ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1835a3814acfSSam Leffler 			    ifr->ifr_mtu <
1836a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
18372cc2df49SGarrett Wollman 				error = EINVAL;
1838a3814acfSSam Leffler 			else
18392cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1840d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
1841a3814acfSSam Leffler 		} else
1842a3814acfSSam Leffler 			error = EINVAL;
1843d148c2a2SMatt Joras 		VLAN_SUNLOCK();
18442cc2df49SGarrett Wollman 		break;
18452cc2df49SGarrett Wollman 
18462cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1847ccf7ba97SMarko Zec #ifdef VIMAGE
184815f6780eSRobert Watson 		/*
184915f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
185015f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
185115f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
185215f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
18535a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
185415f6780eSRobert Watson 		 */
1855ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1856ccf7ba97SMarko Zec 			error = EPERM;
1857ccf7ba97SMarko Zec 			break;
1858ccf7ba97SMarko Zec 		}
1859ccf7ba97SMarko Zec #endif
1860*541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
18612cc2df49SGarrett Wollman 		if (error)
18622cc2df49SGarrett Wollman 			break;
18632cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1864f731f104SBill Paul 			vlan_unconfig(ifp);
18652cc2df49SGarrett Wollman 			break;
18662cc2df49SGarrett Wollman 		}
18679bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
18681bdc73d3SEd Maste 		if (p == NULL) {
18692cc2df49SGarrett Wollman 			error = ENOENT;
18702cc2df49SGarrett Wollman 			break;
18712cc2df49SGarrett Wollman 		}
187275ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
18739bcf3ae4SAlexander Motin 		if_rele(p);
18742cc2df49SGarrett Wollman 		break;
18752cc2df49SGarrett Wollman 
18762cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1877ccf7ba97SMarko Zec #ifdef VIMAGE
1878ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1879ccf7ba97SMarko Zec 			error = EPERM;
1880ccf7ba97SMarko Zec 			break;
1881ccf7ba97SMarko Zec 		}
1882ccf7ba97SMarko Zec #endif
188315a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
1884d148c2a2SMatt Joras 		VLAN_SLOCK();
188575ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
188675ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
18879bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
18887983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
18892cc2df49SGarrett Wollman 		}
1890d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1891*541d96aaSBrooks Davis 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
18922cc2df49SGarrett Wollman 		break;
18932cc2df49SGarrett Wollman 
18942cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
18952cc2df49SGarrett Wollman 		/*
18961cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
18971cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
18982cc2df49SGarrett Wollman 		 */
1899d148c2a2SMatt Joras 		VLAN_XLOCK();
190075ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
19011cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
1902d148c2a2SMatt Joras 		VLAN_XUNLOCK();
19032cc2df49SGarrett Wollman 		break;
1904a3814acfSSam Leffler 
1905f731f104SBill Paul 	case SIOCADDMULTI:
1906f731f104SBill Paul 	case SIOCDELMULTI:
190775ee267cSGleb Smirnoff 		/*
190875ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
190975ee267cSGleb Smirnoff 		 * when we do.
1910d148c2a2SMatt Joras 		 *
1911d148c2a2SMatt Joras 		 * XXX We need the rmlock here to avoid sleeping while
1912d148c2a2SMatt Joras 		 * holding in6_multi_mtx.
191375ee267cSGleb Smirnoff 		 */
1914d148c2a2SMatt Joras 		VLAN_RLOCK();
19152d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
19162d222cb7SAlexander Motin 		if (trunk != NULL) {
1917d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1918f731f104SBill Paul 			error = vlan_setmulti(ifp);
1919d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
19202d222cb7SAlexander Motin 		}
1921d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1922f731f104SBill Paul 		break;
192375ee267cSGleb Smirnoff 
19242ccbbd06SMarcelo Araujo 	case SIOCGVLANPCP:
19252ccbbd06SMarcelo Araujo #ifdef VIMAGE
19262ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19272ccbbd06SMarcelo Araujo 			error = EPERM;
19282ccbbd06SMarcelo Araujo 			break;
19292ccbbd06SMarcelo Araujo 		}
19302ccbbd06SMarcelo Araujo #endif
19312ccbbd06SMarcelo Araujo 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
19322ccbbd06SMarcelo Araujo 		break;
19332ccbbd06SMarcelo Araujo 
19342ccbbd06SMarcelo Araujo 	case SIOCSVLANPCP:
19352ccbbd06SMarcelo Araujo #ifdef VIMAGE
19362ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
19372ccbbd06SMarcelo Araujo 			error = EPERM;
19382ccbbd06SMarcelo Araujo 			break;
19392ccbbd06SMarcelo Araujo 		}
19402ccbbd06SMarcelo Araujo #endif
19412ccbbd06SMarcelo Araujo 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
19422ccbbd06SMarcelo Araujo 		if (error)
19432ccbbd06SMarcelo Araujo 			break;
19442ccbbd06SMarcelo Araujo 		if (ifr->ifr_vlan_pcp > 7) {
19452ccbbd06SMarcelo Araujo 			error = EINVAL;
19462ccbbd06SMarcelo Araujo 			break;
19472ccbbd06SMarcelo Araujo 		}
19482ccbbd06SMarcelo Araujo 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
19492ccbbd06SMarcelo Araujo 		vlan_tag_recalculate(ifv);
19502ccbbd06SMarcelo Araujo 		break;
19512ccbbd06SMarcelo Araujo 
1952d89baa5aSAlexander Motin 	case SIOCSIFCAP:
1953d148c2a2SMatt Joras 		VLAN_SLOCK();
1954d89baa5aSAlexander Motin 		ifv->ifv_capenable = ifr->ifr_reqcap;
1955d89baa5aSAlexander Motin 		trunk = TRUNK(ifv);
1956d89baa5aSAlexander Motin 		if (trunk != NULL) {
1957d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1958d89baa5aSAlexander Motin 			vlan_capabilities(ifv);
1959d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
1960d89baa5aSAlexander Motin 		}
1961d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1962d89baa5aSAlexander Motin 		break;
1963d89baa5aSAlexander Motin 
19642cc2df49SGarrett Wollman 	default:
1965e4cd31ddSJeff Roberson 		error = EINVAL;
1966e4cd31ddSJeff Roberson 		break;
19672cc2df49SGarrett Wollman 	}
196815a66c21SBruce M Simpson 
196915a66c21SBruce M Simpson 	return (error);
19702cc2df49SGarrett Wollman }
1971f3e7afe2SHans Petter Selasky 
1972f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1973f3e7afe2SHans Petter Selasky static int
1974f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp,
1975f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *params,
1976f3e7afe2SHans Petter Selasky     struct m_snd_tag **ppmt)
1977f3e7afe2SHans Petter Selasky {
1978f3e7afe2SHans Petter Selasky 
1979f3e7afe2SHans Petter Selasky 	/* get trunk device */
1980f3e7afe2SHans Petter Selasky 	ifp = vlan_trunkdev(ifp);
1981f3e7afe2SHans Petter Selasky 	if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1982f3e7afe2SHans Petter Selasky 		return (EOPNOTSUPP);
1983f3e7afe2SHans Petter Selasky 	/* forward allocation request */
1984f3e7afe2SHans Petter Selasky 	return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1985f3e7afe2SHans Petter Selasky }
1986f3e7afe2SHans Petter Selasky #endif
1987