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


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

FreeBSD/Linux Kernel Cross Reference
sys/net/if_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 /*      $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $       */
    2 
    3 /*
    4  * Copyright 2001 Wasabi Systems, Inc.
    5  * All rights reserved.
    6  *
    7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  * 3. All advertising materials mentioning features or use of this software
   18  *    must display the following acknowledgement:
   19  *      This product includes software developed for the NetBSD Project by
   20  *      Wasabi Systems, Inc.
   21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
   22  *    or promote products derived from this software without specific prior
   23  *    written permission.
   24  *
   25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
   26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
   29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   35  * POSSIBILITY OF SUCH DAMAGE.
   36  */
   37 
   38 /*
   39  * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
   40  * All rights reserved.
   41  *
   42  * Redistribution and use in source and binary forms, with or without
   43  * modification, are permitted provided that the following conditions
   44  * are met:
   45  * 1. Redistributions of source code must retain the above copyright
   46  *    notice, this list of conditions and the following disclaimer.
   47  * 2. Redistributions in binary form must reproduce the above copyright
   48  *    notice, this list of conditions and the following disclaimer in the
   49  *    documentation and/or other materials provided with the distribution.
   50  *
   51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   53  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   54  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
   55  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   56  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   57  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   59  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   60  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   61  * POSSIBILITY OF SUCH DAMAGE.
   62  *
   63  * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
   64  */
   65 
   66 /*
   67  * Network interface bridge support.
   68  *
   69  * TODO:
   70  *
   71  *      - Currently only supports Ethernet-like interfaces (Ethernet,
   72  *        802.11, VLANs on Ethernet, etc.)  Figure out a nice way
   73  *        to bridge other types of interfaces (FDDI-FDDI, and maybe
   74  *        consider heterogenous bridges).
   75  */
   76 
   77 #include <sys/cdefs.h>
   78 __FBSDID("$FreeBSD$");
   79 
   80 #include "opt_inet.h"
   81 #include "opt_inet6.h"
   82 #include "opt_carp.h"
   83 
   84 #include <sys/param.h>
   85 #include <sys/mbuf.h>
   86 #include <sys/malloc.h>
   87 #include <sys/protosw.h>
   88 #include <sys/systm.h>
   89 #include <sys/time.h>
   90 #include <sys/socket.h> /* for net/if.h */
   91 #include <sys/sockio.h>
   92 #include <sys/ctype.h>  /* string functions */
   93 #include <sys/kernel.h>
   94 #include <sys/random.h>
   95 #include <sys/syslog.h>
   96 #include <sys/sysctl.h>
   97 #include <vm/uma.h>
   98 #include <sys/module.h>
   99 #include <sys/proc.h>
  100 #include <sys/lock.h>
  101 #include <sys/mutex.h>
  102 
  103 #include <net/bpf.h>
  104 #include <net/if.h>
  105 #include <net/if_clone.h>
  106 #include <net/if_dl.h>
  107 #include <net/if_types.h>
  108 #include <net/if_var.h>
  109 #include <net/pfil.h>
  110 
  111 #include <netinet/in.h> /* for struct arpcom */
  112 #include <netinet/in_systm.h>
  113 #include <netinet/in_var.h>
  114 #include <netinet/ip.h>
  115 #include <netinet/ip_var.h>
  116 #ifdef INET6
  117 #include <netinet/ip6.h>
  118 #include <netinet6/ip6_var.h>
  119 #endif
  120 #ifdef DEV_CARP
  121 #include <netinet/ip_carp.h>
  122 #endif
  123 #include <machine/in_cksum.h>
  124 #include <netinet/if_ether.h> /* for struct arpcom */
  125 #include <net/bridgestp.h>
  126 #include <net/if_bridgevar.h>
  127 #include <net/if_llc.h>
  128 
  129 #include <net/route.h>
  130 #include <netinet/ip_fw.h>
  131 #include <netinet/ip_dummynet.h>
  132 
  133 /*
  134  * Size of the route hash table.  Must be a power of two.
  135  */
  136 #ifndef BRIDGE_RTHASH_SIZE
  137 #define BRIDGE_RTHASH_SIZE              1024
  138 #endif
  139 
  140 #define BRIDGE_RTHASH_MASK              (BRIDGE_RTHASH_SIZE - 1)
  141 
  142 /*
  143  * Maximum number of addresses to cache.
  144  */
  145 #ifndef BRIDGE_RTABLE_MAX
  146 #define BRIDGE_RTABLE_MAX               100
  147 #endif
  148 
  149 /*
  150  * Timeout (in seconds) for entries learned dynamically.
  151  */
  152 #ifndef BRIDGE_RTABLE_TIMEOUT
  153 #define BRIDGE_RTABLE_TIMEOUT           (20 * 60)       /* same as ARP */
  154 #endif
  155 
  156 /*
  157  * Number of seconds between walks of the route list.
  158  */
  159 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
  160 #define BRIDGE_RTABLE_PRUNE_PERIOD      (5 * 60)
  161 #endif
  162 
  163 /*
  164  * List of capabilities to possibly mask on the member interface.
  165  */
  166 #define BRIDGE_IFCAPS_MASK              IFCAP_TXCSUM
  167 /*
  168  * List of capabilities to disable on the member interface.
  169  */
  170 #define BRIDGE_IFCAPS_STRIP             0       /* IFCAP_LRO */
  171 
  172 /*
  173  * Bridge interface list entry.
  174  */
  175 struct bridge_iflist {
  176         LIST_ENTRY(bridge_iflist) bif_next;
  177         struct ifnet            *bif_ifp;       /* member if */
  178         struct bstp_port        bif_stp;        /* STP state */
  179         uint32_t                bif_flags;      /* member if flags */
  180         int                     bif_savedcaps;  /* saved capabilities */
  181 };
  182 
  183 /*
  184  * Bridge route node.
  185  */
  186 struct bridge_rtnode {
  187         LIST_ENTRY(bridge_rtnode) brt_hash;     /* hash table linkage */
  188         LIST_ENTRY(bridge_rtnode) brt_list;     /* list linkage */
  189         struct ifnet            *brt_ifp;       /* destination if */
  190         unsigned long           brt_expire;     /* expiration time */
  191         uint8_t                 brt_flags;      /* address flags */
  192         uint8_t                 brt_addr[ETHER_ADDR_LEN];
  193 };
  194 
  195 /*
  196  * Software state for each bridge.
  197  */
  198 struct bridge_softc {
  199         struct ifnet            *sc_ifp;        /* make this an interface */
  200         LIST_ENTRY(bridge_softc) sc_list;
  201         struct mtx              sc_mtx;
  202         struct cv               sc_cv;
  203         uint32_t                sc_brtmax;      /* max # of addresses */
  204         uint32_t                sc_brtcnt;      /* cur. # of addresses */
  205         uint32_t                sc_brttimeout;  /* rt timeout in seconds */
  206         struct callout          sc_brcallout;   /* bridge callout */
  207         uint32_t                sc_iflist_ref;  /* refcount for sc_iflist */
  208         uint32_t                sc_iflist_xcnt; /* refcount for sc_iflist */
  209         LIST_HEAD(, bridge_iflist) sc_iflist;   /* member interface list */
  210         LIST_HEAD(, bridge_rtnode) *sc_rthash;  /* our forwarding table */
  211         LIST_HEAD(, bridge_rtnode) sc_rtlist;   /* list version of above */
  212         uint32_t                sc_rthash_key;  /* key for hash */
  213         LIST_HEAD(, bridge_iflist) sc_spanlist; /* span ports list */
  214         struct bstp_state       sc_stp;         /* STP state */
  215         uint32_t                sc_brtexceeded; /* # of cache drops */
  216 };
  217 
  218 static struct mtx       bridge_list_mtx;
  219 eventhandler_tag        bridge_detach_cookie = NULL;
  220 
  221 int     bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
  222 
  223 uma_zone_t bridge_rtnode_zone;
  224 
  225 static int      bridge_clone_create(struct if_clone *, int);
  226 static void     bridge_clone_destroy(struct ifnet *);
  227 
  228 static int      bridge_ioctl(struct ifnet *, u_long, caddr_t);
  229 static void     bridge_mutecaps(struct bridge_softc *);
  230 static void     bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *,
  231                     int);
  232 static void     bridge_ifdetach(void *arg __unused, struct ifnet *);
  233 static void     bridge_init(void *);
  234 static void     bridge_dummynet(struct mbuf *, struct ifnet *);
  235 static void     bridge_stop(struct ifnet *, int);
  236 static void     bridge_start(struct ifnet *);
  237 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
  238 static int      bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
  239                     struct rtentry *);
  240 static void     bridge_enqueue(struct bridge_softc *, struct ifnet *,
  241                     struct mbuf *);
  242 static void     bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
  243 
  244 static void     bridge_forward(struct bridge_softc *, struct bridge_iflist *,
  245                     struct mbuf *m);
  246 
  247 static void     bridge_timer(void *);
  248 
  249 static void     bridge_broadcast(struct bridge_softc *, struct ifnet *,
  250                     struct mbuf *, int);
  251 static void     bridge_span(struct bridge_softc *, struct mbuf *);
  252 
  253 static int      bridge_rtupdate(struct bridge_softc *, const uint8_t *,
  254                     struct bridge_iflist *, int, uint8_t);
  255 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
  256 static void     bridge_rttrim(struct bridge_softc *);
  257 static void     bridge_rtage(struct bridge_softc *);
  258 static void     bridge_rtflush(struct bridge_softc *, int);
  259 static int      bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
  260 
  261 static int      bridge_rtable_init(struct bridge_softc *);
  262 static void     bridge_rtable_fini(struct bridge_softc *);
  263 
  264 static int      bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
  265 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
  266                     const uint8_t *);
  267 static int      bridge_rtnode_insert(struct bridge_softc *,
  268                     struct bridge_rtnode *);
  269 static void     bridge_rtnode_destroy(struct bridge_softc *,
  270                     struct bridge_rtnode *);
  271 static void     bridge_rtable_expire(struct ifnet *, int);
  272 static void     bridge_state_change(struct ifnet *, int);
  273 
  274 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
  275                     const char *name);
  276 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
  277                     struct ifnet *ifp);
  278 static void     bridge_delete_member(struct bridge_softc *,
  279                     struct bridge_iflist *, int);
  280 static void     bridge_delete_span(struct bridge_softc *,
  281                     struct bridge_iflist *);
  282 
  283 static int      bridge_ioctl_add(struct bridge_softc *, void *);
  284 static int      bridge_ioctl_del(struct bridge_softc *, void *);
  285 static int      bridge_ioctl_gifflags(struct bridge_softc *, void *);
  286 static int      bridge_ioctl_sifflags(struct bridge_softc *, void *);
  287 static int      bridge_ioctl_scache(struct bridge_softc *, void *);
  288 static int      bridge_ioctl_gcache(struct bridge_softc *, void *);
  289 static int      bridge_ioctl_gifs(struct bridge_softc *, void *);
  290 static int      bridge_ioctl_rts(struct bridge_softc *, void *);
  291 static int      bridge_ioctl_saddr(struct bridge_softc *, void *);
  292 static int      bridge_ioctl_sto(struct bridge_softc *, void *);
  293 static int      bridge_ioctl_gto(struct bridge_softc *, void *);
  294 static int      bridge_ioctl_daddr(struct bridge_softc *, void *);
  295 static int      bridge_ioctl_flush(struct bridge_softc *, void *);
  296 static int      bridge_ioctl_gpri(struct bridge_softc *, void *);
  297 static int      bridge_ioctl_spri(struct bridge_softc *, void *);
  298 static int      bridge_ioctl_ght(struct bridge_softc *, void *);
  299 static int      bridge_ioctl_sht(struct bridge_softc *, void *);
  300 static int      bridge_ioctl_gfd(struct bridge_softc *, void *);
  301 static int      bridge_ioctl_sfd(struct bridge_softc *, void *);
  302 static int      bridge_ioctl_gma(struct bridge_softc *, void *);
  303 static int      bridge_ioctl_sma(struct bridge_softc *, void *);
  304 static int      bridge_ioctl_sifprio(struct bridge_softc *, void *);
  305 static int      bridge_ioctl_sifcost(struct bridge_softc *, void *);
  306 static int      bridge_ioctl_addspan(struct bridge_softc *, void *);
  307 static int      bridge_ioctl_delspan(struct bridge_softc *, void *);
  308 static int      bridge_ioctl_gbparam(struct bridge_softc *, void *);
  309 static int      bridge_ioctl_grte(struct bridge_softc *, void *);
  310 static int      bridge_ioctl_gifsstp(struct bridge_softc *, void *);
  311 static int      bridge_ioctl_sproto(struct bridge_softc *, void *);
  312 static int      bridge_ioctl_stxhc(struct bridge_softc *, void *);
  313 static int      bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
  314                     int);
  315 static int      bridge_ip_checkbasic(struct mbuf **mp);
  316 #ifdef INET6
  317 static int      bridge_ip6_checkbasic(struct mbuf **mp);
  318 #endif /* INET6 */
  319 static int      bridge_fragment(struct ifnet *, struct mbuf *,
  320                     struct ether_header *, int, struct llc *);
  321 
  322 static struct bstp_cb_ops bridge_ops = {
  323         .bcb_state = bridge_state_change,
  324         .bcb_rtage = bridge_rtable_expire
  325 };
  326 
  327 SYSCTL_DECL(_net_link);
  328 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
  329 
  330 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
  331 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
  332 static int pfil_member = 1; /* run pfil hooks on the member interface */
  333 static int pfil_ipfw = 0;   /* layer2 filter with ipfw */
  334 static int pfil_ipfw_arp = 0;   /* layer2 filter with ipfw */
  335 static int pfil_local_phys = 0; /* run pfil hooks on the physical interface for
  336                                    locally destined packets */
  337 static int log_stp   = 0;   /* log STP state changes */
  338 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
  339     &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
  340 SYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp, CTLFLAG_RW,
  341     &pfil_ipfw_arp, 0, "Filter ARP packets through IPFW layer2");
  342 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
  343     &pfil_bridge, 0, "Packet filter on the bridge interface");
  344 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
  345     &pfil_member, 0, "Packet filter on the member interface");
  346 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_local_phys, CTLFLAG_RW,
  347     &pfil_local_phys, 0,
  348     "Packet filter on the physical interface for locally destined packets");
  349 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, CTLFLAG_RW,
  350     &log_stp, 0, "Log STP state changes");
  351 
  352 struct bridge_control {
  353         int     (*bc_func)(struct bridge_softc *, void *);
  354         int     bc_argsize;
  355         int     bc_flags;
  356 };
  357 
  358 #define BC_F_COPYIN             0x01    /* copy arguments in */
  359 #define BC_F_COPYOUT            0x02    /* copy arguments out */
  360 #define BC_F_SUSER              0x04    /* do super-user check */
  361 
  362 const struct bridge_control bridge_control_table[] = {
  363         { bridge_ioctl_add,             sizeof(struct ifbreq),
  364           BC_F_COPYIN|BC_F_SUSER },
  365         { bridge_ioctl_del,             sizeof(struct ifbreq),
  366           BC_F_COPYIN|BC_F_SUSER },
  367 
  368         { bridge_ioctl_gifflags,        sizeof(struct ifbreq),
  369           BC_F_COPYIN|BC_F_COPYOUT },
  370         { bridge_ioctl_sifflags,        sizeof(struct ifbreq),
  371           BC_F_COPYIN|BC_F_SUSER },
  372 
  373         { bridge_ioctl_scache,          sizeof(struct ifbrparam),
  374           BC_F_COPYIN|BC_F_SUSER },
  375         { bridge_ioctl_gcache,          sizeof(struct ifbrparam),
  376           BC_F_COPYOUT },
  377 
  378         { bridge_ioctl_gifs,            sizeof(struct ifbifconf),
  379           BC_F_COPYIN|BC_F_COPYOUT },
  380         { bridge_ioctl_rts,             sizeof(struct ifbaconf),
  381           BC_F_COPYIN|BC_F_COPYOUT },
  382 
  383         { bridge_ioctl_saddr,           sizeof(struct ifbareq),
  384           BC_F_COPYIN|BC_F_SUSER },
  385 
  386         { bridge_ioctl_sto,             sizeof(struct ifbrparam),
  387           BC_F_COPYIN|BC_F_SUSER },
  388         { bridge_ioctl_gto,             sizeof(struct ifbrparam),
  389           BC_F_COPYOUT },
  390 
  391         { bridge_ioctl_daddr,           sizeof(struct ifbareq),
  392           BC_F_COPYIN|BC_F_SUSER },
  393 
  394         { bridge_ioctl_flush,           sizeof(struct ifbreq),
  395           BC_F_COPYIN|BC_F_SUSER },
  396 
  397         { bridge_ioctl_gpri,            sizeof(struct ifbrparam),
  398           BC_F_COPYOUT },
  399         { bridge_ioctl_spri,            sizeof(struct ifbrparam),
  400           BC_F_COPYIN|BC_F_SUSER },
  401 
  402         { bridge_ioctl_ght,             sizeof(struct ifbrparam),
  403           BC_F_COPYOUT },
  404         { bridge_ioctl_sht,             sizeof(struct ifbrparam),
  405           BC_F_COPYIN|BC_F_SUSER },
  406 
  407         { bridge_ioctl_gfd,             sizeof(struct ifbrparam),
  408           BC_F_COPYOUT },
  409         { bridge_ioctl_sfd,             sizeof(struct ifbrparam),
  410           BC_F_COPYIN|BC_F_SUSER },
  411 
  412         { bridge_ioctl_gma,             sizeof(struct ifbrparam),
  413           BC_F_COPYOUT },
  414         { bridge_ioctl_sma,             sizeof(struct ifbrparam),
  415           BC_F_COPYIN|BC_F_SUSER },
  416 
  417         { bridge_ioctl_sifprio,         sizeof(struct ifbreq),
  418           BC_F_COPYIN|BC_F_SUSER },
  419 
  420         { bridge_ioctl_sifcost,         sizeof(struct ifbreq),
  421           BC_F_COPYIN|BC_F_SUSER },
  422 
  423         { bridge_ioctl_addspan,         sizeof(struct ifbreq),
  424           BC_F_COPYIN|BC_F_SUSER },
  425         { bridge_ioctl_delspan,         sizeof(struct ifbreq),
  426           BC_F_COPYIN|BC_F_SUSER },
  427 
  428         { bridge_ioctl_gbparam,         sizeof(struct ifbropreq),
  429           BC_F_COPYOUT },
  430 
  431         { bridge_ioctl_grte,            sizeof(struct ifbrparam),
  432           BC_F_COPYOUT },
  433 
  434         { bridge_ioctl_gifsstp,         sizeof(struct ifbpstpconf),
  435           BC_F_COPYIN|BC_F_COPYOUT },
  436 
  437         { bridge_ioctl_sproto,          sizeof(struct ifbrparam),
  438           BC_F_COPYIN|BC_F_SUSER },
  439 
  440         { bridge_ioctl_stxhc,           sizeof(struct ifbrparam),
  441           BC_F_COPYIN|BC_F_SUSER },
  442 };
  443 const int bridge_control_table_size =
  444     sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
  445 
  446 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
  447                         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  448 
  449 LIST_HEAD(, bridge_softc) bridge_list;
  450 
  451 IFC_SIMPLE_DECLARE(bridge, 0);
  452 
  453 static int
  454 bridge_modevent(module_t mod, int type, void *data)
  455 {
  456 
  457         switch (type) {
  458         case MOD_LOAD:
  459                 mtx_init(&bridge_list_mtx, "if_bridge list", NULL, MTX_DEF);
  460                 if_clone_attach(&bridge_cloner);
  461                 bridge_rtnode_zone = uma_zcreate("bridge_rtnode",
  462                     sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL,
  463                     UMA_ALIGN_PTR, 0);
  464                 LIST_INIT(&bridge_list);
  465                 bridge_input_p = bridge_input;
  466                 bridge_output_p = bridge_output;
  467                 bridge_dn_p = bridge_dummynet;
  468                 bridge_detach_cookie = EVENTHANDLER_REGISTER(
  469                     ifnet_departure_event, bridge_ifdetach, NULL,
  470                     EVENTHANDLER_PRI_ANY);
  471                 break;
  472         case MOD_UNLOAD:
  473                 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
  474                     bridge_detach_cookie);
  475                 if_clone_detach(&bridge_cloner);
  476                 while (!LIST_EMPTY(&bridge_list))
  477                         bridge_clone_destroy(LIST_FIRST(&bridge_list)->sc_ifp);
  478                 uma_zdestroy(bridge_rtnode_zone);
  479                 bridge_input_p = NULL;
  480                 bridge_output_p = NULL;
  481                 bridge_dn_p = NULL;
  482                 mtx_destroy(&bridge_list_mtx);
  483                 break;
  484         default:
  485                 return (EOPNOTSUPP);
  486         }
  487         return (0);
  488 }
  489 
  490 static moduledata_t bridge_mod = {
  491         "if_bridge",
  492         bridge_modevent,
  493         0
  494 };
  495 
  496 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  497 MODULE_DEPEND(if_bridge, bridgestp, 1, 1, 1);
  498 
  499 /*
  500  * handler for net.link.bridge.pfil_ipfw
  501  */
  502 static int
  503 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS)
  504 {
  505         int enable = pfil_ipfw;
  506         int error;
  507 
  508         error = sysctl_handle_int(oidp, &enable, 0, req);
  509         enable = (enable) ? 1 : 0;
  510 
  511         if (enable != pfil_ipfw) {
  512                 pfil_ipfw = enable;
  513 
  514                 /*
  515                  * Disable pfil so that ipfw doesnt run twice, if the user
  516                  * really wants both then they can re-enable pfil_bridge and/or
  517                  * pfil_member. Also allow non-ip packets as ipfw can filter by
  518                  * layer2 type.
  519                  */
  520                 if (pfil_ipfw) {
  521                         pfil_onlyip = 0;
  522                         pfil_bridge = 0;
  523                         pfil_member = 0;
  524                 }
  525         }
  526 
  527         return (error);
  528 }
  529 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw, CTLTYPE_INT|CTLFLAG_RW,
  530             &pfil_ipfw, 0, &sysctl_pfil_ipfw, "I", "Layer2 filter with IPFW");
  531 
  532 /*
  533  * bridge_clone_create:
  534  *
  535  *      Create a new bridge instance.
  536  */
  537 static int
  538 bridge_clone_create(struct if_clone *ifc, int unit)
  539 {
  540         struct bridge_softc *sc, *sc2;
  541         struct ifnet *bifp, *ifp;
  542         u_char eaddr[6];
  543         int retry;
  544 
  545         sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
  546         ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
  547         if (ifp == NULL) {
  548                 free(sc, M_DEVBUF);
  549                 return (ENOSPC);
  550         }
  551 
  552         BRIDGE_LOCK_INIT(sc);
  553         sc->sc_brtmax = BRIDGE_RTABLE_MAX;
  554         sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
  555 
  556         /* Initialize our routing table. */
  557         bridge_rtable_init(sc);
  558 
  559         callout_init_mtx(&sc->sc_brcallout, &sc->sc_mtx, 0);
  560 
  561         LIST_INIT(&sc->sc_iflist);
  562         LIST_INIT(&sc->sc_spanlist);
  563 
  564         ifp->if_softc = sc;
  565         if_initname(ifp, ifc->ifc_name, unit);
  566         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
  567         ifp->if_ioctl = bridge_ioctl;
  568         ifp->if_start = bridge_start;
  569         ifp->if_init = bridge_init;
  570         ifp->if_type = IFT_BRIDGE;
  571         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
  572         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
  573         IFQ_SET_READY(&ifp->if_snd);
  574 
  575         /*
  576          * Generate a random ethernet address with a locally administered
  577          * address.
  578          *
  579          * Since we are using random ethernet addresses for the bridge, it is
  580          * possible that we might have address collisions, so make sure that
  581          * this hardware address isn't already in use on another bridge.
  582          */
  583         for (retry = 1; retry != 0;) {
  584                 arc4rand(eaddr, ETHER_ADDR_LEN, 1);
  585                 eaddr[0] &= ~1;         /* clear multicast bit */
  586                 eaddr[0] |= 2;          /* set the LAA bit */
  587                 retry = 0;
  588                 mtx_lock(&bridge_list_mtx);
  589                 LIST_FOREACH(sc2, &bridge_list, sc_list) {
  590                         bifp = sc2->sc_ifp;
  591                         if (memcmp(eaddr, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0)
  592                                 retry = 1;
  593                 }
  594                 mtx_unlock(&bridge_list_mtx);
  595         }
  596 
  597         bstp_attach(&sc->sc_stp, &bridge_ops);
  598         ether_ifattach(ifp, eaddr);
  599         /* Now undo some of the damage... */
  600         ifp->if_baudrate = 0;
  601         ifp->if_type = IFT_BRIDGE;
  602 
  603         mtx_lock(&bridge_list_mtx);
  604         LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
  605         mtx_unlock(&bridge_list_mtx);
  606 
  607         return (0);
  608 }
  609 
  610 /*
  611  * bridge_clone_destroy:
  612  *
  613  *      Destroy a bridge instance.
  614  */
  615 static void
  616 bridge_clone_destroy(struct ifnet *ifp)
  617 {
  618         struct bridge_softc *sc = ifp->if_softc;
  619         struct bridge_iflist *bif;
  620 
  621         BRIDGE_LOCK(sc);
  622 
  623         bridge_stop(ifp, 1);
  624         ifp->if_flags &= ~IFF_UP;
  625 
  626         while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
  627                 bridge_delete_member(sc, bif, 0);
  628 
  629         while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
  630                 bridge_delete_span(sc, bif);
  631         }
  632 
  633         BRIDGE_UNLOCK(sc);
  634 
  635         callout_drain(&sc->sc_brcallout);
  636 
  637         mtx_lock(&bridge_list_mtx);
  638         LIST_REMOVE(sc, sc_list);
  639         mtx_unlock(&bridge_list_mtx);
  640 
  641         bstp_detach(&sc->sc_stp);
  642         ether_ifdetach(ifp);
  643         if_free_type(ifp, IFT_ETHER);
  644 
  645         /* Tear down the routing table. */
  646         bridge_rtable_fini(sc);
  647 
  648         BRIDGE_LOCK_DESTROY(sc);
  649         free(sc, M_DEVBUF);
  650 }
  651 
  652 /*
  653  * bridge_ioctl:
  654  *
  655  *      Handle a control request from the operator.
  656  */
  657 static int
  658 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  659 {
  660         struct bridge_softc *sc = ifp->if_softc;
  661         struct thread *td = curthread;
  662         union {
  663                 struct ifbreq ifbreq;
  664                 struct ifbifconf ifbifconf;
  665                 struct ifbareq ifbareq;
  666                 struct ifbaconf ifbaconf;
  667                 struct ifbrparam ifbrparam;
  668                 struct ifbropreq ifbropreq;
  669         } args;
  670         struct ifdrv *ifd = (struct ifdrv *) data;
  671         const struct bridge_control *bc;
  672         int error = 0;
  673 
  674         switch (cmd) {
  675 
  676         case SIOCADDMULTI:
  677         case SIOCDELMULTI:
  678                 break;
  679 
  680         case SIOCGDRVSPEC:
  681         case SIOCSDRVSPEC:
  682                 if (ifd->ifd_cmd >= bridge_control_table_size) {
  683                         error = EINVAL;
  684                         break;
  685                 }
  686                 bc = &bridge_control_table[ifd->ifd_cmd];
  687 
  688                 if (cmd == SIOCGDRVSPEC &&
  689                     (bc->bc_flags & BC_F_COPYOUT) == 0) {
  690                         error = EINVAL;
  691                         break;
  692                 }
  693                 else if (cmd == SIOCSDRVSPEC &&
  694                     (bc->bc_flags & BC_F_COPYOUT) != 0) {
  695                         error = EINVAL;
  696                         break;
  697                 }
  698 
  699                 if (bc->bc_flags & BC_F_SUSER) {
  700                         error = suser(td);
  701                         if (error)
  702                                 break;
  703                 }
  704 
  705                 if (ifd->ifd_len != bc->bc_argsize ||
  706                     ifd->ifd_len > sizeof(args)) {
  707                         error = EINVAL;
  708                         break;
  709                 }
  710 
  711                 bzero(&args, sizeof(args));
  712                 if (bc->bc_flags & BC_F_COPYIN) {
  713                         error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
  714                         if (error)
  715                                 break;
  716                 }
  717 
  718                 BRIDGE_LOCK(sc);
  719                 error = (*bc->bc_func)(sc, &args);
  720                 BRIDGE_UNLOCK(sc);
  721                 if (error)
  722                         break;
  723 
  724                 if (bc->bc_flags & BC_F_COPYOUT)
  725                         error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
  726 
  727                 break;
  728 
  729         case SIOCSIFFLAGS:
  730                 if (!(ifp->if_flags & IFF_UP) &&
  731                     (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  732                         /*
  733                          * If interface is marked down and it is running,
  734                          * then stop and disable it.
  735                          */
  736                         BRIDGE_LOCK(sc);
  737                         bridge_stop(ifp, 1);
  738                         BRIDGE_UNLOCK(sc);
  739                 } else if ((ifp->if_flags & IFF_UP) &&
  740                     !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
  741                         /*
  742                          * If interface is marked up and it is stopped, then
  743                          * start it.
  744                          */
  745                         (*ifp->if_init)(sc);
  746                 }
  747                 break;
  748 
  749         case SIOCSIFMTU:
  750                 /* Do not allow the MTU to be changed on the bridge */
  751                 error = EINVAL;
  752                 break;
  753 
  754         default:
  755                 /*
  756                  * drop the lock as ether_ioctl() will call bridge_start() and
  757                  * cause the lock to be recursed.
  758                  */
  759                 error = ether_ioctl(ifp, cmd, data);
  760                 break;
  761         }
  762 
  763         return (error);
  764 }
  765 
  766 /*
  767  * bridge_mutecaps:
  768  *
  769  *      Clear or restore unwanted capabilities on the member interface
  770  */
  771 static void
  772 bridge_mutecaps(struct bridge_softc *sc)
  773 {
  774         struct bridge_iflist *bif;
  775         int enabled, mask;
  776 
  777         /* Initial bitmask of capabilities to test */
  778         mask = BRIDGE_IFCAPS_MASK;
  779 
  780         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
  781                 /* Every member must support it or its disabled */
  782                 mask &= bif->bif_savedcaps;
  783         }
  784 
  785         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
  786                 enabled = bif->bif_ifp->if_capenable;
  787                 enabled &= ~BRIDGE_IFCAPS_STRIP;
  788                 /* strip off mask bits and enable them again if allowed */
  789                 enabled &= ~BRIDGE_IFCAPS_MASK;
  790                 enabled |= mask;
  791 
  792                 bridge_set_ifcap(sc, bif, enabled);
  793         }
  794 
  795 }
  796 
  797 static void
  798 bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set)
  799 {
  800         struct ifnet *ifp = bif->bif_ifp;
  801         struct ifreq ifr;
  802         int error;
  803 
  804         bzero(&ifr, sizeof(ifr));
  805         ifr.ifr_reqcap = set;
  806 
  807         if (ifp->if_capenable != set) {
  808                 IFF_LOCKGIANT(ifp);
  809                 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
  810                 IFF_UNLOCKGIANT(ifp);
  811                 if (error)
  812                         if_printf(sc->sc_ifp,
  813                             "error setting interface capabilities on %s\n",
  814                             ifp->if_xname);
  815         }
  816 }
  817 
  818 /*
  819  * bridge_lookup_member:
  820  *
  821  *      Lookup a bridge member interface.
  822  */
  823 static struct bridge_iflist *
  824 bridge_lookup_member(struct bridge_softc *sc, const char *name)
  825 {
  826         struct bridge_iflist *bif;
  827         struct ifnet *ifp;
  828 
  829         BRIDGE_LOCK_ASSERT(sc);
  830 
  831         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
  832                 ifp = bif->bif_ifp;
  833                 if (strcmp(ifp->if_xname, name) == 0)
  834                         return (bif);
  835         }
  836 
  837         return (NULL);
  838 }
  839 
  840 /*
  841  * bridge_lookup_member_if:
  842  *
  843  *      Lookup a bridge member interface by ifnet*.
  844  */
  845 static struct bridge_iflist *
  846 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
  847 {
  848         struct bridge_iflist *bif;
  849 
  850         BRIDGE_LOCK_ASSERT(sc);
  851 
  852         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
  853                 if (bif->bif_ifp == member_ifp)
  854                         return (bif);
  855         }
  856 
  857         return (NULL);
  858 }
  859 
  860 /*
  861  * bridge_delete_member:
  862  *
  863  *      Delete the specified member interface.
  864  */
  865 static void
  866 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
  867     int gone)
  868 {
  869         struct ifnet *ifs = bif->bif_ifp;
  870 
  871         BRIDGE_LOCK_ASSERT(sc);
  872 
  873         if (!gone) {
  874                 switch (ifs->if_type) {
  875                 case IFT_ETHER:
  876                 case IFT_L2VLAN:
  877                         /*
  878                          * Take the interface out of promiscuous mode.
  879                          */
  880                         (void) ifpromisc(ifs, 0);
  881                         break;
  882 
  883                 case IFT_GIF:
  884                         break;
  885 
  886                 default:
  887 #ifdef DIAGNOSTIC
  888                         panic("bridge_delete_member: impossible");
  889 #endif
  890                         break;
  891                 }
  892                 /* reneable any interface capabilities */
  893                 bridge_set_ifcap(sc, bif, bif->bif_savedcaps);
  894         }
  895 
  896         if (bif->bif_flags & IFBIF_STP)
  897                 bstp_disable(&bif->bif_stp);
  898 
  899         ifs->if_bridge = NULL;
  900         BRIDGE_XLOCK(sc);
  901         LIST_REMOVE(bif, bif_next);
  902         BRIDGE_XDROP(sc);
  903 
  904         bridge_mutecaps(sc);    /* recalcuate now this interface is removed */
  905         bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
  906 
  907         BRIDGE_UNLOCK(sc);
  908         bstp_destroy(&bif->bif_stp);    /* prepare to free */
  909         BRIDGE_LOCK(sc);
  910         free(bif, M_DEVBUF);
  911 }
  912 
  913 /*
  914  * bridge_delete_span:
  915  *
  916  *      Delete the specified span interface.
  917  */
  918 static void
  919 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
  920 {
  921         BRIDGE_LOCK_ASSERT(sc);
  922 
  923         KASSERT(bif->bif_ifp->if_bridge == NULL,
  924             ("%s: not a span interface", __func__));
  925 
  926         LIST_REMOVE(bif, bif_next);
  927         free(bif, M_DEVBUF);
  928 }
  929 
  930 static int
  931 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
  932 {
  933         struct ifbreq *req = arg;
  934         struct bridge_iflist *bif = NULL;
  935         struct ifnet *ifs;
  936         int error = 0;
  937 
  938         ifs = ifunit(req->ifbr_ifsname);
  939         if (ifs == NULL)
  940                 return (ENOENT);
  941         if (ifs->if_ioctl == NULL)      /* must be supported */
  942                 return (EINVAL);
  943 
  944         /* If it's in the span list, it can't be a member. */
  945         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
  946                 if (ifs == bif->bif_ifp)
  947                         return (EBUSY);
  948 
  949         /* Allow the first Ethernet member to define the MTU */
  950         if (ifs->if_type != IFT_GIF) {
  951                 if (LIST_EMPTY(&sc->sc_iflist))
  952                         sc->sc_ifp->if_mtu = ifs->if_mtu;
  953                 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
  954                         if_printf(sc->sc_ifp, "invalid MTU for %s\n",
  955                             ifs->if_xname);
  956                         return (EINVAL);
  957                 }
  958         }
  959 
  960         if (ifs->if_bridge == sc)
  961                 return (EEXIST);
  962 
  963         if (ifs->if_bridge != NULL)
  964                 return (EBUSY);
  965 
  966         bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
  967         if (bif == NULL)
  968                 return (ENOMEM);
  969 
  970         bif->bif_ifp = ifs;
  971         bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
  972         bif->bif_savedcaps = ifs->if_capenable;
  973 
  974         switch (ifs->if_type) {
  975         case IFT_ETHER:
  976         case IFT_L2VLAN:
  977                 /*
  978                  * Place the interface into promiscuous mode.
  979                  */
  980                 error = ifpromisc(ifs, 1);
  981                 if (error)
  982                         goto out;
  983                 break;
  984 
  985         case IFT_GIF:
  986                 break;
  987 
  988         default:
  989                 error = EINVAL;
  990                 goto out;
  991         }
  992 
  993         ifs->if_bridge = sc;
  994         bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
  995         /*
  996          * XXX: XLOCK HERE!?!
  997          *
  998          * NOTE: insert_***HEAD*** should be safe for the traversals.
  999          */
 1000         LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
 1001 
 1002         /* Set interface capabilities to the intersection set of all members */
 1003         bridge_mutecaps(sc);
 1004 out:
 1005         if (error) {
 1006                 if (bif != NULL)
 1007                         free(bif, M_DEVBUF);
 1008         }
 1009         return (error);
 1010 }
 1011 
 1012 static int
 1013 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
 1014 {
 1015         struct ifbreq *req = arg;
 1016         struct bridge_iflist *bif;
 1017 
 1018         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
 1019         if (bif == NULL)
 1020                 return (ENOENT);
 1021 
 1022         bridge_delete_member(sc, bif, 0);
 1023 
 1024         return (0);
 1025 }
 1026 
 1027 static int
 1028 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
 1029 {
 1030         struct ifbreq *req = arg;
 1031         struct bridge_iflist *bif;
 1032         struct bstp_port *bp;
 1033 
 1034         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
 1035         if (bif == NULL)
 1036                 return (ENOENT);
 1037 
 1038         bp = &bif->bif_stp;
 1039         req->ifbr_ifsflags = bif->bif_flags;
 1040         req->ifbr_state = bp->bp_state;
 1041         req->ifbr_priority = bp->bp_priority;
 1042         req->ifbr_path_cost = bp->bp_path_cost;
 1043         req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
 1044         req->ifbr_proto = bp->bp_protover;
 1045         req->ifbr_role = bp->bp_role;
 1046         req->ifbr_stpflags = bp->bp_flags;
 1047 
 1048         /* Copy STP state options as flags */
 1049         if (bp->bp_operedge)
 1050                 req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
 1051         if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
 1052                 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
 1053         if (bp->bp_ptp_link)
 1054                 req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
 1055         if (bp->bp_flags & BSTP_PORT_AUTOPTP)
 1056                 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
 1057         if (bp->bp_flags & BSTP_PORT_ADMEDGE)
 1058                 req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE;
 1059         if (bp->bp_flags & BSTP_PORT_ADMCOST)
 1060                 req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST;
 1061         return (0);
 1062 }
 1063 
 1064 static int
 1065 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
 1066 {
 1067         struct ifbreq *req = arg;
 1068         struct bridge_iflist *bif;
 1069         struct bstp_port *bp;
 1070         int error;
 1071 
 1072         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
 1073         if (bif == NULL)
 1074                 return (ENOENT);
 1075         bp = &bif->bif_stp;
 1076 
 1077         if (req->ifbr_ifsflags & IFBIF_SPAN)
 1078                 /* SPAN is readonly */
 1079                 return (EINVAL);
 1080 
 1081         if (req->ifbr_ifsflags & IFBIF_STP) {
 1082                 if ((bif->bif_flags & IFBIF_STP) == 0) {
 1083                         error = bstp_enable(&bif->bif_stp);
 1084                         if (error)
 1085                                 return (error);
 1086                 }
 1087         } else {
 1088                 if ((bif->bif_flags & IFBIF_STP) != 0)
 1089                         bstp_disable(&bif->bif_stp);
 1090         }
 1091 
 1092         /* Pass on STP flags */
 1093         bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
 1094         bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
 1095         bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
 1096         bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
 1097 
 1098         /* Save the bits relating to the bridge */
 1099         bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK;
 1100 
 1101         return (0);
 1102 }
 1103 
 1104 static int
 1105 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
 1106 {
 1107         struct ifbrparam *param = arg;
 1108 
 1109         sc->sc_brtmax = param->ifbrp_csize;
 1110         bridge_rttrim(sc);
 1111 
 1112         return (0);
 1113 }
 1114 
 1115 static int
 1116 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
 1117 {
 1118         struct ifbrparam *param = arg;
 1119 
 1120         param->ifbrp_csize = sc->sc_brtmax;
 1121 
 1122         return (0);
 1123 }
 1124 
 1125 static int
 1126 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
 1127 {
 1128         struct ifbifconf *bifc = arg;
 1129         struct bridge_iflist *bif;
 1130         struct ifbreq breq;
 1131         char *buf, *outbuf;
 1132         int count, buflen, len, error = 0;
 1133 
 1134         count = 0;
 1135         LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
 1136                 count++;
 1137         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
 1138                 count++;
 1139 
 1140         buflen = sizeof(breq) * count;
 1141         if (bifc->ifbic_len == 0) {
 1142                 bifc->ifbic_len = buflen;
 1143                 return (0);
 1144         }
 1145         BRIDGE_UNLOCK(sc);
 1146         outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
 1147         BRIDGE_LOCK(sc);
 1148 
 1149         count = 0;
 1150         buf = outbuf;
 1151         len = min(bifc->ifbic_len, buflen);
 1152         bzero(&breq, sizeof(breq));
 1153         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
 1154                 if (len < sizeof(breq))
 1155                         break;
 1156 
 1157                 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
 1158                     sizeof(breq.ifbr_ifsname));
 1159                 /* Fill in the ifbreq structure */
 1160                 error = bridge_ioctl_gifflags(sc, &breq);
 1161                 if (error)
 1162                         break;
 1163                 memcpy(buf, &breq, sizeof(breq));
 1164                 count++;
 1165                 buf += sizeof(breq);
 1166                 len -= sizeof(breq);
 1167         }
 1168         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
 1169                 if (len < sizeof(breq))
 1170                         break;
 1171 
 1172                 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
 1173                     sizeof(breq.ifbr_ifsname));
 1174                 breq.ifbr_ifsflags = bif->bif_flags;
 1175                 breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff;
 1176                 memcpy(buf, &breq, sizeof(breq));
 1177                 count++;
 1178                 buf += sizeof(breq);
 1179                 len -= sizeof(breq);
 1180         }
 1181 
 1182         BRIDGE_UNLOCK(sc);
 1183         bifc->ifbic_len = sizeof(breq) * count;
 1184         error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len);
 1185         BRIDGE_LOCK(sc);
 1186         free(outbuf, M_TEMP);
 1187         return (error);
 1188 }
 1189 
 1190 static int
 1191 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
 1192 {
 1193         struct ifbaconf *bac = arg;
 1194         struct bridge_rtnode *brt;
 1195         struct ifbareq bareq;
 1196         char *buf, *outbuf;
 1197         int count, buflen, len, error = 0;
 1198 
 1199         if (bac->ifbac_len == 0)
 1200                 return (0);
 1201 
 1202         count = 0;
 1203         LIST_FOREACH(brt, &sc->sc_rtlist, brt_list)
 1204                 count++;
 1205         buflen = sizeof(bareq) * count;
 1206 
 1207         BRIDGE_UNLOCK(sc);
 1208         outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
 1209         BRIDGE_LOCK(sc);
 1210 
 1211         count = 0;
 1212         buf = outbuf;
 1213         len = min(bac->ifbac_len, buflen);
 1214         bzero(&bareq, sizeof(bareq));
 1215         LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
 1216                 if (len < sizeof(bareq))
 1217                         goto out;
 1218                 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
 1219                     sizeof(bareq.ifba_ifsname));
 1220                 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
 1221                 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
 1222                                 time_uptime < brt->brt_expire)
 1223                         bareq.ifba_expire = brt->brt_expire - time_uptime;
 1224                 else
 1225                         bareq.ifba_expire = 0;
 1226                 bareq.ifba_flags = brt->brt_flags;
 1227 
 1228                 memcpy(buf, &bareq, sizeof(bareq));
 1229                 count++;
 1230                 buf += sizeof(bareq);
 1231                 len -= sizeof(bareq);
 1232         }
 1233 out:
 1234         BRIDGE_UNLOCK(sc);
 1235         bac->ifbac_len = sizeof(bareq) * count;
 1236         error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len);
 1237         BRIDGE_LOCK(sc);
 1238         free(outbuf, M_TEMP);
 1239         return (error);
 1240 }
 1241 
 1242 static int
 1243 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
 1244 {
 1245         struct ifbareq *req = arg;
 1246         struct bridge_iflist *bif;
 1247         int error;
 1248 
 1249         bif = bridge_lookup_member(sc, req->ifba_ifsname);
 1250         if (bif == NULL)
 1251                 return (ENOENT);
 1252 
 1253         error = bridge_rtupdate(sc, req->ifba_dst, bif, 1,
 1254             req->ifba_flags);
 1255 
 1256         return (error);
 1257 }
 1258 
 1259 static int
 1260 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
 1261 {
 1262         struct ifbrparam *param = arg;
 1263 
 1264         sc->sc_brttimeout = param->ifbrp_ctime;
 1265         return (0);
 1266 }
 1267 
 1268 static int
 1269 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
 1270 {
 1271         struct ifbrparam *param = arg;
 1272 
 1273         param->ifbrp_ctime = sc->sc_brttimeout;
 1274         return (0);
 1275 }
 1276 
 1277 static int
 1278 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
 1279 {
 1280         struct ifbareq *req = arg;
 1281 
 1282         return (bridge_rtdaddr(sc, req->ifba_dst));
 1283 }
 1284 
 1285 static int
 1286 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
 1287 {
 1288         struct ifbreq *req = arg;
 1289 
 1290         bridge_rtflush(sc, req->ifbr_ifsflags);
 1291         return (0);
 1292 }
 1293 
 1294 static int
 1295 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
 1296 {
 1297         struct ifbrparam *param = arg;
 1298         struct bstp_state *bs = &sc->sc_stp;
 1299 
 1300         param->ifbrp_prio = bs->bs_bridge_priority;
 1301         return (0);
 1302 }
 1303 
 1304 static int
 1305 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
 1306 {
 1307         struct ifbrparam *param = arg;
 1308 
 1309         return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio));
 1310 }
 1311 
 1312 static int
 1313 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
 1314 {
 1315         struct ifbrparam *param = arg;
 1316         struct bstp_state *bs = &sc->sc_stp;
 1317 
 1318         param->ifbrp_hellotime = bs->bs_bridge_htime >> 8;
 1319         return (0);
 1320 }
 1321 
 1322 static int
 1323 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
 1324 {
 1325         struct ifbrparam *param = arg;
 1326 
 1327         return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime));
 1328 }
 1329 
 1330 static int
 1331 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
 1332 {
 1333         struct ifbrparam *param = arg;
 1334         struct bstp_state *bs = &sc->sc_stp;
 1335 
 1336         param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
 1337         return (0);
 1338 }
 1339 
 1340 static int
 1341 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
 1342 {
 1343         struct ifbrparam *param = arg;
 1344 
 1345         return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay));
 1346 }
 1347 
 1348 static int
 1349 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
 1350 {
 1351         struct ifbrparam *param = arg;
 1352         struct bstp_state *bs = &sc->sc_stp;
 1353 
 1354         param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
 1355         return (0);
 1356 }
 1357 
 1358 static int
 1359 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
 1360 {
 1361         struct ifbrparam *param = arg;
 1362 
 1363         return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage));
 1364 }
 1365 
 1366 static int
 1367 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
 1368 {
 1369         struct ifbreq *req = arg;
 1370         struct bridge_iflist *bif;
 1371 
 1372         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
 1373         if (bif == NULL)
 1374                 return (ENOENT);
 1375 
 1376         return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority));
 1377 }
 1378 
 1379 static int
 1380 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
 1381 {
 1382         struct ifbreq *req = arg;
 1383         struct bridge_iflist *bif;
 1384 
 1385         bif = bridge_lookup_member(sc, req->ifbr_ifsname);
 1386         if (bif == NULL)
 1387                 return (ENOENT);
 1388 
 1389         return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost));
 1390 }
 1391 
 1392 static int
 1393 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
 1394 {
 1395         struct ifbreq *req = arg;
 1396         struct bridge_iflist *bif = NULL;
 1397         struct ifnet *ifs;
 1398 
 1399         ifs = ifunit(req->ifbr_ifsname);
 1400         if (ifs == NULL)
 1401                 return (ENOENT);
 1402 
 1403         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
 1404                 if (ifs == bif->bif_ifp)
 1405                         return (EBUSY);
 1406 
 1407         if (ifs->if_bridge != NULL)
 1408                 return (EBUSY);
 1409 
 1410         switch (ifs->if_type) {
 1411                 case IFT_ETHER:
 1412                 case IFT_GIF:
 1413                 case IFT_L2VLAN:
 1414                         break;
 1415                 default:
 1416                         return (EINVAL);
 1417         }
 1418 
 1419         bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
 1420         if (bif == NULL)
 1421                 return (ENOMEM);
 1422 
 1423         bif->bif_ifp = ifs;
 1424         bif->bif_flags = IFBIF_SPAN;
 1425 
 1426         LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
 1427 
 1428         return (0);
 1429 }
 1430 
 1431 static int
 1432 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
 1433 {
 1434         struct ifbreq *req = arg;
 1435         struct bridge_iflist *bif;
 1436         struct ifnet *ifs;
 1437 
 1438         ifs = ifunit(req->ifbr_ifsname);
 1439         if (ifs == NULL)
 1440                 return (ENOENT);
 1441 
 1442         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
 1443                 if (ifs == bif->bif_ifp)
 1444                         break;
 1445 
 1446         if (bif == NULL)
 1447                 return (ENOENT);
 1448 
 1449         bridge_delete_span(sc, bif);
 1450 
 1451         return (0);
 1452 }
 1453 
 1454 static int
 1455 bridge_ioctl_gbparam(struct bridge_softc *sc, void *arg)
 1456 {
 1457         struct ifbropreq *req = arg;
 1458         struct bstp_state *bs = &sc->sc_stp;
 1459         struct bstp_port *root_port;
 1460 
 1461         req->ifbop_maxage = bs->bs_bridge_max_age >> 8;
 1462         req->ifbop_hellotime = bs->bs_bridge_htime >> 8;
 1463         req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8;
 1464 
 1465         root_port = bs->bs_root_port;
 1466         if (root_port == NULL)
 1467                 req->ifbop_root_port = 0;
 1468         else
 1469                 req->ifbop_root_port = root_port->bp_ifp->if_index;
 1470 
 1471         req->ifbop_holdcount = bs->bs_txholdcount;
 1472         req->ifbop_priority = bs->bs_bridge_priority;
 1473         req->ifbop_protocol = bs->bs_protover;
 1474         req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost;
 1475         req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id;
 1476         req->ifbop_designated_root = bs->bs_root_pv.pv_root_id;
 1477         req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id;
 1478         req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec;
 1479         req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec;
 1480 
 1481         return (0);
 1482 }
 1483 
 1484 static int
 1485 bridge_ioctl_grte(struct bridge_softc *sc, void *arg)
 1486 {
 1487         struct ifbrparam *param = arg;
 1488 
 1489         param->ifbrp_cexceeded = sc->sc_brtexceeded;
 1490         return (0);
 1491 }
 1492 
 1493 static int
 1494 bridge_ioctl_gifsstp(struct bridge_softc *sc, void *arg)
 1495 {
 1496         struct ifbpstpconf *bifstp = arg;
 1497         struct bridge_iflist *bif;
 1498         struct bstp_port *bp;
 1499         struct ifbpstpreq bpreq;
 1500         char *buf, *outbuf;
 1501         int count, buflen, len, error = 0;
 1502 
 1503         count = 0;
 1504         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
 1505                 if ((bif->bif_flags & IFBIF_STP) != 0)
 1506                         count++;
 1507         }
 1508 
 1509         buflen = sizeof(bpreq) * count;
 1510         if (bifstp->ifbpstp_len == 0) {
 1511                 bifstp->ifbpstp_len = buflen;
 1512                 return (0);
 1513         }
 1514 
 1515         BRIDGE_UNLOCK(sc);
 1516         outbuf = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
 1517         BRIDGE_LOCK(sc);
 1518 
 1519         count = 0;
 1520         buf = outbuf;
 1521         len = min(bifstp->ifbpstp_len, buflen);
 1522         bzero(&bpreq, sizeof(bpreq));
 1523         LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
 1524                 if (len < sizeof(bpreq))
 1525                         break;
 1526 
 1527                 if ((bif->bif_flags & IFBIF_STP) == 0)
 1528                         continue;
 1529 
 1530                 bp = &bif->bif_stp;
 1531                 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff;
 1532                 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions;
 1533                 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost;
 1534                 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id;
 1535                 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id;
 1536                 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id;
 1537 
 1538                 memcpy(buf, &bpreq, sizeof(bpreq));
 1539                 count++;
 1540                 buf += sizeof(bpreq);
 1541                 len -= sizeof(bpreq);
 1542         }
 1543 
 1544         BRIDGE_UNLOCK(sc);
 1545         bifstp->ifbpstp_len = sizeof(bpreq) * count;
 1546         error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len);
 1547         BRIDGE_LOCK(sc);
 1548         free(outbuf, M_TEMP);
 1549         return (error);
 1550 }
 1551 
 1552 static int
 1553 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg)
 1554 {
 1555         struct ifbrparam *param = arg;
 1556 
 1557         return (bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto));
 1558 }
 1559 
 1560 static int
 1561 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg)
 1562 {
 1563         struct ifbrparam *param = arg;
 1564 
 1565         return (bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc));
 1566 }
 1567 
 1568 /*
 1569  * bridge_ifdetach:
 1570  *
 1571  *      Detach an interface from a bridge.  Called when a member
 1572  *      interface is detaching.
 1573  */
 1574 static void
 1575 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
 1576 {
 1577         struct bridge_softc *sc = ifp->if_bridge;
 1578         struct bridge_iflist *bif;
 1579 
 1580         /* Check if the interface is a bridge member */
 1581         if (sc != NULL) {
 1582                 BRIDGE_LOCK(sc);
 1583 
 1584                 bif = bridge_lookup_member_if(sc, ifp);
 1585                 if (bif != NULL)
 1586                         bridge_delete_member(sc, bif, 1);
 1587 
 1588                 BRIDGE_UNLOCK(sc);
 1589                 return;
 1590         }
 1591 
 1592         /* Check if the interface is a span port */
 1593         mtx_lock(&bridge_list_mtx);
 1594         LIST_FOREACH(sc, &bridge_list, sc_list) {
 1595                 BRIDGE_LOCK(sc);
 1596                 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
 1597                         if (ifp == bif->bif_ifp) {
 1598                                 bridge_delete_span(sc, bif);
 1599                                 break;
 1600                         }
 1601 
 1602                 BRIDGE_UNLOCK(sc);
 1603         }
 1604         mtx_unlock(&bridge_list_mtx);
 1605 }
 1606 
 1607 /*
 1608  * bridge_init:
 1609  *
 1610  *      Initialize a bridge interface.
 1611  */
 1612 static void
 1613 bridge_init(void *xsc)
 1614 {
 1615         struct bridge_softc *sc = (struct bridge_softc *)xsc;
 1616         struct ifnet *ifp = sc->sc_ifp;
 1617 
 1618         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1619                 return;
 1620 
 1621         BRIDGE_LOCK(sc);
 1622         callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
 1623             bridge_timer, sc);
 1624 
 1625         ifp->if_drv_flags |= IFF_DRV_RUNNING;
 1626         bstp_init(&sc->sc_stp);         /* Initialize Spanning Tree */
 1627 
 1628         BRIDGE_UNLOCK(sc);
 1629 }
 1630 
 1631 /*
 1632  * bridge_stop:
 1633  *
 1634  *      Stop the bridge interface.
 1635  */
 1636 static void
 1637 bridge_stop(struct ifnet *ifp, int disable)
 1638 {
 1639         struct bridge_softc *sc = ifp->if_softc;
 1640 
 1641         BRIDGE_LOCK_ASSERT(sc);
 1642 
 1643         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
 1644                 return;
 1645 
 1646         callout_stop(&sc->sc_brcallout);
 1647         bstp_stop(&sc->sc_stp);
 1648 
 1649         bridge_rtflush(sc, IFBF_FLUSHDYN);
 1650 
 1651         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 1652 }
 1653 
 1654 /*
 1655  * bridge_enqueue:
 1656  *
 1657  *      Enqueue a packet on a bridge member interface.
 1658  *
 1659  */
 1660 static void
 1661 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
 1662 {
 1663         int len, err = 0;
 1664         short mflags;
 1665         struct mbuf *m0;
 1666 
 1667         len = m->m_pkthdr.len;
 1668         mflags = m->m_flags;
 1669 
 1670         /* We may be sending a fragment so traverse the mbuf */
 1671         for (; m; m = m0) {
 1672                 m0 = m->m_nextpkt;
 1673                 m->m_nextpkt = NULL;
 1674                 
 1675                 if (err == 0)
 1676                         IFQ_ENQUEUE(&dst_ifp->if_snd, m, err);
 1677         }
 1678 
 1679         if (err == 0) {
 1680 
 1681                 sc->sc_ifp->if_opackets++;
 1682                 sc->sc_ifp->if_obytes += len;
 1683 
 1684                 dst_ifp->if_obytes += len;
 1685 
 1686                 if (mflags & M_MCAST) {
 1687                         sc->sc_ifp->if_omcasts++;
 1688                         dst_ifp->if_omcasts++;
 1689                 }
 1690         }
 1691 
 1692         if ((dst_ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
 1693                 (*dst_ifp->if_start)(dst_ifp);
 1694 }
 1695 
 1696 /*
 1697  * bridge_dummynet:
 1698  *
 1699  *      Receive a queued packet from dummynet and pass it on to the output
 1700  *      interface.
 1701  *
 1702  *      The mbuf has the Ethernet header already attached.
 1703  */
 1704 static void
 1705 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
 1706 {
 1707         struct bridge_softc *sc;
 1708 
 1709         sc = ifp->if_bridge;
 1710 
 1711         /*
 1712          * The packet didnt originate from a member interface. This should only
 1713          * ever happen if a member interface is removed while packets are
 1714          * queued for it.
 1715          */
 1716         if (sc == NULL) {
 1717                 m_freem(m);
 1718                 return;
 1719         }
 1720 
 1721         if (inet_pfil_hook.ph_busy_count >= 0
 1722 #ifdef INET6
 1723             || inet6_pfil_hook.ph_busy_count >= 0
 1724 #endif
 1725             ) {
 1726                 if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
 1727                         return;
 1728                 if (m == NULL)
 1729                         return;
 1730         }
 1731 
 1732         bridge_enqueue(sc, ifp, m);
 1733 }
 1734 
 1735 /*
 1736  * bridge_output:
 1737  *
 1738  *      Send output from a bridge member interface.  This
 1739  *      performs the bridging function for locally originated
 1740  *      packets.
 1741  *
 1742  *      The mbuf has the Ethernet header already attached.  We must
 1743  *      enqueue or free the mbuf before returning.
 1744  */
 1745 static int
 1746 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
 1747     struct rtentry *rt)
 1748 {
 1749         struct ether_header *eh;
 1750         struct ifnet *dst_if;
 1751         struct bridge_softc *sc;
 1752 
 1753         if (m->m_len < ETHER_HDR_LEN) {
 1754                 m = m_pullup(m, ETHER_HDR_LEN);
 1755                 if (m == NULL)
 1756                         return (0);
 1757         }
 1758 
 1759         eh = mtod(m, struct ether_header *);
 1760         sc = ifp->if_bridge;
 1761 
 1762         BRIDGE_LOCK(sc);
 1763 
 1764         /*
 1765          * If bridge is down, but the original output interface is up,
 1766          * go ahead and send out that interface.  Otherwise, the packet
 1767          * is dropped below.
 1768          */
 1769         if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
 1770                 dst_if = ifp;
 1771                 goto sendunicast;
 1772         }
 1773 
 1774         /*
 1775          * If the packet is a multicast, or we don't know a better way to
 1776          * get there, send to all interfaces.
 1777          */
 1778         if (ETHER_IS_MULTICAST(eh->ether_dhost))
 1779                 dst_if = NULL;
 1780         else
 1781                 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
 1782         if (dst_if == NULL) {
 1783                 struct bridge_iflist *bif;
 1784                 struct mbuf *mc;
 1785                 int error = 0, used = 0;
 1786 
 1787                 bridge_span(sc, m);
 1788 
 1789                 BRIDGE_LOCK2REF(sc, error);
 1790                 if (error) {
 1791                         m_freem(m);
 1792                         return (0);
 1793                 }
 1794 
 1795                 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
 1796                         dst_if = bif->bif_ifp;
 1797 
 1798                         if (dst_if->if_type == IFT_GIF)
 1799                                 continue;
 1800                         if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
 1801                                 continue;
 1802 
 1803                         /*
 1804                          * If this is not the original output interface,
 1805                          * and the interface is participating in spanning
 1806                          * tree, make sure the port is in a state that
 1807                          * allows forwarding.
 1808                          */
 1809                         if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) &&
 1810                             bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
 1811                                 continue;
 1812 
 1813                         if (LIST_NEXT(bif, bif_next) == NULL) {
 1814                                 used = 1;
 1815                                 mc = m;
 1816                         } else {
 1817                                 mc = m_copypacket(m, M_DONTWAIT);
 1818                                 if (mc == NULL) {
 1819                                         sc->sc_ifp->if_oerrors++;
 1820                                         continue;
 1821                                 }
 1822                         }
 1823 
 1824                         bridge_enqueue(sc, dst_if, mc);
 1825                 }
 1826                 if (used == 0)
 1827                         m_freem(m);
 1828                 BRIDGE_UNREF(sc);
 1829                 return (0);
 1830         }
 1831 
 1832 sendunicast:
 1833         /*
 1834          * XXX Spanning tree consideration here?
 1835          */
 1836 
 1837         bridge_span(sc, m);
 1838         if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
 1839                 m_freem(m);
 1840                 BRIDGE_UNLOCK(sc);
 1841                 return (0);
 1842         }
 1843 
 1844         BRIDGE_UNLOCK(sc);
 1845         bridge_enqueue(sc, dst_if, m);
 1846         return (0);
 1847 }
 1848 
 1849 /*
 1850  * bridge_start:
 1851  *
 1852  *      Start output on a bridge.
 1853  *
 1854  */
 1855 static void
 1856 bridge_start(struct ifnet *ifp)
 1857 {
 1858         struct bridge_softc *sc;
 1859         struct mbuf *m;
 1860         struct ether_header *eh;
 1861         struct ifnet *dst_if;
 1862 
 1863         sc = ifp->if_softc;
 1864 
 1865         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 1866         for (;;) {
 1867                 IFQ_DEQUEUE(&ifp->if_snd, m);
 1868                 if (m == 0)
 1869                         break;
 1870                 ETHER_BPF_MTAP(ifp, m);
 1871 
 1872                 eh = mtod(m, struct ether_header *);
 1873                 dst_if = NULL;
 1874 
 1875                 BRIDGE_LOCK(sc);
 1876                 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
 1877                         dst_if = bridge_rtlookup(sc, eh->ether_dhost);
 1878                 }
 1879 
 1880                 if (dst_if == NULL)
 1881                         bridge_broadcast(sc, ifp, m, 0);
 1882                 else {
 1883                         BRIDGE_UNLOCK(sc);
 1884                         bridge_enqueue(sc, dst_if, m);
 1885                 }
 1886         }
 1887         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 1888 }
 1889 
 1890 /*
 1891  * bridge_forward:
 1892  *
 1893  *      The forwarding function of the bridge.
 1894  *
 1895  *      NOTE: Releases the lock on return.
 1896  */
 1897 static void
 1898 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif,
 1899     struct mbuf *m)
 1900 {
 1901         struct bridge_iflist *dbif;
 1902         struct ifnet *src_if, *dst_if, *ifp;
 1903         struct ether_header *eh;
 1904         uint8_t *dst;
 1905 
 1906         src_if = m->m_pkthdr.rcvif;
 1907         ifp = sc->sc_ifp;
 1908 
 1909         ifp->if_ipackets++;
 1910         ifp->if_ibytes += m->m_pkthdr.len;
 1911 
 1912         if ((sbif->bif_flags & IFBIF_STP) &&
 1913             sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
 1914                 goto drop;
 1915 
 1916         eh = mtod(m, struct ether_header *);
 1917         dst = eh->ether_dhost;
 1918 
 1919         /*
 1920          * If the interface is learning, and the source
 1921          * address is valid and not multicast, record
 1922          * the address.
 1923          */
 1924         if ((sbif->bif_flags & IFBIF_LEARNING) != 0 &&
 1925             ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
 1926             (eh->ether_shost[0] == 0 &&
 1927              eh->ether_shost[1] == 0 &&
 1928              eh->ether_shost[2] == 0 &&
 1929              eh->ether_shost[3] == 0 &&
 1930              eh->ether_shost[4] == 0 &&
 1931              eh->ether_shost[5] == 0) == 0) {
 1932                 (void) bridge_rtupdate(sc, eh->ether_shost,
 1933                     sbif, 0, IFBAF_DYNAMIC);
 1934         }
 1935 
 1936         if ((sbif->bif_flags & IFBIF_STP) != 0 &&
 1937             sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING)
 1938                 goto drop;
 1939 
 1940         /*
 1941          * At this point, the port either doesn't participate
 1942          * in spanning tree or it is in the forwarding state.
 1943          */
 1944 
 1945         /*
 1946          * If the packet is unicast, destined for someone on
 1947          * "this" side of the bridge, drop it.
 1948          */
 1949         if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
 1950                 dst_if = bridge_rtlookup(sc, dst);
 1951                 if (src_if == dst_if)
 1952                         goto drop;
 1953         } else {
 1954                 /*
 1955                  * Check if its a reserved multicast address, any address
 1956                  * listed in 802.1D section 7.12.6 may not be forwarded by the
 1957                  * bridge.
 1958                  * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
 1959                  */
 1960                 if (dst[0] == 0x01 && dst[1] == 0x80 &&
 1961                     dst[2] == 0xc2 && dst[3] == 0x00 &&
 1962                     dst[4] == 0x00 && dst[5] <= 0x0f)
 1963                         goto drop;
 1964 
 1965                 /* ...forward it to all interfaces. */
 1966                 ifp->if_imcasts++;
 1967                 dst_if = NULL;
 1968         }
 1969 
 1970         /*
 1971          * If we have a destination interface which is a member of our bridge,
 1972          * OR this is a unicast packet, push it through the bpf(4) machinery.
 1973          * For broadcast or multicast packets, don't bother because it will
 1974          * be reinjected into ether_input. We do this before we pass the packets
 1975          * through the pfil(9) framework, as it is possible that pfil(9) will
 1976          * drop the packet, or possibly modify it, making it difficult to debug
 1977          * firewall issues on the bridge.
 1978          */
 1979         if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0)
 1980                 ETHER_BPF_MTAP(ifp, m);
 1981 
 1982         /* run the packet filter */
 1983         if (inet_pfil_hook.ph_busy_count >= 0
 1984 #ifdef INET6
 1985             || inet6_pfil_hook.ph_busy_count >= 0
 1986 #endif
 1987             ) {
 1988                 BRIDGE_UNLOCK(sc);
 1989                 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
 1990                         return;
 1991                 if (m == NULL)
 1992                         return;
 1993                 BRIDGE_LOCK(sc);
 1994         }
 1995 
 1996         if (dst_if == NULL) {
 1997                 bridge_broadcast(sc, src_if, m, 1);
 1998                 return;
 1999         }
 2000 
 2001         /*
 2002          * At this point, we're dealing with a unicast frame
 2003          * going to a different interface.
 2004          */
 2005         if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
 2006                 goto drop;
 2007 
 2008         dbif = bridge_lookup_member_if(sc, dst_if);
 2009         if (dbif == NULL)
 2010                 /* Not a member of the bridge (anymore?) */
 2011                 goto drop;
 2012 
 2013         /* Private segments can not talk to each other */
 2014         if (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE)
 2015                 goto drop;
 2016 
 2017         if ((dbif->bif_flags & IFBIF_STP) &&
 2018             dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
 2019                 goto drop;
 2020 
 2021         BRIDGE_UNLOCK(sc);
 2022 
 2023         if (inet_pfil_hook.ph_busy_count >= 0
 2024 #ifdef INET6
 2025             || inet6_pfil_hook.ph_busy_count >= 0
 2026 #endif
 2027             ) {
 2028                 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
 2029                         return;
 2030                 if (m == NULL)
 2031                         return;
 2032         }
 2033 
 2034         bridge_enqueue(sc, dst_if, m);
 2035         return;
 2036 
 2037 drop:
 2038         BRIDGE_UNLOCK(sc);
 2039         m_freem(m);
 2040 }
 2041 
 2042 /*
 2043  * bridge_input:
 2044  *
 2045  *      Receive input from a member interface.  Queue the packet for
 2046  *      bridging if it is not for us.
 2047  */
 2048 static struct mbuf *
 2049 bridge_input(struct ifnet *ifp, struct mbuf *m)
 2050 {
 2051         struct bridge_softc *sc = ifp->if_bridge;
 2052         struct bridge_iflist *bif, *bif2;
 2053         struct ifnet *bifp;
 2054         struct ether_header *eh;
 2055         struct mbuf *mc, *mc2;
 2056 
 2057         if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
 2058                 return (m);
 2059 
 2060         bifp = sc->sc_ifp;
 2061 
 2062         /*
 2063          * Implement support for bridge monitoring. If this flag has been
 2064          * set on this interface, discard the packet once we push it through
 2065          * the bpf(4) machinery, but before we do, increment the byte and
 2066          * packet counters associated with this interface.
 2067          */
 2068         if ((bifp->if_flags & IFF_MONITOR) != 0) {
 2069                 m->m_pkthdr.rcvif  = bifp;
 2070                 ETHER_BPF_MTAP(bifp, m);
 2071                 bifp->if_ipackets++;
 2072                 bifp->if_ibytes += m->m_pkthdr.len;
 2073                 m_freem(m);
 2074                 return (NULL);
 2075         }
 2076         BRIDGE_LOCK(sc);
 2077         bif = bridge_lookup_member_if(sc, ifp);
 2078         if (bif == NULL) {
 2079                 BRIDGE_UNLOCK(sc);
 2080                 return (m);
 2081         }
 2082 
 2083         eh = mtod(m, struct ether_header *);
 2084 
 2085         bridge_span(sc, m);
 2086 
 2087         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
 2088                 /* Tap off 802.1D packets; they do not get forwarded. */
 2089                 if (memcmp(eh->ether_dhost, bstp_etheraddr,
 2090                     ETHER_ADDR_LEN) == 0) {
 2091                         m = bstp_input(&bif->bif_stp, ifp, m);
 2092                         if (m == NULL) {
 2093                                 BRIDGE_UNLOCK(sc);
 2094                                 return (NULL);
 2095                         }
 2096                 }
 2097 
 2098                 if ((bif->bif_flags & IFBIF_STP) &&
 2099                     bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
 2100                         BRIDGE_UNLOCK(sc);
 2101                         return (m);
 2102                 }
 2103 
 2104                 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
 2105                     sizeof(etherbroadcastaddr)) == 0)
 2106                         m->m_flags |= M_BCAST;
 2107                 else
 2108                         m->m_flags |= M_MCAST;
 2109 
 2110                 /*
 2111                  * Make a deep copy of the packet and enqueue the copy
 2112                  * for bridge processing; return the original packet for
 2113                  * local processing.
 2114                  */
 2115                 mc = m_dup(m, M_DONTWAIT);
 2116                 if (mc == NULL) {
 2117                         BRIDGE_UNLOCK(sc);
 2118                         return (m);
 2119                 }
 2120 
 2121                 /* Perform the bridge forwarding function with the copy. */
 2122                 bridge_forward(sc, bif, mc);
 2123 
 2124                 /*
 2125                  * Reinject the mbuf as arriving on the bridge so we have a
 2126                  * chance at claiming multicast packets. We can not loop back
 2127                  * here from ether_input as a bridge is never a member of a
 2128                  * bridge.
 2129                  */
 2130                 KASSERT(bifp->if_bridge == NULL,
 2131                     ("loop created in bridge_input"));
 2132                 mc2 = m_dup(m, M_DONTWAIT);
 2133                 if (mc2 != NULL) {
 2134                         /* Keep the layer3 header aligned */
 2135                         int i = min(mc2->m_pkthdr.len, max_protohdr);
 2136                         mc2 = m_copyup(mc2, i, ETHER_ALIGN);
 2137                 }
 2138                 if (mc2 != NULL) {
 2139                         mc2->m_pkthdr.rcvif = bifp;
 2140                         (*bifp->if_input)(bifp, mc2);
 2141                 }
 2142 
 2143                 /* Return the original packet for local processing. */
 2144                 return (m);
 2145         }
 2146 
 2147         if ((bif->bif_flags & IFBIF_STP) &&
 2148             bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
 2149                 BRIDGE_UNLOCK(sc);
 2150                 return (m);
 2151         }
 2152 
 2153 #ifdef DEV_CARP
 2154 #   define OR_CARP_CHECK_WE_ARE_DST(iface) \
 2155         || ((iface)->if_carp \
 2156             && carp_forus((iface)->if_carp, eh->ether_dhost))
 2157 #   define OR_CARP_CHECK_WE_ARE_SRC(iface) \
 2158         || ((iface)->if_carp \
 2159             && carp_forus((iface)->if_carp, eh->ether_shost))
 2160 #else
 2161 #   define OR_CARP_CHECK_WE_ARE_DST(iface)
 2162 #   define OR_CARP_CHECK_WE_ARE_SRC(iface)
 2163 #endif
 2164 
 2165 #ifdef INET6
 2166 #   define OR_PFIL_HOOKED_INET6 \
 2167         || inet6_pfil_hook.ph_busy_count >= 0
 2168 #else
 2169 #   define OR_PFIL_HOOKED_INET6
 2170 #endif
 2171 
 2172 #define GRAB_OUR_PACKETS(iface) \
 2173         if ((iface)->if_type == IFT_GIF) \
 2174                 continue; \
 2175         /* It is destined for us. */ \
 2176         if (memcmp(IF_LLADDR((iface)), eh->ether_dhost,  ETHER_ADDR_LEN) == 0 \
 2177             OR_CARP_CHECK_WE_ARE_DST((iface))                           \
 2178             ) {                                                         \
 2179                 if ((iface)->if_type == IFT_BRIDGE) {                   \
 2180                         ETHER_BPF_MTAP(iface, m);                       \
 2181                         iface->if_ipackets++;                           \
 2182                         /* Filter on the physical interface. */         \
 2183                         if (pfil_local_phys &&                          \
 2184                             (inet_pfil_hook.ph_busy_count >= 0          \
 2185                              OR_PFIL_HOOKED_INET6)) {                   \
 2186                                 if (bridge_pfil(&m, NULL, ifp,          \
 2187                                     PFIL_IN) != 0 || m == NULL) {       \
 2188                                         BRIDGE_UNLOCK(sc);              \
 2189                                         return (NULL);                  \
 2190                                 }                                       \
 2191                         }                                               \
 2192                 }                                                       \
 2193                 if (bif->bif_flags & IFBIF_LEARNING)                    \
 2194                         (void) bridge_rtupdate(sc,                      \
 2195                             eh->ether_shost, bif, 0, IFBAF_DYNAMIC);    \
 2196                 m->m_pkthdr.rcvif = iface;                              \
 2197                 BRIDGE_UNLOCK(sc);                                      \
 2198                 return (m);                                             \
 2199         }                                                               \
 2200                                                                         \
 2201         /* We just received a packet that we sent out. */               \
 2202         if (memcmp(IF_LLADDR((iface)), eh->ether_shost, ETHER_ADDR_LEN) == 0 \
 2203             OR_CARP_CHECK_WE_ARE_SRC((iface))                   \
 2204             ) {                                                         \
 2205                 BRIDGE_UNLOCK(sc);                                      \
 2206                 m_freem(m);                                             \
 2207                 return (NULL);                                          \
 2208         }
 2209 
 2210         /*
 2211          * Unicast.  Make sure it's not for the bridge.
 2212          */
 2213         do { GRAB_OUR_PACKETS(bifp) } while (0);
 2214 
 2215         /*
 2216          * Give a chance for ifp at first priority. This will help when the
 2217          * packet comes through the interface like VLAN's with the same MACs
 2218          * on several interfaces from the same bridge. This also will save
 2219          * some CPU cycles in case the destination interface and the input
 2220          * interface (eq ifp) are the same.
 2221          */
 2222         do { GRAB_OUR_PACKETS(ifp) } while (0);
 2223 
 2224         /* Now check the all bridge members. */
 2225         LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) {
 2226                 GRAB_OUR_PACKETS(bif2->bif_ifp)
 2227         }
 2228 
 2229 #undef OR_CARP_CHECK_WE_ARE_DST
 2230 #undef OR_CARP_CHECK_WE_ARE_SRC
 2231 #undef OR_PFIL_HOOKED_INET6
 2232 #undef GRAB_OUR_PACKETS
 2233 
 2234         /* Perform the bridge forwarding function. */
 2235         bridge_forward(sc, bif, m);
 2236 
 2237         return (NULL);
 2238 }
 2239 
 2240 /*
 2241  * bridge_broadcast:
 2242  *
 2243  *      Send a frame to all interfaces that are members of
 2244  *      the bridge, except for the one on which the packet
 2245  *      arrived.
 2246  *
 2247  *      NOTE: Releases the lock on return.
 2248  */
 2249 static void
 2250 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
 2251     struct mbuf *m, int runfilt)
 2252 {
 2253         struct bridge_iflist *dbif, *sbif;
 2254         struct mbuf *mc;
 2255         struct ifnet *dst_if;
 2256         int error = 0, used = 0, i;
 2257 
 2258         sbif = bridge_lookup_member_if(sc, src_if);
 2259 
 2260         BRIDGE_LOCK2REF(sc, error);
 2261         if (error) {
 2262                 m_freem(m);
 2263                 return;
 2264         }
 2265 
 2266         /* Filter on the bridge interface before broadcasting */
 2267         if (runfilt && (inet_pfil_hook.ph_busy_count >= 0
 2268 #ifdef INET6
 2269             || inet6_pfil_hook.ph_busy_count >= 0
 2270 #endif
 2271             )) {
 2272                 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
 2273                         goto out;
 2274                 if (m == NULL)
 2275                         goto out;
 2276         }
 2277 
 2278         LIST_FOREACH(dbif, &sc->sc_iflist, bif_next) {
 2279                 dst_if = dbif->bif_ifp;
 2280                 if (dst_if == src_if)
 2281                         continue;
 2282 
 2283                 /* Private segments can not talk to each other */
 2284                 if (sbif && (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE))
 2285                         continue;
 2286 
 2287                 if ((dbif->bif_flags & IFBIF_STP) &&
 2288                     dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
 2289                         continue;
 2290 
 2291                 if ((dbif->bif_flags & IFBIF_DISCOVER) == 0 &&
 2292                     (m->m_flags & (M_BCAST|M_MCAST)) == 0)
 2293                         continue;
 2294 
 2295                 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
 2296                         continue;
 2297 
 2298                 if (LIST_NEXT(dbif, bif_next) == NULL) {
 2299                         mc = m;
 2300                         used = 1;
 2301                 } else {
 2302                         mc = m_dup(m, M_DONTWAIT);
 2303                         if (mc == NULL) {
 2304                                 sc->sc_ifp->if_oerrors++;
 2305                                 continue;
 2306                         }
 2307                 }
 2308 
 2309                 /*
 2310                  * Filter on the output interface. Pass a NULL bridge interface
 2311                  * pointer so we do not redundantly filter on the bridge for
 2312                  * each interface we broadcast on.
 2313                  */
 2314                 if (runfilt && (inet_pfil_hook.ph_busy_count >= 0
 2315 #ifdef INET6
 2316                     || inet6_pfil_hook.ph_busy_count >= 0
 2317 #endif
 2318                     )) {
 2319                         if (used == 0) {
 2320                                 /* Keep the layer3 header aligned */
 2321                                 i = min(mc->m_pkthdr.len, max_protohdr);
 2322                                 mc = m_copyup(mc, i, ETHER_ALIGN);
 2323                                 if (mc == NULL) {
 2324                                         sc->sc_ifp->if_oerrors++;
 2325                                         continue;
 2326                                 }
 2327                         }
 2328                         if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
 2329                                 continue;
 2330                         if (mc == NULL)
 2331                                 continue;
 2332                 }
 2333 
 2334                 bridge_enqueue(sc, dst_if, mc);
 2335         }
 2336         if (used == 0)
 2337                 m_freem(m);
 2338 
 2339 out:
 2340         BRIDGE_UNREF(sc);
 2341 }
 2342 
 2343 /*
 2344  * bridge_span:
 2345  *
 2346  *      Duplicate a packet out one or more interfaces that are in span mode,
 2347  *      the original mbuf is unmodified.
 2348  */
 2349 static void
 2350 bridge_span(struct bridge_softc *sc, struct mbuf *m)
 2351 {
 2352         struct bridge_iflist *bif;
 2353         struct ifnet *dst_if;
 2354         struct mbuf *mc;
 2355 
 2356         if (LIST_EMPTY(&sc->sc_spanlist))
 2357                 return;
 2358 
 2359         LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
 2360                 dst_if = bif->bif_ifp;
 2361 
 2362                 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
 2363                         continue;
 2364 
 2365                 mc = m_copypacket(m, M_DONTWAIT);
 2366                 if (mc == NULL) {
 2367                         sc->sc_ifp->if_oerrors++;
 2368                         continue;
 2369                 }
 2370 
 2371                 bridge_enqueue(sc, dst_if, mc);
 2372         }
 2373 }
 2374 
 2375 /*
 2376  * bridge_rtupdate:
 2377  *
 2378  *      Add a bridge routing entry.
 2379  */
 2380 static int
 2381 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
 2382     struct bridge_iflist *bif, int setflags, uint8_t flags)
 2383 {
 2384         struct bridge_rtnode *brt;
 2385         struct ifnet *dst_if = bif->bif_ifp;
 2386         int error;
 2387 
 2388         BRIDGE_LOCK_ASSERT(sc);
 2389 
 2390         /*
 2391          * A route for this destination might already exist.  If so,
 2392          * update it, otherwise create a new one.
 2393          */
 2394         if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
 2395                 if (sc->sc_brtcnt >= sc->sc_brtmax) {
 2396                         sc->sc_brtexceeded++;
 2397                         return (ENOSPC);
 2398                 }
 2399 
 2400                 /*
 2401                  * Allocate a new bridge forwarding node, and
 2402                  * initialize the expiration time and Ethernet
 2403                  * address.
 2404                  */
 2405                 brt = uma_zalloc(bridge_rtnode_zone, M_NOWAIT | M_ZERO);
 2406                 if (brt == NULL)
 2407                         return (ENOMEM);
 2408 
 2409                 if (bif->bif_flags & IFBIF_STICKY)
 2410                         brt->brt_flags = IFBAF_STICKY;
 2411                 else
 2412                         brt->brt_flags = IFBAF_DYNAMIC;
 2413 
 2414                 brt->brt_ifp = dst_if;
 2415                 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
 2416 
 2417                 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
 2418                         uma_zfree(bridge_rtnode_zone, brt);
 2419                         return (error);
 2420                 }
 2421         }
 2422 
 2423         if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
 2424                 brt->brt_ifp = dst_if;
 2425         if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
 2426                 brt->brt_expire = time_uptime + sc->sc_brttimeout;
 2427         if (setflags)
 2428                 brt->brt_flags = flags;
 2429 
 2430         return (0);
 2431 }
 2432 
 2433 /*
 2434  * bridge_rtlookup:
 2435  *
 2436  *      Lookup the destination interface for an address.
 2437  */
 2438 static struct ifnet *
 2439 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
 2440 {
 2441         struct bridge_rtnode *brt;
 2442 
 2443         BRIDGE_LOCK_ASSERT(sc);
 2444 
 2445         if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
 2446                 return (NULL);
 2447 
 2448         return (brt->brt_ifp);
 2449 }
 2450 
 2451 /*
 2452  * bridge_rttrim:
 2453  *
 2454  *      Trim the routine table so that we have a number
 2455  *      of routing entries less than or equal to the
 2456  *      maximum number.
 2457  */
 2458 static void
 2459 bridge_rttrim(struct bridge_softc *sc)
 2460 {
 2461         struct bridge_rtnode *brt, *nbrt;
 2462 
 2463         BRIDGE_LOCK_ASSERT(sc);
 2464 
 2465         /* Make sure we actually need to do this. */
 2466         if (sc->sc_brtcnt <= sc->sc_brtmax)
 2467                 return;
 2468 
 2469         /* Force an aging cycle; this might trim enough addresses. */
 2470         bridge_rtage(sc);
 2471         if (sc->sc_brtcnt <= sc->sc_brtmax)
 2472                 return;
 2473 
 2474         LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
 2475                 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
 2476                         bridge_rtnode_destroy(sc, brt);
 2477                         if (sc->sc_brtcnt <= sc->sc_brtmax)
 2478                                 return;
 2479                 }
 2480         }
 2481 }
 2482 
 2483 /*
 2484  * bridge_timer:
 2485  *
 2486  *      Aging timer for the bridge.
 2487  */
 2488 static void
 2489 bridge_timer(void *arg)
 2490 {
 2491         struct bridge_softc *sc = arg;
 2492 
 2493         BRIDGE_LOCK_ASSERT(sc);
 2494 
 2495         bridge_rtage(sc);
 2496 
 2497         if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
 2498                 callout_reset(&sc->sc_brcallout,
 2499                     bridge_rtable_prune_period * hz, bridge_timer, sc);
 2500 }
 2501 
 2502 /*
 2503  * bridge_rtage:
 2504  *
 2505  *      Perform an aging cycle.
 2506  */
 2507 static void
 2508 bridge_rtage(struct bridge_softc *sc)
 2509 {
 2510         struct bridge_rtnode *brt, *nbrt;
 2511 
 2512         BRIDGE_LOCK_ASSERT(sc);
 2513 
 2514         LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
 2515                 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
 2516                         if (time_uptime >= brt->brt_expire)
 2517                                 bridge_rtnode_destroy(sc, brt);
 2518                 }
 2519         }
 2520 }
 2521 
 2522 /*
 2523  * bridge_rtflush:
 2524  *
 2525  *      Remove all dynamic addresses from the bridge.
 2526  */
 2527 static void
 2528 bridge_rtflush(struct bridge_softc *sc, int full)
 2529 {
 2530         struct bridge_rtnode *brt, *nbrt;
 2531 
 2532         BRIDGE_LOCK_ASSERT(sc);
 2533 
 2534         LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
 2535                 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
 2536                         bridge_rtnode_destroy(sc, brt);
 2537         }
 2538 }
 2539 
 2540 /*
 2541  * bridge_rtdaddr:
 2542  *
 2543  *      Remove an address from the table.
 2544  */
 2545 static int
 2546 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
 2547 {
 2548         struct bridge_rtnode *brt;
 2549 
 2550         BRIDGE_LOCK_ASSERT(sc);
 2551 
 2552         if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
 2553                 return (ENOENT);
 2554 
 2555         bridge_rtnode_destroy(sc, brt);
 2556         return (0);
 2557 }
 2558 
 2559 /*
 2560  * bridge_rtdelete:
 2561  *
 2562  *      Delete routes to a speicifc member interface.
 2563  */
 2564 static void
 2565 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
 2566 {
 2567         struct bridge_rtnode *brt, *nbrt;
 2568 
 2569         BRIDGE_LOCK_ASSERT(sc);
 2570 
 2571         LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
 2572                 if (brt->brt_ifp == ifp && (full ||
 2573                             (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
 2574                         bridge_rtnode_destroy(sc, brt);
 2575         }
 2576 }
 2577 
 2578 /*
 2579  * bridge_rtable_init:
 2580  *
 2581  *      Initialize the route table for this bridge.
 2582  */
 2583 static int
 2584 bridge_rtable_init(struct bridge_softc *sc)
 2585 {
 2586         int i;
 2587 
 2588         sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
 2589             M_DEVBUF, M_NOWAIT);
 2590         if (sc->sc_rthash == NULL)
 2591                 return (ENOMEM);
 2592 
 2593         for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
 2594                 LIST_INIT(&sc->sc_rthash[i]);
 2595 
 2596         sc->sc_rthash_key = arc4random();
 2597 
 2598         LIST_INIT(&sc->sc_rtlist);
 2599 
 2600         return (0);
 2601 }
 2602 
 2603 /*
 2604  * bridge_rtable_fini:
 2605  *
 2606  *      Deconstruct the route table for this bridge.
 2607  */
 2608 static void
 2609 bridge_rtable_fini(struct bridge_softc *sc)
 2610 {
 2611 
 2612         free(sc->sc_rthash, M_DEVBUF);
 2613 }
 2614 
 2615 /*
 2616  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
 2617  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
 2618  */
 2619 #define mix(a, b, c)                                                    \
 2620 do {                                                                    \
 2621         a -= b; a -= c; a ^= (c >> 13);                                 \
 2622         b -= c; b -= a; b ^= (a << 8);                                  \
 2623         c -= a; c -= b; c ^= (b >> 13);                                 \
 2624         a -= b; a -= c; a ^= (c >> 12);                                 \
 2625         b -= c; b -= a; b ^= (a << 16);                                 \
 2626         c -= a; c -= b; c ^= (b >> 5);                                  \
 2627         a -= b; a -= c; a ^= (c >> 3);                                  \
 2628         b -= c; b -= a; b ^= (a << 10);                                 \
 2629         c -= a; c -= b; c ^= (b >> 15);                                 \
 2630 } while (/*CONSTCOND*/0)
 2631 
 2632 static __inline uint32_t
 2633 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
 2634 {
 2635         uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
 2636 
 2637         b += addr[5] << 8;
 2638         b += addr[4];
 2639         a += addr[3] << 24;
 2640         a += addr[2] << 16;
 2641         a += addr[1] << 8;
 2642         a += addr[0];
 2643 
 2644         mix(a, b, c);
 2645 
 2646         return (c & BRIDGE_RTHASH_MASK);
 2647 }
 2648 
 2649 #undef mix
 2650 
 2651 static int
 2652 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
 2653 {
 2654         int i, d;
 2655 
 2656         for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
 2657                 d = ((int)a[i]) - ((int)b[i]);
 2658         }
 2659 
 2660         return (d);
 2661 }
 2662 
 2663 /*
 2664  * bridge_rtnode_lookup:
 2665  *
 2666  *      Look up a bridge route node for the specified destination.
 2667  */
 2668 static struct bridge_rtnode *
 2669 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
 2670 {
 2671         struct bridge_rtnode *brt;
 2672         uint32_t hash;
 2673         int dir;
 2674 
 2675         BRIDGE_LOCK_ASSERT(sc);
 2676 
 2677         hash = bridge_rthash(sc, addr);
 2678         LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
 2679                 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
 2680                 if (dir == 0)
 2681                         return (brt);
 2682                 if (dir > 0)
 2683                         return (NULL);
 2684         }
 2685 
 2686         return (NULL);
 2687 }
 2688 
 2689 /*
 2690  * bridge_rtnode_insert:
 2691  *
 2692  *      Insert the specified bridge node into the route table.  We
 2693  *      assume the entry is not already in the table.
 2694  */
 2695 static int
 2696 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
 2697 {
 2698         struct bridge_rtnode *lbrt;
 2699         uint32_t hash;
 2700         int dir;
 2701 
 2702         BRIDGE_LOCK_ASSERT(sc);
 2703 
 2704         hash = bridge_rthash(sc, brt->brt_addr);
 2705 
 2706         lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
 2707         if (lbrt == NULL) {
 2708                 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
 2709                 goto out;
 2710         }
 2711 
 2712         do {
 2713                 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
 2714                 if (dir == 0)
 2715                         return (EEXIST);
 2716                 if (dir > 0) {
 2717                         LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
 2718                         goto out;
 2719                 }
 2720                 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
 2721                         LIST_INSERT_AFTER(lbrt, brt, brt_hash);
 2722                         goto out;
 2723                 }
 2724                 lbrt = LIST_NEXT(lbrt, brt_hash);
 2725         } while (lbrt != NULL);
 2726 
 2727 #ifdef DIAGNOSTIC
 2728         panic("bridge_rtnode_insert: impossible");
 2729 #endif
 2730 
 2731 out:
 2732         LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
 2733         sc->sc_brtcnt++;
 2734 
 2735         return (0);
 2736 }
 2737 
 2738 /*
 2739  * bridge_rtnode_destroy:
 2740  *
 2741  *      Destroy a bridge rtnode.
 2742  */
 2743 static void
 2744 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
 2745 {
 2746         BRIDGE_LOCK_ASSERT(sc);
 2747 
 2748         LIST_REMOVE(brt, brt_hash);
 2749 
 2750         LIST_REMOVE(brt, brt_list);
 2751         sc->sc_brtcnt--;
 2752         uma_zfree(bridge_rtnode_zone, brt);
 2753 }
 2754 
 2755 /*
 2756  * bridge_rtable_expire:
 2757  *
 2758  *      Set the expiry time for all routes on an interface.
 2759  */
 2760 static void
 2761 bridge_rtable_expire(struct ifnet *ifp, int age)
 2762 {
 2763         struct bridge_softc *sc = ifp->if_bridge;
 2764         struct bridge_rtnode *brt;
 2765 
 2766         BRIDGE_LOCK(sc);
 2767 
 2768         /*
 2769          * If the age is zero then flush, otherwise set all the expiry times to
 2770          * age for the interface
 2771          */
 2772         if (age == 0)
 2773                 bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
 2774         else {
 2775                 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
 2776                         /* Cap the expiry time to 'age' */
 2777                         if (brt->brt_ifp == ifp &&
 2778                             brt->brt_expire > time_uptime + age &&
 2779                             (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
 2780                                 brt->brt_expire = time_uptime + age;
 2781                 }
 2782         }
 2783         BRIDGE_UNLOCK(sc);
 2784 }
 2785 
 2786 /*
 2787  * bridge_state_change:
 2788  *
 2789  *      Callback from the bridgestp code when a port changes states.
 2790  */
 2791 static void
 2792 bridge_state_change(struct ifnet *ifp, int state)
 2793 {
 2794         struct bridge_softc *sc = ifp->if_bridge;
 2795         static const char *stpstates[] = {
 2796                 "disabled",
 2797                 "listening",
 2798                 "learning",
 2799                 "forwarding",
 2800                 "blocking",
 2801                 "discarding"
 2802         };
 2803 
 2804         if (log_stp)
 2805                 log(LOG_NOTICE, "%s: state changed to %s on %s\n",
 2806                     sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname);
 2807 }
 2808 
 2809 /*
 2810  * Send bridge packets through pfil if they are one of the types pfil can deal
 2811  * with, or if they are ARP or REVARP.  (pfil will pass ARP and REVARP without
 2812  * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
 2813  * that interface.
 2814  */
 2815 static int
 2816 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
 2817 {
 2818         int snap, error, i, hlen;
 2819         struct ether_header *eh1, eh2;
 2820         struct ip_fw_args args;
 2821         struct ip *ip;
 2822         struct llc llc1;
 2823         u_int16_t ether_type;
 2824 
 2825         snap = 0;
 2826         error = -1;     /* Default error if not error == 0 */
 2827 
 2828 #if 0
 2829         /* we may return with the IP fields swapped, ensure its not shared */
 2830         KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__));
 2831 #endif
 2832 
 2833         if (pfil_bridge == 0 && pfil_member == 0 && pfil_ipfw == 0)
 2834                 return (0); /* filtering is disabled */
 2835 
 2836         i = min((*mp)->m_pkthdr.len, max_protohdr);
 2837         if ((*mp)->m_len < i) {
 2838             *mp = m_pullup(*mp, i);
 2839             if (*mp == NULL) {
 2840                 printf("%s: m_pullup failed\n", __func__);
 2841                 return (-1);
 2842             }
 2843         }
 2844 
 2845         eh1 = mtod(*mp, struct ether_header *);
 2846         ether_type = ntohs(eh1->ether_type);
 2847 
 2848         /*
 2849          * Check for SNAP/LLC.
 2850          */
 2851         if (ether_type < ETHERMTU) {
 2852                 struct llc *llc2 = (struct llc *)(eh1 + 1);
 2853 
 2854                 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
 2855                     llc2->llc_dsap == LLC_SNAP_LSAP &&
 2856                     llc2->llc_ssap == LLC_SNAP_LSAP &&
 2857                     llc2->llc_control == LLC_UI) {
 2858                         ether_type = htons(llc2->llc_un.type_snap.ether_type);
 2859                         snap = 1;
 2860                 }
 2861         }
 2862 
 2863         /*
 2864          * If we're trying to filter bridge traffic, don't look at anything
 2865          * other than IP and ARP traffic.  If the filter doesn't understand
 2866          * IPv6, don't allow IPv6 through the bridge either.  This is lame
 2867          * since if we really wanted, say, an AppleTalk filter, we are hosed,
 2868          * but of course we don't have an AppleTalk filter to begin with.
 2869          * (Note that since pfil doesn't understand ARP it will pass *ALL*
 2870          * ARP traffic.)
 2871          */
 2872         switch (ether_type) {
 2873                 case ETHERTYPE_ARP:
 2874                 case ETHERTYPE_REVARP:
 2875                         if (pfil_ipfw_arp == 0)
 2876                                 return (0); /* Automatically pass */
 2877                         break;
 2878 
 2879                 case ETHERTYPE_IP:
 2880 #ifdef INET6
 2881                 case ETHERTYPE_IPV6:
 2882 #endif /* INET6 */
 2883                         break;
 2884                 default:
 2885                         /*
 2886                          * Check to see if the user wants to pass non-ip
 2887                          * packets, these will not be checked by pfil(9) and
 2888                          * passed unconditionally so the default is to drop.
 2889                          */
 2890                         if (pfil_onlyip)
 2891                                 goto bad;
 2892         }
 2893 
 2894         /* Strip off the Ethernet header and keep a copy. */
 2895         m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
 2896         m_adj(*mp, ETHER_HDR_LEN);
 2897 
 2898         /* Strip off snap header, if present */
 2899         if (snap) {
 2900                 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
 2901                 m_adj(*mp, sizeof(struct llc));
 2902         }
 2903 
 2904         /*
 2905          * Check the IP header for alignment and errors
 2906          */
 2907         if (dir == PFIL_IN) {
 2908                 switch (ether_type) {
 2909                         case ETHERTYPE_IP:
 2910                                 error = bridge_ip_checkbasic(mp);
 2911                                 break;
 2912 #ifdef INET6
 2913                         case ETHERTYPE_IPV6:
 2914                                 error = bridge_ip6_checkbasic(mp);
 2915                                 break;
 2916 #endif /* INET6 */
 2917                         default:
 2918                                 error = 0;
 2919                 }
 2920                 if (error)
 2921                         goto bad;
 2922         }
 2923 
 2924         if (IPFW_LOADED && pfil_ipfw != 0 && dir == PFIL_OUT && ifp != NULL) {
 2925                 error = -1;
 2926                 args.rule = ip_dn_claim_rule(*mp);
 2927                 if (args.rule != NULL && fw_one_pass)
 2928                         goto ipfwpass; /* packet already partially processed */
 2929 
 2930                 args.m = *mp;
 2931                 args.oif = ifp;
 2932                 args.next_hop = NULL;
 2933                 args.eh = &eh2;
 2934                 args.inp = NULL;        /* used by ipfw uid/gid/jail rules */
 2935                 i = ip_fw_chk_ptr(&args);
 2936                 *mp = args.m;
 2937 
 2938                 if (*mp == NULL)
 2939                         return (error);
 2940 
 2941                 if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
 2942 
 2943                         /* put the Ethernet header back on */
 2944                         M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
 2945                         if (*mp == NULL)
 2946                                 return (error);
 2947                         bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
 2948 
 2949                         /*
 2950                          * Pass the pkt to dummynet, which consumes it. The
 2951                          * packet will return to us via bridge_dummynet().
 2952                          */
 2953                         args.oif = ifp;
 2954                         ip_dn_io_ptr(mp, DN_TO_IFB_FWD, &args);
 2955                         return (error);
 2956                 }
 2957 
 2958                 if (i != IP_FW_PASS) /* drop */
 2959                         goto bad;
 2960         }
 2961 
 2962 ipfwpass:
 2963         error = 0;
 2964 
 2965         /*
 2966          * Run the packet through pfil
 2967          */
 2968         switch (ether_type) {
 2969         case ETHERTYPE_IP:
 2970                 /*
 2971                  * before calling the firewall, swap fields the same as
 2972                  * IP does. here we assume the header is contiguous
 2973                  */
 2974                 ip = mtod(*mp, struct ip *);
 2975 
 2976                 ip->ip_len = ntohs(ip->ip_len);
 2977                 ip->ip_off = ntohs(ip->ip_off);
 2978 
 2979                 /*
 2980                  * Run pfil on the member interface and the bridge, both can
 2981                  * be skipped by clearing pfil_member or pfil_bridge.
 2982                  *
 2983                  * Keep the order:
 2984                  *   in_if -> bridge_if -> out_if
 2985                  */
 2986                 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
 2987                         error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
 2988                                         dir, NULL);
 2989 
 2990                 if (*mp == NULL || error != 0) /* filter may consume */
 2991                         break;
 2992 
 2993                 if (pfil_member && ifp != NULL)
 2994                         error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
 2995                                         dir, NULL);
 2996 
 2997                 if (*mp == NULL || error != 0) /* filter may consume */
 2998                         break;
 2999 
 3000                 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
 3001                         error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
 3002                                         dir, NULL);
 3003 
 3004                 if (*mp == NULL || error != 0) /* filter may consume */
 3005                         break;
 3006 
 3007                 /* check if we need to fragment the packet */
 3008                 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
 3009                         i = (*mp)->m_pkthdr.len;
 3010                         if (i > ifp->if_mtu) {
 3011                                 error = bridge_fragment(ifp, *mp, &eh2, snap,
 3012                                             &llc1);
 3013                                 return (error);
 3014                         }
 3015                 }
 3016 
 3017                 /* Recalculate the ip checksum and restore byte ordering */
 3018                 ip = mtod(*mp, struct ip *);
 3019                 hlen = ip->ip_hl << 2;
 3020                 if (hlen < sizeof(struct ip))
 3021                         goto bad;
 3022                 if (hlen > (*mp)->m_len) {
 3023                         if ((*mp = m_pullup(*mp, hlen)) == 0)
 3024                                 goto bad;
 3025                         ip = mtod(*mp, struct ip *);
 3026                         if (ip == NULL)
 3027                                 goto bad;
 3028                 }
 3029                 ip->ip_len = htons(ip->ip_len);
 3030                 ip->ip_off = htons(ip->ip_off);
 3031                 ip->ip_sum = 0;
 3032                 if (hlen == sizeof(struct ip))
 3033                         ip->ip_sum = in_cksum_hdr(ip);
 3034                 else
 3035                         ip->ip_sum = in_cksum(*mp, hlen);
 3036 
 3037                 break;
 3038 #ifdef INET6
 3039         case ETHERTYPE_IPV6:
 3040                 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
 3041                         error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
 3042                                         dir, NULL);
 3043 
 3044                 if (*mp == NULL || error != 0) /* filter may consume */
 3045                         break;
 3046 
 3047                 if (pfil_member && ifp != NULL)
 3048                         error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
 3049                                         dir, NULL);
 3050 
 3051                 if (*mp == NULL || error != 0) /* filter may consume */
 3052                         break;
 3053 
 3054                 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
 3055                         error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
 3056                                         dir, NULL);
 3057                 break;
 3058 #endif
 3059         default:
 3060                 error = 0;
 3061                 break;
 3062         }
 3063 
 3064         if (*mp == NULL)
 3065                 return (error);
 3066         if (error != 0)
 3067                 goto bad;
 3068 
 3069         error = -1;
 3070 
 3071         /*
 3072          * Finally, put everything back the way it was and return
 3073          */
 3074         if (snap) {
 3075                 M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
 3076                 if (*mp == NULL)
 3077                         return (error);
 3078                 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
 3079         }
 3080 
 3081         M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
 3082         if (*mp == NULL)
 3083                 return (error);
 3084         bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
 3085 
 3086         return (0);
 3087 
 3088 bad:
 3089         m_freem(*mp);
 3090         *mp = NULL;
 3091         return (error);
 3092 }
 3093 
 3094 /*
 3095  * Perform basic checks on header size since
 3096  * pfil assumes ip_input has already processed
 3097  * it for it.  Cut-and-pasted from ip_input.c.
 3098  * Given how simple the IPv6 version is,
 3099  * does the IPv4 version really need to be
 3100  * this complicated?
 3101  *
 3102  * XXX Should we update ipstat here, or not?
 3103  * XXX Right now we update ipstat but not
 3104  * XXX csum_counter.
 3105  */
 3106 static int
 3107 bridge_ip_checkbasic(struct mbuf **mp)
 3108 {
 3109         struct mbuf *m = *mp;
 3110         struct ip *ip;
 3111         int len, hlen;
 3112         u_short sum;
 3113 
 3114         if (*mp == NULL)
 3115                 return (-1);
 3116 
 3117         if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
 3118                 if ((m = m_copyup(m, sizeof(struct ip),
 3119                         (max_linkhdr + 3) & ~3)) == NULL) {
 3120                         /* XXXJRT new stat, please */
 3121                         ipstat.ips_toosmall++;
 3122                         goto bad;
 3123                 }
 3124         } else if (__predict_false(m->m_len < sizeof (struct ip))) {
 3125                 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
 3126                         ipstat.ips_toosmall++;
 3127                         goto bad;
 3128                 }
 3129         }
 3130         ip = mtod(m, struct ip *);
 3131         if (ip == NULL) goto bad;
 3132 
 3133         if (ip->ip_v != IPVERSION) {
 3134                 ipstat.ips_badvers++;
 3135                 goto bad;
 3136         }
 3137         hlen = ip->ip_hl << 2;
 3138         if (hlen < sizeof(struct ip)) { /* minimum header length */
 3139                 ipstat.ips_badhlen++;
 3140                 goto bad;
 3141         }
 3142         if (hlen > m->m_len) {
 3143                 if ((m = m_pullup(m, hlen)) == 0) {
 3144                         ipstat.ips_badhlen++;
 3145                         goto bad;
 3146                 }
 3147                 ip = mtod(m, struct ip *);
 3148                 if (ip == NULL) goto bad;
 3149         }
 3150 
 3151         if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
 3152                 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
 3153         } else {
 3154                 if (hlen == sizeof(struct ip)) {
 3155                         sum = in_cksum_hdr(ip);
 3156                 } else {
 3157                         sum = in_cksum(m, hlen);
 3158                 }
 3159         }
 3160         if (sum) {
 3161                 ipstat.ips_badsum++;
 3162                 goto bad;
 3163         }
 3164 
 3165         /* Retrieve the packet length. */
 3166         len = ntohs(ip->ip_len);
 3167 
 3168         /*
 3169          * Check for additional length bogosity
 3170          */
 3171         if (len < hlen) {
 3172                 ipstat.ips_badlen++;
 3173                 goto bad;
 3174         }
 3175 
 3176         /*
 3177          * Check that the amount of data in the buffers
 3178          * is as at least much as the IP header would have us expect.
 3179          * Drop packet if shorter than we expect.
 3180          */
 3181         if (m->m_pkthdr.len < len) {
 3182                 ipstat.ips_tooshort++;
 3183                 goto bad;
 3184         }
 3185 
 3186         /* Checks out, proceed */
 3187         *mp = m;
 3188         return (0);
 3189 
 3190 bad:
 3191         *mp = m;
 3192         return (-1);
 3193 }
 3194 
 3195 #ifdef INET6
 3196 /*
 3197  * Same as above, but for IPv6.
 3198  * Cut-and-pasted from ip6_input.c.
 3199  * XXX Should we update ip6stat, or not?
 3200  */
 3201 static int
 3202 bridge_ip6_checkbasic(struct mbuf **mp)
 3203 {
 3204         struct mbuf *m = *mp;
 3205         struct ip6_hdr *ip6;
 3206 
 3207         /*
 3208          * If the IPv6 header is not aligned, slurp it up into a new
 3209          * mbuf with space for link headers, in the event we forward
 3210          * it.  Otherwise, if it is aligned, make sure the entire base
 3211          * IPv6 header is in the first mbuf of the chain.
 3212          */
 3213         if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
 3214                 struct ifnet *inifp = m->m_pkthdr.rcvif;
 3215                 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
 3216                             (max_linkhdr + 3) & ~3)) == NULL) {
 3217                         /* XXXJRT new stat, please */
 3218                         ip6stat.ip6s_toosmall++;
 3219                         in6_ifstat_inc(inifp, ifs6_in_hdrerr);
 3220                         goto bad;
 3221                 }
 3222         } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
 3223                 struct ifnet *inifp = m->m_pkthdr.rcvif;
 3224                 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
 3225                         ip6stat.ip6s_toosmall++;
 3226                         in6_ifstat_inc(inifp, ifs6_in_hdrerr);
 3227                         goto bad;
 3228                 }
 3229         }
 3230 
 3231         ip6 = mtod(m, struct ip6_hdr *);
 3232 
 3233         if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
 3234                 ip6stat.ip6s_badvers++;
 3235                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
 3236                 goto bad;
 3237         }
 3238 
 3239         /* Checks out, proceed */
 3240         *mp = m;
 3241         return (0);
 3242 
 3243 bad:
 3244         *mp = m;
 3245         return (-1);
 3246 }
 3247 #endif /* INET6 */
 3248 
 3249 /*
 3250  * bridge_fragment:
 3251  *
 3252  *      Return a fragmented mbuf chain.
 3253  */
 3254 static int
 3255 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
 3256     int snap, struct llc *llc)
 3257 {
 3258         struct mbuf *m0;
 3259         struct ip *ip;
 3260         int error = -1;
 3261 
 3262         if (m->m_len < sizeof(struct ip) &&
 3263             (m = m_pullup(m, sizeof(struct ip))) == NULL)
 3264                 goto out;
 3265         ip = mtod(m, struct ip *);
 3266 
 3267         error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
 3268                     CSUM_DELAY_IP);
 3269         if (error)
 3270                 goto out;
 3271 
 3272         /* walk the chain and re-add the Ethernet header */
 3273         for (m0 = m; m0; m0 = m0->m_nextpkt) {
 3274                 if (error == 0) {
 3275                         if (snap) {
 3276                                 M_PREPEND(m0, sizeof(struct llc), M_DONTWAIT);
 3277                                 if (m0 == NULL) {
 3278                                         error = ENOBUFS;
 3279                                         continue;
 3280                                 }
 3281                                 bcopy(llc, mtod(m0, caddr_t),
 3282                                     sizeof(struct llc));
 3283                         }
 3284                         M_PREPEND(m0, ETHER_HDR_LEN, M_DONTWAIT);
 3285                         if (m0 == NULL) {
 3286                                 error = ENOBUFS;
 3287                                 continue;
 3288                         }
 3289                         bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
 3290                 } else 
 3291                         m_freem(m);
 3292         }
 3293 
 3294         if (error == 0)
 3295                 ipstat.ips_fragmented++;
 3296 
 3297         return (error);
 3298 
 3299 out:
 3300         if (m != NULL)
 3301                 m_freem(m);
 3302         return (error);
 3303 }

Cache object: 9c53cad19a9bbc5880e1e3b33d6e6760


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