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


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

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

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

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

Cache object: 1eab5641917d9c14c977c4e3d18ca9b0


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