Home | History | Annotate | Line # | Download | only in net
if_vlan.c revision 1.60
      1 /*	$NetBSD: if_vlan.c,v 1.60 2008/10/11 17:19:41 bouyer 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright 1998 Massachusetts Institute of Technology
     34  *
     35  * Permission to use, copy, modify, and distribute this software and
     36  * its documentation for any purpose and without fee is hereby
     37  * granted, provided that both the above copyright notice and this
     38  * permission notice appear in all copies, that both the above
     39  * copyright notice and this permission notice appear in all
     40  * supporting documentation, and that the name of M.I.T. not be used
     41  * in advertising or publicity pertaining to distribution of the
     42  * software without specific, written prior permission.  M.I.T. makes
     43  * no representations about the suitability of this software for any
     44  * purpose.  It is provided "as is" without express or implied
     45  * warranty.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
     48  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
     49  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     50  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
     51  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     52  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     53  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     54  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     55  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     56  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     57  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
     61  * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
     62  */
     63 
     64 /*
     65  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.  Might be
     66  * extended some day to also handle IEEE 802.1P priority tagging.  This is
     67  * sort of sneaky in the implementation, since we need to pretend to be
     68  * enough of an Ethernet implementation to make ARP work.  The way we do
     69  * this is by telling everyone that we are an Ethernet interface, and then
     70  * catch the packets that ether_output() left on our output queue when it
     71  * calls if_start(), rewrite them for use by the real outgoing interface,
     72  * and ask it to send them.
     73  *
     74  * TODO:
     75  *
     76  *	- Need some way to notify vlan interfaces when the parent
     77  *	  interface changes MTU.
     78  */
     79 
     80 #include <sys/cdefs.h>
     81 __KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.60 2008/10/11 17:19:41 bouyer Exp $");
     82 
     83 #include "opt_inet.h"
     84 #include "bpfilter.h"
     85 
     86 #include <sys/param.h>
     87 #include <sys/kernel.h>
     88 #include <sys/mbuf.h>
     89 #include <sys/queue.h>
     90 #include <sys/socket.h>
     91 #include <sys/sockio.h>
     92 #include <sys/systm.h>
     93 #include <sys/proc.h>
     94 #include <sys/kauth.h>
     95 
     96 #if NBPFILTER > 0
     97 #include <net/bpf.h>
     98 #endif
     99 #include <net/if.h>
    100 #include <net/if_dl.h>
    101 #include <net/if_types.h>
    102 #include <net/if_ether.h>
    103 #include <net/if_vlanvar.h>
    104 
    105 #ifdef INET
    106 #include <netinet/in.h>
    107 #include <netinet/if_inarp.h>
    108 #endif
    109 
    110 struct vlan_mc_entry {
    111 	LIST_ENTRY(vlan_mc_entry)	mc_entries;
    112 	/*
    113 	 * A key to identify this entry.  The mc_addr below can't be
    114 	 * used since multiple sockaddr may mapped into the same
    115 	 * ether_multi (e.g., AF_UNSPEC).
    116 	 */
    117 	union {
    118 		struct ether_multi	*mcu_enm;
    119 	} mc_u;
    120 	struct sockaddr_storage		mc_addr;
    121 };
    122 
    123 #define	mc_enm		mc_u.mcu_enm
    124 
    125 struct ifvlan {
    126 	union {
    127 		struct ethercom ifvu_ec;
    128 	} ifv_u;
    129 	struct ifnet *ifv_p;	/* parent interface of this vlan */
    130 	struct ifv_linkmib {
    131 		const struct vlan_multisw *ifvm_msw;
    132 		int	ifvm_encaplen;	/* encapsulation length */
    133 		int	ifvm_mtufudge;	/* MTU fudged by this much */
    134 		int	ifvm_mintu;	/* min transmission unit */
    135 		uint16_t ifvm_proto;	/* encapsulation ethertype */
    136 		uint16_t ifvm_tag;	/* tag to apply on packets */
    137 	} ifv_mib;
    138 	LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
    139 	LIST_ENTRY(ifvlan) ifv_list;
    140 	int ifv_flags;
    141 };
    142 
    143 #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
    144 
    145 #define	ifv_ec		ifv_u.ifvu_ec
    146 
    147 #define	ifv_if		ifv_ec.ec_if
    148 
    149 #define	ifv_msw		ifv_mib.ifvm_msw
    150 #define	ifv_encaplen	ifv_mib.ifvm_encaplen
    151 #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
    152 #define	ifv_mintu	ifv_mib.ifvm_mintu
    153 #define	ifv_tag		ifv_mib.ifvm_tag
    154 
    155 struct vlan_multisw {
    156 	int	(*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
    157 	int	(*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
    158 	void	(*vmsw_purgemulti)(struct ifvlan *);
    159 };
    160 
    161 static int	vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
    162 static int	vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
    163 static void	vlan_ether_purgemulti(struct ifvlan *);
    164 
    165 const struct vlan_multisw vlan_ether_multisw = {
    166 	vlan_ether_addmulti,
    167 	vlan_ether_delmulti,
    168 	vlan_ether_purgemulti,
    169 };
    170 
    171 static int	vlan_clone_create(struct if_clone *, int);
    172 static int	vlan_clone_destroy(struct ifnet *);
    173 static int	vlan_config(struct ifvlan *, struct ifnet *);
    174 static int	vlan_ioctl(struct ifnet *, u_long, void *);
    175 static void	vlan_start(struct ifnet *);
    176 static void	vlan_unconfig(struct ifnet *);
    177 
    178 void		vlanattach(int);
    179 
    180 /* XXX This should be a hash table with the tag as the basis of the key. */
    181 static LIST_HEAD(, ifvlan) ifv_list;
    182 
    183 struct if_clone vlan_cloner =
    184     IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
    185 
    186 /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
    187 static char vlan_zero_pad_buff[ETHER_MIN_LEN];
    188 
    189 void
    190 vlanattach(int n)
    191 {
    192 
    193 	LIST_INIT(&ifv_list);
    194 	if_clone_attach(&vlan_cloner);
    195 }
    196 
    197 static void
    198 vlan_reset_linkname(struct ifnet *ifp)
    199 {
    200 
    201 	/*
    202 	 * We start out with a "802.1Q VLAN" type and zero-length
    203 	 * addresses.  When we attach to a parent interface, we
    204 	 * inherit its type, address length, address, and data link
    205 	 * type.
    206 	 */
    207 
    208 	ifp->if_type = IFT_L2VLAN;
    209 	ifp->if_addrlen = 0;
    210 	ifp->if_dlt = DLT_NULL;
    211 	if_alloc_sadl(ifp);
    212 }
    213 
    214 static int
    215 vlan_clone_create(struct if_clone *ifc, int unit)
    216 {
    217 	struct ifvlan *ifv;
    218 	struct ifnet *ifp;
    219 	int s;
    220 
    221 	ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK|M_ZERO);
    222 	ifp = &ifv->ifv_if;
    223 	LIST_INIT(&ifv->ifv_mc_listhead);
    224 
    225 	s = splnet();
    226 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
    227 	splx(s);
    228 
    229 	if_initname(ifp, ifc->ifc_name, unit);
    230 	ifp->if_softc = ifv;
    231 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    232 	ifp->if_start = vlan_start;
    233 	ifp->if_ioctl = vlan_ioctl;
    234 	IFQ_SET_READY(&ifp->if_snd);
    235 
    236 	if_attach(ifp);
    237 	vlan_reset_linkname(ifp);
    238 
    239 	return (0);
    240 }
    241 
    242 static int
    243 vlan_clone_destroy(struct ifnet *ifp)
    244 {
    245 	struct ifvlan *ifv = ifp->if_softc;
    246 	int s;
    247 
    248 	s = splnet();
    249 	LIST_REMOVE(ifv, ifv_list);
    250 	vlan_unconfig(ifp);
    251 	splx(s);
    252 
    253 	if_detach(ifp);
    254 	free(ifv, M_DEVBUF);
    255 
    256 	return (0);
    257 }
    258 
    259 /*
    260  * Configure a VLAN interface.  Must be called at splnet().
    261  */
    262 static int
    263 vlan_config(struct ifvlan *ifv, struct ifnet *p)
    264 {
    265 	struct ifnet *ifp = &ifv->ifv_if;
    266 	int error;
    267 
    268 	if (ifv->ifv_p != NULL)
    269 		return (EBUSY);
    270 
    271 	switch (p->if_type) {
    272 	case IFT_ETHER:
    273 	    {
    274 		struct ethercom *ec = (void *) p;
    275 
    276 		ifv->ifv_msw = &vlan_ether_multisw;
    277 		ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
    278 		ifv->ifv_mintu = ETHERMIN;
    279 
    280 		/*
    281 		 * If the parent supports the VLAN_MTU capability,
    282 		 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
    283 		 * enable it.
    284 		 */
    285 		if (ec->ec_nvlans++ == 0 &&
    286 		    (ec->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
    287 			/*
    288 			 * Enable Tx/Rx of VLAN-sized frames.
    289 			 */
    290 			ec->ec_capenable |= ETHERCAP_VLAN_MTU;
    291 			if (p->if_flags & IFF_UP) {
    292 				struct ifreq ifr;
    293 
    294 				ifr.ifr_flags = p->if_flags;
    295 				error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
    296 				    (void *) &ifr);
    297 				if (error) {
    298 					if (ec->ec_nvlans-- == 1)
    299 						ec->ec_capenable &=
    300 						    ~ETHERCAP_VLAN_MTU;
    301 					return (error);
    302 				}
    303 			}
    304 			ifv->ifv_mtufudge = 0;
    305 		} else if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) {
    306 			/*
    307 			 * Fudge the MTU by the encapsulation size.  This
    308 			 * makes us incompatible with strictly compliant
    309 			 * 802.1Q implementations, but allows us to use
    310 			 * the feature with other NetBSD implementations,
    311 			 * which might still be useful.
    312 			 */
    313 			ifv->ifv_mtufudge = ifv->ifv_encaplen;
    314 		}
    315 
    316 		/*
    317 		 * If the parent interface can do hardware-assisted
    318 		 * VLAN encapsulation, then propagate its hardware-
    319 		 * assisted checksumming flags.
    320 		 */
    321 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING)
    322 			ifp->if_capabilities = p->if_capabilities &
    323 			    (IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx|
    324 			     IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx|
    325 			     IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx|
    326 			     IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx|
    327 			     IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx);
    328 
    329 		/*
    330 		 * We inherit the parent's Ethernet address.
    331 		 */
    332 		ether_ifattach(ifp, CLLADDR(p->if_sadl));
    333 		ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
    334 		break;
    335 	    }
    336 
    337 	default:
    338 		return (EPROTONOSUPPORT);
    339 	}
    340 
    341 	ifv->ifv_p = p;
    342 	ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
    343 	ifv->ifv_if.if_flags = p->if_flags &
    344 	    (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
    345 
    346 	/*
    347 	 * Inherit the if_type from the parent.  This allows us
    348 	 * to participate in bridges of that type.
    349 	 */
    350 	ifv->ifv_if.if_type = p->if_type;
    351 
    352 	return (0);
    353 }
    354 
    355 /*
    356  * Unconfigure a VLAN interface.  Must be called at splnet().
    357  */
    358 static void
    359 vlan_unconfig(struct ifnet *ifp)
    360 {
    361 	struct ifvlan *ifv = ifp->if_softc;
    362 
    363 	if (ifv->ifv_p == NULL)
    364 		return;
    365 
    366 	/*
    367  	 * Since the interface is being unconfigured, we need to empty the
    368 	 * list of multicast groups that we may have joined while we were
    369 	 * alive and remove them from the parent's list also.
    370 	 */
    371 	(*ifv->ifv_msw->vmsw_purgemulti)(ifv);
    372 
    373 	/* Disconnect from parent. */
    374 	switch (ifv->ifv_p->if_type) {
    375 	case IFT_ETHER:
    376 	    {
    377 		struct ethercom *ec = (void *) ifv->ifv_p;
    378 
    379 		if (ec->ec_nvlans-- == 1) {
    380 			/*
    381 			 * Disable Tx/Rx of VLAN-sized frames.
    382 			 */
    383 			ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
    384 			if (ifv->ifv_p->if_flags & IFF_UP) {
    385 				struct ifreq ifr;
    386 
    387 				ifr.ifr_flags = ifv->ifv_p->if_flags;
    388 				(void) (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
    389 				    SIOCSIFFLAGS, (void *) &ifr);
    390 			}
    391 		}
    392 
    393 		ether_ifdetach(ifp);
    394 		vlan_reset_linkname(ifp);
    395 		break;
    396 	    }
    397 
    398 #ifdef DIAGNOSTIC
    399 	default:
    400 		panic("vlan_unconfig: impossible");
    401 #endif
    402 	}
    403 
    404 	ifv->ifv_p = NULL;
    405 	ifv->ifv_if.if_mtu = 0;
    406 	ifv->ifv_flags = 0;
    407 
    408 	if_down(ifp);
    409 	ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
    410 	ifp->if_capabilities = 0;
    411 }
    412 
    413 /*
    414  * Called when a parent interface is detaching; destroy any VLAN
    415  * configuration for the parent interface.
    416  */
    417 void
    418 vlan_ifdetach(struct ifnet *p)
    419 {
    420 	struct ifvlan *ifv;
    421 	int s;
    422 
    423 	s = splnet();
    424 
    425 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    426 	     ifv = LIST_NEXT(ifv, ifv_list)) {
    427 		if (ifv->ifv_p == p)
    428 			vlan_unconfig(&ifv->ifv_if);
    429 	}
    430 
    431 	splx(s);
    432 }
    433 
    434 static int
    435 vlan_set_promisc(struct ifnet *ifp)
    436 {
    437 	struct ifvlan *ifv = ifp->if_softc;
    438 	int error = 0;
    439 
    440 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    441 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
    442 			error = ifpromisc(ifv->ifv_p, 1);
    443 			if (error == 0)
    444 				ifv->ifv_flags |= IFVF_PROMISC;
    445 		}
    446 	} else {
    447 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
    448 			error = ifpromisc(ifv->ifv_p, 0);
    449 			if (error == 0)
    450 				ifv->ifv_flags &= ~IFVF_PROMISC;
    451 		}
    452 	}
    453 
    454 	return (error);
    455 }
    456 
    457 static int
    458 vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    459 {
    460 	struct lwp *l = curlwp;	/* XXX */
    461 	struct ifvlan *ifv = ifp->if_softc;
    462 	struct ifaddr *ifa = (struct ifaddr *) data;
    463 	struct ifreq *ifr = (struct ifreq *) data;
    464 	struct ifnet *pr;
    465 	struct ifcapreq *ifcr;
    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, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
    494 		break;
    495 
    496 	case SIOCSIFMTU:
    497 		if (ifv->ifv_p == NULL)
    498 			error = EINVAL;
    499 		else if (
    500 		    ifr->ifr_mtu > (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
    501 		    ifr->ifr_mtu < (ifv->ifv_mintu - ifv->ifv_mtufudge))
    502 			error = EINVAL;
    503 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    504 			error = 0;
    505 		break;
    506 
    507 	case SIOCSETVLAN:
    508 		if ((error = kauth_authorize_network(l->l_cred,
    509 		    KAUTH_NETWORK_INTERFACE,
    510 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    511 		    NULL)) != 0)
    512 			break;
    513 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
    514 			break;
    515 		if (vlr.vlr_parent[0] == '\0') {
    516 			vlan_unconfig(ifp);
    517 			break;
    518 		}
    519 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
    520 			error = EINVAL;		 /* check for valid tag */
    521 			break;
    522 		}
    523 		if ((pr = ifunit(vlr.vlr_parent)) == 0) {
    524 			error = ENOENT;
    525 			break;
    526 		}
    527 		if ((error = vlan_config(ifv, pr)) != 0)
    528 			break;
    529 		ifv->ifv_tag = vlr.vlr_tag;
    530 		ifp->if_flags |= IFF_RUNNING;
    531 
    532 		/* Update promiscuous mode, if necessary. */
    533 		vlan_set_promisc(ifp);
    534 		break;
    535 
    536 	case SIOCGETVLAN:
    537 		memset(&vlr, 0, sizeof(vlr));
    538 		if (ifv->ifv_p != NULL) {
    539 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
    540 			    ifv->ifv_p->if_xname);
    541 			vlr.vlr_tag = ifv->ifv_tag;
    542 		}
    543 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
    544 		break;
    545 
    546 	case SIOCSIFFLAGS:
    547 		/*
    548 		 * For promiscuous mode, we enable promiscuous mode on
    549 		 * the parent if we need promiscuous on the VLAN interface.
    550 		 */
    551 		if (ifv->ifv_p != NULL)
    552 			error = vlan_set_promisc(ifp);
    553 		break;
    554 
    555 	case SIOCADDMULTI:
    556 		error = (ifv->ifv_p != NULL) ?
    557 		    (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
    558 		break;
    559 
    560 	case SIOCDELMULTI:
    561 		error = (ifv->ifv_p != NULL) ?
    562 		    (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
    563 		break;
    564 
    565 	case SIOCSIFCAP:
    566 		ifcr = data;
    567 		/* make sure caps are enabled on parent */
    568 		if ((ifv->ifv_p->if_capenable & ifcr->ifcr_capenable) !=
    569 		    ifcr->ifcr_capenable) {
    570 			error = EINVAL;
    571 			break;
    572 		}
    573 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    574 			error = 0;
    575 		break;
    576 	default:
    577 		error = EINVAL;
    578 	}
    579 
    580 	splx(s);
    581 
    582 	return (error);
    583 }
    584 
    585 static int
    586 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
    587 {
    588 	const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
    589 	struct vlan_mc_entry *mc;
    590 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    591 	int error;
    592 
    593 	if (sa->sa_len > sizeof(struct sockaddr_storage))
    594 		return (EINVAL);
    595 
    596 	error = ether_addmulti(sa, &ifv->ifv_ec);
    597 	if (error != ENETRESET)
    598 		return (error);
    599 
    600 	/*
    601 	 * This is new multicast address.  We have to tell parent
    602 	 * about it.  Also, remember this multicast address so that
    603 	 * we can delete them on unconfigure.
    604 	 */
    605 	MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
    606 	    M_DEVBUF, M_NOWAIT);
    607 	if (mc == NULL) {
    608 		error = ENOMEM;
    609 		goto alloc_failed;
    610 	}
    611 
    612 	/*
    613 	 * As ether_addmulti() returns ENETRESET, following two
    614 	 * statement shouldn't fail.
    615 	 */
    616 	(void)ether_multiaddr(sa, addrlo, addrhi);
    617 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
    618 	memcpy(&mc->mc_addr, sa, sa->sa_len);
    619 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
    620 
    621 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
    622 	    (void *)ifr);
    623 	if (error != 0)
    624 		goto ioctl_failed;
    625 	return (error);
    626 
    627  ioctl_failed:
    628 	LIST_REMOVE(mc, mc_entries);
    629 	FREE(mc, M_DEVBUF);
    630  alloc_failed:
    631 	(void)ether_delmulti(sa, &ifv->ifv_ec);
    632 	return (error);
    633 }
    634 
    635 static int
    636 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
    637 {
    638 	const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
    639 	struct ether_multi *enm;
    640 	struct vlan_mc_entry *mc;
    641 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    642 	int error;
    643 
    644 	/*
    645 	 * Find a key to lookup vlan_mc_entry.  We have to do this
    646 	 * before calling ether_delmulti for obvious reason.
    647 	 */
    648 	if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
    649 		return (error);
    650 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
    651 
    652 	error = ether_delmulti(sa, &ifv->ifv_ec);
    653 	if (error != ENETRESET)
    654 		return (error);
    655 
    656 	/* We no longer use this multicast address.  Tell parent so. */
    657 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
    658 	    (void *)ifr);
    659 	if (error == 0) {
    660 		/* And forget about this address. */
    661 		for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
    662 		    mc = LIST_NEXT(mc, mc_entries)) {
    663 			if (mc->mc_enm == enm) {
    664 				LIST_REMOVE(mc, mc_entries);
    665 				FREE(mc, M_DEVBUF);
    666 				break;
    667 			}
    668 		}
    669 		KASSERT(mc != NULL);
    670 	} else
    671 		(void)ether_addmulti(sa, &ifv->ifv_ec);
    672 	return (error);
    673 }
    674 
    675 /*
    676  * Delete any multicast address we have asked to add from parent
    677  * interface.  Called when the vlan is being unconfigured.
    678  */
    679 static void
    680 vlan_ether_purgemulti(struct ifvlan *ifv)
    681 {
    682 	struct ifnet *ifp = ifv->ifv_p;		/* Parent. */
    683 	struct vlan_mc_entry *mc;
    684 	union {
    685 		struct ifreq ifreq;
    686 		struct {
    687 			char ifr_name[IFNAMSIZ];
    688 			struct sockaddr_storage ifr_ss;
    689 		} ifreq_storage;
    690 	} ifreq;
    691 	struct ifreq *ifr = &ifreq.ifreq;
    692 
    693 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
    694 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
    695 		ifreq_setaddr(SIOCDELMULTI, ifr,
    696 		    (const struct sockaddr *)&mc->mc_addr);
    697 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (void *)ifr);
    698 		LIST_REMOVE(mc, mc_entries);
    699 		FREE(mc, M_DEVBUF);
    700 	}
    701 }
    702 
    703 static void
    704 vlan_start(struct ifnet *ifp)
    705 {
    706 	struct ifvlan *ifv = ifp->if_softc;
    707 	struct ifnet *p = ifv->ifv_p;
    708 	struct ethercom *ec = (void *) ifv->ifv_p;
    709 	struct mbuf *m;
    710 	int error;
    711 	ALTQ_DECL(struct altq_pktattr pktattr;)
    712 
    713 	ifp->if_flags |= IFF_OACTIVE;
    714 
    715 	for (;;) {
    716 		IFQ_DEQUEUE(&ifp->if_snd, m);
    717 		if (m == NULL)
    718 			break;
    719 
    720 #ifdef ALTQ
    721 		/*
    722 		 * If ALTQ is enabled on the parent interface, do
    723 		 * classification; the queueing discipline might
    724 		 * not require classification, but might require
    725 		 * the address family/header pointer in the pktattr.
    726 		 */
    727 		if (ALTQ_IS_ENABLED(&p->if_snd)) {
    728 			switch (p->if_type) {
    729 			case IFT_ETHER:
    730 				altq_etherclassify(&p->if_snd, m, &pktattr);
    731 				break;
    732 #ifdef DIAGNOSTIC
    733 			default:
    734 				panic("vlan_start: impossible (altq)");
    735 #endif
    736 			}
    737 		}
    738 #endif /* ALTQ */
    739 
    740 #if NBPFILTER > 0
    741 		if (ifp->if_bpf)
    742 			bpf_mtap(ifp->if_bpf, m);
    743 #endif
    744 		/*
    745 		 * If the parent can insert the tag itself, just mark
    746 		 * the tag in the mbuf header.
    747 		 */
    748 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
    749 			struct m_tag *mtag;
    750 
    751 			mtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
    752 			    M_NOWAIT);
    753 			if (mtag == NULL) {
    754 				ifp->if_oerrors++;
    755 				m_freem(m);
    756 				continue;
    757 			}
    758 			*(u_int *)(mtag + 1) = ifv->ifv_tag;
    759 			m_tag_prepend(m, mtag);
    760 		} else {
    761 			/*
    762 			 * insert the tag ourselves
    763 			 */
    764 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
    765 			if (m == NULL) {
    766 				printf("%s: unable to prepend encap header",
    767 				    ifv->ifv_p->if_xname);
    768 				ifp->if_oerrors++;
    769 				continue;
    770 			}
    771 
    772 			switch (p->if_type) {
    773 			case IFT_ETHER:
    774 			    {
    775 				struct ether_vlan_header *evl;
    776 
    777 				if (m->m_len < sizeof(struct ether_vlan_header))
    778 					m = m_pullup(m,
    779 					    sizeof(struct ether_vlan_header));
    780 				if (m == NULL) {
    781 					printf("%s: unable to pullup encap "
    782 					    "header", ifv->ifv_p->if_xname);
    783 					ifp->if_oerrors++;
    784 					continue;
    785 				}
    786 
    787 				/*
    788 				 * Transform the Ethernet header into an
    789 				 * Ethernet header with 802.1Q encapsulation.
    790 				 */
    791 				memmove(mtod(m, void *),
    792 				    mtod(m, char *) + ifv->ifv_encaplen,
    793 				    sizeof(struct ether_header));
    794 				evl = mtod(m, struct ether_vlan_header *);
    795 				evl->evl_proto = evl->evl_encap_proto;
    796 				evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
    797 				evl->evl_tag = htons(ifv->ifv_tag);
    798 
    799 				/*
    800 				 * To cater for VLAN-aware layer 2 ethernet
    801 				 * switches which may need to strip the tag
    802 				 * before forwarding the packet, make sure
    803 				 * the packet+tag is at least 68 bytes long.
    804 				 * This is necessary because our parent will
    805 				 * only pad to 64 bytes (ETHER_MIN_LEN) and
    806 				 * some switches will not pad by themselves
    807 				 * after deleting a tag.
    808 				 */
    809 				if (m->m_pkthdr.len <
    810 				    (ETHER_MIN_LEN + ETHER_VLAN_ENCAP_LEN)) {
    811 					m_copyback(m, m->m_pkthdr.len,
    812 					    (ETHER_MIN_LEN +
    813 					     ETHER_VLAN_ENCAP_LEN) -
    814 					     m->m_pkthdr.len,
    815 					    vlan_zero_pad_buff);
    816 				}
    817 				break;
    818 			    }
    819 
    820 #ifdef DIAGNOSTIC
    821 			default:
    822 				panic("vlan_start: impossible");
    823 #endif
    824 			}
    825 		}
    826 
    827 		/*
    828 		 * Send it, precisely as the parent's output routine
    829 		 * would have.  We are already running at splnet.
    830 		 */
    831 		IFQ_ENQUEUE(&p->if_snd, m, &pktattr, error);
    832 		if (error) {
    833 			/* mbuf is already freed */
    834 			ifp->if_oerrors++;
    835 			continue;
    836 		}
    837 
    838 		ifp->if_opackets++;
    839 		if ((p->if_flags & (IFF_RUNNING|IFF_OACTIVE)) == IFF_RUNNING)
    840 			(*p->if_start)(p);
    841 	}
    842 
    843 	ifp->if_flags &= ~IFF_OACTIVE;
    844 }
    845 
    846 /*
    847  * Given an Ethernet frame, find a valid vlan interface corresponding to the
    848  * given source interface and tag, then run the real packet through the
    849  * parent's input routine.
    850  */
    851 void
    852 vlan_input(struct ifnet *ifp, struct mbuf *m)
    853 {
    854 	struct ifvlan *ifv;
    855 	u_int tag;
    856 	struct m_tag *mtag;
    857 
    858 	mtag = m_tag_find(m, PACKET_TAG_VLAN, NULL);
    859 	if (mtag != NULL) {
    860 		/* m contains a normal ethernet frame, the tag is in mtag */
    861 		tag = EVL_VLANOFTAG(*(u_int *)(mtag + 1));
    862 		m_tag_delete(m, mtag);
    863 	} else {
    864 		switch (ifp->if_type) {
    865 		case IFT_ETHER:
    866 		    {
    867 			struct ether_vlan_header *evl;
    868 
    869 			if (m->m_len < sizeof(struct ether_vlan_header) &&
    870 			    (m = m_pullup(m,
    871 			     sizeof(struct ether_vlan_header))) == NULL) {
    872 				printf("%s: no memory for VLAN header, "
    873 				    "dropping packet.\n", ifp->if_xname);
    874 				return;
    875 			}
    876 			evl = mtod(m, struct ether_vlan_header *);
    877 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
    878 
    879 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
    880 
    881 			/*
    882 			 * Restore the original ethertype.  We'll remove
    883 			 * the encapsulation after we've found the vlan
    884 			 * interface corresponding to the tag.
    885 			 */
    886 			evl->evl_encap_proto = evl->evl_proto;
    887 			break;
    888 		    }
    889 
    890 		default:
    891 			tag = (u_int) -1;	/* XXX GCC */
    892 #ifdef DIAGNOSTIC
    893 			panic("vlan_input: impossible");
    894 #endif
    895 		}
    896 	}
    897 
    898 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    899 	    ifv = LIST_NEXT(ifv, ifv_list))
    900 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
    901 			break;
    902 
    903 	if (ifv == NULL ||
    904 	    (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
    905 	     (IFF_UP|IFF_RUNNING)) {
    906 		m_freem(m);
    907 		ifp->if_noproto++;
    908 		return;
    909 	}
    910 
    911 	/*
    912 	 * Now, remove the encapsulation header.  The original
    913 	 * header has already been fixed up above.
    914 	 */
    915 	if (mtag == NULL) {
    916 		memmove(mtod(m, char *) + ifv->ifv_encaplen,
    917 		    mtod(m, void *), sizeof(struct ether_header));
    918 		m_adj(m, ifv->ifv_encaplen);
    919 	}
    920 
    921 	m->m_pkthdr.rcvif = &ifv->ifv_if;
    922 	ifv->ifv_if.if_ipackets++;
    923 
    924 #if NBPFILTER > 0
    925 	if (ifv->ifv_if.if_bpf)
    926 		bpf_mtap(ifv->ifv_if.if_bpf, m);
    927 #endif
    928 
    929 	/* Pass it back through the parent's input routine. */
    930 	(*ifp->if_input)(&ifv->ifv_if, m);
    931 }
    932