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

Cache object: 455c0d2bed208859f4cf022e7bed8d63


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