xref: /freebsd/sys/net/if_bridge.c (revision c0b9f4fe659b6839541970eb5675e57f4d814969)
1 /*	$NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc 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 __FBSDID("$FreeBSD$");
84 
85 #include "opt_inet.h"
86 #include "opt_inet6.h"
87 
88 #include <sys/param.h>
89 #include <sys/mbuf.h>
90 #include <sys/malloc.h>
91 #include <sys/protosw.h>
92 #include <sys/systm.h>
93 #include <sys/time.h>
94 #include <sys/socket.h> /* for net/if.h */
95 #include <sys/sockio.h>
96 #include <sys/ctype.h>  /* string functions */
97 #include <sys/kernel.h>
98 #include <sys/random.h>
99 #include <sys/sysctl.h>
100 #include <vm/uma.h>
101 #include <sys/module.h>
102 #include <sys/proc.h>
103 #include <sys/lock.h>
104 #include <sys/mutex.h>
105 
106 #include <net/bpf.h>
107 #include <net/if.h>
108 #include <net/if_clone.h>
109 #include <net/if_dl.h>
110 #include <net/if_types.h>
111 #include <net/if_var.h>
112 #include <net/pfil.h>
113 
114 #include <netinet/in.h> /* for struct arpcom */
115 #include <netinet/in_systm.h>
116 #include <netinet/in_var.h>
117 #include <netinet/ip.h>
118 #include <netinet/ip_var.h>
119 #ifdef INET6
120 #include <netinet/ip6.h>
121 #include <netinet6/ip6_var.h>
122 #endif
123 #include <machine/in_cksum.h>
124 #include <netinet/if_ether.h> /* for struct arpcom */
125 #include <net/if_bridgevar.h>
126 #include <net/if_llc.h>
127 
128 #include <net/route.h>
129 #include <netinet/ip_fw.h>
130 #include <netinet/ip_dummynet.h>
131 
132 /*
133  * Size of the route hash table.  Must be a power of two.
134  */
135 #ifndef BRIDGE_RTHASH_SIZE
136 #define	BRIDGE_RTHASH_SIZE		1024
137 #endif
138 
139 #define	BRIDGE_RTHASH_MASK		(BRIDGE_RTHASH_SIZE - 1)
140 
141 /*
142  * Maximum number of addresses to cache.
143  */
144 #ifndef BRIDGE_RTABLE_MAX
145 #define	BRIDGE_RTABLE_MAX		100
146 #endif
147 
148 /*
149  * Spanning tree defaults.
150  */
151 #define	BSTP_DEFAULT_MAX_AGE		(20 * 256)
152 #define	BSTP_DEFAULT_HELLO_TIME		(2 * 256)
153 #define	BSTP_DEFAULT_FORWARD_DELAY	(15 * 256)
154 #define	BSTP_DEFAULT_HOLD_TIME		(1 * 256)
155 #define	BSTP_DEFAULT_BRIDGE_PRIORITY	0x8000
156 #define	BSTP_DEFAULT_PORT_PRIORITY	0x80
157 #define	BSTP_DEFAULT_PATH_COST		55
158 
159 /*
160  * Timeout (in seconds) for entries learned dynamically.
161  */
162 #ifndef BRIDGE_RTABLE_TIMEOUT
163 #define	BRIDGE_RTABLE_TIMEOUT		(20 * 60)	/* same as ARP */
164 #endif
165 
166 /*
167  * Number of seconds between walks of the route list.
168  */
169 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
170 #define	BRIDGE_RTABLE_PRUNE_PERIOD	(5 * 60)
171 #endif
172 
173 static struct mtx 	bridge_list_mtx;
174 eventhandler_tag	bridge_detach_cookie = NULL;
175 
176 int	bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
177 
178 uma_zone_t bridge_rtnode_zone;
179 
180 static int	bridge_clone_create(struct if_clone *, int);
181 static void	bridge_clone_destroy(struct ifnet *);
182 
183 static int	bridge_ioctl(struct ifnet *, u_long, caddr_t);
184 static void	bridge_ifdetach(void *arg __unused, struct ifnet *);
185 static void	bridge_init(void *);
186 static void	bridge_dummynet(struct mbuf *, struct ifnet *);
187 static void	bridge_stop(struct ifnet *, int);
188 static void	bridge_start(struct ifnet *);
189 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
190 static int	bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
191 		    struct rtentry *);
192 
193 static void	bridge_forward(struct bridge_softc *, struct mbuf *m);
194 
195 static void	bridge_timer(void *);
196 
197 static void	bridge_broadcast(struct bridge_softc *, struct ifnet *,
198 		    struct mbuf *, int);
199 static void	bridge_span(struct bridge_softc *, struct mbuf *);
200 
201 static int	bridge_rtupdate(struct bridge_softc *, const uint8_t *,
202 		    struct ifnet *, int, uint8_t);
203 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
204 static void	bridge_rttrim(struct bridge_softc *);
205 static void	bridge_rtage(struct bridge_softc *);
206 static void	bridge_rtflush(struct bridge_softc *, int);
207 static int	bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
208 
209 static int	bridge_rtable_init(struct bridge_softc *);
210 static void	bridge_rtable_fini(struct bridge_softc *);
211 
212 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
213 		    const uint8_t *);
214 static int	bridge_rtnode_insert(struct bridge_softc *,
215 		    struct bridge_rtnode *);
216 static void	bridge_rtnode_destroy(struct bridge_softc *,
217 		    struct bridge_rtnode *);
218 
219 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
220 		    const char *name);
221 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
222 		    struct ifnet *ifp);
223 static void	bridge_delete_member(struct bridge_softc *,
224 		    struct bridge_iflist *, int);
225 static void	bridge_delete_span(struct bridge_softc *,
226 		    struct bridge_iflist *);
227 
228 static int	bridge_ioctl_add(struct bridge_softc *, void *);
229 static int	bridge_ioctl_del(struct bridge_softc *, void *);
230 static int	bridge_ioctl_gifflags(struct bridge_softc *, void *);
231 static int	bridge_ioctl_sifflags(struct bridge_softc *, void *);
232 static int	bridge_ioctl_scache(struct bridge_softc *, void *);
233 static int	bridge_ioctl_gcache(struct bridge_softc *, void *);
234 static int	bridge_ioctl_gifs(struct bridge_softc *, void *);
235 static int	bridge_ioctl_rts(struct bridge_softc *, void *);
236 static int	bridge_ioctl_saddr(struct bridge_softc *, void *);
237 static int	bridge_ioctl_sto(struct bridge_softc *, void *);
238 static int	bridge_ioctl_gto(struct bridge_softc *, void *);
239 static int	bridge_ioctl_daddr(struct bridge_softc *, void *);
240 static int	bridge_ioctl_flush(struct bridge_softc *, void *);
241 static int	bridge_ioctl_gpri(struct bridge_softc *, void *);
242 static int	bridge_ioctl_spri(struct bridge_softc *, void *);
243 static int	bridge_ioctl_ght(struct bridge_softc *, void *);
244 static int	bridge_ioctl_sht(struct bridge_softc *, void *);
245 static int	bridge_ioctl_gfd(struct bridge_softc *, void *);
246 static int	bridge_ioctl_sfd(struct bridge_softc *, void *);
247 static int	bridge_ioctl_gma(struct bridge_softc *, void *);
248 static int	bridge_ioctl_sma(struct bridge_softc *, void *);
249 static int	bridge_ioctl_sifprio(struct bridge_softc *, void *);
250 static int	bridge_ioctl_sifcost(struct bridge_softc *, void *);
251 static int	bridge_ioctl_addspan(struct bridge_softc *, void *);
252 static int	bridge_ioctl_delspan(struct bridge_softc *, void *);
253 static int	bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
254 		    int);
255 static int	bridge_ip_checkbasic(struct mbuf **mp);
256 # ifdef INET6
257 static int	bridge_ip6_checkbasic(struct mbuf **mp);
258 # endif /* INET6 */
259 
260 SYSCTL_DECL(_net_link);
261 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
262 
263 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
264 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
265 static int pfil_member = 1; /* run pfil hooks on the member interface */
266 static int pfil_ipfw = 0;   /* layer2 filter with ipfw */
267 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
268     &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
269 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
270     &pfil_bridge, 0, "Packet filter on the bridge interface");
271 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
272     &pfil_member, 0, "Packet filter on the member interface");
273 
274 struct bridge_control {
275 	int	(*bc_func)(struct bridge_softc *, void *);
276 	int	bc_argsize;
277 	int	bc_flags;
278 };
279 
280 #define	BC_F_COPYIN		0x01	/* copy arguments in */
281 #define	BC_F_COPYOUT		0x02	/* copy arguments out */
282 #define	BC_F_SUSER		0x04	/* do super-user check */
283 
284 const struct bridge_control bridge_control_table[] = {
285 	{ bridge_ioctl_add,		sizeof(struct ifbreq),
286 	  BC_F_COPYIN|BC_F_SUSER },
287 	{ bridge_ioctl_del,		sizeof(struct ifbreq),
288 	  BC_F_COPYIN|BC_F_SUSER },
289 
290 	{ bridge_ioctl_gifflags,	sizeof(struct ifbreq),
291 	  BC_F_COPYIN|BC_F_COPYOUT },
292 	{ bridge_ioctl_sifflags,	sizeof(struct ifbreq),
293 	  BC_F_COPYIN|BC_F_SUSER },
294 
295 	{ bridge_ioctl_scache,		sizeof(struct ifbrparam),
296 	  BC_F_COPYIN|BC_F_SUSER },
297 	{ bridge_ioctl_gcache,		sizeof(struct ifbrparam),
298 	  BC_F_COPYOUT },
299 
300 	{ bridge_ioctl_gifs,		sizeof(struct ifbifconf),
301 	  BC_F_COPYIN|BC_F_COPYOUT },
302 	{ bridge_ioctl_rts,		sizeof(struct ifbaconf),
303 	  BC_F_COPYIN|BC_F_COPYOUT },
304 
305 	{ bridge_ioctl_saddr,		sizeof(struct ifbareq),
306 	  BC_F_COPYIN|BC_F_SUSER },
307 
308 	{ bridge_ioctl_sto,		sizeof(struct ifbrparam),
309 	  BC_F_COPYIN|BC_F_SUSER },
310 	{ bridge_ioctl_gto,		sizeof(struct ifbrparam),
311 	  BC_F_COPYOUT },
312 
313 	{ bridge_ioctl_daddr,		sizeof(struct ifbareq),
314 	  BC_F_COPYIN|BC_F_SUSER },
315 
316 	{ bridge_ioctl_flush,		sizeof(struct ifbreq),
317 	  BC_F_COPYIN|BC_F_SUSER },
318 
319 	{ bridge_ioctl_gpri,		sizeof(struct ifbrparam),
320 	  BC_F_COPYOUT },
321 	{ bridge_ioctl_spri,		sizeof(struct ifbrparam),
322 	  BC_F_COPYIN|BC_F_SUSER },
323 
324 	{ bridge_ioctl_ght,		sizeof(struct ifbrparam),
325 	  BC_F_COPYOUT },
326 	{ bridge_ioctl_sht,		sizeof(struct ifbrparam),
327 	  BC_F_COPYIN|BC_F_SUSER },
328 
329 	{ bridge_ioctl_gfd,		sizeof(struct ifbrparam),
330 	  BC_F_COPYOUT },
331 	{ bridge_ioctl_sfd,		sizeof(struct ifbrparam),
332 	  BC_F_COPYIN|BC_F_SUSER },
333 
334 	{ bridge_ioctl_gma,		sizeof(struct ifbrparam),
335 	  BC_F_COPYOUT },
336 	{ bridge_ioctl_sma,		sizeof(struct ifbrparam),
337 	  BC_F_COPYIN|BC_F_SUSER },
338 
339 	{ bridge_ioctl_sifprio,		sizeof(struct ifbreq),
340 	  BC_F_COPYIN|BC_F_SUSER },
341 
342 	{ bridge_ioctl_sifcost,		sizeof(struct ifbreq),
343 	  BC_F_COPYIN|BC_F_SUSER },
344 
345 	{ bridge_ioctl_addspan,		sizeof(struct ifbreq),
346 	  BC_F_COPYIN|BC_F_SUSER },
347 	{ bridge_ioctl_delspan,		sizeof(struct ifbreq),
348 	  BC_F_COPYIN|BC_F_SUSER },
349 };
350 const int bridge_control_table_size =
351     sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
352 
353 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
354 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
355 
356 LIST_HEAD(, bridge_softc) bridge_list;
357 
358 IFC_SIMPLE_DECLARE(bridge, 0);
359 
360 static int
361 bridge_modevent(module_t mod, int type, void *data)
362 {
363 
364 	switch (type) {
365 	case MOD_LOAD:
366 		mtx_init(&bridge_list_mtx, "if_bridge list", NULL, MTX_DEF);
367 		if_clone_attach(&bridge_cloner);
368 		bridge_rtnode_zone = uma_zcreate("bridge_rtnode",
369 		    sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL,
370 		    UMA_ALIGN_PTR, 0);
371 		LIST_INIT(&bridge_list);
372 		bridge_input_p = bridge_input;
373 		bridge_output_p = bridge_output;
374 		bridge_dn_p = bridge_dummynet;
375 		bstp_linkstate_p = bstp_linkstate;
376 		bridge_detach_cookie = EVENTHANDLER_REGISTER(
377 		    ifnet_departure_event, bridge_ifdetach, NULL,
378 		    EVENTHANDLER_PRI_ANY);
379 		break;
380 	case MOD_UNLOAD:
381 		EVENTHANDLER_DEREGISTER(ifnet_departure_event,
382 		    bridge_detach_cookie);
383 		if_clone_detach(&bridge_cloner);
384 		uma_zdestroy(bridge_rtnode_zone);
385 		bridge_input_p = NULL;
386 		bridge_output_p = NULL;
387 		bridge_dn_p = NULL;
388 		bstp_linkstate_p = NULL;
389 		mtx_destroy(&bridge_list_mtx);
390 		break;
391 	default:
392 		return EOPNOTSUPP;
393 	}
394 	return 0;
395 }
396 
397 static moduledata_t bridge_mod = {
398 	"if_bridge",
399 	bridge_modevent,
400 	0
401 };
402 
403 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
404 
405 /*
406  * handler for net.link.bridge.pfil_ipfw
407  */
408 static int
409 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS)
410 {
411 	int enable = pfil_ipfw;
412 	int error;
413 
414 	error = sysctl_handle_int(oidp, &enable, 0, req);
415 	enable = (enable) ? 1 : 0;
416 
417 	if (enable != pfil_ipfw) {
418 		pfil_ipfw = enable;
419 
420 		/*
421 		 * Disable pfil so that ipfw doesnt run twice, if the user
422 		 * really wants both then they can re-enable pfil_bridge and/or
423 		 * pfil_member. Also allow non-ip packets as ipfw can filter by
424 		 * layer2 type.
425 		 */
426 		if (pfil_ipfw) {
427 			pfil_onlyip = 0;
428 			pfil_bridge = 0;
429 			pfil_member = 0;
430 		}
431 	}
432 
433 	return error;
434 }
435 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw, CTLTYPE_INT|CTLFLAG_RW,
436 	    &pfil_ipfw, 0, &sysctl_pfil_ipfw, "I", "Layer2 filter with IPFW");
437 
438 /*
439  * bridge_clone_create:
440  *
441  *	Create a new bridge instance.
442  */
443 static int
444 bridge_clone_create(struct if_clone *ifc, int unit)
445 {
446 	struct bridge_softc *sc;
447 	struct ifnet *ifp;
448 	u_char eaddr[6];
449 
450 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
451 	BRIDGE_LOCK_INIT(sc);
452 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
453 	if (ifp == NULL) {
454 		free(sc, M_DEVBUF);
455 		return (ENOSPC);
456 	}
457 
458 	sc->sc_brtmax = BRIDGE_RTABLE_MAX;
459 	sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
460 	sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
461 	sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
462 	sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
463 	sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
464 	sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
465 
466 	/* Initialize our routing table. */
467 	bridge_rtable_init(sc);
468 
469 	callout_init_mtx(&sc->sc_brcallout, &sc->sc_mtx, 0);
470 	callout_init_mtx(&sc->sc_bstpcallout, &sc->sc_mtx, 0);
471 
472 	LIST_INIT(&sc->sc_iflist);
473 	LIST_INIT(&sc->sc_spanlist);
474 
475 	ifp->if_softc = sc;
476 	if_initname(ifp, ifc->ifc_name, unit);
477 	ifp->if_mtu = ETHERMTU;
478 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
479 	ifp->if_ioctl = bridge_ioctl;
480 	ifp->if_output = bridge_output;
481 	ifp->if_start = bridge_start;
482 	ifp->if_init = bridge_init;
483 	ifp->if_type = IFT_BRIDGE;
484 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
485 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
486 	IFQ_SET_READY(&ifp->if_snd);
487 	ifp->if_hdrlen = ETHER_HDR_LEN;
488 
489 	/*
490 	 * Generate a random ethernet address and use the private AC:DE:48
491 	 * OUI code.
492 	 */
493 	arc4rand(eaddr, ETHER_ADDR_LEN, 1);
494 	eaddr[0] = 0xAC;
495 	eaddr[1] = 0xDE;
496 	eaddr[2] = 0x48;
497 
498 	ether_ifattach(ifp, eaddr);
499 	/* Now undo some of the damage... */
500 	ifp->if_baudrate = 0;
501 	ifp->if_type = IFT_BRIDGE;
502 
503 	mtx_lock(&bridge_list_mtx);
504 	LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
505 	mtx_unlock(&bridge_list_mtx);
506 
507 	return (0);
508 }
509 
510 /*
511  * bridge_clone_destroy:
512  *
513  *	Destroy a bridge instance.
514  */
515 static void
516 bridge_clone_destroy(struct ifnet *ifp)
517 {
518 	struct bridge_softc *sc = ifp->if_softc;
519 	struct bridge_iflist *bif;
520 
521 	BRIDGE_LOCK(sc);
522 
523 	bridge_stop(ifp, 1);
524 	ifp->if_flags &= ~IFF_UP;
525 
526 	while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
527 		bridge_delete_member(sc, bif, 0);
528 
529 	while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
530 		bridge_delete_span(sc, bif);
531 	}
532 
533 	BRIDGE_UNLOCK(sc);
534 
535 	callout_drain(&sc->sc_brcallout);
536 	callout_drain(&sc->sc_bstpcallout);
537 
538 	mtx_lock(&bridge_list_mtx);
539 	LIST_REMOVE(sc, sc_list);
540 	mtx_unlock(&bridge_list_mtx);
541 
542 	ether_ifdetach(ifp);
543 	if_free_type(ifp, IFT_ETHER);
544 
545 	/* Tear down the routing table. */
546 	bridge_rtable_fini(sc);
547 
548 	BRIDGE_LOCK_DESTROY(sc);
549 	free(sc, M_DEVBUF);
550 }
551 
552 /*
553  * bridge_ioctl:
554  *
555  *	Handle a control request from the operator.
556  */
557 static int
558 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
559 {
560 	struct bridge_softc *sc = ifp->if_softc;
561 	struct thread *td = curthread;
562 	union {
563 		struct ifbreq ifbreq;
564 		struct ifbifconf ifbifconf;
565 		struct ifbareq ifbareq;
566 		struct ifbaconf ifbaconf;
567 		struct ifbrparam ifbrparam;
568 	} args;
569 	struct ifdrv *ifd = (struct ifdrv *) data;
570 	const struct bridge_control *bc;
571 	int error = 0;
572 
573 	BRIDGE_LOCK(sc);
574 
575 	switch (cmd) {
576 
577 	case SIOCADDMULTI:
578 	case SIOCDELMULTI:
579 		break;
580 
581 	case SIOCGDRVSPEC:
582 	case SIOCSDRVSPEC:
583 		if (ifd->ifd_cmd >= bridge_control_table_size) {
584 			error = EINVAL;
585 			break;
586 		}
587 		bc = &bridge_control_table[ifd->ifd_cmd];
588 
589 		if (cmd == SIOCGDRVSPEC &&
590 		    (bc->bc_flags & BC_F_COPYOUT) == 0) {
591 			error = EINVAL;
592 			break;
593 		}
594 		else if (cmd == SIOCSDRVSPEC &&
595 		    (bc->bc_flags & BC_F_COPYOUT) != 0) {
596 			error = EINVAL;
597 			break;
598 		}
599 
600 		if (bc->bc_flags & BC_F_SUSER) {
601 			error = suser(td);
602 			if (error)
603 				break;
604 		}
605 
606 		if (ifd->ifd_len != bc->bc_argsize ||
607 		    ifd->ifd_len > sizeof(args)) {
608 			error = EINVAL;
609 			break;
610 		}
611 
612 		if (bc->bc_flags & BC_F_COPYIN) {
613 			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
614 			if (error)
615 				break;
616 		}
617 
618 		error = (*bc->bc_func)(sc, &args);
619 		if (error)
620 			break;
621 
622 		if (bc->bc_flags & BC_F_COPYOUT)
623 			error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
624 
625 		break;
626 
627 	case SIOCSIFFLAGS:
628 		if (!(ifp->if_flags & IFF_UP) &&
629 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
630 			/*
631 			 * If interface is marked down and it is running,
632 			 * then stop and disable it.
633 			 */
634 			bridge_stop(ifp, 1);
635 		} else if ((ifp->if_flags & IFF_UP) &&
636 		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
637 			/*
638 			 * If interface is marked up and it is stopped, then
639 			 * start it.
640 			 */
641 			BRIDGE_UNLOCK(sc);
642 			(*ifp->if_init)(sc);
643 		}
644 		break;
645 
646 	case SIOCSIFMTU:
647 		/* Do not allow the MTU to be changed on the bridge */
648 		error = EINVAL;
649 		break;
650 
651 	default:
652 		/*
653 		 * drop the lock as ether_ioctl() will call bridge_start() and
654 		 * cause the lock to be recursed.
655 		 */
656 		BRIDGE_UNLOCK(sc);
657 		error = ether_ioctl(ifp, cmd, data);
658 		break;
659 	}
660 
661 	if (BRIDGE_LOCKED(sc))
662 		BRIDGE_UNLOCK(sc);
663 
664 	return (error);
665 }
666 
667 /*
668  * bridge_lookup_member:
669  *
670  *	Lookup a bridge member interface.
671  */
672 static struct bridge_iflist *
673 bridge_lookup_member(struct bridge_softc *sc, const char *name)
674 {
675 	struct bridge_iflist *bif;
676 	struct ifnet *ifp;
677 
678 	BRIDGE_LOCK_ASSERT(sc);
679 
680 	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
681 		ifp = bif->bif_ifp;
682 		if (strcmp(ifp->if_xname, name) == 0)
683 			return (bif);
684 	}
685 
686 	return (NULL);
687 }
688 
689 /*
690  * bridge_lookup_member_if:
691  *
692  *	Lookup a bridge member interface by ifnet*.
693  */
694 static struct bridge_iflist *
695 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
696 {
697 	struct bridge_iflist *bif;
698 
699 	BRIDGE_LOCK_ASSERT(sc);
700 
701 	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
702 		if (bif->bif_ifp == member_ifp)
703 			return (bif);
704 	}
705 
706 	return (NULL);
707 }
708 
709 /*
710  * bridge_delete_member:
711  *
712  *	Delete the specified member interface.
713  */
714 static void
715 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
716     int gone)
717 {
718 	struct ifnet *ifs = bif->bif_ifp;
719 
720 	BRIDGE_LOCK_ASSERT(sc);
721 
722 	if (!gone) {
723 	    switch (ifs->if_type) {
724 	    case IFT_ETHER:
725 	    case IFT_L2VLAN:
726 		    /*
727 		     * Take the interface out of promiscuous mode.
728 		     */
729 		    (void) ifpromisc(ifs, 0);
730 		    break;
731 
732 	    case IFT_GIF:
733 		    break;
734 
735 	    default:
736 #ifdef DIAGNOSTIC
737 		    panic("bridge_delete_member: impossible");
738 #endif
739 		    break;
740 	    }
741 	}
742 
743 	ifs->if_bridge = NULL;
744 	BRIDGE_XLOCK(sc);
745 	LIST_REMOVE(bif, bif_next);
746 	BRIDGE_XDROP(sc);
747 
748 	bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
749 
750 	free(bif, M_DEVBUF);
751 
752 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
753 		bstp_initialization(sc);
754 }
755 
756 /*
757  * bridge_delete_span:
758  *
759  *	Delete the specified span interface.
760  */
761 static void
762 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
763 {
764 	BRIDGE_LOCK_ASSERT(sc);
765 
766 	KASSERT(bif->bif_ifp->if_bridge == NULL,
767 	    ("%s: not a span interface", __func__));
768 
769 	LIST_REMOVE(bif, bif_next);
770 	free(bif, M_DEVBUF);
771 }
772 
773 static int
774 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
775 {
776 	struct ifbreq *req = arg;
777 	struct bridge_iflist *bif = NULL;
778 	struct ifnet *ifs;
779 	int error = 0;
780 
781 	BRIDGE_LOCK_ASSERT(sc);
782 
783 	ifs = ifunit(req->ifbr_ifsname);
784 	if (ifs == NULL)
785 		return (ENOENT);
786 
787 	/* If it's in the span list, it can't be a member. */
788 	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
789 		if (ifs == bif->bif_ifp)
790 			return (EBUSY);
791 
792 	/* Allow the first Ethernet member to define the MTU */
793 	if (ifs->if_type != IFT_GIF) {
794 		if (LIST_EMPTY(&sc->sc_iflist))
795 			sc->sc_ifp->if_mtu = ifs->if_mtu;
796 		else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
797 			if_printf(sc->sc_ifp, "invalid MTU for %s\n",
798 			    ifs->if_xname);
799 			return (EINVAL);
800 		}
801 	}
802 
803 	if (ifs->if_bridge == sc)
804 		return (EEXIST);
805 
806 	if (ifs->if_bridge != NULL)
807 		return (EBUSY);
808 
809 	bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
810 	if (bif == NULL)
811 		return (ENOMEM);
812 
813 	switch (ifs->if_type) {
814 	case IFT_ETHER:
815 	case IFT_L2VLAN:
816 		/*
817 		 * Place the interface into promiscuous mode.
818 		 */
819 		error = ifpromisc(ifs, 1);
820 		if (error)
821 			goto out;
822 		break;
823 
824 	case IFT_GIF:
825 		break;
826 
827 	default:
828 		error = EINVAL;
829 		goto out;
830 	}
831 
832 	bif->bif_ifp = ifs;
833 	bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
834 	bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
835 	bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
836 
837 	ifs->if_bridge = sc;
838 	/*
839 	 * XXX: XLOCK HERE!?!
840 	 *
841 	 * NOTE: insert_***HEAD*** should be safe for the traversals.
842 	 */
843 	LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
844 
845 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
846 		bstp_initialization(sc);
847 	else
848 		bstp_stop(sc);
849 
850 out:
851 	if (error) {
852 		if (bif != NULL)
853 			free(bif, M_DEVBUF);
854 	}
855 	return (error);
856 }
857 
858 static int
859 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
860 {
861 	struct ifbreq *req = arg;
862 	struct bridge_iflist *bif;
863 
864 	BRIDGE_LOCK_ASSERT(sc);
865 
866 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
867 	if (bif == NULL)
868 		return (ENOENT);
869 
870 	bridge_delete_member(sc, bif, 0);
871 
872 	return (0);
873 }
874 
875 static int
876 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
877 {
878 	struct ifbreq *req = arg;
879 	struct bridge_iflist *bif;
880 
881 	BRIDGE_LOCK_ASSERT(sc);
882 
883 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
884 	if (bif == NULL)
885 		return (ENOENT);
886 
887 	req->ifbr_ifsflags = bif->bif_flags;
888 	req->ifbr_state = bif->bif_state;
889 	req->ifbr_priority = bif->bif_priority;
890 	req->ifbr_path_cost = bif->bif_path_cost;
891 	req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
892 
893 	return (0);
894 }
895 
896 static int
897 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
898 {
899 	struct ifbreq *req = arg;
900 	struct bridge_iflist *bif;
901 
902 	BRIDGE_LOCK_ASSERT(sc);
903 
904 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
905 	if (bif == NULL)
906 		return (ENOENT);
907 
908 	if (req->ifbr_ifsflags & IFBIF_SPAN)
909 		/* SPAN is readonly */
910 		return (EINVAL);
911 
912 	if (req->ifbr_ifsflags & IFBIF_STP) {
913 		switch (bif->bif_ifp->if_type) {
914 		case IFT_ETHER:
915 			/* These can do spanning tree. */
916 			break;
917 
918 		default:
919 			/* Nothing else can. */
920 			return (EINVAL);
921 		}
922 	}
923 
924 	bif->bif_flags = req->ifbr_ifsflags;
925 
926 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
927 		bstp_initialization(sc);
928 
929 	return (0);
930 }
931 
932 static int
933 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
934 {
935 	struct ifbrparam *param = arg;
936 
937 	BRIDGE_LOCK_ASSERT(sc);
938 
939 	sc->sc_brtmax = param->ifbrp_csize;
940 	bridge_rttrim(sc);
941 
942 	return (0);
943 }
944 
945 static int
946 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
947 {
948 	struct ifbrparam *param = arg;
949 
950 	BRIDGE_LOCK_ASSERT(sc);
951 
952 	param->ifbrp_csize = sc->sc_brtmax;
953 
954 	return (0);
955 }
956 
957 static int
958 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
959 {
960 	struct ifbifconf *bifc = arg;
961 	struct bridge_iflist *bif;
962 	struct ifbreq breq;
963 	int count, len, error = 0;
964 
965 	BRIDGE_LOCK_ASSERT(sc);
966 
967 	count = 0;
968 	LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
969 		count++;
970 	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
971 		count++;
972 
973 	if (bifc->ifbic_len == 0) {
974 		bifc->ifbic_len = sizeof(breq) * count;
975 		return (0);
976 	}
977 
978 	count = 0;
979 	len = bifc->ifbic_len;
980 	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
981 		if (len < sizeof(breq))
982 			break;
983 
984 		strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
985 		    sizeof(breq.ifbr_ifsname));
986 		breq.ifbr_ifsflags = bif->bif_flags;
987 		breq.ifbr_state = bif->bif_state;
988 		breq.ifbr_priority = bif->bif_priority;
989 		breq.ifbr_path_cost = bif->bif_path_cost;
990 		breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
991 		error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
992 		if (error)
993 			break;
994 		count++;
995 		len -= sizeof(breq);
996 	}
997 	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
998 		if (len < sizeof(breq))
999 			break;
1000 
1001 		strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1002 		    sizeof(breq.ifbr_ifsname));
1003 		breq.ifbr_ifsflags = bif->bif_flags;
1004 		breq.ifbr_state = bif->bif_state;
1005 		breq.ifbr_priority = bif->bif_priority;
1006 		breq.ifbr_path_cost = bif->bif_path_cost;
1007 		breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
1008 		error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
1009 		if (error)
1010 			break;
1011 		count++;
1012 		len -= sizeof(breq);
1013 	}
1014 
1015 	bifc->ifbic_len = sizeof(breq) * count;
1016 	return (error);
1017 }
1018 
1019 static int
1020 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1021 {
1022 	struct ifbaconf *bac = arg;
1023 	struct bridge_rtnode *brt;
1024 	struct ifbareq bareq;
1025 	struct timeval tv;
1026 	int count = 0, error = 0, len;
1027 
1028 	BRIDGE_LOCK_ASSERT(sc);
1029 
1030 	if (bac->ifbac_len == 0)
1031 		return (0);
1032 
1033 	getmicrotime(&tv);
1034 
1035 	len = bac->ifbac_len;
1036 	LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1037 		if (len < sizeof(bareq))
1038 			goto out;
1039 		strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1040 		    sizeof(bareq.ifba_ifsname));
1041 		memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1042 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1043 				tv.tv_sec < brt->brt_expire)
1044 			bareq.ifba_expire = brt->brt_expire - tv.tv_sec;
1045 		else
1046 			bareq.ifba_expire = 0;
1047 		bareq.ifba_flags = brt->brt_flags;
1048 
1049 		error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1050 		if (error)
1051 			goto out;
1052 		count++;
1053 		len -= sizeof(bareq);
1054 	}
1055 out:
1056 	bac->ifbac_len = sizeof(bareq) * count;
1057 	return (error);
1058 }
1059 
1060 static int
1061 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1062 {
1063 	struct ifbareq *req = arg;
1064 	struct bridge_iflist *bif;
1065 	int error;
1066 
1067 	BRIDGE_LOCK_ASSERT(sc);
1068 
1069 	bif = bridge_lookup_member(sc, req->ifba_ifsname);
1070 	if (bif == NULL)
1071 		return (ENOENT);
1072 
1073 	error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1074 	    req->ifba_flags);
1075 
1076 	return (error);
1077 }
1078 
1079 static int
1080 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1081 {
1082 	struct ifbrparam *param = arg;
1083 
1084 	BRIDGE_LOCK_ASSERT(sc);
1085 
1086 	sc->sc_brttimeout = param->ifbrp_ctime;
1087 
1088 	return (0);
1089 }
1090 
1091 static int
1092 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1093 {
1094 	struct ifbrparam *param = arg;
1095 
1096 	BRIDGE_LOCK_ASSERT(sc);
1097 
1098 	param->ifbrp_ctime = sc->sc_brttimeout;
1099 
1100 	return (0);
1101 }
1102 
1103 static int
1104 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1105 {
1106 	struct ifbareq *req = arg;
1107 
1108 	BRIDGE_LOCK_ASSERT(sc);
1109 
1110 	return (bridge_rtdaddr(sc, req->ifba_dst));
1111 }
1112 
1113 static int
1114 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1115 {
1116 	struct ifbreq *req = arg;
1117 
1118 	BRIDGE_LOCK_ASSERT(sc);
1119 
1120 	bridge_rtflush(sc, req->ifbr_ifsflags);
1121 
1122 	return (0);
1123 }
1124 
1125 static int
1126 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1127 {
1128 	struct ifbrparam *param = arg;
1129 
1130 	BRIDGE_LOCK_ASSERT(sc);
1131 
1132 	param->ifbrp_prio = sc->sc_bridge_priority;
1133 
1134 	return (0);
1135 }
1136 
1137 static int
1138 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1139 {
1140 	struct ifbrparam *param = arg;
1141 
1142 	BRIDGE_LOCK_ASSERT(sc);
1143 
1144 	sc->sc_bridge_priority = param->ifbrp_prio;
1145 
1146 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
1147 		bstp_initialization(sc);
1148 
1149 	return (0);
1150 }
1151 
1152 static int
1153 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1154 {
1155 	struct ifbrparam *param = arg;
1156 
1157 	BRIDGE_LOCK_ASSERT(sc);
1158 
1159 	param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1160 
1161 	return (0);
1162 }
1163 
1164 static int
1165 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1166 {
1167 	struct ifbrparam *param = arg;
1168 
1169 	BRIDGE_LOCK_ASSERT(sc);
1170 
1171 	if (param->ifbrp_hellotime == 0)
1172 		return (EINVAL);
1173 	sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1174 
1175 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
1176 		bstp_initialization(sc);
1177 
1178 	return (0);
1179 }
1180 
1181 static int
1182 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1183 {
1184 	struct ifbrparam *param = arg;
1185 
1186 	BRIDGE_LOCK_ASSERT(sc);
1187 
1188 	param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1189 
1190 	return (0);
1191 }
1192 
1193 static int
1194 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1195 {
1196 	struct ifbrparam *param = arg;
1197 
1198 	BRIDGE_LOCK_ASSERT(sc);
1199 
1200 	if (param->ifbrp_fwddelay == 0)
1201 		return (EINVAL);
1202 	sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1203 
1204 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
1205 		bstp_initialization(sc);
1206 
1207 	return (0);
1208 }
1209 
1210 static int
1211 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1212 {
1213 	struct ifbrparam *param = arg;
1214 
1215 	BRIDGE_LOCK_ASSERT(sc);
1216 
1217 	param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1218 
1219 	return (0);
1220 }
1221 
1222 static int
1223 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1224 {
1225 	struct ifbrparam *param = arg;
1226 
1227 	BRIDGE_LOCK_ASSERT(sc);
1228 
1229 	if (param->ifbrp_maxage == 0)
1230 		return (EINVAL);
1231 	sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1232 
1233 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
1234 		bstp_initialization(sc);
1235 
1236 	return (0);
1237 }
1238 
1239 static int
1240 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1241 {
1242 	struct ifbreq *req = arg;
1243 	struct bridge_iflist *bif;
1244 
1245 	BRIDGE_LOCK_ASSERT(sc);
1246 
1247 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1248 	if (bif == NULL)
1249 		return (ENOENT);
1250 
1251 	bif->bif_priority = req->ifbr_priority;
1252 
1253 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
1254 		bstp_initialization(sc);
1255 
1256 	return (0);
1257 }
1258 
1259 static int
1260 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1261 {
1262 	struct ifbreq *req = arg;
1263 	struct bridge_iflist *bif;
1264 
1265 	BRIDGE_LOCK_ASSERT(sc);
1266 
1267 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1268 	if (bif == NULL)
1269 		return (ENOENT);
1270 
1271 	bif->bif_path_cost = req->ifbr_path_cost;
1272 
1273 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
1274 		bstp_initialization(sc);
1275 
1276 	return (0);
1277 }
1278 
1279 static int
1280 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1281 {
1282 	struct ifbreq *req = arg;
1283 	struct bridge_iflist *bif = NULL;
1284 	struct ifnet *ifs;
1285 
1286 	BRIDGE_LOCK_ASSERT(sc);
1287 
1288 	ifs = ifunit(req->ifbr_ifsname);
1289 	if (ifs == NULL)
1290 		return (ENOENT);
1291 
1292 	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1293 		if (ifs == bif->bif_ifp)
1294 			return (EBUSY);
1295 
1296 	if (ifs->if_bridge != NULL)
1297 		return (EBUSY);
1298 
1299 	switch (ifs->if_type) {
1300 		case IFT_ETHER:
1301 		case IFT_L2VLAN:
1302 			break;
1303 		default:
1304 			return (EINVAL);
1305 	}
1306 
1307 	bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1308 	if (bif == NULL)
1309 		return (ENOMEM);
1310 
1311 	bif->bif_ifp = ifs;
1312 	bif->bif_flags = IFBIF_SPAN;
1313 
1314 	LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1315 
1316 	return (0);
1317 }
1318 
1319 static int
1320 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1321 {
1322 	struct ifbreq *req = arg;
1323 	struct bridge_iflist *bif;
1324 	struct ifnet *ifs;
1325 
1326 	BRIDGE_LOCK_ASSERT(sc);
1327 
1328 	ifs = ifunit(req->ifbr_ifsname);
1329 	if (ifs == NULL)
1330 		return (ENOENT);
1331 
1332 	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1333 		if (ifs == bif->bif_ifp)
1334 			break;
1335 
1336 	if (bif == NULL)
1337 		return (ENOENT);
1338 
1339 	bridge_delete_span(sc, bif);
1340 
1341 	return (0);
1342 }
1343 
1344 /*
1345  * bridge_ifdetach:
1346  *
1347  *	Detach an interface from a bridge.  Called when a member
1348  *	interface is detaching.
1349  */
1350 static void
1351 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1352 {
1353 	struct bridge_softc *sc = ifp->if_bridge;
1354 	struct bridge_iflist *bif;
1355 
1356 	/* Check if the interface is a bridge member */
1357 	if (sc != NULL) {
1358 		BRIDGE_LOCK(sc);
1359 
1360 		bif = bridge_lookup_member_if(sc, ifp);
1361 		if (bif != NULL)
1362 			bridge_delete_member(sc, bif, 1);
1363 
1364 
1365 		BRIDGE_UNLOCK(sc);
1366 		return;
1367 	}
1368 
1369 	/* Check if the interface is a span port */
1370 	mtx_lock(&bridge_list_mtx);
1371 	LIST_FOREACH(sc, &bridge_list, sc_list) {
1372 		BRIDGE_LOCK(sc);
1373 		LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1374 			if (ifp == bif->bif_ifp) {
1375 				bridge_delete_span(sc, bif);
1376 				break;
1377 			}
1378 
1379 		BRIDGE_UNLOCK(sc);
1380 	}
1381 	mtx_unlock(&bridge_list_mtx);
1382 }
1383 
1384 /*
1385  * bridge_init:
1386  *
1387  *	Initialize a bridge interface.
1388  */
1389 static void
1390 bridge_init(void *xsc)
1391 {
1392 	struct bridge_softc *sc = (struct bridge_softc *)xsc;
1393 	struct ifnet *ifp = sc->sc_ifp;
1394 
1395 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1396 		return;
1397 
1398 	BRIDGE_LOCK(sc);
1399 	callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1400 	    bridge_timer, sc);
1401 
1402 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1403 	bstp_initialization(sc);
1404 	BRIDGE_UNLOCK(sc);
1405 	return;
1406 }
1407 
1408 /*
1409  * bridge_stop:
1410  *
1411  *	Stop the bridge interface.
1412  */
1413 static void
1414 bridge_stop(struct ifnet *ifp, int disable)
1415 {
1416 	struct bridge_softc *sc = ifp->if_softc;
1417 
1418 	BRIDGE_LOCK_ASSERT(sc);
1419 
1420 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1421 		return;
1422 
1423 	callout_stop(&sc->sc_brcallout);
1424 	bstp_stop(sc);
1425 
1426 	bridge_rtflush(sc, IFBF_FLUSHDYN);
1427 
1428 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1429 }
1430 
1431 /*
1432  * bridge_enqueue:
1433  *
1434  *	Enqueue a packet on a bridge member interface.
1435  *
1436  */
1437 __inline void
1438 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1439 {
1440 	int len, err;
1441 	short mflags;
1442 
1443 	/*
1444 	 * Clear any in-bound checksum flags for this packet.
1445 	 */
1446 	m->m_pkthdr.csum_flags = 0;
1447 
1448 	len = m->m_pkthdr.len;
1449 	mflags = m->m_flags;
1450 
1451 	IFQ_ENQUEUE(&dst_ifp->if_snd, m, err);
1452 	if (err == 0) {
1453 
1454 		sc->sc_ifp->if_opackets++;
1455 		sc->sc_ifp->if_obytes += len;
1456 
1457 		dst_ifp->if_obytes += len;
1458 
1459 		if (mflags & M_MCAST) {
1460 			sc->sc_ifp->if_omcasts++;
1461 			dst_ifp->if_omcasts++;
1462 		}
1463 	}
1464 
1465 	if ((dst_ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
1466 		(*dst_ifp->if_start)(dst_ifp);
1467 }
1468 
1469 /*
1470  * bridge_dummynet:
1471  *
1472  * 	Receive a queued packet from dummynet and pass it on to the output
1473  * 	interface.
1474  *
1475  *	The mbuf has the Ethernet header already attached.
1476  */
1477 static void
1478 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
1479 {
1480 	struct bridge_softc *sc;
1481 
1482 	sc = ifp->if_bridge;
1483 
1484 	/*
1485 	 * The packet didnt originate from a member interface. This should only
1486 	 * ever happen if a member interface is removed while packets are
1487 	 * queued for it.
1488 	 */
1489 	if (sc == NULL) {
1490 		m_freem(m);
1491 		return;
1492 	}
1493 
1494 	if (inet_pfil_hook.ph_busy_count >= 0
1495 #ifdef INET6
1496 	    || inet6_pfil_hook.ph_busy_count >= 0
1497 #endif
1498 	    ) {
1499 		if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
1500 			return;
1501 		if (m == NULL)
1502 			return;
1503 	}
1504 
1505 	bridge_enqueue(sc, ifp, m);
1506 }
1507 
1508 /*
1509  * bridge_output:
1510  *
1511  *	Send output from a bridge member interface.  This
1512  *	performs the bridging function for locally originated
1513  *	packets.
1514  *
1515  *	The mbuf has the Ethernet header already attached.  We must
1516  *	enqueue or free the mbuf before returning.
1517  */
1518 static int
1519 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1520     struct rtentry *rt)
1521 {
1522 	struct ether_header *eh;
1523 	struct ifnet *dst_if;
1524 	struct bridge_softc *sc;
1525 
1526 	if (m->m_len < ETHER_HDR_LEN) {
1527 		m = m_pullup(m, ETHER_HDR_LEN);
1528 		if (m == NULL)
1529 			return (0);
1530 	}
1531 
1532 	eh = mtod(m, struct ether_header *);
1533 	sc = ifp->if_bridge;
1534 
1535 	BRIDGE_LOCK(sc);
1536 
1537 	/*
1538 	 * If bridge is down, but the original output interface is up,
1539 	 * go ahead and send out that interface.  Otherwise, the packet
1540 	 * is dropped below.
1541 	 */
1542 	if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1543 		dst_if = ifp;
1544 		goto sendunicast;
1545 	}
1546 
1547 	/*
1548 	 * If the packet is a multicast, or we don't know a better way to
1549 	 * get there, send to all interfaces.
1550 	 */
1551 	if (ETHER_IS_MULTICAST(eh->ether_dhost))
1552 		dst_if = NULL;
1553 	else
1554 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1555 	if (dst_if == NULL) {
1556 		struct bridge_iflist *bif;
1557 		struct mbuf *mc;
1558 		int error = 0, used = 0;
1559 
1560 		BRIDGE_LOCK2REF(sc, error);
1561 		if (error) {
1562 			m_freem(m);
1563 			return (0);
1564 		}
1565 
1566 		bridge_span(sc, m);
1567 
1568 		LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1569 			dst_if = bif->bif_ifp;
1570 
1571 			if (dst_if->if_type == IFT_GIF)
1572 				continue;
1573 			if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
1574 				continue;
1575 
1576 			/*
1577 			 * If this is not the original output interface,
1578 			 * and the interface is participating in spanning
1579 			 * tree, make sure the port is in a state that
1580 			 * allows forwarding.
1581 			 */
1582 			if (dst_if != ifp &&
1583 			    (bif->bif_flags & IFBIF_STP) != 0) {
1584 				switch (bif->bif_state) {
1585 				case BSTP_IFSTATE_BLOCKING:
1586 				case BSTP_IFSTATE_LISTENING:
1587 				case BSTP_IFSTATE_DISABLED:
1588 					continue;
1589 				}
1590 			}
1591 
1592 			if (LIST_NEXT(bif, bif_next) == NULL) {
1593 				used = 1;
1594 				mc = m;
1595 			} else {
1596 				mc = m_copypacket(m, M_DONTWAIT);
1597 				if (mc == NULL) {
1598 					sc->sc_ifp->if_oerrors++;
1599 					continue;
1600 				}
1601 			}
1602 
1603 			bridge_enqueue(sc, dst_if, mc);
1604 		}
1605 		if (used == 0)
1606 			m_freem(m);
1607 		BRIDGE_UNREF(sc);
1608 		return (0);
1609 	}
1610 
1611 sendunicast:
1612 	/*
1613 	 * XXX Spanning tree consideration here?
1614 	 */
1615 
1616 	bridge_span(sc, m);
1617 	if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1618 		m_freem(m);
1619 		BRIDGE_UNLOCK(sc);
1620 		return (0);
1621 	}
1622 
1623 	BRIDGE_UNLOCK(sc);
1624 	bridge_enqueue(sc, dst_if, m);
1625 	return (0);
1626 }
1627 
1628 /*
1629  * bridge_start:
1630  *
1631  *	Start output on a bridge.
1632  *
1633  */
1634 static void
1635 bridge_start(struct ifnet *ifp)
1636 {
1637 	struct bridge_softc *sc;
1638 	struct mbuf *m;
1639 	struct ether_header *eh;
1640 	struct ifnet *dst_if;
1641 
1642 	sc = ifp->if_softc;
1643 
1644 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1645 	for (;;) {
1646 		IFQ_DEQUEUE(&ifp->if_snd, m);
1647 		if (m == 0)
1648 			break;
1649 		BPF_MTAP(ifp, m);
1650 
1651 		eh = mtod(m, struct ether_header *);
1652 		dst_if = NULL;
1653 
1654 		BRIDGE_LOCK(sc);
1655 		if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1656 			dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1657 		}
1658 
1659 		if (dst_if == NULL)
1660 			bridge_broadcast(sc, ifp, m, 0);
1661 		else {
1662 			BRIDGE_UNLOCK(sc);
1663 			bridge_enqueue(sc, dst_if, m);
1664 		}
1665 	}
1666 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1667 
1668 	return;
1669 }
1670 
1671 /*
1672  * bridge_forward:
1673  *
1674  *	The forwarding function of the bridge.
1675  *
1676  *	NOTE: Releases the lock on return.
1677  */
1678 static void
1679 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1680 {
1681 	struct bridge_iflist *bif;
1682 	struct ifnet *src_if, *dst_if, *ifp;
1683 	struct ether_header *eh;
1684 
1685 	src_if = m->m_pkthdr.rcvif;
1686 	BRIDGE_LOCK_ASSERT(sc);
1687 	ifp = sc->sc_ifp;
1688 
1689 	sc->sc_ifp->if_ipackets++;
1690 	sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
1691 
1692 	/*
1693 	 * Look up the bridge_iflist.
1694 	 */
1695 	bif = bridge_lookup_member_if(sc, src_if);
1696 	if (bif == NULL) {
1697 		/* Interface is not a bridge member (anymore?) */
1698 		BRIDGE_UNLOCK(sc);
1699 		m_freem(m);
1700 		return;
1701 	}
1702 
1703 	if (bif->bif_flags & IFBIF_STP) {
1704 		switch (bif->bif_state) {
1705 		case BSTP_IFSTATE_BLOCKING:
1706 		case BSTP_IFSTATE_LISTENING:
1707 		case BSTP_IFSTATE_DISABLED:
1708 			BRIDGE_UNLOCK(sc);
1709 			m_freem(m);
1710 			return;
1711 		}
1712 	}
1713 
1714 	eh = mtod(m, struct ether_header *);
1715 
1716 	/*
1717 	 * If the interface is learning, and the source
1718 	 * address is valid and not multicast, record
1719 	 * the address.
1720 	 */
1721 	if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1722 	    ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1723 	    (eh->ether_shost[0] == 0 &&
1724 	     eh->ether_shost[1] == 0 &&
1725 	     eh->ether_shost[2] == 0 &&
1726 	     eh->ether_shost[3] == 0 &&
1727 	     eh->ether_shost[4] == 0 &&
1728 	     eh->ether_shost[5] == 0) == 0) {
1729 		(void) bridge_rtupdate(sc, eh->ether_shost,
1730 		    src_if, 0, IFBAF_DYNAMIC);
1731 	}
1732 
1733 	if ((bif->bif_flags & IFBIF_STP) != 0 &&
1734 	    bif->bif_state == BSTP_IFSTATE_LEARNING) {
1735 		m_freem(m);
1736 		BRIDGE_UNLOCK(sc);
1737 		return;
1738 	}
1739 
1740 	/*
1741 	 * At this point, the port either doesn't participate
1742 	 * in spanning tree or it is in the forwarding state.
1743 	 */
1744 
1745 	/*
1746 	 * If the packet is unicast, destined for someone on
1747 	 * "this" side of the bridge, drop it.
1748 	 */
1749 	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1750 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1751 		if (src_if == dst_if) {
1752 			BRIDGE_UNLOCK(sc);
1753 			m_freem(m);
1754 			return;
1755 		}
1756 	} else {
1757 		/* ...forward it to all interfaces. */
1758 		sc->sc_ifp->if_imcasts++;
1759 		dst_if = NULL;
1760 	}
1761 
1762 	/* run the packet filter */
1763 	if (inet_pfil_hook.ph_busy_count >= 0
1764 #ifdef INET6
1765 	    || inet6_pfil_hook.ph_busy_count >= 0
1766 #endif
1767 	    ) {
1768 		BRIDGE_UNLOCK(sc);
1769 		if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1770 			return;
1771 		if (m == NULL)
1772 			return;
1773 		BRIDGE_LOCK(sc);
1774 	}
1775 
1776 	if (dst_if == NULL) {
1777 		/*
1778 		 * Tap off packets passing the bridge. Broadcast packets will
1779 		 * already be tapped as they are reinjected into ether_input.
1780 		 */
1781 		if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1782 			BPF_MTAP(ifp, m);
1783 
1784 		bridge_broadcast(sc, src_if, m, 1);
1785 		return;
1786 	}
1787 
1788 	/*
1789 	 * At this point, we're dealing with a unicast frame
1790 	 * going to a different interface.
1791 	 */
1792 	if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1793 		BRIDGE_UNLOCK(sc);
1794 		m_freem(m);
1795 		return;
1796 	}
1797 	bif = bridge_lookup_member_if(sc, dst_if);
1798 	if (bif == NULL) {
1799 		/* Not a member of the bridge (anymore?) */
1800 		BRIDGE_UNLOCK(sc);
1801 		m_freem(m);
1802 		return;
1803 	}
1804 
1805 	if (bif->bif_flags & IFBIF_STP) {
1806 		switch (bif->bif_state) {
1807 		case BSTP_IFSTATE_DISABLED:
1808 		case BSTP_IFSTATE_BLOCKING:
1809 			BRIDGE_UNLOCK(sc);
1810 			m_freem(m);
1811 			return;
1812 		}
1813 	}
1814 
1815 	/* tap off packets passing the bridge */
1816 	BPF_MTAP(ifp, m);
1817 
1818 	BRIDGE_UNLOCK(sc);
1819 
1820 	if (inet_pfil_hook.ph_busy_count >= 0
1821 #ifdef INET6
1822 	    || inet6_pfil_hook.ph_busy_count >= 0
1823 #endif
1824 	    ) {
1825 		if (bridge_pfil(&m, sc->sc_ifp, dst_if, PFIL_OUT) != 0)
1826 			return;
1827 		if (m == NULL)
1828 			return;
1829 	}
1830 
1831 	bridge_enqueue(sc, dst_if, m);
1832 }
1833 
1834 /*
1835  * bridge_input:
1836  *
1837  *	Receive input from a member interface.  Queue the packet for
1838  *	bridging if it is not for us.
1839  */
1840 static struct mbuf *
1841 bridge_input(struct ifnet *ifp, struct mbuf *m)
1842 {
1843 	struct bridge_softc *sc = ifp->if_bridge;
1844 	struct bridge_iflist *bif;
1845 	struct ifnet *bifp;
1846 	struct ether_header *eh;
1847 	struct mbuf *mc, *mc2;
1848 
1849 	if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1850 		return (m);
1851 
1852 	bifp = sc->sc_ifp;
1853 
1854 	BRIDGE_LOCK(sc);
1855 	bif = bridge_lookup_member_if(sc, ifp);
1856 	if (bif == NULL) {
1857 		BRIDGE_UNLOCK(sc);
1858 		return (m);
1859 	}
1860 
1861 	eh = mtod(m, struct ether_header *);
1862 
1863 	if (memcmp(eh->ether_dhost, IF_LLADDR(bifp),
1864 	    ETHER_ADDR_LEN) == 0) {
1865 		/*
1866 		 * If the packet is for us, set the packets source as the
1867 		 * bridge, and return the packet back to ether_input for
1868 		 * local processing.
1869 		 */
1870 
1871 		/* XXX Do we tap the packet for the member interface too?
1872 		 * BPF_MTAP(&m->m_pkthdr.rcvif, m);
1873 		 */
1874 
1875 		/* Mark the packet as arriving on the bridge interface */
1876 		m->m_pkthdr.rcvif = bifp;
1877 		BPF_MTAP(bifp, m);
1878 		bifp->if_ipackets++;
1879 
1880 		BRIDGE_UNLOCK(sc);
1881 		return (m);
1882 	}
1883 
1884 	bridge_span(sc, m);
1885 
1886 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1887 		/* Tap off 802.1D packets; they do not get forwarded. */
1888 		if (memcmp(eh->ether_dhost, bstp_etheraddr,
1889 		    ETHER_ADDR_LEN) == 0) {
1890 			m = bstp_input(ifp, m);
1891 			if (m == NULL) {
1892 				BRIDGE_UNLOCK(sc);
1893 				return (NULL);
1894 			}
1895 		}
1896 
1897 		if (bif->bif_flags & IFBIF_STP) {
1898 			switch (bif->bif_state) {
1899 			case BSTP_IFSTATE_BLOCKING:
1900 			case BSTP_IFSTATE_LISTENING:
1901 			case BSTP_IFSTATE_DISABLED:
1902 				BRIDGE_UNLOCK(sc);
1903 				return (m);
1904 			}
1905 		}
1906 
1907 		if (bcmp(etherbroadcastaddr, eh->ether_dhost,
1908 		    sizeof(etherbroadcastaddr)) == 0)
1909 			m->m_flags |= M_BCAST;
1910 		else
1911 			m->m_flags |= M_MCAST;
1912 
1913 		/*
1914 		 * Make a deep copy of the packet and enqueue the copy
1915 		 * for bridge processing; return the original packet for
1916 		 * local processing.
1917 		 */
1918 		mc = m_dup(m, M_DONTWAIT);
1919 		if (mc == NULL) {
1920 			BRIDGE_UNLOCK(sc);
1921 			return (m);
1922 		}
1923 
1924 		/* Perform the bridge forwarding function with the copy. */
1925 		bridge_forward(sc, mc);
1926 
1927 		/*
1928 		 * Reinject the mbuf as arriving on the bridge so we have a
1929 		 * chance at claiming multicast packets. We can not loop back
1930 		 * here from ether_input as a bridge is never a member of a
1931 		 * bridge.
1932 		 */
1933 		KASSERT(bifp->if_bridge == NULL,
1934 		    ("loop created in bridge_input"));
1935 		mc2 = m_dup(m, M_DONTWAIT);
1936 		if (mc2 != NULL) {
1937 			/* Keep the layer3 header aligned */
1938 			int i = min(mc2->m_pkthdr.len, max_protohdr);
1939 			mc2 = m_copyup(mc2, i, ETHER_ALIGN);
1940 		}
1941 		if (mc2 != NULL) {
1942 			mc2->m_pkthdr.rcvif = bifp;
1943 			(*bifp->if_input)(bifp, mc2);
1944 		}
1945 
1946 		/* Return the original packet for local processing. */
1947 		return (m);
1948 	}
1949 
1950 	if (bif->bif_flags & IFBIF_STP) {
1951 		switch (bif->bif_state) {
1952 		case BSTP_IFSTATE_BLOCKING:
1953 		case BSTP_IFSTATE_LISTENING:
1954 		case BSTP_IFSTATE_DISABLED:
1955 			BRIDGE_UNLOCK(sc);
1956 			return (m);
1957 		}
1958 	}
1959 
1960 	/*
1961 	 * Unicast.  Make sure it's not for us.
1962 	 */
1963 	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1964 		if(bif->bif_ifp->if_type == IFT_GIF)
1965 			continue;
1966 		/* It is destined for us. */
1967 		if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
1968 		    ETHER_ADDR_LEN) == 0) {
1969 			if (bif->bif_flags & IFBIF_LEARNING)
1970 				(void) bridge_rtupdate(sc,
1971 				    eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1972 			m->m_pkthdr.rcvif = bif->bif_ifp;
1973 			BRIDGE_UNLOCK(sc);
1974 			return (m);
1975 		}
1976 
1977 		/* We just received a packet that we sent out. */
1978 		if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
1979 		    ETHER_ADDR_LEN) == 0) {
1980 			BRIDGE_UNLOCK(sc);
1981 			m_freem(m);
1982 			return (NULL);
1983 		}
1984 	}
1985 
1986 	/* Perform the bridge forwarding function. */
1987 	bridge_forward(sc, m);
1988 
1989 	return (NULL);
1990 }
1991 
1992 /*
1993  * bridge_broadcast:
1994  *
1995  *	Send a frame to all interfaces that are members of
1996  *	the bridge, except for the one on which the packet
1997  *	arrived.
1998  *
1999  *	NOTE: Releases the lock on return.
2000  */
2001 static void
2002 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2003     struct mbuf *m, int runfilt)
2004 {
2005 	struct bridge_iflist *bif;
2006 	struct mbuf *mc;
2007 	struct ifnet *dst_if;
2008 	int error = 0, used = 0;
2009 
2010 	BRIDGE_LOCK_ASSERT(sc);
2011 	BRIDGE_LOCK2REF(sc, error);
2012 	if (error) {
2013 		m_freem(m);
2014 		return;
2015 	}
2016 
2017 	/* Filter on the bridge interface before broadcasting */
2018 	if (runfilt && (inet_pfil_hook.ph_busy_count >= 0
2019 #ifdef INET6
2020 	    || inet6_pfil_hook.ph_busy_count >= 0
2021 #endif
2022 	    )) {
2023 		if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
2024 			goto out;
2025 		if (m == NULL)
2026 			goto out;
2027 	}
2028 
2029 	LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
2030 		dst_if = bif->bif_ifp;
2031 		if (dst_if == src_if)
2032 			continue;
2033 
2034 		if (bif->bif_flags & IFBIF_STP) {
2035 			switch (bif->bif_state) {
2036 			case BSTP_IFSTATE_BLOCKING:
2037 			case BSTP_IFSTATE_DISABLED:
2038 				continue;
2039 			}
2040 		}
2041 
2042 		if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2043 		    (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2044 			continue;
2045 
2046 		if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2047 			continue;
2048 
2049 		if (LIST_NEXT(bif, bif_next) == NULL) {
2050 			mc = m;
2051 			used = 1;
2052 		} else {
2053 			mc = m_copypacket(m, M_DONTWAIT);
2054 			if (mc == NULL) {
2055 				sc->sc_ifp->if_oerrors++;
2056 				continue;
2057 			}
2058 		}
2059 
2060 		/*
2061 		 * Filter on the output interface. Pass a NULL bridge interface
2062 		 * pointer so we do not redundantly filter on the bridge for
2063 		 * each interface we broadcast on.
2064 		 */
2065 		if (runfilt && (inet_pfil_hook.ph_busy_count >= 0
2066 #ifdef INET6
2067 		    || inet6_pfil_hook.ph_busy_count >= 0
2068 #endif
2069 		    )) {
2070 			if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2071 				continue;
2072 			if (mc == NULL)
2073 				continue;
2074 		}
2075 
2076 		bridge_enqueue(sc, dst_if, mc);
2077 	}
2078 	if (used == 0)
2079 		m_freem(m);
2080 
2081 out:
2082 	BRIDGE_UNREF(sc);
2083 }
2084 
2085 /*
2086  * bridge_span:
2087  *
2088  *	Duplicate a packet out one or more interfaces that are in span mode,
2089  *	the original mbuf is unmodified.
2090  */
2091 static void
2092 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2093 {
2094 	struct bridge_iflist *bif;
2095 	struct ifnet *dst_if;
2096 	struct mbuf *mc;
2097 
2098 	if (LIST_EMPTY(&sc->sc_spanlist))
2099 		return;
2100 
2101 	LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2102 		dst_if = bif->bif_ifp;
2103 
2104 		if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2105 			continue;
2106 
2107 		mc = m_copypacket(m, M_DONTWAIT);
2108 		if (mc == NULL) {
2109 			sc->sc_ifp->if_oerrors++;
2110 			continue;
2111 		}
2112 
2113 		bridge_enqueue(sc, dst_if, mc);
2114 	}
2115 }
2116 
2117 /*
2118  * bridge_rtupdate:
2119  *
2120  *	Add a bridge routing entry.
2121  */
2122 static int
2123 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2124     struct ifnet *dst_if, int setflags, uint8_t flags)
2125 {
2126 	struct bridge_rtnode *brt;
2127 	struct timeval tv;
2128 	int error;
2129 
2130 	BRIDGE_LOCK_ASSERT(sc);
2131 
2132 	/*
2133 	 * A route for this destination might already exist.  If so,
2134 	 * update it, otherwise create a new one.
2135 	 */
2136 	getmicrotime(&tv);
2137 	if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2138 		if (sc->sc_brtcnt >= sc->sc_brtmax)
2139 			return (ENOSPC);
2140 
2141 		/*
2142 		 * Allocate a new bridge forwarding node, and
2143 		 * initialize the expiration time and Ethernet
2144 		 * address.
2145 		 */
2146 		brt = uma_zalloc(bridge_rtnode_zone, M_NOWAIT | M_ZERO);
2147 		if (brt == NULL)
2148 			return (ENOMEM);
2149 
2150 		brt->brt_expire = tv.tv_sec + sc->sc_brttimeout;
2151 		brt->brt_flags = IFBAF_DYNAMIC;
2152 		memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2153 
2154 		if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2155 			uma_zfree(bridge_rtnode_zone, brt);
2156 			return (error);
2157 		}
2158 	}
2159 
2160 	brt->brt_ifp = dst_if;
2161 	if (setflags) {
2162 		brt->brt_flags = flags;
2163 		brt->brt_expire = (flags & IFBAF_STATIC) ? 0 :
2164 		    tv.tv_sec + sc->sc_brttimeout;
2165 	}
2166 
2167 	return (0);
2168 }
2169 
2170 /*
2171  * bridge_rtlookup:
2172  *
2173  *	Lookup the destination interface for an address.
2174  */
2175 static struct ifnet *
2176 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2177 {
2178 	struct bridge_rtnode *brt;
2179 
2180 	BRIDGE_LOCK_ASSERT(sc);
2181 
2182 	if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2183 		return (NULL);
2184 
2185 	return (brt->brt_ifp);
2186 }
2187 
2188 /*
2189  * bridge_rttrim:
2190  *
2191  *	Trim the routine table so that we have a number
2192  *	of routing entries less than or equal to the
2193  *	maximum number.
2194  */
2195 static void
2196 bridge_rttrim(struct bridge_softc *sc)
2197 {
2198 	struct bridge_rtnode *brt, *nbrt;
2199 
2200 	BRIDGE_LOCK_ASSERT(sc);
2201 
2202 	/* Make sure we actually need to do this. */
2203 	if (sc->sc_brtcnt <= sc->sc_brtmax)
2204 		return;
2205 
2206 	/* Force an aging cycle; this might trim enough addresses. */
2207 	bridge_rtage(sc);
2208 	if (sc->sc_brtcnt <= sc->sc_brtmax)
2209 		return;
2210 
2211 	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2212 		nbrt = LIST_NEXT(brt, brt_list);
2213 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2214 			bridge_rtnode_destroy(sc, brt);
2215 			if (sc->sc_brtcnt <= sc->sc_brtmax)
2216 				return;
2217 		}
2218 	}
2219 }
2220 
2221 /*
2222  * bridge_timer:
2223  *
2224  *	Aging timer for the bridge.
2225  */
2226 static void
2227 bridge_timer(void *arg)
2228 {
2229 	struct bridge_softc *sc = arg;
2230 
2231 	BRIDGE_LOCK_ASSERT(sc);
2232 
2233 	bridge_rtage(sc);
2234 
2235 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
2236 		callout_reset(&sc->sc_brcallout,
2237 		    bridge_rtable_prune_period * hz, bridge_timer, sc);
2238 }
2239 
2240 /*
2241  * bridge_rtage:
2242  *
2243  *	Perform an aging cycle.
2244  */
2245 static void
2246 bridge_rtage(struct bridge_softc *sc)
2247 {
2248 	struct bridge_rtnode *brt, *nbrt;
2249 	struct timeval tv;
2250 
2251 	BRIDGE_LOCK_ASSERT(sc);
2252 
2253 	getmicrotime(&tv);
2254 
2255 	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2256 		nbrt = LIST_NEXT(brt, brt_list);
2257 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2258 			if (tv.tv_sec >= brt->brt_expire)
2259 				bridge_rtnode_destroy(sc, brt);
2260 		}
2261 	}
2262 }
2263 
2264 /*
2265  * bridge_rtflush:
2266  *
2267  *	Remove all dynamic addresses from the bridge.
2268  */
2269 static void
2270 bridge_rtflush(struct bridge_softc *sc, int full)
2271 {
2272 	struct bridge_rtnode *brt, *nbrt;
2273 
2274 	BRIDGE_LOCK_ASSERT(sc);
2275 
2276 	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2277 		nbrt = LIST_NEXT(brt, brt_list);
2278 		if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2279 			bridge_rtnode_destroy(sc, brt);
2280 	}
2281 }
2282 
2283 /*
2284  * bridge_rtdaddr:
2285  *
2286  *	Remove an address from the table.
2287  */
2288 static int
2289 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2290 {
2291 	struct bridge_rtnode *brt;
2292 
2293 	BRIDGE_LOCK_ASSERT(sc);
2294 
2295 	if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2296 		return (ENOENT);
2297 
2298 	bridge_rtnode_destroy(sc, brt);
2299 	return (0);
2300 }
2301 
2302 /*
2303  * bridge_rtdelete:
2304  *
2305  *	Delete routes to a speicifc member interface.
2306  */
2307 void
2308 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2309 {
2310 	struct bridge_rtnode *brt, *nbrt;
2311 
2312 	BRIDGE_LOCK_ASSERT(sc);
2313 
2314 	for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2315 		nbrt = LIST_NEXT(brt, brt_list);
2316 		if (brt->brt_ifp == ifp && (full ||
2317 			    (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2318 			bridge_rtnode_destroy(sc, brt);
2319 	}
2320 }
2321 
2322 /*
2323  * bridge_rtable_init:
2324  *
2325  *	Initialize the route table for this bridge.
2326  */
2327 static int
2328 bridge_rtable_init(struct bridge_softc *sc)
2329 {
2330 	int i;
2331 
2332 	sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2333 	    M_DEVBUF, M_NOWAIT);
2334 	if (sc->sc_rthash == NULL)
2335 		return (ENOMEM);
2336 
2337 	for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2338 		LIST_INIT(&sc->sc_rthash[i]);
2339 
2340 	sc->sc_rthash_key = arc4random();
2341 
2342 	LIST_INIT(&sc->sc_rtlist);
2343 
2344 	return (0);
2345 }
2346 
2347 /*
2348  * bridge_rtable_fini:
2349  *
2350  *	Deconstruct the route table for this bridge.
2351  */
2352 static void
2353 bridge_rtable_fini(struct bridge_softc *sc)
2354 {
2355 
2356 	free(sc->sc_rthash, M_DEVBUF);
2357 }
2358 
2359 /*
2360  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2361  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2362  */
2363 #define	mix(a, b, c)							\
2364 do {									\
2365 	a -= b; a -= c; a ^= (c >> 13);					\
2366 	b -= c; b -= a; b ^= (a << 8);					\
2367 	c -= a; c -= b; c ^= (b >> 13);					\
2368 	a -= b; a -= c; a ^= (c >> 12);					\
2369 	b -= c; b -= a; b ^= (a << 16);					\
2370 	c -= a; c -= b; c ^= (b >> 5);					\
2371 	a -= b; a -= c; a ^= (c >> 3);					\
2372 	b -= c; b -= a; b ^= (a << 10);					\
2373 	c -= a; c -= b; c ^= (b >> 15);					\
2374 } while (/*CONSTCOND*/0)
2375 
2376 static __inline uint32_t
2377 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2378 {
2379 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2380 
2381 	b += addr[5] << 8;
2382 	b += addr[4];
2383 	a += addr[3] << 24;
2384 	a += addr[2] << 16;
2385 	a += addr[1] << 8;
2386 	a += addr[0];
2387 
2388 	mix(a, b, c);
2389 
2390 	return (c & BRIDGE_RTHASH_MASK);
2391 }
2392 
2393 #undef mix
2394 
2395 /*
2396  * bridge_rtnode_lookup:
2397  *
2398  *	Look up a bridge route node for the specified destination.
2399  */
2400 static struct bridge_rtnode *
2401 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2402 {
2403 	struct bridge_rtnode *brt;
2404 	uint32_t hash;
2405 	int dir;
2406 
2407 	BRIDGE_LOCK_ASSERT(sc);
2408 
2409 	hash = bridge_rthash(sc, addr);
2410 	LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2411 		dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
2412 		if (dir == 0)
2413 			return (brt);
2414 		if (dir > 0)
2415 			return (NULL);
2416 	}
2417 
2418 	return (NULL);
2419 }
2420 
2421 /*
2422  * bridge_rtnode_insert:
2423  *
2424  *	Insert the specified bridge node into the route table.  We
2425  *	assume the entry is not already in the table.
2426  */
2427 static int
2428 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2429 {
2430 	struct bridge_rtnode *lbrt;
2431 	uint32_t hash;
2432 	int dir;
2433 
2434 	BRIDGE_LOCK_ASSERT(sc);
2435 
2436 	hash = bridge_rthash(sc, brt->brt_addr);
2437 
2438 	lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2439 	if (lbrt == NULL) {
2440 		LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2441 		goto out;
2442 	}
2443 
2444 	do {
2445 		dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
2446 		if (dir == 0)
2447 			return (EEXIST);
2448 		if (dir > 0) {
2449 			LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2450 			goto out;
2451 		}
2452 		if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2453 			LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2454 			goto out;
2455 		}
2456 		lbrt = LIST_NEXT(lbrt, brt_hash);
2457 	} while (lbrt != NULL);
2458 
2459 #ifdef DIAGNOSTIC
2460 	panic("bridge_rtnode_insert: impossible");
2461 #endif
2462 
2463 out:
2464 	LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2465 	sc->sc_brtcnt++;
2466 
2467 	return (0);
2468 }
2469 
2470 /*
2471  * bridge_rtnode_destroy:
2472  *
2473  *	Destroy a bridge rtnode.
2474  */
2475 static void
2476 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2477 {
2478 	BRIDGE_LOCK_ASSERT(sc);
2479 
2480 	LIST_REMOVE(brt, brt_hash);
2481 
2482 	LIST_REMOVE(brt, brt_list);
2483 	sc->sc_brtcnt--;
2484 	uma_zfree(bridge_rtnode_zone, brt);
2485 }
2486 
2487 /*
2488  * Send bridge packets through pfil if they are one of the types pfil can deal
2489  * with, or if they are ARP or REVARP.  (pfil will pass ARP and REVARP without
2490  * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2491  * that interface.
2492  */
2493 static int
2494 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2495 {
2496 	int snap, error, i;
2497 	struct ether_header *eh1, eh2;
2498 	struct ip_fw_args args;
2499 	struct ip *ip;
2500 	struct llc llc1;
2501 	u_int16_t ether_type;
2502 
2503 	snap = 0;
2504 	error = -1;	/* Default error if not error == 0 */
2505 
2506 	if (pfil_bridge == 0 && pfil_member == 0 && pfil_ipfw == 0)
2507 		return 0; /* filtering is disabled */
2508 
2509 	i = min((*mp)->m_pkthdr.len, max_protohdr);
2510 	if ((*mp)->m_len < i) {
2511 	    *mp = m_pullup(*mp, i);
2512 	    if (*mp == NULL) {
2513 		printf("%s: m_pullup failed\n", __func__);
2514 		return -1;
2515 	    }
2516 	}
2517 
2518 	eh1 = mtod(*mp, struct ether_header *);
2519 	ether_type = ntohs(eh1->ether_type);
2520 
2521 	/*
2522 	 * Check for SNAP/LLC.
2523 	 */
2524 	if (ether_type < ETHERMTU) {
2525 		struct llc *llc2 = (struct llc *)(eh1 + 1);
2526 
2527 		if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2528 		    llc2->llc_dsap == LLC_SNAP_LSAP &&
2529 		    llc2->llc_ssap == LLC_SNAP_LSAP &&
2530 		    llc2->llc_control == LLC_UI) {
2531 			ether_type = htons(llc2->llc_un.type_snap.ether_type);
2532 			snap = 1;
2533 		}
2534 	}
2535 
2536 	/*
2537 	 * If we're trying to filter bridge traffic, don't look at anything
2538 	 * other than IP and ARP traffic.  If the filter doesn't understand
2539 	 * IPv6, don't allow IPv6 through the bridge either.  This is lame
2540 	 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2541 	 * but of course we don't have an AppleTalk filter to begin with.
2542 	 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2543 	 * ARP traffic.)
2544 	 */
2545 	switch (ether_type) {
2546 		case ETHERTYPE_ARP:
2547 		case ETHERTYPE_REVARP:
2548 			return 0; /* Automatically pass */
2549 		case ETHERTYPE_IP:
2550 # ifdef INET6
2551 		case ETHERTYPE_IPV6:
2552 # endif /* INET6 */
2553 			break;
2554 		default:
2555 			/*
2556 			 * Check to see if the user wants to pass non-ip
2557 			 * packets, these will not be checked by pfil(9) and
2558 			 * passed unconditionally so the default is to drop.
2559 			 */
2560 			if (pfil_onlyip)
2561 				goto bad;
2562 	}
2563 
2564 	/* Strip off the Ethernet header and keep a copy. */
2565 	m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2566 	m_adj(*mp, ETHER_HDR_LEN);
2567 
2568 	/* Strip off snap header, if present */
2569 	if (snap) {
2570 		m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2571 		m_adj(*mp, sizeof(struct llc));
2572 	}
2573 
2574 	/*
2575 	 * Check the IP header for alignment and errors
2576 	 */
2577 	if (dir == PFIL_IN) {
2578 		switch (ether_type) {
2579 			case ETHERTYPE_IP:
2580 				error = bridge_ip_checkbasic(mp);
2581 				break;
2582 # ifdef INET6
2583 			case ETHERTYPE_IPV6:
2584 				error = bridge_ip6_checkbasic(mp);
2585 				break;
2586 # endif /* INET6 */
2587 			default:
2588 				error = 0;
2589 		}
2590 		if (error)
2591 			goto bad;
2592 	}
2593 
2594 	if (IPFW_LOADED && pfil_ipfw != 0 && dir == PFIL_OUT && ifp != NULL) {
2595 		error = -1;
2596 		args.rule = ip_dn_claim_rule(*mp);
2597 		if (args.rule != NULL && fw_one_pass)
2598 			goto ipfwpass; /* packet already partially processed */
2599 
2600 		args.m = *mp;
2601 		args.oif = ifp;
2602 		args.next_hop = NULL;
2603 		args.eh = &eh2;
2604 		i = ip_fw_chk_ptr(&args);
2605 		*mp = args.m;
2606 
2607 		if (*mp == NULL)
2608 			return error;
2609 
2610 		if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
2611 
2612 			/* put the Ethernet header back on */
2613 			M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
2614 			if (*mp == NULL)
2615 				return error;
2616 			bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2617 
2618 			/*
2619 			 * Pass the pkt to dummynet, which consumes it. The
2620 			 * packet will return to us via bridge_dummynet().
2621 			 */
2622 			args.oif = ifp;
2623 			ip_dn_io_ptr(*mp, DN_TO_IFB_FWD, &args);
2624 			return error;
2625 		}
2626 
2627 		if (i != IP_FW_PASS) /* drop */
2628 			goto bad;
2629 	}
2630 
2631 ipfwpass:
2632 	error = 0;
2633 
2634 	/*
2635 	 * Run the packet through pfil
2636 	 */
2637 	switch (ether_type)
2638 	{
2639 	case ETHERTYPE_IP :
2640 		/*
2641 		 * before calling the firewall, swap fields the same as
2642 		 * IP does. here we assume the header is contiguous
2643 		 */
2644 		ip = mtod(*mp, struct ip *);
2645 
2646 		ip->ip_len = ntohs(ip->ip_len);
2647 		ip->ip_off = ntohs(ip->ip_off);
2648 
2649 		/*
2650 		 * Run pfil on the member interface and the bridge, both can
2651 		 * be skipped by clearing pfil_member or pfil_bridge.
2652 		 *
2653 		 * Keep the order:
2654 		 *   in_if -> bridge_if -> out_if
2655 		 */
2656 		if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2657 			error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2658 					dir, NULL);
2659 
2660 		if (*mp == NULL || error != 0) /* filter may consume */
2661 			break;
2662 
2663 		if (pfil_member && ifp != NULL)
2664 			error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2665 					dir, NULL);
2666 
2667 		if (*mp == NULL || error != 0) /* filter may consume */
2668 			break;
2669 
2670 		if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2671 			error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2672 					dir, NULL);
2673 
2674 		/* Restore ip and the fields ntohs()'d. */
2675 		if (*mp != NULL && error == 0) {
2676 			ip = mtod(*mp, struct ip *);
2677 			ip->ip_len = htons(ip->ip_len);
2678 			ip->ip_off = htons(ip->ip_off);
2679 		}
2680 
2681 		break;
2682 # ifdef INET6
2683 	case ETHERTYPE_IPV6 :
2684 		if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2685 			error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2686 					dir, NULL);
2687 
2688 		if (*mp == NULL || error != 0) /* filter may consume */
2689 			break;
2690 
2691 		if (pfil_member && ifp != NULL)
2692 			error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2693 					dir, NULL);
2694 
2695 		if (*mp == NULL || error != 0) /* filter may consume */
2696 			break;
2697 
2698 		if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2699 			error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2700 					dir, NULL);
2701 		break;
2702 # endif
2703 	default :
2704 		error = 0;
2705 		break;
2706 	}
2707 
2708 	if (*mp == NULL)
2709 		return error;
2710 	if (error != 0)
2711 		goto bad;
2712 
2713 	error = -1;
2714 
2715 	/*
2716 	 * Finally, put everything back the way it was and return
2717 	 */
2718 	if (snap) {
2719 		M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
2720 		if (*mp == NULL)
2721 			return error;
2722 		bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2723 	}
2724 
2725 	M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
2726 	if (*mp == NULL)
2727 		return error;
2728 	bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2729 
2730 	return 0;
2731 
2732 bad:
2733 	m_freem(*mp);
2734 	*mp = NULL;
2735 	return error;
2736 }
2737 
2738 /*
2739  * Perform basic checks on header size since
2740  * pfil assumes ip_input has already processed
2741  * it for it.  Cut-and-pasted from ip_input.c.
2742  * Given how simple the IPv6 version is,
2743  * does the IPv4 version really need to be
2744  * this complicated?
2745  *
2746  * XXX Should we update ipstat here, or not?
2747  * XXX Right now we update ipstat but not
2748  * XXX csum_counter.
2749  */
2750 static int
2751 bridge_ip_checkbasic(struct mbuf **mp)
2752 {
2753 	struct mbuf *m = *mp;
2754 	struct ip *ip;
2755 	int len, hlen;
2756 	u_short sum;
2757 
2758 	if (*mp == NULL)
2759 		return -1;
2760 
2761 	if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2762 		if ((m = m_copyup(m, sizeof(struct ip),
2763 			(max_linkhdr + 3) & ~3)) == NULL) {
2764 			/* XXXJRT new stat, please */
2765 			ipstat.ips_toosmall++;
2766 			goto bad;
2767 		}
2768 	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
2769 		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2770 			ipstat.ips_toosmall++;
2771 			goto bad;
2772 		}
2773 	}
2774 	ip = mtod(m, struct ip *);
2775 	if (ip == NULL) goto bad;
2776 
2777 	if (ip->ip_v != IPVERSION) {
2778 		ipstat.ips_badvers++;
2779 		goto bad;
2780 	}
2781 	hlen = ip->ip_hl << 2;
2782 	if (hlen < sizeof(struct ip)) { /* minimum header length */
2783 		ipstat.ips_badhlen++;
2784 		goto bad;
2785 	}
2786 	if (hlen > m->m_len) {
2787 		if ((m = m_pullup(m, hlen)) == 0) {
2788 			ipstat.ips_badhlen++;
2789 			goto bad;
2790 		}
2791 		ip = mtod(m, struct ip *);
2792 		if (ip == NULL) goto bad;
2793 	}
2794 
2795 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2796 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2797 	} else {
2798 		if (hlen == sizeof(struct ip)) {
2799 			sum = in_cksum_hdr(ip);
2800 		} else {
2801 			sum = in_cksum(m, hlen);
2802 		}
2803 	}
2804 	if (sum) {
2805 		ipstat.ips_badsum++;
2806 		goto bad;
2807 	}
2808 
2809 	/* Retrieve the packet length. */
2810 	len = ntohs(ip->ip_len);
2811 
2812 	/*
2813 	 * Check for additional length bogosity
2814 	 */
2815 	if (len < hlen) {
2816 		ipstat.ips_badlen++;
2817 		goto bad;
2818 	}
2819 
2820 	/*
2821 	 * Check that the amount of data in the buffers
2822 	 * is as at least much as the IP header would have us expect.
2823 	 * Drop packet if shorter than we expect.
2824 	 */
2825 	if (m->m_pkthdr.len < len) {
2826 		ipstat.ips_tooshort++;
2827 		goto bad;
2828 	}
2829 
2830 	/* Checks out, proceed */
2831 	*mp = m;
2832 	return 0;
2833 
2834 bad:
2835 	*mp = m;
2836 	return -1;
2837 }
2838 
2839 # ifdef INET6
2840 /*
2841  * Same as above, but for IPv6.
2842  * Cut-and-pasted from ip6_input.c.
2843  * XXX Should we update ip6stat, or not?
2844  */
2845 static int
2846 bridge_ip6_checkbasic(struct mbuf **mp)
2847 {
2848 	struct mbuf *m = *mp;
2849 	struct ip6_hdr *ip6;
2850 
2851 	/*
2852 	 * If the IPv6 header is not aligned, slurp it up into a new
2853 	 * mbuf with space for link headers, in the event we forward
2854 	 * it.  Otherwise, if it is aligned, make sure the entire base
2855 	 * IPv6 header is in the first mbuf of the chain.
2856 	 */
2857 	if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2858 		struct ifnet *inifp = m->m_pkthdr.rcvif;
2859 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2860 			    (max_linkhdr + 3) & ~3)) == NULL) {
2861 			/* XXXJRT new stat, please */
2862 			ip6stat.ip6s_toosmall++;
2863 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2864 			goto bad;
2865 		}
2866 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2867 		struct ifnet *inifp = m->m_pkthdr.rcvif;
2868 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2869 			ip6stat.ip6s_toosmall++;
2870 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2871 			goto bad;
2872 		}
2873 	}
2874 
2875 	ip6 = mtod(m, struct ip6_hdr *);
2876 
2877 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2878 		ip6stat.ip6s_badvers++;
2879 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2880 		goto bad;
2881 	}
2882 
2883 	/* Checks out, proceed */
2884 	*mp = m;
2885 	return 0;
2886 
2887 bad:
2888 	*mp = m;
2889 	return -1;
2890 }
2891 # endif /* INET6 */
2892