xref: /freebsd/sys/net/if_vlan.c (revision 656acce4f4908a6a397d9b76c97c6798c9a1d9c1)
12cc2df49SGarrett Wollman /*
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>
602cc2df49SGarrett Wollman #include <net/if_arp.h>
612cc2df49SGarrett Wollman #include <net/if_dl.h>
622cc2df49SGarrett Wollman #include <net/if_types.h>
632cc2df49SGarrett Wollman #include <net/if_vlan_var.h>
64e6d95d51SScott Long #include <net/route.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 {
79a3814acfSSam Leffler 	struct	arpcom ifv_ac;	/* make this an interface */
80a3814acfSSam Leffler 	struct	ifnet *ifv_p;	/* parent inteface of this vlan */
81a3814acfSSam Leffler 	struct	ifv_linkmib {
82a3814acfSSam Leffler 		int	ifvm_parent;
83a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
84a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
85a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
86a3814acfSSam Leffler 		u_int16_t ifvm_proto; /* encapsulation ethertype */
87a3814acfSSam Leffler 		u_int16_t ifvm_tag; /* tag to apply on packets leaving if */
88a3814acfSSam Leffler 	}	ifv_mib;
89a3814acfSSam Leffler 	SLIST_HEAD(__vlan_mchead, vlan_mc_entry)	vlan_mc_listhead;
90a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
91a3814acfSSam Leffler 	int	ifv_flags;
92a3814acfSSam Leffler };
93a3814acfSSam Leffler #define	ifv_if	ifv_ac.ac_if
94a3814acfSSam Leffler #define	ifv_tag	ifv_mib.ifvm_tag
95a3814acfSSam Leffler #define	ifv_encaplen	ifv_mib.ifvm_encaplen
96a3814acfSSam Leffler #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
97a3814acfSSam Leffler #define	ifv_mintu	ifv_mib.ifvm_mintu
98a3814acfSSam Leffler 
99a3814acfSSam Leffler #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
100a3814acfSSam Leffler 
1014a408dcbSBill Paul SYSCTL_DECL(_net_link);
102d2a75853SBill Fenner SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
1032cc2df49SGarrett Wollman SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
1042cc2df49SGarrett Wollman 
1055e17543aSBrooks Davis static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
1069d4fe4b2SBrooks Davis static LIST_HEAD(, ifvlan) ifv_list;
1072cc2df49SGarrett Wollman 
1084faedfe8SSam Leffler /*
1094faedfe8SSam Leffler  * Locking: one lock is used to guard both the ifv_list and modification
1104faedfe8SSam Leffler  * to vlan data structures.  We are rather conservative here; probably
1114faedfe8SSam Leffler  * more than necessary.
1124faedfe8SSam Leffler  */
1134faedfe8SSam Leffler static struct mtx ifv_mtx;
1145e17543aSBrooks Davis #define	VLAN_LOCK_INIT()	mtx_init(&ifv_mtx, VLANNAME, NULL, MTX_DEF)
1154faedfe8SSam Leffler #define	VLAN_LOCK_DESTROY()	mtx_destroy(&ifv_mtx)
1164faedfe8SSam Leffler #define	VLAN_LOCK_ASSERT()	mtx_assert(&ifv_mtx, MA_OWNED)
1174faedfe8SSam Leffler #define	VLAN_LOCK()	mtx_lock(&ifv_mtx)
1184faedfe8SSam Leffler #define	VLAN_UNLOCK()	mtx_unlock(&ifv_mtx)
1194faedfe8SSam Leffler 
1203b16e7b2SMaxime Henrion static	int vlan_clone_create(struct if_clone *, int);
121ae5a19beSBrooks Davis static	void vlan_clone_destroy(struct ifnet *);
1222cc2df49SGarrett Wollman static	void vlan_start(struct ifnet *ifp);
1232cc2df49SGarrett Wollman static	void vlan_ifinit(void *foo);
124a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
125cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
126f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
127f731f104SBill Paul static	int vlan_unconfig(struct ifnet *ifp);
128f731f104SBill Paul static	int vlan_config(struct ifvlan *ifv, struct ifnet *p);
129127d7b2dSAndre Oppermann static	void vlan_link_state(struct ifnet *ifp, int link);
130f731f104SBill Paul 
1315e17543aSBrooks Davis struct if_clone vlan_cloner = IF_CLONE_INITIALIZER(VLANNAME,
132ae5a19beSBrooks Davis     vlan_clone_create, vlan_clone_destroy, 0, IF_MAXUNIT);
1339d4fe4b2SBrooks Davis 
134f731f104SBill Paul /*
135f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
136f731f104SBill Paul  * programming the multicast filter of the parent. This has the
137f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
138f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
139f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
140f731f104SBill Paul  * to avoid this: there really is only one physical interface.
141f731f104SBill Paul  */
1422b120974SPeter Wemm static int
1432b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
144f731f104SBill Paul {
145f731f104SBill Paul 	struct ifnet		*ifp_p;
146f731f104SBill Paul 	struct ifmultiaddr	*ifma, *rifma = NULL;
147f731f104SBill Paul 	struct ifvlan		*sc;
148f731f104SBill Paul 	struct vlan_mc_entry	*mc = NULL;
149f731f104SBill Paul 	struct sockaddr_dl	sdl;
150f731f104SBill Paul 	int			error;
151f731f104SBill Paul 
152f731f104SBill Paul 	/* Find the parent. */
153f731f104SBill Paul 	sc = ifp->if_softc;
154f731f104SBill Paul 	ifp_p = sc->ifv_p;
155f731f104SBill Paul 
1561b2a4f7aSBill Fenner 	/*
1571b2a4f7aSBill Fenner 	 * If we don't have a parent, just remember the membership for
1581b2a4f7aSBill Fenner 	 * when we do.
1591b2a4f7aSBill Fenner 	 */
1601b2a4f7aSBill Fenner 	if (ifp_p == NULL)
1611b2a4f7aSBill Fenner 		return(0);
1621b2a4f7aSBill Fenner 
16324993214SYaroslav Tykhiy 	bzero((char *)&sdl, sizeof sdl);
16424993214SYaroslav Tykhiy 	sdl.sdl_len = sizeof sdl;
165f731f104SBill Paul 	sdl.sdl_family = AF_LINK;
16626e30963SBill Fenner 	sdl.sdl_index = ifp_p->if_index;
16726e30963SBill Fenner 	sdl.sdl_type = IFT_ETHER;
16824993214SYaroslav Tykhiy 	sdl.sdl_alen = ETHER_ADDR_LEN;
169f731f104SBill Paul 
170f731f104SBill Paul 	/* First, remove any existing filter entries. */
17122f29826SPoul-Henning Kamp 	while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
17222f29826SPoul-Henning Kamp 		mc = SLIST_FIRST(&sc->vlan_mc_listhead);
173f731f104SBill Paul 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
174f731f104SBill Paul 		error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
175f731f104SBill Paul 		if (error)
176f731f104SBill Paul 			return(error);
177f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
1789d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
179f731f104SBill Paul 	}
180f731f104SBill Paul 
181f731f104SBill Paul 	/* Now program new ones. */
1826817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
183f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
184f731f104SBill Paul 			continue;
185a163d034SWarner Losh 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
186f731f104SBill Paul 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
187f731f104SBill Paul 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
188f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
18926e30963SBill Fenner 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
19026e30963SBill Fenner 		    LLADDR(&sdl), ETHER_ADDR_LEN);
191f731f104SBill Paul 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
192f731f104SBill Paul 		if (error)
193f731f104SBill Paul 			return(error);
194f731f104SBill Paul 	}
195f731f104SBill Paul 
196f731f104SBill Paul 	return(0);
197f731f104SBill Paul }
1982cc2df49SGarrett Wollman 
199a3814acfSSam Leffler /*
200a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
201a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
202a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
203a3814acfSSam Leffler  * set here.  Noone else in the system should be aware of this so
204a3814acfSSam Leffler  * we use an explicit reference here.
205a3814acfSSam Leffler  *
206a3814acfSSam Leffler  * NB: Noone should ever need to check if vlan_input_p is null or
207a3814acfSSam Leffler  *     not.  This is because interfaces have a count of the number
208a3814acfSSam Leffler  *     of active vlans (if_nvlans) and this should never be bumped
209a3814acfSSam Leffler  *     except by vlan_config--which is in this module so therefore
210a3814acfSSam Leffler  *     the module must be loaded and vlan_input_p must be non-NULL.
211a3814acfSSam Leffler  */
212a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
213a3814acfSSam Leffler 
214127d7b2dSAndre Oppermann /* For MII eyes only... */
215127d7b2dSAndre Oppermann extern	void (*vlan_link_state_p)(struct ifnet *, int);
216127d7b2dSAndre Oppermann 
2172b120974SPeter Wemm static int
2182b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
2192b120974SPeter Wemm {
2209d4fe4b2SBrooks Davis 
2212b120974SPeter Wemm 	switch (type) {
2222b120974SPeter Wemm 	case MOD_LOAD:
2239d4fe4b2SBrooks Davis 		LIST_INIT(&ifv_list);
2244faedfe8SSam Leffler 		VLAN_LOCK_INIT();
2259d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
226127d7b2dSAndre Oppermann 		vlan_link_state_p = vlan_link_state;
2279d4fe4b2SBrooks Davis 		if_clone_attach(&vlan_cloner);
2282b120974SPeter Wemm 		break;
2292b120974SPeter Wemm 	case MOD_UNLOAD:
2309d4fe4b2SBrooks Davis 		if_clone_detach(&vlan_cloner);
2319d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
232127d7b2dSAndre Oppermann 		vlan_link_state_p = NULL;
2339d4fe4b2SBrooks Davis 		while (!LIST_EMPTY(&ifv_list))
2349d4fe4b2SBrooks Davis 			vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
2354faedfe8SSam Leffler 		VLAN_LOCK_DESTROY();
2369d4fe4b2SBrooks Davis 		break;
2372b120974SPeter Wemm 	}
2382b120974SPeter Wemm 	return 0;
2392b120974SPeter Wemm }
2402b120974SPeter Wemm 
2412b120974SPeter Wemm static moduledata_t vlan_mod = {
2422b120974SPeter Wemm 	"if_vlan",
2432b120974SPeter Wemm 	vlan_modevent,
2442b120974SPeter Wemm 	0
2452b120974SPeter Wemm };
2462b120974SPeter Wemm 
2472b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
248d35bcd3bSRuslan Ermilov MODULE_DEPEND(if_vlan, miibus, 1, 1, 1);
2492cc2df49SGarrett Wollman 
2509d4fe4b2SBrooks Davis static int
2513b16e7b2SMaxime Henrion vlan_clone_create(struct if_clone *ifc, int unit)
2529d4fe4b2SBrooks Davis {
2539d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
2549d4fe4b2SBrooks Davis 	struct ifnet *ifp;
2559d4fe4b2SBrooks Davis 
256a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
2579d4fe4b2SBrooks Davis 	ifp = &ifv->ifv_if;
2589d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
2599d4fe4b2SBrooks Davis 
2609d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
2619bf40edeSBrooks Davis 	if_initname(ifp, ifc->ifc_name, unit);
2629d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
2639d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
2649d4fe4b2SBrooks Davis 	ifp->if_linkmiblen = sizeof ifv->ifv_mib;
2659d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
2669d4fe4b2SBrooks Davis 
2679d4fe4b2SBrooks Davis 	ifp->if_init = vlan_ifinit;
2689d4fe4b2SBrooks Davis 	ifp->if_start = vlan_start;
2699d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
2709d4fe4b2SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
271a3814acfSSam Leffler 	ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr);
2729d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
273211f625aSBill Fenner 	ifp->if_baudrate = 0;
274a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
275a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
2769d4fe4b2SBrooks Davis 
2774faedfe8SSam Leffler 	VLAN_LOCK();
2784faedfe8SSam Leffler 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
2794faedfe8SSam Leffler 	VLAN_UNLOCK();
2804faedfe8SSam Leffler 
2819d4fe4b2SBrooks Davis 	return (0);
2829d4fe4b2SBrooks Davis }
2839d4fe4b2SBrooks Davis 
284ae5a19beSBrooks Davis static void
2859d4fe4b2SBrooks Davis vlan_clone_destroy(struct ifnet *ifp)
2869d4fe4b2SBrooks Davis {
2879d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
2889d4fe4b2SBrooks Davis 
2894faedfe8SSam Leffler 	VLAN_LOCK();
2909d4fe4b2SBrooks Davis 	LIST_REMOVE(ifv, ifv_list);
2919d4fe4b2SBrooks Davis 	vlan_unconfig(ifp);
2924faedfe8SSam Leffler 	VLAN_UNLOCK();
2939d4fe4b2SBrooks Davis 
294a3814acfSSam Leffler 	ether_ifdetach(ifp);
2959d4fe4b2SBrooks Davis 
2969d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
2979d4fe4b2SBrooks Davis }
2989d4fe4b2SBrooks Davis 
2992cc2df49SGarrett Wollman static void
3002cc2df49SGarrett Wollman vlan_ifinit(void *foo)
3012cc2df49SGarrett Wollman {
302f731f104SBill Paul 	return;
3032cc2df49SGarrett Wollman }
3042cc2df49SGarrett Wollman 
3052cc2df49SGarrett Wollman static void
3062cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp)
3072cc2df49SGarrett Wollman {
3082cc2df49SGarrett Wollman 	struct ifvlan *ifv;
3092cc2df49SGarrett Wollman 	struct ifnet *p;
3102cc2df49SGarrett Wollman 	struct ether_vlan_header *evl;
3115326b23cSMatthew N. Dodd 	struct mbuf *m;
3122cc2df49SGarrett Wollman 
3132cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
3142cc2df49SGarrett Wollman 	p = ifv->ifv_p;
3152cc2df49SGarrett Wollman 
3162cc2df49SGarrett Wollman 	ifp->if_flags |= IFF_OACTIVE;
3172cc2df49SGarrett Wollman 	for (;;) {
3182cc2df49SGarrett Wollman 		IF_DEQUEUE(&ifp->if_snd, m);
3192cc2df49SGarrett Wollman 		if (m == 0)
3202cc2df49SGarrett Wollman 			break;
321a3814acfSSam Leffler 		BPF_MTAP(ifp, m);
3222cc2df49SGarrett Wollman 
323f731f104SBill Paul 		/*
32424993214SYaroslav Tykhiy 		 * Do not run parent's if_start() if the parent is not up,
32524993214SYaroslav Tykhiy 		 * or parent's driver will cause a system crash.
32624993214SYaroslav Tykhiy 		 */
32724993214SYaroslav Tykhiy 		if ((p->if_flags & (IFF_UP | IFF_RUNNING)) !=
32824993214SYaroslav Tykhiy 					(IFF_UP | IFF_RUNNING)) {
32924993214SYaroslav Tykhiy 			m_freem(m);
330a3814acfSSam Leffler 			ifp->if_collisions++;
33124993214SYaroslav Tykhiy 			continue;
33224993214SYaroslav Tykhiy 		}
33324993214SYaroslav Tykhiy 
33424993214SYaroslav Tykhiy 		/*
335a3814acfSSam Leffler 		 * If underlying interface can do VLAN tag insertion itself,
336a3814acfSSam Leffler 		 * just pass the packet along. However, we need some way to
337a3814acfSSam Leffler 		 * tell the interface where the packet came from so that it
338a3814acfSSam Leffler 		 * knows how to find the VLAN tag to use, so we attach a
339a3814acfSSam Leffler 		 * packet tag that holds it.
340f731f104SBill Paul 		 */
341b08347a0SYaroslav Tykhiy 		if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
3425016de40SSam Leffler 			struct m_tag *mtag = m_tag_alloc(MTAG_VLAN,
3435016de40SSam Leffler 							 MTAG_VLAN_TAG,
3445016de40SSam Leffler 							 sizeof (u_int),
3455016de40SSam Leffler 							 M_NOWAIT);
346a3814acfSSam Leffler 			if (mtag == NULL) {
347a3814acfSSam Leffler 				ifp->if_oerrors++;
348a3814acfSSam Leffler 				m_freem(m);
349a3814acfSSam Leffler 				continue;
350a3814acfSSam Leffler 			}
351a3814acfSSam Leffler 			*(u_int*)(mtag+1) = ifv->ifv_tag;
352a3814acfSSam Leffler 			m_tag_prepend(m, mtag);
353f731f104SBill Paul 		} else {
354a163d034SWarner Losh 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
3554af90a4dSMatthew N. Dodd 			if (m == NULL) {
356a3814acfSSam Leffler 				if_printf(ifp, "unable to prepend VLAN header");
35726f9b263SRuslan Ermilov 				ifp->if_oerrors++;
3582cc2df49SGarrett Wollman 				continue;
3594af90a4dSMatthew N. Dodd 			}
3602cc2df49SGarrett Wollman 			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
3612cc2df49SGarrett Wollman 
362a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl)) {
363a3814acfSSam Leffler 				m = m_pullup(m, sizeof(*evl));
3645326b23cSMatthew N. Dodd 				if (m == NULL) {
365a3814acfSSam Leffler 					if_printf(ifp,
366a3814acfSSam Leffler 					    "cannot pullup VLAN header");
36726f9b263SRuslan Ermilov 					ifp->if_oerrors++;
3684af90a4dSMatthew N. Dodd 					continue;
3694af90a4dSMatthew N. Dodd 				}
370a3814acfSSam Leffler 			}
3714af90a4dSMatthew N. Dodd 
3722cc2df49SGarrett Wollman 			/*
3732cc2df49SGarrett Wollman 			 * Transform the Ethernet header into an Ethernet header
3742cc2df49SGarrett Wollman 			 * with 802.1Q encapsulation.
3752cc2df49SGarrett Wollman 			 */
376a3814acfSSam Leffler 			bcopy(mtod(m, char *) + ifv->ifv_encaplen,
377797f247bSMatthew N. Dodd 			      mtod(m, char *), ETHER_HDR_LEN);
3782cc2df49SGarrett Wollman 			evl = mtod(m, struct ether_vlan_header *);
3792cc2df49SGarrett Wollman 			evl->evl_proto = evl->evl_encap_proto;
3809d4fe4b2SBrooks Davis 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
3812cc2df49SGarrett Wollman 			evl->evl_tag = htons(ifv->ifv_tag);
382f731f104SBill Paul #ifdef DEBUG
383a9283398SJohn Baldwin 			printf("vlan_start: %*D\n", (int)sizeof *evl,
38465f1bb65SPeter Wemm 			    (unsigned char *)evl, ":");
385f731f104SBill Paul #endif
386f731f104SBill Paul 		}
3872cc2df49SGarrett Wollman 
3882cc2df49SGarrett Wollman 		/*
3892cc2df49SGarrett Wollman 		 * Send it, precisely as ether_output() would have.
3902cc2df49SGarrett Wollman 		 * We are already running at splimp.
3912cc2df49SGarrett Wollman 		 */
392df5e1987SJonathan Lemon 		if (IF_HANDOFF(&p->if_snd, m, p))
393f731f104SBill Paul 			ifp->if_opackets++;
394df5e1987SJonathan Lemon 		else
395df5e1987SJonathan Lemon 			ifp->if_oerrors++;
3962cc2df49SGarrett Wollman 	}
3972cc2df49SGarrett Wollman 	ifp->if_flags &= ~IFF_OACTIVE;
398f731f104SBill Paul 
399f731f104SBill Paul 	return;
400f731f104SBill Paul }
401f731f104SBill Paul 
402a3814acfSSam Leffler static void
403a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
404f731f104SBill Paul {
405a3814acfSSam Leffler 	struct ether_vlan_header *evl;
406f731f104SBill Paul 	struct ifvlan *ifv;
407a3814acfSSam Leffler 	struct m_tag *mtag;
408a3814acfSSam Leffler 	u_int tag;
409a3814acfSSam Leffler 
410a3814acfSSam Leffler 	mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
411a3814acfSSam Leffler 	if (mtag != NULL) {
412a3814acfSSam Leffler 		/*
413a3814acfSSam Leffler 		 * Packet is tagged, m contains a normal
414a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
415a3814acfSSam Leffler 		 */
41626f9b263SRuslan Ermilov 		tag = EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag));
417a3814acfSSam Leffler 		m_tag_delete(m, mtag);
418a3814acfSSam Leffler 	} else {
419a3814acfSSam Leffler 		switch (ifp->if_type) {
420a3814acfSSam Leffler 		case IFT_ETHER:
421a3814acfSSam Leffler 			if (m->m_len < sizeof (*evl) &&
422a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof (*evl))) == NULL) {
423a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
424a3814acfSSam Leffler 				return;
425a3814acfSSam Leffler 			}
426a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
427a3814acfSSam Leffler 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
428a3814acfSSam Leffler 				("vlan_input: bad encapsulated protocols (%u)",
429a3814acfSSam Leffler 				 ntohs(evl->evl_encap_proto)));
430a3814acfSSam Leffler 
431fb88a3e0SBill Paul 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
432f731f104SBill Paul 
4337a46ec8fSBrooks Davis 			/*
434a3814acfSSam Leffler 			 * Restore the original ethertype.  We'll remove
435a3814acfSSam Leffler 			 * the encapsulation after we've found the vlan
436a3814acfSSam Leffler 			 * interface corresponding to the tag.
4377a46ec8fSBrooks Davis 			 */
438a3814acfSSam Leffler 			evl->evl_encap_proto = evl->evl_proto;
439a3814acfSSam Leffler 			break;
440a3814acfSSam Leffler 		default:
441a3814acfSSam Leffler 			tag = (u_int) -1;
442a3814acfSSam Leffler #ifdef DIAGNOSTIC
443a3814acfSSam Leffler 			panic("vlan_input: unsupported if type %u", ifp->if_type);
444a3814acfSSam Leffler #endif
445a3814acfSSam Leffler 			break;
446a3814acfSSam Leffler 		}
4477a46ec8fSBrooks Davis 	}
4487a46ec8fSBrooks Davis 
4494faedfe8SSam Leffler 	VLAN_LOCK();
4504faedfe8SSam Leffler 	LIST_FOREACH(ifv, &ifv_list, ifv_list)
451a3814acfSSam Leffler 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
452f731f104SBill Paul 			break;
453f731f104SBill Paul 
454242c766bSBill Fenner 	if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
4554faedfe8SSam Leffler 		VLAN_UNLOCK();
4567d3e4c6eSLuigi Rizzo 		m_freem(m);
457a3814acfSSam Leffler 		ifp->if_noproto++;
458a3814acfSSam Leffler 		return;
459f731f104SBill Paul 	}
4604faedfe8SSam Leffler 	VLAN_UNLOCK();		/* XXX extend below? */
461f731f104SBill Paul 
462a3814acfSSam Leffler 	if (mtag == NULL) {
463f731f104SBill Paul 		/*
464a3814acfSSam Leffler 		 * Packet had an in-line encapsulation header;
465a3814acfSSam Leffler 		 * remove it.  The original header has already
466a3814acfSSam Leffler 		 * been fixed up above.
467f731f104SBill Paul 		 */
468a3814acfSSam Leffler 		bcopy(mtod(m, caddr_t),
469a3814acfSSam Leffler 		      mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
470797f247bSMatthew N. Dodd 		      ETHER_HDR_LEN);
471a3814acfSSam Leffler 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
472a3814acfSSam Leffler 	}
473a3814acfSSam Leffler 
474f731f104SBill Paul 	m->m_pkthdr.rcvif = &ifv->ifv_if;
475f731f104SBill Paul 	ifv->ifv_if.if_ipackets++;
4762cc2df49SGarrett Wollman 
477a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
478a3814acfSSam Leffler 	(*ifp->if_input)(&ifv->ifv_if, m);
4792cc2df49SGarrett Wollman }
4802cc2df49SGarrett Wollman 
4812cc2df49SGarrett Wollman static int
4822cc2df49SGarrett Wollman vlan_config(struct ifvlan *ifv, struct ifnet *p)
4832cc2df49SGarrett Wollman {
4842cc2df49SGarrett Wollman 	struct ifaddr *ifa1, *ifa2;
4852cc2df49SGarrett Wollman 	struct sockaddr_dl *sdl1, *sdl2;
4862cc2df49SGarrett Wollman 
4874faedfe8SSam Leffler 	VLAN_LOCK_ASSERT();
4884faedfe8SSam Leffler 
4892cc2df49SGarrett Wollman 	if (p->if_data.ifi_type != IFT_ETHER)
4902cc2df49SGarrett Wollman 		return EPROTONOSUPPORT;
4912cc2df49SGarrett Wollman 	if (ifv->ifv_p)
4922cc2df49SGarrett Wollman 		return EBUSY;
4932cc2df49SGarrett Wollman 
494a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
495a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
496a3814acfSSam Leffler 	ifv->ifv_flags = 0;
497a3814acfSSam Leffler 
498a3814acfSSam Leffler 	/*
499a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
500a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
501656acce4SYaroslav Tykhiy 	 * use it.
502656acce4SYaroslav Tykhiy 	 * First of all, enable Tx/Rx of such extended frames on the
503656acce4SYaroslav Tykhiy 	 * parent if it's disabled and we're the first to attach.
504a3814acfSSam Leffler 	 */
505a3814acfSSam Leffler 	p->if_nvlans++;
506656acce4SYaroslav Tykhiy 	if (p->if_nvlans == 1 &&
507656acce4SYaroslav Tykhiy 	    (p->if_capabilities & IFCAP_VLAN_MTU) &&
508656acce4SYaroslav Tykhiy 	    (p->if_capenable & IFCAP_VLAN_MTU) == 0) {
509a3814acfSSam Leffler 		struct ifreq ifr;
510a3814acfSSam Leffler 		int error;
511a3814acfSSam Leffler 
512656acce4SYaroslav Tykhiy 		ifr.ifr_reqcap = p->if_capenable | IFCAP_VLAN_MTU;
513656acce4SYaroslav Tykhiy 		error = (*p->if_ioctl)(p, SIOCSIFCAP, (caddr_t) &ifr);
514a3814acfSSam Leffler 		if (error) {
515a3814acfSSam Leffler 			p->if_nvlans--;
516a3814acfSSam Leffler 			return (error);
517a3814acfSSam Leffler 		}
518a3814acfSSam Leffler 	}
519656acce4SYaroslav Tykhiy 	if (p->if_capenable & IFCAP_VLAN_MTU) {
520656acce4SYaroslav Tykhiy 		/*
521656acce4SYaroslav Tykhiy 		 * No need to fudge the MTU since the parent can
522656acce4SYaroslav Tykhiy 		 * handle extended frames.
523656acce4SYaroslav Tykhiy 		 */
524a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
525656acce4SYaroslav Tykhiy 	} else {
526a3814acfSSam Leffler 		/*
527a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
528a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
529a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
530a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
531a3814acfSSam Leffler 		 * which might still be useful.
532a3814acfSSam Leffler 		 */
533a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
534a3814acfSSam Leffler 	}
535a3814acfSSam Leffler 
536a3814acfSSam Leffler 	ifv->ifv_p = p;
537a3814acfSSam Leffler 	ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
5382cc2df49SGarrett Wollman 	/*
53924993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
54024993214SYaroslav Tykhiy 	 * Other flags are none of our business.
5412cc2df49SGarrett Wollman 	 */
54224993214SYaroslav Tykhiy 	ifv->ifv_if.if_flags = (p->if_flags &
54324993214SYaroslav Tykhiy 	    (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
544127d7b2dSAndre Oppermann 	ifv->ifv_if.if_link_state = p->if_link_state;
5452cc2df49SGarrett Wollman 
546b08347a0SYaroslav Tykhiy #if 0
547b08347a0SYaroslav Tykhiy 	/*
548b08347a0SYaroslav Tykhiy 	 * Not ready yet.  We need notification from the parent
549b08347a0SYaroslav Tykhiy 	 * when hw checksumming flags in its if_capenable change.
550b08347a0SYaroslav Tykhiy 	 * Flags set in if_capabilities only are useless.
551b08347a0SYaroslav Tykhiy 	 */
5522cc2df49SGarrett Wollman 	/*
553a3814acfSSam Leffler 	 * If the parent interface can do hardware-assisted
554a3814acfSSam Leffler 	 * VLAN encapsulation, then propagate its hardware-
555a3814acfSSam Leffler 	 * assisted checksumming flags.
556a3814acfSSam Leffler 	 */
557a3814acfSSam Leffler 	if (p->if_capabilities & IFCAP_VLAN_HWTAGGING)
558a3814acfSSam Leffler 		ifv->ifv_if.if_capabilities |= p->if_capabilities & IFCAP_HWCSUM;
559b08347a0SYaroslav Tykhiy #endif
560a3814acfSSam Leffler 
561a3814acfSSam Leffler 	/*
5622cc2df49SGarrett Wollman 	 * Set up our ``Ethernet address'' to reflect the underlying
5632cc2df49SGarrett Wollman 	 * physical interface's.
5642cc2df49SGarrett Wollman 	 */
565f9132cebSJonathan Lemon 	ifa1 = ifaddr_byindex(ifv->ifv_if.if_index);
566f9132cebSJonathan Lemon 	ifa2 = ifaddr_byindex(p->if_index);
5672cc2df49SGarrett Wollman 	sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
5682cc2df49SGarrett Wollman 	sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
5692cc2df49SGarrett Wollman 	sdl1->sdl_type = IFT_ETHER;
5702cc2df49SGarrett Wollman 	sdl1->sdl_alen = ETHER_ADDR_LEN;
5712cc2df49SGarrett Wollman 	bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
5722cc2df49SGarrett Wollman 	bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
5731b2a4f7aSBill Fenner 
5741b2a4f7aSBill Fenner 	/*
5751b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
5761b2a4f7aSBill Fenner 	 * joined on the vlan device.
5771b2a4f7aSBill Fenner 	 */
5781b2a4f7aSBill Fenner 	(void)vlan_setmulti(&ifv->ifv_if);
5791b2a4f7aSBill Fenner 
5802cc2df49SGarrett Wollman 	return 0;
5812cc2df49SGarrett Wollman }
5822cc2df49SGarrett Wollman 
5832cc2df49SGarrett Wollman static int
584f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
585f731f104SBill Paul {
586f731f104SBill Paul 	struct ifaddr *ifa;
587f731f104SBill Paul 	struct sockaddr_dl *sdl;
588f731f104SBill Paul 	struct vlan_mc_entry *mc;
589f731f104SBill Paul 	struct ifvlan *ifv;
590f731f104SBill Paul 	struct ifnet *p;
591f731f104SBill Paul 	int error;
592f731f104SBill Paul 
5934faedfe8SSam Leffler 	VLAN_LOCK_ASSERT();
5944faedfe8SSam Leffler 
595f731f104SBill Paul 	ifv = ifp->if_softc;
596f731f104SBill Paul 	p = ifv->ifv_p;
597f731f104SBill Paul 
5981b2a4f7aSBill Fenner 	if (p) {
5991b2a4f7aSBill Fenner 		struct sockaddr_dl sdl;
6001b2a4f7aSBill Fenner 
601f731f104SBill Paul 		/*
602f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
603f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
6041b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
605f731f104SBill Paul 		 */
6061b2a4f7aSBill Fenner 		bzero((char *)&sdl, sizeof sdl);
6071b2a4f7aSBill Fenner 		sdl.sdl_len = sizeof sdl;
608f731f104SBill Paul 		sdl.sdl_family = AF_LINK;
6091b2a4f7aSBill Fenner 		sdl.sdl_index = p->if_index;
6101b2a4f7aSBill Fenner 		sdl.sdl_type = IFT_ETHER;
6111b2a4f7aSBill Fenner 		sdl.sdl_alen = ETHER_ADDR_LEN;
6121b2a4f7aSBill Fenner 
6131b2a4f7aSBill Fenner 		while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
61422f29826SPoul-Henning Kamp 			mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
615f731f104SBill Paul 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
616f731f104SBill Paul 			error = if_delmulti(p, (struct sockaddr *)&sdl);
617f731f104SBill Paul 			if (error)
618f731f104SBill Paul 				return(error);
619f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
6209d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
621f731f104SBill Paul 		}
622a3814acfSSam Leffler 
623a3814acfSSam Leffler 		p->if_nvlans--;
624a3814acfSSam Leffler 		if (p->if_nvlans == 0) {
625a3814acfSSam Leffler 			struct ifreq ifr;
626a3814acfSSam Leffler 
627656acce4SYaroslav Tykhiy 			/*
628656acce4SYaroslav Tykhiy 			 * Try to disable Tx/Rx of VLAN-sized frames.
629656acce4SYaroslav Tykhiy 			 * This may have no effect for some interfaces,
630656acce4SYaroslav Tykhiy 			 * but only the parent driver knows that.
631656acce4SYaroslav Tykhiy 			 */
632656acce4SYaroslav Tykhiy 			ifr.ifr_reqcap = p->if_capenable & ~IFCAP_VLAN_MTU;
633656acce4SYaroslav Tykhiy 			(*p->if_ioctl)(p, SIOCSIFCAP, (caddr_t) &ifr);
634a3814acfSSam Leffler 		}
6351b2a4f7aSBill Fenner 	}
636f731f104SBill Paul 
637f731f104SBill Paul 	/* Disconnect from parent. */
638f731f104SBill Paul 	ifv->ifv_p = NULL;
639a3814acfSSam Leffler 	ifv->ifv_if.if_mtu = ETHERMTU;		/* XXX why not 0? */
640a3814acfSSam Leffler 	ifv->ifv_flags = 0;
641127d7b2dSAndre Oppermann 	ifv->ifv_if.if_link_state = LINK_STATE_UNKNOWN;
642f731f104SBill Paul 
643f731f104SBill Paul 	/* Clear our MAC address. */
644f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifv->ifv_if.if_index);
645f731f104SBill Paul 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
646f731f104SBill Paul 	sdl->sdl_type = IFT_ETHER;
647f731f104SBill Paul 	sdl->sdl_alen = ETHER_ADDR_LEN;
648f731f104SBill Paul 	bzero(LLADDR(sdl), ETHER_ADDR_LEN);
649f731f104SBill Paul 	bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
650f731f104SBill Paul 
651f731f104SBill Paul 	return 0;
652f731f104SBill Paul }
653f731f104SBill Paul 
654f731f104SBill Paul static int
655a3814acfSSam Leffler vlan_set_promisc(struct ifnet *ifp)
656a3814acfSSam Leffler {
657a3814acfSSam Leffler 	struct ifvlan *ifv = ifp->if_softc;
658a3814acfSSam Leffler 	int error = 0;
659a3814acfSSam Leffler 
660a3814acfSSam Leffler 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
661a3814acfSSam Leffler 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
662a3814acfSSam Leffler 			error = ifpromisc(ifv->ifv_p, 1);
663a3814acfSSam Leffler 			if (error == 0)
664a3814acfSSam Leffler 				ifv->ifv_flags |= IFVF_PROMISC;
665a3814acfSSam Leffler 		}
666a3814acfSSam Leffler 	} else {
667a3814acfSSam Leffler 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
668a3814acfSSam Leffler 			error = ifpromisc(ifv->ifv_p, 0);
669a3814acfSSam Leffler 			if (error == 0)
670a3814acfSSam Leffler 				ifv->ifv_flags &= ~IFVF_PROMISC;
671a3814acfSSam Leffler 		}
672a3814acfSSam Leffler 	}
673a3814acfSSam Leffler 
674a3814acfSSam Leffler 	return (error);
675a3814acfSSam Leffler }
676a3814acfSSam Leffler 
677127d7b2dSAndre Oppermann /* Inform all vlans that their parent has changed link state */
678127d7b2dSAndre Oppermann static void
679127d7b2dSAndre Oppermann vlan_link_state(struct ifnet *ifp, int link)
680127d7b2dSAndre Oppermann {
681127d7b2dSAndre Oppermann 	struct ifvlan *ifv;
682127d7b2dSAndre Oppermann 
683127d7b2dSAndre Oppermann 	VLAN_LOCK();
684127d7b2dSAndre Oppermann 	LIST_FOREACH(ifv, &ifv_list, ifv_list) {
685127d7b2dSAndre Oppermann 		if (ifv->ifv_p == ifp) {
686127d7b2dSAndre Oppermann 			ifv->ifv_if.if_link_state = ifv->ifv_p->if_link_state;
687127d7b2dSAndre Oppermann 			rt_ifmsg(&(ifv->ifv_if));
688127d7b2dSAndre Oppermann 			KNOTE(&ifp->if_klist, link);
689127d7b2dSAndre Oppermann 		}
690127d7b2dSAndre Oppermann 	}
691127d7b2dSAndre Oppermann 	VLAN_UNLOCK();
692127d7b2dSAndre Oppermann }
693127d7b2dSAndre Oppermann 
694a3814acfSSam Leffler static int
695cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
6962cc2df49SGarrett Wollman {
6972cc2df49SGarrett Wollman 	struct ifaddr *ifa;
6982cc2df49SGarrett Wollman 	struct ifnet *p;
6992cc2df49SGarrett Wollman 	struct ifreq *ifr;
7002cc2df49SGarrett Wollman 	struct ifvlan *ifv;
7012cc2df49SGarrett Wollman 	struct vlanreq vlr;
7022cc2df49SGarrett Wollman 	int error = 0;
7032cc2df49SGarrett Wollman 
7042cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
7052cc2df49SGarrett Wollman 	ifa = (struct ifaddr *)data;
7062cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
7072cc2df49SGarrett Wollman 
7082cc2df49SGarrett Wollman 	switch (cmd) {
7092cc2df49SGarrett Wollman 	case SIOCSIFADDR:
7102cc2df49SGarrett Wollman 		ifp->if_flags |= IFF_UP;
7112cc2df49SGarrett Wollman 
7122cc2df49SGarrett Wollman 		switch (ifa->ifa_addr->sa_family) {
7132cc2df49SGarrett Wollman #ifdef INET
7142cc2df49SGarrett Wollman 		case AF_INET:
715322dcb8dSMax Khon 			arp_ifinit(&ifv->ifv_if, ifa);
7162cc2df49SGarrett Wollman 			break;
7172cc2df49SGarrett Wollman #endif
7182cc2df49SGarrett Wollman 		default:
7192cc2df49SGarrett Wollman 			break;
7202cc2df49SGarrett Wollman 		}
7212cc2df49SGarrett Wollman 		break;
7222cc2df49SGarrett Wollman 
7232cc2df49SGarrett Wollman 	case SIOCGIFADDR:
7242cc2df49SGarrett Wollman 		{
7252cc2df49SGarrett Wollman 			struct sockaddr *sa;
7262cc2df49SGarrett Wollman 
7272cc2df49SGarrett Wollman 			sa = (struct sockaddr *) &ifr->ifr_data;
7283fefbff0SLuigi Rizzo 			bcopy(IFP2AC(ifp)->ac_enaddr,
7292cc2df49SGarrett Wollman 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
7302cc2df49SGarrett Wollman 		}
7312cc2df49SGarrett Wollman 		break;
7322cc2df49SGarrett Wollman 
733b3cca108SBill Fenner 	case SIOCGIFMEDIA:
7344faedfe8SSam Leffler 		VLAN_LOCK();
735b3cca108SBill Fenner 		if (ifv->ifv_p != NULL) {
7364faedfe8SSam Leffler 			error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
7374faedfe8SSam Leffler 					SIOCGIFMEDIA, data);
7384faedfe8SSam Leffler 			VLAN_UNLOCK();
739b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
740b3cca108SBill Fenner 			if (error == 0) {
741b3cca108SBill Fenner 				struct ifmediareq *ifmr;
742b3cca108SBill Fenner 
743b3cca108SBill Fenner 				ifmr = (struct ifmediareq *) data;
744b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
745b3cca108SBill Fenner 					ifmr->ifm_count = 1;
746b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
747b3cca108SBill Fenner 						ifmr->ifm_ulist,
748b3cca108SBill Fenner 						sizeof(int));
749b3cca108SBill Fenner 				}
750b3cca108SBill Fenner 			}
7514faedfe8SSam Leffler 		} else {
7524faedfe8SSam Leffler 			VLAN_UNLOCK();
753b3cca108SBill Fenner 			error = EINVAL;
7544faedfe8SSam Leffler 		}
755b3cca108SBill Fenner 		break;
756b3cca108SBill Fenner 
757b3cca108SBill Fenner 	case SIOCSIFMEDIA:
758b3cca108SBill Fenner 		error = EINVAL;
759b3cca108SBill Fenner 		break;
760b3cca108SBill Fenner 
7612cc2df49SGarrett Wollman 	case SIOCSIFMTU:
7622cc2df49SGarrett Wollman 		/*
7632cc2df49SGarrett Wollman 		 * Set the interface MTU.
7642cc2df49SGarrett Wollman 		 */
7654faedfe8SSam Leffler 		VLAN_LOCK();
766a3814acfSSam Leffler 		if (ifv->ifv_p != NULL) {
767a3814acfSSam Leffler 			if (ifr->ifr_mtu >
768a3814acfSSam Leffler 			     (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
769a3814acfSSam Leffler 			    ifr->ifr_mtu <
770a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
7712cc2df49SGarrett Wollman 				error = EINVAL;
772a3814acfSSam Leffler 			else
7732cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
774a3814acfSSam Leffler 		} else
775a3814acfSSam Leffler 			error = EINVAL;
7764faedfe8SSam Leffler 		VLAN_UNLOCK();
7772cc2df49SGarrett Wollman 		break;
7782cc2df49SGarrett Wollman 
7792cc2df49SGarrett Wollman 	case SIOCSETVLAN:
7802cc2df49SGarrett Wollman 		error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
7812cc2df49SGarrett Wollman 		if (error)
7822cc2df49SGarrett Wollman 			break;
7832cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
7844faedfe8SSam Leffler 			VLAN_LOCK();
785f731f104SBill Paul 			vlan_unconfig(ifp);
7864faedfe8SSam Leffler 			if (ifp->if_flags & IFF_UP)
7872cc2df49SGarrett Wollman 				if_down(ifp);
78824993214SYaroslav Tykhiy 			ifp->if_flags &= ~IFF_RUNNING;
7894faedfe8SSam Leffler 			VLAN_UNLOCK();
7902cc2df49SGarrett Wollman 			break;
7912cc2df49SGarrett Wollman 		}
7922cc2df49SGarrett Wollman 		p = ifunit(vlr.vlr_parent);
7932cc2df49SGarrett Wollman 		if (p == 0) {
7942cc2df49SGarrett Wollman 			error = ENOENT;
7952cc2df49SGarrett Wollman 			break;
7962cc2df49SGarrett Wollman 		}
797fb88a3e0SBill Paul 		/*
798fb88a3e0SBill Paul 		 * Don't let the caller set up a VLAN tag with
799fb88a3e0SBill Paul 		 * anything except VLID bits.
800fb88a3e0SBill Paul 		 */
801fb88a3e0SBill Paul 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
802fb88a3e0SBill Paul 			error = EINVAL;
803fb88a3e0SBill Paul 			break;
804fb88a3e0SBill Paul 		}
8054faedfe8SSam Leffler 		VLAN_LOCK();
8062cc2df49SGarrett Wollman 		error = vlan_config(ifv, p);
8074faedfe8SSam Leffler 		if (error) {
8084faedfe8SSam Leffler 			VLAN_UNLOCK();
8092cc2df49SGarrett Wollman 			break;
8104faedfe8SSam Leffler 		}
8112cc2df49SGarrett Wollman 		ifv->ifv_tag = vlr.vlr_tag;
812ae290324SJordan K. Hubbard 		ifp->if_flags |= IFF_RUNNING;
8134faedfe8SSam Leffler 		VLAN_UNLOCK();
814a3814acfSSam Leffler 
815a3814acfSSam Leffler 		/* Update promiscuous mode, if necessary. */
816a3814acfSSam Leffler 		vlan_set_promisc(ifp);
8172cc2df49SGarrett Wollman 		break;
8182cc2df49SGarrett Wollman 
8192cc2df49SGarrett Wollman 	case SIOCGETVLAN:
8202cc2df49SGarrett Wollman 		bzero(&vlr, sizeof vlr);
8214faedfe8SSam Leffler 		VLAN_LOCK();
8222cc2df49SGarrett Wollman 		if (ifv->ifv_p) {
8239bf40edeSBrooks Davis 			strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
8249bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
8252cc2df49SGarrett Wollman 			vlr.vlr_tag = ifv->ifv_tag;
8262cc2df49SGarrett Wollman 		}
8274faedfe8SSam Leffler 		VLAN_UNLOCK();
8282cc2df49SGarrett Wollman 		error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
8292cc2df49SGarrett Wollman 		break;
8302cc2df49SGarrett Wollman 
8312cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
8322cc2df49SGarrett Wollman 		/*
833a3814acfSSam Leffler 		 * For promiscuous mode, we enable promiscuous mode on
834a3814acfSSam Leffler 		 * the parent if we need promiscuous on the VLAN interface.
8352cc2df49SGarrett Wollman 		 */
836a3814acfSSam Leffler 		if (ifv->ifv_p != NULL)
837a3814acfSSam Leffler 			error = vlan_set_promisc(ifp);
8382cc2df49SGarrett Wollman 		break;
839a3814acfSSam Leffler 
840f731f104SBill Paul 	case SIOCADDMULTI:
841f731f104SBill Paul 	case SIOCDELMULTI:
842f731f104SBill Paul 		error = vlan_setmulti(ifp);
843f731f104SBill Paul 		break;
8442cc2df49SGarrett Wollman 	default:
8452cc2df49SGarrett Wollman 		error = EINVAL;
8462cc2df49SGarrett Wollman 	}
8472cc2df49SGarrett Wollman 	return error;
8482cc2df49SGarrett Wollman }
849