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