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