xref: /freebsd/sys/net/if_vlan.c (revision 9bf40ede4a299f315bc4b0ae5329631b8c7dc271)
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>
642cc2df49SGarrett Wollman 
652cc2df49SGarrett Wollman #ifdef INET
662cc2df49SGarrett Wollman #include <netinet/in.h>
672cc2df49SGarrett Wollman #include <netinet/if_ether.h>
682cc2df49SGarrett Wollman #endif
692cc2df49SGarrett Wollman 
709d4fe4b2SBrooks Davis #define VLANNAME	"vlan"
719d4fe4b2SBrooks Davis 
72a3814acfSSam Leffler struct vlan_mc_entry {
73a3814acfSSam Leffler 	struct ether_addr		mc_addr;
74a3814acfSSam Leffler 	SLIST_ENTRY(vlan_mc_entry)	mc_entries;
75a3814acfSSam Leffler };
76a3814acfSSam Leffler 
77a3814acfSSam Leffler struct	ifvlan {
78a3814acfSSam Leffler 	struct	arpcom ifv_ac;	/* make this an interface */
79a3814acfSSam Leffler 	struct	ifnet *ifv_p;	/* parent inteface of this vlan */
80a3814acfSSam Leffler 	struct	ifv_linkmib {
81a3814acfSSam Leffler 		int	ifvm_parent;
82a3814acfSSam Leffler 		int	ifvm_encaplen;	/* encapsulation length */
83a3814acfSSam Leffler 		int	ifvm_mtufudge;	/* MTU fudged by this much */
84a3814acfSSam Leffler 		int	ifvm_mintu;	/* min transmission unit */
85a3814acfSSam Leffler 		u_int16_t ifvm_proto; /* encapsulation ethertype */
86a3814acfSSam Leffler 		u_int16_t ifvm_tag; /* tag to apply on packets leaving if */
87a3814acfSSam Leffler 	}	ifv_mib;
88a3814acfSSam Leffler 	SLIST_HEAD(__vlan_mchead, vlan_mc_entry)	vlan_mc_listhead;
89a3814acfSSam Leffler 	LIST_ENTRY(ifvlan) ifv_list;
90a3814acfSSam Leffler 	int	ifv_flags;
91a3814acfSSam Leffler };
92a3814acfSSam Leffler #define	ifv_if	ifv_ac.ac_if
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 
98a3814acfSSam Leffler #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
99a3814acfSSam Leffler 
1004a408dcbSBill Paul SYSCTL_DECL(_net_link);
101d2a75853SBill Fenner SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
1022cc2df49SGarrett Wollman SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
1032cc2df49SGarrett Wollman 
1045e17543aSBrooks Davis static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
1059d4fe4b2SBrooks Davis static LIST_HEAD(, ifvlan) ifv_list;
1062cc2df49SGarrett Wollman 
1074faedfe8SSam Leffler /*
1084faedfe8SSam Leffler  * Locking: one lock is used to guard both the ifv_list and modification
1094faedfe8SSam Leffler  * to vlan data structures.  We are rather conservative here; probably
1104faedfe8SSam Leffler  * more than necessary.
1114faedfe8SSam Leffler  */
1124faedfe8SSam Leffler static struct mtx ifv_mtx;
1135e17543aSBrooks Davis #define	VLAN_LOCK_INIT()	mtx_init(&ifv_mtx, VLANNAME, NULL, MTX_DEF)
1144faedfe8SSam Leffler #define	VLAN_LOCK_DESTROY()	mtx_destroy(&ifv_mtx)
1154faedfe8SSam Leffler #define	VLAN_LOCK_ASSERT()	mtx_assert(&ifv_mtx, MA_OWNED)
1164faedfe8SSam Leffler #define	VLAN_LOCK()	mtx_lock(&ifv_mtx)
1174faedfe8SSam Leffler #define	VLAN_UNLOCK()	mtx_unlock(&ifv_mtx)
1184faedfe8SSam Leffler 
1193b16e7b2SMaxime Henrion static	int vlan_clone_create(struct if_clone *, int);
120ae5a19beSBrooks Davis static	void vlan_clone_destroy(struct ifnet *);
1212cc2df49SGarrett Wollman static	void vlan_start(struct ifnet *ifp);
1222cc2df49SGarrett Wollman static	void vlan_ifinit(void *foo);
123a3814acfSSam Leffler static	void vlan_input(struct ifnet *ifp, struct mbuf *m);
124cfe8b629SGarrett Wollman static	int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
125f731f104SBill Paul static	int vlan_setmulti(struct ifnet *ifp);
126f731f104SBill Paul static	int vlan_unconfig(struct ifnet *ifp);
127f731f104SBill Paul static	int vlan_config(struct ifvlan *ifv, struct ifnet *p);
128f731f104SBill Paul 
1295e17543aSBrooks Davis struct if_clone vlan_cloner = IF_CLONE_INITIALIZER(VLANNAME,
130ae5a19beSBrooks Davis     vlan_clone_create, vlan_clone_destroy, 0, IF_MAXUNIT);
1319d4fe4b2SBrooks Davis 
132f731f104SBill Paul /*
133f731f104SBill Paul  * Program our multicast filter. What we're actually doing is
134f731f104SBill Paul  * programming the multicast filter of the parent. This has the
135f731f104SBill Paul  * side effect of causing the parent interface to receive multicast
136f731f104SBill Paul  * traffic that it doesn't really want, which ends up being discarded
137f731f104SBill Paul  * later by the upper protocol layers. Unfortunately, there's no way
138f731f104SBill Paul  * to avoid this: there really is only one physical interface.
139f731f104SBill Paul  */
1402b120974SPeter Wemm static int
1412b120974SPeter Wemm vlan_setmulti(struct ifnet *ifp)
142f731f104SBill Paul {
143f731f104SBill Paul 	struct ifnet		*ifp_p;
144f731f104SBill Paul 	struct ifmultiaddr	*ifma, *rifma = NULL;
145f731f104SBill Paul 	struct ifvlan		*sc;
146f731f104SBill Paul 	struct vlan_mc_entry	*mc = NULL;
147f731f104SBill Paul 	struct sockaddr_dl	sdl;
148f731f104SBill Paul 	int			error;
149f731f104SBill Paul 
150f731f104SBill Paul 	/* Find the parent. */
151f731f104SBill Paul 	sc = ifp->if_softc;
152f731f104SBill Paul 	ifp_p = sc->ifv_p;
153f731f104SBill Paul 
1541b2a4f7aSBill Fenner 	/*
1551b2a4f7aSBill Fenner 	 * If we don't have a parent, just remember the membership for
1561b2a4f7aSBill Fenner 	 * when we do.
1571b2a4f7aSBill Fenner 	 */
1581b2a4f7aSBill Fenner 	if (ifp_p == NULL)
1591b2a4f7aSBill Fenner 		return(0);
1601b2a4f7aSBill Fenner 
16124993214SYaroslav Tykhiy 	bzero((char *)&sdl, sizeof sdl);
16224993214SYaroslav Tykhiy 	sdl.sdl_len = sizeof sdl;
163f731f104SBill Paul 	sdl.sdl_family = AF_LINK;
16426e30963SBill Fenner 	sdl.sdl_index = ifp_p->if_index;
16526e30963SBill Fenner 	sdl.sdl_type = IFT_ETHER;
16624993214SYaroslav Tykhiy 	sdl.sdl_alen = ETHER_ADDR_LEN;
167f731f104SBill Paul 
168f731f104SBill Paul 	/* First, remove any existing filter entries. */
16922f29826SPoul-Henning Kamp 	while(SLIST_FIRST(&sc->vlan_mc_listhead) != NULL) {
17022f29826SPoul-Henning Kamp 		mc = SLIST_FIRST(&sc->vlan_mc_listhead);
171f731f104SBill Paul 		bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
172f731f104SBill Paul 		error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
173f731f104SBill Paul 		if (error)
174f731f104SBill Paul 			return(error);
175f731f104SBill Paul 		SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
1769d4fe4b2SBrooks Davis 		free(mc, M_VLAN);
177f731f104SBill Paul 	}
178f731f104SBill Paul 
179f731f104SBill Paul 	/* Now program new ones. */
1806817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
181f731f104SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
182f731f104SBill Paul 			continue;
183a163d034SWarner Losh 		mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_WAITOK);
184f731f104SBill Paul 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
185f731f104SBill Paul 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
186f731f104SBill Paul 		SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
18726e30963SBill Fenner 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
18826e30963SBill Fenner 		    LLADDR(&sdl), ETHER_ADDR_LEN);
189f731f104SBill Paul 		error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
190f731f104SBill Paul 		if (error)
191f731f104SBill Paul 			return(error);
192f731f104SBill Paul 	}
193f731f104SBill Paul 
194f731f104SBill Paul 	return(0);
195f731f104SBill Paul }
1962cc2df49SGarrett Wollman 
197a3814acfSSam Leffler /*
198a3814acfSSam Leffler  * VLAN support can be loaded as a module.  The only place in the
199a3814acfSSam Leffler  * system that's intimately aware of this is ether_input.  We hook
200a3814acfSSam Leffler  * into this code through vlan_input_p which is defined there and
201a3814acfSSam Leffler  * set here.  Noone else in the system should be aware of this so
202a3814acfSSam Leffler  * we use an explicit reference here.
203a3814acfSSam Leffler  *
204a3814acfSSam Leffler  * NB: Noone should ever need to check if vlan_input_p is null or
205a3814acfSSam Leffler  *     not.  This is because interfaces have a count of the number
206a3814acfSSam Leffler  *     of active vlans (if_nvlans) and this should never be bumped
207a3814acfSSam Leffler  *     except by vlan_config--which is in this module so therefore
208a3814acfSSam Leffler  *     the module must be loaded and vlan_input_p must be non-NULL.
209a3814acfSSam Leffler  */
210a3814acfSSam Leffler extern	void (*vlan_input_p)(struct ifnet *, struct mbuf *);
211a3814acfSSam Leffler 
2122b120974SPeter Wemm static int
2132b120974SPeter Wemm vlan_modevent(module_t mod, int type, void *data)
2142b120974SPeter Wemm {
2159d4fe4b2SBrooks Davis 
2162b120974SPeter Wemm 	switch (type) {
2172b120974SPeter Wemm 	case MOD_LOAD:
2189d4fe4b2SBrooks Davis 		LIST_INIT(&ifv_list);
2194faedfe8SSam Leffler 		VLAN_LOCK_INIT();
2209d4fe4b2SBrooks Davis 		vlan_input_p = vlan_input;
2219d4fe4b2SBrooks Davis 		if_clone_attach(&vlan_cloner);
2222b120974SPeter Wemm 		break;
2232b120974SPeter Wemm 	case MOD_UNLOAD:
2249d4fe4b2SBrooks Davis 		if_clone_detach(&vlan_cloner);
2259d4fe4b2SBrooks Davis 		vlan_input_p = NULL;
2269d4fe4b2SBrooks Davis 		while (!LIST_EMPTY(&ifv_list))
2279d4fe4b2SBrooks Davis 			vlan_clone_destroy(&LIST_FIRST(&ifv_list)->ifv_if);
2284faedfe8SSam Leffler 		VLAN_LOCK_DESTROY();
2299d4fe4b2SBrooks Davis 		break;
2302b120974SPeter Wemm 	}
2312b120974SPeter Wemm 	return 0;
2322b120974SPeter Wemm }
2332b120974SPeter Wemm 
2342b120974SPeter Wemm static moduledata_t vlan_mod = {
2352b120974SPeter Wemm 	"if_vlan",
2362b120974SPeter Wemm 	vlan_modevent,
2372b120974SPeter Wemm 	0
2382b120974SPeter Wemm };
2392b120974SPeter Wemm 
2402b120974SPeter Wemm DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2412cc2df49SGarrett Wollman 
2429d4fe4b2SBrooks Davis static int
2433b16e7b2SMaxime Henrion vlan_clone_create(struct if_clone *ifc, int unit)
2449d4fe4b2SBrooks Davis {
2459d4fe4b2SBrooks Davis 	struct ifvlan *ifv;
2469d4fe4b2SBrooks Davis 	struct ifnet *ifp;
2479d4fe4b2SBrooks Davis 
248a163d034SWarner Losh 	ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
2499d4fe4b2SBrooks Davis 	ifp = &ifv->ifv_if;
2509d4fe4b2SBrooks Davis 	SLIST_INIT(&ifv->vlan_mc_listhead);
2519d4fe4b2SBrooks Davis 
2529d4fe4b2SBrooks Davis 	ifp->if_softc = ifv;
2539bf40edeSBrooks Davis 	if_initname(ifp, ifc->ifc_name, unit);
2549d4fe4b2SBrooks Davis 	/* NB: flags are not set here */
2559d4fe4b2SBrooks Davis 	ifp->if_linkmib = &ifv->ifv_mib;
2569d4fe4b2SBrooks Davis 	ifp->if_linkmiblen = sizeof ifv->ifv_mib;
2579d4fe4b2SBrooks Davis 	/* NB: mtu is not set here */
2589d4fe4b2SBrooks Davis 
2599d4fe4b2SBrooks Davis 	ifp->if_init = vlan_ifinit;
2609d4fe4b2SBrooks Davis 	ifp->if_start = vlan_start;
2619d4fe4b2SBrooks Davis 	ifp->if_ioctl = vlan_ioctl;
2629d4fe4b2SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
263a3814acfSSam Leffler 	ether_ifattach(ifp, ifv->ifv_ac.ac_enaddr);
2649d4fe4b2SBrooks Davis 	/* Now undo some of the damage... */
265211f625aSBill Fenner 	ifp->if_baudrate = 0;
266a3814acfSSam Leffler 	ifp->if_type = IFT_L2VLAN;
267a3814acfSSam Leffler 	ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
2689d4fe4b2SBrooks Davis 
2694faedfe8SSam Leffler 	VLAN_LOCK();
2704faedfe8SSam Leffler 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
2714faedfe8SSam Leffler 	VLAN_UNLOCK();
2724faedfe8SSam Leffler 
2739d4fe4b2SBrooks Davis 	return (0);
2749d4fe4b2SBrooks Davis }
2759d4fe4b2SBrooks Davis 
276ae5a19beSBrooks Davis static void
2779d4fe4b2SBrooks Davis vlan_clone_destroy(struct ifnet *ifp)
2789d4fe4b2SBrooks Davis {
2799d4fe4b2SBrooks Davis 	struct ifvlan *ifv = ifp->if_softc;
2809d4fe4b2SBrooks Davis 
2814faedfe8SSam Leffler 	VLAN_LOCK();
2829d4fe4b2SBrooks Davis 	LIST_REMOVE(ifv, ifv_list);
2839d4fe4b2SBrooks Davis 	vlan_unconfig(ifp);
2844faedfe8SSam Leffler 	VLAN_UNLOCK();
2859d4fe4b2SBrooks Davis 
286a3814acfSSam Leffler 	ether_ifdetach(ifp);
2879d4fe4b2SBrooks Davis 
2889d4fe4b2SBrooks Davis 	free(ifv, M_VLAN);
2899d4fe4b2SBrooks Davis }
2909d4fe4b2SBrooks Davis 
2912cc2df49SGarrett Wollman static void
2922cc2df49SGarrett Wollman vlan_ifinit(void *foo)
2932cc2df49SGarrett Wollman {
294f731f104SBill Paul 	return;
2952cc2df49SGarrett Wollman }
2962cc2df49SGarrett Wollman 
2972cc2df49SGarrett Wollman static void
2982cc2df49SGarrett Wollman vlan_start(struct ifnet *ifp)
2992cc2df49SGarrett Wollman {
3002cc2df49SGarrett Wollman 	struct ifvlan *ifv;
3012cc2df49SGarrett Wollman 	struct ifnet *p;
3022cc2df49SGarrett Wollman 	struct ether_vlan_header *evl;
3035326b23cSMatthew N. Dodd 	struct mbuf *m;
3042cc2df49SGarrett Wollman 
3052cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
3062cc2df49SGarrett Wollman 	p = ifv->ifv_p;
3072cc2df49SGarrett Wollman 
3082cc2df49SGarrett Wollman 	ifp->if_flags |= IFF_OACTIVE;
3092cc2df49SGarrett Wollman 	for (;;) {
3102cc2df49SGarrett Wollman 		IF_DEQUEUE(&ifp->if_snd, m);
3112cc2df49SGarrett Wollman 		if (m == 0)
3122cc2df49SGarrett Wollman 			break;
313a3814acfSSam Leffler 		BPF_MTAP(ifp, m);
3142cc2df49SGarrett Wollman 
315f731f104SBill Paul 		/*
31624993214SYaroslav Tykhiy 		 * Do not run parent's if_start() if the parent is not up,
31724993214SYaroslav Tykhiy 		 * or parent's driver will cause a system crash.
31824993214SYaroslav Tykhiy 		 */
31924993214SYaroslav Tykhiy 		if ((p->if_flags & (IFF_UP | IFF_RUNNING)) !=
32024993214SYaroslav Tykhiy 					(IFF_UP | IFF_RUNNING)) {
32124993214SYaroslav Tykhiy 			m_freem(m);
322a3814acfSSam Leffler 			ifp->if_collisions++;
32324993214SYaroslav Tykhiy 			continue;
32424993214SYaroslav Tykhiy 		}
32524993214SYaroslav Tykhiy 
32624993214SYaroslav Tykhiy 		/*
327a3814acfSSam Leffler 		 * If underlying interface can do VLAN tag insertion itself,
328a3814acfSSam Leffler 		 * just pass the packet along. However, we need some way to
329a3814acfSSam Leffler 		 * tell the interface where the packet came from so that it
330a3814acfSSam Leffler 		 * knows how to find the VLAN tag to use, so we attach a
331a3814acfSSam Leffler 		 * packet tag that holds it.
332f731f104SBill Paul 		 */
33379a58745SBill Paul 		if (p->if_capabilities & IFCAP_VLAN_HWTAGGING) {
334a3814acfSSam Leffler 			struct m_tag *mtag = m_tag_alloc(MTAG_VLAN,
335a3814acfSSam Leffler 							 MTAG_VLAN_TAG,
336a3814acfSSam Leffler 							 sizeof (u_int),
3374a692a1fSSam Leffler 							 M_NOWAIT);
338a3814acfSSam Leffler 			if (mtag == NULL) {
339a3814acfSSam Leffler 				ifp->if_oerrors++;
340a3814acfSSam Leffler 				m_freem(m);
341a3814acfSSam Leffler 				continue;
342a3814acfSSam Leffler 			}
343a3814acfSSam Leffler 			*(u_int*)(mtag+1) = ifv->ifv_tag;
344a3814acfSSam Leffler 			m_tag_prepend(m, mtag);
345f731f104SBill Paul 		} else {
346a163d034SWarner Losh 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
3474af90a4dSMatthew N. Dodd 			if (m == NULL) {
348a3814acfSSam Leffler 				if_printf(ifp, "unable to prepend VLAN header");
3494af90a4dSMatthew N. Dodd 				ifp->if_ierrors++;
3502cc2df49SGarrett Wollman 				continue;
3514af90a4dSMatthew N. Dodd 			}
3522cc2df49SGarrett Wollman 			/* M_PREPEND takes care of m_len, m_pkthdr.len for us */
3532cc2df49SGarrett Wollman 
354a3814acfSSam Leffler 			if (m->m_len < sizeof(*evl)) {
355a3814acfSSam Leffler 				m = m_pullup(m, sizeof(*evl));
3565326b23cSMatthew N. Dodd 				if (m == NULL) {
357a3814acfSSam Leffler 					if_printf(ifp,
358a3814acfSSam Leffler 					    "cannot pullup VLAN header");
3594af90a4dSMatthew N. Dodd 					ifp->if_ierrors++;
3604af90a4dSMatthew N. Dodd 					continue;
3614af90a4dSMatthew N. Dodd 				}
362a3814acfSSam Leffler 			}
3634af90a4dSMatthew N. Dodd 
3642cc2df49SGarrett Wollman 			/*
3652cc2df49SGarrett Wollman 			 * Transform the Ethernet header into an Ethernet header
3662cc2df49SGarrett Wollman 			 * with 802.1Q encapsulation.
3672cc2df49SGarrett Wollman 			 */
368a3814acfSSam Leffler 			bcopy(mtod(m, char *) + ifv->ifv_encaplen,
369797f247bSMatthew N. Dodd 			      mtod(m, char *), ETHER_HDR_LEN);
3702cc2df49SGarrett Wollman 			evl = mtod(m, struct ether_vlan_header *);
3712cc2df49SGarrett Wollman 			evl->evl_proto = evl->evl_encap_proto;
3729d4fe4b2SBrooks Davis 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
3732cc2df49SGarrett Wollman 			evl->evl_tag = htons(ifv->ifv_tag);
374f731f104SBill Paul #ifdef DEBUG
375a9283398SJohn Baldwin 			printf("vlan_start: %*D\n", (int)sizeof *evl,
37665f1bb65SPeter Wemm 			    (unsigned char *)evl, ":");
377f731f104SBill Paul #endif
378f731f104SBill Paul 		}
3792cc2df49SGarrett Wollman 
3802cc2df49SGarrett Wollman 		/*
3812cc2df49SGarrett Wollman 		 * Send it, precisely as ether_output() would have.
3822cc2df49SGarrett Wollman 		 * We are already running at splimp.
3832cc2df49SGarrett Wollman 		 */
384df5e1987SJonathan Lemon 		if (IF_HANDOFF(&p->if_snd, m, p))
385f731f104SBill Paul 			ifp->if_opackets++;
386df5e1987SJonathan Lemon 		else
387df5e1987SJonathan Lemon 			ifp->if_oerrors++;
3882cc2df49SGarrett Wollman 	}
3892cc2df49SGarrett Wollman 	ifp->if_flags &= ~IFF_OACTIVE;
390f731f104SBill Paul 
391f731f104SBill Paul 	return;
392f731f104SBill Paul }
393f731f104SBill Paul 
394a3814acfSSam Leffler static void
395a3814acfSSam Leffler vlan_input(struct ifnet *ifp, struct mbuf *m)
396f731f104SBill Paul {
397a3814acfSSam Leffler 	struct ether_vlan_header *evl;
398f731f104SBill Paul 	struct ifvlan *ifv;
399a3814acfSSam Leffler 	struct m_tag *mtag;
400a3814acfSSam Leffler 	u_int tag;
401a3814acfSSam Leffler 
402a3814acfSSam Leffler 	mtag = m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL);
403a3814acfSSam Leffler 	if (mtag != NULL) {
404a3814acfSSam Leffler 		/*
405a3814acfSSam Leffler 		 * Packet is tagged, m contains a normal
406a3814acfSSam Leffler 		 * Ethernet frame; the tag is stored out-of-band.
407a3814acfSSam Leffler 		 */
408fb88a3e0SBill Paul 		tag = EVL_VLANOFTAG(*(u_int*)(mtag+1));
409a3814acfSSam Leffler 		m_tag_delete(m, mtag);
410a3814acfSSam Leffler 	} else {
411a3814acfSSam Leffler 		switch (ifp->if_type) {
412a3814acfSSam Leffler 		case IFT_ETHER:
413a3814acfSSam Leffler 			if (m->m_len < sizeof (*evl) &&
414a3814acfSSam Leffler 			    (m = m_pullup(m, sizeof (*evl))) == NULL) {
415a3814acfSSam Leffler 				if_printf(ifp, "cannot pullup VLAN header\n");
416a3814acfSSam Leffler 				return;
417a3814acfSSam Leffler 			}
418a3814acfSSam Leffler 			evl = mtod(m, struct ether_vlan_header *);
419a3814acfSSam Leffler 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN,
420a3814acfSSam Leffler 				("vlan_input: bad encapsulated protocols (%u)",
421a3814acfSSam Leffler 				 ntohs(evl->evl_encap_proto)));
422a3814acfSSam Leffler 
423fb88a3e0SBill Paul 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
424f731f104SBill Paul 
4257a46ec8fSBrooks Davis 			/*
426a3814acfSSam Leffler 			 * Restore the original ethertype.  We'll remove
427a3814acfSSam Leffler 			 * the encapsulation after we've found the vlan
428a3814acfSSam Leffler 			 * interface corresponding to the tag.
4297a46ec8fSBrooks Davis 			 */
430a3814acfSSam Leffler 			evl->evl_encap_proto = evl->evl_proto;
431a3814acfSSam Leffler 			break;
432a3814acfSSam Leffler 		default:
433a3814acfSSam Leffler 			tag = (u_int) -1;
434a3814acfSSam Leffler #ifdef DIAGNOSTIC
435a3814acfSSam Leffler 			panic("vlan_input: unsupported if type %u", ifp->if_type);
436a3814acfSSam Leffler #endif
437a3814acfSSam Leffler 			break;
438a3814acfSSam Leffler 		}
4397a46ec8fSBrooks Davis 	}
4407a46ec8fSBrooks Davis 
4414faedfe8SSam Leffler 	VLAN_LOCK();
4424faedfe8SSam Leffler 	LIST_FOREACH(ifv, &ifv_list, ifv_list)
443a3814acfSSam Leffler 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
444f731f104SBill Paul 			break;
445f731f104SBill Paul 
446242c766bSBill Fenner 	if (ifv == NULL || (ifv->ifv_if.if_flags & IFF_UP) == 0) {
4474faedfe8SSam Leffler 		VLAN_UNLOCK();
4487d3e4c6eSLuigi Rizzo 		m_freem(m);
449a3814acfSSam Leffler 		ifp->if_noproto++;
450a3814acfSSam Leffler 		return;
451f731f104SBill Paul 	}
4524faedfe8SSam Leffler 	VLAN_UNLOCK();		/* XXX extend below? */
453f731f104SBill Paul 
454a3814acfSSam Leffler 	if (mtag == NULL) {
455f731f104SBill Paul 		/*
456a3814acfSSam Leffler 		 * Packet had an in-line encapsulation header;
457a3814acfSSam Leffler 		 * remove it.  The original header has already
458a3814acfSSam Leffler 		 * been fixed up above.
459f731f104SBill Paul 		 */
460a3814acfSSam Leffler 		bcopy(mtod(m, caddr_t),
461a3814acfSSam Leffler 		      mtod(m, caddr_t) + ETHER_VLAN_ENCAP_LEN,
462797f247bSMatthew N. Dodd 		      ETHER_HDR_LEN);
463a3814acfSSam Leffler 		m_adj(m, ETHER_VLAN_ENCAP_LEN);
464a3814acfSSam Leffler 	}
465a3814acfSSam Leffler 
466f731f104SBill Paul 	m->m_pkthdr.rcvif = &ifv->ifv_if;
467f731f104SBill Paul 	ifv->ifv_if.if_ipackets++;
4682cc2df49SGarrett Wollman 
469a3814acfSSam Leffler 	/* Pass it back through the parent's input routine. */
470a3814acfSSam Leffler 	(*ifp->if_input)(&ifv->ifv_if, m);
4712cc2df49SGarrett Wollman }
4722cc2df49SGarrett Wollman 
4732cc2df49SGarrett Wollman static int
4742cc2df49SGarrett Wollman vlan_config(struct ifvlan *ifv, struct ifnet *p)
4752cc2df49SGarrett Wollman {
4762cc2df49SGarrett Wollman 	struct ifaddr *ifa1, *ifa2;
4772cc2df49SGarrett Wollman 	struct sockaddr_dl *sdl1, *sdl2;
4782cc2df49SGarrett Wollman 
4794faedfe8SSam Leffler 	VLAN_LOCK_ASSERT();
4804faedfe8SSam Leffler 
4812cc2df49SGarrett Wollman 	if (p->if_data.ifi_type != IFT_ETHER)
4822cc2df49SGarrett Wollman 		return EPROTONOSUPPORT;
4832cc2df49SGarrett Wollman 	if (ifv->ifv_p)
4842cc2df49SGarrett Wollman 		return EBUSY;
4852cc2df49SGarrett Wollman 
486a3814acfSSam Leffler 	ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
487a3814acfSSam Leffler 	ifv->ifv_mintu = ETHERMIN;
488a3814acfSSam Leffler 	ifv->ifv_flags = 0;
489a3814acfSSam Leffler 
490a3814acfSSam Leffler 	/*
491a3814acfSSam Leffler 	 * If the parent supports the VLAN_MTU capability,
492a3814acfSSam Leffler 	 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
493a3814acfSSam Leffler 	 * enable it.
494a3814acfSSam Leffler 	 */
495a3814acfSSam Leffler 	p->if_nvlans++;
496a3814acfSSam Leffler 	if (p->if_nvlans == 1 && (p->if_capabilities & IFCAP_VLAN_MTU) != 0) {
497a3814acfSSam Leffler 		/*
498a3814acfSSam Leffler 		 * Enable Tx/Rx of VLAN-sized frames.
499a3814acfSSam Leffler 		 */
500a3814acfSSam Leffler 		p->if_capenable |= IFCAP_VLAN_MTU;
501a3814acfSSam Leffler 		if (p->if_flags & IFF_UP) {
502a3814acfSSam Leffler 			struct ifreq ifr;
503a3814acfSSam Leffler 			int error;
504a3814acfSSam Leffler 
505a3814acfSSam Leffler 			ifr.ifr_flags = p->if_flags;
506a3814acfSSam Leffler 			error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
507a3814acfSSam Leffler 			    (caddr_t) &ifr);
508a3814acfSSam Leffler 			if (error) {
509a3814acfSSam Leffler 				p->if_nvlans--;
510a3814acfSSam Leffler 				if (p->if_nvlans == 0)
511a3814acfSSam Leffler 					p->if_capenable &= ~IFCAP_VLAN_MTU;
512a3814acfSSam Leffler 				return (error);
513a3814acfSSam Leffler 			}
514a3814acfSSam Leffler 		}
515a3814acfSSam Leffler 		ifv->ifv_mtufudge = 0;
516a3814acfSSam Leffler 	} else if ((p->if_capabilities & IFCAP_VLAN_MTU) == 0) {
517a3814acfSSam Leffler 		/*
518a3814acfSSam Leffler 		 * Fudge the MTU by the encapsulation size.  This
519a3814acfSSam Leffler 		 * makes us incompatible with strictly compliant
520a3814acfSSam Leffler 		 * 802.1Q implementations, but allows us to use
521a3814acfSSam Leffler 		 * the feature with other NetBSD implementations,
522a3814acfSSam Leffler 		 * which might still be useful.
523a3814acfSSam Leffler 		 */
524a3814acfSSam Leffler 		ifv->ifv_mtufudge = ifv->ifv_encaplen;
525a3814acfSSam Leffler 	}
526a3814acfSSam Leffler 
527a3814acfSSam Leffler 	ifv->ifv_p = p;
528a3814acfSSam Leffler 	ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
5292cc2df49SGarrett Wollman 	/*
53024993214SYaroslav Tykhiy 	 * Copy only a selected subset of flags from the parent.
53124993214SYaroslav Tykhiy 	 * Other flags are none of our business.
5322cc2df49SGarrett Wollman 	 */
53324993214SYaroslav Tykhiy 	ifv->ifv_if.if_flags = (p->if_flags &
53424993214SYaroslav Tykhiy 	    (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX | IFF_POINTOPOINT));
5352cc2df49SGarrett Wollman 
5362cc2df49SGarrett Wollman 	/*
537a3814acfSSam Leffler 	 * If the parent interface can do hardware-assisted
538a3814acfSSam Leffler 	 * VLAN encapsulation, then propagate its hardware-
539a3814acfSSam Leffler 	 * assisted checksumming flags.
540a3814acfSSam Leffler 	 */
541a3814acfSSam Leffler 	if (p->if_capabilities & IFCAP_VLAN_HWTAGGING)
542a3814acfSSam Leffler 		ifv->ifv_if.if_capabilities |= p->if_capabilities & IFCAP_HWCSUM;
543a3814acfSSam Leffler 
544a3814acfSSam Leffler 	/*
5452cc2df49SGarrett Wollman 	 * Set up our ``Ethernet address'' to reflect the underlying
5462cc2df49SGarrett Wollman 	 * physical interface's.
5472cc2df49SGarrett Wollman 	 */
548f9132cebSJonathan Lemon 	ifa1 = ifaddr_byindex(ifv->ifv_if.if_index);
549f9132cebSJonathan Lemon 	ifa2 = ifaddr_byindex(p->if_index);
5502cc2df49SGarrett Wollman 	sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
5512cc2df49SGarrett Wollman 	sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
5522cc2df49SGarrett Wollman 	sdl1->sdl_type = IFT_ETHER;
5532cc2df49SGarrett Wollman 	sdl1->sdl_alen = ETHER_ADDR_LEN;
5542cc2df49SGarrett Wollman 	bcopy(LLADDR(sdl2), LLADDR(sdl1), ETHER_ADDR_LEN);
5552cc2df49SGarrett Wollman 	bcopy(LLADDR(sdl2), ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
5561b2a4f7aSBill Fenner 
5571b2a4f7aSBill Fenner 	/*
5581b2a4f7aSBill Fenner 	 * Configure multicast addresses that may already be
5591b2a4f7aSBill Fenner 	 * joined on the vlan device.
5601b2a4f7aSBill Fenner 	 */
5611b2a4f7aSBill Fenner 	(void)vlan_setmulti(&ifv->ifv_if);
5621b2a4f7aSBill Fenner 
5632cc2df49SGarrett Wollman 	return 0;
5642cc2df49SGarrett Wollman }
5652cc2df49SGarrett Wollman 
5662cc2df49SGarrett Wollman static int
567f731f104SBill Paul vlan_unconfig(struct ifnet *ifp)
568f731f104SBill Paul {
569f731f104SBill Paul 	struct ifaddr *ifa;
570f731f104SBill Paul 	struct sockaddr_dl *sdl;
571f731f104SBill Paul 	struct vlan_mc_entry *mc;
572f731f104SBill Paul 	struct ifvlan *ifv;
573f731f104SBill Paul 	struct ifnet *p;
574f731f104SBill Paul 	int error;
575f731f104SBill Paul 
5764faedfe8SSam Leffler 	VLAN_LOCK_ASSERT();
5774faedfe8SSam Leffler 
578f731f104SBill Paul 	ifv = ifp->if_softc;
579f731f104SBill Paul 	p = ifv->ifv_p;
580f731f104SBill Paul 
5811b2a4f7aSBill Fenner 	if (p) {
5821b2a4f7aSBill Fenner 		struct sockaddr_dl sdl;
5831b2a4f7aSBill Fenner 
584f731f104SBill Paul 		/*
585f731f104SBill Paul 		 * Since the interface is being unconfigured, we need to
586f731f104SBill Paul 		 * empty the list of multicast groups that we may have joined
5871b2a4f7aSBill Fenner 		 * while we were alive from the parent's list.
588f731f104SBill Paul 		 */
5891b2a4f7aSBill Fenner 		bzero((char *)&sdl, sizeof sdl);
5901b2a4f7aSBill Fenner 		sdl.sdl_len = sizeof sdl;
591f731f104SBill Paul 		sdl.sdl_family = AF_LINK;
5921b2a4f7aSBill Fenner 		sdl.sdl_index = p->if_index;
5931b2a4f7aSBill Fenner 		sdl.sdl_type = IFT_ETHER;
5941b2a4f7aSBill Fenner 		sdl.sdl_alen = ETHER_ADDR_LEN;
5951b2a4f7aSBill Fenner 
5961b2a4f7aSBill Fenner 		while(SLIST_FIRST(&ifv->vlan_mc_listhead) != NULL) {
59722f29826SPoul-Henning Kamp 			mc = SLIST_FIRST(&ifv->vlan_mc_listhead);
598f731f104SBill Paul 			bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
599f731f104SBill Paul 			error = if_delmulti(p, (struct sockaddr *)&sdl);
600f731f104SBill Paul 			if (error)
601f731f104SBill Paul 				return(error);
602f731f104SBill Paul 			SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
6039d4fe4b2SBrooks Davis 			free(mc, M_VLAN);
604f731f104SBill Paul 		}
605a3814acfSSam Leffler 
606a3814acfSSam Leffler 		p->if_nvlans--;
607a3814acfSSam Leffler 		if (p->if_nvlans == 0) {
608a3814acfSSam Leffler 			/*
609a3814acfSSam Leffler 			 * Disable Tx/Rx of VLAN-sized frames.
610a3814acfSSam Leffler 			 */
611a3814acfSSam Leffler 			p->if_capenable &= ~IFCAP_VLAN_MTU;
612a3814acfSSam Leffler 			if (p->if_flags & IFF_UP) {
613a3814acfSSam Leffler 				struct ifreq ifr;
614a3814acfSSam Leffler 
615a3814acfSSam Leffler 				ifr.ifr_flags = p->if_flags;
616a3814acfSSam Leffler 				(*p->if_ioctl)(p, SIOCSIFFLAGS, (caddr_t) &ifr);
617a3814acfSSam Leffler 			}
618a3814acfSSam Leffler 		}
6191b2a4f7aSBill Fenner 	}
620f731f104SBill Paul 
621f731f104SBill Paul 	/* Disconnect from parent. */
622f731f104SBill Paul 	ifv->ifv_p = NULL;
623a3814acfSSam Leffler 	ifv->ifv_if.if_mtu = ETHERMTU;		/* XXX why not 0? */
624a3814acfSSam Leffler 	ifv->ifv_flags = 0;
625f731f104SBill Paul 
626f731f104SBill Paul 	/* Clear our MAC address. */
627f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifv->ifv_if.if_index);
628f731f104SBill Paul 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
629f731f104SBill Paul 	sdl->sdl_type = IFT_ETHER;
630f731f104SBill Paul 	sdl->sdl_alen = ETHER_ADDR_LEN;
631f731f104SBill Paul 	bzero(LLADDR(sdl), ETHER_ADDR_LEN);
632f731f104SBill Paul 	bzero(ifv->ifv_ac.ac_enaddr, ETHER_ADDR_LEN);
633f731f104SBill Paul 
634f731f104SBill Paul 	return 0;
635f731f104SBill Paul }
636f731f104SBill Paul 
637f731f104SBill Paul static int
638a3814acfSSam Leffler vlan_set_promisc(struct ifnet *ifp)
639a3814acfSSam Leffler {
640a3814acfSSam Leffler 	struct ifvlan *ifv = ifp->if_softc;
641a3814acfSSam Leffler 	int error = 0;
642a3814acfSSam Leffler 
643a3814acfSSam Leffler 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
644a3814acfSSam Leffler 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
645a3814acfSSam Leffler 			error = ifpromisc(ifv->ifv_p, 1);
646a3814acfSSam Leffler 			if (error == 0)
647a3814acfSSam Leffler 				ifv->ifv_flags |= IFVF_PROMISC;
648a3814acfSSam Leffler 		}
649a3814acfSSam Leffler 	} else {
650a3814acfSSam Leffler 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
651a3814acfSSam Leffler 			error = ifpromisc(ifv->ifv_p, 0);
652a3814acfSSam Leffler 			if (error == 0)
653a3814acfSSam Leffler 				ifv->ifv_flags &= ~IFVF_PROMISC;
654a3814acfSSam Leffler 		}
655a3814acfSSam Leffler 	}
656a3814acfSSam Leffler 
657a3814acfSSam Leffler 	return (error);
658a3814acfSSam Leffler }
659a3814acfSSam Leffler 
660a3814acfSSam Leffler static int
661cfe8b629SGarrett Wollman vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
6622cc2df49SGarrett Wollman {
6632cc2df49SGarrett Wollman 	struct ifaddr *ifa;
6642cc2df49SGarrett Wollman 	struct ifnet *p;
6652cc2df49SGarrett Wollman 	struct ifreq *ifr;
6662cc2df49SGarrett Wollman 	struct ifvlan *ifv;
6672cc2df49SGarrett Wollman 	struct vlanreq vlr;
6682cc2df49SGarrett Wollman 	int error = 0;
6692cc2df49SGarrett Wollman 
6702cc2df49SGarrett Wollman 	ifr = (struct ifreq *)data;
6712cc2df49SGarrett Wollman 	ifa = (struct ifaddr *)data;
6722cc2df49SGarrett Wollman 	ifv = ifp->if_softc;
6732cc2df49SGarrett Wollman 
6742cc2df49SGarrett Wollman 	switch (cmd) {
6752cc2df49SGarrett Wollman 	case SIOCSIFADDR:
6762cc2df49SGarrett Wollman 		ifp->if_flags |= IFF_UP;
6772cc2df49SGarrett Wollman 
6782cc2df49SGarrett Wollman 		switch (ifa->ifa_addr->sa_family) {
6792cc2df49SGarrett Wollman #ifdef INET
6802cc2df49SGarrett Wollman 		case AF_INET:
681322dcb8dSMax Khon 			arp_ifinit(&ifv->ifv_if, ifa);
6822cc2df49SGarrett Wollman 			break;
6832cc2df49SGarrett Wollman #endif
6842cc2df49SGarrett Wollman 		default:
6852cc2df49SGarrett Wollman 			break;
6862cc2df49SGarrett Wollman 		}
6872cc2df49SGarrett Wollman 		break;
6882cc2df49SGarrett Wollman 
6892cc2df49SGarrett Wollman 	case SIOCGIFADDR:
6902cc2df49SGarrett Wollman 		{
6912cc2df49SGarrett Wollman 			struct sockaddr *sa;
6922cc2df49SGarrett Wollman 
6932cc2df49SGarrett Wollman 			sa = (struct sockaddr *) &ifr->ifr_data;
6942cc2df49SGarrett Wollman 			bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
6952cc2df49SGarrett Wollman 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
6962cc2df49SGarrett Wollman 		}
6972cc2df49SGarrett Wollman 		break;
6982cc2df49SGarrett Wollman 
699b3cca108SBill Fenner 	case SIOCGIFMEDIA:
7004faedfe8SSam Leffler 		VLAN_LOCK();
701b3cca108SBill Fenner 		if (ifv->ifv_p != NULL) {
7024faedfe8SSam Leffler 			error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
7034faedfe8SSam Leffler 					SIOCGIFMEDIA, data);
7044faedfe8SSam Leffler 			VLAN_UNLOCK();
705b3cca108SBill Fenner 			/* Limit the result to the parent's current config. */
706b3cca108SBill Fenner 			if (error == 0) {
707b3cca108SBill Fenner 				struct ifmediareq *ifmr;
708b3cca108SBill Fenner 
709b3cca108SBill Fenner 				ifmr = (struct ifmediareq *) data;
710b3cca108SBill Fenner 				if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
711b3cca108SBill Fenner 					ifmr->ifm_count = 1;
712b3cca108SBill Fenner 					error = copyout(&ifmr->ifm_current,
713b3cca108SBill Fenner 						ifmr->ifm_ulist,
714b3cca108SBill Fenner 						sizeof(int));
715b3cca108SBill Fenner 				}
716b3cca108SBill Fenner 			}
7174faedfe8SSam Leffler 		} else {
7184faedfe8SSam Leffler 			VLAN_UNLOCK();
719b3cca108SBill Fenner 			error = EINVAL;
7204faedfe8SSam Leffler 		}
721b3cca108SBill Fenner 		break;
722b3cca108SBill Fenner 
723b3cca108SBill Fenner 	case SIOCSIFMEDIA:
724b3cca108SBill Fenner 		error = EINVAL;
725b3cca108SBill Fenner 		break;
726b3cca108SBill Fenner 
7272cc2df49SGarrett Wollman 	case SIOCSIFMTU:
7282cc2df49SGarrett Wollman 		/*
7292cc2df49SGarrett Wollman 		 * Set the interface MTU.
7302cc2df49SGarrett Wollman 		 */
7314faedfe8SSam Leffler 		VLAN_LOCK();
732a3814acfSSam Leffler 		if (ifv->ifv_p != NULL) {
733a3814acfSSam Leffler 			if (ifr->ifr_mtu >
734a3814acfSSam Leffler 			     (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
735a3814acfSSam Leffler 			    ifr->ifr_mtu <
736a3814acfSSam Leffler 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
7372cc2df49SGarrett Wollman 				error = EINVAL;
738a3814acfSSam Leffler 			else
7392cc2df49SGarrett Wollman 				ifp->if_mtu = ifr->ifr_mtu;
740a3814acfSSam Leffler 		} else
741a3814acfSSam Leffler 			error = EINVAL;
7424faedfe8SSam Leffler 		VLAN_UNLOCK();
7432cc2df49SGarrett Wollman 		break;
7442cc2df49SGarrett Wollman 
7452cc2df49SGarrett Wollman 	case SIOCSETVLAN:
7462cc2df49SGarrett Wollman 		error = copyin(ifr->ifr_data, &vlr, sizeof vlr);
7472cc2df49SGarrett Wollman 		if (error)
7482cc2df49SGarrett Wollman 			break;
7492cc2df49SGarrett Wollman 		if (vlr.vlr_parent[0] == '\0') {
7504faedfe8SSam Leffler 			VLAN_LOCK();
751f731f104SBill Paul 			vlan_unconfig(ifp);
7524faedfe8SSam Leffler 			if (ifp->if_flags & IFF_UP)
7532cc2df49SGarrett Wollman 				if_down(ifp);
75424993214SYaroslav Tykhiy 			ifp->if_flags &= ~IFF_RUNNING;
7554faedfe8SSam Leffler 			VLAN_UNLOCK();
7562cc2df49SGarrett Wollman 			break;
7572cc2df49SGarrett Wollman 		}
7582cc2df49SGarrett Wollman 		p = ifunit(vlr.vlr_parent);
7592cc2df49SGarrett Wollman 		if (p == 0) {
7602cc2df49SGarrett Wollman 			error = ENOENT;
7612cc2df49SGarrett Wollman 			break;
7622cc2df49SGarrett Wollman 		}
763fb88a3e0SBill Paul 		/*
764fb88a3e0SBill Paul 		 * Don't let the caller set up a VLAN tag with
765fb88a3e0SBill Paul 		 * anything except VLID bits.
766fb88a3e0SBill Paul 		 */
767fb88a3e0SBill Paul 		if (vlr.vlr_tag & ~EVL_VLID_MASK) {
768fb88a3e0SBill Paul 			error = EINVAL;
769fb88a3e0SBill Paul 			break;
770fb88a3e0SBill Paul 		}
7714faedfe8SSam Leffler 		VLAN_LOCK();
7722cc2df49SGarrett Wollman 		error = vlan_config(ifv, p);
7734faedfe8SSam Leffler 		if (error) {
7744faedfe8SSam Leffler 			VLAN_UNLOCK();
7752cc2df49SGarrett Wollman 			break;
7764faedfe8SSam Leffler 		}
7772cc2df49SGarrett Wollman 		ifv->ifv_tag = vlr.vlr_tag;
778ae290324SJordan K. Hubbard 		ifp->if_flags |= IFF_RUNNING;
7794faedfe8SSam Leffler 		VLAN_UNLOCK();
780a3814acfSSam Leffler 
781a3814acfSSam Leffler 		/* Update promiscuous mode, if necessary. */
782a3814acfSSam Leffler 		vlan_set_promisc(ifp);
7832cc2df49SGarrett Wollman 		break;
7842cc2df49SGarrett Wollman 
7852cc2df49SGarrett Wollman 	case SIOCGETVLAN:
7862cc2df49SGarrett Wollman 		bzero(&vlr, sizeof vlr);
7874faedfe8SSam Leffler 		VLAN_LOCK();
7882cc2df49SGarrett Wollman 		if (ifv->ifv_p) {
7899bf40edeSBrooks Davis 			strlcpy(vlr.vlr_parent, ifv->ifv_p->if_xname,
7909bf40edeSBrooks Davis 			    sizeof(vlr.vlr_parent));
7912cc2df49SGarrett Wollman 			vlr.vlr_tag = ifv->ifv_tag;
7922cc2df49SGarrett Wollman 		}
7934faedfe8SSam Leffler 		VLAN_UNLOCK();
7942cc2df49SGarrett Wollman 		error = copyout(&vlr, ifr->ifr_data, sizeof vlr);
7952cc2df49SGarrett Wollman 		break;
7962cc2df49SGarrett Wollman 
7972cc2df49SGarrett Wollman 	case SIOCSIFFLAGS:
7982cc2df49SGarrett Wollman 		/*
799a3814acfSSam Leffler 		 * For promiscuous mode, we enable promiscuous mode on
800a3814acfSSam Leffler 		 * the parent if we need promiscuous on the VLAN interface.
8012cc2df49SGarrett Wollman 		 */
802a3814acfSSam Leffler 		if (ifv->ifv_p != NULL)
803a3814acfSSam Leffler 			error = vlan_set_promisc(ifp);
8042cc2df49SGarrett Wollman 		break;
805a3814acfSSam Leffler 
806f731f104SBill Paul 	case SIOCADDMULTI:
807f731f104SBill Paul 	case SIOCDELMULTI:
808f731f104SBill Paul 		error = vlan_setmulti(ifp);
809f731f104SBill Paul 		break;
8102cc2df49SGarrett Wollman 	default:
8112cc2df49SGarrett Wollman 		error = EINVAL;
8122cc2df49SGarrett Wollman 	}
8132cc2df49SGarrett Wollman 	return error;
8142cc2df49SGarrett Wollman }
815