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