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