xref: /freebsd/sys/net/if_vlan.c (revision 2884a93651a85d419e271b8fb6aaf3d91408d545)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32ccbbd06SMarcelo Araujo  * Copyright 2012 ADARA Networks, Inc.
4d148c2a2SMatt Joras  * Copyright 2017 Dell EMC Isilon
52ccbbd06SMarcelo Araujo  *
62ccbbd06SMarcelo Araujo  * Portions of this software were developed by Robert N. M. Watson under
72ccbbd06SMarcelo Araujo  * contract to ADARA Networks, Inc.
82cc2df49SGarrett Wollman  *
92cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
102cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
112cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
122cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
132cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
142cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
152cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
162cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
172cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
182cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
192cc2df49SGarrett Wollman  * warranty.
202cc2df49SGarrett Wollman  *
212cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
222cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
232cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
242cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
252cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
262cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
272cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
282cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
292cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
302cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
312cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322cc2df49SGarrett Wollman  * SUCH DAMAGE.
332cc2df49SGarrett Wollman  */
342cc2df49SGarrett Wollman 
352cc2df49SGarrett Wollman /*
362cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
372ccbbd06SMarcelo Araujo  * This is sort of sneaky in the implementation, since
382cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
392cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
402cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
41d9b1d615SJohn Baldwin  * ether_output() sends to us via if_transmit(), rewrite them for
42d9b1d615SJohn Baldwin  * use by the real outgoing interface, and ask it to send them.
432cc2df49SGarrett Wollman  */
442cc2df49SGarrett Wollman 
458b2d9181SPyun YongHyeon #include <sys/cdefs.h>
468b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$");
478b2d9181SPyun YongHyeon 
482c5b403eSOleg Bulyzhin #include "opt_inet.h"
4984becee1SAlexander Motin #include "opt_inet6.h"
50b2e60773SJohn Baldwin #include "opt_kern_tls.h"
5175ee267cSGleb Smirnoff #include "opt_vlan.h"
52f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
532cc2df49SGarrett Wollman 
542cc2df49SGarrett Wollman #include <sys/param.h>
55c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
562cc2df49SGarrett Wollman #include <sys/kernel.h>
5775ee267cSGleb Smirnoff #include <sys/lock.h>
58f731f104SBill Paul #include <sys/malloc.h>
592cc2df49SGarrett Wollman #include <sys/mbuf.h>
602b120974SPeter Wemm #include <sys/module.h>
61772b000fSAlexander V. Chernikov #include <sys/rmlock.h>
622ccbbd06SMarcelo Araujo #include <sys/priv.h>
63f731f104SBill Paul #include <sys/queue.h>
642cc2df49SGarrett Wollman #include <sys/socket.h>
652cc2df49SGarrett Wollman #include <sys/sockio.h>
662cc2df49SGarrett Wollman #include <sys/sysctl.h>
672cc2df49SGarrett Wollman #include <sys/systm.h>
68e4cd31ddSJeff Roberson #include <sys/sx.h>
69d148c2a2SMatt Joras #include <sys/taskqueue.h>
702cc2df49SGarrett Wollman 
712cc2df49SGarrett Wollman #include <net/bpf.h>
722cc2df49SGarrett Wollman #include <net/ethernet.h>
732cc2df49SGarrett Wollman #include <net/if.h>
7476039bc8SGleb Smirnoff #include <net/if_var.h>
75f889d2efSBrooks Davis #include <net/if_clone.h>
762cc2df49SGarrett Wollman #include <net/if_dl.h>
772cc2df49SGarrett Wollman #include <net/if_types.h>
782cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
7984becee1SAlexander Motin #include <net/route.h>
804b79449eSBjoern A. Zeeb #include <net/vnet.h>
812cc2df49SGarrett Wollman 
822c5b403eSOleg Bulyzhin #ifdef INET
832c5b403eSOleg Bulyzhin #include <netinet/in.h>
842c5b403eSOleg Bulyzhin #include <netinet/if_ether.h>
852c5b403eSOleg Bulyzhin #endif
862c5b403eSOleg Bulyzhin 
8784becee1SAlexander Motin #ifdef INET6
8884becee1SAlexander Motin /*
8984becee1SAlexander Motin  * XXX: declare here to avoid to include many inet6 related files..
9084becee1SAlexander Motin  * should be more generalized?
9184becee1SAlexander Motin  */
9284becee1SAlexander Motin extern void	nd6_setmtu(struct ifnet *);
9384becee1SAlexander Motin #endif
9484becee1SAlexander Motin 
9575ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
9664a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
9775ee267cSGleb Smirnoff 
982dc879b3SYaroslav Tykhiy #define	UP_AND_RUNNING(ifp) \
992dc879b3SYaroslav Tykhiy     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
1002dc879b3SYaroslav Tykhiy 
101b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan);
10275ee267cSGleb Smirnoff 
10375ee267cSGleb Smirnoff struct ifvlantrunk {
10475ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
105b08d611dSMatt Macy 	struct	mtx	lock;
10675ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
1075cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
1085cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
10975ee267cSGleb Smirnoff #else
11075ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
11175ee267cSGleb Smirnoff 	uint16_t	hmask;
11275ee267cSGleb Smirnoff 	uint16_t	hwidth;
11375ee267cSGleb Smirnoff #endif
11475ee267cSGleb Smirnoff 	int		refcnt;
11575ee267cSGleb Smirnoff };
1169d4fe4b2SBrooks Davis 
117b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
118fb3bc596SJohn Baldwin struct vlan_snd_tag {
119fb3bc596SJohn Baldwin 	struct m_snd_tag com;
120fb3bc596SJohn Baldwin 	struct m_snd_tag *tag;
121fb3bc596SJohn Baldwin };
122fb3bc596SJohn Baldwin 
123fb3bc596SJohn Baldwin static inline struct vlan_snd_tag *
124fb3bc596SJohn Baldwin mst_to_vst(struct m_snd_tag *mst)
125fb3bc596SJohn Baldwin {
126fb3bc596SJohn Baldwin 
127fb3bc596SJohn Baldwin 	return (__containerof(mst, struct vlan_snd_tag, com));
128fb3bc596SJohn Baldwin }
129fb3bc596SJohn Baldwin #endif
130fb3bc596SJohn Baldwin 
131d148c2a2SMatt Joras /*
132d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk with
133d148c2a2SMatt Joras  * the assumption that none will be added/removed during iteration.
134d148c2a2SMatt Joras  */
135d148c2a2SMatt Joras #ifdef VLAN_ARRAY
136d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
137d148c2a2SMatt Joras 	size_t _i; \
138d148c2a2SMatt Joras 	for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
139d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
140d148c2a2SMatt Joras #else /* VLAN_ARRAY */
141d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
142d148c2a2SMatt Joras 	struct ifvlan *_next; \
143d148c2a2SMatt Joras 	size_t _i; \
144d148c2a2SMatt Joras 	for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
145b08d611dSMatt Macy 		CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
146d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
147d148c2a2SMatt Joras 
148d148c2a2SMatt Joras /*
149d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk while
150d148c2a2SMatt Joras  * also modifying the number of vlans on the trunk. The iteration continues
151d148c2a2SMatt Joras  * until some condition is met or there are no more vlans on the trunk.
152d148c2a2SMatt Joras  */
153d148c2a2SMatt Joras #ifdef VLAN_ARRAY
154d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
155d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
156d148c2a2SMatt Joras 	size_t _i; \
157d148c2a2SMatt Joras 	for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
158d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]))
159d148c2a2SMatt Joras #else /* VLAN_ARRAY */
160d148c2a2SMatt Joras /*
161d148c2a2SMatt Joras  * The hash table case is more complicated. We allow for the hash table to be
162d148c2a2SMatt Joras  * modified (i.e. vlans removed) while we are iterating over it. To allow for
163d148c2a2SMatt Joras  * this we must restart the iteration every time we "touch" something during
164d148c2a2SMatt Joras  * the iteration, since removal will resize the hash table and invalidate our
165d148c2a2SMatt Joras  * current position. If acting on the touched element causes the trunk to be
166d148c2a2SMatt Joras  * emptied, then iteration also stops.
167d148c2a2SMatt Joras  */
168d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
169d148c2a2SMatt Joras 	size_t _i; \
170d148c2a2SMatt Joras 	bool _touch = false; \
171d148c2a2SMatt Joras 	for (_i = 0; \
172d148c2a2SMatt Joras 	    !(_cond) && _i < (1 << (_trunk)->hwidth); \
173d148c2a2SMatt Joras 	    _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
174b08d611dSMatt Macy 		if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
175d148c2a2SMatt Joras 		    (_touch = true))
176d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
177d148c2a2SMatt Joras 
178a3814acfSSam Leffler struct vlan_mc_entry {
179e4cd31ddSJeff Roberson 	struct sockaddr_dl		mc_addr;
180b08d611dSMatt Macy 	CK_SLIST_ENTRY(vlan_mc_entry)	mc_entries;
181c32a9d66SHans Petter Selasky 	struct epoch_context		mc_epoch_ctx;
182a3814acfSSam Leffler };
183a3814acfSSam Leffler 
184a3814acfSSam Leffler struct ifvlan {
18575ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
186fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
18775ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
188c7cffd65SAlexander V. Chernikov #define	PARENT(ifv)	(TRUNK(ifv)->parent)
1896667db31SAlexander V. Chernikov 	void	*ifv_cookie;
1901cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
191d89baa5aSAlexander Motin 	int	ifv_capenable;
19272755d28SMark Johnston 	int	ifv_encaplen;	/* encapsulation length */
19372755d28SMark Johnston 	int	ifv_mtufudge;	/* MTU fudged by this much */
19472755d28SMark Johnston 	int	ifv_mintu;	/* min transmission unit */
195c7cffd65SAlexander V. Chernikov 	struct  ether_8021q_tag ifv_qtag;
196c7cffd65SAlexander V. Chernikov #define ifv_proto	ifv_qtag.proto
197c7cffd65SAlexander V. Chernikov #define ifv_vid		ifv_qtag.vid
198c7cffd65SAlexander V. Chernikov #define ifv_pcp		ifv_qtag.pcp
199d148c2a2SMatt Joras 	struct task lladdr_task;
200b08d611dSMatt Macy 	CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
201c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY
202b08d611dSMatt Macy 	CK_SLIST_ENTRY(ifvlan) ifv_list;
203c0cb022bSYaroslav Tykhiy #endif
204a3814acfSSam Leffler };
205a3814acfSSam Leffler 
20675ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
2071cf236fbSYaroslav Tykhiy static struct {
2081cf236fbSYaroslav Tykhiy 	int flag;
2091cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
2101cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
2111cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
2121cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
2131cf236fbSYaroslav Tykhiy 	{0, NULL}
2141cf236fbSYaroslav Tykhiy };
215a3814acfSSam Leffler 
21678bc3d5eSKristof Provost VNET_DECLARE(int, vlan_mtag_pcp);
21778bc3d5eSKristof Provost #define	V_vlan_mtag_pcp	VNET(vlan_mtag_pcp)
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 /*
226b08d611dSMatt Macy  * if_vlan uses two module-level synchronizations primitives to allow concurrent
227b08d611dSMatt Macy  * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
228b08d611dSMatt Macy  * while they are being used for tx/rx. To accomplish this in a way that has
229b08d611dSMatt Macy  * acceptable performance and cooperation with other parts of the network stack
230b08d611dSMatt Macy  * there is a non-sleepable epoch(9) and an sx(9).
23175ee267cSGleb Smirnoff  *
232b08d611dSMatt Macy  * The performance-sensitive paths that warrant using the epoch(9) are
233d148c2a2SMatt Joras  * vlan_transmit and vlan_input. Both have to check for the vlan interface's
234d148c2a2SMatt Joras  * existence using if_vlantrunk, and being in the network tx/rx paths the use
235b08d611dSMatt Macy  * of an epoch(9) gives a measureable improvement in performance.
23675ee267cSGleb Smirnoff  *
237d148c2a2SMatt Joras  * The reason for having an sx(9) is mostly because there are still areas that
238d148c2a2SMatt Joras  * must be sleepable and also have safe concurrent access to a vlan interface.
239d148c2a2SMatt Joras  * Since the sx(9) exists, it is used by default in most paths unless sleeping
240d148c2a2SMatt Joras  * is not permitted, or if it is not clear whether sleeping is permitted.
241d148c2a2SMatt Joras  *
2424faedfe8SSam Leffler  */
243d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx
244d148c2a2SMatt Joras 
245d148c2a2SMatt Joras static struct sx _VLAN_SX_ID;
246d148c2a2SMatt Joras 
247d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \
248c7cffd65SAlexander V. Chernikov 	sx_init_flags(&_VLAN_SX_ID, "vlan_sx", SX_RECURSE)
249d148c2a2SMatt Joras 
250d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \
251d148c2a2SMatt Joras 	sx_destroy(&_VLAN_SX_ID)
252d148c2a2SMatt Joras 
253d148c2a2SMatt Joras #define	VLAN_SLOCK()			sx_slock(&_VLAN_SX_ID)
254d148c2a2SMatt Joras #define	VLAN_SUNLOCK()			sx_sunlock(&_VLAN_SX_ID)
255d148c2a2SMatt Joras #define	VLAN_XLOCK()			sx_xlock(&_VLAN_SX_ID)
256d148c2a2SMatt Joras #define	VLAN_XUNLOCK()			sx_xunlock(&_VLAN_SX_ID)
257d148c2a2SMatt Joras #define	VLAN_SLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
258d148c2a2SMatt Joras #define	VLAN_XLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
259d148c2a2SMatt Joras #define	VLAN_SXLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_LOCKED)
260d148c2a2SMatt Joras 
261d148c2a2SMatt Joras /*
262b08d611dSMatt Macy  * We also have a per-trunk mutex that should be acquired when changing
263b08d611dSMatt Macy  * its state.
264d148c2a2SMatt Joras  */
265b08d611dSMatt Macy #define	TRUNK_LOCK_INIT(trunk)		mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
266b08d611dSMatt Macy #define	TRUNK_LOCK_DESTROY(trunk)	mtx_destroy(&(trunk)->lock)
267b08d611dSMatt Macy #define	TRUNK_WLOCK(trunk)		mtx_lock(&(trunk)->lock)
268b08d611dSMatt Macy #define	TRUNK_WUNLOCK(trunk)		mtx_unlock(&(trunk)->lock)
269b08d611dSMatt Macy #define	TRUNK_WLOCK_ASSERT(trunk)	mtx_assert(&(trunk)->lock, MA_OWNED);
27075ee267cSGleb Smirnoff 
271d148c2a2SMatt Joras /*
272d148c2a2SMatt Joras  * The VLAN_ARRAY substitutes the dynamic hash with a static array
273d148c2a2SMatt Joras  * with 4096 entries. In theory this can give a boost in processing,
274d148c2a2SMatt Joras  * however in practice it does not. Probably this is because the array
275d148c2a2SMatt Joras  * is too big to fit into CPU cache.
276d148c2a2SMatt Joras  */
27775ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
27875ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
27975ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
28075ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
28175ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
28275ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
28375ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
2847983103aSRobert Watson 	uint16_t vid);
28575ee267cSGleb Smirnoff #endif
28675ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
2874faedfe8SSam Leffler 
288114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
289a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
290cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
291b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
292f3e7afe2SHans Petter Selasky static	int vlan_snd_tag_alloc(struct ifnet *,
293f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *, struct m_snd_tag **);
294fb3bc596SJohn Baldwin static	int vlan_snd_tag_modify(struct m_snd_tag *,
295fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *);
296fb3bc596SJohn Baldwin static	int vlan_snd_tag_query(struct m_snd_tag *,
297fb3bc596SJohn Baldwin     union if_snd_tag_query_params *);
298fa91f845SRandall Stewart static	void vlan_snd_tag_free(struct m_snd_tag *);
2991a714ff2SRandall Stewart static struct m_snd_tag *vlan_next_snd_tag(struct m_snd_tag *);
3001a714ff2SRandall Stewart static void vlan_ratelimit_query(struct ifnet *,
3011a714ff2SRandall Stewart     struct if_ratelimit_query_results *);
302f3e7afe2SHans Petter Selasky #endif
303d9b1d615SJohn Baldwin static	void vlan_qflush(struct ifnet *ifp);
3041cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
3051cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
3061cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
307f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
308d9b1d615SJohn Baldwin static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
3092e5ff01dSLuiz Otavio O Souza #ifdef ALTQ
3102e5ff01dSLuiz Otavio O Souza static void vlan_altq_start(struct ifnet *ifp);
3112e5ff01dSLuiz Otavio O Souza static	int vlan_altq_transmit(struct ifnet *ifp, struct mbuf *m);
3122e5ff01dSLuiz Otavio O Souza #endif
31316cf6bdbSMatt Joras static	int vlan_output(struct ifnet *ifp, struct mbuf *m,
31416cf6bdbSMatt Joras     const struct sockaddr *dst, struct route *ro);
3156f359e28SJohn Baldwin static	void vlan_unconfig(struct ifnet *ifp);
31628cc4d37SJohn Baldwin static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
317c7cffd65SAlexander V. Chernikov static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag,
318c7cffd65SAlexander V. Chernikov 	uint16_t proto);
319a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
32075ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
32175ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
322f731f104SBill Paul 
323f941c31aSGleb Smirnoff static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
324f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
3256b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
326f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
327f889d2efSBrooks Davis 
3285cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
329ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
3305cb8c31aSYaroslav Tykhiy 
331d148c2a2SMatt Joras static  void vlan_lladdr_fn(void *arg, int pending);
332d148c2a2SMatt Joras 
33342a58907SGleb Smirnoff static struct if_clone *vlan_cloner;
3349d4fe4b2SBrooks Davis 
335ccf7ba97SMarko Zec #ifdef VIMAGE
3365f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
337ccf7ba97SMarko Zec #define	V_vlan_cloner	VNET(vlan_cloner)
338ccf7ba97SMarko Zec #endif
339ccf7ba97SMarko Zec 
340c782ea8bSJohn Baldwin #ifdef RATELIMIT
341c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_ul_sw = {
342c782ea8bSJohn Baldwin 	.snd_tag_modify = vlan_snd_tag_modify,
343c782ea8bSJohn Baldwin 	.snd_tag_query = vlan_snd_tag_query,
344c782ea8bSJohn Baldwin 	.snd_tag_free = vlan_snd_tag_free,
345c782ea8bSJohn Baldwin 	.next_snd_tag = vlan_next_snd_tag,
346c782ea8bSJohn Baldwin 	.type = IF_SND_TAG_TYPE_UNLIMITED
347c782ea8bSJohn Baldwin };
348c782ea8bSJohn Baldwin 
349c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_rl_sw = {
350c782ea8bSJohn Baldwin 	.snd_tag_modify = vlan_snd_tag_modify,
351c782ea8bSJohn Baldwin 	.snd_tag_query = vlan_snd_tag_query,
352c782ea8bSJohn Baldwin 	.snd_tag_free = vlan_snd_tag_free,
353c782ea8bSJohn Baldwin 	.next_snd_tag = vlan_next_snd_tag,
354c782ea8bSJohn Baldwin 	.type = IF_SND_TAG_TYPE_RATE_LIMIT
355c782ea8bSJohn Baldwin };
356c782ea8bSJohn Baldwin #endif
357c782ea8bSJohn Baldwin 
358c782ea8bSJohn Baldwin #ifdef KERN_TLS
359c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_sw = {
360c782ea8bSJohn Baldwin 	.snd_tag_modify = vlan_snd_tag_modify,
361c782ea8bSJohn Baldwin 	.snd_tag_query = vlan_snd_tag_query,
362c782ea8bSJohn Baldwin 	.snd_tag_free = vlan_snd_tag_free,
363c782ea8bSJohn Baldwin 	.next_snd_tag = vlan_next_snd_tag,
364c782ea8bSJohn Baldwin 	.type = IF_SND_TAG_TYPE_TLS
365c782ea8bSJohn Baldwin };
366c782ea8bSJohn Baldwin 
367c782ea8bSJohn Baldwin #ifdef RATELIMIT
368c782ea8bSJohn Baldwin static const struct if_snd_tag_sw vlan_snd_tag_tls_rl_sw = {
369c782ea8bSJohn Baldwin 	.snd_tag_modify = vlan_snd_tag_modify,
370c782ea8bSJohn Baldwin 	.snd_tag_query = vlan_snd_tag_query,
371c782ea8bSJohn Baldwin 	.snd_tag_free = vlan_snd_tag_free,
372c782ea8bSJohn Baldwin 	.next_snd_tag = vlan_next_snd_tag,
373c782ea8bSJohn Baldwin 	.type = IF_SND_TAG_TYPE_TLS_RATE_LIMIT
374c782ea8bSJohn Baldwin };
375c782ea8bSJohn Baldwin #endif
376c782ea8bSJohn Baldwin #endif
377c782ea8bSJohn Baldwin 
37875ee267cSGleb Smirnoff static void
379c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx)
380c32a9d66SHans Petter Selasky {
381c32a9d66SHans Petter Selasky 	struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
382c32a9d66SHans Petter Selasky 	free(mc, M_VLAN);
383c32a9d66SHans Petter Selasky }
384c32a9d66SHans Petter Selasky 
385cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY
386cac30248SOleg Bulyzhin #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
387cac30248SOleg Bulyzhin 
388c32a9d66SHans Petter Selasky static void
38975ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
39075ee267cSGleb Smirnoff {
39175ee267cSGleb Smirnoff 	int i, n;
39275ee267cSGleb Smirnoff 
39375ee267cSGleb Smirnoff 	/*
39475ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
39575ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
39675ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
39775ee267cSGleb Smirnoff 	 */
39875ee267cSGleb Smirnoff 
39975ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
40075ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
40175ee267cSGleb Smirnoff 
40275ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
40375ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
40475ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
40575ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
40675ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
407b08d611dSMatt Macy 		CK_SLIST_INIT(&trunk->hash[i]);
40875ee267cSGleb Smirnoff }
40975ee267cSGleb Smirnoff 
41075ee267cSGleb Smirnoff static void
41175ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
41275ee267cSGleb Smirnoff {
41375ee267cSGleb Smirnoff #ifdef INVARIANTS
41475ee267cSGleb Smirnoff 	int i;
41575ee267cSGleb Smirnoff 
41675ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
41775ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
418b08d611dSMatt Macy 		KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
41975ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
42075ee267cSGleb Smirnoff #endif
42175ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
42275ee267cSGleb Smirnoff 	trunk->hash = NULL;
42375ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
42475ee267cSGleb Smirnoff }
42575ee267cSGleb Smirnoff 
42675ee267cSGleb Smirnoff static int
42775ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
42875ee267cSGleb Smirnoff {
42975ee267cSGleb Smirnoff 	int i, b;
43075ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
43175ee267cSGleb Smirnoff 
432b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
43375ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
43475ee267cSGleb Smirnoff 
43575ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
4367983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
437b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
4387983103aSRobert Watson 		if (ifv->ifv_vid == ifv2->ifv_vid)
43975ee267cSGleb Smirnoff 			return (EEXIST);
44075ee267cSGleb Smirnoff 
44175ee267cSGleb Smirnoff 	/*
44275ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
44375ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
44475ee267cSGleb Smirnoff 	 * buckets/2.
44575ee267cSGleb Smirnoff 	 */
44675ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
44775ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
4487983103aSRobert Watson 		i = HASH(ifv->ifv_vid, trunk->hmask);
44975ee267cSGleb Smirnoff 	}
450b08d611dSMatt Macy 	CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
45175ee267cSGleb Smirnoff 	trunk->refcnt++;
45275ee267cSGleb Smirnoff 
45375ee267cSGleb Smirnoff 	return (0);
45475ee267cSGleb Smirnoff }
45575ee267cSGleb Smirnoff 
45675ee267cSGleb Smirnoff static int
45775ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
45875ee267cSGleb Smirnoff {
45975ee267cSGleb Smirnoff 	int i, b;
46075ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
46175ee267cSGleb Smirnoff 
462b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
46375ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
46475ee267cSGleb Smirnoff 
46575ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
4667983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
467b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
46875ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
46975ee267cSGleb Smirnoff 			trunk->refcnt--;
470b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
47175ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
47275ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
47375ee267cSGleb Smirnoff 			return (0);
47475ee267cSGleb Smirnoff 		}
47575ee267cSGleb Smirnoff 
47675ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
47775ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
47875ee267cSGleb Smirnoff }
47975ee267cSGleb Smirnoff 
48075ee267cSGleb Smirnoff /*
48175ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
48275ee267cSGleb Smirnoff  */
48375ee267cSGleb Smirnoff static void
48475ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
48575ee267cSGleb Smirnoff {
48675ee267cSGleb Smirnoff 	struct ifvlan *ifv;
48775ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
48875ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
48975ee267cSGleb Smirnoff 
490b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
49175ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
49275ee267cSGleb Smirnoff 
49375ee267cSGleb Smirnoff 	if (howmuch == 0) {
49475ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
49575ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
49675ee267cSGleb Smirnoff 		return;
49775ee267cSGleb Smirnoff 	}
49875ee267cSGleb Smirnoff 
49975ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
50075ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
50175ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
50275ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
50375ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
50475ee267cSGleb Smirnoff 		return;
50575ee267cSGleb Smirnoff 
506b08d611dSMatt Macy 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
50775ee267cSGleb Smirnoff 	if (hash2 == NULL) {
50875ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
50975ee267cSGleb Smirnoff 		    __func__);
51075ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
51175ee267cSGleb Smirnoff 	}
51275ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
513b08d611dSMatt Macy 		CK_SLIST_INIT(&hash2[j]);
51475ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
515b08d611dSMatt Macy 		while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
516b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
5177983103aSRobert Watson 			j = HASH(ifv->ifv_vid, n2 - 1);
518b08d611dSMatt Macy 			CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
51975ee267cSGleb Smirnoff 		}
520b08d611dSMatt Macy 	NET_EPOCH_WAIT();
52175ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
52275ee267cSGleb Smirnoff 	trunk->hash = hash2;
52375ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
52475ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
525f84b2d69SYaroslav Tykhiy 
526f84b2d69SYaroslav Tykhiy 	if (bootverbose)
527f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
528f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
52975ee267cSGleb Smirnoff }
53075ee267cSGleb Smirnoff 
53175ee267cSGleb Smirnoff static __inline struct ifvlan *
5327983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
53375ee267cSGleb Smirnoff {
53475ee267cSGleb Smirnoff 	struct ifvlan *ifv;
53575ee267cSGleb Smirnoff 
536a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
53775ee267cSGleb Smirnoff 
538b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
5397983103aSRobert Watson 		if (ifv->ifv_vid == vid)
54075ee267cSGleb Smirnoff 			return (ifv);
54175ee267cSGleb Smirnoff 	return (NULL);
54275ee267cSGleb Smirnoff }
54375ee267cSGleb Smirnoff 
54475ee267cSGleb Smirnoff #if 0
54575ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
54675ee267cSGleb Smirnoff static void
54775ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
54875ee267cSGleb Smirnoff {
54975ee267cSGleb Smirnoff 	int i;
55075ee267cSGleb Smirnoff 	struct ifvlan *ifv;
55175ee267cSGleb Smirnoff 
55275ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
55375ee267cSGleb Smirnoff 		printf("%d: ", i);
554b08d611dSMatt Macy 		CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
55575ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
55675ee267cSGleb Smirnoff 		printf("\n");
55775ee267cSGleb Smirnoff 	}
55875ee267cSGleb Smirnoff }
55975ee267cSGleb Smirnoff #endif /* 0 */
560e4cd31ddSJeff Roberson #else
561e4cd31ddSJeff Roberson 
562e4cd31ddSJeff Roberson static __inline struct ifvlan *
5637983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
564e4cd31ddSJeff Roberson {
565e4cd31ddSJeff Roberson 
5667983103aSRobert Watson 	return trunk->vlans[vid];
567e4cd31ddSJeff Roberson }
568e4cd31ddSJeff Roberson 
569e4cd31ddSJeff Roberson static __inline int
570e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
571e4cd31ddSJeff Roberson {
572e4cd31ddSJeff Roberson 
5737983103aSRobert Watson 	if (trunk->vlans[ifv->ifv_vid] != NULL)
574e4cd31ddSJeff Roberson 		return EEXIST;
5757983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = ifv;
576e4cd31ddSJeff Roberson 	trunk->refcnt++;
577e4cd31ddSJeff Roberson 
578e4cd31ddSJeff Roberson 	return (0);
579e4cd31ddSJeff Roberson }
580e4cd31ddSJeff Roberson 
581e4cd31ddSJeff Roberson static __inline int
582e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
583e4cd31ddSJeff Roberson {
584e4cd31ddSJeff Roberson 
5857983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = NULL;
586e4cd31ddSJeff Roberson 	trunk->refcnt--;
587e4cd31ddSJeff Roberson 
588e4cd31ddSJeff Roberson 	return (0);
589e4cd31ddSJeff Roberson }
590e4cd31ddSJeff Roberson 
591e4cd31ddSJeff Roberson static __inline void
592e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk)
593e4cd31ddSJeff Roberson {
594e4cd31ddSJeff Roberson }
595e4cd31ddSJeff Roberson 
596e4cd31ddSJeff Roberson static __inline void
597e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk)
598e4cd31ddSJeff Roberson {
599e4cd31ddSJeff Roberson }
600e4cd31ddSJeff Roberson 
60175ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
60275ee267cSGleb Smirnoff 
60375ee267cSGleb Smirnoff static void
60475ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
60575ee267cSGleb Smirnoff {
606d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
60775ee267cSGleb Smirnoff 
60875ee267cSGleb Smirnoff 	vlan_freehash(trunk);
60975ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
61033499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
6119bcf3ae4SAlexander Motin 	if_rele(trunk->parent);
61275ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
61375ee267cSGleb Smirnoff }
61475ee267cSGleb Smirnoff 
615f731f104SBill Paul /*
616f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
617f731f104SBill Paul  * programming the multicast filter of the parent. This has the
618f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
619f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
620f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
621f731f104SBill Paul  * to avoid this: there really is only one physical interface.
622f731f104SBill Paul  */
6232b120974SPeter Wemm static int
6242b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
625f731f104SBill Paul {
626f731f104SBill Paul 	struct ifnet		*ifp_p;
6272d222cb7SAlexander Motin 	struct ifmultiaddr	*ifma;
628f731f104SBill Paul 	struct ifvlan		*sc;
629c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
630f731f104SBill Paul 	int			error;
631f731f104SBill Paul 
632b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
633d148c2a2SMatt Joras 
634f731f104SBill Paul 	/* Find the parent. */
635f731f104SBill Paul 	sc = ifp->if_softc;
63675ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
6371b2a4f7aSBill Fenner 
6388b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
6398b615593SMarko Zec 
640f731f104SBill Paul 	/* First, remove any existing filter entries. */
641b08d611dSMatt Macy 	while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
642b08d611dSMatt Macy 		CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
6432d222cb7SAlexander Motin 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
6442a4bd982SGleb Smirnoff 		NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
645f731f104SBill Paul 	}
646f731f104SBill Paul 
647f731f104SBill Paul 	/* Now program new ones. */
6482d222cb7SAlexander Motin 	IF_ADDR_WLOCK(ifp);
649d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
650f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
651f731f104SBill Paul 			continue;
65229c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
6532d222cb7SAlexander Motin 		if (mc == NULL) {
6542d222cb7SAlexander Motin 			IF_ADDR_WUNLOCK(ifp);
655c6b2d024SGeorge V. Neville-Neil 			CURVNET_RESTORE();
65629c2dfbeSBruce M Simpson 			return (ENOMEM);
6572d222cb7SAlexander Motin 		}
658e4cd31ddSJeff Roberson 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
659e4cd31ddSJeff Roberson 		mc->mc_addr.sdl_index = ifp_p->if_index;
660b08d611dSMatt Macy 		CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
6612d222cb7SAlexander Motin 	}
6622d222cb7SAlexander Motin 	IF_ADDR_WUNLOCK(ifp);
663b08d611dSMatt Macy 	CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
664e4cd31ddSJeff Roberson 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
6652d222cb7SAlexander Motin 		    NULL);
666c6b2d024SGeorge V. Neville-Neil 		if (error) {
667c6b2d024SGeorge V. Neville-Neil 			CURVNET_RESTORE();
668f731f104SBill Paul 			return (error);
669f731f104SBill Paul 		}
670c6b2d024SGeorge V. Neville-Neil 	}
671f731f104SBill Paul 
6728b615593SMarko Zec 	CURVNET_RESTORE();
673f731f104SBill Paul 	return (0);
674f731f104SBill Paul }
6752cc2df49SGarrett Wollman 
676a3814acfSSam Leffler /*
677ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
678ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
679ea4ca115SAndrew Thompson  * should also change it on all children vlans.
680ea4ca115SAndrew Thompson  */
681ea4ca115SAndrew Thompson static void
682ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
683ea4ca115SAndrew Thompson {
684a68cc388SGleb Smirnoff 	struct epoch_tracker et;
685ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
686d148c2a2SMatt Joras 	struct ifnet *ifv_ifp;
687d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
688d148c2a2SMatt Joras 	struct sockaddr_dl *sdl;
689ea4ca115SAndrew Thompson 
6907b7f772fSGleb Smirnoff 	/* Need the epoch since this is run on taskqueue_swi. */
691a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
692d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
693d148c2a2SMatt Joras 	if (trunk == NULL) {
694a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
695ea4ca115SAndrew Thompson 		return;
696d148c2a2SMatt Joras 	}
697ea4ca115SAndrew Thompson 
698ea4ca115SAndrew Thompson 	/*
699ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
700d148c2a2SMatt Joras 	 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
701d148c2a2SMatt Joras 	 * ioctl calls on the parent garbling the lladdr of the child vlan.
702ea4ca115SAndrew Thompson 	 */
703d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
704d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
705d148c2a2SMatt Joras 		/*
706d148c2a2SMatt Joras 		 * Copy new new lladdr into the ifv_ifp, enqueue a task
707d148c2a2SMatt Joras 		 * to actually call if_setlladdr. if_setlladdr needs to
708d148c2a2SMatt Joras 		 * be deferred to a taskqueue because it will call into
709d148c2a2SMatt Joras 		 * the if_vlan ioctl path and try to acquire the global
710d148c2a2SMatt Joras 		 * lock.
711d148c2a2SMatt Joras 		 */
712d148c2a2SMatt Joras 		ifv_ifp = ifv->ifv_ifp;
713d148c2a2SMatt Joras 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
714e4cd31ddSJeff Roberson 		    ifp->if_addrlen);
715d148c2a2SMatt Joras 		sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
716d148c2a2SMatt Joras 		sdl->sdl_alen = ifp->if_addrlen;
717d148c2a2SMatt Joras 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
7186117727bSAndrew Thompson 	}
719d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
720a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
721ea4ca115SAndrew Thompson }
722ea4ca115SAndrew Thompson 
723ea4ca115SAndrew Thompson /*
7245cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
7255cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
7265cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
7275428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
7285428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
7295cb8c31aSYaroslav Tykhiy  */
7305cb8c31aSYaroslav Tykhiy static void
7315cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
7325cb8c31aSYaroslav Tykhiy {
7335cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
734d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
7355cb8c31aSYaroslav Tykhiy 
7365428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
7375428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
7385428776eSJohn Baldwin 		return;
739d148c2a2SMatt Joras 	VLAN_XLOCK();
740d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
741d148c2a2SMatt Joras 	if (trunk == NULL) {
742d148c2a2SMatt Joras 		VLAN_XUNLOCK();
743d148c2a2SMatt Joras 		return;
744d148c2a2SMatt Joras 	}
7455428776eSJohn Baldwin 
7465cb8c31aSYaroslav Tykhiy 	/*
7475cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
7485cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
7495cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
7505cb8c31aSYaroslav Tykhiy 	 */
751d148c2a2SMatt Joras 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
752d148c2a2SMatt Joras 	    ifp->if_vlantrunk == NULL)
75328cc4d37SJohn Baldwin 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
754d148c2a2SMatt Joras 
7555cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
7565cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
757d148c2a2SMatt Joras 	VLAN_XUNLOCK();
7585cb8c31aSYaroslav Tykhiy }
7595cb8c31aSYaroslav Tykhiy 
7605cb8c31aSYaroslav Tykhiy /*
761e4cd31ddSJeff Roberson  * Return the trunk device for a virtual interface.
762e4cd31ddSJeff Roberson  */
763e4cd31ddSJeff Roberson static struct ifnet  *
764e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp)
765e4cd31ddSJeff Roberson {
766e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
767e4cd31ddSJeff Roberson 
768b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
769b8a6e03fSGleb Smirnoff 
770e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
771e4cd31ddSJeff Roberson 		return (NULL);
772d148c2a2SMatt Joras 
773e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
774e4cd31ddSJeff Roberson 	ifp = NULL;
775e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
776e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
777e4cd31ddSJeff Roberson 	return (ifp);
778e4cd31ddSJeff Roberson }
779e4cd31ddSJeff Roberson 
780e4cd31ddSJeff Roberson /*
7817983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
7827983103aSRobert Watson  * components such as Infiniband.
7837983103aSRobert Watson  *
7847983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
7857983103aSRobert Watson  * vlan_vid().
786e4cd31ddSJeff Roberson  */
787e4cd31ddSJeff Roberson static int
7887983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
789e4cd31ddSJeff Roberson {
790e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
791e4cd31ddSJeff Roberson 
792e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
793e4cd31ddSJeff Roberson 		return (EINVAL);
794e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
7957983103aSRobert Watson 	*vidp = ifv->ifv_vid;
796e4cd31ddSJeff Roberson 	return (0);
797e4cd31ddSJeff Roberson }
798e4cd31ddSJeff Roberson 
79932d2623aSNavdeep Parhar static int
80032d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
80132d2623aSNavdeep Parhar {
80232d2623aSNavdeep Parhar 	struct ifvlan *ifv;
80332d2623aSNavdeep Parhar 
80432d2623aSNavdeep Parhar 	if (ifp->if_type != IFT_L2VLAN)
80532d2623aSNavdeep Parhar 		return (EINVAL);
80632d2623aSNavdeep Parhar 	ifv = ifp->if_softc;
80732d2623aSNavdeep Parhar 	*pcpp = ifv->ifv_pcp;
80832d2623aSNavdeep Parhar 	return (0);
80932d2623aSNavdeep Parhar }
81032d2623aSNavdeep Parhar 
811e4cd31ddSJeff Roberson /*
812e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
813e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
814e4cd31ddSJeff Roberson  */
815e4cd31ddSJeff Roberson static void *
816e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
817e4cd31ddSJeff Roberson {
818e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
819e4cd31ddSJeff Roberson 
820e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
821e4cd31ddSJeff Roberson 		return (NULL);
822e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
823e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
824e4cd31ddSJeff Roberson }
825e4cd31ddSJeff Roberson 
826e4cd31ddSJeff Roberson /*
827e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
828e4cd31ddSJeff Roberson  * private per-instance data in.
829e4cd31ddSJeff Roberson  */
830e4cd31ddSJeff Roberson static int
831e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
832e4cd31ddSJeff Roberson {
833e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
834e4cd31ddSJeff Roberson 
835e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
836e4cd31ddSJeff Roberson 		return (EINVAL);
837e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
838e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
839e4cd31ddSJeff Roberson 	return (0);
840e4cd31ddSJeff Roberson }
841e4cd31ddSJeff Roberson 
842e4cd31ddSJeff Roberson /*
8437983103aSRobert Watson  * Return the vlan device present at the specific VID.
844e4cd31ddSJeff Roberson  */
845e4cd31ddSJeff Roberson static struct ifnet *
8467983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
847e4cd31ddSJeff Roberson {
848e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
849e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
850e4cd31ddSJeff Roberson 
851b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
852b8a6e03fSGleb Smirnoff 
853e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
854b8a6e03fSGleb Smirnoff 	if (trunk == NULL)
855e4cd31ddSJeff Roberson 		return (NULL);
856e4cd31ddSJeff Roberson 	ifp = NULL;
8577983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
858e4cd31ddSJeff Roberson 	if (ifv)
859e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
860e4cd31ddSJeff Roberson 	return (ifp);
861e4cd31ddSJeff Roberson }
862e4cd31ddSJeff Roberson 
863e4cd31ddSJeff Roberson /*
864a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
865a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
866a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
867a3814acfSSam Leffler  * set here.  No one else in the system should be aware of this so
868a3814acfSSam Leffler  * we use an explicit reference here.
869a3814acfSSam Leffler  */
870a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
871a3814acfSSam Leffler 
872984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
873a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
874127d7b2dSAndre Oppermann 
8752b120974SPeter Wemm static int
8762b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
8772b120974SPeter Wemm {
8789d4fe4b2SBrooks Davis 
8792b120974SPeter Wemm 	switch (type) {
8802b120974SPeter Wemm 	case MOD_LOAD:
8815cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
8825cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
8835cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
8845cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
885ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
886ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
887ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
888ea4ca115SAndrew Thompson 			return (ENOMEM);
889d148c2a2SMatt Joras 		VLAN_LOCKING_INIT();
8909d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
891127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
89275ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
893e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
894e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
895e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
896e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
89732d2623aSNavdeep Parhar 		vlan_pcp_p = vlan_pcp;
898e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
899ccf7ba97SMarko Zec #ifndef VIMAGE
90042a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
90142a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
902ccf7ba97SMarko Zec #endif
90325c0f7b3SYaroslav Tykhiy 		if (bootverbose)
90425c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
90525c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
90625c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
90725c0f7b3SYaroslav Tykhiy #else
90825c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
90925c0f7b3SYaroslav Tykhiy #endif
91025c0f7b3SYaroslav Tykhiy 
91125c0f7b3SYaroslav Tykhiy 			       "\n");
9122b120974SPeter Wemm 		break;
9132b120974SPeter Wemm 	case MOD_UNLOAD:
914ccf7ba97SMarko Zec #ifndef VIMAGE
91542a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
916ccf7ba97SMarko Zec #endif
9175cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
918ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
9199d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
920127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
92175ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
922e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
923e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
92409fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
92509fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
926e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
927d148c2a2SMatt Joras 		VLAN_LOCKING_DESTROY();
92825c0f7b3SYaroslav Tykhiy 		if (bootverbose)
92925c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
9309d4fe4b2SBrooks Davis 		break;
9313e019deaSPoul-Henning Kamp 	default:
9323e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
9332b120974SPeter Wemm 	}
93415a66c21SBruce M Simpson 	return (0);
9352b120974SPeter Wemm }
9362b120974SPeter Wemm 
9372b120974SPeter Wemm static moduledata_t vlan_mod = {
9382b120974SPeter Wemm 	"if_vlan",
9392b120974SPeter Wemm 	vlan_modevent,
9409823d527SKevin Lo 	0
9412b120974SPeter Wemm };
9422b120974SPeter Wemm 
9432b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
94411edc477SEd Maste MODULE_VERSION(if_vlan, 3);
9452cc2df49SGarrett Wollman 
946ccf7ba97SMarko Zec #ifdef VIMAGE
947ccf7ba97SMarko Zec static void
948ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
949ccf7ba97SMarko Zec {
950ccf7ba97SMarko Zec 
95142a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
95242a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
953ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
954ccf7ba97SMarko Zec }
955ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
956ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
957ccf7ba97SMarko Zec 
958ccf7ba97SMarko Zec static void
959ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
960ccf7ba97SMarko Zec {
961ccf7ba97SMarko Zec 
96242a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
963ccf7ba97SMarko Zec }
964eb03a443SKristof Provost VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
965ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
966ccf7ba97SMarko Zec #endif
967ccf7ba97SMarko Zec 
968f941c31aSGleb Smirnoff /*
969c7cffd65SAlexander V. Chernikov  * Check for <etherif>.<vlan>[.<vlan> ...] style interface names.
970f941c31aSGleb Smirnoff  */
971f889d2efSBrooks Davis static struct ifnet *
972f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp)
9739d4fe4b2SBrooks Davis {
974f941c31aSGleb Smirnoff 	char ifname[IFNAMSIZ];
975f941c31aSGleb Smirnoff 	char *cp;
976f889d2efSBrooks Davis 	struct ifnet *ifp;
9777983103aSRobert Watson 	int vid;
978f889d2efSBrooks Davis 
979f941c31aSGleb Smirnoff 	strlcpy(ifname, name, IFNAMSIZ);
980c7cffd65SAlexander V. Chernikov 	if ((cp = strrchr(ifname, '.')) == NULL)
981f941c31aSGleb Smirnoff 		return (NULL);
982f941c31aSGleb Smirnoff 	*cp = '\0';
9839bcf3ae4SAlexander Motin 	if ((ifp = ifunit_ref(ifname)) == NULL)
984f941c31aSGleb Smirnoff 		return (NULL);
985f941c31aSGleb Smirnoff 	/* Parse VID. */
9869bcf3ae4SAlexander Motin 	if (*++cp == '\0') {
9879bcf3ae4SAlexander Motin 		if_rele(ifp);
988f941c31aSGleb Smirnoff 		return (NULL);
9899bcf3ae4SAlexander Motin 	}
9907983103aSRobert Watson 	vid = 0;
991fb92ad4aSJohn Baldwin 	for(; *cp >= '0' && *cp <= '9'; cp++)
9927983103aSRobert Watson 		vid = (vid * 10) + (*cp - '0');
9939bcf3ae4SAlexander Motin 	if (*cp != '\0') {
9949bcf3ae4SAlexander Motin 		if_rele(ifp);
995f941c31aSGleb Smirnoff 		return (NULL);
9969bcf3ae4SAlexander Motin 	}
9977983103aSRobert Watson 	if (vidp != NULL)
9987983103aSRobert Watson 		*vidp = vid;
999f889d2efSBrooks Davis 
100015a66c21SBruce M Simpson 	return (ifp);
1001f889d2efSBrooks Davis }
1002f889d2efSBrooks Davis 
1003f889d2efSBrooks Davis static int
1004f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
1005f889d2efSBrooks Davis {
10064be465abSAlexander V. Chernikov 	struct ifnet *ifp;
1007f889d2efSBrooks Davis 	const char *cp;
1008f889d2efSBrooks Davis 
10094be465abSAlexander V. Chernikov 	ifp = vlan_clone_match_ethervid(name, NULL);
10104be465abSAlexander V. Chernikov 	if (ifp != NULL) {
10114be465abSAlexander V. Chernikov 		if_rele(ifp);
1012f889d2efSBrooks Davis 		return (1);
10134be465abSAlexander V. Chernikov 	}
1014f889d2efSBrooks Davis 
101542a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
1016f889d2efSBrooks Davis 		return (0);
1017f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
1018f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
1019f889d2efSBrooks Davis 			return (0);
1020f889d2efSBrooks Davis 	}
1021f889d2efSBrooks Davis 
1022f889d2efSBrooks Davis 	return (1);
1023f889d2efSBrooks Davis }
1024f889d2efSBrooks Davis 
1025f889d2efSBrooks Davis static int
10266b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
1027f889d2efSBrooks Davis {
1028f889d2efSBrooks Davis 	char *dp;
102953729367SAlexander V. Chernikov 	bool wildcard = false;
103053729367SAlexander V. Chernikov 	bool subinterface = false;
1031f889d2efSBrooks Davis 	int unit;
1032f889d2efSBrooks Davis 	int error;
103353729367SAlexander V. Chernikov 	int vid = 0;
103453729367SAlexander V. Chernikov 	uint16_t proto = ETHERTYPE_VLAN;
10359d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
10369d4fe4b2SBrooks Davis 	struct ifnet *ifp;
103753729367SAlexander V. Chernikov 	struct ifnet *p = NULL;
10383ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
10393ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
10406b7330e2SSam Leffler 	struct vlanreq vlr;
104165239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
1042f889d2efSBrooks Davis 
1043c7cffd65SAlexander V. Chernikov 
10446b7330e2SSam Leffler 	/*
104553729367SAlexander V. Chernikov 	 * There are three ways to specify the cloned device:
10466b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
104753729367SAlexander V. Chernikov 	 * o specify parameters in the text of the clone device name
10486b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
10496b7330e2SSam Leffler 	 *   must be configured separately.
105053729367SAlexander V. Chernikov 	 * The first technique is preferred; the latter two are supported
1051c7cffd65SAlexander V. Chernikov 	 * for backwards compatibility.
10527983103aSRobert Watson 	 *
10537983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
10547983103aSRobert Watson 	 * called for.
10556b7330e2SSam Leffler 	 */
105653729367SAlexander V. Chernikov 
10576b7330e2SSam Leffler 	if (params) {
10586b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
10596b7330e2SSam Leffler 		if (error)
10606b7330e2SSam Leffler 			return error;
106153729367SAlexander V. Chernikov 		vid = vlr.vlr_tag;
106253729367SAlexander V. Chernikov 		proto = vlr.vlr_proto;
106353729367SAlexander V. Chernikov 
1064afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12
1065afbb64f1SAlexander V. Chernikov 		if (proto == 0)
1066afbb64f1SAlexander V. Chernikov 			proto = ETHERTYPE_VLAN;
1067afbb64f1SAlexander V. Chernikov #endif
10689bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
10696b7330e2SSam Leffler 		if (p == NULL)
1070b1828acfSGleb Smirnoff 			return (ENXIO);
107153729367SAlexander V. Chernikov 	}
107253729367SAlexander V. Chernikov 
107353729367SAlexander V. Chernikov 	if ((error = ifc_name2unit(name, &unit)) == 0) {
107453729367SAlexander V. Chernikov 
107553729367SAlexander V. Chernikov 		/*
107653729367SAlexander V. Chernikov 		 * vlanX interface. Set wildcard to true if the unit number
107753729367SAlexander V. Chernikov 		 * is not fixed (-1)
107853729367SAlexander V. Chernikov 		 */
107953729367SAlexander V. Chernikov 		wildcard = (unit < 0);
108053729367SAlexander V. Chernikov 	} else {
108153729367SAlexander V. Chernikov 		struct ifnet *p_tmp = vlan_clone_match_ethervid(name, &vid);
108253729367SAlexander V. Chernikov 		if (p_tmp != NULL) {
108353729367SAlexander V. Chernikov 			error = 0;
108453729367SAlexander V. Chernikov 			subinterface = true;
108553729367SAlexander V. Chernikov 			unit = IF_DUNIT_NONE;
108653729367SAlexander V. Chernikov 			wildcard = false;
108753729367SAlexander V. Chernikov 			if (p != NULL) {
108853729367SAlexander V. Chernikov 				if_rele(p_tmp);
108953729367SAlexander V. Chernikov 				if (p != p_tmp)
109053729367SAlexander V. Chernikov 					error = EINVAL;
109153729367SAlexander V. Chernikov 			} else
109253729367SAlexander V. Chernikov 				p = p_tmp;
109353729367SAlexander V. Chernikov 		} else
109453729367SAlexander V. Chernikov 			error = ENXIO;
109553729367SAlexander V. Chernikov 	}
109653729367SAlexander V. Chernikov 
10979bcf3ae4SAlexander Motin 	if (error != 0) {
109853729367SAlexander V. Chernikov 		if (p != NULL)
10999bcf3ae4SAlexander Motin 			if_rele(p);
11006b7330e2SSam Leffler 		return (error);
11019bcf3ae4SAlexander Motin 	}
1102f889d2efSBrooks Davis 
110353729367SAlexander V. Chernikov 	if (!subinterface) {
110453729367SAlexander V. Chernikov 		/* vlanX interface, mark X as busy or allocate new unit # */
1105f889d2efSBrooks Davis 		error = ifc_alloc_unit(ifc, &unit);
11069bcf3ae4SAlexander Motin 		if (error != 0) {
11079bcf3ae4SAlexander Motin 			if (p != NULL)
11089bcf3ae4SAlexander Motin 				if_rele(p);
1109f889d2efSBrooks Davis 			return (error);
11109bcf3ae4SAlexander Motin 		}
111153729367SAlexander V. Chernikov 	}
1112f889d2efSBrooks Davis 
1113f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
1114f889d2efSBrooks Davis 	if (wildcard) {
1115f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
1116f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1117f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
1118f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
1119f889d2efSBrooks Davis 		}
1120f889d2efSBrooks Davis 	}
11219d4fe4b2SBrooks Davis 
1122a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1123fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1124fc74a9f9SBrooks Davis 	if (ifp == NULL) {
112553729367SAlexander V. Chernikov 		if (!subinterface)
1126fc74a9f9SBrooks Davis 			ifc_free_unit(ifc, unit);
1127fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
11289bcf3ae4SAlexander Motin 		if (p != NULL)
11299bcf3ae4SAlexander Motin 			if_rele(p);
1130fc74a9f9SBrooks Davis 		return (ENOSPC);
1131fc74a9f9SBrooks Davis 	}
1132b08d611dSMatt Macy 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
11339d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
1134f889d2efSBrooks Davis 	/*
1135cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
1136f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
1137f889d2efSBrooks Davis 	 */
1138f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
113942a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
1140f889d2efSBrooks Davis 	ifp->if_dunit = unit;
11419d4fe4b2SBrooks Davis 
1142114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
11432e5ff01dSLuiz Otavio O Souza #ifdef ALTQ
11442e5ff01dSLuiz Otavio O Souza 	ifp->if_start = vlan_altq_start;
11452e5ff01dSLuiz Otavio O Souza 	ifp->if_transmit = vlan_altq_transmit;
11462e5ff01dSLuiz Otavio O Souza 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
11472e5ff01dSLuiz Otavio O Souza 	ifp->if_snd.ifq_drv_maxlen = 0;
11482e5ff01dSLuiz Otavio O Souza 	IFQ_SET_READY(&ifp->if_snd);
11492e5ff01dSLuiz Otavio O Souza #else
1150d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
11512e5ff01dSLuiz Otavio O Souza #endif
1152d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
11539d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
1154b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1155f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
11561a714ff2SRandall Stewart 	ifp->if_ratelimit_query = vlan_ratelimit_query;
1157f3e7afe2SHans Petter Selasky #endif
115864a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
1159fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
11609d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
1161211f625aSBill Fenner 	ifp->if_baudrate = 0;
1162a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
1163a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
11643ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
11653ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
11663ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
11679d4fe4b2SBrooks Davis 
11689bcf3ae4SAlexander Motin 	if (p != NULL) {
1169c7cffd65SAlexander V. Chernikov 		error = vlan_config(ifv, p, vid, proto);
11709bcf3ae4SAlexander Motin 		if_rele(p);
1171f889d2efSBrooks Davis 		if (error != 0) {
1172f889d2efSBrooks Davis 			/*
117328cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
1174f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
1175f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1176f889d2efSBrooks Davis 			 */
1177f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1178249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
11794b22573aSBrooks Davis 			if_free(ifp);
118053729367SAlexander V. Chernikov 			if (!subinterface)
118196c8ef3aSMaxim Konovalov 				ifc_free_unit(ifc, unit);
1182f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1183f889d2efSBrooks Davis 
1184f889d2efSBrooks Davis 			return (error);
1185f889d2efSBrooks Davis 		}
1186f889d2efSBrooks Davis 	}
1187f889d2efSBrooks Davis 
11889d4fe4b2SBrooks Davis 	return (0);
11899d4fe4b2SBrooks Davis }
11909d4fe4b2SBrooks Davis 
1191f889d2efSBrooks Davis static int
1192f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
11939d4fe4b2SBrooks Davis {
11949d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
119553729367SAlexander V. Chernikov 	int unit = ifp->if_dunit;
1196c7cffd65SAlexander V. Chernikov 
1197c7cffd65SAlexander V. Chernikov 	if (ifp->if_vlantrunk)
1198c7cffd65SAlexander V. Chernikov 		return (EBUSY);
1199b4e9f837SBrooks Davis 
12002e5ff01dSLuiz Otavio O Souza #ifdef ALTQ
12012e5ff01dSLuiz Otavio O Souza 	IFQ_PURGE(&ifp->if_snd);
12022e5ff01dSLuiz Otavio O Souza #endif
1203249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1204249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1205d148c2a2SMatt Joras 	/*
1206d148c2a2SMatt Joras 	 * We should have the only reference to the ifv now, so we can now
1207d148c2a2SMatt Joras 	 * drain any remaining lladdr task before freeing the ifnet and the
1208d148c2a2SMatt Joras 	 * ifvlan.
1209d148c2a2SMatt Joras 	 */
1210d148c2a2SMatt Joras 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1211b08d611dSMatt Macy 	NET_EPOCH_WAIT();
12124b22573aSBrooks Davis 	if_free(ifp);
12139d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
121453729367SAlexander V. Chernikov 	if (unit != IF_DUNIT_NONE)
121553729367SAlexander V. Chernikov 		ifc_free_unit(ifc, unit);
1216b4e9f837SBrooks Davis 
1217f889d2efSBrooks Davis 	return (0);
12189d4fe4b2SBrooks Davis }
12199d4fe4b2SBrooks Davis 
122015a66c21SBruce M Simpson /*
122115a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
122215a66c21SBruce M Simpson  */
12232cc2df49SGarrett Wollman static void
1224114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
12252cc2df49SGarrett Wollman {
12262cc2df49SGarrett Wollman }
12272cc2df49SGarrett Wollman 
12286d3a3ab7SGleb Smirnoff /*
1229d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
12306d3a3ab7SGleb Smirnoff  */
1231d9b1d615SJohn Baldwin static int
1232d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
12332cc2df49SGarrett Wollman {
12342cc2df49SGarrett Wollman 	struct ifvlan *ifv;
12352cc2df49SGarrett Wollman 	struct ifnet *p;
12361ad7a257SPyun YongHyeon 	int error, len, mcast;
12372cc2df49SGarrett Wollman 
1238b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1239b8a6e03fSGleb Smirnoff 
12402cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
1241d148c2a2SMatt Joras 	if (TRUNK(ifv) == NULL) {
1242d148c2a2SMatt Joras 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1243d148c2a2SMatt Joras 		m_freem(m);
1244d148c2a2SMatt Joras 		return (ENETDOWN);
1245d148c2a2SMatt Joras 	}
124675ee267cSGleb Smirnoff 	p = PARENT(ifv);
12471ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
12481ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
12492cc2df49SGarrett Wollman 
1250a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
12512cc2df49SGarrett Wollman 
1252b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
1253fb3bc596SJohn Baldwin 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG) {
1254fb3bc596SJohn Baldwin 		struct vlan_snd_tag *vst;
1255fb3bc596SJohn Baldwin 		struct m_snd_tag *mst;
1256fb3bc596SJohn Baldwin 
1257fb3bc596SJohn Baldwin 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
1258fb3bc596SJohn Baldwin 		mst = m->m_pkthdr.snd_tag;
1259fb3bc596SJohn Baldwin 		vst = mst_to_vst(mst);
1260fb3bc596SJohn Baldwin 		if (vst->tag->ifp != p) {
1261fb3bc596SJohn Baldwin 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1262fb3bc596SJohn Baldwin 			m_freem(m);
1263fb3bc596SJohn Baldwin 			return (EAGAIN);
1264fb3bc596SJohn Baldwin 		}
1265fb3bc596SJohn Baldwin 
1266fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(vst->tag);
1267fb3bc596SJohn Baldwin 		m_snd_tag_rele(mst);
1268fb3bc596SJohn Baldwin 	}
1269fb3bc596SJohn Baldwin #endif
1270fb3bc596SJohn Baldwin 
1271f731f104SBill Paul 	/*
1272d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
127324993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
127424993214SYaroslav Tykhiy 	 */
12752dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
1276a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1277d148c2a2SMatt Joras 		m_freem(m);
12788b20f6cfSHiroki Sato 		return (ENETDOWN);
127924993214SYaroslav Tykhiy 	}
128024993214SYaroslav Tykhiy 
1281c7cffd65SAlexander V. Chernikov 	if (!ether_8021q_frame(&m, ifp, p, &ifv->ifv_qtag)) {
1282a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1283d9b1d615SJohn Baldwin 		return (0);
12844af90a4dSMatthew N. Dodd 	}
12852cc2df49SGarrett Wollman 
12862cc2df49SGarrett Wollman 	/*
12872cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
12882cc2df49SGarrett Wollman 	 */
1289aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1290299153b5SAlexander V. Chernikov 	if (error == 0) {
1291a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1292a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1293a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
12941ad7a257SPyun YongHyeon 	} else
1295a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1296d9b1d615SJohn Baldwin 	return (error);
12972cc2df49SGarrett Wollman }
1298d9b1d615SJohn Baldwin 
129916cf6bdbSMatt Joras static int
130016cf6bdbSMatt Joras vlan_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
130116cf6bdbSMatt Joras     struct route *ro)
130216cf6bdbSMatt Joras {
130316cf6bdbSMatt Joras 	struct ifvlan *ifv;
130416cf6bdbSMatt Joras 	struct ifnet *p;
130516cf6bdbSMatt Joras 
1306b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1307b8a6e03fSGleb Smirnoff 
1308c7cffd65SAlexander V. Chernikov 	/*
1309c7cffd65SAlexander V. Chernikov 	 * Find the first non-VLAN parent interface.
1310c7cffd65SAlexander V. Chernikov 	 */
131116cf6bdbSMatt Joras 	ifv = ifp->if_softc;
1312c7cffd65SAlexander V. Chernikov 	do {
131316cf6bdbSMatt Joras 		if (TRUNK(ifv) == NULL) {
131416cf6bdbSMatt Joras 			m_freem(m);
131516cf6bdbSMatt Joras 			return (ENETDOWN);
131616cf6bdbSMatt Joras 		}
131716cf6bdbSMatt Joras 		p = PARENT(ifv);
1318c7cffd65SAlexander V. Chernikov 		ifv = p->if_softc;
1319c7cffd65SAlexander V. Chernikov 	} while (p->if_type == IFT_L2VLAN);
1320c7cffd65SAlexander V. Chernikov 
132116cf6bdbSMatt Joras 	return p->if_output(ifp, m, dst, ro);
132216cf6bdbSMatt Joras }
132316cf6bdbSMatt Joras 
13242e5ff01dSLuiz Otavio O Souza #ifdef ALTQ
13252e5ff01dSLuiz Otavio O Souza static void
13262e5ff01dSLuiz Otavio O Souza vlan_altq_start(if_t ifp)
13272e5ff01dSLuiz Otavio O Souza {
13282e5ff01dSLuiz Otavio O Souza 	struct ifaltq *ifq = &ifp->if_snd;
13292e5ff01dSLuiz Otavio O Souza 	struct mbuf *m;
13302e5ff01dSLuiz Otavio O Souza 
13312e5ff01dSLuiz Otavio O Souza 	IFQ_LOCK(ifq);
13322e5ff01dSLuiz Otavio O Souza 	IFQ_DEQUEUE_NOLOCK(ifq, m);
13332e5ff01dSLuiz Otavio O Souza 	while (m != NULL) {
13342e5ff01dSLuiz Otavio O Souza 		vlan_transmit(ifp, m);
13352e5ff01dSLuiz Otavio O Souza 		IFQ_DEQUEUE_NOLOCK(ifq, m);
13362e5ff01dSLuiz Otavio O Souza 	}
13372e5ff01dSLuiz Otavio O Souza 	IFQ_UNLOCK(ifq);
13382e5ff01dSLuiz Otavio O Souza }
13392e5ff01dSLuiz Otavio O Souza 
13402e5ff01dSLuiz Otavio O Souza static int
13412e5ff01dSLuiz Otavio O Souza vlan_altq_transmit(if_t ifp, struct mbuf *m)
13422e5ff01dSLuiz Otavio O Souza {
13432e5ff01dSLuiz Otavio O Souza 	int err;
13442e5ff01dSLuiz Otavio O Souza 
13452e5ff01dSLuiz Otavio O Souza 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
13462e5ff01dSLuiz Otavio O Souza 		IFQ_ENQUEUE(&ifp->if_snd, m, err);
13472e5ff01dSLuiz Otavio O Souza 		if (err == 0)
13482e5ff01dSLuiz Otavio O Souza 			vlan_altq_start(ifp);
13492e5ff01dSLuiz Otavio O Souza 	} else
13502e5ff01dSLuiz Otavio O Souza 		err = vlan_transmit(ifp, m);
13512e5ff01dSLuiz Otavio O Souza 
13522e5ff01dSLuiz Otavio O Souza 	return (err);
13532e5ff01dSLuiz Otavio O Souza }
13542e5ff01dSLuiz Otavio O Souza #endif	/* ALTQ */
13552e5ff01dSLuiz Otavio O Souza 
1356d9b1d615SJohn Baldwin /*
1357d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1358d9b1d615SJohn Baldwin  */
1359d9b1d615SJohn Baldwin static void
1360d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1361d9b1d615SJohn Baldwin {
1362f731f104SBill Paul }
1363f731f104SBill Paul 
1364a3814acfSSam Leffler static void
1365a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1366f731f104SBill Paul {
1367d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1368f731f104SBill Paul 	struct ifvlan *ifv;
13692ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
13702ccbbd06SMarcelo Araujo 	uint16_t vid, tag;
137175ee267cSGleb Smirnoff 
1372b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1373b8a6e03fSGleb Smirnoff 
1374d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1375d148c2a2SMatt Joras 	if (trunk == NULL) {
1376d148c2a2SMatt Joras 		m_freem(m);
1377d148c2a2SMatt Joras 		return;
1378d148c2a2SMatt Joras 	}
1379a3814acfSSam Leffler 
1380f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1381a3814acfSSam Leffler 		/*
138214e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1383a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1384a3814acfSSam Leffler 		 */
13852ccbbd06SMarcelo Araujo 		tag = m->m_pkthdr.ether_vtag;
13866ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1387a3814acfSSam Leffler 	} else {
138875ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
138975ee267cSGleb Smirnoff 
139014e98256SYaroslav Tykhiy 		/*
139114e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
139214e98256SYaroslav Tykhiy 		 */
1393a3814acfSSam Leffler 		switch (ifp->if_type) {
1394a3814acfSSam Leffler 		case IFT_ETHER:
1395a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1396a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1397a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1398a3814acfSSam Leffler 				return;
1399a3814acfSSam Leffler 			}
1400a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
14012ccbbd06SMarcelo Araujo 			tag = ntohs(evl->evl_tag);
1402db8b5973SYaroslav Tykhiy 
1403db8b5973SYaroslav Tykhiy 			/*
14042dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
14052dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
14062dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
14072dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1408db8b5973SYaroslav Tykhiy 			 */
14092dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
14102dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
14112dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1412a3814acfSSam Leffler 			break;
14132dc879b3SYaroslav Tykhiy 
1414a3814acfSSam Leffler 		default:
1415db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
141660c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
141760c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1418a3814acfSSam Leffler #endif
14193751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1420d148c2a2SMatt Joras 			m_freem(m);
142160c60618SYaroslav Tykhiy 			return;
1422a3814acfSSam Leffler 		}
14237a46ec8fSBrooks Davis 	}
14247a46ec8fSBrooks Davis 
14252ccbbd06SMarcelo Araujo 	vid = EVL_VLANOFTAG(tag);
14262ccbbd06SMarcelo Araujo 
14277983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
14282dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1429b08d611dSMatt Macy 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1430d148c2a2SMatt Joras 		m_freem(m);
143175ee267cSGleb Smirnoff 		return;
143275ee267cSGleb Smirnoff 	}
1433f731f104SBill Paul 
143478bc3d5eSKristof Provost 	if (V_vlan_mtag_pcp) {
14352ccbbd06SMarcelo Araujo 		/*
14362ccbbd06SMarcelo Araujo 		 * While uncommon, it is possible that we will find a 802.1q
14372ccbbd06SMarcelo Araujo 		 * packet encapsulated inside another packet that also had an
14382ccbbd06SMarcelo Araujo 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
14392ccbbd06SMarcelo Araujo 		 * arriving over ethernet.  In that case, we replace the
14402ccbbd06SMarcelo Araujo 		 * existing 802.1q PCP m_tag value.
14412ccbbd06SMarcelo Araujo 		 */
14422ccbbd06SMarcelo Araujo 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
14432ccbbd06SMarcelo Araujo 		if (mtag == NULL) {
14442ccbbd06SMarcelo Araujo 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
14452ccbbd06SMarcelo Araujo 			    sizeof(uint8_t), M_NOWAIT);
14462ccbbd06SMarcelo Araujo 			if (mtag == NULL) {
14472ccbbd06SMarcelo Araujo 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1448d148c2a2SMatt Joras 				m_freem(m);
14492ccbbd06SMarcelo Araujo 				return;
14502ccbbd06SMarcelo Araujo 			}
14512ccbbd06SMarcelo Araujo 			m_tag_prepend(m, mtag);
14522ccbbd06SMarcelo Araujo 		}
14532ccbbd06SMarcelo Araujo 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
14542ccbbd06SMarcelo Araujo 	}
14552ccbbd06SMarcelo Araujo 
1456fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1457c0304424SGleb Smirnoff 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
14582cc2df49SGarrett Wollman 
1459a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1460fdbf1174SMatt Joras 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
14612cc2df49SGarrett Wollman }
14622cc2df49SGarrett Wollman 
1463d148c2a2SMatt Joras static void
1464d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused)
1465d148c2a2SMatt Joras {
1466d148c2a2SMatt Joras 	struct ifvlan *ifv;
1467d148c2a2SMatt Joras 	struct ifnet *ifp;
1468d148c2a2SMatt Joras 
1469d148c2a2SMatt Joras 	ifv = (struct ifvlan *)arg;
1470d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
14715191a3aeSKristof Provost 
14725191a3aeSKristof Provost 	CURVNET_SET(ifp->if_vnet);
14735191a3aeSKristof Provost 
1474d148c2a2SMatt Joras 	/* The ifv_ifp already has the lladdr copied in. */
1475d148c2a2SMatt Joras 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
14765191a3aeSKristof Provost 
14775191a3aeSKristof Provost 	CURVNET_RESTORE();
1478d148c2a2SMatt Joras }
1479d148c2a2SMatt Joras 
14802cc2df49SGarrett Wollman static int
1481c7cffd65SAlexander V. Chernikov vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid,
1482c7cffd65SAlexander V. Chernikov 	uint16_t proto)
14832cc2df49SGarrett Wollman {
14846dcec895SGleb Smirnoff 	struct epoch_tracker et;
148575ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
14861cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
148775ee267cSGleb Smirnoff 	int error = 0;
14882cc2df49SGarrett Wollman 
1489b1828acfSGleb Smirnoff 	/*
1490b1828acfSGleb Smirnoff 	 * We can handle non-ethernet hardware types as long as
1491b1828acfSGleb Smirnoff 	 * they handle the tagging and headers themselves.
1492b1828acfSGleb Smirnoff 	 */
1493e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1494c7cffd65SAlexander V. Chernikov 	    p->if_type != IFT_L2VLAN &&
1495e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
149615a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
149764a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
149864a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
1499b1828acfSGleb Smirnoff 	/*
1500b1828acfSGleb Smirnoff 	 * Don't let the caller set up a VLAN VID with
1501b1828acfSGleb Smirnoff 	 * anything except VLID bits.
1502b1828acfSGleb Smirnoff 	 * VID numbers 0x0 and 0xFFF are reserved.
1503b1828acfSGleb Smirnoff 	 */
1504b1828acfSGleb Smirnoff 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1505b1828acfSGleb Smirnoff 		return (EINVAL);
150675ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
150715a66c21SBruce M Simpson 		return (EBUSY);
15082cc2df49SGarrett Wollman 
1509d148c2a2SMatt Joras 	VLAN_XLOCK();
151075ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
151175ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
151275ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
151375ee267cSGleb Smirnoff 		vlan_inithash(trunk);
151475ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
1515d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
151675ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
151775ee267cSGleb Smirnoff 		trunk->parent = p;
15189bcf3ae4SAlexander Motin 		if_ref(trunk->parent);
1519b08d611dSMatt Macy 		TRUNK_WUNLOCK(trunk);
152075ee267cSGleb Smirnoff 	} else {
152175ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
152275ee267cSGleb Smirnoff 	}
152375ee267cSGleb Smirnoff 
15247983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
15252ccbbd06SMarcelo Araujo 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
152675ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
152775ee267cSGleb Smirnoff 	if (error)
152875ee267cSGleb Smirnoff 		goto done;
1529c7cffd65SAlexander V. Chernikov 	ifv->ifv_proto = proto;
1530a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1531a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
15321cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1533d89baa5aSAlexander Motin 	ifv->ifv_capenable = -1;
1534a3814acfSSam Leffler 
1535a3814acfSSam Leffler 	/*
1536a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1537a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1538656acce4SYaroslav Tykhiy 	 * use it.
1539a3814acfSSam Leffler 	 */
1540656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1541656acce4SYaroslav Tykhiy 		/*
1542656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1543656acce4SYaroslav Tykhiy 		 * handle extended frames.
1544656acce4SYaroslav Tykhiy 		 */
1545a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1546656acce4SYaroslav Tykhiy 	} else {
1547a3814acfSSam Leffler 		/*
1548a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1549a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1550a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1551a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1552a3814acfSSam Leffler 		 * which might still be useful.
1553a3814acfSSam Leffler 		 */
1554a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1555a3814acfSSam Leffler 	}
1556a3814acfSSam Leffler 
155775ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
15581cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1559e4cd31ddSJeff Roberson 	/*
1560e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1561e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1562e4cd31ddSJeff Roberson 	 * interfaces to also work.
1563e4cd31ddSJeff Roberson 	 */
15641cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
156575ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1566e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1567e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1568e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1569e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
157032a52e9eSNavdeep Parhar 	ifp->if_pcp = ifv->ifv_pcp;
1571e4cd31ddSJeff Roberson 
15722cc2df49SGarrett Wollman 	/*
157316cf6bdbSMatt Joras 	 * We wrap the parent's if_output using vlan_output to ensure that it
157416cf6bdbSMatt Joras 	 * can't become stale.
157516cf6bdbSMatt Joras 	 */
157616cf6bdbSMatt Joras 	ifp->if_output = vlan_output;
157716cf6bdbSMatt Joras 
157816cf6bdbSMatt Joras 	/*
157924993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
158024993214SYaroslav Tykhiy 	 * Other flags are none of our business.
15812cc2df49SGarrett Wollman 	 */
158264a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
15831cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
15841cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
15851cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
15861cf236fbSYaroslav Tykhiy 
15871cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
15882cc2df49SGarrett Wollman 
15896dcec895SGleb Smirnoff 	NET_EPOCH_ENTER(et);
159075ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
15916dcec895SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1592a3814acfSSam Leffler 
1593a3814acfSSam Leffler 	/*
1594e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
15952cc2df49SGarrett Wollman 	 * physical interface's.
15962cc2df49SGarrett Wollman 	 */
1597a961401eSAndrey V. Elsukov 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
1598e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1599e4cd31ddSJeff Roberson 	    p->if_addrlen;
16001b2a4f7aSBill Fenner 
1601a961401eSAndrey V. Elsukov 	/*
1602a961401eSAndrey V. Elsukov 	 * Do not schedule link address update if it was the same
1603a961401eSAndrey V. Elsukov 	 * as previous parent's. This helps avoid updating for each
1604a961401eSAndrey V. Elsukov 	 * associated llentry.
1605a961401eSAndrey V. Elsukov 	 */
1606a961401eSAndrey V. Elsukov 	if (memcmp(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen) != 0) {
1607a961401eSAndrey V. Elsukov 		bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1608a961401eSAndrey V. Elsukov 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
1609a961401eSAndrey V. Elsukov 	}
16102ada9747SYaroslav Tykhiy 
16112ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
16122ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1613d148c2a2SMatt Joras 
1614d148c2a2SMatt Joras 	/* Update flags on the parent, if necessary. */
1615d148c2a2SMatt Joras 	vlan_setflags(ifp, 1);
1616b08d611dSMatt Macy 
1617d148c2a2SMatt Joras 	/*
1618b08d611dSMatt Macy 	 * Configure multicast addresses that may already be
1619b08d611dSMatt Macy 	 * joined on the vlan device.
1620d148c2a2SMatt Joras 	 */
1621b08d611dSMatt Macy 	(void)vlan_setmulti(ifp);
1622b08d611dSMatt Macy 
1623b08d611dSMatt Macy done:
1624c725524cSJack F Vogel 	if (error == 0)
16257983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1626d148c2a2SMatt Joras 	VLAN_XUNLOCK();
162775ee267cSGleb Smirnoff 
162875ee267cSGleb Smirnoff 	return (error);
16292cc2df49SGarrett Wollman }
16302cc2df49SGarrett Wollman 
16316f359e28SJohn Baldwin static void
1632f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1633f731f104SBill Paul {
16345cb8c31aSYaroslav Tykhiy 
1635d148c2a2SMatt Joras 	VLAN_XLOCK();
163628cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
1637d148c2a2SMatt Joras 	VLAN_XUNLOCK();
16385cb8c31aSYaroslav Tykhiy }
16395cb8c31aSYaroslav Tykhiy 
16406f359e28SJohn Baldwin static void
164128cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
16425cb8c31aSYaroslav Tykhiy {
164375ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1644f731f104SBill Paul 	struct vlan_mc_entry *mc;
1645f731f104SBill Paul 	struct ifvlan *ifv;
1646c725524cSJack F Vogel 	struct ifnet  *parent;
164728cc4d37SJohn Baldwin 	int error;
1648f731f104SBill Paul 
1649d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
16504faedfe8SSam Leffler 
1651f731f104SBill Paul 	ifv = ifp->if_softc;
165275ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
165322893351SJack F Vogel 	parent = NULL;
1654f731f104SBill Paul 
165522893351SJack F Vogel 	if (trunk != NULL) {
165622893351SJack F Vogel 		parent = trunk->parent;
16571b2a4f7aSBill Fenner 
1658f731f104SBill Paul 		/*
1659f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1660f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
16611b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1662f731f104SBill Paul 		 */
1663b08d611dSMatt Macy 		while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
16646f359e28SJohn Baldwin 			/*
166528cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1666b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
166728cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
166828cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
166928cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
167028cc4d37SJohn Baldwin 			 * succeed.
16716f359e28SJohn Baldwin 			 */
167228cc4d37SJohn Baldwin 			if (!departing) {
167328cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1674e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
167528cc4d37SJohn Baldwin 				if (error)
167628cc4d37SJohn Baldwin 					if_printf(ifp,
167728cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
167828cc4d37SJohn Baldwin 					    error);
167928cc4d37SJohn Baldwin 			}
1680b08d611dSMatt Macy 			CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
16812a4bd982SGleb Smirnoff 			NET_EPOCH_CALL(vlan_mc_free, &mc->mc_epoch_ctx);
1682f731f104SBill Paul 		}
1683a3814acfSSam Leffler 
16841cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1685d148c2a2SMatt Joras 
168675ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
168775ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
168875ee267cSGleb Smirnoff 
168975ee267cSGleb Smirnoff 		/*
169075ee267cSGleb Smirnoff 		 * Check if we were the last.
169175ee267cSGleb Smirnoff 		 */
169275ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
16932d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
1694b08d611dSMatt Macy 			NET_EPOCH_WAIT();
169575ee267cSGleb Smirnoff 			trunk_destroy(trunk);
1696d148c2a2SMatt Joras 		}
16971b2a4f7aSBill Fenner 	}
1698f731f104SBill Paul 
1699f731f104SBill Paul 	/* Disconnect from parent. */
17001cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
17011cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
17025cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
17035cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
17045cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1705f731f104SBill Paul 
170622893351SJack F Vogel 	/*
170722893351SJack F Vogel 	 * Only dispatch an event if vlan was
170822893351SJack F Vogel 	 * attached, otherwise there is nothing
170922893351SJack F Vogel 	 * to cleanup anyway.
171022893351SJack F Vogel 	 */
171122893351SJack F Vogel 	if (parent != NULL)
17127983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1713f731f104SBill Paul }
1714f731f104SBill Paul 
17151cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1716f731f104SBill Paul static int
17171cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
17181cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1719a3814acfSSam Leffler {
17201cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
17211cf236fbSYaroslav Tykhiy 	int error;
1722a3814acfSSam Leffler 
1723d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1724a3814acfSSam Leffler 
17251cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
17261cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
17271cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
17281cf236fbSYaroslav Tykhiy 
17291cf236fbSYaroslav Tykhiy 	/*
17301cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
17311cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
17321cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
17331cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
17341cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
17351cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
17361cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
17371cf236fbSYaroslav Tykhiy 	 */
17381cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
173975ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
17401cf236fbSYaroslav Tykhiy 		if (error)
1741a3814acfSSam Leffler 			return (error);
17421cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
17431cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
17441cf236fbSYaroslav Tykhiy 	}
17451cf236fbSYaroslav Tykhiy 	return (0);
17461cf236fbSYaroslav Tykhiy }
17471cf236fbSYaroslav Tykhiy 
17481cf236fbSYaroslav Tykhiy /*
17491cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
17501cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
17511cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
17521cf236fbSYaroslav Tykhiy  */
17531cf236fbSYaroslav Tykhiy static int
17541cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
17551cf236fbSYaroslav Tykhiy {
17561cf236fbSYaroslav Tykhiy 	int error, i;
17571cf236fbSYaroslav Tykhiy 
17581cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
17591cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
17601cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
17611cf236fbSYaroslav Tykhiy 		if (error)
17621cf236fbSYaroslav Tykhiy 			return (error);
17631cf236fbSYaroslav Tykhiy 	}
17641cf236fbSYaroslav Tykhiy 	return (0);
1765a3814acfSSam Leffler }
1766a3814acfSSam Leffler 
1767127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1768127d7b2dSAndre Oppermann static void
1769a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1770127d7b2dSAndre Oppermann {
1771b2807792SGleb Smirnoff 	struct epoch_tracker et;
1772d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1773127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
1774127d7b2dSAndre Oppermann 
1775b2807792SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1776d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1777b2807792SGleb Smirnoff 	if (trunk == NULL) {
1778b2807792SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1779d148c2a2SMatt Joras 		return;
1780b2807792SGleb Smirnoff 	}
1781d148c2a2SMatt Joras 
1782d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1783d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
1784aad0be7aSGleb Smirnoff 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1785fc74a9f9SBrooks Davis 		if_link_state_change(ifv->ifv_ifp,
178675ee267cSGleb Smirnoff 		    trunk->parent->if_link_state);
1787127d7b2dSAndre Oppermann 	}
1788d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1789b2807792SGleb Smirnoff 	NET_EPOCH_EXIT(et);
179075ee267cSGleb Smirnoff }
179175ee267cSGleb Smirnoff 
179275ee267cSGleb Smirnoff static void
179375ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
179475ee267cSGleb Smirnoff {
1795d148c2a2SMatt Joras 	struct ifnet *p;
1796d148c2a2SMatt Joras 	struct ifnet *ifp;
17979fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
1798d89baa5aSAlexander Motin 	int cap = 0, ena = 0, mena;
1799d89baa5aSAlexander Motin 	u_long hwa = 0;
180075ee267cSGleb Smirnoff 
1801a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
1802b8a6e03fSGleb Smirnoff 	VLAN_SXLOCK_ASSERT();
1803b8a6e03fSGleb Smirnoff 
1804d148c2a2SMatt Joras 	p = PARENT(ifv);
1805d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
180675ee267cSGleb Smirnoff 
1807d89baa5aSAlexander Motin 	/* Mask parent interface enabled capabilities disabled by user. */
1808d89baa5aSAlexander Motin 	mena = p->if_capenable & ifv->ifv_capenable;
1809d89baa5aSAlexander Motin 
181075ee267cSGleb Smirnoff 	/*
181175ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
181275ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
181375ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
181475ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
181575ee267cSGleb Smirnoff 	 */
181675ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1817d89baa5aSAlexander Motin 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
181875ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
181975ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1820d89baa5aSAlexander Motin 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1821d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM)
1822d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1823d89baa5aSAlexander Motin 			    CSUM_UDP | CSUM_SCTP);
1824d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM_IPV6)
1825d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1826d89baa5aSAlexander Motin 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
182775ee267cSGleb Smirnoff 	}
1828d89baa5aSAlexander Motin 
18299b76d9cbSPyun YongHyeon 	/*
18309b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
18319b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
18329b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
18339b76d9cbSPyun YongHyeon 	 */
18349fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
18359fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
18369fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
18379b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1838d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TSO;
18399b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1840d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TSO;
1841d89baa5aSAlexander Motin 		if (ena & IFCAP_TSO)
1842d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & CSUM_TSO;
18439b76d9cbSPyun YongHyeon 	}
184409fe6320SNavdeep Parhar 
184509fe6320SNavdeep Parhar 	/*
184659150e91SAlexander Motin 	 * If the parent interface can do LRO and checksum offloading on
184759150e91SAlexander Motin 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
184859150e91SAlexander Motin 	 * cost nothing, while false negative may lead to some confusions.
184959150e91SAlexander Motin 	 */
185059150e91SAlexander Motin 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
185159150e91SAlexander Motin 		cap |= p->if_capabilities & IFCAP_LRO;
185259150e91SAlexander Motin 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
185359150e91SAlexander Motin 		ena |= p->if_capenable & IFCAP_LRO;
185459150e91SAlexander Motin 
185559150e91SAlexander Motin 	/*
185609fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
185709fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
185809fe6320SNavdeep Parhar 	 *
185909fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
186009fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
186109fe6320SNavdeep Parhar 	 * with its own bit.
186209fe6320SNavdeep Parhar 	 */
186309fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
186409fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1865d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TOE;
186609fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
186709fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
1868d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TOE;
186909fe6320SNavdeep Parhar 	}
1870f3e7afe2SHans Petter Selasky 
1871d89baa5aSAlexander Motin 	/*
1872d89baa5aSAlexander Motin 	 * If the parent interface supports dynamic link state, so does the
1873d89baa5aSAlexander Motin 	 * VLAN interface.
1874d89baa5aSAlexander Motin 	 */
1875d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1876d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_LINKSTATE);
1877d89baa5aSAlexander Motin 
1878f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1879f3e7afe2SHans Petter Selasky 	/*
1880f3e7afe2SHans Petter Selasky 	 * If the parent interface supports ratelimiting, so does the
1881f3e7afe2SHans Petter Selasky 	 * VLAN interface.
1882f3e7afe2SHans Petter Selasky 	 */
1883d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1884d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_TXRTLMT);
1885f3e7afe2SHans Petter Selasky #endif
1886d89baa5aSAlexander Motin 
188766d0c056SJohn Baldwin 	/*
188866d0c056SJohn Baldwin 	 * If the parent interface supports unmapped mbufs, so does
188966d0c056SJohn Baldwin 	 * the VLAN interface.  Note that this should be fine even for
189066d0c056SJohn Baldwin 	 * interfaces that don't support hardware tagging as headers
189166d0c056SJohn Baldwin 	 * are prepended in normal mbufs to unmapped mbufs holding
189266d0c056SJohn Baldwin 	 * payload data.
189366d0c056SJohn Baldwin 	 */
18943f43ada9SGleb Smirnoff 	cap |= (p->if_capabilities & IFCAP_MEXTPG);
18953f43ada9SGleb Smirnoff 	ena |= (mena & IFCAP_MEXTPG);
189666d0c056SJohn Baldwin 
1897b2e60773SJohn Baldwin 	/*
1898b2e60773SJohn Baldwin 	 * If the parent interface can offload encryption and segmentation
1899b2e60773SJohn Baldwin 	 * of TLS records over TCP, propagate it's capability to the VLAN
1900b2e60773SJohn Baldwin 	 * interface.
1901b2e60773SJohn Baldwin 	 *
1902b2e60773SJohn Baldwin 	 * All TLS drivers in the tree today can deal with VLANs.  If
1903b2e60773SJohn Baldwin 	 * this ever changes, then a new IFCAP_VLAN_TXTLS can be
1904b2e60773SJohn Baldwin 	 * defined.
1905b2e60773SJohn Baldwin 	 */
1906521eac97SJohn Baldwin 	if (p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
1907521eac97SJohn Baldwin 		cap |= p->if_capabilities & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
1908521eac97SJohn Baldwin 	if (p->if_capenable & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT))
1909521eac97SJohn Baldwin 		ena |= mena & (IFCAP_TXTLS | IFCAP_TXTLS_RTLMT);
1910b2e60773SJohn Baldwin 
1911d89baa5aSAlexander Motin 	ifp->if_capabilities = cap;
1912d89baa5aSAlexander Motin 	ifp->if_capenable = ena;
1913d89baa5aSAlexander Motin 	ifp->if_hwassist = hwa;
191475ee267cSGleb Smirnoff }
191575ee267cSGleb Smirnoff 
191675ee267cSGleb Smirnoff static void
191775ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
191875ee267cSGleb Smirnoff {
1919b2807792SGleb Smirnoff 	struct epoch_tracker et;
1920d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
192175ee267cSGleb Smirnoff 	struct ifvlan *ifv;
192275ee267cSGleb Smirnoff 
1923d148c2a2SMatt Joras 	VLAN_SLOCK();
1924d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1925d148c2a2SMatt Joras 	if (trunk == NULL) {
1926d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1927d148c2a2SMatt Joras 		return;
1928d148c2a2SMatt Joras 	}
1929b2807792SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1930b8a6e03fSGleb Smirnoff 	VLAN_FOREACH(ifv, trunk)
193175ee267cSGleb Smirnoff 		vlan_capabilities(ifv);
1932b2807792SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1933d148c2a2SMatt Joras 	VLAN_SUNLOCK();
1934127d7b2dSAndre Oppermann }
1935127d7b2dSAndre Oppermann 
1936a3814acfSSam Leffler static int
1937cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
19382cc2df49SGarrett Wollman {
19392cc2df49SGarrett Wollman 	struct ifnet *p;
19402cc2df49SGarrett Wollman 	struct ifreq *ifr;
1941*2884a936SJohn Baldwin #ifdef INET
1942e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
1943*2884a936SJohn Baldwin #endif
19442cc2df49SGarrett Wollman 	struct ifvlan *ifv;
19452d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
19462cc2df49SGarrett Wollman 	struct vlanreq vlr;
194784becee1SAlexander Motin 	int error = 0, oldmtu;
19482cc2df49SGarrett Wollman 
19492cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1950*2884a936SJohn Baldwin #ifdef INET
1951e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
1952*2884a936SJohn Baldwin #endif
19532cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
19542cc2df49SGarrett Wollman 
19552cc2df49SGarrett Wollman 	switch (cmd) {
1956e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1957e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1958e4cd31ddSJeff Roberson #ifdef INET
1959e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1960e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1961e4cd31ddSJeff Roberson #endif
1962e4cd31ddSJeff Roberson 		break;
1963e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
196438d958a6SBrooks Davis 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
196538d958a6SBrooks Davis 		    ifp->if_addrlen);
1966e4cd31ddSJeff Roberson 		break;
1967b3cca108SBill Fenner 	case SIOCGIFMEDIA:
1968d148c2a2SMatt Joras 		VLAN_SLOCK();
196975ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1970d8564efdSEd Maste 			p = PARENT(ifv);
19719bcf3ae4SAlexander Motin 			if_ref(p);
1972d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
19739bcf3ae4SAlexander Motin 			if_rele(p);
1974b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1975b3cca108SBill Fenner 			if (error == 0) {
1976b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1977b3cca108SBill Fenner 
1978b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1979b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1980b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1981b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1982b3cca108SBill Fenner 						ifmr->ifm_ulist,
1983b3cca108SBill Fenner 						sizeof(int));
1984b3cca108SBill Fenner 				}
1985b3cca108SBill Fenner 			}
19864faedfe8SSam Leffler 		} else {
1987b3cca108SBill Fenner 			error = EINVAL;
19884faedfe8SSam Leffler 		}
1989d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1990b3cca108SBill Fenner 		break;
1991b3cca108SBill Fenner 
1992b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1993b3cca108SBill Fenner 		error = EINVAL;
1994b3cca108SBill Fenner 		break;
1995b3cca108SBill Fenner 
19962cc2df49SGarrett Wollman 	case SIOCSIFMTU:
19972cc2df49SGarrett Wollman 		/*
19982cc2df49SGarrett Wollman 		 * Set the interface MTU.
19992cc2df49SGarrett Wollman 		 */
2000d148c2a2SMatt Joras 		VLAN_SLOCK();
2001d148c2a2SMatt Joras 		trunk = TRUNK(ifv);
2002d148c2a2SMatt Joras 		if (trunk != NULL) {
2003d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
2004a3814acfSSam Leffler 			if (ifr->ifr_mtu >
200575ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
2006a3814acfSSam Leffler 			    ifr->ifr_mtu <
2007a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
20082cc2df49SGarrett Wollman 				error = EINVAL;
2009a3814acfSSam Leffler 			else
20102cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
2011d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
2012a3814acfSSam Leffler 		} else
2013a3814acfSSam Leffler 			error = EINVAL;
2014d148c2a2SMatt Joras 		VLAN_SUNLOCK();
20152cc2df49SGarrett Wollman 		break;
20162cc2df49SGarrett Wollman 
20172cc2df49SGarrett Wollman 	case SIOCSETVLAN:
2018ccf7ba97SMarko Zec #ifdef VIMAGE
201915f6780eSRobert Watson 		/*
202015f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
202115f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
202215f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
202315f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
20245a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
202515f6780eSRobert Watson 		 */
2026ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
2027ccf7ba97SMarko Zec 			error = EPERM;
2028ccf7ba97SMarko Zec 			break;
2029ccf7ba97SMarko Zec 		}
2030ccf7ba97SMarko Zec #endif
2031541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
20322cc2df49SGarrett Wollman 		if (error)
20332cc2df49SGarrett Wollman 			break;
20342cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
2035f731f104SBill Paul 			vlan_unconfig(ifp);
20362cc2df49SGarrett Wollman 			break;
20372cc2df49SGarrett Wollman 		}
20389bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
20391bdc73d3SEd Maste 		if (p == NULL) {
20402cc2df49SGarrett Wollman 			error = ENOENT;
20412cc2df49SGarrett Wollman 			break;
20422cc2df49SGarrett Wollman 		}
2043afbb64f1SAlexander V. Chernikov #ifdef COMPAT_FREEBSD12
2044afbb64f1SAlexander V. Chernikov 		if (vlr.vlr_proto == 0)
2045afbb64f1SAlexander V. Chernikov 			vlr.vlr_proto = ETHERTYPE_VLAN;
2046afbb64f1SAlexander V. Chernikov #endif
204784becee1SAlexander Motin 		oldmtu = ifp->if_mtu;
2048c7cffd65SAlexander V. Chernikov 		error = vlan_config(ifv, p, vlr.vlr_tag, vlr.vlr_proto);
20499bcf3ae4SAlexander Motin 		if_rele(p);
205084becee1SAlexander Motin 
205184becee1SAlexander Motin 		/*
205284becee1SAlexander Motin 		 * VLAN MTU may change during addition of the vlandev.
205384becee1SAlexander Motin 		 * If it did, do network layer specific procedure.
205484becee1SAlexander Motin 		 */
205584becee1SAlexander Motin 		if (ifp->if_mtu != oldmtu) {
205684becee1SAlexander Motin #ifdef INET6
205784becee1SAlexander Motin 			nd6_setmtu(ifp);
205884becee1SAlexander Motin #endif
205984becee1SAlexander Motin 			rt_updatemtu(ifp);
206084becee1SAlexander Motin 		}
20612cc2df49SGarrett Wollman 		break;
20622cc2df49SGarrett Wollman 
20632cc2df49SGarrett Wollman 	case SIOCGETVLAN:
2064ccf7ba97SMarko Zec #ifdef VIMAGE
2065ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
2066ccf7ba97SMarko Zec 			error = EPERM;
2067ccf7ba97SMarko Zec 			break;
2068ccf7ba97SMarko Zec 		}
2069ccf7ba97SMarko Zec #endif
207015a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
2071d148c2a2SMatt Joras 		VLAN_SLOCK();
207275ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
207375ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
20749bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
20757983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
2076c7cffd65SAlexander V. Chernikov 			vlr.vlr_proto = ifv->ifv_proto;
20772cc2df49SGarrett Wollman 		}
2078d148c2a2SMatt Joras 		VLAN_SUNLOCK();
2079541d96aaSBrooks Davis 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
20802cc2df49SGarrett Wollman 		break;
20812cc2df49SGarrett Wollman 
20822cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
20832cc2df49SGarrett Wollman 		/*
20841cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
20851cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
20862cc2df49SGarrett Wollman 		 */
2087d148c2a2SMatt Joras 		VLAN_XLOCK();
208875ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
20891cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
2090d148c2a2SMatt Joras 		VLAN_XUNLOCK();
20912cc2df49SGarrett Wollman 		break;
2092a3814acfSSam Leffler 
2093f731f104SBill Paul 	case SIOCADDMULTI:
2094f731f104SBill Paul 	case SIOCDELMULTI:
209575ee267cSGleb Smirnoff 		/*
209675ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
209775ee267cSGleb Smirnoff 		 * when we do.
2098d148c2a2SMatt Joras 		 *
2099d148c2a2SMatt Joras 		 * XXX We need the rmlock here to avoid sleeping while
2100d148c2a2SMatt Joras 		 * holding in6_multi_mtx.
210175ee267cSGleb Smirnoff 		 */
2102b08d611dSMatt Macy 		VLAN_XLOCK();
21032d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
2104b08d611dSMatt Macy 		if (trunk != NULL)
2105f731f104SBill Paul 			error = vlan_setmulti(ifp);
2106b08d611dSMatt Macy 		VLAN_XUNLOCK();
210775ee267cSGleb Smirnoff 
2108b08d611dSMatt Macy 		break;
21092ccbbd06SMarcelo Araujo 	case SIOCGVLANPCP:
21102ccbbd06SMarcelo Araujo #ifdef VIMAGE
21112ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
21122ccbbd06SMarcelo Araujo 			error = EPERM;
21132ccbbd06SMarcelo Araujo 			break;
21142ccbbd06SMarcelo Araujo 		}
21152ccbbd06SMarcelo Araujo #endif
21162ccbbd06SMarcelo Araujo 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
21172ccbbd06SMarcelo Araujo 		break;
21182ccbbd06SMarcelo Araujo 
21192ccbbd06SMarcelo Araujo 	case SIOCSVLANPCP:
21202ccbbd06SMarcelo Araujo #ifdef VIMAGE
21212ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
21222ccbbd06SMarcelo Araujo 			error = EPERM;
21232ccbbd06SMarcelo Araujo 			break;
21242ccbbd06SMarcelo Araujo 		}
21252ccbbd06SMarcelo Araujo #endif
21262ccbbd06SMarcelo Araujo 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
21272ccbbd06SMarcelo Araujo 		if (error)
21282ccbbd06SMarcelo Araujo 			break;
21299ef8cd0bSKristof Provost 		if (ifr->ifr_vlan_pcp > VLAN_PCP_MAX) {
21302ccbbd06SMarcelo Araujo 			error = EINVAL;
21312ccbbd06SMarcelo Araujo 			break;
21322ccbbd06SMarcelo Araujo 		}
21332ccbbd06SMarcelo Araujo 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
213432a52e9eSNavdeep Parhar 		ifp->if_pcp = ifv->ifv_pcp;
21354a381a9eSHans Petter Selasky 		/* broadcast event about PCP change */
21364a381a9eSHans Petter Selasky 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
21372ccbbd06SMarcelo Araujo 		break;
21382ccbbd06SMarcelo Araujo 
2139d89baa5aSAlexander Motin 	case SIOCSIFCAP:
2140d148c2a2SMatt Joras 		VLAN_SLOCK();
2141d89baa5aSAlexander Motin 		ifv->ifv_capenable = ifr->ifr_reqcap;
2142d89baa5aSAlexander Motin 		trunk = TRUNK(ifv);
21436dcec895SGleb Smirnoff 		if (trunk != NULL) {
21446dcec895SGleb Smirnoff 			struct epoch_tracker et;
21456dcec895SGleb Smirnoff 
21466dcec895SGleb Smirnoff 			NET_EPOCH_ENTER(et);
2147d89baa5aSAlexander Motin 			vlan_capabilities(ifv);
21486dcec895SGleb Smirnoff 			NET_EPOCH_EXIT(et);
21496dcec895SGleb Smirnoff 		}
2150d148c2a2SMatt Joras 		VLAN_SUNLOCK();
2151d89baa5aSAlexander Motin 		break;
2152d89baa5aSAlexander Motin 
21532cc2df49SGarrett Wollman 	default:
2154e4cd31ddSJeff Roberson 		error = EINVAL;
2155e4cd31ddSJeff Roberson 		break;
21562cc2df49SGarrett Wollman 	}
215715a66c21SBruce M Simpson 
215815a66c21SBruce M Simpson 	return (error);
21592cc2df49SGarrett Wollman }
2160f3e7afe2SHans Petter Selasky 
2161b2e60773SJohn Baldwin #if defined(KERN_TLS) || defined(RATELIMIT)
2162f3e7afe2SHans Petter Selasky static int
2163f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp,
2164f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *params,
2165f3e7afe2SHans Petter Selasky     struct m_snd_tag **ppmt)
2166f3e7afe2SHans Petter Selasky {
2167fb3bc596SJohn Baldwin 	struct epoch_tracker et;
2168c782ea8bSJohn Baldwin 	const struct if_snd_tag_sw *sw;
2169fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2170fb3bc596SJohn Baldwin 	struct ifvlan *ifv;
2171fb3bc596SJohn Baldwin 	struct ifnet *parent;
2172fb3bc596SJohn Baldwin 	int error;
2173f3e7afe2SHans Petter Selasky 
2174c782ea8bSJohn Baldwin 	switch (params->hdr.type) {
2175c782ea8bSJohn Baldwin #ifdef RATELIMIT
2176c782ea8bSJohn Baldwin 	case IF_SND_TAG_TYPE_UNLIMITED:
2177c782ea8bSJohn Baldwin 		sw = &vlan_snd_tag_ul_sw;
2178c782ea8bSJohn Baldwin 		break;
2179c782ea8bSJohn Baldwin 	case IF_SND_TAG_TYPE_RATE_LIMIT:
2180c782ea8bSJohn Baldwin 		sw = &vlan_snd_tag_rl_sw;
2181c782ea8bSJohn Baldwin 		break;
2182c782ea8bSJohn Baldwin #endif
2183c782ea8bSJohn Baldwin #ifdef KERN_TLS
2184c782ea8bSJohn Baldwin 	case IF_SND_TAG_TYPE_TLS:
2185c782ea8bSJohn Baldwin 		sw = &vlan_snd_tag_tls_sw;
2186c782ea8bSJohn Baldwin 		break;
2187c782ea8bSJohn Baldwin #ifdef RATELIMIT
2188c782ea8bSJohn Baldwin 	case IF_SND_TAG_TYPE_TLS_RATE_LIMIT:
2189c782ea8bSJohn Baldwin 		sw = &vlan_snd_tag_tls_rl_sw;
2190c782ea8bSJohn Baldwin 		break;
2191c782ea8bSJohn Baldwin #endif
2192c782ea8bSJohn Baldwin #endif
2193c782ea8bSJohn Baldwin 	default:
2194c782ea8bSJohn Baldwin 		return (EOPNOTSUPP);
2195c782ea8bSJohn Baldwin 	}
2196c782ea8bSJohn Baldwin 
2197fb3bc596SJohn Baldwin 	NET_EPOCH_ENTER(et);
2198fb3bc596SJohn Baldwin 	ifv = ifp->if_softc;
2199fb3bc596SJohn Baldwin 	if (ifv->ifv_trunk != NULL)
2200fb3bc596SJohn Baldwin 		parent = PARENT(ifv);
2201fb3bc596SJohn Baldwin 	else
2202fb3bc596SJohn Baldwin 		parent = NULL;
220336e0a362SJohn Baldwin 	if (parent == NULL) {
2204fb3bc596SJohn Baldwin 		NET_EPOCH_EXIT(et);
2205f3e7afe2SHans Petter Selasky 		return (EOPNOTSUPP);
2206fb3bc596SJohn Baldwin 	}
2207fb3bc596SJohn Baldwin 	if_ref(parent);
2208fb3bc596SJohn Baldwin 	NET_EPOCH_EXIT(et);
2209fb3bc596SJohn Baldwin 
2210fb3bc596SJohn Baldwin 	vst = malloc(sizeof(*vst), M_VLAN, M_NOWAIT);
2211fb3bc596SJohn Baldwin 	if (vst == NULL) {
2212fb3bc596SJohn Baldwin 		if_rele(parent);
2213fb3bc596SJohn Baldwin 		return (ENOMEM);
2214fb3bc596SJohn Baldwin 	}
2215fb3bc596SJohn Baldwin 
221636e0a362SJohn Baldwin 	error = m_snd_tag_alloc(parent, params, &vst->tag);
2217fb3bc596SJohn Baldwin 	if_rele(parent);
2218fb3bc596SJohn Baldwin 	if (error) {
2219fb3bc596SJohn Baldwin 		free(vst, M_VLAN);
2220fb3bc596SJohn Baldwin 		return (error);
2221fb3bc596SJohn Baldwin 	}
2222fb3bc596SJohn Baldwin 
2223c782ea8bSJohn Baldwin 	m_snd_tag_init(&vst->com, ifp, sw);
2224fb3bc596SJohn Baldwin 
2225fb3bc596SJohn Baldwin 	*ppmt = &vst->com;
2226fb3bc596SJohn Baldwin 	return (0);
2227fb3bc596SJohn Baldwin }
2228fb3bc596SJohn Baldwin 
22291a714ff2SRandall Stewart static struct m_snd_tag *
22301a714ff2SRandall Stewart vlan_next_snd_tag(struct m_snd_tag *mst)
22311a714ff2SRandall Stewart {
22321a714ff2SRandall Stewart 	struct vlan_snd_tag *vst;
22331a714ff2SRandall Stewart 
22341a714ff2SRandall Stewart 	vst = mst_to_vst(mst);
22351a714ff2SRandall Stewart 	return (vst->tag);
22361a714ff2SRandall Stewart }
22371a714ff2SRandall Stewart 
2238fb3bc596SJohn Baldwin static int
2239fb3bc596SJohn Baldwin vlan_snd_tag_modify(struct m_snd_tag *mst,
2240fb3bc596SJohn Baldwin     union if_snd_tag_modify_params *params)
2241fb3bc596SJohn Baldwin {
2242fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2243fb3bc596SJohn Baldwin 
2244fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2245c782ea8bSJohn Baldwin 	return (vst->tag->sw->snd_tag_modify(vst->tag, params));
2246fb3bc596SJohn Baldwin }
2247fb3bc596SJohn Baldwin 
2248fb3bc596SJohn Baldwin static int
2249fb3bc596SJohn Baldwin vlan_snd_tag_query(struct m_snd_tag *mst,
2250fb3bc596SJohn Baldwin     union if_snd_tag_query_params *params)
2251fb3bc596SJohn Baldwin {
2252fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2253fb3bc596SJohn Baldwin 
2254fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2255c782ea8bSJohn Baldwin 	return (vst->tag->sw->snd_tag_query(vst->tag, params));
2256f3e7afe2SHans Petter Selasky }
2257fa91f845SRandall Stewart 
2258fa91f845SRandall Stewart static void
2259fb3bc596SJohn Baldwin vlan_snd_tag_free(struct m_snd_tag *mst)
2260fa91f845SRandall Stewart {
2261fb3bc596SJohn Baldwin 	struct vlan_snd_tag *vst;
2262fb3bc596SJohn Baldwin 
2263fb3bc596SJohn Baldwin 	vst = mst_to_vst(mst);
2264fb3bc596SJohn Baldwin 	m_snd_tag_rele(vst->tag);
2265fb3bc596SJohn Baldwin 	free(vst, M_VLAN);
2266fa91f845SRandall Stewart }
22671a714ff2SRandall Stewart 
22681a714ff2SRandall Stewart static void
22691a714ff2SRandall Stewart vlan_ratelimit_query(struct ifnet *ifp __unused, struct if_ratelimit_query_results *q)
22701a714ff2SRandall Stewart {
22711a714ff2SRandall Stewart 	/*
22721a714ff2SRandall Stewart 	 * For vlan, we have an indirect
22731a714ff2SRandall Stewart 	 * interface. The caller needs to
22741a714ff2SRandall Stewart 	 * get a ratelimit tag on the actual
22751a714ff2SRandall Stewart 	 * interface the flow will go on.
22761a714ff2SRandall Stewart 	 */
22771a714ff2SRandall Stewart 	q->rate_table = NULL;
22781a714ff2SRandall Stewart 	q->flags = RT_IS_INDIRECT;
22791a714ff2SRandall Stewart 	q->max_flows = 0;
22801a714ff2SRandall Stewart 	q->number_of_rates = 0;
22811a714ff2SRandall Stewart }
22821a714ff2SRandall Stewart 
2283f3e7afe2SHans Petter Selasky #endif
2284