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