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