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