xref: /freebsd/sys/net/if_vlan.c (revision fdbf11746af89f8ba9e1ad3bf85e404b5bbcc2d7)
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 
1994a408dcbSBill Paul SYSCTL_DECL(_net_link);
2006472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0,
2016472ac3dSEd Schouten     "IEEE 802.1Q VLAN");
2026472ac3dSEd Schouten static SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0,
2036472ac3dSEd Schouten     "for consistency");
2042cc2df49SGarrett Wollman 
205478e0520SHiroki Sato static VNET_DEFINE(int, soft_pad);
206478e0520SHiroki Sato #define	V_soft_pad	VNET(soft_pad)
207478e0520SHiroki Sato SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET,
208478e0520SHiroki Sato     &VNET_NAME(soft_pad), 0, "pad short frames before tagging");
209f6e5e0adSYaroslav Tykhiy 
2102ccbbd06SMarcelo Araujo /*
2112ccbbd06SMarcelo Araujo  * For now, make preserving PCP via an mbuf tag optional, as it increases
2122ccbbd06SMarcelo Araujo  * per-packet memory allocations and frees.  In the future, it would be
2132ccbbd06SMarcelo Araujo  * preferable to reuse ether_vtag for this, or similar.
2142ccbbd06SMarcelo Araujo  */
2152ccbbd06SMarcelo Araujo static int vlan_mtag_pcp = 0;
2162ccbbd06SMarcelo Araujo SYSCTL_INT(_net_link_vlan, OID_AUTO, mtag_pcp, CTLFLAG_RW, &vlan_mtag_pcp, 0,
2172ccbbd06SMarcelo Araujo 	"Retain VLAN PCP information as packets are passed up the stack");
2182ccbbd06SMarcelo Araujo 
21942a58907SGleb Smirnoff static const char vlanname[] = "vlan";
22042a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
2212cc2df49SGarrett Wollman 
2225cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
223ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag;
2245cb8c31aSYaroslav Tykhiy 
2254faedfe8SSam Leffler /*
226d148c2a2SMatt Joras  * if_vlan uses two module-level locks to allow concurrent modification of vlan
227d148c2a2SMatt Joras  * interfaces and (mostly) allow for vlans to be destroyed while they are being
228d148c2a2SMatt Joras  * used for tx/rx. To accomplish this in a way that has acceptable performance
229d148c2a2SMatt Joras  * and cooperation with other parts of the network stack there is a
230d148c2a2SMatt Joras  * non-sleepable rmlock(9) and an sx(9). Both locks are exclusively acquired
231d148c2a2SMatt Joras  * when destroying a vlan interface, i.e. when the if_vlantrunk field of struct
232d148c2a2SMatt Joras  * ifnet is de-allocated and NULL'd. Thus a reader holding either lock has a
233d148c2a2SMatt Joras  * guarantee that the struct ifvlantrunk references a valid vlan trunk.
23475ee267cSGleb Smirnoff  *
235d148c2a2SMatt Joras  * The performance-sensitive paths that warrant using the rmlock(9) are
236d148c2a2SMatt Joras  * vlan_transmit and vlan_input. Both have to check for the vlan interface's
237d148c2a2SMatt Joras  * existence using if_vlantrunk, and being in the network tx/rx paths the use
238d148c2a2SMatt Joras  * of an rmlock(9) gives a measureable improvement in performance.
23975ee267cSGleb Smirnoff  *
240d148c2a2SMatt Joras  * The reason for having an sx(9) is mostly because there are still areas that
241d148c2a2SMatt Joras  * must be sleepable and also have safe concurrent access to a vlan interface.
242d148c2a2SMatt Joras  * Since the sx(9) exists, it is used by default in most paths unless sleeping
243d148c2a2SMatt Joras  * is not permitted, or if it is not clear whether sleeping is permitted.
244d148c2a2SMatt Joras  *
245d148c2a2SMatt Joras  * Note that despite these protections, there is still an inherent race in the
246d148c2a2SMatt Joras  * destruction of vlans since there's no guarantee that the ifnet hasn't been
247d148c2a2SMatt Joras  * freed/reused when the tx/rx functions are called by the stack. This can only
248d148c2a2SMatt Joras  * be fixed by addressing ifnet's lifetime issues.
2494faedfe8SSam Leffler  */
250d148c2a2SMatt Joras #define _VLAN_RM_ID ifv_rm_lock
251d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx
252d148c2a2SMatt Joras 
253d148c2a2SMatt Joras static struct rmlock _VLAN_RM_ID;
254d148c2a2SMatt Joras static struct sx _VLAN_SX_ID;
255d148c2a2SMatt Joras 
256d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \
257d148c2a2SMatt Joras 	rm_init(&_VLAN_RM_ID, "vlan_rm"); \
258d148c2a2SMatt Joras 	sx_init(&_VLAN_SX_ID, "vlan_sx")
259d148c2a2SMatt Joras 
260d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \
261d148c2a2SMatt Joras 	rm_destroy(&_VLAN_RM_ID); \
262d148c2a2SMatt Joras 	sx_destroy(&_VLAN_SX_ID)
263d148c2a2SMatt Joras 
264d148c2a2SMatt Joras #define	_VLAN_RM_TRACKER		_vlan_rm_tracker
265d148c2a2SMatt Joras #define	VLAN_RLOCK()			rm_rlock(&_VLAN_RM_ID, \
266d148c2a2SMatt Joras 					    &_VLAN_RM_TRACKER)
267d148c2a2SMatt Joras #define	VLAN_RUNLOCK()			rm_runlock(&_VLAN_RM_ID, \
268d148c2a2SMatt Joras 					    &_VLAN_RM_TRACKER)
269d148c2a2SMatt Joras #define	VLAN_WLOCK()			rm_wlock(&_VLAN_RM_ID)
270d148c2a2SMatt Joras #define	VLAN_WUNLOCK()			rm_wunlock(&_VLAN_RM_ID)
271d148c2a2SMatt Joras #define	VLAN_RLOCK_ASSERT()		rm_assert(&_VLAN_RM_ID, RA_RLOCKED)
272d148c2a2SMatt Joras #define	VLAN_WLOCK_ASSERT()		rm_assert(&_VLAN_RM_ID, RA_WLOCKED)
273d148c2a2SMatt Joras #define	VLAN_RWLOCK_ASSERT()		rm_assert(&_VLAN_RM_ID, RA_LOCKED)
274d148c2a2SMatt Joras #define	VLAN_LOCK_READER		struct rm_priotracker _VLAN_RM_TRACKER
275d148c2a2SMatt Joras 
276d148c2a2SMatt Joras #define	VLAN_SLOCK()			sx_slock(&_VLAN_SX_ID)
277d148c2a2SMatt Joras #define	VLAN_SUNLOCK()			sx_sunlock(&_VLAN_SX_ID)
278d148c2a2SMatt Joras #define	VLAN_XLOCK()			sx_xlock(&_VLAN_SX_ID)
279d148c2a2SMatt Joras #define	VLAN_XUNLOCK()			sx_xunlock(&_VLAN_SX_ID)
280d148c2a2SMatt Joras #define	VLAN_SLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
281d148c2a2SMatt Joras #define	VLAN_XLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
282d148c2a2SMatt Joras #define	VLAN_SXLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_LOCKED)
283d148c2a2SMatt Joras 
284d148c2a2SMatt Joras 
285d148c2a2SMatt Joras /*
286d148c2a2SMatt Joras  * We also have a per-trunk rmlock(9), that is locked shared on packet
287d148c2a2SMatt Joras  * processing and exclusive when configuration is changed. Note: This should
288d148c2a2SMatt Joras  * only be acquired while there is a shared lock on either of the global locks
289d148c2a2SMatt Joras  * via VLAN_SLOCK or VLAN_RLOCK. Thus, an exclusive lock on the global locks
290d148c2a2SMatt Joras  * makes a call to TRUNK_RLOCK/TRUNK_WLOCK technically superfluous.
291d148c2a2SMatt Joras  */
292d148c2a2SMatt Joras #define	_TRUNK_RM_TRACKER		_trunk_rm_tracker
293772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_INIT(trunk)		rm_init(&(trunk)->lock, vlanname)
294772b000fSAlexander V. Chernikov #define	TRUNK_LOCK_DESTROY(trunk)	rm_destroy(&(trunk)->lock)
295d148c2a2SMatt Joras #define	TRUNK_RLOCK(trunk)		rm_rlock(&(trunk)->lock, \
296d148c2a2SMatt Joras     &_TRUNK_RM_TRACKER)
297d148c2a2SMatt Joras #define	TRUNK_WLOCK(trunk)		rm_wlock(&(trunk)->lock)
298d148c2a2SMatt Joras #define	TRUNK_RUNLOCK(trunk)		rm_runlock(&(trunk)->lock, \
299d148c2a2SMatt Joras     &_TRUNK_RM_TRACKER)
300d148c2a2SMatt Joras #define	TRUNK_WUNLOCK(trunk)		rm_wunlock(&(trunk)->lock)
301d148c2a2SMatt Joras #define	TRUNK_RLOCK_ASSERT(trunk)	rm_assert(&(trunk)->lock, RA_RLOCKED)
302d148c2a2SMatt Joras #define	TRUNK_LOCK_ASSERT(trunk)	rm_assert(&(trunk)->lock, RA_LOCKED)
303d148c2a2SMatt Joras #define	TRUNK_WLOCK_ASSERT(trunk)	rm_assert(&(trunk)->lock, RA_WLOCKED)
304d148c2a2SMatt Joras #define	TRUNK_LOCK_READER		struct rm_priotracker _TRUNK_RM_TRACKER
30575ee267cSGleb Smirnoff 
306d148c2a2SMatt Joras /*
307d148c2a2SMatt Joras  * The VLAN_ARRAY substitutes the dynamic hash with a static array
308d148c2a2SMatt Joras  * with 4096 entries. In theory this can give a boost in processing,
309d148c2a2SMatt Joras  * however in practice it does not. Probably this is because the array
310d148c2a2SMatt Joras  * is too big to fit into CPU cache.
311d148c2a2SMatt Joras  */
31275ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
31375ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
31475ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
31575ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
31675ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
31775ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
31875ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
3197983103aSRobert Watson 	uint16_t vid);
32075ee267cSGleb Smirnoff #endif
32175ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
3224faedfe8SSam Leffler 
323114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
324a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
325cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
326f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
327f3e7afe2SHans Petter Selasky static	int vlan_snd_tag_alloc(struct ifnet *,
328f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *, struct m_snd_tag **);
329f3e7afe2SHans Petter Selasky #endif
330d9b1d615SJohn Baldwin static	void vlan_qflush(struct ifnet *ifp);
3311cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
3321cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
3331cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
334f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
335d9b1d615SJohn Baldwin static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
3366f359e28SJohn Baldwin static	void vlan_unconfig(struct ifnet *ifp);
33728cc4d37SJohn Baldwin static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
33875ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
339a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
34075ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
34175ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
342f731f104SBill Paul 
343f941c31aSGleb Smirnoff static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
344f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
3456b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
346f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
347f889d2efSBrooks Davis 
3485cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
349ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
3505cb8c31aSYaroslav Tykhiy 
351d148c2a2SMatt Joras static  void vlan_lladdr_fn(void *arg, int pending);
352d148c2a2SMatt Joras 
35342a58907SGleb Smirnoff static struct if_clone *vlan_cloner;
3549d4fe4b2SBrooks Davis 
355ccf7ba97SMarko Zec #ifdef VIMAGE
35642a58907SGleb Smirnoff static VNET_DEFINE(struct if_clone *, vlan_cloner);
357ccf7ba97SMarko Zec #define	V_vlan_cloner	VNET(vlan_cloner)
358ccf7ba97SMarko Zec #endif
359ccf7ba97SMarko Zec 
36075ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
36175ee267cSGleb Smirnoff #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
362114c608cSYaroslav Tykhiy 
36375ee267cSGleb Smirnoff static void
36475ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
36575ee267cSGleb Smirnoff {
36675ee267cSGleb Smirnoff 	int i, n;
36775ee267cSGleb Smirnoff 
36875ee267cSGleb Smirnoff 	/*
36975ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
37075ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
37175ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
37275ee267cSGleb Smirnoff 	 */
37375ee267cSGleb Smirnoff 
37475ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
37575ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
37675ee267cSGleb Smirnoff 
37775ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
37875ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
37975ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
38075ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
38175ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
38275ee267cSGleb Smirnoff 		LIST_INIT(&trunk->hash[i]);
38375ee267cSGleb Smirnoff }
38475ee267cSGleb Smirnoff 
38575ee267cSGleb Smirnoff static void
38675ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
38775ee267cSGleb Smirnoff {
38875ee267cSGleb Smirnoff #ifdef INVARIANTS
38975ee267cSGleb Smirnoff 	int i;
39075ee267cSGleb Smirnoff 
39175ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
39275ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
39375ee267cSGleb Smirnoff 		KASSERT(LIST_EMPTY(&trunk->hash[i]),
39475ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
39575ee267cSGleb Smirnoff #endif
39675ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
39775ee267cSGleb Smirnoff 	trunk->hash = NULL;
39875ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
39975ee267cSGleb Smirnoff }
40075ee267cSGleb Smirnoff 
40175ee267cSGleb Smirnoff static int
40275ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
40375ee267cSGleb Smirnoff {
40475ee267cSGleb Smirnoff 	int i, b;
40575ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
40675ee267cSGleb Smirnoff 
407d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(trunk);
40875ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
40975ee267cSGleb Smirnoff 
41075ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
4117983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
41275ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
4137983103aSRobert Watson 		if (ifv->ifv_vid == ifv2->ifv_vid)
41475ee267cSGleb Smirnoff 			return (EEXIST);
41575ee267cSGleb Smirnoff 
41675ee267cSGleb Smirnoff 	/*
41775ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
41875ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
41975ee267cSGleb Smirnoff 	 * buckets/2.
42075ee267cSGleb Smirnoff 	 */
42175ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
42275ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
4237983103aSRobert Watson 		i = HASH(ifv->ifv_vid, trunk->hmask);
42475ee267cSGleb Smirnoff 	}
42575ee267cSGleb Smirnoff 	LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
42675ee267cSGleb Smirnoff 	trunk->refcnt++;
42775ee267cSGleb Smirnoff 
42875ee267cSGleb Smirnoff 	return (0);
42975ee267cSGleb Smirnoff }
43075ee267cSGleb Smirnoff 
43175ee267cSGleb Smirnoff static int
43275ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
43375ee267cSGleb Smirnoff {
43475ee267cSGleb Smirnoff 	int i, b;
43575ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
43675ee267cSGleb Smirnoff 
437d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(trunk);
43875ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
43975ee267cSGleb Smirnoff 
44075ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
4417983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
44275ee267cSGleb Smirnoff 	LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
44375ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
44475ee267cSGleb Smirnoff 			trunk->refcnt--;
44575ee267cSGleb Smirnoff 			LIST_REMOVE(ifv2, ifv_list);
44675ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
44775ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
44875ee267cSGleb Smirnoff 			return (0);
44975ee267cSGleb Smirnoff 		}
45075ee267cSGleb Smirnoff 
45175ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
45275ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
45375ee267cSGleb Smirnoff }
45475ee267cSGleb Smirnoff 
45575ee267cSGleb Smirnoff /*
45675ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
45775ee267cSGleb Smirnoff  */
45875ee267cSGleb Smirnoff static void
45975ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
46075ee267cSGleb Smirnoff {
46175ee267cSGleb Smirnoff 	struct ifvlan *ifv;
46275ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
46375ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
46475ee267cSGleb Smirnoff 
465d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(trunk);
46675ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
46775ee267cSGleb Smirnoff 
46875ee267cSGleb Smirnoff 	if (howmuch == 0) {
46975ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
47075ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
47175ee267cSGleb Smirnoff 		return;
47275ee267cSGleb Smirnoff 	}
47375ee267cSGleb Smirnoff 
47475ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
47575ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
47675ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
47775ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
47875ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
47975ee267cSGleb Smirnoff 		return;
48075ee267cSGleb Smirnoff 
48175ee267cSGleb Smirnoff 	/* M_NOWAIT because we're called with trunk mutex held */
48275ee267cSGleb Smirnoff 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
48375ee267cSGleb Smirnoff 	if (hash2 == NULL) {
48475ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
48575ee267cSGleb Smirnoff 		    __func__);
48675ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
48775ee267cSGleb Smirnoff 	}
48875ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
48975ee267cSGleb Smirnoff 		LIST_INIT(&hash2[j]);
49075ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
491c0cb022bSYaroslav Tykhiy 		while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
49275ee267cSGleb Smirnoff 			LIST_REMOVE(ifv, ifv_list);
4937983103aSRobert Watson 			j = HASH(ifv->ifv_vid, n2 - 1);
49475ee267cSGleb Smirnoff 			LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
49575ee267cSGleb Smirnoff 		}
49675ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
49775ee267cSGleb Smirnoff 	trunk->hash = hash2;
49875ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
49975ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
500f84b2d69SYaroslav Tykhiy 
501f84b2d69SYaroslav Tykhiy 	if (bootverbose)
502f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
503f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
50475ee267cSGleb Smirnoff }
50575ee267cSGleb Smirnoff 
50675ee267cSGleb Smirnoff static __inline struct ifvlan *
5077983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
50875ee267cSGleb Smirnoff {
50975ee267cSGleb Smirnoff 	struct ifvlan *ifv;
51075ee267cSGleb Smirnoff 
511d148c2a2SMatt Joras 	TRUNK_RLOCK_ASSERT(trunk);
51275ee267cSGleb Smirnoff 
5137983103aSRobert Watson 	LIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
5147983103aSRobert Watson 		if (ifv->ifv_vid == vid)
51575ee267cSGleb Smirnoff 			return (ifv);
51675ee267cSGleb Smirnoff 	return (NULL);
51775ee267cSGleb Smirnoff }
51875ee267cSGleb Smirnoff 
51975ee267cSGleb Smirnoff #if 0
52075ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
52175ee267cSGleb Smirnoff static void
52275ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
52375ee267cSGleb Smirnoff {
52475ee267cSGleb Smirnoff 	int i;
52575ee267cSGleb Smirnoff 	struct ifvlan *ifv;
52675ee267cSGleb Smirnoff 
52775ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
52875ee267cSGleb Smirnoff 		printf("%d: ", i);
52975ee267cSGleb Smirnoff 		LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
53075ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
53175ee267cSGleb Smirnoff 		printf("\n");
53275ee267cSGleb Smirnoff 	}
53375ee267cSGleb Smirnoff }
53475ee267cSGleb Smirnoff #endif /* 0 */
535e4cd31ddSJeff Roberson #else
536e4cd31ddSJeff Roberson 
537e4cd31ddSJeff Roberson static __inline struct ifvlan *
5387983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
539e4cd31ddSJeff Roberson {
540e4cd31ddSJeff Roberson 
5417983103aSRobert Watson 	return trunk->vlans[vid];
542e4cd31ddSJeff Roberson }
543e4cd31ddSJeff Roberson 
544e4cd31ddSJeff Roberson static __inline int
545e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
546e4cd31ddSJeff Roberson {
547e4cd31ddSJeff Roberson 
5487983103aSRobert Watson 	if (trunk->vlans[ifv->ifv_vid] != NULL)
549e4cd31ddSJeff Roberson 		return EEXIST;
5507983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = ifv;
551e4cd31ddSJeff Roberson 	trunk->refcnt++;
552e4cd31ddSJeff Roberson 
553e4cd31ddSJeff Roberson 	return (0);
554e4cd31ddSJeff Roberson }
555e4cd31ddSJeff Roberson 
556e4cd31ddSJeff Roberson static __inline int
557e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
558e4cd31ddSJeff Roberson {
559e4cd31ddSJeff Roberson 
5607983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = NULL;
561e4cd31ddSJeff Roberson 	trunk->refcnt--;
562e4cd31ddSJeff Roberson 
563e4cd31ddSJeff Roberson 	return (0);
564e4cd31ddSJeff Roberson }
565e4cd31ddSJeff Roberson 
566e4cd31ddSJeff Roberson static __inline void
567e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk)
568e4cd31ddSJeff Roberson {
569e4cd31ddSJeff Roberson }
570e4cd31ddSJeff Roberson 
571e4cd31ddSJeff Roberson static __inline void
572e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk)
573e4cd31ddSJeff Roberson {
574e4cd31ddSJeff Roberson }
575e4cd31ddSJeff Roberson 
57675ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
57775ee267cSGleb Smirnoff 
57875ee267cSGleb Smirnoff static void
57975ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
58075ee267cSGleb Smirnoff {
581d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
582d148c2a2SMatt Joras 	VLAN_WLOCK_ASSERT();
58375ee267cSGleb Smirnoff 
58475ee267cSGleb Smirnoff 	vlan_freehash(trunk);
58575ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
58633499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
5879bcf3ae4SAlexander Motin 	if_rele(trunk->parent);
58875ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
58975ee267cSGleb Smirnoff }
59075ee267cSGleb Smirnoff 
591f731f104SBill Paul /*
592f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
593f731f104SBill Paul  * programming the multicast filter of the parent. This has the
594f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
595f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
596f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
597f731f104SBill Paul  * to avoid this: there really is only one physical interface.
598f731f104SBill Paul  */
5992b120974SPeter Wemm static int
6002b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
601f731f104SBill Paul {
602f731f104SBill Paul 	struct ifnet		*ifp_p;
6032d222cb7SAlexander Motin 	struct ifmultiaddr	*ifma;
604f731f104SBill Paul 	struct ifvlan		*sc;
605c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
606f731f104SBill Paul 	int			error;
607f731f104SBill Paul 
608d148c2a2SMatt Joras 	/*
609d148c2a2SMatt Joras 	 * XXX This stupidly needs the rmlock to avoid sleeping while holding
610d148c2a2SMatt Joras 	 * the in6_multi_mtx (see in6_mc_join_locked).
611d148c2a2SMatt Joras 	 */
612d148c2a2SMatt Joras 	VLAN_RWLOCK_ASSERT();
613d148c2a2SMatt Joras 
614f731f104SBill Paul 	/* Find the parent. */
615f731f104SBill Paul 	sc = ifp->if_softc;
616d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(TRUNK(sc));
61775ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
6181b2a4f7aSBill Fenner 
6198b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
6208b615593SMarko Zec 
621f731f104SBill Paul 	/* First, remove any existing filter entries. */
622c0cb022bSYaroslav Tykhiy 	while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
623f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
6242d222cb7SAlexander Motin 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
6259d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
626f731f104SBill Paul 	}
627f731f104SBill Paul 
628f731f104SBill Paul 	/* Now program new ones. */
6292d222cb7SAlexander Motin 	IF_ADDR_WLOCK(ifp);
6306817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
631f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
632f731f104SBill Paul 			continue;
63329c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
6342d222cb7SAlexander Motin 		if (mc == NULL) {
6352d222cb7SAlexander Motin 			IF_ADDR_WUNLOCK(ifp);
63629c2dfbeSBruce M Simpson 			return (ENOMEM);
6372d222cb7SAlexander Motin 		}
638e4cd31ddSJeff Roberson 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
639e4cd31ddSJeff Roberson 		mc->mc_addr.sdl_index = ifp_p->if_index;
640f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
6412d222cb7SAlexander Motin 	}
6422d222cb7SAlexander Motin 	IF_ADDR_WUNLOCK(ifp);
6432d222cb7SAlexander Motin 	SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
644e4cd31ddSJeff Roberson 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
6452d222cb7SAlexander Motin 		    NULL);
646f731f104SBill Paul 		if (error)
647f731f104SBill Paul 			return (error);
648f731f104SBill Paul 	}
649f731f104SBill Paul 
6508b615593SMarko Zec 	CURVNET_RESTORE();
651f731f104SBill Paul 	return (0);
652f731f104SBill Paul }
6532cc2df49SGarrett Wollman 
654a3814acfSSam Leffler /*
655ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
656ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
657ea4ca115SAndrew Thompson  * should also change it on all children vlans.
658ea4ca115SAndrew Thompson  */
659ea4ca115SAndrew Thompson static void
660ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
661ea4ca115SAndrew Thompson {
662ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
663d148c2a2SMatt Joras 	struct ifnet *ifv_ifp;
664d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
665d148c2a2SMatt Joras 	struct sockaddr_dl *sdl;
666d148c2a2SMatt Joras 	VLAN_LOCK_READER;
667ea4ca115SAndrew Thompson 
668d148c2a2SMatt Joras 	/* Need the rmlock since this is run on taskqueue_swi. */
669d148c2a2SMatt Joras 	VLAN_RLOCK();
670d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
671d148c2a2SMatt Joras 	if (trunk == NULL) {
672d148c2a2SMatt Joras 		VLAN_RUNLOCK();
673ea4ca115SAndrew Thompson 		return;
674d148c2a2SMatt Joras 	}
675ea4ca115SAndrew Thompson 
676ea4ca115SAndrew Thompson 	/*
677ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
678d148c2a2SMatt Joras 	 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
679d148c2a2SMatt Joras 	 * ioctl calls on the parent garbling the lladdr of the child vlan.
680ea4ca115SAndrew Thompson 	 */
681d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
682d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
683d148c2a2SMatt Joras 		/*
684d148c2a2SMatt Joras 		 * Copy new new lladdr into the ifv_ifp, enqueue a task
685d148c2a2SMatt Joras 		 * to actually call if_setlladdr. if_setlladdr needs to
686d148c2a2SMatt Joras 		 * be deferred to a taskqueue because it will call into
687d148c2a2SMatt Joras 		 * the if_vlan ioctl path and try to acquire the global
688d148c2a2SMatt Joras 		 * lock.
689d148c2a2SMatt Joras 		 */
690d148c2a2SMatt Joras 		ifv_ifp = ifv->ifv_ifp;
691d148c2a2SMatt Joras 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
692e4cd31ddSJeff Roberson 		    ifp->if_addrlen);
693d148c2a2SMatt Joras 		sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
694d148c2a2SMatt Joras 		sdl->sdl_alen = ifp->if_addrlen;
695d148c2a2SMatt Joras 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
6966117727bSAndrew Thompson 	}
697d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
698d148c2a2SMatt Joras 	VLAN_RUNLOCK();
699ea4ca115SAndrew Thompson }
700ea4ca115SAndrew Thompson 
701ea4ca115SAndrew Thompson /*
7025cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
7035cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
7045cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
7055428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
7065428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
7075cb8c31aSYaroslav Tykhiy  */
7085cb8c31aSYaroslav Tykhiy static void
7095cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
7105cb8c31aSYaroslav Tykhiy {
7115cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
712d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
7135cb8c31aSYaroslav Tykhiy 
7145428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
7155428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
7165428776eSJohn Baldwin 		return;
717d148c2a2SMatt Joras 	VLAN_XLOCK();
718d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
719d148c2a2SMatt Joras 	if (trunk == NULL) {
720d148c2a2SMatt Joras 		VLAN_XUNLOCK();
721d148c2a2SMatt Joras 		return;
722d148c2a2SMatt Joras 	}
7235428776eSJohn Baldwin 
7245cb8c31aSYaroslav Tykhiy 	/*
7255cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
7265cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
7275cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
7285cb8c31aSYaroslav Tykhiy 	 */
729d148c2a2SMatt Joras 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
730d148c2a2SMatt Joras 	    ifp->if_vlantrunk == NULL)
73128cc4d37SJohn Baldwin 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
732d148c2a2SMatt Joras 
7335cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
7345cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
735d148c2a2SMatt Joras 	VLAN_XUNLOCK();
7365cb8c31aSYaroslav Tykhiy }
7375cb8c31aSYaroslav Tykhiy 
7385cb8c31aSYaroslav Tykhiy /*
739e4cd31ddSJeff Roberson  * Return the trunk device for a virtual interface.
740e4cd31ddSJeff Roberson  */
741e4cd31ddSJeff Roberson static struct ifnet  *
742e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp)
743e4cd31ddSJeff Roberson {
744e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
745d148c2a2SMatt Joras 	VLAN_LOCK_READER;
746e4cd31ddSJeff Roberson 
747e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
748e4cd31ddSJeff Roberson 		return (NULL);
749d148c2a2SMatt Joras 
750d148c2a2SMatt Joras 	/* Not clear if callers are sleepable, so acquire the rmlock. */
751d148c2a2SMatt Joras 	VLAN_RLOCK();
752e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
753e4cd31ddSJeff Roberson 	ifp = NULL;
754e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
755e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
756d148c2a2SMatt Joras 	VLAN_RUNLOCK();
757e4cd31ddSJeff Roberson 	return (ifp);
758e4cd31ddSJeff Roberson }
759e4cd31ddSJeff Roberson 
760e4cd31ddSJeff Roberson /*
7617983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
7627983103aSRobert Watson  * components such as Infiniband.
7637983103aSRobert Watson  *
7647983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
7657983103aSRobert Watson  * vlan_vid().
766e4cd31ddSJeff Roberson  */
767e4cd31ddSJeff Roberson static int
7687983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
769e4cd31ddSJeff Roberson {
770e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
771e4cd31ddSJeff Roberson 
772e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
773e4cd31ddSJeff Roberson 		return (EINVAL);
774e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
7757983103aSRobert Watson 	*vidp = ifv->ifv_vid;
776e4cd31ddSJeff Roberson 	return (0);
777e4cd31ddSJeff Roberson }
778e4cd31ddSJeff Roberson 
779e4cd31ddSJeff Roberson /*
780e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
781e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
782e4cd31ddSJeff Roberson  */
783e4cd31ddSJeff Roberson static void *
784e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
785e4cd31ddSJeff Roberson {
786e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
787e4cd31ddSJeff Roberson 
788e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
789e4cd31ddSJeff Roberson 		return (NULL);
790e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
791e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
792e4cd31ddSJeff Roberson }
793e4cd31ddSJeff Roberson 
794e4cd31ddSJeff Roberson /*
795e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
796e4cd31ddSJeff Roberson  * private per-instance data in.
797e4cd31ddSJeff Roberson  */
798e4cd31ddSJeff Roberson static int
799e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
800e4cd31ddSJeff Roberson {
801e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
802e4cd31ddSJeff Roberson 
803e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
804e4cd31ddSJeff Roberson 		return (EINVAL);
805e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
806e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
807e4cd31ddSJeff Roberson 	return (0);
808e4cd31ddSJeff Roberson }
809e4cd31ddSJeff Roberson 
810e4cd31ddSJeff Roberson /*
8117983103aSRobert Watson  * Return the vlan device present at the specific VID.
812e4cd31ddSJeff Roberson  */
813e4cd31ddSJeff Roberson static struct ifnet *
8147983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
815e4cd31ddSJeff Roberson {
816e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
817e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
818d148c2a2SMatt Joras 	VLAN_LOCK_READER;
819772b000fSAlexander V. Chernikov 	TRUNK_LOCK_READER;
820e4cd31ddSJeff Roberson 
821d148c2a2SMatt Joras 	/* Not clear if callers are sleepable, so acquire the rmlock. */
822d148c2a2SMatt Joras 	VLAN_RLOCK();
823e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
824d148c2a2SMatt Joras 	if (trunk == NULL) {
825d148c2a2SMatt Joras 		VLAN_RUNLOCK();
826e4cd31ddSJeff Roberson 		return (NULL);
827d148c2a2SMatt Joras 	}
828e4cd31ddSJeff Roberson 	ifp = NULL;
829e4cd31ddSJeff Roberson 	TRUNK_RLOCK(trunk);
8307983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
831e4cd31ddSJeff Roberson 	if (ifv)
832e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
833e4cd31ddSJeff Roberson 	TRUNK_RUNLOCK(trunk);
834d148c2a2SMatt Joras 	VLAN_RUNLOCK();
835e4cd31ddSJeff Roberson 	return (ifp);
836e4cd31ddSJeff Roberson }
837e4cd31ddSJeff Roberson 
838e4cd31ddSJeff Roberson /*
8392ccbbd06SMarcelo Araujo  * Recalculate the cached VLAN tag exposed via the MIB.
8402ccbbd06SMarcelo Araujo  */
8412ccbbd06SMarcelo Araujo static void
8422ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv)
8432ccbbd06SMarcelo Araujo {
8442ccbbd06SMarcelo Araujo 
8452ccbbd06SMarcelo Araujo        ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
8462ccbbd06SMarcelo Araujo }
8472ccbbd06SMarcelo Araujo 
8482ccbbd06SMarcelo Araujo /*
849a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
850a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
851a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
852a3814acfSSam Leffler  * set here.  No one else in the system should be aware of this so
853a3814acfSSam Leffler  * we use an explicit reference here.
854a3814acfSSam Leffler  */
855a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
856a3814acfSSam Leffler 
857984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
858a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
859127d7b2dSAndre Oppermann 
8602b120974SPeter Wemm static int
8612b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
8622b120974SPeter Wemm {
8639d4fe4b2SBrooks Davis 
8642b120974SPeter Wemm 	switch (type) {
8652b120974SPeter Wemm 	case MOD_LOAD:
8665cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
8675cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
8685cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
8695cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
870ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
871ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
872ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
873ea4ca115SAndrew Thompson 			return (ENOMEM);
874d148c2a2SMatt Joras 		VLAN_LOCKING_INIT();
8759d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
876127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
87775ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
878e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
879e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
880e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
881e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
882e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
883ccf7ba97SMarko Zec #ifndef VIMAGE
88442a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
88542a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
886ccf7ba97SMarko Zec #endif
88725c0f7b3SYaroslav Tykhiy 		if (bootverbose)
88825c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
88925c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
89025c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
89125c0f7b3SYaroslav Tykhiy #else
89225c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
89325c0f7b3SYaroslav Tykhiy #endif
89425c0f7b3SYaroslav Tykhiy 
89525c0f7b3SYaroslav Tykhiy 			       "\n");
8962b120974SPeter Wemm 		break;
8972b120974SPeter Wemm 	case MOD_UNLOAD:
898ccf7ba97SMarko Zec #ifndef VIMAGE
89942a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
900ccf7ba97SMarko Zec #endif
9015cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
902ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
9039d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
904127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
90575ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
906e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
907e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
90809fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
90909fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
910e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
911d148c2a2SMatt Joras 		VLAN_LOCKING_DESTROY();
91225c0f7b3SYaroslav Tykhiy 		if (bootverbose)
91325c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
9149d4fe4b2SBrooks Davis 		break;
9153e019deaSPoul-Henning Kamp 	default:
9163e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
9172b120974SPeter Wemm 	}
91815a66c21SBruce M Simpson 	return (0);
9192b120974SPeter Wemm }
9202b120974SPeter Wemm 
9212b120974SPeter Wemm static moduledata_t vlan_mod = {
9222b120974SPeter Wemm 	"if_vlan",
9232b120974SPeter Wemm 	vlan_modevent,
9249823d527SKevin Lo 	0
9252b120974SPeter Wemm };
9262b120974SPeter Wemm 
9272b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
92811edc477SEd Maste MODULE_VERSION(if_vlan, 3);
9292cc2df49SGarrett Wollman 
930ccf7ba97SMarko Zec #ifdef VIMAGE
931ccf7ba97SMarko Zec static void
932ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
933ccf7ba97SMarko Zec {
934ccf7ba97SMarko Zec 
93542a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
93642a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
937ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
938ccf7ba97SMarko Zec }
939ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
940ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
941ccf7ba97SMarko Zec 
942ccf7ba97SMarko Zec static void
943ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
944ccf7ba97SMarko Zec {
945ccf7ba97SMarko Zec 
94642a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
947ccf7ba97SMarko Zec }
94889856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
949ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
950ccf7ba97SMarko Zec #endif
951ccf7ba97SMarko Zec 
952f941c31aSGleb Smirnoff /*
953f941c31aSGleb Smirnoff  * Check for <etherif>.<vlan> style interface names.
954f941c31aSGleb Smirnoff  */
955f889d2efSBrooks Davis static struct ifnet *
956f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp)
9579d4fe4b2SBrooks Davis {
958f941c31aSGleb Smirnoff 	char ifname[IFNAMSIZ];
959f941c31aSGleb Smirnoff 	char *cp;
960f889d2efSBrooks Davis 	struct ifnet *ifp;
9617983103aSRobert Watson 	int vid;
962f889d2efSBrooks Davis 
963f941c31aSGleb Smirnoff 	strlcpy(ifname, name, IFNAMSIZ);
964f941c31aSGleb Smirnoff 	if ((cp = strchr(ifname, '.')) == NULL)
965f941c31aSGleb Smirnoff 		return (NULL);
966f941c31aSGleb Smirnoff 	*cp = '\0';
9679bcf3ae4SAlexander Motin 	if ((ifp = ifunit_ref(ifname)) == NULL)
968f941c31aSGleb Smirnoff 		return (NULL);
969f941c31aSGleb Smirnoff 	/* Parse VID. */
9709bcf3ae4SAlexander Motin 	if (*++cp == '\0') {
9719bcf3ae4SAlexander Motin 		if_rele(ifp);
972f941c31aSGleb Smirnoff 		return (NULL);
9739bcf3ae4SAlexander Motin 	}
9747983103aSRobert Watson 	vid = 0;
975fb92ad4aSJohn Baldwin 	for(; *cp >= '0' && *cp <= '9'; cp++)
9767983103aSRobert Watson 		vid = (vid * 10) + (*cp - '0');
9779bcf3ae4SAlexander Motin 	if (*cp != '\0') {
9789bcf3ae4SAlexander Motin 		if_rele(ifp);
979f941c31aSGleb Smirnoff 		return (NULL);
9809bcf3ae4SAlexander Motin 	}
9817983103aSRobert Watson 	if (vidp != NULL)
9827983103aSRobert Watson 		*vidp = vid;
983f889d2efSBrooks Davis 
98415a66c21SBruce M Simpson 	return (ifp);
985f889d2efSBrooks Davis }
986f889d2efSBrooks Davis 
987f889d2efSBrooks Davis static int
988f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
989f889d2efSBrooks Davis {
990f889d2efSBrooks Davis 	const char *cp;
991f889d2efSBrooks Davis 
992f941c31aSGleb Smirnoff 	if (vlan_clone_match_ethervid(name, NULL) != NULL)
993f889d2efSBrooks Davis 		return (1);
994f889d2efSBrooks Davis 
99542a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
996f889d2efSBrooks Davis 		return (0);
997f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
998f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
999f889d2efSBrooks Davis 			return (0);
1000f889d2efSBrooks Davis 	}
1001f889d2efSBrooks Davis 
1002f889d2efSBrooks Davis 	return (1);
1003f889d2efSBrooks Davis }
1004f889d2efSBrooks Davis 
1005f889d2efSBrooks Davis static int
10066b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
1007f889d2efSBrooks Davis {
1008f889d2efSBrooks Davis 	char *dp;
1009f889d2efSBrooks Davis 	int wildcard;
1010f889d2efSBrooks Davis 	int unit;
1011f889d2efSBrooks Davis 	int error;
10127983103aSRobert Watson 	int vid;
10139d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
10149d4fe4b2SBrooks Davis 	struct ifnet *ifp;
1015f889d2efSBrooks Davis 	struct ifnet *p;
10163ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
10173ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
10186b7330e2SSam Leffler 	struct vlanreq vlr;
101965239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
1020f889d2efSBrooks Davis 
10216b7330e2SSam Leffler 	/*
10226b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
10236b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
10246b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
10256b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
10266b7330e2SSam Leffler 	 *   must be configured separately.
10276b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
1028a4641f4eSPedro F. Giffuni 	 * supported for backwards compatibility.
10297983103aSRobert Watson 	 *
10307983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
10317983103aSRobert Watson 	 * called for.
10326b7330e2SSam Leffler 	 */
10336b7330e2SSam Leffler 	if (params) {
10346b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
10356b7330e2SSam Leffler 		if (error)
10366b7330e2SSam Leffler 			return error;
10379bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
10386b7330e2SSam Leffler 		if (p == NULL)
1039b1828acfSGleb Smirnoff 			return (ENXIO);
10406b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
10419bcf3ae4SAlexander Motin 		if (error != 0) {
10429bcf3ae4SAlexander Motin 			if_rele(p);
10436b7330e2SSam Leffler 			return (error);
10449bcf3ae4SAlexander Motin 		}
10457983103aSRobert Watson 		vid = vlr.vlr_tag;
10466b7330e2SSam Leffler 		wildcard = (unit < 0);
1047f941c31aSGleb Smirnoff 	} else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1048f889d2efSBrooks Davis 		unit = -1;
1049f889d2efSBrooks Davis 		wildcard = 0;
1050f889d2efSBrooks Davis 	} else {
10519bcf3ae4SAlexander Motin 		p = NULL;
1052f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
1053f889d2efSBrooks Davis 		if (error != 0)
1054f889d2efSBrooks Davis 			return (error);
1055f889d2efSBrooks Davis 
1056f889d2efSBrooks Davis 		wildcard = (unit < 0);
1057f889d2efSBrooks Davis 	}
1058f889d2efSBrooks Davis 
1059f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
10609bcf3ae4SAlexander Motin 	if (error != 0) {
10619bcf3ae4SAlexander Motin 		if (p != NULL)
10629bcf3ae4SAlexander Motin 			if_rele(p);
1063f889d2efSBrooks Davis 		return (error);
10649bcf3ae4SAlexander Motin 	}
1065f889d2efSBrooks Davis 
1066f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
1067f889d2efSBrooks Davis 	if (wildcard) {
1068f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
1069f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1070f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
1071f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
1072f889d2efSBrooks Davis 		}
1073f889d2efSBrooks Davis 	}
10749d4fe4b2SBrooks Davis 
1075a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1076fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1077fc74a9f9SBrooks Davis 	if (ifp == NULL) {
1078fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
1079fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
10809bcf3ae4SAlexander Motin 		if (p != NULL)
10819bcf3ae4SAlexander Motin 			if_rele(p);
1082fc74a9f9SBrooks Davis 		return (ENOSPC);
1083fc74a9f9SBrooks Davis 	}
10849d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
10859d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
1086f889d2efSBrooks Davis 	/*
1087cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
1088f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
1089f889d2efSBrooks Davis 	 */
1090f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
109142a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
1092f889d2efSBrooks Davis 	ifp->if_dunit = unit;
10939d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
10949d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
109515a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
10969d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
10979d4fe4b2SBrooks Davis 
1098114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
1099d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
1100d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
11019d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
1102f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1103f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1104f3e7afe2SHans Petter Selasky #endif
110564a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
1106fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
11079d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
1108211f625aSBill Fenner 	ifp->if_baudrate = 0;
1109a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
1110a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
11113ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
11123ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
11133ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
11149d4fe4b2SBrooks Davis 
11159bcf3ae4SAlexander Motin 	if (p != NULL) {
11167983103aSRobert Watson 		error = vlan_config(ifv, p, vid);
11179bcf3ae4SAlexander Motin 		if_rele(p);
1118f889d2efSBrooks Davis 		if (error != 0) {
1119f889d2efSBrooks Davis 			/*
112028cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
1121f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
1122f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1123f889d2efSBrooks Davis 			 */
1124f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1125249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
11264b22573aSBrooks Davis 			if_free(ifp);
112796c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
1128f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1129f889d2efSBrooks Davis 
1130f889d2efSBrooks Davis 			return (error);
1131f889d2efSBrooks Davis 		}
1132f889d2efSBrooks Davis 	}
1133f889d2efSBrooks Davis 
11349d4fe4b2SBrooks Davis 	return (0);
11359d4fe4b2SBrooks Davis }
11369d4fe4b2SBrooks Davis 
1137f889d2efSBrooks Davis static int
1138f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
11399d4fe4b2SBrooks Davis {
11409d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1141114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
1142b4e9f837SBrooks Davis 
1143249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1144249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1145d148c2a2SMatt Joras 	/*
1146d148c2a2SMatt Joras 	 * We should have the only reference to the ifv now, so we can now
1147d148c2a2SMatt Joras 	 * drain any remaining lladdr task before freeing the ifnet and the
1148d148c2a2SMatt Joras 	 * ifvlan.
1149d148c2a2SMatt Joras 	 */
1150d148c2a2SMatt Joras 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
11514b22573aSBrooks Davis 	if_free(ifp);
11529d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1153b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
1154b4e9f837SBrooks Davis 
1155f889d2efSBrooks Davis 	return (0);
11569d4fe4b2SBrooks Davis }
11579d4fe4b2SBrooks Davis 
115815a66c21SBruce M Simpson /*
115915a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
116015a66c21SBruce M Simpson  */
11612cc2df49SGarrett Wollman static void
1162114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
11632cc2df49SGarrett Wollman {
11642cc2df49SGarrett Wollman }
11652cc2df49SGarrett Wollman 
11666d3a3ab7SGleb Smirnoff /*
1167d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
11686d3a3ab7SGleb Smirnoff  */
1169d9b1d615SJohn Baldwin static int
1170d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
11712cc2df49SGarrett Wollman {
11722cc2df49SGarrett Wollman 	struct ifvlan *ifv;
11732cc2df49SGarrett Wollman 	struct ifnet *p;
11742ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
11752ccbbd06SMarcelo Araujo 	uint16_t tag;
11761ad7a257SPyun YongHyeon 	int error, len, mcast;
1177d148c2a2SMatt Joras 	VLAN_LOCK_READER;
11782cc2df49SGarrett Wollman 
1179d148c2a2SMatt Joras 	VLAN_RLOCK();
11802cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
1181d148c2a2SMatt Joras 	if (TRUNK(ifv) == NULL) {
1182d148c2a2SMatt Joras 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1183d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1184d148c2a2SMatt Joras 		m_freem(m);
1185d148c2a2SMatt Joras 		return (ENETDOWN);
1186d148c2a2SMatt Joras 	}
118775ee267cSGleb Smirnoff 	p = PARENT(ifv);
11881ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
11891ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
11902cc2df49SGarrett Wollman 
1191a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
11922cc2df49SGarrett Wollman 
1193f731f104SBill Paul 	/*
1194d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
119524993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
119624993214SYaroslav Tykhiy 	 */
11972dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
1198a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1199d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1200d148c2a2SMatt Joras 		m_freem(m);
12018b20f6cfSHiroki Sato 		return (ENETDOWN);
120224993214SYaroslav Tykhiy 	}
120324993214SYaroslav Tykhiy 
120424993214SYaroslav Tykhiy 	/*
1205f6e5e0adSYaroslav Tykhiy 	 * Pad the frame to the minimum size allowed if told to.
1206f6e5e0adSYaroslav Tykhiy 	 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
1207f6e5e0adSYaroslav Tykhiy 	 * paragraph C.4.4.3.b.  It can help to work around buggy
1208f6e5e0adSYaroslav Tykhiy 	 * bridges that violate paragraph C.4.4.3.a from the same
1209f6e5e0adSYaroslav Tykhiy 	 * document, i.e., fail to pad short frames after untagging.
1210f6e5e0adSYaroslav Tykhiy 	 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
1211f6e5e0adSYaroslav Tykhiy 	 * untagging it will produce a 62-byte frame, which is a runt
1212f6e5e0adSYaroslav Tykhiy 	 * and requires padding.  There are VLAN-enabled network
1213f6e5e0adSYaroslav Tykhiy 	 * devices that just discard such runts instead or mishandle
1214f6e5e0adSYaroslav Tykhiy 	 * them somehow.
1215f6e5e0adSYaroslav Tykhiy 	 */
1216478e0520SHiroki Sato 	if (V_soft_pad && p->if_type == IFT_ETHER) {
1217f6e5e0adSYaroslav Tykhiy 		static char pad[8];	/* just zeros */
1218f6e5e0adSYaroslav Tykhiy 		int n;
1219f6e5e0adSYaroslav Tykhiy 
1220f6e5e0adSYaroslav Tykhiy 		for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
1221f6e5e0adSYaroslav Tykhiy 		     n > 0; n -= sizeof(pad))
1222f6e5e0adSYaroslav Tykhiy 			if (!m_append(m, min(n, sizeof(pad)), pad))
1223f6e5e0adSYaroslav Tykhiy 				break;
1224f6e5e0adSYaroslav Tykhiy 
1225f6e5e0adSYaroslav Tykhiy 		if (n > 0) {
1226f6e5e0adSYaroslav Tykhiy 			if_printf(ifp, "cannot pad short frame\n");
1227a58ea6b1SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1228d148c2a2SMatt Joras 			VLAN_RUNLOCK();
1229f6e5e0adSYaroslav Tykhiy 			m_freem(m);
1230d9b1d615SJohn Baldwin 			return (0);
1231f6e5e0adSYaroslav Tykhiy 		}
1232f6e5e0adSYaroslav Tykhiy 	}
1233f6e5e0adSYaroslav Tykhiy 
1234f6e5e0adSYaroslav Tykhiy 	/*
1235a3814acfSSam Leffler 	 * If underlying interface can do VLAN tag insertion itself,
1236a3814acfSSam Leffler 	 * just pass the packet along. However, we need some way to
1237a3814acfSSam Leffler 	 * tell the interface where the packet came from so that it
1238a3814acfSSam Leffler 	 * knows how to find the VLAN tag to use, so we attach a
1239a3814acfSSam Leffler 	 * packet tag that holds it.
1240f731f104SBill Paul 	 */
12412ccbbd06SMarcelo Araujo 	if (vlan_mtag_pcp && (mtag = m_tag_locate(m, MTAG_8021Q,
12422ccbbd06SMarcelo Araujo 	    MTAG_8021Q_PCP_OUT, NULL)) != NULL)
12432ccbbd06SMarcelo Araujo 		tag = EVL_MAKETAG(ifv->ifv_vid, *(uint8_t *)(mtag + 1), 0);
12442ccbbd06SMarcelo Araujo 	else
12452ccbbd06SMarcelo Araujo               tag = ifv->ifv_tag;
1246b08347a0SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
12472ccbbd06SMarcelo Araujo 		m->m_pkthdr.ether_vtag = tag;
1248ba26134bSGleb Smirnoff 		m->m_flags |= M_VLANTAG;
1249f731f104SBill Paul 	} else {
12502ccbbd06SMarcelo Araujo 		m = ether_vlanencap(m, tag);
12514af90a4dSMatthew N. Dodd 		if (m == NULL) {
1252d9b1d615SJohn Baldwin 			if_printf(ifp, "unable to prepend VLAN header\n");
1253a58ea6b1SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1254d148c2a2SMatt Joras 			VLAN_RUNLOCK();
1255d9b1d615SJohn Baldwin 			return (0);
12564af90a4dSMatthew N. Dodd 		}
1257f731f104SBill Paul 	}
12582cc2df49SGarrett Wollman 
12592cc2df49SGarrett Wollman 	/*
12602cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
12612cc2df49SGarrett Wollman 	 */
1262aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1263299153b5SAlexander V. Chernikov 	if (error == 0) {
1264a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1265a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1266a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
12671ad7a257SPyun YongHyeon 	} else
1268a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1269d148c2a2SMatt Joras 	VLAN_RUNLOCK();
1270d9b1d615SJohn Baldwin 	return (error);
12712cc2df49SGarrett Wollman }
1272d9b1d615SJohn Baldwin 
1273d9b1d615SJohn Baldwin /*
1274d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1275d9b1d615SJohn Baldwin  */
1276d9b1d615SJohn Baldwin static void
1277d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1278d9b1d615SJohn Baldwin {
1279f731f104SBill Paul }
1280f731f104SBill Paul 
1281a3814acfSSam Leffler static void
1282a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1283f731f104SBill Paul {
1284d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1285f731f104SBill Paul 	struct ifvlan *ifv;
1286d148c2a2SMatt Joras 	VLAN_LOCK_READER;
1287772b000fSAlexander V. Chernikov 	TRUNK_LOCK_READER;
12882ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
12892ccbbd06SMarcelo Araujo 	uint16_t vid, tag;
129075ee267cSGleb Smirnoff 
1291d148c2a2SMatt Joras 	VLAN_RLOCK();
1292d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1293d148c2a2SMatt Joras 	if (trunk == NULL) {
1294d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1295d148c2a2SMatt Joras 		m_freem(m);
1296d148c2a2SMatt Joras 		return;
1297d148c2a2SMatt Joras 	}
1298a3814acfSSam Leffler 
1299f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1300a3814acfSSam Leffler 		/*
130114e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1302a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1303a3814acfSSam Leffler 		 */
13042ccbbd06SMarcelo Araujo 		tag = m->m_pkthdr.ether_vtag;
13056ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1306a3814acfSSam Leffler 	} else {
130775ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
130875ee267cSGleb Smirnoff 
130914e98256SYaroslav Tykhiy 		/*
131014e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
131114e98256SYaroslav Tykhiy 		 */
1312a3814acfSSam Leffler 		switch (ifp->if_type) {
1313a3814acfSSam Leffler 		case IFT_ETHER:
1314a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1315a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1316a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1317d148c2a2SMatt Joras 				VLAN_RUNLOCK();
1318a3814acfSSam Leffler 				return;
1319a3814acfSSam Leffler 			}
1320a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
13212ccbbd06SMarcelo Araujo 			tag = ntohs(evl->evl_tag);
1322db8b5973SYaroslav Tykhiy 
1323db8b5973SYaroslav Tykhiy 			/*
13242dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
13252dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
13262dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
13272dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1328db8b5973SYaroslav Tykhiy 			 */
13292dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
13302dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
13312dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1332a3814acfSSam Leffler 			break;
13332dc879b3SYaroslav Tykhiy 
1334a3814acfSSam Leffler 		default:
1335db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
133660c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
133760c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1338a3814acfSSam Leffler #endif
13393751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1340d148c2a2SMatt Joras 			VLAN_RUNLOCK();
1341d148c2a2SMatt Joras 			m_freem(m);
134260c60618SYaroslav Tykhiy 			return;
1343a3814acfSSam Leffler 		}
13447a46ec8fSBrooks Davis 	}
13457a46ec8fSBrooks Davis 
13462ccbbd06SMarcelo Araujo 	vid = EVL_VLANOFTAG(tag);
13472ccbbd06SMarcelo Araujo 
134815ed2fa1SYaroslav Tykhiy 	TRUNK_RLOCK(trunk);
13497983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
13502dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
135175ee267cSGleb Smirnoff 		TRUNK_RUNLOCK(trunk);
13523751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1353d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1354d148c2a2SMatt Joras 		m_freem(m);
135575ee267cSGleb Smirnoff 		return;
135675ee267cSGleb Smirnoff 	}
135775ee267cSGleb Smirnoff 	TRUNK_RUNLOCK(trunk);
1358f731f104SBill Paul 
13592ccbbd06SMarcelo Araujo 	if (vlan_mtag_pcp) {
13602ccbbd06SMarcelo Araujo 		/*
13612ccbbd06SMarcelo Araujo 		 * While uncommon, it is possible that we will find a 802.1q
13622ccbbd06SMarcelo Araujo 		 * packet encapsulated inside another packet that also had an
13632ccbbd06SMarcelo Araujo 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
13642ccbbd06SMarcelo Araujo 		 * arriving over ethernet.  In that case, we replace the
13652ccbbd06SMarcelo Araujo 		 * existing 802.1q PCP m_tag value.
13662ccbbd06SMarcelo Araujo 		 */
13672ccbbd06SMarcelo Araujo 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
13682ccbbd06SMarcelo Araujo 		if (mtag == NULL) {
13692ccbbd06SMarcelo Araujo 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
13702ccbbd06SMarcelo Araujo 			    sizeof(uint8_t), M_NOWAIT);
13712ccbbd06SMarcelo Araujo 			if (mtag == NULL) {
13722ccbbd06SMarcelo Araujo 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1373d148c2a2SMatt Joras 				VLAN_RUNLOCK();
1374d148c2a2SMatt Joras 				m_freem(m);
13752ccbbd06SMarcelo Araujo 				return;
13762ccbbd06SMarcelo Araujo 			}
13772ccbbd06SMarcelo Araujo 			m_tag_prepend(m, mtag);
13782ccbbd06SMarcelo Araujo 		}
13792ccbbd06SMarcelo Araujo 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
13802ccbbd06SMarcelo Araujo 	}
13812ccbbd06SMarcelo Araujo 
1382fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1383c0304424SGleb Smirnoff 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1384d148c2a2SMatt Joras 	VLAN_RUNLOCK();
13852cc2df49SGarrett Wollman 
1386a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1387*fdbf1174SMatt Joras 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
13882cc2df49SGarrett Wollman }
13892cc2df49SGarrett Wollman 
1390d148c2a2SMatt Joras static void
1391d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused)
1392d148c2a2SMatt Joras {
1393d148c2a2SMatt Joras 	struct ifvlan *ifv;
1394d148c2a2SMatt Joras 	struct ifnet *ifp;
1395d148c2a2SMatt Joras 
1396d148c2a2SMatt Joras 	ifv = (struct ifvlan *)arg;
1397d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
1398d148c2a2SMatt Joras 	/* The ifv_ifp already has the lladdr copied in. */
1399d148c2a2SMatt Joras 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
1400d148c2a2SMatt Joras }
1401d148c2a2SMatt Joras 
14022cc2df49SGarrett Wollman static int
14037983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
14042cc2df49SGarrett Wollman {
140575ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
14061cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
140775ee267cSGleb Smirnoff 	int error = 0;
14082cc2df49SGarrett Wollman 
1409b1828acfSGleb Smirnoff 	/*
1410b1828acfSGleb Smirnoff 	 * We can handle non-ethernet hardware types as long as
1411b1828acfSGleb Smirnoff 	 * they handle the tagging and headers themselves.
1412b1828acfSGleb Smirnoff 	 */
1413e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1414e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
141515a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
141664a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
141764a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
1418b1828acfSGleb Smirnoff 	/*
1419b1828acfSGleb Smirnoff 	 * Don't let the caller set up a VLAN VID with
1420b1828acfSGleb Smirnoff 	 * anything except VLID bits.
1421b1828acfSGleb Smirnoff 	 * VID numbers 0x0 and 0xFFF are reserved.
1422b1828acfSGleb Smirnoff 	 */
1423b1828acfSGleb Smirnoff 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1424b1828acfSGleb Smirnoff 		return (EINVAL);
142575ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
142615a66c21SBruce M Simpson 		return (EBUSY);
14272cc2df49SGarrett Wollman 
1428d148c2a2SMatt Joras 	/* Acquire rmlock after the branch so we can M_WAITOK. */
1429d148c2a2SMatt Joras 	VLAN_XLOCK();
143075ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
143175ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
143275ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
143375ee267cSGleb Smirnoff 		vlan_inithash(trunk);
143475ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
1435d148c2a2SMatt Joras 		VLAN_WLOCK();
1436d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
143775ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
143875ee267cSGleb Smirnoff 		trunk->parent = p;
14399bcf3ae4SAlexander Motin 		if_ref(trunk->parent);
144075ee267cSGleb Smirnoff 	} else {
1441d148c2a2SMatt Joras 		VLAN_WLOCK();
144275ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
1443d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
144475ee267cSGleb Smirnoff 	}
144575ee267cSGleb Smirnoff 
14467983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
14472ccbbd06SMarcelo Araujo 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
14482ccbbd06SMarcelo Araujo 	vlan_tag_recalculate(ifv);
144975ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
145075ee267cSGleb Smirnoff 	if (error)
145175ee267cSGleb Smirnoff 		goto done;
145273f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1453a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1454a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
14551cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1456d89baa5aSAlexander Motin 	ifv->ifv_capenable = -1;
1457a3814acfSSam Leffler 
1458a3814acfSSam Leffler 	/*
1459a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1460a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1461656acce4SYaroslav Tykhiy 	 * use it.
1462a3814acfSSam Leffler 	 */
1463656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1464656acce4SYaroslav Tykhiy 		/*
1465656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1466656acce4SYaroslav Tykhiy 		 * handle extended frames.
1467656acce4SYaroslav Tykhiy 		 */
1468a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1469656acce4SYaroslav Tykhiy 	} else {
1470a3814acfSSam Leffler 		/*
1471a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1472a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1473a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1474a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1475a3814acfSSam Leffler 		 * which might still be useful.
1476a3814acfSSam Leffler 		 */
1477a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1478a3814acfSSam Leffler 	}
1479a3814acfSSam Leffler 
148075ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
14811cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1482e4cd31ddSJeff Roberson 	/*
1483e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1484e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1485e4cd31ddSJeff Roberson 	 * interfaces to also work.
1486e4cd31ddSJeff Roberson 	 */
14871cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
148875ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1489e4cd31ddSJeff Roberson 	ifp->if_output = p->if_output;
1490e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1491e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1492e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1493e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
1494e4cd31ddSJeff Roberson 
14952cc2df49SGarrett Wollman 	/*
149624993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
149724993214SYaroslav Tykhiy 	 * Other flags are none of our business.
14982cc2df49SGarrett Wollman 	 */
149964a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
15001cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
15011cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
15021cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
15031cf236fbSYaroslav Tykhiy 
15041cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
15052cc2df49SGarrett Wollman 
150675ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1507a3814acfSSam Leffler 
1508a3814acfSSam Leffler 	/*
1509e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
15102cc2df49SGarrett Wollman 	 * physical interface's.
15112cc2df49SGarrett Wollman 	 */
1512e4cd31ddSJeff Roberson 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1513e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1514e4cd31ddSJeff Roberson 	    p->if_addrlen;
15151b2a4f7aSBill Fenner 
15161b2a4f7aSBill Fenner 	/*
15171b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
15181b2a4f7aSBill Fenner 	 * joined on the vlan device.
15191b2a4f7aSBill Fenner 	 */
1520d148c2a2SMatt Joras 	(void)vlan_setmulti(ifp);
1521d148c2a2SMatt Joras 
1522d148c2a2SMatt Joras 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
15232ada9747SYaroslav Tykhiy 
15242ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
15252ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1526d148c2a2SMatt Joras 
1527d148c2a2SMatt Joras 	/* Update flags on the parent, if necessary. */
1528d148c2a2SMatt Joras 	vlan_setflags(ifp, 1);
152975ee267cSGleb Smirnoff done:
1530d148c2a2SMatt Joras 	/*
1531d148c2a2SMatt Joras 	 * We need to drop the non-sleepable rmlock so that the underlying
1532d148c2a2SMatt Joras 	 * devices can sleep in their vlan_config hooks.
1533d148c2a2SMatt Joras 	 */
1534d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1535d148c2a2SMatt Joras 	VLAN_WUNLOCK();
1536c725524cSJack F Vogel 	if (error == 0)
15377983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1538d148c2a2SMatt Joras 	VLAN_XUNLOCK();
153975ee267cSGleb Smirnoff 
154075ee267cSGleb Smirnoff 	return (error);
15412cc2df49SGarrett Wollman }
15422cc2df49SGarrett Wollman 
15436f359e28SJohn Baldwin static void
1544f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1545f731f104SBill Paul {
15465cb8c31aSYaroslav Tykhiy 
1547d148c2a2SMatt Joras 	VLAN_XLOCK();
154828cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
1549d148c2a2SMatt Joras 	VLAN_XUNLOCK();
15505cb8c31aSYaroslav Tykhiy }
15515cb8c31aSYaroslav Tykhiy 
15526f359e28SJohn Baldwin static void
155328cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
15545cb8c31aSYaroslav Tykhiy {
155575ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1556f731f104SBill Paul 	struct vlan_mc_entry *mc;
1557f731f104SBill Paul 	struct ifvlan *ifv;
1558c725524cSJack F Vogel 	struct ifnet  *parent;
155928cc4d37SJohn Baldwin 	int error;
1560f731f104SBill Paul 
1561d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
15624faedfe8SSam Leffler 
1563f731f104SBill Paul 	ifv = ifp->if_softc;
156475ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
156522893351SJack F Vogel 	parent = NULL;
1566f731f104SBill Paul 
156722893351SJack F Vogel 	if (trunk != NULL) {
1568d148c2a2SMatt Joras 		/*
1569d148c2a2SMatt Joras 		 * Both vlan_transmit and vlan_input rely on the trunk fields
1570d148c2a2SMatt Joras 		 * being NULL to determine whether to bail, so we need to get
1571d148c2a2SMatt Joras 		 * an exclusive lock here to prevent them from using bad
1572d148c2a2SMatt Joras 		 * ifvlans.
1573d148c2a2SMatt Joras 		 */
1574d148c2a2SMatt Joras 		VLAN_WLOCK();
157522893351SJack F Vogel 		parent = trunk->parent;
15761b2a4f7aSBill Fenner 
1577f731f104SBill Paul 		/*
1578f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1579f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
15801b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1581f731f104SBill Paul 		 */
1582c0cb022bSYaroslav Tykhiy 		while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
15836f359e28SJohn Baldwin 			/*
158428cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1585b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
158628cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
158728cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
158828cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
158928cc4d37SJohn Baldwin 			 * succeed.
15906f359e28SJohn Baldwin 			 */
159128cc4d37SJohn Baldwin 			if (!departing) {
159228cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1593e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
159428cc4d37SJohn Baldwin 				if (error)
159528cc4d37SJohn Baldwin 					if_printf(ifp,
159628cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
159728cc4d37SJohn Baldwin 					    error);
159828cc4d37SJohn Baldwin 			}
1599f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
16009d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
1601f731f104SBill Paul 		}
1602a3814acfSSam Leffler 
16031cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1604d148c2a2SMatt Joras 
1605d148c2a2SMatt Joras 		/*
1606d148c2a2SMatt Joras 		 * The trunk lock isn't actually required here, but
1607d148c2a2SMatt Joras 		 * vlan_remhash expects it.
1608d148c2a2SMatt Joras 		 */
1609d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
161075ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
1611d148c2a2SMatt Joras 		TRUNK_WUNLOCK(trunk);
161275ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
161375ee267cSGleb Smirnoff 
161475ee267cSGleb Smirnoff 		/*
161575ee267cSGleb Smirnoff 		 * Check if we were the last.
161675ee267cSGleb Smirnoff 		 */
161775ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
16182d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
161975ee267cSGleb Smirnoff 			trunk_destroy(trunk);
1620d148c2a2SMatt Joras 		}
1621d148c2a2SMatt Joras 		VLAN_WUNLOCK();
16221b2a4f7aSBill Fenner 	}
1623f731f104SBill Paul 
1624f731f104SBill Paul 	/* Disconnect from parent. */
16251cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
16261cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
16275cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
16285cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
16295cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1630f731f104SBill Paul 
163122893351SJack F Vogel 	/*
163222893351SJack F Vogel 	 * Only dispatch an event if vlan was
163322893351SJack F Vogel 	 * attached, otherwise there is nothing
163422893351SJack F Vogel 	 * to cleanup anyway.
163522893351SJack F Vogel 	 */
163622893351SJack F Vogel 	if (parent != NULL)
16377983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1638f731f104SBill Paul }
1639f731f104SBill Paul 
16401cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1641f731f104SBill Paul static int
16421cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
16431cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1644a3814acfSSam Leffler {
16451cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
16461cf236fbSYaroslav Tykhiy 	int error;
1647a3814acfSSam Leffler 
1648d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1649a3814acfSSam Leffler 
16501cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
16511cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
16521cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
16531cf236fbSYaroslav Tykhiy 
16541cf236fbSYaroslav Tykhiy 	/*
16551cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
16561cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
16571cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
16581cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
16591cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
16601cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
16611cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
16621cf236fbSYaroslav Tykhiy 	 */
16631cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
166475ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
16651cf236fbSYaroslav Tykhiy 		if (error)
1666a3814acfSSam Leffler 			return (error);
16671cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
16681cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
16691cf236fbSYaroslav Tykhiy 	}
16701cf236fbSYaroslav Tykhiy 	return (0);
16711cf236fbSYaroslav Tykhiy }
16721cf236fbSYaroslav Tykhiy 
16731cf236fbSYaroslav Tykhiy /*
16741cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
16751cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
16761cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
16771cf236fbSYaroslav Tykhiy  */
16781cf236fbSYaroslav Tykhiy static int
16791cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
16801cf236fbSYaroslav Tykhiy {
16811cf236fbSYaroslav Tykhiy 	int error, i;
16821cf236fbSYaroslav Tykhiy 
16831cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
16841cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
16851cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
16861cf236fbSYaroslav Tykhiy 		if (error)
16871cf236fbSYaroslav Tykhiy 			return (error);
16881cf236fbSYaroslav Tykhiy 	}
16891cf236fbSYaroslav Tykhiy 	return (0);
1690a3814acfSSam Leffler }
1691a3814acfSSam Leffler 
1692127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1693127d7b2dSAndre Oppermann static void
1694a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1695127d7b2dSAndre Oppermann {
1696d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1697127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
1698d148c2a2SMatt Joras 	VLAN_LOCK_READER;
1699127d7b2dSAndre Oppermann 
1700d148c2a2SMatt Joras 	/* Called from a taskqueue_swi task, so we cannot sleep. */
1701d148c2a2SMatt Joras 	VLAN_RLOCK();
1702d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1703d148c2a2SMatt Joras 	if (trunk == NULL) {
1704d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1705d148c2a2SMatt Joras 		return;
1706d148c2a2SMatt Joras 	}
1707d148c2a2SMatt Joras 
1708d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1709d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
1710aad0be7aSGleb Smirnoff 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1711fc74a9f9SBrooks Davis 		if_link_state_change(ifv->ifv_ifp,
171275ee267cSGleb Smirnoff 		    trunk->parent->if_link_state);
1713127d7b2dSAndre Oppermann 	}
1714d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1715d148c2a2SMatt Joras 	VLAN_RUNLOCK();
171675ee267cSGleb Smirnoff }
171775ee267cSGleb Smirnoff 
171875ee267cSGleb Smirnoff static void
171975ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
172075ee267cSGleb Smirnoff {
1721d148c2a2SMatt Joras 	struct ifnet *p;
1722d148c2a2SMatt Joras 	struct ifnet *ifp;
17239fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
1724d89baa5aSAlexander Motin 	int cap = 0, ena = 0, mena;
1725d89baa5aSAlexander Motin 	u_long hwa = 0;
172675ee267cSGleb Smirnoff 
1727d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1728d148c2a2SMatt Joras 	TRUNK_WLOCK_ASSERT(TRUNK(ifv));
1729d148c2a2SMatt Joras 	p = PARENT(ifv);
1730d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
173175ee267cSGleb Smirnoff 
1732d89baa5aSAlexander Motin 	/* Mask parent interface enabled capabilities disabled by user. */
1733d89baa5aSAlexander Motin 	mena = p->if_capenable & ifv->ifv_capenable;
1734d89baa5aSAlexander Motin 
173575ee267cSGleb Smirnoff 	/*
173675ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
173775ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
173875ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
173975ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
174075ee267cSGleb Smirnoff 	 */
174175ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1742d89baa5aSAlexander Motin 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
174375ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
174475ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1745d89baa5aSAlexander Motin 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1746d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM)
1747d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1748d89baa5aSAlexander Motin 			    CSUM_UDP | CSUM_SCTP);
1749d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM_IPV6)
1750d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1751d89baa5aSAlexander Motin 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
175275ee267cSGleb Smirnoff 	}
1753d89baa5aSAlexander Motin 
17549b76d9cbSPyun YongHyeon 	/*
17559b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
17569b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
17579b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
17589b76d9cbSPyun YongHyeon 	 */
17599fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
17609fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
17619fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
17629b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1763d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TSO;
17649b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1765d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TSO;
1766d89baa5aSAlexander Motin 		if (ena & IFCAP_TSO)
1767d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & CSUM_TSO;
17689b76d9cbSPyun YongHyeon 	}
176909fe6320SNavdeep Parhar 
177009fe6320SNavdeep Parhar 	/*
177159150e91SAlexander Motin 	 * If the parent interface can do LRO and checksum offloading on
177259150e91SAlexander Motin 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
177359150e91SAlexander Motin 	 * cost nothing, while false negative may lead to some confusions.
177459150e91SAlexander Motin 	 */
177559150e91SAlexander Motin 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
177659150e91SAlexander Motin 		cap |= p->if_capabilities & IFCAP_LRO;
177759150e91SAlexander Motin 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
177859150e91SAlexander Motin 		ena |= p->if_capenable & IFCAP_LRO;
177959150e91SAlexander Motin 
178059150e91SAlexander Motin 	/*
178109fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
178209fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
178309fe6320SNavdeep Parhar 	 *
178409fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
178509fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
178609fe6320SNavdeep Parhar 	 * with its own bit.
178709fe6320SNavdeep Parhar 	 */
178809fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
178909fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1790d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TOE;
179109fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
179209fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
1793d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TOE;
179409fe6320SNavdeep Parhar 	}
1795f3e7afe2SHans Petter Selasky 
1796d89baa5aSAlexander Motin 	/*
1797d89baa5aSAlexander Motin 	 * If the parent interface supports dynamic link state, so does the
1798d89baa5aSAlexander Motin 	 * VLAN interface.
1799d89baa5aSAlexander Motin 	 */
1800d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1801d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_LINKSTATE);
1802d89baa5aSAlexander Motin 
1803f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1804f3e7afe2SHans Petter Selasky 	/*
1805f3e7afe2SHans Petter Selasky 	 * If the parent interface supports ratelimiting, so does the
1806f3e7afe2SHans Petter Selasky 	 * VLAN interface.
1807f3e7afe2SHans Petter Selasky 	 */
1808d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1809d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_TXRTLMT);
1810f3e7afe2SHans Petter Selasky #endif
1811d89baa5aSAlexander Motin 
1812d89baa5aSAlexander Motin 	ifp->if_capabilities = cap;
1813d89baa5aSAlexander Motin 	ifp->if_capenable = ena;
1814d89baa5aSAlexander Motin 	ifp->if_hwassist = hwa;
181575ee267cSGleb Smirnoff }
181675ee267cSGleb Smirnoff 
181775ee267cSGleb Smirnoff static void
181875ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
181975ee267cSGleb Smirnoff {
1820d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
182175ee267cSGleb Smirnoff 	struct ifvlan *ifv;
182275ee267cSGleb Smirnoff 
1823d148c2a2SMatt Joras 	VLAN_SLOCK();
1824d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1825d148c2a2SMatt Joras 	if (trunk == NULL) {
1826d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1827d148c2a2SMatt Joras 		return;
1828d148c2a2SMatt Joras 	}
1829d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1830d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
183175ee267cSGleb Smirnoff 		vlan_capabilities(ifv);
183275ee267cSGleb Smirnoff 	}
1833d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1834d148c2a2SMatt Joras 	VLAN_SUNLOCK();
1835127d7b2dSAndre Oppermann }
1836127d7b2dSAndre Oppermann 
1837a3814acfSSam Leffler static int
1838cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
18392cc2df49SGarrett Wollman {
18402cc2df49SGarrett Wollman 	struct ifnet *p;
18412cc2df49SGarrett Wollman 	struct ifreq *ifr;
1842e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
18432cc2df49SGarrett Wollman 	struct ifvlan *ifv;
18442d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
18452cc2df49SGarrett Wollman 	struct vlanreq vlr;
18462cc2df49SGarrett Wollman 	int error = 0;
1847d148c2a2SMatt Joras 	VLAN_LOCK_READER;
18482cc2df49SGarrett Wollman 
18492cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1850e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
18512cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
18522cc2df49SGarrett Wollman 
18532cc2df49SGarrett Wollman 	switch (cmd) {
1854e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1855e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1856e4cd31ddSJeff Roberson #ifdef INET
1857e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1858e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1859e4cd31ddSJeff Roberson #endif
1860e4cd31ddSJeff Roberson 		break;
1861e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
1862e4cd31ddSJeff Roberson                 {
1863e4cd31ddSJeff Roberson 			struct sockaddr *sa;
1864e4cd31ddSJeff Roberson 
1865e4cd31ddSJeff Roberson 			sa = (struct sockaddr *)&ifr->ifr_data;
1866e4cd31ddSJeff Roberson 			bcopy(IF_LLADDR(ifp), sa->sa_data, ifp->if_addrlen);
1867e4cd31ddSJeff Roberson                 }
1868e4cd31ddSJeff Roberson 		break;
1869b3cca108SBill Fenner 	case SIOCGIFMEDIA:
1870d148c2a2SMatt Joras 		VLAN_SLOCK();
187175ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1872d8564efdSEd Maste 			p = PARENT(ifv);
18739bcf3ae4SAlexander Motin 			if_ref(p);
1874d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
18759bcf3ae4SAlexander Motin 			if_rele(p);
1876b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1877b3cca108SBill Fenner 			if (error == 0) {
1878b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1879b3cca108SBill Fenner 
1880b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1881b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1882b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1883b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1884b3cca108SBill Fenner 						ifmr->ifm_ulist,
1885b3cca108SBill Fenner 						sizeof(int));
1886b3cca108SBill Fenner 				}
1887b3cca108SBill Fenner 			}
18884faedfe8SSam Leffler 		} else {
1889b3cca108SBill Fenner 			error = EINVAL;
18904faedfe8SSam Leffler 		}
1891d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1892b3cca108SBill Fenner 		break;
1893b3cca108SBill Fenner 
1894b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1895b3cca108SBill Fenner 		error = EINVAL;
1896b3cca108SBill Fenner 		break;
1897b3cca108SBill Fenner 
18982cc2df49SGarrett Wollman 	case SIOCSIFMTU:
18992cc2df49SGarrett Wollman 		/*
19002cc2df49SGarrett Wollman 		 * Set the interface MTU.
19012cc2df49SGarrett Wollman 		 */
1902d148c2a2SMatt Joras 		VLAN_SLOCK();
1903d148c2a2SMatt Joras 		trunk = TRUNK(ifv);
1904d148c2a2SMatt Joras 		if (trunk != NULL) {
1905d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1906a3814acfSSam Leffler 			if (ifr->ifr_mtu >
190775ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1908a3814acfSSam Leffler 			    ifr->ifr_mtu <
1909a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
19102cc2df49SGarrett Wollman 				error = EINVAL;
1911a3814acfSSam Leffler 			else
19122cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1913d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
1914a3814acfSSam Leffler 		} else
1915a3814acfSSam Leffler 			error = EINVAL;
1916d148c2a2SMatt Joras 		VLAN_SUNLOCK();
19172cc2df49SGarrett Wollman 		break;
19182cc2df49SGarrett Wollman 
19192cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1920ccf7ba97SMarko Zec #ifdef VIMAGE
192115f6780eSRobert Watson 		/*
192215f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
192315f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
192415f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
192515f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
19265a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
192715f6780eSRobert Watson 		 */
1928ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1929ccf7ba97SMarko Zec 			error = EPERM;
1930ccf7ba97SMarko Zec 			break;
1931ccf7ba97SMarko Zec 		}
1932ccf7ba97SMarko Zec #endif
193315a66c21SBruce M Simpson 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
19342cc2df49SGarrett Wollman 		if (error)
19352cc2df49SGarrett Wollman 			break;
19362cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1937f731f104SBill Paul 			vlan_unconfig(ifp);
19382cc2df49SGarrett Wollman 			break;
19392cc2df49SGarrett Wollman 		}
19409bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
19411bdc73d3SEd Maste 		if (p == NULL) {
19422cc2df49SGarrett Wollman 			error = ENOENT;
19432cc2df49SGarrett Wollman 			break;
19442cc2df49SGarrett Wollman 		}
194575ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
19469bcf3ae4SAlexander Motin 		if_rele(p);
19472cc2df49SGarrett Wollman 		break;
19482cc2df49SGarrett Wollman 
19492cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1950ccf7ba97SMarko Zec #ifdef VIMAGE
1951ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1952ccf7ba97SMarko Zec 			error = EPERM;
1953ccf7ba97SMarko Zec 			break;
1954ccf7ba97SMarko Zec 		}
1955ccf7ba97SMarko Zec #endif
195615a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
1957d148c2a2SMatt Joras 		VLAN_SLOCK();
195875ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
195975ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
19609bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
19617983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
19622cc2df49SGarrett Wollman 		}
1963d148c2a2SMatt Joras 		VLAN_SUNLOCK();
196415a66c21SBruce M Simpson 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
19652cc2df49SGarrett Wollman 		break;
19662cc2df49SGarrett Wollman 
19672cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
19682cc2df49SGarrett Wollman 		/*
19691cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
19701cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
19712cc2df49SGarrett Wollman 		 */
1972d148c2a2SMatt Joras 		VLAN_XLOCK();
197375ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
19741cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
1975d148c2a2SMatt Joras 		VLAN_XUNLOCK();
19762cc2df49SGarrett Wollman 		break;
1977a3814acfSSam Leffler 
1978f731f104SBill Paul 	case SIOCADDMULTI:
1979f731f104SBill Paul 	case SIOCDELMULTI:
198075ee267cSGleb Smirnoff 		/*
198175ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
198275ee267cSGleb Smirnoff 		 * when we do.
1983d148c2a2SMatt Joras 		 *
1984d148c2a2SMatt Joras 		 * XXX We need the rmlock here to avoid sleeping while
1985d148c2a2SMatt Joras 		 * holding in6_multi_mtx.
198675ee267cSGleb Smirnoff 		 */
1987d148c2a2SMatt Joras 		VLAN_RLOCK();
19882d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
19892d222cb7SAlexander Motin 		if (trunk != NULL) {
1990d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1991f731f104SBill Paul 			error = vlan_setmulti(ifp);
1992d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
19932d222cb7SAlexander Motin 		}
1994d148c2a2SMatt Joras 		VLAN_RUNLOCK();
1995f731f104SBill Paul 		break;
199675ee267cSGleb Smirnoff 
19972ccbbd06SMarcelo Araujo 	case SIOCGVLANPCP:
19982ccbbd06SMarcelo Araujo #ifdef VIMAGE
19992ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
20002ccbbd06SMarcelo Araujo 			error = EPERM;
20012ccbbd06SMarcelo Araujo 			break;
20022ccbbd06SMarcelo Araujo 		}
20032ccbbd06SMarcelo Araujo #endif
20042ccbbd06SMarcelo Araujo 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
20052ccbbd06SMarcelo Araujo 		break;
20062ccbbd06SMarcelo Araujo 
20072ccbbd06SMarcelo Araujo 	case SIOCSVLANPCP:
20082ccbbd06SMarcelo Araujo #ifdef VIMAGE
20092ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
20102ccbbd06SMarcelo Araujo 			error = EPERM;
20112ccbbd06SMarcelo Araujo 			break;
20122ccbbd06SMarcelo Araujo 		}
20132ccbbd06SMarcelo Araujo #endif
20142ccbbd06SMarcelo Araujo 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
20152ccbbd06SMarcelo Araujo 		if (error)
20162ccbbd06SMarcelo Araujo 			break;
20172ccbbd06SMarcelo Araujo 		if (ifr->ifr_vlan_pcp > 7) {
20182ccbbd06SMarcelo Araujo 			error = EINVAL;
20192ccbbd06SMarcelo Araujo 			break;
20202ccbbd06SMarcelo Araujo 		}
20212ccbbd06SMarcelo Araujo 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
20222ccbbd06SMarcelo Araujo 		vlan_tag_recalculate(ifv);
20232ccbbd06SMarcelo Araujo 		break;
20242ccbbd06SMarcelo Araujo 
2025d89baa5aSAlexander Motin 	case SIOCSIFCAP:
2026d148c2a2SMatt Joras 		VLAN_SLOCK();
2027d89baa5aSAlexander Motin 		ifv->ifv_capenable = ifr->ifr_reqcap;
2028d89baa5aSAlexander Motin 		trunk = TRUNK(ifv);
2029d89baa5aSAlexander Motin 		if (trunk != NULL) {
2030d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
2031d89baa5aSAlexander Motin 			vlan_capabilities(ifv);
2032d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
2033d89baa5aSAlexander Motin 		}
2034d148c2a2SMatt Joras 		VLAN_SUNLOCK();
2035d89baa5aSAlexander Motin 		break;
2036d89baa5aSAlexander Motin 
20372cc2df49SGarrett Wollman 	default:
2038e4cd31ddSJeff Roberson 		error = EINVAL;
2039e4cd31ddSJeff Roberson 		break;
20402cc2df49SGarrett Wollman 	}
204115a66c21SBruce M Simpson 
204215a66c21SBruce M Simpson 	return (error);
20432cc2df49SGarrett Wollman }
2044f3e7afe2SHans Petter Selasky 
2045f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
2046f3e7afe2SHans Petter Selasky static int
2047f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp,
2048f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *params,
2049f3e7afe2SHans Petter Selasky     struct m_snd_tag **ppmt)
2050f3e7afe2SHans Petter Selasky {
2051f3e7afe2SHans Petter Selasky 
2052f3e7afe2SHans Petter Selasky 	/* get trunk device */
2053f3e7afe2SHans Petter Selasky 	ifp = vlan_trunkdev(ifp);
2054f3e7afe2SHans Petter Selasky 	if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
2055f3e7afe2SHans Petter Selasky 		return (EOPNOTSUPP);
2056f3e7afe2SHans Petter Selasky 	/* forward allocation request */
2057f3e7afe2SHans Petter Selasky 	return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
2058f3e7afe2SHans Petter Selasky }
2059f3e7afe2SHans Petter Selasky #endif
2060