if_bridge.c revision 1.5.4.2 1 /* $NetBSD: if_bridge.c,v 1.5.4.2 2003/06/30 03:13:39 grant 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 * - Add packet filter hooks.
82 */
83
84 #include <sys/cdefs.h>
85 __KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.5.4.2 2003/06/30 03:13:39 grant Exp $");
86
87 #include "bpfilter.h"
88 #include "rnd.h"
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/sockio.h>
96 #include <sys/systm.h>
97 #include <sys/proc.h>
98 #include <sys/pool.h>
99
100 #if NRND > 0
101 #include <sys/rnd.h>
102 #endif
103
104 #if NBPFILTER > 0
105 #include <net/bpf.h>
106 #endif
107 #include <net/if.h>
108 #include <net/if_dl.h>
109 #include <net/if_types.h>
110 #include <net/if_llc.h>
111
112 #include <net/if_ether.h>
113 #include <net/if_bridgevar.h>
114
115 /*
116 * Size of the route hash table. Must be a power of two.
117 */
118 #ifndef BRIDGE_RTHASH_SIZE
119 #define BRIDGE_RTHASH_SIZE 1024
120 #endif
121
122 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
123
124 /*
125 * Maximum number of addresses to cache.
126 */
127 #ifndef BRIDGE_RTABLE_MAX
128 #define BRIDGE_RTABLE_MAX 100
129 #endif
130
131 /*
132 * Spanning tree defaults.
133 */
134 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
135 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
136 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
137 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
138 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
139 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
140 #define BSTP_DEFAULT_PATH_COST 55
141
142 /*
143 * Timeout (in seconds) for entries learned dynamically.
144 */
145 #ifndef BRIDGE_RTABLE_TIMEOUT
146 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
147 #endif
148
149 /*
150 * Number of seconds between walks of the route list.
151 */
152 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
153 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
154 #endif
155
156 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
157
158 struct pool bridge_rtnode_pool;
159
160 void bridgeattach(int);
161
162 int bridge_clone_create(struct if_clone *, int);
163 void bridge_clone_destroy(struct ifnet *);
164
165 int bridge_ioctl(struct ifnet *, u_long, caddr_t);
166 int bridge_init(struct ifnet *);
167 void bridge_stop(struct ifnet *, int);
168 void bridge_start(struct ifnet *);
169
170 void bridge_forward(struct bridge_softc *, struct mbuf *m);
171
172 void bridge_timer(void *);
173
174 void bridge_broadcast(struct bridge_softc *, struct ifnet *, struct mbuf *);
175
176 int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
177 struct ifnet *, int, uint8_t);
178 struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
179 void bridge_rttrim(struct bridge_softc *);
180 void bridge_rtage(struct bridge_softc *);
181 void bridge_rtflush(struct bridge_softc *, int);
182 int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
183 void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp);
184
185 int bridge_rtable_init(struct bridge_softc *);
186 void bridge_rtable_fini(struct bridge_softc *);
187
188 struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
189 const uint8_t *);
190 int bridge_rtnode_insert(struct bridge_softc *, struct bridge_rtnode *);
191 void bridge_rtnode_destroy(struct bridge_softc *, struct bridge_rtnode *);
192
193 struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
194 const char *name);
195 void bridge_delete_member(struct bridge_softc *, struct bridge_iflist *);
196
197 int bridge_ioctl_add(struct bridge_softc *, void *);
198 int bridge_ioctl_del(struct bridge_softc *, void *);
199 int bridge_ioctl_gifflags(struct bridge_softc *, void *);
200 int bridge_ioctl_sifflags(struct bridge_softc *, void *);
201 int bridge_ioctl_scache(struct bridge_softc *, void *);
202 int bridge_ioctl_gcache(struct bridge_softc *, void *);
203 int bridge_ioctl_gifs(struct bridge_softc *, void *);
204 int bridge_ioctl_rts(struct bridge_softc *, void *);
205 int bridge_ioctl_saddr(struct bridge_softc *, void *);
206 int bridge_ioctl_sto(struct bridge_softc *, void *);
207 int bridge_ioctl_gto(struct bridge_softc *, void *);
208 int bridge_ioctl_daddr(struct bridge_softc *, void *);
209 int bridge_ioctl_flush(struct bridge_softc *, void *);
210 int bridge_ioctl_gpri(struct bridge_softc *, void *);
211 int bridge_ioctl_spri(struct bridge_softc *, void *);
212 int bridge_ioctl_ght(struct bridge_softc *, void *);
213 int bridge_ioctl_sht(struct bridge_softc *, void *);
214 int bridge_ioctl_gfd(struct bridge_softc *, void *);
215 int bridge_ioctl_sfd(struct bridge_softc *, void *);
216 int bridge_ioctl_gma(struct bridge_softc *, void *);
217 int bridge_ioctl_sma(struct bridge_softc *, void *);
218 int bridge_ioctl_sifprio(struct bridge_softc *, void *);
219 int bridge_ioctl_sifcost(struct bridge_softc *, void *);
220
221 struct bridge_control {
222 int (*bc_func)(struct bridge_softc *, void *);
223 int bc_argsize;
224 int bc_flags;
225 };
226
227 #define BC_F_COPYIN 0x01 /* copy arguments in */
228 #define BC_F_COPYOUT 0x02 /* copy arguments out */
229 #define BC_F_SUSER 0x04 /* do super-user check */
230
231 const struct bridge_control bridge_control_table[] = {
232 { bridge_ioctl_add, sizeof(struct ifbreq),
233 BC_F_COPYIN|BC_F_SUSER },
234 { bridge_ioctl_del, sizeof(struct ifbreq),
235 BC_F_COPYIN|BC_F_SUSER },
236
237 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
238 BC_F_COPYIN|BC_F_COPYOUT },
239 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
240 BC_F_COPYIN|BC_F_SUSER },
241
242 { bridge_ioctl_scache, sizeof(struct ifbrparam),
243 BC_F_COPYIN|BC_F_SUSER },
244 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
245 BC_F_COPYOUT },
246
247 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
248 BC_F_COPYIN|BC_F_COPYOUT },
249 { bridge_ioctl_rts, sizeof(struct ifbaconf),
250 BC_F_COPYIN|BC_F_COPYOUT },
251
252 { bridge_ioctl_saddr, sizeof(struct ifbareq),
253 BC_F_COPYIN|BC_F_SUSER },
254
255 { bridge_ioctl_sto, sizeof(struct ifbrparam),
256 BC_F_COPYIN|BC_F_SUSER },
257 { bridge_ioctl_gto, sizeof(struct ifbrparam),
258 BC_F_COPYOUT },
259
260 { bridge_ioctl_daddr, sizeof(struct ifbareq),
261 BC_F_COPYIN|BC_F_SUSER },
262
263 { bridge_ioctl_flush, sizeof(struct ifbreq),
264 BC_F_COPYIN|BC_F_SUSER },
265
266 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
267 BC_F_COPYOUT },
268 { bridge_ioctl_spri, sizeof(struct ifbrparam),
269 BC_F_COPYIN|BC_F_SUSER },
270
271 { bridge_ioctl_ght, sizeof(struct ifbrparam),
272 BC_F_COPYOUT },
273 { bridge_ioctl_sht, sizeof(struct ifbrparam),
274 BC_F_COPYIN|BC_F_SUSER },
275
276 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
277 BC_F_COPYOUT },
278 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
279 BC_F_COPYIN|BC_F_SUSER },
280
281 { bridge_ioctl_gma, sizeof(struct ifbrparam),
282 BC_F_COPYOUT },
283 { bridge_ioctl_sma, sizeof(struct ifbrparam),
284 BC_F_COPYIN|BC_F_SUSER },
285
286 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
287 BC_F_COPYIN|BC_F_SUSER },
288
289 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
290 BC_F_COPYIN|BC_F_SUSER },
291 };
292 const int bridge_control_table_size =
293 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
294
295 LIST_HEAD(, bridge_softc) bridge_list;
296
297 struct if_clone bridge_cloner =
298 IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy);
299
300 /*
301 * bridgeattach:
302 *
303 * Pseudo-device attach routine.
304 */
305 void
306 bridgeattach(int n)
307 {
308
309 pool_init(&bridge_rtnode_pool, sizeof(struct bridge_rtnode),
310 0, 0, 0, "brtpl", NULL);
311
312 LIST_INIT(&bridge_list);
313 if_clone_attach(&bridge_cloner);
314 }
315
316 /*
317 * bridge_clone_create:
318 *
319 * Create a new bridge instance.
320 */
321 int
322 bridge_clone_create(struct if_clone *ifc, int unit)
323 {
324 struct bridge_softc *sc;
325 struct ifnet *ifp;
326 int s;
327
328 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK);
329 memset(sc, 0, sizeof(*sc));
330 ifp = &sc->sc_if;
331
332 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
333 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
334 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
335 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
336 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
337 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
338 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
339
340 /* Initialize our routing table. */
341 bridge_rtable_init(sc);
342
343 callout_init(&sc->sc_brcallout);
344 callout_init(&sc->sc_bstpcallout);
345
346 LIST_INIT(&sc->sc_iflist);
347
348 sprintf(ifp->if_xname, "%s%d", ifc->ifc_name, unit);
349 ifp->if_softc = sc;
350 ifp->if_mtu = ETHERMTU;
351 ifp->if_ioctl = bridge_ioctl;
352 ifp->if_output = bridge_output;
353 ifp->if_start = bridge_start;
354 ifp->if_stop = bridge_stop;
355 ifp->if_init = bridge_init;
356 ifp->if_type = IFT_PROPVIRTUAL; /* XXX IFT_BRIDGE */
357 ifp->if_addrlen = 0;
358 ifp->if_dlt = DLT_EN10MB;
359 ifp->if_hdrlen = ETHER_HDR_LEN;
360
361 if_attach(ifp);
362
363 if_alloc_sadl(ifp);
364
365 s = splnet();
366 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
367 splx(s);
368
369 return (0);
370 }
371
372 /*
373 * bridge_clone_destroy:
374 *
375 * Destroy a bridge instance.
376 */
377 void
378 bridge_clone_destroy(struct ifnet *ifp)
379 {
380 struct bridge_softc *sc = ifp->if_softc;
381 struct bridge_iflist *bif;
382 int s;
383
384 s = splnet();
385
386 bridge_stop(ifp, 1);
387
388 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
389 bridge_delete_member(sc, bif);
390
391 LIST_REMOVE(sc, sc_list);
392
393 splx(s);
394
395 if_detach(ifp);
396
397 /* Tear down the routing table. */
398 bridge_rtable_fini(sc);
399
400 free(sc, M_DEVBUF);
401 }
402
403 /*
404 * bridge_ioctl:
405 *
406 * Handle a control request from the operator.
407 */
408 int
409 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
410 {
411 struct bridge_softc *sc = ifp->if_softc;
412 struct proc *p = curproc; /* XXX */
413 union {
414 struct ifbreq ifbreq;
415 struct ifbifconf ifbifconf;
416 struct ifbareq ifbareq;
417 struct ifbaconf ifbaconf;
418 struct ifbrparam ifbrparam;
419 } args;
420 struct ifdrv *ifd = (struct ifdrv *) data;
421 const struct bridge_control *bc;
422 int s, error = 0;
423
424 s = splnet();
425
426 switch (cmd) {
427 case SIOCGDRVSPEC:
428 case SIOCSDRVSPEC:
429 if (ifd->ifd_cmd >= bridge_control_table_size) {
430 error = EINVAL;
431 break;
432 }
433 bc = &bridge_control_table[ifd->ifd_cmd];
434
435 if (cmd == SIOCGDRVSPEC &&
436 (bc->bc_flags & BC_F_COPYOUT) == 0)
437 return (EINVAL);
438 else if (cmd == SIOCSDRVSPEC &&
439 (bc->bc_flags & BC_F_COPYOUT) != 0)
440 return (EINVAL);
441
442 if (bc->bc_flags & BC_F_SUSER) {
443 error = suser(p->p_ucred, &p->p_acflag);
444 if (error)
445 break;
446 }
447
448 if (ifd->ifd_len != bc->bc_argsize ||
449 ifd->ifd_len > sizeof(args)) {
450 error = EINVAL;
451 break;
452 }
453
454 if (bc->bc_flags & BC_F_COPYIN) {
455 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
456 if (error)
457 break;
458 }
459
460 error = (*bc->bc_func)(sc, &args);
461 if (error)
462 break;
463
464 if (bc->bc_flags & BC_F_COPYOUT)
465 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
466
467 break;
468
469 case SIOCSIFFLAGS:
470 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
471 /*
472 * If interface is marked down and it is running,
473 * then stop and disable it.
474 */
475 (*ifp->if_stop)(ifp, 1);
476 } else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
477 /*
478 * If interface is marked up and it is stopped, then
479 * start it.
480 */
481 error = (*ifp->if_init)(ifp);
482 }
483 break;
484
485 default:
486 error = ENOTTY;
487 break;
488 }
489
490 splx(s);
491
492 return (error);
493 }
494
495 /*
496 * bridge_lookup_member:
497 *
498 * Lookup a bridge member interface. Must be called at splnet().
499 */
500 struct bridge_iflist *
501 bridge_lookup_member(struct bridge_softc *sc, const char *name)
502 {
503 struct bridge_iflist *bif;
504 struct ifnet *ifp;
505
506 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
507 ifp = bif->bif_ifp;
508 if (strcmp(ifp->if_xname, name) == 0)
509 return (bif);
510 }
511
512 return (NULL);
513 }
514
515 /*
516 * bridge_delete_member:
517 *
518 * Delete the specified member interface.
519 */
520 void
521 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif)
522 {
523 struct ifnet *ifs = bif->bif_ifp;
524
525 switch (ifs->if_type) {
526 case IFT_ETHER:
527 /*
528 * Take the interface out of promiscuous mode.
529 */
530 (void) ifpromisc(ifs, 0);
531 break;
532
533 default:
534 #ifdef DIAGNOSTIC
535 panic("bridge_delete_member: impossible");
536 #endif
537 break;
538 }
539
540 ifs->if_bridge = NULL;
541 LIST_REMOVE(bif, bif_next);
542
543 bridge_rtdelete(sc, ifs);
544
545 free(bif, M_DEVBUF);
546
547 if (sc->sc_if.if_flags & IFF_RUNNING)
548 bstp_initialization(sc);
549 }
550
551 int
552 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
553 {
554 struct ifbreq *req = arg;
555 struct bridge_iflist *bif = NULL;
556 struct ifnet *ifs;
557 int error = 0;
558
559 ifs = ifunit(req->ifbr_ifsname);
560 if (ifs == NULL)
561 return (ENOENT);
562
563 if (sc->sc_if.if_mtu != ifs->if_mtu)
564 return (EINVAL);
565
566 if (ifs->if_bridge == sc)
567 return (EEXIST);
568
569 if (ifs->if_bridge != NULL)
570 return (EBUSY);
571
572 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT);
573 if (bif == NULL)
574 return (ENOMEM);
575
576 switch (ifs->if_type) {
577 case IFT_ETHER:
578 /*
579 * Place the interface into promiscuous mode.
580 */
581 error = ifpromisc(ifs, 1);
582 if (error)
583 goto out;
584 break;
585
586 default:
587 error = EINVAL;
588 goto out;
589 }
590
591 bif->bif_ifp = ifs;
592 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
593 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
594 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
595
596 ifs->if_bridge = sc;
597 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
598
599 if (sc->sc_if.if_flags & IFF_RUNNING)
600 bstp_initialization(sc);
601 else
602 bstp_stop(sc);
603
604 out:
605 if (error) {
606 if (bif != NULL)
607 free(bif, M_DEVBUF);
608 }
609 return (error);
610 }
611
612 int
613 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
614 {
615 struct ifbreq *req = arg;
616 struct bridge_iflist *bif;
617
618 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
619 if (bif == NULL)
620 return (ENOENT);
621
622 bridge_delete_member(sc, bif);
623
624 return (0);
625 }
626
627 int
628 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
629 {
630 struct ifbreq *req = arg;
631 struct bridge_iflist *bif;
632
633 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
634 if (bif == NULL)
635 return (ENOENT);
636
637 req->ifbr_ifsflags = bif->bif_flags;
638 req->ifbr_state = bif->bif_state;
639 req->ifbr_priority = bif->bif_priority;
640 req->ifbr_path_cost = bif->bif_path_cost;
641 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
642
643 return (0);
644 }
645
646 int
647 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
648 {
649 struct ifbreq *req = arg;
650 struct bridge_iflist *bif;
651
652 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
653 if (bif == NULL)
654 return (ENOENT);
655
656 if (req->ifbr_ifsflags & IFBIF_STP) {
657 switch (bif->bif_ifp->if_type) {
658 case IFT_ETHER:
659 /* These can do spanning tree. */
660 break;
661
662 default:
663 /* Nothing else can. */
664 return (EINVAL);
665 }
666 }
667
668 bif->bif_flags = req->ifbr_ifsflags;
669
670 if (sc->sc_if.if_flags & IFF_RUNNING)
671 bstp_initialization(sc);
672
673 return (0);
674 }
675
676 int
677 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
678 {
679 struct ifbrparam *param = arg;
680
681 sc->sc_brtmax = param->ifbrp_csize;
682 bridge_rttrim(sc);
683
684 return (0);
685 }
686
687 int
688 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
689 {
690 struct ifbrparam *param = arg;
691
692 param->ifbrp_csize = sc->sc_brtmax;
693
694 return (0);
695 }
696
697 int
698 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
699 {
700 struct ifbifconf *bifc = arg;
701 struct bridge_iflist *bif;
702 struct ifbreq breq;
703 int count, len, error = 0;
704
705 count = 0;
706 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
707 count++;
708
709 if (bifc->ifbic_len == 0) {
710 bifc->ifbic_len = sizeof(breq) * count;
711 return (0);
712 }
713
714 count = 0;
715 len = bifc->ifbic_len;
716 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
717 if (len < sizeof(breq))
718 break;
719
720 strcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname);
721 breq.ifbr_ifsflags = bif->bif_flags;
722 breq.ifbr_state = bif->bif_state;
723 breq.ifbr_priority = bif->bif_priority;
724 breq.ifbr_path_cost = bif->bif_path_cost;
725 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
726 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
727 if (error)
728 break;
729 count++;
730 len -= sizeof(breq);
731 }
732
733 bifc->ifbic_len = sizeof(breq) * count;
734 return (error);
735 }
736
737 int
738 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
739 {
740 struct ifbaconf *bac = arg;
741 struct bridge_rtnode *brt;
742 struct ifbareq bareq;
743 int count = 0, error = 0, len;
744
745 if (bac->ifbac_len == 0)
746 return (0);
747
748 len = bac->ifbac_len;
749 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
750 if (len < sizeof(bareq))
751 goto out;
752 strcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname);
753 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
754 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
755 bareq.ifba_expire = brt->brt_expire - mono_time.tv_sec;
756 else
757 bareq.ifba_expire = 0;
758 bareq.ifba_flags = brt->brt_flags;
759
760 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
761 if (error)
762 goto out;
763 count++;
764 len -= sizeof(bareq);
765 }
766 out:
767 bac->ifbac_len = sizeof(bareq) * count;
768 return (error);
769 }
770
771 int
772 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
773 {
774 struct ifbareq *req = arg;
775 struct bridge_iflist *bif;
776 int error;
777
778 bif = bridge_lookup_member(sc, req->ifba_ifsname);
779 if (bif == NULL)
780 return (ENOENT);
781
782 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
783 req->ifba_flags);
784
785 return (error);
786 }
787
788 int
789 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
790 {
791 struct ifbrparam *param = arg;
792
793 sc->sc_brttimeout = param->ifbrp_ctime;
794
795 return (0);
796 }
797
798 int
799 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
800 {
801 struct ifbrparam *param = arg;
802
803 param->ifbrp_ctime = sc->sc_brttimeout;
804
805 return (0);
806 }
807
808 int
809 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
810 {
811 struct ifbareq *req = arg;
812
813 return (bridge_rtdaddr(sc, req->ifba_dst));
814 }
815
816 int
817 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
818 {
819 struct ifbreq *req = arg;
820
821 bridge_rtflush(sc, req->ifbr_ifsflags);
822
823 return (0);
824 }
825
826 int
827 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
828 {
829 struct ifbrparam *param = arg;
830
831 param->ifbrp_prio = sc->sc_bridge_priority;
832
833 return (0);
834 }
835
836 int
837 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
838 {
839 struct ifbrparam *param = arg;
840
841 sc->sc_bridge_priority = param->ifbrp_prio;
842
843 if (sc->sc_if.if_flags & IFF_RUNNING)
844 bstp_initialization(sc);
845
846 return (0);
847 }
848
849 int
850 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
851 {
852 struct ifbrparam *param = arg;
853
854 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
855
856 return (0);
857 }
858
859 int
860 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
861 {
862 struct ifbrparam *param = arg;
863
864 if (param->ifbrp_hellotime == 0)
865 return (EINVAL);
866 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
867
868 if (sc->sc_if.if_flags & IFF_RUNNING)
869 bstp_initialization(sc);
870
871 return (0);
872 }
873
874 int
875 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
876 {
877 struct ifbrparam *param = arg;
878
879 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
880
881 return (0);
882 }
883
884 int
885 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
886 {
887 struct ifbrparam *param = arg;
888
889 if (param->ifbrp_fwddelay == 0)
890 return (EINVAL);
891 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
892
893 if (sc->sc_if.if_flags & IFF_RUNNING)
894 bstp_initialization(sc);
895
896 return (0);
897 }
898
899 int
900 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
901 {
902 struct ifbrparam *param = arg;
903
904 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
905
906 return (0);
907 }
908
909 int
910 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
911 {
912 struct ifbrparam *param = arg;
913
914 if (param->ifbrp_maxage == 0)
915 return (EINVAL);
916 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
917
918 if (sc->sc_if.if_flags & IFF_RUNNING)
919 bstp_initialization(sc);
920
921 return (0);
922 }
923
924 int
925 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
926 {
927 struct ifbreq *req = arg;
928 struct bridge_iflist *bif;
929
930 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
931 if (bif == NULL)
932 return (ENOENT);
933
934 bif->bif_priority = req->ifbr_priority;
935
936 if (sc->sc_if.if_flags & IFF_RUNNING)
937 bstp_initialization(sc);
938
939 return (0);
940 }
941
942 int
943 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
944 {
945 struct ifbreq *req = arg;
946 struct bridge_iflist *bif;
947
948 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
949 if (bif == NULL)
950 return (ENOENT);
951
952 bif->bif_path_cost = req->ifbr_path_cost;
953
954 if (sc->sc_if.if_flags & IFF_RUNNING)
955 bstp_initialization(sc);
956
957 return (0);
958 }
959
960 /*
961 * bridge_ifdetach:
962 *
963 * Detach an interface from a bridge. Called when a member
964 * interface is detaching.
965 */
966 void
967 bridge_ifdetach(struct ifnet *ifp)
968 {
969 struct bridge_softc *sc = ifp->if_bridge;
970 struct ifbreq breq;
971
972 memset(&breq, 0, sizeof(breq));
973 sprintf(breq.ifbr_ifsname, ifp->if_xname);
974
975 (void) bridge_ioctl_del(sc, &breq);
976 }
977
978 /*
979 * bridge_init:
980 *
981 * Initialize a bridge interface.
982 */
983 int
984 bridge_init(struct ifnet *ifp)
985 {
986 struct bridge_softc *sc = ifp->if_softc;
987
988 if (ifp->if_flags & IFF_RUNNING)
989 return (0);
990
991 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
992 bridge_timer, sc);
993
994 ifp->if_flags |= IFF_RUNNING;
995 bstp_initialization(sc);
996 return (0);
997 }
998
999 /*
1000 * bridge_stop:
1001 *
1002 * Stop the bridge interface.
1003 */
1004 void
1005 bridge_stop(struct ifnet *ifp, int disable)
1006 {
1007 struct bridge_softc *sc = ifp->if_softc;
1008
1009 if ((ifp->if_flags & IFF_RUNNING) == 0)
1010 return;
1011
1012 callout_stop(&sc->sc_brcallout);
1013 bstp_stop(sc);
1014
1015 IF_PURGE(&ifp->if_snd);
1016
1017 bridge_rtflush(sc, IFBF_FLUSHDYN);
1018
1019 ifp->if_flags &= ~IFF_RUNNING;
1020 }
1021
1022 /*
1023 * bridge_enqueue:
1024 *
1025 * Enqueue a packet on a bridge member interface.
1026 *
1027 * NOTE: must be called at splnet().
1028 */
1029 __inline void
1030 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1031 {
1032 ALTQ_DECL(struct altq_pktattr pktattr;)
1033 int len, error;
1034 short mflags;
1035
1036 #ifdef ALTQ
1037 /*
1038 * If ALTQ is enabled on the member interface, do
1039 * classification; the queueing discipline might
1040 * not require classification, but might require
1041 * the address family/header pointer in the pktattr.
1042 */
1043 if (ALTQ_IS_ENABLED(&dst_ifp->if_snd)) {
1044 /* XXX IFT_ETHER */
1045 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
1046 }
1047 #endif /* ALTQ */
1048
1049 len = m->m_pkthdr.len;
1050 mflags = m->m_flags;
1051 IFQ_ENQUEUE(&dst_ifp->if_snd, m, &pktattr, error);
1052 if (error) {
1053 /* mbuf is already freed */
1054 sc->sc_if.if_oerrors++;
1055 return;
1056 }
1057
1058 sc->sc_if.if_opackets++;
1059 sc->sc_if.if_obytes += len;
1060
1061 dst_ifp->if_obytes += len;
1062
1063 if (mflags & M_MCAST) {
1064 sc->sc_if.if_omcasts++;
1065 dst_ifp->if_omcasts++;
1066 }
1067
1068 if ((dst_ifp->if_flags & IFF_OACTIVE) == 0)
1069 (*dst_ifp->if_start)(dst_ifp);
1070 }
1071
1072 /*
1073 * bridge_output:
1074 *
1075 * Send output from a bridge member interface. This
1076 * performs the bridging function for locally originated
1077 * packets.
1078 *
1079 * The mbuf has the Ethernet header already attached. We must
1080 * enqueue or free the mbuf before returning.
1081 */
1082 int
1083 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1084 struct rtentry *rt)
1085 {
1086 struct ether_header *eh;
1087 struct ifnet *dst_if;
1088 struct bridge_softc *sc;
1089 int s;
1090
1091 if (m->m_len < ETHER_HDR_LEN) {
1092 m = m_pullup(m, ETHER_HDR_LEN);
1093 if (m == NULL)
1094 return (0);
1095 }
1096
1097 eh = mtod(m, struct ether_header *);
1098 sc = ifp->if_bridge;
1099
1100 s = splnet();
1101
1102 /*
1103 * If bridge is down, but the original output interface is up,
1104 * go ahead and send out that interface. Otherwise, the packet
1105 * is dropped below.
1106 */
1107 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
1108 dst_if = ifp;
1109 goto sendunicast;
1110 }
1111
1112 /*
1113 * If the packet is a multicast, or we don't know a better way to
1114 * get there, send to all interfaces.
1115 */
1116 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1117 dst_if = NULL;
1118 else
1119 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1120 if (dst_if == NULL) {
1121 struct bridge_iflist *bif;
1122 struct mbuf *mc;
1123 int used = 0;
1124
1125 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1126 dst_if = bif->bif_ifp;
1127 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1128 continue;
1129
1130 /*
1131 * If this is not the original output interface,
1132 * and the interface is participating in spanning
1133 * tree, make sure the port is in a state that
1134 * allows forwarding.
1135 */
1136 if (dst_if != ifp &&
1137 (bif->bif_flags & IFBIF_STP) != 0) {
1138 switch (bif->bif_state) {
1139 case BSTP_IFSTATE_BLOCKING:
1140 case BSTP_IFSTATE_LISTENING:
1141 case BSTP_IFSTATE_DISABLED:
1142 continue;
1143 }
1144 }
1145
1146 if (LIST_NEXT(bif, bif_next) == NULL) {
1147 used = 1;
1148 mc = m;
1149 } else {
1150 mc = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1151 if (mc == NULL) {
1152 sc->sc_if.if_oerrors++;
1153 continue;
1154 }
1155 }
1156
1157 bridge_enqueue(sc, dst_if, mc);
1158 }
1159 if (used == 0)
1160 m_freem(m);
1161 splx(s);
1162 return (0);
1163 }
1164
1165 sendunicast:
1166 /*
1167 * XXX Spanning tree consideration here?
1168 */
1169
1170 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1171 m_freem(m);
1172 splx(s);
1173 return (0);
1174 }
1175
1176 bridge_enqueue(sc, dst_if, m);
1177
1178 splx(s);
1179 return (0);
1180 }
1181
1182 /*
1183 * bridge_start:
1184 *
1185 * Start output on a bridge.
1186 *
1187 * NOTE: This routine should never be called in this implementation.
1188 */
1189 void
1190 bridge_start(struct ifnet *ifp)
1191 {
1192
1193 printf("%s: bridge_start() called\n", ifp->if_xname);
1194 }
1195
1196 /*
1197 * bridge_forward:
1198 *
1199 * The fowarding function of the bridge.
1200 */
1201 void
1202 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1203 {
1204 struct bridge_iflist *bif;
1205 struct ifnet *src_if, *dst_if;
1206 struct ether_header *eh;
1207
1208 src_if = m->m_pkthdr.rcvif;
1209
1210 sc->sc_if.if_ipackets++;
1211 sc->sc_if.if_ibytes += m->m_pkthdr.len;
1212
1213 /*
1214 * Look up the bridge_iflist.
1215 * XXX This should be more efficient.
1216 */
1217 bif = bridge_lookup_member(sc, src_if->if_xname);
1218 if (bif == NULL) {
1219 /* Interface is not a bridge member (anymore?) */
1220 m_freem(m);
1221 return;
1222 }
1223
1224 if (bif->bif_flags & IFBIF_STP) {
1225 switch (bif->bif_state) {
1226 case BSTP_IFSTATE_BLOCKING:
1227 case BSTP_IFSTATE_LISTENING:
1228 case BSTP_IFSTATE_DISABLED:
1229 m_freem(m);
1230 return;
1231 }
1232 }
1233
1234 eh = mtod(m, struct ether_header *);
1235
1236 /*
1237 * If the interface is learning, and the source
1238 * address is valid and not multicast, record
1239 * the address.
1240 */
1241 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1242 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1243 (eh->ether_shost[0] == 0 &&
1244 eh->ether_shost[1] == 0 &&
1245 eh->ether_shost[2] == 0 &&
1246 eh->ether_shost[3] == 0 &&
1247 eh->ether_shost[4] == 0 &&
1248 eh->ether_shost[5] == 0) == 0) {
1249 (void) bridge_rtupdate(sc, eh->ether_shost,
1250 src_if, 0, IFBAF_DYNAMIC);
1251 }
1252
1253 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1254 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1255 m_freem(m);
1256 return;
1257 }
1258
1259 /*
1260 * At this point, the port either doesn't participate
1261 * in spanning tree or it is in the forwarding state.
1262 */
1263
1264 /*
1265 * If the packet is unicast, destined for someone on
1266 * "this" side of the bridge, drop it.
1267 */
1268 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1269 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1270 if (src_if == dst_if) {
1271 m_freem(m);
1272 return;
1273 }
1274 } else {
1275 /* ...forward it to all interfaces. */
1276 sc->sc_if.if_imcasts++;
1277 dst_if = NULL;
1278 }
1279
1280 if (dst_if == NULL) {
1281 bridge_broadcast(sc, src_if, m);
1282 return;
1283 }
1284
1285 /*
1286 * At this point, we're dealing with a unicast frame
1287 * going to a different interface.
1288 */
1289 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1290 m_freem(m);
1291 return;
1292 }
1293 /* XXX This needs to be more efficient. */
1294 bif = bridge_lookup_member(sc, dst_if->if_xname);
1295 if (bif == NULL) {
1296 /* Not a member of the bridge (anymore?) */
1297 m_freem(m);
1298 return;
1299 }
1300
1301 if (bif->bif_flags & IFBIF_STP) {
1302 switch (bif->bif_state) {
1303 case BSTP_IFSTATE_DISABLED:
1304 case BSTP_IFSTATE_BLOCKING:
1305 m_freem(m);
1306 return;
1307 }
1308 }
1309
1310 bridge_enqueue(sc, dst_if, m);
1311 }
1312
1313 /*
1314 * bridge_input:
1315 *
1316 * Receive input from a member interface. Queue the packet for
1317 * bridging if it is not for us.
1318 */
1319 struct mbuf *
1320 bridge_input(struct ifnet *ifp, struct mbuf *m)
1321 {
1322 struct bridge_softc *sc = ifp->if_bridge;
1323 struct bridge_iflist *bif;
1324 struct ether_header *eh;
1325 struct mbuf *mc;
1326
1327 if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
1328 return (m);
1329
1330 /* XXX This needs to be more efficient. */
1331 bif = bridge_lookup_member(sc, ifp->if_xname);
1332 if (bif == NULL)
1333 return (m);
1334
1335 eh = mtod(m, struct ether_header *);
1336
1337 if (m->m_flags & (M_BCAST|M_MCAST)) {
1338 /* Tap off 802.1D packets; they do not get forwarded. */
1339 if (memcmp(eh->ether_dhost, bstp_etheraddr,
1340 ETHER_ADDR_LEN) == 0) {
1341 m = bstp_input(ifp, m);
1342 if (m == NULL)
1343 return (NULL);
1344 }
1345
1346 if (bif->bif_flags & IFBIF_STP) {
1347 switch (bif->bif_state) {
1348 case BSTP_IFSTATE_BLOCKING:
1349 case BSTP_IFSTATE_LISTENING:
1350 case BSTP_IFSTATE_DISABLED:
1351 return (m);
1352 }
1353 }
1354
1355 /*
1356 * Make a deep copy of the packet and enqueue the copy
1357 * for bridge processing; return the original packet for
1358 * local processing.
1359 */
1360 mc = m_dup(m, 0, M_COPYALL, M_NOWAIT);
1361 if (mc == NULL)
1362 return (m);
1363
1364 /* Perform the bridge forwarding function with the copy. */
1365 bridge_forward(sc, mc);
1366
1367 /* Return the original packet for local processing. */
1368 return (m);
1369 }
1370
1371 if (bif->bif_flags & IFBIF_STP) {
1372 switch (bif->bif_state) {
1373 case BSTP_IFSTATE_BLOCKING:
1374 case BSTP_IFSTATE_LISTENING:
1375 case BSTP_IFSTATE_DISABLED:
1376 return (m);
1377 }
1378 }
1379
1380 /*
1381 * Unicast. Make sure it's not for us.
1382 */
1383 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1384 /* It is destined for us. */
1385 if (memcmp(LLADDR(bif->bif_ifp->if_sadl), eh->ether_dhost,
1386 ETHER_ADDR_LEN) == 0) {
1387 if (bif->bif_flags & IFBIF_LEARNING)
1388 (void) bridge_rtupdate(sc,
1389 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1390 m->m_pkthdr.rcvif = bif->bif_ifp;
1391 return (m);
1392 }
1393
1394 /* We just received a packet that we sent out. */
1395 if (memcmp(LLADDR(bif->bif_ifp->if_sadl), eh->ether_shost,
1396 ETHER_ADDR_LEN) == 0) {
1397 m_freem(m);
1398 return (NULL);
1399 }
1400 }
1401
1402 /* Perform the bridge forwarding function. */
1403 bridge_forward(sc, m);
1404
1405 return (NULL);
1406 }
1407
1408 /*
1409 * bridge_broadcast:
1410 *
1411 * Send a frame to all interfaces that are members of
1412 * the bridge, except for the one on which the packet
1413 * arrived.
1414 */
1415 void
1416 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1417 struct mbuf *m)
1418 {
1419 struct bridge_iflist *bif;
1420 struct mbuf *mc;
1421 struct ifnet *dst_if;
1422 int used = 0;
1423
1424 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1425 dst_if = bif->bif_ifp;
1426 if (dst_if == src_if)
1427 continue;
1428
1429 if (bif->bif_flags & IFBIF_STP) {
1430 switch (bif->bif_state) {
1431 case BSTP_IFSTATE_BLOCKING:
1432 case BSTP_IFSTATE_DISABLED:
1433 continue;
1434 }
1435 }
1436
1437 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
1438 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
1439 continue;
1440
1441 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1442 continue;
1443
1444 if (LIST_NEXT(bif, bif_next) == NULL) {
1445 mc = m;
1446 used = 1;
1447 } else {
1448 mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
1449 if (mc == NULL) {
1450 sc->sc_if.if_oerrors++;
1451 continue;
1452 }
1453 }
1454
1455 bridge_enqueue(sc, dst_if, mc);
1456 }
1457 if (used == 0)
1458 m_freem(m);
1459 }
1460
1461 /*
1462 * bridge_rtupdate:
1463 *
1464 * Add a bridge routing entry.
1465 */
1466 int
1467 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
1468 struct ifnet *dst_if, int setflags, uint8_t flags)
1469 {
1470 struct bridge_rtnode *brt;
1471 int error;
1472
1473 /*
1474 * A route for this destination might already exist. If so,
1475 * update it, otherwise create a new one.
1476 */
1477 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
1478 if (sc->sc_brtcnt >= sc->sc_brtmax)
1479 return (ENOSPC);
1480
1481 /*
1482 * Allocate a new bridge forwarding node, and
1483 * initialize the expiration time and Ethernet
1484 * address.
1485 */
1486 brt = pool_get(&bridge_rtnode_pool, PR_NOWAIT);
1487 if (brt == NULL)
1488 return (ENOMEM);
1489
1490 memset(brt, 0, sizeof(*brt));
1491 brt->brt_expire = mono_time.tv_sec + sc->sc_brttimeout;
1492 brt->brt_flags = IFBAF_DYNAMIC;
1493 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
1494
1495 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
1496 pool_put(&bridge_rtnode_pool, brt);
1497 return (error);
1498 }
1499 }
1500
1501 brt->brt_ifp = dst_if;
1502 if (setflags) {
1503 brt->brt_flags = flags;
1504 brt->brt_expire = (flags & IFBAF_STATIC) ? 0 :
1505 mono_time.tv_sec + sc->sc_brttimeout;
1506 }
1507
1508 return (0);
1509 }
1510
1511 /*
1512 * bridge_rtlookup:
1513 *
1514 * Lookup the destination interface for an address.
1515 */
1516 struct ifnet *
1517 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
1518 {
1519 struct bridge_rtnode *brt;
1520
1521 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
1522 return (NULL);
1523
1524 return (brt->brt_ifp);
1525 }
1526
1527 /*
1528 * bridge_rttrim:
1529 *
1530 * Trim the routine table so that we have a number
1531 * of routing entries less than or equal to the
1532 * maximum number.
1533 */
1534 void
1535 bridge_rttrim(struct bridge_softc *sc)
1536 {
1537 struct bridge_rtnode *brt, *nbrt;
1538
1539 /* Make sure we actually need to do this. */
1540 if (sc->sc_brtcnt <= sc->sc_brtmax)
1541 return;
1542
1543 /* Force an aging cycle; this might trim enough addresses. */
1544 bridge_rtage(sc);
1545 if (sc->sc_brtcnt <= sc->sc_brtmax)
1546 return;
1547
1548 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1549 nbrt = LIST_NEXT(brt, brt_list);
1550 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1551 bridge_rtnode_destroy(sc, brt);
1552 if (sc->sc_brtcnt <= sc->sc_brtmax)
1553 return;
1554 }
1555 }
1556 }
1557
1558 /*
1559 * bridge_timer:
1560 *
1561 * Aging timer for the bridge.
1562 */
1563 void
1564 bridge_timer(void *arg)
1565 {
1566 struct bridge_softc *sc = arg;
1567 int s;
1568
1569 s = splnet();
1570 bridge_rtage(sc);
1571 splx(s);
1572
1573 if (sc->sc_if.if_flags & IFF_RUNNING)
1574 callout_reset(&sc->sc_brcallout,
1575 bridge_rtable_prune_period * hz, bridge_timer, sc);
1576 }
1577
1578 /*
1579 * bridge_rtage:
1580 *
1581 * Perform an aging cycle.
1582 */
1583 void
1584 bridge_rtage(struct bridge_softc *sc)
1585 {
1586 struct bridge_rtnode *brt, *nbrt;
1587
1588 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1589 nbrt = LIST_NEXT(brt, brt_list);
1590 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1591 if (mono_time.tv_sec >= brt->brt_expire)
1592 bridge_rtnode_destroy(sc, brt);
1593 }
1594 }
1595 }
1596
1597 /*
1598 * bridge_rtflush:
1599 *
1600 * Remove all dynamic addresses from the bridge.
1601 */
1602 void
1603 bridge_rtflush(struct bridge_softc *sc, int full)
1604 {
1605 struct bridge_rtnode *brt, *nbrt;
1606
1607 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1608 nbrt = LIST_NEXT(brt, brt_list);
1609 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
1610 bridge_rtnode_destroy(sc, brt);
1611 }
1612 }
1613
1614 /*
1615 * bridge_rtdaddr:
1616 *
1617 * Remove an address from the table.
1618 */
1619 int
1620 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
1621 {
1622 struct bridge_rtnode *brt;
1623
1624 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
1625 return (ENOENT);
1626
1627 bridge_rtnode_destroy(sc, brt);
1628 return (0);
1629 }
1630
1631 /*
1632 * bridge_rtdelete:
1633 *
1634 * Delete routes to a speicifc member interface.
1635 */
1636 void
1637 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp)
1638 {
1639 struct bridge_rtnode *brt, *nbrt;
1640
1641 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1642 nbrt = LIST_NEXT(brt, brt_list);
1643 if (brt->brt_ifp == ifp)
1644 bridge_rtnode_destroy(sc, brt);
1645 }
1646 }
1647
1648 /*
1649 * bridge_rtable_init:
1650 *
1651 * Initialize the route table for this bridge.
1652 */
1653 int
1654 bridge_rtable_init(struct bridge_softc *sc)
1655 {
1656 int i;
1657
1658 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
1659 M_DEVBUF, M_NOWAIT);
1660 if (sc->sc_rthash == NULL)
1661 return (ENOMEM);
1662
1663 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
1664 LIST_INIT(&sc->sc_rthash[i]);
1665
1666 #if NRND > 0
1667 rnd_extract_data(&sc->sc_rthash_key, sizeof(sc->sc_rthash_key),
1668 RND_EXTRACT_ANY);
1669 #else
1670 sc->sc_rthash_key = random();
1671 #endif /* NRND > 0 */
1672
1673 LIST_INIT(&sc->sc_rtlist);
1674
1675 return (0);
1676 }
1677
1678 /*
1679 * bridge_rtable_fini:
1680 *
1681 * Deconstruct the route table for this bridge.
1682 */
1683 void
1684 bridge_rtable_fini(struct bridge_softc *sc)
1685 {
1686
1687 free(sc->sc_rthash, M_DEVBUF);
1688 }
1689
1690 /*
1691 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1692 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1693 */
1694 #define mix(a, b, c) \
1695 do { \
1696 a -= b; a -= c; a ^= (c >> 13); \
1697 b -= c; b -= a; b ^= (a << 8); \
1698 c -= a; c -= b; c ^= (b >> 13); \
1699 a -= b; a -= c; a ^= (c >> 12); \
1700 b -= c; b -= a; b ^= (a << 16); \
1701 c -= a; c -= b; c ^= (b >> 5); \
1702 a -= b; a -= c; a ^= (c >> 3); \
1703 b -= c; b -= a; b ^= (a << 10); \
1704 c -= a; c -= b; c ^= (b >> 15); \
1705 } while (/*CONSTCOND*/0)
1706
1707 static __inline uint32_t
1708 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
1709 {
1710 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
1711
1712 b += addr[5] << 8;
1713 b += addr[4];
1714 a += addr[3] << 24;
1715 a += addr[2] << 16;
1716 a += addr[1] << 8;
1717 a += addr[0];
1718
1719 mix(a, b, c);
1720
1721 return (c & BRIDGE_RTHASH_MASK);
1722 }
1723
1724 #undef mix
1725
1726 /*
1727 * bridge_rtnode_lookup:
1728 *
1729 * Look up a bridge route node for the specified destination.
1730 */
1731 struct bridge_rtnode *
1732 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
1733 {
1734 struct bridge_rtnode *brt;
1735 uint32_t hash;
1736 int dir;
1737
1738 hash = bridge_rthash(sc, addr);
1739 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
1740 dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
1741 if (dir == 0)
1742 return (brt);
1743 if (dir > 0)
1744 return (NULL);
1745 }
1746
1747 return (NULL);
1748 }
1749
1750 /*
1751 * bridge_rtnode_insert:
1752 *
1753 * Insert the specified bridge node into the route table. We
1754 * assume the entry is not already in the table.
1755 */
1756 int
1757 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
1758 {
1759 struct bridge_rtnode *lbrt;
1760 uint32_t hash;
1761 int dir;
1762
1763 hash = bridge_rthash(sc, brt->brt_addr);
1764
1765 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
1766 if (lbrt == NULL) {
1767 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
1768 goto out;
1769 }
1770
1771 do {
1772 dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
1773 if (dir == 0)
1774 return (EEXIST);
1775 if (dir > 0) {
1776 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
1777 goto out;
1778 }
1779 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
1780 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
1781 goto out;
1782 }
1783 lbrt = LIST_NEXT(lbrt, brt_hash);
1784 } while (lbrt != NULL);
1785
1786 #ifdef DIAGNOSTIC
1787 panic("bridge_rtnode_insert: impossible");
1788 #endif
1789
1790 out:
1791 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
1792 sc->sc_brtcnt++;
1793
1794 return (0);
1795 }
1796
1797 /*
1798 * bridge_rtnode_destroy:
1799 *
1800 * Destroy a bridge rtnode.
1801 */
1802 void
1803 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
1804 {
1805
1806 LIST_REMOVE(brt, brt_hash);
1807
1808 LIST_REMOVE(brt, brt_list);
1809 sc->sc_brtcnt--;
1810 pool_put(&bridge_rtnode_pool, brt);
1811 }
1812