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