Home | History | Annotate | Line # | Download | only in net
if_bridge.c revision 1.25
      1 /*	$NetBSD: if_bridge.c,v 1.25 2004/10/05 03:36:45 christos 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.25 2004/10/05 03:36:45 christos 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 #ifdef PFIL_HOOKS
   1127 	if (runfilt) {
   1128 		if (pfil_run_hooks(&sc->sc_if.if_pfil, &m,
   1129 		    dst_ifp, PFIL_OUT) != 0) {
   1130 			if (m != NULL)
   1131 				m_freem(m);
   1132 			return;
   1133 		}
   1134 		if (m == NULL)
   1135 			return;
   1136 	}
   1137 #endif /* PFIL_HOOKS */
   1138 
   1139 #ifdef ALTQ
   1140 	/*
   1141 	 * If ALTQ is enabled on the member interface, do
   1142 	 * classification; the queueing discipline might
   1143 	 * not require classification, but might require
   1144 	 * the address family/header pointer in the pktattr.
   1145 	 */
   1146 	if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
   1147 		/* XXX IFT_ETHER */
   1148 		altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
   1149 	}
   1150 #endif /* ALTQ */
   1151 
   1152 	len = m->m_pkthdr.len;
   1153 	mflags = m->m_flags;
   1154 	IFQ_ENQUEUE(&dst_ifp->if_snd, m, &pktattr, error);
   1155 	if (error) {
   1156 		/* mbuf is already freed */
   1157 		sc->sc_if.if_oerrors++;
   1158 		return;
   1159 	}
   1160 
   1161 	sc->sc_if.if_opackets++;
   1162 	sc->sc_if.if_obytes += len;
   1163 
   1164 	dst_ifp->if_obytes += len;
   1165 
   1166 	if (mflags & M_MCAST) {
   1167 		sc->sc_if.if_omcasts++;
   1168 		dst_ifp->if_omcasts++;
   1169 	}
   1170 
   1171 	if ((dst_ifp->if_flags & IFF_OACTIVE) == 0)
   1172 		(*dst_ifp->if_start)(dst_ifp);
   1173 }
   1174 
   1175 /*
   1176  * bridge_output:
   1177  *
   1178  *	Send output from a bridge member interface.  This
   1179  *	performs the bridging function for locally originated
   1180  *	packets.
   1181  *
   1182  *	The mbuf has the Ethernet header already attached.  We must
   1183  *	enqueue or free the mbuf before returning.
   1184  */
   1185 int
   1186 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
   1187     struct rtentry *rt)
   1188 {
   1189 	struct ether_header *eh;
   1190 	struct ifnet *dst_if;
   1191 	struct bridge_softc *sc;
   1192 	int s;
   1193 
   1194 	if (m->m_len < ETHER_HDR_LEN) {
   1195 		m = m_pullup(m, ETHER_HDR_LEN);
   1196 		if (m == NULL)
   1197 			return (0);
   1198 	}
   1199 
   1200 	eh = mtod(m, struct ether_header *);
   1201 	sc = ifp->if_bridge;
   1202 
   1203 	s = splnet();
   1204 
   1205 	/*
   1206 	 * If bridge is down, but the original output interface is up,
   1207 	 * go ahead and send out that interface.  Otherwise, the packet
   1208 	 * is dropped below.
   1209 	 */
   1210 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   1211 		dst_if = ifp;
   1212 		goto sendunicast;
   1213 	}
   1214 
   1215 	/*
   1216 	 * If the packet is a multicast, or we don't know a better way to
   1217 	 * get there, send to all interfaces.
   1218 	 */
   1219 	if (ETHER_IS_MULTICAST(eh->ether_dhost))
   1220 		dst_if = NULL;
   1221 	else
   1222 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
   1223 	if (dst_if == NULL) {
   1224 		struct bridge_iflist *bif;
   1225 		struct mbuf *mc;
   1226 		int used = 0;
   1227 
   1228 		LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
   1229 			dst_if = bif->bif_ifp;
   1230 			if ((dst_if->if_flags & IFF_RUNNING) == 0)
   1231 				continue;
   1232 
   1233 			/*
   1234 			 * If this is not the original output interface,
   1235 			 * and the interface is participating in spanning
   1236 			 * tree, make sure the port is in a state that
   1237 			 * allows forwarding.
   1238 			 */
   1239 			if (dst_if != ifp &&
   1240 			    (bif->bif_flags & IFBIF_STP) != 0) {
   1241 				switch (bif->bif_state) {
   1242 				case BSTP_IFSTATE_BLOCKING:
   1243 				case BSTP_IFSTATE_LISTENING:
   1244 				case BSTP_IFSTATE_DISABLED:
   1245 					continue;
   1246 				}
   1247 			}
   1248 
   1249 			if (LIST_NEXT(bif, bif_next) == NULL) {
   1250 				used = 1;
   1251 				mc = m;
   1252 			} else {
   1253 				mc = m_copym(m, 0, M_COPYALL, M_NOWAIT);
   1254 				if (mc == NULL) {
   1255 					sc->sc_if.if_oerrors++;
   1256 					continue;
   1257 				}
   1258 			}
   1259 
   1260 			bridge_enqueue(sc, dst_if, mc, 0);
   1261 		}
   1262 		if (used == 0)
   1263 			m_freem(m);
   1264 		splx(s);
   1265 		return (0);
   1266 	}
   1267 
   1268  sendunicast:
   1269 	/*
   1270 	 * XXX Spanning tree consideration here?
   1271 	 */
   1272 
   1273 	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
   1274 		m_freem(m);
   1275 		splx(s);
   1276 		return (0);
   1277 	}
   1278 
   1279 	bridge_enqueue(sc, dst_if, m, 0);
   1280 
   1281 	splx(s);
   1282 	return (0);
   1283 }
   1284 
   1285 /*
   1286  * bridge_start:
   1287  *
   1288  *	Start output on a bridge.
   1289  *
   1290  *	NOTE: This routine should never be called in this implementation.
   1291  */
   1292 void
   1293 bridge_start(struct ifnet *ifp)
   1294 {
   1295 
   1296 	printf("%s: bridge_start() called\n", ifp->if_xname);
   1297 }
   1298 
   1299 /*
   1300  * bridge_forward:
   1301  *
   1302  *	The forwarding function of the bridge.
   1303  */
   1304 void
   1305 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
   1306 {
   1307 	struct bridge_iflist *bif;
   1308 	struct ifnet *src_if, *dst_if;
   1309 	struct ether_header *eh;
   1310 
   1311 	src_if = m->m_pkthdr.rcvif;
   1312 
   1313 	sc->sc_if.if_ipackets++;
   1314 	sc->sc_if.if_ibytes += m->m_pkthdr.len;
   1315 
   1316 	/*
   1317 	 * Look up the bridge_iflist.
   1318 	 */
   1319 	bif = bridge_lookup_member_if(sc, src_if);
   1320 	if (bif == NULL) {
   1321 		/* Interface is not a bridge member (anymore?) */
   1322 		m_freem(m);
   1323 		return;
   1324 	}
   1325 
   1326 	if (bif->bif_flags & IFBIF_STP) {
   1327 		switch (bif->bif_state) {
   1328 		case BSTP_IFSTATE_BLOCKING:
   1329 		case BSTP_IFSTATE_LISTENING:
   1330 		case BSTP_IFSTATE_DISABLED:
   1331 			m_freem(m);
   1332 			return;
   1333 		}
   1334 	}
   1335 
   1336 	eh = mtod(m, struct ether_header *);
   1337 
   1338 	/*
   1339 	 * If the interface is learning, and the source
   1340 	 * address is valid and not multicast, record
   1341 	 * the address.
   1342 	 */
   1343 	if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
   1344 	    ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
   1345 	    (eh->ether_shost[0] == 0 &&
   1346 	     eh->ether_shost[1] == 0 &&
   1347 	     eh->ether_shost[2] == 0 &&
   1348 	     eh->ether_shost[3] == 0 &&
   1349 	     eh->ether_shost[4] == 0 &&
   1350 	     eh->ether_shost[5] == 0) == 0) {
   1351 		(void) bridge_rtupdate(sc, eh->ether_shost,
   1352 		    src_if, 0, IFBAF_DYNAMIC);
   1353 	}
   1354 
   1355 	if ((bif->bif_flags & IFBIF_STP) != 0 &&
   1356 	    bif->bif_state == BSTP_IFSTATE_LEARNING) {
   1357 		m_freem(m);
   1358 		return;
   1359 	}
   1360 
   1361 	/*
   1362 	 * At this point, the port either doesn't participate
   1363 	 * in spanning tree or it is in the forwarding state.
   1364 	 */
   1365 
   1366 	/*
   1367 	 * If the packet is unicast, destined for someone on
   1368 	 * "this" side of the bridge, drop it.
   1369 	 */
   1370 	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
   1371 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
   1372 		if (src_if == dst_if) {
   1373 			m_freem(m);
   1374 			return;
   1375 		}
   1376 	} else {
   1377 		/* ...forward it to all interfaces. */
   1378 		sc->sc_if.if_imcasts++;
   1379 		dst_if = NULL;
   1380 	}
   1381 
   1382 #ifdef PFIL_HOOKS
   1383 	if (pfil_run_hooks(&sc->sc_if.if_pfil, &m,
   1384 	    m->m_pkthdr.rcvif, PFIL_IN) != 0) {
   1385 		if (m != NULL)
   1386 			m_freem(m);
   1387 		return;
   1388 	}
   1389 	if (m == NULL)
   1390 		return;
   1391 #endif /* PFIL_HOOKS */
   1392 
   1393 	if (dst_if == NULL) {
   1394 		bridge_broadcast(sc, src_if, m);
   1395 		return;
   1396 	}
   1397 
   1398 	/*
   1399 	 * At this point, we're dealing with a unicast frame
   1400 	 * going to a different interface.
   1401 	 */
   1402 	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
   1403 		m_freem(m);
   1404 		return;
   1405 	}
   1406 	bif = bridge_lookup_member_if(sc, dst_if);
   1407 	if (bif == NULL) {
   1408 		/* Not a member of the bridge (anymore?) */
   1409 		m_freem(m);
   1410 		return;
   1411 	}
   1412 
   1413 	if (bif->bif_flags & IFBIF_STP) {
   1414 		switch (bif->bif_state) {
   1415 		case BSTP_IFSTATE_DISABLED:
   1416 		case BSTP_IFSTATE_BLOCKING:
   1417 			m_freem(m);
   1418 			return;
   1419 		}
   1420 	}
   1421 
   1422 	bridge_enqueue(sc, dst_if, m, 1);
   1423 }
   1424 
   1425 /*
   1426  * bridge_input:
   1427  *
   1428  *	Receive input from a member interface.  Queue the packet for
   1429  *	bridging if it is not for us.
   1430  */
   1431 struct mbuf *
   1432 bridge_input(struct ifnet *ifp, struct mbuf *m)
   1433 {
   1434 	struct bridge_softc *sc = ifp->if_bridge;
   1435 	struct bridge_iflist *bif;
   1436 	struct ether_header *eh;
   1437 	struct mbuf *mc;
   1438 
   1439 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   1440 		return (m);
   1441 
   1442 	bif = bridge_lookup_member_if(sc, ifp);
   1443 	if (bif == NULL)
   1444 		return (m);
   1445 
   1446 	eh = mtod(m, struct ether_header *);
   1447 
   1448 	if (m->m_flags & (M_BCAST|M_MCAST)) {
   1449 		/* Tap off 802.1D packets; they do not get forwarded. */
   1450 		if (memcmp(eh->ether_dhost, bstp_etheraddr,
   1451 		    ETHER_ADDR_LEN) == 0) {
   1452 			m = bstp_input(ifp, m);
   1453 			if (m == NULL)
   1454 				return (NULL);
   1455 		}
   1456 
   1457 		if (bif->bif_flags & IFBIF_STP) {
   1458 			switch (bif->bif_state) {
   1459 			case BSTP_IFSTATE_BLOCKING:
   1460 			case BSTP_IFSTATE_LISTENING:
   1461 			case BSTP_IFSTATE_DISABLED:
   1462 				return (m);
   1463 			}
   1464 		}
   1465 
   1466 		/*
   1467 		 * Make a deep copy of the packet and enqueue the copy
   1468 		 * for bridge processing; return the original packet for
   1469 		 * local processing.
   1470 		 */
   1471 		mc = m_dup(m, 0, M_COPYALL, M_NOWAIT);
   1472 		if (mc == NULL)
   1473 			return (m);
   1474 
   1475 		/* Perform the bridge forwarding function with the copy. */
   1476 		bridge_forward(sc, mc);
   1477 
   1478 		/* Return the original packet for local processing. */
   1479 		return (m);
   1480 	}
   1481 
   1482 	if (bif->bif_flags & IFBIF_STP) {
   1483 		switch (bif->bif_state) {
   1484 		case BSTP_IFSTATE_BLOCKING:
   1485 		case BSTP_IFSTATE_LISTENING:
   1486 		case BSTP_IFSTATE_DISABLED:
   1487 			return (m);
   1488 		}
   1489 	}
   1490 
   1491 	/*
   1492 	 * Unicast.  Make sure it's not for us.
   1493 	 */
   1494 	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
   1495 		/* It is destined for us. */
   1496 		if (memcmp(LLADDR(bif->bif_ifp->if_sadl), eh->ether_dhost,
   1497 		    ETHER_ADDR_LEN) == 0) {
   1498 			if (bif->bif_flags & IFBIF_LEARNING)
   1499 				(void) bridge_rtupdate(sc,
   1500 				    eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
   1501 			m->m_pkthdr.rcvif = bif->bif_ifp;
   1502 			return (m);
   1503 		}
   1504 
   1505 		/* We just received a packet that we sent out. */
   1506 		if (memcmp(LLADDR(bif->bif_ifp->if_sadl), eh->ether_shost,
   1507 		    ETHER_ADDR_LEN) == 0) {
   1508 			m_freem(m);
   1509 			return (NULL);
   1510 		}
   1511 	}
   1512 
   1513 	/* Perform the bridge forwarding function. */
   1514 	bridge_forward(sc, m);
   1515 
   1516 	return (NULL);
   1517 }
   1518 
   1519 /*
   1520  * bridge_broadcast:
   1521  *
   1522  *	Send a frame to all interfaces that are members of
   1523  *	the bridge, except for the one on which the packet
   1524  *	arrived.
   1525  */
   1526 void
   1527 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
   1528     struct mbuf *m)
   1529 {
   1530 	struct bridge_iflist *bif;
   1531 	struct mbuf *mc;
   1532 	struct ifnet *dst_if;
   1533 	int used = 0;
   1534 
   1535 	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
   1536 		dst_if = bif->bif_ifp;
   1537 		if (dst_if == src_if)
   1538 			continue;
   1539 
   1540 		if (bif->bif_flags & IFBIF_STP) {
   1541 			switch (bif->bif_state) {
   1542 			case BSTP_IFSTATE_BLOCKING:
   1543 			case BSTP_IFSTATE_DISABLED:
   1544 				continue;
   1545 			}
   1546 		}
   1547 
   1548 		if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
   1549 		    (m->m_flags & (M_BCAST|M_MCAST)) == 0)
   1550 			continue;
   1551 
   1552 		if ((dst_if->if_flags & IFF_RUNNING) == 0)
   1553 			continue;
   1554 
   1555 		if (LIST_NEXT(bif, bif_next) == NULL) {
   1556 			mc = m;
   1557 			used = 1;
   1558 		} else {
   1559 			mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
   1560 			if (mc == NULL) {
   1561 				sc->sc_if.if_oerrors++;
   1562 				continue;
   1563 			}
   1564 		}
   1565 
   1566 		bridge_enqueue(sc, dst_if, mc, 1);
   1567 	}
   1568 	if (used == 0)
   1569 		m_freem(m);
   1570 }
   1571 
   1572 /*
   1573  * bridge_rtupdate:
   1574  *
   1575  *	Add a bridge routing entry.
   1576  */
   1577 int
   1578 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
   1579     struct ifnet *dst_if, int setflags, uint8_t flags)
   1580 {
   1581 	struct bridge_rtnode *brt;
   1582 	int error;
   1583 
   1584 	/*
   1585 	 * A route for this destination might already exist.  If so,
   1586 	 * update it, otherwise create a new one.
   1587 	 */
   1588 	if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
   1589 		if (sc->sc_brtcnt >= sc->sc_brtmax)
   1590 			return (ENOSPC);
   1591 
   1592 		/*
   1593 		 * Allocate a new bridge forwarding node, and
   1594 		 * initialize the expiration time and Ethernet
   1595 		 * address.
   1596 		 */
   1597 		brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
   1598 		if (brt == NULL)
   1599 			return (ENOMEM);
   1600 
   1601 		memset(brt, 0, sizeof(*brt));
   1602 		brt->brt_expire = mono_time.tv_sec + sc->sc_brttimeout;
   1603 		brt->brt_flags = IFBAF_DYNAMIC;
   1604 		memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
   1605 
   1606 		if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
   1607 			pool_put(&bridge_rtnode_pool, brt);
   1608 			return (error);
   1609 		}
   1610 	}
   1611 
   1612 	brt->brt_ifp = dst_if;
   1613 	if (setflags) {
   1614 		brt->brt_flags = flags;
   1615 		brt->brt_expire = (flags & IFBAF_STATIC) ? 0 :
   1616 		    mono_time.tv_sec + sc->sc_brttimeout;
   1617 	}
   1618 
   1619 	return (0);
   1620 }
   1621 
   1622 /*
   1623  * bridge_rtlookup:
   1624  *
   1625  *	Lookup the destination interface for an address.
   1626  */
   1627 struct ifnet *
   1628 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
   1629 {
   1630 	struct bridge_rtnode *brt;
   1631 
   1632 	if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
   1633 		return (NULL);
   1634 
   1635 	return (brt->brt_ifp);
   1636 }
   1637 
   1638 /*
   1639  * bridge_rttrim:
   1640  *
   1641  *	Trim the routine table so that we have a number
   1642  *	of routing entries less than or equal to the
   1643  *	maximum number.
   1644  */
   1645 void
   1646 bridge_rttrim(struct bridge_softc *sc)
   1647 {
   1648 	struct bridge_rtnode *brt, *nbrt;
   1649 
   1650 	/* Make sure we actually need to do this. */
   1651 	if (sc->sc_brtcnt <= sc->sc_brtmax)
   1652 		return;
   1653 
   1654 	/* Force an aging cycle; this might trim enough addresses. */
   1655 	bridge_rtage(sc);
   1656 	if (sc->sc_brtcnt <= sc->sc_brtmax)
   1657 		return;
   1658 
   1659 	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
   1660 		nbrt = LIST_NEXT(brt, brt_list);
   1661 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
   1662 			bridge_rtnode_destroy(sc, brt);
   1663 			if (sc->sc_brtcnt <= sc->sc_brtmax)
   1664 				return;
   1665 		}
   1666 	}
   1667 }
   1668 
   1669 /*
   1670  * bridge_timer:
   1671  *
   1672  *	Aging timer for the bridge.
   1673  */
   1674 void
   1675 bridge_timer(void *arg)
   1676 {
   1677 	struct bridge_softc *sc = arg;
   1678 	int s;
   1679 
   1680 	s = splnet();
   1681 	bridge_rtage(sc);
   1682 	splx(s);
   1683 
   1684 	if (sc->sc_if.if_flags & IFF_RUNNING)
   1685 		callout_reset(&sc->sc_brcallout,
   1686 		    bridge_rtable_prune_period * hz, bridge_timer, sc);
   1687 }
   1688 
   1689 /*
   1690  * bridge_rtage:
   1691  *
   1692  *	Perform an aging cycle.
   1693  */
   1694 void
   1695 bridge_rtage(struct bridge_softc *sc)
   1696 {
   1697 	struct bridge_rtnode *brt, *nbrt;
   1698 
   1699 	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
   1700 		nbrt = LIST_NEXT(brt, brt_list);
   1701 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
   1702 			if (mono_time.tv_sec >= brt->brt_expire)
   1703 				bridge_rtnode_destroy(sc, brt);
   1704 		}
   1705 	}
   1706 }
   1707 
   1708 /*
   1709  * bridge_rtflush:
   1710  *
   1711  *	Remove all dynamic addresses from the bridge.
   1712  */
   1713 void
   1714 bridge_rtflush(struct bridge_softc *sc, int full)
   1715 {
   1716 	struct bridge_rtnode *brt, *nbrt;
   1717 
   1718 	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
   1719 		nbrt = LIST_NEXT(brt, brt_list);
   1720 		if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
   1721 			bridge_rtnode_destroy(sc, brt);
   1722 	}
   1723 }
   1724 
   1725 /*
   1726  * bridge_rtdaddr:
   1727  *
   1728  *	Remove an address from the table.
   1729  */
   1730 int
   1731 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
   1732 {
   1733 	struct bridge_rtnode *brt;
   1734 
   1735 	if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
   1736 		return (ENOENT);
   1737 
   1738 	bridge_rtnode_destroy(sc, brt);
   1739 	return (0);
   1740 }
   1741 
   1742 /*
   1743  * bridge_rtdelete:
   1744  *
   1745  *	Delete routes to a speicifc member interface.
   1746  */
   1747 void
   1748 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
   1749 {
   1750 	struct bridge_rtnode *brt, *nbrt;
   1751 
   1752 	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
   1753 		nbrt = LIST_NEXT(brt, brt_list);
   1754 		if (brt->brt_ifp == ifp)
   1755 			bridge_rtnode_destroy(sc, brt);
   1756 	}
   1757 }
   1758 
   1759 /*
   1760  * bridge_rtable_init:
   1761  *
   1762  *	Initialize the route table for this bridge.
   1763  */
   1764 int
   1765 bridge_rtable_init(struct bridge_softc *sc)
   1766 {
   1767 	int i;
   1768 
   1769 	sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
   1770 	    M_DEVBUF, M_NOWAIT);
   1771 	if (sc->sc_rthash == NULL)
   1772 		return (ENOMEM);
   1773 
   1774 	for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
   1775 		LIST_INIT(&sc->sc_rthash[i]);
   1776 
   1777 	sc->sc_rthash_key = arc4random();
   1778 
   1779 	LIST_INIT(&sc->sc_rtlist);
   1780 
   1781 	return (0);
   1782 }
   1783 
   1784 /*
   1785  * bridge_rtable_fini:
   1786  *
   1787  *	Deconstruct the route table for this bridge.
   1788  */
   1789 void
   1790 bridge_rtable_fini(struct bridge_softc *sc)
   1791 {
   1792 
   1793 	free(sc->sc_rthash, M_DEVBUF);
   1794 }
   1795 
   1796 /*
   1797  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
   1798  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
   1799  */
   1800 #define	mix(a, b, c)							\
   1801 do {									\
   1802 	a -= b; a -= c; a ^= (c >> 13);					\
   1803 	b -= c; b -= a; b ^= (a << 8);					\
   1804 	c -= a; c -= b; c ^= (b >> 13);					\
   1805 	a -= b; a -= c; a ^= (c >> 12);					\
   1806 	b -= c; b -= a; b ^= (a << 16);					\
   1807 	c -= a; c -= b; c ^= (b >> 5);					\
   1808 	a -= b; a -= c; a ^= (c >> 3);					\
   1809 	b -= c; b -= a; b ^= (a << 10);					\
   1810 	c -= a; c -= b; c ^= (b >> 15);					\
   1811 } while (/*CONSTCOND*/0)
   1812 
   1813 static __inline uint32_t
   1814 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
   1815 {
   1816 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
   1817 
   1818 	b += addr[5] << 8;
   1819 	b += addr[4];
   1820 	a += addr[3] << 24;
   1821 	a += addr[2] << 16;
   1822 	a += addr[1] << 8;
   1823 	a += addr[0];
   1824 
   1825 	mix(a, b, c);
   1826 
   1827 	return (c & BRIDGE_RTHASH_MASK);
   1828 }
   1829 
   1830 #undef mix
   1831 
   1832 /*
   1833  * bridge_rtnode_lookup:
   1834  *
   1835  *	Look up a bridge route node for the specified destination.
   1836  */
   1837 struct bridge_rtnode *
   1838 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
   1839 {
   1840 	struct bridge_rtnode *brt;
   1841 	uint32_t hash;
   1842 	int dir;
   1843 
   1844 	hash = bridge_rthash(sc, addr);
   1845 	LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
   1846 		dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
   1847 		if (dir == 0)
   1848 			return (brt);
   1849 		if (dir > 0)
   1850 			return (NULL);
   1851 	}
   1852 
   1853 	return (NULL);
   1854 }
   1855 
   1856 /*
   1857  * bridge_rtnode_insert:
   1858  *
   1859  *	Insert the specified bridge node into the route table.  We
   1860  *	assume the entry is not already in the table.
   1861  */
   1862 int
   1863 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
   1864 {
   1865 	struct bridge_rtnode *lbrt;
   1866 	uint32_t hash;
   1867 	int dir;
   1868 
   1869 	hash = bridge_rthash(sc, brt->brt_addr);
   1870 
   1871 	lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
   1872 	if (lbrt == NULL) {
   1873 		LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
   1874 		goto out;
   1875 	}
   1876 
   1877 	do {
   1878 		dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
   1879 		if (dir == 0)
   1880 			return (EEXIST);
   1881 		if (dir > 0) {
   1882 			LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
   1883 			goto out;
   1884 		}
   1885 		if (LIST_NEXT(lbrt, brt_hash) == NULL) {
   1886 			LIST_INSERT_AFTER(lbrt, brt, brt_hash);
   1887 			goto out;
   1888 		}
   1889 		lbrt = LIST_NEXT(lbrt, brt_hash);
   1890 	} while (lbrt != NULL);
   1891 
   1892 #ifdef DIAGNOSTIC
   1893 	panic("bridge_rtnode_insert: impossible");
   1894 #endif
   1895 
   1896  out:
   1897 	LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
   1898 	sc->sc_brtcnt++;
   1899 
   1900 	return (0);
   1901 }
   1902 
   1903 /*
   1904  * bridge_rtnode_destroy:
   1905  *
   1906  *	Destroy a bridge rtnode.
   1907  */
   1908 void
   1909 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
   1910 {
   1911 
   1912 	LIST_REMOVE(brt, brt_hash);
   1913 
   1914 	LIST_REMOVE(brt, brt_list);
   1915 	sc->sc_brtcnt--;
   1916 	pool_put(&bridge_rtnode_pool, brt);
   1917 }
   1918 
   1919 #if defined(BRIDGE_IPF) && defined(PFIL_HOOKS)
   1920 extern struct pfil_head inet_pfil_hook;                 /* XXX */
   1921 extern struct pfil_head inet6_pfil_hook;                /* XXX */
   1922 
   1923 /*
   1924  * Send bridge packets through IPF if they are one of the types IPF can deal
   1925  * with, or if they are ARP or REVARP.  (IPF will pass ARP and REVARP without
   1926  * question.)
   1927  */
   1928 static int bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
   1929 {
   1930 	int snap, error;
   1931 	struct ether_header *eh1, eh2;
   1932 	struct llc llc;
   1933 	u_int16_t ether_type;
   1934 
   1935 	snap = 0;
   1936 	error = -1;	/* Default error if not error == 0 */
   1937 	eh1 = mtod(*mp, struct ether_header *);
   1938 	ether_type = ntohs(eh1->ether_type);
   1939 
   1940 	/*
   1941 	 * Check for SNAP/LLC.
   1942 	 */
   1943         if (ether_type < ETHERMTU) {
   1944                 struct llc *llc = (struct llc *)(eh1 + 1);
   1945 
   1946                 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
   1947                     llc->llc_dsap == LLC_SNAP_LSAP &&
   1948                     llc->llc_ssap == LLC_SNAP_LSAP &&
   1949                     llc->llc_control == LLC_UI) {
   1950                 	ether_type = htons(llc->llc_un.type_snap.ether_type);
   1951 			snap = 1;
   1952                 }
   1953         }
   1954 
   1955 	/*
   1956 	 * If we're trying to filter bridge traffic, don't look at anything
   1957 	 * other than IP and ARP traffic.  If the filter doesn't understand
   1958 	 * IPv6, don't allow IPv6 through the bridge either.  This is lame
   1959 	 * since if we really wanted, say, an AppleTalk filter, we are hosed,
   1960 	 * but of course we don't have an AppleTalk filter to begin with.
   1961 	 * (Note that since IPF doesn't understand ARP it will pass *ALL*
   1962 	 * ARP traffic.)
   1963 	 */
   1964 	switch (ether_type) {
   1965 		case ETHERTYPE_ARP:
   1966 		case ETHERTYPE_REVARP:
   1967 			return 0; /* Automatically pass */
   1968 		case ETHERTYPE_IP:
   1969 # ifdef INET6
   1970 		case ETHERTYPE_IPV6:
   1971 # endif /* INET6 */
   1972 			break;
   1973 		default:
   1974 			goto bad;
   1975 	}
   1976 
   1977 	/* Strip off the Ethernet header and keep a copy. */
   1978 	m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
   1979 	m_adj(*mp, ETHER_HDR_LEN);
   1980 
   1981 	/* Strip off snap header, if present */
   1982 	if (snap) {
   1983 		m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc);
   1984 		m_adj(*mp, sizeof(struct llc));
   1985 	}
   1986 
   1987 	/*
   1988 	 * Check basic packet sanity and run IPF through pfil.
   1989 	 */
   1990 	switch (ether_type)
   1991 	{
   1992 	case ETHERTYPE_IP :
   1993 		error = (dir == PFIL_IN) ? bridge_ip_checkbasic(mp) : 0;
   1994 		if (error == 0)
   1995 			error = pfil_run_hooks(&inet_pfil_hook, mp, ifp, dir);
   1996 		break;
   1997 # ifdef INET6
   1998 	case ETHERTYPE_IPV6 :
   1999 		error = (dir == PFIL_IN) ? bridge_ip6_checkbasic(mp) : 0;
   2000 		if (error == 0)
   2001 			error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp, dir);
   2002 		break;
   2003 # endif
   2004 	default :
   2005 		error = 0;
   2006 		break;
   2007 	}
   2008 
   2009 	if (*mp == NULL)
   2010 		return error;
   2011 	if (error != 0)
   2012 		goto bad;
   2013 
   2014 	error = -1;
   2015 
   2016 	/*
   2017 	 * Finally, put everything back the way it was and return
   2018 	 */
   2019 	if (snap) {
   2020 		M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
   2021 		if (*mp == NULL)
   2022 			return error;
   2023 		bcopy(&llc, mtod(*mp, caddr_t), sizeof(struct llc));
   2024 	}
   2025 
   2026 	M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
   2027 	if (*mp == NULL)
   2028 		return error;
   2029 	bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
   2030 
   2031 	return 0;
   2032 
   2033     bad:
   2034 	m_freem(*mp);
   2035 	*mp = NULL;
   2036 	return error;
   2037 }
   2038 
   2039 /*
   2040  * Perform basic checks on header size since
   2041  * IPF assumes ip_input has already processed
   2042  * it for it.  Cut-and-pasted from ip_input.c.
   2043  * Given how simple the IPv6 version is,
   2044  * does the IPv4 version really need to be
   2045  * this complicated?
   2046  *
   2047  * XXX Should we update ipstat here, or not?
   2048  * XXX Right now we update ipstat but not
   2049  * XXX csum_counter.
   2050  */
   2051 static int
   2052 bridge_ip_checkbasic(struct mbuf **mp)
   2053 {
   2054 	struct mbuf *m = *mp;
   2055 	struct ip *ip;
   2056 	int len, hlen;
   2057 
   2058 	if (*mp == NULL)
   2059 		return -1;
   2060 
   2061 	if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
   2062 		if ((m = m_copyup(m, sizeof(struct ip),
   2063 			(max_linkhdr + 3) & ~3)) == NULL) {
   2064 			/* XXXJRT new stat, please */
   2065 			ipstat.ips_toosmall++;
   2066 			goto bad;
   2067 		}
   2068 	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
   2069 		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
   2070 			ipstat.ips_toosmall++;
   2071 			goto bad;
   2072 		}
   2073 	}
   2074 	ip = mtod(m, struct ip *);
   2075 	if (ip == NULL) goto bad;
   2076 
   2077 	if (ip->ip_v != IPVERSION) {
   2078 		ipstat.ips_badvers++;
   2079 		goto bad;
   2080 	}
   2081 	hlen = ip->ip_hl << 2;
   2082 	if (hlen < sizeof(struct ip)) { /* minimum header length */
   2083 		ipstat.ips_badhlen++;
   2084 		goto bad;
   2085 	}
   2086 	if (hlen > m->m_len) {
   2087 		if ((m = m_pullup(m, hlen)) == 0) {
   2088 			ipstat.ips_badhlen++;
   2089 			goto bad;
   2090 		}
   2091 		ip = mtod(m, struct ip *);
   2092 		if (ip == NULL) goto bad;
   2093 	}
   2094 
   2095         switch (m->m_pkthdr.csum_flags &
   2096                 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
   2097                  M_CSUM_IPv4_BAD)) {
   2098         case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
   2099                 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */
   2100                 goto bad;
   2101 
   2102         case M_CSUM_IPv4:
   2103                 /* Checksum was okay. */
   2104                 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */
   2105                 break;
   2106 
   2107         default:
   2108                 /* Must compute it ourselves. */
   2109                 /* INET_CSUM_COUNTER_INCR(&ip_swcsum); */
   2110                 if (in_cksum(m, hlen) != 0)
   2111                         goto bad;
   2112                 break;
   2113         }
   2114 
   2115         /* Retrieve the packet length. */
   2116         len = ntohs(ip->ip_len);
   2117 
   2118         /*
   2119          * Check for additional length bogosity
   2120          */
   2121         if (len < hlen) {
   2122                 ipstat.ips_badlen++;
   2123                 goto bad;
   2124         }
   2125 
   2126         /*
   2127          * Check that the amount of data in the buffers
   2128          * is as at least much as the IP header would have us expect.
   2129          * Drop packet if shorter than we expect.
   2130          */
   2131         if (m->m_pkthdr.len < len) {
   2132                 ipstat.ips_tooshort++;
   2133                 goto bad;
   2134         }
   2135 
   2136 	/* Checks out, proceed */
   2137 	*mp = m;
   2138 	return 0;
   2139 
   2140     bad:
   2141 	*mp = m;
   2142 	return -1;
   2143 }
   2144 
   2145 # ifdef INET6
   2146 /*
   2147  * Same as above, but for IPv6.
   2148  * Cut-and-pasted from ip6_input.c.
   2149  * XXX Should we update ip6stat, or not?
   2150  */
   2151 static int
   2152 bridge_ip6_checkbasic(struct mbuf **mp)
   2153 {
   2154 	struct mbuf *m = *mp;
   2155 	struct ip6_hdr *ip6;
   2156 
   2157         /*
   2158          * If the IPv6 header is not aligned, slurp it up into a new
   2159          * mbuf with space for link headers, in the event we forward
   2160          * it.  Otherwise, if it is aligned, make sure the entire base
   2161          * IPv6 header is in the first mbuf of the chain.
   2162          */
   2163         if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
   2164                 struct ifnet *inifp = m->m_pkthdr.rcvif;
   2165                 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
   2166                                   (max_linkhdr + 3) & ~3)) == NULL) {
   2167                         /* XXXJRT new stat, please */
   2168                         ip6stat.ip6s_toosmall++;
   2169                         in6_ifstat_inc(inifp, ifs6_in_hdrerr);
   2170                         goto bad;
   2171                 }
   2172         } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
   2173                 struct ifnet *inifp = m->m_pkthdr.rcvif;
   2174                 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
   2175                         ip6stat.ip6s_toosmall++;
   2176                         in6_ifstat_inc(inifp, ifs6_in_hdrerr);
   2177                         goto bad;
   2178                 }
   2179         }
   2180 
   2181         ip6 = mtod(m, struct ip6_hdr *);
   2182 
   2183         if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
   2184                 ip6stat.ip6s_badvers++;
   2185                 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
   2186                 goto bad;
   2187         }
   2188 
   2189 	/* Checks out, proceed */
   2190 	*mp = m;
   2191 	return 0;
   2192 
   2193     bad:
   2194 	*mp = m;
   2195 	return -1;
   2196 }
   2197 # endif /* INET6 */
   2198 #endif /* BRIDGE_IPF && PFIL_HOOKS */
   2199