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

Cache object: e61ab80dd612993fd8b484375cfbf07d


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