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