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