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

Cache object: 5f85a15d651920e3125fd950ec5dfa88


[ 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.