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