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