Home | History | Annotate | Line # | Download | only in net
if_vlan.c revision 1.147
      1 /*	$NetBSD: if_vlan.c,v 1.147 2019/10/21 06:26:12 ozaki-r 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.147 2019/10/21 06:26:12 ozaki-r 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 		vidmem->vid = vid;
    491 		ETHER_LOCK(ec);
    492 		SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidmem, vid_list);
    493 		ETHER_UNLOCK(ec);
    494 
    495 		if (ec->ec_vlan_cb != NULL) {
    496 			/*
    497 			 * Call ec_vlan_cb(). It will setup VLAN HW filter or
    498 			 * HW tagging function.
    499 			 */
    500 			error = (*ec->ec_vlan_cb)(ec, vid, true);
    501 			if (error) {
    502 				ec->ec_nvlans--;
    503 				if (ec->ec_nvlans == 0) {
    504 					IFNET_LOCK(p);
    505 					(void)ether_disable_vlan_mtu(p);
    506 					IFNET_UNLOCK(p);
    507 				}
    508 				goto done;
    509 			}
    510 		}
    511 		/*
    512 		 * If the parent interface can do hardware-assisted
    513 		 * VLAN encapsulation, then propagate its hardware-
    514 		 * assisted checksumming flags and tcp segmentation
    515 		 * offload.
    516 		 */
    517 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
    518 			ifp->if_capabilities = p->if_capabilities &
    519 			    (IFCAP_TSOv4 | IFCAP_TSOv6 |
    520 				IFCAP_CSUM_IPv4_Tx  | IFCAP_CSUM_IPv4_Rx |
    521 				IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    522 				IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
    523 				IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx |
    524 				IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx);
    525 		}
    526 
    527 		/*
    528 		 * We inherit the parent's Ethernet address.
    529 		 */
    530 		ether_ifattach(ifp, CLLADDR(p->if_sadl));
    531 		ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
    532 		break;
    533 	    }
    534 
    535 	default:
    536 		error = EPROTONOSUPPORT;
    537 		goto done;
    538 	}
    539 
    540 	nmib->ifvm_p = p;
    541 	nmib->ifvm_tag = vid;
    542 	ifv->ifv_if.if_mtu = p->if_mtu - nmib->ifvm_mtufudge;
    543 	ifv->ifv_if.if_flags = p->if_flags &
    544 	    (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
    545 
    546 	/*
    547 	 * Inherit the if_type from the parent.  This allows us
    548 	 * to participate in bridges of that type.
    549 	 */
    550 	ifv->ifv_if.if_type = p->if_type;
    551 
    552 	PSLIST_ENTRY_INIT(ifv, ifv_hash);
    553 	idx = vlan_tag_hash(vid, ifv_hash.mask);
    554 
    555 	mutex_enter(&ifv_hash.lock);
    556 	PSLIST_WRITER_INSERT_HEAD(&ifv_hash.lists[idx], ifv, ifv_hash);
    557 	mutex_exit(&ifv_hash.lock);
    558 
    559 	vlan_linkmib_update(ifv, nmib);
    560 	nmib = NULL;
    561 	nmib_psref = NULL;
    562 	omib_cleanup = true;
    563 
    564 done:
    565 	mutex_exit(&ifv->ifv_lock);
    566 
    567 	if (nmib_psref)
    568 		psref_target_destroy(nmib_psref, ifvm_psref_class);
    569 	if (nmib)
    570 		kmem_free(nmib, sizeof(*nmib));
    571 	if (omib_cleanup)
    572 		kmem_free(omib, sizeof(*omib));
    573 
    574 	return error;
    575 }
    576 
    577 /*
    578  * Unconfigure a VLAN interface.
    579  */
    580 static void
    581 vlan_unconfig(struct ifnet *ifp)
    582 {
    583 	struct ifvlan *ifv = ifp->if_softc;
    584 	struct ifvlan_linkmib *nmib = NULL;
    585 	int error;
    586 
    587 	KASSERT(IFNET_LOCKED(ifp));
    588 
    589 	nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
    590 
    591 	mutex_enter(&ifv->ifv_lock);
    592 	error = vlan_unconfig_locked(ifv, nmib);
    593 	mutex_exit(&ifv->ifv_lock);
    594 
    595 	if (error)
    596 		kmem_free(nmib, sizeof(*nmib));
    597 }
    598 static int
    599 vlan_unconfig_locked(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
    600 {
    601 	struct ifnet *p;
    602 	struct ifnet *ifp = &ifv->ifv_if;
    603 	struct psref_target *nmib_psref = NULL;
    604 	struct ifvlan_linkmib *omib;
    605 	int error = 0;
    606 
    607 	KASSERT(IFNET_LOCKED(ifp));
    608 	KASSERT(mutex_owned(&ifv->ifv_lock));
    609 
    610 	ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
    611 
    612 	omib = ifv->ifv_mib;
    613 	p = omib->ifvm_p;
    614 
    615 	if (p == NULL) {
    616 		error = -1;
    617 		goto done;
    618 	}
    619 
    620 	*nmib = *omib;
    621 	nmib_psref = &nmib->ifvm_psref;
    622 	psref_target_init(nmib_psref, ifvm_psref_class);
    623 
    624 	/*
    625 	 * Since the interface is being unconfigured, we need to empty the
    626 	 * list of multicast groups that we may have joined while we were
    627 	 * alive and remove them from the parent's list also.
    628 	 */
    629 	(*nmib->ifvm_msw->vmsw_purgemulti)(ifv);
    630 
    631 	/* Disconnect from parent. */
    632 	switch (p->if_type) {
    633 	case IFT_ETHER:
    634 	    {
    635 		struct ethercom *ec = (void *)p;
    636 		struct vlanid_list *vlanidp;
    637 		uint16_t vid = EVL_VLANOFTAG(nmib->ifvm_tag);
    638 
    639 		ETHER_LOCK(ec);
    640 		SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
    641 			if (vlanidp->vid == vid) {
    642 				SIMPLEQ_REMOVE(&ec->ec_vids, vlanidp,
    643 				    vlanid_list, vid_list);
    644 				break;
    645 			}
    646 		}
    647 		ETHER_UNLOCK(ec);
    648 		if (vlanidp != NULL)
    649 			kmem_free(vlanidp, sizeof(*vlanidp));
    650 
    651 		if (ec->ec_vlan_cb != NULL) {
    652 			/*
    653 			 * Call ec_vlan_cb(). It will setup VLAN HW filter or
    654 			 * HW tagging function.
    655 			 */
    656 			(void)(*ec->ec_vlan_cb)(ec, vid, false);
    657 		}
    658 		if (--ec->ec_nvlans == 0) {
    659 			IFNET_LOCK(p);
    660 			(void)ether_disable_vlan_mtu(p);
    661 			IFNET_UNLOCK(p);
    662 		}
    663 
    664 		/* XXX ether_ifdetach must not be called with IFNET_LOCK */
    665 		mutex_exit(&ifv->ifv_lock);
    666 		IFNET_UNLOCK(ifp);
    667 		ether_ifdetach(ifp);
    668 		IFNET_LOCK(ifp);
    669 		mutex_enter(&ifv->ifv_lock);
    670 
    671 		/* if_free_sadl must be called with IFNET_LOCK */
    672 		if_free_sadl(ifp, 1);
    673 
    674 		/* Restore vlan_ioctl overwritten by ether_ifdetach */
    675 		ifp->if_ioctl = vlan_ioctl;
    676 		vlan_reset_linkname(ifp);
    677 		break;
    678 	    }
    679 
    680 	default:
    681 		panic("%s: impossible", __func__);
    682 	}
    683 
    684 	nmib->ifvm_p = NULL;
    685 	ifv->ifv_if.if_mtu = 0;
    686 	ifv->ifv_flags = 0;
    687 
    688 	mutex_enter(&ifv_hash.lock);
    689 	PSLIST_WRITER_REMOVE(ifv, ifv_hash);
    690 	pserialize_perform(vlan_psz);
    691 	mutex_exit(&ifv_hash.lock);
    692 	PSLIST_ENTRY_DESTROY(ifv, ifv_hash);
    693 
    694 	vlan_linkmib_update(ifv, nmib);
    695 
    696 	mutex_exit(&ifv->ifv_lock);
    697 
    698 	nmib_psref = NULL;
    699 	kmem_free(omib, sizeof(*omib));
    700 
    701 #ifdef INET6
    702 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
    703 	/* To delete v6 link local addresses */
    704 	if (in6_present)
    705 		in6_ifdetach(ifp);
    706 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    707 #endif
    708 
    709 	if ((ifp->if_flags & IFF_PROMISC) != 0)
    710 		vlan_safe_ifpromisc_locked(ifp, 0);
    711 	if_down_locked(ifp);
    712 	ifp->if_capabilities = 0;
    713 	mutex_enter(&ifv->ifv_lock);
    714 done:
    715 
    716 	if (nmib_psref)
    717 		psref_target_destroy(nmib_psref, ifvm_psref_class);
    718 
    719 	return error;
    720 }
    721 
    722 static void
    723 vlan_hash_init(void)
    724 {
    725 
    726 	ifv_hash.lists = hashinit(VLAN_TAG_HASH_SIZE, HASH_PSLIST, true,
    727 	    &ifv_hash.mask);
    728 }
    729 
    730 static int
    731 vlan_hash_fini(void)
    732 {
    733 	int i;
    734 
    735 	mutex_enter(&ifv_hash.lock);
    736 
    737 	for (i = 0; i < ifv_hash.mask + 1; i++) {
    738 		if (PSLIST_WRITER_FIRST(&ifv_hash.lists[i], struct ifvlan,
    739 		    ifv_hash) != NULL) {
    740 			mutex_exit(&ifv_hash.lock);
    741 			return EBUSY;
    742 		}
    743 	}
    744 
    745 	for (i = 0; i < ifv_hash.mask + 1; i++)
    746 		PSLIST_DESTROY(&ifv_hash.lists[i]);
    747 
    748 	mutex_exit(&ifv_hash.lock);
    749 
    750 	hashdone(ifv_hash.lists, HASH_PSLIST, ifv_hash.mask);
    751 
    752 	ifv_hash.lists = NULL;
    753 	ifv_hash.mask = 0;
    754 
    755 	return 0;
    756 }
    757 
    758 static int
    759 vlan_tag_hash(uint16_t tag, u_long mask)
    760 {
    761 	uint32_t hash;
    762 
    763 	hash = (tag >> 8) ^ tag;
    764 	hash = (hash >> 2) ^ hash;
    765 
    766 	return hash & mask;
    767 }
    768 
    769 static struct ifvlan_linkmib *
    770 vlan_getref_linkmib(struct ifvlan *sc, struct psref *psref)
    771 {
    772 	struct ifvlan_linkmib *mib;
    773 	int s;
    774 
    775 	s = pserialize_read_enter();
    776 	mib = sc->ifv_mib;
    777 	if (mib == NULL) {
    778 		pserialize_read_exit(s);
    779 		return NULL;
    780 	}
    781 	membar_datadep_consumer();
    782 	psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
    783 	pserialize_read_exit(s);
    784 
    785 	return mib;
    786 }
    787 
    788 static void
    789 vlan_putref_linkmib(struct ifvlan_linkmib *mib, struct psref *psref)
    790 {
    791 	if (mib == NULL)
    792 		return;
    793 	psref_release(psref, &mib->ifvm_psref, ifvm_psref_class);
    794 }
    795 
    796 static struct ifvlan_linkmib *
    797 vlan_lookup_tag_psref(struct ifnet *ifp, uint16_t tag, struct psref *psref)
    798 {
    799 	int idx;
    800 	int s;
    801 	struct ifvlan *sc;
    802 
    803 	idx = vlan_tag_hash(tag, ifv_hash.mask);
    804 
    805 	s = pserialize_read_enter();
    806 	PSLIST_READER_FOREACH(sc, &ifv_hash.lists[idx], struct ifvlan,
    807 	    ifv_hash) {
    808 		struct ifvlan_linkmib *mib = sc->ifv_mib;
    809 		if (mib == NULL)
    810 			continue;
    811 		if (mib->ifvm_tag != tag)
    812 			continue;
    813 		if (mib->ifvm_p != ifp)
    814 			continue;
    815 
    816 		psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
    817 		pserialize_read_exit(s);
    818 		return mib;
    819 	}
    820 	pserialize_read_exit(s);
    821 	return NULL;
    822 }
    823 
    824 static void
    825 vlan_linkmib_update(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
    826 {
    827 	struct ifvlan_linkmib *omib = ifv->ifv_mib;
    828 
    829 	KASSERT(mutex_owned(&ifv->ifv_lock));
    830 
    831 	membar_producer();
    832 	ifv->ifv_mib = nmib;
    833 
    834 	pserialize_perform(ifv->ifv_psz);
    835 	psref_target_destroy(&omib->ifvm_psref, ifvm_psref_class);
    836 }
    837 
    838 /*
    839  * Called when a parent interface is detaching; destroy any VLAN
    840  * configuration for the parent interface.
    841  */
    842 void
    843 vlan_ifdetach(struct ifnet *p)
    844 {
    845 	struct ifvlan *ifv;
    846 	struct ifvlan_linkmib *mib, **nmibs;
    847 	struct psref psref;
    848 	int error;
    849 	int bound;
    850 	int i, cnt = 0;
    851 
    852 	bound = curlwp_bind();
    853 
    854 	mutex_enter(&ifv_list.lock);
    855 	LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
    856 		mib = vlan_getref_linkmib(ifv, &psref);
    857 		if (mib == NULL)
    858 			continue;
    859 
    860 		if (mib->ifvm_p == p)
    861 			cnt++;
    862 
    863 		vlan_putref_linkmib(mib, &psref);
    864 	}
    865 	mutex_exit(&ifv_list.lock);
    866 
    867 	if (cnt == 0) {
    868 		curlwp_bindx(bound);
    869 		return;
    870 	}
    871 
    872 	/*
    873 	 * The value of "cnt" does not increase while ifv_list.lock
    874 	 * and ifv->ifv_lock are released here, because the parent
    875 	 * interface is detaching.
    876 	 */
    877 	nmibs = kmem_alloc(sizeof(*nmibs) * cnt, KM_SLEEP);
    878 	for (i = 0; i < cnt; i++) {
    879 		nmibs[i] = kmem_alloc(sizeof(*nmibs[i]), KM_SLEEP);
    880 	}
    881 
    882 	mutex_enter(&ifv_list.lock);
    883 
    884 	i = 0;
    885 	LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
    886 		struct ifnet *ifp = &ifv->ifv_if;
    887 
    888 		/* IFNET_LOCK must be held before ifv_lock. */
    889 		IFNET_LOCK(ifp);
    890 		mutex_enter(&ifv->ifv_lock);
    891 
    892 		/* XXX ifv_mib = NULL? */
    893 		if (ifv->ifv_mib->ifvm_p == p) {
    894 			KASSERTMSG(i < cnt,
    895 			    "no memory for unconfig, parent=%s", p->if_xname);
    896 			error = vlan_unconfig_locked(ifv, nmibs[i]);
    897 			if (!error) {
    898 				nmibs[i] = NULL;
    899 				i++;
    900 			}
    901 
    902 		}
    903 
    904 		mutex_exit(&ifv->ifv_lock);
    905 		IFNET_UNLOCK(ifp);
    906 	}
    907 
    908 	mutex_exit(&ifv_list.lock);
    909 
    910 	curlwp_bindx(bound);
    911 
    912 	for (i = 0; i < cnt; i++) {
    913 		if (nmibs[i])
    914 			kmem_free(nmibs[i], sizeof(*nmibs[i]));
    915 	}
    916 
    917 	kmem_free(nmibs, sizeof(*nmibs) * cnt);
    918 
    919 	return;
    920 }
    921 
    922 static int
    923 vlan_set_promisc(struct ifnet *ifp)
    924 {
    925 	struct ifvlan *ifv = ifp->if_softc;
    926 	struct ifvlan_linkmib *mib;
    927 	struct psref psref;
    928 	int error = 0;
    929 	int bound;
    930 
    931 	bound = curlwp_bind();
    932 	mib = vlan_getref_linkmib(ifv, &psref);
    933 	if (mib == NULL) {
    934 		curlwp_bindx(bound);
    935 		return EBUSY;
    936 	}
    937 
    938 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    939 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
    940 			error = vlan_safe_ifpromisc(mib->ifvm_p, 1);
    941 			if (error == 0)
    942 				ifv->ifv_flags |= IFVF_PROMISC;
    943 		}
    944 	} else {
    945 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
    946 			error = vlan_safe_ifpromisc(mib->ifvm_p, 0);
    947 			if (error == 0)
    948 				ifv->ifv_flags &= ~IFVF_PROMISC;
    949 		}
    950 	}
    951 	vlan_putref_linkmib(mib, &psref);
    952 	curlwp_bindx(bound);
    953 
    954 	return error;
    955 }
    956 
    957 static int
    958 vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    959 {
    960 	struct lwp *l = curlwp;
    961 	struct ifvlan *ifv = ifp->if_softc;
    962 	struct ifaddr *ifa = (struct ifaddr *) data;
    963 	struct ifreq *ifr = (struct ifreq *) data;
    964 	struct ifnet *pr;
    965 	struct ifcapreq *ifcr;
    966 	struct vlanreq vlr;
    967 	struct ifvlan_linkmib *mib;
    968 	struct psref psref;
    969 	int error = 0;
    970 	int bound;
    971 
    972 	switch (cmd) {
    973 	case SIOCSIFMTU:
    974 		bound = curlwp_bind();
    975 		mib = vlan_getref_linkmib(ifv, &psref);
    976 		if (mib == NULL) {
    977 			curlwp_bindx(bound);
    978 			error = EBUSY;
    979 			break;
    980 		}
    981 
    982 		if (mib->ifvm_p == NULL) {
    983 			vlan_putref_linkmib(mib, &psref);
    984 			curlwp_bindx(bound);
    985 			error = EINVAL;
    986 		} else if (
    987 		    ifr->ifr_mtu > (mib->ifvm_p->if_mtu - mib->ifvm_mtufudge) ||
    988 		    ifr->ifr_mtu < (mib->ifvm_mintu - mib->ifvm_mtufudge)) {
    989 			vlan_putref_linkmib(mib, &psref);
    990 			curlwp_bindx(bound);
    991 			error = EINVAL;
    992 		} else {
    993 			vlan_putref_linkmib(mib, &psref);
    994 			curlwp_bindx(bound);
    995 
    996 			error = ifioctl_common(ifp, cmd, data);
    997 			if (error == ENETRESET)
    998 				error = 0;
    999 		}
   1000 
   1001 		break;
   1002 
   1003 	case SIOCSETVLAN:
   1004 		if ((error = kauth_authorize_network(l->l_cred,
   1005 		    KAUTH_NETWORK_INTERFACE,
   1006 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   1007 		    NULL)) != 0)
   1008 			break;
   1009 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
   1010 			break;
   1011 
   1012 		if (vlr.vlr_parent[0] == '\0') {
   1013 			bound = curlwp_bind();
   1014 			mib = vlan_getref_linkmib(ifv, &psref);
   1015 			if (mib == NULL) {
   1016 				curlwp_bindx(bound);
   1017 				error = EBUSY;
   1018 				break;
   1019 			}
   1020 
   1021 			if (mib->ifvm_p != NULL &&
   1022 			    (ifp->if_flags & IFF_PROMISC) != 0)
   1023 				error = vlan_safe_ifpromisc(mib->ifvm_p, 0);
   1024 
   1025 			vlan_putref_linkmib(mib, &psref);
   1026 			curlwp_bindx(bound);
   1027 
   1028 			vlan_unconfig(ifp);
   1029 			break;
   1030 		}
   1031 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
   1032 			error = EINVAL;		 /* check for valid tag */
   1033 			break;
   1034 		}
   1035 		if ((pr = ifunit(vlr.vlr_parent)) == NULL) {
   1036 			error = ENOENT;
   1037 			break;
   1038 		}
   1039 
   1040 		error = vlan_config(ifv, pr, vlr.vlr_tag);
   1041 		if (error != 0)
   1042 			break;
   1043 
   1044 		/* Update promiscuous mode, if necessary. */
   1045 		vlan_set_promisc(ifp);
   1046 
   1047 		ifp->if_flags |= IFF_RUNNING;
   1048 		break;
   1049 
   1050 	case SIOCGETVLAN:
   1051 		memset(&vlr, 0, sizeof(vlr));
   1052 		bound = curlwp_bind();
   1053 		mib = vlan_getref_linkmib(ifv, &psref);
   1054 		if (mib == NULL) {
   1055 			curlwp_bindx(bound);
   1056 			error = EBUSY;
   1057 			break;
   1058 		}
   1059 		if (mib->ifvm_p != NULL) {
   1060 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
   1061 			    mib->ifvm_p->if_xname);
   1062 			vlr.vlr_tag = mib->ifvm_tag;
   1063 		}
   1064 		vlan_putref_linkmib(mib, &psref);
   1065 		curlwp_bindx(bound);
   1066 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
   1067 		break;
   1068 
   1069 	case SIOCSIFFLAGS:
   1070 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1071 			break;
   1072 		/*
   1073 		 * For promiscuous mode, we enable promiscuous mode on
   1074 		 * the parent if we need promiscuous on the VLAN interface.
   1075 		 */
   1076 		bound = curlwp_bind();
   1077 		mib = vlan_getref_linkmib(ifv, &psref);
   1078 		if (mib == NULL) {
   1079 			curlwp_bindx(bound);
   1080 			error = EBUSY;
   1081 			break;
   1082 		}
   1083 
   1084 		if (mib->ifvm_p != NULL)
   1085 			error = vlan_set_promisc(ifp);
   1086 		vlan_putref_linkmib(mib, &psref);
   1087 		curlwp_bindx(bound);
   1088 		break;
   1089 
   1090 	case SIOCADDMULTI:
   1091 		mutex_enter(&ifv->ifv_lock);
   1092 		mib = ifv->ifv_mib;
   1093 		if (mib == NULL) {
   1094 			error = EBUSY;
   1095 			mutex_exit(&ifv->ifv_lock);
   1096 			break;
   1097 		}
   1098 
   1099 		error = (mib->ifvm_p != NULL) ?
   1100 		    (*mib->ifvm_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
   1101 		mib = NULL;
   1102 		mutex_exit(&ifv->ifv_lock);
   1103 		break;
   1104 
   1105 	case SIOCDELMULTI:
   1106 		mutex_enter(&ifv->ifv_lock);
   1107 		mib = ifv->ifv_mib;
   1108 		if (mib == NULL) {
   1109 			error = EBUSY;
   1110 			mutex_exit(&ifv->ifv_lock);
   1111 			break;
   1112 		}
   1113 		error = (mib->ifvm_p != NULL) ?
   1114 		    (*mib->ifvm_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
   1115 		mib = NULL;
   1116 		mutex_exit(&ifv->ifv_lock);
   1117 		break;
   1118 
   1119 	case SIOCSIFCAP:
   1120 		ifcr = data;
   1121 		/* make sure caps are enabled on parent */
   1122 		bound = curlwp_bind();
   1123 		mib = vlan_getref_linkmib(ifv, &psref);
   1124 		if (mib == NULL) {
   1125 			curlwp_bindx(bound);
   1126 			error = EBUSY;
   1127 			break;
   1128 		}
   1129 
   1130 		if (mib->ifvm_p == NULL) {
   1131 			vlan_putref_linkmib(mib, &psref);
   1132 			curlwp_bindx(bound);
   1133 			error = EINVAL;
   1134 			break;
   1135 		}
   1136 		if ((mib->ifvm_p->if_capenable & ifcr->ifcr_capenable) !=
   1137 		    ifcr->ifcr_capenable) {
   1138 			vlan_putref_linkmib(mib, &psref);
   1139 			curlwp_bindx(bound);
   1140 			error = EINVAL;
   1141 			break;
   1142 		}
   1143 
   1144 		vlan_putref_linkmib(mib, &psref);
   1145 		curlwp_bindx(bound);
   1146 
   1147 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
   1148 			error = 0;
   1149 		break;
   1150 	case SIOCINITIFADDR:
   1151 		bound = curlwp_bind();
   1152 		mib = vlan_getref_linkmib(ifv, &psref);
   1153 		if (mib == NULL) {
   1154 			curlwp_bindx(bound);
   1155 			error = EBUSY;
   1156 			break;
   1157 		}
   1158 
   1159 		if (mib->ifvm_p == NULL) {
   1160 			error = EINVAL;
   1161 			vlan_putref_linkmib(mib, &psref);
   1162 			curlwp_bindx(bound);
   1163 			break;
   1164 		}
   1165 		vlan_putref_linkmib(mib, &psref);
   1166 		curlwp_bindx(bound);
   1167 
   1168 		ifp->if_flags |= IFF_UP;
   1169 #ifdef INET
   1170 		if (ifa->ifa_addr->sa_family == AF_INET)
   1171 			arp_ifinit(ifp, ifa);
   1172 #endif
   1173 		break;
   1174 
   1175 	default:
   1176 		error = ether_ioctl(ifp, cmd, data);
   1177 	}
   1178 
   1179 	return error;
   1180 }
   1181 
   1182 static int
   1183 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
   1184 {
   1185 	const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
   1186 	struct vlan_mc_entry *mc;
   1187 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
   1188 	struct ifvlan_linkmib *mib;
   1189 	int error;
   1190 
   1191 	KASSERT(mutex_owned(&ifv->ifv_lock));
   1192 
   1193 	if (sa->sa_len > sizeof(struct sockaddr_storage))
   1194 		return EINVAL;
   1195 
   1196 	error = ether_addmulti(sa, &ifv->ifv_ec);
   1197 	if (error != ENETRESET)
   1198 		return error;
   1199 
   1200 	/*
   1201 	 * This is a new multicast address.  We have to tell parent
   1202 	 * about it.  Also, remember this multicast address so that
   1203 	 * we can delete it on unconfigure.
   1204 	 */
   1205 	mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
   1206 	if (mc == NULL) {
   1207 		error = ENOMEM;
   1208 		goto alloc_failed;
   1209 	}
   1210 
   1211 	/*
   1212 	 * Since ether_addmulti() returned ENETRESET, the following two
   1213 	 * statements shouldn't fail. Here ifv_ec is implicitly protected
   1214 	 * by the ifv_lock lock.
   1215 	 */
   1216 	error = ether_multiaddr(sa, addrlo, addrhi);
   1217 	KASSERT(error == 0);
   1218 
   1219 	ETHER_LOCK(&ifv->ifv_ec);
   1220 	mc->mc_enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec);
   1221 	ETHER_UNLOCK(&ifv->ifv_ec);
   1222 
   1223 	KASSERT(mc->mc_enm != NULL);
   1224 
   1225 	memcpy(&mc->mc_addr, sa, sa->sa_len);
   1226 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
   1227 
   1228 	mib = ifv->ifv_mib;
   1229 
   1230 	KERNEL_LOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p);
   1231 	error = if_mcast_op(mib->ifvm_p, SIOCADDMULTI, sa);
   1232 	KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(mib->ifvm_p);
   1233 
   1234 	if (error != 0)
   1235 		goto ioctl_failed;
   1236 	return error;
   1237 
   1238 ioctl_failed:
   1239 	LIST_REMOVE(mc, mc_entries);
   1240 	free(mc, M_DEVBUF);
   1241 
   1242 alloc_failed:
   1243 	(void)ether_delmulti(sa, &ifv->ifv_ec);
   1244 	return error;
   1245 }
   1246 
   1247 static int
   1248 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
   1249 {
   1250 	const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
   1251 	struct ether_multi *enm;
   1252 	struct vlan_mc_entry *mc;
   1253 	struct ifvlan_linkmib *mib;
   1254 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
   1255 	int error;
   1256 
   1257 	KASSERT(mutex_owned(&ifv->ifv_lock));
   1258 
   1259 	/*
   1260 	 * Find a key to lookup vlan_mc_entry.  We have to do this
   1261 	 * before calling ether_delmulti for obvious reasons.
   1262 	 */
   1263 	if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
   1264 		return error;
   1265 
   1266 	ETHER_LOCK(&ifv->ifv_ec);
   1267 	enm = ether_lookup_multi(addrlo, addrhi, &ifv->ifv_ec);
   1268 	ETHER_UNLOCK(&ifv->ifv_ec);
   1269 	if (enm == NULL)
   1270 		return EINVAL;
   1271 
   1272 	LIST_FOREACH(mc, &ifv->ifv_mc_listhead, mc_entries) {
   1273 		if (mc->mc_enm == enm)
   1274 			break;
   1275 	}
   1276 
   1277 	/* We woun't delete entries we didn't add */
   1278 	if (mc == NULL)
   1279 		return EINVAL;
   1280 
   1281 	error = ether_delmulti(sa, &ifv->ifv_ec);
   1282 	if (error != ENETRESET)
   1283 		return error;
   1284 
   1285 	/* We no longer use this multicast address.  Tell parent so. */
   1286 	mib = ifv->ifv_mib;
   1287 	error = if_mcast_op(mib->ifvm_p, SIOCDELMULTI, sa);
   1288 
   1289 	if (error == 0) {
   1290 		/* And forget about this address. */
   1291 		LIST_REMOVE(mc, mc_entries);
   1292 		free(mc, M_DEVBUF);
   1293 	} else {
   1294 		(void)ether_addmulti(sa, &ifv->ifv_ec);
   1295 	}
   1296 
   1297 	return error;
   1298 }
   1299 
   1300 /*
   1301  * Delete any multicast address we have asked to add from parent
   1302  * interface.  Called when the vlan is being unconfigured.
   1303  */
   1304 static void
   1305 vlan_ether_purgemulti(struct ifvlan *ifv)
   1306 {
   1307 	struct vlan_mc_entry *mc;
   1308 	struct ifvlan_linkmib *mib;
   1309 
   1310 	KASSERT(mutex_owned(&ifv->ifv_lock));
   1311 	mib = ifv->ifv_mib;
   1312 	if (mib == NULL) {
   1313 		return;
   1314 	}
   1315 
   1316 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
   1317 		(void)if_mcast_op(mib->ifvm_p, SIOCDELMULTI,
   1318 		    sstocsa(&mc->mc_addr));
   1319 		LIST_REMOVE(mc, mc_entries);
   1320 		free(mc, M_DEVBUF);
   1321 	}
   1322 }
   1323 
   1324 static void
   1325 vlan_start(struct ifnet *ifp)
   1326 {
   1327 	struct ifvlan *ifv = ifp->if_softc;
   1328 	struct ifnet *p;
   1329 	struct ethercom *ec;
   1330 	struct mbuf *m;
   1331 	struct ifvlan_linkmib *mib;
   1332 	struct psref psref;
   1333 	int error;
   1334 
   1335 	mib = vlan_getref_linkmib(ifv, &psref);
   1336 	if (mib == NULL)
   1337 		return;
   1338 	p = mib->ifvm_p;
   1339 	ec = (void *)mib->ifvm_p;
   1340 
   1341 	ifp->if_flags |= IFF_OACTIVE;
   1342 
   1343 	for (;;) {
   1344 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1345 		if (m == NULL)
   1346 			break;
   1347 
   1348 #ifdef ALTQ
   1349 		/*
   1350 		 * KERNEL_LOCK is required for ALTQ even if NET_MPSAFE is
   1351 		 * defined.
   1352 		 */
   1353 		KERNEL_LOCK(1, NULL);
   1354 		/*
   1355 		 * If ALTQ is enabled on the parent interface, do
   1356 		 * classification; the queueing discipline might
   1357 		 * not require classification, but might require
   1358 		 * the address family/header pointer in the pktattr.
   1359 		 */
   1360 		if (ALTQ_IS_ENABLED(&p->if_snd)) {
   1361 			switch (p->if_type) {
   1362 			case IFT_ETHER:
   1363 				altq_etherclassify(&p->if_snd, m);
   1364 				break;
   1365 			default:
   1366 				panic("%s: impossible (altq)", __func__);
   1367 			}
   1368 		}
   1369 		KERNEL_UNLOCK_ONE(NULL);
   1370 #endif /* ALTQ */
   1371 
   1372 		bpf_mtap(ifp, m, BPF_D_OUT);
   1373 		/*
   1374 		 * If the parent can insert the tag itself, just mark
   1375 		 * the tag in the mbuf header.
   1376 		 */
   1377 		if (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) {
   1378 			vlan_set_tag(m, mib->ifvm_tag);
   1379 		} else {
   1380 			/*
   1381 			 * insert the tag ourselves
   1382 			 */
   1383 			M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
   1384 			if (m == NULL) {
   1385 				printf("%s: unable to prepend encap header",
   1386 				    p->if_xname);
   1387 				ifp->if_oerrors++;
   1388 				continue;
   1389 			}
   1390 
   1391 			switch (p->if_type) {
   1392 			case IFT_ETHER:
   1393 			    {
   1394 				struct ether_vlan_header *evl;
   1395 
   1396 				if (m->m_len < sizeof(struct ether_vlan_header))
   1397 					m = m_pullup(m,
   1398 					    sizeof(struct ether_vlan_header));
   1399 				if (m == NULL) {
   1400 					printf("%s: unable to pullup encap "
   1401 					    "header", p->if_xname);
   1402 					ifp->if_oerrors++;
   1403 					continue;
   1404 				}
   1405 
   1406 				/*
   1407 				 * Transform the Ethernet header into an
   1408 				 * Ethernet header with 802.1Q encapsulation.
   1409 				 */
   1410 				memmove(mtod(m, void *),
   1411 				    mtod(m, char *) + mib->ifvm_encaplen,
   1412 				    sizeof(struct ether_header));
   1413 				evl = mtod(m, struct ether_vlan_header *);
   1414 				evl->evl_proto = evl->evl_encap_proto;
   1415 				evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
   1416 				evl->evl_tag = htons(mib->ifvm_tag);
   1417 
   1418 				/*
   1419 				 * To cater for VLAN-aware layer 2 ethernet
   1420 				 * switches which may need to strip the tag
   1421 				 * before forwarding the packet, make sure
   1422 				 * the packet+tag is at least 68 bytes long.
   1423 				 * This is necessary because our parent will
   1424 				 * only pad to 64 bytes (ETHER_MIN_LEN) and
   1425 				 * some switches will not pad by themselves
   1426 				 * after deleting a tag.
   1427 				 */
   1428 				const size_t min_data_len = ETHER_MIN_LEN -
   1429 				    ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
   1430 				if (m->m_pkthdr.len < min_data_len) {
   1431 					m_copyback(m, m->m_pkthdr.len,
   1432 					    min_data_len - m->m_pkthdr.len,
   1433 					    vlan_zero_pad_buff);
   1434 				}
   1435 				break;
   1436 			    }
   1437 
   1438 			default:
   1439 				panic("%s: impossible", __func__);
   1440 			}
   1441 		}
   1442 
   1443 		if ((p->if_flags & IFF_RUNNING) == 0) {
   1444 			m_freem(m);
   1445 			continue;
   1446 		}
   1447 
   1448 		error = if_transmit_lock(p, m);
   1449 		if (error) {
   1450 			/* mbuf is already freed */
   1451 			ifp->if_oerrors++;
   1452 			continue;
   1453 		}
   1454 		ifp->if_opackets++;
   1455 	}
   1456 
   1457 	ifp->if_flags &= ~IFF_OACTIVE;
   1458 
   1459 	/* Remove reference to mib before release */
   1460 	vlan_putref_linkmib(mib, &psref);
   1461 }
   1462 
   1463 static int
   1464 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
   1465 {
   1466 	struct ifvlan *ifv = ifp->if_softc;
   1467 	struct ifnet *p;
   1468 	struct ethercom *ec;
   1469 	struct ifvlan_linkmib *mib;
   1470 	struct psref psref;
   1471 	int error;
   1472 	size_t pktlen = m->m_pkthdr.len;
   1473 	bool mcast = (m->m_flags & M_MCAST) != 0;
   1474 
   1475 	mib = vlan_getref_linkmib(ifv, &psref);
   1476 	if (mib == NULL) {
   1477 		m_freem(m);
   1478 		return ENETDOWN;
   1479 	}
   1480 
   1481 	p = mib->ifvm_p;
   1482 	ec = (void *)mib->ifvm_p;
   1483 
   1484 	bpf_mtap(ifp, m, BPF_D_OUT);
   1485 
   1486 	if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
   1487 		goto out;
   1488 	if (m == NULL)
   1489 		goto out;
   1490 
   1491 	/*
   1492 	 * If the parent can insert the tag itself, just mark
   1493 	 * the tag in the mbuf header.
   1494 	 */
   1495 	if (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) {
   1496 		vlan_set_tag(m, mib->ifvm_tag);
   1497 	} else {
   1498 		/*
   1499 		 * insert the tag ourselves
   1500 		 */
   1501 		M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
   1502 		if (m == NULL) {
   1503 			printf("%s: unable to prepend encap header",
   1504 			    p->if_xname);
   1505 			ifp->if_oerrors++;
   1506 			error = ENOBUFS;
   1507 			goto out;
   1508 		}
   1509 
   1510 		switch (p->if_type) {
   1511 		case IFT_ETHER:
   1512 		    {
   1513 			struct ether_vlan_header *evl;
   1514 
   1515 			if (m->m_len < sizeof(struct ether_vlan_header))
   1516 				m = m_pullup(m,
   1517 				    sizeof(struct ether_vlan_header));
   1518 			if (m == NULL) {
   1519 				printf("%s: unable to pullup encap "
   1520 				    "header", p->if_xname);
   1521 				ifp->if_oerrors++;
   1522 				error = ENOBUFS;
   1523 				goto out;
   1524 			}
   1525 
   1526 			/*
   1527 			 * Transform the Ethernet header into an
   1528 			 * Ethernet header with 802.1Q encapsulation.
   1529 			 */
   1530 			memmove(mtod(m, void *),
   1531 			    mtod(m, char *) + mib->ifvm_encaplen,
   1532 			    sizeof(struct ether_header));
   1533 			evl = mtod(m, struct ether_vlan_header *);
   1534 			evl->evl_proto = evl->evl_encap_proto;
   1535 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
   1536 			evl->evl_tag = htons(mib->ifvm_tag);
   1537 
   1538 			/*
   1539 			 * To cater for VLAN-aware layer 2 ethernet
   1540 			 * switches which may need to strip the tag
   1541 			 * before forwarding the packet, make sure
   1542 			 * the packet+tag is at least 68 bytes long.
   1543 			 * This is necessary because our parent will
   1544 			 * only pad to 64 bytes (ETHER_MIN_LEN) and
   1545 			 * some switches will not pad by themselves
   1546 			 * after deleting a tag.
   1547 			 */
   1548 			const size_t min_data_len = ETHER_MIN_LEN -
   1549 			    ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
   1550 			if (m->m_pkthdr.len < min_data_len) {
   1551 				m_copyback(m, m->m_pkthdr.len,
   1552 				    min_data_len - m->m_pkthdr.len,
   1553 				    vlan_zero_pad_buff);
   1554 			}
   1555 			break;
   1556 		    }
   1557 
   1558 		default:
   1559 			panic("%s: impossible", __func__);
   1560 		}
   1561 	}
   1562 
   1563 	if ((p->if_flags & IFF_RUNNING) == 0) {
   1564 		m_freem(m);
   1565 		error = ENETDOWN;
   1566 		goto out;
   1567 	}
   1568 
   1569 	error = if_transmit_lock(p, m);
   1570 	if (error) {
   1571 		/* mbuf is already freed */
   1572 		ifp->if_oerrors++;
   1573 	} else {
   1574 
   1575 		ifp->if_opackets++;
   1576 		ifp->if_obytes += pktlen;
   1577 		if (mcast)
   1578 			ifp->if_omcasts++;
   1579 	}
   1580 
   1581 out:
   1582 	/* Remove reference to mib before release */
   1583 	vlan_putref_linkmib(mib, &psref);
   1584 	return error;
   1585 }
   1586 
   1587 /*
   1588  * Given an Ethernet frame, find a valid vlan interface corresponding to the
   1589  * given source interface and tag, then run the real packet through the
   1590  * parent's input routine.
   1591  */
   1592 void
   1593 vlan_input(struct ifnet *ifp, struct mbuf *m)
   1594 {
   1595 	struct ifvlan *ifv;
   1596 	uint16_t vid;
   1597 	struct ifvlan_linkmib *mib;
   1598 	struct psref psref;
   1599 	bool have_vtag;
   1600 
   1601 	have_vtag = vlan_has_tag(m);
   1602 	if (have_vtag) {
   1603 		vid = EVL_VLANOFTAG(vlan_get_tag(m));
   1604 		m->m_flags &= ~M_VLANTAG;
   1605 	} else {
   1606 		struct ether_vlan_header *evl;
   1607 
   1608 		if (ifp->if_type != IFT_ETHER) {
   1609 			panic("%s: impossible", __func__);
   1610 		}
   1611 
   1612 		if (m->m_len < sizeof(struct ether_vlan_header) &&
   1613 		    (m = m_pullup(m,
   1614 		     sizeof(struct ether_vlan_header))) == NULL) {
   1615 			printf("%s: no memory for VLAN header, "
   1616 			    "dropping packet.\n", ifp->if_xname);
   1617 			return;
   1618 		}
   1619 		evl = mtod(m, struct ether_vlan_header *);
   1620 		KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
   1621 
   1622 		vid = EVL_VLANOFTAG(ntohs(evl->evl_tag));
   1623 
   1624 		/*
   1625 		 * Restore the original ethertype.  We'll remove
   1626 		 * the encapsulation after we've found the vlan
   1627 		 * interface corresponding to the tag.
   1628 		 */
   1629 		evl->evl_encap_proto = evl->evl_proto;
   1630 	}
   1631 
   1632 	mib = vlan_lookup_tag_psref(ifp, vid, &psref);
   1633 	if (mib == NULL) {
   1634 		m_freem(m);
   1635 		ifp->if_noproto++;
   1636 		return;
   1637 	}
   1638 	KASSERT(mib->ifvm_encaplen == ETHER_VLAN_ENCAP_LEN);
   1639 
   1640 	ifv = mib->ifvm_ifvlan;
   1641 	if ((ifv->ifv_if.if_flags & (IFF_UP | IFF_RUNNING)) !=
   1642 	    (IFF_UP | IFF_RUNNING)) {
   1643 		m_freem(m);
   1644 		ifp->if_noproto++;
   1645 		goto out;
   1646 	}
   1647 
   1648 	/*
   1649 	 * Now, remove the encapsulation header.  The original
   1650 	 * header has already been fixed up above.
   1651 	 */
   1652 	if (!have_vtag) {
   1653 		memmove(mtod(m, char *) + mib->ifvm_encaplen,
   1654 		    mtod(m, void *), sizeof(struct ether_header));
   1655 		m_adj(m, mib->ifvm_encaplen);
   1656 	}
   1657 
   1658 	m_set_rcvif(m, &ifv->ifv_if);
   1659 
   1660 	if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
   1661 		goto out;
   1662 	if (m == NULL)
   1663 		goto out;
   1664 
   1665 	m->m_flags &= ~M_PROMISC;
   1666 	if_input(&ifv->ifv_if, m);
   1667 out:
   1668 	vlan_putref_linkmib(mib, &psref);
   1669 }
   1670 
   1671 /*
   1672  * Module infrastructure
   1673  */
   1674 #include "if_module.h"
   1675 
   1676 IF_MODULE(MODULE_CLASS_DRIVER, vlan, NULL)
   1677