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