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