if_bridge.c revision 1.90 1 /* $NetBSD: if_bridge.c,v 1.90 2014/07/23 05:32:23 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.90 2014/07/23 05:32:23 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
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 /*
810 * Place the interface into promiscuous mode.
811 */
812 error = ifpromisc(ifs, 1);
813 if (error)
814 goto out;
815 break;
816 default:
817 error = EINVAL;
818 goto out;
819 }
820
821 bif->bif_ifp = ifs;
822 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
823 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
824 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
825 bif->bif_refs = 0;
826 bif->bif_waiting = false;
827
828 BRIDGE_LOCK(sc);
829
830 ifs->if_bridge = sc;
831 ifs->if_bridgeif = bif;
832 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
833 ifs->if_input = bridge_input;
834
835 BRIDGE_UNLOCK(sc);
836
837 if (sc->sc_if.if_flags & IFF_RUNNING)
838 bstp_initialization(sc);
839 else
840 bstp_stop(sc);
841
842 out:
843 if (error) {
844 if (bif != NULL)
845 free(bif, M_DEVBUF);
846 }
847 return (error);
848 }
849
850 static int
851 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
852 {
853 struct ifbreq *req = arg;
854 const char *name = req->ifbr_ifsname;
855 struct bridge_iflist *bif;
856 struct ifnet *ifs;
857
858 BRIDGE_LOCK(sc);
859
860 /*
861 * Don't use bridge_lookup_member. We want to get a member
862 * with bif_refs == 0.
863 */
864 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
865 ifs = bif->bif_ifp;
866 if (strcmp(ifs->if_xname, name) == 0)
867 break;
868 }
869
870 if (bif == NULL) {
871 BRIDGE_UNLOCK(sc);
872 return ENOENT;
873 }
874
875 bridge_delete_member(sc, bif);
876
877 BRIDGE_UNLOCK(sc);
878
879 switch (ifs->if_type) {
880 case IFT_ETHER:
881 /*
882 * Take the interface out of promiscuous mode.
883 * Don't call it with holding sc_iflist_lock.
884 */
885 (void) ifpromisc(ifs, 0);
886 break;
887 default:
888 #ifdef DIAGNOSTIC
889 panic("bridge_delete_member: impossible");
890 #endif
891 break;
892 }
893
894 bridge_rtdelete(sc, ifs);
895
896 if (sc->sc_if.if_flags & IFF_RUNNING)
897 bstp_initialization(sc);
898
899 return 0;
900 }
901
902 static int
903 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
904 {
905 struct ifbreq *req = arg;
906 struct bridge_iflist *bif;
907
908 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
909 if (bif == NULL)
910 return (ENOENT);
911
912 req->ifbr_ifsflags = bif->bif_flags;
913 req->ifbr_state = bif->bif_state;
914 req->ifbr_priority = bif->bif_priority;
915 req->ifbr_path_cost = bif->bif_path_cost;
916 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
917
918 bridge_release_member(sc, bif);
919
920 return (0);
921 }
922
923 static int
924 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
925 {
926 struct ifbreq *req = arg;
927 struct bridge_iflist *bif;
928
929 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
930 if (bif == NULL)
931 return (ENOENT);
932
933 if (req->ifbr_ifsflags & IFBIF_STP) {
934 switch (bif->bif_ifp->if_type) {
935 case IFT_ETHER:
936 /* These can do spanning tree. */
937 break;
938
939 default:
940 /* Nothing else can. */
941 bridge_release_member(sc, bif);
942 return (EINVAL);
943 }
944 }
945
946 bif->bif_flags = req->ifbr_ifsflags;
947
948 bridge_release_member(sc, bif);
949
950 if (sc->sc_if.if_flags & IFF_RUNNING)
951 bstp_initialization(sc);
952
953 return (0);
954 }
955
956 static int
957 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
958 {
959 struct ifbrparam *param = arg;
960
961 sc->sc_brtmax = param->ifbrp_csize;
962 bridge_rttrim(sc);
963
964 return (0);
965 }
966
967 static int
968 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
969 {
970 struct ifbrparam *param = arg;
971
972 param->ifbrp_csize = sc->sc_brtmax;
973
974 return (0);
975 }
976
977 static int
978 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
979 {
980 struct ifbifconf *bifc = arg;
981 struct bridge_iflist *bif;
982 struct ifbreq *breqs;
983 int i, count, error = 0;
984
985 BRIDGE_LOCK(sc);
986
987 count = 0;
988 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
989 count++;
990
991 if (bifc->ifbic_len == 0 || bifc->ifbic_len < (sizeof(*breqs) * count)) {
992 BRIDGE_UNLOCK(sc);
993 /* Tell that a larger buffer is needed */
994 bifc->ifbic_len = sizeof(*breqs) * count;
995 return 0;
996 }
997
998 breqs = malloc(sizeof(*breqs) * count, M_DEVBUF, M_NOWAIT);
999
1000 i = 0;
1001 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1002 struct ifbreq *breq = &breqs[i++];
1003 memset(breq, 0, sizeof(*breq));
1004
1005 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1006 sizeof(breq->ifbr_ifsname));
1007 breq->ifbr_ifsflags = bif->bif_flags;
1008 breq->ifbr_state = bif->bif_state;
1009 breq->ifbr_priority = bif->bif_priority;
1010 breq->ifbr_path_cost = bif->bif_path_cost;
1011 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1012 }
1013
1014 /* Don't call copyout with holding the mutex */
1015 BRIDGE_UNLOCK(sc);
1016
1017 for (i = 0; i < count; i++) {
1018 error = copyout(&breqs[i], bifc->ifbic_req + i, sizeof(*breqs));
1019 if (error)
1020 break;
1021 }
1022 bifc->ifbic_len = sizeof(*breqs) * i;
1023
1024 free(breqs, M_DEVBUF);
1025
1026 return error;
1027 }
1028
1029 static int
1030 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1031 {
1032 struct ifbaconf *bac = arg;
1033 struct bridge_rtnode *brt;
1034 struct ifbareq bareq;
1035 int count = 0, error = 0, len;
1036
1037 if (bac->ifbac_len == 0)
1038 return (0);
1039
1040 mutex_enter(sc->sc_rtlist_lock);
1041
1042 len = bac->ifbac_len;
1043 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1044 if (len < sizeof(bareq))
1045 goto out;
1046 memset(&bareq, 0, sizeof(bareq));
1047 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1048 sizeof(bareq.ifba_ifsname));
1049 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1050 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1051 bareq.ifba_expire = brt->brt_expire - time_uptime;
1052 } else
1053 bareq.ifba_expire = 0;
1054 bareq.ifba_flags = brt->brt_flags;
1055
1056 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1057 if (error)
1058 goto out;
1059 count++;
1060 len -= sizeof(bareq);
1061 }
1062 out:
1063 mutex_exit(sc->sc_rtlist_lock);
1064
1065 bac->ifbac_len = sizeof(bareq) * count;
1066 return (error);
1067 }
1068
1069 static int
1070 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1071 {
1072 struct ifbareq *req = arg;
1073 struct bridge_iflist *bif;
1074 int error;
1075
1076 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1077 if (bif == NULL)
1078 return (ENOENT);
1079
1080 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
1081 req->ifba_flags);
1082
1083 bridge_release_member(sc, bif);
1084
1085 return (error);
1086 }
1087
1088 static int
1089 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1090 {
1091 struct ifbrparam *param = arg;
1092
1093 sc->sc_brttimeout = param->ifbrp_ctime;
1094
1095 return (0);
1096 }
1097
1098 static int
1099 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1100 {
1101 struct ifbrparam *param = arg;
1102
1103 param->ifbrp_ctime = sc->sc_brttimeout;
1104
1105 return (0);
1106 }
1107
1108 static int
1109 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1110 {
1111 struct ifbareq *req = arg;
1112
1113 return (bridge_rtdaddr(sc, req->ifba_dst));
1114 }
1115
1116 static int
1117 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1118 {
1119 struct ifbreq *req = arg;
1120
1121 bridge_rtflush(sc, req->ifbr_ifsflags);
1122
1123 return (0);
1124 }
1125
1126 static int
1127 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1128 {
1129 struct ifbrparam *param = arg;
1130
1131 param->ifbrp_prio = sc->sc_bridge_priority;
1132
1133 return (0);
1134 }
1135
1136 static int
1137 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1138 {
1139 struct ifbrparam *param = arg;
1140
1141 sc->sc_bridge_priority = param->ifbrp_prio;
1142
1143 if (sc->sc_if.if_flags & IFF_RUNNING)
1144 bstp_initialization(sc);
1145
1146 return (0);
1147 }
1148
1149 static int
1150 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1151 {
1152 struct ifbrparam *param = arg;
1153
1154 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1155
1156 return (0);
1157 }
1158
1159 static int
1160 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1161 {
1162 struct ifbrparam *param = arg;
1163
1164 if (param->ifbrp_hellotime == 0)
1165 return (EINVAL);
1166 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1167
1168 if (sc->sc_if.if_flags & IFF_RUNNING)
1169 bstp_initialization(sc);
1170
1171 return (0);
1172 }
1173
1174 static int
1175 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1176 {
1177 struct ifbrparam *param = arg;
1178
1179 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1180
1181 return (0);
1182 }
1183
1184 static int
1185 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1186 {
1187 struct ifbrparam *param = arg;
1188
1189 if (param->ifbrp_fwddelay == 0)
1190 return (EINVAL);
1191 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1192
1193 if (sc->sc_if.if_flags & IFF_RUNNING)
1194 bstp_initialization(sc);
1195
1196 return (0);
1197 }
1198
1199 static int
1200 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1201 {
1202 struct ifbrparam *param = arg;
1203
1204 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1205
1206 return (0);
1207 }
1208
1209 static int
1210 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1211 {
1212 struct ifbrparam *param = arg;
1213
1214 if (param->ifbrp_maxage == 0)
1215 return (EINVAL);
1216 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1217
1218 if (sc->sc_if.if_flags & IFF_RUNNING)
1219 bstp_initialization(sc);
1220
1221 return (0);
1222 }
1223
1224 static int
1225 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1226 {
1227 struct ifbreq *req = arg;
1228 struct bridge_iflist *bif;
1229
1230 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1231 if (bif == NULL)
1232 return (ENOENT);
1233
1234 bif->bif_priority = req->ifbr_priority;
1235
1236 if (sc->sc_if.if_flags & IFF_RUNNING)
1237 bstp_initialization(sc);
1238
1239 bridge_release_member(sc, bif);
1240
1241 return (0);
1242 }
1243
1244 #if defined(BRIDGE_IPF)
1245 static int
1246 bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg)
1247 {
1248 struct ifbrparam *param = arg;
1249
1250 param->ifbrp_filter = sc->sc_filter_flags;
1251
1252 return (0);
1253 }
1254
1255 static int
1256 bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg)
1257 {
1258 struct ifbrparam *param = arg;
1259 uint32_t nflags, oflags;
1260
1261 if (param->ifbrp_filter & ~IFBF_FILT_MASK)
1262 return (EINVAL);
1263
1264 nflags = param->ifbrp_filter;
1265 oflags = sc->sc_filter_flags;
1266
1267 if ((nflags & IFBF_FILT_USEIPF) && !(oflags & IFBF_FILT_USEIPF)) {
1268 pfil_add_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1269 sc->sc_if.if_pfil);
1270 }
1271 if (!(nflags & IFBF_FILT_USEIPF) && (oflags & IFBF_FILT_USEIPF)) {
1272 pfil_remove_hook((void *)bridge_ipf, NULL, PFIL_IN|PFIL_OUT,
1273 sc->sc_if.if_pfil);
1274 }
1275
1276 sc->sc_filter_flags = nflags;
1277
1278 return (0);
1279 }
1280 #endif /* BRIDGE_IPF */
1281
1282 static int
1283 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1284 {
1285 struct ifbreq *req = arg;
1286 struct bridge_iflist *bif;
1287
1288 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1289 if (bif == NULL)
1290 return (ENOENT);
1291
1292 bif->bif_path_cost = req->ifbr_path_cost;
1293
1294 if (sc->sc_if.if_flags & IFF_RUNNING)
1295 bstp_initialization(sc);
1296
1297 bridge_release_member(sc, bif);
1298
1299 return (0);
1300 }
1301
1302 /*
1303 * bridge_ifdetach:
1304 *
1305 * Detach an interface from a bridge. Called when a member
1306 * interface is detaching.
1307 */
1308 void
1309 bridge_ifdetach(struct ifnet *ifp)
1310 {
1311 struct bridge_softc *sc = ifp->if_bridge;
1312 struct ifbreq breq;
1313
1314 /* ioctl_lock should prevent this from happening */
1315 KASSERT(sc != NULL);
1316
1317 memset(&breq, 0, sizeof(breq));
1318 strlcpy(breq.ifbr_ifsname, ifp->if_xname, sizeof(breq.ifbr_ifsname));
1319
1320 (void) bridge_ioctl_del(sc, &breq);
1321 }
1322
1323 /*
1324 * bridge_init:
1325 *
1326 * Initialize a bridge interface.
1327 */
1328 static int
1329 bridge_init(struct ifnet *ifp)
1330 {
1331 struct bridge_softc *sc = ifp->if_softc;
1332
1333 if (ifp->if_flags & IFF_RUNNING)
1334 return (0);
1335
1336 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1337 bridge_timer, sc);
1338
1339 ifp->if_flags |= IFF_RUNNING;
1340 bstp_initialization(sc);
1341 return (0);
1342 }
1343
1344 /*
1345 * bridge_stop:
1346 *
1347 * Stop the bridge interface.
1348 */
1349 static void
1350 bridge_stop(struct ifnet *ifp, int disable)
1351 {
1352 struct bridge_softc *sc = ifp->if_softc;
1353
1354 if ((ifp->if_flags & IFF_RUNNING) == 0)
1355 return;
1356
1357 callout_stop(&sc->sc_brcallout);
1358 bstp_stop(sc);
1359
1360 bridge_rtflush(sc, IFBF_FLUSHDYN);
1361
1362 ifp->if_flags &= ~IFF_RUNNING;
1363 }
1364
1365 /*
1366 * bridge_enqueue:
1367 *
1368 * Enqueue a packet on a bridge member interface.
1369 */
1370 void
1371 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m,
1372 int runfilt)
1373 {
1374 ALTQ_DECL(struct altq_pktattr pktattr;)
1375 int len, error;
1376 short mflags;
1377
1378 /*
1379 * Clear any in-bound checksum flags for this packet.
1380 */
1381 m->m_pkthdr.csum_flags = 0;
1382
1383 if (runfilt) {
1384 if (pfil_run_hooks(sc->sc_if.if_pfil, &m,
1385 dst_ifp, PFIL_OUT) != 0) {
1386 if (m != NULL)
1387 m_freem(m);
1388 return;
1389 }
1390 if (m == NULL)
1391 return;
1392 }
1393
1394 #ifdef ALTQ
1395 /*
1396 * If ALTQ is enabled on the member interface, do
1397 * classification; the queueing discipline might
1398 * not require classification, but might require
1399 * the address family/header pointer in the pktattr.
1400 */
1401 if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
1402 /* XXX IFT_ETHER */
1403 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
1404 }
1405 #endif /* ALTQ */
1406
1407 len = m->m_pkthdr.len;
1408 m->m_flags |= M_PROTO1;
1409 mflags = m->m_flags;
1410
1411 IFQ_ENQUEUE(&dst_ifp->if_snd, m, &pktattr, error);
1412
1413 if (error) {
1414 /* mbuf is already freed */
1415 sc->sc_if.if_oerrors++;
1416 return;
1417 }
1418
1419 sc->sc_if.if_opackets++;
1420 sc->sc_if.if_obytes += len;
1421
1422 dst_ifp->if_obytes += len;
1423
1424 if (mflags & M_MCAST) {
1425 sc->sc_if.if_omcasts++;
1426 dst_ifp->if_omcasts++;
1427 }
1428
1429 if ((dst_ifp->if_flags & IFF_OACTIVE) == 0)
1430 (*dst_ifp->if_start)(dst_ifp);
1431 }
1432
1433 /*
1434 * bridge_output:
1435 *
1436 * Send output from a bridge member interface. This
1437 * performs the bridging function for locally originated
1438 * packets.
1439 *
1440 * The mbuf has the Ethernet header already attached. We must
1441 * enqueue or free the mbuf before returning.
1442 */
1443 int
1444 bridge_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
1445 struct rtentry *rt)
1446 {
1447 struct ether_header *eh;
1448 struct ifnet *dst_if;
1449 struct bridge_softc *sc;
1450 #ifndef BRIDGE_MPSAFE
1451 int s;
1452 #endif
1453
1454 if (m->m_len < ETHER_HDR_LEN) {
1455 m = m_pullup(m, ETHER_HDR_LEN);
1456 if (m == NULL)
1457 return (0);
1458 }
1459
1460 eh = mtod(m, struct ether_header *);
1461 sc = ifp->if_bridge;
1462
1463 #ifndef BRIDGE_MPSAFE
1464 s = splnet();
1465 #endif
1466
1467 /*
1468 * If bridge is down, but the original output interface is up,
1469 * go ahead and send out that interface. Otherwise, the packet
1470 * is dropped below.
1471 */
1472 if (__predict_false(sc == NULL) ||
1473 (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1474 dst_if = ifp;
1475 goto sendunicast;
1476 }
1477
1478 /*
1479 * If the packet is a multicast, or we don't know a better way to
1480 * get there, send to all interfaces.
1481 */
1482 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1483 dst_if = NULL;
1484 else
1485 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1486 if (dst_if == NULL) {
1487 struct bridge_iflist *bif;
1488 struct mbuf *mc;
1489 int used = 0;
1490
1491 BRIDGE_LOCK(sc);
1492
1493 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1494 dst_if = bif->bif_ifp;
1495 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1496 continue;
1497
1498 /*
1499 * If this is not the original output interface,
1500 * and the interface is participating in spanning
1501 * tree, make sure the port is in a state that
1502 * allows forwarding.
1503 */
1504 if (dst_if != ifp &&
1505 (bif->bif_flags & IFBIF_STP) != 0) {
1506 switch (bif->bif_state) {
1507 case BSTP_IFSTATE_BLOCKING:
1508 case BSTP_IFSTATE_LISTENING:
1509 case BSTP_IFSTATE_DISABLED:
1510 continue;
1511 }
1512 }
1513
1514 if (LIST_NEXT(bif, bif_next) == NULL) {
1515 used = 1;
1516 mc = m;
1517 } else {
1518 mc = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1519 if (mc == NULL) {
1520 sc->sc_if.if_oerrors++;
1521 continue;
1522 }
1523 }
1524
1525 bridge_enqueue(sc, dst_if, mc, 0);
1526 }
1527
1528 BRIDGE_UNLOCK(sc);
1529
1530 if (used == 0)
1531 m_freem(m);
1532 #ifndef BRIDGE_MPSAFE
1533 splx(s);
1534 #endif
1535 return (0);
1536 }
1537
1538 sendunicast:
1539 /*
1540 * XXX Spanning tree consideration here?
1541 */
1542
1543 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1544 m_freem(m);
1545 #ifndef BRIDGE_MPSAFE
1546 splx(s);
1547 #endif
1548 return (0);
1549 }
1550
1551 bridge_enqueue(sc, dst_if, m, 0);
1552
1553 #ifndef BRIDGE_MPSAFE
1554 splx(s);
1555 #endif
1556 return (0);
1557 }
1558
1559 /*
1560 * bridge_start:
1561 *
1562 * Start output on a bridge.
1563 *
1564 * NOTE: This routine should never be called in this implementation.
1565 */
1566 static void
1567 bridge_start(struct ifnet *ifp)
1568 {
1569
1570 printf("%s: bridge_start() called\n", ifp->if_xname);
1571 }
1572
1573 /*
1574 * bridge_forward:
1575 *
1576 * The forwarding function of the bridge.
1577 */
1578 static void
1579 bridge_forward(void *v)
1580 {
1581 struct bridge_softc *sc = v;
1582 struct mbuf *m;
1583 struct bridge_iflist *bif;
1584 struct ifnet *src_if, *dst_if;
1585 struct ether_header *eh;
1586 #ifndef BRIDGE_MPSAFE
1587 int s;
1588
1589 KERNEL_LOCK(1, NULL);
1590 mutex_enter(softnet_lock);
1591 #endif
1592
1593 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1594 #ifndef BRIDGE_MPSAFE
1595 mutex_exit(softnet_lock);
1596 KERNEL_UNLOCK_ONE(NULL);
1597 #endif
1598 return;
1599 }
1600
1601 #ifndef BRIDGE_MPSAFE
1602 s = splnet();
1603 #endif
1604 while ((m = pktq_dequeue(sc->sc_fwd_pktq)) != NULL) {
1605 src_if = m->m_pkthdr.rcvif;
1606
1607 sc->sc_if.if_ipackets++;
1608 sc->sc_if.if_ibytes += m->m_pkthdr.len;
1609
1610 /*
1611 * Look up the bridge_iflist.
1612 */
1613 bif = bridge_lookup_member_if(sc, src_if);
1614 if (bif == NULL) {
1615 /* Interface is not a bridge member (anymore?) */
1616 m_freem(m);
1617 continue;
1618 }
1619
1620 if (bif->bif_flags & IFBIF_STP) {
1621 switch (bif->bif_state) {
1622 case BSTP_IFSTATE_BLOCKING:
1623 case BSTP_IFSTATE_LISTENING:
1624 case BSTP_IFSTATE_DISABLED:
1625 m_freem(m);
1626 bridge_release_member(sc, bif);
1627 continue;
1628 }
1629 }
1630
1631 eh = mtod(m, struct ether_header *);
1632
1633 /*
1634 * If the interface is learning, and the source
1635 * address is valid and not multicast, record
1636 * the address.
1637 */
1638 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1639 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1640 (eh->ether_shost[0] == 0 &&
1641 eh->ether_shost[1] == 0 &&
1642 eh->ether_shost[2] == 0 &&
1643 eh->ether_shost[3] == 0 &&
1644 eh->ether_shost[4] == 0 &&
1645 eh->ether_shost[5] == 0) == 0) {
1646 (void) bridge_rtupdate(sc, eh->ether_shost,
1647 src_if, 0, IFBAF_DYNAMIC);
1648 }
1649
1650 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1651 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1652 m_freem(m);
1653 bridge_release_member(sc, bif);
1654 continue;
1655 }
1656
1657 bridge_release_member(sc, bif);
1658
1659 /*
1660 * At this point, the port either doesn't participate
1661 * in spanning tree or it is in the forwarding state.
1662 */
1663
1664 /*
1665 * If the packet is unicast, destined for someone on
1666 * "this" side of the bridge, drop it.
1667 */
1668 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1669 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1670 if (src_if == dst_if) {
1671 m_freem(m);
1672 continue;
1673 }
1674 } else {
1675 /* ...forward it to all interfaces. */
1676 sc->sc_if.if_imcasts++;
1677 dst_if = NULL;
1678 }
1679
1680 if (pfil_run_hooks(sc->sc_if.if_pfil, &m,
1681 m->m_pkthdr.rcvif, PFIL_IN) != 0) {
1682 if (m != NULL)
1683 m_freem(m);
1684 continue;
1685 }
1686 if (m == NULL)
1687 continue;
1688
1689 if (dst_if == NULL) {
1690 bridge_broadcast(sc, src_if, m);
1691 continue;
1692 }
1693
1694 /*
1695 * At this point, we're dealing with a unicast frame
1696 * going to a different interface.
1697 */
1698 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1699 m_freem(m);
1700 continue;
1701 }
1702
1703 bif = bridge_lookup_member_if(sc, dst_if);
1704 if (bif == NULL) {
1705 /* Not a member of the bridge (anymore?) */
1706 m_freem(m);
1707 continue;
1708 }
1709
1710 if (bif->bif_flags & IFBIF_STP) {
1711 switch (bif->bif_state) {
1712 case BSTP_IFSTATE_DISABLED:
1713 case BSTP_IFSTATE_BLOCKING:
1714 m_freem(m);
1715 bridge_release_member(sc, bif);
1716 continue;
1717 }
1718 }
1719
1720 bridge_release_member(sc, bif);
1721
1722 bridge_enqueue(sc, dst_if, m, 1);
1723 }
1724 #ifndef BRIDGE_MPSAFE
1725 splx(s);
1726 mutex_exit(softnet_lock);
1727 KERNEL_UNLOCK_ONE(NULL);
1728 #endif
1729 }
1730
1731 static bool
1732 bstp_state_before_learning(struct bridge_iflist *bif)
1733 {
1734 if (bif->bif_flags & IFBIF_STP) {
1735 switch (bif->bif_state) {
1736 case BSTP_IFSTATE_BLOCKING:
1737 case BSTP_IFSTATE_LISTENING:
1738 case BSTP_IFSTATE_DISABLED:
1739 return true;
1740 }
1741 }
1742 return false;
1743 }
1744
1745 static bool
1746 bridge_ourether(struct bridge_iflist *bif, struct ether_header *eh, int src)
1747 {
1748 uint8_t *ether = src ? eh->ether_shost : eh->ether_dhost;
1749
1750 if (memcmp(CLLADDR(bif->bif_ifp->if_sadl), ether, ETHER_ADDR_LEN) == 0
1751 #if NCARP > 0
1752 || (bif->bif_ifp->if_carp &&
1753 carp_ourether(bif->bif_ifp->if_carp, eh, IFT_ETHER, src) != NULL)
1754 #endif /* NCARP > 0 */
1755 )
1756 return true;
1757
1758 return false;
1759 }
1760
1761 /*
1762 * bridge_input:
1763 *
1764 * Receive input from a member interface. Queue the packet for
1765 * bridging if it is not for us.
1766 */
1767 static void
1768 bridge_input(struct ifnet *ifp, struct mbuf *m)
1769 {
1770 struct bridge_softc *sc = ifp->if_bridge;
1771 struct bridge_iflist *bif;
1772 struct ether_header *eh;
1773
1774 if (__predict_false(sc == NULL) ||
1775 (sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1776 ether_input(ifp, m);
1777 return;
1778 }
1779
1780 bif = bridge_lookup_member_if(sc, ifp);
1781 if (bif == NULL) {
1782 ether_input(ifp, m);
1783 return;
1784 }
1785
1786 eh = mtod(m, struct ether_header *);
1787
1788 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1789 if (memcmp(etherbroadcastaddr,
1790 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
1791 m->m_flags |= M_BCAST;
1792 else
1793 m->m_flags |= M_MCAST;
1794 }
1795
1796 /*
1797 * A 'fast' path for packets addressed to interfaces that are
1798 * part of this bridge.
1799 */
1800 if (!(m->m_flags & (M_BCAST|M_MCAST)) &&
1801 !bstp_state_before_learning(bif)) {
1802 struct bridge_iflist *_bif;
1803
1804 BRIDGE_LOCK(sc);
1805 LIST_FOREACH(_bif, &sc->sc_iflist, bif_next) {
1806 /* It is destined for us. */
1807 if (bridge_ourether(_bif, eh, 0)) {
1808 if (_bif->bif_flags & IFBIF_LEARNING)
1809 (void) bridge_rtupdate(sc,
1810 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1811 m->m_pkthdr.rcvif = _bif->bif_ifp;
1812 ether_input(_bif->bif_ifp, m);
1813 break;
1814 }
1815
1816 /* We just received a packet that we sent out. */
1817 if (bridge_ourether(_bif, eh, 1)) {
1818 m_freem(m);
1819 break;
1820 }
1821 }
1822 BRIDGE_UNLOCK(sc);
1823
1824 if (_bif != NULL) {
1825 bridge_release_member(sc, bif);
1826 return;
1827 }
1828 }
1829
1830 /* Tap off 802.1D packets; they do not get forwarded. */
1831 if (bif->bif_flags & IFBIF_STP &&
1832 memcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN) == 0) {
1833 bstp_input(sc, bif, m);
1834 bridge_release_member(sc, bif);
1835 return;
1836 }
1837
1838 /*
1839 * A normal switch would discard the packet here, but that's not what
1840 * we've done historically. This also prevents some obnoxious behaviour.
1841 */
1842 if (bstp_state_before_learning(bif)) {
1843 ether_input(ifp, m);
1844 bridge_release_member(sc, bif);
1845 return;
1846 }
1847
1848 bridge_release_member(sc, bif);
1849
1850 /* Queue the packet for bridge forwarding. */
1851 if (__predict_false(!pktq_enqueue(sc->sc_fwd_pktq, m, 0)))
1852 m_freem(m);
1853 }
1854
1855 /*
1856 * bridge_broadcast:
1857 *
1858 * Send a frame to all interfaces that are members of
1859 * the bridge, except for the one on which the packet
1860 * arrived.
1861 */
1862 static void
1863 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1864 struct mbuf *m)
1865 {
1866 struct bridge_iflist *bif;
1867 struct mbuf *mc;
1868 struct ifnet *dst_if;
1869 bool used, bmcast;
1870
1871 used = bmcast = m->m_flags & (M_BCAST|M_MCAST);
1872
1873 BRIDGE_LOCK(sc);
1874
1875 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1876 dst_if = bif->bif_ifp;
1877 if (dst_if == src_if)
1878 continue;
1879
1880 if (bif->bif_flags & IFBIF_STP) {
1881 switch (bif->bif_state) {
1882 case BSTP_IFSTATE_BLOCKING:
1883 case BSTP_IFSTATE_DISABLED:
1884 continue;
1885 }
1886 }
1887
1888 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && !bmcast)
1889 continue;
1890
1891 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1892 continue;
1893
1894 if (!used && LIST_NEXT(bif, bif_next) == NULL) {
1895 mc = m;
1896 used = true;
1897 } else {
1898 mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
1899 if (mc == NULL) {
1900 sc->sc_if.if_oerrors++;
1901 continue;
1902 }
1903 }
1904
1905 bridge_enqueue(sc, dst_if, mc, 1);
1906 }
1907
1908 if (bmcast)
1909 ether_input(src_if, m);
1910 else if (!used)
1911 m_freem(m);
1912
1913 BRIDGE_UNLOCK(sc);
1914 }
1915
1916 /*
1917 * bridge_rtupdate:
1918 *
1919 * Add a bridge routing entry.
1920 */
1921 static int
1922 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
1923 struct ifnet *dst_if, int setflags, uint8_t flags)
1924 {
1925 struct bridge_rtnode *brt;
1926 int error = 0;
1927
1928 mutex_enter(sc->sc_rtlist_lock);
1929
1930 /*
1931 * A route for this destination might already exist. If so,
1932 * update it, otherwise create a new one.
1933 */
1934 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
1935 if (sc->sc_brtcnt >= sc->sc_brtmax) {
1936 error = ENOSPC;
1937 goto out;
1938 }
1939
1940 /*
1941 * Allocate a new bridge forwarding node, and
1942 * initialize the expiration time and Ethernet
1943 * address.
1944 */
1945 brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
1946 if (brt == NULL) {
1947 error = ENOMEM;
1948 goto out;
1949 }
1950
1951 memset(brt, 0, sizeof(*brt));
1952 brt->brt_expire = time_uptime + sc->sc_brttimeout;
1953 brt->brt_flags = IFBAF_DYNAMIC;
1954 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
1955
1956 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
1957 pool_put(&bridge_rtnode_pool, brt);
1958 goto out;
1959 }
1960 }
1961
1962 brt->brt_ifp = dst_if;
1963 if (setflags) {
1964 brt->brt_flags = flags;
1965 if (flags & IFBAF_STATIC)
1966 brt->brt_expire = 0;
1967 else
1968 brt->brt_expire = time_uptime + sc->sc_brttimeout;
1969 }
1970
1971 out:
1972 mutex_exit(sc->sc_rtlist_lock);
1973
1974 return error;
1975 }
1976
1977 /*
1978 * bridge_rtlookup:
1979 *
1980 * Lookup the destination interface for an address.
1981 */
1982 static struct ifnet *
1983 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
1984 {
1985 struct bridge_rtnode *brt;
1986 struct ifnet *ifs = NULL;
1987
1988 mutex_enter(sc->sc_rtlist_lock);
1989
1990 brt = bridge_rtnode_lookup(sc, addr);
1991 if (brt != NULL)
1992 ifs = brt->brt_ifp;
1993
1994 mutex_exit(sc->sc_rtlist_lock);
1995
1996 return ifs;
1997 }
1998
1999 /*
2000 * bridge_rttrim:
2001 *
2002 * Trim the routine table so that we have a number
2003 * of routing entries less than or equal to the
2004 * maximum number.
2005 */
2006 static void
2007 bridge_rttrim(struct bridge_softc *sc)
2008 {
2009 struct bridge_rtnode *brt, *nbrt;
2010
2011 mutex_enter(sc->sc_rtlist_lock);
2012
2013 /* Make sure we actually need to do this. */
2014 if (sc->sc_brtcnt <= sc->sc_brtmax)
2015 goto out;
2016
2017 /* Force an aging cycle; this might trim enough addresses. */
2018 bridge_rtage(sc);
2019 if (sc->sc_brtcnt <= sc->sc_brtmax)
2020 goto out;
2021
2022 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2023 nbrt = LIST_NEXT(brt, brt_list);
2024 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2025 bridge_rtnode_destroy(sc, brt);
2026 if (sc->sc_brtcnt <= sc->sc_brtmax)
2027 goto out;
2028 }
2029 }
2030
2031 out:
2032 mutex_exit(sc->sc_rtlist_lock);
2033
2034 return;
2035 }
2036
2037 /*
2038 * bridge_timer:
2039 *
2040 * Aging timer for the bridge.
2041 */
2042 static void
2043 bridge_timer(void *arg)
2044 {
2045 struct bridge_softc *sc = arg;
2046
2047 mutex_enter(sc->sc_rtlist_lock);
2048
2049 bridge_rtage(sc);
2050
2051 if (sc->sc_if.if_flags & IFF_RUNNING)
2052 callout_reset(&sc->sc_brcallout,
2053 bridge_rtable_prune_period * hz, bridge_timer, sc);
2054
2055 mutex_exit(sc->sc_rtlist_lock);
2056 }
2057
2058 /*
2059 * bridge_rtage:
2060 *
2061 * Perform an aging cycle.
2062 */
2063 static void
2064 bridge_rtage(struct bridge_softc *sc)
2065 {
2066 struct bridge_rtnode *brt, *nbrt;
2067
2068 KASSERT(mutex_owned(sc->sc_rtlist_lock));
2069
2070 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2071 nbrt = LIST_NEXT(brt, brt_list);
2072 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2073 if (time_uptime >= brt->brt_expire)
2074 bridge_rtnode_destroy(sc, brt);
2075 }
2076 }
2077 }
2078
2079 /*
2080 * bridge_rtflush:
2081 *
2082 * Remove all dynamic addresses from the bridge.
2083 */
2084 static void
2085 bridge_rtflush(struct bridge_softc *sc, int full)
2086 {
2087 struct bridge_rtnode *brt, *nbrt;
2088
2089 mutex_enter(sc->sc_rtlist_lock);
2090
2091 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2092 nbrt = LIST_NEXT(brt, brt_list);
2093 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2094 bridge_rtnode_destroy(sc, brt);
2095 }
2096
2097 mutex_exit(sc->sc_rtlist_lock);
2098 }
2099
2100 /*
2101 * bridge_rtdaddr:
2102 *
2103 * Remove an address from the table.
2104 */
2105 static int
2106 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2107 {
2108 struct bridge_rtnode *brt;
2109 int error = 0;
2110
2111 mutex_enter(sc->sc_rtlist_lock);
2112
2113 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL) {
2114 error = ENOENT;
2115 goto out;
2116 }
2117
2118 bridge_rtnode_destroy(sc, brt);
2119
2120 out:
2121 mutex_exit(sc->sc_rtlist_lock);
2122
2123 return error;
2124 }
2125
2126 /*
2127 * bridge_rtdelete:
2128 *
2129 * Delete routes to a speicifc member interface.
2130 */
2131 static void
2132 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
2133 {
2134 struct bridge_rtnode *brt, *nbrt;
2135
2136 mutex_enter(sc->sc_rtlist_lock);
2137
2138 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
2139 nbrt = LIST_NEXT(brt, brt_list);
2140 if (brt->brt_ifp == ifp)
2141 bridge_rtnode_destroy(sc, brt);
2142 }
2143
2144 mutex_exit(sc->sc_rtlist_lock);
2145 }
2146
2147 /*
2148 * bridge_rtable_init:
2149 *
2150 * Initialize the route table for this bridge.
2151 */
2152 static int
2153 bridge_rtable_init(struct bridge_softc *sc)
2154 {
2155 int i;
2156
2157 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2158 M_DEVBUF, M_NOWAIT);
2159 if (sc->sc_rthash == NULL)
2160 return (ENOMEM);
2161
2162 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2163 LIST_INIT(&sc->sc_rthash[i]);
2164
2165 sc->sc_rthash_key = cprng_fast32();
2166
2167 LIST_INIT(&sc->sc_rtlist);
2168
2169 sc->sc_rtlist_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
2170
2171 return (0);
2172 }
2173
2174 /*
2175 * bridge_rtable_fini:
2176 *
2177 * Deconstruct the route table for this bridge.
2178 */
2179 static void
2180 bridge_rtable_fini(struct bridge_softc *sc)
2181 {
2182
2183 free(sc->sc_rthash, M_DEVBUF);
2184 if (sc->sc_rtlist_lock)
2185 mutex_obj_free(sc->sc_rtlist_lock);
2186 }
2187
2188 /*
2189 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2190 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2191 */
2192 #define mix(a, b, c) \
2193 do { \
2194 a -= b; a -= c; a ^= (c >> 13); \
2195 b -= c; b -= a; b ^= (a << 8); \
2196 c -= a; c -= b; c ^= (b >> 13); \
2197 a -= b; a -= c; a ^= (c >> 12); \
2198 b -= c; b -= a; b ^= (a << 16); \
2199 c -= a; c -= b; c ^= (b >> 5); \
2200 a -= b; a -= c; a ^= (c >> 3); \
2201 b -= c; b -= a; b ^= (a << 10); \
2202 c -= a; c -= b; c ^= (b >> 15); \
2203 } while (/*CONSTCOND*/0)
2204
2205 static inline uint32_t
2206 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2207 {
2208 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2209
2210 b += addr[5] << 8;
2211 b += addr[4];
2212 a += addr[3] << 24;
2213 a += addr[2] << 16;
2214 a += addr[1] << 8;
2215 a += addr[0];
2216
2217 mix(a, b, c);
2218
2219 return (c & BRIDGE_RTHASH_MASK);
2220 }
2221
2222 #undef mix
2223
2224 /*
2225 * bridge_rtnode_lookup:
2226 *
2227 * Look up a bridge route node for the specified destination.
2228 */
2229 static struct bridge_rtnode *
2230 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2231 {
2232 struct bridge_rtnode *brt;
2233 uint32_t hash;
2234 int dir;
2235
2236 KASSERT(mutex_owned(sc->sc_rtlist_lock));
2237
2238 hash = bridge_rthash(sc, addr);
2239 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2240 dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
2241 if (dir == 0)
2242 return (brt);
2243 if (dir > 0)
2244 return (NULL);
2245 }
2246
2247 return (NULL);
2248 }
2249
2250 /*
2251 * bridge_rtnode_insert:
2252 *
2253 * Insert the specified bridge node into the route table. We
2254 * assume the entry is not already in the table.
2255 */
2256 static int
2257 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2258 {
2259 struct bridge_rtnode *lbrt;
2260 uint32_t hash;
2261 int dir;
2262
2263 KASSERT(mutex_owned(sc->sc_rtlist_lock));
2264
2265 hash = bridge_rthash(sc, brt->brt_addr);
2266
2267 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2268 if (lbrt == NULL) {
2269 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2270 goto out;
2271 }
2272
2273 do {
2274 dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
2275 if (dir == 0)
2276 return (EEXIST);
2277 if (dir > 0) {
2278 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2279 goto out;
2280 }
2281 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2282 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2283 goto out;
2284 }
2285 lbrt = LIST_NEXT(lbrt, brt_hash);
2286 } while (lbrt != NULL);
2287
2288 #ifdef DIAGNOSTIC
2289 panic("bridge_rtnode_insert: impossible");
2290 #endif
2291
2292 out:
2293 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2294 sc->sc_brtcnt++;
2295
2296 return (0);
2297 }
2298
2299 /*
2300 * bridge_rtnode_destroy:
2301 *
2302 * Destroy a bridge rtnode.
2303 */
2304 static void
2305 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2306 {
2307
2308 KASSERT(mutex_owned(sc->sc_rtlist_lock));
2309
2310 LIST_REMOVE(brt, brt_hash);
2311
2312 LIST_REMOVE(brt, brt_list);
2313 sc->sc_brtcnt--;
2314 pool_put(&bridge_rtnode_pool, brt);
2315 }
2316
2317 #if defined(BRIDGE_IPF)
2318 extern pfil_head_t *inet_pfil_hook; /* XXX */
2319 extern pfil_head_t *inet6_pfil_hook; /* XXX */
2320
2321 /*
2322 * Send bridge packets through IPF if they are one of the types IPF can deal
2323 * with, or if they are ARP or REVARP. (IPF will pass ARP and REVARP without
2324 * question.)
2325 */
2326 static int
2327 bridge_ipf(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
2328 {
2329 int snap, error;
2330 struct ether_header *eh1, eh2;
2331 struct llc llc1;
2332 uint16_t ether_type;
2333
2334 snap = 0;
2335 error = -1; /* Default error if not error == 0 */
2336 eh1 = mtod(*mp, struct ether_header *);
2337 ether_type = ntohs(eh1->ether_type);
2338
2339 /*
2340 * Check for SNAP/LLC.
2341 */
2342 if (ether_type < ETHERMTU) {
2343 struct llc *llc2 = (struct llc *)(eh1 + 1);
2344
2345 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2346 llc2->llc_dsap == LLC_SNAP_LSAP &&
2347 llc2->llc_ssap == LLC_SNAP_LSAP &&
2348 llc2->llc_control == LLC_UI) {
2349 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2350 snap = 1;
2351 }
2352 }
2353
2354 /*
2355 * If we're trying to filter bridge traffic, don't look at anything
2356 * other than IP and ARP traffic. If the filter doesn't understand
2357 * IPv6, don't allow IPv6 through the bridge either. This is lame
2358 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2359 * but of course we don't have an AppleTalk filter to begin with.
2360 * (Note that since IPF doesn't understand ARP it will pass *ALL*
2361 * ARP traffic.)
2362 */
2363 switch (ether_type) {
2364 case ETHERTYPE_ARP:
2365 case ETHERTYPE_REVARP:
2366 return 0; /* Automatically pass */
2367 case ETHERTYPE_IP:
2368 # ifdef INET6
2369 case ETHERTYPE_IPV6:
2370 # endif /* INET6 */
2371 break;
2372 default:
2373 goto bad;
2374 }
2375
2376 /* Strip off the Ethernet header and keep a copy. */
2377 m_copydata(*mp, 0, ETHER_HDR_LEN, (void *) &eh2);
2378 m_adj(*mp, ETHER_HDR_LEN);
2379
2380 /* Strip off snap header, if present */
2381 if (snap) {
2382 m_copydata(*mp, 0, sizeof(struct llc), (void *) &llc1);
2383 m_adj(*mp, sizeof(struct llc));
2384 }
2385
2386 /*
2387 * Check basic packet sanity and run IPF through pfil.
2388 */
2389 KASSERT(!cpu_intr_p());
2390 switch (ether_type)
2391 {
2392 case ETHERTYPE_IP :
2393 error = (dir == PFIL_IN) ? bridge_ip_checkbasic(mp) : 0;
2394 if (error == 0)
2395 error = pfil_run_hooks(inet_pfil_hook, mp, ifp, dir);
2396 break;
2397 # ifdef INET6
2398 case ETHERTYPE_IPV6 :
2399 error = (dir == PFIL_IN) ? bridge_ip6_checkbasic(mp) : 0;
2400 if (error == 0)
2401 error = pfil_run_hooks(inet6_pfil_hook, mp, ifp, dir);
2402 break;
2403 # endif
2404 default :
2405 error = 0;
2406 break;
2407 }
2408
2409 if (*mp == NULL)
2410 return error;
2411 if (error != 0)
2412 goto bad;
2413
2414 error = -1;
2415
2416 /*
2417 * Finally, put everything back the way it was and return
2418 */
2419 if (snap) {
2420 M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
2421 if (*mp == NULL)
2422 return error;
2423 bcopy(&llc1, mtod(*mp, void *), sizeof(struct llc));
2424 }
2425
2426 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
2427 if (*mp == NULL)
2428 return error;
2429 bcopy(&eh2, mtod(*mp, void *), ETHER_HDR_LEN);
2430
2431 return 0;
2432
2433 bad:
2434 m_freem(*mp);
2435 *mp = NULL;
2436 return error;
2437 }
2438
2439 /*
2440 * Perform basic checks on header size since
2441 * IPF assumes ip_input has already processed
2442 * it for it. Cut-and-pasted from ip_input.c.
2443 * Given how simple the IPv6 version is,
2444 * does the IPv4 version really need to be
2445 * this complicated?
2446 *
2447 * XXX Should we update ipstat here, or not?
2448 * XXX Right now we update ipstat but not
2449 * XXX csum_counter.
2450 */
2451 static int
2452 bridge_ip_checkbasic(struct mbuf **mp)
2453 {
2454 struct mbuf *m = *mp;
2455 struct ip *ip;
2456 int len, hlen;
2457
2458 if (*mp == NULL)
2459 return -1;
2460
2461 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
2462 if ((m = m_copyup(m, sizeof(struct ip),
2463 (max_linkhdr + 3) & ~3)) == NULL) {
2464 /* XXXJRT new stat, please */
2465 ip_statinc(IP_STAT_TOOSMALL);
2466 goto bad;
2467 }
2468 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
2469 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2470 ip_statinc(IP_STAT_TOOSMALL);
2471 goto bad;
2472 }
2473 }
2474 ip = mtod(m, struct ip *);
2475 if (ip == NULL) goto bad;
2476
2477 if (ip->ip_v != IPVERSION) {
2478 ip_statinc(IP_STAT_BADVERS);
2479 goto bad;
2480 }
2481 hlen = ip->ip_hl << 2;
2482 if (hlen < sizeof(struct ip)) { /* minimum header length */
2483 ip_statinc(IP_STAT_BADHLEN);
2484 goto bad;
2485 }
2486 if (hlen > m->m_len) {
2487 if ((m = m_pullup(m, hlen)) == 0) {
2488 ip_statinc(IP_STAT_BADHLEN);
2489 goto bad;
2490 }
2491 ip = mtod(m, struct ip *);
2492 if (ip == NULL) goto bad;
2493 }
2494
2495 switch (m->m_pkthdr.csum_flags &
2496 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_IPv4) |
2497 M_CSUM_IPv4_BAD)) {
2498 case M_CSUM_IPv4|M_CSUM_IPv4_BAD:
2499 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_bad); */
2500 goto bad;
2501
2502 case M_CSUM_IPv4:
2503 /* Checksum was okay. */
2504 /* INET_CSUM_COUNTER_INCR(&ip_hwcsum_ok); */
2505 break;
2506
2507 default:
2508 /* Must compute it ourselves. */
2509 /* INET_CSUM_COUNTER_INCR(&ip_swcsum); */
2510 if (in_cksum(m, hlen) != 0)
2511 goto bad;
2512 break;
2513 }
2514
2515 /* Retrieve the packet length. */
2516 len = ntohs(ip->ip_len);
2517
2518 /*
2519 * Check for additional length bogosity
2520 */
2521 if (len < hlen) {
2522 ip_statinc(IP_STAT_BADLEN);
2523 goto bad;
2524 }
2525
2526 /*
2527 * Check that the amount of data in the buffers
2528 * is as at least much as the IP header would have us expect.
2529 * Drop packet if shorter than we expect.
2530 */
2531 if (m->m_pkthdr.len < len) {
2532 ip_statinc(IP_STAT_TOOSHORT);
2533 goto bad;
2534 }
2535
2536 /* Checks out, proceed */
2537 *mp = m;
2538 return 0;
2539
2540 bad:
2541 *mp = m;
2542 return -1;
2543 }
2544
2545 # ifdef INET6
2546 /*
2547 * Same as above, but for IPv6.
2548 * Cut-and-pasted from ip6_input.c.
2549 * XXX Should we update ip6stat, or not?
2550 */
2551 static int
2552 bridge_ip6_checkbasic(struct mbuf **mp)
2553 {
2554 struct mbuf *m = *mp;
2555 struct ip6_hdr *ip6;
2556
2557 /*
2558 * If the IPv6 header is not aligned, slurp it up into a new
2559 * mbuf with space for link headers, in the event we forward
2560 * it. Otherwise, if it is aligned, make sure the entire base
2561 * IPv6 header is in the first mbuf of the chain.
2562 */
2563 if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
2564 struct ifnet *inifp = m->m_pkthdr.rcvif;
2565 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2566 (max_linkhdr + 3) & ~3)) == NULL) {
2567 /* XXXJRT new stat, please */
2568 ip6_statinc(IP6_STAT_TOOSMALL);
2569 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2570 goto bad;
2571 }
2572 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2573 struct ifnet *inifp = m->m_pkthdr.rcvif;
2574 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2575 ip6_statinc(IP6_STAT_TOOSMALL);
2576 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2577 goto bad;
2578 }
2579 }
2580
2581 ip6 = mtod(m, struct ip6_hdr *);
2582
2583 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2584 ip6_statinc(IP6_STAT_BADVERS);
2585 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2586 goto bad;
2587 }
2588
2589 /* Checks out, proceed */
2590 *mp = m;
2591 return 0;
2592
2593 bad:
2594 *mp = m;
2595 return -1;
2596 }
2597 # endif /* INET6 */
2598 #endif /* BRIDGE_IPF */
2599