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