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