Home | History | Annotate | Line # | Download | only in net
if_vlan.c revision 1.21
      1 /*	$NetBSD: if_vlan.c,v 1.21 2000/11/12 19:39:42 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 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  *	- Need a way to facilitate parent interfaces that can do
     87  *	  tag insertion and/or extraction in hardware.
     88  */
     89 
     90 #include "opt_inet.h"
     91 #include "bpfilter.h"
     92 
     93 #include <sys/param.h>
     94 #include <sys/kernel.h>
     95 #include <sys/mbuf.h>
     96 #include <sys/queue.h>
     97 #include <sys/socket.h>
     98 #include <sys/sockio.h>
     99 #include <sys/systm.h>
    100 #include <sys/proc.h>
    101 
    102 #if NBPFILTER > 0
    103 #include <net/bpf.h>
    104 #endif
    105 #include <net/if.h>
    106 #include <net/if_dl.h>
    107 #include <net/if_types.h>
    108 #include <net/if_ether.h>
    109 #include <net/if_vlanvar.h>
    110 
    111 #ifdef INET
    112 #include <netinet/in.h>
    113 #include <netinet/if_inarp.h>
    114 #endif
    115 
    116 extern struct	ifaddr **ifnet_addrs;	/* XXX if.c */
    117 
    118 struct vlan_mc_entry {
    119 	LIST_ENTRY(vlan_mc_entry)	mc_entries;
    120 	/*
    121 	 * A key to identify this entry.  The mc_addr below can't be
    122 	 * used since multiple sockaddr may mapped into the same
    123 	 * ether_multi (e.g., AF_UNSPEC).
    124 	 */
    125 	union {
    126 		struct ether_multi	*mcu_enm;
    127 	} mc_u;
    128 	struct sockaddr_storage		mc_addr;
    129 };
    130 
    131 #define	mc_enm		mc_u.mcu_enm
    132 
    133 struct ifvlan {
    134 	union {
    135 		struct ethercom ifvu_ec;
    136 	} ifv_u;
    137 	struct ifnet *ifv_p;	/* parent interface of this vlan */
    138 	struct ifv_linkmib {
    139 		const struct vlan_multisw *ifvm_msw;
    140 		int	ifvm_encaplen;	/* encapsulation length */
    141 		int	ifvm_mtufudge;	/* MTU fudged by this much */
    142 		int	ifvm_mintu;	/* min transmission unit */
    143 		u_int16_t ifvm_proto;	/* encapsulation ethertype */
    144 		u_int16_t ifvm_tag;	/* tag to apply on packets */
    145 	} ifv_mib;
    146 	LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
    147 	LIST_ENTRY(ifvlan) ifv_list;
    148 	int ifv_flags;
    149 };
    150 
    151 #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
    152 
    153 #define	ifv_ec		ifv_u.ifvu_ec
    154 
    155 #define	ifv_if		ifv_ec.ec_if
    156 
    157 #define	ifv_msw		ifv_mib.ifvm_msw
    158 #define	ifv_encaplen	ifv_mib.ifvm_encaplen
    159 #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
    160 #define	ifv_mintu	ifv_mib.ifvm_mintu
    161 #define	ifv_tag		ifv_mib.ifvm_tag
    162 
    163 struct vlan_multisw {
    164 	int	(*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
    165 	int	(*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
    166 	void	(*vmsw_purgemulti)(struct ifvlan *);
    167 };
    168 
    169 static int	vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
    170 static int	vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
    171 static void	vlan_ether_purgemulti(struct ifvlan *);
    172 
    173 const struct vlan_multisw vlan_ether_multisw = {
    174 	vlan_ether_addmulti,
    175 	vlan_ether_delmulti,
    176 	vlan_ether_purgemulti,
    177 };
    178 
    179 static int	vlan_clone_create(struct if_clone *, int);
    180 static void	vlan_clone_destroy(struct ifnet *);
    181 static int	vlan_config(struct ifvlan *, struct ifnet *);
    182 static int	vlan_ioctl(struct ifnet *, u_long, caddr_t);
    183 static void	vlan_start(struct ifnet *);
    184 static void	vlan_unconfig(struct ifnet *);
    185 
    186 void		vlanattach(int);
    187 
    188 /* XXX This should be a hash table with the tag as the basis of the key. */
    189 static LIST_HEAD(, ifvlan) ifv_list;
    190 
    191 struct if_clone vlan_cloner =
    192     IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
    193 
    194 void
    195 vlanattach(int n)
    196 {
    197 
    198 	LIST_INIT(&ifv_list);
    199 	if_clone_attach(&vlan_cloner);
    200 }
    201 
    202 static int
    203 vlan_clone_create(struct if_clone *ifc, int unit)
    204 {
    205 	struct ifvlan *ifv;
    206 	struct ifnet *ifp;
    207 	int s;
    208 
    209 	ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK);
    210 	memset(ifv, 0, sizeof(struct ifvlan));
    211 	ifp = &ifv->ifv_if;
    212 	LIST_INIT(&ifv->ifv_mc_listhead);
    213 
    214 	s = splnet();
    215 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
    216 	splx(s);
    217 
    218 	sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
    219 	ifp->if_softc = ifv;
    220 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    221 	ifp->if_start = vlan_start;
    222 	ifp->if_ioctl = vlan_ioctl;
    223 
    224 	if_attach(ifp);
    225 
    226 	return (0);
    227 }
    228 
    229 static void
    230 vlan_clone_destroy(struct ifnet *ifp)
    231 {
    232 	struct ifvlan *ifv = ifp->if_softc;
    233 	int s;
    234 
    235 	s = splnet();
    236 	LIST_REMOVE(ifv, ifv_list);
    237 	vlan_unconfig(ifp);
    238 	splx(s);
    239 
    240 	if_detach(ifp);
    241 	free(ifv, M_DEVBUF);
    242 }
    243 
    244 /*
    245  * Configure a VLAN interface.  Must be called at splnet().
    246  */
    247 static int
    248 vlan_config(struct ifvlan *ifv, struct ifnet *p)
    249 {
    250 	struct ifnet *ifp = &ifv->ifv_if;
    251 	int error;
    252 
    253 	if (ifv->ifv_p != NULL)
    254 		return (EBUSY);
    255 
    256 	switch (p->if_type) {
    257 	case IFT_ETHER:
    258 	    {
    259 		struct ethercom *ec = (void *) p;
    260 
    261 		ifv->ifv_msw = &vlan_ether_multisw;
    262 		ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
    263 		ifv->ifv_mintu = ETHERMIN;
    264 
    265 		/*
    266 		 * If the parent supports the VLAN_MTU capability,
    267 		 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
    268 		 * enable it.
    269 		 */
    270 		if (ec->ec_nvlans++ == 0 &&
    271 		    (ec->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
    272 			/*
    273 			 * Enable Tx/Rx of VLAN-sized frames.
    274 			 */
    275 			ec->ec_capenable |= ETHERCAP_VLAN_MTU;
    276 			if (p->if_flags & IFF_UP) {
    277 				struct ifreq ifr;
    278 
    279 				ifr.ifr_flags = p->if_flags;
    280 				error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
    281 				    (caddr_t) &ifr);
    282 				if (error) {
    283 					if (ec->ec_nvlans-- == 1)
    284 						ec->ec_capenable &=
    285 						    ~ETHERCAP_VLAN_MTU;
    286 					return (error);
    287 				}
    288 			}
    289 			ifv->ifv_mtufudge = 0;
    290 		} else if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) {
    291 			/*
    292 			 * Fudge the MTU by the encapsulation size.  This
    293 			 * makes us incompatible with strictly compliant
    294 			 * 802.1Q implementations, but allows us to use
    295 			 * the feature with other NetBSD implementations,
    296 			 * which might still be useful.
    297 			 */
    298 			ifv->ifv_mtufudge = ifv->ifv_encaplen;
    299 		}
    300 
    301 		/*
    302 		 * We inherit the parent's Ethernet address.
    303 		 */
    304 		ether_ifattach(ifp, LLADDR(p->if_sadl));
    305 		ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
    306 #if NBPFILTER > 0
    307 		bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    308 		    sizeof(struct ether_header));
    309 #endif
    310 		break;
    311 	    }
    312 
    313 	default:
    314 		return (EPROTONOSUPPORT);
    315 	}
    316 
    317 	ifv->ifv_p = p;
    318 	ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
    319 	ifv->ifv_if.if_flags = p->if_flags &
    320 	    (IFF_UP | IFF_BROADCAST | IFF_RUNNING | IFF_ALLMULTI | IFF_SIMPLEX);
    321 
    322 	/*
    323 	 * Inherit the if_type from the parent.  This allows us
    324 	 * to participate in bridges of that type.
    325 	 */
    326 	ifv->ifv_if.if_type = p->if_type;
    327 
    328 	return (0);
    329 }
    330 
    331 /*
    332  * Unconfigure a VLAN interface.  Must be called at splnet().
    333  */
    334 static void
    335 vlan_unconfig(struct ifnet *ifp)
    336 {
    337 	struct ifvlan *ifv = ifp->if_softc;
    338 
    339 	if (ifv->ifv_p == NULL)
    340 		return;
    341 
    342 	/*
    343  	 * Since the interface is being unconfigured, we need to empty the
    344 	 * list of multicast groups that we may have joined while we were
    345 	 * alive and remove them from the parent's list also.
    346 	 */
    347 	(*ifv->ifv_msw->vmsw_purgemulti)(ifv);
    348 
    349 	/* Disconnect from parent. */
    350 	switch (ifv->ifv_p->if_type) {
    351 	case IFT_ETHER:
    352 	    {
    353 		struct ethercom *ec = (void *) ifv->ifv_p;
    354 
    355 		if (ec->ec_nvlans-- == 1) {
    356 			/*
    357 			 * Disable Tx/Rx of VLAN-sized frames.
    358 			 */
    359 			ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
    360 			if (ifv->ifv_p->if_flags & IFF_UP) {
    361 				struct ifreq ifr;
    362 
    363 				ifr.ifr_flags = ifv->ifv_p->if_flags;
    364 				(void) (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
    365 				    SIOCSIFFLAGS, (caddr_t) &ifr);
    366 			}
    367 		}
    368 
    369 #if NBPFILTER > 0
    370 		bpfdetach(ifp);
    371 #endif
    372 		ether_ifdetach(ifp);
    373 		break;
    374 	    }
    375 
    376 #ifdef DIAGNOSTIC
    377 	default:
    378 		panic("vlan_unconfig: impossible");
    379 #endif
    380 	}
    381 
    382 	ifv->ifv_p = NULL;
    383 	ifv->ifv_if.if_mtu = 0;
    384 	ifv->ifv_flags = 0;
    385 
    386 	if_down(ifp);
    387 	ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
    388 }
    389 
    390 /*
    391  * Called when a parent interface is detaching; destroy any VLAN
    392  * configuration for the parent interface.
    393  */
    394 void
    395 vlan_ifdetach(struct ifnet *p)
    396 {
    397 	struct ifvlan *ifv;
    398 	int s;
    399 
    400 	s = splnet();
    401 
    402 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    403 	     ifv = LIST_NEXT(ifv, ifv_list)) {
    404 		if (ifv->ifv_p == p)
    405 			vlan_unconfig(&ifv->ifv_if);
    406 	}
    407 
    408 	splx(s);
    409 }
    410 
    411 static int
    412 vlan_set_promisc(struct ifnet *ifp)
    413 {
    414 	struct ifvlan *ifv = ifp->if_softc;
    415 	int error = 0;
    416 
    417 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    418 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
    419 			error = ifpromisc(ifv->ifv_p, 1);
    420 			if (error == 0)
    421 				ifv->ifv_flags |= IFVF_PROMISC;
    422 		}
    423 	} else {
    424 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
    425 			error = ifpromisc(ifv->ifv_p, 0);
    426 			if (error == 0)
    427 				ifv->ifv_flags &= ~IFVF_PROMISC;
    428 		}
    429 	}
    430 
    431 	return (error);
    432 }
    433 
    434 static int
    435 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    436 {
    437 	struct proc *p = curproc;	/* XXX */
    438 	struct ifvlan *ifv = ifp->if_softc;
    439 	struct ifaddr *ifa = (struct ifaddr *) data;
    440 	struct ifreq *ifr = (struct ifreq *) data;
    441 	struct ifnet *pr;
    442 	struct vlanreq vlr;
    443 	struct sockaddr *sa;
    444 	int s, error = 0;
    445 
    446 	s = splnet();
    447 
    448 	switch (cmd) {
    449 	case SIOCSIFADDR:
    450 		if (ifv->ifv_p != NULL) {
    451 			ifp->if_flags |= IFF_UP;
    452 
    453 			switch (ifa->ifa_addr->sa_family) {
    454 #ifdef INET
    455 			case AF_INET:
    456 				arp_ifinit(ifp, ifa);
    457 				break;
    458 #endif
    459 			default:
    460 				break;
    461 			}
    462 		} else {
    463 			error = EINVAL;
    464 		}
    465 		break;
    466 
    467 	case SIOCGIFADDR:
    468 		sa = (struct sockaddr *)&ifr->ifr_data;
    469 		memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    470 		break;
    471 
    472 	case SIOCSIFMTU:
    473 		if (ifv->ifv_p != NULL) {
    474 			if (ifr->ifr_mtu >
    475 			     (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
    476 			    ifr->ifr_mtu <
    477 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
    478 				error = EINVAL;
    479 			else
    480 				ifp->if_mtu = ifr->ifr_mtu;
    481 		} else
    482 			error = EINVAL;
    483 		break;
    484 
    485 	case SIOCSETVLAN:
    486 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    487 			break;
    488 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
    489 			break;
    490 		if (vlr.vlr_parent[0] == '\0') {
    491 			vlan_unconfig(ifp);
    492 			break;
    493 		}
    494 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
    495 			error = EINVAL;		 /* check for valid tag */
    496 			break;
    497 		}
    498 		if ((pr = ifunit(vlr.vlr_parent)) == 0) {
    499 			error = ENOENT;
    500 			break;
    501 		}
    502 		if ((error = vlan_config(ifv, pr)) != 0)
    503 			break;
    504 		ifv->ifv_tag = vlr.vlr_tag;
    505 		ifp->if_flags |= IFF_RUNNING;
    506 
    507 		/* Update promiscuous mode, if necessary. */
    508 		vlan_set_promisc(ifp);
    509 		break;
    510 
    511 	case SIOCGETVLAN:
    512 		memset(&vlr, 0, sizeof(vlr));
    513 		if (ifv->ifv_p != NULL) {
    514 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
    515 			    ifv->ifv_p->if_xname);
    516 			vlr.vlr_tag = ifv->ifv_tag;
    517 		}
    518 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
    519 		break;
    520 
    521 	case SIOCSIFFLAGS:
    522 		/*
    523 		 * For promiscuous mode, we enable promiscuous mode on
    524 		 * the parent if we need promiscuous on the VLAN interface.
    525 		 */
    526 		if (ifv->ifv_p != NULL)
    527 			error = vlan_set_promisc(ifp);
    528 		break;
    529 
    530 	case SIOCADDMULTI:
    531 		if (ifv->ifv_p != NULL) {
    532 			error = (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr);
    533 		} else {
    534 			error = EINVAL;
    535 		}
    536 		break;
    537 
    538 	case SIOCDELMULTI:
    539 		if (ifv->ifv_p != NULL) {
    540 			error = (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr);
    541 		} else {
    542 			error = EINVAL;
    543 		}
    544 		break;
    545 
    546 	default:
    547 		error = EINVAL;
    548 	}
    549 
    550 	splx(s);
    551 
    552 	return (error);
    553 }
    554 
    555 static int
    556 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
    557 {
    558 	struct vlan_mc_entry *mc;
    559 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    560 	int error;
    561 
    562 	if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr_storage))
    563 		return (EINVAL);
    564 
    565 	error = ether_addmulti(ifr, &ifv->ifv_ec);
    566 	if (error != ENETRESET)
    567 		return (error);
    568 
    569 	/*
    570 	 * This is new multicast address.  We have to tell parent
    571 	 * about it.  Also, remember this multicast address so that
    572 	 * we can delete them on unconfigure.
    573 	 */
    574 	MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
    575 	    M_DEVBUF, M_NOWAIT);
    576 	if (mc == NULL) {
    577 		error = ENOMEM;
    578 		goto alloc_failed;
    579 	}
    580 
    581 	/*
    582 	 * As ether_addmulti() returns ENETRESET, following two
    583 	 * statement shouldn't fail.
    584 	 */
    585 	(void)ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
    586 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
    587 	memcpy(&mc->mc_addr, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
    588 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
    589 
    590 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
    591 	    (caddr_t)ifr);
    592 	if (error != 0)
    593 		goto ioctl_failed;
    594 	return (error);
    595 
    596  ioctl_failed:
    597 	LIST_REMOVE(mc, mc_entries);
    598 	FREE(mc, M_DEVBUF);
    599  alloc_failed:
    600 	(void)ether_delmulti(ifr, &ifv->ifv_ec);
    601 	return (error);
    602 }
    603 
    604 static int
    605 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
    606 {
    607 	struct ether_multi *enm;
    608 	struct vlan_mc_entry *mc;
    609 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    610 	int error;
    611 
    612 	/*
    613 	 * Find a key to lookup vlan_mc_entry.  We have to do this
    614 	 * before calling ether_delmulti for obvious reason.
    615 	 */
    616 	if ((error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi)) != 0)
    617 		return (error);
    618 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
    619 
    620 	error = ether_delmulti(ifr, &ifv->ifv_ec);
    621 	if (error != ENETRESET)
    622 		return (error);
    623 
    624 	/* We no longer use this multicast address.  Tell parent so. */
    625 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
    626 	    (caddr_t)ifr);
    627 	if (error == 0) {
    628 		/* And forget about this address. */
    629 		for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
    630 		    mc = LIST_NEXT(mc, mc_entries)) {
    631 			if (mc->mc_enm == enm) {
    632 				LIST_REMOVE(mc, mc_entries);
    633 				FREE(mc, M_DEVBUF);
    634 				break;
    635 			}
    636 		}
    637 		KASSERT(mc != NULL);
    638 	} else
    639 		(void)ether_addmulti(ifr, &ifv->ifv_ec);
    640 	return (error);
    641 }
    642 
    643 /*
    644  * Delete any multicast address we have asked to add form parent
    645  * interface.  Called when the vlan is being unconfigured.
    646  */
    647 static void
    648 vlan_ether_purgemulti(struct ifvlan *ifv)
    649 {
    650 	struct ifnet *ifp = ifv->ifv_p;		/* Parent. */
    651 	struct vlan_mc_entry *mc;
    652 	union {
    653 		struct ifreq ifreq;
    654 		struct {
    655 			char ifr_name[IFNAMSIZ];
    656 			struct sockaddr_storage ifr_ss;
    657 		} ifreq_storage;
    658 	} ifreq;
    659 	struct ifreq *ifr = &ifreq.ifreq;
    660 
    661 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
    662 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
    663 		memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len);
    664 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)ifr);
    665 		LIST_REMOVE(mc, mc_entries);
    666 		FREE(mc, M_DEVBUF);
    667 	}
    668 }
    669 
    670 static void
    671 vlan_start(struct ifnet *ifp)
    672 {
    673 	struct ifvlan *ifv = ifp->if_softc;
    674 	struct ifnet *p = ifv->ifv_p;
    675 	struct mbuf *m;
    676 
    677 	ifp->if_flags |= IFF_OACTIVE;
    678 
    679 	for (;;) {
    680 		IF_DEQUEUE(&ifp->if_snd, m);
    681 		if (m == NULL)
    682 			break;
    683 
    684 #if NBPFILTER > 0
    685 		if (ifp->if_bpf)
    686 			bpf_mtap(ifp->if_bpf, m);
    687 #endif
    688 
    689 		/*
    690 		 * XXX Should handle the case where the underlying hardware
    691 		 * interface can do VLAN tag insertion itself.
    692 		 */
    693 		M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
    694 		if (m == NULL) {
    695 			printf("%s: unable to prepend encap header",
    696 			    ifv->ifv_p->if_xname);
    697 			ifp->if_oerrors++;
    698 			continue;
    699 		}
    700 
    701 		switch (p->if_type) {
    702 		case IFT_ETHER:
    703 		    {
    704 			struct ether_vlan_header *evl;
    705 
    706 			if (m->m_len < sizeof(struct ether_vlan_header) &&
    707 			    (m = m_pullup(m,
    708 			     sizeof(struct ether_vlan_header))) == NULL) {
    709 				printf("%s: unable to pullup encap header",
    710 				    ifv->ifv_p->if_xname);
    711 				ifp->if_oerrors++;
    712 				continue;
    713 			}
    714 
    715 			/*
    716 			 * Transform the Ethernet header into an Ethernet
    717 			 * header with 802.1Q encapsulation.
    718 			 */
    719 			memmove(mtod(m, caddr_t),
    720 			    mtod(m, caddr_t) + ifv->ifv_encaplen,
    721 			    sizeof(struct ether_header));
    722 			evl = mtod(m, struct ether_vlan_header *);
    723 			evl->evl_proto = evl->evl_encap_proto;
    724 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
    725 			evl->evl_tag = htons(ifv->ifv_tag);
    726 			break;
    727 		    }
    728 
    729 #ifdef DIAGNOSTIC
    730 		default:
    731 			panic("vlan_start: impossible");
    732 #endif
    733 		}
    734 
    735 		/*
    736 		 * Send it, precisely as the parent's output routine
    737 		 * would have.  We are already running at splimp.
    738 		 */
    739 		if (IF_QFULL(&p->if_snd)) {
    740 			IF_DROP(&p->if_snd);
    741 			/* XXX stats */
    742 			ifp->if_oerrors++;
    743 			m_freem(m);
    744 			continue;
    745 		}
    746 
    747 		IF_ENQUEUE(&p->if_snd, m);
    748 		if ((p->if_flags & IFF_OACTIVE) == 0) {
    749 			(*p->if_start)(p);
    750 			ifp->if_opackets++;
    751 		}
    752 	}
    753 
    754 	ifp->if_flags &= ~IFF_OACTIVE;
    755 }
    756 
    757 /*
    758  * Given an Ethernet frame, find a valid vlan interface corresponding to the
    759  * given source interface and tag, then run the the real packet through
    760  * the parent's input routine.
    761  */
    762 void
    763 vlan_input(struct ifnet *ifp, struct mbuf *m)
    764 {
    765 	struct ifvlan *ifv;
    766 	u_int tag;
    767 
    768 	switch (ifp->if_type) {
    769 	case IFT_ETHER:
    770 	    {
    771 		struct ether_vlan_header *evl;
    772 
    773 		if (m->m_len < sizeof(struct ether_vlan_header) &&
    774 		    (m = m_pullup(m,
    775 		     sizeof(struct ether_vlan_header))) == NULL) {
    776 			printf("%s: no memory for VLAN header, "
    777 			    "dropping packet.\n", ifp->if_xname);
    778 			return;
    779 		}
    780 		evl = mtod(m, struct ether_vlan_header *);
    781 		KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
    782 
    783 		tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
    784 
    785 		/*
    786 		 * Restore the original ethertype.  We'll remove
    787 		 * the encapsulation after we've found the vlan
    788 		 * interface corresponding to the tag.
    789 		 */
    790 		evl->evl_encap_proto = evl->evl_proto;
    791 		break;
    792 	    }
    793 
    794 	default:
    795 		tag = (u_int) -1;	/* XXX GCC */
    796 #ifdef DIAGNOSTIC
    797 		panic("vlan_input: impossible");
    798 #endif
    799 	}
    800 
    801 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    802 	     ifv = LIST_NEXT(ifv, ifv_list))
    803 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
    804 			break;
    805 
    806 	if (ifv == NULL ||
    807 	    (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
    808 	     (IFF_UP|IFF_RUNNING)) {
    809 		m_free(m);
    810 		ifp->if_noproto++;
    811 		return;
    812 	}
    813 
    814 	/*
    815 	 * Now, remove the encapsulation header.  The original
    816 	 * header has already been fixed up above.
    817 	 */
    818 	memmove(mtod(m, caddr_t) + ifv->ifv_encaplen, mtod(m, caddr_t),
    819 	    ifv->ifv_encaplen);
    820 	m_adj(m, ifv->ifv_encaplen);
    821 
    822 	m->m_pkthdr.rcvif = &ifv->ifv_if;
    823 	ifv->ifv_if.if_ipackets++;
    824 
    825 #if NBPFILTER > 0
    826 	if (ifv->ifv_if.if_bpf)
    827 		bpf_mtap(ifv->ifv_if.if_bpf, m);
    828 #endif
    829 
    830 	/* Pass it back through the parent's input routine. */
    831 	(*ifp->if_input)(&ifv->ifv_if, m);
    832 }
    833