Home | History | Annotate | Line # | Download | only in net
if_vlan.c revision 1.47.10.2
      1  1.47.10.2      elad /*	$NetBSD: if_vlan.c,v 1.47.10.2 2006/03/10 15:05:23 elad 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  * 3. All advertising materials mentioning features or use of this software
     19        1.1   thorpej  *    must display the following acknowledgement:
     20        1.1   thorpej  *	This product includes software developed by the NetBSD
     21        1.1   thorpej  *	Foundation, Inc. and its contributors.
     22        1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23        1.1   thorpej  *    contributors may be used to endorse or promote products derived
     24        1.1   thorpej  *    from this software without specific prior written permission.
     25        1.1   thorpej  *
     26        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27        1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37        1.1   thorpej  */
     38        1.1   thorpej 
     39        1.1   thorpej /*
     40        1.1   thorpej  * Copyright 1998 Massachusetts Institute of Technology
     41        1.1   thorpej  *
     42        1.1   thorpej  * Permission to use, copy, modify, and distribute this software and
     43        1.1   thorpej  * its documentation for any purpose and without fee is hereby
     44        1.1   thorpej  * granted, provided that both the above copyright notice and this
     45        1.1   thorpej  * permission notice appear in all copies, that both the above
     46        1.1   thorpej  * copyright notice and this permission notice appear in all
     47        1.1   thorpej  * supporting documentation, and that the name of M.I.T. not be used
     48        1.1   thorpej  * in advertising or publicity pertaining to distribution of the
     49        1.1   thorpej  * software without specific, written prior permission.  M.I.T. makes
     50        1.1   thorpej  * no representations about the suitability of this software for any
     51        1.1   thorpej  * purpose.  It is provided "as is" without express or implied
     52        1.1   thorpej  * warranty.
     53       1.44     perry  *
     54        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
     55        1.1   thorpej  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
     56        1.1   thorpej  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     57        1.1   thorpej  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
     58        1.1   thorpej  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     59        1.1   thorpej  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     60        1.1   thorpej  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     61        1.1   thorpej  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     62        1.1   thorpej  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     63        1.1   thorpej  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     64        1.1   thorpej  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65        1.1   thorpej  * SUCH DAMAGE.
     66        1.1   thorpej  *
     67        1.1   thorpej  * from FreeBSD: if_vlan.c,v 1.16 2000/03/26 15:21:40 charnier Exp
     68        1.1   thorpej  * via OpenBSD: if_vlan.c,v 1.4 2000/05/15 19:15:00 chris Exp
     69        1.1   thorpej  */
     70        1.1   thorpej 
     71        1.1   thorpej /*
     72        1.1   thorpej  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.  Might be
     73        1.1   thorpej  * extended some day to also handle IEEE 802.1P priority tagging.  This is
     74        1.1   thorpej  * sort of sneaky in the implementation, since we need to pretend to be
     75        1.1   thorpej  * enough of an Ethernet implementation to make ARP work.  The way we do
     76        1.1   thorpej  * this is by telling everyone that we are an Ethernet interface, and then
     77        1.1   thorpej  * catch the packets that ether_output() left on our output queue when it
     78        1.1   thorpej  * calls if_start(), rewrite them for use by the real outgoing interface,
     79        1.1   thorpej  * and ask it to send them.
     80        1.1   thorpej  *
     81        1.1   thorpej  * TODO:
     82        1.1   thorpej  *
     83        1.1   thorpej  *	- Need some way to notify vlan interfaces when the parent
     84        1.1   thorpej  *	  interface changes MTU.
     85        1.1   thorpej  */
     86       1.33     lukem 
     87       1.33     lukem #include <sys/cdefs.h>
     88  1.47.10.2      elad __KERNEL_RCSID(0, "$NetBSD: if_vlan.c,v 1.47.10.2 2006/03/10 15:05:23 elad Exp $");
     89        1.1   thorpej 
     90        1.1   thorpej #include "opt_inet.h"
     91        1.1   thorpej #include "bpfilter.h"
     92        1.1   thorpej 
     93        1.1   thorpej #include <sys/param.h>
     94        1.1   thorpej #include <sys/kernel.h>
     95        1.1   thorpej #include <sys/mbuf.h>
     96        1.1   thorpej #include <sys/queue.h>
     97        1.1   thorpej #include <sys/socket.h>
     98        1.1   thorpej #include <sys/sockio.h>
     99        1.1   thorpej #include <sys/systm.h>
    100        1.1   thorpej #include <sys/proc.h>
    101        1.1   thorpej 
    102        1.1   thorpej #if NBPFILTER > 0
    103        1.1   thorpej #include <net/bpf.h>
    104        1.1   thorpej #endif
    105        1.1   thorpej #include <net/if.h>
    106        1.1   thorpej #include <net/if_dl.h>
    107        1.1   thorpej #include <net/if_types.h>
    108        1.1   thorpej #include <net/if_ether.h>
    109        1.1   thorpej #include <net/if_vlanvar.h>
    110        1.1   thorpej 
    111        1.1   thorpej #ifdef INET
    112        1.1   thorpej #include <netinet/in.h>
    113        1.1   thorpej #include <netinet/if_inarp.h>
    114        1.1   thorpej #endif
    115        1.1   thorpej 
    116       1.10   thorpej struct vlan_mc_entry {
    117       1.10   thorpej 	LIST_ENTRY(vlan_mc_entry)	mc_entries;
    118       1.10   thorpej 	/*
    119       1.10   thorpej 	 * A key to identify this entry.  The mc_addr below can't be
    120       1.10   thorpej 	 * used since multiple sockaddr may mapped into the same
    121       1.10   thorpej 	 * ether_multi (e.g., AF_UNSPEC).
    122       1.10   thorpej 	 */
    123       1.10   thorpej 	union {
    124       1.10   thorpej 		struct ether_multi	*mcu_enm;
    125       1.10   thorpej 	} mc_u;
    126       1.10   thorpej 	struct sockaddr_storage		mc_addr;
    127       1.10   thorpej };
    128       1.10   thorpej 
    129       1.10   thorpej #define	mc_enm		mc_u.mcu_enm
    130       1.10   thorpej 
    131       1.10   thorpej struct ifvlan {
    132       1.10   thorpej 	union {
    133       1.10   thorpej 		struct ethercom ifvu_ec;
    134       1.10   thorpej 	} ifv_u;
    135       1.10   thorpej 	struct ifnet *ifv_p;	/* parent interface of this vlan */
    136       1.10   thorpej 	struct ifv_linkmib {
    137       1.10   thorpej 		const struct vlan_multisw *ifvm_msw;
    138       1.10   thorpej 		int	ifvm_encaplen;	/* encapsulation length */
    139       1.10   thorpej 		int	ifvm_mtufudge;	/* MTU fudged by this much */
    140       1.10   thorpej 		int	ifvm_mintu;	/* min transmission unit */
    141       1.10   thorpej 		u_int16_t ifvm_proto;	/* encapsulation ethertype */
    142       1.10   thorpej 		u_int16_t ifvm_tag;	/* tag to apply on packets */
    143       1.10   thorpej 	} ifv_mib;
    144       1.10   thorpej 	LIST_HEAD(__vlan_mchead, vlan_mc_entry) ifv_mc_listhead;
    145       1.10   thorpej 	LIST_ENTRY(ifvlan) ifv_list;
    146       1.17   thorpej 	int ifv_flags;
    147       1.10   thorpej };
    148       1.10   thorpej 
    149       1.17   thorpej #define	IFVF_PROMISC	0x01		/* promiscuous mode enabled */
    150       1.17   thorpej 
    151       1.10   thorpej #define	ifv_ec		ifv_u.ifvu_ec
    152       1.10   thorpej 
    153       1.10   thorpej #define	ifv_if		ifv_ec.ec_if
    154       1.10   thorpej 
    155       1.10   thorpej #define	ifv_msw		ifv_mib.ifvm_msw
    156       1.10   thorpej #define	ifv_encaplen	ifv_mib.ifvm_encaplen
    157       1.10   thorpej #define	ifv_mtufudge	ifv_mib.ifvm_mtufudge
    158       1.10   thorpej #define	ifv_mintu	ifv_mib.ifvm_mintu
    159       1.10   thorpej #define	ifv_tag		ifv_mib.ifvm_tag
    160       1.10   thorpej 
    161       1.10   thorpej struct vlan_multisw {
    162       1.10   thorpej 	int	(*vmsw_addmulti)(struct ifvlan *, struct ifreq *);
    163       1.10   thorpej 	int	(*vmsw_delmulti)(struct ifvlan *, struct ifreq *);
    164       1.10   thorpej 	void	(*vmsw_purgemulti)(struct ifvlan *);
    165       1.10   thorpej };
    166       1.10   thorpej 
    167       1.10   thorpej static int	vlan_ether_addmulti(struct ifvlan *, struct ifreq *);
    168       1.10   thorpej static int	vlan_ether_delmulti(struct ifvlan *, struct ifreq *);
    169       1.10   thorpej static void	vlan_ether_purgemulti(struct ifvlan *);
    170       1.10   thorpej 
    171       1.10   thorpej const struct vlan_multisw vlan_ether_multisw = {
    172       1.10   thorpej 	vlan_ether_addmulti,
    173       1.10   thorpej 	vlan_ether_delmulti,
    174       1.10   thorpej 	vlan_ether_purgemulti,
    175       1.10   thorpej };
    176       1.10   thorpej 
    177        1.1   thorpej static int	vlan_clone_create(struct if_clone *, int);
    178       1.42     peter static int	vlan_clone_destroy(struct ifnet *);
    179        1.1   thorpej static int	vlan_config(struct ifvlan *, struct ifnet *);
    180        1.1   thorpej static int	vlan_ioctl(struct ifnet *, u_long, caddr_t);
    181        1.1   thorpej static void	vlan_start(struct ifnet *);
    182       1.11   thorpej static void	vlan_unconfig(struct ifnet *);
    183       1.10   thorpej 
    184       1.10   thorpej void		vlanattach(int);
    185        1.1   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.1   thorpej struct if_clone vlan_cloner =
    190        1.1   thorpej     IF_CLONE_INITIALIZER("vlan", vlan_clone_create, vlan_clone_destroy);
    191        1.1   thorpej 
    192       1.38       scw /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
    193       1.38       scw static char vlan_zero_pad_buff[ETHER_MIN_LEN];
    194       1.38       scw 
    195        1.1   thorpej void
    196        1.1   thorpej vlanattach(int n)
    197        1.1   thorpej {
    198        1.1   thorpej 
    199        1.1   thorpej 	LIST_INIT(&ifv_list);
    200        1.1   thorpej 	if_clone_attach(&vlan_cloner);
    201        1.1   thorpej }
    202        1.1   thorpej 
    203       1.30   thorpej static void
    204       1.30   thorpej vlan_reset_linkname(struct ifnet *ifp)
    205       1.30   thorpej {
    206       1.30   thorpej 
    207       1.30   thorpej 	/*
    208       1.30   thorpej 	 * We start out with a "802.1Q VLAN" type and zero-length
    209       1.30   thorpej 	 * addresses.  When we attach to a parent interface, we
    210       1.30   thorpej 	 * inherit its type, address length, address, and data link
    211       1.30   thorpej 	 * type.
    212       1.30   thorpej 	 */
    213       1.30   thorpej 
    214       1.30   thorpej 	ifp->if_type = IFT_L2VLAN;
    215       1.30   thorpej 	ifp->if_addrlen = 0;
    216       1.30   thorpej 	ifp->if_dlt = DLT_NULL;
    217       1.30   thorpej 	if_alloc_sadl(ifp);
    218       1.30   thorpej }
    219       1.30   thorpej 
    220        1.1   thorpej static int
    221        1.1   thorpej vlan_clone_create(struct if_clone *ifc, int unit)
    222        1.1   thorpej {
    223        1.1   thorpej 	struct ifvlan *ifv;
    224        1.1   thorpej 	struct ifnet *ifp;
    225       1.11   thorpej 	int s;
    226        1.1   thorpej 
    227       1.10   thorpej 	ifv = malloc(sizeof(struct ifvlan), M_DEVBUF, M_WAITOK);
    228        1.1   thorpej 	memset(ifv, 0, sizeof(struct ifvlan));
    229       1.14     enami 	ifp = &ifv->ifv_if;
    230        1.5     enami 	LIST_INIT(&ifv->ifv_mc_listhead);
    231       1.11   thorpej 
    232       1.11   thorpej 	s = splnet();
    233        1.1   thorpej 	LIST_INSERT_HEAD(&ifv_list, ifv, ifv_list);
    234       1.11   thorpej 	splx(s);
    235        1.1   thorpej 
    236       1.39    itojun 	snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d", ifc->ifc_name,
    237       1.39    itojun 	    unit);
    238        1.1   thorpej 	ifp->if_softc = ifv;
    239        1.1   thorpej 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    240        1.1   thorpej 	ifp->if_start = vlan_start;
    241        1.1   thorpej 	ifp->if_ioctl = vlan_ioctl;
    242       1.31   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    243        1.1   thorpej 
    244        1.1   thorpej 	if_attach(ifp);
    245       1.30   thorpej 	vlan_reset_linkname(ifp);
    246        1.1   thorpej 
    247        1.1   thorpej 	return (0);
    248        1.1   thorpej }
    249        1.1   thorpej 
    250       1.42     peter static int
    251        1.1   thorpej vlan_clone_destroy(struct ifnet *ifp)
    252        1.1   thorpej {
    253       1.11   thorpej 	struct ifvlan *ifv = ifp->if_softc;
    254        1.1   thorpej 	int s;
    255        1.1   thorpej 
    256       1.11   thorpej 	s = splnet();
    257        1.1   thorpej 	LIST_REMOVE(ifv, ifv_list);
    258        1.1   thorpej 	vlan_unconfig(ifp);
    259       1.11   thorpej 	splx(s);
    260        1.1   thorpej 
    261        1.1   thorpej 	if_detach(ifp);
    262        1.1   thorpej 	free(ifv, M_DEVBUF);
    263       1.42     peter 
    264       1.42     peter 	return (0);
    265        1.1   thorpej }
    266        1.1   thorpej 
    267       1.11   thorpej /*
    268       1.11   thorpej  * Configure a VLAN interface.  Must be called at splnet().
    269       1.11   thorpej  */
    270        1.1   thorpej static int
    271        1.1   thorpej vlan_config(struct ifvlan *ifv, struct ifnet *p)
    272        1.1   thorpej {
    273       1.10   thorpej 	struct ifnet *ifp = &ifv->ifv_if;
    274       1.10   thorpej 	int error;
    275        1.1   thorpej 
    276        1.1   thorpej 	if (ifv->ifv_p != NULL)
    277        1.1   thorpej 		return (EBUSY);
    278       1.10   thorpej 
    279       1.10   thorpej 	switch (p->if_type) {
    280       1.10   thorpej 	case IFT_ETHER:
    281       1.10   thorpej 	    {
    282       1.10   thorpej 		struct ethercom *ec = (void *) p;
    283       1.10   thorpej 
    284       1.10   thorpej 		ifv->ifv_msw = &vlan_ether_multisw;
    285       1.10   thorpej 		ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
    286       1.10   thorpej 		ifv->ifv_mintu = ETHERMIN;
    287       1.10   thorpej 
    288       1.10   thorpej 		/*
    289       1.10   thorpej 		 * If the parent supports the VLAN_MTU capability,
    290       1.10   thorpej 		 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
    291       1.10   thorpej 		 * enable it.
    292       1.10   thorpej 		 */
    293       1.10   thorpej 		if (ec->ec_nvlans++ == 0 &&
    294       1.10   thorpej 		    (ec->ec_capabilities & ETHERCAP_VLAN_MTU) != 0) {
    295       1.10   thorpej 			/*
    296       1.10   thorpej 			 * Enable Tx/Rx of VLAN-sized frames.
    297       1.10   thorpej 			 */
    298       1.10   thorpej 			ec->ec_capenable |= ETHERCAP_VLAN_MTU;
    299       1.10   thorpej 			if (p->if_flags & IFF_UP) {
    300       1.10   thorpej 				struct ifreq ifr;
    301       1.10   thorpej 
    302       1.10   thorpej 				ifr.ifr_flags = p->if_flags;
    303       1.10   thorpej 				error = (*p->if_ioctl)(p, SIOCSIFFLAGS,
    304       1.10   thorpej 				    (caddr_t) &ifr);
    305       1.10   thorpej 				if (error) {
    306       1.10   thorpej 					if (ec->ec_nvlans-- == 1)
    307       1.10   thorpej 						ec->ec_capenable &=
    308       1.10   thorpej 						    ~ETHERCAP_VLAN_MTU;
    309       1.10   thorpej 					return (error);
    310       1.10   thorpej 				}
    311       1.10   thorpej 			}
    312       1.10   thorpej 			ifv->ifv_mtufudge = 0;
    313       1.10   thorpej 		} else if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0) {
    314       1.10   thorpej 			/*
    315       1.10   thorpej 			 * Fudge the MTU by the encapsulation size.  This
    316       1.10   thorpej 			 * makes us incompatible with strictly compliant
    317       1.10   thorpej 			 * 802.1Q implementations, but allows us to use
    318       1.10   thorpej 			 * the feature with other NetBSD implementations,
    319       1.10   thorpej 			 * which might still be useful.
    320       1.10   thorpej 			 */
    321       1.10   thorpej 			ifv->ifv_mtufudge = ifv->ifv_encaplen;
    322       1.10   thorpej 		}
    323       1.10   thorpej 
    324       1.10   thorpej 		/*
    325       1.32   thorpej 		 * If the parent interface can do hardware-assisted
    326       1.32   thorpej 		 * VLAN encapsulation, then propagate its hardware-
    327       1.32   thorpej 		 * assisted checksumming flags.
    328       1.32   thorpej 		 */
    329       1.32   thorpej 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING)
    330       1.32   thorpej 			ifp->if_capabilities = p->if_capabilities &
    331       1.46      yamt 			    (IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx|
    332       1.46      yamt 			     IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx|
    333       1.46      yamt 			     IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx|
    334       1.46      yamt 			     IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx|
    335       1.46      yamt 			     IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx);
    336       1.32   thorpej 
    337       1.32   thorpej 		/*
    338       1.10   thorpej 		 * We inherit the parent's Ethernet address.
    339       1.10   thorpej 		 */
    340       1.10   thorpej 		ether_ifattach(ifp, LLADDR(p->if_sadl));
    341       1.10   thorpej 		ifp->if_hdrlen = sizeof(struct ether_vlan_header); /* XXX? */
    342       1.10   thorpej 		break;
    343       1.10   thorpej 	    }
    344       1.10   thorpej 
    345       1.10   thorpej 	default:
    346       1.10   thorpej 		return (EPROTONOSUPPORT);
    347       1.10   thorpej 	}
    348       1.10   thorpej 
    349        1.1   thorpej 	ifv->ifv_p = p;
    350       1.10   thorpej 	ifv->ifv_if.if_mtu = p->if_mtu - ifv->ifv_mtufudge;
    351       1.21    bouyer 	ifv->ifv_if.if_flags = p->if_flags &
    352       1.23    bouyer 	    (IFF_UP | IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
    353        1.1   thorpej 
    354        1.1   thorpej 	/*
    355       1.10   thorpej 	 * Inherit the if_type from the parent.  This allows us
    356       1.10   thorpej 	 * to participate in bridges of that type.
    357        1.1   thorpej 	 */
    358       1.10   thorpej 	ifv->ifv_if.if_type = p->if_type;
    359       1.10   thorpej 
    360        1.1   thorpej 	return (0);
    361        1.1   thorpej }
    362        1.1   thorpej 
    363       1.11   thorpej /*
    364       1.11   thorpej  * Unconfigure a VLAN interface.  Must be called at splnet().
    365       1.11   thorpej  */
    366       1.11   thorpej static void
    367        1.1   thorpej vlan_unconfig(struct ifnet *ifp)
    368        1.1   thorpej {
    369       1.10   thorpej 	struct ifvlan *ifv = ifp->if_softc;
    370        1.1   thorpej 
    371        1.7     enami 	if (ifv->ifv_p == NULL)
    372       1.11   thorpej 		return;
    373        1.1   thorpej 
    374        1.1   thorpej 	/*
    375        1.1   thorpej  	 * Since the interface is being unconfigured, we need to empty the
    376        1.1   thorpej 	 * list of multicast groups that we may have joined while we were
    377        1.1   thorpej 	 * alive and remove them from the parent's list also.
    378        1.1   thorpej 	 */
    379       1.10   thorpej 	(*ifv->ifv_msw->vmsw_purgemulti)(ifv);
    380        1.1   thorpej 
    381        1.1   thorpej 	/* Disconnect from parent. */
    382       1.10   thorpej 	switch (ifv->ifv_p->if_type) {
    383       1.10   thorpej 	case IFT_ETHER:
    384       1.10   thorpej 	    {
    385       1.10   thorpej 		struct ethercom *ec = (void *) ifv->ifv_p;
    386       1.10   thorpej 
    387       1.10   thorpej 		if (ec->ec_nvlans-- == 1) {
    388       1.10   thorpej 			/*
    389       1.10   thorpej 			 * Disable Tx/Rx of VLAN-sized frames.
    390       1.10   thorpej 			 */
    391       1.10   thorpej 			ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
    392       1.10   thorpej 			if (ifv->ifv_p->if_flags & IFF_UP) {
    393       1.10   thorpej 				struct ifreq ifr;
    394       1.10   thorpej 
    395       1.10   thorpej 				ifr.ifr_flags = ifv->ifv_p->if_flags;
    396       1.10   thorpej 				(void) (*ifv->ifv_p->if_ioctl)(ifv->ifv_p,
    397       1.10   thorpej 				    SIOCSIFFLAGS, (caddr_t) &ifr);
    398       1.10   thorpej 			}
    399       1.10   thorpej 		}
    400       1.10   thorpej 
    401       1.10   thorpej 		ether_ifdetach(ifp);
    402       1.30   thorpej 		vlan_reset_linkname(ifp);
    403       1.10   thorpej 		break;
    404       1.10   thorpej 	    }
    405       1.10   thorpej 
    406       1.10   thorpej #ifdef DIAGNOSTIC
    407       1.10   thorpej 	default:
    408       1.10   thorpej 		panic("vlan_unconfig: impossible");
    409       1.10   thorpej #endif
    410       1.10   thorpej 	}
    411       1.10   thorpej 
    412        1.1   thorpej 	ifv->ifv_p = NULL;
    413       1.10   thorpej 	ifv->ifv_if.if_mtu = 0;
    414       1.17   thorpej 	ifv->ifv_flags = 0;
    415        1.1   thorpej 
    416       1.11   thorpej 	if_down(ifp);
    417       1.11   thorpej 	ifp->if_flags &= ~(IFF_UP|IFF_RUNNING);
    418       1.32   thorpej 	ifp->if_capabilities = 0;
    419       1.11   thorpej }
    420       1.11   thorpej 
    421       1.11   thorpej /*
    422       1.11   thorpej  * Called when a parent interface is detaching; destroy any VLAN
    423       1.11   thorpej  * configuration for the parent interface.
    424       1.11   thorpej  */
    425       1.11   thorpej void
    426       1.11   thorpej vlan_ifdetach(struct ifnet *p)
    427       1.11   thorpej {
    428       1.11   thorpej 	struct ifvlan *ifv;
    429       1.11   thorpej 	int s;
    430       1.11   thorpej 
    431       1.11   thorpej 	s = splnet();
    432       1.11   thorpej 
    433       1.11   thorpej 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    434       1.11   thorpej 	     ifv = LIST_NEXT(ifv, ifv_list)) {
    435       1.11   thorpej 		if (ifv->ifv_p == p)
    436       1.11   thorpej 			vlan_unconfig(&ifv->ifv_if);
    437       1.11   thorpej 	}
    438       1.11   thorpej 
    439        1.1   thorpej 	splx(s);
    440        1.1   thorpej }
    441        1.1   thorpej 
    442        1.1   thorpej static int
    443       1.17   thorpej vlan_set_promisc(struct ifnet *ifp)
    444       1.17   thorpej {
    445       1.17   thorpej 	struct ifvlan *ifv = ifp->if_softc;
    446       1.20     enami 	int error = 0;
    447       1.17   thorpej 
    448       1.17   thorpej 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    449       1.17   thorpej 		if ((ifv->ifv_flags & IFVF_PROMISC) == 0) {
    450       1.17   thorpej 			error = ifpromisc(ifv->ifv_p, 1);
    451       1.17   thorpej 			if (error == 0)
    452       1.17   thorpej 				ifv->ifv_flags |= IFVF_PROMISC;
    453       1.17   thorpej 		}
    454       1.17   thorpej 	} else {
    455       1.17   thorpej 		if ((ifv->ifv_flags & IFVF_PROMISC) != 0) {
    456       1.17   thorpej 			error = ifpromisc(ifv->ifv_p, 0);
    457       1.17   thorpej 			if (error == 0)
    458       1.17   thorpej 				ifv->ifv_flags &= ~IFVF_PROMISC;
    459       1.17   thorpej 		}
    460       1.17   thorpej 	}
    461       1.17   thorpej 
    462       1.17   thorpej 	return (error);
    463       1.17   thorpej }
    464       1.17   thorpej 
    465       1.17   thorpej static int
    466        1.1   thorpej vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    467        1.1   thorpej {
    468        1.1   thorpej 	struct proc *p = curproc;	/* XXX */
    469       1.11   thorpej 	struct ifvlan *ifv = ifp->if_softc;
    470       1.11   thorpej 	struct ifaddr *ifa = (struct ifaddr *) data;
    471       1.11   thorpej 	struct ifreq *ifr = (struct ifreq *) data;
    472        1.1   thorpej 	struct ifnet *pr;
    473        1.1   thorpej 	struct vlanreq vlr;
    474        1.1   thorpej 	struct sockaddr *sa;
    475       1.11   thorpej 	int s, error = 0;
    476        1.1   thorpej 
    477       1.11   thorpej 	s = splnet();
    478        1.1   thorpej 
    479        1.1   thorpej 	switch (cmd) {
    480        1.1   thorpej 	case SIOCSIFADDR:
    481       1.16    bouyer 		if (ifv->ifv_p != NULL) {
    482       1.16    bouyer 			ifp->if_flags |= IFF_UP;
    483        1.1   thorpej 
    484       1.16    bouyer 			switch (ifa->ifa_addr->sa_family) {
    485        1.1   thorpej #ifdef INET
    486       1.16    bouyer 			case AF_INET:
    487       1.16    bouyer 				arp_ifinit(ifp, ifa);
    488       1.16    bouyer 				break;
    489        1.1   thorpej #endif
    490       1.16    bouyer 			default:
    491       1.16    bouyer 				break;
    492       1.16    bouyer 			}
    493       1.16    bouyer 		} else {
    494       1.16    bouyer 			error = EINVAL;
    495        1.1   thorpej 		}
    496        1.1   thorpej 		break;
    497        1.1   thorpej 
    498        1.1   thorpej 	case SIOCGIFADDR:
    499        1.1   thorpej 		sa = (struct sockaddr *)&ifr->ifr_data;
    500       1.28   thorpej 		memcpy(sa->sa_data, LLADDR(ifp->if_sadl), ifp->if_addrlen);
    501        1.1   thorpej 		break;
    502        1.1   thorpej 
    503        1.1   thorpej 	case SIOCSIFMTU:
    504        1.1   thorpej 		if (ifv->ifv_p != NULL) {
    505       1.10   thorpej 			if (ifr->ifr_mtu >
    506       1.10   thorpej 			     (ifv->ifv_p->if_mtu - ifv->ifv_mtufudge) ||
    507       1.10   thorpej 			    ifr->ifr_mtu <
    508       1.10   thorpej 			     (ifv->ifv_mintu - ifv->ifv_mtufudge))
    509        1.1   thorpej 				error = EINVAL;
    510        1.1   thorpej 			else
    511        1.1   thorpej 				ifp->if_mtu = ifr->ifr_mtu;
    512        1.1   thorpej 		} else
    513        1.1   thorpej 			error = EINVAL;
    514        1.1   thorpej 		break;
    515        1.1   thorpej 
    516        1.1   thorpej 	case SIOCSETVLAN:
    517  1.47.10.2      elad 		if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
    518        1.1   thorpej 			break;
    519        1.1   thorpej 		if ((error = copyin(ifr->ifr_data, &vlr, sizeof(vlr))) != 0)
    520        1.1   thorpej 			break;
    521        1.1   thorpej 		if (vlr.vlr_parent[0] == '\0') {
    522        1.1   thorpej 			vlan_unconfig(ifp);
    523        1.1   thorpej 			break;
    524        1.1   thorpej 		}
    525        1.1   thorpej 		if (vlr.vlr_tag != EVL_VLANOFTAG(vlr.vlr_tag)) {
    526        1.1   thorpej 			error = EINVAL;		 /* check for valid tag */
    527        1.1   thorpej 			break;
    528        1.1   thorpej 		}
    529        1.1   thorpej 		if ((pr = ifunit(vlr.vlr_parent)) == 0) {
    530        1.1   thorpej 			error = ENOENT;
    531        1.1   thorpej 			break;
    532        1.1   thorpej 		}
    533        1.1   thorpej 		if ((error = vlan_config(ifv, pr)) != 0)
    534        1.1   thorpej 			break;
    535        1.1   thorpej 		ifv->ifv_tag = vlr.vlr_tag;
    536        1.1   thorpej 		ifp->if_flags |= IFF_RUNNING;
    537       1.17   thorpej 
    538       1.17   thorpej 		/* Update promiscuous mode, if necessary. */
    539       1.17   thorpej 		vlan_set_promisc(ifp);
    540        1.1   thorpej 		break;
    541        1.1   thorpej 
    542        1.1   thorpej 	case SIOCGETVLAN:
    543        1.1   thorpej 		memset(&vlr, 0, sizeof(vlr));
    544        1.1   thorpej 		if (ifv->ifv_p != NULL) {
    545        1.1   thorpej 			snprintf(vlr.vlr_parent, sizeof(vlr.vlr_parent), "%s",
    546        1.1   thorpej 			    ifv->ifv_p->if_xname);
    547        1.1   thorpej 			vlr.vlr_tag = ifv->ifv_tag;
    548        1.1   thorpej 		}
    549        1.1   thorpej 		error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
    550        1.1   thorpej 		break;
    551        1.1   thorpej 
    552        1.1   thorpej 	case SIOCSIFFLAGS:
    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.1   thorpej 	default:
    572        1.1   thorpej 		error = EINVAL;
    573        1.1   thorpej 	}
    574       1.11   thorpej 
    575       1.11   thorpej 	splx(s);
    576        1.1   thorpej 
    577        1.1   thorpej 	return (error);
    578        1.1   thorpej }
    579        1.1   thorpej 
    580        1.1   thorpej static int
    581       1.10   thorpej vlan_ether_addmulti(struct ifvlan *ifv, struct ifreq *ifr)
    582        1.1   thorpej {
    583        1.1   thorpej 	struct vlan_mc_entry *mc;
    584        1.5     enami 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    585        1.1   thorpej 	int error;
    586        1.1   thorpej 
    587        1.5     enami 	if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr_storage))
    588        1.5     enami 		return (EINVAL);
    589        1.5     enami 
    590        1.5     enami 	error = ether_addmulti(ifr, &ifv->ifv_ec);
    591        1.5     enami 	if (error != ENETRESET)
    592        1.5     enami 		return (error);
    593        1.1   thorpej 
    594        1.5     enami 	/*
    595        1.5     enami 	 * This is new multicast address.  We have to tell parent
    596        1.5     enami 	 * about it.  Also, remember this multicast address so that
    597        1.5     enami 	 * we can delete them on unconfigure.
    598        1.5     enami 	 */
    599        1.5     enami 	MALLOC(mc, struct vlan_mc_entry *, sizeof(struct vlan_mc_entry),
    600        1.5     enami 	    M_DEVBUF, M_NOWAIT);
    601        1.5     enami 	if (mc == NULL) {
    602        1.5     enami 		error = ENOMEM;
    603        1.5     enami 		goto alloc_failed;
    604        1.1   thorpej 	}
    605        1.1   thorpej 
    606        1.5     enami 	/*
    607        1.5     enami 	 * As ether_addmulti() returns ENETRESET, following two
    608        1.5     enami 	 * statement shouldn't fail.
    609        1.5     enami 	 */
    610        1.5     enami 	(void)ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
    611        1.5     enami 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, mc->mc_enm);
    612        1.5     enami 	memcpy(&mc->mc_addr, &ifr->ifr_addr, ifr->ifr_addr.sa_len);
    613        1.5     enami 	LIST_INSERT_HEAD(&ifv->ifv_mc_listhead, mc, mc_entries);
    614        1.5     enami 
    615        1.5     enami 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCADDMULTI,
    616        1.5     enami 	    (caddr_t)ifr);
    617        1.5     enami 	if (error != 0)
    618        1.5     enami 		goto ioctl_failed;
    619        1.5     enami 	return (error);
    620        1.5     enami 
    621        1.5     enami  ioctl_failed:
    622        1.5     enami 	LIST_REMOVE(mc, mc_entries);
    623        1.5     enami 	FREE(mc, M_DEVBUF);
    624        1.5     enami  alloc_failed:
    625        1.5     enami 	(void)ether_delmulti(ifr, &ifv->ifv_ec);
    626        1.5     enami 	return (error);
    627        1.5     enami }
    628        1.5     enami 
    629        1.5     enami static int
    630       1.10   thorpej vlan_ether_delmulti(struct ifvlan *ifv, struct ifreq *ifr)
    631        1.5     enami {
    632        1.5     enami 	struct ether_multi *enm;
    633        1.5     enami 	struct vlan_mc_entry *mc;
    634        1.5     enami 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
    635        1.5     enami 	int error;
    636        1.5     enami 
    637        1.5     enami 	/*
    638        1.5     enami 	 * Find a key to lookup vlan_mc_entry.  We have to do this
    639        1.5     enami 	 * before calling ether_delmulti for obvious reason.
    640        1.5     enami 	 */
    641        1.5     enami 	if ((error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi)) != 0)
    642        1.5     enami 		return (error);
    643        1.5     enami 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &ifv->ifv_ec, enm);
    644        1.5     enami 
    645        1.5     enami 	error = ether_delmulti(ifr, &ifv->ifv_ec);
    646        1.5     enami 	if (error != ENETRESET)
    647        1.5     enami 		return (error);
    648        1.5     enami 
    649        1.5     enami 	/* We no longer use this multicast address.  Tell parent so. */
    650        1.5     enami 	error = (*ifv->ifv_p->if_ioctl)(ifv->ifv_p, SIOCDELMULTI,
    651        1.5     enami 	    (caddr_t)ifr);
    652        1.5     enami 	if (error == 0) {
    653        1.5     enami 		/* And forget about this address. */
    654        1.5     enami 		for (mc = LIST_FIRST(&ifv->ifv_mc_listhead); mc != NULL;
    655        1.5     enami 		    mc = LIST_NEXT(mc, mc_entries)) {
    656        1.5     enami 			if (mc->mc_enm == enm) {
    657        1.5     enami 				LIST_REMOVE(mc, mc_entries);
    658        1.5     enami 				FREE(mc, M_DEVBUF);
    659        1.5     enami 				break;
    660        1.5     enami 			}
    661        1.5     enami 		}
    662        1.5     enami 		KASSERT(mc != NULL);
    663        1.5     enami 	} else
    664        1.5     enami 		(void)ether_addmulti(ifr, &ifv->ifv_ec);
    665        1.5     enami 	return (error);
    666        1.5     enami }
    667        1.5     enami 
    668        1.5     enami /*
    669       1.34     pooka  * Delete any multicast address we have asked to add from parent
    670        1.5     enami  * interface.  Called when the vlan is being unconfigured.
    671        1.5     enami  */
    672        1.5     enami static void
    673       1.10   thorpej vlan_ether_purgemulti(struct ifvlan *ifv)
    674        1.5     enami {
    675        1.5     enami 	struct ifnet *ifp = ifv->ifv_p;		/* Parent. */
    676        1.5     enami 	struct vlan_mc_entry *mc;
    677        1.5     enami 	union {
    678        1.5     enami 		struct ifreq ifreq;
    679        1.5     enami 		struct {
    680        1.5     enami 			char ifr_name[IFNAMSIZ];
    681       1.18     enami 			struct sockaddr_storage ifr_ss;
    682        1.5     enami 		} ifreq_storage;
    683        1.5     enami 	} ifreq;
    684        1.5     enami 	struct ifreq *ifr = &ifreq.ifreq;
    685        1.5     enami 
    686        1.5     enami 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
    687        1.5     enami 	while ((mc = LIST_FIRST(&ifv->ifv_mc_listhead)) != NULL) {
    688        1.5     enami 		memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len);
    689        1.5     enami 		(void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)ifr);
    690        1.5     enami 		LIST_REMOVE(mc, mc_entries);
    691        1.5     enami 		FREE(mc, M_DEVBUF);
    692        1.1   thorpej 	}
    693        1.1   thorpej }
    694        1.1   thorpej 
    695        1.1   thorpej static void
    696        1.1   thorpej vlan_start(struct ifnet *ifp)
    697        1.1   thorpej {
    698       1.14     enami 	struct ifvlan *ifv = ifp->if_softc;
    699       1.14     enami 	struct ifnet *p = ifv->ifv_p;
    700       1.24    bouyer 	struct ethercom *ec = (void *) ifv->ifv_p;
    701        1.1   thorpej 	struct mbuf *m;
    702       1.31   thorpej 	int error;
    703       1.31   thorpej 	ALTQ_DECL(struct altq_pktattr pktattr;)
    704        1.1   thorpej 
    705        1.1   thorpej 	ifp->if_flags |= IFF_OACTIVE;
    706        1.1   thorpej 
    707        1.1   thorpej 	for (;;) {
    708       1.31   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m);
    709        1.1   thorpej 		if (m == NULL)
    710        1.1   thorpej 			break;
    711        1.1   thorpej 
    712       1.31   thorpej #ifdef ALTQ
    713       1.31   thorpej 		/*
    714       1.31   thorpej 		 * If ALTQ is enabled on the parent interface, do
    715       1.31   thorpej 		 * classification; the queueing discipline might
    716       1.31   thorpej 		 * not require classification, but might require
    717       1.31   thorpej 		 * the address family/header pointer in the pktattr.
    718       1.31   thorpej 		 */
    719       1.31   thorpej 		if (ALTQ_IS_ENABLED(&p->if_snd)) {
    720       1.31   thorpej 			switch (p->if_type) {
    721       1.31   thorpej 			case IFT_ETHER:
    722       1.31   thorpej 				altq_etherclassify(&p->if_snd, m, &pktattr);
    723       1.31   thorpej 				break;
    724       1.31   thorpej #ifdef DIAGNOSTIC
    725       1.31   thorpej 			default:
    726       1.31   thorpej 				panic("vlan_start: impossible (altq)");
    727       1.31   thorpej #endif
    728       1.31   thorpej 			}
    729       1.31   thorpej 		}
    730       1.31   thorpej #endif /* ALTQ */
    731       1.31   thorpej 
    732        1.1   thorpej #if NBPFILTER > 0
    733        1.1   thorpej 		if (ifp->if_bpf)
    734        1.1   thorpej 			bpf_mtap(ifp->if_bpf, m);
    735        1.1   thorpej #endif
    736        1.1   thorpej 		/*
    737       1.24    bouyer 		 * If the parent can insert the tag itself, just mark
    738       1.24    bouyer 		 * the tag in the mbuf header.
    739        1.1   thorpej 		 */
    740       1.24    bouyer 		if (ec->ec_capabilities & ETHERCAP_VLAN_HWTAGGING) {
    741       1.35    itojun 			struct m_tag *mtag;
    742       1.35    itojun 
    743       1.35    itojun 			mtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
    744       1.35    itojun 			    M_NOWAIT);
    745       1.35    itojun 			if (mtag == NULL) {
    746       1.24    bouyer 				ifp->if_oerrors++;
    747       1.24    bouyer 				m_freem(m);
    748       1.24    bouyer 				continue;
    749       1.24    bouyer 			}
    750       1.35    itojun 			*(u_int *)(mtag + 1) = ifv->ifv_tag;
    751       1.36  drochner 			m_tag_prepend(m, mtag);
    752       1.24    bouyer 		} else {
    753       1.24    bouyer 			/*
    754       1.34     pooka 			 * insert the tag ourselves
    755       1.24    bouyer 			 */
    756       1.24    bouyer 			M_PREPEND(m, ifv->ifv_encaplen, M_DONTWAIT);
    757       1.24    bouyer 			if (m == NULL) {
    758       1.24    bouyer 				printf("%s: unable to prepend encap header",
    759       1.10   thorpej 				    ifv->ifv_p->if_xname);
    760       1.10   thorpej 				ifp->if_oerrors++;
    761       1.10   thorpej 				continue;
    762       1.10   thorpej 			}
    763       1.10   thorpej 
    764       1.24    bouyer 			switch (p->if_type) {
    765       1.24    bouyer 			case IFT_ETHER:
    766       1.24    bouyer 			    {
    767       1.24    bouyer 				struct ether_vlan_header *evl;
    768       1.24    bouyer 
    769       1.24    bouyer 				if (m->m_len < sizeof(struct ether_vlan_header))
    770       1.24    bouyer 					m = m_pullup(m,
    771       1.24    bouyer 					    sizeof(struct ether_vlan_header));
    772       1.24    bouyer 				if (m == NULL) {
    773       1.24    bouyer 					printf("%s: unable to pullup encap "
    774       1.24    bouyer 					    "header", ifv->ifv_p->if_xname);
    775       1.24    bouyer 					ifp->if_oerrors++;
    776       1.24    bouyer 					continue;
    777       1.24    bouyer 				}
    778       1.24    bouyer 
    779       1.24    bouyer 				/*
    780       1.24    bouyer 				 * Transform the Ethernet header into an
    781       1.24    bouyer 				 * Ethernet header with 802.1Q encapsulation.
    782       1.24    bouyer 				 */
    783       1.24    bouyer 				memmove(mtod(m, caddr_t),
    784       1.44     perry 				    mtod(m, caddr_t) + ifv->ifv_encaplen,
    785       1.24    bouyer 				    sizeof(struct ether_header));
    786       1.24    bouyer 				evl = mtod(m, struct ether_vlan_header *);
    787       1.24    bouyer 				evl->evl_proto = evl->evl_encap_proto;
    788       1.24    bouyer 				evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
    789       1.24    bouyer 				evl->evl_tag = htons(ifv->ifv_tag);
    790       1.38       scw 
    791       1.38       scw 				/*
    792       1.38       scw 				 * To cater for VLAN-aware layer 2 ethernet
    793       1.38       scw 				 * switches which may need to strip the tag
    794       1.38       scw 				 * before forwarding the packet, make sure
    795       1.38       scw 				 * the packet+tag is at least 68 bytes long.
    796       1.38       scw 				 * This is necessary because our parent will
    797       1.38       scw 				 * only pad to 64 bytes (ETHER_MIN_LEN) and
    798       1.38       scw 				 * some switches will not pad by themselves
    799       1.38       scw 				 * after deleting a tag.
    800       1.38       scw 				 */
    801       1.38       scw 				if (m->m_pkthdr.len <
    802       1.38       scw 				    (ETHER_MIN_LEN + ETHER_VLAN_ENCAP_LEN)) {
    803       1.38       scw 					m_copyback(m, m->m_pkthdr.len,
    804       1.38       scw 					    (ETHER_MIN_LEN +
    805       1.38       scw 					     ETHER_VLAN_ENCAP_LEN) -
    806       1.38       scw 					     m->m_pkthdr.len,
    807       1.38       scw 					    vlan_zero_pad_buff);
    808       1.38       scw 				}
    809       1.24    bouyer 				break;
    810       1.24    bouyer 			    }
    811       1.10   thorpej 
    812       1.10   thorpej #ifdef DIAGNOSTIC
    813       1.24    bouyer 			default:
    814       1.24    bouyer 				panic("vlan_start: impossible");
    815       1.10   thorpej #endif
    816       1.24    bouyer 			}
    817        1.1   thorpej 		}
    818        1.1   thorpej 
    819        1.1   thorpej 		/*
    820       1.10   thorpej 		 * Send it, precisely as the parent's output routine
    821       1.34     pooka 		 * would have.  We are already running at splnet.
    822        1.1   thorpej 		 */
    823       1.31   thorpej 		IFQ_ENQUEUE(&p->if_snd, m, &pktattr, error);
    824       1.31   thorpej 		if (error) {
    825       1.31   thorpej 			/* mbuf is already freed */
    826        1.1   thorpej 			ifp->if_oerrors++;
    827        1.1   thorpej 			continue;
    828        1.1   thorpej 		}
    829       1.31   thorpej 
    830       1.23    bouyer 		ifp->if_opackets++;
    831       1.41   mycroft 		if ((p->if_flags & (IFF_RUNNING|IFF_OACTIVE)) == IFF_RUNNING)
    832       1.14     enami 			(*p->if_start)(p);
    833        1.1   thorpej 	}
    834        1.1   thorpej 
    835        1.1   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    836        1.1   thorpej }
    837        1.1   thorpej 
    838        1.1   thorpej /*
    839        1.1   thorpej  * Given an Ethernet frame, find a valid vlan interface corresponding to the
    840       1.44     perry  * given source interface and tag, then run the real packet through the
    841       1.40    simonb  * parent's input routine.
    842        1.1   thorpej  */
    843        1.1   thorpej void
    844        1.1   thorpej vlan_input(struct ifnet *ifp, struct mbuf *m)
    845        1.1   thorpej {
    846        1.1   thorpej 	struct ifvlan *ifv;
    847        1.1   thorpej 	u_int tag;
    848       1.35    itojun 	struct m_tag *mtag;
    849       1.24    bouyer 
    850       1.35    itojun 	mtag = m_tag_find(m, PACKET_TAG_VLAN, NULL);
    851       1.35    itojun 	if (mtag != NULL) {
    852       1.35    itojun 		/* m contains a normal ethernet frame, the tag is in mtag */
    853       1.45      yamt 		tag = EVL_VLANOFTAG(*(u_int *)(mtag + 1));
    854       1.35    itojun 		m_tag_delete(m, mtag);
    855       1.24    bouyer 	} else {
    856       1.24    bouyer 		switch (ifp->if_type) {
    857       1.24    bouyer 		case IFT_ETHER:
    858       1.24    bouyer 		    {
    859       1.24    bouyer 			struct ether_vlan_header *evl;
    860       1.24    bouyer 
    861       1.24    bouyer 			if (m->m_len < sizeof(struct ether_vlan_header) &&
    862       1.24    bouyer 			    (m = m_pullup(m,
    863       1.24    bouyer 			     sizeof(struct ether_vlan_header))) == NULL) {
    864       1.24    bouyer 				printf("%s: no memory for VLAN header, "
    865       1.24    bouyer 				    "dropping packet.\n", ifp->if_xname);
    866       1.24    bouyer 				return;
    867       1.24    bouyer 			}
    868       1.24    bouyer 			evl = mtod(m, struct ether_vlan_header *);
    869       1.24    bouyer 			KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
    870       1.24    bouyer 
    871       1.24    bouyer 			tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
    872        1.1   thorpej 
    873       1.24    bouyer 			/*
    874       1.24    bouyer 			 * Restore the original ethertype.  We'll remove
    875       1.24    bouyer 			 * the encapsulation after we've found the vlan
    876       1.24    bouyer 			 * interface corresponding to the tag.
    877       1.24    bouyer 			 */
    878       1.24    bouyer 			evl->evl_encap_proto = evl->evl_proto;
    879       1.24    bouyer 			break;
    880       1.24    bouyer 		    }
    881       1.10   thorpej 
    882       1.24    bouyer 		default:
    883       1.24    bouyer 			tag = (u_int) -1;	/* XXX GCC */
    884       1.24    bouyer #ifdef DIAGNOSTIC
    885       1.24    bouyer 			panic("vlan_input: impossible");
    886       1.24    bouyer #endif
    887       1.10   thorpej 		}
    888       1.43  christos 	}
    889       1.10   thorpej 
    890       1.43  christos 	for (ifv = LIST_FIRST(&ifv_list); ifv != NULL;
    891       1.43  christos 	    ifv = LIST_NEXT(ifv, ifv_list))
    892       1.43  christos 		if (ifp == ifv->ifv_p && tag == ifv->ifv_tag)
    893       1.43  christos 			break;
    894        1.1   thorpej 
    895       1.10   thorpej 	if (ifv == NULL ||
    896       1.10   thorpej 	    (ifv->ifv_if.if_flags & (IFF_UP|IFF_RUNNING)) !=
    897       1.10   thorpej 	     (IFF_UP|IFF_RUNNING)) {
    898       1.37    itojun 		m_freem(m);
    899       1.14     enami 		ifp->if_noproto++;
    900        1.1   thorpej 		return;
    901        1.1   thorpej 	}
    902       1.43  christos 
    903       1.43  christos 	/*
    904       1.43  christos 	 * Now, remove the encapsulation header.  The original
    905       1.43  christos 	 * header has already been fixed up above.
    906       1.43  christos 	 */
    907       1.43  christos 	if (mtag == NULL) {
    908       1.43  christos 		memmove(mtod(m, caddr_t) + ifv->ifv_encaplen,
    909       1.43  christos 		    mtod(m, caddr_t), sizeof(struct ether_header));
    910       1.43  christos 		m_adj(m, ifv->ifv_encaplen);
    911       1.43  christos 	}
    912       1.43  christos 
    913        1.1   thorpej 	m->m_pkthdr.rcvif = &ifv->ifv_if;
    914        1.1   thorpej 	ifv->ifv_if.if_ipackets++;
    915        1.1   thorpej 
    916        1.1   thorpej #if NBPFILTER > 0
    917        1.1   thorpej 	if (ifv->ifv_if.if_bpf)
    918        1.1   thorpej 		bpf_mtap(ifv->ifv_if.if_bpf, m);
    919        1.1   thorpej #endif
    920        1.1   thorpej 
    921        1.1   thorpej 	/* Pass it back through the parent's input routine. */
    922        1.1   thorpej 	(*ifp->if_input)(&ifv->ifv_if, m);
    923        1.1   thorpej }
    924