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