xref: /freebsd/sys/net/if_vlan.c (revision 1cf236fb0c897e1943e4fdc8cd536c167f281883)
1c398230bSWarner Losh /*-
22cc2df49SGarrett Wollman  * Copyright 1998 Massachusetts Institute of Technology
32cc2df49SGarrett Wollman  *
42cc2df49SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
52cc2df49SGarrett Wollman  * its documentation for any purpose and without fee is hereby
62cc2df49SGarrett Wollman  * granted, provided that both the above copyright notice and this
72cc2df49SGarrett Wollman  * permission notice appear in all copies, that both the above
82cc2df49SGarrett Wollman  * copyright notice and this permission notice appear in all
92cc2df49SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
102cc2df49SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
112cc2df49SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
122cc2df49SGarrett Wollman  * no representations about the suitability of this software for any
132cc2df49SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
142cc2df49SGarrett Wollman  * warranty.
152cc2df49SGarrett Wollman  *
162cc2df49SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
172cc2df49SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
182cc2df49SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
192cc2df49SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
202cc2df49SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212cc2df49SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222cc2df49SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
232cc2df49SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
242cc2df49SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
252cc2df49SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
262cc2df49SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272cc2df49SGarrett Wollman  * SUCH DAMAGE.
282cc2df49SGarrett Wollman  *
29c3aac50fSPeter Wemm  * $FreeBSD$
302cc2df49SGarrett Wollman  */
312cc2df49SGarrett Wollman 
322cc2df49SGarrett Wollman /*
332cc2df49SGarrett Wollman  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
342cc2df49SGarrett Wollman  * Might be extended some day to also handle IEEE 802.1p priority
352cc2df49SGarrett Wollman  * tagging.  This is sort of sneaky in the implementation, since
362cc2df49SGarrett Wollman  * we need to pretend to be enough of an Ethernet implementation
372cc2df49SGarrett Wollman  * to make arp work.  The way we do this is by telling everyone
382cc2df49SGarrett Wollman  * that we are an Ethernet, and then catch the packets that
3946a32e61SPhilippe Charnier  * ether_output() left on our output queue when it calls
402cc2df49SGarrett Wollman  * if_start(), rewrite them for use by the real outgoing interface,
412cc2df49SGarrett Wollman  * and ask it to send them.
422cc2df49SGarrett Wollman  */
432cc2df49SGarrett Wollman 
442cc2df49SGarrett Wollman #include "opt_inet.h"
452cc2df49SGarrett Wollman 
462cc2df49SGarrett Wollman #include <sys/param.h>
472cc2df49SGarrett Wollman #include <sys/kernel.h>
48f731f104SBill Paul #include <sys/malloc.h>
492cc2df49SGarrett Wollman #include <sys/mbuf.h>
502b120974SPeter Wemm #include <sys/module.h>
51f731f104SBill Paul #include <sys/queue.h>
522cc2df49SGarrett Wollman #include <sys/socket.h>
532cc2df49SGarrett Wollman #include <sys/sockio.h>
542cc2df49SGarrett Wollman #include <sys/sysctl.h>
552cc2df49SGarrett Wollman #include <sys/systm.h>
562cc2df49SGarrett Wollman 
572cc2df49SGarrett Wollman #include <net/bpf.h>
582cc2df49SGarrett Wollman #include <net/ethernet.h>
592cc2df49SGarrett Wollman #include <net/if.h>
60f889d2efSBrooks Davis #include <net/if_clone.h>
612cc2df49SGarrett Wollman #include <net/if_arp.h>
622cc2df49SGarrett Wollman #include <net/if_dl.h>
632cc2df49SGarrett Wollman #include <net/if_types.h>
642cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
652cc2df49SGarrett Wollman 
662cc2df49SGarrett Wollman #ifdef INET
672cc2df49SGarrett Wollman #include <netinet/in.h>
682cc2df49SGarrett Wollman #include <netinet/if_ether.h>
692cc2df49SGarrett Wollman #endif
702cc2df49SGarrett Wollman 
719d4fe4b2SBrooks Davis #define VLANNAME	"vlan"
729d4fe4b2SBrooks Davis 
73a3814acfSSam Leffler struct vlan_mc_entry {
74a3814acfSSam Leffler 	struct ether_addr		mc_addr;
75a3814acfSSam Leffler 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
76a3814acfSSam Leffler };
77a3814acfSSam Leffler 
78a3814acfSSam Leffler struct	ifvlan {
79fc74a9f9SBrooks Davis 	struct	ifnet *ifv_ifp;
80a3814acfSSam Leffler 	struct	ifnet *ifv_p;	/* parent inteface of this vlan */
811cf236fbSYaroslav Tykhiy 	int	ifv_pflags;	/* special flags we have set on parent */
82a3814acfSSam Leffler 	struct	ifv_linkmib {
83a3814acfSSam Leffler 		int	ifvm_parent;
84a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
85a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
86a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
87a3814acfSSam Leffler 		u_int16_t ifvm_proto; /* encapsulation ethertype */
88a3814acfSSam Leffler 		u_int16_t ifvm_tag; /* tag to apply on packets leaving if */
89a3814acfSSam Leffler 	}	ifv_mib;
90a3814acfSSam Leffler 	SLIST_HEAD(__vlan_mchead, vlan_mc_entry)	vlan_mc_listhead;
91a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
92a3814acfSSam Leffler };
93a3814acfSSam Leffler #define	ifv_tag	ifv_mib.ifvm_tag
94a3814acfSSam Leffler #define	ifv_encaplen	ifv_mib.ifvm_encaplen
95a3814acfSSam Leffler #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
96a3814acfSSam Leffler #define	ifv_mintu	ifv_mib.ifvm_mintu
97a3814acfSSam Leffler 
981cf236fbSYaroslav Tykhiy /* Special flags we should propagate to parent */
991cf236fbSYaroslav Tykhiy static struct {
1001cf236fbSYaroslav Tykhiy 	int flag;
1011cf236fbSYaroslav Tykhiy 	int (*func)(struct ifnet *, int);
1021cf236fbSYaroslav Tykhiy } vlan_pflags[] = {
1031cf236fbSYaroslav Tykhiy 	{IFF_PROMISC, ifpromisc},
1041cf236fbSYaroslav Tykhiy 	{IFF_ALLMULTI, if_allmulti},
1051cf236fbSYaroslav Tykhiy 	{0, NULL}
1061cf236fbSYaroslav Tykhiy };
107a3814acfSSam Leffler 
1084a408dcbSBill Paul SYSCTL_DECL(_net_link);
109d2a75853SBill Fenner SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
1102cc2df49SGarrett Wollman SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
1112cc2df49SGarrett Wollman 
1125e17543aSBrooks Davis static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
1139d4fe4b2SBrooks Davis static LIST_HEAD(, ifvlan) ifv_list;
1142cc2df49SGarrett Wollman 
1154faedfe8SSam Leffler /*
1164faedfe8SSam Leffler  * Locking: one lock is used to guard both the ifv_list and modification
1174faedfe8SSam Leffler  * to vlan data structures.  We are rather conservative here; probably
1184faedfe8SSam Leffler  * more than necessary.
1194faedfe8SSam Leffler  */
1204faedfe8SSam Leffler static struct mtx ifv_mtx;
1215e17543aSBrooks Davis #define	VLAN_LOCK_INIT()	mtx_init(&ifv_mtx, VLANNAME, NULL, MTX_DEF)
1224faedfe8SSam Leffler #define	VLAN_LOCK_DESTROY()	mtx_destroy(&ifv_mtx)
1234faedfe8SSam Leffler #define	VLAN_LOCK_ASSERT()	mtx_assert(&ifv_mtx, MA_OWNED)
1244faedfe8SSam Leffler #define	VLAN_LOCK()	mtx_lock(&ifv_mtx)
1254faedfe8SSam Leffler #define	VLAN_UNLOCK()	mtx_unlock(&ifv_mtx)
1264faedfe8SSam Leffler 
1272cc2df49SGarrett Wollman static	void vlan_start(struct ifnet *ifp);
1282cc2df49SGarrett Wollman static	void vlan_ifinit(void *foo);
129a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
130cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
1311cf236fbSYaroslav Tykhiy static	int vlan_setflag(struct ifnet *ifp, int flag, int status,
1321cf236fbSYaroslav Tykhiy     int (*func)(struct ifnet *, int));
1331cf236fbSYaroslav Tykhiy static	int vlan_setflags(struct ifnet *ifp, int status);
134f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
135f731f104SBill Paul static	int vlan_unconfig(struct ifnet *ifp);
136f731f104SBill Paul static	int vlan_config(struct ifvlan *ifv, struct ifnet *p);
137127d7b2dSAndre Oppermann static	void vlan_link_state(struct ifnet *ifp, int link);
138f731f104SBill Paul 
139f889d2efSBrooks Davis static	struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
140f889d2efSBrooks Davis     const char *, int *);
141f889d2efSBrooks Davis static	int vlan_clone_match(struct if_clone *, const char *);
142f889d2efSBrooks Davis static	int vlan_clone_create(struct if_clone *, char *, size_t);
143f889d2efSBrooks Davis static	int vlan_clone_destroy(struct if_clone *, struct ifnet *);
144f889d2efSBrooks Davis 
145c6e6ca3eSYaroslav Tykhiy static	struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
146c6e6ca3eSYaroslav Tykhiy     IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
1479d4fe4b2SBrooks Davis 
148f731f104SBill Paul /*
149f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
150f731f104SBill Paul  * programming the multicast filter of the parent. This has the
151f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
152f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
153f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
154f731f104SBill Paul  * to avoid this: there really is only one physical interface.
15529c2dfbeSBruce M Simpson  *
15629c2dfbeSBruce M Simpson  * XXX: There is a possible race here if more than one thread is
15729c2dfbeSBruce M Simpson  *      modifying the multicast state of the vlan interface at the same time.
158f731f104SBill Paul  */
1592b120974SPeter Wemm static int
1602b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
161f731f104SBill Paul {
162f731f104SBill Paul 	struct ifnet		*ifp_p;
163f731f104SBill Paul 	struct ifmultiaddr	*ifma, *rifma = NULL;
164f731f104SBill Paul 	struct ifvlan		*sc;
165f731f104SBill Paul 	struct vlan_mc_entry	*mc = NULL;
166f731f104SBill Paul 	struct sockaddr_dl	sdl;
167f731f104SBill Paul 	int			error;
168f731f104SBill Paul 
16929c2dfbeSBruce M Simpson 	/*VLAN_LOCK_ASSERT();*/
17029c2dfbeSBruce M Simpson 
171f731f104SBill Paul 	/* Find the parent. */
172f731f104SBill Paul 	sc = ifp->if_softc;
173f731f104SBill Paul 	ifp_p = sc->ifv_p;
174f731f104SBill Paul 
1751b2a4f7aSBill Fenner 	/*
1761b2a4f7aSBill Fenner 	 * If we don't have a parent, just remember the membership for
1771b2a4f7aSBill Fenner 	 * when we do.
1781b2a4f7aSBill Fenner 	 */
1791b2a4f7aSBill Fenner 	if (ifp_p == NULL)
1801b2a4f7aSBill Fenner 		return (0);
1811b2a4f7aSBill Fenner 
18215a66c21SBruce M Simpson 	bzero((char *)&sdl, sizeof(sdl));
18315a66c21SBruce M Simpson 	sdl.sdl_len = sizeof(sdl);
184f731f104SBill Paul 	sdl.sdl_family = AF_LINK;
18526e30963SBill Fenner 	sdl.sdl_index = ifp_p->if_index;
18626e30963SBill Fenner 	sdl.sdl_type = IFT_ETHER;
18724993214SYaroslav Tykhiy 	sdl.sdl_alen = ETHER_ADDR_LEN;
188f731f104SBill Paul 
189f731f104SBill Paul 	/* First, remove any existing filter entries. */
19022f29826SPoul-Henning Kamp 	while (SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
19122f29826SPoul-Henning Kamp 		mc = SLIST_FIRST(&sc->vlan_mc_listhead);
192f731f104SBill Paul 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
193f731f104SBill Paul 		error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
194f731f104SBill Paul 		if (error)
195f731f104SBill Paul 			return (error);
196f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
1979d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
198f731f104SBill Paul 	}
199f731f104SBill Paul 
200f731f104SBill Paul 	/* Now program new ones. */
2016817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
202f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
203f731f104SBill Paul 			continue;
20429c2dfbeSBruce M Simpson 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
20529c2dfbeSBruce M Simpson 		if (mc == NULL)
20629c2dfbeSBruce M Simpson 			return (ENOMEM);
207f731f104SBill Paul 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
208f731f104SBill Paul 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
209f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
21026e30963SBill Fenner 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
21126e30963SBill Fenner 		    LLADDR(&sdl), ETHER_ADDR_LEN);
212f731f104SBill Paul 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
213f731f104SBill Paul 		if (error)
214f731f104SBill Paul 			return (error);
215f731f104SBill Paul 	}
216f731f104SBill Paul 
217f731f104SBill Paul 	return (0);
218f731f104SBill Paul }
2192cc2df49SGarrett Wollman 
220a3814acfSSam Leffler /*
221a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
222a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
223a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
224a3814acfSSam Leffler  * set here.  Noone else in the system should be aware of this so
225a3814acfSSam Leffler  * we use an explicit reference here.
226a3814acfSSam Leffler  *
227a3814acfSSam Leffler  * NB: Noone should ever need to check if vlan_input_p is null or
228a3814acfSSam Leffler  *     not.  This is because interfaces have a count of the number
229a3814acfSSam Leffler  *     of active vlans (if_nvlans) and this should never be bumped
230a3814acfSSam Leffler  *     except by vlan_config--which is in this module so therefore
231a3814acfSSam Leffler  *     the module must be loaded and vlan_input_p must be non-NULL.
232a3814acfSSam Leffler  */
233a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
234a3814acfSSam Leffler 
235984be3efSGleb Smirnoff /* For if_link_state_change() eyes only... */
236127d7b2dSAndre Oppermann extern	void (*vlan_link_state_p)(struct ifnet *, int);
237127d7b2dSAndre Oppermann 
2382b120974SPeter Wemm static int
2392b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
2402b120974SPeter Wemm {
2419d4fe4b2SBrooks Davis 
2422b120974SPeter Wemm 	switch (type) {
2432b120974SPeter Wemm 	case MOD_LOAD:
2449d4fe4b2SBrooks Davis 		LIST_INIT(&ifv_list);
2454faedfe8SSam Leffler 		VLAN_LOCK_INIT();
2469d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
247127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
2489d4fe4b2SBrooks Davis 		if_clone_attach(&vlan_cloner);
2492b120974SPeter Wemm 		break;
2502b120974SPeter Wemm 	case MOD_UNLOAD:
2519d4fe4b2SBrooks Davis 		if_clone_detach(&vlan_cloner);
2529d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
253127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
2549d4fe4b2SBrooks Davis 		while (!LIST_EMPTY(&ifv_list))
255f889d2efSBrooks Davis 			vlan_clone_destroy(&vlan_cloner,
256fc74a9f9SBrooks Davis 			    LIST_FIRST(&ifv_list)->ifv_ifp);
2574faedfe8SSam Leffler 		VLAN_LOCK_DESTROY();
2589d4fe4b2SBrooks Davis 		break;
2593e019deaSPoul-Henning Kamp 	default:
2603e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
2612b120974SPeter Wemm 	}
26215a66c21SBruce M Simpson 	return (0);
2632b120974SPeter Wemm }
2642b120974SPeter Wemm 
2652b120974SPeter Wemm static moduledata_t vlan_mod = {
2662b120974SPeter Wemm 	"if_vlan",
2672b120974SPeter Wemm 	vlan_modevent,
2682b120974SPeter Wemm 	0
2692b120974SPeter Wemm };
2702b120974SPeter Wemm 
2712b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
272d35bcd3bSRuslan Ermilov MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
2732cc2df49SGarrett Wollman 
274f889d2efSBrooks Davis static struct ifnet *
275f889d2efSBrooks Davis vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
2769d4fe4b2SBrooks Davis {
277f889d2efSBrooks Davis 	const char *cp;
278f889d2efSBrooks Davis 	struct ifnet *ifp;
27915a66c21SBruce M Simpson 	int t = 0;
280f889d2efSBrooks Davis 
281f889d2efSBrooks Davis 	/* Check for <etherif>.<vlan> style interface names. */
282f889d2efSBrooks Davis 	IFNET_RLOCK();
283f889d2efSBrooks Davis 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
284f889d2efSBrooks Davis 		if (ifp->if_type != IFT_ETHER)
285f889d2efSBrooks Davis 			continue;
286f889d2efSBrooks Davis 		if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
287f889d2efSBrooks Davis 			continue;
288f889d2efSBrooks Davis 		cp = name + strlen(ifp->if_xname);
289f889d2efSBrooks Davis 		if (*cp != '.')
290f889d2efSBrooks Davis 			continue;
291f889d2efSBrooks Davis 		for(; *cp != '\0'; cp++) {
292f889d2efSBrooks Davis 			if (*cp < '0' || *cp > '9')
293f889d2efSBrooks Davis 				continue;
294f889d2efSBrooks Davis 			t = (t * 10) + (*cp - '0');
295f889d2efSBrooks Davis 		}
296f889d2efSBrooks Davis 		if (tag != NULL)
297f889d2efSBrooks Davis 			*tag = t;
298f889d2efSBrooks Davis 		break;
299f889d2efSBrooks Davis 	}
300f889d2efSBrooks Davis 	IFNET_RUNLOCK();
301f889d2efSBrooks Davis 
30215a66c21SBruce M Simpson 	return (ifp);
303f889d2efSBrooks Davis }
304f889d2efSBrooks Davis 
305f889d2efSBrooks Davis static int
306f889d2efSBrooks Davis vlan_clone_match(struct if_clone *ifc, const char *name)
307f889d2efSBrooks Davis {
308f889d2efSBrooks Davis 	const char *cp;
309f889d2efSBrooks Davis 
310f889d2efSBrooks Davis 	if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
311f889d2efSBrooks Davis 		return (1);
312f889d2efSBrooks Davis 
313f889d2efSBrooks Davis 	if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
314f889d2efSBrooks Davis 		return (0);
315f889d2efSBrooks Davis 	for (cp = name + 4; *cp != '\0'; cp++) {
316f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
317f889d2efSBrooks Davis 			return (0);
318f889d2efSBrooks Davis 	}
319f889d2efSBrooks Davis 
320f889d2efSBrooks Davis 	return (1);
321f889d2efSBrooks Davis }
322f889d2efSBrooks Davis 
323f889d2efSBrooks Davis static int
324f889d2efSBrooks Davis vlan_clone_create(struct if_clone *ifc, char *name, size_t len)
325f889d2efSBrooks Davis {
326f889d2efSBrooks Davis 	char *dp;
327f889d2efSBrooks Davis 	int wildcard;
328f889d2efSBrooks Davis 	int unit;
329f889d2efSBrooks Davis 	int error;
330f889d2efSBrooks Davis 	int tag;
331f889d2efSBrooks Davis 	int ethertag;
3329d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
3339d4fe4b2SBrooks Davis 	struct ifnet *ifp;
334f889d2efSBrooks Davis 	struct ifnet *p;
335fc74a9f9SBrooks Davis 	u_char eaddr[6] = {0,0,0,0,0,0};
336f889d2efSBrooks Davis 
337f889d2efSBrooks Davis 	if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
338f889d2efSBrooks Davis 		ethertag = 1;
339f889d2efSBrooks Davis 		unit = -1;
340f889d2efSBrooks Davis 		wildcard = 0;
341f889d2efSBrooks Davis 
342f889d2efSBrooks Davis 		/*
343f889d2efSBrooks Davis 		 * Don't let the caller set up a VLAN tag with
344f889d2efSBrooks Davis 		 * anything except VLID bits.
345f889d2efSBrooks Davis 		 */
34615a66c21SBruce M Simpson 		if (tag & ~EVL_VLID_MASK)
347f889d2efSBrooks Davis 			return (EINVAL);
348f889d2efSBrooks Davis 	} else {
349f889d2efSBrooks Davis 		ethertag = 0;
350f889d2efSBrooks Davis 
351f889d2efSBrooks Davis 		error = ifc_name2unit(name, &unit);
352f889d2efSBrooks Davis 		if (error != 0)
353f889d2efSBrooks Davis 			return (error);
354f889d2efSBrooks Davis 
355f889d2efSBrooks Davis 		wildcard = (unit < 0);
356f889d2efSBrooks Davis 	}
357f889d2efSBrooks Davis 
358f889d2efSBrooks Davis 	error = ifc_alloc_unit(ifc, &unit);
359f889d2efSBrooks Davis 	if (error != 0)
360f889d2efSBrooks Davis 		return (error);
361f889d2efSBrooks Davis 
362f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
363f889d2efSBrooks Davis 	if (wildcard) {
364f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
365f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
366f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
367f889d2efSBrooks Davis 			panic("%s: interface name too long", __func__);
368f889d2efSBrooks Davis 		}
369f889d2efSBrooks Davis 	}
3709d4fe4b2SBrooks Davis 
371a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
372fc74a9f9SBrooks Davis 	ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
373fc74a9f9SBrooks Davis 	if (ifp == NULL) {
374fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
375fc74a9f9SBrooks Davis 		free(ifv, M_VLAN);
376fc74a9f9SBrooks Davis 		return (ENOSPC);
377fc74a9f9SBrooks Davis 	}
3789d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
3799d4fe4b2SBrooks Davis 
3809d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
381f889d2efSBrooks Davis 	/*
382cab574d8SYaroslav Tykhiy 	 * Set the name manually rather than using if_initname because
383f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
384f889d2efSBrooks Davis 	 */
385f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
386f889d2efSBrooks Davis 	ifp->if_dname = ifc->ifc_name;
387f889d2efSBrooks Davis 	ifp->if_dunit = unit;
3889d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
3899d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
39015a66c21SBruce M Simpson 	ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
3919d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
3929d4fe4b2SBrooks Davis 
3939d4fe4b2SBrooks Davis 	ifp->if_init = vlan_ifinit;
3949d4fe4b2SBrooks Davis 	ifp->if_start = vlan_start;
3959d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
3969d4fe4b2SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
397fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
3989d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
399211f625aSBill Fenner 	ifp->if_baudrate = 0;
400a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
401a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
4029d4fe4b2SBrooks Davis 
4034faedfe8SSam Leffler 	VLAN_LOCK();
4044faedfe8SSam Leffler 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
4054faedfe8SSam Leffler 	VLAN_UNLOCK();
4064faedfe8SSam Leffler 
407f889d2efSBrooks Davis 	if (ethertag) {
408f889d2efSBrooks Davis 		VLAN_LOCK();
409f889d2efSBrooks Davis 		error = vlan_config(ifv, p);
410f889d2efSBrooks Davis 		if (error != 0) {
411f889d2efSBrooks Davis 			/*
412f889d2efSBrooks Davis 			 * Since we've partialy failed, we need to back
413f889d2efSBrooks Davis 			 * out all the way, otherwise userland could get
414f889d2efSBrooks Davis 			 * confused.  Thus, we destroy the interface.
415f889d2efSBrooks Davis 			 */
416f889d2efSBrooks Davis 			LIST_REMOVE(ifv, ifv_list);
417f889d2efSBrooks Davis 			vlan_unconfig(ifp);
418f889d2efSBrooks Davis 			VLAN_UNLOCK();
419f889d2efSBrooks Davis 			ether_ifdetach(ifp);
420fc74a9f9SBrooks Davis 			if_free_type(ifp, IFT_ETHER);
421f889d2efSBrooks Davis 			free(ifv, M_VLAN);
422f889d2efSBrooks Davis 
423f889d2efSBrooks Davis 			return (error);
424f889d2efSBrooks Davis 		}
425f889d2efSBrooks Davis 		ifv->ifv_tag = tag;
42613f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
427f889d2efSBrooks Davis 		VLAN_UNLOCK();
428f889d2efSBrooks Davis 
4291cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
4301cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
431f889d2efSBrooks Davis 	}
432f889d2efSBrooks Davis 
4339d4fe4b2SBrooks Davis 	return (0);
4349d4fe4b2SBrooks Davis }
4359d4fe4b2SBrooks Davis 
436f889d2efSBrooks Davis static int
437f889d2efSBrooks Davis vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
4389d4fe4b2SBrooks Davis {
439b4e9f837SBrooks Davis 	int unit;
4409d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
4419d4fe4b2SBrooks Davis 
442b4e9f837SBrooks Davis 	unit = ifp->if_dunit;
443b4e9f837SBrooks Davis 
4444faedfe8SSam Leffler 	VLAN_LOCK();
4459d4fe4b2SBrooks Davis 	LIST_REMOVE(ifv, ifv_list);
4469d4fe4b2SBrooks Davis 	vlan_unconfig(ifp);
4474faedfe8SSam Leffler 	VLAN_UNLOCK();
4489d4fe4b2SBrooks Davis 
449a3814acfSSam Leffler 	ether_ifdetach(ifp);
450f3447eb4SBrooks Davis 	if_free_type(ifp, IFT_ETHER);
4519d4fe4b2SBrooks Davis 
4529d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
453f889d2efSBrooks Davis 
454b4e9f837SBrooks Davis 	ifc_free_unit(ifc, unit);
455b4e9f837SBrooks Davis 
456f889d2efSBrooks Davis 	return (0);
4579d4fe4b2SBrooks Davis }
4589d4fe4b2SBrooks Davis 
45915a66c21SBruce M Simpson /*
46015a66c21SBruce M Simpson  * The ifp->if_init entry point for vlan(4) is a no-op.
46115a66c21SBruce M Simpson  */
4622cc2df49SGarrett Wollman static void
4632cc2df49SGarrett Wollman vlan_ifinit(void *foo)
4642cc2df49SGarrett Wollman {
46515a66c21SBruce M Simpson 
4662cc2df49SGarrett Wollman }
4672cc2df49SGarrett Wollman 
4682cc2df49SGarrett Wollman static void
4692cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp)
4702cc2df49SGarrett Wollman {
4712cc2df49SGarrett Wollman 	struct ifvlan *ifv;
4722cc2df49SGarrett Wollman 	struct ifnet *p;
4732cc2df49SGarrett Wollman 	struct ether_vlan_header *evl;
4745326b23cSMatthew N. Dodd 	struct mbuf *m;
475affc907dSMax Laier 	int error;
4762cc2df49SGarrett Wollman 
4772cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
4782cc2df49SGarrett Wollman 	p = ifv->ifv_p;
4792cc2df49SGarrett Wollman 
48013f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4812cc2df49SGarrett Wollman 	for (;;) {
4822cc2df49SGarrett Wollman 		IF_DEQUEUE(&ifp->if_snd, m);
4832cc2df49SGarrett Wollman 		if (m == 0)
4842cc2df49SGarrett Wollman 			break;
485a3814acfSSam Leffler 		BPF_MTAP(ifp, m);
4862cc2df49SGarrett Wollman 
487f731f104SBill Paul 		/*
48824993214SYaroslav Tykhiy 		 * Do not run parent's if_start() if the parent is not up,
48924993214SYaroslav Tykhiy 		 * or parent's driver will cause a system crash.
49024993214SYaroslav Tykhiy 		 */
49113f4c340SRobert Watson 		if (!((p->if_flags & IFF_UP) &&
49213f4c340SRobert Watson 		    (p->if_drv_flags & IFF_DRV_RUNNING))) {
49324993214SYaroslav Tykhiy 			m_freem(m);
494a3814acfSSam Leffler 			ifp->if_collisions++;
49524993214SYaroslav Tykhiy 			continue;
49624993214SYaroslav Tykhiy 		}
49724993214SYaroslav Tykhiy 
49824993214SYaroslav Tykhiy 		/*
499a3814acfSSam Leffler 		 * If underlying interface can do VLAN tag insertion itself,
500a3814acfSSam Leffler 		 * just pass the packet along. However, we need some way to
501a3814acfSSam Leffler 		 * tell the interface where the packet came from so that it
502a3814acfSSam Leffler 		 * knows how to find the VLAN tag to use, so we attach a
503a3814acfSSam Leffler 		 * packet tag that holds it.
504f731f104SBill Paul 		 */
505b08347a0SYaroslav Tykhiy 		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
5065016de40SSam Leffler 			struct m_tag *mtag = m_tag_alloc(MTAG_VLAN,
5075016de40SSam Leffler 							 MTAG_VLAN_TAG,
5085016de40SSam Leffler 							 sizeof(u_int),
5095016de40SSam Leffler 							 M_NOWAIT);
510a3814acfSSam Leffler 			if (mtag == NULL) {
511a3814acfSSam Leffler 				ifp->if_oerrors++;
512a3814acfSSam Leffler 				m_freem(m);
513a3814acfSSam Leffler 				continue;
514a3814acfSSam Leffler 			}
515eefbcf0eSYaroslav Tykhiy 			VLAN_TAG_VALUE(mtag) = ifv->ifv_tag;
516a3814acfSSam Leffler 			m_tag_prepend(m, mtag);
517ba26134bSGleb Smirnoff 			m->m_flags |= M_VLANTAG;
518f731f104SBill Paul 		} else {
519a163d034SWarner Losh 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
5204af90a4dSMatthew N. Dodd 			if (m == NULL) {
5216cbd3e99SYaroslav Tykhiy 				if_printf(ifp,
5226cbd3e99SYaroslav Tykhiy 				    "unable to prepend VLAN header\n");
52326f9b263SRuslan Ermilov 				ifp->if_oerrors++;
5242cc2df49SGarrett Wollman 				continue;
5254af90a4dSMatthew N. Dodd 			}
5262cc2df49SGarrett Wollman 			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
5272cc2df49SGarrett Wollman 
528a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl)) {
529a3814acfSSam Leffler 				m = m_pullup(m, sizeof(*evl));
5305326b23cSMatthew N. Dodd 				if (m == NULL) {
531a3814acfSSam Leffler 					if_printf(ifp,
5326cbd3e99SYaroslav Tykhiy 					    "cannot pullup VLAN header\n");
53326f9b263SRuslan Ermilov 					ifp->if_oerrors++;
5344af90a4dSMatthew N. Dodd 					continue;
5354af90a4dSMatthew N. Dodd 				}
536a3814acfSSam Leffler 			}
5374af90a4dSMatthew N. Dodd 
5382cc2df49SGarrett Wollman 			/*
5392cc2df49SGarrett Wollman 			 * Transform the Ethernet header into an Ethernet header
5402cc2df49SGarrett Wollman 			 * with 802.1Q encapsulation.
5412cc2df49SGarrett Wollman 			 */
542a3814acfSSam Leffler 			bcopy(mtod(m, char *) + ifv->ifv_encaplen,
543797f247bSMatthew N. Dodd 			      mtod(m, char *), ETHER_HDR_LEN);
5442cc2df49SGarrett Wollman 			evl = mtod(m, struct ether_vlan_header *);
5452cc2df49SGarrett Wollman 			evl->evl_proto = evl->evl_encap_proto;
5469d4fe4b2SBrooks Davis 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
5472cc2df49SGarrett Wollman 			evl->evl_tag = htons(ifv->ifv_tag);
548f731f104SBill Paul #ifdef DEBUG
54983908c65SRuslan Ermilov 			printf("%s: %*D\n", __func__, (int)sizeof(*evl),
55083908c65SRuslan Ermilov 			    (unsigned char *)evl, ":");
551f731f104SBill Paul #endif
552f731f104SBill Paul 		}
5532cc2df49SGarrett Wollman 
5542cc2df49SGarrett Wollman 		/*
5552cc2df49SGarrett Wollman 		 * Send it, precisely as ether_output() would have.
5562cc2df49SGarrett Wollman 		 * We are already running at splimp.
5572cc2df49SGarrett Wollman 		 */
558affc907dSMax Laier 		IFQ_HANDOFF(p, m, error);
559affc907dSMax Laier 		if (!error)
560f731f104SBill Paul 			ifp->if_opackets++;
561df5e1987SJonathan Lemon 		else
562df5e1987SJonathan Lemon 			ifp->if_oerrors++;
5632cc2df49SGarrett Wollman 	}
56413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
565f731f104SBill Paul }
566f731f104SBill Paul 
567a3814acfSSam Leffler static void
568a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
569f731f104SBill Paul {
570a3814acfSSam Leffler 	struct ether_vlan_header *evl;
571f731f104SBill Paul 	struct ifvlan *ifv;
572a3814acfSSam Leffler 	struct m_tag *mtag;
573a3814acfSSam Leffler 	u_int tag;
574a3814acfSSam Leffler 
575f4ec4126SYaroslav Tykhiy 	if (m->m_flags & M_VLANTAG) {
576a3814acfSSam Leffler 		/*
57714e98256SYaroslav Tykhiy 		 * Packet is tagged, but m contains a normal
578a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
579a3814acfSSam Leffler 		 */
580f4ec4126SYaroslav Tykhiy 		mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
581f4ec4126SYaroslav Tykhiy 		KASSERT(mtag != NULL,
582f4ec4126SYaroslav Tykhiy 			("%s: M_VLANTAG without m_tag", __func__));
58326f9b263SRuslan Ermilov 		tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
584a3814acfSSam Leffler 		m_tag_delete(m, mtag);
5856ee20ab5SRuslan Ermilov 		m->m_flags &= ~M_VLANTAG;
586a3814acfSSam Leffler 	} else {
58714e98256SYaroslav Tykhiy 		/*
58814e98256SYaroslav Tykhiy 		 * Packet is tagged in-band as specified by 802.1q.
58914e98256SYaroslav Tykhiy 		 */
590f4ec4126SYaroslav Tykhiy 		mtag = NULL;
591a3814acfSSam Leffler 		switch (ifp->if_type) {
592a3814acfSSam Leffler 		case IFT_ETHER:
593a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl) &&
594a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof(*evl))) == NULL) {
595a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
596a3814acfSSam Leffler 				return;
597a3814acfSSam Leffler 			}
598a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
599a3814acfSSam Leffler 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
600ffdd61c3SYaroslav Tykhiy 				("%s: bad encapsulation protocol (%u)",
601ffdd61c3SYaroslav Tykhiy 				 __func__, ntohs(evl->evl_encap_proto)));
602a3814acfSSam Leffler 
603fb88a3e0SBill Paul 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
604f731f104SBill Paul 
6057a46ec8fSBrooks Davis 			/*
606a3814acfSSam Leffler 			 * Restore the original ethertype.  We'll remove
607a3814acfSSam Leffler 			 * the encapsulation after we've found the vlan
608a3814acfSSam Leffler 			 * interface corresponding to the tag.
6097a46ec8fSBrooks Davis 			 */
610a3814acfSSam Leffler 			evl->evl_encap_proto = evl->evl_proto;
611a3814acfSSam Leffler 			break;
612a3814acfSSam Leffler 		default:
613a3814acfSSam Leffler 			tag = (u_int) -1;
614ffdd61c3SYaroslav Tykhiy #ifdef INVARIANTS
615ffdd61c3SYaroslav Tykhiy 			panic("%s: unsupported if_type (%u)",
616ffdd61c3SYaroslav Tykhiy 			      __func__, ifp->if_type);
617a3814acfSSam Leffler #endif
618a3814acfSSam Leffler 			break;
619a3814acfSSam Leffler 		}
6207a46ec8fSBrooks Davis 	}
6217a46ec8fSBrooks Davis 
6224faedfe8SSam Leffler 	VLAN_LOCK();
6234faedfe8SSam Leffler 	LIST_FOREACH(ifv, &ifv_list, ifv_list)
624a3814acfSSam Leffler 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
625f731f104SBill Paul 			break;
626f731f104SBill Paul 
627fc74a9f9SBrooks Davis 	if (ifv == NULL || (ifv->ifv_ifp->if_flags & IFF_UP) == 0) {
6284faedfe8SSam Leffler 		VLAN_UNLOCK();
6297d3e4c6eSLuigi Rizzo 		m_freem(m);
630a3814acfSSam Leffler 		ifp->if_noproto++;
631b46f884bSJoerg Wunsch #ifdef DEBUG
632ffdd61c3SYaroslav Tykhiy 		printf("%s: tag %d, no interface\n", __func__, tag);
633b46f884bSJoerg Wunsch #endif
634a3814acfSSam Leffler 		return;
635f731f104SBill Paul 	}
6364faedfe8SSam Leffler 	VLAN_UNLOCK();		/* XXX extend below? */
637b46f884bSJoerg Wunsch #ifdef DEBUG
638ffdd61c3SYaroslav Tykhiy 	printf("%s: tag %d, parent %s\n", __func__, tag, ifv->ifv_p->if_xname);
639b46f884bSJoerg Wunsch #endif
640f731f104SBill Paul 
641a3814acfSSam Leffler 	if (mtag == NULL) {
642f731f104SBill Paul 		/*
643a3814acfSSam Leffler 		 * Packet had an in-line encapsulation header;
644a3814acfSSam Leffler 		 * remove it.  The original header has already
645a3814acfSSam Leffler 		 * been fixed up above.
646f731f104SBill Paul 		 */
647a3814acfSSam Leffler 		bcopy(mtod(m, caddr_t),
648a3814acfSSam Leffler 		      mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
649797f247bSMatthew N. Dodd 		      ETHER_HDR_LEN);
650a3814acfSSam Leffler 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
651a3814acfSSam Leffler 	}
652a3814acfSSam Leffler 
653fc74a9f9SBrooks Davis 	m->m_pkthdr.rcvif = ifv->ifv_ifp;
654fc74a9f9SBrooks Davis 	ifv->ifv_ifp->if_ipackets++;
6552cc2df49SGarrett Wollman 
656a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
657fc74a9f9SBrooks Davis 	(*ifp->if_input)(ifv->ifv_ifp, m);
6582cc2df49SGarrett Wollman }
6592cc2df49SGarrett Wollman 
6602cc2df49SGarrett Wollman static int
6612cc2df49SGarrett Wollman vlan_config(struct ifvlan *ifv, struct ifnet *p)
6622cc2df49SGarrett Wollman {
6632cc2df49SGarrett Wollman 	struct ifaddr *ifa1, *ifa2;
6641cf236fbSYaroslav Tykhiy 	struct ifnet *ifp;
6652cc2df49SGarrett Wollman 	struct sockaddr_dl *sdl1, *sdl2;
6662cc2df49SGarrett Wollman 
6674faedfe8SSam Leffler 	VLAN_LOCK_ASSERT();
6684faedfe8SSam Leffler 
6691cf236fbSYaroslav Tykhiy 	if (p->if_type != IFT_ETHER)
67015a66c21SBruce M Simpson 		return (EPROTONOSUPPORT);
6712cc2df49SGarrett Wollman 	if (ifv->ifv_p)
67215a66c21SBruce M Simpson 		return (EBUSY);
6732cc2df49SGarrett Wollman 
674a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
675a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
6761cf236fbSYaroslav Tykhiy 	ifv->ifv_pflags = 0;
677a3814acfSSam Leffler 
678a3814acfSSam Leffler 	/*
679d6fcfb7aSYaroslav Tykhiy 	 * The active VLAN counter on the parent is used
680d6fcfb7aSYaroslav Tykhiy 	 * at various places to see if there is a vlan(4)
681d6fcfb7aSYaroslav Tykhiy 	 * attached to this physical interface.
682d6fcfb7aSYaroslav Tykhiy 	 */
683d6fcfb7aSYaroslav Tykhiy 	p->if_nvlans++;
684d6fcfb7aSYaroslav Tykhiy 
685d6fcfb7aSYaroslav Tykhiy 	/*
686a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
687a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
688656acce4SYaroslav Tykhiy 	 * use it.
689a3814acfSSam Leffler 	 */
690656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
691656acce4SYaroslav Tykhiy 		/*
692656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
693656acce4SYaroslav Tykhiy 		 * handle extended frames.
694656acce4SYaroslav Tykhiy 		 */
695a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
696656acce4SYaroslav Tykhiy 	} else {
697a3814acfSSam Leffler 		/*
698a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
699a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
700a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
701a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
702a3814acfSSam Leffler 		 * which might still be useful.
703a3814acfSSam Leffler 		 */
704a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
705a3814acfSSam Leffler 	}
706a3814acfSSam Leffler 
707a3814acfSSam Leffler 	ifv->ifv_p = p;
7081cf236fbSYaroslav Tykhiy 	ifp = ifv->ifv_ifp;
7091cf236fbSYaroslav Tykhiy 	ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
7102cc2df49SGarrett Wollman 	/*
71124993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
71224993214SYaroslav Tykhiy 	 * Other flags are none of our business.
7132cc2df49SGarrett Wollman 	 */
7141cf236fbSYaroslav Tykhiy #define VLAN_COPY_FLAGS \
7151cf236fbSYaroslav Tykhiy     (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT)
7161cf236fbSYaroslav Tykhiy 	ifp->if_flags &= ~VLAN_COPY_FLAGS;
7171cf236fbSYaroslav Tykhiy 	ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
7181cf236fbSYaroslav Tykhiy #undef VLAN_COPY_FLAGS
7191cf236fbSYaroslav Tykhiy 
7201cf236fbSYaroslav Tykhiy 	ifp->if_link_state = p->if_link_state;
7212cc2df49SGarrett Wollman 
722b08347a0SYaroslav Tykhiy #if 0
723b08347a0SYaroslav Tykhiy 	/*
724b08347a0SYaroslav Tykhiy 	 * Not ready yet.  We need notification from the parent
725b08347a0SYaroslav Tykhiy 	 * when hw checksumming flags in its if_capenable change.
726b08347a0SYaroslav Tykhiy 	 * Flags set in if_capabilities only are useless.
727b08347a0SYaroslav Tykhiy 	 */
7282cc2df49SGarrett Wollman 	/*
729a3814acfSSam Leffler 	 * If the parent interface can do hardware-assisted
730a3814acfSSam Leffler 	 * VLAN encapsulation, then propagate its hardware-
731a3814acfSSam Leffler 	 * assisted checksumming flags.
732a3814acfSSam Leffler 	 */
733a3814acfSSam Leffler 	if (p->if_capabilities & IFCAP_VLAN_HWTAGGING)
7341cf236fbSYaroslav Tykhiy 		ifp->if_capabilities |= p->if_capabilities & IFCAP_HWCSUM;
735b08347a0SYaroslav Tykhiy #endif
736a3814acfSSam Leffler 
737a3814acfSSam Leffler 	/*
7382cc2df49SGarrett Wollman 	 * Set up our ``Ethernet address'' to reflect the underlying
7392cc2df49SGarrett Wollman 	 * physical interface's.
7402cc2df49SGarrett Wollman 	 */
7411cf236fbSYaroslav Tykhiy 	ifa1 = ifaddr_byindex(ifp->if_index);
742f9132cebSJonathan Lemon 	ifa2 = ifaddr_byindex(p->if_index);
7432cc2df49SGarrett Wollman 	sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
7442cc2df49SGarrett Wollman 	sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
7452cc2df49SGarrett Wollman 	sdl1->sdl_type = IFT_ETHER;
7462cc2df49SGarrett Wollman 	sdl1->sdl_alen = ETHER_ADDR_LEN;
7472cc2df49SGarrett Wollman 	bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
7481cf236fbSYaroslav Tykhiy 	bcopy(LLADDR(sdl2), IFP2ENADDR(ifp), ETHER_ADDR_LEN);
7491b2a4f7aSBill Fenner 
7501b2a4f7aSBill Fenner 	/*
7511b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
7521b2a4f7aSBill Fenner 	 * joined on the vlan device.
7531b2a4f7aSBill Fenner 	 */
7541cf236fbSYaroslav Tykhiy 	(void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
7551b2a4f7aSBill Fenner 
75615a66c21SBruce M Simpson 	return (0);
7572cc2df49SGarrett Wollman }
7582cc2df49SGarrett Wollman 
7592cc2df49SGarrett Wollman static int
760f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
761f731f104SBill Paul {
762f731f104SBill Paul 	struct ifaddr *ifa;
763f731f104SBill Paul 	struct sockaddr_dl *sdl;
764f731f104SBill Paul 	struct vlan_mc_entry *mc;
765f731f104SBill Paul 	struct ifvlan *ifv;
766f731f104SBill Paul 	struct ifnet *p;
767f731f104SBill Paul 	int error;
768f731f104SBill Paul 
7694faedfe8SSam Leffler 	VLAN_LOCK_ASSERT();
7704faedfe8SSam Leffler 
771f731f104SBill Paul 	ifv = ifp->if_softc;
772f731f104SBill Paul 	p = ifv->ifv_p;
773f731f104SBill Paul 
7741b2a4f7aSBill Fenner 	if (p) {
7751b2a4f7aSBill Fenner 		struct sockaddr_dl sdl;
7761b2a4f7aSBill Fenner 
777f731f104SBill Paul 		/*
778f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
779f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
7801b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
781f731f104SBill Paul 		 */
78215a66c21SBruce M Simpson 		bzero((char *)&sdl, sizeof(sdl));
78315a66c21SBruce M Simpson 		sdl.sdl_len = sizeof(sdl);
784f731f104SBill Paul 		sdl.sdl_family = AF_LINK;
7851b2a4f7aSBill Fenner 		sdl.sdl_index = p->if_index;
7861b2a4f7aSBill Fenner 		sdl.sdl_type = IFT_ETHER;
7871b2a4f7aSBill Fenner 		sdl.sdl_alen = ETHER_ADDR_LEN;
7881b2a4f7aSBill Fenner 
7891b2a4f7aSBill Fenner 		while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
79022f29826SPoul-Henning Kamp 			mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
79115a66c21SBruce M Simpson 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
79215a66c21SBruce M Simpson 			    ETHER_ADDR_LEN);
793f731f104SBill Paul 			error = if_delmulti(p, (struct sockaddr *)&sdl);
794f731f104SBill Paul 			if (error)
795f731f104SBill Paul 				return (error);
796f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
7979d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
798f731f104SBill Paul 		}
799a3814acfSSam Leffler 
8001cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 0); /* clear special flags on parent */
801a3814acfSSam Leffler 		p->if_nvlans--;
8021b2a4f7aSBill Fenner 	}
803f731f104SBill Paul 
804f731f104SBill Paul 	/* Disconnect from parent. */
8051cf236fbSYaroslav Tykhiy 	if (ifv->ifv_pflags)
8061cf236fbSYaroslav Tykhiy 		if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
807f731f104SBill Paul 	ifv->ifv_p = NULL;
808fc74a9f9SBrooks Davis 	ifv->ifv_ifp->if_mtu = ETHERMTU;		/* XXX why not 0? */
809fc74a9f9SBrooks Davis 	ifv->ifv_ifp->if_link_state = LINK_STATE_UNKNOWN;
810f731f104SBill Paul 
811f731f104SBill Paul 	/* Clear our MAC address. */
812fc74a9f9SBrooks Davis 	ifa = ifaddr_byindex(ifv->ifv_ifp->if_index);
813f731f104SBill Paul 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
814f731f104SBill Paul 	sdl->sdl_type = IFT_ETHER;
815f731f104SBill Paul 	sdl->sdl_alen = ETHER_ADDR_LEN;
816f731f104SBill Paul 	bzero(LLADDR(sdl), ETHER_ADDR_LEN);
817fc74a9f9SBrooks Davis 	bzero(IFP2ENADDR(ifv->ifv_ifp), ETHER_ADDR_LEN);
818f731f104SBill Paul 
81915a66c21SBruce M Simpson 	return (0);
820f731f104SBill Paul }
821f731f104SBill Paul 
8221cf236fbSYaroslav Tykhiy /* Handle a reference counted flag that should be set on the parent as well */
823f731f104SBill Paul static int
8241cf236fbSYaroslav Tykhiy vlan_setflag(struct ifnet *ifp, int flag, int status,
8251cf236fbSYaroslav Tykhiy 	     int (*func)(struct ifnet *, int))
826a3814acfSSam Leffler {
8271cf236fbSYaroslav Tykhiy 	struct ifvlan *ifv;
8281cf236fbSYaroslav Tykhiy 	int error;
829a3814acfSSam Leffler 
8301cf236fbSYaroslav Tykhiy 	/* XXX VLAN_LOCK_ASSERT(); */
831a3814acfSSam Leffler 
8321cf236fbSYaroslav Tykhiy 	ifv = ifp->if_softc;
8331cf236fbSYaroslav Tykhiy 	status = status ? (ifp->if_flags & flag) : 0;
8341cf236fbSYaroslav Tykhiy 	/* Now "status" contains the flag value or 0 */
8351cf236fbSYaroslav Tykhiy 
8361cf236fbSYaroslav Tykhiy 	/*
8371cf236fbSYaroslav Tykhiy 	 * See if recorded parent's status is different from what
8381cf236fbSYaroslav Tykhiy 	 * we want it to be.  If it is, flip it.  We record parent's
8391cf236fbSYaroslav Tykhiy 	 * status in ifv_pflags so that we won't clear parent's flag
8401cf236fbSYaroslav Tykhiy 	 * we haven't set.  In fact, we don't clear or set parent's
8411cf236fbSYaroslav Tykhiy 	 * flags directly, but get or release references to them.
8421cf236fbSYaroslav Tykhiy 	 * That's why we can be sure that recorded flags still are
8431cf236fbSYaroslav Tykhiy 	 * in accord with actual parent's flags.
8441cf236fbSYaroslav Tykhiy 	 */
8451cf236fbSYaroslav Tykhiy 	if (status != (ifv->ifv_pflags & flag)) {
8461cf236fbSYaroslav Tykhiy 		error = (*func)(ifv->ifv_p, status);
8471cf236fbSYaroslav Tykhiy 		if (error)
848a3814acfSSam Leffler 			return (error);
8491cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags &= ~flag;
8501cf236fbSYaroslav Tykhiy 		ifv->ifv_pflags |= status;
8511cf236fbSYaroslav Tykhiy 	}
8521cf236fbSYaroslav Tykhiy 	return (0);
8531cf236fbSYaroslav Tykhiy }
8541cf236fbSYaroslav Tykhiy 
8551cf236fbSYaroslav Tykhiy /*
8561cf236fbSYaroslav Tykhiy  * Handle IFF_* flags that require certain changes on the parent:
8571cf236fbSYaroslav Tykhiy  * if "status" is true, update parent's flags respective to our if_flags;
8581cf236fbSYaroslav Tykhiy  * if "status" is false, forcedly clear the flags set on parent.
8591cf236fbSYaroslav Tykhiy  */
8601cf236fbSYaroslav Tykhiy static int
8611cf236fbSYaroslav Tykhiy vlan_setflags(struct ifnet *ifp, int status)
8621cf236fbSYaroslav Tykhiy {
8631cf236fbSYaroslav Tykhiy 	int error, i;
8641cf236fbSYaroslav Tykhiy 
8651cf236fbSYaroslav Tykhiy 	for (i = 0; vlan_pflags[i].flag; i++) {
8661cf236fbSYaroslav Tykhiy 		error = vlan_setflag(ifp, vlan_pflags[i].flag,
8671cf236fbSYaroslav Tykhiy 				     status, vlan_pflags[i].func);
8681cf236fbSYaroslav Tykhiy 		if (error)
8691cf236fbSYaroslav Tykhiy 			return (error);
8701cf236fbSYaroslav Tykhiy 	}
8711cf236fbSYaroslav Tykhiy 	return (0);
872a3814acfSSam Leffler }
873a3814acfSSam Leffler 
874127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
875127d7b2dSAndre Oppermann static void
876127d7b2dSAndre Oppermann vlan_link_state(struct ifnet *ifp, int link)
877127d7b2dSAndre Oppermann {
878127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
879127d7b2dSAndre Oppermann 
880127d7b2dSAndre Oppermann 	VLAN_LOCK();
881127d7b2dSAndre Oppermann 	LIST_FOREACH(ifv, &ifv_list, ifv_list) {
882984be3efSGleb Smirnoff 		if (ifv->ifv_p == ifp)
883fc74a9f9SBrooks Davis 			if_link_state_change(ifv->ifv_ifp,
884984be3efSGleb Smirnoff 			    ifv->ifv_p->if_link_state);
885127d7b2dSAndre Oppermann 	}
886127d7b2dSAndre Oppermann 	VLAN_UNLOCK();
887127d7b2dSAndre Oppermann }
888127d7b2dSAndre Oppermann 
889a3814acfSSam Leffler static int
890cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
8912cc2df49SGarrett Wollman {
8922cc2df49SGarrett Wollman 	struct ifaddr *ifa;
8932cc2df49SGarrett Wollman 	struct ifnet *p;
8942cc2df49SGarrett Wollman 	struct ifreq *ifr;
8952cc2df49SGarrett Wollman 	struct ifvlan *ifv;
8962cc2df49SGarrett Wollman 	struct vlanreq vlr;
8972cc2df49SGarrett Wollman 	int error = 0;
8982cc2df49SGarrett Wollman 
8992cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
9002cc2df49SGarrett Wollman 	ifa = (struct ifaddr *)data;
9012cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
9022cc2df49SGarrett Wollman 
9032cc2df49SGarrett Wollman 	switch (cmd) {
9042cc2df49SGarrett Wollman 	case SIOCSIFADDR:
9052cc2df49SGarrett Wollman 		ifp->if_flags |= IFF_UP;
9062cc2df49SGarrett Wollman 
9072cc2df49SGarrett Wollman 		switch (ifa->ifa_addr->sa_family) {
9082cc2df49SGarrett Wollman #ifdef INET
9092cc2df49SGarrett Wollman 		case AF_INET:
910fc74a9f9SBrooks Davis 			arp_ifinit(ifv->ifv_ifp, ifa);
9112cc2df49SGarrett Wollman 			break;
9122cc2df49SGarrett Wollman #endif
9132cc2df49SGarrett Wollman 		default:
9142cc2df49SGarrett Wollman 			break;
9152cc2df49SGarrett Wollman 		}
9162cc2df49SGarrett Wollman 		break;
9172cc2df49SGarrett Wollman 
9182cc2df49SGarrett Wollman 	case SIOCGIFADDR:
9192cc2df49SGarrett Wollman 		{
9202cc2df49SGarrett Wollman 			struct sockaddr *sa;
9212cc2df49SGarrett Wollman 
9222cc2df49SGarrett Wollman 			sa = (struct sockaddr *) &ifr->ifr_data;
923fc74a9f9SBrooks Davis 			bcopy(IFP2ENADDR(ifp), (caddr_t)sa->sa_data,
92415a66c21SBruce M Simpson 			    ETHER_ADDR_LEN);
9252cc2df49SGarrett Wollman 		}
9262cc2df49SGarrett Wollman 		break;
9272cc2df49SGarrett Wollman 
928b3cca108SBill Fenner 	case SIOCGIFMEDIA:
9294faedfe8SSam Leffler 		VLAN_LOCK();
930b3cca108SBill Fenner 		if (ifv->ifv_p != NULL) {
9314faedfe8SSam Leffler 			error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
9324faedfe8SSam Leffler 					SIOCGIFMEDIA, data);
9334faedfe8SSam Leffler 			VLAN_UNLOCK();
934b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
935b3cca108SBill Fenner 			if (error == 0) {
936b3cca108SBill Fenner 				struct ifmediareq *ifmr;
937b3cca108SBill Fenner 
938b3cca108SBill Fenner 				ifmr = (struct ifmediareq *)data;
939b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
940b3cca108SBill Fenner 					ifmr->ifm_count = 1;
941b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
942b3cca108SBill Fenner 						ifmr->ifm_ulist,
943b3cca108SBill Fenner 						sizeof(int));
944b3cca108SBill Fenner 				}
945b3cca108SBill Fenner 			}
9464faedfe8SSam Leffler 		} else {
9474faedfe8SSam Leffler 			VLAN_UNLOCK();
948b3cca108SBill Fenner 			error = EINVAL;
9494faedfe8SSam Leffler 		}
950b3cca108SBill Fenner 		break;
951b3cca108SBill Fenner 
952b3cca108SBill Fenner 	case SIOCSIFMEDIA:
953b3cca108SBill Fenner 		error = EINVAL;
954b3cca108SBill Fenner 		break;
955b3cca108SBill Fenner 
9562cc2df49SGarrett Wollman 	case SIOCSIFMTU:
9572cc2df49SGarrett Wollman 		/*
9582cc2df49SGarrett Wollman 		 * Set the interface MTU.
9592cc2df49SGarrett Wollman 		 */
9604faedfe8SSam Leffler 		VLAN_LOCK();
961a3814acfSSam Leffler 		if (ifv->ifv_p != NULL) {
962a3814acfSSam Leffler 			if (ifr->ifr_mtu >
963a3814acfSSam Leffler 			     (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
964a3814acfSSam Leffler 			    ifr->ifr_mtu <
965a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
9662cc2df49SGarrett Wollman 				error = EINVAL;
967a3814acfSSam Leffler 			else
9682cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
969a3814acfSSam Leffler 		} else
970a3814acfSSam Leffler 			error = EINVAL;
9714faedfe8SSam Leffler 		VLAN_UNLOCK();
9722cc2df49SGarrett Wollman 		break;
9732cc2df49SGarrett Wollman 
9742cc2df49SGarrett Wollman 	case SIOCSETVLAN:
97515a66c21SBruce M Simpson 		error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
9762cc2df49SGarrett Wollman 		if (error)
9772cc2df49SGarrett Wollman 			break;
9782cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
9794faedfe8SSam Leffler 			VLAN_LOCK();
980f731f104SBill Paul 			vlan_unconfig(ifp);
9814faedfe8SSam Leffler 			if (ifp->if_flags & IFF_UP)
9822cc2df49SGarrett Wollman 				if_down(ifp);
98313f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
9844faedfe8SSam Leffler 			VLAN_UNLOCK();
9852cc2df49SGarrett Wollman 			break;
9862cc2df49SGarrett Wollman 		}
9872cc2df49SGarrett Wollman 		p = ifunit(vlr.vlr_parent);
9882cc2df49SGarrett Wollman 		if (p == 0) {
9892cc2df49SGarrett Wollman 			error = ENOENT;
9902cc2df49SGarrett Wollman 			break;
9912cc2df49SGarrett Wollman 		}
992fb88a3e0SBill Paul 		/*
993fb88a3e0SBill Paul 		 * Don't let the caller set up a VLAN tag with
994fb88a3e0SBill Paul 		 * anything except VLID bits.
995fb88a3e0SBill Paul 		 */
996fb88a3e0SBill Paul 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
997fb88a3e0SBill Paul 			error = EINVAL;
998fb88a3e0SBill Paul 			break;
999fb88a3e0SBill Paul 		}
10004faedfe8SSam Leffler 		VLAN_LOCK();
10012cc2df49SGarrett Wollman 		error = vlan_config(ifv, p);
10024faedfe8SSam Leffler 		if (error) {
10034faedfe8SSam Leffler 			VLAN_UNLOCK();
10042cc2df49SGarrett Wollman 			break;
10054faedfe8SSam Leffler 		}
10062cc2df49SGarrett Wollman 		ifv->ifv_tag = vlr.vlr_tag;
100713f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
10084faedfe8SSam Leffler 		VLAN_UNLOCK();
1009a3814acfSSam Leffler 
10101cf236fbSYaroslav Tykhiy 		/* Update flags on the parent, if necessary. */
10111cf236fbSYaroslav Tykhiy 		vlan_setflags(ifp, 1);
10122cc2df49SGarrett Wollman 		break;
10132cc2df49SGarrett Wollman 
10142cc2df49SGarrett Wollman 	case SIOCGETVLAN:
101515a66c21SBruce M Simpson 		bzero(&vlr, sizeof(vlr));
10164faedfe8SSam Leffler 		VLAN_LOCK();
10172cc2df49SGarrett Wollman 		if (ifv->ifv_p) {
10189bf40edeSBrooks Davis 			strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
10199bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
10202cc2df49SGarrett Wollman 			vlr.vlr_tag = ifv->ifv_tag;
10212cc2df49SGarrett Wollman 		}
10224faedfe8SSam Leffler 		VLAN_UNLOCK();
102315a66c21SBruce M Simpson 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
10242cc2df49SGarrett Wollman 		break;
10252cc2df49SGarrett Wollman 
10262cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
10272cc2df49SGarrett Wollman 		/*
10281cf236fbSYaroslav Tykhiy 		 * We should propagate selected flags to the parent,
10291cf236fbSYaroslav Tykhiy 		 * e.g., promiscuous mode.
10302cc2df49SGarrett Wollman 		 */
1031a3814acfSSam Leffler 		if (ifv->ifv_p != NULL)
10321cf236fbSYaroslav Tykhiy 			error = vlan_setflags(ifp, 1);
10332cc2df49SGarrett Wollman 		break;
1034a3814acfSSam Leffler 
1035f731f104SBill Paul 	case SIOCADDMULTI:
1036f731f104SBill Paul 	case SIOCDELMULTI:
103729c2dfbeSBruce M Simpson 		/*VLAN_LOCK();*/
1038f731f104SBill Paul 		error = vlan_setmulti(ifp);
103929c2dfbeSBruce M Simpson 		/*VLAN_UNLOCK();*/
1040f731f104SBill Paul 		break;
10412cc2df49SGarrett Wollman 	default:
10422cc2df49SGarrett Wollman 		error = EINVAL;
10432cc2df49SGarrett Wollman 	}
104415a66c21SBruce M Simpson 
104515a66c21SBruce M Simpson 	return (error);
10462cc2df49SGarrett Wollman }
1047