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