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