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