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