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