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