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