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