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