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