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