Home | History | Annotate | Line # | Download | only in net
if_vlan.c revision 1.92
      1  1.92     joerg /*	$NetBSD: if_vlan.c,v 1.92 2016/11/28 00:39:03 joerg 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.92     joerg __KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.92 2016/11/28 00:39:03 joerg 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.1   thorpej #include <sys/kernel.h>
     90   1.1   thorpej #include <sys/mbuf.h>
     91   1.1   thorpej #include <sys/queue.h>
     92   1.1   thorpej #include <sys/socket.h>
     93   1.1   thorpej #include <sys/sockio.h>
     94   1.1   thorpej #include <sys/systm.h>
     95   1.1   thorpej #include <sys/proc.h>
     96  1.48      elad #include <sys/kauth.h>
     97  1.76     ozaki #include <sys/mutex.h>
     98  1.91  christos #include <sys/device.h>
     99  1.91  christos #include <sys/module.h>
    100   1.1   thorpej 
    101   1.1   thorpej #include <net/bpf.h>
    102   1.1   thorpej #include <net/if.h>
    103   1.1   thorpej #include <net/if_dl.h>
    104   1.1   thorpej #include <net/if_types.h>
    105   1.1   thorpej #include <net/if_ether.h>
    106   1.1   thorpej #include <net/if_vlanvar.h>
    107   1.1   thorpej 
    108   1.1   thorpej #ifdef INET
    109   1.1   thorpej #include <netinet/in.h>
    110   1.1   thorpej #include <netinet/if_inarp.h>
    111   1.1   thorpej #endif
    112  1.74     ozaki #ifdef INET6
    113  1.74     ozaki #include <netinet6/in6_ifattach.h>
    114  1.74     ozaki #endif
    115   1.1   thorpej 
    116  1.82  christos #include "ioconf.h"
    117  1.82  christos 
    118  1.10   thorpej struct vlan_mc_entry {
    119  1.10   thorpej 	LIST_ENTRY(vlan_mc_entry)	mc_entries;
    120  1.10   thorpej 	/*
    121  1.10   thorpej 	 * A key to identify this entry.  The mc_addr below can't be
    122  1.10   thorpej 	 * used since multiple sockaddr may mapped into the same
    123  1.10   thorpej 	 * ether_multi (e.g., AF_UNSPEC).
    124  1.10   thorpej 	 */
    125  1.10   thorpej 	union {
    126  1.10   thorpej 		struct ether_multi	*mcu_enm;
    127  1.10   thorpej 	} mc_u;
    128  1.10   thorpej 	struct sockaddr_storage		mc_addr;
    129  1.10   thorpej };
    130  1.10   thorpej 
    131  1.10   thorpej #define	mc_enm		mc_u.mcu_enm
    132  1.10   thorpej 
    133  1.10   thorpej struct ifvlan {
    134  1.10   thorpej 	union {
    135  1.10   thorpej 		struct ethercom ifvu_ec;
    136  1.10   thorpej 	} ifv_u;
    137  1.10   thorpej 	struct ifnet *ifv_p;	/* parent interface of this vlan */
    138  1.10   thorpej 	struct ifv_linkmib {
    139  1.10   thorpej 		const struct vlan_multisw *ifvm_msw;
    140  1.10   thorpej 		int	ifvm_encaplen;	/* encapsulation length */
    141  1.10   thorpej 		int	ifvm_mtufudge;	/* MTU fudged by this much */
    142  1.10   thorpej 		int	ifvm_mintu;	/* min transmission unit */
    143  1.57      matt 		uint16_t ifvm_proto;	/* encapsulation ethertype */
    144  1.57      matt 		uint16_t ifvm_tag;	/* tag to apply on packets */
    145  1.10   thorpej 	} ifv_mib;
    146  1.10   thorpej 	LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
    147  1.10   thorpej 	LIST_ENTRY(ifvlan) ifv_list;
    148  1.17   thorpej 	int ifv_flags;
    149  1.10   thorpej };
    150  1.10   thorpej 
    151  1.17   thorpej #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
    152  1.17   thorpej 
    153  1.10   thorpej #define	ifv_ec		ifv_u.ifvu_ec
    154  1.10   thorpej 
    155  1.10   thorpej #define	ifv_if		ifv_ec.ec_if
    156  1.10   thorpej 
    157  1.10   thorpej #define	ifv_msw		ifv_mib.ifvm_msw
    158  1.10   thorpej #define	ifv_encaplen	ifv_mib.ifvm_encaplen
    159  1.10   thorpej #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
    160  1.10   thorpej #define	ifv_mintu	ifv_mib.ifvm_mintu
    161  1.10   thorpej #define	ifv_tag		ifv_mib.ifvm_tag
    162  1.10   thorpej 
    163  1.10   thorpej struct vlan_multisw {
    164  1.10   thorpej 	int	(*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
    165  1.10   thorpej 	int	(*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
    166  1.10   thorpej 	void	(*vmsw_purgemulti)(struct ifvlan *);
    167  1.10   thorpej };
    168  1.10   thorpej 
    169  1.10   thorpej static int	vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
    170  1.10   thorpej static int	vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
    171  1.10   thorpej static void	vlan_ether_purgemulti(struct ifvlan *);
    172  1.10   thorpej 
    173  1.10   thorpej const struct vlan_multisw vlan_ether_multisw = {
    174  1.10   thorpej 	vlan_ether_addmulti,
    175  1.10   thorpej 	vlan_ether_delmulti,
    176  1.10   thorpej 	vlan_ether_purgemulti,
    177  1.10   thorpej };
    178  1.10   thorpej 
    179   1.1   thorpej static int	vlan_clone_create(struct if_clone *, int);
    180  1.42     peter static int	vlan_clone_destroy(struct ifnet *);
    181   1.1   thorpej static int	vlan_config(struct ifvlan *, struct ifnet *);
    182  1.53  christos static int	vlan_ioctl(struct ifnet *, u_long, void *);
    183   1.1   thorpej static void	vlan_start(struct ifnet *);
    184  1.11   thorpej static void	vlan_unconfig(struct ifnet *);
    185  1.10   thorpej 
    186   1.1   thorpej /* XXX This should be a hash table with the tag as the basis of the key. */
    187   1.1   thorpej static LIST_HEAD(, ifvlan) ifv_list;
    188   1.1   thorpej 
    189  1.76     ozaki static kmutex_t ifv_mtx __cacheline_aligned;
    190  1.76     ozaki 
    191   1.1   thorpej struct if_clone vlan_cloner =
    192   1.1   thorpej     IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
    193   1.1   thorpej 
    194  1.38       scw /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
    195  1.38       scw static char vlan_zero_pad_buff[ETHER_MIN_LEN];
    196  1.38       scw 
    197   1.1   thorpej void
    198  1.52  christos vlanattach(int n)
    199   1.1   thorpej {
    200   1.1   thorpej 
    201  1.91  christos 	/*
    202  1.91  christos 	 * Nothing to do here, initialization is handled by the
    203  1.91  christos 	 * module initialization code in vlaninit() below).
    204  1.91  christos 	 */
    205  1.91  christos }
    206  1.91  christos 
    207  1.91  christos static void
    208  1.91  christos vlaninit(void)
    209  1.91  christos {
    210  1.91  christos 
    211   1.1   thorpej 	LIST_INIT(&ifv_list);
    212  1.76     ozaki 	mutex_init(&ifv_mtx, MUTEX_DEFAULT, IPL_NONE);
    213   1.1   thorpej 	if_clone_attach(&vlan_cloner);
    214   1.1   thorpej }
    215   1.1   thorpej 
    216  1.91  christos static int
    217  1.91  christos vlandetach(void)
    218  1.91  christos {
    219  1.91  christos 	int error = 0;
    220  1.91  christos 
    221  1.91  christos 	if (!LIST_EMPTY(&ifv_list))
    222  1.91  christos 		error = EBUSY;
    223  1.91  christos 
    224  1.91  christos 	if (error == 0) {
    225  1.91  christos 		if_clone_detach(&vlan_cloner);
    226  1.91  christos 		mutex_destroy(&ifv_mtx);
    227  1.91  christos 	}
    228  1.91  christos 
    229  1.91  christos 	return error;
    230  1.91  christos }
    231  1.91  christos 
    232  1.30   thorpej static void
    233  1.30   thorpej vlan_reset_linkname(struct ifnet *ifp)
    234  1.30   thorpej {
    235  1.30   thorpej 
    236  1.30   thorpej 	/*
    237  1.30   thorpej 	 * We start out with a "802.1Q VLAN" type and zero-length
    238  1.30   thorpej 	 * addresses.  When we attach to a parent interface, we
    239  1.30   thorpej 	 * inherit its type, address length, address, and data link
    240  1.30   thorpej 	 * type.
    241  1.30   thorpej 	 */
    242  1.30   thorpej 
    243  1.30   thorpej 	ifp->if_type = IFT_L2VLAN;
    244  1.30   thorpej 	ifp->if_addrlen = 0;
    245  1.30   thorpej 	ifp->if_dlt = DLT_NULL;
    246  1.30   thorpej 	if_alloc_sadl(ifp);
    247  1.30   thorpej }
    248  1.30   thorpej 
    249   1.1   thorpej static int
    250   1.1   thorpej vlan_clone_create(struct if_clone *ifc, int unit)
    251   1.1   thorpej {
    252   1.1   thorpej 	struct ifvlan *ifv;
    253   1.1   thorpej 	struct ifnet *ifp;
    254  1.11   thorpej 	int s;
    255   1.1   thorpej 
    256  1.59  christos 	ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK|M_ZERO);
    257  1.14     enami 	ifp = &ifv->ifv_if;
    258   1.5     enami 	LIST_INIT(&ifv->ifv_mc_listhead);
    259  1.11   thorpej 
    260  1.11   thorpej 	s = splnet();
    261   1.1   thorpej 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
    262  1.11   thorpej 	splx(s);
    263   1.1   thorpej 
    264  1.59  christos 	if_initname(ifp, ifc->ifc_name, unit);
    265   1.1   thorpej 	ifp->if_softc = ifv;
    266   1.1   thorpej 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    267   1.1   thorpej 	ifp->if_start = vlan_start;
    268   1.1   thorpej 	ifp->if_ioctl = vlan_ioctl;
    269  1.31   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    270   1.1   thorpej 
    271  1.84     ozaki 	if_initialize(ifp);
    272  1.30   thorpej 	vlan_reset_linkname(ifp);
    273  1.84     ozaki 	if_register(ifp);
    274   1.1   thorpej 
    275   1.1   thorpej 	return (0);
    276   1.1   thorpej }
    277   1.1   thorpej 
    278  1.42     peter static int
    279   1.1   thorpej vlan_clone_destroy(struct ifnet *ifp)
    280   1.1   thorpej {
    281  1.11   thorpej 	struct ifvlan *ifv = ifp->if_softc;
    282   1.1   thorpej 	int s;
    283   1.1   thorpej 
    284  1.11   thorpej 	s = splnet();
    285   1.1   thorpej 	LIST_REMOVE(ifv, ifv_list);
    286   1.1   thorpej 	vlan_unconfig(ifp);
    287  1.78     ozaki 	if_detach(ifp);
    288  1.11   thorpej 	splx(s);
    289   1.1   thorpej 
    290   1.1   thorpej 	free(ifv, M_DEVBUF);
    291  1.42     peter 
    292  1.42     peter 	return (0);
    293   1.1   thorpej }
    294   1.1   thorpej 
    295  1.11   thorpej /*
    296  1.11   thorpej  * Configure a VLAN interface.  Must be called at splnet().
    297  1.11   thorpej  */
    298   1.1   thorpej static int
    299   1.1   thorpej vlan_config(struct ifvlan *ifv, struct ifnet *p)
    300   1.1   thorpej {
    301  1.10   thorpej 	struct ifnet *ifp = &ifv->ifv_if;
    302  1.10   thorpej 	int error;
    303   1.1   thorpej 
    304   1.1   thorpej 	if (ifv->ifv_p != NULL)
    305   1.1   thorpej 		return (EBUSY);
    306  1.10   thorpej 
    307  1.10   thorpej 	switch (p->if_type) {
    308  1.10   thorpej 	case IFT_ETHER:
    309  1.10   thorpej 	    {
    310  1.10   thorpej 		struct ethercom *ec = (void *) p;
    311  1.10   thorpej 
    312  1.10   thorpej 		ifv->ifv_msw = &vlan_ether_multisw;
    313  1.10   thorpej 		ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
    314  1.10   thorpej 		ifv->ifv_mintu = ETHERMIN;
    315  1.10   thorpej 
    316  1.83  christos 		if (ec->ec_nvlans == 0) {
    317  1.83  christos 			if ((error = ether_enable_vlan_mtu(p)) >= 0) {
    318  1.83  christos 				if (error)
    319  1.83  christos 					return error;
    320  1.83  christos 				ifv->ifv_mtufudge = 0;
    321  1.83  christos 			} else {
    322  1.83  christos 				/*
    323  1.83  christos 				 * Fudge the MTU by the encapsulation size. This
    324  1.83  christos 				 * makes us incompatible with strictly compliant
    325  1.83  christos 				 * 802.1Q implementations, but allows us to use
    326  1.83  christos 				 * the feature with other NetBSD
    327  1.83  christos 				 * implementations, which might still be useful.
    328  1.83  christos 				 */
    329  1.83  christos 				ifv->ifv_mtufudge = ifv->ifv_encaplen;
    330  1.10   thorpej 			}
    331  1.10   thorpej 		}
    332  1.88  christos 		ec->ec_nvlans++;
    333  1.10   thorpej 
    334  1.10   thorpej 		/*
    335  1.32   thorpej 		 * If the parent interface can do hardware-assisted
    336  1.32   thorpej 		 * VLAN encapsulation, then propagate its hardware-
    337  1.63    darran 		 * assisted checksumming flags and tcp segmentation
    338  1.63    darran 		 * offload.
    339  1.32   thorpej 		 */
    340  1.67  sborrill 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
    341  1.67  sborrill 		        ec->ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
    342  1.32   thorpej 			ifp->if_capabilities = p->if_capabilities &
    343  1.65    darran 			    (IFCAP_TSOv4 | IFCAP_TSOv6 |
    344  1.63    darran 			     IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx|
    345  1.46      yamt 			     IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx|
    346  1.46      yamt 			     IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx|
    347  1.46      yamt 			     IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx|
    348  1.46      yamt 			     IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx);
    349  1.67  sborrill                 }
    350  1.32   thorpej 		/*
    351  1.10   thorpej 		 * We inherit the parent's Ethernet address.
    352  1.10   thorpej 		 */
    353  1.54    dyoung 		ether_ifattach(ifp, CLLADDR(p->if_sadl));
    354  1.10   thorpej 		ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
    355  1.10   thorpej 		break;
    356  1.10   thorpej 	    }
    357  1.10   thorpej 
    358  1.10   thorpej 	default:
    359  1.10   thorpej 		return (EPROTONOSUPPORT);
    360  1.10   thorpej 	}
    361  1.10   thorpej 
    362   1.1   thorpej 	ifv->ifv_p = p;
    363  1.10   thorpej 	ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
    364  1.21    bouyer 	ifv->ifv_if.if_flags = p->if_flags &
    365  1.23    bouyer 	    (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
    366   1.1   thorpej 
    367   1.1   thorpej 	/*
    368  1.10   thorpej 	 * Inherit the if_type from the parent.  This allows us
    369  1.10   thorpej 	 * to participate in bridges of that type.
    370   1.1   thorpej 	 */
    371  1.10   thorpej 	ifv->ifv_if.if_type = p->if_type;
    372  1.10   thorpej 
    373   1.1   thorpej 	return (0);
    374   1.1   thorpej }
    375   1.1   thorpej 
    376  1.11   thorpej /*
    377  1.11   thorpej  * Unconfigure a VLAN interface.  Must be called at splnet().
    378  1.11   thorpej  */
    379  1.11   thorpej static void
    380   1.1   thorpej vlan_unconfig(struct ifnet *ifp)
    381   1.1   thorpej {
    382  1.10   thorpej 	struct ifvlan *ifv = ifp->if_softc;
    383  1.77     ozaki 	struct ifnet *p;
    384   1.1   thorpej 
    385  1.76     ozaki 	mutex_enter(&ifv_mtx);
    386  1.77     ozaki 	p = ifv->ifv_p;
    387  1.76     ozaki 
    388  1.77     ozaki 	if (p == NULL) {
    389  1.76     ozaki 		mutex_exit(&ifv_mtx);
    390  1.11   thorpej 		return;
    391  1.76     ozaki 	}
    392   1.1   thorpej 
    393   1.1   thorpej 	/*
    394   1.1   thorpej  	 * Since the interface is being unconfigured, we need to empty the
    395   1.1   thorpej 	 * list of multicast groups that we may have joined while we were
    396   1.1   thorpej 	 * alive and remove them from the parent's list also.
    397   1.1   thorpej 	 */
    398  1.10   thorpej 	(*ifv->ifv_msw->vmsw_purgemulti)(ifv);
    399   1.1   thorpej 
    400   1.1   thorpej 	/* Disconnect from parent. */
    401  1.77     ozaki 	switch (p->if_type) {
    402  1.10   thorpej 	case IFT_ETHER:
    403  1.10   thorpej 	    {
    404  1.83  christos 		struct ethercom *ec = (void *)p;
    405  1.83  christos 		if (--ec->ec_nvlans == 0)
    406  1.83  christos 			(void)ether_disable_vlan_mtu(p);
    407  1.10   thorpej 
    408  1.10   thorpej 		ether_ifdetach(ifp);
    409  1.71     ozaki 		/* Restore vlan_ioctl overwritten by ether_ifdetach */
    410  1.71     ozaki 		ifp->if_ioctl = vlan_ioctl;
    411  1.30   thorpej 		vlan_reset_linkname(ifp);
    412  1.10   thorpej 		break;
    413  1.10   thorpej 	    }
    414  1.10   thorpej 
    415  1.10   thorpej #ifdef DIAGNOSTIC
    416  1.10   thorpej 	default:
    417  1.10   thorpej 		panic("vlan_unconfig: impossible");
    418  1.10   thorpej #endif
    419  1.10   thorpej 	}
    420  1.10   thorpej 
    421   1.1   thorpej 	ifv->ifv_p = NULL;
    422  1.10   thorpej 	ifv->ifv_if.if_mtu = 0;
    423  1.17   thorpej 	ifv->ifv_flags = 0;
    424   1.1   thorpej 
    425  1.74     ozaki #ifdef INET6
    426  1.74     ozaki 	/* To delete v6 link local addresses */
    427  1.74     ozaki 	in6_ifdetach(ifp);
    428  1.74     ozaki #endif
    429  1.73     ozaki 	if ((ifp->if_flags & IFF_PROMISC) != 0)
    430  1.73     ozaki 		ifpromisc(ifp, 0);
    431  1.11   thorpej 	if_down(ifp);
    432  1.11   thorpej 	ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
    433  1.32   thorpej 	ifp->if_capabilities = 0;
    434  1.76     ozaki 
    435  1.76     ozaki 	mutex_exit(&ifv_mtx);
    436  1.11   thorpej }
    437  1.11   thorpej 
    438  1.11   thorpej /*
    439  1.11   thorpej  * Called when a parent interface is detaching; destroy any VLAN
    440  1.11   thorpej  * configuration for the parent interface.
    441  1.11   thorpej  */
    442  1.11   thorpej void
    443  1.11   thorpej vlan_ifdetach(struct ifnet *p)
    444  1.11   thorpej {
    445  1.11   thorpej 	struct ifvlan *ifv;
    446  1.11   thorpej 	int s;
    447  1.11   thorpej 
    448  1.11   thorpej 	s = splnet();
    449  1.11   thorpej 
    450  1.11   thorpej 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    451  1.11   thorpej 	     ifv = LIST_NEXT(ifv, ifv_list)) {
    452  1.11   thorpej 		if (ifv->ifv_p == p)
    453  1.11   thorpej 			vlan_unconfig(&ifv->ifv_if);
    454  1.11   thorpej 	}
    455  1.11   thorpej 
    456   1.1   thorpej 	splx(s);
    457   1.1   thorpej }
    458   1.1   thorpej 
    459   1.1   thorpej static int
    460  1.17   thorpej vlan_set_promisc(struct ifnet *ifp)
    461  1.17   thorpej {
    462  1.17   thorpej 	struct ifvlan *ifv = ifp->if_softc;
    463  1.20     enami 	int error = 0;
    464  1.17   thorpej 
    465  1.17   thorpej 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    466  1.17   thorpej 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
    467  1.17   thorpej 			error = ifpromisc(ifv->ifv_p, 1);
    468  1.17   thorpej 			if (error == 0)
    469  1.17   thorpej 				ifv->ifv_flags |= IFVF_PROMISC;
    470  1.17   thorpej 		}
    471  1.17   thorpej 	} else {
    472  1.17   thorpej 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
    473  1.17   thorpej 			error = ifpromisc(ifv->ifv_p, 0);
    474  1.17   thorpej 			if (error == 0)
    475  1.17   thorpej 				ifv->ifv_flags &= ~IFVF_PROMISC;
    476  1.17   thorpej 		}
    477  1.17   thorpej 	}
    478  1.17   thorpej 
    479  1.17   thorpej 	return (error);
    480  1.17   thorpej }
    481  1.17   thorpej 
    482  1.17   thorpej static int
    483  1.53  christos vlan_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    484   1.1   thorpej {
    485  1.49        ad 	struct lwp *l = curlwp;	/* XXX */
    486  1.11   thorpej 	struct ifvlan *ifv = ifp->if_softc;
    487  1.11   thorpej 	struct ifaddr *ifa = (struct ifaddr *) data;
    488  1.11   thorpej 	struct ifreq *ifr = (struct ifreq *) data;
    489   1.1   thorpej 	struct ifnet *pr;
    490  1.60    bouyer 	struct ifcapreq *ifcr;
    491   1.1   thorpej 	struct vlanreq vlr;
    492  1.11   thorpej 	int s, error = 0;
    493   1.1   thorpej 
    494  1.11   thorpej 	s = splnet();
    495   1.1   thorpej 
    496   1.1   thorpej 	switch (cmd) {
    497   1.1   thorpej 	case SIOCSIFMTU:
    498  1.56    dyoung 		if (ifv->ifv_p == NULL)
    499  1.56    dyoung 			error = EINVAL;
    500  1.56    dyoung 		else if (
    501  1.56    dyoung 		    ifr->ifr_mtu > (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
    502  1.56    dyoung 		    ifr->ifr_mtu < (ifv->ifv_mintu - ifv->ifv_mtufudge))
    503   1.1   thorpej 			error = EINVAL;
    504  1.56    dyoung 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    505  1.56    dyoung 			error = 0;
    506   1.1   thorpej 		break;
    507   1.1   thorpej 
    508   1.1   thorpej 	case SIOCSETVLAN:
    509  1.51      elad 		if ((error = kauth_authorize_network(l->l_cred,
    510  1.51      elad 		    KAUTH_NETWORK_INTERFACE,
    511  1.51      elad 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    512  1.51      elad 		    NULL)) != 0)
    513   1.1   thorpej 			break;
    514   1.1   thorpej 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
    515   1.1   thorpej 			break;
    516   1.1   thorpej 		if (vlr.vlr_parent[0] == '\0') {
    517  1.73     ozaki 			if (ifv->ifv_p != NULL &&
    518  1.73     ozaki 			    (ifp->if_flags & IFF_PROMISC) != 0)
    519  1.73     ozaki 				error = ifpromisc(ifv->ifv_p, 0);
    520   1.1   thorpej 			vlan_unconfig(ifp);
    521   1.1   thorpej 			break;
    522   1.1   thorpej 		}
    523   1.1   thorpej 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
    524   1.1   thorpej 			error = EINVAL;		 /* check for valid tag */
    525   1.1   thorpej 			break;
    526   1.1   thorpej 		}
    527   1.1   thorpej 		if ((pr = ifunit(vlr.vlr_parent)) == 0) {
    528   1.1   thorpej 			error = ENOENT;
    529   1.1   thorpej 			break;
    530   1.1   thorpej 		}
    531   1.1   thorpej 		if ((error = vlan_config(ifv, pr)) != 0)
    532   1.1   thorpej 			break;
    533   1.1   thorpej 		ifv->ifv_tag = vlr.vlr_tag;
    534   1.1   thorpej 		ifp->if_flags |= IFF_RUNNING;
    535  1.17   thorpej 
    536  1.17   thorpej 		/* Update promiscuous mode, if necessary. */
    537  1.17   thorpej 		vlan_set_promisc(ifp);
    538   1.1   thorpej 		break;
    539   1.1   thorpej 
    540   1.1   thorpej 	case SIOCGETVLAN:
    541   1.1   thorpej 		memset(&vlr, 0, sizeof(vlr));
    542   1.1   thorpej 		if (ifv->ifv_p != NULL) {
    543   1.1   thorpej 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
    544   1.1   thorpej 			    ifv->ifv_p->if_xname);
    545   1.1   thorpej 			vlr.vlr_tag = ifv->ifv_tag;
    546   1.1   thorpej 		}
    547   1.1   thorpej 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
    548   1.1   thorpej 		break;
    549   1.1   thorpej 
    550   1.1   thorpej 	case SIOCSIFFLAGS:
    551  1.61    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    552  1.61    dyoung 			break;
    553   1.1   thorpej 		/*
    554  1.17   thorpej 		 * For promiscuous mode, we enable promiscuous mode on
    555  1.17   thorpej 		 * the parent if we need promiscuous on the VLAN interface.
    556   1.1   thorpej 		 */
    557  1.17   thorpej 		if (ifv->ifv_p != NULL)
    558  1.17   thorpej 			error = vlan_set_promisc(ifp);
    559   1.1   thorpej 		break;
    560   1.1   thorpej 
    561   1.1   thorpej 	case SIOCADDMULTI:
    562  1.26   thorpej 		error = (ifv->ifv_p != NULL) ?
    563  1.26   thorpej 		    (*ifv->ifv_msw->vmsw_addmulti)(ifv, ifr) : EINVAL;
    564   1.6     enami 		break;
    565   1.6     enami 
    566   1.1   thorpej 	case SIOCDELMULTI:
    567  1.26   thorpej 		error = (ifv->ifv_p != NULL) ?
    568  1.26   thorpej 		    (*ifv->ifv_msw->vmsw_delmulti)(ifv, ifr) : EINVAL;
    569   1.1   thorpej 		break;
    570   1.1   thorpej 
    571  1.60    bouyer 	case SIOCSIFCAP:
    572  1.60    bouyer 		ifcr = data;
    573  1.60    bouyer 		/* make sure caps are enabled on parent */
    574  1.92     joerg 		if (ifv->ifv_p == NULL) {
    575  1.92     joerg 			error = EINVAL;
    576  1.92     joerg 			break;
    577  1.92     joerg 		}
    578  1.60    bouyer 		if ((ifv->ifv_p->if_capenable & ifcr->ifcr_capenable) !=
    579  1.60    bouyer 		    ifcr->ifcr_capenable) {
    580  1.60    bouyer 			error = EINVAL;
    581  1.60    bouyer 			break;
    582  1.60    bouyer 		}
    583  1.60    bouyer 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    584  1.60    bouyer 			error = 0;
    585  1.60    bouyer 		break;
    586  1.68    dyoung 	case SIOCINITIFADDR:
    587  1.68    dyoung 		if (ifv->ifv_p == NULL) {
    588  1.68    dyoung 			error = EINVAL;
    589  1.68    dyoung 			break;
    590  1.68    dyoung 		}
    591  1.68    dyoung 
    592  1.68    dyoung 		ifp->if_flags |= IFF_UP;
    593  1.68    dyoung #ifdef INET
    594  1.68    dyoung 		if (ifa->ifa_addr->sa_family == AF_INET)
    595  1.68    dyoung 			arp_ifinit(ifp, ifa);
    596  1.68    dyoung #endif
    597  1.68    dyoung 		break;
    598  1.68    dyoung 
    599   1.1   thorpej 	default:
    600  1.61    dyoung 		error = ether_ioctl(ifp, cmd, data);
    601   1.1   thorpej 	}
    602  1.11   thorpej 
    603  1.11   thorpej 	splx(s);
    604   1.1   thorpej 
    605   1.1   thorpej 	return (error);
    606   1.1   thorpej }
    607   1.1   thorpej 
    608   1.1   thorpej static int
    609  1.10   thorpej vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
    610   1.1   thorpej {
    611  1.55    dyoung 	const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
    612   1.1   thorpej 	struct vlan_mc_entry *mc;
    613  1.57      matt 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    614   1.1   thorpej 	int error;
    615   1.1   thorpej 
    616  1.55    dyoung 	if (sa->sa_len > sizeof(struct sockaddr_storage))
    617   1.5     enami 		return (EINVAL);
    618   1.5     enami 
    619  1.55    dyoung 	error = ether_addmulti(sa, &ifv->ifv_ec);
    620   1.5     enami 	if (error != ENETRESET)
    621   1.5     enami 		return (error);
    622   1.1   thorpej 
    623   1.5     enami 	/*
    624   1.5     enami 	 * This is new multicast address.  We have to tell parent
    625   1.5     enami 	 * about it.  Also, remember this multicast address so that
    626   1.5     enami 	 * we can delete them on unconfigure.
    627   1.5     enami 	 */
    628  1.62    cegger 	mc = malloc(sizeof(struct vlan_mc_entry), M_DEVBUF, M_NOWAIT);
    629   1.5     enami 	if (mc == NULL) {
    630   1.5     enami 		error = ENOMEM;
    631   1.5     enami 		goto alloc_failed;
    632   1.1   thorpej 	}
    633   1.1   thorpej 
    634   1.5     enami 	/*
    635   1.5     enami 	 * As ether_addmulti() returns ENETRESET, following two
    636   1.5     enami 	 * statement shouldn't fail.
    637   1.5     enami 	 */
    638  1.55    dyoung 	(void)ether_multiaddr(sa, addrlo, addrhi);
    639   1.5     enami 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
    640  1.55    dyoung 	memcpy(&mc->mc_addr, sa, sa->sa_len);
    641   1.5     enami 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
    642   1.5     enami 
    643  1.69    dyoung 	error = if_mcast_op(ifv->ifv_p, SIOCADDMULTI, sa);
    644   1.5     enami 	if (error != 0)
    645   1.5     enami 		goto ioctl_failed;
    646   1.5     enami 	return (error);
    647   1.5     enami 
    648   1.5     enami  ioctl_failed:
    649   1.5     enami 	LIST_REMOVE(mc, mc_entries);
    650  1.62    cegger 	free(mc, M_DEVBUF);
    651   1.5     enami  alloc_failed:
    652  1.55    dyoung 	(void)ether_delmulti(sa, &ifv->ifv_ec);
    653   1.5     enami 	return (error);
    654   1.5     enami }
    655   1.5     enami 
    656   1.5     enami static int
    657  1.10   thorpej vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
    658   1.5     enami {
    659  1.55    dyoung 	const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
    660   1.5     enami 	struct ether_multi *enm;
    661   1.5     enami 	struct vlan_mc_entry *mc;
    662  1.57      matt 	uint8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    663   1.5     enami 	int error;
    664   1.5     enami 
    665   1.5     enami 	/*
    666   1.5     enami 	 * Find a key to lookup vlan_mc_entry.  We have to do this
    667   1.5     enami 	 * before calling ether_delmulti for obvious reason.
    668   1.5     enami 	 */
    669  1.55    dyoung 	if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
    670   1.5     enami 		return (error);
    671   1.5     enami 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
    672   1.5     enami 
    673  1.55    dyoung 	error = ether_delmulti(sa, &ifv->ifv_ec);
    674   1.5     enami 	if (error != ENETRESET)
    675   1.5     enami 		return (error);
    676   1.5     enami 
    677   1.5     enami 	/* We no longer use this multicast address.  Tell parent so. */
    678  1.69    dyoung 	error = if_mcast_op(ifv->ifv_p, SIOCDELMULTI, sa);
    679   1.5     enami 	if (error == 0) {
    680   1.5     enami 		/* And forget about this address. */
    681   1.5     enami 		for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
    682   1.5     enami 		    mc = LIST_NEXT(mc, mc_entries)) {
    683   1.5     enami 			if (mc->mc_enm == enm) {
    684   1.5     enami 				LIST_REMOVE(mc, mc_entries);
    685  1.62    cegger 				free(mc, M_DEVBUF);
    686   1.5     enami 				break;
    687   1.5     enami 			}
    688   1.5     enami 		}
    689   1.5     enami 		KASSERT(mc != NULL);
    690   1.5     enami 	} else
    691  1.55    dyoung 		(void)ether_addmulti(sa, &ifv->ifv_ec);
    692   1.5     enami 	return (error);
    693   1.5     enami }
    694   1.5     enami 
    695   1.5     enami /*
    696  1.34     pooka  * Delete any multicast address we have asked to add from parent
    697   1.5     enami  * interface.  Called when the vlan is being unconfigured.
    698   1.5     enami  */
    699   1.5     enami static void
    700  1.10   thorpej vlan_ether_purgemulti(struct ifvlan *ifv)
    701   1.5     enami {
    702   1.5     enami 	struct ifnet *ifp = ifv->ifv_p;		/* Parent. */
    703   1.5     enami 	struct vlan_mc_entry *mc;
    704   1.5     enami 
    705   1.5     enami 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
    706  1.69    dyoung 		(void)if_mcast_op(ifp, SIOCDELMULTI,
    707  1.55    dyoung 		    (const struct sockaddr *)&mc->mc_addr);
    708   1.5     enami 		LIST_REMOVE(mc, mc_entries);
    709  1.62    cegger 		free(mc, M_DEVBUF);
    710   1.1   thorpej 	}
    711   1.1   thorpej }
    712   1.1   thorpej 
    713   1.1   thorpej static void
    714   1.1   thorpej vlan_start(struct ifnet *ifp)
    715   1.1   thorpej {
    716  1.14     enami 	struct ifvlan *ifv = ifp->if_softc;
    717  1.14     enami 	struct ifnet *p = ifv->ifv_p;
    718  1.24    bouyer 	struct ethercom *ec = (void *) ifv->ifv_p;
    719   1.1   thorpej 	struct mbuf *m;
    720  1.31   thorpej 	int error;
    721   1.1   thorpej 
    722  1.75     ozaki #ifndef NET_MPSAFE
    723  1.70    bouyer 	KASSERT(KERNEL_LOCKED_P());
    724  1.75     ozaki #endif
    725  1.70    bouyer 
    726   1.1   thorpej 	ifp->if_flags |= IFF_OACTIVE;
    727   1.1   thorpej 
    728   1.1   thorpej 	for (;;) {
    729  1.31   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m);
    730   1.1   thorpej 		if (m == NULL)
    731   1.1   thorpej 			break;
    732   1.1   thorpej 
    733  1.31   thorpej #ifdef ALTQ
    734  1.31   thorpej 		/*
    735  1.90  knakahar 		 * KERNEL_LOCK is required for ALTQ even if NET_MPSAFE if defined.
    736  1.90  knakahar 		 */
    737  1.90  knakahar 		KERNEL_LOCK(1, NULL);
    738  1.90  knakahar 		/*
    739  1.31   thorpej 		 * If ALTQ is enabled on the parent interface, do
    740  1.31   thorpej 		 * classification; the queueing discipline might
    741  1.31   thorpej 		 * not require classification, but might require
    742  1.31   thorpej 		 * the address family/header pointer in the pktattr.
    743  1.31   thorpej 		 */
    744  1.31   thorpej 		if (ALTQ_IS_ENABLED(&p->if_snd)) {
    745  1.31   thorpej 			switch (p->if_type) {
    746  1.31   thorpej 			case IFT_ETHER:
    747  1.85  knakahar 				altq_etherclassify(&p->if_snd, m);
    748  1.31   thorpej 				break;
    749  1.31   thorpej #ifdef DIAGNOSTIC
    750  1.31   thorpej 			default:
    751  1.31   thorpej 				panic("vlan_start: impossible (altq)");
    752  1.31   thorpej #endif
    753  1.31   thorpej 			}
    754  1.31   thorpej 		}
    755  1.90  knakahar 		KERNEL_UNLOCK_ONE(NULL);
    756  1.31   thorpej #endif /* ALTQ */
    757  1.31   thorpej 
    758  1.66     joerg 		bpf_mtap(ifp, m);
    759   1.1   thorpej 		/*
    760  1.24    bouyer 		 * If the parent can insert the tag itself, just mark
    761  1.24    bouyer 		 * the tag in the mbuf header.
    762   1.1   thorpej 		 */
    763  1.24    bouyer 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
    764  1.35    itojun 			struct m_tag *mtag;
    765  1.35    itojun 
    766  1.35    itojun 			mtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
    767  1.35    itojun 			    M_NOWAIT);
    768  1.35    itojun 			if (mtag == NULL) {
    769  1.24    bouyer 				ifp->if_oerrors++;
    770  1.24    bouyer 				m_freem(m);
    771  1.24    bouyer 				continue;
    772  1.24    bouyer 			}
    773  1.35    itojun 			*(u_int *)(mtag + 1) = ifv->ifv_tag;
    774  1.36  drochner 			m_tag_prepend(m, mtag);
    775  1.24    bouyer 		} else {
    776  1.24    bouyer 			/*
    777  1.34     pooka 			 * insert the tag ourselves
    778  1.24    bouyer 			 */
    779  1.24    bouyer 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
    780  1.24    bouyer 			if (m == NULL) {
    781  1.24    bouyer 				printf("%s: unable to prepend encap header",
    782  1.10   thorpej 				    ifv->ifv_p->if_xname);
    783  1.10   thorpej 				ifp->if_oerrors++;
    784  1.10   thorpej 				continue;
    785  1.10   thorpej 			}
    786  1.10   thorpej 
    787  1.24    bouyer 			switch (p->if_type) {
    788  1.24    bouyer 			case IFT_ETHER:
    789  1.24    bouyer 			    {
    790  1.24    bouyer 				struct ether_vlan_header *evl;
    791  1.24    bouyer 
    792  1.24    bouyer 				if (m->m_len < sizeof(struct ether_vlan_header))
    793  1.24    bouyer 					m = m_pullup(m,
    794  1.24    bouyer 					    sizeof(struct ether_vlan_header));
    795  1.24    bouyer 				if (m == NULL) {
    796  1.24    bouyer 					printf("%s: unable to pullup encap "
    797  1.24    bouyer 					    "header", ifv->ifv_p->if_xname);
    798  1.24    bouyer 					ifp->if_oerrors++;
    799  1.24    bouyer 					continue;
    800  1.24    bouyer 				}
    801  1.24    bouyer 
    802  1.24    bouyer 				/*
    803  1.24    bouyer 				 * Transform the Ethernet header into an
    804  1.24    bouyer 				 * Ethernet header with 802.1Q encapsulation.
    805  1.24    bouyer 				 */
    806  1.53  christos 				memmove(mtod(m, void *),
    807  1.53  christos 				    mtod(m, char *) + ifv->ifv_encaplen,
    808  1.24    bouyer 				    sizeof(struct ether_header));
    809  1.24    bouyer 				evl = mtod(m, struct ether_vlan_header *);
    810  1.24    bouyer 				evl->evl_proto = evl->evl_encap_proto;
    811  1.24    bouyer 				evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
    812  1.24    bouyer 				evl->evl_tag = htons(ifv->ifv_tag);
    813  1.38       scw 
    814  1.38       scw 				/*
    815  1.38       scw 				 * To cater for VLAN-aware layer 2 ethernet
    816  1.38       scw 				 * switches which may need to strip the tag
    817  1.38       scw 				 * before forwarding the packet, make sure
    818  1.38       scw 				 * the packet+tag is at least 68 bytes long.
    819  1.38       scw 				 * This is necessary because our parent will
    820  1.38       scw 				 * only pad to 64 bytes (ETHER_MIN_LEN) and
    821  1.38       scw 				 * some switches will not pad by themselves
    822  1.38       scw 				 * after deleting a tag.
    823  1.38       scw 				 */
    824  1.38       scw 				if (m->m_pkthdr.len <
    825  1.80     ozaki 				    (ETHER_MIN_LEN - ETHER_CRC_LEN +
    826  1.80     ozaki 				     ETHER_VLAN_ENCAP_LEN)) {
    827  1.38       scw 					m_copyback(m, m->m_pkthdr.len,
    828  1.80     ozaki 					    (ETHER_MIN_LEN - ETHER_CRC_LEN +
    829  1.38       scw 					     ETHER_VLAN_ENCAP_LEN) -
    830  1.38       scw 					     m->m_pkthdr.len,
    831  1.38       scw 					    vlan_zero_pad_buff);
    832  1.38       scw 				}
    833  1.24    bouyer 				break;
    834  1.24    bouyer 			    }
    835  1.10   thorpej 
    836  1.10   thorpej #ifdef DIAGNOSTIC
    837  1.24    bouyer 			default:
    838  1.24    bouyer 				panic("vlan_start: impossible");
    839  1.10   thorpej #endif
    840  1.24    bouyer 			}
    841   1.1   thorpej 		}
    842   1.1   thorpej 
    843   1.1   thorpej 		/*
    844  1.10   thorpej 		 * Send it, precisely as the parent's output routine
    845  1.34     pooka 		 * would have.  We are already running at splnet.
    846   1.1   thorpej 		 */
    847  1.87  knakahar 		if ((p->if_flags & IFF_RUNNING) != 0) {
    848  1.90  knakahar 			error = if_transmit_lock(p, m);
    849  1.87  knakahar 			if (error) {
    850  1.87  knakahar 				/* mbuf is already freed */
    851  1.87  knakahar 				ifp->if_oerrors++;
    852  1.87  knakahar 				continue;
    853  1.87  knakahar 			}
    854   1.1   thorpej 		}
    855  1.31   thorpej 
    856  1.23    bouyer 		ifp->if_opackets++;
    857   1.1   thorpej 	}
    858   1.1   thorpej 
    859   1.1   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    860   1.1   thorpej }
    861   1.1   thorpej 
    862   1.1   thorpej /*
    863   1.1   thorpej  * Given an Ethernet frame, find a valid vlan interface corresponding to the
    864  1.44     perry  * given source interface and tag, then run the real packet through the
    865  1.40    simonb  * parent's input routine.
    866   1.1   thorpej  */
    867   1.1   thorpej void
    868   1.1   thorpej vlan_input(struct ifnet *ifp, struct mbuf *m)
    869   1.1   thorpej {
    870   1.1   thorpej 	struct ifvlan *ifv;
    871   1.1   thorpej 	u_int tag;
    872  1.35    itojun 	struct m_tag *mtag;
    873  1.24    bouyer 
    874  1.35    itojun 	mtag = m_tag_find(m, PACKET_TAG_VLAN, NULL);
    875  1.35    itojun 	if (mtag != NULL) {
    876  1.35    itojun 		/* m contains a normal ethernet frame, the tag is in mtag */
    877  1.45      yamt 		tag = EVL_VLANOFTAG(*(u_int *)(mtag + 1));
    878  1.35    itojun 		m_tag_delete(m, mtag);
    879  1.24    bouyer 	} else {
    880  1.24    bouyer 		switch (ifp->if_type) {
    881  1.24    bouyer 		case IFT_ETHER:
    882  1.24    bouyer 		    {
    883  1.24    bouyer 			struct ether_vlan_header *evl;
    884  1.24    bouyer 
    885  1.24    bouyer 			if (m->m_len < sizeof(struct ether_vlan_header) &&
    886  1.24    bouyer 			    (m = m_pullup(m,
    887  1.24    bouyer 			     sizeof(struct ether_vlan_header))) == NULL) {
    888  1.24    bouyer 				printf("%s: no memory for VLAN header, "
    889  1.24    bouyer 				    "dropping packet.\n", ifp->if_xname);
    890  1.24    bouyer 				return;
    891  1.24    bouyer 			}
    892  1.24    bouyer 			evl = mtod(m, struct ether_vlan_header *);
    893  1.24    bouyer 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
    894  1.24    bouyer 
    895  1.24    bouyer 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
    896   1.1   thorpej 
    897  1.24    bouyer 			/*
    898  1.24    bouyer 			 * Restore the original ethertype.  We'll remove
    899  1.24    bouyer 			 * the encapsulation after we've found the vlan
    900  1.24    bouyer 			 * interface corresponding to the tag.
    901  1.24    bouyer 			 */
    902  1.24    bouyer 			evl->evl_encap_proto = evl->evl_proto;
    903  1.24    bouyer 			break;
    904  1.24    bouyer 		    }
    905  1.10   thorpej 
    906  1.24    bouyer 		default:
    907  1.24    bouyer 			tag = (u_int) -1;	/* XXX GCC */
    908  1.24    bouyer #ifdef DIAGNOSTIC
    909  1.24    bouyer 			panic("vlan_input: impossible");
    910  1.24    bouyer #endif
    911  1.10   thorpej 		}
    912  1.43  christos 	}
    913  1.10   thorpej 
    914  1.43  christos 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    915  1.43  christos 	    ifv = LIST_NEXT(ifv, ifv_list))
    916  1.43  christos 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
    917  1.43  christos 			break;
    918   1.1   thorpej 
    919  1.10   thorpej 	if (ifv == NULL ||
    920  1.10   thorpej 	    (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
    921  1.10   thorpej 	     (IFF_UP|IFF_RUNNING)) {
    922  1.37    itojun 		m_freem(m);
    923  1.14     enami 		ifp->if_noproto++;
    924   1.1   thorpej 		return;
    925   1.1   thorpej 	}
    926  1.43  christos 
    927  1.43  christos 	/*
    928  1.43  christos 	 * Now, remove the encapsulation header.  The original
    929  1.43  christos 	 * header has already been fixed up above.
    930  1.43  christos 	 */
    931  1.43  christos 	if (mtag == NULL) {
    932  1.53  christos 		memmove(mtod(m, char *) + ifv->ifv_encaplen,
    933  1.53  christos 		    mtod(m, void *), sizeof(struct ether_header));
    934  1.43  christos 		m_adj(m, ifv->ifv_encaplen);
    935  1.43  christos 	}
    936  1.43  christos 
    937  1.89     ozaki 	m_set_rcvif(m, &ifv->ifv_if);
    938   1.1   thorpej 	ifv->ifv_if.if_ipackets++;
    939   1.1   thorpej 
    940  1.66     joerg 	bpf_mtap(&ifv->ifv_if, m);
    941   1.1   thorpej 
    942  1.72     ozaki 	m->m_flags &= ~M_PROMISC;
    943  1.84     ozaki 	if_input(&ifv->ifv_if, m);
    944   1.1   thorpej }
    945  1.91  christos 
    946  1.91  christos /*
    947  1.91  christos  * Module infrastructure
    948  1.91  christos  */
    949  1.91  christos #include "if_module.h"
    950  1.91  christos 
    951  1.91  christos IF_MODULE(MODULE_CLASS_DRIVER, vlan, "")
    952