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