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