The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/if_vlan.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright 1998 Massachusetts Institute of Technology
    3  *
    4  * Permission to use, copy, modify, and distribute this software and
    5  * its documentation for any purpose and without fee is hereby
    6  * granted, provided that both the above copyright notice and this
    7  * permission notice appear in all copies, that both the above
    8  * copyright notice and this permission notice appear in all
    9  * supporting documentation, and that the name of M.I.T. not be used
   10  * in advertising or publicity pertaining to distribution of the
   11  * software without specific, written prior permission.  M.I.T. makes
   12  * no representations about the suitability of this software for any
   13  * purpose.  It is provided "as is" without express or implied
   14  * warranty.
   15  * 
   16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
   17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
   18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
   20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 /*
   31  * if_vlan.c - pseudo-device driver for IEEE 802.1Q virtual LANs.
   32  * Might be extended some day to also handle IEEE 802.1p priority
   33  * tagging.  This is sort of sneaky in the implementation, since
   34  * we need to pretend to be enough of an Ethernet implementation
   35  * to make arp work.  The way we do this is by telling everyone
   36  * that we are an Ethernet, and then catch the packets that
   37  * ether_output() sends to us via if_transmit(), rewrite them for
   38  * use by the real outgoing interface, and ask it to send them.
   39  */
   40 
   41 #include <sys/cdefs.h>
   42 __FBSDID("$FreeBSD$");
   43 
   44 #include "opt_inet.h"
   45 #include "opt_vlan.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/kernel.h>
   49 #include <sys/lock.h>
   50 #include <sys/malloc.h>
   51 #include <sys/mbuf.h>
   52 #include <sys/module.h>
   53 #include <sys/rwlock.h>
   54 #include <sys/queue.h>
   55 #include <sys/socket.h>
   56 #include <sys/sockio.h>
   57 #include <sys/sysctl.h>
   58 #include <sys/systm.h>
   59 
   60 #include <net/bpf.h>
   61 #include <net/ethernet.h>
   62 #include <net/if.h>
   63 #include <net/if_clone.h>
   64 #include <net/if_dl.h>
   65 #include <net/if_types.h>
   66 #include <net/if_vlan_var.h>
   67 #include <net/vnet.h>
   68 
   69 #ifdef INET
   70 #include <netinet/in.h>
   71 #include <netinet/if_ether.h>
   72 #endif
   73 
   74 #define VLANNAME        "vlan"
   75 #define VLAN_DEF_HWIDTH 4
   76 #define VLAN_IFFLAGS    (IFF_BROADCAST | IFF_MULTICAST)
   77 
   78 #define UP_AND_RUNNING(ifp) \
   79     ((ifp)->if_flags & IFF_UP && (ifp)->if_drv_flags & IFF_DRV_RUNNING)
   80 
   81 LIST_HEAD(ifvlanhead, ifvlan);
   82 
   83 struct ifvlantrunk {
   84         struct  ifnet   *parent;        /* parent interface of this trunk */
   85         struct  rwlock  rw;
   86 #ifdef VLAN_ARRAY
   87 #define VLAN_ARRAY_SIZE (EVL_VLID_MASK + 1)
   88         struct  ifvlan  *vlans[VLAN_ARRAY_SIZE]; /* static table */
   89 #else
   90         struct  ifvlanhead *hash;       /* dynamic hash-list table */
   91         uint16_t        hmask;
   92         uint16_t        hwidth;
   93 #endif
   94         int             refcnt;
   95 };
   96 
   97 struct vlan_mc_entry {
   98         struct ether_addr               mc_addr;
   99         SLIST_ENTRY(vlan_mc_entry)      mc_entries;
  100 };
  101 
  102 struct  ifvlan {
  103         struct  ifvlantrunk *ifv_trunk;
  104         struct  ifnet *ifv_ifp;
  105 #define TRUNK(ifv)      ((ifv)->ifv_trunk)
  106 #define PARENT(ifv)     ((ifv)->ifv_trunk->parent)
  107         int     ifv_pflags;     /* special flags we have set on parent */
  108         struct  ifv_linkmib {
  109                 int     ifvm_encaplen;  /* encapsulation length */
  110                 int     ifvm_mtufudge;  /* MTU fudged by this much */
  111                 int     ifvm_mintu;     /* min transmission unit */
  112                 uint16_t ifvm_proto;    /* encapsulation ethertype */
  113                 uint16_t ifvm_tag;      /* tag to apply on packets leaving if */
  114         }       ifv_mib;
  115         SLIST_HEAD(, vlan_mc_entry) vlan_mc_listhead;
  116 #ifndef VLAN_ARRAY
  117         LIST_ENTRY(ifvlan) ifv_list;
  118 #endif
  119 };
  120 #define ifv_proto       ifv_mib.ifvm_proto
  121 #define ifv_tag         ifv_mib.ifvm_tag
  122 #define ifv_encaplen    ifv_mib.ifvm_encaplen
  123 #define ifv_mtufudge    ifv_mib.ifvm_mtufudge
  124 #define ifv_mintu       ifv_mib.ifvm_mintu
  125 
  126 /* Special flags we should propagate to parent. */
  127 static struct {
  128         int flag;
  129         int (*func)(struct ifnet *, int);
  130 } vlan_pflags[] = {
  131         {IFF_PROMISC, ifpromisc},
  132         {IFF_ALLMULTI, if_allmulti},
  133         {0, NULL}
  134 };
  135 
  136 SYSCTL_DECL(_net_link);
  137 SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW, 0, "IEEE 802.1Q VLAN");
  138 SYSCTL_NODE(_net_link_vlan, PF_LINK, link, CTLFLAG_RW, 0, "for consistency");
  139 
  140 static int soft_pad = 0;
  141 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW, &soft_pad, 0,
  142            "pad short frames before tagging");
  143 
  144 static MALLOC_DEFINE(M_VLAN, VLANNAME, "802.1Q Virtual LAN Interface");
  145 
  146 static eventhandler_tag ifdetach_tag;
  147 static eventhandler_tag iflladdr_tag;
  148 
  149 /*
  150  * We have a global mutex, that is used to serialize configuration
  151  * changes and isn't used in normal packet delivery.
  152  *
  153  * We also have a per-trunk rwlock, that is locked shared on packet
  154  * processing and exclusive when configuration is changed.
  155  *
  156  * The VLAN_ARRAY substitutes the dynamic hash with a static array
  157  * with 4096 entries. In theory this can give a boost in processing,
  158  * however on practice it does not. Probably this is because array
  159  * is too big to fit into CPU cache.
  160  */
  161 static struct mtx ifv_mtx;
  162 #define VLAN_LOCK_INIT()        mtx_init(&ifv_mtx, "vlan_global", NULL, MTX_DEF)
  163 #define VLAN_LOCK_DESTROY()     mtx_destroy(&ifv_mtx)
  164 #define VLAN_LOCK_ASSERT()      mtx_assert(&ifv_mtx, MA_OWNED)
  165 #define VLAN_LOCK()             mtx_lock(&ifv_mtx)
  166 #define VLAN_UNLOCK()           mtx_unlock(&ifv_mtx)
  167 #define TRUNK_LOCK_INIT(trunk)  rw_init(&(trunk)->rw, VLANNAME)
  168 #define TRUNK_LOCK_DESTROY(trunk) rw_destroy(&(trunk)->rw)
  169 #define TRUNK_LOCK(trunk)       rw_wlock(&(trunk)->rw)
  170 #define TRUNK_UNLOCK(trunk)     rw_wunlock(&(trunk)->rw)
  171 #define TRUNK_LOCK_ASSERT(trunk) rw_assert(&(trunk)->rw, RA_WLOCKED)
  172 #define TRUNK_RLOCK(trunk)      rw_rlock(&(trunk)->rw)
  173 #define TRUNK_RUNLOCK(trunk)    rw_runlock(&(trunk)->rw)
  174 #define TRUNK_LOCK_RASSERT(trunk) rw_assert(&(trunk)->rw, RA_RLOCKED)
  175 
  176 #ifndef VLAN_ARRAY
  177 static  void vlan_inithash(struct ifvlantrunk *trunk);
  178 static  void vlan_freehash(struct ifvlantrunk *trunk);
  179 static  int vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
  180 static  int vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv);
  181 static  void vlan_growhash(struct ifvlantrunk *trunk, int howmuch);
  182 static __inline struct ifvlan * vlan_gethash(struct ifvlantrunk *trunk,
  183         uint16_t tag);
  184 #endif
  185 static  void trunk_destroy(struct ifvlantrunk *trunk);
  186 
  187 static  void vlan_init(void *foo);
  188 static  void vlan_input(struct ifnet *ifp, struct mbuf *m);
  189 static  int vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr);
  190 static  void vlan_qflush(struct ifnet *ifp);
  191 static  int vlan_setflag(struct ifnet *ifp, int flag, int status,
  192     int (*func)(struct ifnet *, int));
  193 static  int vlan_setflags(struct ifnet *ifp, int status);
  194 static  int vlan_setmulti(struct ifnet *ifp);
  195 static  int vlan_transmit(struct ifnet *ifp, struct mbuf *m);
  196 static  void vlan_unconfig(struct ifnet *ifp);
  197 static  void vlan_unconfig_locked(struct ifnet *ifp, int departing);
  198 static  int vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag);
  199 static  void vlan_link_state(struct ifnet *ifp, int link);
  200 static  void vlan_capabilities(struct ifvlan *ifv);
  201 static  void vlan_trunk_capabilities(struct ifnet *ifp);
  202 
  203 static  struct ifnet *vlan_clone_match_ethertag(struct if_clone *,
  204     const char *, int *);
  205 static  int vlan_clone_match(struct if_clone *, const char *);
  206 static  int vlan_clone_create(struct if_clone *, char *, size_t, caddr_t);
  207 static  int vlan_clone_destroy(struct if_clone *, struct ifnet *);
  208 
  209 static  void vlan_ifdetach(void *arg, struct ifnet *ifp);
  210 static  void vlan_iflladdr(void *arg, struct ifnet *ifp);
  211 
  212 static  struct if_clone vlan_cloner = IFC_CLONE_INITIALIZER(VLANNAME, NULL,
  213     IF_MAXUNIT, NULL, vlan_clone_match, vlan_clone_create, vlan_clone_destroy);
  214 
  215 #ifdef VIMAGE
  216 static VNET_DEFINE(struct if_clone, vlan_cloner);
  217 #define V_vlan_cloner   VNET(vlan_cloner)
  218 #endif
  219 
  220 #ifndef VLAN_ARRAY
  221 #define HASH(n, m)      ((((n) >> 8) ^ ((n) >> 4) ^ (n)) & (m))
  222 
  223 static void
  224 vlan_inithash(struct ifvlantrunk *trunk)
  225 {
  226         int i, n;
  227         
  228         /*
  229          * The trunk must not be locked here since we call malloc(M_WAITOK).
  230          * It is OK in case this function is called before the trunk struct
  231          * gets hooked up and becomes visible from other threads.
  232          */
  233 
  234         KASSERT(trunk->hwidth == 0 && trunk->hash == NULL,
  235             ("%s: hash already initialized", __func__));
  236 
  237         trunk->hwidth = VLAN_DEF_HWIDTH;
  238         n = 1 << trunk->hwidth;
  239         trunk->hmask = n - 1;
  240         trunk->hash = malloc(sizeof(struct ifvlanhead) * n, M_VLAN, M_WAITOK);
  241         for (i = 0; i < n; i++)
  242                 LIST_INIT(&trunk->hash[i]);
  243 }
  244 
  245 static void
  246 vlan_freehash(struct ifvlantrunk *trunk)
  247 {
  248 #ifdef INVARIANTS
  249         int i;
  250 
  251         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
  252         for (i = 0; i < (1 << trunk->hwidth); i++)
  253                 KASSERT(LIST_EMPTY(&trunk->hash[i]),
  254                     ("%s: hash table not empty", __func__));
  255 #endif
  256         free(trunk->hash, M_VLAN);
  257         trunk->hash = NULL;
  258         trunk->hwidth = trunk->hmask = 0;
  259 }
  260 
  261 static int
  262 vlan_inshash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
  263 {
  264         int i, b;
  265         struct ifvlan *ifv2;
  266 
  267         TRUNK_LOCK_ASSERT(trunk);
  268         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
  269 
  270         b = 1 << trunk->hwidth;
  271         i = HASH(ifv->ifv_tag, trunk->hmask);
  272         LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
  273                 if (ifv->ifv_tag == ifv2->ifv_tag)
  274                         return (EEXIST);
  275 
  276         /*
  277          * Grow the hash when the number of vlans exceeds half of the number of
  278          * hash buckets squared. This will make the average linked-list length
  279          * buckets/2.
  280          */
  281         if (trunk->refcnt > (b * b) / 2) {
  282                 vlan_growhash(trunk, 1);
  283                 i = HASH(ifv->ifv_tag, trunk->hmask);
  284         }
  285         LIST_INSERT_HEAD(&trunk->hash[i], ifv, ifv_list);
  286         trunk->refcnt++;
  287 
  288         return (0);
  289 }
  290 
  291 static int
  292 vlan_remhash(struct ifvlantrunk *trunk, struct ifvlan *ifv)
  293 {
  294         int i, b;
  295         struct ifvlan *ifv2;
  296 
  297         TRUNK_LOCK_ASSERT(trunk);
  298         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
  299         
  300         b = 1 << trunk->hwidth;
  301         i = HASH(ifv->ifv_tag, trunk->hmask);
  302         LIST_FOREACH(ifv2, &trunk->hash[i], ifv_list)
  303                 if (ifv2 == ifv) {
  304                         trunk->refcnt--;
  305                         LIST_REMOVE(ifv2, ifv_list);
  306                         if (trunk->refcnt < (b * b) / 2)
  307                                 vlan_growhash(trunk, -1);
  308                         return (0);
  309                 }
  310 
  311         panic("%s: vlan not found\n", __func__);
  312         return (ENOENT); /*NOTREACHED*/
  313 }
  314 
  315 /*
  316  * Grow the hash larger or smaller if memory permits.
  317  */
  318 static void
  319 vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
  320 {
  321         struct ifvlan *ifv;
  322         struct ifvlanhead *hash2;
  323         int hwidth2, i, j, n, n2;
  324 
  325         TRUNK_LOCK_ASSERT(trunk);
  326         KASSERT(trunk->hwidth > 0, ("%s: hwidth not positive", __func__));
  327 
  328         if (howmuch == 0) {
  329                 /* Harmless yet obvious coding error */
  330                 printf("%s: howmuch is 0\n", __func__);
  331                 return;
  332         }
  333 
  334         hwidth2 = trunk->hwidth + howmuch;
  335         n = 1 << trunk->hwidth;
  336         n2 = 1 << hwidth2;
  337         /* Do not shrink the table below the default */
  338         if (hwidth2 < VLAN_DEF_HWIDTH)
  339                 return;
  340 
  341         /* M_NOWAIT because we're called with trunk mutex held */
  342         hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
  343         if (hash2 == NULL) {
  344                 printf("%s: out of memory -- hash size not changed\n",
  345                     __func__);
  346                 return;         /* We can live with the old hash table */
  347         }
  348         for (j = 0; j < n2; j++)
  349                 LIST_INIT(&hash2[j]);
  350         for (i = 0; i < n; i++)
  351                 while ((ifv = LIST_FIRST(&trunk->hash[i])) != NULL) {
  352                         LIST_REMOVE(ifv, ifv_list);
  353                         j = HASH(ifv->ifv_tag, n2 - 1);
  354                         LIST_INSERT_HEAD(&hash2[j], ifv, ifv_list);
  355                 }
  356         free(trunk->hash, M_VLAN);
  357         trunk->hash = hash2;
  358         trunk->hwidth = hwidth2;
  359         trunk->hmask = n2 - 1;
  360 
  361         if (bootverbose)
  362                 if_printf(trunk->parent,
  363                     "VLAN hash table resized from %d to %d buckets\n", n, n2);
  364 }
  365 
  366 static __inline struct ifvlan *
  367 vlan_gethash(struct ifvlantrunk *trunk, uint16_t tag)
  368 {
  369         struct ifvlan *ifv;
  370 
  371         TRUNK_LOCK_RASSERT(trunk);
  372 
  373         LIST_FOREACH(ifv, &trunk->hash[HASH(tag, trunk->hmask)], ifv_list)
  374                 if (ifv->ifv_tag == tag)
  375                         return (ifv);
  376         return (NULL);
  377 }
  378 
  379 #if 0
  380 /* Debugging code to view the hashtables. */
  381 static void
  382 vlan_dumphash(struct ifvlantrunk *trunk)
  383 {
  384         int i;
  385         struct ifvlan *ifv;
  386 
  387         for (i = 0; i < (1 << trunk->hwidth); i++) {
  388                 printf("%d: ", i);
  389                 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
  390                         printf("%s ", ifv->ifv_ifp->if_xname);
  391                 printf("\n");
  392         }
  393 }
  394 #endif /* 0 */
  395 #endif /* !VLAN_ARRAY */
  396 
  397 static void
  398 trunk_destroy(struct ifvlantrunk *trunk)
  399 {
  400         VLAN_LOCK_ASSERT();
  401 
  402         TRUNK_LOCK(trunk);
  403 #ifndef VLAN_ARRAY
  404         vlan_freehash(trunk);
  405 #endif
  406         trunk->parent->if_vlantrunk = NULL;
  407         TRUNK_UNLOCK(trunk);
  408         TRUNK_LOCK_DESTROY(trunk);
  409         free(trunk, M_VLAN);
  410 }
  411 
  412 /*
  413  * Program our multicast filter. What we're actually doing is
  414  * programming the multicast filter of the parent. This has the
  415  * side effect of causing the parent interface to receive multicast
  416  * traffic that it doesn't really want, which ends up being discarded
  417  * later by the upper protocol layers. Unfortunately, there's no way
  418  * to avoid this: there really is only one physical interface.
  419  *
  420  * XXX: There is a possible race here if more than one thread is
  421  *      modifying the multicast state of the vlan interface at the same time.
  422  */
  423 static int
  424 vlan_setmulti(struct ifnet *ifp)
  425 {
  426         struct ifnet            *ifp_p;
  427         struct ifmultiaddr      *ifma, *rifma = NULL;
  428         struct ifvlan           *sc;
  429         struct vlan_mc_entry    *mc;
  430         struct sockaddr_dl      sdl;
  431         int                     error;
  432 
  433         /*VLAN_LOCK_ASSERT();*/
  434 
  435         /* Find the parent. */
  436         sc = ifp->if_softc;
  437         ifp_p = PARENT(sc);
  438 
  439         CURVNET_SET_QUIET(ifp_p->if_vnet);
  440 
  441         bzero((char *)&sdl, sizeof(sdl));
  442         sdl.sdl_len = sizeof(sdl);
  443         sdl.sdl_family = AF_LINK;
  444         sdl.sdl_index = ifp_p->if_index;
  445         sdl.sdl_type = IFT_ETHER;
  446         sdl.sdl_alen = ETHER_ADDR_LEN;
  447 
  448         /* First, remove any existing filter entries. */
  449         while ((mc = SLIST_FIRST(&sc->vlan_mc_listhead)) != NULL) {
  450                 bcopy((char *)&mc->mc_addr, LLADDR(&sdl), ETHER_ADDR_LEN);
  451                 error = if_delmulti(ifp_p, (struct sockaddr *)&sdl);
  452                 if (error)
  453                         return (error);
  454                 SLIST_REMOVE_HEAD(&sc->vlan_mc_listhead, mc_entries);
  455                 free(mc, M_VLAN);
  456         }
  457 
  458         /* Now program new ones. */
  459         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
  460                 if (ifma->ifma_addr->sa_family != AF_LINK)
  461                         continue;
  462                 mc = malloc(sizeof(struct vlan_mc_entry), M_VLAN, M_NOWAIT);
  463                 if (mc == NULL)
  464                         return (ENOMEM);
  465                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
  466                     (char *)&mc->mc_addr, ETHER_ADDR_LEN);
  467                 SLIST_INSERT_HEAD(&sc->vlan_mc_listhead, mc, mc_entries);
  468                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
  469                     LLADDR(&sdl), ETHER_ADDR_LEN);
  470                 error = if_addmulti(ifp_p, (struct sockaddr *)&sdl, &rifma);
  471                 if (error)
  472                         return (error);
  473         }
  474 
  475         CURVNET_RESTORE();
  476         return (0);
  477 }
  478 
  479 /*
  480  * A handler for parent interface link layer address changes.
  481  * If the parent interface link layer address is changed we
  482  * should also change it on all children vlans.
  483  */
  484 static void
  485 vlan_iflladdr(void *arg __unused, struct ifnet *ifp)
  486 {
  487         struct ifvlan *ifv;
  488 #ifndef VLAN_ARRAY
  489         struct ifvlan *next;
  490 #endif
  491         int i;
  492 
  493         /*
  494          * Check if it's a trunk interface first of all
  495          * to avoid needless locking.
  496          */
  497         if (ifp->if_vlantrunk == NULL)
  498                 return;
  499 
  500         VLAN_LOCK();
  501         /*
  502          * OK, it's a trunk.  Loop over and change all vlan's lladdrs on it.
  503          */
  504 #ifdef VLAN_ARRAY
  505         for (i = 0; i < VLAN_ARRAY_SIZE; i++)
  506                 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
  507 #else /* VLAN_ARRAY */
  508         for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
  509                 LIST_FOREACH_SAFE(ifv, &ifp->if_vlantrunk->hash[i], ifv_list, next) {
  510 #endif /* VLAN_ARRAY */
  511                         VLAN_UNLOCK();
  512                         if_setlladdr(ifv->ifv_ifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
  513                         VLAN_LOCK();
  514                 }
  515         VLAN_UNLOCK();
  516 
  517 }
  518 
  519 /*
  520  * A handler for network interface departure events.
  521  * Track departure of trunks here so that we don't access invalid
  522  * pointers or whatever if a trunk is ripped from under us, e.g.,
  523  * by ejecting its hot-plug card.  However, if an ifnet is simply
  524  * being renamed, then there's no need to tear down the state.
  525  */
  526 static void
  527 vlan_ifdetach(void *arg __unused, struct ifnet *ifp)
  528 {
  529         struct ifvlan *ifv;
  530         int i;
  531 
  532         /*
  533          * Check if it's a trunk interface first of all
  534          * to avoid needless locking.
  535          */
  536         if (ifp->if_vlantrunk == NULL)
  537                 return;
  538 
  539         /* If the ifnet is just being renamed, don't do anything. */
  540         if (ifp->if_flags & IFF_RENAMING)
  541                 return;
  542 
  543         VLAN_LOCK();
  544         /*
  545          * OK, it's a trunk.  Loop over and detach all vlan's on it.
  546          * Check trunk pointer after each vlan_unconfig() as it will
  547          * free it and set to NULL after the last vlan was detached.
  548          */
  549 #ifdef VLAN_ARRAY
  550         for (i = 0; i < VLAN_ARRAY_SIZE; i++)
  551                 if ((ifv = ifp->if_vlantrunk->vlans[i])) {
  552                         vlan_unconfig_locked(ifv->ifv_ifp, 1);
  553                         if (ifp->if_vlantrunk == NULL)
  554                                 break;
  555                 }
  556 #else /* VLAN_ARRAY */
  557 restart:
  558         for (i = 0; i < (1 << ifp->if_vlantrunk->hwidth); i++)
  559                 if ((ifv = LIST_FIRST(&ifp->if_vlantrunk->hash[i]))) {
  560                         vlan_unconfig_locked(ifv->ifv_ifp, 1);
  561                         if (ifp->if_vlantrunk)
  562                                 goto restart;   /* trunk->hwidth can change */
  563                         else
  564                                 break;
  565                 }
  566 #endif /* VLAN_ARRAY */
  567         /* Trunk should have been destroyed in vlan_unconfig(). */
  568         KASSERT(ifp->if_vlantrunk == NULL, ("%s: purge failed", __func__));
  569         VLAN_UNLOCK();
  570 }
  571 
  572 /*
  573  * VLAN support can be loaded as a module.  The only place in the
  574  * system that's intimately aware of this is ether_input.  We hook
  575  * into this code through vlan_input_p which is defined there and
  576  * set here.  Noone else in the system should be aware of this so
  577  * we use an explicit reference here.
  578  */
  579 extern  void (*vlan_input_p)(struct ifnet *, struct mbuf *);
  580 
  581 /* For if_link_state_change() eyes only... */
  582 extern  void (*vlan_link_state_p)(struct ifnet *, int);
  583 
  584 static int
  585 vlan_modevent(module_t mod, int type, void *data)
  586 {
  587 
  588         switch (type) {
  589         case MOD_LOAD:
  590                 ifdetach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
  591                     vlan_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
  592                 if (ifdetach_tag == NULL)
  593                         return (ENOMEM);
  594                 iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
  595                     vlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
  596                 if (iflladdr_tag == NULL)
  597                         return (ENOMEM);
  598                 VLAN_LOCK_INIT();
  599                 vlan_input_p = vlan_input;
  600                 vlan_link_state_p = vlan_link_state;
  601                 vlan_trunk_cap_p = vlan_trunk_capabilities;
  602 #ifndef VIMAGE
  603                 if_clone_attach(&vlan_cloner);
  604 #endif
  605                 if (bootverbose)
  606                         printf("vlan: initialized, using "
  607 #ifdef VLAN_ARRAY
  608                                "full-size arrays"
  609 #else
  610                                "hash tables with chaining"
  611 #endif
  612                         
  613                                "\n");
  614                 break;
  615         case MOD_UNLOAD:
  616 #ifndef VIMAGE
  617                 if_clone_detach(&vlan_cloner);
  618 #endif
  619                 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ifdetach_tag);
  620                 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_tag);
  621                 vlan_input_p = NULL;
  622                 vlan_link_state_p = NULL;
  623                 vlan_trunk_cap_p = NULL;
  624                 VLAN_LOCK_DESTROY();
  625                 if (bootverbose)
  626                         printf("vlan: unloaded\n");
  627                 break;
  628         default:
  629                 return (EOPNOTSUPP);
  630         }
  631         return (0);
  632 }
  633 
  634 static moduledata_t vlan_mod = {
  635         "if_vlan",
  636         vlan_modevent,
  637         0
  638 };
  639 
  640 DECLARE_MODULE(if_vlan, vlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  641 MODULE_VERSION(if_vlan, 3);
  642 
  643 #ifdef VIMAGE
  644 static void
  645 vnet_vlan_init(const void *unused __unused)
  646 {
  647 
  648         V_vlan_cloner = vlan_cloner;
  649         if_clone_attach(&V_vlan_cloner);
  650 }
  651 VNET_SYSINIT(vnet_vlan_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
  652     vnet_vlan_init, NULL);
  653 
  654 static void
  655 vnet_vlan_uninit(const void *unused __unused)
  656 {
  657 
  658         if_clone_detach(&V_vlan_cloner);
  659 }
  660 VNET_SYSUNINIT(vnet_vlan_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
  661     vnet_vlan_uninit, NULL);
  662 #endif
  663 
  664 static struct ifnet *
  665 vlan_clone_match_ethertag(struct if_clone *ifc, const char *name, int *tag)
  666 {
  667         const char *cp;
  668         struct ifnet *ifp;
  669         int t;
  670 
  671         /* Check for <etherif>.<vlan> style interface names. */
  672         IFNET_RLOCK_NOSLEEP();
  673         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  674                 if (ifp->if_type != IFT_ETHER)
  675                         continue;
  676                 if (strncmp(ifp->if_xname, name, strlen(ifp->if_xname)) != 0)
  677                         continue;
  678                 cp = name + strlen(ifp->if_xname);
  679                 if (*cp++ != '.')
  680                         continue;
  681                 if (*cp == '\0')
  682                         continue;
  683                 t = 0;
  684                 for(; *cp >= '' && *cp <= '9'; cp++)
  685                         t = (t * 10) + (*cp - '');
  686                 if (*cp != '\0')
  687                         continue;
  688                 if (tag != NULL)
  689                         *tag = t;
  690                 break;
  691         }
  692         IFNET_RUNLOCK_NOSLEEP();
  693 
  694         return (ifp);
  695 }
  696 
  697 static int
  698 vlan_clone_match(struct if_clone *ifc, const char *name)
  699 {
  700         const char *cp;
  701 
  702         if (vlan_clone_match_ethertag(ifc, name, NULL) != NULL)
  703                 return (1);
  704 
  705         if (strncmp(VLANNAME, name, strlen(VLANNAME)) != 0)
  706                 return (0);
  707         for (cp = name + 4; *cp != '\0'; cp++) {
  708                 if (*cp < '' || *cp > '9')
  709                         return (0);
  710         }
  711 
  712         return (1);
  713 }
  714 
  715 static int
  716 vlan_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
  717 {
  718         char *dp;
  719         int wildcard;
  720         int unit;
  721         int error;
  722         int tag;
  723         int ethertag;
  724         struct ifvlan *ifv;
  725         struct ifnet *ifp;
  726         struct ifnet *p;
  727         struct vlanreq vlr;
  728         static const u_char eaddr[ETHER_ADDR_LEN];      /* 00:00:00:00:00:00 */
  729 
  730         /*
  731          * There are 3 (ugh) ways to specify the cloned device:
  732          * o pass a parameter block with the clone request.
  733          * o specify parameters in the text of the clone device name
  734          * o specify no parameters and get an unattached device that
  735          *   must be configured separately.
  736          * The first technique is preferred; the latter two are
  737          * supported for backwards compatibilty.
  738          */
  739         if (params) {
  740                 error = copyin(params, &vlr, sizeof(vlr));
  741                 if (error)
  742                         return error;
  743                 p = ifunit(vlr.vlr_parent);
  744                 if (p == NULL)
  745                         return ENXIO;
  746                 /*
  747                  * Don't let the caller set up a VLAN tag with
  748                  * anything except VLID bits.
  749                  */
  750                 if (vlr.vlr_tag & ~EVL_VLID_MASK)
  751                         return (EINVAL);
  752                 error = ifc_name2unit(name, &unit);
  753                 if (error != 0)
  754                         return (error);
  755 
  756                 ethertag = 1;
  757                 tag = vlr.vlr_tag;
  758                 wildcard = (unit < 0);
  759         } else if ((p = vlan_clone_match_ethertag(ifc, name, &tag)) != NULL) {
  760                 ethertag = 1;
  761                 unit = -1;
  762                 wildcard = 0;
  763 
  764                 /*
  765                  * Don't let the caller set up a VLAN tag with
  766                  * anything except VLID bits.
  767                  */
  768                 if (tag & ~EVL_VLID_MASK)
  769                         return (EINVAL);
  770         } else {
  771                 ethertag = 0;
  772 
  773                 error = ifc_name2unit(name, &unit);
  774                 if (error != 0)
  775                         return (error);
  776 
  777                 wildcard = (unit < 0);
  778         }
  779 
  780         error = ifc_alloc_unit(ifc, &unit);
  781         if (error != 0)
  782                 return (error);
  783 
  784         /* In the wildcard case, we need to update the name. */
  785         if (wildcard) {
  786                 for (dp = name; *dp != '\0'; dp++);
  787                 if (snprintf(dp, len - (dp-name), "%d", unit) >
  788                     len - (dp-name) - 1) {
  789                         panic("%s: interface name too long", __func__);
  790                 }
  791         }
  792 
  793         ifv = malloc(sizeof(struct ifvlan), M_VLAN, M_WAITOK | M_ZERO);
  794         ifp = ifv->ifv_ifp = if_alloc(IFT_ETHER);
  795         if (ifp == NULL) {
  796                 ifc_free_unit(ifc, unit);
  797                 free(ifv, M_VLAN);
  798                 return (ENOSPC);
  799         }
  800         SLIST_INIT(&ifv->vlan_mc_listhead);
  801 
  802         ifp->if_softc = ifv;
  803         /*
  804          * Set the name manually rather than using if_initname because
  805          * we don't conform to the default naming convention for interfaces.
  806          */
  807         strlcpy(ifp->if_xname, name, IFNAMSIZ);
  808         ifp->if_dname = ifc->ifc_name;
  809         ifp->if_dunit = unit;
  810         /* NB: flags are not set here */
  811         ifp->if_linkmib = &ifv->ifv_mib;
  812         ifp->if_linkmiblen = sizeof(ifv->ifv_mib);
  813         /* NB: mtu is not set here */
  814 
  815         ifp->if_init = vlan_init;
  816         ifp->if_transmit = vlan_transmit;
  817         ifp->if_qflush = vlan_qflush;
  818         ifp->if_ioctl = vlan_ioctl;
  819         ifp->if_flags = VLAN_IFFLAGS;
  820         ether_ifattach(ifp, eaddr);
  821         /* Now undo some of the damage... */
  822         ifp->if_baudrate = 0;
  823         ifp->if_type = IFT_L2VLAN;
  824         ifp->if_hdrlen = ETHER_VLAN_ENCAP_LEN;
  825 
  826         if (ethertag) {
  827                 error = vlan_config(ifv, p, tag);
  828                 if (error != 0) {
  829                         /*
  830                          * Since we've partially failed, we need to back
  831                          * out all the way, otherwise userland could get
  832                          * confused.  Thus, we destroy the interface.
  833                          */
  834                         ether_ifdetach(ifp);
  835                         vlan_unconfig(ifp);
  836                         if_free_type(ifp, IFT_ETHER);
  837                         ifc_free_unit(ifc, unit);
  838                         free(ifv, M_VLAN);
  839 
  840                         return (error);
  841                 }
  842 
  843                 /* Update flags on the parent, if necessary. */
  844                 vlan_setflags(ifp, 1);
  845         }
  846 
  847         return (0);
  848 }
  849 
  850 static int
  851 vlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
  852 {
  853         struct ifvlan *ifv = ifp->if_softc;
  854         int unit = ifp->if_dunit;
  855 
  856         ether_ifdetach(ifp);    /* first, remove it from system-wide lists */
  857         vlan_unconfig(ifp);     /* now it can be unconfigured and freed */
  858         if_free_type(ifp, IFT_ETHER);
  859         free(ifv, M_VLAN);
  860         ifc_free_unit(ifc, unit);
  861 
  862         return (0);
  863 }
  864 
  865 /*
  866  * The ifp->if_init entry point for vlan(4) is a no-op.
  867  */
  868 static void
  869 vlan_init(void *foo __unused)
  870 {
  871 }
  872 
  873 /*
  874  * The if_transmit method for vlan(4) interface.
  875  */
  876 static int
  877 vlan_transmit(struct ifnet *ifp, struct mbuf *m)
  878 {
  879         struct ifvlan *ifv;
  880         struct ifnet *p;
  881         int error, len, mcast;
  882 
  883         ifv = ifp->if_softc;
  884         p = PARENT(ifv);
  885         len = m->m_pkthdr.len;
  886         mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
  887 
  888         BPF_MTAP(ifp, m);
  889 
  890         /*
  891          * Do not run parent's if_transmit() if the parent is not up,
  892          * or parent's driver will cause a system crash.
  893          */
  894         if (!UP_AND_RUNNING(p)) {
  895                 m_freem(m);
  896                 ifp->if_oerrors++;
  897                 return (0);
  898         }
  899 
  900         /*
  901          * Pad the frame to the minimum size allowed if told to.
  902          * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
  903          * paragraph C.4.4.3.b.  It can help to work around buggy
  904          * bridges that violate paragraph C.4.4.3.a from the same
  905          * document, i.e., fail to pad short frames after untagging.
  906          * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
  907          * untagging it will produce a 62-byte frame, which is a runt
  908          * and requires padding.  There are VLAN-enabled network
  909          * devices that just discard such runts instead or mishandle
  910          * them somehow.
  911          */
  912         if (soft_pad) {
  913                 static char pad[8];     /* just zeros */
  914                 int n;
  915 
  916                 for (n = ETHERMIN + ETHER_HDR_LEN - m->m_pkthdr.len;
  917                      n > 0; n -= sizeof(pad))
  918                         if (!m_append(m, min(n, sizeof(pad)), pad))
  919                                 break;
  920 
  921                 if (n > 0) {
  922                         if_printf(ifp, "cannot pad short frame\n");
  923                         ifp->if_oerrors++;
  924                         m_freem(m);
  925                         return (0);
  926                 }
  927         }
  928 
  929         /*
  930          * If underlying interface can do VLAN tag insertion itself,
  931          * just pass the packet along. However, we need some way to
  932          * tell the interface where the packet came from so that it
  933          * knows how to find the VLAN tag to use, so we attach a
  934          * packet tag that holds it.
  935          */
  936         if (p->if_capenable & IFCAP_VLAN_HWTAGGING) {
  937                 m->m_pkthdr.ether_vtag = ifv->ifv_tag;
  938                 m->m_flags |= M_VLANTAG;
  939         } else {
  940                 m = ether_vlanencap(m, ifv->ifv_tag);
  941                 if (m == NULL) {
  942                         if_printf(ifp, "unable to prepend VLAN header\n");
  943                         ifp->if_oerrors++;
  944                         return (0);
  945                 }
  946         }
  947 
  948         /*
  949          * Send it, precisely as ether_output() would have.
  950          */
  951         error = (p->if_transmit)(p, m);
  952         if (!error) {
  953                 ifp->if_opackets++;
  954                 ifp->if_omcasts += mcast;
  955                 ifp->if_obytes += len;
  956         } else
  957                 ifp->if_oerrors++;
  958         return (error);
  959 }
  960 
  961 /*
  962  * The ifp->if_qflush entry point for vlan(4) is a no-op.
  963  */
  964 static void
  965 vlan_qflush(struct ifnet *ifp __unused)
  966 {
  967 }
  968 
  969 static void
  970 vlan_input(struct ifnet *ifp, struct mbuf *m)
  971 {
  972         struct ifvlantrunk *trunk = ifp->if_vlantrunk;
  973         struct ifvlan *ifv;
  974         uint16_t tag;
  975 
  976         KASSERT(trunk != NULL, ("%s: no trunk", __func__));
  977 
  978         if (m->m_flags & M_VLANTAG) {
  979                 /*
  980                  * Packet is tagged, but m contains a normal
  981                  * Ethernet frame; the tag is stored out-of-band.
  982                  */
  983                 tag = EVL_VLANOFTAG(m->m_pkthdr.ether_vtag);
  984                 m->m_flags &= ~M_VLANTAG;
  985         } else {
  986                 struct ether_vlan_header *evl;
  987 
  988                 /*
  989                  * Packet is tagged in-band as specified by 802.1q.
  990                  */
  991                 switch (ifp->if_type) {
  992                 case IFT_ETHER:
  993                         if (m->m_len < sizeof(*evl) &&
  994                             (m = m_pullup(m, sizeof(*evl))) == NULL) {
  995                                 if_printf(ifp, "cannot pullup VLAN header\n");
  996                                 return;
  997                         }
  998                         evl = mtod(m, struct ether_vlan_header *);
  999                         tag = EVL_VLANOFTAG(ntohs(evl->evl_tag));
 1000 
 1001                         /*
 1002                          * Remove the 802.1q header by copying the Ethernet
 1003                          * addresses over it and adjusting the beginning of
 1004                          * the data in the mbuf.  The encapsulated Ethernet
 1005                          * type field is already in place.
 1006                          */
 1007                         bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
 1008                               ETHER_HDR_LEN - ETHER_TYPE_LEN);
 1009                         m_adj(m, ETHER_VLAN_ENCAP_LEN);
 1010                         break;
 1011 
 1012                 default:
 1013 #ifdef INVARIANTS
 1014                         panic("%s: %s has unsupported if_type %u",
 1015                               __func__, ifp->if_xname, ifp->if_type);
 1016 #endif
 1017                         m_freem(m);
 1018                         ifp->if_noproto++;
 1019                         return;
 1020                 }
 1021         }
 1022 
 1023         TRUNK_RLOCK(trunk);
 1024 #ifdef VLAN_ARRAY
 1025         ifv = trunk->vlans[tag];
 1026 #else
 1027         ifv = vlan_gethash(trunk, tag);
 1028 #endif
 1029         if (ifv == NULL || !UP_AND_RUNNING(ifv->ifv_ifp)) {
 1030                 TRUNK_RUNLOCK(trunk);
 1031                 m_freem(m);
 1032                 ifp->if_noproto++;
 1033                 return;
 1034         }
 1035         TRUNK_RUNLOCK(trunk);
 1036 
 1037         m->m_pkthdr.rcvif = ifv->ifv_ifp;
 1038         ifv->ifv_ifp->if_ipackets++;
 1039 
 1040         /* Pass it back through the parent's input routine. */
 1041         (*ifp->if_input)(ifv->ifv_ifp, m);
 1042 }
 1043 
 1044 static int
 1045 vlan_config(struct ifvlan *ifv, struct ifnet *p, uint16_t tag)
 1046 {
 1047         struct ifvlantrunk *trunk;
 1048         struct ifnet *ifp;
 1049         int error = 0;
 1050 
 1051         /* VID numbers 0x0 and 0xFFF are reserved */
 1052         if (tag == 0 || tag == 0xFFF)
 1053                 return (EINVAL);
 1054         if (p->if_type != IFT_ETHER)
 1055                 return (EPROTONOSUPPORT);
 1056         if ((p->if_flags & VLAN_IFFLAGS) != VLAN_IFFLAGS)
 1057                 return (EPROTONOSUPPORT);
 1058         if (ifv->ifv_trunk)
 1059                 return (EBUSY);
 1060 
 1061         if (p->if_vlantrunk == NULL) {
 1062                 trunk = malloc(sizeof(struct ifvlantrunk),
 1063                     M_VLAN, M_WAITOK | M_ZERO);
 1064 #ifndef VLAN_ARRAY
 1065                 vlan_inithash(trunk);
 1066 #endif
 1067                 VLAN_LOCK();
 1068                 if (p->if_vlantrunk != NULL) {
 1069                         /* A race that that is very unlikely to be hit. */
 1070 #ifndef VLAN_ARRAY
 1071                         vlan_freehash(trunk);
 1072 #endif
 1073                         free(trunk, M_VLAN);
 1074                         goto exists;
 1075                 }
 1076                 TRUNK_LOCK_INIT(trunk);
 1077                 TRUNK_LOCK(trunk);
 1078                 p->if_vlantrunk = trunk;
 1079                 trunk->parent = p;
 1080         } else {
 1081                 VLAN_LOCK();
 1082 exists:
 1083                 trunk = p->if_vlantrunk;
 1084                 TRUNK_LOCK(trunk);
 1085         }
 1086 
 1087         ifv->ifv_tag = tag;     /* must set this before vlan_inshash() */
 1088 #ifdef VLAN_ARRAY
 1089         if (trunk->vlans[tag] != NULL) {
 1090                 error = EEXIST;
 1091                 goto done;
 1092         }
 1093         trunk->vlans[tag] = ifv;
 1094         trunk->refcnt++;
 1095 #else
 1096         error = vlan_inshash(trunk, ifv);
 1097         if (error)
 1098                 goto done;
 1099 #endif
 1100         ifv->ifv_proto = ETHERTYPE_VLAN;
 1101         ifv->ifv_encaplen = ETHER_VLAN_ENCAP_LEN;
 1102         ifv->ifv_mintu = ETHERMIN;
 1103         ifv->ifv_pflags = 0;
 1104 
 1105         /*
 1106          * If the parent supports the VLAN_MTU capability,
 1107          * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames,
 1108          * use it.
 1109          */
 1110         if (p->if_capenable & IFCAP_VLAN_MTU) {
 1111                 /*
 1112                  * No need to fudge the MTU since the parent can
 1113                  * handle extended frames.
 1114                  */
 1115                 ifv->ifv_mtufudge = 0;
 1116         } else {
 1117                 /*
 1118                  * Fudge the MTU by the encapsulation size.  This
 1119                  * makes us incompatible with strictly compliant
 1120                  * 802.1Q implementations, but allows us to use
 1121                  * the feature with other NetBSD implementations,
 1122                  * which might still be useful.
 1123                  */
 1124                 ifv->ifv_mtufudge = ifv->ifv_encaplen;
 1125         }
 1126 
 1127         ifv->ifv_trunk = trunk;
 1128         ifp = ifv->ifv_ifp;
 1129         ifp->if_mtu = p->if_mtu - ifv->ifv_mtufudge;
 1130         ifp->if_baudrate = p->if_baudrate;
 1131         /*
 1132          * Copy only a selected subset of flags from the parent.
 1133          * Other flags are none of our business.
 1134          */
 1135 #define VLAN_COPY_FLAGS (IFF_SIMPLEX)
 1136         ifp->if_flags &= ~VLAN_COPY_FLAGS;
 1137         ifp->if_flags |= p->if_flags & VLAN_COPY_FLAGS;
 1138 #undef VLAN_COPY_FLAGS
 1139 
 1140         ifp->if_link_state = p->if_link_state;
 1141 
 1142         vlan_capabilities(ifv);
 1143 
 1144         /*
 1145          * Set up our ``Ethernet address'' to reflect the underlying
 1146          * physical interface's.
 1147          */
 1148         bcopy(IF_LLADDR(p), IF_LLADDR(ifp), ETHER_ADDR_LEN);
 1149 
 1150         /*
 1151          * Configure multicast addresses that may already be
 1152          * joined on the vlan device.
 1153          */
 1154         (void)vlan_setmulti(ifp); /* XXX: VLAN lock held */
 1155 
 1156         /* We are ready for operation now. */
 1157         ifp->if_drv_flags |= IFF_DRV_RUNNING;
 1158 done:
 1159         TRUNK_UNLOCK(trunk);
 1160         if (error == 0)
 1161                 EVENTHANDLER_INVOKE(vlan_config, p, ifv->ifv_tag);
 1162         VLAN_UNLOCK();
 1163 
 1164         return (error);
 1165 }
 1166 
 1167 static void
 1168 vlan_unconfig(struct ifnet *ifp)
 1169 {
 1170 
 1171         VLAN_LOCK();
 1172         vlan_unconfig_locked(ifp, 0);
 1173         VLAN_UNLOCK();
 1174 }
 1175 
 1176 static void
 1177 vlan_unconfig_locked(struct ifnet *ifp, int departing)
 1178 {
 1179         struct ifvlantrunk *trunk;
 1180         struct vlan_mc_entry *mc;
 1181         struct ifvlan *ifv;
 1182         struct ifnet  *parent;
 1183         int error;
 1184 
 1185         VLAN_LOCK_ASSERT();
 1186 
 1187         ifv = ifp->if_softc;
 1188         trunk = ifv->ifv_trunk;
 1189         parent = NULL;
 1190 
 1191         if (trunk != NULL) {
 1192                 struct sockaddr_dl sdl;
 1193 
 1194                 TRUNK_LOCK(trunk);
 1195                 parent = trunk->parent;
 1196 
 1197                 /*
 1198                  * Since the interface is being unconfigured, we need to
 1199                  * empty the list of multicast groups that we may have joined
 1200                  * while we were alive from the parent's list.
 1201                  */
 1202                 bzero((char *)&sdl, sizeof(sdl));
 1203                 sdl.sdl_len = sizeof(sdl);
 1204                 sdl.sdl_family = AF_LINK;
 1205                 sdl.sdl_index = parent->if_index;
 1206                 sdl.sdl_type = IFT_ETHER;
 1207                 sdl.sdl_alen = ETHER_ADDR_LEN;
 1208 
 1209                 while ((mc = SLIST_FIRST(&ifv->vlan_mc_listhead)) != NULL) {
 1210                         bcopy((char *)&mc->mc_addr, LLADDR(&sdl),
 1211                             ETHER_ADDR_LEN);
 1212 
 1213                         /*
 1214                          * If the parent interface is being detached,
 1215                          * all its multicast addresses have already
 1216                          * been removed.  Warn about errors if
 1217                          * if_delmulti() does fail, but don't abort as
 1218                          * all callers expect vlan destruction to
 1219                          * succeed.
 1220                          */
 1221                         if (!departing) {
 1222                                 error = if_delmulti(parent,
 1223                                     (struct sockaddr *)&sdl);
 1224                                 if (error)
 1225                                         if_printf(ifp,
 1226                     "Failed to delete multicast address from parent: %d\n",
 1227                                             error);
 1228                         }
 1229                         SLIST_REMOVE_HEAD(&ifv->vlan_mc_listhead, mc_entries);
 1230                         free(mc, M_VLAN);
 1231                 }
 1232 
 1233                 vlan_setflags(ifp, 0); /* clear special flags on parent */
 1234 #ifdef VLAN_ARRAY
 1235                 trunk->vlans[ifv->ifv_tag] = NULL;
 1236                 trunk->refcnt--;
 1237 #else
 1238                 vlan_remhash(trunk, ifv);
 1239 #endif
 1240                 ifv->ifv_trunk = NULL;
 1241 
 1242                 /*
 1243                  * Check if we were the last.
 1244                  */
 1245                 if (trunk->refcnt == 0) {
 1246                         trunk->parent->if_vlantrunk = NULL;
 1247                         /*
 1248                          * XXXGL: If some ithread has already entered
 1249                          * vlan_input() and is now blocked on the trunk
 1250                          * lock, then it should preempt us right after
 1251                          * unlock and finish its work. Then we will acquire
 1252                          * lock again in trunk_destroy().
 1253                          */
 1254                         TRUNK_UNLOCK(trunk);
 1255                         trunk_destroy(trunk);
 1256                 } else
 1257                         TRUNK_UNLOCK(trunk);
 1258         }
 1259 
 1260         /* Disconnect from parent. */
 1261         if (ifv->ifv_pflags)
 1262                 if_printf(ifp, "%s: ifv_pflags unclean\n", __func__);
 1263         ifp->if_mtu = ETHERMTU;
 1264         ifp->if_link_state = LINK_STATE_UNKNOWN;
 1265         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 1266 
 1267         /*
 1268          * Only dispatch an event if vlan was
 1269          * attached, otherwise there is nothing
 1270          * to cleanup anyway.
 1271          */
 1272         if (parent != NULL)
 1273                 EVENTHANDLER_INVOKE(vlan_unconfig, parent, ifv->ifv_tag);
 1274 }
 1275 
 1276 /* Handle a reference counted flag that should be set on the parent as well */
 1277 static int
 1278 vlan_setflag(struct ifnet *ifp, int flag, int status,
 1279              int (*func)(struct ifnet *, int))
 1280 {
 1281         struct ifvlan *ifv;
 1282         int error;
 1283 
 1284         /* XXX VLAN_LOCK_ASSERT(); */
 1285 
 1286         ifv = ifp->if_softc;
 1287         status = status ? (ifp->if_flags & flag) : 0;
 1288         /* Now "status" contains the flag value or 0 */
 1289 
 1290         /*
 1291          * See if recorded parent's status is different from what
 1292          * we want it to be.  If it is, flip it.  We record parent's
 1293          * status in ifv_pflags so that we won't clear parent's flag
 1294          * we haven't set.  In fact, we don't clear or set parent's
 1295          * flags directly, but get or release references to them.
 1296          * That's why we can be sure that recorded flags still are
 1297          * in accord with actual parent's flags.
 1298          */
 1299         if (status != (ifv->ifv_pflags & flag)) {
 1300                 error = (*func)(PARENT(ifv), status);
 1301                 if (error)
 1302                         return (error);
 1303                 ifv->ifv_pflags &= ~flag;
 1304                 ifv->ifv_pflags |= status;
 1305         }
 1306         return (0);
 1307 }
 1308 
 1309 /*
 1310  * Handle IFF_* flags that require certain changes on the parent:
 1311  * if "status" is true, update parent's flags respective to our if_flags;
 1312  * if "status" is false, forcedly clear the flags set on parent.
 1313  */
 1314 static int
 1315 vlan_setflags(struct ifnet *ifp, int status)
 1316 {
 1317         int error, i;
 1318         
 1319         for (i = 0; vlan_pflags[i].flag; i++) {
 1320                 error = vlan_setflag(ifp, vlan_pflags[i].flag,
 1321                                      status, vlan_pflags[i].func);
 1322                 if (error)
 1323                         return (error);
 1324         }
 1325         return (0);
 1326 }
 1327 
 1328 /* Inform all vlans that their parent has changed link state */
 1329 static void
 1330 vlan_link_state(struct ifnet *ifp, int link)
 1331 {
 1332         struct ifvlantrunk *trunk = ifp->if_vlantrunk;
 1333         struct ifvlan *ifv;
 1334         int i;
 1335 
 1336         TRUNK_LOCK(trunk);
 1337 #ifdef VLAN_ARRAY
 1338         for (i = 0; i < VLAN_ARRAY_SIZE; i++)
 1339                 if (trunk->vlans[i] != NULL) {
 1340                         ifv = trunk->vlans[i];
 1341 #else
 1342         for (i = 0; i < (1 << trunk->hwidth); i++)
 1343                 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list) {
 1344 #endif
 1345                         ifv->ifv_ifp->if_baudrate = trunk->parent->if_baudrate;
 1346                         if_link_state_change(ifv->ifv_ifp,
 1347                             trunk->parent->if_link_state);
 1348                 }
 1349         TRUNK_UNLOCK(trunk);
 1350 }
 1351 
 1352 static void
 1353 vlan_capabilities(struct ifvlan *ifv)
 1354 {
 1355         struct ifnet *p = PARENT(ifv);
 1356         struct ifnet *ifp = ifv->ifv_ifp;
 1357 
 1358         TRUNK_LOCK_ASSERT(TRUNK(ifv));
 1359 
 1360         /*
 1361          * If the parent interface can do checksum offloading
 1362          * on VLANs, then propagate its hardware-assisted
 1363          * checksumming flags. Also assert that checksum
 1364          * offloading requires hardware VLAN tagging.
 1365          */
 1366         if (p->if_capabilities & IFCAP_VLAN_HWCSUM)
 1367                 ifp->if_capabilities = p->if_capabilities & IFCAP_HWCSUM;
 1368 
 1369         if (p->if_capenable & IFCAP_VLAN_HWCSUM &&
 1370             p->if_capenable & IFCAP_VLAN_HWTAGGING) {
 1371                 ifp->if_capenable = p->if_capenable & IFCAP_HWCSUM;
 1372                 ifp->if_hwassist = p->if_hwassist & (CSUM_IP | CSUM_TCP |
 1373                     CSUM_UDP | CSUM_SCTP | CSUM_IP_FRAGS | CSUM_FRAGMENT);
 1374         } else {
 1375                 ifp->if_capenable = 0;
 1376                 ifp->if_hwassist = 0;
 1377         }
 1378         /*
 1379          * If the parent interface can do TSO on VLANs then
 1380          * propagate the hardware-assisted flag. TSO on VLANs
 1381          * does not necessarily require hardware VLAN tagging.
 1382          */
 1383         if (p->if_capabilities & IFCAP_VLAN_HWTSO)
 1384                 ifp->if_capabilities |= p->if_capabilities & IFCAP_TSO;
 1385         if (p->if_capenable & IFCAP_VLAN_HWTSO) {
 1386                 ifp->if_capenable |= p->if_capenable & IFCAP_TSO;
 1387                 ifp->if_hwassist |= p->if_hwassist & CSUM_TSO;
 1388         } else {
 1389                 ifp->if_capenable &= ~(p->if_capenable & IFCAP_TSO);
 1390                 ifp->if_hwassist &= ~(p->if_hwassist & CSUM_TSO);
 1391         }
 1392 }
 1393 
 1394 static void
 1395 vlan_trunk_capabilities(struct ifnet *ifp)
 1396 {
 1397         struct ifvlantrunk *trunk = ifp->if_vlantrunk;
 1398         struct ifvlan *ifv;
 1399         int i;
 1400 
 1401         TRUNK_LOCK(trunk);
 1402 #ifdef VLAN_ARRAY
 1403         for (i = 0; i < VLAN_ARRAY_SIZE; i++)
 1404                 if (trunk->vlans[i] != NULL) {
 1405                         ifv = trunk->vlans[i];
 1406 #else
 1407         for (i = 0; i < (1 << trunk->hwidth); i++) {
 1408                 LIST_FOREACH(ifv, &trunk->hash[i], ifv_list)
 1409 #endif
 1410                         vlan_capabilities(ifv);
 1411         }
 1412         TRUNK_UNLOCK(trunk);
 1413 }
 1414 
 1415 static int
 1416 vlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 1417 {
 1418         struct ifnet *p;
 1419         struct ifreq *ifr;
 1420         struct ifvlan *ifv;
 1421         struct vlanreq vlr;
 1422         int error = 0;
 1423 
 1424         ifr = (struct ifreq *)data;
 1425         ifv = ifp->if_softc;
 1426 
 1427         switch (cmd) {
 1428         case SIOCGIFMEDIA:
 1429                 VLAN_LOCK();
 1430                 if (TRUNK(ifv) != NULL) {
 1431                         p = PARENT(ifv);
 1432                         VLAN_UNLOCK();
 1433                         error = (*p->if_ioctl)(p, SIOCGIFMEDIA, data);
 1434                         /* Limit the result to the parent's current config. */
 1435                         if (error == 0) {
 1436                                 struct ifmediareq *ifmr;
 1437 
 1438                                 ifmr = (struct ifmediareq *)data;
 1439                                 if (ifmr->ifm_count >= 1 && ifmr->ifm_ulist) {
 1440                                         ifmr->ifm_count = 1;
 1441                                         error = copyout(&ifmr->ifm_current,
 1442                                                 ifmr->ifm_ulist,
 1443                                                 sizeof(int));
 1444                                 }
 1445                         }
 1446                 } else {
 1447                         VLAN_UNLOCK();
 1448                         error = EINVAL;
 1449                 }
 1450                 break;
 1451 
 1452         case SIOCSIFMEDIA:
 1453                 error = EINVAL;
 1454                 break;
 1455 
 1456         case SIOCSIFMTU:
 1457                 /*
 1458                  * Set the interface MTU.
 1459                  */
 1460                 VLAN_LOCK();
 1461                 if (TRUNK(ifv) != NULL) {
 1462                         if (ifr->ifr_mtu >
 1463                              (PARENT(ifv)->if_mtu - ifv->ifv_mtufudge) ||
 1464                             ifr->ifr_mtu <
 1465                              (ifv->ifv_mintu - ifv->ifv_mtufudge))
 1466                                 error = EINVAL;
 1467                         else
 1468                                 ifp->if_mtu = ifr->ifr_mtu;
 1469                 } else
 1470                         error = EINVAL;
 1471                 VLAN_UNLOCK();
 1472                 break;
 1473 
 1474         case SIOCSETVLAN:
 1475 #ifdef VIMAGE
 1476                 if (ifp->if_vnet != ifp->if_home_vnet) {
 1477                         error = EPERM;
 1478                         break;
 1479                 }
 1480 #endif
 1481                 error = copyin(ifr->ifr_data, &vlr, sizeof(vlr));
 1482                 if (error)
 1483                         break;
 1484                 if (vlr.vlr_parent[0] == '\0') {
 1485                         vlan_unconfig(ifp);
 1486                         break;
 1487                 }
 1488                 p = ifunit(vlr.vlr_parent);
 1489                 if (p == 0) {
 1490                         error = ENOENT;
 1491                         break;
 1492                 }
 1493                 /*
 1494                  * Don't let the caller set up a VLAN tag with
 1495                  * anything except VLID bits.
 1496                  */
 1497                 if (vlr.vlr_tag & ~EVL_VLID_MASK) {
 1498                         error = EINVAL;
 1499                         break;
 1500                 }
 1501                 error = vlan_config(ifv, p, vlr.vlr_tag);
 1502                 if (error)
 1503                         break;
 1504 
 1505                 /* Update flags on the parent, if necessary. */
 1506                 vlan_setflags(ifp, 1);
 1507                 break;
 1508 
 1509         case SIOCGETVLAN:
 1510 #ifdef VIMAGE
 1511                 if (ifp->if_vnet != ifp->if_home_vnet) {
 1512                         error = EPERM;
 1513                         break;
 1514                 }
 1515 #endif
 1516                 bzero(&vlr, sizeof(vlr));
 1517                 VLAN_LOCK();
 1518                 if (TRUNK(ifv) != NULL) {
 1519                         strlcpy(vlr.vlr_parent, PARENT(ifv)->if_xname,
 1520                             sizeof(vlr.vlr_parent));
 1521                         vlr.vlr_tag = ifv->ifv_tag;
 1522                 }
 1523                 VLAN_UNLOCK();
 1524                 error = copyout(&vlr, ifr->ifr_data, sizeof(vlr));
 1525                 break;
 1526                 
 1527         case SIOCSIFFLAGS:
 1528                 /*
 1529                  * We should propagate selected flags to the parent,
 1530                  * e.g., promiscuous mode.
 1531                  */
 1532                 if (TRUNK(ifv) != NULL)
 1533                         error = vlan_setflags(ifp, 1);
 1534                 break;
 1535 
 1536         case SIOCADDMULTI:
 1537         case SIOCDELMULTI:
 1538                 /*
 1539                  * If we don't have a parent, just remember the membership for
 1540                  * when we do.
 1541                  */
 1542                 if (TRUNK(ifv) != NULL)
 1543                         error = vlan_setmulti(ifp);
 1544                 break;
 1545 
 1546         default:
 1547                 error = ether_ioctl(ifp, cmd, data);
 1548         }
 1549 
 1550         return (error);
 1551 }

Cache object: e19a8e64b0ba60f07a9b6a0590f6b821


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.