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