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