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