Home | History | Annotate | Line # | Download | only in net
if_vlan.c revision 1.99
      1 /*	$NetBSD: if_vlan.c,v 1.99 2017/08/09 06:17:23 knakahara 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.99 2017/08/09 06:17:23 knakahara Exp $");
     82 
     83 #ifdef _KERNEL_OPT
     84 #include "opt_inet.h"
     85 #include "opt_net_mpsafe.h"
     86 #endif
     87 
     88 #include <sys/param.h>
     89 #include <sys/systm.h>
     90 #include <sys/kernel.h>
     91 #include <sys/mbuf.h>
     92 #include <sys/queue.h>
     93 #include <sys/socket.h>
     94 #include <sys/sockio.h>
     95 #include <sys/systm.h>
     96 #include <sys/proc.h>
     97 #include <sys/kauth.h>
     98 #include <sys/mutex.h>
     99 #include <sys/kmem.h>
    100 #include <sys/cpu.h>
    101 #include <sys/pserialize.h>
    102 #include <sys/psref.h>
    103 #include <sys/pslist.h>
    104 #include <sys/atomic.h>
    105 #include <sys/device.h>
    106 #include <sys/module.h>
    107 
    108 #include <net/bpf.h>
    109 #include <net/if.h>
    110 #include <net/if_dl.h>
    111 #include <net/if_types.h>
    112 #include <net/if_ether.h>
    113 #include <net/if_vlanvar.h>
    114 
    115 #ifdef INET
    116 #include <netinet/in.h>
    117 #include <netinet/if_inarp.h>
    118 #endif
    119 #ifdef INET6
    120 #include <netinet6/in6_ifattach.h>
    121 #include <netinet6/in6_var.h>
    122 #endif
    123 
    124 #include "ioconf.h"
    125 
    126 #ifdef NET_MPSAFE
    127 #define VLAN_MPSAFE		1
    128 #endif
    129 
    130 struct vlan_mc_entry {
    131 	LIST_ENTRY(vlan_mc_entry)	mc_entries;
    132 	/*
    133 	 * A key to identify this entry.  The mc_addr below can't be
    134 	 * used since multiple sockaddr may mapped into the same
    135 	 * ether_multi (e.g., AF_UNSPEC).
    136 	 */
    137 	union {
    138 		struct ether_multi	*mcu_enm;
    139 	} mc_u;
    140 	struct sockaddr_storage		mc_addr;
    141 };
    142 
    143 #define	mc_enm		mc_u.mcu_enm
    144 
    145 
    146 struct ifvlan_linkmib {
    147 	struct ifvlan *ifvm_ifvlan;
    148 	const struct vlan_multisw *ifvm_msw;
    149 	int	ifvm_encaplen;	/* encapsulation length */
    150 	int	ifvm_mtufudge;	/* MTU fudged by this much */
    151 	int	ifvm_mintu;	/* min transmission unit */
    152 	uint16_t ifvm_proto;	/* encapsulation ethertype */
    153 	uint16_t ifvm_tag;	/* tag to apply on packets */
    154 	struct ifnet *ifvm_p;		/* parent interface of this vlan */
    155 
    156 	struct psref_target ifvm_psref;
    157 };
    158 
    159 struct ifvlan {
    160 	union {
    161 		struct ethercom ifvu_ec;
    162 	} ifv_u;
    163 	struct ifvlan_linkmib *ifv_mib;	/*
    164 					 * reader must use vlan_getref_linkmib()
    165 					 * instead of direct dereference
    166 					 */
    167 	kmutex_t ifv_lock;		/* writer lock for ifv_mib */
    168 
    169 	LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
    170 	LIST_ENTRY(ifvlan) ifv_list;
    171 	struct pslist_entry ifv_hash;
    172 	int ifv_flags;
    173 };
    174 
    175 #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
    176 
    177 #define	ifv_ec		ifv_u.ifvu_ec
    178 
    179 #define	ifv_if		ifv_ec.ec_if
    180 
    181 #define	ifv_msw		ifv_mib.ifvm_msw
    182 #define	ifv_encaplen	ifv_mib.ifvm_encaplen
    183 #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
    184 #define	ifv_mintu	ifv_mib.ifvm_mintu
    185 #define	ifv_tag		ifv_mib.ifvm_tag
    186 
    187 struct vlan_multisw {
    188 	int	(*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
    189 	int	(*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
    190 	void	(*vmsw_purgemulti)(struct ifvlan *);
    191 };
    192 
    193 static int	vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
    194 static int	vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
    195 static void	vlan_ether_purgemulti(struct ifvlan *);
    196 
    197 const struct vlan_multisw vlan_ether_multisw = {
    198 	vlan_ether_addmulti,
    199 	vlan_ether_delmulti,
    200 	vlan_ether_purgemulti,
    201 };
    202 
    203 static int	vlan_clone_create(struct if_clone *, int);
    204 static int	vlan_clone_destroy(struct ifnet *);
    205 static int	vlan_config(struct ifvlan *, struct ifnet *,
    206     uint16_t);
    207 static int	vlan_ioctl(struct ifnet *, u_long, void *);
    208 static void	vlan_start(struct ifnet *);
    209 static int	vlan_transmit(struct ifnet *, struct mbuf *);
    210 static void	vlan_unconfig(struct ifnet *);
    211 static int	vlan_unconfig_locked(struct ifvlan *,
    212     struct ifvlan_linkmib *);
    213 static void	vlan_hash_init(void);
    214 static int	vlan_hash_fini(void);
    215 static struct ifvlan_linkmib*	vlan_getref_linkmib(struct ifvlan *,
    216     struct psref *);
    217 static void	vlan_putref_linkmib(struct ifvlan_linkmib *,
    218     struct psref *);
    219 static void	vlan_linkmib_update(struct ifvlan *,
    220     struct ifvlan_linkmib *);
    221 static struct ifvlan_linkmib*	vlan_lookup_tag_psref(struct ifnet *,
    222     uint16_t, struct psref *);
    223 static int	tag_hash_func(uint16_t, u_long);
    224 
    225 LIST_HEAD(vlan_ifvlist, ifvlan);
    226 static struct {
    227 	kmutex_t lock;
    228 	struct vlan_ifvlist list;
    229 } ifv_list __cacheline_aligned;
    230 
    231 
    232 #if !defined(VLAN_TAG_HASH_SIZE)
    233 #define VLAN_TAG_HASH_SIZE 32
    234 #endif
    235 static struct {
    236 	kmutex_t lock;
    237 	struct pslist_head *lists;
    238 	u_long mask;
    239 } ifv_hash __cacheline_aligned = {
    240 	.lists = NULL,
    241 	.mask = 0,
    242 };
    243 
    244 pserialize_t vlan_psz __read_mostly;
    245 static struct psref_class *ifvm_psref_class __read_mostly;
    246 
    247 struct if_clone vlan_cloner =
    248     IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
    249 
    250 /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
    251 static char vlan_zero_pad_buff[ETHER_MIN_LEN];
    252 
    253 void
    254 vlanattach(int n)
    255 {
    256 
    257 	/*
    258 	 * Nothing to do here, initialization is handled by the
    259 	 * module initialization code in vlaninit() below).
    260 	 */
    261 }
    262 
    263 static void
    264 vlaninit(void)
    265 {
    266 	mutex_init(&ifv_list.lock, MUTEX_DEFAULT, IPL_NONE);
    267 	LIST_INIT(&ifv_list.list);
    268 
    269 	mutex_init(&ifv_hash.lock, MUTEX_DEFAULT, IPL_NONE);
    270 	vlan_psz = pserialize_create();
    271 	ifvm_psref_class = psref_class_create("vlanlinkmib", IPL_SOFTNET);
    272 	if_clone_attach(&vlan_cloner);
    273 
    274 	vlan_hash_init();
    275 }
    276 
    277 static int
    278 vlandetach(void)
    279 {
    280 	int error = 0;
    281 
    282 	mutex_enter(&ifv_list.lock);
    283 	if (!LIST_EMPTY(&ifv_list.list)) {
    284 		mutex_exit(&ifv_list.lock);
    285 		return EBUSY;
    286 	}
    287 	mutex_exit(&ifv_list.lock);
    288 
    289 	error = vlan_hash_fini();
    290 	if (error != 0)
    291 		return error;
    292 
    293 	if_clone_detach(&vlan_cloner);
    294 	psref_class_destroy(ifvm_psref_class);
    295 	pserialize_destroy(vlan_psz);
    296 	mutex_destroy(&ifv_hash.lock);
    297 	mutex_destroy(&ifv_list.lock);
    298 
    299 	return 0;
    300 }
    301 
    302 static void
    303 vlan_reset_linkname(struct ifnet *ifp)
    304 {
    305 
    306 	/*
    307 	 * We start out with a "802.1Q VLAN" type and zero-length
    308 	 * addresses.  When we attach to a parent interface, we
    309 	 * inherit its type, address length, address, and data link
    310 	 * type.
    311 	 */
    312 
    313 	ifp->if_type = IFT_L2VLAN;
    314 	ifp->if_addrlen = 0;
    315 	ifp->if_dlt = DLT_NULL;
    316 	if_alloc_sadl(ifp);
    317 }
    318 
    319 static int
    320 vlan_clone_create(struct if_clone *ifc, int unit)
    321 {
    322 	struct ifvlan *ifv;
    323 	struct ifnet *ifp;
    324 	struct ifvlan_linkmib *mib;
    325 
    326 	ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK|M_ZERO);
    327 	mib = kmem_zalloc(sizeof(struct ifvlan_linkmib), KM_SLEEP);
    328 	ifp = &ifv->ifv_if;
    329 	LIST_INIT(&ifv->ifv_mc_listhead);
    330 
    331 	mib->ifvm_ifvlan = ifv;
    332 	mib->ifvm_p = NULL;
    333 	psref_target_init(&mib->ifvm_psref, ifvm_psref_class);
    334 
    335 	mutex_init(&ifv->ifv_lock, MUTEX_DEFAULT, IPL_NONE);
    336 	ifv->ifv_mib = mib;
    337 
    338 	mutex_enter(&ifv_list.lock);
    339 	LIST_INSERT_HEAD(&ifv_list.list, ifv, ifv_list);
    340 	mutex_exit(&ifv_list.lock);
    341 
    342 	if_initname(ifp, ifc->ifc_name, unit);
    343 	ifp->if_softc = ifv;
    344 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    345 #ifdef VLAN_MPSAFE
    346 	ifp->if_extflags = IFEF_START_MPSAFE;
    347 #endif
    348 	ifp->if_start = vlan_start;
    349 	ifp->if_transmit = vlan_transmit;
    350 	ifp->if_ioctl = vlan_ioctl;
    351 	IFQ_SET_READY(&ifp->if_snd);
    352 
    353 	if_initialize(ifp);
    354 	vlan_reset_linkname(ifp);
    355 	if_register(ifp);
    356 
    357 	return (0);
    358 }
    359 
    360 static int
    361 vlan_clone_destroy(struct ifnet *ifp)
    362 {
    363 	struct ifvlan *ifv = ifp->if_softc;
    364 
    365 	mutex_enter(&ifv_list.lock);
    366 	LIST_REMOVE(ifv, ifv_list);
    367 	mutex_exit(&ifv_list.lock);
    368 
    369 	vlan_unconfig(ifp);
    370 	if_detach(ifp);
    371 
    372 	psref_target_destroy(&ifv->ifv_mib->ifvm_psref, ifvm_psref_class);
    373 	kmem_free(ifv->ifv_mib, sizeof(struct ifvlan_linkmib));
    374 	mutex_destroy(&ifv->ifv_lock);
    375 	free(ifv, M_DEVBUF);
    376 
    377 	return (0);
    378 }
    379 
    380 /*
    381  * Configure a VLAN interface.
    382  */
    383 static int
    384 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
    385 {
    386 	struct ifnet *ifp = &ifv->ifv_if;
    387 	struct ifvlan_linkmib *nmib = NULL;
    388 	struct ifvlan_linkmib *omib = NULL;
    389 	struct psref_target *nmib_psref = NULL;
    390 	int error = 0;
    391 	int idx;
    392 	bool omib_cleanup = false;
    393 
    394 	nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
    395 
    396 	mutex_enter(&ifv->ifv_lock);
    397 	omib = ifv->ifv_mib;
    398 
    399 	if (omib->ifvm_p != NULL) {
    400 		error = EBUSY;
    401 		goto done;
    402 	}
    403 
    404 	*nmib = *omib;
    405 	nmib_psref = &nmib->ifvm_psref;
    406 
    407 	psref_target_init(nmib_psref, ifvm_psref_class);
    408 
    409 	switch (p->if_type) {
    410 	case IFT_ETHER:
    411 	    {
    412 		struct ethercom *ec = (void *) p;
    413 		nmib->ifvm_msw = &vlan_ether_multisw;
    414 		nmib->ifvm_encaplen = ETHER_VLAN_ENCAP_LEN;
    415 		nmib->ifvm_mintu = ETHERMIN;
    416 
    417 		if (ec->ec_nvlans++ == 0) {
    418 			if ((error = ether_enable_vlan_mtu(p)) >= 0) {
    419 				if (error) {
    420 					ec->ec_nvlans--;
    421 					goto done;
    422 				}
    423 				nmib->ifvm_mtufudge = 0;
    424 			} else {
    425 				/*
    426 				 * Fudge the MTU by the encapsulation size. This
    427 				 * makes us incompatible with strictly compliant
    428 				 * 802.1Q implementations, but allows us to use
    429 				 * the feature with other NetBSD
    430 				 * implementations, which might still be useful.
    431 				 */
    432 				nmib->ifvm_mtufudge = nmib->ifvm_encaplen;
    433 			}
    434 			error = 0;
    435 		}
    436 
    437 		/*
    438 		 * If the parent interface can do hardware-assisted
    439 		 * VLAN encapsulation, then propagate its hardware-
    440 		 * assisted checksumming flags and tcp segmentation
    441 		 * offload.
    442 		 */
    443 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
    444 		        ec->ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
    445 			ifp->if_capabilities = p->if_capabilities &
    446 			    (IFCAP_TSOv4 | IFCAP_TSOv6 |
    447 			     IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx|
    448 			     IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx|
    449 			     IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx|
    450 			     IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx|
    451 			     IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx);
    452                 }
    453 		/*
    454 		 * We inherit the parent's Ethernet address.
    455 		 */
    456 		ether_ifattach(ifp, CLLADDR(p->if_sadl));
    457 		ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
    458 		break;
    459 	    }
    460 
    461 	default:
    462 		error = EPROTONOSUPPORT;
    463 		goto done;
    464 	}
    465 
    466 	nmib->ifvm_p = p;
    467 	nmib->ifvm_tag = tag;
    468 	ifv->ifv_if.if_mtu = p->if_mtu - nmib->ifvm_mtufudge;
    469 	ifv->ifv_if.if_flags = p->if_flags &
    470 	    (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
    471 
    472 	/*
    473 	 * Inherit the if_type from the parent.  This allows us
    474 	 * to participate in bridges of that type.
    475 	 */
    476 	ifv->ifv_if.if_type = p->if_type;
    477 
    478 	idx = tag_hash_func(tag, ifv_hash.mask);
    479 
    480 	mutex_enter(&ifv_hash.lock);
    481 	PSLIST_WRITER_INSERT_HEAD(&ifv_hash.lists[idx], ifv, ifv_hash);
    482 	mutex_exit(&ifv_hash.lock);
    483 
    484 	vlan_linkmib_update(ifv, nmib);
    485 	nmib = NULL;
    486 	nmib_psref = NULL;
    487 	omib_cleanup = true;
    488 
    489 done:
    490 	mutex_exit(&ifv->ifv_lock);
    491 
    492 	if (nmib_psref)
    493 		psref_target_destroy(nmib_psref, ifvm_psref_class);
    494 
    495 	if (nmib)
    496 		kmem_free(nmib, sizeof(*nmib));
    497 
    498 	if (omib_cleanup)
    499 		kmem_free(omib, sizeof(*omib));
    500 
    501 	return error;
    502 }
    503 
    504 /*
    505  * Unconfigure a VLAN interface.
    506  */
    507 static void
    508 vlan_unconfig(struct ifnet *ifp)
    509 {
    510 	struct ifvlan *ifv = ifp->if_softc;
    511 	struct ifvlan_linkmib *nmib = NULL;
    512 	int error;
    513 
    514 	nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
    515 
    516 	mutex_enter(&ifv->ifv_lock);
    517 	error = vlan_unconfig_locked(ifv, nmib);
    518 	mutex_exit(&ifv->ifv_lock);
    519 
    520 	if (error)
    521 		kmem_free(nmib, sizeof(*nmib));
    522 }
    523 static int
    524 vlan_unconfig_locked(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
    525 {
    526 	struct ifnet *p;
    527 	struct ifnet *ifp = &ifv->ifv_if;
    528 	struct psref_target *nmib_psref = NULL;
    529 	struct ifvlan_linkmib *omib;
    530 	int error = 0;
    531 
    532 	KASSERT(mutex_owned(&ifv->ifv_lock));
    533 
    534 	omib = ifv->ifv_mib;
    535 	p = omib->ifvm_p;
    536 
    537 	if (p == NULL) {
    538 		error = -1;
    539 		goto done;
    540 	}
    541 
    542 	*nmib = *omib;
    543 	nmib_psref = &nmib->ifvm_psref;
    544 	psref_target_init(nmib_psref, ifvm_psref_class);
    545 
    546 	/*
    547  	 * Since the interface is being unconfigured, we need to empty the
    548 	 * list of multicast groups that we may have joined while we were
    549 	 * alive and remove them from the parent's list also.
    550 	 */
    551 	(*nmib->ifvm_msw->vmsw_purgemulti)(ifv);
    552 
    553 	/* Disconnect from parent. */
    554 	switch (p->if_type) {
    555 	case IFT_ETHER:
    556 	    {
    557 		struct ethercom *ec = (void *)p;
    558 		if (--ec->ec_nvlans == 0)
    559 			(void)ether_disable_vlan_mtu(p);
    560 
    561 		ether_ifdetach(ifp);
    562 		/* Restore vlan_ioctl overwritten by ether_ifdetach */
    563 		ifp->if_ioctl = vlan_ioctl;
    564 		vlan_reset_linkname(ifp);
    565 		break;
    566 	    }
    567 
    568 #ifdef DIAGNOSTIC
    569 	default:
    570 		panic("vlan_unconfig: impossible");
    571 #endif
    572 	}
    573 
    574 	nmib->ifvm_p = NULL;
    575 	ifv->ifv_if.if_mtu = 0;
    576 	ifv->ifv_flags = 0;
    577 
    578 	mutex_enter(&ifv_hash.lock);
    579 	PSLIST_WRITER_REMOVE(ifv, ifv_hash);
    580 	pserialize_perform(vlan_psz);
    581 	mutex_exit(&ifv_hash.lock);
    582 
    583 	vlan_linkmib_update(ifv, nmib);
    584 
    585 	mutex_exit(&ifv->ifv_lock);
    586 
    587 	nmib_psref = NULL;
    588 	kmem_free(omib, sizeof(*omib));
    589 
    590 #ifdef INET6
    591 	/* To delete v6 link local addresses */
    592 	if (in6_present)
    593 		in6_ifdetach(ifp);
    594 #endif
    595 
    596 	if ((ifp->if_flags & IFF_PROMISC) != 0)
    597 		ifpromisc(ifp, 0);
    598 	if_down(ifp);
    599 	ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
    600 	ifp->if_capabilities = 0;
    601 	mutex_enter(&ifv->ifv_lock);
    602 done:
    603 
    604 	if (nmib_psref)
    605 		psref_target_destroy(nmib_psref, ifvm_psref_class);
    606 
    607 	return error;
    608 }
    609 
    610 static void
    611 vlan_hash_init(void)
    612 {
    613 
    614 	ifv_hash.lists = hashinit(VLAN_TAG_HASH_SIZE, HASH_PSLIST, true,
    615 	    &ifv_hash.mask);
    616 }
    617 
    618 static int
    619 vlan_hash_fini(void)
    620 {
    621 	int i;
    622 
    623 	mutex_enter(&ifv_hash.lock);
    624 
    625 	for (i = 0; i < ifv_hash.mask + 1; i++) {
    626 		if (PSLIST_WRITER_FIRST(&ifv_hash.lists[i], struct ifvlan,
    627 		    ifv_hash) != NULL) {
    628 			mutex_exit(&ifv_hash.lock);
    629 			return EBUSY;
    630 		}
    631 	}
    632 
    633 	for (i = 0; i < ifv_hash.mask + 1; i++)
    634 		PSLIST_DESTROY(&ifv_hash.lists[i]);
    635 
    636 	mutex_exit(&ifv_hash.lock);
    637 
    638 	hashdone(ifv_hash.lists, HASH_PSLIST, ifv_hash.mask);
    639 
    640 	ifv_hash.lists = NULL;
    641 	ifv_hash.mask = 0;
    642 
    643 	return 0;
    644 }
    645 
    646 static int
    647 tag_hash_func(uint16_t tag, u_long mask)
    648 {
    649 	uint32_t hash;
    650 
    651 	hash = (tag >> 8) ^ tag;
    652 	hash = (hash >> 2) ^ hash;
    653 
    654 	return hash & mask;
    655 }
    656 
    657 static struct ifvlan_linkmib *
    658 vlan_getref_linkmib(struct ifvlan *sc, struct psref *psref)
    659 {
    660 	struct ifvlan_linkmib *mib;
    661 	int s;
    662 
    663 	s = pserialize_read_enter();
    664 	mib = sc->ifv_mib;
    665 	if (mib == NULL) {
    666 		pserialize_read_exit(s);
    667 		return NULL;
    668 	}
    669 	membar_datadep_consumer();
    670 	psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
    671 	pserialize_read_exit(s);
    672 
    673 	return mib;
    674 }
    675 
    676 static void
    677 vlan_putref_linkmib(struct ifvlan_linkmib *mib, struct psref *psref)
    678 {
    679 	if (mib == NULL)
    680 		return;
    681 	psref_release(psref, &mib->ifvm_psref, ifvm_psref_class);
    682 }
    683 
    684 static struct ifvlan_linkmib *
    685 vlan_lookup_tag_psref(struct ifnet *ifp, uint16_t tag, struct psref *psref)
    686 {
    687 	int idx;
    688 	int s;
    689 	struct ifvlan *sc;
    690 
    691 	idx = tag_hash_func(tag, ifv_hash.mask);
    692 
    693 	s = pserialize_read_enter();
    694 	PSLIST_READER_FOREACH(sc, &ifv_hash.lists[idx], struct ifvlan,
    695 	    ifv_hash) {
    696 		struct ifvlan_linkmib *mib = sc->ifv_mib;
    697 		if (mib == NULL)
    698 			continue;
    699 		if (mib->ifvm_tag != tag)
    700 			continue;
    701 		if (mib->ifvm_p != ifp)
    702 			continue;
    703 
    704 		psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
    705 		pserialize_read_exit(s);
    706 		return mib;
    707 	}
    708 	pserialize_read_exit(s);
    709 	return NULL;
    710 }
    711 
    712 static void
    713 vlan_linkmib_update(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
    714 {
    715 	struct ifvlan_linkmib *omib = ifv->ifv_mib;
    716 
    717 	KASSERT(mutex_owned(&ifv->ifv_lock));
    718 
    719 	membar_producer();
    720 	ifv->ifv_mib = nmib;
    721 
    722 	pserialize_perform(vlan_psz);
    723 	psref_target_destroy(&omib->ifvm_psref, ifvm_psref_class);
    724 }
    725 
    726 /*
    727  * Called when a parent interface is detaching; destroy any VLAN
    728  * configuration for the parent interface.
    729  */
    730 void
    731 vlan_ifdetach(struct ifnet *p)
    732 {
    733 	struct ifvlan *ifv;
    734 	struct ifvlan_linkmib *mib, **nmibs;
    735 	struct psref psref;
    736 	int error;
    737 	int bound;
    738 	int i, cnt = 0;
    739 
    740 	bound = curlwp_bind();
    741 	mutex_enter(&ifv_list.lock);
    742 	LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
    743 		mib = vlan_getref_linkmib(ifv, &psref);
    744 		if (mib == NULL)
    745 			continue;
    746 
    747 		if (mib->ifvm_p == p)
    748 			cnt++;
    749 
    750 		vlan_putref_linkmib(mib, &psref);
    751 	}
    752 	mutex_exit(&ifv_list.lock);
    753 
    754 	/*
    755 	 * The value of "cnt" does not increase while ifv_list.lock
    756 	 * and ifv->ifv_lock are released here, because the parent
    757 	 * interface is detaching.
    758 	 */
    759 	nmibs = kmem_alloc(sizeof(*nmibs) * cnt, KM_SLEEP);
    760 	for (i=0; i < cnt; i++) {
    761 		nmibs[i] = kmem_alloc(sizeof(*nmibs[i]), KM_SLEEP);
    762 	}
    763 
    764 	mutex_enter(&ifv_list.lock);
    765 
    766 	i = 0;
    767 	LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
    768 		mutex_enter(&ifv->ifv_lock);
    769 		if (ifv->ifv_mib->ifvm_p == p) {
    770 			KASSERTMSG(i < cnt, "no memory for unconfig, parent=%s",
    771 			    p->if_xname);
    772 			error = vlan_unconfig_locked(ifv, nmibs[i]);
    773 			if (!error) {
    774 				nmibs[i] = NULL;
    775 				i++;
    776 			}
    777 
    778 		}
    779 		mutex_exit(&ifv->ifv_lock);
    780 	}
    781 
    782 	mutex_exit(&ifv_list.lock);
    783 	curlwp_bindx(bound);
    784 
    785 	for (i=0; i < cnt; i++) {
    786 		if (nmibs[i])
    787 			kmem_free(nmibs[i], sizeof(*nmibs[i]));
    788 	}
    789 
    790 	kmem_free(nmibs, sizeof(*nmibs) * cnt);
    791 
    792 	return;
    793 }
    794 
    795 static int
    796 vlan_set_promisc(struct ifnet *ifp)
    797 {
    798 	struct ifvlan *ifv = ifp->if_softc;
    799 	struct ifvlan_linkmib *mib;
    800 	struct psref psref;
    801 	int error = 0;
    802 	int bound;
    803 
    804 	bound = curlwp_bind();
    805 	mib = vlan_getref_linkmib(ifv, &psref);
    806 	if (mib == NULL) {
    807 		curlwp_bindx(bound);
    808 		return EBUSY;
    809 	}
    810 
    811 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    812 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
    813 			error = ifpromisc(mib->ifvm_p, 1);
    814 			if (error == 0)
    815 				ifv->ifv_flags |= IFVF_PROMISC;
    816 		}
    817 	} else {
    818 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
    819 			error = ifpromisc(mib->ifvm_p, 0);
    820 			if (error == 0)
    821 				ifv->ifv_flags &= ~IFVF_PROMISC;
    822 		}
    823 	}
    824 	vlan_putref_linkmib(mib, &psref);
    825 	curlwp_bindx(bound);
    826 
    827 	return (error);
    828 }
    829 
    830 static int
    831 vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    832 {
    833 	struct lwp *l = curlwp;	/* XXX */
    834 	struct ifvlan *ifv = ifp->if_softc;
    835 	struct ifaddr *ifa = (struct ifaddr *) data;
    836 	struct ifreq *ifr = (struct ifreq *) data;
    837 	struct ifnet *pr;
    838 	struct ifcapreq *ifcr;
    839 	struct vlanreq vlr;
    840 	struct ifvlan_linkmib *mib;
    841 	struct psref psref;
    842 	int error = 0;
    843 	int bound;
    844 
    845 	switch (cmd) {
    846 	case SIOCSIFMTU:
    847 		bound = curlwp_bind();
    848 		mib = vlan_getref_linkmib(ifv, &psref);
    849 		if (mib == NULL) {
    850 			curlwp_bindx(bound);
    851 			error = EBUSY;
    852 			break;
    853 		}
    854 
    855 		if (mib->ifvm_p == NULL) {
    856 			vlan_putref_linkmib(mib, &psref);
    857 			curlwp_bindx(bound);
    858 			error = EINVAL;
    859 		} else if (
    860 		    ifr->ifr_mtu > (mib->ifvm_p->if_mtu - mib->ifvm_mtufudge) ||
    861 		    ifr->ifr_mtu < (mib->ifvm_mintu - mib->ifvm_mtufudge)) {
    862 			vlan_putref_linkmib(mib, &psref);
    863 			curlwp_bindx(bound);
    864 			error = EINVAL;
    865 		} else {
    866 			vlan_putref_linkmib(mib, &psref);
    867 			curlwp_bindx(bound);
    868 
    869 			error = ifioctl_common(ifp, cmd, data);
    870 			if (error == ENETRESET)
    871 					error = 0;
    872 		}
    873 
    874 		break;
    875 
    876 	case SIOCSETVLAN:
    877 		if ((error = kauth_authorize_network(l->l_cred,
    878 		    KAUTH_NETWORK_INTERFACE,
    879 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    880 		    NULL)) != 0)
    881 			break;
    882 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
    883 			break;
    884 
    885 		if (vlr.vlr_parent[0] == '\0') {
    886 			bound = curlwp_bind();
    887 			mib = vlan_getref_linkmib(ifv, &psref);
    888 			if (mib == NULL) {
    889 				curlwp_bindx(bound);
    890 				error = EBUSY;
    891 				break;
    892 			}
    893 
    894 			if (mib->ifvm_p != NULL &&
    895 			    (ifp->if_flags & IFF_PROMISC) != 0)
    896 				error = ifpromisc(mib->ifvm_p, 0);
    897 
    898 			vlan_putref_linkmib(mib, &psref);
    899 			curlwp_bindx(bound);
    900 
    901 			vlan_unconfig(ifp);
    902 			break;
    903 		}
    904 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
    905 			error = EINVAL;		 /* check for valid tag */
    906 			break;
    907 		}
    908 		if ((pr = ifunit(vlr.vlr_parent)) == 0) {
    909 			error = ENOENT;
    910 			break;
    911 		}
    912 		error = vlan_config(ifv, pr, vlr.vlr_tag);
    913 		if (error != 0) {
    914 			break;
    915 		}
    916 		ifp->if_flags |= IFF_RUNNING;
    917 
    918 		/* Update promiscuous mode, if necessary. */
    919 		vlan_set_promisc(ifp);
    920 		break;
    921 
    922 	case SIOCGETVLAN:
    923 		memset(&vlr, 0, sizeof(vlr));
    924 		bound = curlwp_bind();
    925 		mib = vlan_getref_linkmib(ifv, &psref);
    926 		if (mib == NULL) {
    927 			curlwp_bindx(bound);
    928 			error = EBUSY;
    929 			break;
    930 		}
    931 		if (mib->ifvm_p != NULL) {
    932 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
    933 			    mib->ifvm_p->if_xname);
    934 			vlr.vlr_tag = mib->ifvm_tag;
    935 		}
    936 		vlan_putref_linkmib(mib, &psref);
    937 		curlwp_bindx(bound);
    938 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
    939 		break;
    940 
    941 	case SIOCSIFFLAGS:
    942 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    943 			break;
    944 		/*
    945 		 * For promiscuous mode, we enable promiscuous mode on
    946 		 * the parent if we need promiscuous on the VLAN interface.
    947 		 */
    948 		bound = curlwp_bind();
    949 		mib = vlan_getref_linkmib(ifv, &psref);
    950 		if (mib == NULL) {
    951 			curlwp_bindx(bound);
    952 			error = EBUSY;
    953 			break;
    954 		}
    955 
    956 		if (mib->ifvm_p != NULL)
    957 			error = vlan_set_promisc(ifp);
    958 		vlan_putref_linkmib(mib, &psref);
    959 		curlwp_bindx(bound);
    960 		break;
    961 
    962 	case SIOCADDMULTI:
    963 		mutex_enter(&ifv->ifv_lock);
    964 		mib = ifv->ifv_mib;
    965 		if (mib == NULL) {
    966 			error = EBUSY;
    967 			mutex_exit(&ifv->ifv_lock);
    968 			break;
    969 		}
    970 
    971 		error = (mib->ifvm_p != NULL) ?
    972 		    (*mib->ifvm_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
    973 		mib = NULL;
    974 		mutex_exit(&ifv->ifv_lock);
    975 		break;
    976 
    977 	case SIOCDELMULTI:
    978 		mutex_enter(&ifv->ifv_lock);
    979 		mib = ifv->ifv_mib;
    980 		if (mib == NULL) {
    981 			error = EBUSY;
    982 			mutex_exit(&ifv->ifv_lock);
    983 			break;
    984 		}
    985 		error = (mib->ifvm_p != NULL) ?
    986 		    (*mib->ifvm_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
    987 		mib = NULL;
    988 		mutex_exit(&ifv->ifv_lock);
    989 		break;
    990 
    991 	case SIOCSIFCAP:
    992 		ifcr = data;
    993 		/* make sure caps are enabled on parent */
    994 		bound = curlwp_bind();
    995 		mib = vlan_getref_linkmib(ifv, &psref);
    996 		if (mib == NULL) {
    997 			curlwp_bindx(bound);
    998 			error = EBUSY;
    999 			break;
   1000 		}
   1001 
   1002 		if (mib->ifvm_p == NULL) {
   1003 			vlan_putref_linkmib(mib, &psref);
   1004 			curlwp_bindx(bound);
   1005 			error = EINVAL;
   1006 			break;
   1007 		}
   1008 		if ((mib->ifvm_p->if_capenable & ifcr->ifcr_capenable) !=
   1009 		    ifcr->ifcr_capenable) {
   1010 			vlan_putref_linkmib(mib, &psref);
   1011 			curlwp_bindx(bound);
   1012 			error = EINVAL;
   1013 			break;
   1014 		}
   1015 
   1016 		vlan_putref_linkmib(mib, &psref);
   1017 		curlwp_bindx(bound);
   1018 
   1019 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
   1020 			error = 0;
   1021 		break;
   1022 	case SIOCINITIFADDR:
   1023 		bound = curlwp_bind();
   1024 		mib = vlan_getref_linkmib(ifv, &psref);
   1025 		if (mib == NULL) {
   1026 			curlwp_bindx(bound);
   1027 			error = EBUSY;
   1028 			break;
   1029 		}
   1030 
   1031 		if (mib->ifvm_p == NULL) {
   1032 			error = EINVAL;
   1033 			vlan_putref_linkmib(mib, &psref);
   1034 			curlwp_bindx(bound);
   1035 			break;
   1036 		}
   1037 		vlan_putref_linkmib(mib, &psref);
   1038 		curlwp_bindx(bound);
   1039 
   1040 		ifp->if_flags |= IFF_UP;
   1041 #ifdef INET
   1042 		if (ifa->ifa_addr->sa_family == AF_INET)
   1043 			arp_ifinit(ifp, ifa);
   1044 #endif
   1045 		break;
   1046 
   1047 	default:
   1048 		error = ether_ioctl(ifp, cmd, data);
   1049 	}
   1050 
   1051 	return (error);
   1052 }
   1053 
   1054 static int
   1055 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
   1056 {
   1057 	const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
   1058 	struct vlan_mc_entry *mc;
   1059 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
   1060 	struct ifvlan_linkmib *mib;
   1061 	int error;
   1062 
   1063 	KASSERT(mutex_owned(&ifv->ifv_lock));
   1064 
   1065 	if (sa->sa_len > sizeof(struct sockaddr_storage))
   1066 		return (EINVAL);
   1067 
   1068 	error = ether_addmulti(sa, &ifv->ifv_ec);
   1069 	if (error != ENETRESET)
   1070 		return (error);
   1071 
   1072 	/*
   1073 	 * This is new multicast address.  We have to tell parent
   1074 	 * about it.  Also, remember this multicast address so that
   1075 	 * we can delete them on unconfigure.
   1076 	 */
   1077 	mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
   1078 	if (mc == NULL) {
   1079 		error = ENOMEM;
   1080 		goto alloc_failed;
   1081 	}
   1082 
   1083 	/*
   1084 	 * As ether_addmulti() returns ENETRESET, following two
   1085 	 * statement shouldn't fail.
   1086 	 */
   1087 	(void)ether_multiaddr(sa, addrlo, addrhi);
   1088 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
   1089 	memcpy(&mc->mc_addr, sa, sa->sa_len);
   1090 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
   1091 
   1092 	mib = ifv->ifv_mib;
   1093 	error = if_mcast_op(mib->ifvm_p, SIOCADDMULTI, sa);
   1094 
   1095 	if (error != 0)
   1096 		goto ioctl_failed;
   1097 	return (error);
   1098 
   1099  ioctl_failed:
   1100 	LIST_REMOVE(mc, mc_entries);
   1101 	free(mc, M_DEVBUF);
   1102  alloc_failed:
   1103 	(void)ether_delmulti(sa, &ifv->ifv_ec);
   1104 	return (error);
   1105 }
   1106 
   1107 static int
   1108 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
   1109 {
   1110 	const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
   1111 	struct ether_multi *enm;
   1112 	struct vlan_mc_entry *mc;
   1113 	struct ifvlan_linkmib *mib;
   1114 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
   1115 	int error;
   1116 
   1117 	KASSERT(mutex_owned(&ifv->ifv_lock));
   1118 
   1119 	/*
   1120 	 * Find a key to lookup vlan_mc_entry.  We have to do this
   1121 	 * before calling ether_delmulti for obvious reason.
   1122 	 */
   1123 	if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
   1124 		return (error);
   1125 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
   1126 
   1127 	error = ether_delmulti(sa, &ifv->ifv_ec);
   1128 	if (error != ENETRESET)
   1129 		return (error);
   1130 
   1131 	/* We no longer use this multicast address.  Tell parent so. */
   1132 	mib = ifv->ifv_mib;
   1133 	error = if_mcast_op(mib->ifvm_p, SIOCDELMULTI, sa);
   1134 
   1135 	if (error == 0) {
   1136 		/* And forget about this address. */
   1137 		for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
   1138 		    mc = LIST_NEXT(mc, mc_entries)) {
   1139 			if (mc->mc_enm == enm) {
   1140 				LIST_REMOVE(mc, mc_entries);
   1141 				free(mc, M_DEVBUF);
   1142 				break;
   1143 			}
   1144 		}
   1145 		KASSERT(mc != NULL);
   1146 	} else
   1147 		(void)ether_addmulti(sa, &ifv->ifv_ec);
   1148 	return (error);
   1149 }
   1150 
   1151 /*
   1152  * Delete any multicast address we have asked to add from parent
   1153  * interface.  Called when the vlan is being unconfigured.
   1154  */
   1155 static void
   1156 vlan_ether_purgemulti(struct ifvlan *ifv)
   1157 {
   1158 	struct vlan_mc_entry *mc;
   1159 	struct ifvlan_linkmib *mib;
   1160 
   1161 	KASSERT(mutex_owned(&ifv->ifv_lock));
   1162 	mib = ifv->ifv_mib;
   1163 	if (mib == NULL) {
   1164 		return;
   1165 	}
   1166 
   1167 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
   1168 		(void)if_mcast_op(mib->ifvm_p, SIOCDELMULTI,
   1169 		    (const struct sockaddr *)&mc->mc_addr);
   1170 		LIST_REMOVE(mc, mc_entries);
   1171 		free(mc, M_DEVBUF);
   1172 	}
   1173 }
   1174 
   1175 static void
   1176 vlan_start(struct ifnet *ifp)
   1177 {
   1178 	struct ifvlan *ifv = ifp->if_softc;
   1179 	struct ifnet *p;
   1180 	struct ethercom *ec;
   1181 	struct mbuf *m;
   1182 	struct ifvlan_linkmib *mib;
   1183 	struct psref psref;
   1184 	int error;
   1185 
   1186 #ifndef NET_MPSAFE
   1187 	KASSERT(KERNEL_LOCKED_P());
   1188 #endif
   1189 
   1190 	mib = vlan_getref_linkmib(ifv, &psref);
   1191 	if (mib == NULL)
   1192 		return;
   1193 	p = mib->ifvm_p;
   1194 	ec = (void *)mib->ifvm_p;
   1195 
   1196 	ifp->if_flags |= IFF_OACTIVE;
   1197 
   1198 	for (;;) {
   1199 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1200 		if (m == NULL)
   1201 			break;
   1202 
   1203 #ifdef ALTQ
   1204 		/*
   1205 		 * KERNEL_LOCK is required for ALTQ even if NET_MPSAFE is defined.
   1206 		 */
   1207 		KERNEL_LOCK(1, NULL);
   1208 		/*
   1209 		 * If ALTQ is enabled on the parent interface, do
   1210 		 * classification; the queueing discipline might
   1211 		 * not require classification, but might require
   1212 		 * the address family/header pointer in the pktattr.
   1213 		 */
   1214 		if (ALTQ_IS_ENABLED(&p->if_snd)) {
   1215 			switch (p->if_type) {
   1216 			case IFT_ETHER:
   1217 				altq_etherclassify(&p->if_snd, m);
   1218 				break;
   1219 #ifdef DIAGNOSTIC
   1220 			default:
   1221 				panic("vlan_start: impossible (altq)");
   1222 #endif
   1223 			}
   1224 		}
   1225 		KERNEL_UNLOCK_ONE(NULL);
   1226 #endif /* ALTQ */
   1227 
   1228 		bpf_mtap(ifp, m);
   1229 		/*
   1230 		 * If the parent can insert the tag itself, just mark
   1231 		 * the tag in the mbuf header.
   1232 		 */
   1233 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
   1234 			struct m_tag *mtag;
   1235 
   1236 			mtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
   1237 			    M_NOWAIT);
   1238 			if (mtag == NULL) {
   1239 				ifp->if_oerrors++;
   1240 				m_freem(m);
   1241 				continue;
   1242 			}
   1243 			*(u_int *)(mtag + 1) = mib->ifvm_tag;
   1244 			m_tag_prepend(m, mtag);
   1245 		} else {
   1246 			/*
   1247 			 * insert the tag ourselves
   1248 			 */
   1249 			M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
   1250 			if (m == NULL) {
   1251 				printf("%s: unable to prepend encap header",
   1252 				    p->if_xname);
   1253 				ifp->if_oerrors++;
   1254 				continue;
   1255 			}
   1256 
   1257 			switch (p->if_type) {
   1258 			case IFT_ETHER:
   1259 			    {
   1260 				struct ether_vlan_header *evl;
   1261 
   1262 				if (m->m_len < sizeof(struct ether_vlan_header))
   1263 					m = m_pullup(m,
   1264 					    sizeof(struct ether_vlan_header));
   1265 				if (m == NULL) {
   1266 					printf("%s: unable to pullup encap "
   1267 					    "header", p->if_xname);
   1268 					ifp->if_oerrors++;
   1269 					continue;
   1270 				}
   1271 
   1272 				/*
   1273 				 * Transform the Ethernet header into an
   1274 				 * Ethernet header with 802.1Q encapsulation.
   1275 				 */
   1276 				memmove(mtod(m, void *),
   1277 				    mtod(m, char *) + mib->ifvm_encaplen,
   1278 				    sizeof(struct ether_header));
   1279 				evl = mtod(m, struct ether_vlan_header *);
   1280 				evl->evl_proto = evl->evl_encap_proto;
   1281 				evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
   1282 				evl->evl_tag = htons(mib->ifvm_tag);
   1283 
   1284 				/*
   1285 				 * To cater for VLAN-aware layer 2 ethernet
   1286 				 * switches which may need to strip the tag
   1287 				 * before forwarding the packet, make sure
   1288 				 * the packet+tag is at least 68 bytes long.
   1289 				 * This is necessary because our parent will
   1290 				 * only pad to 64 bytes (ETHER_MIN_LEN) and
   1291 				 * some switches will not pad by themselves
   1292 				 * after deleting a tag.
   1293 				 */
   1294 				if (m->m_pkthdr.len <
   1295 				    (ETHER_MIN_LEN - ETHER_CRC_LEN +
   1296 				     ETHER_VLAN_ENCAP_LEN)) {
   1297 					m_copyback(m, m->m_pkthdr.len,
   1298 					    (ETHER_MIN_LEN - ETHER_CRC_LEN +
   1299 					     ETHER_VLAN_ENCAP_LEN) -
   1300 					     m->m_pkthdr.len,
   1301 					    vlan_zero_pad_buff);
   1302 				}
   1303 				break;
   1304 			    }
   1305 
   1306 #ifdef DIAGNOSTIC
   1307 			default:
   1308 				panic("vlan_start: impossible");
   1309 #endif
   1310 			}
   1311 		}
   1312 
   1313 		if ((p->if_flags & IFF_RUNNING) == 0) {
   1314 			m_freem(m);
   1315 			continue;
   1316 		}
   1317 
   1318 		error = if_transmit_lock(p, m);
   1319 		if (error) {
   1320 			/* mbuf is already freed */
   1321 			ifp->if_oerrors++;
   1322 			continue;
   1323 		}
   1324 		ifp->if_opackets++;
   1325 	}
   1326 
   1327 	ifp->if_flags &= ~IFF_OACTIVE;
   1328 
   1329 	/* Remove reference to mib before release */
   1330 	p = NULL;
   1331 	ec = NULL;
   1332 
   1333 	vlan_putref_linkmib(mib, &psref);
   1334 }
   1335 
   1336 static int
   1337 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
   1338 {
   1339 	struct ifvlan *ifv = ifp->if_softc;
   1340 	struct ifnet *p;
   1341 	struct ethercom *ec;
   1342 	struct ifvlan_linkmib *mib;
   1343 	struct psref psref;
   1344 	int error;
   1345 	size_t pktlen = m->m_pkthdr.len;
   1346 	bool mcast = (m->m_flags & M_MCAST) != 0;
   1347 
   1348 	mib = vlan_getref_linkmib(ifv, &psref);
   1349 	if (mib == NULL) {
   1350 		m_freem(m);
   1351 		return ENETDOWN;
   1352 	}
   1353 
   1354 	p = mib->ifvm_p;
   1355 	ec = (void *)mib->ifvm_p;
   1356 
   1357 	bpf_mtap(ifp, m);
   1358 	/*
   1359 	 * If the parent can insert the tag itself, just mark
   1360 	 * the tag in the mbuf header.
   1361 	 */
   1362 	if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
   1363 		struct m_tag *mtag;
   1364 
   1365 		mtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
   1366 		    M_NOWAIT);
   1367 		if (mtag == NULL) {
   1368 			ifp->if_oerrors++;
   1369 			m_freem(m);
   1370 			error = ENOBUFS;
   1371 			goto out;
   1372 		}
   1373 		*(u_int *)(mtag + 1) = mib->ifvm_tag;
   1374 		m_tag_prepend(m, mtag);
   1375 	} else {
   1376 		/*
   1377 		 * insert the tag ourselves
   1378 		 */
   1379 		M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
   1380 		if (m == NULL) {
   1381 			printf("%s: unable to prepend encap header",
   1382 			    p->if_xname);
   1383 			ifp->if_oerrors++;
   1384 			error = ENOBUFS;
   1385 			goto out;
   1386 		}
   1387 
   1388 		switch (p->if_type) {
   1389 		case IFT_ETHER:
   1390 		    {
   1391 			struct ether_vlan_header *evl;
   1392 
   1393 			if (m->m_len < sizeof(struct ether_vlan_header))
   1394 				m = m_pullup(m,
   1395 				    sizeof(struct ether_vlan_header));
   1396 			if (m == NULL) {
   1397 				printf("%s: unable to pullup encap "
   1398 				    "header", p->if_xname);
   1399 				ifp->if_oerrors++;
   1400 				error = ENOBUFS;
   1401 				goto out;
   1402 			}
   1403 
   1404 			/*
   1405 			 * Transform the Ethernet header into an
   1406 			 * Ethernet header with 802.1Q encapsulation.
   1407 			 */
   1408 			memmove(mtod(m, void *),
   1409 			    mtod(m, char *) + mib->ifvm_encaplen,
   1410 			    sizeof(struct ether_header));
   1411 			evl = mtod(m, struct ether_vlan_header *);
   1412 			evl->evl_proto = evl->evl_encap_proto;
   1413 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
   1414 			evl->evl_tag = htons(mib->ifvm_tag);
   1415 
   1416 			/*
   1417 			 * To cater for VLAN-aware layer 2 ethernet
   1418 			 * switches which may need to strip the tag
   1419 			 * before forwarding the packet, make sure
   1420 			 * the packet+tag is at least 68 bytes long.
   1421 			 * This is necessary because our parent will
   1422 			 * only pad to 64 bytes (ETHER_MIN_LEN) and
   1423 			 * some switches will not pad by themselves
   1424 			 * after deleting a tag.
   1425 			 */
   1426 			if (m->m_pkthdr.len <
   1427 			    (ETHER_MIN_LEN - ETHER_CRC_LEN +
   1428 			     ETHER_VLAN_ENCAP_LEN)) {
   1429 				m_copyback(m, m->m_pkthdr.len,
   1430 				    (ETHER_MIN_LEN - ETHER_CRC_LEN +
   1431 				     ETHER_VLAN_ENCAP_LEN) -
   1432 				     m->m_pkthdr.len,
   1433 				    vlan_zero_pad_buff);
   1434 			}
   1435 			break;
   1436 		    }
   1437 
   1438 #ifdef DIAGNOSTIC
   1439 		default:
   1440 			panic("vlan_transmit: impossible");
   1441 #endif
   1442 		}
   1443 	}
   1444 
   1445 	if ((p->if_flags & IFF_RUNNING) == 0) {
   1446 		m_freem(m);
   1447 		error = ENETDOWN;
   1448 		goto out;
   1449 	}
   1450 
   1451 	error = if_transmit_lock(p, m);
   1452 	if (error) {
   1453 		/* mbuf is already freed */
   1454 		ifp->if_oerrors++;
   1455 	} else {
   1456 
   1457 		ifp->if_opackets++;
   1458 		ifp->if_obytes += pktlen;
   1459 		if (mcast)
   1460 			ifp->if_omcasts++;
   1461 	}
   1462 
   1463 out:
   1464 	/* Remove reference to mib before release */
   1465 	p = NULL;
   1466 	ec = NULL;
   1467 
   1468 	vlan_putref_linkmib(mib, &psref);
   1469 	return error;
   1470 }
   1471 
   1472 /*
   1473  * Given an Ethernet frame, find a valid vlan interface corresponding to the
   1474  * given source interface and tag, then run the real packet through the
   1475  * parent's input routine.
   1476  */
   1477 void
   1478 vlan_input(struct ifnet *ifp, struct mbuf *m)
   1479 {
   1480 	struct ifvlan *ifv;
   1481 	u_int tag;
   1482 	struct m_tag *mtag;
   1483 	struct ifvlan_linkmib *mib;
   1484 	struct psref psref;
   1485 
   1486 	mtag = m_tag_find(m, PACKET_TAG_VLAN, NULL);
   1487 	if (mtag != NULL) {
   1488 		/* m contains a normal ethernet frame, the tag is in mtag */
   1489 		tag = EVL_VLANOFTAG(*(u_int *)(mtag + 1));
   1490 		m_tag_delete(m, mtag);
   1491 	} else {
   1492 		switch (ifp->if_type) {
   1493 		case IFT_ETHER:
   1494 		    {
   1495 			struct ether_vlan_header *evl;
   1496 
   1497 			if (m->m_len < sizeof(struct ether_vlan_header) &&
   1498 			    (m = m_pullup(m,
   1499 			     sizeof(struct ether_vlan_header))) == NULL) {
   1500 				printf("%s: no memory for VLAN header, "
   1501 				    "dropping packet.\n", ifp->if_xname);
   1502 				return;
   1503 			}
   1504 			evl = mtod(m, struct ether_vlan_header *);
   1505 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
   1506 
   1507 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
   1508 
   1509 			/*
   1510 			 * Restore the original ethertype.  We'll remove
   1511 			 * the encapsulation after we've found the vlan
   1512 			 * interface corresponding to the tag.
   1513 			 */
   1514 			evl->evl_encap_proto = evl->evl_proto;
   1515 			break;
   1516 		    }
   1517 
   1518 		default:
   1519 			tag = (u_int) -1;	/* XXX GCC */
   1520 #ifdef DIAGNOSTIC
   1521 			panic("vlan_input: impossible");
   1522 #endif
   1523 		}
   1524 	}
   1525 
   1526 	mib = vlan_lookup_tag_psref(ifp, tag, &psref);
   1527 	if (mib == NULL) {
   1528 		m_freem(m);
   1529 		ifp->if_noproto++;
   1530 		return;
   1531 	}
   1532 
   1533 	ifv = mib->ifvm_ifvlan;
   1534 	if ((ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
   1535 	    (IFF_UP|IFF_RUNNING)) {
   1536 		m_freem(m);
   1537 		ifp->if_noproto++;
   1538 		goto out;
   1539 	}
   1540 
   1541 	/*
   1542 	 * Now, remove the encapsulation header.  The original
   1543 	 * header has already been fixed up above.
   1544 	 */
   1545 	if (mtag == NULL) {
   1546 		memmove(mtod(m, char *) + mib->ifvm_encaplen,
   1547 		    mtod(m, void *), sizeof(struct ether_header));
   1548 		m_adj(m, mib->ifvm_encaplen);
   1549 	}
   1550 
   1551 	m_set_rcvif(m, &ifv->ifv_if);
   1552 	ifv->ifv_if.if_ipackets++;
   1553 
   1554 	m->m_flags &= ~M_PROMISC;
   1555 	if_input(&ifv->ifv_if, m);
   1556 out:
   1557 	vlan_putref_linkmib(mib, &psref);
   1558 }
   1559 
   1560 /*
   1561  * Module infrastructure
   1562  */
   1563 #include "if_module.h"
   1564 
   1565 IF_MODULE(MODULE_CLASS_DRIVER, vlan, "")
   1566