Home | History | Annotate | Line # | Download | only in net
if_vlan.c revision 1.30.2.2
      1 /*	$NetBSD: if_vlan.c,v 1.30.2.2 2001/04/09 01:58:16 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright 1998 Massachusetts Institute of Technology
     41  *
     42  * Permission to use, copy, modify, and distribute this software and
     43  * its documentation for any purpose and without fee is hereby
     44  * granted, provided that both the above copyright notice and this
     45  * permission notice appear in all copies, that both the above
     46  * copyright notice and this permission notice appear in all
     47  * supporting documentation, and that the name of M.I.T. not be used
     48  * in advertising or publicity pertaining to distribution of the
     49  * software without specific, written prior permission.  M.I.T. makes
     50  * no representations about the suitability of this software for any
     51  * purpose.  It is provided "as is" without express or implied
     52  * warranty.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
     55  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
     56  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     57  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
     58  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     60  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     61  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     62  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     63  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     64  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
     68  * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
     69  */
     70 
     71 /*
     72  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.  Might be
     73  * extended some day to also handle IEEE 802.1P priority tagging.  This is
     74  * sort of sneaky in the implementation, since we need to pretend to be
     75  * enough of an Ethernet implementation to make ARP work.  The way we do
     76  * this is by telling everyone that we are an Ethernet interface, and then
     77  * catch the packets that ether_output() left on our output queue when it
     78  * calls if_start(), rewrite them for use by the real outgoing interface,
     79  * and ask it to send them.
     80  *
     81  * TODO:
     82  *
     83  *	- Need some way to notify vlan interfaces when the parent
     84  *	  interface changes MTU.
     85  */
     86 
     87 #include "opt_inet.h"
     88 #include "bpfilter.h"
     89 
     90 #include <sys/param.h>
     91 #include <sys/kernel.h>
     92 #include <sys/mbuf.h>
     93 #include <sys/queue.h>
     94 #include <sys/socket.h>
     95 #include <sys/sockio.h>
     96 #include <sys/systm.h>
     97 #include <sys/lwp.h>
     98 #include <sys/proc.h>
     99 
    100 #if NBPFILTER > 0
    101 #include <net/bpf.h>
    102 #endif
    103 #include <net/if.h>
    104 #include <net/if_dl.h>
    105 #include <net/if_types.h>
    106 #include <net/if_ether.h>
    107 #include <net/if_vlanvar.h>
    108 
    109 #ifdef INET
    110 #include <netinet/in.h>
    111 #include <netinet/if_inarp.h>
    112 #endif
    113 
    114 struct vlan_mc_entry {
    115 	LIST_ENTRY(vlan_mc_entry)	mc_entries;
    116 	/*
    117 	 * A key to identify this entry.  The mc_addr below can't be
    118 	 * used since multiple sockaddr may mapped into the same
    119 	 * ether_multi (e.g., AF_UNSPEC).
    120 	 */
    121 	union {
    122 		struct ether_multi	*mcu_enm;
    123 	} mc_u;
    124 	struct sockaddr_storage		mc_addr;
    125 };
    126 
    127 #define	mc_enm		mc_u.mcu_enm
    128 
    129 struct ifvlan {
    130 	union {
    131 		struct ethercom ifvu_ec;
    132 	} ifv_u;
    133 	struct ifnet *ifv_p;	/* parent interface of this vlan */
    134 	struct ifv_linkmib {
    135 		const struct vlan_multisw *ifvm_msw;
    136 		int	ifvm_encaplen;	/* encapsulation length */
    137 		int	ifvm_mtufudge;	/* MTU fudged by this much */
    138 		int	ifvm_mintu;	/* min transmission unit */
    139 		u_int16_t ifvm_proto;	/* encapsulation ethertype */
    140 		u_int16_t ifvm_tag;	/* tag to apply on packets */
    141 	} ifv_mib;
    142 	LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
    143 	LIST_ENTRY(ifvlan) ifv_list;
    144 	int ifv_flags;
    145 };
    146 
    147 #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
    148 
    149 #define	ifv_ec		ifv_u.ifvu_ec
    150 
    151 #define	ifv_if		ifv_ec.ec_if
    152 
    153 #define	ifv_msw		ifv_mib.ifvm_msw
    154 #define	ifv_encaplen	ifv_mib.ifvm_encaplen
    155 #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
    156 #define	ifv_mintu	ifv_mib.ifvm_mintu
    157 #define	ifv_tag		ifv_mib.ifvm_tag
    158 
    159 struct vlan_multisw {
    160 	int	(*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
    161 	int	(*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
    162 	void	(*vmsw_purgemulti)(struct ifvlan *);
    163 };
    164 
    165 static int	vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
    166 static int	vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
    167 static void	vlan_ether_purgemulti(struct ifvlan *);
    168 
    169 const struct vlan_multisw vlan_ether_multisw = {
    170 	vlan_ether_addmulti,
    171 	vlan_ether_delmulti,
    172 	vlan_ether_purgemulti,
    173 };
    174 
    175 static int	vlan_clone_create(struct if_clone *, int);
    176 static void	vlan_clone_destroy(struct ifnet *);
    177 static int	vlan_config(struct ifvlan *, struct ifnet *);
    178 static int	vlan_ioctl(struct ifnet *, u_long, caddr_t);
    179 static void	vlan_start(struct ifnet *);
    180 static void	vlan_unconfig(struct ifnet *);
    181 
    182 void		vlanattach(int);
    183 
    184 /* XXX This should be a hash table with the tag as the basis of the key. */
    185 static LIST_HEAD(, ifvlan) ifv_list;
    186 
    187 struct if_clone vlan_cloner =
    188     IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
    189 
    190 void
    191 vlanattach(int n)
    192 {
    193 
    194 	LIST_INIT(&ifv_list);
    195 	if_clone_attach(&vlan_cloner);
    196 }
    197 
    198 static void
    199 vlan_reset_linkname(struct ifnet *ifp)
    200 {
    201 
    202 	/*
    203 	 * We start out with a "802.1Q VLAN" type and zero-length
    204 	 * addresses.  When we attach to a parent interface, we
    205 	 * inherit its type, address length, address, and data link
    206 	 * type.
    207 	 */
    208 
    209 	ifp->if_type = IFT_L2VLAN;
    210 	ifp->if_addrlen = 0;
    211 	ifp->if_dlt = DLT_NULL;
    212 	if_alloc_sadl(ifp);
    213 }
    214 
    215 static int
    216 vlan_clone_create(struct if_clone *ifc, int unit)
    217 {
    218 	struct ifvlan *ifv;
    219 	struct ifnet *ifp;
    220 	int s;
    221 
    222 	ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK);
    223 	memset(ifv, 0, sizeof(struct ifvlan));
    224 	ifp = &ifv->ifv_if;
    225 	LIST_INIT(&ifv->ifv_mc_listhead);
    226 
    227 	s = splnet();
    228 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
    229 	splx(s);
    230 
    231 	sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
    232 	ifp->if_softc = ifv;
    233 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    234 	ifp->if_start = vlan_start;
    235 	ifp->if_ioctl = vlan_ioctl;
    236 	IFQ_SET_READY(&ifp->if_snd);
    237 
    238 	if_attach(ifp);
    239 	vlan_reset_linkname(ifp);
    240 
    241 	return (0);
    242 }
    243 
    244 static void
    245 vlan_clone_destroy(struct ifnet *ifp)
    246 {
    247 	struct ifvlan *ifv = ifp->if_softc;
    248 	int s;
    249 
    250 	s = splnet();
    251 	LIST_REMOVE(ifv, ifv_list);
    252 	vlan_unconfig(ifp);
    253 	splx(s);
    254 
    255 	if_detach(ifp);
    256 	free(ifv, M_DEVBUF);
    257 }
    258 
    259 /*
    260  * Configure a VLAN interface.  Must be called at splnet().
    261  */
    262 static int
    263 vlan_config(struct ifvlan *ifv, struct ifnet *p)
    264 {
    265 	struct ifnet *ifp = &ifv->ifv_if;
    266 	int error;
    267 
    268 	if (ifv->ifv_p != NULL)
    269 		return (EBUSY);
    270 
    271 	switch (p->if_type) {
    272 	case IFT_ETHER:
    273 	    {
    274 		struct ethercom *ec = (void *) p;
    275 
    276 		ifv->ifv_msw = &vlan_ether_multisw;
    277 		ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
    278 		ifv->ifv_mintu = ETHERMIN;
    279 
    280 		/*
    281 		 * If the parent supports the VLAN_MTU capability,
    282 		 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
    283 		 * enable it.
    284 		 */
    285 		if (ec->ec_nvlans++ == 0 &&
    286 		    (ec->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
    287 			/*
    288 			 * Enable Tx/Rx of VLAN-sized frames.
    289 			 */
    290 			ec->ec_capenable |= ETHERCAP_VLAN_MTU;
    291 			if (p->if_flags & IFF_UP) {
    292 				struct ifreq ifr;
    293 
    294 				ifr.ifr_flags = p->if_flags;
    295 				error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
    296 				    (caddr_t) &ifr);
    297 				if (error) {
    298 					if (ec->ec_nvlans-- == 1)
    299 						ec->ec_capenable &=
    300 						    ~ETHERCAP_VLAN_MTU;
    301 					return (error);
    302 				}
    303 			}
    304 			ifv->ifv_mtufudge = 0;
    305 		} else if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) {
    306 			/*
    307 			 * Fudge the MTU by the encapsulation size.  This
    308 			 * makes us incompatible with strictly compliant
    309 			 * 802.1Q implementations, but allows us to use
    310 			 * the feature with other NetBSD implementations,
    311 			 * which might still be useful.
    312 			 */
    313 			ifv->ifv_mtufudge = ifv->ifv_encaplen;
    314 		}
    315 
    316 		/*
    317 		 * We inherit the parent's Ethernet address.
    318 		 */
    319 		ether_ifattach(ifp, LLADDR(p->if_sadl));
    320 		ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
    321 		break;
    322 	    }
    323 
    324 	default:
    325 		return (EPROTONOSUPPORT);
    326 	}
    327 
    328 	ifv->ifv_p = p;
    329 	ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
    330 	ifv->ifv_if.if_flags = p->if_flags &
    331 	    (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
    332 
    333 	/*
    334 	 * Inherit the if_type from the parent.  This allows us
    335 	 * to participate in bridges of that type.
    336 	 */
    337 	ifv->ifv_if.if_type = p->if_type;
    338 
    339 	return (0);
    340 }
    341 
    342 /*
    343  * Unconfigure a VLAN interface.  Must be called at splnet().
    344  */
    345 static void
    346 vlan_unconfig(struct ifnet *ifp)
    347 {
    348 	struct ifvlan *ifv = ifp->if_softc;
    349 
    350 	if (ifv->ifv_p == NULL)
    351 		return;
    352 
    353 	/*
    354  	 * Since the interface is being unconfigured, we need to empty the
    355 	 * list of multicast groups that we may have joined while we were
    356 	 * alive and remove them from the parent's list also.
    357 	 */
    358 	(*ifv->ifv_msw->vmsw_purgemulti)(ifv);
    359 
    360 	/* Disconnect from parent. */
    361 	switch (ifv->ifv_p->if_type) {
    362 	case IFT_ETHER:
    363 	    {
    364 		struct ethercom *ec = (void *) ifv->ifv_p;
    365 
    366 		if (ec->ec_nvlans-- == 1) {
    367 			/*
    368 			 * Disable Tx/Rx of VLAN-sized frames.
    369 			 */
    370 			ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
    371 			if (ifv->ifv_p->if_flags & IFF_UP) {
    372 				struct ifreq ifr;
    373 
    374 				ifr.ifr_flags = ifv->ifv_p->if_flags;
    375 				(void) (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
    376 				    SIOCSIFFLAGS, (caddr_t) &ifr);
    377 			}
    378 		}
    379 
    380 		ether_ifdetach(ifp);
    381 		vlan_reset_linkname(ifp);
    382 		break;
    383 	    }
    384 
    385 #ifdef DIAGNOSTIC
    386 	default:
    387 		panic("vlan_unconfig: impossible");
    388 #endif
    389 	}
    390 
    391 	ifv->ifv_p = NULL;
    392 	ifv->ifv_if.if_mtu = 0;
    393 	ifv->ifv_flags = 0;
    394 
    395 	if_down(ifp);
    396 	ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
    397 }
    398 
    399 /*
    400  * Called when a parent interface is detaching; destroy any VLAN
    401  * configuration for the parent interface.
    402  */
    403 void
    404 vlan_ifdetach(struct ifnet *p)
    405 {
    406 	struct ifvlan *ifv;
    407 	int s;
    408 
    409 	s = splnet();
    410 
    411 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    412 	     ifv = LIST_NEXT(ifv, ifv_list)) {
    413 		if (ifv->ifv_p == p)
    414 			vlan_unconfig(&ifv->ifv_if);
    415 	}
    416 
    417 	splx(s);
    418 }
    419 
    420 static int
    421 vlan_set_promisc(struct ifnet *ifp)
    422 {
    423 	struct ifvlan *ifv = ifp->if_softc;
    424 	int error = 0;
    425 
    426 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    427 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
    428 			error = ifpromisc(ifv->ifv_p, 1);
    429 			if (error == 0)
    430 				ifv->ifv_flags |= IFVF_PROMISC;
    431 		}
    432 	} else {
    433 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
    434 			error = ifpromisc(ifv->ifv_p, 0);
    435 			if (error == 0)
    436 				ifv->ifv_flags &= ~IFVF_PROMISC;
    437 		}
    438 	}
    439 
    440 	return (error);
    441 }
    442 
    443 static int
    444 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    445 {
    446 	struct proc *p = curproc->l_proc;	/* XXX */
    447 	struct ifvlan *ifv = ifp->if_softc;
    448 	struct ifaddr *ifa = (struct ifaddr *) data;
    449 	struct ifreq *ifr = (struct ifreq *) data;
    450 	struct ifnet *pr;
    451 	struct vlanreq vlr;
    452 	struct sockaddr *sa;
    453 	int s, error = 0;
    454 
    455 	s = splnet();
    456 
    457 	switch (cmd) {
    458 	case SIOCSIFADDR:
    459 		if (ifv->ifv_p != NULL) {
    460 			ifp->if_flags |= IFF_UP;
    461 
    462 			switch (ifa->ifa_addr->sa_family) {
    463 #ifdef INET
    464 			case AF_INET:
    465 				arp_ifinit(ifp, ifa);
    466 				break;
    467 #endif
    468 			default:
    469 				break;
    470 			}
    471 		} else {
    472 			error = EINVAL;
    473 		}
    474 		break;
    475 
    476 	case SIOCGIFADDR:
    477 		sa = (struct sockaddr *)&ifr->ifr_data;
    478 		memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ifp->if_addrlen);
    479 		break;
    480 
    481 	case SIOCSIFMTU:
    482 		if (ifv->ifv_p != NULL) {
    483 			if (ifr->ifr_mtu >
    484 			     (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
    485 			    ifr->ifr_mtu <
    486 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
    487 				error = EINVAL;
    488 			else
    489 				ifp->if_mtu = ifr->ifr_mtu;
    490 		} else
    491 			error = EINVAL;
    492 		break;
    493 
    494 	case SIOCSETVLAN:
    495 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    496 			break;
    497 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
    498 			break;
    499 		if (vlr.vlr_parent[0] == '\0') {
    500 			vlan_unconfig(ifp);
    501 			break;
    502 		}
    503 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
    504 			error = EINVAL;		 /* check for valid tag */
    505 			break;
    506 		}
    507 		if ((pr = ifunit(vlr.vlr_parent)) == 0) {
    508 			error = ENOENT;
    509 			break;
    510 		}
    511 		if ((error = vlan_config(ifv, pr)) != 0)
    512 			break;
    513 		ifv->ifv_tag = vlr.vlr_tag;
    514 		ifp->if_flags |= IFF_RUNNING;
    515 
    516 		/* Update promiscuous mode, if necessary. */
    517 		vlan_set_promisc(ifp);
    518 		break;
    519 
    520 	case SIOCGETVLAN:
    521 		memset(&vlr, 0, sizeof(vlr));
    522 		if (ifv->ifv_p != NULL) {
    523 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
    524 			    ifv->ifv_p->if_xname);
    525 			vlr.vlr_tag = ifv->ifv_tag;
    526 		}
    527 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
    528 		break;
    529 
    530 	case SIOCSIFFLAGS:
    531 		/*
    532 		 * For promiscuous mode, we enable promiscuous mode on
    533 		 * the parent if we need promiscuous on the VLAN interface.
    534 		 */
    535 		if (ifv->ifv_p != NULL)
    536 			error = vlan_set_promisc(ifp);
    537 		break;
    538 
    539 	case SIOCADDMULTI:
    540 		error = (ifv->ifv_p != NULL) ?
    541 		    (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
    542 		break;
    543 
    544 	case SIOCDELMULTI:
    545 		error = (ifv->ifv_p != NULL) ?
    546 		    (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
    547 		break;
    548 
    549 	default:
    550 		error = EINVAL;
    551 	}
    552 
    553 	splx(s);
    554 
    555 	return (error);
    556 }
    557 
    558 static int
    559 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
    560 {
    561 	struct vlan_mc_entry *mc;
    562 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    563 	int error;
    564 
    565 	if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr_storage))
    566 		return (EINVAL);
    567 
    568 	error = ether_addmulti(ifr, &ifv->ifv_ec);
    569 	if (error != ENETRESET)
    570 		return (error);
    571 
    572 	/*
    573 	 * This is new multicast address.  We have to tell parent
    574 	 * about it.  Also, remember this multicast address so that
    575 	 * we can delete them on unconfigure.
    576 	 */
    577 	MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
    578 	    M_DEVBUF, M_NOWAIT);
    579 	if (mc == NULL) {
    580 		error = ENOMEM;
    581 		goto alloc_failed;
    582 	}
    583 
    584 	/*
    585 	 * As ether_addmulti() returns ENETRESET, following two
    586 	 * statement shouldn't fail.
    587 	 */
    588 	(void)ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
    589 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
    590 	memcpy(&mc->mc_addr, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
    591 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
    592 
    593 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
    594 	    (caddr_t)ifr);
    595 	if (error != 0)
    596 		goto ioctl_failed;
    597 	return (error);
    598 
    599  ioctl_failed:
    600 	LIST_REMOVE(mc, mc_entries);
    601 	FREE(mc, M_DEVBUF);
    602  alloc_failed:
    603 	(void)ether_delmulti(ifr, &ifv->ifv_ec);
    604 	return (error);
    605 }
    606 
    607 static int
    608 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
    609 {
    610 	struct ether_multi *enm;
    611 	struct vlan_mc_entry *mc;
    612 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    613 	int error;
    614 
    615 	/*
    616 	 * Find a key to lookup vlan_mc_entry.  We have to do this
    617 	 * before calling ether_delmulti for obvious reason.
    618 	 */
    619 	if ((error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi)) != 0)
    620 		return (error);
    621 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
    622 
    623 	error = ether_delmulti(ifr, &ifv->ifv_ec);
    624 	if (error != ENETRESET)
    625 		return (error);
    626 
    627 	/* We no longer use this multicast address.  Tell parent so. */
    628 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
    629 	    (caddr_t)ifr);
    630 	if (error == 0) {
    631 		/* And forget about this address. */
    632 		for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
    633 		    mc = LIST_NEXT(mc, mc_entries)) {
    634 			if (mc->mc_enm == enm) {
    635 				LIST_REMOVE(mc, mc_entries);
    636 				FREE(mc, M_DEVBUF);
    637 				break;
    638 			}
    639 		}
    640 		KASSERT(mc != NULL);
    641 	} else
    642 		(void)ether_addmulti(ifr, &ifv->ifv_ec);
    643 	return (error);
    644 }
    645 
    646 /*
    647  * Delete any multicast address we have asked to add form parent
    648  * interface.  Called when the vlan is being unconfigured.
    649  */
    650 static void
    651 vlan_ether_purgemulti(struct ifvlan *ifv)
    652 {
    653 	struct ifnet *ifp = ifv->ifv_p;		/* Parent. */
    654 	struct vlan_mc_entry *mc;
    655 	union {
    656 		struct ifreq ifreq;
    657 		struct {
    658 			char ifr_name[IFNAMSIZ];
    659 			struct sockaddr_storage ifr_ss;
    660 		} ifreq_storage;
    661 	} ifreq;
    662 	struct ifreq *ifr = &ifreq.ifreq;
    663 
    664 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
    665 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
    666 		memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len);
    667 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)ifr);
    668 		LIST_REMOVE(mc, mc_entries);
    669 		FREE(mc, M_DEVBUF);
    670 	}
    671 }
    672 
    673 static void
    674 vlan_start(struct ifnet *ifp)
    675 {
    676 	struct ifvlan *ifv = ifp->if_softc;
    677 	struct ifnet *p = ifv->ifv_p;
    678 	struct ethercom *ec = (void *) ifv->ifv_p;
    679 	struct mbuf *m;
    680 	int error;
    681 	ALTQ_DECL(struct altq_pktattr pktattr;)
    682 
    683 	ifp->if_flags |= IFF_OACTIVE;
    684 
    685 	for (;;) {
    686 		IFQ_DEQUEUE(&ifp->if_snd, m);
    687 		if (m == NULL)
    688 			break;
    689 
    690 #ifdef ALTQ
    691 		/*
    692 		 * If ALTQ is enabled on the parent interface, do
    693 		 * classification; the queueing discipline might
    694 		 * not require classification, but might require
    695 		 * the address family/header pointer in the pktattr.
    696 		 */
    697 		if (ALTQ_IS_ENABLED(&p->if_snd)) {
    698 			switch (p->if_type) {
    699 			case IFT_ETHER:
    700 				altq_etherclassify(&p->if_snd, m, &pktattr);
    701 				break;
    702 #ifdef DIAGNOSTIC
    703 			default:
    704 				panic("vlan_start: impossible (altq)");
    705 #endif
    706 			}
    707 		}
    708 #endif /* ALTQ */
    709 
    710 #if NBPFILTER > 0
    711 		if (ifp->if_bpf)
    712 			bpf_mtap(ifp->if_bpf, m);
    713 #endif
    714 		/*
    715 		 * If the parent can insert the tag itself, just mark
    716 		 * the tag in the mbuf header.
    717 		 */
    718 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
    719 			struct mbuf *n;
    720 			n = m_aux_add(m, AF_LINK, ETHERTYPE_VLAN);
    721 			if (n == NULL) {
    722 				ifp->if_oerrors++;
    723 				m_freem(m);
    724 				continue;
    725 			}
    726 			*mtod(n, int *) = ifv->ifv_tag;
    727 			n->m_len = sizeof(int);
    728 		} else {
    729 			/*
    730 			 * insert the tag ourselve
    731 			 */
    732 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
    733 			if (m == NULL) {
    734 				printf("%s: unable to prepend encap header",
    735 				    ifv->ifv_p->if_xname);
    736 				ifp->if_oerrors++;
    737 				continue;
    738 			}
    739 
    740 			switch (p->if_type) {
    741 			case IFT_ETHER:
    742 			    {
    743 				struct ether_vlan_header *evl;
    744 
    745 				if (m->m_len < sizeof(struct ether_vlan_header))
    746 					m = m_pullup(m,
    747 					    sizeof(struct ether_vlan_header));
    748 				if (m == NULL) {
    749 					printf("%s: unable to pullup encap "
    750 					    "header", ifv->ifv_p->if_xname);
    751 					ifp->if_oerrors++;
    752 					continue;
    753 				}
    754 
    755 				/*
    756 				 * Transform the Ethernet header into an
    757 				 * Ethernet header with 802.1Q encapsulation.
    758 				 */
    759 				memmove(mtod(m, caddr_t),
    760 				    mtod(m, caddr_t) + ifv->ifv_encaplen,
    761 				    sizeof(struct ether_header));
    762 				evl = mtod(m, struct ether_vlan_header *);
    763 				evl->evl_proto = evl->evl_encap_proto;
    764 				evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
    765 				evl->evl_tag = htons(ifv->ifv_tag);
    766 				break;
    767 			    }
    768 
    769 #ifdef DIAGNOSTIC
    770 			default:
    771 				panic("vlan_start: impossible");
    772 #endif
    773 			}
    774 		}
    775 
    776 		/*
    777 		 * Send it, precisely as the parent's output routine
    778 		 * would have.  We are already running at splimp.
    779 		 */
    780 		IFQ_ENQUEUE(&p->if_snd, m, &pktattr, error);
    781 		if (error) {
    782 			/* mbuf is already freed */
    783 			ifp->if_oerrors++;
    784 			continue;
    785 		}
    786 
    787 		ifp->if_opackets++;
    788 		if ((p->if_flags & IFF_OACTIVE) == 0)
    789 			(*p->if_start)(p);
    790 	}
    791 
    792 	ifp->if_flags &= ~IFF_OACTIVE;
    793 }
    794 
    795 /*
    796  * Given an Ethernet frame, find a valid vlan interface corresponding to the
    797  * given source interface and tag, then run the the real packet through
    798  * the parent's input routine.
    799  */
    800 void
    801 vlan_input(struct ifnet *ifp, struct mbuf *m)
    802 {
    803 	struct ifvlan *ifv;
    804 	u_int tag;
    805 	struct mbuf *n;
    806 
    807 	n = m_aux_find(m, AF_LINK, ETHERTYPE_VLAN);
    808 	if (n) {
    809 		/* m contains a normal ethernet frame, the tag is in m_aux */
    810 		tag = *mtod(n, int *);
    811 		m_aux_delete(m, n);
    812 		for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    813 		    ifv = LIST_NEXT(ifv, ifv_list))
    814 			if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
    815 				break;
    816 	} else {
    817 		switch (ifp->if_type) {
    818 		case IFT_ETHER:
    819 		    {
    820 			struct ether_vlan_header *evl;
    821 
    822 			if (m->m_len < sizeof(struct ether_vlan_header) &&
    823 			    (m = m_pullup(m,
    824 			     sizeof(struct ether_vlan_header))) == NULL) {
    825 				printf("%s: no memory for VLAN header, "
    826 				    "dropping packet.\n", ifp->if_xname);
    827 				return;
    828 			}
    829 			evl = mtod(m, struct ether_vlan_header *);
    830 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
    831 
    832 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
    833 
    834 			/*
    835 			 * Restore the original ethertype.  We'll remove
    836 			 * the encapsulation after we've found the vlan
    837 			 * interface corresponding to the tag.
    838 			 */
    839 			evl->evl_encap_proto = evl->evl_proto;
    840 			break;
    841 		    }
    842 
    843 		default:
    844 			tag = (u_int) -1;	/* XXX GCC */
    845 #ifdef DIAGNOSTIC
    846 			panic("vlan_input: impossible");
    847 #endif
    848 		}
    849 
    850 		for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    851 		     ifv = LIST_NEXT(ifv, ifv_list))
    852 			if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
    853 				break;
    854 
    855 
    856 		/*
    857 		 * Now, remove the encapsulation header.  The original
    858 		 * header has already been fixed up above.
    859 		 */
    860 		if (ifv) {
    861 			memmove(mtod(m, caddr_t) + ifv->ifv_encaplen,
    862 			    mtod(m, caddr_t), sizeof(struct ether_header));
    863 			m_adj(m, ifv->ifv_encaplen);
    864 		}
    865 	}
    866 
    867 	if (ifv == NULL ||
    868 	    (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
    869 	     (IFF_UP|IFF_RUNNING)) {
    870 		m_free(m);
    871 		ifp->if_noproto++;
    872 		return;
    873 	}
    874 	m->m_pkthdr.rcvif = &ifv->ifv_if;
    875 	ifv->ifv_if.if_ipackets++;
    876 
    877 #if NBPFILTER > 0
    878 	if (ifv->ifv_if.if_bpf)
    879 		bpf_mtap(ifv->ifv_if.if_bpf, m);
    880 #endif
    881 
    882 	/* Pass it back through the parent's input routine. */
    883 	(*ifp->if_input)(&ifv->ifv_if, m);
    884 }
    885