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