xref: /freebsd/sys/net/if_vlan.c (revision a68cc388790587b330a01380a0c1864fb9ff3f1e)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32ccbbd06SMarcelo Araujo  * Copyright 2012 ADARA Networks, Inc.
4d148c2a2SMatt Joras  * Copyright 2017 Dell EMC Isilon
52ccbbd06SMarcelo Araujo  *
62ccbbd06SMarcelo Araujo  * Portions of this software were developed by Robert N. M. Watson under
72ccbbd06SMarcelo Araujo  * contract to ADARA Networks, Inc.
82cc2df49SGarrett Wollman  *
92cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
102cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
112cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
122cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
132cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
142cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
152cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
162cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
172cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
182cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
192cc2df49SGarrett Wollman  * warranty.
202cc2df49SGarrett Wollman  *
212cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
222cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
232cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
242cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
252cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
262cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
272cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
282cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
292cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
302cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
312cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322cc2df49SGarrett Wollman  * SUCH DAMAGE.
332cc2df49SGarrett Wollman  */
342cc2df49SGarrett Wollman 
352cc2df49SGarrett Wollman /*
362cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
372ccbbd06SMarcelo Araujo  * This is sort of sneaky in the implementation, since
382cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
392cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
402cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
41d9b1d615SJohn Baldwin  * ether_output() sends to us via if_transmit(), rewrite them for
42d9b1d615SJohn Baldwin  * use by the real outgoing interface, and ask it to send them.
432cc2df49SGarrett Wollman  */
442cc2df49SGarrett Wollman 
458b2d9181SPyun YongHyeon #include <sys/cdefs.h>
468b2d9181SPyun YongHyeon __FBSDID("$FreeBSD$");
478b2d9181SPyun YongHyeon 
482c5b403eSOleg Bulyzhin #include "opt_inet.h"
4975ee267cSGleb Smirnoff #include "opt_vlan.h"
50f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
512cc2df49SGarrett Wollman 
522cc2df49SGarrett Wollman #include <sys/param.h>
53c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
542cc2df49SGarrett Wollman #include <sys/kernel.h>
5575ee267cSGleb Smirnoff #include <sys/lock.h>
56f731f104SBill Paul #include <sys/malloc.h>
572cc2df49SGarrett Wollman #include <sys/mbuf.h>
582b120974SPeter Wemm #include <sys/module.h>
59772b000fSAlexander V. Chernikov #include <sys/rmlock.h>
602ccbbd06SMarcelo Araujo #include <sys/priv.h>
61f731f104SBill Paul #include <sys/queue.h>
622cc2df49SGarrett Wollman #include <sys/socket.h>
632cc2df49SGarrett Wollman #include <sys/sockio.h>
642cc2df49SGarrett Wollman #include <sys/sysctl.h>
652cc2df49SGarrett Wollman #include <sys/systm.h>
66e4cd31ddSJeff Roberson #include <sys/sx.h>
67d148c2a2SMatt Joras #include <sys/taskqueue.h>
682cc2df49SGarrett Wollman 
692cc2df49SGarrett Wollman #include <net/bpf.h>
702cc2df49SGarrett Wollman #include <net/ethernet.h>
712cc2df49SGarrett Wollman #include <net/if.h>
7276039bc8SGleb Smirnoff #include <net/if_var.h>
73f889d2efSBrooks Davis #include <net/if_clone.h>
742cc2df49SGarrett Wollman #include <net/if_dl.h>
752cc2df49SGarrett Wollman #include <net/if_types.h>
762cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
774b79449eSBjoern A. Zeeb #include <net/vnet.h>
782cc2df49SGarrett Wollman 
792c5b403eSOleg Bulyzhin #ifdef INET
802c5b403eSOleg Bulyzhin #include <netinet/in.h>
812c5b403eSOleg Bulyzhin #include <netinet/if_ether.h>
822c5b403eSOleg Bulyzhin #endif
832c5b403eSOleg Bulyzhin 
8475ee267cSGleb Smirnoff #define	VLAN_DEF_HWIDTH	4
8564a17d2eSYaroslav Tykhiy #define	VLAN_IFFLAGS	(IFF_BROADCAST | IFF_MULTICAST)
8675ee267cSGleb Smirnoff 
872dc879b3SYaroslav Tykhiy #define	UP_AND_RUNNING(ifp) \
882dc879b3SYaroslav Tykhiy     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
892dc879b3SYaroslav Tykhiy 
90b08d611dSMatt Macy CK_SLIST_HEAD(ifvlanhead, ifvlan);
9175ee267cSGleb Smirnoff 
9275ee267cSGleb Smirnoff struct ifvlantrunk {
9375ee267cSGleb Smirnoff 	struct	ifnet   *parent;	/* parent interface of this trunk */
94b08d611dSMatt Macy 	struct	mtx	lock;
9575ee267cSGleb Smirnoff #ifdef VLAN_ARRAY
965cb8c31aSYaroslav Tykhiy #define	VLAN_ARRAY_SIZE	(EVL_VLID_MASK + 1)
975cb8c31aSYaroslav Tykhiy 	struct	ifvlan	*vlans[VLAN_ARRAY_SIZE]; /* static table */
9875ee267cSGleb Smirnoff #else
9975ee267cSGleb Smirnoff 	struct	ifvlanhead *hash;	/* dynamic hash-list table */
10075ee267cSGleb Smirnoff 	uint16_t	hmask;
10175ee267cSGleb Smirnoff 	uint16_t	hwidth;
10275ee267cSGleb Smirnoff #endif
10375ee267cSGleb Smirnoff 	int		refcnt;
10475ee267cSGleb Smirnoff };
1059d4fe4b2SBrooks Davis 
106d148c2a2SMatt Joras /*
107d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk with
108d148c2a2SMatt Joras  * the assumption that none will be added/removed during iteration.
109d148c2a2SMatt Joras  */
110d148c2a2SMatt Joras #ifdef VLAN_ARRAY
111d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
112d148c2a2SMatt Joras 	size_t _i; \
113d148c2a2SMatt Joras 	for (_i = 0; _i < VLAN_ARRAY_SIZE; _i++) \
114d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]) != NULL)
115d148c2a2SMatt Joras #else /* VLAN_ARRAY */
116d148c2a2SMatt Joras #define VLAN_FOREACH(_ifv, _trunk) \
117d148c2a2SMatt Joras 	struct ifvlan *_next; \
118d148c2a2SMatt Joras 	size_t _i; \
119d148c2a2SMatt Joras 	for (_i = 0; _i < (1 << (_trunk)->hwidth); _i++) \
120b08d611dSMatt Macy 		CK_SLIST_FOREACH_SAFE((_ifv), &(_trunk)->hash[_i], ifv_list, _next)
121d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
122d148c2a2SMatt Joras 
123d148c2a2SMatt Joras /*
124d148c2a2SMatt Joras  * This macro provides a facility to iterate over every vlan on a trunk while
125d148c2a2SMatt Joras  * also modifying the number of vlans on the trunk. The iteration continues
126d148c2a2SMatt Joras  * until some condition is met or there are no more vlans on the trunk.
127d148c2a2SMatt Joras  */
128d148c2a2SMatt Joras #ifdef VLAN_ARRAY
129d148c2a2SMatt Joras /* The VLAN_ARRAY case is simple -- just a for loop using the condition. */
130d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
131d148c2a2SMatt Joras 	size_t _i; \
132d148c2a2SMatt Joras 	for (_i = 0; !(_cond) && _i < VLAN_ARRAY_SIZE; _i++) \
133d148c2a2SMatt Joras 		if (((_ifv) = (_trunk)->vlans[_i]))
134d148c2a2SMatt Joras #else /* VLAN_ARRAY */
135d148c2a2SMatt Joras /*
136d148c2a2SMatt Joras  * The hash table case is more complicated. We allow for the hash table to be
137d148c2a2SMatt Joras  * modified (i.e. vlans removed) while we are iterating over it. To allow for
138d148c2a2SMatt Joras  * this we must restart the iteration every time we "touch" something during
139d148c2a2SMatt Joras  * the iteration, since removal will resize the hash table and invalidate our
140d148c2a2SMatt Joras  * current position. If acting on the touched element causes the trunk to be
141d148c2a2SMatt Joras  * emptied, then iteration also stops.
142d148c2a2SMatt Joras  */
143d148c2a2SMatt Joras #define VLAN_FOREACH_UNTIL_SAFE(_ifv, _trunk, _cond) \
144d148c2a2SMatt Joras 	size_t _i; \
145d148c2a2SMatt Joras 	bool _touch = false; \
146d148c2a2SMatt Joras 	for (_i = 0; \
147d148c2a2SMatt Joras 	    !(_cond) && _i < (1 << (_trunk)->hwidth); \
148d148c2a2SMatt Joras 	    _i = (_touch && ((_trunk) != NULL) ? 0 : _i + 1), _touch = false) \
149b08d611dSMatt Macy 		if (((_ifv) = CK_SLIST_FIRST(&(_trunk)->hash[_i])) != NULL && \
150d148c2a2SMatt Joras 		    (_touch = true))
151d148c2a2SMatt Joras #endif /* VLAN_ARRAY */
152d148c2a2SMatt Joras 
153a3814acfSSam Leffler struct vlan_mc_entry {
154e4cd31ddSJeff Roberson 	struct sockaddr_dl		mc_addr;
155b08d611dSMatt Macy 	CK_SLIST_ENTRY(vlan_mc_entry)	mc_entries;
156c32a9d66SHans Petter Selasky 	struct epoch_context		mc_epoch_ctx;
157a3814acfSSam Leffler };
158a3814acfSSam Leffler 
159a3814acfSSam Leffler struct	ifvlan {
16075ee267cSGleb Smirnoff 	struct	ifvlantrunk *ifv_trunk;
161fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
16275ee267cSGleb Smirnoff #define	TRUNK(ifv)	((ifv)->ifv_trunk)
16375ee267cSGleb Smirnoff #define	PARENT(ifv)	((ifv)->ifv_trunk->parent)
1646667db31SAlexander V. Chernikov 	void	*ifv_cookie;
1651cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
166d89baa5aSAlexander Motin 	int	ifv_capenable;
167a3814acfSSam Leffler 	struct	ifv_linkmib {
168a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
169a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
170a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
17173f2233dSYaroslav Tykhiy 		uint16_t ifvm_proto;	/* encapsulation ethertype */
17275ee267cSGleb Smirnoff 		uint16_t ifvm_tag;	/* tag to apply on packets leaving if */
1732ccbbd06SMarcelo Araujo               	uint16_t ifvm_vid;	/* VLAN ID */
1742ccbbd06SMarcelo Araujo 		uint8_t	ifvm_pcp;	/* Priority Code Point (PCP). */
175a3814acfSSam Leffler 	}	ifv_mib;
176d148c2a2SMatt Joras 	struct task lladdr_task;
177b08d611dSMatt Macy 	CK_SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
178c0cb022bSYaroslav Tykhiy #ifndef VLAN_ARRAY
179b08d611dSMatt Macy 	CK_SLIST_ENTRY(ifvlan) ifv_list;
180c0cb022bSYaroslav Tykhiy #endif
181a3814acfSSam Leffler };
18273f2233dSYaroslav Tykhiy #define	ifv_proto	ifv_mib.ifvm_proto
1832ccbbd06SMarcelo Araujo #define	ifv_tag		ifv_mib.ifvm_tag
1842ccbbd06SMarcelo Araujo #define	ifv_vid 	ifv_mib.ifvm_vid
1852ccbbd06SMarcelo Araujo #define	ifv_pcp		ifv_mib.ifvm_pcp
186a3814acfSSam Leffler #define	ifv_encaplen	ifv_mib.ifvm_encaplen
187a3814acfSSam Leffler #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
188a3814acfSSam Leffler #define	ifv_mintu	ifv_mib.ifvm_mintu
189a3814acfSSam Leffler 
19075ee267cSGleb Smirnoff /* Special flags we should propagate to parent. */
1911cf236fbSYaroslav Tykhiy static struct {
1921cf236fbSYaroslav Tykhiy 	int flag;
1931cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
1941cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
1951cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
1961cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
1971cf236fbSYaroslav Tykhiy 	{0, NULL}
1981cf236fbSYaroslav Tykhiy };
199a3814acfSSam Leffler 
200f1379734SKonstantin Belousov extern int vlan_mtag_pcp;
2012ccbbd06SMarcelo Araujo 
20242a58907SGleb Smirnoff static const char vlanname[] = "vlan";
20342a58907SGleb Smirnoff static MALLOC_DEFINE(M_VLAN, vlanname, "802.1Q Virtual LAN Interface");
2042cc2df49SGarrett Wollman 
2055cb8c31aSYaroslav Tykhiy static eventhandler_tag ifdetach_tag;
206ea4ca115SAndrew Thompson static eventhandler_tag iflladdr_tag;
2075cb8c31aSYaroslav Tykhiy 
2084faedfe8SSam Leffler /*
209b08d611dSMatt Macy  * if_vlan uses two module-level synchronizations primitives to allow concurrent
210b08d611dSMatt Macy  * modification of vlan interfaces and (mostly) allow for vlans to be destroyed
211b08d611dSMatt Macy  * while they are being used for tx/rx. To accomplish this in a way that has
212b08d611dSMatt Macy  * acceptable performance and cooperation with other parts of the network stack
213b08d611dSMatt Macy  * there is a non-sleepable epoch(9) and an sx(9).
21475ee267cSGleb Smirnoff  *
215b08d611dSMatt Macy  * The performance-sensitive paths that warrant using the epoch(9) are
216d148c2a2SMatt Joras  * vlan_transmit and vlan_input. Both have to check for the vlan interface's
217d148c2a2SMatt Joras  * existence using if_vlantrunk, and being in the network tx/rx paths the use
218b08d611dSMatt Macy  * of an epoch(9) gives a measureable improvement in performance.
21975ee267cSGleb Smirnoff  *
220d148c2a2SMatt Joras  * The reason for having an sx(9) is mostly because there are still areas that
221d148c2a2SMatt Joras  * must be sleepable and also have safe concurrent access to a vlan interface.
222d148c2a2SMatt Joras  * Since the sx(9) exists, it is used by default in most paths unless sleeping
223d148c2a2SMatt Joras  * is not permitted, or if it is not clear whether sleeping is permitted.
224d148c2a2SMatt Joras  *
2254faedfe8SSam Leffler  */
226d148c2a2SMatt Joras #define _VLAN_SX_ID ifv_sx
227d148c2a2SMatt Joras 
228d148c2a2SMatt Joras static struct sx _VLAN_SX_ID;
229d148c2a2SMatt Joras 
230d148c2a2SMatt Joras #define VLAN_LOCKING_INIT() \
231d148c2a2SMatt Joras 	sx_init(&_VLAN_SX_ID, "vlan_sx")
232d148c2a2SMatt Joras 
233d148c2a2SMatt Joras #define VLAN_LOCKING_DESTROY() \
234d148c2a2SMatt Joras 	sx_destroy(&_VLAN_SX_ID)
235d148c2a2SMatt Joras 
236d148c2a2SMatt Joras #define	VLAN_SLOCK()			sx_slock(&_VLAN_SX_ID)
237d148c2a2SMatt Joras #define	VLAN_SUNLOCK()			sx_sunlock(&_VLAN_SX_ID)
238d148c2a2SMatt Joras #define	VLAN_XLOCK()			sx_xlock(&_VLAN_SX_ID)
239d148c2a2SMatt Joras #define	VLAN_XUNLOCK()			sx_xunlock(&_VLAN_SX_ID)
240d148c2a2SMatt Joras #define	VLAN_SLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_SLOCKED)
241d148c2a2SMatt Joras #define	VLAN_XLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_XLOCKED)
242d148c2a2SMatt Joras #define	VLAN_SXLOCK_ASSERT()		sx_assert(&_VLAN_SX_ID, SA_LOCKED)
243d148c2a2SMatt Joras 
244d148c2a2SMatt Joras 
245d148c2a2SMatt Joras /*
246b08d611dSMatt Macy  * We also have a per-trunk mutex that should be acquired when changing
247b08d611dSMatt Macy  * its state.
248d148c2a2SMatt Joras  */
249b08d611dSMatt Macy #define	TRUNK_LOCK_INIT(trunk)		mtx_init(&(trunk)->lock, vlanname, NULL, MTX_DEF)
250b08d611dSMatt Macy #define	TRUNK_LOCK_DESTROY(trunk)	mtx_destroy(&(trunk)->lock)
251b08d611dSMatt Macy #define	TRUNK_WLOCK(trunk)		mtx_lock(&(trunk)->lock)
252b08d611dSMatt Macy #define	TRUNK_WUNLOCK(trunk)		mtx_unlock(&(trunk)->lock)
253b08d611dSMatt Macy #define	TRUNK_LOCK_ASSERT(trunk)	MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(trunk)->lock))
254b08d611dSMatt Macy #define	TRUNK_WLOCK_ASSERT(trunk)	mtx_assert(&(trunk)->lock, MA_OWNED);
25575ee267cSGleb Smirnoff 
256d148c2a2SMatt Joras /*
257d148c2a2SMatt Joras  * The VLAN_ARRAY substitutes the dynamic hash with a static array
258d148c2a2SMatt Joras  * with 4096 entries. In theory this can give a boost in processing,
259d148c2a2SMatt Joras  * however in practice it does not. Probably this is because the array
260d148c2a2SMatt Joras  * is too big to fit into CPU cache.
261d148c2a2SMatt Joras  */
26275ee267cSGleb Smirnoff #ifndef VLAN_ARRAY
26375ee267cSGleb Smirnoff static	void vlan_inithash(struct ifvlantrunk *trunk);
26475ee267cSGleb Smirnoff static	void vlan_freehash(struct ifvlantrunk *trunk);
26575ee267cSGleb Smirnoff static	int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
26675ee267cSGleb Smirnoff static	int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
26775ee267cSGleb Smirnoff static	void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
26875ee267cSGleb Smirnoff static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
2697983103aSRobert Watson 	uint16_t vid);
27075ee267cSGleb Smirnoff #endif
27175ee267cSGleb Smirnoff static	void trunk_destroy(struct ifvlantrunk *trunk);
2724faedfe8SSam Leffler 
273114c608cSYaroslav Tykhiy static	void vlan_init(void *foo);
274a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
275cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
276f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
277f3e7afe2SHans Petter Selasky static	int vlan_snd_tag_alloc(struct ifnet *,
278f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *, struct m_snd_tag **);
279f3e7afe2SHans Petter Selasky #endif
280d9b1d615SJohn Baldwin static	void vlan_qflush(struct ifnet *ifp);
2811cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
2821cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
2831cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
284f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
285d9b1d615SJohn Baldwin static	int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
2866f359e28SJohn Baldwin static	void vlan_unconfig(struct ifnet *ifp);
28728cc4d37SJohn Baldwin static	void vlan_unconfig_locked(struct ifnet *ifp, int departing);
28875ee267cSGleb Smirnoff static	int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
289a6fffd6cSBrooks Davis static	void vlan_link_state(struct ifnet *ifp);
29075ee267cSGleb Smirnoff static	void vlan_capabilities(struct ifvlan *ifv);
29175ee267cSGleb Smirnoff static	void vlan_trunk_capabilities(struct ifnet *ifp);
292f731f104SBill Paul 
293f941c31aSGleb Smirnoff static	struct ifnet *vlan_clone_match_ethervid(const char *, int *);
294f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
2956b7330e2SSam Leffler static	int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
296f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
297f889d2efSBrooks Davis 
2985cb8c31aSYaroslav Tykhiy static	void vlan_ifdetach(void *arg, struct ifnet *ifp);
299ea4ca115SAndrew Thompson static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
3005cb8c31aSYaroslav Tykhiy 
301d148c2a2SMatt Joras static  void vlan_lladdr_fn(void *arg, int pending);
302d148c2a2SMatt Joras 
30342a58907SGleb Smirnoff static struct if_clone *vlan_cloner;
3049d4fe4b2SBrooks Davis 
305ccf7ba97SMarko Zec #ifdef VIMAGE
3065f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, vlan_cloner);
307ccf7ba97SMarko Zec #define	V_vlan_cloner	VNET(vlan_cloner)
308ccf7ba97SMarko Zec #endif
309ccf7ba97SMarko Zec 
31075ee267cSGleb Smirnoff static void
311c32a9d66SHans Petter Selasky vlan_mc_free(struct epoch_context *ctx)
312c32a9d66SHans Petter Selasky {
313c32a9d66SHans Petter Selasky 	struct vlan_mc_entry *mc = __containerof(ctx, struct vlan_mc_entry, mc_epoch_ctx);
314c32a9d66SHans Petter Selasky 	free(mc, M_VLAN);
315c32a9d66SHans Petter Selasky }
316c32a9d66SHans Petter Selasky 
317cac30248SOleg Bulyzhin #ifndef VLAN_ARRAY
318cac30248SOleg Bulyzhin #define HASH(n, m)	((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
319cac30248SOleg Bulyzhin 
320c32a9d66SHans Petter Selasky static void
32175ee267cSGleb Smirnoff vlan_inithash(struct ifvlantrunk *trunk)
32275ee267cSGleb Smirnoff {
32375ee267cSGleb Smirnoff 	int i, n;
32475ee267cSGleb Smirnoff 
32575ee267cSGleb Smirnoff 	/*
32675ee267cSGleb Smirnoff 	 * The trunk must not be locked here since we call malloc(M_WAITOK).
32775ee267cSGleb Smirnoff 	 * It is OK in case this function is called before the trunk struct
32875ee267cSGleb Smirnoff 	 * gets hooked up and becomes visible from other threads.
32975ee267cSGleb Smirnoff 	 */
33075ee267cSGleb Smirnoff 
33175ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
33275ee267cSGleb Smirnoff 	    ("%s: hash already initialized", __func__));
33375ee267cSGleb Smirnoff 
33475ee267cSGleb Smirnoff 	trunk->hwidth = VLAN_DEF_HWIDTH;
33575ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
33675ee267cSGleb Smirnoff 	trunk->hmask = n - 1;
33775ee267cSGleb Smirnoff 	trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
33875ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
339b08d611dSMatt Macy 		CK_SLIST_INIT(&trunk->hash[i]);
34075ee267cSGleb Smirnoff }
34175ee267cSGleb Smirnoff 
34275ee267cSGleb Smirnoff static void
34375ee267cSGleb Smirnoff vlan_freehash(struct ifvlantrunk *trunk)
34475ee267cSGleb Smirnoff {
34575ee267cSGleb Smirnoff #ifdef INVARIANTS
34675ee267cSGleb Smirnoff 	int i;
34775ee267cSGleb Smirnoff 
34875ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
34975ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++)
350b08d611dSMatt Macy 		KASSERT(CK_SLIST_EMPTY(&trunk->hash[i]),
35175ee267cSGleb Smirnoff 		    ("%s: hash table not empty", __func__));
35275ee267cSGleb Smirnoff #endif
35375ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
35475ee267cSGleb Smirnoff 	trunk->hash = NULL;
35575ee267cSGleb Smirnoff 	trunk->hwidth = trunk->hmask = 0;
35675ee267cSGleb Smirnoff }
35775ee267cSGleb Smirnoff 
35875ee267cSGleb Smirnoff static int
35975ee267cSGleb Smirnoff vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
36075ee267cSGleb Smirnoff {
36175ee267cSGleb Smirnoff 	int i, b;
36275ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
36375ee267cSGleb Smirnoff 
364b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
36575ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
36675ee267cSGleb Smirnoff 
36775ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
3687983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
369b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
3707983103aSRobert Watson 		if (ifv->ifv_vid == ifv2->ifv_vid)
37175ee267cSGleb Smirnoff 			return (EEXIST);
37275ee267cSGleb Smirnoff 
37375ee267cSGleb Smirnoff 	/*
37475ee267cSGleb Smirnoff 	 * Grow the hash when the number of vlans exceeds half of the number of
37575ee267cSGleb Smirnoff 	 * hash buckets squared. This will make the average linked-list length
37675ee267cSGleb Smirnoff 	 * buckets/2.
37775ee267cSGleb Smirnoff 	 */
37875ee267cSGleb Smirnoff 	if (trunk->refcnt > (b * b) / 2) {
37975ee267cSGleb Smirnoff 		vlan_growhash(trunk, 1);
3807983103aSRobert Watson 		i = HASH(ifv->ifv_vid, trunk->hmask);
38175ee267cSGleb Smirnoff 	}
382b08d611dSMatt Macy 	CK_SLIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
38375ee267cSGleb Smirnoff 	trunk->refcnt++;
38475ee267cSGleb Smirnoff 
38575ee267cSGleb Smirnoff 	return (0);
38675ee267cSGleb Smirnoff }
38775ee267cSGleb Smirnoff 
38875ee267cSGleb Smirnoff static int
38975ee267cSGleb Smirnoff vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
39075ee267cSGleb Smirnoff {
39175ee267cSGleb Smirnoff 	int i, b;
39275ee267cSGleb Smirnoff 	struct ifvlan *ifv2;
39375ee267cSGleb Smirnoff 
394b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
39575ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
39675ee267cSGleb Smirnoff 
39775ee267cSGleb Smirnoff 	b = 1 << trunk->hwidth;
3987983103aSRobert Watson 	i = HASH(ifv->ifv_vid, trunk->hmask);
399b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
40075ee267cSGleb Smirnoff 		if (ifv2 == ifv) {
40175ee267cSGleb Smirnoff 			trunk->refcnt--;
402b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv2, ifvlan, ifv_list);
40375ee267cSGleb Smirnoff 			if (trunk->refcnt < (b * b) / 2)
40475ee267cSGleb Smirnoff 				vlan_growhash(trunk, -1);
40575ee267cSGleb Smirnoff 			return (0);
40675ee267cSGleb Smirnoff 		}
40775ee267cSGleb Smirnoff 
40875ee267cSGleb Smirnoff 	panic("%s: vlan not found\n", __func__);
40975ee267cSGleb Smirnoff 	return (ENOENT); /*NOTREACHED*/
41075ee267cSGleb Smirnoff }
41175ee267cSGleb Smirnoff 
41275ee267cSGleb Smirnoff /*
41375ee267cSGleb Smirnoff  * Grow the hash larger or smaller if memory permits.
41475ee267cSGleb Smirnoff  */
41575ee267cSGleb Smirnoff static void
41675ee267cSGleb Smirnoff vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
41775ee267cSGleb Smirnoff {
41875ee267cSGleb Smirnoff 	struct ifvlan *ifv;
41975ee267cSGleb Smirnoff 	struct ifvlanhead *hash2;
42075ee267cSGleb Smirnoff 	int hwidth2, i, j, n, n2;
42175ee267cSGleb Smirnoff 
422b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
42375ee267cSGleb Smirnoff 	KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
42475ee267cSGleb Smirnoff 
42575ee267cSGleb Smirnoff 	if (howmuch == 0) {
42675ee267cSGleb Smirnoff 		/* Harmless yet obvious coding error */
42775ee267cSGleb Smirnoff 		printf("%s: howmuch is 0\n", __func__);
42875ee267cSGleb Smirnoff 		return;
42975ee267cSGleb Smirnoff 	}
43075ee267cSGleb Smirnoff 
43175ee267cSGleb Smirnoff 	hwidth2 = trunk->hwidth + howmuch;
43275ee267cSGleb Smirnoff 	n = 1 << trunk->hwidth;
43375ee267cSGleb Smirnoff 	n2 = 1 << hwidth2;
43475ee267cSGleb Smirnoff 	/* Do not shrink the table below the default */
43575ee267cSGleb Smirnoff 	if (hwidth2 < VLAN_DEF_HWIDTH)
43675ee267cSGleb Smirnoff 		return;
43775ee267cSGleb Smirnoff 
438b08d611dSMatt Macy 	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_WAITOK);
43975ee267cSGleb Smirnoff 	if (hash2 == NULL) {
44075ee267cSGleb Smirnoff 		printf("%s: out of memory -- hash size not changed\n",
44175ee267cSGleb Smirnoff 		    __func__);
44275ee267cSGleb Smirnoff 		return;		/* We can live with the old hash table */
44375ee267cSGleb Smirnoff 	}
44475ee267cSGleb Smirnoff 	for (j = 0; j < n2; j++)
445b08d611dSMatt Macy 		CK_SLIST_INIT(&hash2[j]);
44675ee267cSGleb Smirnoff 	for (i = 0; i < n; i++)
447b08d611dSMatt Macy 		while ((ifv = CK_SLIST_FIRST(&trunk->hash[i])) != NULL) {
448b08d611dSMatt Macy 			CK_SLIST_REMOVE(&trunk->hash[i], ifv, ifvlan, ifv_list);
4497983103aSRobert Watson 			j = HASH(ifv->ifv_vid, n2 - 1);
450b08d611dSMatt Macy 			CK_SLIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
45175ee267cSGleb Smirnoff 		}
452b08d611dSMatt Macy 	NET_EPOCH_WAIT();
45375ee267cSGleb Smirnoff 	free(trunk->hash, M_VLAN);
45475ee267cSGleb Smirnoff 	trunk->hash = hash2;
45575ee267cSGleb Smirnoff 	trunk->hwidth = hwidth2;
45675ee267cSGleb Smirnoff 	trunk->hmask = n2 - 1;
457f84b2d69SYaroslav Tykhiy 
458f84b2d69SYaroslav Tykhiy 	if (bootverbose)
459f84b2d69SYaroslav Tykhiy 		if_printf(trunk->parent,
460f84b2d69SYaroslav Tykhiy 		    "VLAN hash table resized from %d to %d buckets\n", n, n2);
46175ee267cSGleb Smirnoff }
46275ee267cSGleb Smirnoff 
46375ee267cSGleb Smirnoff static __inline struct ifvlan *
4647983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
46575ee267cSGleb Smirnoff {
46675ee267cSGleb Smirnoff 	struct ifvlan *ifv;
46775ee267cSGleb Smirnoff 
468*a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
46975ee267cSGleb Smirnoff 
470b08d611dSMatt Macy 	CK_SLIST_FOREACH(ifv, &trunk->hash[HASH(vid, trunk->hmask)], ifv_list)
4717983103aSRobert Watson 		if (ifv->ifv_vid == vid)
47275ee267cSGleb Smirnoff 			return (ifv);
47375ee267cSGleb Smirnoff 	return (NULL);
47475ee267cSGleb Smirnoff }
47575ee267cSGleb Smirnoff 
47675ee267cSGleb Smirnoff #if 0
47775ee267cSGleb Smirnoff /* Debugging code to view the hashtables. */
47875ee267cSGleb Smirnoff static void
47975ee267cSGleb Smirnoff vlan_dumphash(struct ifvlantrunk *trunk)
48075ee267cSGleb Smirnoff {
48175ee267cSGleb Smirnoff 	int i;
48275ee267cSGleb Smirnoff 	struct ifvlan *ifv;
48375ee267cSGleb Smirnoff 
48475ee267cSGleb Smirnoff 	for (i = 0; i < (1 << trunk->hwidth); i++) {
48575ee267cSGleb Smirnoff 		printf("%d: ", i);
486b08d611dSMatt Macy 		CK_SLIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
48775ee267cSGleb Smirnoff 			printf("%s ", ifv->ifv_ifp->if_xname);
48875ee267cSGleb Smirnoff 		printf("\n");
48975ee267cSGleb Smirnoff 	}
49075ee267cSGleb Smirnoff }
49175ee267cSGleb Smirnoff #endif /* 0 */
492e4cd31ddSJeff Roberson #else
493e4cd31ddSJeff Roberson 
494e4cd31ddSJeff Roberson static __inline struct ifvlan *
4957983103aSRobert Watson vlan_gethash(struct ifvlantrunk *trunk, uint16_t vid)
496e4cd31ddSJeff Roberson {
497e4cd31ddSJeff Roberson 
4987983103aSRobert Watson 	return trunk->vlans[vid];
499e4cd31ddSJeff Roberson }
500e4cd31ddSJeff Roberson 
501e4cd31ddSJeff Roberson static __inline int
502e4cd31ddSJeff Roberson vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
503e4cd31ddSJeff Roberson {
504e4cd31ddSJeff Roberson 
5057983103aSRobert Watson 	if (trunk->vlans[ifv->ifv_vid] != NULL)
506e4cd31ddSJeff Roberson 		return EEXIST;
5077983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = ifv;
508e4cd31ddSJeff Roberson 	trunk->refcnt++;
509e4cd31ddSJeff Roberson 
510e4cd31ddSJeff Roberson 	return (0);
511e4cd31ddSJeff Roberson }
512e4cd31ddSJeff Roberson 
513e4cd31ddSJeff Roberson static __inline int
514e4cd31ddSJeff Roberson vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
515e4cd31ddSJeff Roberson {
516e4cd31ddSJeff Roberson 
5177983103aSRobert Watson 	trunk->vlans[ifv->ifv_vid] = NULL;
518e4cd31ddSJeff Roberson 	trunk->refcnt--;
519e4cd31ddSJeff Roberson 
520e4cd31ddSJeff Roberson 	return (0);
521e4cd31ddSJeff Roberson }
522e4cd31ddSJeff Roberson 
523e4cd31ddSJeff Roberson static __inline void
524e4cd31ddSJeff Roberson vlan_freehash(struct ifvlantrunk *trunk)
525e4cd31ddSJeff Roberson {
526e4cd31ddSJeff Roberson }
527e4cd31ddSJeff Roberson 
528e4cd31ddSJeff Roberson static __inline void
529e4cd31ddSJeff Roberson vlan_inithash(struct ifvlantrunk *trunk)
530e4cd31ddSJeff Roberson {
531e4cd31ddSJeff Roberson }
532e4cd31ddSJeff Roberson 
53375ee267cSGleb Smirnoff #endif /* !VLAN_ARRAY */
53475ee267cSGleb Smirnoff 
53575ee267cSGleb Smirnoff static void
53675ee267cSGleb Smirnoff trunk_destroy(struct ifvlantrunk *trunk)
53775ee267cSGleb Smirnoff {
538d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
53975ee267cSGleb Smirnoff 
54075ee267cSGleb Smirnoff 	vlan_freehash(trunk);
54175ee267cSGleb Smirnoff 	trunk->parent->if_vlantrunk = NULL;
54233499e2aSYaroslav Tykhiy 	TRUNK_LOCK_DESTROY(trunk);
5439bcf3ae4SAlexander Motin 	if_rele(trunk->parent);
54475ee267cSGleb Smirnoff 	free(trunk, M_VLAN);
54575ee267cSGleb Smirnoff }
54675ee267cSGleb Smirnoff 
547f731f104SBill Paul /*
548f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
549f731f104SBill Paul  * programming the multicast filter of the parent. This has the
550f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
551f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
552f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
553f731f104SBill Paul  * to avoid this: there really is only one physical interface.
554f731f104SBill Paul  */
5552b120974SPeter Wemm static int
5562b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
557f731f104SBill Paul {
558f731f104SBill Paul 	struct ifnet		*ifp_p;
5592d222cb7SAlexander Motin 	struct ifmultiaddr	*ifma;
560f731f104SBill Paul 	struct ifvlan		*sc;
561c0cb022bSYaroslav Tykhiy 	struct vlan_mc_entry	*mc;
562f731f104SBill Paul 	int			error;
563f731f104SBill Paul 
564b08d611dSMatt Macy 	VLAN_XLOCK_ASSERT();
565d148c2a2SMatt Joras 
566f731f104SBill Paul 	/* Find the parent. */
567f731f104SBill Paul 	sc = ifp->if_softc;
56875ee267cSGleb Smirnoff 	ifp_p = PARENT(sc);
5691b2a4f7aSBill Fenner 
5708b615593SMarko Zec 	CURVNET_SET_QUIET(ifp_p->if_vnet);
5718b615593SMarko Zec 
572f731f104SBill Paul 	/* First, remove any existing filter entries. */
573b08d611dSMatt Macy 	while ((mc = CK_SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
574b08d611dSMatt Macy 		CK_SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
5752d222cb7SAlexander Motin 		(void)if_delmulti(ifp_p, (struct sockaddr *)&mc->mc_addr);
576c32a9d66SHans Petter Selasky 		epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
577f731f104SBill Paul 	}
578f731f104SBill Paul 
579f731f104SBill Paul 	/* Now program new ones. */
5802d222cb7SAlexander Motin 	IF_ADDR_WLOCK(ifp);
581d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
582f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
583f731f104SBill Paul 			continue;
58429c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
5852d222cb7SAlexander Motin 		if (mc == NULL) {
5862d222cb7SAlexander Motin 			IF_ADDR_WUNLOCK(ifp);
58729c2dfbeSBruce M Simpson 			return (ENOMEM);
5882d222cb7SAlexander Motin 		}
589e4cd31ddSJeff Roberson 		bcopy(ifma->ifma_addr, &mc->mc_addr, ifma->ifma_addr->sa_len);
590e4cd31ddSJeff Roberson 		mc->mc_addr.sdl_index = ifp_p->if_index;
591b08d611dSMatt Macy 		CK_SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
5922d222cb7SAlexander Motin 	}
5932d222cb7SAlexander Motin 	IF_ADDR_WUNLOCK(ifp);
594b08d611dSMatt Macy 	CK_SLIST_FOREACH (mc, &sc->vlan_mc_listhead, mc_entries) {
595e4cd31ddSJeff Roberson 		error = if_addmulti(ifp_p, (struct sockaddr *)&mc->mc_addr,
5962d222cb7SAlexander Motin 		    NULL);
597f731f104SBill Paul 		if (error)
598f731f104SBill Paul 			return (error);
599f731f104SBill Paul 	}
600f731f104SBill Paul 
6018b615593SMarko Zec 	CURVNET_RESTORE();
602f731f104SBill Paul 	return (0);
603f731f104SBill Paul }
6042cc2df49SGarrett Wollman 
605a3814acfSSam Leffler /*
606ea4ca115SAndrew Thompson  * A handler for parent interface link layer address changes.
607ea4ca115SAndrew Thompson  * If the parent interface link layer address is changed we
608ea4ca115SAndrew Thompson  * should also change it on all children vlans.
609ea4ca115SAndrew Thompson  */
610ea4ca115SAndrew Thompson static void
611ea4ca115SAndrew Thompson vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
612ea4ca115SAndrew Thompson {
613*a68cc388SGleb Smirnoff 	struct epoch_tracker et;
614ea4ca115SAndrew Thompson 	struct ifvlan *ifv;
615d148c2a2SMatt Joras 	struct ifnet *ifv_ifp;
616d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
617d148c2a2SMatt Joras 	struct sockaddr_dl *sdl;
618ea4ca115SAndrew Thompson 
619d148c2a2SMatt Joras 	/* Need the rmlock since this is run on taskqueue_swi. */
620*a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
621d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
622d148c2a2SMatt Joras 	if (trunk == NULL) {
623*a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
624ea4ca115SAndrew Thompson 		return;
625d148c2a2SMatt Joras 	}
626ea4ca115SAndrew Thompson 
627ea4ca115SAndrew Thompson 	/*
628ea4ca115SAndrew Thompson 	 * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
629d148c2a2SMatt Joras 	 * We need an exclusive lock here to prevent concurrent SIOCSIFLLADDR
630d148c2a2SMatt Joras 	 * ioctl calls on the parent garbling the lladdr of the child vlan.
631ea4ca115SAndrew Thompson 	 */
632d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
633d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
634d148c2a2SMatt Joras 		/*
635d148c2a2SMatt Joras 		 * Copy new new lladdr into the ifv_ifp, enqueue a task
636d148c2a2SMatt Joras 		 * to actually call if_setlladdr. if_setlladdr needs to
637d148c2a2SMatt Joras 		 * be deferred to a taskqueue because it will call into
638d148c2a2SMatt Joras 		 * the if_vlan ioctl path and try to acquire the global
639d148c2a2SMatt Joras 		 * lock.
640d148c2a2SMatt Joras 		 */
641d148c2a2SMatt Joras 		ifv_ifp = ifv->ifv_ifp;
642d148c2a2SMatt Joras 		bcopy(IF_LLADDR(ifp), IF_LLADDR(ifv_ifp),
643e4cd31ddSJeff Roberson 		    ifp->if_addrlen);
644d148c2a2SMatt Joras 		sdl = (struct sockaddr_dl *)ifv_ifp->if_addr->ifa_addr;
645d148c2a2SMatt Joras 		sdl->sdl_alen = ifp->if_addrlen;
646d148c2a2SMatt Joras 		taskqueue_enqueue(taskqueue_thread, &ifv->lladdr_task);
6476117727bSAndrew Thompson 	}
648d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
649*a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
650ea4ca115SAndrew Thompson }
651ea4ca115SAndrew Thompson 
652ea4ca115SAndrew Thompson /*
6535cb8c31aSYaroslav Tykhiy  * A handler for network interface departure events.
6545cb8c31aSYaroslav Tykhiy  * Track departure of trunks here so that we don't access invalid
6555cb8c31aSYaroslav Tykhiy  * pointers or whatever if a trunk is ripped from under us, e.g.,
6565428776eSJohn Baldwin  * by ejecting its hot-plug card.  However, if an ifnet is simply
6575428776eSJohn Baldwin  * being renamed, then there's no need to tear down the state.
6585cb8c31aSYaroslav Tykhiy  */
6595cb8c31aSYaroslav Tykhiy static void
6605cb8c31aSYaroslav Tykhiy vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
6615cb8c31aSYaroslav Tykhiy {
6625cb8c31aSYaroslav Tykhiy 	struct ifvlan *ifv;
663d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
6645cb8c31aSYaroslav Tykhiy 
6655428776eSJohn Baldwin 	/* If the ifnet is just being renamed, don't do anything. */
6665428776eSJohn Baldwin 	if (ifp->if_flags & IFF_RENAMING)
6675428776eSJohn Baldwin 		return;
668d148c2a2SMatt Joras 	VLAN_XLOCK();
669d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
670d148c2a2SMatt Joras 	if (trunk == NULL) {
671d148c2a2SMatt Joras 		VLAN_XUNLOCK();
672d148c2a2SMatt Joras 		return;
673d148c2a2SMatt Joras 	}
6745428776eSJohn Baldwin 
6755cb8c31aSYaroslav Tykhiy 	/*
6765cb8c31aSYaroslav Tykhiy 	 * OK, it's a trunk.  Loop over and detach all vlan's on it.
6775cb8c31aSYaroslav Tykhiy 	 * Check trunk pointer after each vlan_unconfig() as it will
6785cb8c31aSYaroslav Tykhiy 	 * free it and set to NULL after the last vlan was detached.
6795cb8c31aSYaroslav Tykhiy 	 */
680d148c2a2SMatt Joras 	VLAN_FOREACH_UNTIL_SAFE(ifv, ifp->if_vlantrunk,
681d148c2a2SMatt Joras 	    ifp->if_vlantrunk == NULL)
68228cc4d37SJohn Baldwin 		vlan_unconfig_locked(ifv->ifv_ifp, 1);
683d148c2a2SMatt Joras 
6845cb8c31aSYaroslav Tykhiy 	/* Trunk should have been destroyed in vlan_unconfig(). */
6855cb8c31aSYaroslav Tykhiy 	KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
686d148c2a2SMatt Joras 	VLAN_XUNLOCK();
6875cb8c31aSYaroslav Tykhiy }
6885cb8c31aSYaroslav Tykhiy 
6895cb8c31aSYaroslav Tykhiy /*
690e4cd31ddSJeff Roberson  * Return the trunk device for a virtual interface.
691e4cd31ddSJeff Roberson  */
692e4cd31ddSJeff Roberson static struct ifnet  *
693e4cd31ddSJeff Roberson vlan_trunkdev(struct ifnet *ifp)
694e4cd31ddSJeff Roberson {
695*a68cc388SGleb Smirnoff 	struct epoch_tracker et;
696e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
697e4cd31ddSJeff Roberson 
698e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
699e4cd31ddSJeff Roberson 		return (NULL);
700d148c2a2SMatt Joras 
701*a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
702e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
703e4cd31ddSJeff Roberson 	ifp = NULL;
704e4cd31ddSJeff Roberson 	if (ifv->ifv_trunk)
705e4cd31ddSJeff Roberson 		ifp = PARENT(ifv);
706*a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
707e4cd31ddSJeff Roberson 	return (ifp);
708e4cd31ddSJeff Roberson }
709e4cd31ddSJeff Roberson 
710e4cd31ddSJeff Roberson /*
7117983103aSRobert Watson  * Return the 12-bit VLAN VID for this interface, for use by external
7127983103aSRobert Watson  * components such as Infiniband.
7137983103aSRobert Watson  *
7147983103aSRobert Watson  * XXXRW: Note that the function name here is historical; it should be named
7157983103aSRobert Watson  * vlan_vid().
716e4cd31ddSJeff Roberson  */
717e4cd31ddSJeff Roberson static int
7187983103aSRobert Watson vlan_tag(struct ifnet *ifp, uint16_t *vidp)
719e4cd31ddSJeff Roberson {
720e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
721e4cd31ddSJeff Roberson 
722e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
723e4cd31ddSJeff Roberson 		return (EINVAL);
724e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
7257983103aSRobert Watson 	*vidp = ifv->ifv_vid;
726e4cd31ddSJeff Roberson 	return (0);
727e4cd31ddSJeff Roberson }
728e4cd31ddSJeff Roberson 
72932d2623aSNavdeep Parhar static int
73032d2623aSNavdeep Parhar vlan_pcp(struct ifnet *ifp, uint16_t *pcpp)
73132d2623aSNavdeep Parhar {
73232d2623aSNavdeep Parhar 	struct ifvlan *ifv;
73332d2623aSNavdeep Parhar 
73432d2623aSNavdeep Parhar 	if (ifp->if_type != IFT_L2VLAN)
73532d2623aSNavdeep Parhar 		return (EINVAL);
73632d2623aSNavdeep Parhar 	ifv = ifp->if_softc;
73732d2623aSNavdeep Parhar 	*pcpp = ifv->ifv_pcp;
73832d2623aSNavdeep Parhar 	return (0);
73932d2623aSNavdeep Parhar }
74032d2623aSNavdeep Parhar 
741e4cd31ddSJeff Roberson /*
742e4cd31ddSJeff Roberson  * Return a driver specific cookie for this interface.  Synchronization
743e4cd31ddSJeff Roberson  * with setcookie must be provided by the driver.
744e4cd31ddSJeff Roberson  */
745e4cd31ddSJeff Roberson static void *
746e4cd31ddSJeff Roberson vlan_cookie(struct ifnet *ifp)
747e4cd31ddSJeff Roberson {
748e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
749e4cd31ddSJeff Roberson 
750e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
751e4cd31ddSJeff Roberson 		return (NULL);
752e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
753e4cd31ddSJeff Roberson 	return (ifv->ifv_cookie);
754e4cd31ddSJeff Roberson }
755e4cd31ddSJeff Roberson 
756e4cd31ddSJeff Roberson /*
757e4cd31ddSJeff Roberson  * Store a cookie in our softc that drivers can use to store driver
758e4cd31ddSJeff Roberson  * private per-instance data in.
759e4cd31ddSJeff Roberson  */
760e4cd31ddSJeff Roberson static int
761e4cd31ddSJeff Roberson vlan_setcookie(struct ifnet *ifp, void *cookie)
762e4cd31ddSJeff Roberson {
763e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
764e4cd31ddSJeff Roberson 
765e4cd31ddSJeff Roberson 	if (ifp->if_type != IFT_L2VLAN)
766e4cd31ddSJeff Roberson 		return (EINVAL);
767e4cd31ddSJeff Roberson 	ifv = ifp->if_softc;
768e4cd31ddSJeff Roberson 	ifv->ifv_cookie = cookie;
769e4cd31ddSJeff Roberson 	return (0);
770e4cd31ddSJeff Roberson }
771e4cd31ddSJeff Roberson 
772e4cd31ddSJeff Roberson /*
7737983103aSRobert Watson  * Return the vlan device present at the specific VID.
774e4cd31ddSJeff Roberson  */
775e4cd31ddSJeff Roberson static struct ifnet *
7767983103aSRobert Watson vlan_devat(struct ifnet *ifp, uint16_t vid)
777e4cd31ddSJeff Roberson {
778*a68cc388SGleb Smirnoff 	struct epoch_tracker et;
779e4cd31ddSJeff Roberson 	struct ifvlantrunk *trunk;
780e4cd31ddSJeff Roberson 	struct ifvlan *ifv;
781e4cd31ddSJeff Roberson 
782*a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
783e4cd31ddSJeff Roberson 	trunk = ifp->if_vlantrunk;
784d148c2a2SMatt Joras 	if (trunk == NULL) {
785*a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
786e4cd31ddSJeff Roberson 		return (NULL);
787d148c2a2SMatt Joras 	}
788e4cd31ddSJeff Roberson 	ifp = NULL;
7897983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
790e4cd31ddSJeff Roberson 	if (ifv)
791e4cd31ddSJeff Roberson 		ifp = ifv->ifv_ifp;
792*a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
793e4cd31ddSJeff Roberson 	return (ifp);
794e4cd31ddSJeff Roberson }
795e4cd31ddSJeff Roberson 
796e4cd31ddSJeff Roberson /*
7972ccbbd06SMarcelo Araujo  * Recalculate the cached VLAN tag exposed via the MIB.
7982ccbbd06SMarcelo Araujo  */
7992ccbbd06SMarcelo Araujo static void
8002ccbbd06SMarcelo Araujo vlan_tag_recalculate(struct ifvlan *ifv)
8012ccbbd06SMarcelo Araujo {
8022ccbbd06SMarcelo Araujo 
8032ccbbd06SMarcelo Araujo        ifv->ifv_tag = EVL_MAKETAG(ifv->ifv_vid, ifv->ifv_pcp, 0);
8042ccbbd06SMarcelo Araujo }
8052ccbbd06SMarcelo Araujo 
8062ccbbd06SMarcelo Araujo /*
807a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
808a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
809a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
810a3814acfSSam Leffler  * set here.  No one else in the system should be aware of this so
811a3814acfSSam Leffler  * we use an explicit reference here.
812a3814acfSSam Leffler  */
813a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
814a3814acfSSam Leffler 
815984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
816a6fffd6cSBrooks Davis extern	void (*vlan_link_state_p)(struct ifnet *);
817127d7b2dSAndre Oppermann 
8182b120974SPeter Wemm static int
8192b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
8202b120974SPeter Wemm {
8219d4fe4b2SBrooks Davis 
8222b120974SPeter Wemm 	switch (type) {
8232b120974SPeter Wemm 	case MOD_LOAD:
8245cb8c31aSYaroslav Tykhiy 		ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
8255cb8c31aSYaroslav Tykhiy 		    vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
8265cb8c31aSYaroslav Tykhiy 		if (ifdetach_tag == NULL)
8275cb8c31aSYaroslav Tykhiy 			return (ENOMEM);
828ea4ca115SAndrew Thompson 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
829ea4ca115SAndrew Thompson 		    vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
830ea4ca115SAndrew Thompson 		if (iflladdr_tag == NULL)
831ea4ca115SAndrew Thompson 			return (ENOMEM);
832d148c2a2SMatt Joras 		VLAN_LOCKING_INIT();
8339d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
834127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
83575ee267cSGleb Smirnoff 		vlan_trunk_cap_p = vlan_trunk_capabilities;
836e4cd31ddSJeff Roberson 		vlan_trunkdev_p = vlan_trunkdev;
837e4cd31ddSJeff Roberson 		vlan_cookie_p = vlan_cookie;
838e4cd31ddSJeff Roberson 		vlan_setcookie_p = vlan_setcookie;
839e4cd31ddSJeff Roberson 		vlan_tag_p = vlan_tag;
84032d2623aSNavdeep Parhar 		vlan_pcp_p = vlan_pcp;
841e4cd31ddSJeff Roberson 		vlan_devat_p = vlan_devat;
842ccf7ba97SMarko Zec #ifndef VIMAGE
84342a58907SGleb Smirnoff 		vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
84442a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
845ccf7ba97SMarko Zec #endif
84625c0f7b3SYaroslav Tykhiy 		if (bootverbose)
84725c0f7b3SYaroslav Tykhiy 			printf("vlan: initialized, using "
84825c0f7b3SYaroslav Tykhiy #ifdef VLAN_ARRAY
84925c0f7b3SYaroslav Tykhiy 			       "full-size arrays"
85025c0f7b3SYaroslav Tykhiy #else
85125c0f7b3SYaroslav Tykhiy 			       "hash tables with chaining"
85225c0f7b3SYaroslav Tykhiy #endif
85325c0f7b3SYaroslav Tykhiy 
85425c0f7b3SYaroslav Tykhiy 			       "\n");
8552b120974SPeter Wemm 		break;
8562b120974SPeter Wemm 	case MOD_UNLOAD:
857ccf7ba97SMarko Zec #ifndef VIMAGE
85842a58907SGleb Smirnoff 		if_clone_detach(vlan_cloner);
859ccf7ba97SMarko Zec #endif
8605cb8c31aSYaroslav Tykhiy 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
861ea4ca115SAndrew Thompson 		EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
8629d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
863127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
86475ee267cSGleb Smirnoff 		vlan_trunk_cap_p = NULL;
865e4cd31ddSJeff Roberson 		vlan_trunkdev_p = NULL;
866e4cd31ddSJeff Roberson 		vlan_tag_p = NULL;
86709fe6320SNavdeep Parhar 		vlan_cookie_p = NULL;
86809fe6320SNavdeep Parhar 		vlan_setcookie_p = NULL;
869e4cd31ddSJeff Roberson 		vlan_devat_p = NULL;
870d148c2a2SMatt Joras 		VLAN_LOCKING_DESTROY();
87125c0f7b3SYaroslav Tykhiy 		if (bootverbose)
87225c0f7b3SYaroslav Tykhiy 			printf("vlan: unloaded\n");
8739d4fe4b2SBrooks Davis 		break;
8743e019deaSPoul-Henning Kamp 	default:
8753e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
8762b120974SPeter Wemm 	}
87715a66c21SBruce M Simpson 	return (0);
8782b120974SPeter Wemm }
8792b120974SPeter Wemm 
8802b120974SPeter Wemm static moduledata_t vlan_mod = {
8812b120974SPeter Wemm 	"if_vlan",
8822b120974SPeter Wemm 	vlan_modevent,
8839823d527SKevin Lo 	0
8842b120974SPeter Wemm };
8852b120974SPeter Wemm 
8862b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
88711edc477SEd Maste MODULE_VERSION(if_vlan, 3);
8882cc2df49SGarrett Wollman 
889ccf7ba97SMarko Zec #ifdef VIMAGE
890ccf7ba97SMarko Zec static void
891ccf7ba97SMarko Zec vnet_vlan_init(const void *unused __unused)
892ccf7ba97SMarko Zec {
893ccf7ba97SMarko Zec 
89442a58907SGleb Smirnoff 	vlan_cloner = if_clone_advanced(vlanname, 0, vlan_clone_match,
89542a58907SGleb Smirnoff 		    vlan_clone_create, vlan_clone_destroy);
896ccf7ba97SMarko Zec 	V_vlan_cloner = vlan_cloner;
897ccf7ba97SMarko Zec }
898ccf7ba97SMarko Zec VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
899ccf7ba97SMarko Zec     vnet_vlan_init, NULL);
900ccf7ba97SMarko Zec 
901ccf7ba97SMarko Zec static void
902ccf7ba97SMarko Zec vnet_vlan_uninit(const void *unused __unused)
903ccf7ba97SMarko Zec {
904ccf7ba97SMarko Zec 
90542a58907SGleb Smirnoff 	if_clone_detach(V_vlan_cloner);
906ccf7ba97SMarko Zec }
90789856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
908ccf7ba97SMarko Zec     vnet_vlan_uninit, NULL);
909ccf7ba97SMarko Zec #endif
910ccf7ba97SMarko Zec 
911f941c31aSGleb Smirnoff /*
912f941c31aSGleb Smirnoff  * Check for <etherif>.<vlan> style interface names.
913f941c31aSGleb Smirnoff  */
914f889d2efSBrooks Davis static struct ifnet *
915f941c31aSGleb Smirnoff vlan_clone_match_ethervid(const char *name, int *vidp)
9169d4fe4b2SBrooks Davis {
917f941c31aSGleb Smirnoff 	char ifname[IFNAMSIZ];
918f941c31aSGleb Smirnoff 	char *cp;
919f889d2efSBrooks Davis 	struct ifnet *ifp;
9207983103aSRobert Watson 	int vid;
921f889d2efSBrooks Davis 
922f941c31aSGleb Smirnoff 	strlcpy(ifname, name, IFNAMSIZ);
923f941c31aSGleb Smirnoff 	if ((cp = strchr(ifname, '.')) == NULL)
924f941c31aSGleb Smirnoff 		return (NULL);
925f941c31aSGleb Smirnoff 	*cp = '\0';
9269bcf3ae4SAlexander Motin 	if ((ifp = ifunit_ref(ifname)) == NULL)
927f941c31aSGleb Smirnoff 		return (NULL);
928f941c31aSGleb Smirnoff 	/* Parse VID. */
9299bcf3ae4SAlexander Motin 	if (*++cp == '\0') {
9309bcf3ae4SAlexander Motin 		if_rele(ifp);
931f941c31aSGleb Smirnoff 		return (NULL);
9329bcf3ae4SAlexander Motin 	}
9337983103aSRobert Watson 	vid = 0;
934fb92ad4aSJohn Baldwin 	for(; *cp >= '0' && *cp <= '9'; cp++)
9357983103aSRobert Watson 		vid = (vid * 10) + (*cp - '0');
9369bcf3ae4SAlexander Motin 	if (*cp != '\0') {
9379bcf3ae4SAlexander Motin 		if_rele(ifp);
938f941c31aSGleb Smirnoff 		return (NULL);
9399bcf3ae4SAlexander Motin 	}
9407983103aSRobert Watson 	if (vidp != NULL)
9417983103aSRobert Watson 		*vidp = vid;
942f889d2efSBrooks Davis 
94315a66c21SBruce M Simpson 	return (ifp);
944f889d2efSBrooks Davis }
945f889d2efSBrooks Davis 
946f889d2efSBrooks Davis static int
947f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
948f889d2efSBrooks Davis {
949f889d2efSBrooks Davis 	const char *cp;
950f889d2efSBrooks Davis 
951f941c31aSGleb Smirnoff 	if (vlan_clone_match_ethervid(name, NULL) != NULL)
952f889d2efSBrooks Davis 		return (1);
953f889d2efSBrooks Davis 
95442a58907SGleb Smirnoff 	if (strncmp(vlanname, name, strlen(vlanname)) != 0)
955f889d2efSBrooks Davis 		return (0);
956f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
957f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
958f889d2efSBrooks Davis 			return (0);
959f889d2efSBrooks Davis 	}
960f889d2efSBrooks Davis 
961f889d2efSBrooks Davis 	return (1);
962f889d2efSBrooks Davis }
963f889d2efSBrooks Davis 
964f889d2efSBrooks Davis static int
9656b7330e2SSam Leffler vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
966f889d2efSBrooks Davis {
967f889d2efSBrooks Davis 	char *dp;
968f889d2efSBrooks Davis 	int wildcard;
969f889d2efSBrooks Davis 	int unit;
970f889d2efSBrooks Davis 	int error;
9717983103aSRobert Watson 	int vid;
9729d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
9739d4fe4b2SBrooks Davis 	struct ifnet *ifp;
974f889d2efSBrooks Davis 	struct ifnet *p;
9753ba24fdeSJohn Baldwin 	struct ifaddr *ifa;
9763ba24fdeSJohn Baldwin 	struct sockaddr_dl *sdl;
9776b7330e2SSam Leffler 	struct vlanreq vlr;
97865239942SYaroslav Tykhiy 	static const u_char eaddr[ETHER_ADDR_LEN];	/* 00:00:00:00:00:00 */
979f889d2efSBrooks Davis 
9806b7330e2SSam Leffler 	/*
9816b7330e2SSam Leffler 	 * There are 3 (ugh) ways to specify the cloned device:
9826b7330e2SSam Leffler 	 * o pass a parameter block with the clone request.
9836b7330e2SSam Leffler 	 * o specify parameters in the text of the clone device name
9846b7330e2SSam Leffler 	 * o specify no parameters and get an unattached device that
9856b7330e2SSam Leffler 	 *   must be configured separately.
9866b7330e2SSam Leffler 	 * The first technique is preferred; the latter two are
987a4641f4eSPedro F. Giffuni 	 * supported for backwards compatibility.
9887983103aSRobert Watson 	 *
9897983103aSRobert Watson 	 * XXXRW: Note historic use of the word "tag" here.  New ioctls may be
9907983103aSRobert Watson 	 * called for.
9916b7330e2SSam Leffler 	 */
9926b7330e2SSam Leffler 	if (params) {
9936b7330e2SSam Leffler 		error = copyin(params, &vlr, sizeof(vlr));
9946b7330e2SSam Leffler 		if (error)
9956b7330e2SSam Leffler 			return error;
9969bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
9976b7330e2SSam Leffler 		if (p == NULL)
998b1828acfSGleb Smirnoff 			return (ENXIO);
9996b7330e2SSam Leffler 		error = ifc_name2unit(name, &unit);
10009bcf3ae4SAlexander Motin 		if (error != 0) {
10019bcf3ae4SAlexander Motin 			if_rele(p);
10026b7330e2SSam Leffler 			return (error);
10039bcf3ae4SAlexander Motin 		}
10047983103aSRobert Watson 		vid = vlr.vlr_tag;
10056b7330e2SSam Leffler 		wildcard = (unit < 0);
1006f941c31aSGleb Smirnoff 	} else if ((p = vlan_clone_match_ethervid(name, &vid)) != NULL) {
1007f889d2efSBrooks Davis 		unit = -1;
1008f889d2efSBrooks Davis 		wildcard = 0;
1009f889d2efSBrooks Davis 	} else {
10109bcf3ae4SAlexander Motin 		p = NULL;
1011f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
1012f889d2efSBrooks Davis 		if (error != 0)
1013f889d2efSBrooks Davis 			return (error);
1014f889d2efSBrooks Davis 
1015f889d2efSBrooks Davis 		wildcard = (unit < 0);
1016f889d2efSBrooks Davis 	}
1017f889d2efSBrooks Davis 
1018f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
10199bcf3ae4SAlexander Motin 	if (error != 0) {
10209bcf3ae4SAlexander Motin 		if (p != NULL)
10219bcf3ae4SAlexander Motin 			if_rele(p);
1022f889d2efSBrooks Davis 		return (error);
10239bcf3ae4SAlexander Motin 	}
1024f889d2efSBrooks Davis 
1025f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
1026f889d2efSBrooks Davis 	if (wildcard) {
1027f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
1028f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
1029f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
1030f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
1031f889d2efSBrooks Davis 		}
1032f889d2efSBrooks Davis 	}
10339d4fe4b2SBrooks Davis 
1034a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
1035fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
1036fc74a9f9SBrooks Davis 	if (ifp == NULL) {
1037fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
1038fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
10399bcf3ae4SAlexander Motin 		if (p != NULL)
10409bcf3ae4SAlexander Motin 			if_rele(p);
1041fc74a9f9SBrooks Davis 		return (ENOSPC);
1042fc74a9f9SBrooks Davis 	}
1043b08d611dSMatt Macy 	CK_SLIST_INIT(&ifv->vlan_mc_listhead);
10449d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
1045f889d2efSBrooks Davis 	/*
1046cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
1047f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
1048f889d2efSBrooks Davis 	 */
1049f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
105042a58907SGleb Smirnoff 	ifp->if_dname = vlanname;
1051f889d2efSBrooks Davis 	ifp->if_dunit = unit;
10529d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
10539d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
105415a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
10559d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
10569d4fe4b2SBrooks Davis 
1057114c608cSYaroslav Tykhiy 	ifp->if_init = vlan_init;
1058d9b1d615SJohn Baldwin 	ifp->if_transmit = vlan_transmit;
1059d9b1d615SJohn Baldwin 	ifp->if_qflush = vlan_qflush;
10609d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
1061f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1062f3e7afe2SHans Petter Selasky 	ifp->if_snd_tag_alloc = vlan_snd_tag_alloc;
1063f3e7afe2SHans Petter Selasky #endif
106464a17d2eSYaroslav Tykhiy 	ifp->if_flags = VLAN_IFFLAGS;
1065fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
10669d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
1067211f625aSBill Fenner 	ifp->if_baudrate = 0;
1068a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
1069a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
10703ba24fdeSJohn Baldwin 	ifa = ifp->if_addr;
10713ba24fdeSJohn Baldwin 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
10723ba24fdeSJohn Baldwin 	sdl->sdl_type = IFT_L2VLAN;
10739d4fe4b2SBrooks Davis 
10749bcf3ae4SAlexander Motin 	if (p != NULL) {
10757983103aSRobert Watson 		error = vlan_config(ifv, p, vid);
10769bcf3ae4SAlexander Motin 		if_rele(p);
1077f889d2efSBrooks Davis 		if (error != 0) {
1078f889d2efSBrooks Davis 			/*
107928cc4d37SJohn Baldwin 			 * Since we've partially failed, we need to back
1080f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
1081f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
1082f889d2efSBrooks Davis 			 */
1083f889d2efSBrooks Davis 			ether_ifdetach(ifp);
1084249f4297SYaroslav Tykhiy 			vlan_unconfig(ifp);
10854b22573aSBrooks Davis 			if_free(ifp);
108696c8ef3aSMaxim Konovalov 			ifc_free_unit(ifc, unit);
1087f889d2efSBrooks Davis 			free(ifv, M_VLAN);
1088f889d2efSBrooks Davis 
1089f889d2efSBrooks Davis 			return (error);
1090f889d2efSBrooks Davis 		}
1091f889d2efSBrooks Davis 	}
1092f889d2efSBrooks Davis 
10939d4fe4b2SBrooks Davis 	return (0);
10949d4fe4b2SBrooks Davis }
10959d4fe4b2SBrooks Davis 
1096f889d2efSBrooks Davis static int
1097f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
10989d4fe4b2SBrooks Davis {
10999d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
1100114c608cSYaroslav Tykhiy 	int unit = ifp->if_dunit;
1101b4e9f837SBrooks Davis 
1102249f4297SYaroslav Tykhiy 	ether_ifdetach(ifp);	/* first, remove it from system-wide lists */
1103249f4297SYaroslav Tykhiy 	vlan_unconfig(ifp);	/* now it can be unconfigured and freed */
1104d148c2a2SMatt Joras 	/*
1105d148c2a2SMatt Joras 	 * We should have the only reference to the ifv now, so we can now
1106d148c2a2SMatt Joras 	 * drain any remaining lladdr task before freeing the ifnet and the
1107d148c2a2SMatt Joras 	 * ifvlan.
1108d148c2a2SMatt Joras 	 */
1109d148c2a2SMatt Joras 	taskqueue_drain(taskqueue_thread, &ifv->lladdr_task);
1110b08d611dSMatt Macy 	NET_EPOCH_WAIT();
11114b22573aSBrooks Davis 	if_free(ifp);
11129d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
1113b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
1114b4e9f837SBrooks Davis 
1115f889d2efSBrooks Davis 	return (0);
11169d4fe4b2SBrooks Davis }
11179d4fe4b2SBrooks Davis 
111815a66c21SBruce M Simpson /*
111915a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
112015a66c21SBruce M Simpson  */
11212cc2df49SGarrett Wollman static void
1122114c608cSYaroslav Tykhiy vlan_init(void *foo __unused)
11232cc2df49SGarrett Wollman {
11242cc2df49SGarrett Wollman }
11252cc2df49SGarrett Wollman 
11266d3a3ab7SGleb Smirnoff /*
1127d9b1d615SJohn Baldwin  * The if_transmit method for vlan(4) interface.
11286d3a3ab7SGleb Smirnoff  */
1129d9b1d615SJohn Baldwin static int
1130d9b1d615SJohn Baldwin vlan_transmit(struct ifnet *ifp, struct mbuf *m)
11312cc2df49SGarrett Wollman {
1132*a68cc388SGleb Smirnoff 	struct epoch_tracker et;
11332cc2df49SGarrett Wollman 	struct ifvlan *ifv;
11342cc2df49SGarrett Wollman 	struct ifnet *p;
11351ad7a257SPyun YongHyeon 	int error, len, mcast;
11362cc2df49SGarrett Wollman 
1137*a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
11382cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
1139d148c2a2SMatt Joras 	if (TRUNK(ifv) == NULL) {
1140d148c2a2SMatt Joras 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1141*a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1142d148c2a2SMatt Joras 		m_freem(m);
1143d148c2a2SMatt Joras 		return (ENETDOWN);
1144d148c2a2SMatt Joras 	}
114575ee267cSGleb Smirnoff 	p = PARENT(ifv);
11461ad7a257SPyun YongHyeon 	len = m->m_pkthdr.len;
11471ad7a257SPyun YongHyeon 	mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
11482cc2df49SGarrett Wollman 
1149a3814acfSSam Leffler 	BPF_MTAP(ifp, m);
11502cc2df49SGarrett Wollman 
1151f731f104SBill Paul 	/*
1152d9b1d615SJohn Baldwin 	 * Do not run parent's if_transmit() if the parent is not up,
115324993214SYaroslav Tykhiy 	 * or parent's driver will cause a system crash.
115424993214SYaroslav Tykhiy 	 */
11552dc879b3SYaroslav Tykhiy 	if (!UP_AND_RUNNING(p)) {
1156a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1157*a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1158d148c2a2SMatt Joras 		m_freem(m);
11598b20f6cfSHiroki Sato 		return (ENETDOWN);
116024993214SYaroslav Tykhiy 	}
116124993214SYaroslav Tykhiy 
1162f1379734SKonstantin Belousov 	if (!ether_8021q_frame(&m, ifp, p, ifv->ifv_vid, ifv->ifv_pcp)) {
1163a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1164*a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1165d9b1d615SJohn Baldwin 		return (0);
11664af90a4dSMatthew N. Dodd 	}
11672cc2df49SGarrett Wollman 
11682cc2df49SGarrett Wollman 	/*
11692cc2df49SGarrett Wollman 	 * Send it, precisely as ether_output() would have.
11702cc2df49SGarrett Wollman 	 */
1171aea78d20SKip Macy 	error = (p->if_transmit)(p, m);
1172299153b5SAlexander V. Chernikov 	if (error == 0) {
1173a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1174a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
1175a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast);
11761ad7a257SPyun YongHyeon 	} else
1177a58ea6b1SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1178*a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1179d9b1d615SJohn Baldwin 	return (error);
11802cc2df49SGarrett Wollman }
1181d9b1d615SJohn Baldwin 
1182d9b1d615SJohn Baldwin /*
1183d9b1d615SJohn Baldwin  * The ifp->if_qflush entry point for vlan(4) is a no-op.
1184d9b1d615SJohn Baldwin  */
1185d9b1d615SJohn Baldwin static void
1186d9b1d615SJohn Baldwin vlan_qflush(struct ifnet *ifp __unused)
1187d9b1d615SJohn Baldwin {
1188f731f104SBill Paul }
1189f731f104SBill Paul 
1190a3814acfSSam Leffler static void
1191a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
1192f731f104SBill Paul {
1193*a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1194d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1195f731f104SBill Paul 	struct ifvlan *ifv;
11962ccbbd06SMarcelo Araujo 	struct m_tag *mtag;
11972ccbbd06SMarcelo Araujo 	uint16_t vid, tag;
119875ee267cSGleb Smirnoff 
1199*a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1200d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1201d148c2a2SMatt Joras 	if (trunk == NULL) {
1202*a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1203d148c2a2SMatt Joras 		m_freem(m);
1204d148c2a2SMatt Joras 		return;
1205d148c2a2SMatt Joras 	}
1206a3814acfSSam Leffler 
1207f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
1208a3814acfSSam Leffler 		/*
120914e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
1210a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
1211a3814acfSSam Leffler 		 */
12122ccbbd06SMarcelo Araujo 		tag = m->m_pkthdr.ether_vtag;
12136ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
1214a3814acfSSam Leffler 	} else {
121575ee267cSGleb Smirnoff 		struct ether_vlan_header *evl;
121675ee267cSGleb Smirnoff 
121714e98256SYaroslav Tykhiy 		/*
121814e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
121914e98256SYaroslav Tykhiy 		 */
1220a3814acfSSam Leffler 		switch (ifp->if_type) {
1221a3814acfSSam Leffler 		case IFT_ETHER:
1222a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
1223a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
1224a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
1225*a68cc388SGleb Smirnoff 				NET_EPOCH_EXIT(et);
1226a3814acfSSam Leffler 				return;
1227a3814acfSSam Leffler 			}
1228a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
12292ccbbd06SMarcelo Araujo 			tag = ntohs(evl->evl_tag);
1230db8b5973SYaroslav Tykhiy 
1231db8b5973SYaroslav Tykhiy 			/*
12322dc879b3SYaroslav Tykhiy 			 * Remove the 802.1q header by copying the Ethernet
12332dc879b3SYaroslav Tykhiy 			 * addresses over it and adjusting the beginning of
12342dc879b3SYaroslav Tykhiy 			 * the data in the mbuf.  The encapsulated Ethernet
12352dc879b3SYaroslav Tykhiy 			 * type field is already in place.
1236db8b5973SYaroslav Tykhiy 			 */
12372dc879b3SYaroslav Tykhiy 			bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
12382dc879b3SYaroslav Tykhiy 			      ETHER_HDR_LEN - ETHER_TYPE_LEN);
12392dc879b3SYaroslav Tykhiy 			m_adj(m, ETHER_VLAN_ENCAP_LEN);
1240a3814acfSSam Leffler 			break;
12412dc879b3SYaroslav Tykhiy 
1242a3814acfSSam Leffler 		default:
1243db8b5973SYaroslav Tykhiy #ifdef INVARIANTS
124460c60618SYaroslav Tykhiy 			panic("%s: %s has unsupported if_type %u",
124560c60618SYaroslav Tykhiy 			      __func__, ifp->if_xname, ifp->if_type);
1246a3814acfSSam Leffler #endif
12473751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1248*a68cc388SGleb Smirnoff 			NET_EPOCH_EXIT(et);
1249d148c2a2SMatt Joras 			m_freem(m);
125060c60618SYaroslav Tykhiy 			return;
1251a3814acfSSam Leffler 		}
12527a46ec8fSBrooks Davis 	}
12537a46ec8fSBrooks Davis 
12542ccbbd06SMarcelo Araujo 	vid = EVL_VLANOFTAG(tag);
12552ccbbd06SMarcelo Araujo 
12567983103aSRobert Watson 	ifv = vlan_gethash(trunk, vid);
12572dc879b3SYaroslav Tykhiy 	if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
1258*a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1259b08d611dSMatt Macy 		if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
1260d148c2a2SMatt Joras 		m_freem(m);
126175ee267cSGleb Smirnoff 		return;
126275ee267cSGleb Smirnoff 	}
1263f731f104SBill Paul 
12642ccbbd06SMarcelo Araujo 	if (vlan_mtag_pcp) {
12652ccbbd06SMarcelo Araujo 		/*
12662ccbbd06SMarcelo Araujo 		 * While uncommon, it is possible that we will find a 802.1q
12672ccbbd06SMarcelo Araujo 		 * packet encapsulated inside another packet that also had an
12682ccbbd06SMarcelo Araujo 		 * 802.1q header.  For example, ethernet tunneled over IPSEC
12692ccbbd06SMarcelo Araujo 		 * arriving over ethernet.  In that case, we replace the
12702ccbbd06SMarcelo Araujo 		 * existing 802.1q PCP m_tag value.
12712ccbbd06SMarcelo Araujo 		 */
12722ccbbd06SMarcelo Araujo 		mtag = m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_IN, NULL);
12732ccbbd06SMarcelo Araujo 		if (mtag == NULL) {
12742ccbbd06SMarcelo Araujo 			mtag = m_tag_alloc(MTAG_8021Q, MTAG_8021Q_PCP_IN,
12752ccbbd06SMarcelo Araujo 			    sizeof(uint8_t), M_NOWAIT);
12762ccbbd06SMarcelo Araujo 			if (mtag == NULL) {
12772ccbbd06SMarcelo Araujo 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1278*a68cc388SGleb Smirnoff 				NET_EPOCH_EXIT(et);
1279d148c2a2SMatt Joras 				m_freem(m);
12802ccbbd06SMarcelo Araujo 				return;
12812ccbbd06SMarcelo Araujo 			}
12822ccbbd06SMarcelo Araujo 			m_tag_prepend(m, mtag);
12832ccbbd06SMarcelo Araujo 		}
12842ccbbd06SMarcelo Araujo 		*(uint8_t *)(mtag + 1) = EVL_PRIOFTAG(tag);
12852ccbbd06SMarcelo Araujo 	}
12862ccbbd06SMarcelo Araujo 
1287fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
1288c0304424SGleb Smirnoff 	if_inc_counter(ifv->ifv_ifp, IFCOUNTER_IPACKETS, 1);
1289*a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
12902cc2df49SGarrett Wollman 
1291a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
1292fdbf1174SMatt Joras 	(*ifv->ifv_ifp->if_input)(ifv->ifv_ifp, m);
12932cc2df49SGarrett Wollman }
12942cc2df49SGarrett Wollman 
1295d148c2a2SMatt Joras static void
1296d148c2a2SMatt Joras vlan_lladdr_fn(void *arg, int pending __unused)
1297d148c2a2SMatt Joras {
1298d148c2a2SMatt Joras 	struct ifvlan *ifv;
1299d148c2a2SMatt Joras 	struct ifnet *ifp;
1300d148c2a2SMatt Joras 
1301d148c2a2SMatt Joras 	ifv = (struct ifvlan *)arg;
1302d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
13035191a3aeSKristof Provost 
13045191a3aeSKristof Provost 	CURVNET_SET(ifp->if_vnet);
13055191a3aeSKristof Provost 
1306d148c2a2SMatt Joras 	/* The ifv_ifp already has the lladdr copied in. */
1307d148c2a2SMatt Joras 	if_setlladdr(ifp, IF_LLADDR(ifp), ifp->if_addrlen);
13085191a3aeSKristof Provost 
13095191a3aeSKristof Provost 	CURVNET_RESTORE();
1310d148c2a2SMatt Joras }
1311d148c2a2SMatt Joras 
13122cc2df49SGarrett Wollman static int
13137983103aSRobert Watson vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t vid)
13142cc2df49SGarrett Wollman {
1315*a68cc388SGleb Smirnoff 	struct epoch_tracker et;
131675ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
13171cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
131875ee267cSGleb Smirnoff 	int error = 0;
13192cc2df49SGarrett Wollman 
1320b1828acfSGleb Smirnoff 	/*
1321b1828acfSGleb Smirnoff 	 * We can handle non-ethernet hardware types as long as
1322b1828acfSGleb Smirnoff 	 * they handle the tagging and headers themselves.
1323b1828acfSGleb Smirnoff 	 */
1324e4cd31ddSJeff Roberson 	if (p->if_type != IFT_ETHER &&
1325e4cd31ddSJeff Roberson 	    (p->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
132615a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
132764a17d2eSYaroslav Tykhiy 	if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
132864a17d2eSYaroslav Tykhiy 		return (EPROTONOSUPPORT);
1329b1828acfSGleb Smirnoff 	/*
1330b1828acfSGleb Smirnoff 	 * Don't let the caller set up a VLAN VID with
1331b1828acfSGleb Smirnoff 	 * anything except VLID bits.
1332b1828acfSGleb Smirnoff 	 * VID numbers 0x0 and 0xFFF are reserved.
1333b1828acfSGleb Smirnoff 	 */
1334b1828acfSGleb Smirnoff 	if (vid == 0 || vid == 0xFFF || (vid & ~EVL_VLID_MASK))
1335b1828acfSGleb Smirnoff 		return (EINVAL);
133675ee267cSGleb Smirnoff 	if (ifv->ifv_trunk)
133715a66c21SBruce M Simpson 		return (EBUSY);
13382cc2df49SGarrett Wollman 
1339d148c2a2SMatt Joras 	VLAN_XLOCK();
134075ee267cSGleb Smirnoff 	if (p->if_vlantrunk == NULL) {
134175ee267cSGleb Smirnoff 		trunk = malloc(sizeof(struct ifvlantrunk),
134275ee267cSGleb Smirnoff 		    M_VLAN, M_WAITOK | M_ZERO);
134375ee267cSGleb Smirnoff 		vlan_inithash(trunk);
134475ee267cSGleb Smirnoff 		TRUNK_LOCK_INIT(trunk);
1345d148c2a2SMatt Joras 		TRUNK_WLOCK(trunk);
134675ee267cSGleb Smirnoff 		p->if_vlantrunk = trunk;
134775ee267cSGleb Smirnoff 		trunk->parent = p;
13489bcf3ae4SAlexander Motin 		if_ref(trunk->parent);
1349b08d611dSMatt Macy 		TRUNK_WUNLOCK(trunk);
135075ee267cSGleb Smirnoff 	} else {
135175ee267cSGleb Smirnoff 		trunk = p->if_vlantrunk;
135275ee267cSGleb Smirnoff 	}
135375ee267cSGleb Smirnoff 
13547983103aSRobert Watson 	ifv->ifv_vid = vid;	/* must set this before vlan_inshash() */
13552ccbbd06SMarcelo Araujo 	ifv->ifv_pcp = 0;       /* Default: best effort delivery. */
13562ccbbd06SMarcelo Araujo 	vlan_tag_recalculate(ifv);
135775ee267cSGleb Smirnoff 	error = vlan_inshash(trunk, ifv);
135875ee267cSGleb Smirnoff 	if (error)
135975ee267cSGleb Smirnoff 		goto done;
136073f2233dSYaroslav Tykhiy 	ifv->ifv_proto = ETHERTYPE_VLAN;
1361a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
1362a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
13631cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
1364d89baa5aSAlexander Motin 	ifv->ifv_capenable = -1;
1365a3814acfSSam Leffler 
1366a3814acfSSam Leffler 	/*
1367a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
1368a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
1369656acce4SYaroslav Tykhiy 	 * use it.
1370a3814acfSSam Leffler 	 */
1371656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
1372656acce4SYaroslav Tykhiy 		/*
1373656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
1374656acce4SYaroslav Tykhiy 		 * handle extended frames.
1375656acce4SYaroslav Tykhiy 		 */
1376a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
1377656acce4SYaroslav Tykhiy 	} else {
1378a3814acfSSam Leffler 		/*
1379a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
1380a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
1381a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
1382a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
1383a3814acfSSam Leffler 		 * which might still be useful.
1384a3814acfSSam Leffler 		 */
1385a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
1386a3814acfSSam Leffler 	}
1387a3814acfSSam Leffler 
138875ee267cSGleb Smirnoff 	ifv->ifv_trunk = trunk;
13891cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
1390e4cd31ddSJeff Roberson 	/*
1391e4cd31ddSJeff Roberson 	 * Initialize fields from our parent.  This duplicates some
1392e4cd31ddSJeff Roberson 	 * work with ether_ifattach() but allows for non-ethernet
1393e4cd31ddSJeff Roberson 	 * interfaces to also work.
1394e4cd31ddSJeff Roberson 	 */
13951cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
139675ee267cSGleb Smirnoff 	ifp->if_baudrate = p->if_baudrate;
1397e4cd31ddSJeff Roberson 	ifp->if_output = p->if_output;
1398e4cd31ddSJeff Roberson 	ifp->if_input = p->if_input;
1399e4cd31ddSJeff Roberson 	ifp->if_resolvemulti = p->if_resolvemulti;
1400e4cd31ddSJeff Roberson 	ifp->if_addrlen = p->if_addrlen;
1401e4cd31ddSJeff Roberson 	ifp->if_broadcastaddr = p->if_broadcastaddr;
140232a52e9eSNavdeep Parhar 	ifp->if_pcp = ifv->ifv_pcp;
1403e4cd31ddSJeff Roberson 
14042cc2df49SGarrett Wollman 	/*
140524993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
140624993214SYaroslav Tykhiy 	 * Other flags are none of our business.
14072cc2df49SGarrett Wollman 	 */
140864a17d2eSYaroslav Tykhiy #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
14091cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
14101cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
14111cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
14121cf236fbSYaroslav Tykhiy 
14131cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
14142cc2df49SGarrett Wollman 
1415*a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
141675ee267cSGleb Smirnoff 	vlan_capabilities(ifv);
1417*a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1418a3814acfSSam Leffler 
1419a3814acfSSam Leffler 	/*
1420e4cd31ddSJeff Roberson 	 * Set up our interface address to reflect the underlying
14212cc2df49SGarrett Wollman 	 * physical interface's.
14222cc2df49SGarrett Wollman 	 */
1423e4cd31ddSJeff Roberson 	bcopy(IF_LLADDR(p), IF_LLADDR(ifp), p->if_addrlen);
1424e4cd31ddSJeff Roberson 	((struct sockaddr_dl *)ifp->if_addr->ifa_addr)->sdl_alen =
1425e4cd31ddSJeff Roberson 	    p->if_addrlen;
14261b2a4f7aSBill Fenner 
1427d148c2a2SMatt Joras 	TASK_INIT(&ifv->lladdr_task, 0, vlan_lladdr_fn, ifv);
14282ada9747SYaroslav Tykhiy 
14292ada9747SYaroslav Tykhiy 	/* We are ready for operation now. */
14302ada9747SYaroslav Tykhiy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1431d148c2a2SMatt Joras 
1432d148c2a2SMatt Joras 	/* Update flags on the parent, if necessary. */
1433d148c2a2SMatt Joras 	vlan_setflags(ifp, 1);
1434b08d611dSMatt Macy 
1435d148c2a2SMatt Joras 	/*
1436b08d611dSMatt Macy 	 * Configure multicast addresses that may already be
1437b08d611dSMatt Macy 	 * joined on the vlan device.
1438d148c2a2SMatt Joras 	 */
1439b08d611dSMatt Macy 	(void)vlan_setmulti(ifp);
1440b08d611dSMatt Macy 
1441b08d611dSMatt Macy done:
1442c725524cSJack F Vogel 	if (error == 0)
14437983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_vid);
1444d148c2a2SMatt Joras 	VLAN_XUNLOCK();
144575ee267cSGleb Smirnoff 
144675ee267cSGleb Smirnoff 	return (error);
14472cc2df49SGarrett Wollman }
14482cc2df49SGarrett Wollman 
14496f359e28SJohn Baldwin static void
1450f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
1451f731f104SBill Paul {
14525cb8c31aSYaroslav Tykhiy 
1453d148c2a2SMatt Joras 	VLAN_XLOCK();
145428cc4d37SJohn Baldwin 	vlan_unconfig_locked(ifp, 0);
1455d148c2a2SMatt Joras 	VLAN_XUNLOCK();
14565cb8c31aSYaroslav Tykhiy }
14575cb8c31aSYaroslav Tykhiy 
14586f359e28SJohn Baldwin static void
145928cc4d37SJohn Baldwin vlan_unconfig_locked(struct ifnet *ifp, int departing)
14605cb8c31aSYaroslav Tykhiy {
146175ee267cSGleb Smirnoff 	struct ifvlantrunk *trunk;
1462f731f104SBill Paul 	struct vlan_mc_entry *mc;
1463f731f104SBill Paul 	struct ifvlan *ifv;
1464c725524cSJack F Vogel 	struct ifnet  *parent;
146528cc4d37SJohn Baldwin 	int error;
1466f731f104SBill Paul 
1467d148c2a2SMatt Joras 	VLAN_XLOCK_ASSERT();
14684faedfe8SSam Leffler 
1469f731f104SBill Paul 	ifv = ifp->if_softc;
147075ee267cSGleb Smirnoff 	trunk = ifv->ifv_trunk;
147122893351SJack F Vogel 	parent = NULL;
1472f731f104SBill Paul 
147322893351SJack F Vogel 	if (trunk != NULL) {
147422893351SJack F Vogel 		parent = trunk->parent;
14751b2a4f7aSBill Fenner 
1476f731f104SBill Paul 		/*
1477f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
1478f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
14791b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
1480f731f104SBill Paul 		 */
1481b08d611dSMatt Macy 		while ((mc = CK_SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
14826f359e28SJohn Baldwin 			/*
148328cc4d37SJohn Baldwin 			 * If the parent interface is being detached,
1484b90dde2fSJohn Baldwin 			 * all its multicast addresses have already
148528cc4d37SJohn Baldwin 			 * been removed.  Warn about errors if
148628cc4d37SJohn Baldwin 			 * if_delmulti() does fail, but don't abort as
148728cc4d37SJohn Baldwin 			 * all callers expect vlan destruction to
148828cc4d37SJohn Baldwin 			 * succeed.
14896f359e28SJohn Baldwin 			 */
149028cc4d37SJohn Baldwin 			if (!departing) {
149128cc4d37SJohn Baldwin 				error = if_delmulti(parent,
1492e4cd31ddSJeff Roberson 				    (struct sockaddr *)&mc->mc_addr);
149328cc4d37SJohn Baldwin 				if (error)
149428cc4d37SJohn Baldwin 					if_printf(ifp,
149528cc4d37SJohn Baldwin 		    "Failed to delete multicast address from parent: %d\n",
149628cc4d37SJohn Baldwin 					    error);
149728cc4d37SJohn Baldwin 			}
1498b08d611dSMatt Macy 			CK_SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
1499c32a9d66SHans Petter Selasky 			epoch_call(net_epoch_preempt, &mc->mc_epoch_ctx, vlan_mc_free);
1500f731f104SBill Paul 		}
1501a3814acfSSam Leffler 
15021cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
1503d148c2a2SMatt Joras 
150475ee267cSGleb Smirnoff 		vlan_remhash(trunk, ifv);
150575ee267cSGleb Smirnoff 		ifv->ifv_trunk = NULL;
150675ee267cSGleb Smirnoff 
150775ee267cSGleb Smirnoff 		/*
150875ee267cSGleb Smirnoff 		 * Check if we were the last.
150975ee267cSGleb Smirnoff 		 */
151075ee267cSGleb Smirnoff 		if (trunk->refcnt == 0) {
15112d222cb7SAlexander Motin 			parent->if_vlantrunk = NULL;
1512b08d611dSMatt Macy 			NET_EPOCH_WAIT();
151375ee267cSGleb Smirnoff 			trunk_destroy(trunk);
1514d148c2a2SMatt Joras 		}
15151b2a4f7aSBill Fenner 	}
1516f731f104SBill Paul 
1517f731f104SBill Paul 	/* Disconnect from parent. */
15181cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
15191cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
15205cb8c31aSYaroslav Tykhiy 	ifp->if_mtu = ETHERMTU;
15215cb8c31aSYaroslav Tykhiy 	ifp->if_link_state = LINK_STATE_UNKNOWN;
15225cb8c31aSYaroslav Tykhiy 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1523f731f104SBill Paul 
152422893351SJack F Vogel 	/*
152522893351SJack F Vogel 	 * Only dispatch an event if vlan was
152622893351SJack F Vogel 	 * attached, otherwise there is nothing
152722893351SJack F Vogel 	 * to cleanup anyway.
152822893351SJack F Vogel 	 */
152922893351SJack F Vogel 	if (parent != NULL)
15307983103aSRobert Watson 		EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_vid);
1531f731f104SBill Paul }
1532f731f104SBill Paul 
15331cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
1534f731f104SBill Paul static int
15351cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
15361cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
1537a3814acfSSam Leffler {
15381cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
15391cf236fbSYaroslav Tykhiy 	int error;
1540a3814acfSSam Leffler 
1541d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1542a3814acfSSam Leffler 
15431cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
15441cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
15451cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
15461cf236fbSYaroslav Tykhiy 
15471cf236fbSYaroslav Tykhiy 	/*
15481cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
15491cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
15501cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
15511cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
15521cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
15531cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
15541cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
15551cf236fbSYaroslav Tykhiy 	 */
15561cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
155775ee267cSGleb Smirnoff 		error = (*func)(PARENT(ifv), status);
15581cf236fbSYaroslav Tykhiy 		if (error)
1559a3814acfSSam Leffler 			return (error);
15601cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
15611cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
15621cf236fbSYaroslav Tykhiy 	}
15631cf236fbSYaroslav Tykhiy 	return (0);
15641cf236fbSYaroslav Tykhiy }
15651cf236fbSYaroslav Tykhiy 
15661cf236fbSYaroslav Tykhiy /*
15671cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
15681cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
15691cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
15701cf236fbSYaroslav Tykhiy  */
15711cf236fbSYaroslav Tykhiy static int
15721cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
15731cf236fbSYaroslav Tykhiy {
15741cf236fbSYaroslav Tykhiy 	int error, i;
15751cf236fbSYaroslav Tykhiy 
15761cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
15771cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
15781cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
15791cf236fbSYaroslav Tykhiy 		if (error)
15801cf236fbSYaroslav Tykhiy 			return (error);
15811cf236fbSYaroslav Tykhiy 	}
15821cf236fbSYaroslav Tykhiy 	return (0);
1583a3814acfSSam Leffler }
1584a3814acfSSam Leffler 
1585127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
1586127d7b2dSAndre Oppermann static void
1587a6fffd6cSBrooks Davis vlan_link_state(struct ifnet *ifp)
1588127d7b2dSAndre Oppermann {
1589*a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1590d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
1591127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
1592127d7b2dSAndre Oppermann 
1593d148c2a2SMatt Joras 	/* Called from a taskqueue_swi task, so we cannot sleep. */
1594*a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1595d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1596d148c2a2SMatt Joras 	if (trunk == NULL) {
1597*a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1598d148c2a2SMatt Joras 		return;
1599d148c2a2SMatt Joras 	}
1600d148c2a2SMatt Joras 
1601d148c2a2SMatt Joras 	TRUNK_WLOCK(trunk);
1602d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
1603aad0be7aSGleb Smirnoff 		ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
1604fc74a9f9SBrooks Davis 		if_link_state_change(ifv->ifv_ifp,
160575ee267cSGleb Smirnoff 		    trunk->parent->if_link_state);
1606127d7b2dSAndre Oppermann 	}
1607d148c2a2SMatt Joras 	TRUNK_WUNLOCK(trunk);
1608*a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
160975ee267cSGleb Smirnoff }
161075ee267cSGleb Smirnoff 
161175ee267cSGleb Smirnoff static void
161275ee267cSGleb Smirnoff vlan_capabilities(struct ifvlan *ifv)
161375ee267cSGleb Smirnoff {
1614d148c2a2SMatt Joras 	struct ifnet *p;
1615d148c2a2SMatt Joras 	struct ifnet *ifp;
16169fd573c3SHans Petter Selasky 	struct ifnet_hw_tsomax hw_tsomax;
1617d89baa5aSAlexander Motin 	int cap = 0, ena = 0, mena;
1618d89baa5aSAlexander Motin 	u_long hwa = 0;
161975ee267cSGleb Smirnoff 
1620d148c2a2SMatt Joras 	VLAN_SXLOCK_ASSERT();
1621*a68cc388SGleb Smirnoff 	NET_EPOCH_ASSERT();
1622d148c2a2SMatt Joras 	p = PARENT(ifv);
1623d148c2a2SMatt Joras 	ifp = ifv->ifv_ifp;
162475ee267cSGleb Smirnoff 
1625d89baa5aSAlexander Motin 	/* Mask parent interface enabled capabilities disabled by user. */
1626d89baa5aSAlexander Motin 	mena = p->if_capenable & ifv->ifv_capenable;
1627d89baa5aSAlexander Motin 
162875ee267cSGleb Smirnoff 	/*
162975ee267cSGleb Smirnoff 	 * If the parent interface can do checksum offloading
163075ee267cSGleb Smirnoff 	 * on VLANs, then propagate its hardware-assisted
163175ee267cSGleb Smirnoff 	 * checksumming flags. Also assert that checksum
163275ee267cSGleb Smirnoff 	 * offloading requires hardware VLAN tagging.
163375ee267cSGleb Smirnoff 	 */
163475ee267cSGleb Smirnoff 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
1635d89baa5aSAlexander Motin 		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
163675ee267cSGleb Smirnoff 	if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
163775ee267cSGleb Smirnoff 	    p->if_capenable & IFCAP_VLAN_HWTAGGING) {
1638d89baa5aSAlexander Motin 		ena |= mena & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
1639d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM)
1640d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_IP | CSUM_TCP |
1641d89baa5aSAlexander Motin 			    CSUM_UDP | CSUM_SCTP);
1642d89baa5aSAlexander Motin 		if (ena & IFCAP_TXCSUM_IPV6)
1643d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & (CSUM_TCP_IPV6 |
1644d89baa5aSAlexander Motin 			    CSUM_UDP_IPV6 | CSUM_SCTP_IPV6);
164575ee267cSGleb Smirnoff 	}
1646d89baa5aSAlexander Motin 
16479b76d9cbSPyun YongHyeon 	/*
16489b76d9cbSPyun YongHyeon 	 * If the parent interface can do TSO on VLANs then
16499b76d9cbSPyun YongHyeon 	 * propagate the hardware-assisted flag. TSO on VLANs
16509b76d9cbSPyun YongHyeon 	 * does not necessarily require hardware VLAN tagging.
16519b76d9cbSPyun YongHyeon 	 */
16529fd573c3SHans Petter Selasky 	memset(&hw_tsomax, 0, sizeof(hw_tsomax));
16539fd573c3SHans Petter Selasky 	if_hw_tsomax_common(p, &hw_tsomax);
16549fd573c3SHans Petter Selasky 	if_hw_tsomax_update(ifp, &hw_tsomax);
16559b76d9cbSPyun YongHyeon 	if (p->if_capabilities & IFCAP_VLAN_HWTSO)
1656d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TSO;
16579b76d9cbSPyun YongHyeon 	if (p->if_capenable & IFCAP_VLAN_HWTSO) {
1658d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TSO;
1659d89baa5aSAlexander Motin 		if (ena & IFCAP_TSO)
1660d89baa5aSAlexander Motin 			hwa |= p->if_hwassist & CSUM_TSO;
16619b76d9cbSPyun YongHyeon 	}
166209fe6320SNavdeep Parhar 
166309fe6320SNavdeep Parhar 	/*
166459150e91SAlexander Motin 	 * If the parent interface can do LRO and checksum offloading on
166559150e91SAlexander Motin 	 * VLANs, then guess it may do LRO on VLANs.  False positive here
166659150e91SAlexander Motin 	 * cost nothing, while false negative may lead to some confusions.
166759150e91SAlexander Motin 	 */
166859150e91SAlexander Motin 	if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
166959150e91SAlexander Motin 		cap |= p->if_capabilities & IFCAP_LRO;
167059150e91SAlexander Motin 	if (p->if_capenable & IFCAP_VLAN_HWCSUM)
167159150e91SAlexander Motin 		ena |= p->if_capenable & IFCAP_LRO;
167259150e91SAlexander Motin 
167359150e91SAlexander Motin 	/*
167409fe6320SNavdeep Parhar 	 * If the parent interface can offload TCP connections over VLANs then
167509fe6320SNavdeep Parhar 	 * propagate its TOE capability to the VLAN interface.
167609fe6320SNavdeep Parhar 	 *
167709fe6320SNavdeep Parhar 	 * All TOE drivers in the tree today can deal with VLANs.  If this
167809fe6320SNavdeep Parhar 	 * changes then IFCAP_VLAN_TOE should be promoted to a full capability
167909fe6320SNavdeep Parhar 	 * with its own bit.
168009fe6320SNavdeep Parhar 	 */
168109fe6320SNavdeep Parhar #define	IFCAP_VLAN_TOE IFCAP_TOE
168209fe6320SNavdeep Parhar 	if (p->if_capabilities & IFCAP_VLAN_TOE)
1683d89baa5aSAlexander Motin 		cap |= p->if_capabilities & IFCAP_TOE;
168409fe6320SNavdeep Parhar 	if (p->if_capenable & IFCAP_VLAN_TOE) {
168509fe6320SNavdeep Parhar 		TOEDEV(ifp) = TOEDEV(p);
1686d89baa5aSAlexander Motin 		ena |= mena & IFCAP_TOE;
168709fe6320SNavdeep Parhar 	}
1688f3e7afe2SHans Petter Selasky 
1689d89baa5aSAlexander Motin 	/*
1690d89baa5aSAlexander Motin 	 * If the parent interface supports dynamic link state, so does the
1691d89baa5aSAlexander Motin 	 * VLAN interface.
1692d89baa5aSAlexander Motin 	 */
1693d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_LINKSTATE);
1694d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_LINKSTATE);
1695d89baa5aSAlexander Motin 
1696f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1697f3e7afe2SHans Petter Selasky 	/*
1698f3e7afe2SHans Petter Selasky 	 * If the parent interface supports ratelimiting, so does the
1699f3e7afe2SHans Petter Selasky 	 * VLAN interface.
1700f3e7afe2SHans Petter Selasky 	 */
1701d89baa5aSAlexander Motin 	cap |= (p->if_capabilities & IFCAP_TXRTLMT);
1702d89baa5aSAlexander Motin 	ena |= (mena & IFCAP_TXRTLMT);
1703f3e7afe2SHans Petter Selasky #endif
1704d89baa5aSAlexander Motin 
1705d89baa5aSAlexander Motin 	ifp->if_capabilities = cap;
1706d89baa5aSAlexander Motin 	ifp->if_capenable = ena;
1707d89baa5aSAlexander Motin 	ifp->if_hwassist = hwa;
170875ee267cSGleb Smirnoff }
170975ee267cSGleb Smirnoff 
171075ee267cSGleb Smirnoff static void
171175ee267cSGleb Smirnoff vlan_trunk_capabilities(struct ifnet *ifp)
171275ee267cSGleb Smirnoff {
1713*a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1714d148c2a2SMatt Joras 	struct ifvlantrunk *trunk;
171575ee267cSGleb Smirnoff 	struct ifvlan *ifv;
171675ee267cSGleb Smirnoff 
1717d148c2a2SMatt Joras 	VLAN_SLOCK();
1718d148c2a2SMatt Joras 	trunk = ifp->if_vlantrunk;
1719d148c2a2SMatt Joras 	if (trunk == NULL) {
1720d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1721d148c2a2SMatt Joras 		return;
1722d148c2a2SMatt Joras 	}
1723*a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1724d148c2a2SMatt Joras 	VLAN_FOREACH(ifv, trunk) {
172575ee267cSGleb Smirnoff 		vlan_capabilities(ifv);
172675ee267cSGleb Smirnoff 	}
1727*a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1728d148c2a2SMatt Joras 	VLAN_SUNLOCK();
1729127d7b2dSAndre Oppermann }
1730127d7b2dSAndre Oppermann 
1731a3814acfSSam Leffler static int
1732cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
17332cc2df49SGarrett Wollman {
17342cc2df49SGarrett Wollman 	struct ifnet *p;
17352cc2df49SGarrett Wollman 	struct ifreq *ifr;
1736e4cd31ddSJeff Roberson 	struct ifaddr *ifa;
17372cc2df49SGarrett Wollman 	struct ifvlan *ifv;
17382d222cb7SAlexander Motin 	struct ifvlantrunk *trunk;
17392cc2df49SGarrett Wollman 	struct vlanreq vlr;
17402cc2df49SGarrett Wollman 	int error = 0;
17412cc2df49SGarrett Wollman 
17422cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
1743e4cd31ddSJeff Roberson 	ifa = (struct ifaddr *) data;
17442cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
17452cc2df49SGarrett Wollman 
17462cc2df49SGarrett Wollman 	switch (cmd) {
1747e4cd31ddSJeff Roberson 	case SIOCSIFADDR:
1748e4cd31ddSJeff Roberson 		ifp->if_flags |= IFF_UP;
1749e4cd31ddSJeff Roberson #ifdef INET
1750e4cd31ddSJeff Roberson 		if (ifa->ifa_addr->sa_family == AF_INET)
1751e4cd31ddSJeff Roberson 			arp_ifinit(ifp, ifa);
1752e4cd31ddSJeff Roberson #endif
1753e4cd31ddSJeff Roberson 		break;
1754e4cd31ddSJeff Roberson 	case SIOCGIFADDR:
175538d958a6SBrooks Davis 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
175638d958a6SBrooks Davis 		    ifp->if_addrlen);
1757e4cd31ddSJeff Roberson 		break;
1758b3cca108SBill Fenner 	case SIOCGIFMEDIA:
1759d148c2a2SMatt Joras 		VLAN_SLOCK();
176075ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
1761d8564efdSEd Maste 			p = PARENT(ifv);
17629bcf3ae4SAlexander Motin 			if_ref(p);
1763d8564efdSEd Maste 			error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
17649bcf3ae4SAlexander Motin 			if_rele(p);
1765b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
1766b3cca108SBill Fenner 			if (error == 0) {
1767b3cca108SBill Fenner 				struct ifmediareq *ifmr;
1768b3cca108SBill Fenner 
1769b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
1770b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
1771b3cca108SBill Fenner 					ifmr->ifm_count = 1;
1772b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
1773b3cca108SBill Fenner 						ifmr->ifm_ulist,
1774b3cca108SBill Fenner 						sizeof(int));
1775b3cca108SBill Fenner 				}
1776b3cca108SBill Fenner 			}
17774faedfe8SSam Leffler 		} else {
1778b3cca108SBill Fenner 			error = EINVAL;
17794faedfe8SSam Leffler 		}
1780d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1781b3cca108SBill Fenner 		break;
1782b3cca108SBill Fenner 
1783b3cca108SBill Fenner 	case SIOCSIFMEDIA:
1784b3cca108SBill Fenner 		error = EINVAL;
1785b3cca108SBill Fenner 		break;
1786b3cca108SBill Fenner 
17872cc2df49SGarrett Wollman 	case SIOCSIFMTU:
17882cc2df49SGarrett Wollman 		/*
17892cc2df49SGarrett Wollman 		 * Set the interface MTU.
17902cc2df49SGarrett Wollman 		 */
1791d148c2a2SMatt Joras 		VLAN_SLOCK();
1792d148c2a2SMatt Joras 		trunk = TRUNK(ifv);
1793d148c2a2SMatt Joras 		if (trunk != NULL) {
1794d148c2a2SMatt Joras 			TRUNK_WLOCK(trunk);
1795a3814acfSSam Leffler 			if (ifr->ifr_mtu >
179675ee267cSGleb Smirnoff 			     (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
1797a3814acfSSam Leffler 			    ifr->ifr_mtu <
1798a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
17992cc2df49SGarrett Wollman 				error = EINVAL;
1800a3814acfSSam Leffler 			else
18012cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
1802d148c2a2SMatt Joras 			TRUNK_WUNLOCK(trunk);
1803a3814acfSSam Leffler 		} else
1804a3814acfSSam Leffler 			error = EINVAL;
1805d148c2a2SMatt Joras 		VLAN_SUNLOCK();
18062cc2df49SGarrett Wollman 		break;
18072cc2df49SGarrett Wollman 
18082cc2df49SGarrett Wollman 	case SIOCSETVLAN:
1809ccf7ba97SMarko Zec #ifdef VIMAGE
181015f6780eSRobert Watson 		/*
181115f6780eSRobert Watson 		 * XXXRW/XXXBZ: The goal in these checks is to allow a VLAN
181215f6780eSRobert Watson 		 * interface to be delegated to a jail without allowing the
181315f6780eSRobert Watson 		 * jail to change what underlying interface/VID it is
181415f6780eSRobert Watson 		 * associated with.  We are not entirely convinced that this
18155a39f779SRobert Watson 		 * is the right way to accomplish that policy goal.
181615f6780eSRobert Watson 		 */
1817ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1818ccf7ba97SMarko Zec 			error = EPERM;
1819ccf7ba97SMarko Zec 			break;
1820ccf7ba97SMarko Zec 		}
1821ccf7ba97SMarko Zec #endif
1822541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &vlr, sizeof(vlr));
18232cc2df49SGarrett Wollman 		if (error)
18242cc2df49SGarrett Wollman 			break;
18252cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
1826f731f104SBill Paul 			vlan_unconfig(ifp);
18272cc2df49SGarrett Wollman 			break;
18282cc2df49SGarrett Wollman 		}
18299bcf3ae4SAlexander Motin 		p = ifunit_ref(vlr.vlr_parent);
18301bdc73d3SEd Maste 		if (p == NULL) {
18312cc2df49SGarrett Wollman 			error = ENOENT;
18322cc2df49SGarrett Wollman 			break;
18332cc2df49SGarrett Wollman 		}
183475ee267cSGleb Smirnoff 		error = vlan_config(ifv, p, vlr.vlr_tag);
18359bcf3ae4SAlexander Motin 		if_rele(p);
18362cc2df49SGarrett Wollman 		break;
18372cc2df49SGarrett Wollman 
18382cc2df49SGarrett Wollman 	case SIOCGETVLAN:
1839ccf7ba97SMarko Zec #ifdef VIMAGE
1840ccf7ba97SMarko Zec 		if (ifp->if_vnet != ifp->if_home_vnet) {
1841ccf7ba97SMarko Zec 			error = EPERM;
1842ccf7ba97SMarko Zec 			break;
1843ccf7ba97SMarko Zec 		}
1844ccf7ba97SMarko Zec #endif
184515a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
1846d148c2a2SMatt Joras 		VLAN_SLOCK();
184775ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL) {
184875ee267cSGleb Smirnoff 			strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
18499bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
18507983103aSRobert Watson 			vlr.vlr_tag = ifv->ifv_vid;
18512cc2df49SGarrett Wollman 		}
1852d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1853541d96aaSBrooks Davis 		error = copyout(&vlr, ifr_data_get_ptr(ifr), sizeof(vlr));
18542cc2df49SGarrett Wollman 		break;
18552cc2df49SGarrett Wollman 
18562cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
18572cc2df49SGarrett Wollman 		/*
18581cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
18591cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
18602cc2df49SGarrett Wollman 		 */
1861d148c2a2SMatt Joras 		VLAN_XLOCK();
186275ee267cSGleb Smirnoff 		if (TRUNK(ifv) != NULL)
18631cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
1864d148c2a2SMatt Joras 		VLAN_XUNLOCK();
18652cc2df49SGarrett Wollman 		break;
1866a3814acfSSam Leffler 
1867f731f104SBill Paul 	case SIOCADDMULTI:
1868f731f104SBill Paul 	case SIOCDELMULTI:
186975ee267cSGleb Smirnoff 		/*
187075ee267cSGleb Smirnoff 		 * If we don't have a parent, just remember the membership for
187175ee267cSGleb Smirnoff 		 * when we do.
1872d148c2a2SMatt Joras 		 *
1873d148c2a2SMatt Joras 		 * XXX We need the rmlock here to avoid sleeping while
1874d148c2a2SMatt Joras 		 * holding in6_multi_mtx.
187575ee267cSGleb Smirnoff 		 */
1876b08d611dSMatt Macy 		VLAN_XLOCK();
18772d222cb7SAlexander Motin 		trunk = TRUNK(ifv);
1878b08d611dSMatt Macy 		if (trunk != NULL)
1879f731f104SBill Paul 			error = vlan_setmulti(ifp);
1880b08d611dSMatt Macy 		VLAN_XUNLOCK();
188175ee267cSGleb Smirnoff 
1882b08d611dSMatt Macy 		break;
18832ccbbd06SMarcelo Araujo 	case SIOCGVLANPCP:
18842ccbbd06SMarcelo Araujo #ifdef VIMAGE
18852ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
18862ccbbd06SMarcelo Araujo 			error = EPERM;
18872ccbbd06SMarcelo Araujo 			break;
18882ccbbd06SMarcelo Araujo 		}
18892ccbbd06SMarcelo Araujo #endif
18902ccbbd06SMarcelo Araujo 		ifr->ifr_vlan_pcp = ifv->ifv_pcp;
18912ccbbd06SMarcelo Araujo 		break;
18922ccbbd06SMarcelo Araujo 
18932ccbbd06SMarcelo Araujo 	case SIOCSVLANPCP:
18942ccbbd06SMarcelo Araujo #ifdef VIMAGE
18952ccbbd06SMarcelo Araujo 		if (ifp->if_vnet != ifp->if_home_vnet) {
18962ccbbd06SMarcelo Araujo 			error = EPERM;
18972ccbbd06SMarcelo Araujo 			break;
18982ccbbd06SMarcelo Araujo 		}
18992ccbbd06SMarcelo Araujo #endif
19002ccbbd06SMarcelo Araujo 		error = priv_check(curthread, PRIV_NET_SETVLANPCP);
19012ccbbd06SMarcelo Araujo 		if (error)
19022ccbbd06SMarcelo Araujo 			break;
19032ccbbd06SMarcelo Araujo 		if (ifr->ifr_vlan_pcp > 7) {
19042ccbbd06SMarcelo Araujo 			error = EINVAL;
19052ccbbd06SMarcelo Araujo 			break;
19062ccbbd06SMarcelo Araujo 		}
19072ccbbd06SMarcelo Araujo 		ifv->ifv_pcp = ifr->ifr_vlan_pcp;
190832a52e9eSNavdeep Parhar 		ifp->if_pcp = ifv->ifv_pcp;
19092ccbbd06SMarcelo Araujo 		vlan_tag_recalculate(ifv);
19104a381a9eSHans Petter Selasky 		/* broadcast event about PCP change */
19114a381a9eSHans Petter Selasky 		EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
19122ccbbd06SMarcelo Araujo 		break;
19132ccbbd06SMarcelo Araujo 
1914d89baa5aSAlexander Motin 	case SIOCSIFCAP:
1915d148c2a2SMatt Joras 		VLAN_SLOCK();
1916d89baa5aSAlexander Motin 		ifv->ifv_capenable = ifr->ifr_reqcap;
1917d89baa5aSAlexander Motin 		trunk = TRUNK(ifv);
1918d89baa5aSAlexander Motin 		if (trunk != NULL) {
1919*a68cc388SGleb Smirnoff 			struct epoch_tracker et;
1920*a68cc388SGleb Smirnoff 
1921*a68cc388SGleb Smirnoff 			NET_EPOCH_ENTER(et);
1922d89baa5aSAlexander Motin 			vlan_capabilities(ifv);
1923*a68cc388SGleb Smirnoff 			NET_EPOCH_EXIT(et);
1924d89baa5aSAlexander Motin 		}
1925d148c2a2SMatt Joras 		VLAN_SUNLOCK();
1926d89baa5aSAlexander Motin 		break;
1927d89baa5aSAlexander Motin 
19282cc2df49SGarrett Wollman 	default:
1929e4cd31ddSJeff Roberson 		error = EINVAL;
1930e4cd31ddSJeff Roberson 		break;
19312cc2df49SGarrett Wollman 	}
193215a66c21SBruce M Simpson 
193315a66c21SBruce M Simpson 	return (error);
19342cc2df49SGarrett Wollman }
1935f3e7afe2SHans Petter Selasky 
1936f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1937f3e7afe2SHans Petter Selasky static int
1938f3e7afe2SHans Petter Selasky vlan_snd_tag_alloc(struct ifnet *ifp,
1939f3e7afe2SHans Petter Selasky     union if_snd_tag_alloc_params *params,
1940f3e7afe2SHans Petter Selasky     struct m_snd_tag **ppmt)
1941f3e7afe2SHans Petter Selasky {
1942f3e7afe2SHans Petter Selasky 
1943f3e7afe2SHans Petter Selasky 	/* get trunk device */
1944f3e7afe2SHans Petter Selasky 	ifp = vlan_trunkdev(ifp);
1945f3e7afe2SHans Petter Selasky 	if (ifp == NULL || (ifp->if_capenable & IFCAP_TXRTLMT) == 0)
1946f3e7afe2SHans Petter Selasky 		return (EOPNOTSUPP);
1947f3e7afe2SHans Petter Selasky 	/* forward allocation request */
1948f3e7afe2SHans Petter Selasky 	return (ifp->if_snd_tag_alloc(ifp, params, ppmt));
1949f3e7afe2SHans Petter Selasky }
1950f3e7afe2SHans Petter Selasky #endif
1951