if_bridge.c revision 1.192 1 /* $NetBSD: if_bridge.c,v 1.192 2024/07/16 03:32:43 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.192 2024/07/16 03:32:43 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
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 m_freem(m);
1483 return;
1484 }
1485 if (m == NULL)
1486 return;
1487 }
1488
1489 #ifdef ALTQ
1490 KERNEL_LOCK(1, NULL);
1491 /*
1492 * If ALTQ is enabled on the member interface, do
1493 * classification; the queueing discipline might
1494 * not require classification, but might require
1495 * the address family/header pointer in the pktattr.
1496 */
1497 if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
1498 /* XXX IFT_ETHER */
1499 altq_etherclassify(&dst_ifp->if_snd, m);
1500 }
1501 KERNEL_UNLOCK_ONE(NULL);
1502 #endif /* ALTQ */
1503
1504 if (vlan_has_tag(m) &&
1505 !vlan_is_hwtag_enabled(dst_ifp)) {
1506 (void)ether_inject_vlantag(&m, ETHERTYPE_VLAN,
1507 vlan_get_tag(m));
1508 if (m == NULL) {
1509 if_statinc(&sc->sc_if, if_oerrors);
1510 return;
1511 }
1512 }
1513
1514 len = m->m_pkthdr.len;
1515 mflags = m->m_flags;
1516
1517 error = if_transmit_lock(dst_ifp, m);
1518 if (error) {
1519 /* mbuf is already freed */
1520 if_statinc(&sc->sc_if, if_oerrors);
1521 return;
1522 }
1523
1524 net_stat_ref_t nsr = IF_STAT_GETREF(&sc->sc_if);
1525 if_statinc_ref(&sc->sc_if, nsr, if_opackets);
1526 if_statadd_ref(&sc->sc_if, nsr, if_obytes, len);
1527 if (mflags & M_MCAST)
1528 if_statinc_ref(&sc->sc_if, nsr, if_omcasts);
1529 IF_STAT_PUTREF(&sc->sc_if);
1530 }
1531
1532 /*
1533 * bridge_output:
1534 *
1535 * Send output from a bridge member interface. This
1536 * performs the bridging function for locally originated
1537 * packets.
1538 *
1539 * The mbuf has the Ethernet header already attached. We must
1540 * enqueue or free the mbuf before returning.
1541 */
1542 int
1543 bridge_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
1544 const struct rtentry *rt)
1545 {
1546 struct ether_header *eh;
1547 struct ifnet *dst_if;
1548 struct bridge_softc *sc;
1549 struct mbuf *n;
1550 int s, bound;
1551
1552 /*
1553 * bridge_output() is called from ether_output(), furthermore
1554 * ifp argument doesn't point to bridge(4). So, don't assert
1555 * IFEF_MPSAFE here.
1556 */
1557
1558 KASSERT(m->m_len >= ETHER_HDR_LEN);
1559
1560 eh = mtod(m, struct ether_header *);
1561 sc = ifp->if_bridge;
1562
1563 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1564 if (memcmp(etherbroadcastaddr,
1565 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
1566 m->m_flags |= M_BCAST;
1567 else
1568 m->m_flags |= M_MCAST;
1569 }
1570
1571 /*
1572 * If bridge is down, but the original output interface is up,
1573 * go ahead and send out that interface. Otherwise, the packet
1574 * is dropped below.
1575 */
1576 if (__predict_false(sc == NULL) ||
1577 (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1578 dst_if = ifp;
1579 goto unicast_asis;
1580 }
1581
1582 /*
1583 * If the packet is a multicast, or we don't know a better way to
1584 * get there, send to all interfaces.
1585 */
1586 if ((m->m_flags & (M_MCAST | M_BCAST)) != 0)
1587 dst_if = NULL;
1588 else
1589 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1590
1591 /*
1592 * In general, we need to handle TX offload in software before
1593 * enqueueing a packet. However, we can send it as is in the
1594 * cases of unicast via (1) the source interface, or (2) an
1595 * interface which supports the specified offload options.
1596 * For multicast or broadcast, send it as is only if (3) all
1597 * the member interfaces support the specified options.
1598 */
1599
1600 /*
1601 * Unicast via the source interface.
1602 */
1603 if (dst_if == ifp)
1604 goto unicast_asis;
1605
1606 /*
1607 * Unicast via other interface.
1608 */
1609 if (dst_if != NULL) {
1610 KASSERT(m->m_flags & M_PKTHDR);
1611 if (TX_OFFLOAD_SUPPORTED(dst_if->if_csum_flags_tx,
1612 m->m_pkthdr.csum_flags)) {
1613 /*
1614 * Unicast via an interface which supports the
1615 * specified offload options.
1616 */
1617 goto unicast_asis;
1618 }
1619
1620 /*
1621 * Handle TX offload in software. For TSO, a packet is
1622 * split into multiple chunks. Thus, the return value of
1623 * ether_sw_offload_tx() is mbuf queue consists of them.
1624 */
1625 m = ether_sw_offload_tx(ifp, m);
1626 if (m == NULL)
1627 return 0;
1628
1629 do {
1630 n = m->m_nextpkt;
1631 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1632 m_freem(m);
1633 else
1634 bridge_enqueue(sc, dst_if, m, 0);
1635 m = n;
1636 } while (m != NULL);
1637
1638 return 0;
1639 }
1640
1641 /*
1642 * Multicast or broadcast.
1643 */
1644 if (TX_OFFLOAD_SUPPORTED(sc->sc_csum_flags_tx,
1645 m->m_pkthdr.csum_flags)) {
1646 /*
1647 * Specified TX offload options are supported by all
1648 * the member interfaces of this bridge.
1649 */
1650 m->m_nextpkt = NULL; /* XXX */
1651 } else {
1652 /*
1653 * Otherwise, handle TX offload in software.
1654 */
1655 m = ether_sw_offload_tx(ifp, m);
1656 if (m == NULL)
1657 return 0;
1658 }
1659
1660 /*
1661 * When we use pppoe over bridge, bridge_output() can be called
1662 * in a lwp context by pppoe_timeout_wk().
1663 */
1664 bound = curlwp_bind();
1665 do {
1666 /* XXX Should call bridge_broadcast, but there are locking
1667 * issues which need resolving first. */
1668 struct bridge_iflist *bif;
1669 struct mbuf *mc;
1670 bool used = false;
1671
1672 n = m->m_nextpkt;
1673
1674 BRIDGE_PSZ_RENTER(s);
1675 BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
1676 struct psref psref;
1677
1678 bridge_acquire_member(sc, bif, &psref);
1679 BRIDGE_PSZ_REXIT(s);
1680
1681 dst_if = bif->bif_ifp;
1682 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1683 goto next;
1684
1685 /*
1686 * If this is not the original output interface,
1687 * and the interface is participating in spanning
1688 * tree, make sure the port is in a state that
1689 * allows forwarding.
1690 */
1691 if (dst_if != ifp &&
1692 (bif->bif_flags & IFBIF_STP) != 0) {
1693 switch (bif->bif_state) {
1694 case BSTP_IFSTATE_BLOCKING:
1695 case BSTP_IFSTATE_LISTENING:
1696 case BSTP_IFSTATE_DISABLED:
1697 goto next;
1698 }
1699 }
1700
1701 if (PSLIST_READER_NEXT(bif, struct bridge_iflist,
1702 bif_next) == NULL &&
1703 ((m->m_flags & (M_MCAST | M_BCAST)) == 0 ||
1704 dst_if == ifp))
1705 {
1706 used = true;
1707 mc = m;
1708 } else {
1709 mc = m_copypacket(m, M_DONTWAIT);
1710 if (mc == NULL) {
1711 if_statinc(&sc->sc_if, if_oerrors);
1712 goto next;
1713 }
1714 }
1715
1716 bridge_enqueue(sc, dst_if, mc, 0);
1717
1718 if ((m->m_flags & (M_MCAST | M_BCAST)) != 0 &&
1719 dst_if != ifp)
1720 {
1721 if (PSLIST_READER_NEXT(bif,
1722 struct bridge_iflist, bif_next) == NULL)
1723 {
1724 used = true;
1725 mc = m;
1726 } else {
1727 mc = m_copypacket(m, M_DONTWAIT);
1728 if (mc == NULL) {
1729 if_statinc(&sc->sc_if,
1730 if_oerrors);
1731 goto next;
1732 }
1733 }
1734
1735 m_set_rcvif(mc, dst_if);
1736 mc->m_flags &= ~M_PROMISC;
1737
1738 const int _s = splsoftnet();
1739 KERNEL_LOCK_UNLESS_IFP_MPSAFE(dst_if);
1740 ether_input(dst_if, mc);
1741 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(dst_if);
1742 splx(_s);
1743 }
1744
1745 next:
1746 BRIDGE_PSZ_RENTER(s);
1747 bridge_release_member(sc, bif, &psref);
1748
1749 /* Guarantee we don't re-enter the loop as we already
1750 * decided we're at the end. */
1751 if (used)
1752 break;
1753 }
1754 BRIDGE_PSZ_REXIT(s);
1755
1756 if (!used)
1757 m_freem(m);
1758
1759 m = n;
1760 } while (m != NULL);
1761 curlwp_bindx(bound);
1762
1763 return 0;
1764
1765 unicast_asis:
1766 /*
1767 * XXX Spanning tree consideration here?
1768 */
1769 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1770 m_freem(m);
1771 else
1772 bridge_enqueue(sc, dst_if, m, 0);
1773 return 0;
1774 }
1775
1776 /*
1777 * bridge_start:
1778 *
1779 * Start output on a bridge.
1780 *
1781 * NOTE: This routine should never be called in this implementation.
1782 */
1783 static void
1784 bridge_start(struct ifnet *ifp)
1785 {
1786
1787 printf("%s: bridge_start() called\n", ifp->if_xname);
1788 }
1789
1790 /*
1791 * bridge_forward:
1792 *
1793 * The forwarding function of the bridge.
1794 */
1795 static void
1796 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1797 {
1798 struct bridge_iflist *bif;
1799 struct ifnet *src_if, *dst_if;
1800 struct ether_header *eh;
1801 struct psref psref;
1802 struct psref psref_src;
1803 DECLARE_LOCK_VARIABLE;
1804
1805 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
1806 return;
1807
1808 src_if = m_get_rcvif_psref(m, &psref_src);
1809 if (src_if == NULL) {
1810 /* Interface is being destroyed? */
1811 m_freem(m);
1812 goto out;
1813 }
1814
1815 if_statadd2(&sc->sc_if, if_ipackets, 1, if_ibytes, m->m_pkthdr.len);
1816
1817 /*
1818 * Look up the bridge_iflist.
1819 */
1820 bif = bridge_lookup_member_if(sc, src_if, &psref);
1821 if (bif == NULL) {
1822 /* Interface is not a bridge member (anymore?) */
1823 m_freem(m);
1824 goto out;
1825 }
1826
1827 if (bif->bif_flags & IFBIF_STP) {
1828 switch (bif->bif_state) {
1829 case BSTP_IFSTATE_BLOCKING:
1830 case BSTP_IFSTATE_LISTENING:
1831 case BSTP_IFSTATE_DISABLED:
1832 m_freem(m);
1833 bridge_release_member(sc, bif, &psref);
1834 goto out;
1835 }
1836 }
1837
1838 eh = mtod(m, struct ether_header *);
1839
1840 /*
1841 * If the interface is learning, and the source
1842 * address is valid and not multicast, record
1843 * the address.
1844 */
1845 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1846 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1847 (eh->ether_shost[0] == 0 &&
1848 eh->ether_shost[1] == 0 &&
1849 eh->ether_shost[2] == 0 &&
1850 eh->ether_shost[3] == 0 &&
1851 eh->ether_shost[4] == 0 &&
1852 eh->ether_shost[5] == 0) == 0) {
1853 (void) bridge_rtupdate(sc, eh->ether_shost,
1854 src_if, 0, IFBAF_DYNAMIC);
1855 }
1856
1857 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1858 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1859 m_freem(m);
1860 bridge_release_member(sc, bif, &psref);
1861 goto out;
1862 }
1863
1864 bridge_release_member(sc, bif, &psref);
1865
1866 /*
1867 * At this point, the port either doesn't participate
1868 * in spanning tree or it is in the forwarding state.
1869 */
1870
1871 /*
1872 * If the packet is unicast, destined for someone on
1873 * "this" side of the bridge, drop it.
1874 */
1875 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1876 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1877 if (src_if == dst_if) {
1878 m_freem(m);
1879 goto out;
1880 }
1881 } else {
1882 /* ...forward it to all interfaces. */
1883 if_statinc(&sc->sc_if, if_imcasts);
1884 dst_if = NULL;
1885 }
1886
1887 if (pfil_run_hooks(sc->sc_if.if_pfil, &m, src_if, PFIL_IN) != 0) {
1888 m_freem(m);
1889 goto out;
1890 }
1891 if (m == NULL)
1892 goto out;
1893
1894 if (dst_if == NULL) {
1895 bridge_broadcast(sc, src_if, m);
1896 goto out;
1897 }
1898
1899 m_put_rcvif_psref(src_if, &psref_src);
1900 src_if = NULL;
1901
1902 /*
1903 * At this point, we're dealing with a unicast frame
1904 * going to a different interface.
1905 */
1906 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1907 m_freem(m);
1908 goto out;
1909 }
1910
1911 bif = bridge_lookup_member_if(sc, dst_if, &psref);
1912 if (bif == NULL) {
1913 /* Not a member of the bridge (anymore?) */
1914 m_freem(m);
1915 goto out;
1916 }
1917
1918 if (bif->bif_flags & IFBIF_STP) {
1919 switch (bif->bif_state) {
1920 case BSTP_IFSTATE_DISABLED:
1921 case BSTP_IFSTATE_BLOCKING:
1922 m_freem(m);
1923 bridge_release_member(sc, bif, &psref);
1924 goto out;
1925 }
1926 }
1927
1928 bridge_release_member(sc, bif, &psref);
1929
1930 /*
1931 * Before enqueueing this packet to the destination interface,
1932 * clear any in-bound checksum flags to prevent them from being
1933 * misused as out-bound flags.
1934 */
1935 m->m_pkthdr.csum_flags = 0;
1936
1937 ACQUIRE_GLOBAL_LOCKS();
1938 bridge_enqueue(sc, dst_if, m, 1);
1939 RELEASE_GLOBAL_LOCKS();
1940 out:
1941 if (src_if != NULL)
1942 m_put_rcvif_psref(src_if, &psref_src);
1943 return;
1944 }
1945
1946 static bool
1947 bstp_state_before_learning(struct bridge_iflist *bif)
1948 {
1949 if (bif->bif_flags & IFBIF_STP) {
1950 switch (bif->bif_state) {
1951 case BSTP_IFSTATE_BLOCKING:
1952 case BSTP_IFSTATE_LISTENING:
1953 case BSTP_IFSTATE_DISABLED:
1954 return true;
1955 }
1956 }
1957 return false;
1958 }
1959
1960 static bool
1961 bridge_ourether(struct bridge_iflist *bif, struct ether_header *eh, int src)
1962 {
1963 uint8_t *ether = src ? eh->ether_shost : eh->ether_dhost;
1964
1965 if (memcmp(CLLADDR(bif->bif_ifp->if_sadl), ether, ETHER_ADDR_LEN) == 0
1966 #if NCARP > 0
1967 || (bif->bif_ifp->if_carp &&
1968 carp_ourether(bif->bif_ifp->if_carp, eh, IFT_ETHER, src) != NULL)
1969 #endif /* NCARP > 0 */
1970 )
1971 return true;
1972
1973 return false;
1974 }
1975
1976 /*
1977 * bridge_input:
1978 *
1979 * Receive input from a member interface. Queue the packet for
1980 * bridging if it is not for us.
1981 */
1982 static void
1983 bridge_input(struct ifnet *ifp, struct mbuf *m)
1984 {
1985 struct bridge_softc *sc = ifp->if_bridge;
1986 struct bridge_iflist *bif;
1987 struct ether_header *eh;
1988 struct psref psref;
1989 int bound;
1990 DECLARE_LOCK_VARIABLE;
1991
1992 KASSERT(!cpu_intr_p());
1993
1994 if (__predict_false(sc == NULL) ||
1995 (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1996 ACQUIRE_GLOBAL_LOCKS();
1997 ether_input(ifp, m);
1998 RELEASE_GLOBAL_LOCKS();
1999 return;
2000 }
2001
2002 bound = curlwp_bind();
2003 bif = bridge_lookup_member_if(sc, ifp, &psref);
2004 if (bif == NULL) {
2005 curlwp_bindx(bound);
2006 ACQUIRE_GLOBAL_LOCKS();
2007 ether_input(ifp, m);
2008 RELEASE_GLOBAL_LOCKS();
2009 return;
2010 }
2011
2012 eh = mtod(m, struct ether_header *);
2013
2014 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
2015 if (memcmp(etherbroadcastaddr,
2016 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
2017 m->m_flags |= M_BCAST;
2018 else
2019 m->m_flags |= M_MCAST;
2020 }
2021
2022 /*
2023 * A 'fast' path for packets addressed to interfaces that are
2024 * part of this bridge.
2025 */
2026 if (!(m->m_flags & (M_BCAST|M_MCAST)) &&
2027 !bstp_state_before_learning(bif)) {
2028 struct bridge_iflist *_bif;
2029 struct ifnet *_ifp = NULL;
2030 int s;
2031 struct psref _psref;
2032
2033 BRIDGE_PSZ_RENTER(s);
2034 BRIDGE_IFLIST_READER_FOREACH(_bif, sc) {
2035 /* It is destined for us. */
2036 if (bridge_ourether(_bif, eh, 0)) {
2037 bridge_acquire_member(sc, _bif, &_psref);
2038 BRIDGE_PSZ_REXIT(s);
2039 if (_bif->bif_flags & IFBIF_LEARNING)
2040 (void) bridge_rtupdate(sc,
2041 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
2042 m_set_rcvif(m, _bif->bif_ifp);
2043 _ifp = _bif->bif_ifp;
2044 bridge_release_member(sc, _bif, &_psref);
2045 goto out;
2046 }
2047
2048 /* We just received a packet that we sent out. */
2049 if (bridge_ourether(_bif, eh, 1))
2050 break;
2051 }
2052 BRIDGE_PSZ_REXIT(s);
2053 out:
2054
2055 if (_bif != NULL) {
2056 bridge_release_member(sc, bif, &psref);
2057 curlwp_bindx(bound);
2058 if (_ifp != NULL) {
2059 m->m_flags &= ~M_PROMISC;
2060 ACQUIRE_GLOBAL_LOCKS();
2061 ether_input(_ifp, m);
2062 RELEASE_GLOBAL_LOCKS();
2063 } else
2064 m_freem(m);
2065 return;
2066 }
2067 }
2068
2069 /* Tap off 802.1D packets; they do not get forwarded. */
2070 if (bif->bif_flags & IFBIF_STP &&
2071 memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0) {
2072 bstp_input(sc, bif, m);
2073 bridge_release_member(sc, bif, &psref);
2074 curlwp_bindx(bound);
2075 return;
2076 }
2077
2078 /*
2079 * A normal switch would discard the packet here, but that's not what
2080 * we've done historically. This also prevents some obnoxious behaviour.
2081 */
2082 if (bstp_state_before_learning(bif)) {
2083 bridge_release_member(sc, bif, &psref);
2084 curlwp_bindx(bound);
2085 ACQUIRE_GLOBAL_LOCKS();
2086 ether_input(ifp, m);
2087 RELEASE_GLOBAL_LOCKS();
2088 return;
2089 }
2090
2091 bridge_release_member(sc, bif, &psref);
2092
2093 bridge_forward(sc, m);
2094
2095 curlwp_bindx(bound);
2096 }
2097
2098 /*
2099 * bridge_broadcast:
2100 *
2101 * Send a frame to all interfaces that are members of
2102 * the bridge, except for the one on which the packet
2103 * arrived.
2104 */
2105 static void
2106 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2107 struct mbuf *m)
2108 {
2109 struct bridge_iflist *bif;
2110 struct mbuf *mc;
2111 struct ifnet *dst_if;
2112 bool bmcast;
2113 int s;
2114 DECLARE_LOCK_VARIABLE;
2115
2116 bmcast = m->m_flags & (M_BCAST|M_MCAST);
2117
2118 BRIDGE_PSZ_RENTER(s);
2119 BRIDGE_IFLIST_READER_FOREACH(bif, sc) {
2120 struct psref psref;
2121
2122 bridge_acquire_member(sc, bif, &psref);
2123 BRIDGE_PSZ_REXIT(s);
2124
2125 dst_if = bif->bif_ifp;
2126
2127 if (bif->bif_flags & IFBIF_STP) {
2128 switch (bif->bif_state) {
2129 case BSTP_IFSTATE_BLOCKING:
2130 case BSTP_IFSTATE_DISABLED:
2131 goto next;
2132 }
2133 }
2134
2135 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && !bmcast)
2136 goto next;
2137
2138 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2139 goto next;
2140
2141 if (dst_if != src_if) {
2142 mc = m_copypacket(m, M_DONTWAIT);
2143 if (mc == NULL) {
2144 if_statinc(&sc->sc_if, if_oerrors);
2145 goto next;
2146 }
2147 /*
2148 * Before enqueueing this packet to the destination
2149 * interface, clear any in-bound checksum flags to
2150 * prevent them from being misused as out-bound flags.
2151 */
2152 mc->m_pkthdr.csum_flags = 0;
2153
2154 ACQUIRE_GLOBAL_LOCKS();
2155 bridge_enqueue(sc, dst_if, mc, 1);
2156 RELEASE_GLOBAL_LOCKS();
2157 }
2158
2159 if (bmcast) {
2160 mc = m_copypacket(m, M_DONTWAIT);
2161 if (mc == NULL) {
2162 if_statinc(&sc->sc_if, if_oerrors);
2163 goto next;
2164 }
2165 /*
2166 * Before enqueueing this packet to the destination
2167 * interface, clear any in-bound checksum flags to
2168 * prevent them from being misused as out-bound flags.
2169 */
2170 mc->m_pkthdr.csum_flags = 0;
2171
2172 m_set_rcvif(mc, dst_if);
2173 mc->m_flags &= ~M_PROMISC;
2174
2175 ACQUIRE_GLOBAL_LOCKS();
2176 ether_input(dst_if, mc);
2177 RELEASE_GLOBAL_LOCKS();
2178 }
2179 next:
2180 BRIDGE_PSZ_RENTER(s);
2181 bridge_release_member(sc, bif, &psref);
2182 }
2183 BRIDGE_PSZ_REXIT(s);
2184
2185 m_freem(m);
2186 }
2187
2188 static int
2189 bridge_rtalloc(struct bridge_softc *sc, const uint8_t *dst,
2190 struct bridge_rtnode **brtp)
2191 {
2192 struct bridge_rtnode *brt;
2193 int error;
2194
2195 if (sc->sc_brtcnt >= sc->sc_brtmax)
2196 return ENOSPC;
2197
2198 /*
2199 * Allocate a new bridge forwarding node, and
2200 * initialize the expiration time and Ethernet
2201 * address.
2202 */
2203 brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
2204 if (brt == NULL)
2205 return ENOMEM;
2206
2207 memset(brt, 0, sizeof(*brt));
2208 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2209 brt->brt_flags = IFBAF_DYNAMIC;
2210 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2211 PSLIST_ENTRY_INIT(brt, brt_list);
2212 PSLIST_ENTRY_INIT(brt, brt_hash);
2213
2214 BRIDGE_RT_LOCK(sc);
2215 error = bridge_rtnode_insert(sc, brt);
2216 BRIDGE_RT_UNLOCK(sc);
2217
2218 if (error != 0) {
2219 pool_put(&bridge_rtnode_pool, brt);
2220 return error;
2221 }
2222
2223 *brtp = brt;
2224 return 0;
2225 }
2226
2227 /*
2228 * bridge_rtupdate:
2229 *
2230 * Add a bridge routing entry.
2231 */
2232 static int
2233 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2234 struct ifnet *dst_if, int setflags, uint8_t flags)
2235 {
2236 struct bridge_rtnode *brt;
2237 int s;
2238
2239 again:
2240 /*
2241 * A route for this destination might already exist. If so,
2242 * update it, otherwise create a new one.
2243 */
2244 BRIDGE_RT_RENTER(s);
2245 brt = bridge_rtnode_lookup(sc, dst);
2246
2247 if (brt != NULL) {
2248 brt->brt_ifp = dst_if;
2249 if (setflags) {
2250 brt->brt_flags = flags;
2251 if (flags & IFBAF_STATIC)
2252 brt->brt_expire = 0;
2253 else
2254 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2255 } else {
2256 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2257 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2258 }
2259 }
2260 BRIDGE_RT_REXIT(s);
2261
2262 if (brt == NULL) {
2263 int r;
2264
2265 r = bridge_rtalloc(sc, dst, &brt);
2266 if (r != 0)
2267 return r;
2268 goto again;
2269 }
2270
2271 return 0;
2272 }
2273
2274 /*
2275 * bridge_rtlookup:
2276 *
2277 * Lookup the destination interface for an address.
2278 */
2279 static struct ifnet *
2280 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2281 {
2282 struct bridge_rtnode *brt;
2283 struct ifnet *ifs = NULL;
2284 int s;
2285
2286 BRIDGE_RT_RENTER(s);
2287 brt = bridge_rtnode_lookup(sc, addr);
2288 if (brt != NULL)
2289 ifs = brt->brt_ifp;
2290 BRIDGE_RT_REXIT(s);
2291
2292 return ifs;
2293 }
2294
2295 typedef bool (*bridge_iterate_cb_t)
2296 (struct bridge_softc *, struct bridge_rtnode *, bool *, void *);
2297
2298 /*
2299 * bridge_rtlist_iterate_remove:
2300 *
2301 * It iterates on sc->sc_rtlist and removes rtnodes of it which func
2302 * callback judges to remove. Removals of rtnodes are done in a manner
2303 * of pserialize. To this end, all kmem_* operations are placed out of
2304 * mutexes.
2305 */
2306 static void
2307 bridge_rtlist_iterate_remove(struct bridge_softc *sc, bridge_iterate_cb_t func, void *arg)
2308 {
2309 struct bridge_rtnode *brt;
2310 struct bridge_rtnode **brt_list;
2311 int i, count;
2312
2313 retry:
2314 count = sc->sc_brtcnt;
2315 if (count == 0)
2316 return;
2317 brt_list = kmem_alloc(sizeof(*brt_list) * count, KM_SLEEP);
2318
2319 BRIDGE_RT_LOCK(sc);
2320 if (__predict_false(sc->sc_brtcnt > count)) {
2321 /* The rtnodes increased, we need more memory */
2322 BRIDGE_RT_UNLOCK(sc);
2323 kmem_free(brt_list, sizeof(*brt_list) * count);
2324 goto retry;
2325 }
2326
2327 i = 0;
2328 /*
2329 * We don't need to use a _SAFE variant here because we know
2330 * that a removed item keeps its next pointer as-is thanks to
2331 * pslist(9) and isn't freed in the loop.
2332 */
2333 BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) {
2334 bool need_break = false;
2335 if (func(sc, brt, &need_break, arg)) {
2336 bridge_rtnode_remove(sc, brt);
2337 brt_list[i++] = brt;
2338 }
2339 if (need_break)
2340 break;
2341 }
2342
2343 if (i > 0)
2344 BRIDGE_RT_PSZ_PERFORM(sc);
2345 BRIDGE_RT_UNLOCK(sc);
2346
2347 while (--i >= 0)
2348 bridge_rtnode_destroy(brt_list[i]);
2349
2350 kmem_free(brt_list, sizeof(*brt_list) * count);
2351 }
2352
2353 static bool
2354 bridge_rttrim0_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
2355 bool *need_break, void *arg)
2356 {
2357 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2358 /* Take into account of the subsequent removal */
2359 if ((sc->sc_brtcnt - 1) <= sc->sc_brtmax)
2360 *need_break = true;
2361 return true;
2362 } else
2363 return false;
2364 }
2365
2366 static void
2367 bridge_rttrim0(struct bridge_softc *sc)
2368 {
2369 bridge_rtlist_iterate_remove(sc, bridge_rttrim0_cb, NULL);
2370 }
2371
2372 /*
2373 * bridge_rttrim:
2374 *
2375 * Trim the routine table so that we have a number
2376 * of routing entries less than or equal to the
2377 * maximum number.
2378 */
2379 static void
2380 bridge_rttrim(struct bridge_softc *sc)
2381 {
2382
2383 /* Make sure we actually need to do this. */
2384 if (sc->sc_brtcnt <= sc->sc_brtmax)
2385 return;
2386
2387 /* Force an aging cycle; this might trim enough addresses. */
2388 bridge_rtage(sc);
2389 if (sc->sc_brtcnt <= sc->sc_brtmax)
2390 return;
2391
2392 bridge_rttrim0(sc);
2393
2394 return;
2395 }
2396
2397 /*
2398 * bridge_timer:
2399 *
2400 * Aging timer for the bridge.
2401 */
2402 static void
2403 bridge_timer(void *arg)
2404 {
2405 struct bridge_softc *sc = arg;
2406
2407 workqueue_enqueue(sc->sc_rtage_wq, &sc->sc_rtage_wk, NULL);
2408 }
2409
2410 static void
2411 bridge_rtage_work(struct work *wk, void *arg)
2412 {
2413 struct bridge_softc *sc = arg;
2414
2415 KASSERT(wk == &sc->sc_rtage_wk);
2416
2417 bridge_rtage(sc);
2418
2419 if (sc->sc_if.if_flags & IFF_RUNNING)
2420 callout_reset(&sc->sc_brcallout,
2421 bridge_rtable_prune_period * hz, bridge_timer, sc);
2422 }
2423
2424 static bool
2425 bridge_rtage_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
2426 bool *need_break, void *arg)
2427 {
2428 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2429 time_uptime >= brt->brt_expire)
2430 return true;
2431 else
2432 return false;
2433 }
2434
2435 /*
2436 * bridge_rtage:
2437 *
2438 * Perform an aging cycle.
2439 */
2440 static void
2441 bridge_rtage(struct bridge_softc *sc)
2442 {
2443 bridge_rtlist_iterate_remove(sc, bridge_rtage_cb, NULL);
2444 }
2445
2446
2447 static bool
2448 bridge_rtflush_cb(struct bridge_softc *sc, struct bridge_rtnode *brt,
2449 bool *need_break, void *arg)
2450 {
2451 int full = *(int*)arg;
2452
2453 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2454 return true;
2455 else
2456 return false;
2457 }
2458
2459 /*
2460 * bridge_rtflush:
2461 *
2462 * Remove all dynamic addresses from the bridge.
2463 */
2464 static void
2465 bridge_rtflush(struct bridge_softc *sc, int full)
2466 {
2467 bridge_rtlist_iterate_remove(sc, bridge_rtflush_cb, &full);
2468 }
2469
2470 /*
2471 * bridge_rtdaddr:
2472 *
2473 * Remove an address from the table.
2474 */
2475 static int
2476 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2477 {
2478 struct bridge_rtnode *brt;
2479
2480 BRIDGE_RT_LOCK(sc);
2481 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) {
2482 BRIDGE_RT_UNLOCK(sc);
2483 return ENOENT;
2484 }
2485 bridge_rtnode_remove(sc, brt);
2486 BRIDGE_RT_PSZ_PERFORM(sc);
2487 BRIDGE_RT_UNLOCK(sc);
2488
2489 bridge_rtnode_destroy(brt);
2490
2491 return 0;
2492 }
2493
2494 /*
2495 * bridge_rtdelete:
2496 *
2497 * Delete routes to a speicifc member interface.
2498 */
2499 static void
2500 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
2501 {
2502 struct bridge_rtnode *brt;
2503
2504 /* XXX pserialize_perform for each entry is slow */
2505 again:
2506 BRIDGE_RT_LOCK(sc);
2507 BRIDGE_RTLIST_WRITER_FOREACH(brt, sc) {
2508 if (brt->brt_ifp == ifp)
2509 break;
2510 }
2511 if (brt == NULL) {
2512 BRIDGE_RT_UNLOCK(sc);
2513 return;
2514 }
2515 bridge_rtnode_remove(sc, brt);
2516 BRIDGE_RT_PSZ_PERFORM(sc);
2517 BRIDGE_RT_UNLOCK(sc);
2518
2519 bridge_rtnode_destroy(brt);
2520
2521 goto again;
2522 }
2523
2524 /*
2525 * bridge_rtable_init:
2526 *
2527 * Initialize the route table for this bridge.
2528 */
2529 static void
2530 bridge_rtable_init(struct bridge_softc *sc)
2531 {
2532 int i;
2533
2534 sc->sc_rthash = kmem_alloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2535 KM_SLEEP);
2536
2537 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2538 PSLIST_INIT(&sc->sc_rthash[i]);
2539
2540 sc->sc_rthash_key = cprng_fast32();
2541
2542 PSLIST_INIT(&sc->sc_rtlist);
2543
2544 sc->sc_rtlist_psz = pserialize_create();
2545 sc->sc_rtlist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
2546 }
2547
2548 /*
2549 * bridge_rtable_fini:
2550 *
2551 * Deconstruct the route table for this bridge.
2552 */
2553 static void
2554 bridge_rtable_fini(struct bridge_softc *sc)
2555 {
2556
2557 kmem_free(sc->sc_rthash, sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE);
2558 mutex_obj_free(sc->sc_rtlist_lock);
2559 pserialize_destroy(sc->sc_rtlist_psz);
2560 }
2561
2562 /*
2563 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2564 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2565 */
2566 #define mix(a, b, c) \
2567 do { \
2568 a -= b; a -= c; a ^= (c >> 13); \
2569 b -= c; b -= a; b ^= (a << 8); \
2570 c -= a; c -= b; c ^= (b >> 13); \
2571 a -= b; a -= c; a ^= (c >> 12); \
2572 b -= c; b -= a; b ^= (a << 16); \
2573 c -= a; c -= b; c ^= (b >> 5); \
2574 a -= b; a -= c; a ^= (c >> 3); \
2575 b -= c; b -= a; b ^= (a << 10); \
2576 c -= a; c -= b; c ^= (b >> 15); \
2577 } while (/*CONSTCOND*/0)
2578
2579 static inline uint32_t
2580 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2581 {
2582 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2583
2584 b += addr[5] << 8;
2585 b += addr[4];
2586 a += (uint32_t)addr[3] << 24;
2587 a += addr[2] << 16;
2588 a += addr[1] << 8;
2589 a += addr[0];
2590
2591 mix(a, b, c);
2592
2593 return (c & BRIDGE_RTHASH_MASK);
2594 }
2595
2596 #undef mix
2597
2598 /*
2599 * bridge_rtnode_lookup:
2600 *
2601 * Look up a bridge route node for the specified destination.
2602 */
2603 static struct bridge_rtnode *
2604 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2605 {
2606 struct bridge_rtnode *brt;
2607 uint32_t hash;
2608 int dir;
2609
2610 hash = bridge_rthash(sc, addr);
2611 BRIDGE_RTHASH_READER_FOREACH(brt, sc, hash) {
2612 dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
2613 if (dir == 0)
2614 return brt;
2615 if (dir > 0)
2616 return NULL;
2617 }
2618
2619 return NULL;
2620 }
2621
2622 /*
2623 * bridge_rtnode_insert:
2624 *
2625 * Insert the specified bridge node into the route table. We
2626 * assume the entry is not already in the table.
2627 */
2628 static int
2629 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2630 {
2631 struct bridge_rtnode *lbrt, *prev = NULL;
2632 uint32_t hash;
2633
2634 KASSERT(BRIDGE_RT_LOCKED(sc));
2635
2636 hash = bridge_rthash(sc, brt->brt_addr);
2637 BRIDGE_RTHASH_WRITER_FOREACH(lbrt, sc, hash) {
2638 int dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
2639 if (dir == 0)
2640 return EEXIST;
2641 if (dir > 0)
2642 break;
2643 prev = lbrt;
2644 }
2645 if (prev == NULL)
2646 BRIDGE_RTHASH_WRITER_INSERT_HEAD(sc, hash, brt);
2647 else
2648 BRIDGE_RTHASH_WRITER_INSERT_AFTER(prev, brt);
2649
2650 BRIDGE_RTLIST_WRITER_INSERT_HEAD(sc, brt);
2651 sc->sc_brtcnt++;
2652
2653 return 0;
2654 }
2655
2656 /*
2657 * bridge_rtnode_remove:
2658 *
2659 * Remove a bridge rtnode from the rthash and the rtlist of a bridge.
2660 */
2661 static void
2662 bridge_rtnode_remove(struct bridge_softc *sc, struct bridge_rtnode *brt)
2663 {
2664
2665 KASSERT(BRIDGE_RT_LOCKED(sc));
2666
2667 BRIDGE_RTHASH_WRITER_REMOVE(brt);
2668 BRIDGE_RTLIST_WRITER_REMOVE(brt);
2669 sc->sc_brtcnt--;
2670 }
2671
2672 /*
2673 * bridge_rtnode_destroy:
2674 *
2675 * Destroy a bridge rtnode.
2676 */
2677 static void
2678 bridge_rtnode_destroy(struct bridge_rtnode *brt)
2679 {
2680
2681 PSLIST_ENTRY_DESTROY(brt, brt_list);
2682 PSLIST_ENTRY_DESTROY(brt, brt_hash);
2683 pool_put(&bridge_rtnode_pool, brt);
2684 }
2685
2686 extern pfil_head_t *inet_pfil_hook; /* XXX */
2687 extern pfil_head_t *inet6_pfil_hook; /* XXX */
2688
2689 /*
2690 * Send bridge packets through IPF if they are one of the types IPF can deal
2691 * with, or if they are ARP or REVARP. (IPF will pass ARP and REVARP without
2692 * question.)
2693 */
2694 static int
2695 bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
2696 {
2697 int snap, error;
2698 struct ether_header *eh1, eh2;
2699 struct llc llc1;
2700 uint16_t ether_type;
2701
2702 snap = 0;
2703 error = -1; /* Default error if not error == 0 */
2704 eh1 = mtod(*mp, struct ether_header *);
2705 ether_type = ntohs(eh1->ether_type);
2706
2707 /*
2708 * Check for SNAP/LLC.
2709 */
2710 if (ether_type < ETHERMTU) {
2711 struct llc *llc2 = (struct llc *)(eh1 + 1);
2712
2713 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2714 llc2->llc_dsap == LLC_SNAP_LSAP &&
2715 llc2->llc_ssap == LLC_SNAP_LSAP &&
2716 llc2->llc_control == LLC_UI) {
2717 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2718 snap = 1;
2719 }
2720 }
2721
2722 /* drop VLAN traffic untagged by hardware offloading */
2723 if (vlan_has_tag(*mp))
2724 goto bad;
2725
2726 /*
2727 * If we're trying to filter bridge traffic, don't look at anything
2728 * other than IP and ARP traffic. If the filter doesn't understand
2729 * IPv6, don't allow IPv6 through the bridge either. This is lame
2730 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2731 * but of course we don't have an AppleTalk filter to begin with.
2732 * (Note that since IPF doesn't understand ARP it will pass *ALL*
2733 * ARP traffic.)
2734 */
2735 switch (ether_type) {
2736 case ETHERTYPE_ARP:
2737 case ETHERTYPE_REVARP:
2738 return 0; /* Automatically pass */
2739 case ETHERTYPE_IP:
2740 # ifdef INET6
2741 case ETHERTYPE_IPV6:
2742 # endif /* INET6 */
2743 break;
2744 default:
2745 goto bad;
2746 }
2747
2748 /* Strip off the Ethernet header and keep a copy. */
2749 m_copydata(*mp, 0, ETHER_HDR_LEN, (void *) &eh2);
2750 m_adj(*mp, ETHER_HDR_LEN);
2751
2752 /* Strip off snap header, if present */
2753 if (snap) {
2754 m_copydata(*mp, 0, sizeof(struct llc), (void *) &llc1);
2755 m_adj(*mp, sizeof(struct llc));
2756 }
2757
2758 /*
2759 * Check basic packet sanity and run IPF through pfil.
2760 */
2761 KASSERT(!cpu_intr_p());
2762 switch (ether_type)
2763 {
2764 case ETHERTYPE_IP :
2765 error = bridge_ip_checkbasic(mp);
2766 if (error == 0)
2767 error = pfil_run_hooks(inet_pfil_hook, mp, ifp, dir);
2768 break;
2769 # ifdef INET6
2770 case ETHERTYPE_IPV6 :
2771 error = bridge_ip6_checkbasic(mp);
2772 if (error == 0)
2773 error = pfil_run_hooks(inet6_pfil_hook, mp, ifp, dir);
2774 break;
2775 # endif
2776 default :
2777 error = 0;
2778 break;
2779 }
2780
2781 if (*mp == NULL)
2782 return error;
2783 if (error != 0)
2784 goto bad;
2785
2786 error = -1;
2787
2788 /*
2789 * Finally, put everything back the way it was and return
2790 */
2791 if (snap) {
2792 M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
2793 if (*mp == NULL)
2794 return error;
2795 bcopy(&llc1, mtod(*mp, void *), sizeof(struct llc));
2796 }
2797
2798 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
2799 if (*mp == NULL)
2800 return error;
2801 bcopy(&eh2, mtod(*mp, void *), ETHER_HDR_LEN);
2802
2803 return 0;
2804
2805 bad:
2806 m_freem(*mp);
2807 *mp = NULL;
2808 return error;
2809 }
2810
2811 /*
2812 * Perform basic checks on header size since
2813 * IPF assumes ip_input has already processed
2814 * it for it. Cut-and-pasted from ip_input.c.
2815 * Given how simple the IPv6 version is,
2816 * does the IPv4 version really need to be
2817 * this complicated?
2818 *
2819 * XXX Should we update ipstat here, or not?
2820 * XXX Right now we update ipstat but not
2821 * XXX csum_counter.
2822 */
2823 static int
2824 bridge_ip_checkbasic(struct mbuf **mp)
2825 {
2826 struct mbuf *m = *mp;
2827 struct ip *ip;
2828 int len, hlen;
2829
2830 if (*mp == NULL)
2831 return -1;
2832
2833 if (M_GET_ALIGNED_HDR(&m, struct ip, true) != 0) {
2834 /* XXXJRT new stat, please */
2835 ip_statinc(IP_STAT_TOOSMALL);
2836 goto bad;
2837 }
2838 ip = mtod(m, struct ip *);
2839 if (ip == NULL) goto bad;
2840
2841 if (ip->ip_v != IPVERSION) {
2842 ip_statinc(IP_STAT_BADVERS);
2843 goto bad;
2844 }
2845 hlen = ip->ip_hl << 2;
2846 if (hlen < sizeof(struct ip)) { /* minimum header length */
2847 ip_statinc(IP_STAT_BADHLEN);
2848 goto bad;
2849 }
2850 if (hlen > m->m_len) {
2851 if ((m = m_pullup(m, hlen)) == 0) {
2852 ip_statinc(IP_STAT_BADHLEN);
2853 goto bad;
2854 }
2855 ip = mtod(m, struct ip *);
2856 if (ip == NULL) goto bad;
2857 }
2858
2859 switch (m->m_pkthdr.csum_flags &
2860 ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_IPv4) |
2861 M_CSUM_IPv4_BAD)) {
2862 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
2863 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */
2864 goto bad;
2865
2866 case M_CSUM_IPv4:
2867 /* Checksum was okay. */
2868 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */
2869 break;
2870
2871 default:
2872 /* Must compute it ourselves. */
2873 /* INET_CSUM_COUNTER_INCR(&ip_swcsum); */
2874 if (in_cksum(m, hlen) != 0)
2875 goto bad;
2876 break;
2877 }
2878
2879 /* Retrieve the packet length. */
2880 len = ntohs(ip->ip_len);
2881
2882 /*
2883 * Check for additional length bogosity
2884 */
2885 if (len < hlen) {
2886 ip_statinc(IP_STAT_BADLEN);
2887 goto bad;
2888 }
2889
2890 /*
2891 * Check that the amount of data in the buffers
2892 * is as at least much as the IP header would have us expect.
2893 * Drop packet if shorter than we expect.
2894 */
2895 if (m->m_pkthdr.len < len) {
2896 ip_statinc(IP_STAT_TOOSHORT);
2897 goto bad;
2898 }
2899
2900 /* Checks out, proceed */
2901 *mp = m;
2902 return 0;
2903
2904 bad:
2905 *mp = m;
2906 return -1;
2907 }
2908
2909 # ifdef INET6
2910 /*
2911 * Same as above, but for IPv6.
2912 * Cut-and-pasted from ip6_input.c.
2913 * XXX Should we update ip6stat, or not?
2914 */
2915 static int
2916 bridge_ip6_checkbasic(struct mbuf **mp)
2917 {
2918 struct mbuf *m = *mp;
2919 struct ip6_hdr *ip6;
2920
2921 /*
2922 * If the IPv6 header is not aligned, slurp it up into a new
2923 * mbuf with space for link headers, in the event we forward
2924 * it. Otherwise, if it is aligned, make sure the entire base
2925 * IPv6 header is in the first mbuf of the chain.
2926 */
2927 if (M_GET_ALIGNED_HDR(&m, struct ip6_hdr, true) != 0) {
2928 struct ifnet *inifp = m_get_rcvif_NOMPSAFE(m);
2929 /* XXXJRT new stat, please */
2930 ip6_statinc(IP6_STAT_TOOSMALL);
2931 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2932 goto bad;
2933 }
2934
2935 ip6 = mtod(m, struct ip6_hdr *);
2936
2937 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2938 ip6_statinc(IP6_STAT_BADVERS);
2939 in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m), ifs6_in_hdrerr);
2940 goto bad;
2941 }
2942
2943 /* Checks out, proceed */
2944 *mp = m;
2945 return 0;
2946
2947 bad:
2948 *mp = m;
2949 return -1;
2950 }
2951 # endif /* INET6 */
2952