xref: /netbsd-src/sys/net/if_bridge.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1 /*	$NetBSD: if_bridge.c,v 1.174 2020/08/01 06:50:43 maxv Exp $	*/
2 
3 /*
4  * Copyright 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *	This product includes software developed by Jason L. Wright
53  * 4. The name of the author may not be used to endorse or promote products
54  *    derived from this software without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
58  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
59  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
60  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
61  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
62  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
64  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
65  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66  * POSSIBILITY OF SUCH DAMAGE.
67  *
68  * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
69  */
70 
71 /*
72  * Network interface bridge support.
73  *
74  * TODO:
75  *
76  *	- Currently only supports Ethernet-like interfaces (Ethernet,
77  *	  802.11, VLANs on Ethernet, etc.)  Figure out a nice way
78  *	  to bridge other types of interfaces (FDDI-FDDI, and maybe
79  *	  consider heterogenous bridges).
80  */
81 
82 #include <sys/cdefs.h>
83 __KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.174 2020/08/01 06:50:43 maxv Exp $");
84 
85 #ifdef _KERNEL_OPT
86 #include "opt_inet.h"
87 #include "opt_net_mpsafe.h"
88 #endif /* _KERNEL_OPT */
89 
90 #include <sys/param.h>
91 #include <sys/kernel.h>
92 #include <sys/mbuf.h>
93 #include <sys/queue.h>
94 #include <sys/socket.h>
95 #include <sys/socketvar.h> /* for softnet_lock */
96 #include <sys/sockio.h>
97 #include <sys/systm.h>
98 #include <sys/proc.h>
99 #include <sys/pool.h>
100 #include <sys/kauth.h>
101 #include <sys/cpu.h>
102 #include <sys/cprng.h>
103 #include <sys/mutex.h>
104 #include <sys/kmem.h>
105 
106 #include <net/bpf.h>
107 #include <net/if.h>
108 #include <net/if_dl.h>
109 #include <net/if_types.h>
110 #include <net/if_llc.h>
111 
112 #include <net/if_ether.h>
113 #include <net/if_bridgevar.h>
114 #include <net/ether_sw_offload.h>
115 
116 /* Used for bridge_ip[6]_checkbasic */
117 #include <netinet/in.h>
118 #include <netinet/in_systm.h>
119 #include <netinet/ip.h>
120 #include <netinet/ip_var.h>
121 #include <netinet/ip_private.h>		/* XXX */
122 #include <netinet/ip6.h>
123 #include <netinet6/in6_var.h>
124 #include <netinet6/ip6_var.h>
125 #include <netinet6/ip6_private.h>	/* XXX */
126 
127 /*
128  * Size of the route hash table.  Must be a power of two.
129  */
130 #ifndef BRIDGE_RTHASH_SIZE
131 #define	BRIDGE_RTHASH_SIZE		1024
132 #endif
133 
134 #define	BRIDGE_RTHASH_MASK		(BRIDGE_RTHASH_SIZE - 1)
135 
136 #include "carp.h"
137 #if NCARP > 0
138 #include <netinet/in.h>
139 #include <netinet/in_var.h>
140 #include <netinet/ip_carp.h>
141 #endif
142 
143 #include "ioconf.h"
144 
145 __CTASSERT(sizeof(struct ifbifconf) == sizeof(struct ifbaconf));
146 __CTASSERT(offsetof(struct ifbifconf, ifbic_len) == offsetof(struct ifbaconf, ifbac_len));
147 __CTASSERT(offsetof(struct ifbifconf, ifbic_buf) == offsetof(struct ifbaconf, ifbac_buf));
148 
149 /*
150  * Maximum number of addresses to cache.
151  */
152 #ifndef BRIDGE_RTABLE_MAX
153 #define	BRIDGE_RTABLE_MAX		100
154 #endif
155 
156 /*
157  * Spanning tree defaults.
158  */
159 #define	BSTP_DEFAULT_MAX_AGE		(20 * 256)
160 #define	BSTP_DEFAULT_HELLO_TIME		(2 * 256)
161 #define	BSTP_DEFAULT_FORWARD_DELAY	(15 * 256)
162 #define	BSTP_DEFAULT_HOLD_TIME		(1 * 256)
163 #define	BSTP_DEFAULT_BRIDGE_PRIORITY	0x8000
164 #define	BSTP_DEFAULT_PORT_PRIORITY	0x80
165 #define	BSTP_DEFAULT_PATH_COST		55
166 
167 /*
168  * Timeout (in seconds) for entries learned dynamically.
169  */
170 #ifndef BRIDGE_RTABLE_TIMEOUT
171 #define	BRIDGE_RTABLE_TIMEOUT		(20 * 60)	/* same as ARP */
172 #endif
173 
174 /*
175  * Number of seconds between walks of the route list.
176  */
177 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
178 #define	BRIDGE_RTABLE_PRUNE_PERIOD	(5 * 60)
179 #endif
180 
181 #define BRIDGE_RT_LOCK(_sc)	mutex_enter((_sc)->sc_rtlist_lock)
182 #define BRIDGE_RT_UNLOCK(_sc)	mutex_exit((_sc)->sc_rtlist_lock)
183 #define BRIDGE_RT_LOCKED(_sc)	mutex_owned((_sc)->sc_rtlist_lock)
184 
185 #define BRIDGE_RT_PSZ_PERFORM(_sc) \
186 				pserialize_perform((_sc)->sc_rtlist_psz)
187 
188 #define BRIDGE_RT_RENTER(__s)	do { __s = pserialize_read_enter(); } while (0)
189 #define BRIDGE_RT_REXIT(__s)	do { pserialize_read_exit(__s); } while (0)
190 
191 #define BRIDGE_RTLIST_READER_FOREACH(_brt, _sc)			\
192 	PSLIST_READER_FOREACH((_brt), &((_sc)->sc_rtlist),		\
193 	    struct bridge_rtnode, brt_list)
194 #define BRIDGE_RTLIST_WRITER_FOREACH(_brt, _sc)			\
195 	PSLIST_WRITER_FOREACH((_brt), &((_sc)->sc_rtlist),		\
196 	    struct bridge_rtnode, brt_list)
197 #define BRIDGE_RTLIST_WRITER_INSERT_HEAD(_sc, _brt)			\
198 	PSLIST_WRITER_INSERT_HEAD(&(_sc)->sc_rtlist, brt, brt_list)
199 #define BRIDGE_RTLIST_WRITER_REMOVE(_brt)				\
200 	PSLIST_WRITER_REMOVE((_brt), brt_list)
201 
202 #define BRIDGE_RTHASH_READER_FOREACH(_brt, _sc, _hash)			\
203 	PSLIST_READER_FOREACH((_brt), &(_sc)->sc_rthash[(_hash)],	\
204 	    struct bridge_rtnode, brt_hash)
205 #define BRIDGE_RTHASH_WRITER_FOREACH(_brt, _sc, _hash)			\
206 	PSLIST_WRITER_FOREACH((_brt), &(_sc)->sc_rthash[(_hash)],	\
207 	    struct bridge_rtnode, brt_hash)
208 #define BRIDGE_RTHASH_WRITER_INSERT_HEAD(_sc, _hash, _brt)		\
209 	PSLIST_WRITER_INSERT_HEAD(&(_sc)->sc_rthash[(_hash)], brt, brt_hash)
210 #define BRIDGE_RTHASH_WRITER_INSERT_AFTER(_brt, _new)			\
211 	PSLIST_WRITER_INSERT_AFTER((_brt), (_new), brt_hash)
212 #define BRIDGE_RTHASH_WRITER_REMOVE(_brt)				\
213 	PSLIST_WRITER_REMOVE((_brt), brt_hash)
214 
215 #ifdef NET_MPSAFE
216 #define DECLARE_LOCK_VARIABLE
217 #define ACQUIRE_GLOBAL_LOCKS()	do { } while (0)
218 #define RELEASE_GLOBAL_LOCKS()	do { } while (0)
219 #else
220 #define DECLARE_LOCK_VARIABLE	int __s
221 #define ACQUIRE_GLOBAL_LOCKS()	do {					\
222 					KERNEL_LOCK(1, NULL);		\
223 					mutex_enter(softnet_lock);	\
224 					__s = splsoftnet();		\
225 				} while (0)
226 #define RELEASE_GLOBAL_LOCKS()	do {					\
227 					splx(__s);			\
228 					mutex_exit(softnet_lock);	\
229 					KERNEL_UNLOCK_ONE(NULL);	\
230 				} while (0)
231 #endif
232 
233 struct psref_class *bridge_psref_class __read_mostly;
234 
235 int	bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
236 
237 static struct pool bridge_rtnode_pool;
238 
239 static int	bridge_clone_create(struct if_clone *, int);
240 static int	bridge_clone_destroy(struct ifnet *);
241 
242 static int	bridge_ioctl(struct ifnet *, u_long, void *);
243 static int	bridge_init(struct ifnet *);
244 static void	bridge_stop(struct ifnet *, int);
245 static void	bridge_start(struct ifnet *);
246 
247 static void	bridge_input(struct ifnet *, struct mbuf *);
248 static void	bridge_forward(struct bridge_softc *, struct mbuf *);
249 
250 static void	bridge_timer(void *);
251 
252 static void	bridge_broadcast(struct bridge_softc *, struct ifnet *,
253 				 struct mbuf *);
254 
255 static int	bridge_rtupdate(struct bridge_softc *, const uint8_t *,
256 				struct ifnet *, int, uint8_t);
257 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
258 static void	bridge_rttrim(struct bridge_softc *);
259 static void	bridge_rtage(struct bridge_softc *);
260 static void	bridge_rtage_work(struct work *, void *);
261 static void	bridge_rtflush(struct bridge_softc *, int);
262 static int	bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
263 static void	bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp);
264 
265 static void	bridge_rtable_init(struct bridge_softc *);
266 static void	bridge_rtable_fini(struct bridge_softc *);
267 
268 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
269 						  const uint8_t *);
270 static int	bridge_rtnode_insert(struct bridge_softc *,
271 				     struct bridge_rtnode *);
272 static void	bridge_rtnode_remove(struct bridge_softc *,
273 				     struct bridge_rtnode *);
274 static void	bridge_rtnode_destroy(struct bridge_rtnode *);
275 
276 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
277 						  const char *name,
278 						  struct psref *);
279 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
280 						     struct ifnet *ifp,
281 						     struct psref *);
282 static void	bridge_release_member(struct bridge_softc *, struct bridge_iflist *,
283                                       struct psref *);
284 static void	bridge_delete_member(struct bridge_softc *,
285 				     struct bridge_iflist *);
286 static void	bridge_acquire_member(struct bridge_softc *sc,
287                                       struct bridge_iflist *,
288                                       struct psref *);
289 
290 static int	bridge_ioctl_add(struct bridge_softc *, void *);
291 static int	bridge_ioctl_del(struct bridge_softc *, void *);
292 static int	bridge_ioctl_gifflags(struct bridge_softc *, void *);
293 static int	bridge_ioctl_sifflags(struct bridge_softc *, void *);
294 static int	bridge_ioctl_scache(struct bridge_softc *, void *);
295 static int	bridge_ioctl_gcache(struct bridge_softc *, void *);
296 static int	bridge_ioctl_gifs(struct bridge_softc *, void *);
297 static int	bridge_ioctl_rts(struct bridge_softc *, void *);
298 static int	bridge_ioctl_saddr(struct bridge_softc *, void *);
299 static int	bridge_ioctl_sto(struct bridge_softc *, void *);
300 static int	bridge_ioctl_gto(struct bridge_softc *, void *);
301 static int	bridge_ioctl_daddr(struct bridge_softc *, void *);
302 static int	bridge_ioctl_flush(struct bridge_softc *, void *);
303 static int	bridge_ioctl_gpri(struct bridge_softc *, void *);
304 static int	bridge_ioctl_spri(struct bridge_softc *, void *);
305 static int	bridge_ioctl_ght(struct bridge_softc *, void *);
306 static int	bridge_ioctl_sht(struct bridge_softc *, void *);
307 static int	bridge_ioctl_gfd(struct bridge_softc *, void *);
308 static int	bridge_ioctl_sfd(struct bridge_softc *, void *);
309 static int	bridge_ioctl_gma(struct bridge_softc *, void *);
310 static int	bridge_ioctl_sma(struct bridge_softc *, void *);
311 static int	bridge_ioctl_sifprio(struct bridge_softc *, void *);
312 static int	bridge_ioctl_sifcost(struct bridge_softc *, void *);
313 static int	bridge_ioctl_gfilt(struct bridge_softc *, void *);
314 static int	bridge_ioctl_sfilt(struct bridge_softc *, void *);
315 static int	bridge_ipf(void *, struct mbuf **, struct ifnet *, int);
316 static int	bridge_ip_checkbasic(struct mbuf **mp);
317 # ifdef INET6
318 static int	bridge_ip6_checkbasic(struct mbuf **mp);
319 # endif /* INET6 */
320 
321 struct bridge_control {
322 	int	(*bc_func)(struct bridge_softc *, void *);
323 	int	bc_argsize;
324 	int	bc_flags;
325 };
326 
327 #define	BC_F_COPYIN		0x01	/* copy arguments in */
328 #define	BC_F_COPYOUT		0x02	/* copy arguments out */
329 #define	BC_F_SUSER		0x04	/* do super-user check */
330 #define BC_F_XLATEIN		0x08	/* xlate arguments in */
331 #define BC_F_XLATEOUT		0x10	/* xlate arguments out */
332 
333 static const struct bridge_control bridge_control_table[] = {
334 [BRDGADD] = {bridge_ioctl_add, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
335 [BRDGDEL] = {bridge_ioctl_del, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
336 
337 [BRDGGIFFLGS] = {bridge_ioctl_gifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_COPYOUT},
338 [BRDGSIFFLGS] = {bridge_ioctl_sifflags, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
339 
340 [BRDGSCACHE] = {bridge_ioctl_scache, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
341 [BRDGGCACHE] = {bridge_ioctl_gcache, sizeof(struct ifbrparam), BC_F_COPYOUT},
342 
343 [OBRDGGIFS] = {bridge_ioctl_gifs, sizeof(struct ifbifconf), BC_F_COPYIN|BC_F_COPYOUT},
344 [OBRDGRTS] = {bridge_ioctl_rts, sizeof(struct ifbaconf), BC_F_COPYIN|BC_F_COPYOUT},
345 
346 [BRDGSADDR] = {bridge_ioctl_saddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER},
347 
348 [BRDGSTO] = {bridge_ioctl_sto, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
349 [BRDGGTO] = {bridge_ioctl_gto, sizeof(struct ifbrparam), BC_F_COPYOUT},
350 
351 [BRDGDADDR] = {bridge_ioctl_daddr, sizeof(struct ifbareq), BC_F_COPYIN|BC_F_SUSER},
352 
353 [BRDGFLUSH] = {bridge_ioctl_flush, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
354 
355 [BRDGGPRI] = {bridge_ioctl_gpri, sizeof(struct ifbrparam), BC_F_COPYOUT},
356 [BRDGSPRI] = {bridge_ioctl_spri, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
357 
358 [BRDGGHT] = {bridge_ioctl_ght, sizeof(struct ifbrparam), BC_F_COPYOUT},
359 [BRDGSHT] = {bridge_ioctl_sht, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
360 
361 [BRDGGFD] = {bridge_ioctl_gfd, sizeof(struct ifbrparam), BC_F_COPYOUT},
362 [BRDGSFD] = {bridge_ioctl_sfd, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
363 
364 [BRDGGMA] = {bridge_ioctl_gma, sizeof(struct ifbrparam), BC_F_COPYOUT},
365 [BRDGSMA] = {bridge_ioctl_sma, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
366 
367 [BRDGSIFPRIO] = {bridge_ioctl_sifprio, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
368 
369 [BRDGSIFCOST] = {bridge_ioctl_sifcost, sizeof(struct ifbreq), BC_F_COPYIN|BC_F_SUSER},
370 
371 [BRDGGFILT] = {bridge_ioctl_gfilt, sizeof(struct ifbrparam), BC_F_COPYOUT},
372 [BRDGSFILT] = {bridge_ioctl_sfilt, sizeof(struct ifbrparam), BC_F_COPYIN|BC_F_SUSER},
373 
374 [BRDGGIFS] = {bridge_ioctl_gifs, sizeof(struct ifbifconf), BC_F_XLATEIN|BC_F_XLATEOUT},
375 [BRDGRTS] = {bridge_ioctl_rts, sizeof(struct ifbaconf), BC_F_XLATEIN|BC_F_XLATEOUT},
376 };
377 
378 static const int bridge_control_table_size = __arraycount(bridge_control_table);
379 
380 static struct if_clone bridge_cloner =
381     IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy);
382 
383 /*
384  * bridgeattach:
385  *
386  *	Pseudo-device attach routine.
387  */
388 void
389 bridgeattach(int n)
390 {
391 
392 	pool_init(&bridge_rtnode_pool, sizeof(struct bridge_rtnode),
393 	    0, 0, 0, "brtpl", NULL, IPL_NET);
394 
395 	bridge_psref_class = psref_class_create("bridge", IPL_SOFTNET);
396 
397 	if_clone_attach(&bridge_cloner);
398 }
399 
400 /*
401  * bridge_clone_create:
402  *
403  *	Create a new bridge instance.
404  */
405 static int
406 bridge_clone_create(struct if_clone *ifc, int unit)
407 {
408 	struct bridge_softc *sc;
409 	struct ifnet *ifp;
410 	int error;
411 
412 	sc = kmem_zalloc(sizeof(*sc),  KM_SLEEP);
413 	ifp = &sc->sc_if;
414 
415 	sc->sc_brtmax = BRIDGE_RTABLE_MAX;
416 	sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
417 	sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
418 	sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
419 	sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
420 	sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
421 	sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
422 	sc->sc_filter_flags = 0;
423 
424 	/* Initialize our routing table. */
425 	bridge_rtable_init(sc);
426 
427 	error = workqueue_create(&sc->sc_rtage_wq, "bridge_rtage",
428 	    bridge_rtage_work, sc, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
429 	if (error)
430 		panic("%s: workqueue_create %d\n", __func__, error);
431 
432 	callout_init(&sc->sc_brcallout, CALLOUT_MPSAFE);
433 	callout_init(&sc->sc_bstpcallout, CALLOUT_MPSAFE);
434 
435 	mutex_init(&sc->sc_iflist_psref.bip_lock, MUTEX_DEFAULT, IPL_NONE);
436 	PSLIST_INIT(&sc->sc_iflist_psref.bip_iflist);
437 	sc->sc_iflist_psref.bip_psz = pserialize_create();
438 
439 	if_initname(ifp, ifc->ifc_name, unit);
440 	ifp->if_softc = sc;
441 	ifp->if_extflags = IFEF_NO_LINK_STATE_CHANGE;
442 #ifdef NET_MPSAFE
443 	ifp->if_extflags |= IFEF_MPSAFE;
444 #endif
445 	ifp->if_mtu = ETHERMTU;
446 	ifp->if_ioctl = bridge_ioctl;
447 	ifp->if_output = bridge_output;
448 	ifp->if_start = bridge_start;
449 	ifp->if_stop = bridge_stop;
450 	ifp->if_init = bridge_init;
451 	ifp->if_type = IFT_BRIDGE;
452 	ifp->if_addrlen = 0;
453 	ifp->if_dlt = DLT_EN10MB;
454 	ifp->if_hdrlen = ETHER_HDR_LEN;
455 
456 	error = if_initialize(ifp);
457 	if (error != 0) {
458 		pserialize_destroy(sc->sc_iflist_psref.bip_psz);
459 		mutex_destroy(&sc->sc_iflist_psref.bip_lock);
460 		callout_destroy(&sc->sc_brcallout);
461 		callout_destroy(&sc->sc_bstpcallout);
462 		workqueue_destroy(sc->sc_rtage_wq);
463 		bridge_rtable_fini(sc);
464 		kmem_free(sc, sizeof(*sc));
465 
466 		return error;
467 	}
468 	if_alloc_sadl(ifp);
469 	if_register(ifp);
470 
471 	return 0;
472 }
473 
474 /*
475  * bridge_clone_destroy:
476  *
477  *	Destroy a bridge instance.
478  */
479 static int
480 bridge_clone_destroy(struct ifnet *ifp)
481 {
482 	struct bridge_softc *sc = ifp->if_softc;
483 	struct bridge_iflist *bif;
484 
485 	if ((ifp->if_flags & IFF_RUNNING) != 0)
486 		bridge_stop(ifp, 1);
487 
488 	BRIDGE_LOCK(sc);
489 	for (;;) {
490 		bif = PSLIST_WRITER_FIRST(&sc->sc_iflist_psref.bip_iflist, struct bridge_iflist,
491 		    bif_next);
492 		if (bif == NULL)
493 			break;
494 		bridge_delete_member(sc, bif);
495 	}
496 	PSLIST_DESTROY(&sc->sc_iflist_psref.bip_iflist);
497 	BRIDGE_UNLOCK(sc);
498 
499 	if_detach(ifp);
500 
501 	/* Tear down the routing table. */
502 	bridge_rtable_fini(sc);
503 
504 	pserialize_destroy(sc->sc_iflist_psref.bip_psz);
505 	mutex_destroy(&sc->sc_iflist_psref.bip_lock);
506 	callout_destroy(&sc->sc_brcallout);
507 	callout_destroy(&sc->sc_bstpcallout);
508 	workqueue_destroy(sc->sc_rtage_wq);
509 	kmem_free(sc, sizeof(*sc));
510 
511 	return 0;
512 }
513 
514 /*
515  * bridge_ioctl:
516  *
517  *	Handle a control request from the operator.
518  */
519 static int
520 bridge_ioctl(struct ifnet *ifp, u_long cmd, void *data)
521 {
522 	struct bridge_softc *sc = ifp->if_softc;
523 	struct lwp *l = curlwp;	/* XXX */
524 	union {
525 		struct ifbreq ifbreq;
526 		struct ifbifconf ifbifconf;
527 		struct ifbareq ifbareq;
528 		struct ifbaconf ifbaconf;
529 		struct ifbrparam ifbrparam;
530 	} args;
531 	struct ifdrv *ifd = (struct ifdrv *) data;
532 	const struct bridge_control *bc = NULL; /* XXXGCC */
533 	int s, error = 0;
534 
535 	/* Authorize command before calling splsoftnet(). */
536 	switch (cmd) {
537 	case SIOCGDRVSPEC:
538 	case SIOCSDRVSPEC:
539 		if (ifd->ifd_cmd >= bridge_control_table_size
540 		    || (bc = &bridge_control_table[ifd->ifd_cmd]) == NULL) {
541 			error = EINVAL;
542 			return error;
543 		}
544 
545 		/* We only care about BC_F_SUSER at this point. */
546 		if ((bc->bc_flags & BC_F_SUSER) == 0)
547 			break;
548 
549 		error = kauth_authorize_network(l->l_cred,
550 		    KAUTH_NETWORK_INTERFACE_BRIDGE,
551 		    cmd == SIOCGDRVSPEC ?
552 		     KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_GETPRIV :
553 		     KAUTH_REQ_NETWORK_INTERFACE_BRIDGE_SETPRIV,
554 		     ifd, NULL, NULL);
555 		if (error)
556 			return error;
557 
558 		break;
559 	}
560 
561 	s = splsoftnet();
562 
563 	switch (cmd) {
564 	case SIOCGDRVSPEC:
565 	case SIOCSDRVSPEC:
566 		KASSERT(bc != NULL);
567 		if (cmd == SIOCGDRVSPEC &&
568 		    (bc->bc_flags & (BC_F_COPYOUT|BC_F_XLATEOUT)) == 0) {
569 			error = EINVAL;
570 			break;
571 		}
572 		else if (cmd == SIOCSDRVSPEC &&
573 		    (bc->bc_flags & (BC_F_COPYOUT|BC_F_XLATEOUT)) != 0) {
574 			error = EINVAL;
575 			break;
576 		}
577 
578 		/* BC_F_SUSER is checked above, before splsoftnet(). */
579 
580 		if ((bc->bc_flags & (BC_F_XLATEIN|BC_F_XLATEOUT)) == 0
581 		    && (ifd->ifd_len != bc->bc_argsize
582 			|| ifd->ifd_len > sizeof(args))) {
583 			error = EINVAL;
584 			break;
585 		}
586 
587 		memset(&args, 0, sizeof(args));
588 		if (bc->bc_flags & BC_F_COPYIN) {
589 			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
590 			if (error)
591 				break;
592 		} else if (bc->bc_flags & BC_F_XLATEIN) {
593 			args.ifbifconf.ifbic_len = ifd->ifd_len;
594 			args.ifbifconf.ifbic_buf = ifd->ifd_data;
595 		}
596 
597 		error = (*bc->bc_func)(sc, &args);
598 		if (error)
599 			break;
600 
601 		if (bc->bc_flags & BC_F_COPYOUT) {
602 			error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
603 		} else if (bc->bc_flags & BC_F_XLATEOUT) {
604 			ifd->ifd_len = args.ifbifconf.ifbic_len;
605 			ifd->ifd_data = args.ifbifconf.ifbic_buf;
606 		}
607 		break;
608 
609 	case SIOCSIFFLAGS:
610 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
611 			break;
612 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
613 		case IFF_RUNNING:
614 			/*
615 			 * If interface is marked down and it is running,
616 			 * then stop and disable it.
617 			 */
618 			(*ifp->if_stop)(ifp, 1);
619 			break;
620 		case IFF_UP:
621 			/*
622 			 * If interface is marked up and it is stopped, then
623 			 * start it.
624 			 */
625 			error = (*ifp->if_init)(ifp);
626 			break;
627 		default:
628 			break;
629 		}
630 		break;
631 
632 	case SIOCSIFMTU:
633 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
634 			error = 0;
635 		break;
636 
637         case SIOCGIFCAP:
638 	    {
639 		struct ifcapreq *ifcr = (struct ifcapreq *)data;
640                 ifcr->ifcr_capabilities = sc->sc_capenable;
641                 ifcr->ifcr_capenable = sc->sc_capenable;
642 		break;
643 	    }
644 
645 	default:
646 		error = ifioctl_common(ifp, cmd, data);
647 		break;
648 	}
649 
650 	splx(s);
651 
652 	return error;
653 }
654 
655 /*
656  * bridge_lookup_member:
657  *
658  *	Lookup a bridge member interface.
659  */
660 static struct bridge_iflist *
661 bridge_lookup_member(struct bridge_softc *sc, const char *name, struct psref *psref)
662 {
663 	struct bridge_iflist *bif;
664 	struct ifnet *ifp;
665 	int s;
666 
667 	BRIDGE_PSZ_RENTER(s);
668 
669 	BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
670 		ifp = bif->bif_ifp;
671 		if (strcmp(ifp->if_xname, name) == 0)
672 			break;
673 	}
674 	if (bif != NULL)
675 		bridge_acquire_member(sc, bif, psref);
676 
677 	BRIDGE_PSZ_REXIT(s);
678 
679 	return bif;
680 }
681 
682 /*
683  * bridge_lookup_member_if:
684  *
685  *	Lookup a bridge member interface by ifnet*.
686  */
687 static struct bridge_iflist *
688 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp,
689     struct psref *psref)
690 {
691 	struct bridge_iflist *bif;
692 	int s;
693 
694 	BRIDGE_PSZ_RENTER(s);
695 
696 	bif = member_ifp->if_bridgeif;
697 	if (bif != NULL) {
698 		psref_acquire(psref, &bif->bif_psref,
699 		    bridge_psref_class);
700 	}
701 
702 	BRIDGE_PSZ_REXIT(s);
703 
704 	return bif;
705 }
706 
707 static void
708 bridge_acquire_member(struct bridge_softc *sc, struct bridge_iflist *bif,
709     struct psref *psref)
710 {
711 
712 	psref_acquire(psref, &bif->bif_psref, bridge_psref_class);
713 }
714 
715 /*
716  * bridge_release_member:
717  *
718  *	Release the specified member interface.
719  */
720 static void
721 bridge_release_member(struct bridge_softc *sc, struct bridge_iflist *bif,
722     struct psref *psref)
723 {
724 
725 	psref_release(psref, &bif->bif_psref, bridge_psref_class);
726 }
727 
728 /*
729  * bridge_delete_member:
730  *
731  *	Delete the specified member interface.
732  */
733 static void
734 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif)
735 {
736 	struct ifnet *ifs = bif->bif_ifp;
737 
738 	KASSERT(BRIDGE_LOCKED(sc));
739 
740 	ifs->_if_input = ether_input;
741 	ifs->if_bridge = NULL;
742 	ifs->if_bridgeif = NULL;
743 
744 	PSLIST_WRITER_REMOVE(bif, bif_next);
745 	BRIDGE_PSZ_PERFORM(sc);
746 	BRIDGE_UNLOCK(sc);
747 
748 	switch (ifs->if_type) {
749 	case IFT_ETHER:
750 	case IFT_L2TP:
751 		/*
752 		 * Take the interface out of promiscuous mode.
753 		 * Don't call it with holding a spin lock.
754 		 */
755 		(void) ifpromisc(ifs, 0);
756 		IFNET_LOCK(ifs);
757 		(void) ether_disable_vlan_mtu(ifs);
758 		IFNET_UNLOCK(ifs);
759 		break;
760 	default:
761 #ifdef DIAGNOSTIC
762 		panic("%s: impossible", __func__);
763 #endif
764 		break;
765 	}
766 
767 	psref_target_destroy(&bif->bif_psref, bridge_psref_class);
768 
769 	PSLIST_ENTRY_DESTROY(bif, bif_next);
770 	kmem_free(bif, sizeof(*bif));
771 
772 	BRIDGE_LOCK(sc);
773 }
774 
775 /*
776  * bridge_calc_csum_flags:
777  *
778  *	Calculate logical and b/w csum flags each member interface supports.
779  */
780 void
781 bridge_calc_csum_flags(struct bridge_softc *sc)
782 {
783 	struct bridge_iflist *bif;
784 	struct ifnet *ifs = NULL;
785 	int flags = ~0;
786 	int capenable = ~0;
787 
788 	BRIDGE_LOCK(sc);
789 	BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
790 		ifs = bif->bif_ifp;
791 		flags &= ifs->if_csum_flags_tx;
792 		capenable &= ifs->if_capenable;
793 	}
794 	sc->sc_csum_flags_tx = flags;
795 	sc->sc_capenable = (ifs != NULL) ? capenable : 0;
796 	BRIDGE_UNLOCK(sc);
797 }
798 
799 static int
800 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
801 {
802 	struct ifbreq *req = arg;
803 	struct bridge_iflist *bif = NULL;
804 	struct ifnet *ifs;
805 	int error = 0;
806 	struct psref psref;
807 
808 	ifs = if_get(req->ifbr_ifsname, &psref);
809 	if (ifs == NULL)
810 		return ENOENT;
811 
812 	if (ifs->if_bridge == sc) {
813 		error = EEXIST;
814 		goto out;
815 	}
816 
817 	if (ifs->if_bridge != NULL) {
818 		error = EBUSY;
819 		goto out;
820 	}
821 
822 	if (ifs->_if_input != ether_input) {
823 		error = EINVAL;
824 		goto out;
825 	}
826 
827 	/* FIXME: doesn't work with non-IFF_SIMPLEX interfaces */
828 	if ((ifs->if_flags & IFF_SIMPLEX) == 0) {
829 		error = EINVAL;
830 		goto out;
831 	}
832 
833 	bif = kmem_alloc(sizeof(*bif), KM_SLEEP);
834 
835 	switch (ifs->if_type) {
836 	case IFT_ETHER:
837 		if (sc->sc_if.if_mtu != ifs->if_mtu) {
838 			/* Change MTU of added interface to bridge MTU */
839 			struct ifreq ifr;
840 			memset(&ifr, 0, sizeof(ifr));
841 			ifr.ifr_mtu = sc->sc_if.if_mtu;
842 			IFNET_LOCK(ifs);
843 			error = ether_ioctl(ifs, SIOCSIFMTU, &ifr);
844 			IFNET_UNLOCK(ifs);
845 			if (error != 0)
846 				goto out;
847 		}
848 		/* FALLTHROUGH */
849 	case IFT_L2TP:
850 		IFNET_LOCK(ifs);
851 		error = ether_enable_vlan_mtu(ifs);
852 		IFNET_UNLOCK(ifs);
853 		if (error > 0)
854 			goto out;
855 		/*
856 		 * Place the interface into promiscuous mode.
857 		 */
858 		error = ifpromisc(ifs, 1);
859 		if (error)
860 			goto out;
861 		break;
862 	default:
863 		error = EINVAL;
864 		goto out;
865 	}
866 
867 	bif->bif_ifp = ifs;
868 	bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
869 	bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
870 	bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
871 	PSLIST_ENTRY_INIT(bif, bif_next);
872 	psref_target_init(&bif->bif_psref, bridge_psref_class);
873 
874 	BRIDGE_LOCK(sc);
875 
876 	ifs->if_bridge = sc;
877 	ifs->if_bridgeif = bif;
878 	PSLIST_WRITER_INSERT_HEAD(&sc->sc_iflist_psref.bip_iflist, bif, bif_next);
879 	ifs->_if_input = bridge_input;
880 
881 	BRIDGE_UNLOCK(sc);
882 
883 	bridge_calc_csum_flags(sc);
884 
885 	if (sc->sc_if.if_flags & IFF_RUNNING)
886 		bstp_initialization(sc);
887 	else
888 		bstp_stop(sc);
889 
890 out:
891 	if_put(ifs, &psref);
892 	if (error) {
893 		if (bif != NULL)
894 			kmem_free(bif, sizeof(*bif));
895 	}
896 	return error;
897 }
898 
899 static int
900 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
901 {
902 	struct ifbreq *req = arg;
903 	const char *name = req->ifbr_ifsname;
904 	struct bridge_iflist *bif;
905 	struct ifnet *ifs;
906 
907 	BRIDGE_LOCK(sc);
908 
909 	/*
910 	 * Don't use bridge_lookup_member. We want to get a member
911 	 * with bif_refs == 0.
912 	 */
913 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) {
914 		ifs = bif->bif_ifp;
915 		if (strcmp(ifs->if_xname, name) == 0)
916 			break;
917 	}
918 
919 	if (bif == NULL) {
920 		BRIDGE_UNLOCK(sc);
921 		return ENOENT;
922 	}
923 
924 	bridge_delete_member(sc, bif);
925 
926 	BRIDGE_UNLOCK(sc);
927 
928 	bridge_rtdelete(sc, ifs);
929 	bridge_calc_csum_flags(sc);
930 
931 	if (sc->sc_if.if_flags & IFF_RUNNING)
932 		bstp_initialization(sc);
933 
934 	return 0;
935 }
936 
937 static int
938 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
939 {
940 	struct ifbreq *req = arg;
941 	struct bridge_iflist *bif;
942 	struct psref psref;
943 
944 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
945 	if (bif == NULL)
946 		return ENOENT;
947 
948 	req->ifbr_ifsflags = bif->bif_flags;
949 	req->ifbr_state = bif->bif_state;
950 	req->ifbr_priority = bif->bif_priority;
951 	req->ifbr_path_cost = bif->bif_path_cost;
952 	req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
953 
954 	bridge_release_member(sc, bif, &psref);
955 
956 	return 0;
957 }
958 
959 static int
960 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
961 {
962 	struct ifbreq *req = arg;
963 	struct bridge_iflist *bif;
964 	struct psref psref;
965 
966 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
967 	if (bif == NULL)
968 		return ENOENT;
969 
970 	if (req->ifbr_ifsflags & IFBIF_STP) {
971 		switch (bif->bif_ifp->if_type) {
972 		case IFT_ETHER:
973 		case IFT_L2TP:
974 			/* These can do spanning tree. */
975 			break;
976 
977 		default:
978 			/* Nothing else can. */
979 			bridge_release_member(sc, bif, &psref);
980 			return EINVAL;
981 		}
982 	}
983 
984 	bif->bif_flags = req->ifbr_ifsflags;
985 
986 	bridge_release_member(sc, bif, &psref);
987 
988 	if (sc->sc_if.if_flags & IFF_RUNNING)
989 		bstp_initialization(sc);
990 
991 	return 0;
992 }
993 
994 static int
995 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
996 {
997 	struct ifbrparam *param = arg;
998 
999 	sc->sc_brtmax = param->ifbrp_csize;
1000 	bridge_rttrim(sc);
1001 
1002 	return 0;
1003 }
1004 
1005 static int
1006 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1007 {
1008 	struct ifbrparam *param = arg;
1009 
1010 	param->ifbrp_csize = sc->sc_brtmax;
1011 
1012 	return 0;
1013 }
1014 
1015 static int
1016 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1017 {
1018 	struct ifbifconf *bifc = arg;
1019 	struct bridge_iflist *bif;
1020 	struct ifbreq *breqs;
1021 	int i, count, error = 0;
1022 
1023 retry:
1024 	BRIDGE_LOCK(sc);
1025 	count = 0;
1026 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc)
1027 		count++;
1028 	BRIDGE_UNLOCK(sc);
1029 
1030 	if (count == 0) {
1031 		bifc->ifbic_len = 0;
1032 		return 0;
1033 	}
1034 
1035 	if (bifc->ifbic_len == 0 || bifc->ifbic_len < (sizeof(*breqs) * count)) {
1036 		/* Tell that a larger buffer is needed */
1037 		bifc->ifbic_len = sizeof(*breqs) * count;
1038 		return 0;
1039 	}
1040 
1041 	breqs = kmem_alloc(sizeof(*breqs) * count, KM_SLEEP);
1042 
1043 	BRIDGE_LOCK(sc);
1044 
1045 	i = 0;
1046 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc)
1047 		i++;
1048 	if (i > count) {
1049 		/*
1050 		 * The number of members has been increased.
1051 		 * We need more memory!
1052 		 */
1053 		BRIDGE_UNLOCK(sc);
1054 		kmem_free(breqs, sizeof(*breqs) * count);
1055 		goto retry;
1056 	}
1057 
1058 	i = 0;
1059 	BRIDGE_IFLIST_WRITER_FOREACH(bif, sc) {
1060 		struct ifbreq *breq = &breqs[i++];
1061 		memset(breq, 0, sizeof(*breq));
1062 
1063 		strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1064 		    sizeof(breq->ifbr_ifsname));
1065 		breq->ifbr_ifsflags = bif->bif_flags;
1066 		breq->ifbr_state = bif->bif_state;
1067 		breq->ifbr_priority = bif->bif_priority;
1068 		breq->ifbr_path_cost = bif->bif_path_cost;
1069 		breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1070 	}
1071 
1072 	/* Don't call copyout with holding the mutex */
1073 	BRIDGE_UNLOCK(sc);
1074 
1075 	for (i = 0; i < count; i++) {
1076 		error = copyout(&breqs[i], bifc->ifbic_req + i, sizeof(*breqs));
1077 		if (error)
1078 			break;
1079 	}
1080 	bifc->ifbic_len = sizeof(*breqs) * i;
1081 
1082 	kmem_free(breqs, sizeof(*breqs) * count);
1083 
1084 	return error;
1085 }
1086 
1087 static int
1088 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1089 {
1090 	struct ifbaconf *bac = arg;
1091 	struct bridge_rtnode *brt;
1092 	struct ifbareq bareq;
1093 	int count = 0, error = 0, len;
1094 
1095 	if (bac->ifbac_len == 0)
1096 		return 0;
1097 
1098 	BRIDGE_RT_LOCK(sc);
1099 
1100 	/* The passed buffer is not enough, tell a required size. */
1101 	if (bac->ifbac_len < (sizeof(bareq) * sc->sc_brtcnt)) {
1102 		count = sc->sc_brtcnt;
1103 		goto out;
1104 	}
1105 
1106 	len = bac->ifbac_len;
1107 	BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) {
1108 		if (len < sizeof(bareq))
1109 			goto out;
1110 		memset(&bareq, 0, sizeof(bareq));
1111 		strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1112 		    sizeof(bareq.ifba_ifsname));
1113 		memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1114 		if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1115 			bareq.ifba_expire = brt->brt_expire - time_uptime;
1116 		} else
1117 			bareq.ifba_expire = 0;
1118 		bareq.ifba_flags = brt->brt_flags;
1119 
1120 		error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1121 		if (error)
1122 			goto out;
1123 		count++;
1124 		len -= sizeof(bareq);
1125 	}
1126 out:
1127 	BRIDGE_RT_UNLOCK(sc);
1128 
1129 	bac->ifbac_len = sizeof(bareq) * count;
1130 	return error;
1131 }
1132 
1133 static int
1134 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1135 {
1136 	struct ifbareq *req = arg;
1137 	struct bridge_iflist *bif;
1138 	int error;
1139 	struct psref psref;
1140 
1141 	bif = bridge_lookup_member(sc, req->ifba_ifsname, &psref);
1142 	if (bif == NULL)
1143 		return ENOENT;
1144 
1145 	error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1146 	    req->ifba_flags);
1147 
1148 	bridge_release_member(sc, bif, &psref);
1149 
1150 	return error;
1151 }
1152 
1153 static int
1154 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1155 {
1156 	struct ifbrparam *param = arg;
1157 
1158 	sc->sc_brttimeout = param->ifbrp_ctime;
1159 
1160 	return 0;
1161 }
1162 
1163 static int
1164 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1165 {
1166 	struct ifbrparam *param = arg;
1167 
1168 	param->ifbrp_ctime = sc->sc_brttimeout;
1169 
1170 	return 0;
1171 }
1172 
1173 static int
1174 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1175 {
1176 	struct ifbareq *req = arg;
1177 
1178 	return (bridge_rtdaddr(sc, req->ifba_dst));
1179 }
1180 
1181 static int
1182 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1183 {
1184 	struct ifbreq *req = arg;
1185 
1186 	bridge_rtflush(sc, req->ifbr_ifsflags);
1187 
1188 	return 0;
1189 }
1190 
1191 static int
1192 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1193 {
1194 	struct ifbrparam *param = arg;
1195 
1196 	param->ifbrp_prio = sc->sc_bridge_priority;
1197 
1198 	return 0;
1199 }
1200 
1201 static int
1202 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1203 {
1204 	struct ifbrparam *param = arg;
1205 
1206 	sc->sc_bridge_priority = param->ifbrp_prio;
1207 
1208 	if (sc->sc_if.if_flags & IFF_RUNNING)
1209 		bstp_initialization(sc);
1210 
1211 	return 0;
1212 }
1213 
1214 static int
1215 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1216 {
1217 	struct ifbrparam *param = arg;
1218 
1219 	param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1220 
1221 	return 0;
1222 }
1223 
1224 static int
1225 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1226 {
1227 	struct ifbrparam *param = arg;
1228 
1229 	if (param->ifbrp_hellotime == 0)
1230 		return EINVAL;
1231 	sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1232 
1233 	if (sc->sc_if.if_flags & IFF_RUNNING)
1234 		bstp_initialization(sc);
1235 
1236 	return 0;
1237 }
1238 
1239 static int
1240 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1241 {
1242 	struct ifbrparam *param = arg;
1243 
1244 	param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1245 
1246 	return 0;
1247 }
1248 
1249 static int
1250 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1251 {
1252 	struct ifbrparam *param = arg;
1253 
1254 	if (param->ifbrp_fwddelay == 0)
1255 		return EINVAL;
1256 	sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1257 
1258 	if (sc->sc_if.if_flags & IFF_RUNNING)
1259 		bstp_initialization(sc);
1260 
1261 	return 0;
1262 }
1263 
1264 static int
1265 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1266 {
1267 	struct ifbrparam *param = arg;
1268 
1269 	param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1270 
1271 	return 0;
1272 }
1273 
1274 static int
1275 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1276 {
1277 	struct ifbrparam *param = arg;
1278 
1279 	if (param->ifbrp_maxage == 0)
1280 		return EINVAL;
1281 	sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1282 
1283 	if (sc->sc_if.if_flags & IFF_RUNNING)
1284 		bstp_initialization(sc);
1285 
1286 	return 0;
1287 }
1288 
1289 static int
1290 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1291 {
1292 	struct ifbreq *req = arg;
1293 	struct bridge_iflist *bif;
1294 	struct psref psref;
1295 
1296 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
1297 	if (bif == NULL)
1298 		return ENOENT;
1299 
1300 	bif->bif_priority = req->ifbr_priority;
1301 
1302 	if (sc->sc_if.if_flags & IFF_RUNNING)
1303 		bstp_initialization(sc);
1304 
1305 	bridge_release_member(sc, bif, &psref);
1306 
1307 	return 0;
1308 }
1309 
1310 static int
1311 bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg)
1312 {
1313 	struct ifbrparam *param = arg;
1314 
1315 	param->ifbrp_filter = sc->sc_filter_flags;
1316 
1317 	return 0;
1318 }
1319 
1320 static int
1321 bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg)
1322 {
1323 	struct ifbrparam *param = arg;
1324 	uint32_t nflags, oflags;
1325 
1326 	if (param->ifbrp_filter & ~IFBF_FILT_MASK)
1327 		return EINVAL;
1328 
1329 	nflags = param->ifbrp_filter;
1330 	oflags = sc->sc_filter_flags;
1331 
1332 	if ((nflags & IFBF_FILT_USEIPF) && !(oflags & IFBF_FILT_USEIPF)) {
1333 		pfil_add_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1334 			sc->sc_if.if_pfil);
1335 	}
1336 	if (!(nflags & IFBF_FILT_USEIPF) && (oflags & IFBF_FILT_USEIPF)) {
1337 		pfil_remove_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1338 			sc->sc_if.if_pfil);
1339 	}
1340 
1341 	sc->sc_filter_flags = nflags;
1342 
1343 	return 0;
1344 }
1345 
1346 static int
1347 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1348 {
1349 	struct ifbreq *req = arg;
1350 	struct bridge_iflist *bif;
1351 	struct psref psref;
1352 
1353 	bif = bridge_lookup_member(sc, req->ifbr_ifsname, &psref);
1354 	if (bif == NULL)
1355 		return ENOENT;
1356 
1357 	bif->bif_path_cost = req->ifbr_path_cost;
1358 
1359 	if (sc->sc_if.if_flags & IFF_RUNNING)
1360 		bstp_initialization(sc);
1361 
1362 	bridge_release_member(sc, bif, &psref);
1363 
1364 	return 0;
1365 }
1366 
1367 /*
1368  * bridge_ifdetach:
1369  *
1370  *	Detach an interface from a bridge.  Called when a member
1371  *	interface is detaching.
1372  */
1373 void
1374 bridge_ifdetach(struct ifnet *ifp)
1375 {
1376 	struct bridge_softc *sc = ifp->if_bridge;
1377 	struct ifbreq breq;
1378 
1379 	/* ioctl_lock should prevent this from happening */
1380 	KASSERT(sc != NULL);
1381 
1382 	memset(&breq, 0, sizeof(breq));
1383 	strlcpy(breq.ifbr_ifsname, ifp->if_xname, sizeof(breq.ifbr_ifsname));
1384 
1385 	(void) bridge_ioctl_del(sc, &breq);
1386 }
1387 
1388 /*
1389  * bridge_init:
1390  *
1391  *	Initialize a bridge interface.
1392  */
1393 static int
1394 bridge_init(struct ifnet *ifp)
1395 {
1396 	struct bridge_softc *sc = ifp->if_softc;
1397 
1398 	KASSERT((ifp->if_flags & IFF_RUNNING) == 0);
1399 
1400 	callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1401 	    bridge_timer, sc);
1402 	bstp_initialization(sc);
1403 
1404 	ifp->if_flags |= IFF_RUNNING;
1405 	return 0;
1406 }
1407 
1408 /*
1409  * bridge_stop:
1410  *
1411  *	Stop the bridge interface.
1412  */
1413 static void
1414 bridge_stop(struct ifnet *ifp, int disable)
1415 {
1416 	struct bridge_softc *sc = ifp->if_softc;
1417 
1418 	KASSERT((ifp->if_flags & IFF_RUNNING) != 0);
1419 	ifp->if_flags &= ~IFF_RUNNING;
1420 
1421 	callout_halt(&sc->sc_brcallout, NULL);
1422 	workqueue_wait(sc->sc_rtage_wq, &sc->sc_rtage_wk);
1423 	bstp_stop(sc);
1424 	bridge_rtflush(sc, IFBF_FLUSHDYN);
1425 }
1426 
1427 /*
1428  * bridge_enqueue:
1429  *
1430  *	Enqueue a packet on a bridge member interface.
1431  */
1432 void
1433 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m,
1434     int runfilt)
1435 {
1436 	int len, error;
1437 	short mflags;
1438 
1439 	if (runfilt) {
1440 		if (pfil_run_hooks(sc->sc_if.if_pfil, &m,
1441 		    dst_ifp, PFIL_OUT) != 0) {
1442 			if (m != NULL)
1443 				m_freem(m);
1444 			return;
1445 		}
1446 		if (m == NULL)
1447 			return;
1448 	}
1449 
1450 #ifdef ALTQ
1451 	KERNEL_LOCK(1, NULL);
1452 	/*
1453 	 * If ALTQ is enabled on the member interface, do
1454 	 * classification; the queueing discipline might
1455 	 * not require classification, but might require
1456 	 * the address family/header pointer in the pktattr.
1457 	 */
1458 	if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
1459 		/* XXX IFT_ETHER */
1460 		altq_etherclassify(&dst_ifp->if_snd, m);
1461 	}
1462 	KERNEL_UNLOCK_ONE(NULL);
1463 #endif /* ALTQ */
1464 
1465 	len = m->m_pkthdr.len;
1466 	mflags = m->m_flags;
1467 
1468 	error = if_transmit_lock(dst_ifp, m);
1469 	if (error) {
1470 		/* mbuf is already freed */
1471 		if_statinc(&sc->sc_if, if_oerrors);
1472 		return;
1473 	}
1474 
1475 	net_stat_ref_t nsr = IF_STAT_GETREF(&sc->sc_if);
1476 	if_statinc_ref(nsr, if_opackets);
1477 	if_statadd_ref(nsr, if_obytes, len);
1478 	if (mflags & M_MCAST)
1479 		if_statinc_ref(nsr, if_omcasts);
1480 	IF_STAT_PUTREF(&sc->sc_if);
1481 }
1482 
1483 /*
1484  * bridge_output:
1485  *
1486  *	Send output from a bridge member interface.  This
1487  *	performs the bridging function for locally originated
1488  *	packets.
1489  *
1490  *	The mbuf has the Ethernet header already attached.  We must
1491  *	enqueue or free the mbuf before returning.
1492  */
1493 int
1494 bridge_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
1495     const struct rtentry *rt)
1496 {
1497 	struct ether_header *eh;
1498 	struct ifnet *dst_if;
1499 	struct bridge_softc *sc;
1500 	struct mbuf *n;
1501 	int s;
1502 
1503 	/*
1504 	 * bridge_output() is called from ether_output(), furthermore
1505 	 * ifp argument doesn't point to bridge(4). So, don't assert
1506 	 * IFEF_MPSAFE here.
1507 	 */
1508 
1509 	KASSERT(m->m_len >= ETHER_HDR_LEN);
1510 
1511 	eh = mtod(m, struct ether_header *);
1512 	sc = ifp->if_bridge;
1513 
1514 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1515 		if (memcmp(etherbroadcastaddr,
1516 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
1517 			m->m_flags |= M_BCAST;
1518 		else
1519 			m->m_flags |= M_MCAST;
1520 	}
1521 
1522 	/*
1523 	 * If bridge is down, but the original output interface is up,
1524 	 * go ahead and send out that interface.  Otherwise, the packet
1525 	 * is dropped below.
1526 	 */
1527 	if (__predict_false(sc == NULL) ||
1528 	    (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1529 		dst_if = ifp;
1530 		goto unicast_asis;
1531 	}
1532 
1533 	/*
1534 	 * If the packet is a multicast, or we don't know a better way to
1535 	 * get there, send to all interfaces.
1536 	 */
1537 	if ((m->m_flags & (M_MCAST | M_BCAST)) != 0)
1538 		dst_if = NULL;
1539 	else
1540 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1541 
1542 	/*
1543 	 * In general, we need to handle TX offload in software before
1544 	 * enqueueing a packet. However, we can send it as is in the
1545 	 * cases of unicast via (1) the source interface, or (2) an
1546 	 * interface which supports the specified offload options.
1547 	 * For multicast or broadcast, send it as is only if (3) all
1548 	 * the member interfaces support the specified options.
1549 	 */
1550 
1551 	/*
1552 	 * Unicast via the source interface.
1553 	 */
1554 	if (dst_if == ifp)
1555 		goto unicast_asis;
1556 
1557 	/*
1558 	 * Unicast via other interface.
1559 	 */
1560 	if (dst_if != NULL) {
1561 		KASSERT(m->m_flags & M_PKTHDR);
1562 		if (TX_OFFLOAD_SUPPORTED(dst_if->if_csum_flags_tx,
1563 		    m->m_pkthdr.csum_flags)) {
1564 			/*
1565 			 * Unicast via an interface which supports the
1566 			 * specified offload options.
1567 			 */
1568 			goto unicast_asis;
1569 		}
1570 
1571 		/*
1572 		 * Handle TX offload in software. For TSO, a packet is
1573 		 * split into multiple chunks. Thus, the return value of
1574 		 * ether_sw_offload_tx() is mbuf queue consists of them.
1575 		 */
1576 		m = ether_sw_offload_tx(ifp, m);
1577 		if (m == NULL)
1578 			return 0;
1579 
1580 		do {
1581 			n = m->m_nextpkt;
1582 			if ((dst_if->if_flags & IFF_RUNNING) == 0)
1583 				m_freem(m);
1584 			else
1585 				bridge_enqueue(sc, dst_if, m, 0);
1586 			m = n;
1587 		} while (m != NULL);
1588 
1589 		return 0;
1590 	}
1591 
1592 	/*
1593 	 * Multicast or broadcast.
1594 	 */
1595 	if (TX_OFFLOAD_SUPPORTED(sc->sc_csum_flags_tx,
1596 	    m->m_pkthdr.csum_flags)) {
1597 		/*
1598 		 * Specified TX offload options are supported by all
1599 		 * the member interfaces of this bridge.
1600 		 */
1601 		m->m_nextpkt = NULL;	/* XXX */
1602 	} else {
1603 		/*
1604 		 * Otherwise, handle TX offload in software.
1605 		 */
1606 		m = ether_sw_offload_tx(ifp, m);
1607 		if (m == NULL)
1608 			return 0;
1609 	}
1610 
1611 	do {
1612 		/* XXX Should call bridge_broadcast, but there are locking
1613 		 * issues which need resolving first. */
1614 		struct bridge_iflist *bif;
1615 		struct mbuf *mc;
1616 		bool used = false;
1617 
1618 		n = m->m_nextpkt;
1619 
1620 		BRIDGE_PSZ_RENTER(s);
1621 		BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
1622 			struct psref psref;
1623 
1624 			bridge_acquire_member(sc, bif, &psref);
1625 			BRIDGE_PSZ_REXIT(s);
1626 
1627 			dst_if = bif->bif_ifp;
1628 			if ((dst_if->if_flags & IFF_RUNNING) == 0)
1629 				goto next;
1630 
1631 			/*
1632 			 * If this is not the original output interface,
1633 			 * and the interface is participating in spanning
1634 			 * tree, make sure the port is in a state that
1635 			 * allows forwarding.
1636 			 */
1637 			if (dst_if != ifp &&
1638 			    (bif->bif_flags & IFBIF_STP) != 0) {
1639 				switch (bif->bif_state) {
1640 				case BSTP_IFSTATE_BLOCKING:
1641 				case BSTP_IFSTATE_LISTENING:
1642 				case BSTP_IFSTATE_DISABLED:
1643 					goto next;
1644 				}
1645 			}
1646 
1647 			if (PSLIST_READER_NEXT(bif, struct bridge_iflist,
1648 			    bif_next) == NULL &&
1649 			    ((m->m_flags & (M_MCAST | M_BCAST)) == 0 ||
1650 			    dst_if == ifp))
1651 			{
1652 				used = true;
1653 				mc = m;
1654 			} else {
1655 				mc = m_copypacket(m, M_DONTWAIT);
1656 				if (mc == NULL) {
1657 					if_statinc(&sc->sc_if, if_oerrors);
1658 					goto next;
1659 				}
1660 			}
1661 
1662 			bridge_enqueue(sc, dst_if, mc, 0);
1663 
1664 			if ((m->m_flags & (M_MCAST | M_BCAST)) != 0 &&
1665 			    dst_if != ifp)
1666 			{
1667 				if (PSLIST_READER_NEXT(bif,
1668 				    struct bridge_iflist, bif_next) == NULL)
1669 				{
1670 					used = true;
1671 					mc = m;
1672 				} else {
1673 					mc = m_copypacket(m, M_DONTWAIT);
1674 					if (mc == NULL) {
1675 						if_statinc(&sc->sc_if,
1676 						    if_oerrors);
1677 						goto next;
1678 					}
1679 				}
1680 
1681 				m_set_rcvif(mc, dst_if);
1682 				mc->m_flags &= ~M_PROMISC;
1683 
1684 				s = splsoftnet();
1685 				KERNEL_LOCK_UNLESS_IFP_MPSAFE(dst_if);
1686 				ether_input(dst_if, mc);
1687 				KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(dst_if);
1688 				splx(s);
1689 			}
1690 
1691 next:
1692 			BRIDGE_PSZ_RENTER(s);
1693 			bridge_release_member(sc, bif, &psref);
1694 
1695 			/* Guarantee we don't re-enter the loop as we already
1696 			 * decided we're at the end. */
1697 			if (used)
1698 				break;
1699 		}
1700 		BRIDGE_PSZ_REXIT(s);
1701 
1702 		if (!used)
1703 			m_freem(m);
1704 
1705 		m = n;
1706 	} while (m != NULL);
1707 	return 0;
1708 
1709 unicast_asis:
1710 	/*
1711 	 * XXX Spanning tree consideration here?
1712 	 */
1713 	if ((dst_if->if_flags & IFF_RUNNING) == 0)
1714 		m_freem(m);
1715 	else
1716 		bridge_enqueue(sc, dst_if, m, 0);
1717 	return 0;
1718 }
1719 
1720 /*
1721  * bridge_start:
1722  *
1723  *	Start output on a bridge.
1724  *
1725  *	NOTE: This routine should never be called in this implementation.
1726  */
1727 static void
1728 bridge_start(struct ifnet *ifp)
1729 {
1730 
1731 	printf("%s: bridge_start() called\n", ifp->if_xname);
1732 }
1733 
1734 /*
1735  * bridge_forward:
1736  *
1737  *	The forwarding function of the bridge.
1738  */
1739 static void
1740 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1741 {
1742 	struct bridge_iflist *bif;
1743 	struct ifnet *src_if, *dst_if;
1744 	struct ether_header *eh;
1745 	struct psref psref;
1746 	struct psref psref_src;
1747 	DECLARE_LOCK_VARIABLE;
1748 
1749 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
1750 		return;
1751 
1752 	src_if = m_get_rcvif_psref(m, &psref_src);
1753 	if (src_if == NULL) {
1754 		/* Interface is being destroyed? */
1755 		m_freem(m);
1756 		goto out;
1757 	}
1758 
1759 	if_statadd2(&sc->sc_if, if_ipackets, 1, if_ibytes, m->m_pkthdr.len);
1760 
1761 	/*
1762 	 * Look up the bridge_iflist.
1763 	 */
1764 	bif = bridge_lookup_member_if(sc, src_if, &psref);
1765 	if (bif == NULL) {
1766 		/* Interface is not a bridge member (anymore?) */
1767 		m_freem(m);
1768 		goto out;
1769 	}
1770 
1771 	if (bif->bif_flags & IFBIF_STP) {
1772 		switch (bif->bif_state) {
1773 		case BSTP_IFSTATE_BLOCKING:
1774 		case BSTP_IFSTATE_LISTENING:
1775 		case BSTP_IFSTATE_DISABLED:
1776 			m_freem(m);
1777 			bridge_release_member(sc, bif, &psref);
1778 			goto out;
1779 		}
1780 	}
1781 
1782 	eh = mtod(m, struct ether_header *);
1783 
1784 	/*
1785 	 * If the interface is learning, and the source
1786 	 * address is valid and not multicast, record
1787 	 * the address.
1788 	 */
1789 	if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1790 	    ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1791 	    (eh->ether_shost[0] == 0 &&
1792 	     eh->ether_shost[1] == 0 &&
1793 	     eh->ether_shost[2] == 0 &&
1794 	     eh->ether_shost[3] == 0 &&
1795 	     eh->ether_shost[4] == 0 &&
1796 	     eh->ether_shost[5] == 0) == 0) {
1797 		(void) bridge_rtupdate(sc, eh->ether_shost,
1798 		    src_if, 0, IFBAF_DYNAMIC);
1799 	}
1800 
1801 	if ((bif->bif_flags & IFBIF_STP) != 0 &&
1802 	    bif->bif_state == BSTP_IFSTATE_LEARNING) {
1803 		m_freem(m);
1804 		bridge_release_member(sc, bif, &psref);
1805 		goto out;
1806 	}
1807 
1808 	bridge_release_member(sc, bif, &psref);
1809 
1810 	/*
1811 	 * At this point, the port either doesn't participate
1812 	 * in spanning tree or it is in the forwarding state.
1813 	 */
1814 
1815 	/*
1816 	 * If the packet is unicast, destined for someone on
1817 	 * "this" side of the bridge, drop it.
1818 	 */
1819 	if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1820 		dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1821 		if (src_if == dst_if) {
1822 			m_freem(m);
1823 			goto out;
1824 		}
1825 	} else {
1826 		/* ...forward it to all interfaces. */
1827 		if_statinc(&sc->sc_if, if_imcasts);
1828 		dst_if = NULL;
1829 	}
1830 
1831 	if (pfil_run_hooks(sc->sc_if.if_pfil, &m, src_if, PFIL_IN) != 0) {
1832 		if (m != NULL)
1833 			m_freem(m);
1834 		goto out;
1835 	}
1836 	if (m == NULL)
1837 		goto out;
1838 
1839 	if (dst_if == NULL) {
1840 		bridge_broadcast(sc, src_if, m);
1841 		goto out;
1842 	}
1843 
1844 	m_put_rcvif_psref(src_if, &psref_src);
1845 	src_if = NULL;
1846 
1847 	/*
1848 	 * At this point, we're dealing with a unicast frame
1849 	 * going to a different interface.
1850 	 */
1851 	if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1852 		m_freem(m);
1853 		goto out;
1854 	}
1855 
1856 	bif = bridge_lookup_member_if(sc, dst_if, &psref);
1857 	if (bif == NULL) {
1858 		/* Not a member of the bridge (anymore?) */
1859 		m_freem(m);
1860 		goto out;
1861 	}
1862 
1863 	if (bif->bif_flags & IFBIF_STP) {
1864 		switch (bif->bif_state) {
1865 		case BSTP_IFSTATE_DISABLED:
1866 		case BSTP_IFSTATE_BLOCKING:
1867 			m_freem(m);
1868 			bridge_release_member(sc, bif, &psref);
1869 			goto out;
1870 		}
1871 	}
1872 
1873 	bridge_release_member(sc, bif, &psref);
1874 
1875 	/*
1876 	 * Before enqueueing this packet to the destination interface,
1877 	 * clear any in-bound checksum flags to prevent them from being
1878 	 * misused as out-bound flags.
1879 	 */
1880 	m->m_pkthdr.csum_flags = 0;
1881 
1882 	ACQUIRE_GLOBAL_LOCKS();
1883 	bridge_enqueue(sc, dst_if, m, 1);
1884 	RELEASE_GLOBAL_LOCKS();
1885 out:
1886 	if (src_if != NULL)
1887 		m_put_rcvif_psref(src_if, &psref_src);
1888 	return;
1889 }
1890 
1891 static bool
1892 bstp_state_before_learning(struct bridge_iflist *bif)
1893 {
1894 	if (bif->bif_flags & IFBIF_STP) {
1895 		switch (bif->bif_state) {
1896 		case BSTP_IFSTATE_BLOCKING:
1897 		case BSTP_IFSTATE_LISTENING:
1898 		case BSTP_IFSTATE_DISABLED:
1899 			return true;
1900 		}
1901 	}
1902 	return false;
1903 }
1904 
1905 static bool
1906 bridge_ourether(struct bridge_iflist *bif, struct ether_header *eh, int src)
1907 {
1908 	uint8_t *ether = src ? eh->ether_shost : eh->ether_dhost;
1909 
1910 	if (memcmp(CLLADDR(bif->bif_ifp->if_sadl), ether, ETHER_ADDR_LEN) == 0
1911 #if NCARP > 0
1912 	    || (bif->bif_ifp->if_carp &&
1913 	        carp_ourether(bif->bif_ifp->if_carp, eh, IFT_ETHER, src) != NULL)
1914 #endif /* NCARP > 0 */
1915 	    )
1916 		return true;
1917 
1918 	return false;
1919 }
1920 
1921 /*
1922  * bridge_input:
1923  *
1924  *	Receive input from a member interface.  Queue the packet for
1925  *	bridging if it is not for us.
1926  */
1927 static void
1928 bridge_input(struct ifnet *ifp, struct mbuf *m)
1929 {
1930 	struct bridge_softc *sc = ifp->if_bridge;
1931 	struct bridge_iflist *bif;
1932 	struct ether_header *eh;
1933 	struct psref psref;
1934 	int bound;
1935 	DECLARE_LOCK_VARIABLE;
1936 
1937 	KASSERT(!cpu_intr_p());
1938 
1939 	if (__predict_false(sc == NULL) ||
1940 	    (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1941 		ACQUIRE_GLOBAL_LOCKS();
1942 		ether_input(ifp, m);
1943 		RELEASE_GLOBAL_LOCKS();
1944 		return;
1945 	}
1946 
1947 	bound = curlwp_bind();
1948 	bif = bridge_lookup_member_if(sc, ifp, &psref);
1949 	if (bif == NULL) {
1950 		curlwp_bindx(bound);
1951 		ACQUIRE_GLOBAL_LOCKS();
1952 		ether_input(ifp, m);
1953 		RELEASE_GLOBAL_LOCKS();
1954 		return;
1955 	}
1956 
1957 	eh = mtod(m, struct ether_header *);
1958 
1959 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1960 		if (memcmp(etherbroadcastaddr,
1961 		    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
1962 			m->m_flags |= M_BCAST;
1963 		else
1964 			m->m_flags |= M_MCAST;
1965 	}
1966 
1967 	/*
1968 	 * A 'fast' path for packets addressed to interfaces that are
1969 	 * part of this bridge.
1970 	 */
1971 	if (!(m->m_flags & (M_BCAST|M_MCAST)) &&
1972 	    !bstp_state_before_learning(bif)) {
1973 		struct bridge_iflist *_bif;
1974 		struct ifnet *_ifp = NULL;
1975 		int s;
1976 		struct psref _psref;
1977 
1978 		BRIDGE_PSZ_RENTER(s);
1979 		BRIDGE_IFLIST_READER_FOREACH(_bif, sc) {
1980 			/* It is destined for us. */
1981 			if (bridge_ourether(_bif, eh, 0)) {
1982 				bridge_acquire_member(sc, _bif, &_psref);
1983 				BRIDGE_PSZ_REXIT(s);
1984 				if (_bif->bif_flags & IFBIF_LEARNING)
1985 					(void) bridge_rtupdate(sc,
1986 					    eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1987 				m_set_rcvif(m, _bif->bif_ifp);
1988 				_ifp = _bif->bif_ifp;
1989 				bridge_release_member(sc, _bif, &_psref);
1990 				goto out;
1991 			}
1992 
1993 			/* We just received a packet that we sent out. */
1994 			if (bridge_ourether(_bif, eh, 1))
1995 				break;
1996 		}
1997 		BRIDGE_PSZ_REXIT(s);
1998 out:
1999 
2000 		if (_bif != NULL) {
2001 			bridge_release_member(sc, bif, &psref);
2002 			curlwp_bindx(bound);
2003 			if (_ifp != NULL) {
2004 				m->m_flags &= ~M_PROMISC;
2005 				ACQUIRE_GLOBAL_LOCKS();
2006 				ether_input(_ifp, m);
2007 				RELEASE_GLOBAL_LOCKS();
2008 			} else
2009 				m_freem(m);
2010 			return;
2011 		}
2012 	}
2013 
2014 	/* Tap off 802.1D packets; they do not get forwarded. */
2015 	if (bif->bif_flags & IFBIF_STP &&
2016 	    memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0) {
2017 		bstp_input(sc, bif, m);
2018 		bridge_release_member(sc, bif, &psref);
2019 		curlwp_bindx(bound);
2020 		return;
2021 	}
2022 
2023 	/*
2024 	 * A normal switch would discard the packet here, but that's not what
2025 	 * we've done historically. This also prevents some obnoxious behaviour.
2026 	 */
2027 	if (bstp_state_before_learning(bif)) {
2028 		bridge_release_member(sc, bif, &psref);
2029 		curlwp_bindx(bound);
2030 		ACQUIRE_GLOBAL_LOCKS();
2031 		ether_input(ifp, m);
2032 		RELEASE_GLOBAL_LOCKS();
2033 		return;
2034 	}
2035 
2036 	bridge_release_member(sc, bif, &psref);
2037 
2038 	bridge_forward(sc, m);
2039 
2040 	curlwp_bindx(bound);
2041 }
2042 
2043 /*
2044  * bridge_broadcast:
2045  *
2046  *	Send a frame to all interfaces that are members of
2047  *	the bridge, except for the one on which the packet
2048  *	arrived.
2049  */
2050 static void
2051 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2052     struct mbuf *m)
2053 {
2054 	struct bridge_iflist *bif;
2055 	struct mbuf *mc;
2056 	struct ifnet *dst_if;
2057 	bool bmcast;
2058 	int s;
2059 	DECLARE_LOCK_VARIABLE;
2060 
2061 	bmcast = m->m_flags & (M_BCAST|M_MCAST);
2062 
2063 	BRIDGE_PSZ_RENTER(s);
2064 	BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
2065 		struct psref psref;
2066 
2067 		bridge_acquire_member(sc, bif, &psref);
2068 		BRIDGE_PSZ_REXIT(s);
2069 
2070 		dst_if = bif->bif_ifp;
2071 
2072 		if (bif->bif_flags & IFBIF_STP) {
2073 			switch (bif->bif_state) {
2074 			case BSTP_IFSTATE_BLOCKING:
2075 			case BSTP_IFSTATE_DISABLED:
2076 				goto next;
2077 			}
2078 		}
2079 
2080 		if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && !bmcast)
2081 			goto next;
2082 
2083 		if ((dst_if->if_flags & IFF_RUNNING) == 0)
2084 			goto next;
2085 
2086 		if (dst_if != src_if) {
2087 			mc = m_copypacket(m, M_DONTWAIT);
2088 			if (mc == NULL) {
2089 				if_statinc(&sc->sc_if, if_oerrors);
2090 				goto next;
2091 			}
2092 			/*
2093 			 * Before enqueueing this packet to the destination
2094 			 * interface, clear any in-bound checksum flags to
2095 			 * prevent them from being misused as out-bound flags.
2096 			 */
2097 			mc->m_pkthdr.csum_flags = 0;
2098 
2099 			ACQUIRE_GLOBAL_LOCKS();
2100 			bridge_enqueue(sc, dst_if, mc, 1);
2101 			RELEASE_GLOBAL_LOCKS();
2102 		}
2103 
2104 		if (bmcast) {
2105 			mc = m_copypacket(m, M_DONTWAIT);
2106 			if (mc == NULL) {
2107 				if_statinc(&sc->sc_if, if_oerrors);
2108 				goto next;
2109 			}
2110 			/*
2111 			 * Before enqueueing this packet to the destination
2112 			 * interface, clear any in-bound checksum flags to
2113 			 * prevent them from being misused as out-bound flags.
2114 			 */
2115 			mc->m_pkthdr.csum_flags = 0;
2116 
2117 			m_set_rcvif(mc, dst_if);
2118 			mc->m_flags &= ~M_PROMISC;
2119 
2120 			ACQUIRE_GLOBAL_LOCKS();
2121 			ether_input(dst_if, mc);
2122 			RELEASE_GLOBAL_LOCKS();
2123 		}
2124 next:
2125 		BRIDGE_PSZ_RENTER(s);
2126 		bridge_release_member(sc, bif, &psref);
2127 	}
2128 	BRIDGE_PSZ_REXIT(s);
2129 
2130 	m_freem(m);
2131 }
2132 
2133 static int
2134 bridge_rtalloc(struct bridge_softc *sc, const uint8_t *dst,
2135     struct bridge_rtnode **brtp)
2136 {
2137 	struct bridge_rtnode *brt;
2138 	int error;
2139 
2140 	if (sc->sc_brtcnt >= sc->sc_brtmax)
2141 		return ENOSPC;
2142 
2143 	/*
2144 	 * Allocate a new bridge forwarding node, and
2145 	 * initialize the expiration time and Ethernet
2146 	 * address.
2147 	 */
2148 	brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
2149 	if (brt == NULL)
2150 		return ENOMEM;
2151 
2152 	memset(brt, 0, sizeof(*brt));
2153 	brt->brt_expire = time_uptime + sc->sc_brttimeout;
2154 	brt->brt_flags = IFBAF_DYNAMIC;
2155 	memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2156 	PSLIST_ENTRY_INIT(brt, brt_list);
2157 	PSLIST_ENTRY_INIT(brt, brt_hash);
2158 
2159 	BRIDGE_RT_LOCK(sc);
2160 	error = bridge_rtnode_insert(sc, brt);
2161 	BRIDGE_RT_UNLOCK(sc);
2162 
2163 	if (error != 0) {
2164 		pool_put(&bridge_rtnode_pool, brt);
2165 		return error;
2166 	}
2167 
2168 	*brtp = brt;
2169 	return 0;
2170 }
2171 
2172 /*
2173  * bridge_rtupdate:
2174  *
2175  *	Add a bridge routing entry.
2176  */
2177 static int
2178 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2179     struct ifnet *dst_if, int setflags, uint8_t flags)
2180 {
2181 	struct bridge_rtnode *brt;
2182 	int s;
2183 
2184 again:
2185 	/*
2186 	 * A route for this destination might already exist.  If so,
2187 	 * update it, otherwise create a new one.
2188 	 */
2189 	BRIDGE_RT_RENTER(s);
2190 	brt = bridge_rtnode_lookup(sc, dst);
2191 
2192 	if (brt != NULL) {
2193 		brt->brt_ifp = dst_if;
2194 		if (setflags) {
2195 			brt->brt_flags = flags;
2196 			if (flags & IFBAF_STATIC)
2197 				brt->brt_expire = 0;
2198 			else
2199 				brt->brt_expire = time_uptime + sc->sc_brttimeout;
2200 		} else {
2201 			if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2202 				brt->brt_expire = time_uptime + sc->sc_brttimeout;
2203 		}
2204 	}
2205 	BRIDGE_RT_REXIT(s);
2206 
2207 	if (brt == NULL) {
2208 		int r;
2209 
2210 		r = bridge_rtalloc(sc, dst, &brt);
2211 		if (r != 0)
2212 			return r;
2213 		goto again;
2214 	}
2215 
2216 	return 0;
2217 }
2218 
2219 /*
2220  * bridge_rtlookup:
2221  *
2222  *	Lookup the destination interface for an address.
2223  */
2224 static struct ifnet *
2225 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2226 {
2227 	struct bridge_rtnode *brt;
2228 	struct ifnet *ifs = NULL;
2229 	int s;
2230 
2231 	BRIDGE_RT_RENTER(s);
2232 	brt = bridge_rtnode_lookup(sc, addr);
2233 	if (brt != NULL)
2234 		ifs = brt->brt_ifp;
2235 	BRIDGE_RT_REXIT(s);
2236 
2237 	return ifs;
2238 }
2239 
2240 typedef bool (*bridge_iterate_cb_t)
2241     (struct bridge_softc *, struct bridge_rtnode *, bool *, void *);
2242 
2243 /*
2244  * bridge_rtlist_iterate_remove:
2245  *
2246  *	It iterates on sc->sc_rtlist and removes rtnodes of it which func
2247  *	callback judges to remove. Removals of rtnodes are done in a manner
2248  *	of pserialize. To this end, all kmem_* operations are placed out of
2249  *	mutexes.
2250  */
2251 static void
2252 bridge_rtlist_iterate_remove(struct bridge_softc *sc, bridge_iterate_cb_t func, void *arg)
2253 {
2254 	struct bridge_rtnode *brt;
2255 	struct bridge_rtnode **brt_list;
2256 	int i, count;
2257 
2258 retry:
2259 	count = sc->sc_brtcnt;
2260 	if (count == 0)
2261 		return;
2262 	brt_list = kmem_alloc(sizeof(*brt_list) * count, KM_SLEEP);
2263 
2264 	BRIDGE_RT_LOCK(sc);
2265 	if (__predict_false(sc->sc_brtcnt > count)) {
2266 		/* The rtnodes increased, we need more memory */
2267 		BRIDGE_RT_UNLOCK(sc);
2268 		kmem_free(brt_list, sizeof(*brt_list) * count);
2269 		goto retry;
2270 	}
2271 
2272 	i = 0;
2273 	/*
2274 	 * We don't need to use a _SAFE variant here because we know
2275 	 * that a removed item keeps its next pointer as-is thanks to
2276 	 * pslist(9) and isn't freed in the loop.
2277 	 */
2278 	BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) {
2279 		bool need_break = false;
2280 		if (func(sc, brt, &need_break, arg)) {
2281 			bridge_rtnode_remove(sc, brt);
2282 			brt_list[i++] = brt;
2283 		}
2284 		if (need_break)
2285 			break;
2286 	}
2287 
2288 	if (i > 0)
2289 		BRIDGE_RT_PSZ_PERFORM(sc);
2290 	BRIDGE_RT_UNLOCK(sc);
2291 
2292 	while (--i >= 0)
2293 		bridge_rtnode_destroy(brt_list[i]);
2294 
2295 	kmem_free(brt_list, sizeof(*brt_list) * count);
2296 }
2297 
2298 static bool
2299 bridge_rttrim0_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
2300     bool *need_break, void *arg)
2301 {
2302 	if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2303 		/* Take into account of the subsequent removal */
2304 		if ((sc->sc_brtcnt - 1) <= sc->sc_brtmax)
2305 			*need_break = true;
2306 		return true;
2307 	} else
2308 		return false;
2309 }
2310 
2311 static void
2312 bridge_rttrim0(struct bridge_softc *sc)
2313 {
2314 	bridge_rtlist_iterate_remove(sc, bridge_rttrim0_cb, NULL);
2315 }
2316 
2317 /*
2318  * bridge_rttrim:
2319  *
2320  *	Trim the routine table so that we have a number
2321  *	of routing entries less than or equal to the
2322  *	maximum number.
2323  */
2324 static void
2325 bridge_rttrim(struct bridge_softc *sc)
2326 {
2327 
2328 	/* Make sure we actually need to do this. */
2329 	if (sc->sc_brtcnt <= sc->sc_brtmax)
2330 		return;
2331 
2332 	/* Force an aging cycle; this might trim enough addresses. */
2333 	bridge_rtage(sc);
2334 	if (sc->sc_brtcnt <= sc->sc_brtmax)
2335 		return;
2336 
2337 	bridge_rttrim0(sc);
2338 
2339 	return;
2340 }
2341 
2342 /*
2343  * bridge_timer:
2344  *
2345  *	Aging timer for the bridge.
2346  */
2347 static void
2348 bridge_timer(void *arg)
2349 {
2350 	struct bridge_softc *sc = arg;
2351 
2352 	workqueue_enqueue(sc->sc_rtage_wq, &sc->sc_rtage_wk, NULL);
2353 }
2354 
2355 static void
2356 bridge_rtage_work(struct work *wk, void *arg)
2357 {
2358 	struct bridge_softc *sc = arg;
2359 
2360 	KASSERT(wk == &sc->sc_rtage_wk);
2361 
2362 	bridge_rtage(sc);
2363 
2364 	if (sc->sc_if.if_flags & IFF_RUNNING)
2365 		callout_reset(&sc->sc_brcallout,
2366 		    bridge_rtable_prune_period * hz, bridge_timer, sc);
2367 }
2368 
2369 static bool
2370 bridge_rtage_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
2371     bool *need_break, void *arg)
2372 {
2373 	if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2374 	    time_uptime >= brt->brt_expire)
2375 		return true;
2376 	else
2377 		return false;
2378 }
2379 
2380 /*
2381  * bridge_rtage:
2382  *
2383  *	Perform an aging cycle.
2384  */
2385 static void
2386 bridge_rtage(struct bridge_softc *sc)
2387 {
2388 	bridge_rtlist_iterate_remove(sc, bridge_rtage_cb, NULL);
2389 }
2390 
2391 
2392 static bool
2393 bridge_rtflush_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
2394     bool *need_break, void *arg)
2395 {
2396 	int full = *(int*)arg;
2397 
2398 	if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2399 		return true;
2400 	else
2401 		return false;
2402 }
2403 
2404 /*
2405  * bridge_rtflush:
2406  *
2407  *	Remove all dynamic addresses from the bridge.
2408  */
2409 static void
2410 bridge_rtflush(struct bridge_softc *sc, int full)
2411 {
2412 	bridge_rtlist_iterate_remove(sc, bridge_rtflush_cb, &full);
2413 }
2414 
2415 /*
2416  * bridge_rtdaddr:
2417  *
2418  *	Remove an address from the table.
2419  */
2420 static int
2421 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2422 {
2423 	struct bridge_rtnode *brt;
2424 
2425 	BRIDGE_RT_LOCK(sc);
2426 	if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) {
2427 		BRIDGE_RT_UNLOCK(sc);
2428 		return ENOENT;
2429 	}
2430 	bridge_rtnode_remove(sc, brt);
2431 	BRIDGE_RT_PSZ_PERFORM(sc);
2432 	BRIDGE_RT_UNLOCK(sc);
2433 
2434 	bridge_rtnode_destroy(brt);
2435 
2436 	return 0;
2437 }
2438 
2439 /*
2440  * bridge_rtdelete:
2441  *
2442  *	Delete routes to a speicifc member interface.
2443  */
2444 static void
2445 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
2446 {
2447 	struct bridge_rtnode *brt;
2448 
2449 	/* XXX pserialize_perform for each entry is slow */
2450 again:
2451 	BRIDGE_RT_LOCK(sc);
2452 	BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) {
2453 		if (brt->brt_ifp == ifp)
2454 			break;
2455 	}
2456 	if (brt == NULL) {
2457 		BRIDGE_RT_UNLOCK(sc);
2458 		return;
2459 	}
2460 	bridge_rtnode_remove(sc, brt);
2461 	BRIDGE_RT_PSZ_PERFORM(sc);
2462 	BRIDGE_RT_UNLOCK(sc);
2463 
2464 	bridge_rtnode_destroy(brt);
2465 
2466 	goto again;
2467 }
2468 
2469 /*
2470  * bridge_rtable_init:
2471  *
2472  *	Initialize the route table for this bridge.
2473  */
2474 static void
2475 bridge_rtable_init(struct bridge_softc *sc)
2476 {
2477 	int i;
2478 
2479 	sc->sc_rthash = kmem_alloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2480 	    KM_SLEEP);
2481 
2482 	for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2483 		PSLIST_INIT(&sc->sc_rthash[i]);
2484 
2485 	sc->sc_rthash_key = cprng_fast32();
2486 
2487 	PSLIST_INIT(&sc->sc_rtlist);
2488 
2489 	sc->sc_rtlist_psz = pserialize_create();
2490 	sc->sc_rtlist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
2491 }
2492 
2493 /*
2494  * bridge_rtable_fini:
2495  *
2496  *	Deconstruct the route table for this bridge.
2497  */
2498 static void
2499 bridge_rtable_fini(struct bridge_softc *sc)
2500 {
2501 
2502 	kmem_free(sc->sc_rthash, sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE);
2503 	mutex_obj_free(sc->sc_rtlist_lock);
2504 	pserialize_destroy(sc->sc_rtlist_psz);
2505 }
2506 
2507 /*
2508  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2509  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2510  */
2511 #define	mix(a, b, c)							\
2512 do {									\
2513 	a -= b; a -= c; a ^= (c >> 13);					\
2514 	b -= c; b -= a; b ^= (a << 8);					\
2515 	c -= a; c -= b; c ^= (b >> 13);					\
2516 	a -= b; a -= c; a ^= (c >> 12);					\
2517 	b -= c; b -= a; b ^= (a << 16);					\
2518 	c -= a; c -= b; c ^= (b >> 5);					\
2519 	a -= b; a -= c; a ^= (c >> 3);					\
2520 	b -= c; b -= a; b ^= (a << 10);					\
2521 	c -= a; c -= b; c ^= (b >> 15);					\
2522 } while (/*CONSTCOND*/0)
2523 
2524 static inline uint32_t
2525 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2526 {
2527 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2528 
2529 	b += addr[5] << 8;
2530 	b += addr[4];
2531 	a += (uint32_t)addr[3] << 24;
2532 	a += addr[2] << 16;
2533 	a += addr[1] << 8;
2534 	a += addr[0];
2535 
2536 	mix(a, b, c);
2537 
2538 	return (c & BRIDGE_RTHASH_MASK);
2539 }
2540 
2541 #undef mix
2542 
2543 /*
2544  * bridge_rtnode_lookup:
2545  *
2546  *	Look up a bridge route node for the specified destination.
2547  */
2548 static struct bridge_rtnode *
2549 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2550 {
2551 	struct bridge_rtnode *brt;
2552 	uint32_t hash;
2553 	int dir;
2554 
2555 	hash = bridge_rthash(sc, addr);
2556 	BRIDGE_RTHASH_READER_FOREACH(brt, sc, hash) {
2557 		dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
2558 		if (dir == 0)
2559 			return brt;
2560 		if (dir > 0)
2561 			return NULL;
2562 	}
2563 
2564 	return NULL;
2565 }
2566 
2567 /*
2568  * bridge_rtnode_insert:
2569  *
2570  *	Insert the specified bridge node into the route table.  We
2571  *	assume the entry is not already in the table.
2572  */
2573 static int
2574 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2575 {
2576 	struct bridge_rtnode *lbrt, *prev = NULL;
2577 	uint32_t hash;
2578 
2579 	KASSERT(BRIDGE_RT_LOCKED(sc));
2580 
2581 	hash = bridge_rthash(sc, brt->brt_addr);
2582 	BRIDGE_RTHASH_WRITER_FOREACH(lbrt, sc, hash) {
2583 		int dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
2584 		if (dir == 0)
2585 			return EEXIST;
2586 		if (dir > 0)
2587 			break;
2588 		prev = lbrt;
2589 	}
2590 	if (prev == NULL)
2591 		BRIDGE_RTHASH_WRITER_INSERT_HEAD(sc, hash, brt);
2592 	else
2593 		BRIDGE_RTHASH_WRITER_INSERT_AFTER(prev, brt);
2594 
2595 	BRIDGE_RTLIST_WRITER_INSERT_HEAD(sc, brt);
2596 	sc->sc_brtcnt++;
2597 
2598 	return 0;
2599 }
2600 
2601 /*
2602  * bridge_rtnode_remove:
2603  *
2604  *	Remove a bridge rtnode from the rthash and the rtlist of a bridge.
2605  */
2606 static void
2607 bridge_rtnode_remove(struct bridge_softc *sc, struct bridge_rtnode *brt)
2608 {
2609 
2610 	KASSERT(BRIDGE_RT_LOCKED(sc));
2611 
2612 	BRIDGE_RTHASH_WRITER_REMOVE(brt);
2613 	BRIDGE_RTLIST_WRITER_REMOVE(brt);
2614 	sc->sc_brtcnt--;
2615 }
2616 
2617 /*
2618  * bridge_rtnode_destroy:
2619  *
2620  *	Destroy a bridge rtnode.
2621  */
2622 static void
2623 bridge_rtnode_destroy(struct bridge_rtnode *brt)
2624 {
2625 
2626 	PSLIST_ENTRY_DESTROY(brt, brt_list);
2627 	PSLIST_ENTRY_DESTROY(brt, brt_hash);
2628 	pool_put(&bridge_rtnode_pool, brt);
2629 }
2630 
2631 extern pfil_head_t *inet_pfil_hook;                 /* XXX */
2632 extern pfil_head_t *inet6_pfil_hook;                /* XXX */
2633 
2634 /*
2635  * Send bridge packets through IPF if they are one of the types IPF can deal
2636  * with, or if they are ARP or REVARP.  (IPF will pass ARP and REVARP without
2637  * question.)
2638  */
2639 static int
2640 bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
2641 {
2642 	int snap, error;
2643 	struct ether_header *eh1, eh2;
2644 	struct llc llc1;
2645 	uint16_t ether_type;
2646 
2647 	snap = 0;
2648 	error = -1;	/* Default error if not error == 0 */
2649 	eh1 = mtod(*mp, struct ether_header *);
2650 	ether_type = ntohs(eh1->ether_type);
2651 
2652 	/*
2653 	 * Check for SNAP/LLC.
2654 	 */
2655 	if (ether_type < ETHERMTU) {
2656 		struct llc *llc2 = (struct llc *)(eh1 + 1);
2657 
2658 		if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2659 		    llc2->llc_dsap == LLC_SNAP_LSAP &&
2660 		    llc2->llc_ssap == LLC_SNAP_LSAP &&
2661 		    llc2->llc_control == LLC_UI) {
2662 			ether_type = htons(llc2->llc_un.type_snap.ether_type);
2663 			snap = 1;
2664 		}
2665 	}
2666 
2667 	/*
2668 	 * If we're trying to filter bridge traffic, don't look at anything
2669 	 * other than IP and ARP traffic.  If the filter doesn't understand
2670 	 * IPv6, don't allow IPv6 through the bridge either.  This is lame
2671 	 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2672 	 * but of course we don't have an AppleTalk filter to begin with.
2673 	 * (Note that since IPF doesn't understand ARP it will pass *ALL*
2674 	 * ARP traffic.)
2675 	 */
2676 	switch (ether_type) {
2677 		case ETHERTYPE_ARP:
2678 		case ETHERTYPE_REVARP:
2679 			return 0; /* Automatically pass */
2680 		case ETHERTYPE_IP:
2681 # ifdef INET6
2682 		case ETHERTYPE_IPV6:
2683 # endif /* INET6 */
2684 			break;
2685 		default:
2686 			goto bad;
2687 	}
2688 
2689 	/* Strip off the Ethernet header and keep a copy. */
2690 	m_copydata(*mp, 0, ETHER_HDR_LEN, (void *) &eh2);
2691 	m_adj(*mp, ETHER_HDR_LEN);
2692 
2693 	/* Strip off snap header, if present */
2694 	if (snap) {
2695 		m_copydata(*mp, 0, sizeof(struct llc), (void *) &llc1);
2696 		m_adj(*mp, sizeof(struct llc));
2697 	}
2698 
2699 	/*
2700 	 * Check basic packet sanity and run IPF through pfil.
2701 	 */
2702 	KASSERT(!cpu_intr_p());
2703 	switch (ether_type)
2704 	{
2705 	case ETHERTYPE_IP :
2706 		error = bridge_ip_checkbasic(mp);
2707 		if (error == 0)
2708 			error = pfil_run_hooks(inet_pfil_hook, mp, ifp, dir);
2709 		break;
2710 # ifdef INET6
2711 	case ETHERTYPE_IPV6 :
2712 		error = bridge_ip6_checkbasic(mp);
2713 		if (error == 0)
2714 			error = pfil_run_hooks(inet6_pfil_hook, mp, ifp, dir);
2715 		break;
2716 # endif
2717 	default :
2718 		error = 0;
2719 		break;
2720 	}
2721 
2722 	if (*mp == NULL)
2723 		return error;
2724 	if (error != 0)
2725 		goto bad;
2726 
2727 	error = -1;
2728 
2729 	/*
2730 	 * Finally, put everything back the way it was and return
2731 	 */
2732 	if (snap) {
2733 		M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
2734 		if (*mp == NULL)
2735 			return error;
2736 		bcopy(&llc1, mtod(*mp, void *), sizeof(struct llc));
2737 	}
2738 
2739 	M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
2740 	if (*mp == NULL)
2741 		return error;
2742 	bcopy(&eh2, mtod(*mp, void *), ETHER_HDR_LEN);
2743 
2744 	return 0;
2745 
2746     bad:
2747 	m_freem(*mp);
2748 	*mp = NULL;
2749 	return error;
2750 }
2751 
2752 /*
2753  * Perform basic checks on header size since
2754  * IPF assumes ip_input has already processed
2755  * it for it.  Cut-and-pasted from ip_input.c.
2756  * Given how simple the IPv6 version is,
2757  * does the IPv4 version really need to be
2758  * this complicated?
2759  *
2760  * XXX Should we update ipstat here, or not?
2761  * XXX Right now we update ipstat but not
2762  * XXX csum_counter.
2763  */
2764 static int
2765 bridge_ip_checkbasic(struct mbuf **mp)
2766 {
2767 	struct mbuf *m = *mp;
2768 	struct ip *ip;
2769 	int len, hlen;
2770 
2771 	if (*mp == NULL)
2772 		return -1;
2773 
2774 	if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
2775 		if ((m = m_copyup(m, sizeof(struct ip),
2776 			(max_linkhdr + 3) & ~3)) == NULL) {
2777 			/* XXXJRT new stat, please */
2778 			ip_statinc(IP_STAT_TOOSMALL);
2779 			goto bad;
2780 		}
2781 	} else if (__predict_false(m->m_len < sizeof (struct ip))) {
2782 		if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2783 			ip_statinc(IP_STAT_TOOSMALL);
2784 			goto bad;
2785 		}
2786 	}
2787 	ip = mtod(m, struct ip *);
2788 	if (ip == NULL) goto bad;
2789 
2790 	if (ip->ip_v != IPVERSION) {
2791 		ip_statinc(IP_STAT_BADVERS);
2792 		goto bad;
2793 	}
2794 	hlen = ip->ip_hl << 2;
2795 	if (hlen < sizeof(struct ip)) { /* minimum header length */
2796 		ip_statinc(IP_STAT_BADHLEN);
2797 		goto bad;
2798 	}
2799 	if (hlen > m->m_len) {
2800 		if ((m = m_pullup(m, hlen)) == 0) {
2801 			ip_statinc(IP_STAT_BADHLEN);
2802 			goto bad;
2803 		}
2804 		ip = mtod(m, struct ip *);
2805 		if (ip == NULL) goto bad;
2806 	}
2807 
2808 	switch (m->m_pkthdr.csum_flags &
2809 	        ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_IPv4) |
2810 	         M_CSUM_IPv4_BAD)) {
2811 	case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
2812 		/* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */
2813 		goto bad;
2814 
2815 	case M_CSUM_IPv4:
2816 		/* Checksum was okay. */
2817 		/* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */
2818 		break;
2819 
2820 	default:
2821 		/* Must compute it ourselves. */
2822 		/* INET_CSUM_COUNTER_INCR(&ip_swcsum); */
2823 		if (in_cksum(m, hlen) != 0)
2824 			goto bad;
2825 		break;
2826 	}
2827 
2828 	/* Retrieve the packet length. */
2829 	len = ntohs(ip->ip_len);
2830 
2831 	/*
2832 	 * Check for additional length bogosity
2833 	 */
2834 	if (len < hlen) {
2835 		ip_statinc(IP_STAT_BADLEN);
2836 		goto bad;
2837 	}
2838 
2839 	/*
2840 	 * Check that the amount of data in the buffers
2841 	 * is as at least much as the IP header would have us expect.
2842 	 * Drop packet if shorter than we expect.
2843 	 */
2844 	if (m->m_pkthdr.len < len) {
2845 		ip_statinc(IP_STAT_TOOSHORT);
2846 		goto bad;
2847 	}
2848 
2849 	/* Checks out, proceed */
2850 	*mp = m;
2851 	return 0;
2852 
2853     bad:
2854 	*mp = m;
2855 	return -1;
2856 }
2857 
2858 # ifdef INET6
2859 /*
2860  * Same as above, but for IPv6.
2861  * Cut-and-pasted from ip6_input.c.
2862  * XXX Should we update ip6stat, or not?
2863  */
2864 static int
2865 bridge_ip6_checkbasic(struct mbuf **mp)
2866 {
2867 	struct mbuf *m = *mp;
2868 	struct ip6_hdr *ip6;
2869 
2870 	/*
2871 	 * If the IPv6 header is not aligned, slurp it up into a new
2872 	 * mbuf with space for link headers, in the event we forward
2873 	 * it.  Otherwise, if it is aligned, make sure the entire base
2874 	 * IPv6 header is in the first mbuf of the chain.
2875 	 */
2876 	if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
2877 		struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
2878 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2879 		                  (max_linkhdr + 3) & ~3)) == NULL) {
2880 			/* XXXJRT new stat, please */
2881 			ip6_statinc(IP6_STAT_TOOSMALL);
2882 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2883 			goto bad;
2884 		}
2885 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2886 		struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
2887 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2888 			ip6_statinc(IP6_STAT_TOOSMALL);
2889 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2890 			goto bad;
2891 		}
2892 	}
2893 
2894 	ip6 = mtod(m, struct ip6_hdr *);
2895 
2896 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2897 		ip6_statinc(IP6_STAT_BADVERS);
2898 		in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m), ifs6_in_hdrerr);
2899 		goto bad;
2900 	}
2901 
2902 	/* Checks out, proceed */
2903 	*mp = m;
2904 	return 0;
2905 
2906     bad:
2907 	*mp = m;
2908 	return -1;
2909 }
2910 # endif /* INET6 */
2911