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