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/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  * Copyright (c) 1998-2002 Luigi Rizzo
    3  *
    4  * Work partly supported by: Cisco Systems, Inc. - NSITE lab, RTP, NC
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  *
   27  * $FreeBSD: releng/5.3/sys/net/bridge.c 136588 2004-10-16 08:43:07Z cvs2svn $
   28  */
   29 
   30 /*
   31  * This code implements bridging in FreeBSD. It only acts on ethernet
   32  * interfaces, including VLANs (others are still usable for routing).
   33  * A FreeBSD host can implement multiple logical bridges, called
   34  * "clusters". Each cluster is made of a set of interfaces, and
   35  * identified by a "cluster-id" which is a number in the range 1..2^16-1.
   36  *
   37  * Bridging is enabled by the sysctl variable
   38  *      net.link.ether.bridge.enable
   39  * the grouping of interfaces into clusters is done with
   40  *      net.link.ether.bridge.config
   41  * containing a list of interfaces each optionally followed by
   42  * a colon and the cluster it belongs to (1 is the default).
   43  * Separators can be * spaces, commas or tabs, e.g.
   44  *      net.link.ether.bridge.config="fxp0:2 fxp1:2 dc0 dc1:1"
   45  * Optionally bridged packets can be passed through the firewall,
   46  * this is controlled by the variable
   47  *      net.link.ether.bridge.ipfw
   48  *
   49  * For each cluster there is a descriptor (cluster_softc) storing
   50  * the following data structures:
   51  * - a hash table with the MAC address and destination interface for each
   52  *   known node. The table is indexed using a hash of the source address.
   53  * - an array with the MAC addresses of the interfaces used in the cluster.
   54  *
   55  * Input packets are tapped near the beginning of ether_input(), and
   56  * analysed by bridge_in(). Depending on the result, the packet
   57  * can be forwarded to one or more output interfaces using bdg_forward(),
   58  * and/or sent to the upper layer (e.g. in case of multicast).
   59  *
   60  * Output packets are intercepted near the end of ether_output().
   61  * The correct destination is selected by bridge_dst_lookup(),
   62  * and then forwarding is done by bdg_forward().
   63  *
   64  * The arp code is also modified to let a machine answer to requests
   65  * irrespective of the port the request came from.
   66  *
   67  * In case of loops in the bridging topology, the bridge detects this
   68  * event and temporarily mutes output bridging on one of the ports.
   69  * Periodically, interfaces are unmuted by bdg_timeout().
   70  * Muting is only implemented as a safety measure, and also as
   71  * a mechanism to support a user-space implementation of the spanning
   72  * tree algorithm.
   73  *
   74  * To build a bridging kernel, use the following option
   75  *    option BRIDGE
   76  * and then at runtime set the sysctl variable to enable bridging.
   77  *
   78  * Only one interface per cluster is supposed to have addresses set (but
   79  * there are no substantial problems if you set addresses for none or
   80  * for more than one interface).
   81  * Bridging will act before routing, but nothing prevents a machine
   82  * from doing both (modulo bugs in the implementation...).
   83  *
   84  * THINGS TO REMEMBER
   85  *  - bridging is incompatible with multicast routing on the same
   86  *    machine. There is not an easy fix to this.
   87  *  - be very careful when bridging VLANs
   88  *  - loop detection is still not very robust.
   89  */
   90 
   91 #include <sys/param.h>
   92 #include <sys/mbuf.h>
   93 #include <sys/malloc.h>
   94 #include <sys/protosw.h>
   95 #include <sys/systm.h>
   96 #include <sys/socket.h> /* for net/if.h */
   97 #include <sys/ctype.h>  /* string functions */
   98 #include <sys/kernel.h>
   99 #include <sys/module.h>
  100 #include <sys/sysctl.h>
  101 
  102 #include <net/ethernet.h>
  103 #include <net/if.h>
  104 #include <net/if_arp.h>         /* for struct arpcom */
  105 #include <net/if_types.h>
  106 #include <net/if_var.h>
  107 #include <net/pfil.h>
  108 
  109 #include <netinet/in.h>
  110 #include <netinet/in_systm.h>
  111 #include <netinet/in_var.h>
  112 #include <netinet/ip.h>
  113 #include <netinet/ip_var.h>
  114 
  115 #include <net/route.h>
  116 #include <netinet/ip_fw.h>
  117 #include <netinet/ip_dummynet.h>
  118 #include <net/bridge.h>
  119 
  120 /*--------------------*/
  121 
  122 #define ETHER_ADDR_COPY(_dst,_src)      bcopy(_src, _dst, ETHER_ADDR_LEN)
  123 #define ETHER_ADDR_EQ(_a1,_a2)          (bcmp(_a1, _a2, ETHER_ADDR_LEN) == 0)
  124 
  125 /*
  126  * For each cluster, source MAC addresses are stored into a hash
  127  * table which locates the port they reside on.
  128  */
  129 #define HASH_SIZE 8192  /* Table size, must be a power of 2 */
  130 
  131 typedef struct hash_table {             /* each entry.          */
  132     struct ifnet *      name;
  133     u_char              etheraddr[ETHER_ADDR_LEN];
  134     u_int16_t           used;           /* also, padding        */
  135 } bdg_hash_table ;
  136 
  137 /*
  138  * The hash function applied to MAC addresses. Out of the 6 bytes,
  139  * the last ones tend to vary more. Since we are on a little endian machine,
  140  * we have to do some gimmick...
  141  */
  142 #define HASH_FN(addr)   (       \
  143     ntohs( ((u_int16_t *)addr)[1] ^ ((u_int16_t *)addr)[2] ) & (HASH_SIZE -1))
  144 
  145 /*
  146  * This is the data structure where local addresses are stored.
  147  */
  148 struct bdg_addr {
  149     u_char      etheraddr[ETHER_ADDR_LEN];
  150     u_int16_t   _padding;
  151 };
  152 
  153 /*
  154  * The configuration of each cluster includes the cluster id, a pointer to
  155  * the hash table, and an array of local MAC addresses (of size "ports").
  156  */
  157 struct cluster_softc {
  158     u_int16_t   cluster_id;
  159     u_int16_t   ports;
  160     bdg_hash_table *ht;
  161     struct bdg_addr     *my_macs;       /* local MAC addresses */
  162 };
  163 
  164 
  165 extern struct protosw inetsw[];                 /* from netinet/ip_input.c */
  166 
  167 static int n_clusters;                          /* number of clusters */
  168 static struct cluster_softc *clusters;
  169 
  170 static struct mtx bdg_mtx;
  171 #define BDG_LOCK_INIT()         mtx_init(&bdg_mtx, "bridge", NULL, MTX_DEF)
  172 #define BDG_LOCK_DESTROY()      mtx_destroy(&bdg_mtx)
  173 #define BDG_LOCK()              mtx_lock(&bdg_mtx)
  174 #define BDG_UNLOCK()            mtx_unlock(&bdg_mtx)
  175 #define BDG_LOCK_ASSERT()       mtx_assert(&bdg_mtx, MA_OWNED)
  176 
  177 #define BDG_MUTED(ifp) (ifp2sc[ifp->if_index].flags & IFF_MUTE)
  178 #define BDG_MUTE(ifp) ifp2sc[ifp->if_index].flags |= IFF_MUTE
  179 #define BDG_CLUSTER(ifp) (ifp2sc[ifp->if_index].cluster)
  180 
  181 #define BDG_SAMECLUSTER(ifp,src) \
  182         (src == NULL || BDG_CLUSTER(ifp) == BDG_CLUSTER(src) )
  183 
  184 #ifdef __i386__
  185 #define BDG_MATCH(a,b) ( \
  186     ((u_int16_t *)(a))[2] == ((u_int16_t *)(b))[2] && \
  187     *((u_int32_t *)(a)) == *((u_int32_t *)(b)) )
  188 #define IS_ETHER_BROADCAST(a) ( \
  189         *((u_int32_t *)(a)) == 0xffffffff && \
  190         ((u_int16_t *)(a))[2] == 0xffff )
  191 #else
  192 /* for machines that do not support unaligned access */
  193 #define BDG_MATCH(a,b)          ETHER_ADDR_EQ(a,b)
  194 #define IS_ETHER_BROADCAST(a)   ETHER_ADDR_EQ(a,"\377\377\377\377\377\377")
  195 #endif
  196 
  197 SYSCTL_DECL(_net_link_ether);
  198 SYSCTL_NODE(_net_link_ether, OID_AUTO, bridge, CTLFLAG_RD, 0,
  199         "Bridge parameters");
  200 static char bridge_version[] = "031224";
  201 SYSCTL_STRING(_net_link_ether_bridge, OID_AUTO, version, CTLFLAG_RD,
  202         bridge_version, 0, "software version");
  203 
  204 #define BRIDGE_DEBUG
  205 #ifdef BRIDGE_DEBUG
  206 int     bridge_debug = 0;
  207 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, debug, CTLFLAG_RW, &bridge_debug,
  208             0, "control debugging printfs");
  209 #define DPRINTF(X)      if (bridge_debug) printf X
  210 #else
  211 #define DPRINTF(X)
  212 #endif
  213 
  214 #ifdef BRIDGE_TIMING
  215 /*
  216  * For timing-related debugging, you can use the following macros.
  217  * remember, rdtsc() only works on Pentium-class machines
  218 
  219     quad_t ticks;
  220     DDB(ticks = rdtsc();)
  221     ... interesting code ...
  222     DDB(bdg_fw_ticks += (u_long)(rdtsc() - ticks) ; bdg_fw_count++ ;)
  223 
  224  *
  225  */
  226 #define DDB(x)  x
  227 
  228 static int bdg_fw_avg;
  229 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, fw_avg, CTLFLAG_RW,
  230             &bdg_fw_avg, 0,"Cycle counter avg");
  231 static int bdg_fw_ticks;
  232 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, fw_ticks, CTLFLAG_RW,
  233             &bdg_fw_ticks, 0,"Cycle counter item");
  234 static int bdg_fw_count;
  235 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, fw_count, CTLFLAG_RW,
  236             &bdg_fw_count, 0,"Cycle counter count");
  237 #else
  238 #define DDB(x)
  239 #endif
  240 
  241 static int bdginit(void);
  242 static void parse_bdg_cfg(void);
  243 
  244 static int bdg_ipf;             /* IPFilter enabled in bridge */
  245 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, ipf, CTLFLAG_RW,
  246             &bdg_ipf, 0,"Pass bridged pkts through IPFilter");
  247 static int bdg_ipfw;
  248 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, ipfw, CTLFLAG_RW,
  249             &bdg_ipfw,0,"Pass bridged pkts through firewall");
  250 
  251 static int bdg_copy;
  252 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, copy, CTLFLAG_RW,
  253         &bdg_copy, 0, "Force packet copy in bdg_forward");
  254 
  255 int bdg_ipfw_drops;
  256 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, ipfw_drop,
  257         CTLFLAG_RW, &bdg_ipfw_drops,0,"");
  258 int bdg_ipfw_colls;
  259 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, ipfw_collisions,
  260         CTLFLAG_RW, &bdg_ipfw_colls,0,"");
  261 
  262 static int bdg_thru;
  263 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, packets, CTLFLAG_RW,
  264         &bdg_thru, 0, "Packets through bridge");
  265 static int bdg_dropped;
  266 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, dropped, CTLFLAG_RW,
  267         &bdg_dropped, 0, "Packets dropped in bdg_forward");
  268 static int bdg_predict;
  269 SYSCTL_INT(_net_link_ether_bridge, OID_AUTO, predict, CTLFLAG_RW,
  270         &bdg_predict, 0, "Correctly predicted header location");
  271 
  272 #ifdef BRIDGE_DEBUG
  273 static char *bdg_dst_names[] = {
  274         "BDG_NULL    ",
  275         "BDG_BCAST   ",
  276         "BDG_MCAST   ",
  277         "BDG_LOCAL   ",
  278         "BDG_DROP    ",
  279         "BDG_UNKNOWN ",
  280         "BDG_IN      ",
  281         "BDG_OUT     ",
  282         "BDG_FORWARD " };
  283 #endif /* BRIDGE_DEBUG */
  284 
  285 /*
  286  * System initialization
  287  */
  288 static struct bdg_stats bdg_stats ;
  289 SYSCTL_STRUCT(_net_link_ether_bridge, OID_AUTO, stats, CTLFLAG_RD,
  290         &bdg_stats, bdg_stats, "bridge statistics");
  291 
  292 static struct callout bdg_callout;
  293 
  294 /*
  295  * Add an interface to a cluster, possibly creating a new entry in
  296  * the cluster table. This requires reallocation of the table and
  297  * updating pointers in ifp2sc.
  298  */
  299 static struct cluster_softc *
  300 add_cluster(u_int16_t cluster_id, struct ifnet *ifp)
  301 {
  302     struct cluster_softc *c = NULL;
  303     int i;
  304 
  305     BDG_LOCK_ASSERT();
  306 
  307     for (i = 0; i < n_clusters ; i++)
  308         if (clusters[i].cluster_id == cluster_id)
  309             goto found;
  310 
  311     /* Not found, need to reallocate */
  312     c = malloc((1+n_clusters) * sizeof (*c), M_IFADDR, M_NOWAIT | M_ZERO);
  313     if (c == NULL) {/* malloc failure */
  314         printf("-- bridge: cannot add new cluster\n");
  315         goto bad;
  316     }
  317     c[n_clusters].ht = (struct hash_table *)
  318             malloc(HASH_SIZE * sizeof(struct hash_table),
  319                 M_IFADDR, M_NOWAIT | M_ZERO);
  320     if (c[n_clusters].ht == NULL) {
  321         printf("-- bridge: cannot allocate hash table for new cluster\n");
  322         goto bad;
  323     }
  324     c[n_clusters].my_macs = (struct bdg_addr *)
  325             malloc(BDG_MAX_PORTS * sizeof(struct bdg_addr),
  326                 M_IFADDR, M_NOWAIT | M_ZERO);
  327     if (c[n_clusters].my_macs == NULL) {
  328         printf("-- bridge: cannot allocate mac addr table for new cluster\n");
  329         free(c[n_clusters].ht, M_IFADDR);
  330         goto bad;
  331     }
  332 
  333     c[n_clusters].cluster_id = cluster_id;
  334     c[n_clusters].ports = 0;
  335     /*
  336      * now copy old descriptors here
  337      */
  338     if (n_clusters > 0) {
  339         for (i=0; i < n_clusters; i++)
  340             c[i] = clusters[i];
  341         /*
  342          * and finally update pointers in ifp2sc
  343          */
  344         for (i = 0 ; i < if_index && i < BDG_MAX_PORTS; i++)
  345             if (ifp2sc[i].cluster != NULL)
  346                 ifp2sc[i].cluster = c + (ifp2sc[i].cluster - clusters);
  347         free(clusters, M_IFADDR);
  348     }
  349     clusters = c;
  350     i = n_clusters;             /* index of cluster entry */
  351     n_clusters++;
  352 found:
  353     c = clusters + i;           /* the right cluster ... */
  354     ETHER_ADDR_COPY(c->my_macs[c->ports].etheraddr, IFP2AC(ifp)->ac_enaddr);
  355     c->ports++;
  356     return c;
  357 bad:
  358     if (c)
  359         free(c, M_IFADDR);
  360     return NULL;
  361 }
  362 
  363 
  364 /*
  365  * Turn off bridging, by clearing promisc mode on the interface,
  366  * marking the interface as unused, and clearing the name in the
  367  * stats entry.
  368  * Also dispose the hash tables associated with the clusters.
  369  */
  370 static void
  371 bridge_off(void)
  372 {
  373     struct ifnet *ifp ;
  374     int i;
  375 
  376     BDG_LOCK_ASSERT();
  377 
  378     DPRINTF(("%s: n_clusters %d\n", __func__, n_clusters));
  379 
  380     IFNET_RLOCK();
  381     TAILQ_FOREACH(ifp, &ifnet, if_link) {
  382         struct bdg_softc *b;
  383 
  384         if (ifp->if_index >= BDG_MAX_PORTS)
  385             continue;   /* make sure we do not go beyond the end */
  386         b = &ifp2sc[ifp->if_index];
  387 
  388         if ( b->flags & IFF_BDG_PROMISC ) {
  389             ifpromisc(ifp, 0);
  390             b->flags &= ~(IFF_BDG_PROMISC|IFF_MUTE) ;
  391             DPRINTF(("%s: %s promisc OFF if_flags 0x%x "
  392                 "bdg_flags 0x%x\n", __func__, ifp->if_xname,
  393                 ifp->if_flags, b->flags));
  394         }
  395         b->flags &= ~(IFF_USED) ;
  396         b->cluster = NULL;
  397         bdg_stats.s[ifp->if_index].name[0] = '\0';
  398     }
  399     IFNET_RUNLOCK();
  400     /* flush_tables */
  401 
  402     for (i=0; i < n_clusters; i++) {
  403         free(clusters[i].ht, M_IFADDR);
  404         free(clusters[i].my_macs, M_IFADDR);
  405     }
  406     if (clusters != NULL)
  407         free(clusters, M_IFADDR);
  408     clusters = NULL;
  409     n_clusters =0;
  410 }
  411 
  412 /*
  413  * set promisc mode on the interfaces we use.
  414  */
  415 static void
  416 bridge_on(void)
  417 {
  418     struct ifnet *ifp ;
  419 
  420     BDG_LOCK_ASSERT();
  421 
  422     IFNET_RLOCK();
  423     TAILQ_FOREACH(ifp, &ifnet, if_link) {
  424         struct bdg_softc *b = &ifp2sc[ifp->if_index];
  425 
  426         if ( !(b->flags & IFF_USED) )
  427             continue ;
  428         if ( !( ifp->if_flags & IFF_UP) ) {
  429             if_up(ifp);
  430         }
  431         if ( !(b->flags & IFF_BDG_PROMISC) ) {
  432             (void) ifpromisc(ifp, 1);
  433             b->flags |= IFF_BDG_PROMISC ;
  434             DPRINTF(("%s: %s promisc ON if_flags 0x%x bdg_flags 0x%x\n",
  435                 __func__, ifp->if_xname, ifp->if_flags, b->flags));
  436         }
  437         if (b->flags & IFF_MUTE) {
  438             DPRINTF(("%s: unmuting %s\n", __func__, ifp->if_xname));
  439             b->flags &= ~IFF_MUTE;
  440         }
  441     }
  442     IFNET_RUNLOCK();
  443 }
  444 
  445 static char bridge_cfg[1024];           /* NB: in BSS so initialized to zero */
  446 
  447 /**
  448  * reconfigure bridge.
  449  * This is also done every time we attach or detach an interface.
  450  * Main use is to make sure that we do not bridge on some old
  451  * (ejected) device. So, it would be really useful to have a
  452  * pointer to the modified device as an argument. Without it, we
  453  * have to scan all interfaces.
  454  */
  455 static void
  456 reconfigure_bridge_locked(void)
  457 {
  458     BDG_LOCK_ASSERT();
  459 
  460     bridge_off();
  461     if (do_bridge) {
  462         if (if_index >= BDG_MAX_PORTS) {
  463             printf("-- sorry too many interfaces (%d, max is %d),"
  464                 " disabling bridging\n", if_index, BDG_MAX_PORTS);
  465             do_bridge = 0;
  466             return;
  467         }
  468         parse_bdg_cfg();
  469         bridge_on();
  470     }
  471 }
  472 
  473 static void
  474 reconfigure_bridge(void)
  475 {
  476     BDG_LOCK();
  477     reconfigure_bridge_locked();
  478     BDG_UNLOCK();
  479 }
  480 
  481 /*
  482  * parse the config string, set IFF_USED, name and cluster_id
  483  * for all interfaces found.
  484  * The config string is a list of "if[:cluster]" with
  485  * a number of possible separators (see "sep"). In particular the
  486  * use of the space lets you set bridge_cfg with the output from
  487  * "ifconfig -l"
  488  */
  489 static void
  490 parse_bdg_cfg(void)
  491 {
  492     char *p, *beg;
  493     int l, cluster;
  494     static const char *sep = ", \t";
  495 
  496     BDG_LOCK_ASSERT();
  497 
  498     for (p = bridge_cfg; *p ; p++) {
  499         struct ifnet *ifp;
  500         int found = 0;
  501         char c;
  502 
  503         if (index(sep, *p))     /* skip separators */
  504             continue ;
  505         /* names are lowercase and digits */
  506         for ( beg = p ; islower(*p) || isdigit(*p) ; p++ )
  507             ;
  508         l = p - beg ;           /* length of name string */
  509         if (l == 0)             /* invalid name */
  510             break ;
  511         if ( *p != ':' )        /* no ':', assume default cluster 1 */
  512             cluster = 1 ;
  513         else                    /* fetch cluster */
  514             cluster = strtoul( p+1, &p, 10);
  515         c = *p;
  516         *p = '\0';
  517         /*
  518          * now search in interface list for a matching name
  519          */
  520         IFNET_RLOCK();          /* could sleep XXX */
  521         TAILQ_FOREACH(ifp, &ifnet, if_link) {
  522 
  523             if (!strncmp(beg, ifp->if_xname, max(l, strlen(ifp->if_xname)))) {
  524                 struct bdg_softc *b = &ifp2sc[ifp->if_index];
  525                 if (ifp->if_type != IFT_ETHER && ifp->if_type != IFT_L2VLAN) {
  526                     printf("%s is not an ethernet, continue\n", ifp->if_xname);
  527                     continue;
  528                 }
  529                 if (b->flags & IFF_USED) {
  530                     printf("%s already used, skipping\n", ifp->if_xname);
  531                     break;
  532                 }
  533                 b->cluster = add_cluster(htons(cluster), ifp);
  534                 b->flags |= IFF_USED ;
  535                 snprintf(bdg_stats.s[ifp->if_index].name,
  536                     sizeof(bdg_stats.s[ifp->if_index].name),
  537                     "%s:%d", ifp->if_xname, cluster);
  538 
  539                 DPRINTF(("%s: found %s next c %d\n", __func__,
  540                     bdg_stats.s[ifp->if_index].name, c));
  541                 found = 1;
  542                 break ;
  543             }
  544         }
  545         IFNET_RUNLOCK();
  546         if (!found)
  547             printf("interface %s Not found in bridge\n", beg);
  548         *p = c;
  549         if (c == '\0')
  550             break; /* no more */
  551     }
  552 }
  553 
  554 /*
  555  * handler for net.link.ether.bridge
  556  */
  557 static int
  558 sysctl_bdg(SYSCTL_HANDLER_ARGS)
  559 {
  560     int enable = do_bridge;
  561     int error;
  562 
  563     error = sysctl_handle_int(oidp, &enable, 0, req);
  564     enable = (enable) ? 1 : 0;
  565     BDG_LOCK();
  566     if (enable != do_bridge) {
  567         do_bridge = enable;
  568         reconfigure_bridge_locked();
  569     }
  570     BDG_UNLOCK();
  571     return error ;
  572 }
  573 SYSCTL_PROC(_net_link_ether_bridge, OID_AUTO, enable, CTLTYPE_INT|CTLFLAG_RW,
  574             &do_bridge, 0, &sysctl_bdg, "I", "Bridging");
  575 
  576 /*
  577  * handler for net.link.ether.bridge_cfg
  578  */
  579 static int
  580 sysctl_bdg_cfg(SYSCTL_HANDLER_ARGS)
  581 {
  582     int error;
  583     char *new_cfg;
  584 
  585     new_cfg = malloc(sizeof(bridge_cfg), M_TEMP, M_WAITOK);
  586     bcopy(bridge_cfg, new_cfg, sizeof(bridge_cfg));
  587 
  588     error = sysctl_handle_string(oidp, new_cfg, oidp->oid_arg2, req);
  589     if (error == 0) {
  590         BDG_LOCK();
  591         if (strcmp(new_cfg, bridge_cfg)) {
  592             bcopy(new_cfg, bridge_cfg, sizeof(bridge_cfg));
  593             reconfigure_bridge_locked();
  594         }
  595         BDG_UNLOCK();
  596     }
  597 
  598     free(new_cfg, M_TEMP);
  599 
  600     return error;
  601 }
  602 SYSCTL_PROC(_net_link_ether_bridge, OID_AUTO, config, CTLTYPE_STRING|CTLFLAG_RW,
  603             &bridge_cfg, sizeof(bridge_cfg), &sysctl_bdg_cfg, "A",
  604             "Bridge configuration");
  605 
  606 static int
  607 sysctl_refresh(SYSCTL_HANDLER_ARGS)
  608 {
  609     if (req->newptr)
  610         reconfigure_bridge();
  611 
  612     return 0;
  613 }
  614 SYSCTL_PROC(_net_link_ether_bridge, OID_AUTO, refresh, CTLTYPE_INT|CTLFLAG_WR,
  615             NULL, 0, &sysctl_refresh, "I", "iface refresh");
  616 
  617 #ifndef BURN_BRIDGES
  618 #define SYSCTL_OID_COMPAT(parent, nbr, name, kind, a1, a2, handler, fmt, descr)\
  619         static struct sysctl_oid sysctl__##parent##_##name##_compat = {  \
  620                 &sysctl_##parent##_children, { 0 },                      \
  621                 nbr, kind, a1, a2, #name, handler, fmt, 0, descr };      \
  622         DATA_SET(sysctl_set, sysctl__##parent##_##name##_compat)
  623 #define SYSCTL_INT_COMPAT(parent, nbr, name, access, ptr, val, descr)    \
  624         SYSCTL_OID_COMPAT(parent, nbr, name, CTLTYPE_INT|(access),       \
  625                 ptr, val, sysctl_handle_int, "I", descr)
  626 #define SYSCTL_STRUCT_COMPAT(parent, nbr, name, access, ptr, type, descr)\
  627         SYSCTL_OID_COMPAT(parent, nbr, name, CTLTYPE_OPAQUE|(access),    \
  628                 ptr, sizeof(struct type), sysctl_handle_opaque,          \
  629                 "S," #type, descr)
  630 #define SYSCTL_PROC_COMPAT(parent, nbr, name, access, ptr, arg, handler, fmt, descr) \
  631         SYSCTL_OID_COMPAT(parent, nbr, name, (access),                   \
  632                 ptr, arg, handler, fmt, descr)
  633 
  634 SYSCTL_INT_COMPAT(_net_link_ether, OID_AUTO, bridge_ipf, CTLFLAG_RW,
  635             &bdg_ipf, 0,"Pass bridged pkts through IPFilter");
  636 SYSCTL_INT_COMPAT(_net_link_ether, OID_AUTO, bridge_ipfw, CTLFLAG_RW,
  637             &bdg_ipfw,0,"Pass bridged pkts through firewall");
  638 SYSCTL_STRUCT_COMPAT(_net_link_ether, PF_BDG, bdgstats, CTLFLAG_RD,
  639         &bdg_stats, bdg_stats, "bridge statistics");
  640 SYSCTL_PROC_COMPAT(_net_link_ether, OID_AUTO, bridge_cfg, 
  641             CTLTYPE_STRING|CTLFLAG_RW,
  642             &bridge_cfg, sizeof(bridge_cfg), &sysctl_bdg_cfg, "A",
  643             "Bridge configuration");
  644 SYSCTL_PROC_COMPAT(_net_link_ether, OID_AUTO, bridge_refresh,
  645             CTLTYPE_INT|CTLFLAG_WR,
  646             NULL, 0, &sysctl_refresh, "I", "iface refresh");
  647 #endif
  648 
  649 static int bdg_loops;
  650 static int bdg_slowtimer = 0;
  651 static int bdg_age_index = 0;   /* index of table position to age */
  652 
  653 /*
  654  * called periodically to flush entries etc.
  655  */
  656 static void
  657 bdg_timeout(void *dummy)
  658 {
  659     if (do_bridge) {
  660         int l, i;
  661 
  662         BDG_LOCK();
  663         /*
  664          * age entries in the forwarding table.
  665          */
  666         l = bdg_age_index + HASH_SIZE/4 ;
  667         if (l > HASH_SIZE)
  668             l = HASH_SIZE;
  669 
  670         for (i = 0; i < n_clusters; i++) {
  671             bdg_hash_table *bdg_table = clusters[i].ht;
  672             for (; bdg_age_index < l; bdg_age_index++)
  673                 if (bdg_table[bdg_age_index].used)
  674                     bdg_table[bdg_age_index].used = 0;
  675                 else if (bdg_table[bdg_age_index].name) {
  676                     DPRINTF(("%s: flushing stale entry %d\n",
  677                         __func__, bdg_age_index));
  678                     bdg_table[bdg_age_index].name = NULL;
  679                 }
  680         }
  681         if (bdg_age_index >= HASH_SIZE)
  682             bdg_age_index = 0;
  683 
  684         if (--bdg_slowtimer <= 0 ) {
  685             bdg_slowtimer = 5;
  686 
  687             bridge_on();        /* we just need unmute, really */
  688             bdg_loops = 0;
  689         }
  690         BDG_UNLOCK();
  691     }
  692     callout_reset(&bdg_callout, 2*hz, bdg_timeout, NULL);
  693 }
  694 
  695 /*
  696  * Find the right pkt destination:
  697  *      BDG_BCAST       is a broadcast
  698  *      BDG_MCAST       is a multicast
  699  *      BDG_LOCAL       is for a local address
  700  *      BDG_DROP        must be dropped
  701  *      other           ifp of the dest. interface (incl.self)
  702  *
  703  * We assume this is only called for interfaces for which bridging
  704  * is enabled, i.e. BDG_USED(ifp) is true.
  705  */
  706 static __inline struct ifnet *
  707 bridge_dst_lookup(struct ether_header *eh, struct cluster_softc *c)
  708 {
  709     bdg_hash_table *bt;         /* pointer to entry in hash table */
  710 
  711     BDG_LOCK_ASSERT();
  712 
  713     if (ETHER_IS_MULTICAST(eh->ether_dhost))
  714         return IS_ETHER_BROADCAST(eh->ether_dhost) ? BDG_BCAST : BDG_MCAST;
  715     /*
  716      * Lookup local addresses in case one matches.  We optimize
  717      * for the common case of two interfaces.
  718      */
  719     KASSERT(c->ports != 0, ("lookup with no ports!"));
  720     switch (c->ports) {
  721         int i;
  722     default:
  723         for (i = c->ports-1; i > 1; i--) {
  724             if (ETHER_ADDR_EQ(c->my_macs[i].etheraddr, eh->ether_dhost))
  725                 return BDG_LOCAL;
  726         }
  727         /* fall thru... */
  728     case 2:
  729         if (ETHER_ADDR_EQ(c->my_macs[1].etheraddr, eh->ether_dhost))
  730             return BDG_LOCAL;
  731     case 1:
  732         if (ETHER_ADDR_EQ(c->my_macs[0].etheraddr, eh->ether_dhost))
  733             return BDG_LOCAL;
  734     }
  735     /*
  736      * Look for a possible destination in table
  737      */
  738     bt = &c->ht[HASH_FN(eh->ether_dhost)];
  739     if (bt->name && ETHER_ADDR_EQ(bt->etheraddr, eh->ether_dhost))
  740         return bt->name;
  741     else
  742         return BDG_UNKNOWN;
  743 }
  744 
  745 /**
  746  * bridge_in() is invoked to perform bridging decision on input packets.
  747  *
  748  * On Input:
  749  *   eh         Ethernet header of the incoming packet.
  750  *   ifp        interface the packet is coming from.
  751  *
  752  * On Return: destination of packet, one of
  753  *   BDG_BCAST  broadcast
  754  *   BDG_MCAST  multicast
  755  *   BDG_LOCAL  is only for a local address (do not forward)
  756  *   BDG_DROP   drop the packet
  757  *   ifp        ifp of the destination interface.
  758  *
  759  * Forwarding is not done directly to give a chance to some drivers
  760  * to fetch more of the packet, or simply drop it completely.
  761  */
  762 
  763 static struct ifnet *
  764 bridge_in(struct ifnet *ifp, struct ether_header *eh)
  765 {
  766     int index;
  767     struct ifnet *dst, *old;
  768     bdg_hash_table *bt;                 /* location in hash table */
  769     int dropit = BDG_MUTED(ifp);
  770 
  771     /*
  772      * hash the source address
  773      */
  774     BDG_LOCK();
  775     index = HASH_FN(eh->ether_shost);
  776     bt = &BDG_CLUSTER(ifp)->ht[index];
  777     bt->used = 1;
  778     old = bt->name;
  779     if (old) {                          /* the entry is valid */
  780         if (!ETHER_ADDR_EQ(eh->ether_shost, bt->etheraddr)) {
  781             bdg_ipfw_colls++;
  782             bt->name = NULL;            /* NB: will overwrite below */
  783         } else if (old != ifp) {
  784             /*
  785              * Found a loop. Either a machine has moved, or there
  786              * is a misconfiguration/reconfiguration of the network.
  787              * First, do not forward this packet!
  788              * Record the relocation anyways; then, if loops persist,
  789              * suspect a reconfiguration and disable forwarding
  790              * from the old interface.
  791              */
  792             bt->name = ifp;             /* relocate address */
  793             printf("-- loop (%d) %6D to %s from %s (%s)\n",
  794                         bdg_loops, eh->ether_shost, ".",
  795                         ifp->if_xname, old->if_xname,
  796                         BDG_MUTED(old) ? "muted":"active");
  797             dropit = 1;
  798             if (!BDG_MUTED(old)) {
  799                 if (bdg_loops++ > 10)
  800                     BDG_MUTE(old);
  801             }
  802         }
  803     }
  804 
  805     /*
  806      * now write the source address into the table
  807      */
  808     if (bt->name == NULL) {
  809         DPRINTF(("%s: new addr %6D at %d for %s\n",
  810             __func__, eh->ether_shost, ".", index, ifp->if_xname));
  811         ETHER_ADDR_COPY(bt->etheraddr, eh->ether_shost);
  812         bt->name = ifp;
  813     }
  814     dst = bridge_dst_lookup(eh, BDG_CLUSTER(ifp));
  815     BDG_UNLOCK();
  816 
  817     /*
  818      * bridge_dst_lookup can return the following values:
  819      *   BDG_BCAST, BDG_MCAST, BDG_LOCAL, BDG_UNKNOWN, BDG_DROP, ifp.
  820      * For muted interfaces, or when we detect a loop, the first 3 are
  821      * changed in BDG_LOCAL (we still listen to incoming traffic),
  822      * and others to BDG_DROP (no use for the local host).
  823      * Also, for incoming packets, ifp is changed to BDG_DROP if ifp == src.
  824      * These changes are not necessary for outgoing packets from ether_output().
  825      */
  826     BDG_STAT(ifp, BDG_IN);
  827     switch ((uintptr_t)dst) {
  828     case (uintptr_t)BDG_BCAST:
  829     case (uintptr_t)BDG_MCAST:
  830     case (uintptr_t)BDG_LOCAL:
  831     case (uintptr_t)BDG_UNKNOWN:
  832     case (uintptr_t)BDG_DROP:
  833         BDG_STAT(ifp, dst);
  834         break;
  835     default:
  836         if (dst == ifp || dropit)
  837             BDG_STAT(ifp, BDG_DROP);
  838         else
  839             BDG_STAT(ifp, BDG_FORWARD);
  840         break;
  841     }
  842 
  843     if (dropit) {
  844         if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_LOCAL)
  845             dst = BDG_LOCAL;
  846         else
  847             dst = BDG_DROP;
  848     } else {
  849         if (dst == ifp)
  850             dst = BDG_DROP;
  851     }
  852     DPRINTF(("%s: %6D ->%6D ty 0x%04x dst %s\n", __func__,
  853         eh->ether_shost, ".",
  854         eh->ether_dhost, ".",
  855         ntohs(eh->ether_type),
  856         (dst <= BDG_FORWARD) ? bdg_dst_names[(uintptr_t)dst] :
  857                 dst->if_xname));
  858 
  859     return dst;
  860 }
  861 
  862 /*
  863  * Return 1 if it's ok to send a packet out the specified interface.
  864  * The interface must be:
  865  *      used for bridging,
  866  *      not muted,
  867  *      not full,
  868  *      up and running,
  869  *      not the source interface, and
  870  *      belong to the same cluster as the 'real_dst'.
  871  */
  872 static __inline int
  873 bridge_ifok(struct ifnet *ifp, struct ifnet *src, struct ifnet *dst)
  874 {
  875     return (BDG_USED(ifp)
  876         && !BDG_MUTED(ifp)
  877         && !_IF_QFULL(&ifp->if_snd)
  878         && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING)
  879         && ifp != src
  880         && BDG_SAMECLUSTER(ifp, dst));
  881 }
  882 
  883 /*
  884  * Forward a packet to dst -- which can be a single interface or
  885  * an entire cluster. The src port and muted interfaces are excluded.
  886  *
  887  * If src == NULL, the pkt comes from ether_output, and dst is the real
  888  * interface the packet is originally sent to. In this case, we must forward
  889  * it to the whole cluster.
  890  * We never call bdg_forward from ether_output on interfaces which are
  891  * not part of a cluster.
  892  *
  893  * If possible (i.e. we can determine that the caller does not need
  894  * a copy), the packet is consumed here, and bdg_forward returns NULL.
  895  * Otherwise, a pointer to a copy of the packet is returned.
  896  */
  897 static struct mbuf *
  898 bdg_forward(struct mbuf *m0, struct ifnet *dst)
  899 {
  900 #define EH_RESTORE(_m) do {                                                \
  901     M_PREPEND((_m), ETHER_HDR_LEN, M_DONTWAIT);                            \
  902     if ((_m) == NULL) {                                                    \
  903         bdg_dropped++;                                                     \
  904         return NULL;                                                       \
  905     }                                                                      \
  906     if (eh != mtod((_m), struct ether_header *))                           \
  907         bcopy(&save_eh, mtod((_m), struct ether_header *), ETHER_HDR_LEN); \
  908     else                                                                   \
  909         bdg_predict++;                                                     \
  910 } while (0);
  911     struct ether_header *eh;
  912     struct ifnet *src;
  913     struct ifnet *ifp, *last;
  914     int shared = bdg_copy;              /* someone else is using the mbuf */
  915     int error;
  916     struct ifnet *real_dst = dst;       /* real dst from ether_output */
  917     struct ip_fw_args args;
  918     struct ether_header save_eh;
  919     struct mbuf *m;
  920 
  921     DDB(quad_t ticks; ticks = rdtsc();)
  922 
  923     args.rule = ip_dn_claim_rule(m0);
  924     if (args.rule)
  925         shared = 0;                     /* For sure this is our own mbuf. */
  926     else
  927         bdg_thru++;                     /* count 1st time through bdg_forward */
  928 
  929     /*
  930      * The packet arrives with the Ethernet header at the front.
  931      */
  932     eh = mtod(m0, struct ether_header *);
  933 
  934     src = m0->m_pkthdr.rcvif;
  935     if (src == NULL) {                  /* packet from ether_output */
  936         BDG_LOCK();
  937         dst = bridge_dst_lookup(eh, BDG_CLUSTER(real_dst));
  938         BDG_UNLOCK();
  939     }
  940 
  941     if (dst == BDG_DROP) {              /* this should not happen */
  942         printf("xx bdg_forward for BDG_DROP\n");
  943         m_freem(m0);
  944         bdg_dropped++;
  945         return NULL;
  946     }
  947     if (dst == BDG_LOCAL) {             /* this should not happen as well */
  948         printf("xx ouch, bdg_forward for local pkt\n");
  949         return m0;
  950     }
  951     if (dst == BDG_BCAST || dst == BDG_MCAST) {
  952          /* need a copy for the local stack */
  953          shared = 1;
  954     }
  955 
  956     /*
  957      * Do filtering in a very similar way to what is done in ip_output.
  958      * Only if firewall is loaded, enabled, and the packet is not
  959      * from ether_output() (src==NULL, or we would filter it twice).
  960      * Additional restrictions may apply e.g. non-IP, short packets,
  961      * and pkts already gone through a pipe.
  962      */
  963     if (src != NULL && (
  964         (inet_pfil_hook.ph_busy_count >= 0 && bdg_ipf != 0) ||
  965         (IPFW_LOADED && bdg_ipfw != 0))) {
  966 
  967         int i;
  968 
  969         if (args.rule != NULL && fw_one_pass)
  970             goto forward; /* packet already partially processed */
  971         /*
  972          * i need some amt of data to be contiguous, and in case others need
  973          * the packet (shared==1) also better be in the first mbuf.
  974          */
  975         i = min(m0->m_pkthdr.len, max_protohdr) ;
  976         if (shared || m0->m_len < i) {
  977             m0 = m_pullup(m0, i);
  978             if (m0 == NULL) {
  979                 printf("%s: m_pullup failed\n", __func__);      /* XXXDPRINTF*/
  980                 bdg_dropped++;
  981                 return NULL;
  982             }
  983             eh = mtod(m0, struct ether_header *);
  984         }
  985 
  986         /*
  987          * Processing below expects the Ethernet header is stripped.
  988          * Furthermore, the mbuf chain might be replaced at various
  989          * places.  To deal with this we copy the header to a temporary
  990          * location, strip the header, and restore it as needed.
  991          */
  992         bcopy(eh, &save_eh, ETHER_HDR_LEN);     /* local copy for restore */
  993         m_adj(m0, ETHER_HDR_LEN);               /* temporarily strip header */
  994 
  995         /*
  996          * NetBSD-style generic packet filter, pfil(9), hooks.
  997          * Enables ipf(8) in bridging.
  998          */
  999         if (!IPFW_LOADED) { /* XXX: Prevent ipfw from being run twice. */
 1000         if (inet_pfil_hook.ph_busy_count >= 0 &&
 1001             m0->m_pkthdr.len >= sizeof(struct ip) &&
 1002             ntohs(save_eh.ether_type) == ETHERTYPE_IP) {
 1003             /*
 1004              * before calling the firewall, swap fields the same as IP does.
 1005              * here we assume the pkt is an IP one and the header is contiguous
 1006              */
 1007             struct ip *ip = mtod(m0, struct ip *);
 1008 
 1009             ip->ip_len = ntohs(ip->ip_len);
 1010             ip->ip_off = ntohs(ip->ip_off);
 1011 
 1012             if (pfil_run_hooks(&inet_pfil_hook, &m0, src, PFIL_IN, NULL) != 0) {
 1013                 /* NB: hook should consume packet */
 1014                 return NULL;
 1015             }
 1016             if (m0 == NULL)                     /* consumed by filter */
 1017                 return m0;
 1018             /*
 1019              * If we get here, the firewall has passed the pkt, but the mbuf
 1020              * pointer might have changed. Restore ip and the fields ntohs()'d.
 1021              */
 1022             ip = mtod(m0, struct ip *);
 1023             ip->ip_len = htons(ip->ip_len);
 1024             ip->ip_off = htons(ip->ip_off);
 1025         }
 1026         } /* XXX: Prevent ipfw from being run twice. */
 1027 
 1028         /*
 1029          * Prepare arguments and call the firewall.
 1030          */
 1031         if (!IPFW_LOADED || bdg_ipfw == 0) {
 1032             EH_RESTORE(m0);     /* restore Ethernet header */
 1033             goto forward;       /* not using ipfw, accept the packet */
 1034         }
 1035 
 1036         /*
 1037          * XXX The following code is very similar to the one in
 1038          * if_ethersubr.c:ether_ipfw_chk()
 1039          */
 1040 
 1041         args.m = m0;            /* the packet we are looking at         */
 1042         args.oif = NULL;        /* this is an input packet              */
 1043         args.next_hop = NULL;   /* we do not support forward yet        */
 1044         args.eh = &save_eh;     /* MAC header for bridged/MAC packets   */
 1045         i = ip_fw_chk_ptr(&args);
 1046         m0 = args.m;            /* in case the firewall used the mbuf   */
 1047 
 1048         if (m0 != NULL)
 1049                 EH_RESTORE(m0); /* restore Ethernet header */
 1050 
 1051         if ( (i & IP_FW_PORT_DENY_FLAG) || m0 == NULL) /* drop */
 1052             return m0;
 1053 
 1054         if (i == 0) /* a PASS rule.  */
 1055             goto forward;
 1056         if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
 1057             /*
 1058              * Pass the pkt to dummynet, which consumes it.
 1059              * If shared, make a copy and keep the original.
 1060              */
 1061             if (shared) {
 1062                 m = m_copypacket(m0, M_DONTWAIT);
 1063                 if (m == NULL) {        /* copy failed, give up */
 1064                     bdg_dropped++;
 1065                     return NULL;
 1066                 }
 1067             } else {
 1068                 m = m0 ; /* pass the original to dummynet */
 1069                 m0 = NULL ; /* and nothing back to the caller */
 1070             }
 1071 
 1072             args.oif = real_dst;
 1073             ip_dn_io_ptr(m, (i & 0xffff),DN_TO_BDG_FWD, &args);
 1074             return m0;
 1075         }
 1076         /*
 1077          * XXX at some point, add support for divert/forward actions.
 1078          * If none of the above matches, we have to drop the packet.
 1079          */
 1080         bdg_ipfw_drops++;
 1081         return m0;
 1082     }
 1083 forward:
 1084     /*
 1085      * Again, bring up the headers in case of shared bufs to avoid
 1086      * corruptions in the future.
 1087      */
 1088     if (shared) {
 1089         int i = min(m0->m_pkthdr.len, max_protohdr);
 1090 
 1091         m0 = m_pullup(m0, i);
 1092         if (m0 == NULL) {
 1093             bdg_dropped++;
 1094             return NULL;
 1095         }
 1096         /* NB: eh is not used below; no need to recalculate it */
 1097     }
 1098 
 1099     /*
 1100      * now real_dst is used to determine the cluster where to forward.
 1101      * For packets coming from ether_input, this is the one of the 'src'
 1102      * interface, whereas for locally generated packets (src==NULL) it
 1103      * is the cluster of the original destination interface, which
 1104      * was already saved into real_dst.
 1105      */
 1106     if (src != NULL)
 1107         real_dst = src;
 1108 
 1109     last = NULL;
 1110     if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_UNKNOWN) {
 1111         /*
 1112          * Scan all ports and send copies to all but the last.
 1113          */
 1114         IFNET_RLOCK();          /* XXX replace with generation # */
 1115         TAILQ_FOREACH(ifp, &ifnet, if_link) {
 1116             if (bridge_ifok(ifp, src, real_dst)) {
 1117                 if (last) {
 1118                     /*
 1119                      * At this point we know two interfaces need a copy
 1120                      * of the packet (last + ifp) so we must create a
 1121                      * copy to handoff to last.
 1122                      */
 1123                     m = m_copypacket(m0, M_DONTWAIT);
 1124                     if (m == NULL) {
 1125                         IFNET_RUNLOCK();
 1126                         printf("%s: , m_copypacket failed!\n", __func__);
 1127                         bdg_dropped++;
 1128                         return m0;      /* the original is still there... */
 1129                     }
 1130                     IFQ_HANDOFF(last, m, error);
 1131                     if (!error)
 1132                         BDG_STAT(last, BDG_OUT);
 1133                     else
 1134                         bdg_dropped++;
 1135                 }
 1136                 last = ifp;
 1137             }
 1138         }
 1139         IFNET_RUNLOCK();
 1140     } else {
 1141         if (bridge_ifok(dst, src, real_dst))
 1142             last = dst;
 1143     }
 1144     if (last) {
 1145         if (shared) {                   /* need to copy */
 1146             m = m_copypacket(m0, M_DONTWAIT);
 1147             if (m == NULL) {
 1148                 printf("%s: sorry, m_copypacket failed!\n", __func__);
 1149                 bdg_dropped++ ;
 1150                 return m0;              /* the original is still there... */
 1151             }
 1152         } else {                        /* consume original */
 1153             m = m0, m0 = NULL;
 1154         }
 1155         IFQ_HANDOFF(last, m, error);
 1156         if (!error)
 1157             BDG_STAT(last, BDG_OUT);
 1158         else
 1159             bdg_dropped++;
 1160     }
 1161 
 1162     DDB(bdg_fw_ticks += (u_long)(rdtsc() - ticks) ; bdg_fw_count++ ;
 1163         if (bdg_fw_count != 0) bdg_fw_avg = bdg_fw_ticks/bdg_fw_count; )
 1164     return m0;
 1165 #undef EH_RESTORE
 1166 }
 1167 
 1168 /*
 1169  * initialization of bridge code.
 1170  */
 1171 static int
 1172 bdginit(void)
 1173 {
 1174     if (bootverbose)
 1175             printf("BRIDGE %s loaded\n", bridge_version);
 1176 
 1177     ifp2sc = malloc(BDG_MAX_PORTS * sizeof(struct bdg_softc),
 1178                 M_IFADDR, M_WAITOK | M_ZERO );
 1179     if (ifp2sc == NULL)
 1180         return ENOMEM;
 1181 
 1182     BDG_LOCK_INIT();
 1183 
 1184     n_clusters = 0;
 1185     clusters = NULL;
 1186     do_bridge = 0;
 1187 
 1188     bzero(&bdg_stats, sizeof(bdg_stats));
 1189 
 1190     bridge_in_ptr = bridge_in;
 1191     bdg_forward_ptr = bdg_forward;
 1192     bdgtakeifaces_ptr = reconfigure_bridge;
 1193 
 1194     bdgtakeifaces_ptr();                /* XXX does this do anything? */
 1195 
 1196     callout_init(&bdg_callout, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
 1197     bdg_timeout(0);
 1198     return 0 ;
 1199 }
 1200 
 1201 static void
 1202 bdgdestroy(void)
 1203 {
 1204     bridge_in_ptr = NULL;
 1205     bdg_forward_ptr = NULL;
 1206     bdgtakeifaces_ptr = NULL;
 1207 
 1208     callout_stop(&bdg_callout);
 1209     BDG_LOCK();
 1210     bridge_off();
 1211 
 1212     if (ifp2sc) {
 1213         free(ifp2sc, M_IFADDR);
 1214         ifp2sc = NULL;
 1215     }
 1216     BDG_LOCK_DESTROY();
 1217 }
 1218 
 1219 /*
 1220  * initialization code, both for static and dynamic loading.
 1221  */
 1222 static int
 1223 bridge_modevent(module_t mod, int type, void *unused)
 1224 {
 1225         int err;
 1226 
 1227         switch (type) {
 1228         case MOD_LOAD:
 1229                 if (BDG_LOADED)
 1230                         err = EEXIST;
 1231                 else
 1232                         err = bdginit();
 1233                 break;
 1234         case MOD_UNLOAD:
 1235                 do_bridge = 0;
 1236                 bdgdestroy();
 1237                 err = 0;
 1238                 break;
 1239         default:
 1240                 err = EINVAL;
 1241                 break;
 1242         }
 1243         return err;
 1244 }
 1245 
 1246 static moduledata_t bridge_mod = {
 1247         "bridge",
 1248         bridge_modevent,
 1249         0
 1250 };
 1251 
 1252 DECLARE_MODULE(bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
 1253 MODULE_VERSION(bridge, 1);

Cache object: 8f7d87b76cfa382f2fcc83e887b37129


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