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