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