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