xref: /freebsd/sys/net/if_bridge.c (revision e7be843b4a162e68651d3911f0357ed464915629)
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) = false;
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");
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 	ether_reassign(ifp, newvnet, arg);
806 }
807 #endif
808 
809 /*
810  * bridge_get_softc:
811  *
812  * Return the bridge softc for an ifnet.
813  */
814 static void *
815 bridge_get_softc(struct ifnet *ifp)
816 {
817 	struct bridge_iflist *bif;
818 
819 	NET_EPOCH_ASSERT();
820 
821 	bif = ifp->if_bridge;
822 	if (bif == NULL)
823 		return (NULL);
824 	return (bif->bif_sc);
825 }
826 
827 /*
828  * bridge_same:
829  *
830  * Return true if two interfaces are in the same bridge.  This is only used by
831  * bridgestp via bridge_same_p.
832  */
833 static bool
834 bridge_same(const void *bifap, const void *bifbp)
835 {
836 	const struct bridge_iflist *bifa = bifap, *bifb = bifbp;
837 
838 	NET_EPOCH_ASSERT();
839 
840 	if (bifa == NULL || bifb == NULL)
841 		return (false);
842 
843 	return (bifa->bif_sc == bifb->bif_sc);
844 }
845 
846 /*
847  * bridge_clone_create:
848  *
849  *	Create a new bridge instance.
850  */
851 static int
852 bridge_clone_create(struct if_clone *ifc, char *name, size_t len,
853     struct ifc_data *ifd, struct ifnet **ifpp)
854 {
855 	struct bridge_softc *sc;
856 	struct ifnet *ifp;
857 
858 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
859 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
860 
861 	BRIDGE_LOCK_INIT(sc);
862 	sc->sc_brtmax = BRIDGE_RTABLE_MAX;
863 	sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
864 
865 	/* Initialize our routing table. */
866 	bridge_rtable_init(sc);
867 
868 	callout_init_mtx(&sc->sc_brcallout, &sc->sc_rt_mtx, 0);
869 
870 	CK_LIST_INIT(&sc->sc_iflist);
871 	CK_LIST_INIT(&sc->sc_spanlist);
872 
873 	ifp->if_softc = sc;
874 	if_initname(ifp, bridge_name, ifd->unit);
875 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
876 	ifp->if_capabilities = ifp->if_capenable = IFCAP_VLAN_HWTAGGING;
877 	ifp->if_ioctl = bridge_ioctl;
878 #ifdef ALTQ
879 	ifp->if_start = bridge_altq_start;
880 	ifp->if_transmit = bridge_altq_transmit;
881 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
882 	ifp->if_snd.ifq_drv_maxlen = 0;
883 	IFQ_SET_READY(&ifp->if_snd);
884 #else
885 	ifp->if_transmit = bridge_transmit;
886 #endif
887 	ifp->if_qflush = bridge_qflush;
888 	ifp->if_init = bridge_init;
889 	ifp->if_type = IFT_BRIDGE;
890 
891 	ether_gen_addr(ifp, &sc->sc_defaddr);
892 
893 	bstp_attach(&sc->sc_stp, &bridge_ops);
894 	ether_ifattach(ifp, sc->sc_defaddr.octet);
895 	/* Now undo some of the damage... */
896 	ifp->if_baudrate = 0;
897 #ifdef VIMAGE
898 	ifp->if_reassign = bridge_reassign;
899 #endif
900 	sc->sc_if_input = ifp->if_input;	/* ether_input */
901 	ifp->if_input = bridge_inject;
902 
903 	/*
904 	 * Allow BRIDGE_INPUT() to pass in packets originating from the bridge
905 	 * itself via bridge_inject().  This is required for netmap but
906 	 * otherwise has no effect.
907 	 */
908 	ifp->if_bridge_input = bridge_input;
909 
910 	BRIDGE_LIST_LOCK();
911 	LIST_INSERT_HEAD(&V_bridge_list, sc, sc_list);
912 	BRIDGE_LIST_UNLOCK();
913 	*ifpp = ifp;
914 
915 	return (0);
916 }
917 
918 static void
919 bridge_clone_destroy_cb(struct epoch_context *ctx)
920 {
921 	struct bridge_softc *sc;
922 
923 	sc = __containerof(ctx, struct bridge_softc, sc_epoch_ctx);
924 
925 	BRIDGE_LOCK_DESTROY(sc);
926 	free(sc, M_DEVBUF);
927 }
928 
929 /*
930  * bridge_clone_destroy:
931  *
932  *	Destroy a bridge instance.
933  */
934 static int
935 bridge_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
936 {
937 	struct bridge_softc *sc = ifp->if_softc;
938 	struct bridge_iflist *bif;
939 	struct epoch_tracker et;
940 
941 	BRIDGE_LOCK(sc);
942 
943 	bridge_stop(ifp, 1);
944 	ifp->if_flags &= ~IFF_UP;
945 
946 	while ((bif = CK_LIST_FIRST(&sc->sc_iflist)) != NULL)
947 		bridge_delete_member(sc, bif, 0);
948 
949 	while ((bif = CK_LIST_FIRST(&sc->sc_spanlist)) != NULL) {
950 		bridge_delete_span(sc, bif);
951 	}
952 
953 	/* Tear down the routing table. */
954 	bridge_rtable_fini(sc);
955 
956 	BRIDGE_UNLOCK(sc);
957 
958 	NET_EPOCH_ENTER(et);
959 
960 	callout_drain(&sc->sc_brcallout);
961 
962 	BRIDGE_LIST_LOCK();
963 	LIST_REMOVE(sc, sc_list);
964 	BRIDGE_LIST_UNLOCK();
965 
966 	bstp_detach(&sc->sc_stp);
967 #ifdef ALTQ
968 	IFQ_PURGE(&ifp->if_snd);
969 #endif
970 	NET_EPOCH_EXIT(et);
971 
972 	ether_ifdetach(ifp);
973 	if_free(ifp);
974 
975 	NET_EPOCH_CALL(bridge_clone_destroy_cb, &sc->sc_epoch_ctx);
976 
977 	return (0);
978 }
979 
980 /*
981  * bridge_ioctl:
982  *
983  *	Handle a control request from the operator.
984  */
985 static int
986 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
987 {
988 	struct bridge_softc *sc = ifp->if_softc;
989 	struct ifreq *ifr = (struct ifreq *)data;
990 	struct bridge_iflist *bif;
991 	struct thread *td = curthread;
992 	union {
993 		struct ifbreq ifbreq;
994 		struct ifbifconf ifbifconf;
995 		struct ifbareq ifbareq;
996 		struct ifbaconf ifbaconf;
997 		struct ifbrparam ifbrparam;
998 		struct ifbropreq ifbropreq;
999 		struct ifbif_vlan_req ifvlanreq;
1000 	} args;
1001 	struct ifdrv *ifd = (struct ifdrv *) data;
1002 	const struct bridge_control *bc;
1003 	int error = 0, oldmtu;
1004 
1005 	BRIDGE_LOCK(sc);
1006 
1007 	switch (cmd) {
1008 	case SIOCADDMULTI:
1009 	case SIOCDELMULTI:
1010 		break;
1011 
1012 	case SIOCGDRVSPEC:
1013 	case SIOCSDRVSPEC:
1014 		if (ifd->ifd_cmd >= bridge_control_table_size) {
1015 			error = EXTERROR(EINVAL, "Invalid control command");
1016 			break;
1017 		}
1018 		bc = &bridge_control_table[ifd->ifd_cmd];
1019 
1020 		if (cmd == SIOCGDRVSPEC &&
1021 		    (bc->bc_flags & BC_F_COPYOUT) == 0) {
1022 			error = EXTERROR(EINVAL,
1023 			    "Inappropriate ioctl for command "
1024 			    "(expected SIOCSDRVSPEC)");
1025 			break;
1026 		}
1027 		else if (cmd == SIOCSDRVSPEC &&
1028 		    (bc->bc_flags & BC_F_COPYOUT) != 0) {
1029 			error = EXTERROR(EINVAL,
1030 			    "Inappropriate ioctl for command "
1031 			    "(expected SIOCGDRVSPEC)");
1032 			break;
1033 		}
1034 
1035 		if (bc->bc_flags & BC_F_SUSER) {
1036 			error = priv_check(td, PRIV_NET_BRIDGE);
1037 			if (error) {
1038 				EXTERROR(error, "PRIV_NET_BRIDGE required");
1039 				break;
1040 			}
1041 		}
1042 
1043 		if (ifd->ifd_len != bc->bc_argsize ||
1044 		    ifd->ifd_len > sizeof(args)) {
1045 			error = EXTERROR(EINVAL, "Invalid argument size");
1046 			break;
1047 		}
1048 
1049 		bzero(&args, sizeof(args));
1050 		if (bc->bc_flags & BC_F_COPYIN) {
1051 			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
1052 			if (error)
1053 				break;
1054 		}
1055 
1056 		oldmtu = ifp->if_mtu;
1057 		error = (*bc->bc_func)(sc, &args);
1058 		if (error)
1059 			break;
1060 
1061 		/*
1062 		 * Bridge MTU may change during addition of the first port.
1063 		 * If it did, do network layer specific procedure.
1064 		 */
1065 		if (ifp->if_mtu != oldmtu)
1066 			if_notifymtu(ifp);
1067 
1068 		if (bc->bc_flags & BC_F_COPYOUT)
1069 			error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
1070 
1071 		break;
1072 
1073 	case SIOCSIFFLAGS:
1074 		if (!(ifp->if_flags & IFF_UP) &&
1075 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1076 			/*
1077 			 * If interface is marked down and it is running,
1078 			 * then stop and disable it.
1079 			 */
1080 			bridge_stop(ifp, 1);
1081 		} else if ((ifp->if_flags & IFF_UP) &&
1082 		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1083 			/*
1084 			 * If interface is marked up and it is stopped, then
1085 			 * start it.
1086 			 */
1087 			BRIDGE_UNLOCK(sc);
1088 			(*ifp->if_init)(sc);
1089 			BRIDGE_LOCK(sc);
1090 		}
1091 		break;
1092 
1093 	case SIOCSIFMTU:
1094 		oldmtu = sc->sc_ifp->if_mtu;
1095 
1096 		if (ifr->ifr_mtu < IF_MINMTU) {
1097 			error = EXTERROR(EINVAL,
1098 			    "Requested MTU is lower than IF_MINMTU");
1099 			break;
1100 		}
1101 		if (CK_LIST_EMPTY(&sc->sc_iflist)) {
1102 			sc->sc_ifp->if_mtu = ifr->ifr_mtu;
1103 			break;
1104 		}
1105 		CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1106 			error = (*bif->bif_ifp->if_ioctl)(bif->bif_ifp,
1107 			    SIOCSIFMTU, (caddr_t)ifr);
1108 			if (error != 0) {
1109 				log(LOG_NOTICE, "%s: invalid MTU: %u for"
1110 				    " member %s\n", sc->sc_ifp->if_xname,
1111 				    ifr->ifr_mtu,
1112 				    bif->bif_ifp->if_xname);
1113 				error = EINVAL;
1114 				break;
1115 			}
1116 		}
1117 		if (error) {
1118 			/* Restore the previous MTU on all member interfaces. */
1119 			ifr->ifr_mtu = oldmtu;
1120 			CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1121 				(*bif->bif_ifp->if_ioctl)(bif->bif_ifp,
1122 				    SIOCSIFMTU, (caddr_t)ifr);
1123 			}
1124 			EXTERROR(error,
1125 			    "Failed to set MTU on member interface");
1126 		} else {
1127 			sc->sc_ifp->if_mtu = ifr->ifr_mtu;
1128 		}
1129 		break;
1130 	default:
1131 		/*
1132 		 * drop the lock as ether_ioctl() will call bridge_start() and
1133 		 * cause the lock to be recursed.
1134 		 */
1135 		BRIDGE_UNLOCK(sc);
1136 		error = ether_ioctl(ifp, cmd, data);
1137 		BRIDGE_LOCK(sc);
1138 		break;
1139 	}
1140 
1141 	BRIDGE_UNLOCK(sc);
1142 
1143 	return (error);
1144 }
1145 
1146 /*
1147  * bridge_mutecaps:
1148  *
1149  *	Clear or restore unwanted capabilities on the member interface
1150  */
1151 static void
1152 bridge_mutecaps(struct bridge_softc *sc)
1153 {
1154 	struct bridge_iflist *bif;
1155 	int enabled, mask;
1156 
1157 	BRIDGE_LOCK_ASSERT(sc);
1158 
1159 	/* Initial bitmask of capabilities to test */
1160 	mask = BRIDGE_IFCAPS_MASK;
1161 
1162 	CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1163 		/* Every member must support it or it's disabled */
1164 		mask &= bif->bif_savedcaps;
1165 	}
1166 
1167 	CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1168 		enabled = bif->bif_ifp->if_capenable;
1169 		enabled &= ~BRIDGE_IFCAPS_STRIP;
1170 		/* Strip off mask bits and enable them again if allowed */
1171 		enabled &= ~BRIDGE_IFCAPS_MASK;
1172 		enabled |= mask;
1173 		bridge_set_ifcap(sc, bif, enabled);
1174 	}
1175 }
1176 
1177 static void
1178 bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set)
1179 {
1180 	struct ifnet *ifp = bif->bif_ifp;
1181 	struct ifreq ifr;
1182 	int error, mask, stuck;
1183 
1184 	bzero(&ifr, sizeof(ifr));
1185 	ifr.ifr_reqcap = set;
1186 
1187 	if (ifp->if_capenable != set) {
1188 		error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
1189 		if (error)
1190 			if_printf(sc->sc_ifp,
1191 			    "error setting capabilities on %s: %d\n",
1192 			    ifp->if_xname, error);
1193 		mask = BRIDGE_IFCAPS_MASK | BRIDGE_IFCAPS_STRIP;
1194 		stuck = ifp->if_capenable & mask & ~set;
1195 		if (stuck != 0)
1196 			if_printf(sc->sc_ifp,
1197 			    "can't disable some capabilities on %s: 0x%x\n",
1198 			    ifp->if_xname, stuck);
1199 	}
1200 }
1201 
1202 /*
1203  * bridge_lookup_member:
1204  *
1205  *	Lookup a bridge member interface.
1206  */
1207 static struct bridge_iflist *
1208 bridge_lookup_member(struct bridge_softc *sc, const char *name)
1209 {
1210 	struct bridge_iflist *bif;
1211 	struct ifnet *ifp;
1212 
1213 	BRIDGE_LOCK_OR_NET_EPOCH_ASSERT(sc);
1214 
1215 	CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1216 		ifp = bif->bif_ifp;
1217 		if (strcmp(ifp->if_xname, name) == 0)
1218 			return (bif);
1219 	}
1220 
1221 	return (NULL);
1222 }
1223 
1224 /*
1225  * bridge_lookup_member_if:
1226  *
1227  *	Lookup a bridge member interface by ifnet*.
1228  */
1229 static struct bridge_iflist *
1230 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
1231 {
1232 	BRIDGE_LOCK_OR_NET_EPOCH_ASSERT(sc);
1233 	return (member_ifp->if_bridge);
1234 }
1235 
1236 static void
1237 bridge_delete_member_cb(struct epoch_context *ctx)
1238 {
1239 	struct bridge_iflist *bif;
1240 
1241 	bif = __containerof(ctx, struct bridge_iflist, bif_epoch_ctx);
1242 
1243 	free(bif, M_DEVBUF);
1244 }
1245 
1246 /*
1247  * bridge_delete_member:
1248  *
1249  *	Delete the specified member interface.
1250  */
1251 static void
1252 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
1253     int gone)
1254 {
1255 	struct ifnet *ifs = bif->bif_ifp;
1256 	struct ifnet *fif = NULL;
1257 	struct bridge_iflist *bifl;
1258 
1259 	BRIDGE_LOCK_ASSERT(sc);
1260 
1261 	if (bif->bif_flags & IFBIF_STP)
1262 		bstp_disable(&bif->bif_stp);
1263 
1264 	ifs->if_bridge = NULL;
1265 	CK_LIST_REMOVE(bif, bif_next);
1266 
1267 	/*
1268 	 * If removing the interface that gave the bridge its mac address, set
1269 	 * the mac address of the bridge to the address of the next member, or
1270 	 * to its default address if no members are left.
1271 	 */
1272 	if (V_bridge_inherit_mac && sc->sc_ifaddr == ifs) {
1273 		if (CK_LIST_EMPTY(&sc->sc_iflist)) {
1274 			bcopy(&sc->sc_defaddr,
1275 			    IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1276 			sc->sc_ifaddr = NULL;
1277 		} else {
1278 			bifl = CK_LIST_FIRST(&sc->sc_iflist);
1279 			fif = bifl->bif_ifp;
1280 			bcopy(IF_LLADDR(fif),
1281 			    IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1282 			sc->sc_ifaddr = fif;
1283 		}
1284 		EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
1285 	}
1286 
1287 	bridge_linkcheck(sc);
1288 	bridge_mutecaps(sc);	/* recalcuate now this interface is removed */
1289 	BRIDGE_RT_LOCK(sc);
1290 	bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
1291 	BRIDGE_RT_UNLOCK(sc);
1292 	KASSERT(bif->bif_addrcnt == 0,
1293 	    ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt));
1294 
1295 	ifs->if_bridge_output = NULL;
1296 	ifs->if_bridge_input = NULL;
1297 	ifs->if_bridge_linkstate = NULL;
1298 	if (!gone) {
1299 		switch (ifs->if_type) {
1300 		case IFT_ETHER:
1301 		case IFT_L2VLAN:
1302 			/*
1303 			 * Take the interface out of promiscuous mode, but only
1304 			 * if it was promiscuous in the first place. It might
1305 			 * not be if we're in the bridge_ioctl_add() error path.
1306 			 */
1307 			if (ifs->if_flags & IFF_PROMISC)
1308 				(void) ifpromisc(ifs, 0);
1309 			break;
1310 
1311 		case IFT_GIF:
1312 			break;
1313 
1314 		default:
1315 #ifdef DIAGNOSTIC
1316 			panic("bridge_delete_member: impossible");
1317 #endif
1318 			break;
1319 		}
1320 		/* Re-enable any interface capabilities */
1321 		bridge_set_ifcap(sc, bif, bif->bif_savedcaps);
1322 	}
1323 	bstp_destroy(&bif->bif_stp);	/* prepare to free */
1324 
1325 	NET_EPOCH_CALL(bridge_delete_member_cb, &bif->bif_epoch_ctx);
1326 }
1327 
1328 /*
1329  * bridge_delete_span:
1330  *
1331  *	Delete the specified span interface.
1332  */
1333 static void
1334 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1335 {
1336 	BRIDGE_LOCK_ASSERT(sc);
1337 
1338 	KASSERT(bif->bif_ifp->if_bridge == NULL,
1339 	    ("%s: not a span interface", __func__));
1340 
1341 	CK_LIST_REMOVE(bif, bif_next);
1342 
1343 	NET_EPOCH_CALL(bridge_delete_member_cb, &bif->bif_epoch_ctx);
1344 }
1345 
1346 static int
1347 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1348 {
1349 	struct ifbreq *req = arg;
1350 	struct bridge_iflist *bif = NULL;
1351 	struct ifnet *ifs;
1352 	int error = 0;
1353 
1354 	ifs = ifunit(req->ifbr_ifsname);
1355 	if (ifs == NULL)
1356 		return (EXTERROR(ENOENT, "No such interface",
1357 		    req->ifbr_ifsname));
1358 	if (ifs->if_ioctl == NULL)	/* must be supported */
1359 		return (EXTERROR(EINVAL, "Interface must support ioctl(2)"));
1360 
1361 	/*
1362 	 * If the new interface is a vlan(4), it could be a bridge SVI.
1363 	 * Don't allow such things to be added to bridges.
1364 	 */
1365 	if (ifs->if_type == IFT_L2VLAN) {
1366 		struct ifnet *parent;
1367 		struct epoch_tracker et;
1368 		bool is_bridge;
1369 
1370 		/*
1371 		 * Entering NET_EPOCH with BRIDGE_LOCK held, but this is okay
1372 		 * since we don't sleep here.
1373 		 */
1374 		NET_EPOCH_ENTER(et);
1375 		parent = VLAN_TRUNKDEV(ifs);
1376 		is_bridge = (parent != NULL && parent->if_type == IFT_BRIDGE);
1377 		NET_EPOCH_EXIT(et);
1378 
1379 		if (is_bridge)
1380 			return (EXTERROR(EINVAL,
1381 			    "Bridge SVI cannot be added to a bridge"));
1382 	}
1383 
1384 	/* If it's in the span list, it can't be a member. */
1385 	CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1386 		if (ifs == bif->bif_ifp)
1387 			return (EXTERROR(EBUSY,
1388 			    "Span interface cannot be a member"));
1389 
1390 	if (ifs->if_bridge) {
1391 		struct bridge_iflist *sbif = ifs->if_bridge;
1392 		if (sbif->bif_sc == sc)
1393 			return (EXTERROR(EEXIST,
1394 			    "Interface is already a member of this bridge"));
1395 
1396 		return (EXTERROR(EBUSY,
1397 		    "Interface is already a member of another bridge"));
1398 	}
1399 
1400 	switch (ifs->if_type) {
1401 	case IFT_ETHER:
1402 	case IFT_L2VLAN:
1403 	case IFT_GIF:
1404 		/* permitted interface types */
1405 		break;
1406 	default:
1407 		return (EXTERROR(EINVAL, "Unsupported interface type"));
1408 	}
1409 
1410 #ifdef INET6
1411 	/*
1412 	 * Two valid inet6 addresses with link-local scope must not be
1413 	 * on the parent interface and the member interfaces at the
1414 	 * same time.  This restriction is needed to prevent violation
1415 	 * of link-local scope zone.  Attempts to add a member
1416 	 * interface which has inet6 addresses when the parent has
1417 	 * inet6 triggers removal of all inet6 addresses on the member
1418 	 * interface.
1419 	 */
1420 
1421 	/* Check if the parent interface has a link-local scope addr. */
1422 	if (V_allow_llz_overlap == 0 &&
1423 	    in6ifa_llaonifp(sc->sc_ifp) != NULL) {
1424 		/*
1425 		 * If any, remove all inet6 addresses from the member
1426 		 * interfaces.
1427 		 */
1428 		CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1429  			if (in6ifa_llaonifp(bif->bif_ifp)) {
1430 				in6_ifdetach(bif->bif_ifp);
1431 				if_printf(sc->sc_ifp,
1432 				    "IPv6 addresses on %s have been removed "
1433 				    "before adding it as a member to prevent "
1434 				    "IPv6 address scope violation.\n",
1435 				    bif->bif_ifp->if_xname);
1436 			}
1437 		}
1438 		if (in6ifa_llaonifp(ifs)) {
1439 			in6_ifdetach(ifs);
1440 			if_printf(sc->sc_ifp,
1441 			    "IPv6 addresses on %s have been removed "
1442 			    "before adding it as a member to prevent "
1443 			    "IPv6 address scope violation.\n",
1444 			    ifs->if_xname);
1445 		}
1446 	}
1447 #endif
1448 
1449 	/*
1450 	 * If member_ifaddrs is disabled, do not allow an interface with
1451 	 * assigned IP addresses to be added to a bridge.
1452 	 */
1453 	if (!V_member_ifaddrs) {
1454 		struct ifaddr *ifa;
1455 
1456 		CK_STAILQ_FOREACH(ifa, &ifs->if_addrhead, ifa_link) {
1457 #ifdef INET
1458 			if (ifa->ifa_addr->sa_family == AF_INET)
1459 				return (EXTERROR(EINVAL,
1460 				    "Member interface may not have "
1461 				    "an IPv4 address configured"));
1462 #endif
1463 #ifdef INET6
1464 			if (ifa->ifa_addr->sa_family == AF_INET6)
1465 				return (EXTERROR(EINVAL,
1466 				    "Member interface may not have "
1467 				    "an IPv6 address configured"));
1468 #endif
1469 		}
1470 	}
1471 
1472 	/* Allow the first Ethernet member to define the MTU */
1473 	if (CK_LIST_EMPTY(&sc->sc_iflist))
1474 		sc->sc_ifp->if_mtu = ifs->if_mtu;
1475 	else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
1476 		struct ifreq ifr;
1477 
1478 		snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s",
1479 		    ifs->if_xname);
1480 		ifr.ifr_mtu = sc->sc_ifp->if_mtu;
1481 
1482 		error = (*ifs->if_ioctl)(ifs,
1483 		    SIOCSIFMTU, (caddr_t)&ifr);
1484 		if (error != 0) {
1485 			log(LOG_NOTICE, "%s: invalid MTU: %u for"
1486 			    " new member %s\n", sc->sc_ifp->if_xname,
1487 			    ifr.ifr_mtu,
1488 			    ifs->if_xname);
1489 			return (EXTERROR(EINVAL,
1490 			    "Failed to set MTU on new member"));
1491 		}
1492 	}
1493 
1494 	bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1495 	if (bif == NULL)
1496 		return (ENOMEM);
1497 
1498 	bif->bif_sc = sc;
1499 	bif->bif_ifp = ifs;
1500 	bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
1501 	bif->bif_savedcaps = ifs->if_capenable;
1502 	bif->bif_vlanproto = ETHERTYPE_VLAN;
1503 	if (sc->sc_flags & IFBRF_VLANFILTER)
1504 		bif->bif_pvid = sc->sc_defpvid;
1505 	if (sc->sc_flags & IFBRF_DEFQINQ)
1506 		bif->bif_flags |= IFBIF_QINQ;
1507 
1508 	/*
1509 	 * Assign the interface's MAC address to the bridge if it's the first
1510 	 * member and the MAC address of the bridge has not been changed from
1511 	 * the default randomly generated one.
1512 	 */
1513 	if (V_bridge_inherit_mac && CK_LIST_EMPTY(&sc->sc_iflist) &&
1514 	    !memcmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr.octet, ETHER_ADDR_LEN)) {
1515 		bcopy(IF_LLADDR(ifs), IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
1516 		sc->sc_ifaddr = ifs;
1517 		EVENTHANDLER_INVOKE(iflladdr_event, sc->sc_ifp);
1518 	}
1519 
1520 	ifs->if_bridge = bif;
1521 	ifs->if_bridge_output = bridge_output;
1522 	ifs->if_bridge_input = bridge_input;
1523 	ifs->if_bridge_linkstate = bridge_linkstate;
1524 	bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
1525 	/*
1526 	 * XXX: XLOCK HERE!?!
1527 	 *
1528 	 * NOTE: insert_***HEAD*** should be safe for the traversals.
1529 	 */
1530 	CK_LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
1531 
1532 	/* Set interface capabilities to the intersection set of all members */
1533 	bridge_mutecaps(sc);
1534 	bridge_linkcheck(sc);
1535 
1536 	/* Place the interface into promiscuous mode */
1537 	switch (ifs->if_type) {
1538 		case IFT_ETHER:
1539 		case IFT_L2VLAN:
1540 			error = ifpromisc(ifs, 1);
1541 			break;
1542 	}
1543 
1544 	if (error)
1545 		bridge_delete_member(sc, bif, 0);
1546 	return (error);
1547 }
1548 
1549 static int
1550 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1551 {
1552 	struct ifbreq *req = arg;
1553 	struct bridge_iflist *bif;
1554 
1555 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1556 	if (bif == NULL)
1557 		return (EXTERROR(ENOENT, "Interface is not a bridge member"));
1558 
1559 	bridge_delete_member(sc, bif, 0);
1560 
1561 	return (0);
1562 }
1563 
1564 static int
1565 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1566 {
1567 	struct ifbreq *req = arg;
1568 	struct bridge_iflist *bif;
1569 	struct bstp_port *bp;
1570 
1571 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1572 	if (bif == NULL)
1573 		return (EXTERROR(ENOENT, "Interface is not a bridge member"));
1574 
1575 	bp = &bif->bif_stp;
1576 	req->ifbr_ifsflags = bif->bif_flags;
1577 	req->ifbr_state = bp->bp_state;
1578 	req->ifbr_priority = bp->bp_priority;
1579 	req->ifbr_path_cost = bp->bp_path_cost;
1580 	req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1581 	req->ifbr_proto = bp->bp_protover;
1582 	req->ifbr_role = bp->bp_role;
1583 	req->ifbr_stpflags = bp->bp_flags;
1584 	req->ifbr_addrcnt = bif->bif_addrcnt;
1585 	req->ifbr_addrmax = bif->bif_addrmax;
1586 	req->ifbr_addrexceeded = bif->bif_addrexceeded;
1587 	req->ifbr_pvid = bif->bif_pvid;
1588 	req->ifbr_vlanproto = bif->bif_vlanproto;
1589 
1590 	/* Copy STP state options as flags */
1591 	if (bp->bp_operedge)
1592 		req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
1593 	if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
1594 		req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
1595 	if (bp->bp_ptp_link)
1596 		req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
1597 	if (bp->bp_flags & BSTP_PORT_AUTOPTP)
1598 		req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
1599 	if (bp->bp_flags & BSTP_PORT_ADMEDGE)
1600 		req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE;
1601 	if (bp->bp_flags & BSTP_PORT_ADMCOST)
1602 		req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST;
1603 	return (0);
1604 }
1605 
1606 static int
1607 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1608 {
1609 	struct epoch_tracker et;
1610 	struct ifbreq *req = arg;
1611 	struct bridge_iflist *bif;
1612 	struct bstp_port *bp;
1613 	int error;
1614 
1615 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1616 	if (bif == NULL)
1617 		return (EXTERROR(ENOENT, "Interface is not a bridge member"));
1618 	bp = &bif->bif_stp;
1619 
1620 	if (req->ifbr_ifsflags & IFBIF_SPAN)
1621 		/* SPAN is readonly */
1622 		return (EXTERROR(EINVAL, "Span interface cannot be modified"));
1623 
1624 	NET_EPOCH_ENTER(et);
1625 
1626 	if (req->ifbr_ifsflags & IFBIF_STP) {
1627 		if ((bif->bif_flags & IFBIF_STP) == 0) {
1628 			error = bstp_enable(&bif->bif_stp);
1629 			if (error) {
1630 				NET_EPOCH_EXIT(et);
1631 				return (EXTERROR(error,
1632 				    "Failed to enable STP"));
1633 			}
1634 		}
1635 	} else {
1636 		if ((bif->bif_flags & IFBIF_STP) != 0)
1637 			bstp_disable(&bif->bif_stp);
1638 	}
1639 
1640 	/* Pass on STP flags */
1641 	bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
1642 	bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
1643 	bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
1644 	bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
1645 
1646 	/* Save the bits relating to the bridge */
1647 	bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK;
1648 
1649 	NET_EPOCH_EXIT(et);
1650 
1651 	return (0);
1652 }
1653 
1654 static int
1655 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1656 {
1657 	struct ifbrparam *param = arg;
1658 
1659 	sc->sc_brtmax = param->ifbrp_csize;
1660 	bridge_rttrim(sc);
1661 
1662 	return (0);
1663 }
1664 
1665 static int
1666 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1667 {
1668 	struct ifbrparam *param = arg;
1669 
1670 	param->ifbrp_csize = sc->sc_brtmax;
1671 
1672 	return (0);
1673 }
1674 
1675 static int
1676 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1677 {
1678 	struct ifbifconf *bifc = arg;
1679 	struct bridge_iflist *bif;
1680 	struct ifbreq breq;
1681 	char *buf, *outbuf;
1682 	int count, buflen, len, error = 0;
1683 
1684 	count = 0;
1685 	CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
1686 		count++;
1687 	CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1688 		count++;
1689 
1690 	buflen = sizeof(breq) * count;
1691 	if (bifc->ifbic_len == 0) {
1692 		bifc->ifbic_len = buflen;
1693 		return (0);
1694 	}
1695 	outbuf = malloc(buflen, M_TEMP, M_NOWAIT | M_ZERO);
1696 	if (outbuf == NULL)
1697 		return (ENOMEM);
1698 
1699 	count = 0;
1700 	buf = outbuf;
1701 	len = min(bifc->ifbic_len, buflen);
1702 	bzero(&breq, sizeof(breq));
1703 	CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1704 		if (len < sizeof(breq))
1705 			break;
1706 
1707 		strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1708 		    sizeof(breq.ifbr_ifsname));
1709 		/* Fill in the ifbreq structure */
1710 		error = bridge_ioctl_gifflags(sc, &breq);
1711 		if (error)
1712 			break;
1713 		memcpy(buf, &breq, sizeof(breq));
1714 		count++;
1715 		buf += sizeof(breq);
1716 		len -= sizeof(breq);
1717 	}
1718 	CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1719 		if (len < sizeof(breq))
1720 			break;
1721 
1722 		strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1723 		    sizeof(breq.ifbr_ifsname));
1724 		breq.ifbr_ifsflags = bif->bif_flags;
1725 		breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1726 		memcpy(buf, &breq, sizeof(breq));
1727 		count++;
1728 		buf += sizeof(breq);
1729 		len -= sizeof(breq);
1730 	}
1731 
1732 	bifc->ifbic_len = sizeof(breq) * count;
1733 	error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len);
1734 	free(outbuf, M_TEMP);
1735 	return (error);
1736 }
1737 
1738 static int
1739 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1740 {
1741 	struct ifbaconf *bac = arg;
1742 	struct bridge_rtnode *brt;
1743 	struct ifbareq bareq;
1744 	char *buf, *outbuf;
1745 	int count, buflen, len, error = 0;
1746 
1747 	if (bac->ifbac_len == 0)
1748 		return (0);
1749 
1750 	count = 0;
1751 	CK_LIST_FOREACH(brt, &sc->sc_rtlist, brt_list)
1752 		count++;
1753 	buflen = sizeof(bareq) * count;
1754 
1755 	outbuf = malloc(buflen, M_TEMP, M_NOWAIT | M_ZERO);
1756 	if (outbuf == NULL)
1757 		return (ENOMEM);
1758 
1759 	count = 0;
1760 	buf = outbuf;
1761 	len = min(bac->ifbac_len, buflen);
1762 	bzero(&bareq, sizeof(bareq));
1763 	CK_LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1764 		if (len < sizeof(bareq))
1765 			goto out;
1766 		strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1767 		    sizeof(bareq.ifba_ifsname));
1768 		memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1769 		bareq.ifba_vlan = brt->brt_vlan;
1770 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1771 				time_uptime < brt->brt_expire)
1772 			bareq.ifba_expire = brt->brt_expire - time_uptime;
1773 		else
1774 			bareq.ifba_expire = 0;
1775 		bareq.ifba_flags = brt->brt_flags;
1776 
1777 		memcpy(buf, &bareq, sizeof(bareq));
1778 		count++;
1779 		buf += sizeof(bareq);
1780 		len -= sizeof(bareq);
1781 	}
1782 out:
1783 	bac->ifbac_len = sizeof(bareq) * count;
1784 	error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len);
1785 	free(outbuf, M_TEMP);
1786 	return (error);
1787 }
1788 
1789 static int
1790 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1791 {
1792 	struct ifbareq *req = arg;
1793 	struct bridge_iflist *bif;
1794 	struct epoch_tracker et;
1795 	int error;
1796 
1797 	NET_EPOCH_ENTER(et);
1798 	bif = bridge_lookup_member(sc, req->ifba_ifsname);
1799 	if (bif == NULL) {
1800 		NET_EPOCH_EXIT(et);
1801 		return (EXTERROR(ENOENT, "Interface is not a bridge member"));
1802 	}
1803 
1804 	/* bridge_rtupdate() may acquire the lock. */
1805 	error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1,
1806 	    req->ifba_flags);
1807 	NET_EPOCH_EXIT(et);
1808 
1809 	return (error);
1810 }
1811 
1812 static int
1813 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1814 {
1815 	struct ifbrparam *param = arg;
1816 
1817 	sc->sc_brttimeout = param->ifbrp_ctime;
1818 	return (0);
1819 }
1820 
1821 static int
1822 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1823 {
1824 	struct ifbrparam *param = arg;
1825 
1826 	param->ifbrp_ctime = sc->sc_brttimeout;
1827 	return (0);
1828 }
1829 
1830 static int
1831 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1832 {
1833 	struct ifbareq *req = arg;
1834 	int vlan = req->ifba_vlan;
1835 
1836 	/* Userspace uses '0' to mean 'any vlan' */
1837 	if (vlan == 0)
1838 		vlan = DOT1Q_VID_RSVD_IMPL;
1839 
1840 	return (bridge_rtdaddr(sc, req->ifba_dst, vlan));
1841 }
1842 
1843 static int
1844 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1845 {
1846 	struct ifbreq *req = arg;
1847 
1848 	BRIDGE_RT_LOCK(sc);
1849 	bridge_rtflush(sc, req->ifbr_ifsflags);
1850 	BRIDGE_RT_UNLOCK(sc);
1851 
1852 	return (0);
1853 }
1854 
1855 static int
1856 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1857 {
1858 	struct ifbrparam *param = arg;
1859 	struct bstp_state *bs = &sc->sc_stp;
1860 
1861 	param->ifbrp_prio = bs->bs_bridge_priority;
1862 	return (0);
1863 }
1864 
1865 static int
1866 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1867 {
1868 	struct ifbrparam *param = arg;
1869 
1870 	return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio));
1871 }
1872 
1873 static int
1874 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1875 {
1876 	struct ifbrparam *param = arg;
1877 	struct bstp_state *bs = &sc->sc_stp;
1878 
1879 	param->ifbrp_hellotime = bs->bs_bridge_htime >> 8;
1880 	return (0);
1881 }
1882 
1883 static int
1884 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1885 {
1886 	struct ifbrparam *param = arg;
1887 
1888 	return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime));
1889 }
1890 
1891 static int
1892 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1893 {
1894 	struct ifbrparam *param = arg;
1895 	struct bstp_state *bs = &sc->sc_stp;
1896 
1897 	param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
1898 	return (0);
1899 }
1900 
1901 static int
1902 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1903 {
1904 	struct ifbrparam *param = arg;
1905 
1906 	return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay));
1907 }
1908 
1909 static int
1910 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1911 {
1912 	struct ifbrparam *param = arg;
1913 	struct bstp_state *bs = &sc->sc_stp;
1914 
1915 	param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
1916 	return (0);
1917 }
1918 
1919 static int
1920 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1921 {
1922 	struct ifbrparam *param = arg;
1923 
1924 	return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage));
1925 }
1926 
1927 static int
1928 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1929 {
1930 	struct ifbreq *req = arg;
1931 	struct bridge_iflist *bif;
1932 
1933 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1934 	if (bif == NULL)
1935 		return (EXTERROR(ENOENT, "Interface is not a bridge member"));
1936 
1937 	return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority));
1938 }
1939 
1940 static int
1941 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1942 {
1943 	struct ifbreq *req = arg;
1944 	struct bridge_iflist *bif;
1945 
1946 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1947 	if (bif == NULL)
1948 		return (EXTERROR(ENOENT, "Interface is not a bridge member"));
1949 
1950 	return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost));
1951 }
1952 
1953 static int
1954 bridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg)
1955 {
1956 	struct ifbreq *req = arg;
1957 	struct bridge_iflist *bif;
1958 
1959 	bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1960 	if (bif == NULL)
1961 		return (EXTERROR(ENOENT, "Interface is not a bridge member"));
1962 
1963 	bif->bif_addrmax = req->ifbr_addrmax;
1964 	return (0);
1965 }
1966 
1967 static int
1968 bridge_ioctl_sifpvid(struct bridge_softc *sc, void *arg)
1969 {
1970 	struct ifbreq *req = arg;
1971 	struct bridge_iflist *bif;
1972 
1973 	if ((sc->sc_flags & IFBRF_VLANFILTER) == 0)
1974 		return (EXTERROR(EINVAL, "VLAN filtering not enabled"));
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 	/* We may be sending a fragment so traverse the mbuf */
2406 	for (; m; m = m0) {
2407 		m0 = m->m_nextpkt;
2408 		m->m_nextpkt = NULL;
2409 		len = m->m_pkthdr.len;
2410 		mflags = m->m_flags;
2411 
2412 		/*
2413 		 * If VLAN filtering is enabled, and the native VLAN ID of the
2414 		 * outgoing interface matches the VLAN ID of the frame, remove
2415 		 * the VLAN header.
2416 		 */
2417 		if ((sc->sc_flags & IFBRF_VLANFILTER) &&
2418 		    bif->bif_pvid != DOT1Q_VID_NULL &&
2419 		    VLANTAGOF(m) == bif->bif_pvid) {
2420 			m->m_flags &= ~M_VLANTAG;
2421 			m->m_pkthdr.ether_vtag = 0;
2422 		}
2423 
2424 		/*
2425 		 * There are two cases where we have to insert our own tag:
2426 		 * if the member interface doesn't support hardware tagging,
2427 		 * or if the tag proto is not 802.1q.
2428 		 */
2429 		if ((m->m_flags & M_VLANTAG) &&
2430 		    ((dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0 ||
2431 		      bif->bif_vlanproto != ETHERTYPE_VLAN)) {
2432 			m = ether_vlanencap_proto(m, m->m_pkthdr.ether_vtag,
2433 			    bif->bif_vlanproto);
2434 			if (m == NULL) {
2435 				if_printf(dst_ifp,
2436 				    "unable to prepend VLAN header\n");
2437 				if_inc_counter(dst_ifp, IFCOUNTER_OERRORS, 1);
2438 				continue;
2439 			}
2440 			m->m_flags &= ~M_VLANTAG;
2441 		}
2442 
2443 		M_ASSERTPKTHDR(m); /* We shouldn't transmit mbuf without pkthdr */
2444 		/*
2445 		 * XXXZL: gif(4) requires the af to be saved in csum_data field
2446 		 * so that gif_transmit() routine can pull it back.
2447 		 */
2448 		if (dst_ifp->if_type == IFT_GIF)
2449 			m->m_pkthdr.csum_data = AF_LINK;
2450 		if ((err = dst_ifp->if_transmit(dst_ifp, m))) {
2451 			int n;
2452 
2453 			for (m = m0, n = 1; m != NULL; m = m0, n++) {
2454 				m0 = m->m_nextpkt;
2455 				m_freem(m);
2456 			}
2457 			if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, n);
2458 			break;
2459 		}
2460 
2461 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OPACKETS, 1);
2462 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OBYTES, len);
2463 		if (mflags & M_MCAST)
2464 			if_inc_counter(sc->sc_ifp, IFCOUNTER_OMCASTS, 1);
2465 	}
2466 
2467 	return (err);
2468 }
2469 
2470 /*
2471  * bridge_dummynet:
2472  *
2473  * 	Receive a queued packet from dummynet and pass it on to the output
2474  * 	interface.
2475  *
2476  *	The mbuf has the Ethernet header already attached.
2477  */
2478 static void
2479 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
2480 {
2481 	struct bridge_iflist *bif = ifp->if_bridge;
2482 	struct bridge_softc *sc = NULL;
2483 
2484 	if (bif)
2485 		sc = bif->bif_sc;
2486 
2487 	/*
2488 	 * The packet didnt originate from a member interface. This should only
2489 	 * ever happen if a member interface is removed while packets are
2490 	 * queued for it.
2491 	 */
2492 	if (sc == NULL) {
2493 		m_freem(m);
2494 		return;
2495 	}
2496 
2497 	if (PFIL_HOOKED_OUT_46) {
2498 		if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
2499 			return;
2500 		if (m == NULL)
2501 			return;
2502 	}
2503 
2504 	bridge_enqueue(sc, ifp, m, NULL);
2505 }
2506 
2507 /*
2508  * bridge_output:
2509  *
2510  *	Send output from a bridge member interface.  This
2511  *	performs the bridging function for locally originated
2512  *	packets.
2513  *
2514  *	The mbuf has the Ethernet header already attached.  We must
2515  *	enqueue or free the mbuf before returning.
2516  */
2517 static int
2518 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2519     struct rtentry *rt)
2520 {
2521 	struct ether_header *eh;
2522 	struct bridge_iflist *sbif;
2523 	struct ifnet *bifp, *dst_if;
2524 	struct bridge_softc *sc;
2525 	ether_vlanid_t vlan;
2526 
2527 	NET_EPOCH_ASSERT();
2528 
2529 	if (m->m_len < ETHER_HDR_LEN) {
2530 		m = m_pullup(m, ETHER_HDR_LEN);
2531 		if (m == NULL)
2532 			return (0);
2533 	}
2534 
2535 	sbif = ifp->if_bridge;
2536 	sc = sbif->bif_sc;
2537 	bifp = sc->sc_ifp;
2538 
2539 	eh = mtod(m, struct ether_header *);
2540 	vlan = VLANTAGOF(m);
2541 
2542 	/*
2543 	 * If bridge is down, but the original output interface is up,
2544 	 * go ahead and send out that interface.  Otherwise, the packet
2545 	 * is dropped below.
2546 	 */
2547 	if ((bifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2548 		dst_if = ifp;
2549 		goto sendunicast;
2550 	}
2551 
2552 	/*
2553 	 * If the packet is a multicast, or we don't know a better way to
2554 	 * get there, send to all interfaces.
2555 	 */
2556 	if (ETHER_IS_MULTICAST(eh->ether_dhost))
2557 		dst_if = NULL;
2558 	else
2559 		dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan);
2560 	/* Tap any traffic not passing back out the originating interface */
2561 	if (dst_if != ifp)
2562 		ETHER_BPF_MTAP(bifp, m);
2563 	if (dst_if == NULL) {
2564 		struct bridge_iflist *bif;
2565 		struct mbuf *mc;
2566 		int used = 0;
2567 
2568 		bridge_span(sc, m);
2569 
2570 		CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
2571 			dst_if = bif->bif_ifp;
2572 
2573 			if (dst_if->if_type == IFT_GIF)
2574 				continue;
2575 			if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2576 				continue;
2577 
2578 			/*
2579 			 * If this is not the original output interface,
2580 			 * and the interface is participating in spanning
2581 			 * tree, make sure the port is in a state that
2582 			 * allows forwarding.
2583 			 */
2584 			if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) &&
2585 			    bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2586 				continue;
2587 
2588 			if (CK_LIST_NEXT(bif, bif_next) == NULL) {
2589 				used = 1;
2590 				mc = m;
2591 			} else {
2592 				mc = m_dup(m, M_NOWAIT);
2593 				if (mc == NULL) {
2594 					if_inc_counter(bifp, IFCOUNTER_OERRORS, 1);
2595 					continue;
2596 				}
2597 			}
2598 
2599 			bridge_enqueue(sc, dst_if, mc, bif);
2600 		}
2601 		if (used == 0)
2602 			m_freem(m);
2603 		return (0);
2604 	}
2605 
2606 sendunicast:
2607 	/*
2608 	 * XXX Spanning tree consideration here?
2609 	 */
2610 
2611 	bridge_span(sc, m);
2612 	if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2613 		m_freem(m);
2614 		return (0);
2615 	}
2616 
2617 	bridge_enqueue(sc, dst_if, m, NULL);
2618 	return (0);
2619 }
2620 
2621 /*
2622  * bridge_transmit:
2623  *
2624  *	Do output on a bridge.
2625  *
2626  */
2627 static int
2628 bridge_transmit(struct ifnet *ifp, struct mbuf *m)
2629 {
2630 	struct bridge_softc *sc;
2631 	struct ether_header *eh;
2632 	struct ifnet *dst_if;
2633 	int error = 0;
2634 	ether_vlanid_t vlan;
2635 
2636 	sc = ifp->if_softc;
2637 
2638 	ETHER_BPF_MTAP(ifp, m);
2639 
2640 	eh = mtod(m, struct ether_header *);
2641 	vlan = VLANTAGOF(m);
2642 
2643 	if (((m->m_flags & (M_BCAST|M_MCAST)) == 0) &&
2644 	    (dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan)) != NULL) {
2645 		error = bridge_enqueue(sc, dst_if, m, NULL);
2646 	} else
2647 		bridge_broadcast(sc, ifp, m, 0);
2648 
2649 	return (error);
2650 }
2651 
2652 #ifdef ALTQ
2653 static void
2654 bridge_altq_start(if_t ifp)
2655 {
2656 	struct ifaltq *ifq = &ifp->if_snd;
2657 	struct mbuf *m;
2658 
2659 	IFQ_LOCK(ifq);
2660 	IFQ_DEQUEUE_NOLOCK(ifq, m);
2661 	while (m != NULL) {
2662 		bridge_transmit(ifp, m);
2663 		IFQ_DEQUEUE_NOLOCK(ifq, m);
2664 	}
2665 	IFQ_UNLOCK(ifq);
2666 }
2667 
2668 static int
2669 bridge_altq_transmit(if_t ifp, struct mbuf *m)
2670 {
2671 	int err;
2672 
2673 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
2674 		IFQ_ENQUEUE(&ifp->if_snd, m, err);
2675 		if (err == 0)
2676 			bridge_altq_start(ifp);
2677 	} else
2678 		err = bridge_transmit(ifp, m);
2679 
2680 	return (err);
2681 }
2682 #endif	/* ALTQ */
2683 
2684 /*
2685  * The ifp->if_qflush entry point for if_bridge(4) is no-op.
2686  */
2687 static void
2688 bridge_qflush(struct ifnet *ifp __unused)
2689 {
2690 }
2691 
2692 /*
2693  * bridge_forward:
2694  *
2695  *	The forwarding function of the bridge.
2696  *
2697  *	NOTE: Releases the lock on return.
2698  */
2699 static void
2700 bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif,
2701     struct mbuf *m)
2702 {
2703 	struct bridge_iflist *dbif;
2704 	struct ifnet *src_if, *dst_if, *ifp;
2705 	struct ether_header *eh;
2706 	uint8_t *dst;
2707 	int error;
2708 	ether_vlanid_t vlan;
2709 
2710 	NET_EPOCH_ASSERT();
2711 
2712 	src_if = m->m_pkthdr.rcvif;
2713 	ifp = sc->sc_ifp;
2714 	vlan = VLANTAGOF(m);
2715 
2716 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2717 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
2718 
2719 	if ((sbif->bif_flags & IFBIF_STP) &&
2720 	    sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2721 		goto drop;
2722 
2723 	eh = mtod(m, struct ether_header *);
2724 	dst = eh->ether_dhost;
2725 
2726 	/* If the interface is learning, record the address. */
2727 	if (sbif->bif_flags & IFBIF_LEARNING) {
2728 		error = bridge_rtupdate(sc, eh->ether_shost, vlan,
2729 		    sbif, 0, IFBAF_DYNAMIC);
2730 		/*
2731 		 * If the interface has addresses limits then deny any source
2732 		 * that is not in the cache.
2733 		 */
2734 		if (error && sbif->bif_addrmax)
2735 			goto drop;
2736 	}
2737 
2738 	if ((sbif->bif_flags & IFBIF_STP) != 0 &&
2739 	    sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING)
2740 		goto drop;
2741 
2742 #ifdef DEV_NETMAP
2743 	/*
2744 	 * Hand the packet to netmap only if it wasn't injected by netmap
2745 	 * itself.
2746 	 */
2747 	if ((m->m_flags & M_BRIDGE_INJECT) == 0 &&
2748 	    (if_getcapenable(ifp) & IFCAP_NETMAP) != 0) {
2749 		ifp->if_input(ifp, m);
2750 		return;
2751 	}
2752 	m->m_flags &= ~M_BRIDGE_INJECT;
2753 #endif
2754 
2755 	/*
2756 	 * At this point, the port either doesn't participate
2757 	 * in spanning tree or it is in the forwarding state.
2758 	 */
2759 
2760 	/*
2761 	 * If the packet is unicast, destined for someone on
2762 	 * "this" side of the bridge, drop it.
2763 	 */
2764 	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2765 		dst_if = bridge_rtlookup(sc, dst, vlan);
2766 		if (src_if == dst_if)
2767 			goto drop;
2768 	} else {
2769 		/*
2770 		 * Check if its a reserved multicast address, any address
2771 		 * listed in 802.1D section 7.12.6 may not be forwarded by the
2772 		 * bridge.
2773 		 * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F
2774 		 */
2775 		if (dst[0] == 0x01 && dst[1] == 0x80 &&
2776 		    dst[2] == 0xc2 && dst[3] == 0x00 &&
2777 		    dst[4] == 0x00 && dst[5] <= 0x0f)
2778 			goto drop;
2779 
2780 		/* ...forward it to all interfaces. */
2781 		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
2782 		dst_if = NULL;
2783 	}
2784 
2785 	/*
2786 	 * If we have a destination interface which is a member of our bridge,
2787 	 * OR this is a unicast packet, push it through the bpf(4) machinery.
2788 	 * For broadcast or multicast packets, don't bother because it will
2789 	 * be reinjected into ether_input. We do this before we pass the packets
2790 	 * through the pfil(9) framework, as it is possible that pfil(9) will
2791 	 * drop the packet, or possibly modify it, making it difficult to debug
2792 	 * firewall issues on the bridge.
2793 	 */
2794 	if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0)
2795 		ETHER_BPF_MTAP(ifp, m);
2796 
2797 	/* run the packet filter */
2798 	if (PFIL_HOOKED_IN_46) {
2799 		if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2800 			return;
2801 		if (m == NULL)
2802 			return;
2803 	}
2804 
2805 	if (dst_if == NULL) {
2806 		bridge_broadcast(sc, src_if, m, 1);
2807 		return;
2808 	}
2809 
2810 	/*
2811 	 * At this point, we're dealing with a unicast frame
2812 	 * going to a different interface.
2813 	 */
2814 	if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2815 		goto drop;
2816 
2817 	dbif = bridge_lookup_member_if(sc, dst_if);
2818 	if (dbif == NULL)
2819 		/* Not a member of the bridge (anymore?) */
2820 		goto drop;
2821 
2822 	/* Private segments can not talk to each other */
2823 	if (sbif->bif_flags & dbif->bif_flags & IFBIF_PRIVATE)
2824 		goto drop;
2825 
2826 	/* Do VLAN filtering. */
2827 	if (!bridge_vfilter_out(dbif, m))
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 		/* Do VLAN filtering. */
3199 		if (!bridge_vfilter_out(dbif, m))
3200 			continue;
3201 
3202 		if ((dbif->bif_flags & IFBIF_STP) &&
3203 		    dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
3204 			continue;
3205 
3206 		if ((dbif->bif_flags & IFBIF_DISCOVER) == 0 &&
3207 		    (m->m_flags & (M_BCAST|M_MCAST)) == 0)
3208 			continue;
3209 
3210 		if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
3211 			continue;
3212 
3213 		if (CK_LIST_NEXT(dbif, bif_next) == NULL) {
3214 			mc = m;
3215 			used = 1;
3216 		} else {
3217 			mc = m_dup(m, M_NOWAIT);
3218 			if (mc == NULL) {
3219 				if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
3220 				continue;
3221 			}
3222 		}
3223 
3224 		/*
3225 		 * Filter on the output interface. Pass a NULL bridge interface
3226 		 * pointer so we do not redundantly filter on the bridge for
3227 		 * each interface we broadcast on.
3228 		 */
3229 		if (runfilt && PFIL_HOOKED_OUT_46) {
3230 			if (used == 0) {
3231 				/* Keep the layer3 header aligned */
3232 				i = min(mc->m_pkthdr.len, max_protohdr);
3233 				mc = m_copyup(mc, i, ETHER_ALIGN);
3234 				if (mc == NULL) {
3235 					if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
3236 					continue;
3237 				}
3238 			}
3239 			if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
3240 				continue;
3241 			if (mc == NULL)
3242 				continue;
3243 		}
3244 
3245 		bridge_enqueue(sc, dst_if, mc, dbif);
3246 	}
3247 	if (used == 0)
3248 		m_freem(m);
3249 }
3250 
3251 /*
3252  * bridge_span:
3253  *
3254  *	Duplicate a packet out one or more interfaces that are in span mode,
3255  *	the original mbuf is unmodified.
3256  */
3257 static void
3258 bridge_span(struct bridge_softc *sc, struct mbuf *m)
3259 {
3260 	struct bridge_iflist *bif;
3261 	struct ifnet *dst_if;
3262 	struct mbuf *mc;
3263 
3264 	NET_EPOCH_ASSERT();
3265 
3266 	if (CK_LIST_EMPTY(&sc->sc_spanlist))
3267 		return;
3268 
3269 	CK_LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
3270 		dst_if = bif->bif_ifp;
3271 
3272 		if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
3273 			continue;
3274 
3275 		mc = m_dup(m, M_NOWAIT);
3276 		if (mc == NULL) {
3277 			if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
3278 			continue;
3279 		}
3280 
3281 		bridge_enqueue(sc, dst_if, mc, bif);
3282 	}
3283 }
3284 
3285 /*
3286  * Incoming VLAN filtering.  Given a frame and the member interface it was
3287  * received on, decide whether the port configuration allows it.
3288  */
3289 static bool
3290 bridge_vfilter_in(const struct bridge_iflist *sbif, struct mbuf *m)
3291 {
3292 	ether_vlanid_t vlan;
3293 
3294 	vlan = VLANTAGOF(m);
3295 	/* Make sure the vlan id is reasonable. */
3296 	if (vlan > DOT1Q_VID_MAX)
3297 		return (false);
3298 
3299 	/* If VLAN filtering isn't enabled, pass everything. */
3300 	if ((sbif->bif_sc->sc_flags & IFBRF_VLANFILTER) == 0)
3301 		return (true);
3302 
3303 	/* If Q-in-Q is disabled, check for stacked tags. */
3304 	if ((sbif->bif_flags & IFBIF_QINQ) == 0) {
3305 		struct ether_header *eh;
3306 		uint16_t proto;
3307 
3308 		eh = mtod(m, struct ether_header *);
3309 		proto = ntohs(eh->ether_type);
3310 
3311 		if (proto == ETHERTYPE_VLAN || proto == ETHERTYPE_QINQ)
3312 			return (false);
3313 	}
3314 
3315 	if (vlan == DOT1Q_VID_NULL) {
3316 		/*
3317 		 * The frame doesn't have a tag.  If the interface does not
3318 		 * have an untagged vlan configured, drop the frame.
3319 		 */
3320 		if (sbif->bif_pvid == DOT1Q_VID_NULL)
3321 			return (false);
3322 
3323 		/*
3324 		 * Otherwise, insert a new tag based on the interface's
3325 		 * untagged vlan id.
3326 		 */
3327 		m->m_pkthdr.ether_vtag = sbif->bif_pvid;
3328 		m->m_flags |= M_VLANTAG;
3329 	} else {
3330 		/*
3331 		 * The frame has a tag, so check it matches the interface's
3332 		 * vlan access list.  We explicitly do not accept tagged
3333 		 * frames for the untagged vlan id here (unless it's also
3334 		 * in the access list).
3335 		 */
3336 		if (!BRVLAN_TEST(&sbif->bif_vlan_set, vlan))
3337 			return (false);
3338 	}
3339 
3340 	/* Accept the frame. */
3341 	return (true);
3342 }
3343 
3344 /*
3345  * Outgoing VLAN filtering.  Given a frame, its vlan, and the member interface
3346  * we intend to send it to, decide whether the port configuration allows it to
3347  * be sent.
3348  */
3349 static bool
3350 bridge_vfilter_out(const struct bridge_iflist *dbif, const struct mbuf *m)
3351 {
3352 	struct ether_header *eh;
3353 	ether_vlanid_t vlan;
3354 
3355 	NET_EPOCH_ASSERT();
3356 
3357 	/* If VLAN filtering isn't enabled, pass everything. */
3358 	if ((dbif->bif_sc->sc_flags & IFBRF_VLANFILTER) == 0)
3359 		return (true);
3360 
3361 	vlan = VLANTAGOF(m);
3362 
3363 	/*
3364 	 * Always allow untagged 802.1D STP frames, even if they would
3365 	 * otherwise be dropped.  This is required for STP to work on
3366 	 * a filtering bridge.
3367 	 *
3368 	 * Tagged STP (Cisco PVST+) is a non-standard extension, so
3369 	 * handle those frames via the normal filtering path.
3370 	 */
3371 	eh = mtod(m, struct ether_header *);
3372 	if (vlan == DOT1Q_VID_NULL &&
3373 	    memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0)
3374 		return (true);
3375 
3376 	/*
3377 	 * If the frame wasn't assigned to a vlan at ingress, drop it.
3378 	 * We can't forward these frames to filtering ports because we
3379 	 * don't know what VLAN they're supposed to be in.
3380 	 */
3381 	if (vlan == DOT1Q_VID_NULL)
3382 		return (false);
3383 
3384 	/*
3385 	 * If the frame's vlan matches the interfaces's untagged vlan,
3386 	 * allow it.
3387 	 */
3388 	if (vlan == dbif->bif_pvid)
3389 		return (true);
3390 
3391 	/*
3392 	 * If the frame's vlan is on the interface's tagged access list,
3393 	 * allow it.
3394 	 */
3395 	if (BRVLAN_TEST(&dbif->bif_vlan_set, vlan))
3396 		return (true);
3397 
3398 	/* The frame was not permitted, so drop it. */
3399 	return (false);
3400 }
3401 
3402 /*
3403  * bridge_rtupdate:
3404  *
3405  *	Add a bridge routing entry.
3406  */
3407 static int
3408 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
3409 		ether_vlanid_t vlan, struct bridge_iflist *bif,
3410 		int setflags, uint8_t flags)
3411 {
3412 	struct bridge_rtnode *brt;
3413 	struct bridge_iflist *obif;
3414 	int error;
3415 
3416 	BRIDGE_LOCK_OR_NET_EPOCH_ASSERT(sc);
3417 
3418 	/* Check the source address is valid and not multicast. */
3419 	if (ETHER_IS_MULTICAST(dst))
3420 		return (EXTERROR(EINVAL, "Multicast address not permitted"));
3421 	if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3422 	    dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3423 		return (EXTERROR(EINVAL, "Zero address not permitted"));
3424 
3425 	/*
3426 	 * A route for this destination might already exist.  If so,
3427 	 * update it, otherwise create a new one.
3428 	 */
3429 	if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) {
3430 		BRIDGE_RT_LOCK(sc);
3431 
3432 		/* Check again, now that we have the lock. There could have
3433 		 * been a race and we only want to insert this once. */
3434 		if (bridge_rtnode_lookup(sc, dst, vlan) != NULL) {
3435 			BRIDGE_RT_UNLOCK(sc);
3436 			return (0);
3437 		}
3438 
3439 		if (sc->sc_brtcnt >= sc->sc_brtmax) {
3440 			sc->sc_brtexceeded++;
3441 			BRIDGE_RT_UNLOCK(sc);
3442 			return (EXTERROR(ENOSPC, "Address table is full"));
3443 		}
3444 		/* Check per interface address limits (if enabled) */
3445 		if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) {
3446 			bif->bif_addrexceeded++;
3447 			BRIDGE_RT_UNLOCK(sc);
3448 			return (EXTERROR(ENOSPC,
3449 			    "Interface address limit exceeded"));
3450 		}
3451 
3452 		/*
3453 		 * Allocate a new bridge forwarding node, and
3454 		 * initialize the expiration time and Ethernet
3455 		 * address.
3456 		 */
3457 		brt = uma_zalloc(V_bridge_rtnode_zone, M_NOWAIT | M_ZERO);
3458 		if (brt == NULL) {
3459 			BRIDGE_RT_UNLOCK(sc);
3460 			return (EXTERROR(ENOMEM,
3461 			    "Cannot allocate address node"));
3462 		}
3463 		brt->brt_vnet = curvnet;
3464 
3465 		if (bif->bif_flags & IFBIF_STICKY)
3466 			brt->brt_flags = IFBAF_STICKY;
3467 		else
3468 			brt->brt_flags = IFBAF_DYNAMIC;
3469 
3470 		memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
3471 		brt->brt_vlan = vlan;
3472 
3473 		brt->brt_dst = bif;
3474 		if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
3475 			uma_zfree(V_bridge_rtnode_zone, brt);
3476 			BRIDGE_RT_UNLOCK(sc);
3477 			return (error);
3478 		}
3479 		bif->bif_addrcnt++;
3480 
3481 		BRIDGE_RT_UNLOCK(sc);
3482 	}
3483 
3484 	if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
3485 	    (obif = brt->brt_dst) != bif) {
3486 		MPASS(obif != NULL);
3487 
3488 		BRIDGE_RT_LOCK(sc);
3489 		brt->brt_dst->bif_addrcnt--;
3490 		brt->brt_dst = bif;
3491 		brt->brt_dst->bif_addrcnt++;
3492 		BRIDGE_RT_UNLOCK(sc);
3493 
3494 		if (V_log_mac_flap &&
3495 		    ppsratecheck(&V_log_last, &V_log_count, V_log_interval)) {
3496 			log(LOG_NOTICE,
3497 			    "%s: mac address %6D vlan %d moved from %s to %s\n",
3498 			    sc->sc_ifp->if_xname,
3499 			    &brt->brt_addr[0], ":",
3500 			    brt->brt_vlan,
3501 			    obif->bif_ifp->if_xname,
3502 			    bif->bif_ifp->if_xname);
3503 		}
3504 	}
3505 
3506 	if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
3507 		brt->brt_expire = time_uptime + sc->sc_brttimeout;
3508 	if (setflags)
3509 		brt->brt_flags = flags;
3510 
3511 	return (0);
3512 }
3513 
3514 /*
3515  * bridge_rtlookup:
3516  *
3517  *	Lookup the destination interface for an address.
3518  */
3519 static struct ifnet *
3520 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr,
3521 		ether_vlanid_t vlan)
3522 {
3523 	struct bridge_rtnode *brt;
3524 
3525 	NET_EPOCH_ASSERT();
3526 
3527 	if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL)
3528 		return (NULL);
3529 
3530 	return (brt->brt_ifp);
3531 }
3532 
3533 /*
3534  * bridge_rttrim:
3535  *
3536  *	Trim the routine table so that we have a number
3537  *	of routing entries less than or equal to the
3538  *	maximum number.
3539  */
3540 static void
3541 bridge_rttrim(struct bridge_softc *sc)
3542 {
3543 	struct bridge_rtnode *brt, *nbrt;
3544 
3545 	NET_EPOCH_ASSERT();
3546 	BRIDGE_RT_LOCK_ASSERT(sc);
3547 
3548 	/* Make sure we actually need to do this. */
3549 	if (sc->sc_brtcnt <= sc->sc_brtmax)
3550 		return;
3551 
3552 	/* Force an aging cycle; this might trim enough addresses. */
3553 	bridge_rtage(sc);
3554 	if (sc->sc_brtcnt <= sc->sc_brtmax)
3555 		return;
3556 
3557 	CK_LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
3558 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
3559 			bridge_rtnode_destroy(sc, brt);
3560 			if (sc->sc_brtcnt <= sc->sc_brtmax)
3561 				return;
3562 		}
3563 	}
3564 }
3565 
3566 /*
3567  * bridge_timer:
3568  *
3569  *	Aging timer for the bridge.
3570  */
3571 static void
3572 bridge_timer(void *arg)
3573 {
3574 	struct bridge_softc *sc = arg;
3575 
3576 	BRIDGE_RT_LOCK_ASSERT(sc);
3577 
3578 	/* Destruction of rtnodes requires a proper vnet context */
3579 	CURVNET_SET(sc->sc_ifp->if_vnet);
3580 	bridge_rtage(sc);
3581 
3582 	if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
3583 		callout_reset(&sc->sc_brcallout,
3584 		    bridge_rtable_prune_period * hz, bridge_timer, sc);
3585 	CURVNET_RESTORE();
3586 }
3587 
3588 /*
3589  * bridge_rtage:
3590  *
3591  *	Perform an aging cycle.
3592  */
3593 static void
3594 bridge_rtage(struct bridge_softc *sc)
3595 {
3596 	struct bridge_rtnode *brt, *nbrt;
3597 
3598 	BRIDGE_RT_LOCK_ASSERT(sc);
3599 
3600 	CK_LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
3601 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
3602 			if (time_uptime >= brt->brt_expire)
3603 				bridge_rtnode_destroy(sc, brt);
3604 		}
3605 	}
3606 }
3607 
3608 /*
3609  * bridge_rtflush:
3610  *
3611  *	Remove all dynamic addresses from the bridge.
3612  */
3613 static void
3614 bridge_rtflush(struct bridge_softc *sc, int full)
3615 {
3616 	struct bridge_rtnode *brt, *nbrt;
3617 
3618 	BRIDGE_RT_LOCK_ASSERT(sc);
3619 
3620 	CK_LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
3621 		if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
3622 			bridge_rtnode_destroy(sc, brt);
3623 	}
3624 }
3625 
3626 /*
3627  * bridge_rtdaddr:
3628  *
3629  *	Remove an address from the table.
3630  */
3631 static int
3632 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr,
3633 	       ether_vlanid_t vlan)
3634 {
3635 	struct bridge_rtnode *brt;
3636 	int found = 0;
3637 
3638 	BRIDGE_RT_LOCK(sc);
3639 
3640 	/*
3641 	 * If vlan is DOT1Q_VID_RSVD_IMPL then we want to delete for all vlans
3642 	 * so the lookup may return more than one.
3643 	 */
3644 	while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) {
3645 		bridge_rtnode_destroy(sc, brt);
3646 		found = 1;
3647 	}
3648 
3649 	BRIDGE_RT_UNLOCK(sc);
3650 
3651 	return (found ? 0 : ENOENT);
3652 }
3653 
3654 /*
3655  * bridge_rtdelete:
3656  *
3657  *	Delete routes to a speicifc member interface.
3658  */
3659 static void
3660 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
3661 {
3662 	struct bridge_rtnode *brt, *nbrt;
3663 
3664 	BRIDGE_RT_LOCK_ASSERT(sc);
3665 
3666 	CK_LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
3667 		if (brt->brt_ifp == ifp && (full ||
3668 			    (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
3669 			bridge_rtnode_destroy(sc, brt);
3670 	}
3671 }
3672 
3673 /*
3674  * bridge_rtable_init:
3675  *
3676  *	Initialize the route table for this bridge.
3677  */
3678 static void
3679 bridge_rtable_init(struct bridge_softc *sc)
3680 {
3681 	int i;
3682 
3683 	sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
3684 	    M_DEVBUF, M_WAITOK);
3685 
3686 	for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
3687 		CK_LIST_INIT(&sc->sc_rthash[i]);
3688 
3689 	sc->sc_rthash_key = arc4random();
3690 	CK_LIST_INIT(&sc->sc_rtlist);
3691 }
3692 
3693 /*
3694  * bridge_rtable_fini:
3695  *
3696  *	Deconstruct the route table for this bridge.
3697  */
3698 static void
3699 bridge_rtable_fini(struct bridge_softc *sc)
3700 {
3701 
3702 	KASSERT(sc->sc_brtcnt == 0,
3703 	    ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt));
3704 	free(sc->sc_rthash, M_DEVBUF);
3705 }
3706 
3707 /*
3708  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
3709  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
3710  */
3711 #define	mix(a, b, c)							\
3712 do {									\
3713 	a -= b; a -= c; a ^= (c >> 13);					\
3714 	b -= c; b -= a; b ^= (a << 8);					\
3715 	c -= a; c -= b; c ^= (b >> 13);					\
3716 	a -= b; a -= c; a ^= (c >> 12);					\
3717 	b -= c; b -= a; b ^= (a << 16);					\
3718 	c -= a; c -= b; c ^= (b >> 5);					\
3719 	a -= b; a -= c; a ^= (c >> 3);					\
3720 	b -= c; b -= a; b ^= (a << 10);					\
3721 	c -= a; c -= b; c ^= (b >> 15);					\
3722 } while (/*CONSTCOND*/0)
3723 
3724 static __inline uint32_t
3725 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
3726 {
3727 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
3728 
3729 	b += addr[5] << 8;
3730 	b += addr[4];
3731 	a += addr[3] << 24;
3732 	a += addr[2] << 16;
3733 	a += addr[1] << 8;
3734 	a += addr[0];
3735 
3736 	mix(a, b, c);
3737 
3738 	return (c & BRIDGE_RTHASH_MASK);
3739 }
3740 
3741 #undef mix
3742 
3743 static int
3744 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
3745 {
3746 	int i, d;
3747 
3748 	for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
3749 		d = ((int)a[i]) - ((int)b[i]);
3750 	}
3751 
3752 	return (d);
3753 }
3754 
3755 /*
3756  * bridge_rtnode_lookup:
3757  *
3758  *	Look up a bridge route node for the specified destination. Compare the
3759  *	vlan id or if zero then just return the first match.
3760  */
3761 static struct bridge_rtnode *
3762 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr,
3763 		     ether_vlanid_t vlan)
3764 {
3765 	struct bridge_rtnode *brt;
3766 	uint32_t hash;
3767 	int dir;
3768 
3769 	BRIDGE_RT_LOCK_OR_NET_EPOCH_ASSERT(sc);
3770 
3771 	hash = bridge_rthash(sc, addr);
3772 	CK_LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
3773 		dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
3774 		if (dir == 0 && (brt->brt_vlan == vlan || vlan == DOT1Q_VID_RSVD_IMPL))
3775 			return (brt);
3776 		if (dir > 0)
3777 			return (NULL);
3778 	}
3779 
3780 	return (NULL);
3781 }
3782 
3783 /*
3784  * bridge_rtnode_insert:
3785  *
3786  *	Insert the specified bridge node into the route table.  We
3787  *	assume the entry is not already in the table.
3788  */
3789 static int
3790 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
3791 {
3792 	struct bridge_rtnode *lbrt;
3793 	uint32_t hash;
3794 	int dir;
3795 
3796 	BRIDGE_RT_LOCK_ASSERT(sc);
3797 
3798 	hash = bridge_rthash(sc, brt->brt_addr);
3799 
3800 	lbrt = CK_LIST_FIRST(&sc->sc_rthash[hash]);
3801 	if (lbrt == NULL) {
3802 		CK_LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
3803 		goto out;
3804 	}
3805 
3806 	do {
3807 		dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
3808 		if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan)
3809 			return (EXTERROR(EEXIST, "Address already exists"));
3810 		if (dir > 0) {
3811 			CK_LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
3812 			goto out;
3813 		}
3814 		if (CK_LIST_NEXT(lbrt, brt_hash) == NULL) {
3815 			CK_LIST_INSERT_AFTER(lbrt, brt, brt_hash);
3816 			goto out;
3817 		}
3818 		lbrt = CK_LIST_NEXT(lbrt, brt_hash);
3819 	} while (lbrt != NULL);
3820 
3821 #ifdef DIAGNOSTIC
3822 	panic("bridge_rtnode_insert: impossible");
3823 #endif
3824 
3825 out:
3826 	CK_LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
3827 	sc->sc_brtcnt++;
3828 
3829 	return (0);
3830 }
3831 
3832 static void
3833 bridge_rtnode_destroy_cb(struct epoch_context *ctx)
3834 {
3835 	struct bridge_rtnode *brt;
3836 
3837 	brt = __containerof(ctx, struct bridge_rtnode, brt_epoch_ctx);
3838 
3839 	CURVNET_SET(brt->brt_vnet);
3840 	uma_zfree(V_bridge_rtnode_zone, brt);
3841 	CURVNET_RESTORE();
3842 }
3843 
3844 /*
3845  * bridge_rtnode_destroy:
3846  *
3847  *	Destroy a bridge rtnode.
3848  */
3849 static void
3850 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3851 {
3852 	BRIDGE_RT_LOCK_ASSERT(sc);
3853 
3854 	CK_LIST_REMOVE(brt, brt_hash);
3855 
3856 	CK_LIST_REMOVE(brt, brt_list);
3857 	sc->sc_brtcnt--;
3858 	brt->brt_dst->bif_addrcnt--;
3859 
3860 	NET_EPOCH_CALL(bridge_rtnode_destroy_cb, &brt->brt_epoch_ctx);
3861 }
3862 
3863 /*
3864  * bridge_rtable_expire:
3865  *
3866  *	Set the expiry time for all routes on an interface.
3867  */
3868 static void
3869 bridge_rtable_expire(struct ifnet *ifp, int age)
3870 {
3871 	struct bridge_iflist *bif = NULL;
3872 	struct bridge_softc *sc = NULL;
3873 	struct bridge_rtnode *brt;
3874 
3875 	CURVNET_SET(ifp->if_vnet);
3876 
3877 	bif = ifp->if_bridge;
3878 	if (bif)
3879 		sc = bif->bif_sc;
3880 	MPASS(sc != NULL);
3881 	BRIDGE_RT_LOCK(sc);
3882 
3883 	/*
3884 	 * If the age is zero then flush, otherwise set all the expiry times to
3885 	 * age for the interface
3886 	 */
3887 	if (age == 0)
3888 		bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
3889 	else {
3890 		CK_LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
3891 			/* Cap the expiry time to 'age' */
3892 			if (brt->brt_ifp == ifp &&
3893 			    brt->brt_expire > time_uptime + age &&
3894 			    (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
3895 				brt->brt_expire = time_uptime + age;
3896 		}
3897 	}
3898 	BRIDGE_RT_UNLOCK(sc);
3899 	CURVNET_RESTORE();
3900 }
3901 
3902 /*
3903  * bridge_state_change:
3904  *
3905  *	Callback from the bridgestp code when a port changes states.
3906  */
3907 static void
3908 bridge_state_change(struct ifnet *ifp, int state)
3909 {
3910 	struct bridge_iflist *bif = ifp->if_bridge;
3911 	struct bridge_softc *sc = bif->bif_sc;
3912 	static const char *stpstates[] = {
3913 		"disabled",
3914 		"listening",
3915 		"learning",
3916 		"forwarding",
3917 		"blocking",
3918 		"discarding"
3919 	};
3920 
3921 	CURVNET_SET(ifp->if_vnet);
3922 	if (V_log_stp)
3923 		log(LOG_NOTICE, "%s: state changed to %s on %s\n",
3924 		    sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname);
3925 	CURVNET_RESTORE();
3926 }
3927 
3928 /*
3929  * Send bridge packets through pfil if they are one of the types pfil can deal
3930  * with, or if they are ARP or REVARP.  (pfil will pass ARP and REVARP without
3931  * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3932  * that interface.
3933  */
3934 static int
3935 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3936 {
3937 	int snap, error, i;
3938 	struct ether_header *eh1, eh2;
3939 	struct llc llc1;
3940 	u_int16_t ether_type;
3941 	pfil_return_t rv;
3942 #ifdef INET
3943 	struct ip *ip = NULL;
3944 	int hlen = 0;
3945 #endif
3946 
3947 	snap = 0;
3948 	error = -1;	/* Default error if not error == 0 */
3949 
3950 #if 0
3951 	/* we may return with the IP fields swapped, ensure its not shared */
3952 	KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__));
3953 #endif
3954 
3955 	if (V_pfil_bridge == 0 && V_pfil_member == 0 && V_pfil_ipfw == 0)
3956 		return (0); /* filtering is disabled */
3957 
3958 	i = min((*mp)->m_pkthdr.len, max_protohdr);
3959 	if ((*mp)->m_len < i) {
3960 	    *mp = m_pullup(*mp, i);
3961 	    if (*mp == NULL) {
3962 		printf("%s: m_pullup failed\n", __func__);
3963 		return (-1);
3964 	    }
3965 	}
3966 
3967 	eh1 = mtod(*mp, struct ether_header *);
3968 	ether_type = ntohs(eh1->ether_type);
3969 
3970 	/*
3971 	 * Check for SNAP/LLC.
3972 	 */
3973 	if (ether_type < ETHERMTU) {
3974 		struct llc *llc2 = (struct llc *)(eh1 + 1);
3975 
3976 		if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3977 		    llc2->llc_dsap == LLC_SNAP_LSAP &&
3978 		    llc2->llc_ssap == LLC_SNAP_LSAP &&
3979 		    llc2->llc_control == LLC_UI) {
3980 			ether_type = htons(llc2->llc_un.type_snap.ether_type);
3981 			snap = 1;
3982 		}
3983 	}
3984 
3985 	/*
3986 	 * If we're trying to filter bridge traffic, only look at traffic for
3987 	 * protocols available in the kernel (IPv4 and/or IPv6) to avoid
3988 	 * passing traffic for an unsupported protocol to the filter.  This is
3989 	 * lame since if we really wanted, say, an AppleTalk filter, we are
3990 	 * hosed, but of course we don't have an AppleTalk filter to begin
3991 	 * with.  (Note that since pfil doesn't understand ARP it will pass
3992 	 * *ALL* ARP traffic.)
3993 	 */
3994 	switch (ether_type) {
3995 #ifdef INET
3996 		case ETHERTYPE_ARP:
3997 		case ETHERTYPE_REVARP:
3998 			if (V_pfil_ipfw_arp == 0)
3999 				return (0); /* Automatically pass */
4000 
4001 			/* FALLTHROUGH */
4002 		case ETHERTYPE_IP:
4003 #endif
4004 #ifdef INET6
4005 		case ETHERTYPE_IPV6:
4006 #endif /* INET6 */
4007 			break;
4008 
4009 		default:
4010 			/*
4011 			 * We get here if the packet isn't from a supported
4012 			 * protocol.  Check to see if the user wants to pass
4013 			 * non-IP packets, these will not be checked by pfil(9)
4014 			 * and passed unconditionally so the default is to
4015 			 * drop.
4016 			 */
4017 			if (V_pfil_onlyip)
4018 				goto bad;
4019 	}
4020 
4021 	/* Run the packet through pfil before stripping link headers */
4022 	if (PFIL_HOOKED_OUT(V_link_pfil_head) && V_pfil_ipfw != 0 &&
4023 	    dir == PFIL_OUT && ifp != NULL) {
4024 		switch (pfil_mbuf_out(V_link_pfil_head, mp, ifp, NULL)) {
4025 		case PFIL_DROPPED:
4026 			return (EACCES);
4027 		case PFIL_CONSUMED:
4028 			return (0);
4029 		}
4030 	}
4031 
4032 	/* Strip off the Ethernet header and keep a copy. */
4033 	m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
4034 	m_adj(*mp, ETHER_HDR_LEN);
4035 
4036 	/* Strip off snap header, if present */
4037 	if (snap) {
4038 		m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
4039 		m_adj(*mp, sizeof(struct llc));
4040 	}
4041 
4042 	/*
4043 	 * Check the IP header for alignment and errors
4044 	 */
4045 	if (dir == PFIL_IN) {
4046 		switch (ether_type) {
4047 #ifdef INET
4048 			case ETHERTYPE_IP:
4049 				error = bridge_ip_checkbasic(mp);
4050 				break;
4051 #endif
4052 #ifdef INET6
4053 			case ETHERTYPE_IPV6:
4054 				error = bridge_ip6_checkbasic(mp);
4055 				break;
4056 #endif /* INET6 */
4057 			default:
4058 				error = 0;
4059 		}
4060 		if (error)
4061 			goto bad;
4062 	}
4063 
4064 	error = 0;
4065 
4066 	/*
4067 	 * Run the packet through pfil
4068 	 */
4069 	rv = PFIL_PASS;
4070 	switch (ether_type) {
4071 #ifdef INET
4072 	case ETHERTYPE_IP:
4073 		/*
4074 		 * Run pfil on the member interface and the bridge, both can
4075 		 * be skipped by clearing pfil_member or pfil_bridge.
4076 		 *
4077 		 * Keep the order:
4078 		 *   in_if -> bridge_if -> out_if
4079 		 */
4080 		if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL && (rv =
4081 		    pfil_mbuf_out(V_inet_pfil_head, mp, bifp, NULL)) !=
4082 		    PFIL_PASS)
4083 			break;
4084 
4085 		if (V_pfil_member && ifp != NULL) {
4086 			rv = (dir == PFIL_OUT) ?
4087 			    pfil_mbuf_out(V_inet_pfil_head, mp, ifp, NULL) :
4088 			    pfil_mbuf_in(V_inet_pfil_head, mp, ifp, NULL);
4089 			if (rv != PFIL_PASS)
4090 				break;
4091 		}
4092 
4093 		if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL && (rv =
4094 		    pfil_mbuf_in(V_inet_pfil_head, mp, bifp, NULL)) !=
4095 		    PFIL_PASS)
4096 			break;
4097 
4098 		/* check if we need to fragment the packet */
4099 		/* bridge_fragment generates a mbuf chain of packets */
4100 		/* that already include eth headers */
4101 		if (V_pfil_member && ifp != NULL && dir == PFIL_OUT) {
4102 			i = (*mp)->m_pkthdr.len;
4103 			if (i > ifp->if_mtu) {
4104 				error = bridge_fragment(ifp, mp, &eh2, snap,
4105 					    &llc1);
4106 				return (error);
4107 			}
4108 		}
4109 
4110 		/* Recalculate the ip checksum. */
4111 		ip = mtod(*mp, struct ip *);
4112 		hlen = ip->ip_hl << 2;
4113 		if (hlen < sizeof(struct ip))
4114 			goto bad;
4115 		if (hlen > (*mp)->m_len) {
4116 			if ((*mp = m_pullup(*mp, hlen)) == NULL)
4117 				goto bad;
4118 			ip = mtod(*mp, struct ip *);
4119 			if (ip == NULL)
4120 				goto bad;
4121 		}
4122 		ip->ip_sum = 0;
4123 		if (hlen == sizeof(struct ip))
4124 			ip->ip_sum = in_cksum_hdr(ip);
4125 		else
4126 			ip->ip_sum = in_cksum(*mp, hlen);
4127 
4128 		break;
4129 #endif /* INET */
4130 #ifdef INET6
4131 	case ETHERTYPE_IPV6:
4132 		if (V_pfil_bridge && dir == PFIL_OUT && bifp != NULL && (rv =
4133 		    pfil_mbuf_out(V_inet6_pfil_head, mp, bifp, NULL)) !=
4134 		    PFIL_PASS)
4135 			break;
4136 
4137 		if (V_pfil_member && ifp != NULL) {
4138 			rv = (dir == PFIL_OUT) ?
4139 			    pfil_mbuf_out(V_inet6_pfil_head, mp, ifp, NULL) :
4140 			    pfil_mbuf_in(V_inet6_pfil_head, mp, ifp, NULL);
4141 			if (rv != PFIL_PASS)
4142 				break;
4143 		}
4144 
4145 		if (V_pfil_bridge && dir == PFIL_IN && bifp != NULL && (rv =
4146 		    pfil_mbuf_in(V_inet6_pfil_head, mp, bifp, NULL)) !=
4147 		    PFIL_PASS)
4148 			break;
4149 		break;
4150 #endif
4151 	}
4152 
4153 	switch (rv) {
4154 	case PFIL_CONSUMED:
4155 		return (0);
4156 	case PFIL_DROPPED:
4157 		return (EACCES);
4158 	default:
4159 		break;
4160 	}
4161 
4162 	error = -1;
4163 
4164 	/*
4165 	 * Finally, put everything back the way it was and return
4166 	 */
4167 	if (snap) {
4168 		M_PREPEND(*mp, sizeof(struct llc), M_NOWAIT);
4169 		if (*mp == NULL)
4170 			return (error);
4171 		bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
4172 	}
4173 
4174 	M_PREPEND(*mp, ETHER_HDR_LEN, M_NOWAIT);
4175 	if (*mp == NULL)
4176 		return (error);
4177 	bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
4178 
4179 	return (0);
4180 
4181 bad:
4182 	m_freem(*mp);
4183 	*mp = NULL;
4184 	return (error);
4185 }
4186 
4187 #ifdef INET
4188 /*
4189  * Perform basic checks on header size since
4190  * pfil assumes ip_input has already processed
4191  * it for it.  Cut-and-pasted from ip_input.c.
4192  * Given how simple the IPv6 version is,
4193  * does the IPv4 version really need to be
4194  * this complicated?
4195  *
4196  * XXX Should we update ipstat here, or not?
4197  * XXX Right now we update ipstat but not
4198  * XXX csum_counter.
4199  */
4200 static int
4201 bridge_ip_checkbasic(struct mbuf **mp)
4202 {
4203 	struct mbuf *m = *mp;
4204 	struct ip *ip;
4205 	int len, hlen;
4206 	u_short sum;
4207 
4208 	if (*mp == NULL)
4209 		return (-1);
4210 
4211 	if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
4212 		if ((m = m_copyup(m, sizeof(struct ip),
4213 			(max_linkhdr + 3) & ~3)) == NULL) {
4214 			/* XXXJRT new stat, please */
4215 			KMOD_IPSTAT_INC(ips_toosmall);
4216 			goto bad;
4217 		}
4218 	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
4219 		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
4220 			KMOD_IPSTAT_INC(ips_toosmall);
4221 			goto bad;
4222 		}
4223 	}
4224 	ip = mtod(m, struct ip *);
4225 	if (ip == NULL) goto bad;
4226 
4227 	if (ip->ip_v != IPVERSION) {
4228 		KMOD_IPSTAT_INC(ips_badvers);
4229 		goto bad;
4230 	}
4231 	hlen = ip->ip_hl << 2;
4232 	if (hlen < sizeof(struct ip)) { /* minimum header length */
4233 		KMOD_IPSTAT_INC(ips_badhlen);
4234 		goto bad;
4235 	}
4236 	if (hlen > m->m_len) {
4237 		if ((m = m_pullup(m, hlen)) == NULL) {
4238 			KMOD_IPSTAT_INC(ips_badhlen);
4239 			goto bad;
4240 		}
4241 		ip = mtod(m, struct ip *);
4242 		if (ip == NULL) goto bad;
4243 	}
4244 
4245 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
4246 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
4247 	} else {
4248 		if (hlen == sizeof(struct ip)) {
4249 			sum = in_cksum_hdr(ip);
4250 		} else {
4251 			sum = in_cksum(m, hlen);
4252 		}
4253 	}
4254 	if (sum) {
4255 		KMOD_IPSTAT_INC(ips_badsum);
4256 		goto bad;
4257 	}
4258 
4259 	/* Retrieve the packet length. */
4260 	len = ntohs(ip->ip_len);
4261 
4262 	/*
4263 	 * Check for additional length bogosity
4264 	 */
4265 	if (len < hlen) {
4266 		KMOD_IPSTAT_INC(ips_badlen);
4267 		goto bad;
4268 	}
4269 
4270 	/*
4271 	 * Check that the amount of data in the buffers
4272 	 * is as at least much as the IP header would have us expect.
4273 	 * Drop packet if shorter than we expect.
4274 	 */
4275 	if (m->m_pkthdr.len < len) {
4276 		KMOD_IPSTAT_INC(ips_tooshort);
4277 		goto bad;
4278 	}
4279 
4280 	/* Checks out, proceed */
4281 	*mp = m;
4282 	return (0);
4283 
4284 bad:
4285 	*mp = m;
4286 	return (-1);
4287 }
4288 #endif /* INET */
4289 
4290 #ifdef INET6
4291 /*
4292  * Same as above, but for IPv6.
4293  * Cut-and-pasted from ip6_input.c.
4294  * XXX Should we update ip6stat, or not?
4295  */
4296 static int
4297 bridge_ip6_checkbasic(struct mbuf **mp)
4298 {
4299 	struct mbuf *m = *mp;
4300 	struct ip6_hdr *ip6;
4301 
4302 	/*
4303 	 * If the IPv6 header is not aligned, slurp it up into a new
4304 	 * mbuf with space for link headers, in the event we forward
4305 	 * it.  Otherwise, if it is aligned, make sure the entire base
4306 	 * IPv6 header is in the first mbuf of the chain.
4307 	 */
4308 	if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
4309 		struct ifnet *inifp = m->m_pkthdr.rcvif;
4310 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
4311 			    (max_linkhdr + 3) & ~3)) == NULL) {
4312 			/* XXXJRT new stat, please */
4313 			IP6STAT_INC(ip6s_toosmall);
4314 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
4315 			goto bad;
4316 		}
4317 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
4318 		struct ifnet *inifp = m->m_pkthdr.rcvif;
4319 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
4320 			IP6STAT_INC(ip6s_toosmall);
4321 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
4322 			goto bad;
4323 		}
4324 	}
4325 
4326 	ip6 = mtod(m, struct ip6_hdr *);
4327 
4328 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
4329 		IP6STAT_INC(ip6s_badvers);
4330 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
4331 		goto bad;
4332 	}
4333 
4334 	/* Checks out, proceed */
4335 	*mp = m;
4336 	return (0);
4337 
4338 bad:
4339 	*mp = m;
4340 	return (-1);
4341 }
4342 #endif /* INET6 */
4343 
4344 #ifdef INET
4345 /*
4346  * bridge_fragment:
4347  *
4348  *	Fragment mbuf chain in multiple packets and prepend ethernet header.
4349  */
4350 static int
4351 bridge_fragment(struct ifnet *ifp, struct mbuf **mp, struct ether_header *eh,
4352     int snap, struct llc *llc)
4353 {
4354 	struct mbuf *m = *mp, *nextpkt = NULL, *mprev = NULL, *mcur = NULL;
4355 	struct ip *ip;
4356 	int error = -1;
4357 
4358 	if (m->m_len < sizeof(struct ip) &&
4359 	    (m = m_pullup(m, sizeof(struct ip))) == NULL)
4360 		goto dropit;
4361 	ip = mtod(m, struct ip *);
4362 
4363 	m->m_pkthdr.csum_flags |= CSUM_IP;
4364 	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist);
4365 	if (error)
4366 		goto dropit;
4367 
4368 	/*
4369 	 * Walk the chain and re-add the Ethernet header for
4370 	 * each mbuf packet.
4371 	 */
4372 	for (mcur = m; mcur; mcur = mcur->m_nextpkt) {
4373 		nextpkt = mcur->m_nextpkt;
4374 		mcur->m_nextpkt = NULL;
4375 		if (snap) {
4376 			M_PREPEND(mcur, sizeof(struct llc), M_NOWAIT);
4377 			if (mcur == NULL) {
4378 				error = ENOBUFS;
4379 				if (mprev != NULL)
4380 					mprev->m_nextpkt = nextpkt;
4381 				goto dropit;
4382 			}
4383 			bcopy(llc, mtod(mcur, caddr_t),sizeof(struct llc));
4384 		}
4385 
4386 		M_PREPEND(mcur, ETHER_HDR_LEN, M_NOWAIT);
4387 		if (mcur == NULL) {
4388 			error = ENOBUFS;
4389 			if (mprev != NULL)
4390 				mprev->m_nextpkt = nextpkt;
4391 			goto dropit;
4392 		}
4393 		bcopy(eh, mtod(mcur, caddr_t), ETHER_HDR_LEN);
4394 
4395 		/*
4396 		 * The previous two M_PREPEND could have inserted one or two
4397 		 * mbufs in front so we have to update the previous packet's
4398 		 * m_nextpkt.
4399 		 */
4400 		mcur->m_nextpkt = nextpkt;
4401 		if (mprev != NULL)
4402 			mprev->m_nextpkt = mcur;
4403 		else {
4404 			/* The first mbuf in the original chain needs to be
4405 			 * updated. */
4406 			*mp = mcur;
4407 		}
4408 		mprev = mcur;
4409 	}
4410 
4411 	KMOD_IPSTAT_INC(ips_fragmented);
4412 	return (error);
4413 
4414 dropit:
4415 	for (mcur = *mp; mcur; mcur = m) { /* droping the full packet chain */
4416 		m = mcur->m_nextpkt;
4417 		m_freem(mcur);
4418 	}
4419 	return (error);
4420 }
4421 #endif /* INET */
4422 
4423 static void
4424 bridge_linkstate(struct ifnet *ifp)
4425 {
4426 	struct bridge_softc *sc = NULL;
4427 	struct bridge_iflist *bif;
4428 	struct epoch_tracker et;
4429 
4430 	NET_EPOCH_ENTER(et);
4431 
4432 	bif = ifp->if_bridge;
4433 	if (bif)
4434 		sc = bif->bif_sc;
4435 
4436 	if (sc != NULL) {
4437 		bridge_linkcheck(sc);
4438 		bstp_linkstate(&bif->bif_stp);
4439 	}
4440 
4441 	NET_EPOCH_EXIT(et);
4442 }
4443 
4444 static void
4445 bridge_linkcheck(struct bridge_softc *sc)
4446 {
4447 	struct bridge_iflist *bif;
4448 	int new_link, hasls;
4449 
4450 	BRIDGE_LOCK_OR_NET_EPOCH_ASSERT(sc);
4451 
4452 	new_link = LINK_STATE_DOWN;
4453 	hasls = 0;
4454 	/* Our link is considered up if at least one of our ports is active */
4455 	CK_LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
4456 		if (bif->bif_ifp->if_capabilities & IFCAP_LINKSTATE)
4457 			hasls++;
4458 		if (bif->bif_ifp->if_link_state == LINK_STATE_UP) {
4459 			new_link = LINK_STATE_UP;
4460 			break;
4461 		}
4462 	}
4463 	if (!CK_LIST_EMPTY(&sc->sc_iflist) && !hasls) {
4464 		/* If no interfaces support link-state then we default to up */
4465 		new_link = LINK_STATE_UP;
4466 	}
4467 	if_link_state_change(sc->sc_ifp, new_link);
4468 }
4469