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