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-2000 Luigi Rizzo
    3  *
    4  * Redistribution and use in source and binary forms, with or without
    5  * modification, are permitted provided that the following conditions
    6  * are met:
    7  * 1. Redistributions of source code must retain the above copyright
    8  *    notice, this list of conditions and the following disclaimer.
    9  * 2. Redistributions in binary form must reproduce the above copyright
   10  *    notice, this list of conditions and the following disclaimer in the
   11  *    documentation and/or other materials provided with the distribution.
   12  *
   13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
   14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   23  * SUCH DAMAGE.
   24  *
   25  * $FreeBSD$
   26  */
   27 
   28 /*
   29  * This code implements bridging in FreeBSD. It only acts on ethernet
   30  * type of interfaces (others are still usable for routing).
   31  * A bridging table holds the source MAC address/dest. interface for each
   32  * known node. The table is indexed using an hash of the source address.
   33  *
   34  * Input packets are tapped near the end of the input routine in each
   35  * driver (near the call to bpf_mtap, or before the call to ether_input)
   36  * and analysed calling bridge_in(). Depending on the result, the packet
   37  * can be forwarded to one or more output interfaces using bdg_forward(),
   38  * and/or sent to the upper layer (e.g. in case of multicast).
   39  *
   40  * Output packets are intercepted near the end of ether_output(),
   41  * the correct destination is selected calling bdg_dst_lookup(),
   42  * and then forwarding is done using bdg_forward().
   43  * Bridging is controlled by the sysctl variable net.link.ether.bridge
   44  *
   45  * The arp code is also modified to let a machine answer to requests
   46  * irrespective of the port the request came from.
   47  *
   48  * In case of loops in the bridging topology, the bridge detects this
   49  * event and temporarily mutes output bridging on one of the ports.
   50  * Periodically, interfaces are unmuted by bdg_timeout().
   51  * Muting is only implemented as a safety measure, and also as
   52  * a mechanism to support a user-space implementation of the spanning
   53  * tree algorithm. In the final release, unmuting will only occur
   54  * because of explicit action of the user-level daemon.
   55  *
   56  * To build a bridging kernel, use the following option
   57  *    option BRIDGE
   58  * and then at runtime set the sysctl variable to enable bridging.
   59  *
   60  * Only one interface is supposed to have addresses set (but
   61  * there are no problems in practice if you set addresses for more
   62  * than one interface).
   63  * Bridging will act before routing, but nothing prevents a machine
   64  * from doing both (modulo bugs in the implementation...).
   65  *
   66  * THINGS TO REMEMBER
   67  *  - bridging requires some (small) modifications to the interface
   68  *    driver. Not all of them have been changed, see the "ed" and "de"
   69  *    drivers as examples on how to operate.
   70  *  - bridging is incompatible with multicast routing on the same
   71  *    machine. There is not an easy fix to this.
   72  *  - loop detection is still not very robust.
   73  *  - the interface of bdg_forward() could be improved.
   74  */
   75 
   76 #include <sys/param.h>
   77 #include <sys/mbuf.h>
   78 #include <sys/malloc.h>
   79 #include <sys/systm.h>
   80 #include <sys/socket.h> /* for net/if.h */
   81 #include <sys/kernel.h>
   82 #include <sys/sysctl.h>
   83 
   84 #include <net/if.h>
   85 #include <net/if_types.h>
   86 
   87 #include <netinet/in.h> /* for struct arpcom */
   88 #include <netinet/in_systm.h>
   89 #include <netinet/in_var.h>
   90 #include <netinet/ip.h>
   91 #include <netinet/if_ether.h> /* for struct arpcom */
   92 
   93 #include "opt_ipfw.h" 
   94 #include "opt_ipdn.h" 
   95 
   96 #if defined(IPFIREWALL)
   97 #include <net/route.h>
   98 #include <netinet/ip_fw.h>
   99 #if defined(DUMMYNET)
  100 #include <netinet/ip_dummynet.h>
  101 #endif
  102 #endif
  103 
  104 #include <net/bridge.h>
  105 
  106 /*
  107  * For debugging, you can use the following macros.
  108  * remember, rdtsc() only works on Pentium-class machines
  109 
  110     quad_t ticks;
  111     DDB(ticks = rdtsc();)
  112     ... interesting code ...
  113     DDB(bdg_fw_ticks += (u_long)(rdtsc() - ticks) ; bdg_fw_count++ ;)
  114 
  115  *
  116  */
  117 
  118 #define DDB(x) x
  119 #define DEB(x)
  120 
  121 static void bdginit(void *);
  122 static void flush_table(void);
  123 static void bdg_promisc_on(void);
  124 static void parse_bdg_cfg(void);
  125 
  126 static int bdg_ipfw = 0 ;
  127 int do_bridge = 0;
  128 bdg_hash_table *bdg_table = NULL ;
  129 
  130 /*
  131  * System initialization
  132  */
  133 
  134 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, bdginit, NULL)
  135 
  136 /*
  137  * We need additional info for the bridge. The bdg_ifp2sc[] array
  138  * provides a pointer to this struct using the if_index.
  139  * bdg_softc has a backpointer to the struct ifnet, the bridge
  140  * flags, and a cluster (bridging occurs only between port of the
  141  * same cluster).
  142  */
  143 struct bdg_softc {
  144     struct ifnet *ifp ;
  145     /* ((struct arpcom *)ifp)->ac_enaddr is the eth. addr */
  146     int flags ;
  147 #define IFF_BDG_PROMISC 0x0001  /* set promisc mode on this if.  */
  148 #define IFF_MUTE        0x0002  /* mute this if for bridging.   */
  149 #define IFF_USED        0x0004  /* use this if for bridging.    */
  150     short cluster_id ; /* in network format */
  151     u_long magic;
  152 } ;
  153 
  154 static struct bdg_stats bdg_stats ;
  155 static struct bdg_softc *ifp2sc = NULL ;
  156 /* XXX make it static of size BDG_MAX_PORTS */
  157 
  158 #define USED(ifp) (ifp2sc[ifp->if_index].flags & IFF_USED)
  159 #define MUTED(ifp) (ifp2sc[ifp->if_index].flags & IFF_MUTE)
  160 #define MUTE(ifp) ifp2sc[ifp->if_index].flags |= IFF_MUTE
  161 #define UNMUTE(ifp) ifp2sc[ifp->if_index].flags &= ~IFF_MUTE
  162 #define CLUSTER(ifp) (ifp2sc[ifp->if_index].cluster_id)
  163 #define IFP_CHK(ifp, x) \
  164         if (ifp2sc[ifp->if_index].magic != 0xDEADBEEF) { x ; }
  165 
  166 #define SAMECLUSTER(ifp,src,eh) \
  167         (src == NULL || CLUSTER(ifp) == CLUSTER(src) )
  168 
  169 /*
  170  * turn off promisc mode, optionally clear the IFF_USED flag
  171  */
  172 static void
  173 bdg_promisc_off(int clear_used)
  174 {
  175     struct ifnet *ifp ;
  176     for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next ) {
  177         if ( (ifp2sc[ifp->if_index].flags & IFF_BDG_PROMISC) ) {
  178             int s, ret ;
  179             s = splimp();
  180             ret = ifpromisc(ifp, 0);
  181             splx(s);
  182             ifp2sc[ifp->if_index].flags &= ~(IFF_BDG_PROMISC|IFF_MUTE) ;
  183             if (clear_used)
  184                 ifp2sc[ifp->if_index].flags &= ~(IFF_USED) ;
  185             printf(">> now %s%d promisc ON if_flags 0x%x bdg_flags 0x%x\n",
  186                     ifp->if_name, ifp->if_unit,
  187                     ifp->if_flags, ifp2sc[ifp->if_index].flags);
  188         }
  189     }
  190 }
  191 
  192 /*
  193  * set promisc mode on the interfaces we use.
  194  */
  195 static void
  196 bdg_promisc_on()
  197 {
  198     struct ifnet *ifp ;
  199     int s ;
  200 
  201     for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next ) {
  202         if ( !USED(ifp) )
  203             continue ;
  204         if ( 0 == ( ifp->if_flags & IFF_UP) ) {
  205             s = splimp();
  206             if_up(ifp);
  207             splx(s);
  208         }
  209         if ( !(ifp2sc[ifp->if_index].flags & IFF_BDG_PROMISC) ) {
  210             int ret ;
  211             s = splimp();
  212             ret = ifpromisc(ifp, 1);
  213             splx(s);
  214             ifp2sc[ifp->if_index].flags |= IFF_BDG_PROMISC ;
  215             printf(">> now %s%d promisc ON if_flags 0x%x bdg_flags 0x%x\n",
  216                     ifp->if_name, ifp->if_unit,
  217                     ifp->if_flags, ifp2sc[ifp->if_index].flags);
  218         }
  219         if (MUTED(ifp)) {
  220             printf(">> unmuting %s%d\n", ifp->if_name, ifp->if_unit);
  221             UNMUTE(ifp) ;
  222        }
  223     }
  224 }
  225 
  226 static int
  227 sysctl_bdg SYSCTL_HANDLER_ARGS
  228 {
  229     int error, oldval = do_bridge ;
  230 
  231     error = sysctl_handle_int(oidp,
  232         oidp->oid_arg1, oidp->oid_arg2, req);
  233     DEB( printf("called sysctl for bridge name %s arg2 %d val %d->%d\n",
  234         oidp->oid_name, oidp->oid_arg2,
  235         oldval, do_bridge); )
  236 
  237     if (bdg_table == NULL)
  238         do_bridge = 0 ;
  239     if (oldval != do_bridge) {
  240         flush_table();
  241         if (do_bridge == 0)
  242             bdg_promisc_off( 0 ); /* leave IFF_USED set */
  243         else
  244             bdg_promisc_on();
  245     }
  246     return error ;
  247 }
  248 
  249 static char bridge_cfg[256] = { "" } ;
  250 
  251 /*
  252  * parse the config string, set IFF_USED, name and cluster_id
  253  * for all interfaces found.
  254  */
  255 static void
  256 parse_bdg_cfg()
  257 {
  258     char *p, *beg ;
  259     int i, l, cluster;
  260     struct bdg_softc *b;
  261 
  262     for (p= bridge_cfg; *p ; p++) {
  263         /* interface names begin with [a-z]  and continue up to ':' */
  264         if (*p < 'a' || *p > 'z')
  265             continue ;
  266         for ( beg = p ; *p && *p != ':' ; p++ )
  267             ;
  268         if (*p == 0) /* end of string, ':' not found */
  269             return ;
  270         l = p - beg ; /* length of name string */
  271         p++ ;
  272         DDB(printf("-- match beg(%d) <%s> p <%s>\n", l, beg, p);)
  273         for (cluster = 0 ; *p && *p >= '' && *p <= '9' ; p++)
  274             cluster = cluster*10 + (*p -'');
  275         /*
  276          * now search in bridge strings
  277          */
  278         for (i=0, b = ifp2sc ; i < if_index ; i++, b++) {
  279             char buf[32];
  280             struct ifnet *ifp = b->ifp ;
  281 
  282             if (ifp == NULL)
  283                 continue;
  284             sprintf(buf, "%s%d", ifp->if_name, ifp->if_unit);
  285             if (!strncmp(beg, buf, l)) { /* XXX not correct for >10 if! */
  286                 b->cluster_id = htons(cluster) ;
  287                 b->flags |= IFF_USED ;
  288                 sprintf(bdg_stats.s[ifp->if_index].name+l,
  289                         ":%d", cluster);
  290 
  291                 DDB(printf("--++  found %s\n",
  292                     bdg_stats.s[ifp->if_index].name);)
  293                 break ;
  294             }
  295         }
  296     }
  297 }
  298 
  299 static int
  300 sysctl_bdg_cfg SYSCTL_HANDLER_ARGS
  301 {
  302     int error = 0 ;
  303     char oldval[256] ;
  304 
  305     strcpy(oldval, bridge_cfg) ;
  306 
  307     error = sysctl_handle_string(oidp,
  308             bridge_cfg, oidp->oid_arg2, req);
  309     DEB(
  310         printf("called sysctl for bridge name %s arg2 %d err %d val %s->%s\n",
  311                 oidp->oid_name, oidp->oid_arg2,
  312                 error,
  313                 oldval, bridge_cfg);
  314         )
  315     if (strcmp(oldval, bridge_cfg)) {
  316         bdg_promisc_off( 1 );  /* reset previously-used interfaces */
  317         flush_table();
  318         parse_bdg_cfg();        /* and set new ones... */
  319         if (do_bridge)
  320             bdg_promisc_on();   /* re-enable interfaces */
  321     }
  322     return error ;
  323 }
  324 
  325 SYSCTL_PROC(_net_link_ether, OID_AUTO, bridge_cfg, CTLTYPE_STRING|CTLFLAG_RW,
  326             &bridge_cfg, sizeof(bridge_cfg), &sysctl_bdg_cfg, "A",
  327             "Bridge configuration");
  328 
  329 SYSCTL_PROC(_net_link_ether, OID_AUTO, bridge, CTLTYPE_INT|CTLFLAG_RW,
  330             &do_bridge, 0, &sysctl_bdg, "I", "Bridging");
  331 
  332 SYSCTL_INT(_net_link_ether, OID_AUTO, bridge_ipfw, CTLFLAG_RW,
  333             &bdg_ipfw,0,"Pass bridged pkts through firewall");
  334 
  335 int bdg_ipfw_drops;
  336 SYSCTL_INT(_net_link_ether, OID_AUTO, bridge_ipfw_drop,
  337         CTLFLAG_RW, &bdg_ipfw_drops,0,"");
  338 
  339 int bdg_ipfw_colls;
  340 SYSCTL_INT(_net_link_ether, OID_AUTO, bridge_ipfw_collisions,
  341         CTLFLAG_RW, &bdg_ipfw_colls,0,"");
  342 
  343 #if 1 /* diagnostic vars */
  344 int bdg_in_count = 0 , bdg_in_ticks = 0 , bdg_fw_count = 0, bdg_fw_ticks = 0 ;
  345 SYSCTL_INT(_net_link_ether, OID_AUTO, bdginc, CTLFLAG_RW, &bdg_in_count,0,"");
  346 SYSCTL_INT(_net_link_ether, OID_AUTO, bdgint, CTLFLAG_RW, &bdg_in_ticks,0,"");
  347 SYSCTL_INT(_net_link_ether, OID_AUTO, bdgfwc, CTLFLAG_RW, &bdg_fw_count,0,"");
  348 SYSCTL_INT(_net_link_ether, OID_AUTO, bdgfwt, CTLFLAG_RW, &bdg_fw_ticks,0,"");
  349 #endif
  350 
  351 SYSCTL_STRUCT(_net_link_ether, PF_BDG, bdgstats,
  352         CTLFLAG_RD, &bdg_stats , bdg_stats, "bridge statistics");
  353 
  354 static int bdg_loops ;
  355 
  356 /*
  357  * completely flush the bridge table.
  358  */
  359 static void
  360 flush_table()
  361 {   
  362     int s,i;
  363 
  364     if (bdg_table == NULL)
  365         return ;
  366     s = splimp();
  367     for (i=0; i< HASH_SIZE; i++)
  368         bdg_table[i].name= NULL; /* clear table */
  369     splx(s);
  370 }
  371 
  372 /*
  373  * called periodically to flush entries etc.
  374  */
  375 static void
  376 bdg_timeout(void *dummy)
  377 {
  378     static int slowtimer = 0 ;
  379 
  380     if (do_bridge) {
  381         static int age_index = 0 ; /* index of table position to age */
  382         int l = age_index + HASH_SIZE/4 ;
  383         /*
  384          * age entries in the forwarding table.
  385          */
  386         if (l > HASH_SIZE)
  387             l = HASH_SIZE ;
  388         for (; age_index < l ; age_index++)
  389             if (bdg_table[age_index].used)
  390                 bdg_table[age_index].used = 0 ;
  391             else if (bdg_table[age_index].name) {
  392                 /* printf("xx flushing stale entry %d\n", age_index); */
  393                 bdg_table[age_index].name = NULL ;
  394             }
  395         if (age_index >= HASH_SIZE)
  396             age_index = 0 ;
  397 
  398         if (--slowtimer <= 0 ) {
  399             slowtimer = 5 ;
  400 
  401             bdg_promisc_on() ; /* we just need unmute, really */
  402             bdg_loops = 0 ;
  403         }
  404     }
  405     timeout(bdg_timeout, (void *)0, 2*hz );
  406 }
  407 
  408 /*
  409  * local MAC addresses are held in a small array. This makes comparisons
  410  * much faster.
  411  */
  412 unsigned char bdg_addresses[6*BDG_MAX_PORTS];
  413 int bdg_ports ;
  414 
  415 /*
  416  * initialization of bridge code. This needs to be done after all
  417  * interfaces have been configured.
  418  */
  419 static void
  420 bdginit(void *dummy)
  421 {
  422     int i ;
  423     struct ifnet *ifp;
  424     struct arpcom *ac ;
  425     u_char *eth_addr ;
  426     struct bdg_softc *bp;
  427 
  428     if (bdg_table == NULL)
  429         bdg_table = (struct hash_table *)
  430                 malloc(HASH_SIZE * sizeof(struct hash_table),
  431                     M_IFADDR, M_WAITOK);
  432     flush_table();
  433 
  434     ifp2sc = malloc(BDG_MAX_PORTS * sizeof(struct bdg_softc),
  435                 M_IFADDR, M_WAITOK );
  436     bzero(ifp2sc, BDG_MAX_PORTS * sizeof(struct bdg_softc) );
  437 
  438     bzero(&bdg_stats, sizeof(bdg_stats) );
  439     bdg_ports = 0 ;
  440     eth_addr = bdg_addresses ;
  441 
  442     printf("BRIDGE 990810, have %d interfaces\n", if_index);
  443     for (i = 0 , ifp = ifnet.tqh_first ; i < if_index ;
  444                 i++, ifp = ifp->if_link.tqe_next)
  445         if (ifp->if_type == IFT_ETHER) { /* ethernet ? */
  446             bp = &ifp2sc[ifp->if_index] ;
  447             ac = (struct arpcom *)ifp;
  448             sprintf(bridge_cfg + strlen(bridge_cfg),
  449                 "%s%d:1,", ifp->if_name, ifp->if_unit);
  450             printf("-- index %d %s type %d phy %d addrl %d addr %6D\n",
  451                     ifp->if_index,
  452                     bdg_stats.s[ifp->if_index].name,
  453                     (int)ifp->if_type, (int) ifp->if_physical,
  454                     (int)ifp->if_addrlen,
  455                     ac->ac_enaddr, "." );
  456             bcopy(ac->ac_enaddr, eth_addr, 6);
  457             eth_addr += 6 ;
  458             bp->ifp = ifp ;
  459             bp->flags = IFF_USED ;
  460             bp->cluster_id = htons(1) ;
  461             bp->magic = 0xDEADBEEF ;
  462 
  463             sprintf(bdg_stats.s[ifp->if_index].name,
  464                 "%s%d:%d", ifp->if_name, ifp->if_unit,
  465                 ntohs(bp->cluster_id));
  466             bdg_ports ++ ;
  467         }
  468 
  469     bdg_timeout(0);
  470     do_bridge=0;
  471 }
  472 
  473 /*
  474  * bridge_in() is invoked to perform bridging decision on input packets.
  475  * On Input:
  476  *   m          packet to be bridged. The mbuf need not to hold the
  477  *              whole packet, only the first 14 bytes suffice. We
  478  *              assume them to be contiguous. No alignment assumptions
  479  *              because they are not a problem on i386 class machines.
  480  *
  481  * On Return: destination of packet, one of
  482  *   BDG_BCAST  broadcast
  483  *   BDG_MCAST  multicast
  484  *   BDG_LOCAL  is only for a local address (do not forward)
  485  *   BDG_DROP   drop the packet
  486  *   ifp        ifp of the destination interface.
  487  *
  488  * Forwarding is not done directly to give a chance to some drivers
  489  * to fetch more of the packet, or simply drop it completely.
  490  */
  491 
  492 
  493 struct ifnet *
  494 bridge_in(struct mbuf *m)
  495 {
  496     int index;
  497     struct ifnet *ifp = m->m_pkthdr.rcvif,  *dst , *old ;
  498     int dropit = MUTED(ifp) ;
  499     struct ether_header *eh;
  500 
  501     eh = mtod(m, struct ether_header *);
  502 
  503     /*
  504      * hash the source address
  505      */
  506     index= HASH_FN(eh->ether_shost);
  507     bdg_table[index].used = 1 ;
  508     old = bdg_table[index].name ;
  509     if ( old ) { /* the entry is valid. */
  510         IFP_CHK(old, printf("bridge_in-- reading table\n") );
  511 
  512         if (!BDG_MATCH( eh->ether_shost, bdg_table[index].etheraddr) ) {
  513             bdg_ipfw_colls++ ;
  514             bdg_table[index].name = NULL ;
  515         } else if (old != ifp) {
  516             /*
  517              * found a loop. Either a machine has moved, or there
  518              * is a misconfiguration/reconfiguration of the network.
  519              * First, do not forward this packet!
  520              * Record the relocation anyways; then, if loops persist,
  521              * suspect a reconfiguration and disable forwarding
  522              * from the old interface.
  523              */
  524             bdg_table[index].name = ifp ; /* relocate address */
  525             printf("-- loop (%d) %6D to %s%d from %s%d (%s)\n",
  526                         bdg_loops, eh->ether_shost, ".",
  527                         ifp->if_name, ifp->if_unit,
  528                         old->if_name, old->if_unit,
  529                         MUTED(old) ? "muted":"active");
  530             dropit = 1 ;
  531             if ( !MUTED(old) ) {
  532                 if (++bdg_loops > 10)
  533                     MUTE(old) ;
  534             }
  535         }
  536     }
  537 
  538     /*
  539      * now write the source address into the table
  540      */
  541     if (bdg_table[index].name == NULL) {
  542         DEB(printf("new addr %6D at %d for %s%d\n",
  543             eh->ether_shost, ".", index, ifp->if_name, ifp->if_unit);)
  544         bcopy(eh->ether_shost, bdg_table[index].etheraddr, 6);
  545         bdg_table[index].name = ifp ;
  546     }
  547     dst = bridge_dst_lookup(m);
  548     /* Return values:
  549      *   BDG_BCAST, BDG_MCAST, BDG_LOCAL, BDG_UNKNOWN, BDG_DROP, ifp.
  550      * For muted interfaces, the first 3 are changed in BDG_LOCAL,
  551      * and others to BDG_DROP. Also, for incoming packets, ifp is changed
  552      * to BDG_DROP in case ifp == src . These mods are not necessary
  553      * for outgoing packets from ether_output().
  554      */
  555     BDG_STAT(ifp, BDG_IN);
  556     switch ((int)dst) {
  557     case (int)BDG_BCAST:
  558     case (int)BDG_MCAST:
  559     case (int)BDG_LOCAL:
  560     case (int)BDG_UNKNOWN:
  561     case (int)BDG_DROP:
  562         BDG_STAT(ifp, dst);
  563         break ;
  564     default :
  565         if (dst == ifp || dropit )
  566             BDG_STAT(ifp, BDG_DROP);
  567         else
  568             BDG_STAT(ifp, BDG_FORWARD);
  569         break ;
  570     }
  571 
  572     if ( dropit ) {
  573         if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_LOCAL)
  574             return BDG_LOCAL ;
  575         else
  576             return BDG_DROP ;
  577     } else {
  578         return (dst == ifp ? BDG_DROP : dst ) ;
  579     }
  580 }
  581 
  582 /*
  583  * Forward to dst, excluding src port and muted interfaces.
  584  * The packet is freed if possible (i.e. surely not of interest for
  585  * the upper layer), otherwise a copy is left for use by the caller
  586  * (pointer in *m0).
  587  *
  588  * It would be more efficient to make bdg_forward() always consume
  589  * the packet, leaving to the caller the task to check if it needs a copy
  590  * and get one in case. As it is now, bdg_forward() can sometimes make
  591  * a copy whereas it is not necessary.
  592  *
  593  * INPUT:
  594  *    *m0  -- ptr to pkt (not null at call time)
  595  *    *dst -- destination (special value or ifnet *)
  596  *    (*m0)->m_pkthdr.rcvif -- NULL only for output pkts.
  597  * OUTPUT:
  598  *    *m0  -- pointer to the packet (NULL if still existent)
  599  */
  600 int
  601 bdg_forward (struct mbuf **m0, struct ifnet *dst)
  602 {
  603     struct ifnet *src = (*m0)->m_pkthdr.rcvif; /* could be NULL in output */
  604     struct ifnet *ifp, *last = NULL ;
  605     int error=0, s ;
  606     int canfree = 0 ; /* can free the buf at the end if set */
  607     int once = 0;      /* loop only once */
  608     struct mbuf *m ;
  609 
  610     struct ether_header *eh = mtod(*m0, struct ether_header *); /* XXX */
  611 
  612     if (dst == BDG_DROP) { /* this should not happen */
  613         printf("xx bdg_forward for BDG_DROP\n");
  614         m_freem(*m0) ;
  615         *m0 = NULL ;
  616         return 0;
  617     }
  618     if (dst == BDG_LOCAL) { /* this should not happen as well */
  619         printf("xx ouch, bdg_forward for local pkt\n");
  620         return 0;
  621     }
  622     if (dst == BDG_BCAST || dst == BDG_MCAST || dst == BDG_UNKNOWN) {
  623         ifp = ifnet.tqh_first ; /* scan all ports */
  624         once = 0 ;
  625         if (dst != BDG_UNKNOWN) /* need a copy for the local stack */
  626             canfree = 0 ;
  627     } else {
  628         ifp = dst ;
  629         once = 1 ;
  630     }
  631     if ( (u_int)(ifp) <= (u_int)BDG_FORWARD )
  632         panic("bdg_forward: bad dst");
  633 
  634 #ifdef IPFIREWALL
  635     /*
  636      * do filtering in a very similar way to what is done
  637      * in ip_output. Only for IP packets, and only pass/fail/dummynet
  638      * is supported. The tricky thing is to make sure that enough of
  639      * the packet (basically, Eth+IP+TCP/UDP headers) is contiguous
  640      * so that calls to m_pullup in ip_fw_chk will not kill the
  641      * ethernet header.
  642      */
  643     if (ip_fw_chk_ptr) {
  644         u_int16_t dummy = 0 ;
  645         struct ip_fw_chain *rule = NULL ;
  646         int off;
  647         struct ip *ip ;
  648 
  649         m = *m0 ;
  650 #ifdef DUMMYNET
  651         if (m->m_type == MT_DUMMYNET) {
  652             /*
  653              * the packet was already tagged, so part of the
  654              * processing was already done, and we need to go down.
  655              */
  656             rule = (struct ip_fw_chain *)(m->m_data) ;
  657             (*m0) = m = m->m_next ;
  658 
  659             src = m->m_pkthdr.rcvif; /* could be NULL in output */
  660             eh = mtod(m, struct ether_header *); /* XXX */
  661             canfree = 1 ; /* for sure, a copy is not needed later. */
  662             goto forward; /* HACK! I should obey the fw_one_pass */
  663         }
  664 #endif
  665         if (bdg_ipfw == 0) /* this test must be here. */
  666             goto forward ;
  667         if (src == NULL)
  668             goto forward ; /* do not apply to packets from ether_output */
  669         /*
  670          * In this section, canfree=1 means m is the same as *m0.
  671          * canfree==0 means m is a copy. We need to make a copy here
  672          * (to be destroyed on exit from the firewall section) because
  673          * the firewall itself might destroy the packet.
  674          * (This is not very smart... i should really change ipfw to
  675          * leave the pkt alive!)
  676          */
  677         if (canfree == 0 ) {
  678             /*
  679              * Need to make a copy (and for good measure, make sure that
  680              * the header is contiguous). The original is still in *m0
  681              */
  682             int needed = min(MHLEN, 14+max_protohdr) ;
  683             needed = min(needed, (*m0)->m_len ) ;
  684 
  685             m = m_copypacket( (*m0), M_DONTWAIT);
  686             if (m == NULL) {
  687                 printf("-- bdg: m_copypacket failed.\n") ;
  688                 return ENOBUFS ;
  689             }
  690             m = m_pullup(m, needed) ;
  691             if ( m == NULL ) {
  692                 printf("-- bdg: pullup failed.\n") ;
  693                 return ENOBUFS ;
  694             }
  695         }
  696 
  697         /*
  698          * before calling the firewall, swap fields the same as IP does.
  699          * here we assume the pkt is an IP one and the header is contiguous
  700          */
  701         eh = mtod(m, struct ether_header *);
  702         ip = (struct ip *)(eh + 1 ) ;
  703         NTOHS(ip->ip_len);
  704         NTOHS(ip->ip_id);
  705         NTOHS(ip->ip_off);
  706 
  707         /*
  708          * The third parameter to the firewall code is the dst.  interface.
  709          * Since we apply checks only on input pkts we use NULL.
  710          */
  711         off = (*ip_fw_chk_ptr)(NULL, 0, NULL, &dummy, &m, &rule, NULL) ;
  712 
  713         if (m == NULL) { /* pkt discarded by firewall */
  714             /*
  715              * At this point, if canfree==1, m and *m0 were the same
  716              * thing, so just clear ptr. Otherwise, leave it alone, the
  717              * upper layer might still make use of it somewhere.
  718              */
  719             if (canfree)
  720                 *m0 = NULL ;
  721             return 0 ;
  722         }
  723 
  724         /*
  725          * If we get here, the firewall has passed the pkt, but the
  726          * mbuf pointer might have changed. Restore eh, ip, and the
  727          * fields NTOHS()'d. Then, if canfree==1, also restore *m0.
  728          */
  729         eh = mtod(m, struct ether_header *);
  730         ip = (struct ip *)(eh + 1 ) ;
  731         HTONS(ip->ip_len);
  732         HTONS(ip->ip_id);
  733         HTONS(ip->ip_off);
  734         if (canfree) /* m was a reference to *m0, so update *m0 */
  735             *m0 = m ;
  736 
  737         if (off == 0) { /* a PASS rule.  */
  738             if (canfree == 0) /* destroy the copy */
  739                 m_freem(m);
  740             goto forward ;
  741         }
  742 #ifdef DUMMYNET
  743         if (off & 0x10000) {  
  744             /*
  745              * pass the pkt to dummynet. Need to include m, dst, rule.
  746              * Dummynet consumes the packet in all cases.
  747              */
  748             dummynet_io((off & 0xffff), DN_TO_BDG_FWD, m, dst, NULL, 0, rule);
  749             if (canfree) /* dummynet has consumed the original one */
  750                 *m0 = NULL ;
  751             return 0 ;
  752         }
  753 #endif
  754         /* if none of the above matches, we have to drop the pkt */
  755         bdg_ipfw_drops++ ;
  756         m_freem(m);
  757         if (canfree == 0) /* m was a copy */
  758             m_freem(*m0);
  759         *m0 = NULL ;
  760         return 0 ;
  761     }
  762 forward:
  763 #endif /* IPFIREWALL */
  764     /*
  765      * Now *m0 is the only pkt left. If canfree != 0 the pkt might be
  766      * used by the upper layers which could scramble header fields.
  767      * (basically ntoh*() etc.). To avoid problems, make sure that
  768      * all fields that might be changed by the local network stack are not
  769      * in a cluster by calling m_pullup on *m0. We lose some efficiency
  770      * but better than having packets corrupt!
  771      */
  772     if (canfree == 0 ) {
  773         int needed = min(MHLEN, 14+max_protohdr) ;
  774         needed = min(needed, (*m0)->m_len ) ;
  775 
  776         *m0 = m_pullup( *m0, needed) ;
  777         if ( *m0 == NULL ) {
  778             printf("-- bdg: pullup failed.\n") ;
  779             return ENOBUFS ;
  780         }
  781     }
  782     for (;;) {
  783         if (last) { /* need to forward packet */
  784             if (canfree && once ) { /* no need to copy */
  785                 m = *m0 ;
  786                 *m0 = NULL ; /* original is gone */
  787             } else /* on a P5-90, m_copypacket takes 540 ticks */
  788                 m = m_copypacket(*m0, M_DONTWAIT);
  789             if (m == NULL) {
  790                 printf("bdg_forward: sorry, m_copy failed!\n");
  791                 return ENOBUFS ; /* the original is still there... */
  792             }
  793             /*
  794              * Last part of ether_output: queue pkt and start
  795              * output if interface not yet active.
  796              */
  797             s = splimp();
  798             if (IF_QFULL(&last->if_snd)) {
  799                 IF_DROP(&last->if_snd);
  800 #if 0
  801                 MUTE(last); /* should I also mute ? */
  802 #endif
  803                 splx(s);
  804                 m_freem(m); /* consume the pkt anyways */
  805                 error = ENOBUFS ;
  806             } else {
  807                 last->if_obytes += m->m_pkthdr.len ;
  808                 if (m->m_flags & M_MCAST)
  809                     last->if_omcasts++;
  810                 IF_ENQUEUE(&last->if_snd, m);
  811                 if ((last->if_flags & IFF_OACTIVE) == 0)
  812                     (*last->if_start)(last);
  813                 splx(s);
  814             }
  815             BDG_STAT(last, BDG_OUT);
  816             last = NULL ;
  817             if (once)
  818                 break ;
  819         }
  820         if (ifp == NULL)
  821             break ;
  822         if (ifp != src &&       /* do not send to self */
  823                 USED(ifp) &&    /* if used for bridging */
  824                 ! IF_QFULL(&ifp->if_snd) &&
  825                 (ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
  826                          (IFF_UP|IFF_RUNNING) &&
  827                 SAMECLUSTER(ifp, src, eh) && !MUTED(ifp) )
  828             last = ifp ;
  829         ifp = ifp->if_link.tqe_next ;
  830         if (ifp == NULL)
  831             once = 1 ;
  832     }
  833 
  834     return error ;
  835 }

Cache object: 6eef951d682c2df17802239189e3a03d


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