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/netgraph/ng_bridge.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 /*
    3  * ng_bridge.c
    4  *
    5  * Copyright (c) 2000 Whistle Communications, Inc.
    6  * All rights reserved.
    7  * 
    8  * Subject to the following obligations and disclaimer of warranty, use and
    9  * redistribution of this software, in source or object code forms, with or
   10  * without modifications are expressly permitted by Whistle Communications;
   11  * provided, however, that:
   12  * 1. Any and all reproductions of the source or object code must include the
   13  *    copyright notice above and the following disclaimer of warranties; and
   14  * 2. No rights are granted, in any manner or form, to use Whistle
   15  *    Communications, Inc. trademarks, including the mark "WHISTLE
   16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
   17  *    such appears in the above copyright notice or in the software.
   18  * 
   19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
   20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
   21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
   22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
   23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
   24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
   25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
   26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
   27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
   28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
   29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
   30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
   31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
   32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
   35  * OF SUCH DAMAGE.
   36  *
   37  * Author: Archie Cobbs <archie@freebsd.org>
   38  *
   39  * $FreeBSD$
   40  */
   41 
   42 /*
   43  * ng_bridge(4) netgraph node type
   44  *
   45  * The node performs standard intelligent Ethernet bridging over
   46  * each of its connected hooks, or links.  A simple loop detection
   47  * algorithm is included which disables a link for priv->conf.loopTimeout
   48  * seconds when a host is seen to have jumped from one link to
   49  * another within priv->conf.minStableAge seconds.
   50  *
   51  * We keep a hashtable that maps Ethernet addresses to host info,
   52  * which is contained in struct ng_bridge_host's. These structures
   53  * tell us on which link the host may be found. A host's entry will
   54  * expire after priv->conf.maxStaleness seconds.
   55  *
   56  * This node is optimzed for stable networks, where machines jump
   57  * from one port to the other only rarely.
   58  */
   59 
   60 #include <sys/param.h>
   61 #include <sys/systm.h>
   62 #include <sys/kernel.h>
   63 #include <sys/malloc.h>
   64 #include <sys/mbuf.h>
   65 #include <sys/errno.h>
   66 #include <sys/syslog.h>
   67 #include <sys/socket.h>
   68 #include <sys/ctype.h>
   69 
   70 #include <net/if.h>
   71 #include <net/ethernet.h>
   72 
   73 #include <netinet/in.h>
   74 #include <netinet/ip_fw.h>
   75 
   76 #include <netgraph/ng_message.h>
   77 #include <netgraph/netgraph.h>
   78 #include <netgraph/ng_parse.h>
   79 #include <netgraph/ng_bridge.h>
   80 
   81 /* Per-link private data */
   82 struct ng_bridge_link {
   83         hook_p                          hook;           /* netgraph hook */
   84         u_int16_t                       loopCount;      /* loop ignore timer */
   85         struct ng_bridge_link_stats     stats;          /* link stats */
   86 };
   87 
   88 /* Per-node private data */
   89 struct ng_bridge_private {
   90         struct ng_bridge_bucket *tab;           /* hash table bucket array */
   91         struct ng_bridge_link   *links[NG_BRIDGE_MAX_LINKS];
   92         struct ng_bridge_config conf;           /* node configuration */
   93         node_p                  node;           /* netgraph node */
   94         u_int                   numHosts;       /* num entries in table */
   95         u_int                   numBuckets;     /* num buckets in table */
   96         u_int                   hashMask;       /* numBuckets - 1 */
   97         int                     numLinks;       /* num connected links */
   98         struct callout          timer;          /* one second periodic timer */
   99 };
  100 typedef struct ng_bridge_private *priv_p;
  101 
  102 /* Information about a host, stored in a hash table entry */
  103 struct ng_bridge_hent {
  104         struct ng_bridge_host           host;   /* actual host info */
  105         SLIST_ENTRY(ng_bridge_hent)     next;   /* next entry in bucket */
  106 };
  107 
  108 /* Hash table bucket declaration */
  109 SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
  110 
  111 /* Netgraph node methods */
  112 static ng_constructor_t ng_bridge_constructor;
  113 static ng_rcvmsg_t      ng_bridge_rcvmsg;
  114 static ng_shutdown_t    ng_bridge_rmnode;
  115 static ng_newhook_t     ng_bridge_newhook;
  116 static ng_rcvdata_t     ng_bridge_rcvdata;
  117 static ng_disconnect_t  ng_bridge_disconnect;
  118 
  119 /* Other internal functions */
  120 static struct   ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
  121 static int      ng_bridge_put(priv_p priv, const u_char *addr, int linkNum);
  122 static void     ng_bridge_rehash(priv_p priv);
  123 static void     ng_bridge_remove_hosts(priv_p priv, int linkNum);
  124 static void     ng_bridge_timeout(void *arg);
  125 static const    char *ng_bridge_nodename(node_p node);
  126 
  127 /* Ethernet broadcast */
  128 static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
  129     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  130 
  131 /* Store each hook's link number in the private field */
  132 #define LINK_NUM(hook)          (*(u_int16_t *)(&(hook)->private))
  133 
  134 /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
  135 #define ETHER_EQUAL(a,b)        (((const u_int32_t *)(a))[0] \
  136                                         == ((const u_int32_t *)(b))[0] \
  137                                     && ((const u_int16_t *)(a))[2] \
  138                                         == ((const u_int16_t *)(b))[2])
  139 
  140 /* Minimum and maximum number of hash buckets. Must be a power of two. */
  141 #define MIN_BUCKETS             (1 << 5)        /* 32 */
  142 #define MAX_BUCKETS             (1 << 14)       /* 16384 */
  143 
  144 /* Configuration default values */
  145 #define DEFAULT_LOOP_TIMEOUT    60
  146 #define DEFAULT_MAX_STALENESS   (15 * 60)       /* same as ARP timeout */
  147 #define DEFAULT_MIN_STABLE_AGE  1
  148 
  149 /******************************************************************
  150                     NETGRAPH PARSE TYPES
  151 ******************************************************************/
  152 
  153 /*
  154  * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
  155  */
  156 static int
  157 ng_bridge_getTableLength(const struct ng_parse_type *type,
  158         const u_char *start, const u_char *buf)
  159 {
  160         const struct ng_bridge_host_ary *const hary
  161             = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
  162 
  163         return hary->numHosts;
  164 }
  165 
  166 /* Parse type for struct ng_bridge_host_ary */
  167 static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
  168         = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
  169 static const struct ng_parse_type ng_bridge_host_type = {
  170         &ng_parse_struct_type,
  171         &ng_bridge_host_type_fields
  172 };
  173 static const struct ng_parse_array_info ng_bridge_hary_type_info = {
  174         &ng_bridge_host_type,
  175         ng_bridge_getTableLength
  176 };
  177 static const struct ng_parse_type ng_bridge_hary_type = {
  178         &ng_parse_array_type,
  179         &ng_bridge_hary_type_info
  180 };
  181 static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
  182         = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
  183 static const struct ng_parse_type ng_bridge_host_ary_type = {
  184         &ng_parse_struct_type,
  185         &ng_bridge_host_ary_type_fields
  186 };
  187 
  188 /* Parse type for struct ng_bridge_config */
  189 static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = {
  190         &ng_parse_uint8_type,
  191         NG_BRIDGE_MAX_LINKS
  192 };
  193 static const struct ng_parse_type ng_bridge_ipfwary_type = {
  194         &ng_parse_fixedarray_type,
  195         &ng_bridge_ipfwary_type_info
  196 };
  197 static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
  198         = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type);
  199 static const struct ng_parse_type ng_bridge_config_type = {
  200         &ng_parse_struct_type,
  201         &ng_bridge_config_type_fields
  202 };
  203 
  204 /* Parse type for struct ng_bridge_link_stat */
  205 static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
  206         = NG_BRIDGE_STATS_TYPE_INFO;
  207 static const struct ng_parse_type ng_bridge_stats_type = {
  208         &ng_parse_struct_type,
  209         &ng_bridge_stats_type_fields
  210 };
  211 
  212 /* List of commands and how to convert arguments to/from ASCII */
  213 static const struct ng_cmdlist ng_bridge_cmdlist[] = {
  214         {
  215           NGM_BRIDGE_COOKIE,
  216           NGM_BRIDGE_SET_CONFIG,
  217           "setconfig",
  218           &ng_bridge_config_type,
  219           NULL
  220         },
  221         {
  222           NGM_BRIDGE_COOKIE,
  223           NGM_BRIDGE_GET_CONFIG,
  224           "getconfig",
  225           NULL,
  226           &ng_bridge_config_type
  227         },
  228         {
  229           NGM_BRIDGE_COOKIE,
  230           NGM_BRIDGE_RESET,
  231           "reset",
  232           NULL,
  233           NULL
  234         },
  235         {
  236           NGM_BRIDGE_COOKIE,
  237           NGM_BRIDGE_GET_STATS,
  238           "getstats",
  239           &ng_parse_uint32_type,
  240           &ng_bridge_stats_type
  241         },
  242         {
  243           NGM_BRIDGE_COOKIE,
  244           NGM_BRIDGE_CLR_STATS,
  245           "clrstats",
  246           &ng_parse_uint32_type,
  247           NULL
  248         },
  249         {
  250           NGM_BRIDGE_COOKIE,
  251           NGM_BRIDGE_GETCLR_STATS,
  252           "getclrstats",
  253           &ng_parse_uint32_type,
  254           &ng_bridge_stats_type
  255         },
  256         {
  257           NGM_BRIDGE_COOKIE,
  258           NGM_BRIDGE_GET_TABLE,
  259           "gettable",
  260           NULL,
  261           &ng_bridge_host_ary_type
  262         },
  263         { 0 }
  264 };
  265 
  266 /* Node type descriptor */
  267 static struct ng_type ng_bridge_typestruct = {
  268         NG_VERSION,
  269         NG_BRIDGE_NODE_TYPE,
  270         NULL,
  271         ng_bridge_constructor,
  272         ng_bridge_rcvmsg,
  273         ng_bridge_rmnode,
  274         ng_bridge_newhook,
  275         NULL,
  276         NULL,
  277         ng_bridge_rcvdata,
  278         ng_bridge_rcvdata,
  279         ng_bridge_disconnect,
  280         ng_bridge_cmdlist,
  281 };
  282 NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
  283 
  284 /******************************************************************
  285                     NETGRAPH NODE METHODS
  286 ******************************************************************/
  287 
  288 /*
  289  * Node constructor
  290  */
  291 static int
  292 ng_bridge_constructor(node_p *nodep)
  293 {
  294         priv_p priv;
  295         int error;
  296 
  297         /* Allocate and initialize private info */
  298         MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
  299         if (priv == NULL)
  300                 return (ENOMEM);
  301         bzero(priv, sizeof(*priv));
  302         callout_init(&priv->timer);
  303 
  304         /* Allocate and initialize hash table, etc. */
  305         MALLOC(priv->tab, struct ng_bridge_bucket *,
  306             MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH, M_NOWAIT);
  307         if (priv->tab == NULL) {
  308                 FREE(priv, M_NETGRAPH);
  309                 return (ENOMEM);
  310         }
  311         bzero(priv->tab, MIN_BUCKETS * sizeof(*priv->tab));  /* init SLIST's */
  312         priv->numBuckets = MIN_BUCKETS;
  313         priv->hashMask = MIN_BUCKETS - 1;
  314         priv->conf.debugLevel = 1;
  315         priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
  316         priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
  317         priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
  318 
  319         /* Call superclass constructor */
  320         if ((error = ng_make_node_common(&ng_bridge_typestruct, nodep))) {
  321                 FREE(priv, M_NETGRAPH);
  322                 return (error);
  323         }
  324         (*nodep)->private = priv;
  325         priv->node = *nodep;
  326 
  327         /* Start timer; timer is always running while node is alive */
  328         callout_reset(&priv->timer, hz, ng_bridge_timeout, priv->node);
  329 
  330         /* Done */
  331         return (0);
  332 }
  333 
  334 /*
  335  * Method for attaching a new hook
  336  */
  337 static  int
  338 ng_bridge_newhook(node_p node, hook_p hook, const char *name)
  339 {
  340         const priv_p priv = node->private;
  341 
  342         /* Check for a link hook */
  343         if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
  344             strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
  345                 const char *cp;
  346                 char *eptr;
  347                 u_long linkNum;
  348 
  349                 cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
  350                 if (!isdigit(*cp) || (cp[0] == '' && cp[1] != '\0'))
  351                         return (EINVAL);
  352                 linkNum = strtoul(cp, &eptr, 10);
  353                 if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
  354                         return (EINVAL);
  355                 if (priv->links[linkNum] != NULL)
  356                         return (EISCONN);
  357                 MALLOC(priv->links[linkNum], struct ng_bridge_link *,
  358                     sizeof(*priv->links[linkNum]), M_NETGRAPH, M_NOWAIT);
  359                 if (priv->links[linkNum] == NULL)
  360                         return (ENOMEM);
  361                 bzero(priv->links[linkNum], sizeof(*priv->links[linkNum]));
  362                 priv->links[linkNum]->hook = hook;
  363                 LINK_NUM(hook) = linkNum;
  364                 priv->numLinks++;
  365                 return (0);
  366         }
  367 
  368         /* Unknown hook name */
  369         return (EINVAL);
  370 }
  371 
  372 /*
  373  * Receive a control message
  374  */
  375 static int
  376 ng_bridge_rcvmsg(node_p node, struct ng_mesg *msg,
  377         const char *retaddr, struct ng_mesg **rptr)
  378 {
  379         const priv_p priv = node->private;
  380         struct ng_mesg *resp = NULL;
  381         int error = 0;
  382 
  383         switch (msg->header.typecookie) {
  384         case NGM_BRIDGE_COOKIE:
  385                 switch (msg->header.cmd) {
  386                 case NGM_BRIDGE_GET_CONFIG:
  387                     {
  388                         struct ng_bridge_config *conf;
  389 
  390                         NG_MKRESPONSE(resp, msg,
  391                             sizeof(struct ng_bridge_config), M_NOWAIT);
  392                         if (resp == NULL) {
  393                                 error = ENOMEM;
  394                                 break;
  395                         }
  396                         conf = (struct ng_bridge_config *)resp->data;
  397                         *conf = priv->conf;     /* no sanity checking needed */
  398                         break;
  399                     }
  400                 case NGM_BRIDGE_SET_CONFIG:
  401                     {
  402                         struct ng_bridge_config *conf;
  403                         int i;
  404 
  405                         if (msg->header.arglen
  406                             != sizeof(struct ng_bridge_config)) {
  407                                 error = EINVAL;
  408                                 break;
  409                         }
  410                         conf = (struct ng_bridge_config *)msg->data;
  411                         priv->conf = *conf;
  412                         for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
  413                                 priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
  414                         break;
  415                     }
  416                 case NGM_BRIDGE_RESET:
  417                     {
  418                         int i;
  419 
  420                         /* Flush all entries in the hash table */
  421                         ng_bridge_remove_hosts(priv, -1);
  422 
  423                         /* Reset all loop detection counters and stats */
  424                         for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
  425                                 if (priv->links[i] == NULL)
  426                                         continue;
  427                                 priv->links[i]->loopCount = 0;
  428                                 bzero(&priv->links[i]->stats,
  429                                     sizeof(priv->links[i]->stats));
  430                         }
  431                         break;
  432                     }
  433                 case NGM_BRIDGE_GET_STATS:
  434                 case NGM_BRIDGE_CLR_STATS:
  435                 case NGM_BRIDGE_GETCLR_STATS:
  436                     {
  437                         struct ng_bridge_link *link;
  438                         int linkNum;
  439 
  440                         /* Get link number */
  441                         if (msg->header.arglen != sizeof(u_int32_t)) {
  442                                 error = EINVAL;
  443                                 break;
  444                         }
  445                         linkNum = *((u_int32_t *)msg->data);
  446                         if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
  447                                 error = EINVAL;
  448                                 break;
  449                         }
  450                         if ((link = priv->links[linkNum]) == NULL) {
  451                                 error = ENOTCONN;
  452                                 break;
  453                         }
  454 
  455                         /* Get/clear stats */
  456                         if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
  457                                 NG_MKRESPONSE(resp, msg,
  458                                     sizeof(link->stats), M_NOWAIT);
  459                                 if (resp == NULL) {
  460                                         error = ENOMEM;
  461                                         break;
  462                                 }
  463                                 bcopy(&link->stats,
  464                                     resp->data, sizeof(link->stats));
  465                         }
  466                         if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
  467                                 bzero(&link->stats, sizeof(link->stats));
  468                         break;
  469                     }
  470                 case NGM_BRIDGE_GET_TABLE:
  471                     {
  472                         struct ng_bridge_host_ary *ary;
  473                         struct ng_bridge_hent *hent;
  474                         int i = 0, bucket;
  475 
  476                         NG_MKRESPONSE(resp, msg, sizeof(*ary)
  477                             + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
  478                         if (resp == NULL) {
  479                                 error = ENOMEM;
  480                                 break;
  481                         }
  482                         ary = (struct ng_bridge_host_ary *)resp->data;
  483                         ary->numHosts = priv->numHosts;
  484                         for (bucket = 0; bucket < priv->numBuckets; bucket++) {
  485                                 SLIST_FOREACH(hent, &priv->tab[bucket], next)
  486                                         ary->hosts[i++] = hent->host;
  487                         }
  488                         break;
  489                     }
  490                 default:
  491                         error = EINVAL;
  492                         break;
  493                 }
  494                 break;
  495         default:
  496                 error = EINVAL;
  497                 break;
  498         }
  499 
  500         /* Done */
  501         if (rptr)
  502                 *rptr = resp;
  503         else if (resp != NULL)
  504                 FREE(resp, M_NETGRAPH);
  505         FREE(msg, M_NETGRAPH);
  506         return (error);
  507 }
  508 
  509 /*
  510  * Receive data on a hook
  511  */
  512 static int
  513 ng_bridge_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
  514 {
  515         const node_p node = hook->node;
  516         const priv_p priv = node->private;
  517         struct ng_bridge_host *host;
  518         struct ng_bridge_link *link;
  519         struct ether_header *eh;
  520         int error = 0, linkNum, linksSeen;
  521         int manycast;
  522         struct ng_bridge_link *firstLink;
  523 
  524         /* Get link number */
  525         linkNum = LINK_NUM(hook);
  526         KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
  527             ("%s: linkNum=%u", __FUNCTION__, linkNum));
  528         link = priv->links[linkNum];
  529         KASSERT(link != NULL, ("%s: link%d null", __FUNCTION__, linkNum));
  530 
  531         /* Sanity check packet and pull up header */
  532         if (m->m_pkthdr.len < ETHER_HDR_LEN) {
  533                 link->stats.recvRunts++;
  534                 NG_FREE_DATA(m, meta);
  535                 return (EINVAL);
  536         }
  537         if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
  538                 link->stats.memoryFailures++;
  539                 NG_FREE_META(meta);
  540                 return (ENOBUFS);
  541         }
  542         eh = mtod(m, struct ether_header *);
  543         if ((eh->ether_shost[0] & 1) != 0) {
  544                 link->stats.recvInvalid++;
  545                 NG_FREE_DATA(m, meta);
  546                 return (EINVAL);
  547         }
  548 
  549         /* Is link disabled due to a loopback condition? */
  550         if (link->loopCount != 0) {
  551                 link->stats.loopDrops++;
  552                 NG_FREE_DATA(m, meta);
  553                 return (ELOOP);         /* XXX is this an appropriate error? */
  554         }
  555 
  556         /* Update stats */
  557         link->stats.recvPackets++;
  558         link->stats.recvOctets += m->m_pkthdr.len;
  559         if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
  560                 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
  561                         link->stats.recvBroadcasts++;
  562                         manycast = 2;
  563                 } else
  564                         link->stats.recvMulticasts++;
  565         }
  566 
  567         /* Look up packet's source Ethernet address in hashtable */
  568         if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
  569 
  570                 /* Update time since last heard from this host */
  571                 host->staleness = 0;
  572 
  573                 /* Did host jump to a different link? */
  574                 if (host->linkNum != linkNum) {
  575 
  576                         /*
  577                          * If the host's old link was recently established
  578                          * on the old link and it's already jumped to a new
  579                          * link, declare a loopback condition.
  580                          */
  581                         if (host->age < priv->conf.minStableAge) {
  582 
  583                                 /* Log the problem */
  584                                 if (priv->conf.debugLevel >= 2) {
  585                                         struct ifnet *ifp = m->m_pkthdr.rcvif;
  586                                         char suffix[32];
  587 
  588                                         if (ifp != NULL)
  589                                                 snprintf(suffix, sizeof(suffix),
  590                                                     " (%s%d)", ifp->if_name,
  591                                                     ifp->if_unit);
  592                                         else
  593                                                 *suffix = '\0';
  594                                         log(LOG_WARNING, "ng_bridge: %s:"
  595                                             " loopback detected on %s%s\n",
  596                                             ng_bridge_nodename(node),
  597                                             hook->name, suffix);
  598                                 }
  599 
  600                                 /* Mark link as linka non grata */
  601                                 link->loopCount = priv->conf.loopTimeout;
  602                                 link->stats.loopDetects++;
  603 
  604                                 /* Forget all hosts on this link */
  605                                 ng_bridge_remove_hosts(priv, linkNum);
  606 
  607                                 /* Drop packet */
  608                                 link->stats.loopDrops++;
  609                                 NG_FREE_DATA(m, meta);
  610                                 return (ELOOP);         /* XXX appropriate? */
  611                         }
  612 
  613                         /* Move host over to new link */
  614                         host->linkNum = linkNum;
  615                         host->age = 0;
  616                 }
  617         } else {
  618                 if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
  619                         link->stats.memoryFailures++;
  620                         NG_FREE_DATA(m, meta);
  621                         return (ENOMEM);
  622                 }
  623         }
  624 
  625         /* Run packet through ipfw processing, if enabled */
  626         if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) {
  627                 /* XXX not implemented yet */
  628         }
  629 
  630         /*
  631          * If unicast and destination host known, deliver to host's link,
  632          * unless it is the same link as the packet came in on.
  633          */
  634         if (!manycast) {
  635 
  636                 /* Determine packet destination link */
  637                 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
  638                         struct ng_bridge_link *const destLink
  639                             = priv->links[host->linkNum];
  640 
  641                         /* If destination same as incoming link, do nothing */
  642                         KASSERT(destLink != NULL,
  643                             ("%s: link%d null", __FUNCTION__, host->linkNum));
  644                         if (destLink == link) {
  645                                 NG_FREE_DATA(m, meta);
  646                                 return (0);
  647                         }
  648 
  649                         /* Deliver packet out the destination link */
  650                         destLink->stats.xmitPackets++;
  651                         destLink->stats.xmitOctets += m->m_pkthdr.len;
  652                         NG_SEND_DATA(error, destLink->hook, m, meta);
  653                         return (error);
  654                 }
  655 
  656                 /* Destination host is not known */
  657                 link->stats.recvUnknown++;
  658         }
  659 
  660         /* Distribute unknown, multicast, broadcast pkts to all other links */
  661         firstLink = NULL;
  662         for (linkNum = linksSeen = 0; linksSeen <= priv->numLinks; linkNum++) {
  663                 struct ng_bridge_link *destLink;
  664                 meta_p meta2 = NULL;
  665                 struct mbuf *m2 = NULL;
  666 
  667                 /*
  668                  * If we have checked all the links then now
  669                  * send the original on its reserved link
  670                  */
  671                 if (linksSeen == priv->numLinks) {
  672                         /* If we never saw a good link, leave. */
  673                         if (firstLink == NULL) {
  674                                 NG_FREE_DATA(m, meta);
  675                                 return (0);
  676                         }       
  677                         destLink = firstLink;
  678                 } else {
  679                         destLink = priv->links[linkNum];
  680                         if (destLink != NULL)
  681                                 linksSeen++;
  682                         /* Skip incoming link and disconnected links */
  683                         if (destLink == NULL || destLink == link) {
  684                                 continue;
  685                         }
  686                         if (firstLink == NULL) {
  687                                 /*
  688                                  * This is the first usable link we have found.
  689                                  * Reserve it for the originals.
  690                                  * If we never find another we save a copy.
  691                                  */
  692                                 firstLink = destLink;
  693                                 continue;
  694                         }
  695 
  696                         /*
  697                          * It's usable link but not the reserved (first) one.
  698                          * Copy mbuf and meta info for sending.
  699                          */
  700                         m2 = m_dup(m, M_NOWAIT);        /* XXX m_copypacket() */
  701                         if (m2 == NULL) {
  702                                 link->stats.memoryFailures++;
  703                                 NG_FREE_DATA(m, meta);
  704                                 return (ENOBUFS);
  705                         }
  706                         if (meta != NULL
  707                             && (meta2 = ng_copy_meta(meta)) == NULL) {
  708                                 link->stats.memoryFailures++;
  709                                 m_freem(m2);
  710                                 NG_FREE_DATA(m, meta);
  711                                 return (ENOMEM);
  712                         }
  713                 }
  714 
  715                 /* Update stats */
  716                 destLink->stats.xmitPackets++;
  717                 destLink->stats.xmitOctets += m->m_pkthdr.len;
  718                 switch (manycast) {
  719                 case 0:                                 /* unicast */
  720                         break;
  721                 case 1:                                 /* multicast */
  722                         destLink->stats.xmitMulticasts++;
  723                         break;
  724                 case 2:                                 /* broadcast */
  725                         destLink->stats.xmitBroadcasts++;
  726                         break;
  727                 }
  728 
  729                 /* Send packet */
  730                 if (destLink == firstLink) { 
  731                         /*
  732                          * If we've sent all the others, send the original
  733                          * on the first link we found.
  734                          */
  735                         NG_SEND_DATA(error, destLink->hook, m, meta);
  736                         break; /* always done last - not really needed. */
  737                 } else {
  738                         NG_SEND_DATA(error, destLink->hook, m2, meta2);
  739                 }
  740         }
  741         return (error);
  742 }
  743 
  744 /*
  745  * Shutdown node
  746  */
  747 static int
  748 ng_bridge_rmnode(node_p node)
  749 {
  750         const priv_p priv = node->private;
  751 
  752         /*
  753          * Shut down everything except the timer. There's no way to
  754          * avoid another possible timeout event (it may have already
  755          * been dequeued), so we can't free the node yet.
  756          */
  757         ng_unname(node);
  758         ng_cutlinks(node);              /* frees all link and host info */
  759         KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
  760             ("%s: numLinks=%d numHosts=%d",
  761             __FUNCTION__, priv->numLinks, priv->numHosts));
  762         FREE(priv->tab, M_NETGRAPH);
  763 
  764         /* NG_INVALID flag is now set so node will be freed at next timeout */
  765         return (0);
  766 }
  767 
  768 /*
  769  * Hook disconnection.
  770  */
  771 static int
  772 ng_bridge_disconnect(hook_p hook)
  773 {
  774         const priv_p priv = hook->node->private;
  775         int linkNum;
  776 
  777         /* Get link number */
  778         linkNum = LINK_NUM(hook);
  779         KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
  780             ("%s: linkNum=%u", __FUNCTION__, linkNum));
  781 
  782         /* Remove all hosts associated with this link */
  783         ng_bridge_remove_hosts(priv, linkNum);
  784 
  785         /* Free associated link information */
  786         KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __FUNCTION__));
  787         FREE(priv->links[linkNum], M_NETGRAPH);
  788         priv->links[linkNum] = NULL;
  789         priv->numLinks--;
  790 
  791         /* If no more hooks, go away */
  792         if (hook->node->numhooks == 0)
  793                 ng_rmnode(hook->node);
  794         return (0);
  795 }
  796 
  797 /******************************************************************
  798                     HASH TABLE FUNCTIONS
  799 ******************************************************************/
  800 
  801 /*
  802  * Hash algorithm
  803  *
  804  * Only hashing bytes 3-6 of the Ethernet address is sufficient and fast.
  805  */
  806 #define HASH(addr,mask)         ( (((const u_int16_t *)(addr))[0]       \
  807                                  ^ ((const u_int16_t *)(addr))[1]       \
  808                                  ^ ((const u_int16_t *)(addr))[2]) & (mask) )
  809 
  810 /*
  811  * Find a host entry in the table.
  812  */
  813 static struct ng_bridge_host *
  814 ng_bridge_get(priv_p priv, const u_char *addr)
  815 {
  816         const int bucket = HASH(addr, priv->hashMask);
  817         struct ng_bridge_hent *hent;
  818 
  819         SLIST_FOREACH(hent, &priv->tab[bucket], next) {
  820                 if (ETHER_EQUAL(hent->host.addr, addr))
  821                         return (&hent->host);
  822         }
  823         return (NULL);
  824 }
  825 
  826 /*
  827  * Add a new host entry to the table. This assumes the host doesn't
  828  * already exist in the table. Returns 1 on success, 0 if there
  829  * was a memory allocation failure.
  830  */
  831 static int
  832 ng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
  833 {
  834         const int bucket = HASH(addr, priv->hashMask);
  835         struct ng_bridge_hent *hent;
  836 
  837 #ifdef INVARIANTS
  838         /* Assert that entry does not already exist in hashtable */
  839         SLIST_FOREACH(hent, &priv->tab[bucket], next) {
  840                 KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
  841                     ("%s: entry %6D exists in table", __FUNCTION__, addr, ":"));
  842         }
  843 #endif
  844 
  845         /* Allocate and initialize new hashtable entry */
  846         MALLOC(hent, struct ng_bridge_hent *,
  847             sizeof(*hent), M_NETGRAPH, M_NOWAIT);
  848         if (hent == NULL)
  849                 return (0);
  850         bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
  851         hent->host.linkNum = linkNum;
  852         hent->host.staleness = 0;
  853         hent->host.age = 0;
  854 
  855         /* Add new element to hash bucket */
  856         SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
  857         priv->numHosts++;
  858 
  859         /* Resize table if necessary */
  860         ng_bridge_rehash(priv);
  861         return (1);
  862 }
  863 
  864 /*
  865  * Resize the hash table. We try to maintain the number of buckets
  866  * such that the load factor is in the range 0.25 to 1.0.
  867  *
  868  * If we can't get the new memory then we silently fail. This is OK
  869  * because things will still work and we'll try again soon anyway.
  870  */
  871 static void
  872 ng_bridge_rehash(priv_p priv)
  873 {
  874         struct ng_bridge_bucket *newTab;
  875         int oldBucket, newBucket;
  876         int newNumBuckets;
  877         u_int newMask;
  878 
  879         /* Is table too full or too empty? */
  880         if (priv->numHosts > priv->numBuckets
  881             && (priv->numBuckets << 1) <= MAX_BUCKETS)
  882                 newNumBuckets = priv->numBuckets << 1;
  883         else if (priv->numHosts < (priv->numBuckets >> 2)
  884             && (priv->numBuckets >> 2) >= MIN_BUCKETS)
  885                 newNumBuckets = priv->numBuckets >> 2;
  886         else
  887                 return;
  888         newMask = newNumBuckets - 1;
  889 
  890         /* Allocate and initialize new table */
  891         MALLOC(newTab, struct ng_bridge_bucket *,
  892             newNumBuckets * sizeof(*newTab), M_NETGRAPH, M_NOWAIT);
  893         if (newTab == NULL)
  894                 return;
  895         bzero(newTab, newNumBuckets * sizeof(*newTab));
  896 
  897         /* Move all entries from old table to new table */
  898         for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
  899                 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
  900 
  901                 while (!SLIST_EMPTY(oldList)) {
  902                         struct ng_bridge_hent *const hent
  903                             = SLIST_FIRST(oldList);
  904 
  905                         SLIST_REMOVE_HEAD(oldList, next);
  906                         newBucket = HASH(hent->host.addr, newMask);
  907                         SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
  908                 }
  909         }
  910 
  911         /* Replace old table with new one */
  912         if (priv->conf.debugLevel >= 3) {
  913                 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
  914                     ng_bridge_nodename(priv->node),
  915                     priv->numBuckets, newNumBuckets);
  916         }
  917         FREE(priv->tab, M_NETGRAPH);
  918         priv->numBuckets = newNumBuckets;
  919         priv->hashMask = newMask;
  920         priv->tab = newTab;
  921         return;
  922 }
  923 
  924 /******************************************************************
  925                     MISC FUNCTIONS
  926 ******************************************************************/
  927 
  928 /*
  929  * Remove all hosts associated with a specific link from the hashtable.
  930  * If linkNum == -1, then remove all hosts in the table.
  931  */
  932 static void
  933 ng_bridge_remove_hosts(priv_p priv, int linkNum)
  934 {
  935         int bucket;
  936 
  937         for (bucket = 0; bucket < priv->numBuckets; bucket++) {
  938                 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
  939 
  940                 while (*hptr != NULL) {
  941                         struct ng_bridge_hent *const hent = *hptr;
  942 
  943                         if (linkNum == -1 || hent->host.linkNum == linkNum) {
  944                                 *hptr = SLIST_NEXT(hent, next);
  945                                 FREE(hent, M_NETGRAPH);
  946                                 priv->numHosts--;
  947                         } else
  948                                 hptr = &SLIST_NEXT(hent, next);
  949                 }
  950         }
  951 }
  952 
  953 /*
  954  * Handle our once-per-second timeout event. We do two things:
  955  * we decrement link->loopCount for those links being muted due to
  956  * a detected loopback condition, and we remove any hosts from
  957  * the hashtable whom we haven't heard from in a long while.
  958  *
  959  * If the node has the NG_INVALID flag set, our job is to kill it.
  960  */
  961 static void
  962 ng_bridge_timeout(void *arg)
  963 {
  964         const node_p node = arg;
  965         const priv_p priv = node->private;
  966         int s, bucket;
  967         int counter = 0;
  968         int linkNum;
  969 
  970         /* If node was shut down, this is the final lingering timeout */
  971         s = splnet();
  972         if ((node->flags & NG_INVALID) != 0) {
  973                 FREE(priv, M_NETGRAPH);
  974                 node->private = NULL;
  975                 ng_unref(node);
  976                 splx(s);
  977                 return;
  978         }
  979 
  980         /* Register a new timeout, keeping the existing node reference */
  981         callout_reset(&priv->timer, hz, ng_bridge_timeout, node);
  982 
  983         /* Update host time counters and remove stale entries */
  984         for (bucket = 0; bucket < priv->numBuckets; bucket++) {
  985                 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
  986 
  987                 while (*hptr != NULL) {
  988                         struct ng_bridge_hent *const hent = *hptr;
  989 
  990                         /* Make sure host's link really exists */
  991                         KASSERT(priv->links[hent->host.linkNum] != NULL,
  992                             ("%s: host %6D on nonexistent link %d\n",
  993                             __FUNCTION__, hent->host.addr, ":",
  994                             hent->host.linkNum));
  995 
  996                         /* Remove hosts we haven't heard from in a while */
  997                         if (++hent->host.staleness >= priv->conf.maxStaleness) {
  998                                 *hptr = SLIST_NEXT(hent, next);
  999                                 FREE(hent, M_NETGRAPH);
 1000                                 priv->numHosts--;
 1001                         } else {
 1002                                 if (hent->host.age < 0xffff)
 1003                                         hent->host.age++;
 1004                                 hptr = &SLIST_NEXT(hent, next);
 1005                                 counter++;
 1006                         }
 1007                 }
 1008         }
 1009         KASSERT(priv->numHosts == counter,
 1010             ("%s: hosts: %d != %d", __FUNCTION__, priv->numHosts, counter));
 1011 
 1012         /* Decrease table size if necessary */
 1013         ng_bridge_rehash(priv);
 1014 
 1015         /* Decrease loop counter on muted looped back links */
 1016         for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
 1017                 struct ng_bridge_link *const link = priv->links[linkNum];
 1018 
 1019                 if (link != NULL) {
 1020                         if (link->loopCount != 0) {
 1021                                 link->loopCount--;
 1022                                 if (link->loopCount == 0
 1023                                     && priv->conf.debugLevel >= 2) {
 1024                                         log(LOG_INFO, "ng_bridge: %s:"
 1025                                             " restoring looped back link%d\n",
 1026                                             ng_bridge_nodename(node), linkNum);
 1027                                 }
 1028                         }
 1029                         counter++;
 1030                 }
 1031         }
 1032         KASSERT(priv->numLinks == counter,
 1033             ("%s: links: %d != %d", __FUNCTION__, priv->numLinks, counter));
 1034 
 1035         /* Done */
 1036         splx(s);
 1037 }
 1038 
 1039 /*
 1040  * Return node's "name", even if it doesn't have one.
 1041  */
 1042 static const char *
 1043 ng_bridge_nodename(node_p node)
 1044 {
 1045         static char name[NG_NODELEN+1];
 1046 
 1047         if (node->name != NULL)
 1048                 snprintf(name, sizeof(name), "%s", node->name);
 1049         else
 1050                 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
 1051         return name;
 1052 }
 1053 

Cache object: 645c31840d41f1d8feaf3b0aa57b872a


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