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