Home | History | Annotate | Line # | Download | only in net
if_vlan.c revision 1.108
      1 /*	$NetBSD: if_vlan.c,v 1.108 2017/11/22 02:35:24 msaitoh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran, and by Jason R. Thorpe of Zembu Labs, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright 1998 Massachusetts Institute of Technology
     34  *
     35  * Permission to use, copy, modify, and distribute this software and
     36  * its documentation for any purpose and without fee is hereby
     37  * granted, provided that both the above copyright notice and this
     38  * permission notice appear in all copies, that both the above
     39  * copyright notice and this permission notice appear in all
     40  * supporting documentation, and that the name of M.I.T. not be used
     41  * in advertising or publicity pertaining to distribution of the
     42  * software without specific, written prior permission.  M.I.T. makes
     43  * no representations about the suitability of this software for any
     44  * purpose.  It is provided "as is" without express or implied
     45  * warranty.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
     48  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
     49  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     50  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
     51  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     52  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     53  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     54  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     55  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     56  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     57  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
     61  * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
     62  */
     63 
     64 /*
     65  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.  Might be
     66  * extended some day to also handle IEEE 802.1P priority tagging.  This is
     67  * sort of sneaky in the implementation, since we need to pretend to be
     68  * enough of an Ethernet implementation to make ARP work.  The way we do
     69  * this is by telling everyone that we are an Ethernet interface, and then
     70  * catch the packets that ether_output() left on our output queue when it
     71  * calls if_start(), rewrite them for use by the real outgoing interface,
     72  * and ask it to send them.
     73  *
     74  * TODO:
     75  *
     76  *	- Need some way to notify vlan interfaces when the parent
     77  *	  interface changes MTU.
     78  */
     79 
     80 #include <sys/cdefs.h>
     81 __KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.108 2017/11/22 02:35:24 msaitoh 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 struct ifvlan_linkmib*	vlan_getref_linkmib(struct ifvlan *,
    211     struct psref *);
    212 static void	vlan_putref_linkmib(struct ifvlan_linkmib *,
    213     struct psref *);
    214 static void	vlan_linkmib_update(struct ifvlan *,
    215     struct ifvlan_linkmib *);
    216 static struct ifvlan_linkmib*	vlan_lookup_tag_psref(struct ifnet *,
    217     uint16_t, struct psref *);
    218 static int	tag_hash_func(uint16_t, u_long);
    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 void
    249 vlanattach(int n)
    250 {
    251 
    252 	/*
    253 	 * Nothing to do here, initialization is handled by the
    254 	 * module initialization code in vlaninit() below).
    255 	 */
    256 }
    257 
    258 static void
    259 vlaninit(void)
    260 {
    261 	mutex_init(&ifv_list.lock, MUTEX_DEFAULT, IPL_NONE);
    262 	LIST_INIT(&ifv_list.list);
    263 
    264 	mutex_init(&ifv_hash.lock, MUTEX_DEFAULT, IPL_NONE);
    265 	vlan_psz = pserialize_create();
    266 	ifvm_psref_class = psref_class_create("vlanlinkmib", IPL_SOFTNET);
    267 	if_clone_attach(&vlan_cloner);
    268 
    269 	vlan_hash_init();
    270 }
    271 
    272 static int
    273 vlandetach(void)
    274 {
    275 	int error = 0;
    276 
    277 	mutex_enter(&ifv_list.lock);
    278 	if (!LIST_EMPTY(&ifv_list.list)) {
    279 		mutex_exit(&ifv_list.lock);
    280 		return EBUSY;
    281 	}
    282 	mutex_exit(&ifv_list.lock);
    283 
    284 	error = vlan_hash_fini();
    285 	if (error != 0)
    286 		return error;
    287 
    288 	if_clone_detach(&vlan_cloner);
    289 	psref_class_destroy(ifvm_psref_class);
    290 	pserialize_destroy(vlan_psz);
    291 	mutex_destroy(&ifv_hash.lock);
    292 	mutex_destroy(&ifv_list.lock);
    293 
    294 	return 0;
    295 }
    296 
    297 static void
    298 vlan_reset_linkname(struct ifnet *ifp)
    299 {
    300 
    301 	/*
    302 	 * We start out with a "802.1Q VLAN" type and zero-length
    303 	 * addresses.  When we attach to a parent interface, we
    304 	 * inherit its type, address length, address, and data link
    305 	 * type.
    306 	 */
    307 
    308 	ifp->if_type = IFT_L2VLAN;
    309 	ifp->if_addrlen = 0;
    310 	ifp->if_dlt = DLT_NULL;
    311 	if_alloc_sadl(ifp);
    312 }
    313 
    314 static int
    315 vlan_clone_create(struct if_clone *ifc, int unit)
    316 {
    317 	struct ifvlan *ifv;
    318 	struct ifnet *ifp;
    319 	struct ifvlan_linkmib *mib;
    320 	int rv;
    321 
    322 	ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK|M_ZERO);
    323 	mib = kmem_zalloc(sizeof(struct ifvlan_linkmib), KM_SLEEP);
    324 	ifp = &ifv->ifv_if;
    325 	LIST_INIT(&ifv->ifv_mc_listhead);
    326 
    327 	mib->ifvm_ifvlan = ifv;
    328 	mib->ifvm_p = NULL;
    329 	psref_target_init(&mib->ifvm_psref, ifvm_psref_class);
    330 
    331 	mutex_init(&ifv->ifv_lock, MUTEX_DEFAULT, IPL_NONE);
    332 	ifv->ifv_mib = mib;
    333 
    334 	mutex_enter(&ifv_list.lock);
    335 	LIST_INSERT_HEAD(&ifv_list.list, ifv, ifv_list);
    336 	mutex_exit(&ifv_list.lock);
    337 
    338 	if_initname(ifp, ifc->ifc_name, unit);
    339 	ifp->if_softc = ifv;
    340 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    341 	ifp->if_extflags = IFEF_MPSAFE | IFEF_NO_LINK_STATE_CHANGE;
    342 	ifp->if_start = vlan_start;
    343 	ifp->if_transmit = vlan_transmit;
    344 	ifp->if_ioctl = vlan_ioctl;
    345 	IFQ_SET_READY(&ifp->if_snd);
    346 
    347 	rv = if_initialize(ifp);
    348 	if (rv != 0) {
    349 		aprint_error("%s: if_initialize failed(%d)\n", ifp->if_xname,
    350 		    rv);
    351 		goto fail;
    352 	}
    353 
    354 	vlan_reset_linkname(ifp);
    355 	if_register(ifp);
    356 	return 0;
    357 
    358 fail:
    359 	mutex_enter(&ifv_list.lock);
    360 	LIST_REMOVE(ifv, ifv_list);
    361 	mutex_exit(&ifv_list.lock);
    362 
    363 	mutex_destroy(&ifv->ifv_lock);
    364 	psref_target_destroy(&ifv->ifv_mib->ifvm_psref, ifvm_psref_class);
    365 	kmem_free(ifv->ifv_mib, sizeof(struct ifvlan_linkmib));
    366 	free(ifv, M_DEVBUF);
    367 
    368 	return rv;
    369 }
    370 
    371 static int
    372 vlan_clone_destroy(struct ifnet *ifp)
    373 {
    374 	struct ifvlan *ifv = ifp->if_softc;
    375 
    376 	mutex_enter(&ifv_list.lock);
    377 	LIST_REMOVE(ifv, ifv_list);
    378 	mutex_exit(&ifv_list.lock);
    379 
    380 	vlan_unconfig(ifp);
    381 	if_detach(ifp);
    382 
    383 	psref_target_destroy(&ifv->ifv_mib->ifvm_psref, ifvm_psref_class);
    384 	kmem_free(ifv->ifv_mib, sizeof(struct ifvlan_linkmib));
    385 	mutex_destroy(&ifv->ifv_lock);
    386 	free(ifv, M_DEVBUF);
    387 
    388 	return (0);
    389 }
    390 
    391 /*
    392  * Configure a VLAN interface.
    393  */
    394 static int
    395 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
    396 {
    397 	struct ifnet *ifp = &ifv->ifv_if;
    398 	struct ifvlan_linkmib *nmib = NULL;
    399 	struct ifvlan_linkmib *omib = NULL;
    400 	struct ifvlan_linkmib *checkmib = NULL;
    401 	struct psref_target *nmib_psref = NULL;
    402 	uint16_t vid = EVL_VLANOFTAG(tag);
    403 	int error = 0;
    404 	int idx;
    405 	bool omib_cleanup = false;
    406 	struct psref psref;
    407 
    408 	nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
    409 
    410 	mutex_enter(&ifv->ifv_lock);
    411 	omib = ifv->ifv_mib;
    412 
    413 	if (omib->ifvm_p != NULL) {
    414 		error = EBUSY;
    415 		goto done;
    416 	}
    417 
    418 	/* Duplicate check */
    419 	checkmib = vlan_lookup_tag_psref(p, vid, &psref);
    420 	if (checkmib != NULL) {
    421 		vlan_putref_linkmib(checkmib, &psref);
    422 		error = EEXIST;
    423 		goto done;
    424 	}
    425 
    426 	*nmib = *omib;
    427 	nmib_psref = &nmib->ifvm_psref;
    428 
    429 	psref_target_init(nmib_psref, ifvm_psref_class);
    430 
    431 	switch (p->if_type) {
    432 	case IFT_ETHER:
    433 	    {
    434 		struct ethercom *ec = (void *) p;
    435 		nmib->ifvm_msw = &vlan_ether_multisw;
    436 		nmib->ifvm_encaplen = ETHER_VLAN_ENCAP_LEN;
    437 		nmib->ifvm_mintu = ETHERMIN;
    438 
    439 		if (ec->ec_nvlans++ == 0) {
    440 			if ((error = ether_enable_vlan_mtu(p)) >= 0) {
    441 				if (error) {
    442 					ec->ec_nvlans--;
    443 					goto done;
    444 				}
    445 				nmib->ifvm_mtufudge = 0;
    446 			} else {
    447 				/*
    448 				 * Fudge the MTU by the encapsulation size. This
    449 				 * makes us incompatible with strictly compliant
    450 				 * 802.1Q implementations, but allows us to use
    451 				 * the feature with other NetBSD
    452 				 * implementations, which might still be useful.
    453 				 */
    454 				nmib->ifvm_mtufudge = nmib->ifvm_encaplen;
    455 			}
    456 			error = 0;
    457 		}
    458 
    459 		/*
    460 		 * If the parent interface can do hardware-assisted
    461 		 * VLAN encapsulation, then propagate its hardware-
    462 		 * assisted checksumming flags and tcp segmentation
    463 		 * offload.
    464 		 */
    465 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
    466 		        ec->ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
    467 			ifp->if_capabilities = p->if_capabilities &
    468 			    (IFCAP_TSOv4 | IFCAP_TSOv6 |
    469 			     IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx|
    470 			     IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx|
    471 			     IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx|
    472 			     IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx|
    473 			     IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx);
    474                 }
    475 		/*
    476 		 * We inherit the parent's Ethernet address.
    477 		 */
    478 		ether_ifattach(ifp, CLLADDR(p->if_sadl));
    479 		ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
    480 		break;
    481 	    }
    482 
    483 	default:
    484 		error = EPROTONOSUPPORT;
    485 		goto done;
    486 	}
    487 
    488 	nmib->ifvm_p = p;
    489 	nmib->ifvm_tag = vid;
    490 	ifv->ifv_if.if_mtu = p->if_mtu - nmib->ifvm_mtufudge;
    491 	ifv->ifv_if.if_flags = p->if_flags &
    492 	    (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
    493 
    494 	/*
    495 	 * Inherit the if_type from the parent.  This allows us
    496 	 * to participate in bridges of that type.
    497 	 */
    498 	ifv->ifv_if.if_type = p->if_type;
    499 
    500 	PSLIST_ENTRY_INIT(ifv, ifv_hash);
    501 	idx = tag_hash_func(vid, ifv_hash.mask);
    502 
    503 	mutex_enter(&ifv_hash.lock);
    504 	PSLIST_WRITER_INSERT_HEAD(&ifv_hash.lists[idx], ifv, ifv_hash);
    505 	mutex_exit(&ifv_hash.lock);
    506 
    507 	vlan_linkmib_update(ifv, nmib);
    508 	nmib = NULL;
    509 	nmib_psref = NULL;
    510 	omib_cleanup = true;
    511 
    512 done:
    513 	mutex_exit(&ifv->ifv_lock);
    514 
    515 	if (nmib_psref)
    516 		psref_target_destroy(nmib_psref, ifvm_psref_class);
    517 
    518 	if (nmib)
    519 		kmem_free(nmib, sizeof(*nmib));
    520 
    521 	if (omib_cleanup)
    522 		kmem_free(omib, sizeof(*omib));
    523 
    524 	return error;
    525 }
    526 
    527 /*
    528  * Unconfigure a VLAN interface.
    529  */
    530 static void
    531 vlan_unconfig(struct ifnet *ifp)
    532 {
    533 	struct ifvlan *ifv = ifp->if_softc;
    534 	struct ifvlan_linkmib *nmib = NULL;
    535 	int error;
    536 
    537 	nmib = kmem_alloc(sizeof(*nmib), KM_SLEEP);
    538 
    539 	mutex_enter(&ifv->ifv_lock);
    540 	error = vlan_unconfig_locked(ifv, nmib);
    541 	mutex_exit(&ifv->ifv_lock);
    542 
    543 	if (error)
    544 		kmem_free(nmib, sizeof(*nmib));
    545 }
    546 static int
    547 vlan_unconfig_locked(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
    548 {
    549 	struct ifnet *p;
    550 	struct ifnet *ifp = &ifv->ifv_if;
    551 	struct psref_target *nmib_psref = NULL;
    552 	struct ifvlan_linkmib *omib;
    553 	int error = 0;
    554 
    555 	KASSERT(mutex_owned(&ifv->ifv_lock));
    556 
    557 	omib = ifv->ifv_mib;
    558 	p = omib->ifvm_p;
    559 
    560 	if (p == NULL) {
    561 		error = -1;
    562 		goto done;
    563 	}
    564 
    565 	*nmib = *omib;
    566 	nmib_psref = &nmib->ifvm_psref;
    567 	psref_target_init(nmib_psref, ifvm_psref_class);
    568 
    569 	/*
    570  	 * Since the interface is being unconfigured, we need to empty the
    571 	 * list of multicast groups that we may have joined while we were
    572 	 * alive and remove them from the parent's list also.
    573 	 */
    574 	(*nmib->ifvm_msw->vmsw_purgemulti)(ifv);
    575 
    576 	/* Disconnect from parent. */
    577 	switch (p->if_type) {
    578 	case IFT_ETHER:
    579 	    {
    580 		struct ethercom *ec = (void *)p;
    581 		if (--ec->ec_nvlans == 0)
    582 			(void)ether_disable_vlan_mtu(p);
    583 
    584 		ether_ifdetach(ifp);
    585 		/* Restore vlan_ioctl overwritten by ether_ifdetach */
    586 		ifp->if_ioctl = vlan_ioctl;
    587 		vlan_reset_linkname(ifp);
    588 		break;
    589 	    }
    590 
    591 #ifdef DIAGNOSTIC
    592 	default:
    593 		panic("vlan_unconfig: impossible");
    594 #endif
    595 	}
    596 
    597 	nmib->ifvm_p = NULL;
    598 	ifv->ifv_if.if_mtu = 0;
    599 	ifv->ifv_flags = 0;
    600 
    601 	mutex_enter(&ifv_hash.lock);
    602 	PSLIST_WRITER_REMOVE(ifv, ifv_hash);
    603 	pserialize_perform(vlan_psz);
    604 	mutex_exit(&ifv_hash.lock);
    605 	PSLIST_ENTRY_DESTROY(ifv, ifv_hash);
    606 
    607 	vlan_linkmib_update(ifv, nmib);
    608 
    609 	mutex_exit(&ifv->ifv_lock);
    610 
    611 	nmib_psref = NULL;
    612 	kmem_free(omib, sizeof(*omib));
    613 
    614 #ifdef INET6
    615 	/* To delete v6 link local addresses */
    616 	if (in6_present)
    617 		in6_ifdetach(ifp);
    618 #endif
    619 
    620 	if ((ifp->if_flags & IFF_PROMISC) != 0)
    621 		ifpromisc(ifp, 0);
    622 	if_down(ifp);
    623 	ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
    624 	ifp->if_capabilities = 0;
    625 	mutex_enter(&ifv->ifv_lock);
    626 done:
    627 
    628 	if (nmib_psref)
    629 		psref_target_destroy(nmib_psref, ifvm_psref_class);
    630 
    631 	return error;
    632 }
    633 
    634 static void
    635 vlan_hash_init(void)
    636 {
    637 
    638 	ifv_hash.lists = hashinit(VLAN_TAG_HASH_SIZE, HASH_PSLIST, true,
    639 	    &ifv_hash.mask);
    640 }
    641 
    642 static int
    643 vlan_hash_fini(void)
    644 {
    645 	int i;
    646 
    647 	mutex_enter(&ifv_hash.lock);
    648 
    649 	for (i = 0; i < ifv_hash.mask + 1; i++) {
    650 		if (PSLIST_WRITER_FIRST(&ifv_hash.lists[i], struct ifvlan,
    651 		    ifv_hash) != NULL) {
    652 			mutex_exit(&ifv_hash.lock);
    653 			return EBUSY;
    654 		}
    655 	}
    656 
    657 	for (i = 0; i < ifv_hash.mask + 1; i++)
    658 		PSLIST_DESTROY(&ifv_hash.lists[i]);
    659 
    660 	mutex_exit(&ifv_hash.lock);
    661 
    662 	hashdone(ifv_hash.lists, HASH_PSLIST, ifv_hash.mask);
    663 
    664 	ifv_hash.lists = NULL;
    665 	ifv_hash.mask = 0;
    666 
    667 	return 0;
    668 }
    669 
    670 static int
    671 tag_hash_func(uint16_t tag, u_long mask)
    672 {
    673 	uint32_t hash;
    674 
    675 	hash = (tag >> 8) ^ tag;
    676 	hash = (hash >> 2) ^ hash;
    677 
    678 	return hash & mask;
    679 }
    680 
    681 static struct ifvlan_linkmib *
    682 vlan_getref_linkmib(struct ifvlan *sc, struct psref *psref)
    683 {
    684 	struct ifvlan_linkmib *mib;
    685 	int s;
    686 
    687 	s = pserialize_read_enter();
    688 	mib = sc->ifv_mib;
    689 	if (mib == NULL) {
    690 		pserialize_read_exit(s);
    691 		return NULL;
    692 	}
    693 	membar_datadep_consumer();
    694 	psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
    695 	pserialize_read_exit(s);
    696 
    697 	return mib;
    698 }
    699 
    700 static void
    701 vlan_putref_linkmib(struct ifvlan_linkmib *mib, struct psref *psref)
    702 {
    703 	if (mib == NULL)
    704 		return;
    705 	psref_release(psref, &mib->ifvm_psref, ifvm_psref_class);
    706 }
    707 
    708 static struct ifvlan_linkmib *
    709 vlan_lookup_tag_psref(struct ifnet *ifp, uint16_t tag, struct psref *psref)
    710 {
    711 	int idx;
    712 	int s;
    713 	struct ifvlan *sc;
    714 
    715 	idx = tag_hash_func(tag, ifv_hash.mask);
    716 
    717 	s = pserialize_read_enter();
    718 	PSLIST_READER_FOREACH(sc, &ifv_hash.lists[idx], struct ifvlan,
    719 	    ifv_hash) {
    720 		struct ifvlan_linkmib *mib = sc->ifv_mib;
    721 		if (mib == NULL)
    722 			continue;
    723 		if (mib->ifvm_tag != tag)
    724 			continue;
    725 		if (mib->ifvm_p != ifp)
    726 			continue;
    727 
    728 		psref_acquire(psref, &mib->ifvm_psref, ifvm_psref_class);
    729 		pserialize_read_exit(s);
    730 		return mib;
    731 	}
    732 	pserialize_read_exit(s);
    733 	return NULL;
    734 }
    735 
    736 static void
    737 vlan_linkmib_update(struct ifvlan *ifv, struct ifvlan_linkmib *nmib)
    738 {
    739 	struct ifvlan_linkmib *omib = ifv->ifv_mib;
    740 
    741 	KASSERT(mutex_owned(&ifv->ifv_lock));
    742 
    743 	membar_producer();
    744 	ifv->ifv_mib = nmib;
    745 
    746 	pserialize_perform(vlan_psz);
    747 	psref_target_destroy(&omib->ifvm_psref, ifvm_psref_class);
    748 }
    749 
    750 /*
    751  * Called when a parent interface is detaching; destroy any VLAN
    752  * configuration for the parent interface.
    753  */
    754 void
    755 vlan_ifdetach(struct ifnet *p)
    756 {
    757 	struct ifvlan *ifv;
    758 	struct ifvlan_linkmib *mib, **nmibs;
    759 	struct psref psref;
    760 	int error;
    761 	int bound;
    762 	int i, cnt = 0;
    763 
    764 	bound = curlwp_bind();
    765 	mutex_enter(&ifv_list.lock);
    766 	LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
    767 		mib = vlan_getref_linkmib(ifv, &psref);
    768 		if (mib == NULL)
    769 			continue;
    770 
    771 		if (mib->ifvm_p == p)
    772 			cnt++;
    773 
    774 		vlan_putref_linkmib(mib, &psref);
    775 	}
    776 	mutex_exit(&ifv_list.lock);
    777 
    778 	/*
    779 	 * The value of "cnt" does not increase while ifv_list.lock
    780 	 * and ifv->ifv_lock are released here, because the parent
    781 	 * interface is detaching.
    782 	 */
    783 	nmibs = kmem_alloc(sizeof(*nmibs) * cnt, KM_SLEEP);
    784 	for (i=0; i < cnt; i++) {
    785 		nmibs[i] = kmem_alloc(sizeof(*nmibs[i]), KM_SLEEP);
    786 	}
    787 
    788 	mutex_enter(&ifv_list.lock);
    789 
    790 	i = 0;
    791 	LIST_FOREACH(ifv, &ifv_list.list, ifv_list) {
    792 		mutex_enter(&ifv->ifv_lock);
    793 		if (ifv->ifv_mib->ifvm_p == p) {
    794 			KASSERTMSG(i < cnt, "no memory for unconfig, parent=%s",
    795 			    p->if_xname);
    796 			error = vlan_unconfig_locked(ifv, nmibs[i]);
    797 			if (!error) {
    798 				nmibs[i] = NULL;
    799 				i++;
    800 			}
    801 
    802 		}
    803 		mutex_exit(&ifv->ifv_lock);
    804 	}
    805 
    806 	mutex_exit(&ifv_list.lock);
    807 	curlwp_bindx(bound);
    808 
    809 	for (i=0; i < cnt; i++) {
    810 		if (nmibs[i])
    811 			kmem_free(nmibs[i], sizeof(*nmibs[i]));
    812 	}
    813 
    814 	kmem_free(nmibs, sizeof(*nmibs) * cnt);
    815 
    816 	return;
    817 }
    818 
    819 static int
    820 vlan_set_promisc(struct ifnet *ifp)
    821 {
    822 	struct ifvlan *ifv = ifp->if_softc;
    823 	struct ifvlan_linkmib *mib;
    824 	struct psref psref;
    825 	int error = 0;
    826 	int bound;
    827 
    828 	bound = curlwp_bind();
    829 	mib = vlan_getref_linkmib(ifv, &psref);
    830 	if (mib == NULL) {
    831 		curlwp_bindx(bound);
    832 		return EBUSY;
    833 	}
    834 
    835 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    836 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
    837 			error = ifpromisc(mib->ifvm_p, 1);
    838 			if (error == 0)
    839 				ifv->ifv_flags |= IFVF_PROMISC;
    840 		}
    841 	} else {
    842 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
    843 			error = ifpromisc(mib->ifvm_p, 0);
    844 			if (error == 0)
    845 				ifv->ifv_flags &= ~IFVF_PROMISC;
    846 		}
    847 	}
    848 	vlan_putref_linkmib(mib, &psref);
    849 	curlwp_bindx(bound);
    850 
    851 	return (error);
    852 }
    853 
    854 static int
    855 vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    856 {
    857 	struct lwp *l = curlwp;	/* XXX */
    858 	struct ifvlan *ifv = ifp->if_softc;
    859 	struct ifaddr *ifa = (struct ifaddr *) data;
    860 	struct ifreq *ifr = (struct ifreq *) data;
    861 	struct ifnet *pr;
    862 	struct ifcapreq *ifcr;
    863 	struct vlanreq vlr;
    864 	struct ifvlan_linkmib *mib;
    865 	struct psref psref;
    866 	int error = 0;
    867 	int bound;
    868 
    869 	switch (cmd) {
    870 	case SIOCSIFMTU:
    871 		bound = curlwp_bind();
    872 		mib = vlan_getref_linkmib(ifv, &psref);
    873 		if (mib == NULL) {
    874 			curlwp_bindx(bound);
    875 			error = EBUSY;
    876 			break;
    877 		}
    878 
    879 		if (mib->ifvm_p == NULL) {
    880 			vlan_putref_linkmib(mib, &psref);
    881 			curlwp_bindx(bound);
    882 			error = EINVAL;
    883 		} else if (
    884 		    ifr->ifr_mtu > (mib->ifvm_p->if_mtu - mib->ifvm_mtufudge) ||
    885 		    ifr->ifr_mtu < (mib->ifvm_mintu - mib->ifvm_mtufudge)) {
    886 			vlan_putref_linkmib(mib, &psref);
    887 			curlwp_bindx(bound);
    888 			error = EINVAL;
    889 		} else {
    890 			vlan_putref_linkmib(mib, &psref);
    891 			curlwp_bindx(bound);
    892 
    893 			error = ifioctl_common(ifp, cmd, data);
    894 			if (error == ENETRESET)
    895 					error = 0;
    896 		}
    897 
    898 		break;
    899 
    900 	case SIOCSETVLAN:
    901 		if ((error = kauth_authorize_network(l->l_cred,
    902 		    KAUTH_NETWORK_INTERFACE,
    903 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    904 		    NULL)) != 0)
    905 			break;
    906 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
    907 			break;
    908 
    909 		if (vlr.vlr_parent[0] == '\0') {
    910 			bound = curlwp_bind();
    911 			mib = vlan_getref_linkmib(ifv, &psref);
    912 			if (mib == NULL) {
    913 				curlwp_bindx(bound);
    914 				error = EBUSY;
    915 				break;
    916 			}
    917 
    918 			if (mib->ifvm_p != NULL &&
    919 			    (ifp->if_flags & IFF_PROMISC) != 0)
    920 				error = ifpromisc(mib->ifvm_p, 0);
    921 
    922 			vlan_putref_linkmib(mib, &psref);
    923 			curlwp_bindx(bound);
    924 
    925 			vlan_unconfig(ifp);
    926 			break;
    927 		}
    928 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
    929 			error = EINVAL;		 /* check for valid tag */
    930 			break;
    931 		}
    932 		if ((pr = ifunit(vlr.vlr_parent)) == 0) {
    933 			error = ENOENT;
    934 			break;
    935 		}
    936 		error = vlan_config(ifv, pr, vlr.vlr_tag);
    937 		if (error != 0) {
    938 			break;
    939 		}
    940 		ifp->if_flags |= IFF_RUNNING;
    941 
    942 		/* Update promiscuous mode, if necessary. */
    943 		vlan_set_promisc(ifp);
    944 		break;
    945 
    946 	case SIOCGETVLAN:
    947 		memset(&vlr, 0, sizeof(vlr));
    948 		bound = curlwp_bind();
    949 		mib = vlan_getref_linkmib(ifv, &psref);
    950 		if (mib == NULL) {
    951 			curlwp_bindx(bound);
    952 			error = EBUSY;
    953 			break;
    954 		}
    955 		if (mib->ifvm_p != NULL) {
    956 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
    957 			    mib->ifvm_p->if_xname);
    958 			vlr.vlr_tag = mib->ifvm_tag;
    959 		}
    960 		vlan_putref_linkmib(mib, &psref);
    961 		curlwp_bindx(bound);
    962 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
    963 		break;
    964 
    965 	case SIOCSIFFLAGS:
    966 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    967 			break;
    968 		/*
    969 		 * For promiscuous mode, we enable promiscuous mode on
    970 		 * the parent if we need promiscuous on the VLAN interface.
    971 		 */
    972 		bound = curlwp_bind();
    973 		mib = vlan_getref_linkmib(ifv, &psref);
    974 		if (mib == NULL) {
    975 			curlwp_bindx(bound);
    976 			error = EBUSY;
    977 			break;
    978 		}
    979 
    980 		if (mib->ifvm_p != NULL)
    981 			error = vlan_set_promisc(ifp);
    982 		vlan_putref_linkmib(mib, &psref);
    983 		curlwp_bindx(bound);
    984 		break;
    985 
    986 	case SIOCADDMULTI:
    987 		mutex_enter(&ifv->ifv_lock);
    988 		mib = ifv->ifv_mib;
    989 		if (mib == NULL) {
    990 			error = EBUSY;
    991 			mutex_exit(&ifv->ifv_lock);
    992 			break;
    993 		}
    994 
    995 		error = (mib->ifvm_p != NULL) ?
    996 		    (*mib->ifvm_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
    997 		mib = NULL;
    998 		mutex_exit(&ifv->ifv_lock);
    999 		break;
   1000 
   1001 	case SIOCDELMULTI:
   1002 		mutex_enter(&ifv->ifv_lock);
   1003 		mib = ifv->ifv_mib;
   1004 		if (mib == NULL) {
   1005 			error = EBUSY;
   1006 			mutex_exit(&ifv->ifv_lock);
   1007 			break;
   1008 		}
   1009 		error = (mib->ifvm_p != NULL) ?
   1010 		    (*mib->ifvm_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
   1011 		mib = NULL;
   1012 		mutex_exit(&ifv->ifv_lock);
   1013 		break;
   1014 
   1015 	case SIOCSIFCAP:
   1016 		ifcr = data;
   1017 		/* make sure caps are enabled on parent */
   1018 		bound = curlwp_bind();
   1019 		mib = vlan_getref_linkmib(ifv, &psref);
   1020 		if (mib == NULL) {
   1021 			curlwp_bindx(bound);
   1022 			error = EBUSY;
   1023 			break;
   1024 		}
   1025 
   1026 		if (mib->ifvm_p == NULL) {
   1027 			vlan_putref_linkmib(mib, &psref);
   1028 			curlwp_bindx(bound);
   1029 			error = EINVAL;
   1030 			break;
   1031 		}
   1032 		if ((mib->ifvm_p->if_capenable & ifcr->ifcr_capenable) !=
   1033 		    ifcr->ifcr_capenable) {
   1034 			vlan_putref_linkmib(mib, &psref);
   1035 			curlwp_bindx(bound);
   1036 			error = EINVAL;
   1037 			break;
   1038 		}
   1039 
   1040 		vlan_putref_linkmib(mib, &psref);
   1041 		curlwp_bindx(bound);
   1042 
   1043 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
   1044 			error = 0;
   1045 		break;
   1046 	case SIOCINITIFADDR:
   1047 		bound = curlwp_bind();
   1048 		mib = vlan_getref_linkmib(ifv, &psref);
   1049 		if (mib == NULL) {
   1050 			curlwp_bindx(bound);
   1051 			error = EBUSY;
   1052 			break;
   1053 		}
   1054 
   1055 		if (mib->ifvm_p == NULL) {
   1056 			error = EINVAL;
   1057 			vlan_putref_linkmib(mib, &psref);
   1058 			curlwp_bindx(bound);
   1059 			break;
   1060 		}
   1061 		vlan_putref_linkmib(mib, &psref);
   1062 		curlwp_bindx(bound);
   1063 
   1064 		ifp->if_flags |= IFF_UP;
   1065 #ifdef INET
   1066 		if (ifa->ifa_addr->sa_family == AF_INET)
   1067 			arp_ifinit(ifp, ifa);
   1068 #endif
   1069 		break;
   1070 
   1071 	default:
   1072 		error = ether_ioctl(ifp, cmd, data);
   1073 	}
   1074 
   1075 	return (error);
   1076 }
   1077 
   1078 static int
   1079 vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
   1080 {
   1081 	const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
   1082 	struct vlan_mc_entry *mc;
   1083 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
   1084 	struct ifvlan_linkmib *mib;
   1085 	int error;
   1086 
   1087 	KASSERT(mutex_owned(&ifv->ifv_lock));
   1088 
   1089 	if (sa->sa_len > sizeof(struct sockaddr_storage))
   1090 		return (EINVAL);
   1091 
   1092 	error = ether_addmulti(sa, &ifv->ifv_ec);
   1093 	if (error != ENETRESET)
   1094 		return (error);
   1095 
   1096 	/*
   1097 	 * This is new multicast address.  We have to tell parent
   1098 	 * about it.  Also, remember this multicast address so that
   1099 	 * we can delete them on unconfigure.
   1100 	 */
   1101 	mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
   1102 	if (mc == NULL) {
   1103 		error = ENOMEM;
   1104 		goto alloc_failed;
   1105 	}
   1106 
   1107 	/*
   1108 	 * As ether_addmulti() returns ENETRESET, following two
   1109 	 * statement shouldn't fail.
   1110 	 */
   1111 	(void)ether_multiaddr(sa, addrlo, addrhi);
   1112 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
   1113 	memcpy(&mc->mc_addr, sa, sa->sa_len);
   1114 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
   1115 
   1116 	mib = ifv->ifv_mib;
   1117 	error = if_mcast_op(mib->ifvm_p, SIOCADDMULTI, sa);
   1118 
   1119 	if (error != 0)
   1120 		goto ioctl_failed;
   1121 	return (error);
   1122 
   1123  ioctl_failed:
   1124 	LIST_REMOVE(mc, mc_entries);
   1125 	free(mc, M_DEVBUF);
   1126  alloc_failed:
   1127 	(void)ether_delmulti(sa, &ifv->ifv_ec);
   1128 	return (error);
   1129 }
   1130 
   1131 static int
   1132 vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
   1133 {
   1134 	const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
   1135 	struct ether_multi *enm;
   1136 	struct vlan_mc_entry *mc;
   1137 	struct ifvlan_linkmib *mib;
   1138 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
   1139 	int error;
   1140 
   1141 	KASSERT(mutex_owned(&ifv->ifv_lock));
   1142 
   1143 	/*
   1144 	 * Find a key to lookup vlan_mc_entry.  We have to do this
   1145 	 * before calling ether_delmulti for obvious reason.
   1146 	 */
   1147 	if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
   1148 		return (error);
   1149 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
   1150 
   1151 	error = ether_delmulti(sa, &ifv->ifv_ec);
   1152 	if (error != ENETRESET)
   1153 		return (error);
   1154 
   1155 	/* We no longer use this multicast address.  Tell parent so. */
   1156 	mib = ifv->ifv_mib;
   1157 	error = if_mcast_op(mib->ifvm_p, SIOCDELMULTI, sa);
   1158 
   1159 	if (error == 0) {
   1160 		/* And forget about this address. */
   1161 		for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
   1162 		    mc = LIST_NEXT(mc, mc_entries)) {
   1163 			if (mc->mc_enm == enm) {
   1164 				LIST_REMOVE(mc, mc_entries);
   1165 				free(mc, M_DEVBUF);
   1166 				break;
   1167 			}
   1168 		}
   1169 		KASSERT(mc != NULL);
   1170 	} else
   1171 		(void)ether_addmulti(sa, &ifv->ifv_ec);
   1172 	return (error);
   1173 }
   1174 
   1175 /*
   1176  * Delete any multicast address we have asked to add from parent
   1177  * interface.  Called when the vlan is being unconfigured.
   1178  */
   1179 static void
   1180 vlan_ether_purgemulti(struct ifvlan *ifv)
   1181 {
   1182 	struct vlan_mc_entry *mc;
   1183 	struct ifvlan_linkmib *mib;
   1184 
   1185 	KASSERT(mutex_owned(&ifv->ifv_lock));
   1186 	mib = ifv->ifv_mib;
   1187 	if (mib == NULL) {
   1188 		return;
   1189 	}
   1190 
   1191 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
   1192 		(void)if_mcast_op(mib->ifvm_p, SIOCDELMULTI,
   1193 		    (const struct sockaddr *)&mc->mc_addr);
   1194 		LIST_REMOVE(mc, mc_entries);
   1195 		free(mc, M_DEVBUF);
   1196 	}
   1197 }
   1198 
   1199 static void
   1200 vlan_start(struct ifnet *ifp)
   1201 {
   1202 	struct ifvlan *ifv = ifp->if_softc;
   1203 	struct ifnet *p;
   1204 	struct ethercom *ec;
   1205 	struct mbuf *m;
   1206 	struct ifvlan_linkmib *mib;
   1207 	struct psref psref;
   1208 	int error;
   1209 
   1210 	mib = vlan_getref_linkmib(ifv, &psref);
   1211 	if (mib == NULL)
   1212 		return;
   1213 	p = mib->ifvm_p;
   1214 	ec = (void *)mib->ifvm_p;
   1215 
   1216 	ifp->if_flags |= IFF_OACTIVE;
   1217 
   1218 	for (;;) {
   1219 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1220 		if (m == NULL)
   1221 			break;
   1222 
   1223 #ifdef ALTQ
   1224 		/*
   1225 		 * KERNEL_LOCK is required for ALTQ even if NET_MPSAFE is defined.
   1226 		 */
   1227 		KERNEL_LOCK(1, NULL);
   1228 		/*
   1229 		 * If ALTQ is enabled on the parent interface, do
   1230 		 * classification; the queueing discipline might
   1231 		 * not require classification, but might require
   1232 		 * the address family/header pointer in the pktattr.
   1233 		 */
   1234 		if (ALTQ_IS_ENABLED(&p->if_snd)) {
   1235 			switch (p->if_type) {
   1236 			case IFT_ETHER:
   1237 				altq_etherclassify(&p->if_snd, m);
   1238 				break;
   1239 #ifdef DIAGNOSTIC
   1240 			default:
   1241 				panic("vlan_start: impossible (altq)");
   1242 #endif
   1243 			}
   1244 		}
   1245 		KERNEL_UNLOCK_ONE(NULL);
   1246 #endif /* ALTQ */
   1247 
   1248 		bpf_mtap(ifp, m);
   1249 		/*
   1250 		 * If the parent can insert the tag itself, just mark
   1251 		 * the tag in the mbuf header.
   1252 		 */
   1253 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
   1254 			vlan_set_tag(m, mib->ifvm_tag);
   1255 		} else {
   1256 			/*
   1257 			 * insert the tag ourselves
   1258 			 */
   1259 			M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
   1260 			if (m == NULL) {
   1261 				printf("%s: unable to prepend encap header",
   1262 				    p->if_xname);
   1263 				ifp->if_oerrors++;
   1264 				continue;
   1265 			}
   1266 
   1267 			switch (p->if_type) {
   1268 			case IFT_ETHER:
   1269 			    {
   1270 				struct ether_vlan_header *evl;
   1271 
   1272 				if (m->m_len < sizeof(struct ether_vlan_header))
   1273 					m = m_pullup(m,
   1274 					    sizeof(struct ether_vlan_header));
   1275 				if (m == NULL) {
   1276 					printf("%s: unable to pullup encap "
   1277 					    "header", p->if_xname);
   1278 					ifp->if_oerrors++;
   1279 					continue;
   1280 				}
   1281 
   1282 				/*
   1283 				 * Transform the Ethernet header into an
   1284 				 * Ethernet header with 802.1Q encapsulation.
   1285 				 */
   1286 				memmove(mtod(m, void *),
   1287 				    mtod(m, char *) + mib->ifvm_encaplen,
   1288 				    sizeof(struct ether_header));
   1289 				evl = mtod(m, struct ether_vlan_header *);
   1290 				evl->evl_proto = evl->evl_encap_proto;
   1291 				evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
   1292 				evl->evl_tag = htons(mib->ifvm_tag);
   1293 
   1294 				/*
   1295 				 * To cater for VLAN-aware layer 2 ethernet
   1296 				 * switches which may need to strip the tag
   1297 				 * before forwarding the packet, make sure
   1298 				 * the packet+tag is at least 68 bytes long.
   1299 				 * This is necessary because our parent will
   1300 				 * only pad to 64 bytes (ETHER_MIN_LEN) and
   1301 				 * some switches will not pad by themselves
   1302 				 * after deleting a tag.
   1303 				 */
   1304 				if (m->m_pkthdr.len <
   1305 				    (ETHER_MIN_LEN - ETHER_CRC_LEN +
   1306 				     ETHER_VLAN_ENCAP_LEN)) {
   1307 					m_copyback(m, m->m_pkthdr.len,
   1308 					    (ETHER_MIN_LEN - ETHER_CRC_LEN +
   1309 					     ETHER_VLAN_ENCAP_LEN) -
   1310 					     m->m_pkthdr.len,
   1311 					    vlan_zero_pad_buff);
   1312 				}
   1313 				break;
   1314 			    }
   1315 
   1316 #ifdef DIAGNOSTIC
   1317 			default:
   1318 				panic("vlan_start: impossible");
   1319 #endif
   1320 			}
   1321 		}
   1322 
   1323 		if ((p->if_flags & IFF_RUNNING) == 0) {
   1324 			m_freem(m);
   1325 			continue;
   1326 		}
   1327 
   1328 		error = if_transmit_lock(p, m);
   1329 		if (error) {
   1330 			/* mbuf is already freed */
   1331 			ifp->if_oerrors++;
   1332 			continue;
   1333 		}
   1334 		ifp->if_opackets++;
   1335 	}
   1336 
   1337 	ifp->if_flags &= ~IFF_OACTIVE;
   1338 
   1339 	/* Remove reference to mib before release */
   1340 	p = NULL;
   1341 	ec = NULL;
   1342 
   1343 	vlan_putref_linkmib(mib, &psref);
   1344 }
   1345 
   1346 static int
   1347 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
   1348 {
   1349 	struct ifvlan *ifv = ifp->if_softc;
   1350 	struct ifnet *p;
   1351 	struct ethercom *ec;
   1352 	struct ifvlan_linkmib *mib;
   1353 	struct psref psref;
   1354 	int error;
   1355 	size_t pktlen = m->m_pkthdr.len;
   1356 	bool mcast = (m->m_flags & M_MCAST) != 0;
   1357 
   1358 	mib = vlan_getref_linkmib(ifv, &psref);
   1359 	if (mib == NULL) {
   1360 		m_freem(m);
   1361 		return ENETDOWN;
   1362 	}
   1363 
   1364 	p = mib->ifvm_p;
   1365 	ec = (void *)mib->ifvm_p;
   1366 
   1367 	bpf_mtap(ifp, m);
   1368 	/*
   1369 	 * If the parent can insert the tag itself, just mark
   1370 	 * the tag in the mbuf header.
   1371 	 */
   1372 	if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
   1373 		vlan_set_tag(m, mib->ifvm_tag);
   1374 	} else {
   1375 		/*
   1376 		 * insert the tag ourselves
   1377 		 */
   1378 		M_PREPEND(m, mib->ifvm_encaplen, M_DONTWAIT);
   1379 		if (m == NULL) {
   1380 			printf("%s: unable to prepend encap header",
   1381 			    p->if_xname);
   1382 			ifp->if_oerrors++;
   1383 			error = ENOBUFS;
   1384 			goto out;
   1385 		}
   1386 
   1387 		switch (p->if_type) {
   1388 		case IFT_ETHER:
   1389 		    {
   1390 			struct ether_vlan_header *evl;
   1391 
   1392 			if (m->m_len < sizeof(struct ether_vlan_header))
   1393 				m = m_pullup(m,
   1394 				    sizeof(struct ether_vlan_header));
   1395 			if (m == NULL) {
   1396 				printf("%s: unable to pullup encap "
   1397 				    "header", p->if_xname);
   1398 				ifp->if_oerrors++;
   1399 				error = ENOBUFS;
   1400 				goto out;
   1401 			}
   1402 
   1403 			/*
   1404 			 * Transform the Ethernet header into an
   1405 			 * Ethernet header with 802.1Q encapsulation.
   1406 			 */
   1407 			memmove(mtod(m, void *),
   1408 			    mtod(m, char *) + mib->ifvm_encaplen,
   1409 			    sizeof(struct ether_header));
   1410 			evl = mtod(m, struct ether_vlan_header *);
   1411 			evl->evl_proto = evl->evl_encap_proto;
   1412 			evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
   1413 			evl->evl_tag = htons(mib->ifvm_tag);
   1414 
   1415 			/*
   1416 			 * To cater for VLAN-aware layer 2 ethernet
   1417 			 * switches which may need to strip the tag
   1418 			 * before forwarding the packet, make sure
   1419 			 * the packet+tag is at least 68 bytes long.
   1420 			 * This is necessary because our parent will
   1421 			 * only pad to 64 bytes (ETHER_MIN_LEN) and
   1422 			 * some switches will not pad by themselves
   1423 			 * after deleting a tag.
   1424 			 */
   1425 			if (m->m_pkthdr.len <
   1426 			    (ETHER_MIN_LEN - ETHER_CRC_LEN +
   1427 			     ETHER_VLAN_ENCAP_LEN)) {
   1428 				m_copyback(m, m->m_pkthdr.len,
   1429 				    (ETHER_MIN_LEN - ETHER_CRC_LEN +
   1430 				     ETHER_VLAN_ENCAP_LEN) -
   1431 				     m->m_pkthdr.len,
   1432 				    vlan_zero_pad_buff);
   1433 			}
   1434 			break;
   1435 		    }
   1436 
   1437 #ifdef DIAGNOSTIC
   1438 		default:
   1439 			panic("vlan_transmit: impossible");
   1440 #endif
   1441 		}
   1442 	}
   1443 
   1444 	if ((p->if_flags & IFF_RUNNING) == 0) {
   1445 		m_freem(m);
   1446 		error = ENETDOWN;
   1447 		goto out;
   1448 	}
   1449 
   1450 	error = if_transmit_lock(p, m);
   1451 	if (error) {
   1452 		/* mbuf is already freed */
   1453 		ifp->if_oerrors++;
   1454 	} else {
   1455 
   1456 		ifp->if_opackets++;
   1457 		ifp->if_obytes += pktlen;
   1458 		if (mcast)
   1459 			ifp->if_omcasts++;
   1460 	}
   1461 
   1462 out:
   1463 	/* Remove reference to mib before release */
   1464 	p = NULL;
   1465 	ec = NULL;
   1466 
   1467 	vlan_putref_linkmib(mib, &psref);
   1468 	return error;
   1469 }
   1470 
   1471 /*
   1472  * Given an Ethernet frame, find a valid vlan interface corresponding to the
   1473  * given source interface and tag, then run the real packet through the
   1474  * parent's input routine.
   1475  */
   1476 void
   1477 vlan_input(struct ifnet *ifp, struct mbuf *m)
   1478 {
   1479 	struct ifvlan *ifv;
   1480 	uint16_t vid;
   1481 	struct ifvlan_linkmib *mib;
   1482 	struct psref psref;
   1483 	bool have_vtag;
   1484 
   1485 	have_vtag = vlan_has_tag(m);
   1486 	if (have_vtag) {
   1487 		vid = EVL_VLANOFTAG(vlan_get_tag(m));
   1488 		m->m_flags &= ~M_VLANTAG;
   1489 	} else {
   1490 		switch (ifp->if_type) {
   1491 		case IFT_ETHER:
   1492 		    {
   1493 			struct ether_vlan_header *evl;
   1494 
   1495 			if (m->m_len < sizeof(struct ether_vlan_header) &&
   1496 			    (m = m_pullup(m,
   1497 			     sizeof(struct ether_vlan_header))) == NULL) {
   1498 				printf("%s: no memory for VLAN header, "
   1499 				    "dropping packet.\n", ifp->if_xname);
   1500 				return;
   1501 			}
   1502 			evl = mtod(m, struct ether_vlan_header *);
   1503 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
   1504 
   1505 			vid = EVL_VLANOFTAG(ntohs(evl->evl_tag));
   1506 
   1507 			/*
   1508 			 * Restore the original ethertype.  We'll remove
   1509 			 * the encapsulation after we've found the vlan
   1510 			 * interface corresponding to the tag.
   1511 			 */
   1512 			evl->evl_encap_proto = evl->evl_proto;
   1513 			break;
   1514 		    }
   1515 
   1516 		default:
   1517 			vid = (uint16_t) -1;	/* XXX GCC */
   1518 #ifdef DIAGNOSTIC
   1519 			panic("vlan_input: impossible");
   1520 #endif
   1521 		}
   1522 	}
   1523 
   1524 	mib = vlan_lookup_tag_psref(ifp, vid, &psref);
   1525 	if (mib == NULL) {
   1526 		m_freem(m);
   1527 		ifp->if_noproto++;
   1528 		return;
   1529 	}
   1530 
   1531 	ifv = mib->ifvm_ifvlan;
   1532 	if ((ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
   1533 	    (IFF_UP|IFF_RUNNING)) {
   1534 		m_freem(m);
   1535 		ifp->if_noproto++;
   1536 		goto out;
   1537 	}
   1538 
   1539 	/*
   1540 	 * Now, remove the encapsulation header.  The original
   1541 	 * header has already been fixed up above.
   1542 	 */
   1543 	if (!have_vtag) {
   1544 		memmove(mtod(m, char *) + mib->ifvm_encaplen,
   1545 		    mtod(m, void *), sizeof(struct ether_header));
   1546 		m_adj(m, mib->ifvm_encaplen);
   1547 	}
   1548 
   1549 	m_set_rcvif(m, &ifv->ifv_if);
   1550 	ifv->ifv_if.if_ipackets++;
   1551 
   1552 	m->m_flags &= ~M_PROMISC;
   1553 	if_input(&ifv->ifv_if, m);
   1554 out:
   1555 	vlan_putref_linkmib(mib, &psref);
   1556 }
   1557 
   1558 /*
   1559  * Module infrastructure
   1560  */
   1561 #include "if_module.h"
   1562 
   1563 IF_MODULE(MODULE_CLASS_DRIVER, vlan, "")
   1564