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