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