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