Home | History | Annotate | Line # | Download | only in net
if_bridge.c revision 1.148.2.5
      1 /*	$NetBSD: if_bridge.c,v 1.148.2.5 2018/09/30 01:45:56 pgoyette 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 (at) 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  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by Jason L. Wright
     53  * 4. The name of the author may not be used to endorse or promote products
     54  *    derived from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     57  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     58  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     59  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     60  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     61  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     62  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     64  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     65  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  *
     68  * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
     69  */
     70 
     71 /*
     72  * Network interface bridge support.
     73  *
     74  * TODO:
     75  *
     76  *	- Currently only supports Ethernet-like interfaces (Ethernet,
     77  *	  802.11, VLANs on Ethernet, etc.)  Figure out a nice way
     78  *	  to bridge other types of interfaces (FDDI-FDDI, and maybe
     79  *	  consider heterogenous bridges).
     80  */
     81 
     82 #include <sys/cdefs.h>
     83 __KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.148.2.5 2018/09/30 01:45:56 pgoyette Exp $");
     84 
     85 #ifdef _KERNEL_OPT
     86 #include "opt_bridge_ipf.h"
     87 #include "opt_inet.h"
     88 #include "opt_net_mpsafe.h"
     89 #endif /* _KERNEL_OPT */
     90 
     91 #include <sys/param.h>
     92 #include <sys/kernel.h>
     93 #include <sys/mbuf.h>
     94 #include <sys/queue.h>
     95 #include <sys/socket.h>
     96 #include <sys/socketvar.h> /* for softnet_lock */
     97 #include <sys/sockio.h>
     98 #include <sys/systm.h>
     99 #include <sys/proc.h>
    100 #include <sys/pool.h>
    101 #include <sys/kauth.h>
    102 #include <sys/cpu.h>
    103 #include <sys/cprng.h>
    104 #include <sys/mutex.h>
    105 #include <sys/kmem.h>
    106 
    107 #include <net/bpf.h>
    108 #include <net/if.h>
    109 #include <net/if_dl.h>
    110 #include <net/if_types.h>
    111 #include <net/if_llc.h>
    112 
    113 #include <net/if_ether.h>
    114 #include <net/if_bridgevar.h>
    115 
    116 #if defined(BRIDGE_IPF)
    117 /* Used for bridge_ip[6]_checkbasic */
    118 #include <netinet/in.h>
    119 #include <netinet/in_systm.h>
    120 #include <netinet/ip.h>
    121 #include <netinet/ip_var.h>
    122 #include <netinet/ip_private.h>		/* XXX */
    123 
    124 #include <netinet/ip6.h>
    125 #include <netinet6/in6_var.h>
    126 #include <netinet6/ip6_var.h>
    127 #include <netinet6/ip6_private.h>	/* XXX */
    128 #endif /* BRIDGE_IPF */
    129 
    130 /*
    131  * Size of the route hash table.  Must be a power of two.
    132  */
    133 #ifndef BRIDGE_RTHASH_SIZE
    134 #define	BRIDGE_RTHASH_SIZE		1024
    135 #endif
    136 
    137 #define	BRIDGE_RTHASH_MASK		(BRIDGE_RTHASH_SIZE - 1)
    138 
    139 #include "carp.h"
    140 #if NCARP > 0
    141 #include <netinet/in.h>
    142 #include <netinet/in_var.h>
    143 #include <netinet/ip_carp.h>
    144 #endif
    145 
    146 #include "ioconf.h"
    147 
    148 __CTASSERT(sizeof(struct ifbifconf) == sizeof(struct ifbaconf));
    149 __CTASSERT(offsetof(struct ifbifconf, ifbic_len) == offsetof(struct ifbaconf, ifbac_len));
    150 __CTASSERT(offsetof(struct ifbifconf, ifbic_buf) == offsetof(struct ifbaconf, ifbac_buf));
    151 
    152 /*
    153  * Maximum number of addresses to cache.
    154  */
    155 #ifndef BRIDGE_RTABLE_MAX
    156 #define	BRIDGE_RTABLE_MAX		100
    157 #endif
    158 
    159 /*
    160  * Spanning tree defaults.
    161  */
    162 #define	BSTP_DEFAULT_MAX_AGE		(20 * 256)
    163 #define	BSTP_DEFAULT_HELLO_TIME		(2 * 256)
    164 #define	BSTP_DEFAULT_FORWARD_DELAY	(15 * 256)
    165 #define	BSTP_DEFAULT_HOLD_TIME		(1 * 256)
    166 #define	BSTP_DEFAULT_BRIDGE_PRIORITY	0x8000
    167 #define	BSTP_DEFAULT_PORT_PRIORITY	0x80
    168 #define	BSTP_DEFAULT_PATH_COST		55
    169 
    170 /*
    171  * Timeout (in seconds) for entries learned dynamically.
    172  */
    173 #ifndef BRIDGE_RTABLE_TIMEOUT
    174 #define	BRIDGE_RTABLE_TIMEOUT		(20 * 60)	/* same as ARP */
    175 #endif
    176 
    177 /*
    178  * Number of seconds between walks of the route list.
    179  */
    180 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
    181 #define	BRIDGE_RTABLE_PRUNE_PERIOD	(5 * 60)
    182 #endif
    183 
    184 #define BRIDGE_RT_LOCK(_sc)	mutex_enter((_sc)->sc_rtlist_lock)
    185 #define BRIDGE_RT_UNLOCK(_sc)	mutex_exit((_sc)->sc_rtlist_lock)
    186 #define BRIDGE_RT_LOCKED(_sc)	mutex_owned((_sc)->sc_rtlist_lock)
    187 
    188 #define BRIDGE_RT_PSZ_PERFORM(_sc) \
    189 				pserialize_perform((_sc)->sc_rtlist_psz)
    190 
    191 #define BRIDGE_RT_RENTER(__s)	do { __s = pserialize_read_enter(); } while (0)
    192 #define BRIDGE_RT_REXIT(__s)	do { pserialize_read_exit(__s); } while (0)
    193 
    194 #define BRIDGE_RTLIST_READER_FOREACH(_brt, _sc)			\
    195 	PSLIST_READER_FOREACH((_brt), &((_sc)->sc_rtlist),		\
    196 	    struct bridge_rtnode, brt_list)
    197 #define BRIDGE_RTLIST_WRITER_FOREACH(_brt, _sc)			\
    198 	PSLIST_WRITER_FOREACH((_brt), &((_sc)->sc_rtlist),		\
    199 	    struct bridge_rtnode, brt_list)
    200 #define BRIDGE_RTLIST_WRITER_INSERT_HEAD(_sc, _brt)			\
    201 	PSLIST_WRITER_INSERT_HEAD(&(_sc)->sc_rtlist, brt, brt_list)
    202 #define BRIDGE_RTLIST_WRITER_REMOVE(_brt)				\
    203 	PSLIST_WRITER_REMOVE((_brt), brt_list)
    204 
    205 #define BRIDGE_RTHASH_READER_FOREACH(_brt, _sc, _hash)			\
    206 	PSLIST_READER_FOREACH((_brt), &(_sc)->sc_rthash[(_hash)],	\
    207 	    struct bridge_rtnode, brt_hash)
    208 #define BRIDGE_RTHASH_WRITER_FOREACH(_brt, _sc, _hash)			\
    209 	PSLIST_WRITER_FOREACH((_brt), &(_sc)->sc_rthash[(_hash)],	\
    210 	    struct bridge_rtnode, brt_hash)
    211 #define BRIDGE_RTHASH_WRITER_INSERT_HEAD(_sc, _hash, _brt)		\
    212 	PSLIST_WRITER_INSERT_HEAD(&(_sc)->sc_rthash[(_hash)], brt, brt_hash)
    213 #define BRIDGE_RTHASH_WRITER_INSERT_AFTER(_brt, _new)			\
    214 	PSLIST_WRITER_INSERT_AFTER((_brt), (_new), brt_hash)
    215 #define BRIDGE_RTHASH_WRITER_REMOVE(_brt)				\
    216 	PSLIST_WRITER_REMOVE((_brt), brt_hash)
    217 
    218 #ifdef NET_MPSAFE
    219 #define DECLARE_LOCK_VARIABLE
    220 #define ACQUIRE_GLOBAL_LOCKS()	do { } while (0)
    221 #define RELEASE_GLOBAL_LOCKS()	do { } while (0)
    222 #else
    223 #define DECLARE_LOCK_VARIABLE	int __s
    224 #define ACQUIRE_GLOBAL_LOCKS()	do {					\
    225 					KERNEL_LOCK(1, NULL);		\
    226 					mutex_enter(softnet_lock);	\
    227 					__s = splsoftnet();		\
    228 				} while (0)
    229 #define RELEASE_GLOBAL_LOCKS()	do {					\
    230 					splx(__s);			\
    231 					mutex_exit(softnet_lock);	\
    232 					KERNEL_UNLOCK_ONE(NULL);	\
    233 				} while (0)
    234 #endif
    235 
    236 struct psref_class *bridge_psref_class __read_mostly;
    237 
    238 int	bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
    239 
    240 static struct pool bridge_rtnode_pool;
    241 
    242 static int	bridge_clone_create(struct if_clone *, int);
    243 static int	bridge_clone_destroy(struct ifnet *);
    244 
    245 static int	bridge_ioctl(struct ifnet *, u_long, void *);
    246 static int	bridge_init(struct ifnet *);
    247 static void	bridge_stop(struct ifnet *, int);
    248 static void	bridge_start(struct ifnet *);
    249 
    250 static void	bridge_input(struct ifnet *, struct mbuf *);
    251 static void	bridge_forward(struct bridge_softc *, struct mbuf *);
    252 
    253 static void	bridge_timer(void *);
    254 
    255 static void	bridge_broadcast(struct bridge_softc *, struct ifnet *,
    256 				 struct mbuf *);
    257 
    258 static int	bridge_rtupdate(struct bridge_softc *, const uint8_t *,
    259 				struct ifnet *, int, uint8_t);
    260 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
    261 static void	bridge_rttrim(struct bridge_softc *);
    262 static void	bridge_rtage(struct bridge_softc *);
    263 static void	bridge_rtage_work(struct work *, void *);
    264 static void	bridge_rtflush(struct bridge_softc *, int);
    265 static int	bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
    266 static void	bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp);
    267 
    268 static void	bridge_rtable_init(struct bridge_softc *);
    269 static void	bridge_rtable_fini(struct bridge_softc *);
    270 
    271 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
    272 						  const uint8_t *);
    273 static int	bridge_rtnode_insert(struct bridge_softc *,
    274 				     struct bridge_rtnode *);
    275 static void	bridge_rtnode_remove(struct bridge_softc *,
    276 				     struct bridge_rtnode *);
    277 static void	bridge_rtnode_destroy(struct bridge_rtnode *);
    278 
    279 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
    280 						  const char *name,
    281 						  struct psref *);
    282 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
    283 						     struct ifnet *ifp,
    284 						     struct psref *);
    285 static void	bridge_release_member(struct bridge_softc *, struct bridge_iflist *,
    286                                       struct psref *);
    287 static void	bridge_delete_member(struct bridge_softc *,
    288 				     struct bridge_iflist *);
    289 static void	bridge_acquire_member(struct bridge_softc *sc,
    290                                       struct bridge_iflist *,
    291                                       struct psref *);
    292 
    293 static int	bridge_ioctl_add(struct bridge_softc *, void *);
    294 static int	bridge_ioctl_del(struct bridge_softc *, void *);
    295 static int	bridge_ioctl_gifflags(struct bridge_softc *, void *);
    296 static int	bridge_ioctl_sifflags(struct bridge_softc *, void *);
    297 static int	bridge_ioctl_scache(struct bridge_softc *, void *);
    298 static int	bridge_ioctl_gcache(struct bridge_softc *, void *);
    299 static int	bridge_ioctl_gifs(struct bridge_softc *, void *);
    300 static int	bridge_ioctl_rts(struct bridge_softc *, void *);
    301 static int	bridge_ioctl_saddr(struct bridge_softc *, void *);
    302 static int	bridge_ioctl_sto(struct bridge_softc *, void *);
    303 static int	bridge_ioctl_gto(struct bridge_softc *, void *);
    304 static int	bridge_ioctl_daddr(struct bridge_softc *, void *);
    305 static int	bridge_ioctl_flush(struct bridge_softc *, void *);
    306 static int	bridge_ioctl_gpri(struct bridge_softc *, void *);
    307 static int	bridge_ioctl_spri(struct bridge_softc *, void *);
    308 static int	bridge_ioctl_ght(struct bridge_softc *, void *);
    309 static int	bridge_ioctl_sht(struct bridge_softc *, void *);
    310 static int	bridge_ioctl_gfd(struct bridge_softc *, void *);
    311 static int	bridge_ioctl_sfd(struct bridge_softc *, void *);
    312 static int	bridge_ioctl_gma(struct bridge_softc *, void *);
    313 static int	bridge_ioctl_sma(struct bridge_softc *, void *);
    314 static int	bridge_ioctl_sifprio(struct bridge_softc *, void *);
    315 static int	bridge_ioctl_sifcost(struct bridge_softc *, void *);
    316 #if defined(BRIDGE_IPF)
    317 static int	bridge_ioctl_gfilt(struct bridge_softc *, void *);
    318 static int	bridge_ioctl_sfilt(struct bridge_softc *, void *);
    319 static int	bridge_ipf(void *, struct mbuf **, struct ifnet *, int);
    320 static int	bridge_ip_checkbasic(struct mbuf **mp);
    321 # ifdef INET6
    322 static int	bridge_ip6_checkbasic(struct mbuf **mp);
    323 # endif /* INET6 */
    324 #endif /* BRIDGE_IPF */
    325 
    326 struct bridge_control {
    327 	int	(*bc_func)(struct bridge_softc *, void *);
    328 	int	bc_argsize;
    329 	int	bc_flags;
    330 };
    331 
    332 #define	BC_F_COPYIN		0x01	/* copy arguments in */
    333 #define	BC_F_COPYOUT		0x02	/* copy arguments out */
    334 #define	BC_F_SUSER		0x04	/* do super-user check */
    335 #define BC_F_XLATEIN		0x08	/* xlate arguments in */
    336 #define BC_F_XLATEOUT		0x10	/* xlate arguments out */
    337 
    338 static const struct bridge_control bridge_control_table[] = {
    339 [BRDGADD] = {bridge_ioctl_add, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    340 [BRDGDEL] = {bridge_ioctl_del, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    341 
    342 [BRDGGIFFLGS] = {bridge_ioctl_gifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_COPYOUT},
    343 [BRDGSIFFLGS] = {bridge_ioctl_sifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    344 
    345 [BRDGSCACHE] = {bridge_ioctl_scache, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    346 [BRDGGCACHE] = {bridge_ioctl_gcache, sizeof(struct ifbrparam), BC_F_COPYOUT},
    347 
    348 [OBRDGGIFS] = {bridge_ioctl_gifs, sizeof(struct ifbifconf), BC_F_COPYIN|BC_F_COPYOUT},
    349 [OBRDGRTS] = {bridge_ioctl_rts, sizeof(struct ifbaconf), BC_F_COPYIN|BC_F_COPYOUT},
    350 
    351 [BRDGSADDR] = {bridge_ioctl_saddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER},
    352 
    353 [BRDGSTO] = {bridge_ioctl_sto, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    354 [BRDGGTO] = {bridge_ioctl_gto, sizeof(struct ifbrparam), BC_F_COPYOUT},
    355 
    356 [BRDGDADDR] = {bridge_ioctl_daddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER},
    357 
    358 [BRDGFLUSH] = {bridge_ioctl_flush, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    359 
    360 [BRDGGPRI] = {bridge_ioctl_gpri, sizeof(struct ifbrparam), BC_F_COPYOUT},
    361 [BRDGSPRI] = {bridge_ioctl_spri, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    362 
    363 [BRDGGHT] = {bridge_ioctl_ght, sizeof(struct ifbrparam), BC_F_COPYOUT},
    364 [BRDGSHT] = {bridge_ioctl_sht, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    365 
    366 [BRDGGFD] = {bridge_ioctl_gfd, sizeof(struct ifbrparam), BC_F_COPYOUT},
    367 [BRDGSFD] = {bridge_ioctl_sfd, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    368 
    369 [BRDGGMA] = {bridge_ioctl_gma, sizeof(struct ifbrparam), BC_F_COPYOUT},
    370 [BRDGSMA] = {bridge_ioctl_sma, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    371 
    372 [BRDGSIFPRIO] = {bridge_ioctl_sifprio, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    373 
    374 [BRDGSIFCOST] = {bridge_ioctl_sifcost, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
    375 #if defined(BRIDGE_IPF)
    376 [BRDGGFILT] = {bridge_ioctl_gfilt, sizeof(struct ifbrparam), BC_F_COPYOUT},
    377 [BRDGSFILT] = {bridge_ioctl_sfilt, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
    378 #endif /* BRIDGE_IPF */
    379 [BRDGGIFS] = {bridge_ioctl_gifs, sizeof(struct ifbifconf), BC_F_XLATEIN|BC_F_XLATEOUT},
    380 [BRDGRTS] = {bridge_ioctl_rts, sizeof(struct ifbaconf), BC_F_XLATEIN|BC_F_XLATEOUT},
    381 };
    382 
    383 static const int bridge_control_table_size = __arraycount(bridge_control_table);
    384 
    385 static struct if_clone bridge_cloner =
    386     IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy);
    387 
    388 /*
    389  * bridgeattach:
    390  *
    391  *	Pseudo-device attach routine.
    392  */
    393 void
    394 bridgeattach(int n)
    395 {
    396 
    397 	pool_init(&bridge_rtnode_pool, sizeof(struct bridge_rtnode),
    398 	    0, 0, 0, "brtpl", NULL, IPL_NET);
    399 
    400 	bridge_psref_class = psref_class_create("bridge", IPL_SOFTNET);
    401 
    402 	if_clone_attach(&bridge_cloner);
    403 }
    404 
    405 /*
    406  * bridge_clone_create:
    407  *
    408  *	Create a new bridge instance.
    409  */
    410 static int
    411 bridge_clone_create(struct if_clone *ifc, int unit)
    412 {
    413 	struct bridge_softc *sc;
    414 	struct ifnet *ifp;
    415 	int error;
    416 
    417 	sc = kmem_zalloc(sizeof(*sc),  KM_SLEEP);
    418 	ifp = &sc->sc_if;
    419 
    420 	sc->sc_brtmax = BRIDGE_RTABLE_MAX;
    421 	sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
    422 	sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
    423 	sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
    424 	sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
    425 	sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
    426 	sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
    427 	sc->sc_filter_flags = 0;
    428 
    429 	/* Initialize our routing table. */
    430 	bridge_rtable_init(sc);
    431 
    432 	error = workqueue_create(&sc->sc_rtage_wq, "bridge_rtage",
    433 	    bridge_rtage_work, sc, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
    434 	if (error)
    435 		panic("%s: workqueue_create %d\n", __func__, error);
    436 
    437 	callout_init(&sc->sc_brcallout, CALLOUT_MPSAFE);
    438 	callout_init(&sc->sc_bstpcallout, CALLOUT_MPSAFE);
    439 
    440 	mutex_init(&sc->sc_iflist_psref.bip_lock, MUTEX_DEFAULT, IPL_NONE);
    441 	PSLIST_INIT(&sc->sc_iflist_psref.bip_iflist);
    442 	sc->sc_iflist_psref.bip_psz = pserialize_create();
    443 
    444 	if_initname(ifp, ifc->ifc_name, unit);
    445 	ifp->if_softc = sc;
    446 	ifp->if_extflags = IFEF_NO_LINK_STATE_CHANGE;
    447 #ifdef NET_MPSAFE
    448 	ifp->if_extflags |= IFEF_MPSAFE;
    449 #endif
    450 	ifp->if_mtu = ETHERMTU;
    451 	ifp->if_ioctl = bridge_ioctl;
    452 	ifp->if_output = bridge_output;
    453 	ifp->if_start = bridge_start;
    454 	ifp->if_stop = bridge_stop;
    455 	ifp->if_init = bridge_init;
    456 	ifp->if_type = IFT_BRIDGE;
    457 	ifp->if_addrlen = 0;
    458 	ifp->if_dlt = DLT_EN10MB;
    459 	ifp->if_hdrlen = ETHER_HDR_LEN;
    460 
    461 	error = if_initialize(ifp);
    462 	if (error != 0) {
    463 		pserialize_destroy(sc->sc_iflist_psref.bip_psz);
    464 		mutex_destroy(&sc->sc_iflist_psref.bip_lock);
    465 		callout_destroy(&sc->sc_brcallout);
    466 		callout_destroy(&sc->sc_bstpcallout);
    467 		workqueue_destroy(sc->sc_rtage_wq);
    468 		bridge_rtable_fini(sc);
    469 		kmem_free(sc, sizeof(*sc));
    470 
    471 		return error;
    472 	}
    473 	if_alloc_sadl(ifp);
    474 	if_register(ifp);
    475 
    476 	return 0;
    477 }
    478 
    479 /*
    480  * bridge_clone_destroy:
    481  *
    482  *	Destroy a bridge instance.
    483  */
    484 static int
    485 bridge_clone_destroy(struct ifnet *ifp)
    486 {
    487 	struct bridge_softc *sc = ifp->if_softc;
    488 	struct bridge_iflist *bif;
    489 
    490 	if ((ifp->if_flags & IFF_RUNNING) != 0)
    491 		bridge_stop(ifp, 1);
    492 
    493 	BRIDGE_LOCK(sc);
    494 	for (;;) {
    495 		bif = PSLIST_WRITER_FIRST(&sc->sc_iflist_psref.bip_iflist, struct bridge_iflist,
    496 		    bif_next);
    497 		if (bif == NULL)
    498 			break;
    499 		bridge_delete_member(sc, bif);
    500 	}
    501 	PSLIST_DESTROY(&sc->sc_iflist_psref.bip_iflist);
    502 	BRIDGE_UNLOCK(sc);
    503 
    504 	if_detach(ifp);
    505 
    506 	/* Tear down the routing table. */
    507 	bridge_rtable_fini(sc);
    508 
    509 	pserialize_destroy(sc->sc_iflist_psref.bip_psz);
    510 	mutex_destroy(&sc->sc_iflist_psref.bip_lock);
    511 	callout_destroy(&sc->sc_brcallout);
    512 	callout_destroy(&sc->sc_bstpcallout);
    513 	workqueue_destroy(sc->sc_rtage_wq);
    514 	kmem_free(sc, sizeof(*sc));
    515 
    516 	return 0;
    517 }
    518 
    519 /*
    520  * bridge_ioctl:
    521  *
    522  *	Handle a control request from the operator.
    523  */
    524 static int
    525 bridge_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    526 {
    527 	struct bridge_softc *sc = ifp->if_softc;
    528 	struct lwp *l = curlwp;	/* XXX */
    529 	union {
    530 		struct ifbreq ifbreq;
    531 		struct ifbifconf ifbifconf;
    532 		struct ifbareq ifbareq;
    533 		struct ifbaconf ifbaconf;
    534 		struct ifbrparam ifbrparam;
    535 	} args;
    536 	struct ifdrv *ifd = (struct ifdrv *) data;
    537 	const struct bridge_control *bc = NULL; /* XXXGCC */
    538 	int s, error = 0;
    539 
    540 	/* Authorize command before calling splsoftnet(). */
    541 	switch (cmd) {
    542 	case SIOCGDRVSPEC:
    543 	case SIOCSDRVSPEC:
    544 		if (ifd->ifd_cmd >= bridge_control_table_size
    545 		    || (bc = &bridge_control_table[ifd->ifd_cmd]) == NULL) {
    546 			error = EINVAL;
    547 			return error;
    548 		}
    549 
    550 		/* We only care about BC_F_SUSER at this point. */
    551 		if ((bc->bc_flags & BC_F_SUSER) == 0)
    552 			break;
    553 
    554 		error = kauth_authorize_network(l->l_cred,
    555 		    KAUTH_NETWORK_INTERFACE_BRIDGE,
    556 		    cmd == SIOCGDRVSPEC ?
    557 		     KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_GETPRIV :
    558 		     KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_SETPRIV,
    559 		     ifd, NULL, NULL);
    560 		if (error)
    561 			return error;
    562 
    563 		break;
    564 	}
    565 
    566 	s = splsoftnet();
    567 
    568 	switch (cmd) {
    569 	case SIOCGDRVSPEC:
    570 	case SIOCSDRVSPEC:
    571 		KASSERT(bc != NULL);
    572 		if (cmd == SIOCGDRVSPEC &&
    573 		    (bc->bc_flags & (BC_F_COPYOUT|BC_F_XLATEOUT)) == 0) {
    574 			error = EINVAL;
    575 			break;
    576 		}
    577 		else if (cmd == SIOCSDRVSPEC &&
    578 		    (bc->bc_flags & (BC_F_COPYOUT|BC_F_XLATEOUT)) != 0) {
    579 			error = EINVAL;
    580 			break;
    581 		}
    582 
    583 		/* BC_F_SUSER is checked above, before splsoftnet(). */
    584 
    585 		if ((bc->bc_flags & (BC_F_XLATEIN|BC_F_XLATEOUT)) == 0
    586 		    && (ifd->ifd_len != bc->bc_argsize
    587 			|| ifd->ifd_len > sizeof(args))) {
    588 			error = EINVAL;
    589 			break;
    590 		}
    591 
    592 		memset(&args, 0, sizeof(args));
    593 		if (bc->bc_flags & BC_F_COPYIN) {
    594 			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
    595 			if (error)
    596 				break;
    597 		} else if (bc->bc_flags & BC_F_XLATEIN) {
    598 			args.ifbifconf.ifbic_len = ifd->ifd_len;
    599 			args.ifbifconf.ifbic_buf = ifd->ifd_data;
    600 		}
    601 
    602 		error = (*bc->bc_func)(sc, &args);
    603 		if (error)
    604 			break;
    605 
    606 		if (bc->bc_flags & BC_F_COPYOUT) {
    607 			error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
    608 		} else if (bc->bc_flags & BC_F_XLATEOUT) {
    609 			ifd->ifd_len = args.ifbifconf.ifbic_len;
    610 			ifd->ifd_data = args.ifbifconf.ifbic_buf;
    611 		}
    612 		break;
    613 
    614 	case SIOCSIFFLAGS:
    615 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    616 			break;
    617 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    618 		case IFF_RUNNING:
    619 			/*
    620 			 * If interface is marked down and it is running,
    621 			 * then stop and disable it.
    622 			 */
    623 			(*ifp->if_stop)(ifp, 1);
    624 			break;
    625 		case IFF_UP:
    626 			/*
    627 			 * If interface is marked up and it is stopped, then
    628 			 * start it.
    629 			 */
    630 			error = (*ifp->if_init)(ifp);
    631 			break;
    632 		default:
    633 			break;
    634 		}
    635 		break;
    636 
    637 	case SIOCSIFMTU:
    638 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    639 			error = 0;
    640 		break;
    641 
    642 	default:
    643 		error = ifioctl_common(ifp, cmd, data);
    644 		break;
    645 	}
    646 
    647 	splx(s);
    648 
    649 	return error;
    650 }
    651 
    652 /*
    653  * bridge_lookup_member:
    654  *
    655  *	Lookup a bridge member interface.
    656  */
    657 static struct bridge_iflist *
    658 bridge_lookup_member(struct bridge_softc *sc, const char *name, struct psref *psref)
    659 {
    660 	struct bridge_iflist *bif;
    661 	struct ifnet *ifp;
    662 	int s;
    663 
    664 	BRIDGE_PSZ_RENTER(s);
    665 
    666 	BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
    667 		ifp = bif->bif_ifp;
    668 		if (strcmp(ifp->if_xname, name) == 0)
    669 			break;
    670 	}
    671 	if (bif != NULL)
    672 		bridge_acquire_member(sc, bif, psref);
    673 
    674 	BRIDGE_PSZ_REXIT(s);
    675 
    676 	return bif;
    677 }
    678 
    679 /*
    680  * bridge_lookup_member_if:
    681  *
    682  *	Lookup a bridge member interface by ifnet*.
    683  */
    684 static struct bridge_iflist *
    685 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp,
    686     struct psref *psref)
    687 {
    688 	struct bridge_iflist *bif;
    689 	int s;
    690 
    691 	BRIDGE_PSZ_RENTER(s);
    692 
    693 	bif = member_ifp->if_bridgeif;
    694 	if (bif != NULL) {
    695 		psref_acquire(psref, &bif->bif_psref,
    696 		    bridge_psref_class);
    697 	}
    698 
    699 	BRIDGE_PSZ_REXIT(s);
    700 
    701 	return bif;
    702 }
    703 
    704 static void
    705 bridge_acquire_member(struct bridge_softc *sc, struct bridge_iflist *bif,
    706     struct psref *psref)
    707 {
    708 
    709 	psref_acquire(psref, &bif->bif_psref, bridge_psref_class);
    710 }
    711 
    712 /*
    713  * bridge_release_member:
    714  *
    715  *	Release the specified member interface.
    716  */
    717 static void
    718 bridge_release_member(struct bridge_softc *sc, struct bridge_iflist *bif,
    719     struct psref *psref)
    720 {
    721 
    722 	psref_release(psref, &bif->bif_psref, bridge_psref_class);
    723 }
    724 
    725 /*
    726  * bridge_delete_member:
    727  *
    728  *	Delete the specified member interface.
    729  */
    730 static void
    731 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif)
    732 {
    733 	struct ifnet *ifs = bif->bif_ifp;
    734 
    735 	KASSERT(BRIDGE_LOCKED(sc));
    736 
    737 	ifs->_if_input = ether_input;
    738 	ifs->if_bridge = NULL;
    739 	ifs->if_bridgeif = NULL;
    740 
    741 	PSLIST_WRITER_REMOVE(bif, bif_next);
    742 	BRIDGE_PSZ_PERFORM(sc);
    743 	BRIDGE_UNLOCK(sc);
    744 
    745 	psref_target_destroy(&bif->bif_psref, bridge_psref_class);
    746 
    747 	PSLIST_ENTRY_DESTROY(bif, bif_next);
    748 	kmem_free(bif, sizeof(*bif));
    749 
    750 	BRIDGE_LOCK(sc);
    751 }
    752 
    753 static int
    754 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
    755 {
    756 	struct ifbreq *req = arg;
    757 	struct bridge_iflist *bif = NULL;
    758 	struct ifnet *ifs;
    759 	int error = 0;
    760 	struct psref psref;
    761 
    762 	ifs = if_get(req->ifbr_ifsname, &psref);
    763 	if (ifs == NULL)
    764 		return ENOENT;
    765 
    766 	if (ifs->if_bridge == sc) {
    767 		error = EEXIST;
    768 		goto out;
    769 	}
    770 
    771 	if (ifs->if_bridge != NULL) {
    772 		error = EBUSY;
    773 		goto out;
    774 	}
    775 
    776 	if (ifs->_if_input != ether_input) {
    777 		error = EINVAL;
    778 		goto out;
    779 	}
    780 
    781 	/* FIXME: doesn't work with non-IFF_SIMPLEX interfaces */
    782 	if ((ifs->if_flags & IFF_SIMPLEX) == 0) {
    783 		error = EINVAL;
    784 		goto out;
    785 	}
    786 
    787 	bif = kmem_alloc(sizeof(*bif), KM_SLEEP);
    788 
    789 	switch (ifs->if_type) {
    790 	case IFT_ETHER:
    791 		if (sc->sc_if.if_mtu != ifs->if_mtu) {
    792 			error = EINVAL;
    793 			goto out;
    794 		}
    795 		/* FALLTHROUGH */
    796 	case IFT_L2TP:
    797 		IFNET_LOCK(ifs);
    798 		error = ether_enable_vlan_mtu(ifs);
    799 		IFNET_UNLOCK(ifs);
    800 		if (error > 0)
    801 			goto out;
    802 		/*
    803 		 * Place the interface into promiscuous mode.
    804 		 */
    805 		error = ifpromisc(ifs, 1);
    806 		if (error)
    807 			goto out;
    808 		break;
    809 	default:
    810 		error = EINVAL;
    811 		goto out;
    812 	}
    813 
    814 	bif->bif_ifp = ifs;
    815 	bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
    816 	bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
    817 	bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
    818 	PSLIST_ENTRY_INIT(bif, bif_next);
    819 	psref_target_init(&bif->bif_psref, bridge_psref_class);
    820 
    821 	BRIDGE_LOCK(sc);
    822 
    823 	ifs->if_bridge = sc;
    824 	ifs->if_bridgeif = bif;
    825 	PSLIST_WRITER_INSERT_HEAD(&sc->sc_iflist_psref.bip_iflist, bif, bif_next);
    826 	ifs->_if_input = bridge_input;
    827 
    828 	BRIDGE_UNLOCK(sc);
    829 
    830 	if (sc->sc_if.if_flags & IFF_RUNNING)
    831 		bstp_initialization(sc);
    832 	else
    833 		bstp_stop(sc);
    834 
    835  out:
    836 	if_put(ifs, &psref);
    837 	if (error) {
    838 		if (bif != NULL)
    839 			kmem_free(bif, sizeof(*bif));
    840 	}
    841 	return error;
    842 }
    843 
    844 static int
    845 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
    846 {
    847 	struct ifbreq *req = arg;
    848 	const char *name = req->ifbr_ifsname;
    849 	struct bridge_iflist *bif;
    850 	struct ifnet *ifs;
    851 
    852 	BRIDGE_LOCK(sc);
    853 
    854 	/*
    855 	 * Don't use bridge_lookup_member. We want to get a member
    856 	 * with bif_refs == 0.
    857 	 */
    858 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) {
    859 		ifs = bif->bif_ifp;
    860 		if (strcmp(ifs->if_xname, name) == 0)
    861 			break;
    862 	}
    863 
    864 	if (bif == NULL) {
    865 		BRIDGE_UNLOCK(sc);
    866 		return ENOENT;
    867 	}
    868 
    869 	bridge_delete_member(sc, bif);
    870 
    871 	BRIDGE_UNLOCK(sc);
    872 
    873 	switch (ifs->if_type) {
    874 	case IFT_ETHER:
    875 	case IFT_L2TP:
    876 		/*
    877 		 * Take the interface out of promiscuous mode.
    878 		 * Don't call it with holding a spin lock.
    879 		 */
    880 		(void) ifpromisc(ifs, 0);
    881 		IFNET_LOCK(ifs);
    882 		(void) ether_disable_vlan_mtu(ifs);
    883 		IFNET_UNLOCK(ifs);
    884 		break;
    885 	default:
    886 #ifdef DIAGNOSTIC
    887 		panic("bridge_delete_member: impossible");
    888 #endif
    889 		break;
    890 	}
    891 
    892 	bridge_rtdelete(sc, ifs);
    893 
    894 	if (sc->sc_if.if_flags & IFF_RUNNING)
    895 		bstp_initialization(sc);
    896 
    897 	return 0;
    898 }
    899 
    900 static int
    901 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
    902 {
    903 	struct ifbreq *req = arg;
    904 	struct bridge_iflist *bif;
    905 	struct psref psref;
    906 
    907 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
    908 	if (bif == NULL)
    909 		return ENOENT;
    910 
    911 	req->ifbr_ifsflags = bif->bif_flags;
    912 	req->ifbr_state = bif->bif_state;
    913 	req->ifbr_priority = bif->bif_priority;
    914 	req->ifbr_path_cost = bif->bif_path_cost;
    915 	req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
    916 
    917 	bridge_release_member(sc, bif, &psref);
    918 
    919 	return 0;
    920 }
    921 
    922 static int
    923 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
    924 {
    925 	struct ifbreq *req = arg;
    926 	struct bridge_iflist *bif;
    927 	struct psref psref;
    928 
    929 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
    930 	if (bif == NULL)
    931 		return ENOENT;
    932 
    933 	if (req->ifbr_ifsflags & IFBIF_STP) {
    934 		switch (bif->bif_ifp->if_type) {
    935 		case IFT_ETHER:
    936 		case IFT_L2TP:
    937 			/* These can do spanning tree. */
    938 			break;
    939 
    940 		default:
    941 			/* Nothing else can. */
    942 			bridge_release_member(sc, bif, &psref);
    943 			return EINVAL;
    944 		}
    945 	}
    946 
    947 	bif->bif_flags = req->ifbr_ifsflags;
    948 
    949 	bridge_release_member(sc, bif, &psref);
    950 
    951 	if (sc->sc_if.if_flags & IFF_RUNNING)
    952 		bstp_initialization(sc);
    953 
    954 	return 0;
    955 }
    956 
    957 static int
    958 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
    959 {
    960 	struct ifbrparam *param = arg;
    961 
    962 	sc->sc_brtmax = param->ifbrp_csize;
    963 	bridge_rttrim(sc);
    964 
    965 	return 0;
    966 }
    967 
    968 static int
    969 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
    970 {
    971 	struct ifbrparam *param = arg;
    972 
    973 	param->ifbrp_csize = sc->sc_brtmax;
    974 
    975 	return 0;
    976 }
    977 
    978 static int
    979 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
    980 {
    981 	struct ifbifconf *bifc = arg;
    982 	struct bridge_iflist *bif;
    983 	struct ifbreq *breqs;
    984 	int i, count, error = 0;
    985 
    986 retry:
    987 	BRIDGE_LOCK(sc);
    988 	count = 0;
    989 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc)
    990 		count++;
    991 	BRIDGE_UNLOCK(sc);
    992 
    993 	if (count == 0) {
    994 		bifc->ifbic_len = 0;
    995 		return 0;
    996 	}
    997 
    998 	if (bifc->ifbic_len == 0 || bifc->ifbic_len < (sizeof(*breqs) * count)) {
    999 		/* Tell that a larger buffer is needed */
   1000 		bifc->ifbic_len = sizeof(*breqs) * count;
   1001 		return 0;
   1002 	}
   1003 
   1004 	breqs = kmem_alloc(sizeof(*breqs) * count, KM_SLEEP);
   1005 
   1006 	BRIDGE_LOCK(sc);
   1007 
   1008 	i = 0;
   1009 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc)
   1010 		i++;
   1011 	if (i > count) {
   1012 		/*
   1013 		 * The number of members has been increased.
   1014 		 * We need more memory!
   1015 		 */
   1016 		BRIDGE_UNLOCK(sc);
   1017 		kmem_free(breqs, sizeof(*breqs) * count);
   1018 		goto retry;
   1019 	}
   1020 
   1021 	i = 0;
   1022 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) {
   1023 		struct ifbreq *breq = &breqs[i++];
   1024 		memset(breq, 0, sizeof(*breq));
   1025 
   1026 		strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
   1027 		    sizeof(breq->ifbr_ifsname));
   1028 		breq->ifbr_ifsflags = bif->bif_flags;
   1029 		breq->ifbr_state = bif->bif_state;
   1030 		breq->ifbr_priority = bif->bif_priority;
   1031 		breq->ifbr_path_cost = bif->bif_path_cost;
   1032 		breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
   1033 	}
   1034 
   1035 	/* Don't call copyout with holding the mutex */
   1036 	BRIDGE_UNLOCK(sc);
   1037 
   1038 	for (i = 0; i < count; i++) {
   1039 		error = copyout(&breqs[i], bifc->ifbic_req + i, sizeof(*breqs));
   1040 		if (error)
   1041 			break;
   1042 	}
   1043 	bifc->ifbic_len = sizeof(*breqs) * i;
   1044 
   1045 	kmem_free(breqs, sizeof(*breqs) * count);
   1046 
   1047 	return error;
   1048 }
   1049 
   1050 static int
   1051 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
   1052 {
   1053 	struct ifbaconf *bac = arg;
   1054 	struct bridge_rtnode *brt;
   1055 	struct ifbareq bareq;
   1056 	int count = 0, error = 0, len;
   1057 
   1058 	if (bac->ifbac_len == 0)
   1059 		return 0;
   1060 
   1061 	BRIDGE_RT_LOCK(sc);
   1062 
   1063 	len = bac->ifbac_len;
   1064 	BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) {
   1065 		if (len < sizeof(bareq))
   1066 			goto out;
   1067 		memset(&bareq, 0, sizeof(bareq));
   1068 		strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
   1069 		    sizeof(bareq.ifba_ifsname));
   1070 		memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
   1071 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
   1072 			bareq.ifba_expire = brt->brt_expire - time_uptime;
   1073 		} else
   1074 			bareq.ifba_expire = 0;
   1075 		bareq.ifba_flags = brt->brt_flags;
   1076 
   1077 		error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
   1078 		if (error)
   1079 			goto out;
   1080 		count++;
   1081 		len -= sizeof(bareq);
   1082 	}
   1083  out:
   1084 	BRIDGE_RT_UNLOCK(sc);
   1085 
   1086 	bac->ifbac_len = sizeof(bareq) * count;
   1087 	return error;
   1088 }
   1089 
   1090 static int
   1091 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
   1092 {
   1093 	struct ifbareq *req = arg;
   1094 	struct bridge_iflist *bif;
   1095 	int error;
   1096 	struct psref psref;
   1097 
   1098 	bif = bridge_lookup_member(sc, req->ifba_ifsname, &psref);
   1099 	if (bif == NULL)
   1100 		return ENOENT;
   1101 
   1102 	error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
   1103 	    req->ifba_flags);
   1104 
   1105 	bridge_release_member(sc, bif, &psref);
   1106 
   1107 	return error;
   1108 }
   1109 
   1110 static int
   1111 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
   1112 {
   1113 	struct ifbrparam *param = arg;
   1114 
   1115 	sc->sc_brttimeout = param->ifbrp_ctime;
   1116 
   1117 	return 0;
   1118 }
   1119 
   1120 static int
   1121 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
   1122 {
   1123 	struct ifbrparam *param = arg;
   1124 
   1125 	param->ifbrp_ctime = sc->sc_brttimeout;
   1126 
   1127 	return 0;
   1128 }
   1129 
   1130 static int
   1131 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
   1132 {
   1133 	struct ifbareq *req = arg;
   1134 
   1135 	return (bridge_rtdaddr(sc, req->ifba_dst));
   1136 }
   1137 
   1138 static int
   1139 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
   1140 {
   1141 	struct ifbreq *req = arg;
   1142 
   1143 	bridge_rtflush(sc, req->ifbr_ifsflags);
   1144 
   1145 	return 0;
   1146 }
   1147 
   1148 static int
   1149 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
   1150 {
   1151 	struct ifbrparam *param = arg;
   1152 
   1153 	param->ifbrp_prio = sc->sc_bridge_priority;
   1154 
   1155 	return 0;
   1156 }
   1157 
   1158 static int
   1159 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
   1160 {
   1161 	struct ifbrparam *param = arg;
   1162 
   1163 	sc->sc_bridge_priority = param->ifbrp_prio;
   1164 
   1165 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1166 		bstp_initialization(sc);
   1167 
   1168 	return 0;
   1169 }
   1170 
   1171 static int
   1172 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
   1173 {
   1174 	struct ifbrparam *param = arg;
   1175 
   1176 	param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
   1177 
   1178 	return 0;
   1179 }
   1180 
   1181 static int
   1182 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
   1183 {
   1184 	struct ifbrparam *param = arg;
   1185 
   1186 	if (param->ifbrp_hellotime == 0)
   1187 		return EINVAL;
   1188 	sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
   1189 
   1190 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1191 		bstp_initialization(sc);
   1192 
   1193 	return 0;
   1194 }
   1195 
   1196 static int
   1197 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
   1198 {
   1199 	struct ifbrparam *param = arg;
   1200 
   1201 	param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
   1202 
   1203 	return 0;
   1204 }
   1205 
   1206 static int
   1207 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
   1208 {
   1209 	struct ifbrparam *param = arg;
   1210 
   1211 	if (param->ifbrp_fwddelay == 0)
   1212 		return EINVAL;
   1213 	sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
   1214 
   1215 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1216 		bstp_initialization(sc);
   1217 
   1218 	return 0;
   1219 }
   1220 
   1221 static int
   1222 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
   1223 {
   1224 	struct ifbrparam *param = arg;
   1225 
   1226 	param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
   1227 
   1228 	return 0;
   1229 }
   1230 
   1231 static int
   1232 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
   1233 {
   1234 	struct ifbrparam *param = arg;
   1235 
   1236 	if (param->ifbrp_maxage == 0)
   1237 		return EINVAL;
   1238 	sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
   1239 
   1240 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1241 		bstp_initialization(sc);
   1242 
   1243 	return 0;
   1244 }
   1245 
   1246 static int
   1247 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
   1248 {
   1249 	struct ifbreq *req = arg;
   1250 	struct bridge_iflist *bif;
   1251 	struct psref psref;
   1252 
   1253 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
   1254 	if (bif == NULL)
   1255 		return ENOENT;
   1256 
   1257 	bif->bif_priority = req->ifbr_priority;
   1258 
   1259 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1260 		bstp_initialization(sc);
   1261 
   1262 	bridge_release_member(sc, bif, &psref);
   1263 
   1264 	return 0;
   1265 }
   1266 
   1267 #if defined(BRIDGE_IPF)
   1268 static int
   1269 bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg)
   1270 {
   1271 	struct ifbrparam *param = arg;
   1272 
   1273 	param->ifbrp_filter = sc->sc_filter_flags;
   1274 
   1275 	return 0;
   1276 }
   1277 
   1278 static int
   1279 bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg)
   1280 {
   1281 	struct ifbrparam *param = arg;
   1282 	uint32_t nflags, oflags;
   1283 
   1284 	if (param->ifbrp_filter & ~IFBF_FILT_MASK)
   1285 		return EINVAL;
   1286 
   1287 	nflags = param->ifbrp_filter;
   1288 	oflags = sc->sc_filter_flags;
   1289 
   1290 	if ((nflags & IFBF_FILT_USEIPF) && !(oflags & IFBF_FILT_USEIPF)) {
   1291 		pfil_add_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
   1292 			sc->sc_if.if_pfil);
   1293 	}
   1294 	if (!(nflags & IFBF_FILT_USEIPF) && (oflags & IFBF_FILT_USEIPF)) {
   1295 		pfil_remove_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
   1296 			sc->sc_if.if_pfil);
   1297 	}
   1298 
   1299 	sc->sc_filter_flags = nflags;
   1300 
   1301 	return 0;
   1302 }
   1303 #endif /* BRIDGE_IPF */
   1304 
   1305 static int
   1306 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
   1307 {
   1308 	struct ifbreq *req = arg;
   1309 	struct bridge_iflist *bif;
   1310 	struct psref psref;
   1311 
   1312 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
   1313 	if (bif == NULL)
   1314 		return ENOENT;
   1315 
   1316 	bif->bif_path_cost = req->ifbr_path_cost;
   1317 
   1318 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1319 		bstp_initialization(sc);
   1320 
   1321 	bridge_release_member(sc, bif, &psref);
   1322 
   1323 	return 0;
   1324 }
   1325 
   1326 /*
   1327  * bridge_ifdetach:
   1328  *
   1329  *	Detach an interface from a bridge.  Called when a member
   1330  *	interface is detaching.
   1331  */
   1332 void
   1333 bridge_ifdetach(struct ifnet *ifp)
   1334 {
   1335 	struct bridge_softc *sc = ifp->if_bridge;
   1336 	struct ifbreq breq;
   1337 
   1338 	/* ioctl_lock should prevent this from happening */
   1339 	KASSERT(sc != NULL);
   1340 
   1341 	memset(&breq, 0, sizeof(breq));
   1342 	strlcpy(breq.ifbr_ifsname, ifp->if_xname, sizeof(breq.ifbr_ifsname));
   1343 
   1344 	(void) bridge_ioctl_del(sc, &breq);
   1345 }
   1346 
   1347 /*
   1348  * bridge_init:
   1349  *
   1350  *	Initialize a bridge interface.
   1351  */
   1352 static int
   1353 bridge_init(struct ifnet *ifp)
   1354 {
   1355 	struct bridge_softc *sc = ifp->if_softc;
   1356 
   1357 	KASSERT((ifp->if_flags & IFF_RUNNING) == 0);
   1358 
   1359 	callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
   1360 	    bridge_timer, sc);
   1361 	bstp_initialization(sc);
   1362 
   1363 	ifp->if_flags |= IFF_RUNNING;
   1364 	return 0;
   1365 }
   1366 
   1367 /*
   1368  * bridge_stop:
   1369  *
   1370  *	Stop the bridge interface.
   1371  */
   1372 static void
   1373 bridge_stop(struct ifnet *ifp, int disable)
   1374 {
   1375 	struct bridge_softc *sc = ifp->if_softc;
   1376 
   1377 	KASSERT((ifp->if_flags & IFF_RUNNING) != 0);
   1378 	ifp->if_flags &= ~IFF_RUNNING;
   1379 
   1380 	callout_halt(&sc->sc_brcallout, NULL);
   1381 	workqueue_wait(sc->sc_rtage_wq, &sc->sc_rtage_wk);
   1382 	bstp_stop(sc);
   1383 	bridge_rtflush(sc, IFBF_FLUSHDYN);
   1384 }
   1385 
   1386 /*
   1387  * bridge_enqueue:
   1388  *
   1389  *	Enqueue a packet on a bridge member interface.
   1390  */
   1391 void
   1392 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m,
   1393     int runfilt)
   1394 {
   1395 	int len, error;
   1396 	short mflags;
   1397 
   1398 	if (runfilt) {
   1399 		if (pfil_run_hooks(sc->sc_if.if_pfil, &m,
   1400 		    dst_ifp, PFIL_OUT) != 0) {
   1401 			if (m != NULL)
   1402 				m_freem(m);
   1403 			return;
   1404 		}
   1405 		if (m == NULL)
   1406 			return;
   1407 	}
   1408 
   1409 #ifdef ALTQ
   1410 	KERNEL_LOCK(1, NULL);
   1411 	/*
   1412 	 * If ALTQ is enabled on the member interface, do
   1413 	 * classification; the queueing discipline might
   1414 	 * not require classification, but might require
   1415 	 * the address family/header pointer in the pktattr.
   1416 	 */
   1417 	if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
   1418 		/* XXX IFT_ETHER */
   1419 		altq_etherclassify(&dst_ifp->if_snd, m);
   1420 	}
   1421 	KERNEL_UNLOCK_ONE(NULL);
   1422 #endif /* ALTQ */
   1423 
   1424 	len = m->m_pkthdr.len;
   1425 	mflags = m->m_flags;
   1426 
   1427 	error = if_transmit_lock(dst_ifp, m);
   1428 	if (error) {
   1429 		/* mbuf is already freed */
   1430 		sc->sc_if.if_oerrors++;
   1431 		return;
   1432 	}
   1433 
   1434 	sc->sc_if.if_opackets++;
   1435 	sc->sc_if.if_obytes += len;
   1436 	if (mflags & M_MCAST)
   1437 		sc->sc_if.if_omcasts++;
   1438 }
   1439 
   1440 /*
   1441  * bridge_output:
   1442  *
   1443  *	Send output from a bridge member interface.  This
   1444  *	performs the bridging function for locally originated
   1445  *	packets.
   1446  *
   1447  *	The mbuf has the Ethernet header already attached.  We must
   1448  *	enqueue or free the mbuf before returning.
   1449  */
   1450 int
   1451 bridge_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
   1452     const struct rtentry *rt)
   1453 {
   1454 	struct ether_header *eh;
   1455 	struct ifnet *dst_if;
   1456 	struct bridge_softc *sc;
   1457 	int s;
   1458 
   1459 	/*
   1460 	 * bridge_output() is called from ether_output(), furthermore
   1461 	 * ifp argument doesn't point to bridge(4). So, don't assert
   1462 	 * IFEF_MPSAFE here.
   1463 	 */
   1464 
   1465 	if (m->m_len < ETHER_HDR_LEN) {
   1466 		m = m_pullup(m, ETHER_HDR_LEN);
   1467 		if (m == NULL)
   1468 			return 0;
   1469 	}
   1470 
   1471 	eh = mtod(m, struct ether_header *);
   1472 	sc = ifp->if_bridge;
   1473 
   1474 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
   1475 		if (memcmp(etherbroadcastaddr,
   1476 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
   1477 			m->m_flags |= M_BCAST;
   1478 		else
   1479 			m->m_flags |= M_MCAST;
   1480 	}
   1481 
   1482 	/*
   1483 	 * If bridge is down, but the original output interface is up,
   1484 	 * go ahead and send out that interface.  Otherwise, the packet
   1485 	 * is dropped below.
   1486 	 */
   1487 	if (__predict_false(sc == NULL) ||
   1488 	    (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   1489 		dst_if = ifp;
   1490 		goto sendunicast;
   1491 	}
   1492 
   1493 	/*
   1494 	 * If the packet is a multicast, or we don't know a better way to
   1495 	 * get there, send to all interfaces.
   1496 	 */
   1497 	if ((m->m_flags & (M_MCAST | M_BCAST)) != 0)
   1498 		dst_if = NULL;
   1499 	else
   1500 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
   1501 	if (dst_if == NULL) {
   1502 		/* XXX Should call bridge_broadcast, but there are locking
   1503 		 * issues which need resolving first. */
   1504 		struct bridge_iflist *bif;
   1505 		struct mbuf *mc;
   1506 		bool used = false;
   1507 
   1508 		BRIDGE_PSZ_RENTER(s);
   1509 		BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
   1510 			struct psref psref;
   1511 
   1512 			bridge_acquire_member(sc, bif, &psref);
   1513 			BRIDGE_PSZ_REXIT(s);
   1514 
   1515 			dst_if = bif->bif_ifp;
   1516 			if ((dst_if->if_flags & IFF_RUNNING) == 0)
   1517 				goto next;
   1518 
   1519 			/*
   1520 			 * If this is not the original output interface,
   1521 			 * and the interface is participating in spanning
   1522 			 * tree, make sure the port is in a state that
   1523 			 * allows forwarding.
   1524 			 */
   1525 			if (dst_if != ifp &&
   1526 			    (bif->bif_flags & IFBIF_STP) != 0) {
   1527 				switch (bif->bif_state) {
   1528 				case BSTP_IFSTATE_BLOCKING:
   1529 				case BSTP_IFSTATE_LISTENING:
   1530 				case BSTP_IFSTATE_DISABLED:
   1531 					goto next;
   1532 				}
   1533 			}
   1534 
   1535 			if (PSLIST_READER_NEXT(bif, struct bridge_iflist,
   1536 			    bif_next) == NULL &&
   1537 			    ((m->m_flags & (M_MCAST | M_BCAST)) == 0 ||
   1538 			    dst_if == ifp))
   1539 			{
   1540 				used = true;
   1541 				mc = m;
   1542 			} else {
   1543 				mc = m_copypacket(m, M_DONTWAIT);
   1544 				if (mc == NULL) {
   1545 					sc->sc_if.if_oerrors++;
   1546 					goto next;
   1547 				}
   1548 			}
   1549 
   1550 			bridge_enqueue(sc, dst_if, mc, 0);
   1551 
   1552 			if ((m->m_flags & (M_MCAST | M_BCAST)) != 0 &&
   1553 			    dst_if != ifp)
   1554 			{
   1555 				if (PSLIST_READER_NEXT(bif,
   1556 				    struct bridge_iflist, bif_next) == NULL)
   1557 				{
   1558 					used = true;
   1559 					mc = m;
   1560 				} else {
   1561 					mc = m_copypacket(m, M_DONTWAIT);
   1562 					if (mc == NULL) {
   1563 						sc->sc_if.if_oerrors++;
   1564 						goto next;
   1565 					}
   1566 				}
   1567 
   1568 				m_set_rcvif(mc, dst_if);
   1569 				mc->m_flags &= ~M_PROMISC;
   1570 
   1571 				s = splsoftnet();
   1572 				KERNEL_LOCK_UNLESS_IFP_MPSAFE(dst_if);
   1573 				ether_input(dst_if, mc);
   1574 				KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(dst_if);
   1575 				splx(s);
   1576 			}
   1577 
   1578 next:
   1579 			BRIDGE_PSZ_RENTER(s);
   1580 			bridge_release_member(sc, bif, &psref);
   1581 
   1582 			/* Guarantee we don't re-enter the loop as we already
   1583 			 * decided we're at the end. */
   1584 			if (used)
   1585 				break;
   1586 		}
   1587 		BRIDGE_PSZ_REXIT(s);
   1588 
   1589 		if (!used)
   1590 			m_freem(m);
   1591 		return 0;
   1592 	}
   1593 
   1594  sendunicast:
   1595 	/*
   1596 	 * XXX Spanning tree consideration here?
   1597 	 */
   1598 
   1599 	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
   1600 		m_freem(m);
   1601 		return 0;
   1602 	}
   1603 
   1604 	bridge_enqueue(sc, dst_if, m, 0);
   1605 
   1606 	return 0;
   1607 }
   1608 
   1609 /*
   1610  * bridge_start:
   1611  *
   1612  *	Start output on a bridge.
   1613  *
   1614  *	NOTE: This routine should never be called in this implementation.
   1615  */
   1616 static void
   1617 bridge_start(struct ifnet *ifp)
   1618 {
   1619 
   1620 	printf("%s: bridge_start() called\n", ifp->if_xname);
   1621 }
   1622 
   1623 /*
   1624  * bridge_forward:
   1625  *
   1626  *	The forwarding function of the bridge.
   1627  */
   1628 static void
   1629 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
   1630 {
   1631 	struct bridge_iflist *bif;
   1632 	struct ifnet *src_if, *dst_if;
   1633 	struct ether_header *eh;
   1634 	struct psref psref;
   1635 	struct psref psref_src;
   1636 	DECLARE_LOCK_VARIABLE;
   1637 
   1638 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   1639 		return;
   1640 
   1641 	src_if = m_get_rcvif_psref(m, &psref_src);
   1642 	if (src_if == NULL) {
   1643 		/* Interface is being destroyed? */
   1644 		m_freem(m);
   1645 		goto out;
   1646 	}
   1647 
   1648 	sc->sc_if.if_ipackets++;
   1649 	sc->sc_if.if_ibytes += m->m_pkthdr.len;
   1650 
   1651 	/*
   1652 	 * Look up the bridge_iflist.
   1653 	 */
   1654 	bif = bridge_lookup_member_if(sc, src_if, &psref);
   1655 	if (bif == NULL) {
   1656 		/* Interface is not a bridge member (anymore?) */
   1657 		m_freem(m);
   1658 		goto out;
   1659 	}
   1660 
   1661 	if (bif->bif_flags & IFBIF_STP) {
   1662 		switch (bif->bif_state) {
   1663 		case BSTP_IFSTATE_BLOCKING:
   1664 		case BSTP_IFSTATE_LISTENING:
   1665 		case BSTP_IFSTATE_DISABLED:
   1666 			m_freem(m);
   1667 			bridge_release_member(sc, bif, &psref);
   1668 			goto out;
   1669 		}
   1670 	}
   1671 
   1672 	eh = mtod(m, struct ether_header *);
   1673 
   1674 	/*
   1675 	 * If the interface is learning, and the source
   1676 	 * address is valid and not multicast, record
   1677 	 * the address.
   1678 	 */
   1679 	if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
   1680 	    ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
   1681 	    (eh->ether_shost[0] == 0 &&
   1682 	     eh->ether_shost[1] == 0 &&
   1683 	     eh->ether_shost[2] == 0 &&
   1684 	     eh->ether_shost[3] == 0 &&
   1685 	     eh->ether_shost[4] == 0 &&
   1686 	     eh->ether_shost[5] == 0) == 0) {
   1687 		(void) bridge_rtupdate(sc, eh->ether_shost,
   1688 		    src_if, 0, IFBAF_DYNAMIC);
   1689 	}
   1690 
   1691 	if ((bif->bif_flags & IFBIF_STP) != 0 &&
   1692 	    bif->bif_state == BSTP_IFSTATE_LEARNING) {
   1693 		m_freem(m);
   1694 		bridge_release_member(sc, bif, &psref);
   1695 		goto out;
   1696 	}
   1697 
   1698 	bridge_release_member(sc, bif, &psref);
   1699 
   1700 	/*
   1701 	 * At this point, the port either doesn't participate
   1702 	 * in spanning tree or it is in the forwarding state.
   1703 	 */
   1704 
   1705 	/*
   1706 	 * If the packet is unicast, destined for someone on
   1707 	 * "this" side of the bridge, drop it.
   1708 	 */
   1709 	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
   1710 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
   1711 		if (src_if == dst_if) {
   1712 			m_freem(m);
   1713 			goto out;
   1714 		}
   1715 	} else {
   1716 		/* ...forward it to all interfaces. */
   1717 		sc->sc_if.if_imcasts++;
   1718 		dst_if = NULL;
   1719 	}
   1720 
   1721 	if (pfil_run_hooks(sc->sc_if.if_pfil, &m, src_if, PFIL_IN) != 0) {
   1722 		if (m != NULL)
   1723 			m_freem(m);
   1724 		goto out;
   1725 	}
   1726 	if (m == NULL)
   1727 		goto out;
   1728 
   1729 	if (dst_if == NULL) {
   1730 		bridge_broadcast(sc, src_if, m);
   1731 		goto out;
   1732 	}
   1733 
   1734 	m_put_rcvif_psref(src_if, &psref_src);
   1735 	src_if = NULL;
   1736 
   1737 	/*
   1738 	 * At this point, we're dealing with a unicast frame
   1739 	 * going to a different interface.
   1740 	 */
   1741 	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
   1742 		m_freem(m);
   1743 		goto out;
   1744 	}
   1745 
   1746 	bif = bridge_lookup_member_if(sc, dst_if, &psref);
   1747 	if (bif == NULL) {
   1748 		/* Not a member of the bridge (anymore?) */
   1749 		m_freem(m);
   1750 		goto out;
   1751 	}
   1752 
   1753 	if (bif->bif_flags & IFBIF_STP) {
   1754 		switch (bif->bif_state) {
   1755 		case BSTP_IFSTATE_DISABLED:
   1756 		case BSTP_IFSTATE_BLOCKING:
   1757 			m_freem(m);
   1758 			bridge_release_member(sc, bif, &psref);
   1759 			goto out;
   1760 		}
   1761 	}
   1762 
   1763 	bridge_release_member(sc, bif, &psref);
   1764 
   1765 	/*
   1766 	 * Before enqueueing this packet to the destination interface,
   1767 	 * clear any in-bound checksum flags to prevent them from being
   1768 	 * misused as out-bound flags.
   1769 	 */
   1770 	m->m_pkthdr.csum_flags = 0;
   1771 
   1772 	ACQUIRE_GLOBAL_LOCKS();
   1773 	bridge_enqueue(sc, dst_if, m, 1);
   1774 	RELEASE_GLOBAL_LOCKS();
   1775 out:
   1776 	if (src_if != NULL)
   1777 		m_put_rcvif_psref(src_if, &psref_src);
   1778 	return;
   1779 }
   1780 
   1781 static bool
   1782 bstp_state_before_learning(struct bridge_iflist *bif)
   1783 {
   1784 	if (bif->bif_flags & IFBIF_STP) {
   1785 		switch (bif->bif_state) {
   1786 		case BSTP_IFSTATE_BLOCKING:
   1787 		case BSTP_IFSTATE_LISTENING:
   1788 		case BSTP_IFSTATE_DISABLED:
   1789 			return true;
   1790 		}
   1791 	}
   1792 	return false;
   1793 }
   1794 
   1795 static bool
   1796 bridge_ourether(struct bridge_iflist *bif, struct ether_header *eh, int src)
   1797 {
   1798 	uint8_t *ether = src ? eh->ether_shost : eh->ether_dhost;
   1799 
   1800 	if (memcmp(CLLADDR(bif->bif_ifp->if_sadl), ether, ETHER_ADDR_LEN) == 0
   1801 #if NCARP > 0
   1802 	    || (bif->bif_ifp->if_carp &&
   1803 	        carp_ourether(bif->bif_ifp->if_carp, eh, IFT_ETHER, src) != NULL)
   1804 #endif /* NCARP > 0 */
   1805 	    )
   1806 		return true;
   1807 
   1808 	return false;
   1809 }
   1810 
   1811 /*
   1812  * bridge_input:
   1813  *
   1814  *	Receive input from a member interface.  Queue the packet for
   1815  *	bridging if it is not for us.
   1816  */
   1817 static void
   1818 bridge_input(struct ifnet *ifp, struct mbuf *m)
   1819 {
   1820 	struct bridge_softc *sc = ifp->if_bridge;
   1821 	struct bridge_iflist *bif;
   1822 	struct ether_header *eh;
   1823 	struct psref psref;
   1824 	int bound;
   1825 	DECLARE_LOCK_VARIABLE;
   1826 
   1827 	KASSERT(!cpu_intr_p());
   1828 
   1829 	if (__predict_false(sc == NULL) ||
   1830 	    (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   1831 		ACQUIRE_GLOBAL_LOCKS();
   1832 		ether_input(ifp, m);
   1833 		RELEASE_GLOBAL_LOCKS();
   1834 		return;
   1835 	}
   1836 
   1837 	bound = curlwp_bind();
   1838 	bif = bridge_lookup_member_if(sc, ifp, &psref);
   1839 	if (bif == NULL) {
   1840 		curlwp_bindx(bound);
   1841 		ACQUIRE_GLOBAL_LOCKS();
   1842 		ether_input(ifp, m);
   1843 		RELEASE_GLOBAL_LOCKS();
   1844 		return;
   1845 	}
   1846 
   1847 	eh = mtod(m, struct ether_header *);
   1848 
   1849 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
   1850 		if (memcmp(etherbroadcastaddr,
   1851 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
   1852 			m->m_flags |= M_BCAST;
   1853 		else
   1854 			m->m_flags |= M_MCAST;
   1855 	}
   1856 
   1857 	/*
   1858 	 * A 'fast' path for packets addressed to interfaces that are
   1859 	 * part of this bridge.
   1860 	 */
   1861 	if (!(m->m_flags & (M_BCAST|M_MCAST)) &&
   1862 	    !bstp_state_before_learning(bif)) {
   1863 		struct bridge_iflist *_bif;
   1864 		struct ifnet *_ifp = NULL;
   1865 		int s;
   1866 		struct psref _psref;
   1867 
   1868 		BRIDGE_PSZ_RENTER(s);
   1869 		BRIDGE_IFLIST_READER_FOREACH(_bif, sc) {
   1870 			/* It is destined for us. */
   1871 			if (bridge_ourether(_bif, eh, 0)) {
   1872 				bridge_acquire_member(sc, _bif, &_psref);
   1873 				BRIDGE_PSZ_REXIT(s);
   1874 				if (_bif->bif_flags & IFBIF_LEARNING)
   1875 					(void) bridge_rtupdate(sc,
   1876 					    eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
   1877 				m_set_rcvif(m, _bif->bif_ifp);
   1878 				_ifp = _bif->bif_ifp;
   1879 				bridge_release_member(sc, _bif, &_psref);
   1880 				goto out;
   1881 			}
   1882 
   1883 			/* We just received a packet that we sent out. */
   1884 			if (bridge_ourether(_bif, eh, 1))
   1885 				break;
   1886 		}
   1887 		BRIDGE_PSZ_REXIT(s);
   1888 out:
   1889 
   1890 		if (_bif != NULL) {
   1891 			bridge_release_member(sc, bif, &psref);
   1892 			curlwp_bindx(bound);
   1893 			if (_ifp != NULL) {
   1894 				m->m_flags &= ~M_PROMISC;
   1895 				ACQUIRE_GLOBAL_LOCKS();
   1896 				ether_input(_ifp, m);
   1897 				RELEASE_GLOBAL_LOCKS();
   1898 			} else
   1899 				m_freem(m);
   1900 			return;
   1901 		}
   1902 	}
   1903 
   1904 	/* Tap off 802.1D packets; they do not get forwarded. */
   1905 	if (bif->bif_flags & IFBIF_STP &&
   1906 	    memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0) {
   1907 		bstp_input(sc, bif, m);
   1908 		bridge_release_member(sc, bif, &psref);
   1909 		curlwp_bindx(bound);
   1910 		return;
   1911 	}
   1912 
   1913 	/*
   1914 	 * A normal switch would discard the packet here, but that's not what
   1915 	 * we've done historically. This also prevents some obnoxious behaviour.
   1916 	 */
   1917 	if (bstp_state_before_learning(bif)) {
   1918 		bridge_release_member(sc, bif, &psref);
   1919 		curlwp_bindx(bound);
   1920 		ACQUIRE_GLOBAL_LOCKS();
   1921 		ether_input(ifp, m);
   1922 		RELEASE_GLOBAL_LOCKS();
   1923 		return;
   1924 	}
   1925 
   1926 	bridge_release_member(sc, bif, &psref);
   1927 
   1928 	bridge_forward(sc, m);
   1929 
   1930 	curlwp_bindx(bound);
   1931 }
   1932 
   1933 /*
   1934  * bridge_broadcast:
   1935  *
   1936  *	Send a frame to all interfaces that are members of
   1937  *	the bridge, except for the one on which the packet
   1938  *	arrived.
   1939  */
   1940 static void
   1941 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
   1942     struct mbuf *m)
   1943 {
   1944 	struct bridge_iflist *bif;
   1945 	struct mbuf *mc;
   1946 	struct ifnet *dst_if;
   1947 	bool bmcast;
   1948 	int s;
   1949 	DECLARE_LOCK_VARIABLE;
   1950 
   1951 	bmcast = m->m_flags & (M_BCAST|M_MCAST);
   1952 
   1953 	BRIDGE_PSZ_RENTER(s);
   1954 	BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
   1955 		struct psref psref;
   1956 
   1957 		bridge_acquire_member(sc, bif, &psref);
   1958 		BRIDGE_PSZ_REXIT(s);
   1959 
   1960 		dst_if = bif->bif_ifp;
   1961 
   1962 		if (bif->bif_flags & IFBIF_STP) {
   1963 			switch (bif->bif_state) {
   1964 			case BSTP_IFSTATE_BLOCKING:
   1965 			case BSTP_IFSTATE_DISABLED:
   1966 				goto next;
   1967 			}
   1968 		}
   1969 
   1970 		if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && !bmcast)
   1971 			goto next;
   1972 
   1973 		if ((dst_if->if_flags & IFF_RUNNING) == 0)
   1974 			goto next;
   1975 
   1976 		if (dst_if != src_if) {
   1977 			mc = m_copypacket(m, M_DONTWAIT);
   1978 			if (mc == NULL) {
   1979 				sc->sc_if.if_oerrors++;
   1980 				goto next;
   1981 			}
   1982 			/*
   1983 			 * Before enqueueing this packet to the destination
   1984 			 * interface, clear any in-bound checksum flags to
   1985 			 * prevent them from being misused as out-bound flags.
   1986 			 */
   1987 			mc->m_pkthdr.csum_flags = 0;
   1988 
   1989 			ACQUIRE_GLOBAL_LOCKS();
   1990 			bridge_enqueue(sc, dst_if, mc, 1);
   1991 			RELEASE_GLOBAL_LOCKS();
   1992 		}
   1993 
   1994 		if (bmcast) {
   1995 			mc = m_copypacket(m, M_DONTWAIT);
   1996 			if (mc == NULL) {
   1997 				sc->sc_if.if_oerrors++;
   1998 				goto next;
   1999 			}
   2000 
   2001 			m_set_rcvif(mc, dst_if);
   2002 			mc->m_flags &= ~M_PROMISC;
   2003 
   2004 			ACQUIRE_GLOBAL_LOCKS();
   2005 			ether_input(dst_if, mc);
   2006 			RELEASE_GLOBAL_LOCKS();
   2007 		}
   2008 next:
   2009 		BRIDGE_PSZ_RENTER(s);
   2010 		bridge_release_member(sc, bif, &psref);
   2011 	}
   2012 	BRIDGE_PSZ_REXIT(s);
   2013 
   2014 	m_freem(m);
   2015 }
   2016 
   2017 static int
   2018 bridge_rtalloc(struct bridge_softc *sc, const uint8_t *dst,
   2019     struct bridge_rtnode **brtp)
   2020 {
   2021 	struct bridge_rtnode *brt;
   2022 	int error;
   2023 
   2024 	if (sc->sc_brtcnt >= sc->sc_brtmax)
   2025 		return ENOSPC;
   2026 
   2027 	/*
   2028 	 * Allocate a new bridge forwarding node, and
   2029 	 * initialize the expiration time and Ethernet
   2030 	 * address.
   2031 	 */
   2032 	brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
   2033 	if (brt == NULL)
   2034 		return ENOMEM;
   2035 
   2036 	memset(brt, 0, sizeof(*brt));
   2037 	brt->brt_expire = time_uptime + sc->sc_brttimeout;
   2038 	brt->brt_flags = IFBAF_DYNAMIC;
   2039 	memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
   2040 	PSLIST_ENTRY_INIT(brt, brt_list);
   2041 	PSLIST_ENTRY_INIT(brt, brt_hash);
   2042 
   2043 	BRIDGE_RT_LOCK(sc);
   2044 	error = bridge_rtnode_insert(sc, brt);
   2045 	BRIDGE_RT_UNLOCK(sc);
   2046 
   2047 	if (error != 0) {
   2048 		pool_put(&bridge_rtnode_pool, brt);
   2049 		return error;
   2050 	}
   2051 
   2052 	*brtp = brt;
   2053 	return 0;
   2054 }
   2055 
   2056 /*
   2057  * bridge_rtupdate:
   2058  *
   2059  *	Add a bridge routing entry.
   2060  */
   2061 static int
   2062 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
   2063     struct ifnet *dst_if, int setflags, uint8_t flags)
   2064 {
   2065 	struct bridge_rtnode *brt;
   2066 	int s;
   2067 
   2068 again:
   2069 	/*
   2070 	 * A route for this destination might already exist.  If so,
   2071 	 * update it, otherwise create a new one.
   2072 	 */
   2073 	BRIDGE_RT_RENTER(s);
   2074 	brt = bridge_rtnode_lookup(sc, dst);
   2075 
   2076 	if (brt != NULL) {
   2077 		brt->brt_ifp = dst_if;
   2078 		if (setflags) {
   2079 			brt->brt_flags = flags;
   2080 			if (flags & IFBAF_STATIC)
   2081 				brt->brt_expire = 0;
   2082 			else
   2083 				brt->brt_expire = time_uptime + sc->sc_brttimeout;
   2084 		} else {
   2085 			if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
   2086 				brt->brt_expire = time_uptime + sc->sc_brttimeout;
   2087 		}
   2088 	}
   2089 	BRIDGE_RT_REXIT(s);
   2090 
   2091 	if (brt == NULL) {
   2092 		int r;
   2093 
   2094 		r = bridge_rtalloc(sc, dst, &brt);
   2095 		if (r != 0)
   2096 			return r;
   2097 		goto again;
   2098 	}
   2099 
   2100 	return 0;
   2101 }
   2102 
   2103 /*
   2104  * bridge_rtlookup:
   2105  *
   2106  *	Lookup the destination interface for an address.
   2107  */
   2108 static struct ifnet *
   2109 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
   2110 {
   2111 	struct bridge_rtnode *brt;
   2112 	struct ifnet *ifs = NULL;
   2113 	int s;
   2114 
   2115 	BRIDGE_RT_RENTER(s);
   2116 	brt = bridge_rtnode_lookup(sc, addr);
   2117 	if (brt != NULL)
   2118 		ifs = brt->brt_ifp;
   2119 	BRIDGE_RT_REXIT(s);
   2120 
   2121 	return ifs;
   2122 }
   2123 
   2124 typedef bool (*bridge_iterate_cb_t)
   2125     (struct bridge_softc *, struct bridge_rtnode *, bool *, void *);
   2126 
   2127 /*
   2128  * bridge_rtlist_iterate_remove:
   2129  *
   2130  *	It iterates on sc->sc_rtlist and removes rtnodes of it which func
   2131  *	callback judges to remove. Removals of rtnodes are done in a manner
   2132  *	of pserialize. To this end, all kmem_* operations are placed out of
   2133  *	mutexes.
   2134  */
   2135 static void
   2136 bridge_rtlist_iterate_remove(struct bridge_softc *sc, bridge_iterate_cb_t func, void *arg)
   2137 {
   2138 	struct bridge_rtnode *brt;
   2139 	struct bridge_rtnode **brt_list;
   2140 	int i, count;
   2141 
   2142 retry:
   2143 	count = sc->sc_brtcnt;
   2144 	if (count == 0)
   2145 		return;
   2146 	brt_list = kmem_alloc(sizeof(*brt_list) * count, KM_SLEEP);
   2147 
   2148 	BRIDGE_RT_LOCK(sc);
   2149 	if (__predict_false(sc->sc_brtcnt > count)) {
   2150 		/* The rtnodes increased, we need more memory */
   2151 		BRIDGE_RT_UNLOCK(sc);
   2152 		kmem_free(brt_list, sizeof(*brt_list) * count);
   2153 		goto retry;
   2154 	}
   2155 
   2156 	i = 0;
   2157 	/*
   2158 	 * We don't need to use a _SAFE variant here because we know
   2159 	 * that a removed item keeps its next pointer as-is thanks to
   2160 	 * pslist(9) and isn't freed in the loop.
   2161 	 */
   2162 	BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) {
   2163 		bool need_break = false;
   2164 		if (func(sc, brt, &need_break, arg)) {
   2165 			bridge_rtnode_remove(sc, brt);
   2166 			brt_list[i++] = brt;
   2167 		}
   2168 		if (need_break)
   2169 			break;
   2170 	}
   2171 
   2172 	if (i > 0)
   2173 		BRIDGE_RT_PSZ_PERFORM(sc);
   2174 	BRIDGE_RT_UNLOCK(sc);
   2175 
   2176 	while (--i >= 0)
   2177 		bridge_rtnode_destroy(brt_list[i]);
   2178 
   2179 	kmem_free(brt_list, sizeof(*brt_list) * count);
   2180 }
   2181 
   2182 static bool
   2183 bridge_rttrim0_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
   2184     bool *need_break, void *arg)
   2185 {
   2186 	if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
   2187 		/* Take into account of the subsequent removal */
   2188 		if ((sc->sc_brtcnt - 1) <= sc->sc_brtmax)
   2189 			*need_break = true;
   2190 		return true;
   2191 	} else
   2192 		return false;
   2193 }
   2194 
   2195 static void
   2196 bridge_rttrim0(struct bridge_softc *sc)
   2197 {
   2198 	bridge_rtlist_iterate_remove(sc, bridge_rttrim0_cb, NULL);
   2199 }
   2200 
   2201 /*
   2202  * bridge_rttrim:
   2203  *
   2204  *	Trim the routine table so that we have a number
   2205  *	of routing entries less than or equal to the
   2206  *	maximum number.
   2207  */
   2208 static void
   2209 bridge_rttrim(struct bridge_softc *sc)
   2210 {
   2211 
   2212 	/* Make sure we actually need to do this. */
   2213 	if (sc->sc_brtcnt <= sc->sc_brtmax)
   2214 		return;
   2215 
   2216 	/* Force an aging cycle; this might trim enough addresses. */
   2217 	bridge_rtage(sc);
   2218 	if (sc->sc_brtcnt <= sc->sc_brtmax)
   2219 		return;
   2220 
   2221 	bridge_rttrim0(sc);
   2222 
   2223 	return;
   2224 }
   2225 
   2226 /*
   2227  * bridge_timer:
   2228  *
   2229  *	Aging timer for the bridge.
   2230  */
   2231 static void
   2232 bridge_timer(void *arg)
   2233 {
   2234 	struct bridge_softc *sc = arg;
   2235 
   2236 	workqueue_enqueue(sc->sc_rtage_wq, &sc->sc_rtage_wk, NULL);
   2237 }
   2238 
   2239 static void
   2240 bridge_rtage_work(struct work *wk, void *arg)
   2241 {
   2242 	struct bridge_softc *sc = arg;
   2243 
   2244 	KASSERT(wk == &sc->sc_rtage_wk);
   2245 
   2246 	bridge_rtage(sc);
   2247 
   2248 	if (sc->sc_if.if_flags & IFF_RUNNING)
   2249 		callout_reset(&sc->sc_brcallout,
   2250 		    bridge_rtable_prune_period * hz, bridge_timer, sc);
   2251 }
   2252 
   2253 static bool
   2254 bridge_rtage_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
   2255     bool *need_break, void *arg)
   2256 {
   2257 	if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
   2258 	    time_uptime >= brt->brt_expire)
   2259 		return true;
   2260 	else
   2261 		return false;
   2262 }
   2263 
   2264 /*
   2265  * bridge_rtage:
   2266  *
   2267  *	Perform an aging cycle.
   2268  */
   2269 static void
   2270 bridge_rtage(struct bridge_softc *sc)
   2271 {
   2272 	bridge_rtlist_iterate_remove(sc, bridge_rtage_cb, NULL);
   2273 }
   2274 
   2275 
   2276 static bool
   2277 bridge_rtflush_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
   2278     bool *need_break, void *arg)
   2279 {
   2280 	int full = *(int*)arg;
   2281 
   2282 	if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
   2283 		return true;
   2284 	else
   2285 		return false;
   2286 }
   2287 
   2288 /*
   2289  * bridge_rtflush:
   2290  *
   2291  *	Remove all dynamic addresses from the bridge.
   2292  */
   2293 static void
   2294 bridge_rtflush(struct bridge_softc *sc, int full)
   2295 {
   2296 	bridge_rtlist_iterate_remove(sc, bridge_rtflush_cb, &full);
   2297 }
   2298 
   2299 /*
   2300  * bridge_rtdaddr:
   2301  *
   2302  *	Remove an address from the table.
   2303  */
   2304 static int
   2305 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
   2306 {
   2307 	struct bridge_rtnode *brt;
   2308 
   2309 	BRIDGE_RT_LOCK(sc);
   2310 	if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) {
   2311 		BRIDGE_RT_UNLOCK(sc);
   2312 		return ENOENT;
   2313 	}
   2314 	bridge_rtnode_remove(sc, brt);
   2315 	BRIDGE_RT_PSZ_PERFORM(sc);
   2316 	BRIDGE_RT_UNLOCK(sc);
   2317 
   2318 	bridge_rtnode_destroy(brt);
   2319 
   2320 	return 0;
   2321 }
   2322 
   2323 /*
   2324  * bridge_rtdelete:
   2325  *
   2326  *	Delete routes to a speicifc member interface.
   2327  */
   2328 static void
   2329 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
   2330 {
   2331 	struct bridge_rtnode *brt;
   2332 
   2333 	/* XXX pserialize_perform for each entry is slow */
   2334 again:
   2335 	BRIDGE_RT_LOCK(sc);
   2336 	BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) {
   2337 		if (brt->brt_ifp == ifp)
   2338 			break;
   2339 	}
   2340 	if (brt == NULL) {
   2341 		BRIDGE_RT_UNLOCK(sc);
   2342 		return;
   2343 	}
   2344 	bridge_rtnode_remove(sc, brt);
   2345 	BRIDGE_RT_PSZ_PERFORM(sc);
   2346 	BRIDGE_RT_UNLOCK(sc);
   2347 
   2348 	bridge_rtnode_destroy(brt);
   2349 
   2350 	goto again;
   2351 }
   2352 
   2353 /*
   2354  * bridge_rtable_init:
   2355  *
   2356  *	Initialize the route table for this bridge.
   2357  */
   2358 static void
   2359 bridge_rtable_init(struct bridge_softc *sc)
   2360 {
   2361 	int i;
   2362 
   2363 	sc->sc_rthash = kmem_alloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
   2364 	    KM_SLEEP);
   2365 
   2366 	for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
   2367 		PSLIST_INIT(&sc->sc_rthash[i]);
   2368 
   2369 	sc->sc_rthash_key = cprng_fast32();
   2370 
   2371 	PSLIST_INIT(&sc->sc_rtlist);
   2372 
   2373 	sc->sc_rtlist_psz = pserialize_create();
   2374 	sc->sc_rtlist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
   2375 }
   2376 
   2377 /*
   2378  * bridge_rtable_fini:
   2379  *
   2380  *	Deconstruct the route table for this bridge.
   2381  */
   2382 static void
   2383 bridge_rtable_fini(struct bridge_softc *sc)
   2384 {
   2385 
   2386 	kmem_free(sc->sc_rthash, sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE);
   2387 	mutex_obj_free(sc->sc_rtlist_lock);
   2388 	pserialize_destroy(sc->sc_rtlist_psz);
   2389 }
   2390 
   2391 /*
   2392  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
   2393  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
   2394  */
   2395 #define	mix(a, b, c)							\
   2396 do {									\
   2397 	a -= b; a -= c; a ^= (c >> 13);					\
   2398 	b -= c; b -= a; b ^= (a << 8);					\
   2399 	c -= a; c -= b; c ^= (b >> 13);					\
   2400 	a -= b; a -= c; a ^= (c >> 12);					\
   2401 	b -= c; b -= a; b ^= (a << 16);					\
   2402 	c -= a; c -= b; c ^= (b >> 5);					\
   2403 	a -= b; a -= c; a ^= (c >> 3);					\
   2404 	b -= c; b -= a; b ^= (a << 10);					\
   2405 	c -= a; c -= b; c ^= (b >> 15);					\
   2406 } while (/*CONSTCOND*/0)
   2407 
   2408 static inline uint32_t
   2409 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
   2410 {
   2411 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
   2412 
   2413 	b += addr[5] << 8;
   2414 	b += addr[4];
   2415 	a += addr[3] << 24;
   2416 	a += addr[2] << 16;
   2417 	a += addr[1] << 8;
   2418 	a += addr[0];
   2419 
   2420 	mix(a, b, c);
   2421 
   2422 	return (c & BRIDGE_RTHASH_MASK);
   2423 }
   2424 
   2425 #undef mix
   2426 
   2427 /*
   2428  * bridge_rtnode_lookup:
   2429  *
   2430  *	Look up a bridge route node for the specified destination.
   2431  */
   2432 static struct bridge_rtnode *
   2433 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
   2434 {
   2435 	struct bridge_rtnode *brt;
   2436 	uint32_t hash;
   2437 	int dir;
   2438 
   2439 	hash = bridge_rthash(sc, addr);
   2440 	BRIDGE_RTHASH_READER_FOREACH(brt, sc, hash) {
   2441 		dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
   2442 		if (dir == 0)
   2443 			return brt;
   2444 		if (dir > 0)
   2445 			return NULL;
   2446 	}
   2447 
   2448 	return NULL;
   2449 }
   2450 
   2451 /*
   2452  * bridge_rtnode_insert:
   2453  *
   2454  *	Insert the specified bridge node into the route table.  We
   2455  *	assume the entry is not already in the table.
   2456  */
   2457 static int
   2458 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
   2459 {
   2460 	struct bridge_rtnode *lbrt, *prev = NULL;
   2461 	uint32_t hash;
   2462 
   2463 	KASSERT(BRIDGE_RT_LOCKED(sc));
   2464 
   2465 	hash = bridge_rthash(sc, brt->brt_addr);
   2466 	BRIDGE_RTHASH_WRITER_FOREACH(lbrt, sc, hash) {
   2467 		int dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
   2468 		if (dir == 0)
   2469 			return EEXIST;
   2470 		if (dir > 0)
   2471 			break;
   2472 		prev = lbrt;
   2473 	}
   2474 	if (prev == NULL)
   2475 		BRIDGE_RTHASH_WRITER_INSERT_HEAD(sc, hash, brt);
   2476 	else
   2477 		BRIDGE_RTHASH_WRITER_INSERT_AFTER(prev, brt);
   2478 
   2479 	BRIDGE_RTLIST_WRITER_INSERT_HEAD(sc, brt);
   2480 	sc->sc_brtcnt++;
   2481 
   2482 	return 0;
   2483 }
   2484 
   2485 /*
   2486  * bridge_rtnode_remove:
   2487  *
   2488  *	Remove a bridge rtnode from the rthash and the rtlist of a bridge.
   2489  */
   2490 static void
   2491 bridge_rtnode_remove(struct bridge_softc *sc, struct bridge_rtnode *brt)
   2492 {
   2493 
   2494 	KASSERT(BRIDGE_RT_LOCKED(sc));
   2495 
   2496 	BRIDGE_RTHASH_WRITER_REMOVE(brt);
   2497 	BRIDGE_RTLIST_WRITER_REMOVE(brt);
   2498 	sc->sc_brtcnt--;
   2499 }
   2500 
   2501 /*
   2502  * bridge_rtnode_destroy:
   2503  *
   2504  *	Destroy a bridge rtnode.
   2505  */
   2506 static void
   2507 bridge_rtnode_destroy(struct bridge_rtnode *brt)
   2508 {
   2509 
   2510 	PSLIST_ENTRY_DESTROY(brt, brt_list);
   2511 	PSLIST_ENTRY_DESTROY(brt, brt_hash);
   2512 	pool_put(&bridge_rtnode_pool, brt);
   2513 }
   2514 
   2515 #if defined(BRIDGE_IPF)
   2516 extern pfil_head_t *inet_pfil_hook;                 /* XXX */
   2517 extern pfil_head_t *inet6_pfil_hook;                /* XXX */
   2518 
   2519 /*
   2520  * Send bridge packets through IPF if they are one of the types IPF can deal
   2521  * with, or if they are ARP or REVARP.  (IPF will pass ARP and REVARP without
   2522  * question.)
   2523  */
   2524 static int
   2525 bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
   2526 {
   2527 	int snap, error;
   2528 	struct ether_header *eh1, eh2;
   2529 	struct llc llc1;
   2530 	uint16_t ether_type;
   2531 
   2532 	snap = 0;
   2533 	error = -1;	/* Default error if not error == 0 */
   2534 	eh1 = mtod(*mp, struct ether_header *);
   2535 	ether_type = ntohs(eh1->ether_type);
   2536 
   2537 	/*
   2538 	 * Check for SNAP/LLC.
   2539 	 */
   2540 	if (ether_type < ETHERMTU) {
   2541 		struct llc *llc2 = (struct llc *)(eh1 + 1);
   2542 
   2543 		if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
   2544 		    llc2->llc_dsap == LLC_SNAP_LSAP &&
   2545 		    llc2->llc_ssap == LLC_SNAP_LSAP &&
   2546 		    llc2->llc_control == LLC_UI) {
   2547 			ether_type = htons(llc2->llc_un.type_snap.ether_type);
   2548 			snap = 1;
   2549 		}
   2550 	}
   2551 
   2552 	/*
   2553 	 * If we're trying to filter bridge traffic, don't look at anything
   2554 	 * other than IP and ARP traffic.  If the filter doesn't understand
   2555 	 * IPv6, don't allow IPv6 through the bridge either.  This is lame
   2556 	 * since if we really wanted, say, an AppleTalk filter, we are hosed,
   2557 	 * but of course we don't have an AppleTalk filter to begin with.
   2558 	 * (Note that since IPF doesn't understand ARP it will pass *ALL*
   2559 	 * ARP traffic.)
   2560 	 */
   2561 	switch (ether_type) {
   2562 		case ETHERTYPE_ARP:
   2563 		case ETHERTYPE_REVARP:
   2564 			return 0; /* Automatically pass */
   2565 		case ETHERTYPE_IP:
   2566 # ifdef INET6
   2567 		case ETHERTYPE_IPV6:
   2568 # endif /* INET6 */
   2569 			break;
   2570 		default:
   2571 			goto bad;
   2572 	}
   2573 
   2574 	/* Strip off the Ethernet header and keep a copy. */
   2575 	m_copydata(*mp, 0, ETHER_HDR_LEN, (void *) &eh2);
   2576 	m_adj(*mp, ETHER_HDR_LEN);
   2577 
   2578 	/* Strip off snap header, if present */
   2579 	if (snap) {
   2580 		m_copydata(*mp, 0, sizeof(struct llc), (void *) &llc1);
   2581 		m_adj(*mp, sizeof(struct llc));
   2582 	}
   2583 
   2584 	/*
   2585 	 * Check basic packet sanity and run IPF through pfil.
   2586 	 */
   2587 	KASSERT(!cpu_intr_p());
   2588 	switch (ether_type)
   2589 	{
   2590 	case ETHERTYPE_IP :
   2591 		error = bridge_ip_checkbasic(mp);
   2592 		if (error == 0)
   2593 			error = pfil_run_hooks(inet_pfil_hook, mp, ifp, dir);
   2594 		break;
   2595 # ifdef INET6
   2596 	case ETHERTYPE_IPV6 :
   2597 		error = bridge_ip6_checkbasic(mp);
   2598 		if (error == 0)
   2599 			error = pfil_run_hooks(inet6_pfil_hook, mp, ifp, dir);
   2600 		break;
   2601 # endif
   2602 	default :
   2603 		error = 0;
   2604 		break;
   2605 	}
   2606 
   2607 	if (*mp == NULL)
   2608 		return error;
   2609 	if (error != 0)
   2610 		goto bad;
   2611 
   2612 	error = -1;
   2613 
   2614 	/*
   2615 	 * Finally, put everything back the way it was and return
   2616 	 */
   2617 	if (snap) {
   2618 		M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
   2619 		if (*mp == NULL)
   2620 			return error;
   2621 		bcopy(&llc1, mtod(*mp, void *), sizeof(struct llc));
   2622 	}
   2623 
   2624 	M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
   2625 	if (*mp == NULL)
   2626 		return error;
   2627 	bcopy(&eh2, mtod(*mp, void *), ETHER_HDR_LEN);
   2628 
   2629 	return 0;
   2630 
   2631     bad:
   2632 	m_freem(*mp);
   2633 	*mp = NULL;
   2634 	return error;
   2635 }
   2636 
   2637 /*
   2638  * Perform basic checks on header size since
   2639  * IPF assumes ip_input has already processed
   2640  * it for it.  Cut-and-pasted from ip_input.c.
   2641  * Given how simple the IPv6 version is,
   2642  * does the IPv4 version really need to be
   2643  * this complicated?
   2644  *
   2645  * XXX Should we update ipstat here, or not?
   2646  * XXX Right now we update ipstat but not
   2647  * XXX csum_counter.
   2648  */
   2649 static int
   2650 bridge_ip_checkbasic(struct mbuf **mp)
   2651 {
   2652 	struct mbuf *m = *mp;
   2653 	struct ip *ip;
   2654 	int len, hlen;
   2655 
   2656 	if (*mp == NULL)
   2657 		return -1;
   2658 
   2659 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
   2660 		if ((m = m_copyup(m, sizeof(struct ip),
   2661 			(max_linkhdr + 3) & ~3)) == NULL) {
   2662 			/* XXXJRT new stat, please */
   2663 			ip_statinc(IP_STAT_TOOSMALL);
   2664 			goto bad;
   2665 		}
   2666 	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
   2667 		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
   2668 			ip_statinc(IP_STAT_TOOSMALL);
   2669 			goto bad;
   2670 		}
   2671 	}
   2672 	ip = mtod(m, struct ip *);
   2673 	if (ip == NULL) goto bad;
   2674 
   2675 	if (ip->ip_v != IPVERSION) {
   2676 		ip_statinc(IP_STAT_BADVERS);
   2677 		goto bad;
   2678 	}
   2679 	hlen = ip->ip_hl << 2;
   2680 	if (hlen < sizeof(struct ip)) { /* minimum header length */
   2681 		ip_statinc(IP_STAT_BADHLEN);
   2682 		goto bad;
   2683 	}
   2684 	if (hlen > m->m_len) {
   2685 		if ((m = m_pullup(m, hlen)) == 0) {
   2686 			ip_statinc(IP_STAT_BADHLEN);
   2687 			goto bad;
   2688 		}
   2689 		ip = mtod(m, struct ip *);
   2690 		if (ip == NULL) goto bad;
   2691 	}
   2692 
   2693 	switch (m->m_pkthdr.csum_flags &
   2694 	        ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_IPv4) |
   2695 	         M_CSUM_IPv4_BAD)) {
   2696 	case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
   2697 		/* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */
   2698 		goto bad;
   2699 
   2700 	case M_CSUM_IPv4:
   2701 		/* Checksum was okay. */
   2702 		/* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */
   2703 		break;
   2704 
   2705 	default:
   2706 		/* Must compute it ourselves. */
   2707 		/* INET_CSUM_COUNTER_INCR(&ip_swcsum); */
   2708 		if (in_cksum(m, hlen) != 0)
   2709 			goto bad;
   2710 		break;
   2711 	}
   2712 
   2713 	/* Retrieve the packet length. */
   2714 	len = ntohs(ip->ip_len);
   2715 
   2716 	/*
   2717 	 * Check for additional length bogosity
   2718 	 */
   2719 	if (len < hlen) {
   2720 		ip_statinc(IP_STAT_BADLEN);
   2721 		goto bad;
   2722 	}
   2723 
   2724 	/*
   2725 	 * Check that the amount of data in the buffers
   2726 	 * is as at least much as the IP header would have us expect.
   2727 	 * Drop packet if shorter than we expect.
   2728 	 */
   2729 	if (m->m_pkthdr.len < len) {
   2730 		ip_statinc(IP_STAT_TOOSHORT);
   2731 		goto bad;
   2732 	}
   2733 
   2734 	/* Checks out, proceed */
   2735 	*mp = m;
   2736 	return 0;
   2737 
   2738     bad:
   2739 	*mp = m;
   2740 	return -1;
   2741 }
   2742 
   2743 # ifdef INET6
   2744 /*
   2745  * Same as above, but for IPv6.
   2746  * Cut-and-pasted from ip6_input.c.
   2747  * XXX Should we update ip6stat, or not?
   2748  */
   2749 static int
   2750 bridge_ip6_checkbasic(struct mbuf **mp)
   2751 {
   2752 	struct mbuf *m = *mp;
   2753 	struct ip6_hdr *ip6;
   2754 
   2755 	/*
   2756 	 * If the IPv6 header is not aligned, slurp it up into a new
   2757 	 * mbuf with space for link headers, in the event we forward
   2758 	 * it.  Otherwise, if it is aligned, make sure the entire base
   2759 	 * IPv6 header is in the first mbuf of the chain.
   2760 	 */
   2761 	if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
   2762 		struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
   2763 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
   2764 		                  (max_linkhdr + 3) & ~3)) == NULL) {
   2765 			/* XXXJRT new stat, please */
   2766 			ip6_statinc(IP6_STAT_TOOSMALL);
   2767 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
   2768 			goto bad;
   2769 		}
   2770 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
   2771 		struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
   2772 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
   2773 			ip6_statinc(IP6_STAT_TOOSMALL);
   2774 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
   2775 			goto bad;
   2776 		}
   2777 	}
   2778 
   2779 	ip6 = mtod(m, struct ip6_hdr *);
   2780 
   2781 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
   2782 		ip6_statinc(IP6_STAT_BADVERS);
   2783 		in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m), ifs6_in_hdrerr);
   2784 		goto bad;
   2785 	}
   2786 
   2787 	/* Checks out, proceed */
   2788 	*mp = m;
   2789 	return 0;
   2790 
   2791     bad:
   2792 	*mp = m;
   2793 	return -1;
   2794 }
   2795 # endif /* INET6 */
   2796 #endif /* BRIDGE_IPF */
   2797