Home | History | Annotate | Line # | Download | only in net
if_vlan.c revision 1.9
      1 /*	$NetBSD: if_vlan.c,v 1.9 2000/10/02 14:41:26 ad 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 Jason R. Thorpe.
      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  *	- Need to make promiscuous mode work.
     90  */
     91 
     92 #include "opt_inet.h"
     93 #include "bpfilter.h"
     94 
     95 #include <sys/param.h>
     96 #include <sys/kernel.h>
     97 #include <sys/mbuf.h>
     98 #include <sys/queue.h>
     99 #include <sys/socket.h>
    100 #include <sys/sockio.h>
    101 #include <sys/systm.h>
    102 #include <sys/proc.h>
    103 
    104 #if NBPFILTER > 0
    105 #include <net/bpf.h>
    106 #endif
    107 #include <net/if.h>
    108 #include <net/if_dl.h>
    109 #include <net/if_types.h>
    110 #include <net/if_ether.h>
    111 #include <net/if_vlanvar.h>
    112 
    113 #ifdef INET
    114 #include <netinet/in.h>
    115 #include <netinet/if_inarp.h>
    116 #endif
    117 
    118 extern struct	ifaddr **ifnet_addrs;	/* XXX if.c */
    119 
    120 static int	vlan_clone_create(struct if_clone *, int);
    121 static void	vlan_clone_destroy(struct ifnet *);
    122 static int	vlan_config(struct ifvlan *, struct ifnet *);
    123 static int	vlan_ioctl(struct ifnet *, u_long, caddr_t);
    124 static int	vlan_addmulti(struct ifvlan *, struct ifreq *);
    125 static int	vlan_delmulti(struct ifvlan *, struct ifreq *);
    126 static void	vlan_purgemulti(struct ifvlan *);
    127 static void	vlan_start(struct ifnet *);
    128 static int	vlan_unconfig(struct ifnet *);
    129 void	vlanattach(int);
    130 
    131 /* XXX This should be a hash table with the tag as the basis of the key. */
    132 static LIST_HEAD(, ifvlan) ifv_list;
    133 
    134 struct if_clone vlan_cloner =
    135     IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
    136 
    137 void
    138 vlanattach(int n)
    139 {
    140 
    141 	LIST_INIT(&ifv_list);
    142 	if_clone_attach(&vlan_cloner);
    143 }
    144 
    145 static int
    146 vlan_clone_create(struct if_clone *ifc, int unit)
    147 {
    148 	struct ifvlan *ifv;
    149 	struct ifnet *ifp;
    150 	u_int8_t eaddr[ETHER_ADDR_LEN];
    151 
    152 	ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAIT);
    153 	memset(ifv, 0, sizeof(struct ifvlan));
    154 	ifp = &ifv->ifv_ec.ec_if;
    155 	LIST_INIT(&ifv->ifv_mc_listhead);
    156 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
    157 
    158 	sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
    159 	ifp->if_softc = ifv;
    160 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    161 	ifp->if_start = vlan_start;
    162 	ifp->if_ioctl = vlan_ioctl;
    163 
    164 	if_attach(ifp);
    165 	memset(eaddr, 0, sizeof(eaddr));
    166 	ether_ifattach(ifp, eaddr);
    167 
    168 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
    169 	ifp->if_mtu = ETHERMTU - EVL_ENCAPLEN;
    170 
    171 #if NBPFILTER > 0
    172 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    173 	    sizeof(struct ether_header));
    174 #endif
    175 
    176 	return (0);
    177 }
    178 
    179 static void
    180 vlan_clone_destroy(struct ifnet *ifp)
    181 {
    182 	struct ifvlan *ifv;
    183 	int s;
    184 
    185 	ifv = (struct ifvlan *)ifp->if_softc;
    186 	s = splsoftnet();
    187 
    188 	LIST_REMOVE(ifv, ifv_list);
    189 	vlan_unconfig(ifp);
    190 
    191 #if NBPFILTER > 0
    192 	bpfdetach(ifp);
    193 #endif
    194 	ether_ifdetach(ifp);
    195 	if_detach(ifp);
    196 	free(ifv, M_DEVBUF);
    197 
    198 	splx(s);
    199 }
    200 
    201 static int
    202 vlan_config(struct ifvlan *ifv, struct ifnet *p)
    203 {
    204 	struct ifaddr *ifa1, *ifa2;
    205 	struct sockaddr_dl *sdl1, *sdl2;
    206 
    207 	if (p->if_data.ifi_type != IFT_ETHER)
    208 		return (EPROTONOSUPPORT);
    209 	if (ifv->ifv_p != NULL)
    210 		return (EBUSY);
    211 	ifv->ifv_p = p;
    212 	ifv->ifv_if.if_mtu = p->if_data.ifi_mtu - EVL_ENCAPLEN;
    213 	ifv->ifv_if.if_flags = p->if_flags;
    214 
    215 	/*
    216 	 * Set up our ``Ethernet address'' to match the underlying
    217 	 * physical interface's.
    218 	 */
    219 	ifa1 = ifnet_addrs[ifv->ifv_if.if_index];
    220 	ifa2 = ifnet_addrs[p->if_index];
    221 	sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
    222 	sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
    223 	sdl1->sdl_type = IFT_ETHER;
    224 	sdl1->sdl_alen = ETHER_ADDR_LEN;
    225 	memcpy(LLADDR(sdl1), LLADDR(sdl2), ETHER_ADDR_LEN);
    226 	memcpy(LLADDR(ifv->ifv_ec.ec_if.if_sadl), LLADDR(sdl2), ETHER_ADDR_LEN);
    227 	return (0);
    228 }
    229 
    230 static int
    231 vlan_unconfig(struct ifnet *ifp)
    232 {
    233 	struct ifaddr *ifa;
    234 	struct sockaddr_dl *sdl;
    235 	struct ifvlan *ifv;
    236 	int s;
    237 
    238 	ifv = ifp->if_softc;
    239 	if (ifv->ifv_p == NULL)
    240 		return (0);
    241 
    242 	s = splsoftnet();
    243 
    244 	/*
    245  	 * Since the interface is being unconfigured, we need to empty the
    246 	 * list of multicast groups that we may have joined while we were
    247 	 * alive and remove them from the parent's list also.
    248 	 */
    249 	vlan_purgemulti(ifv);
    250 
    251 	/* Disconnect from parent. */
    252 	ifv->ifv_p = NULL;
    253 	ifv->ifv_if.if_mtu = ETHERMTU - EVL_ENCAPLEN;
    254 
    255 	/* Clear our MAC address. */
    256 	ifa = ifnet_addrs[ifv->ifv_if.if_index];
    257 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
    258 	sdl->sdl_type = IFT_ETHER;
    259 	sdl->sdl_alen = ETHER_ADDR_LEN;
    260 	memset(LLADDR(sdl), 0, ETHER_ADDR_LEN);
    261 	memset(LLADDR(ifv->ifv_ec.ec_if.if_sadl), 0, ETHER_ADDR_LEN);
    262 
    263 	splx(s);
    264 	return (0);
    265 }
    266 
    267 static int
    268 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    269 {
    270 	struct proc *p = curproc;	/* XXX */
    271 	struct ifaddr *ifa;
    272 	struct ifnet *pr;
    273 	struct ifreq *ifr;
    274 	struct ifvlan *ifv;
    275 	struct vlanreq vlr;
    276 	struct sockaddr *sa;
    277 	int error;
    278 
    279 	error = 0;
    280 	ifr = (struct ifreq *)data;
    281 	ifa = (struct ifaddr *)data;
    282 	ifv = ifp->if_softc;
    283 
    284 	switch (cmd) {
    285 	case SIOCSIFADDR:
    286 		ifp->if_flags |= IFF_UP;
    287 
    288 		switch (ifa->ifa_addr->sa_family) {
    289 #ifdef INET
    290 		case AF_INET:
    291 			arp_ifinit(ifp, ifa);
    292 			break;
    293 #endif
    294 		default:
    295 			break;
    296 		}
    297 		break;
    298 
    299 	case SIOCGIFADDR:
    300 		sa = (struct sockaddr *)&ifr->ifr_data;
    301 		memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    302 		break;
    303 
    304 	case SIOCSIFMTU:
    305 		if (ifv->ifv_p != NULL) {
    306 			if (ifr->ifr_mtu > ifv->ifv_p->if_mtu - EVL_ENCAPLEN ||
    307 			    ifr->ifr_mtu < ETHERMIN + EVL_ENCAPLEN)
    308 				error = EINVAL;
    309 			else
    310 				ifp->if_mtu = ifr->ifr_mtu;
    311 		} else
    312 			error = EINVAL;
    313 		break;
    314 
    315 	case SIOCSETVLAN:
    316 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    317 			break;
    318 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
    319 			break;
    320 		if (vlr.vlr_parent[0] == '\0') {
    321 			vlan_unconfig(ifp);
    322 			if_down(ifp);
    323 			ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
    324 			break;
    325 		}
    326 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
    327 			error = EINVAL;		 /* check for valid tag */
    328 			break;
    329 		}
    330 		if ((pr = ifunit(vlr.vlr_parent)) == 0) {
    331 			error = ENOENT;
    332 			break;
    333 		}
    334 		if ((error = vlan_config(ifv, pr)) != 0)
    335 			break;
    336 		ifv->ifv_tag = vlr.vlr_tag;
    337 		ifp->if_flags |= IFF_RUNNING;
    338 		break;
    339 
    340 	case SIOCGETVLAN:
    341 		memset(&vlr, 0, sizeof(vlr));
    342 		if (ifv->ifv_p != NULL) {
    343 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
    344 			    ifv->ifv_p->if_xname);
    345 			vlr.vlr_tag = ifv->ifv_tag;
    346 		}
    347 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
    348 		break;
    349 
    350 	case SIOCSIFFLAGS:
    351 		/*
    352 		 * XXX We don't support promiscuous mode right now because
    353 		 * it would require help from the underlying drivers, which
    354 		 * hasn't been implemented.
    355 		 */
    356 		if ((ifr->ifr_flags & IFF_PROMISC) != 0) {
    357 			ifp->if_flags &= ~(IFF_PROMISC);
    358 			error = EINVAL;
    359 		}
    360 		break;
    361 
    362 	case SIOCADDMULTI:
    363 		error = vlan_addmulti(ifv, ifr);
    364 		break;
    365 
    366 	case SIOCDELMULTI:
    367 		error = vlan_delmulti(ifv, ifr);
    368 		break;
    369 
    370 	default:
    371 		error = EINVAL;
    372 	}
    373 
    374 	return (error);
    375 }
    376 
    377 static int
    378 vlan_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
    379 {
    380 	struct vlan_mc_entry *mc;
    381 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    382 	int error;
    383 
    384 	if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr_storage))
    385 		return (EINVAL);
    386 
    387 	error = ether_addmulti(ifr, &ifv->ifv_ec);
    388 	if (error != ENETRESET)
    389 		return (error);
    390 
    391 	/*
    392 	 * This is new multicast address.  We have to tell parent
    393 	 * about it.  Also, remember this multicast address so that
    394 	 * we can delete them on unconfigure.
    395 	 */
    396 	MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
    397 	    M_DEVBUF, M_NOWAIT);
    398 	if (mc == NULL) {
    399 		error = ENOMEM;
    400 		goto alloc_failed;
    401 	}
    402 
    403 	/*
    404 	 * As ether_addmulti() returns ENETRESET, following two
    405 	 * statement shouldn't fail.
    406 	 */
    407 	(void)ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
    408 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
    409 	memcpy(&mc->mc_addr, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
    410 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
    411 
    412 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
    413 	    (caddr_t)ifr);
    414 	if (error != 0)
    415 		goto ioctl_failed;
    416 	return (error);
    417 
    418  ioctl_failed:
    419 	LIST_REMOVE(mc, mc_entries);
    420 	FREE(mc, M_DEVBUF);
    421  alloc_failed:
    422 	(void)ether_delmulti(ifr, &ifv->ifv_ec);
    423 	return (error);
    424 }
    425 
    426 static int
    427 vlan_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
    428 {
    429 	struct ether_multi *enm;
    430 	struct vlan_mc_entry *mc;
    431 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    432 	int error;
    433 
    434 	/*
    435 	 * Find a key to lookup vlan_mc_entry.  We have to do this
    436 	 * before calling ether_delmulti for obvious reason.
    437 	 */
    438 	if ((error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi)) != 0)
    439 		return (error);
    440 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
    441 
    442 	error = ether_delmulti(ifr, &ifv->ifv_ec);
    443 	if (error != ENETRESET)
    444 		return (error);
    445 
    446 	/* We no longer use this multicast address.  Tell parent so. */
    447 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
    448 	    (caddr_t)ifr);
    449 	if (error == 0) {
    450 		/* And forget about this address. */
    451 		for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
    452 		    mc = LIST_NEXT(mc, mc_entries)) {
    453 			if (mc->mc_enm == enm) {
    454 				LIST_REMOVE(mc, mc_entries);
    455 				FREE(mc, M_DEVBUF);
    456 				break;
    457 			}
    458 		}
    459 		KASSERT(mc != NULL);
    460 	} else
    461 		(void)ether_addmulti(ifr, &ifv->ifv_ec);
    462 	return (error);
    463 }
    464 
    465 /*
    466  * Delete any multicast address we have asked to add form parent
    467  * interface.  Called when the vlan is being unconfigured.
    468  */
    469 static void
    470 vlan_purgemulti(struct ifvlan *ifv)
    471 {
    472 	struct ifnet *ifp = ifv->ifv_p;		/* Parent. */
    473 	struct vlan_mc_entry *mc;
    474 	union {
    475 		struct ifreq ifreq;
    476 		struct {
    477 			char ifr_name[IFNAMSIZ];
    478 			struct sockaddr_storage;
    479 		} ifreq_storage;
    480 	} ifreq;
    481 	struct ifreq *ifr = &ifreq.ifreq;
    482 
    483 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
    484 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
    485 		memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len);
    486 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)ifr);
    487 		LIST_REMOVE(mc->mc_enm, enm_list);
    488 		free(mc->mc_enm, M_IFMADDR);
    489 		LIST_REMOVE(mc, mc_entries);
    490 		FREE(mc, M_DEVBUF);
    491 	}
    492 
    493 	KASSERT(LIST_FIRST(&ifv->ifv_ec.ec_multiaddrs) == NULL);
    494 }
    495 
    496 static void
    497 vlan_start(struct ifnet *ifp)
    498 {
    499 	struct ifvlan *ifv;
    500 	struct ifnet *p;
    501 	struct ether_vlan_header *evl;
    502 	struct mbuf *m;
    503 
    504 	ifv = ifp->if_softc;
    505 	p = ifv->ifv_p;
    506 	ifp->if_flags |= IFF_OACTIVE;
    507 
    508 	for (;;) {
    509 		IF_DEQUEUE(&ifp->if_snd, m);
    510 		if (m == NULL)
    511 			break;
    512 
    513 #if NBPFILTER > 0
    514 		if (ifp->if_bpf)
    515 			bpf_mtap(ifp->if_bpf, m);
    516 #endif
    517 
    518 		/*
    519 		 * XXX Should handle the case where the underlying hardware
    520 		 * interface can do VLAN tag insertion itself.
    521 		 */
    522 		M_PREPEND(m, EVL_ENCAPLEN, M_DONTWAIT);
    523 		if (m == NULL) {
    524 			printf("%s: M_PREPEND failed", ifv->ifv_p->if_xname);
    525 			ifp->if_ierrors++;
    526 			continue;
    527 		}
    528 
    529 		if (m->m_len < sizeof(struct ether_vlan_header) &&
    530 		    (m = m_pullup(m,
    531 		     sizeof(struct ether_vlan_header))) == NULL) {
    532 			printf("%s: m_pullup failed", ifv->ifv_p->if_xname);
    533 			ifp->if_ierrors++;
    534 			continue;
    535 		}
    536 
    537 		/*
    538 		 * Transform the Ethernet header into an Ethernet header
    539 		 * with 802.1Q encapsulation.
    540 		 */
    541 		memmove(mtod(m, caddr_t), mtod(m, caddr_t) + EVL_ENCAPLEN,
    542 		    sizeof(struct ether_header));
    543 		evl = mtod(m, struct ether_vlan_header *);
    544 		evl->evl_proto = evl->evl_encap_proto;
    545 		evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
    546 		evl->evl_tag = htons(ifv->ifv_tag);
    547 
    548 		/*
    549 		 * Send it, precisely as ether_output() would have.  We are
    550 		 * already running at splimp.
    551 		 */
    552 		if (IF_QFULL(&p->if_snd)) {
    553 			IF_DROP(&p->if_snd);
    554 			/* XXX stats */
    555 			ifp->if_oerrors++;
    556 			m_freem(m);
    557 			continue;
    558 		}
    559 
    560 		IF_ENQUEUE(&p->if_snd, m);
    561 		if ((p->if_flags & IFF_OACTIVE) == 0) {
    562 			p->if_start(p);
    563 			ifp->if_opackets++;
    564 		}
    565 	}
    566 
    567 	ifp->if_flags &= ~IFF_OACTIVE;
    568 }
    569 
    570 /*
    571  * Given an Ethernet frame, find a valid vlan interface corresponding to the
    572  * given source interface and tag, then run the the real packet through
    573  * the parent's input routine.
    574  */
    575 void
    576 vlan_input(struct ifnet *ifp, struct mbuf *m)
    577 {
    578 	struct ether_vlan_header *evl;
    579 	struct ifvlan *ifv;
    580 	u_int tag;
    581 
    582 	if (m->m_len < sizeof(struct ether_vlan_header) &&
    583 	    (m = m_pullup(m, sizeof(struct ether_vlan_header))) == NULL) {
    584 		printf("%s: no memory for VLAN header, dropping packet.\n",
    585 		    ifp->if_xname);
    586 		return;
    587 	}
    588 	evl = mtod(m, struct ether_vlan_header *);
    589 	KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
    590 
    591 	tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
    592 
    593 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    594 	    ifv = LIST_NEXT(ifv, ifv_list))
    595 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
    596 			break;
    597 
    598 	if (ifv == NULL || (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
    599 	    (IFF_UP|IFF_RUNNING)) {
    600 		m_free(m);
    601 		ifp->if_data.ifi_noproto++;
    602 		return;
    603 	}
    604 
    605 	/*
    606 	 * Having found a valid vlan interface corresponding to the given
    607 	 * source interface and vlan tag, remove the encapsulation.
    608 	 */
    609 	evl->evl_encap_proto = evl->evl_proto;
    610 	memmove(mtod(m, caddr_t) + EVL_ENCAPLEN, mtod(m, caddr_t),
    611 	    EVL_ENCAPLEN);
    612 	m_adj(m, EVL_ENCAPLEN);
    613 
    614 	m->m_pkthdr.rcvif = &ifv->ifv_if;
    615 	ifv->ifv_if.if_ipackets++;
    616 
    617 #if NBPFILTER > 0
    618 	if (ifv->ifv_if.if_bpf)
    619 		bpf_mtap(ifv->ifv_if.if_bpf, m);
    620 #endif
    621 
    622 	/* Pass it back through the parent's input routine. */
    623 	(*ifp->if_input)(&ifv->ifv_if, m);
    624 }
    625