2 * Copyright 2001 Wasabi Systems, Inc.
5 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Wasabi Systems, Inc.
19 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
20 * or promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by Jason L. Wright
51 * 4. The name of the author may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
56 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
57 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
63 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
66 * $OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp $
67 * $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $
68 * $FreeBSD: src/sys/net/if_bridge.c,v 1.26 2005/10/13 23:05:55 thompsa Exp $
69 * $DragonFly: src/sys/net/bridge/if_bridge.c,v 1.5 2006/01/31 19:05:37 dillon Exp $
73 * Network interface bridge support.
77 * - Currently only supports Ethernet-like interfaces (Ethernet,
78 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
79 * to bridge other types of interfaces (FDDI-FDDI, and maybe
80 * consider heterogenous bridges).
83 #include <sys/cdefs.h>
86 #include "opt_inet6.h"
88 #include <sys/param.h>
90 #include <sys/malloc.h>
91 #include <sys/protosw.h>
92 #include <sys/systm.h>
94 #include <sys/socket.h> /* for net/if.h */
95 #include <sys/sockio.h>
96 #include <sys/ctype.h> /* string functions */
97 #include <sys/kernel.h>
98 #include <sys/random.h>
99 #include <sys/sysctl.h>
100 #include <sys/module.h>
101 #include <sys/proc.h>
102 #include <sys/lock.h>
103 #include <sys/thread.h>
104 #include <sys/thread2.h>
105 #include <sys/mpipe.h>
109 #include <net/if_dl.h>
110 #include <net/if_types.h>
111 #include <net/if_var.h>
112 #include <net/pfil.h>
113 #include <net/ifq_var.h>
115 #include <netinet/in.h> /* for struct arpcom */
116 #include <netinet/in_systm.h>
117 #include <netinet/in_var.h>
118 #include <netinet/ip.h>
119 #include <netinet/ip_var.h>
121 #include <netinet/ip6.h>
122 #include <netinet6/ip6_var.h>
124 #include <netinet/if_ether.h> /* for struct arpcom */
125 #include <net/bridge/if_bridgevar.h>
126 #include <net/if_llc.h>
128 #include <net/route.h>
129 #include <sys/in_cksum.h>
132 * Size of the route hash table. Must be a power of two.
134 #ifndef BRIDGE_RTHASH_SIZE
135 #define BRIDGE_RTHASH_SIZE 1024
138 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
141 * Maximum number of addresses to cache.
143 #ifndef BRIDGE_RTABLE_MAX
144 #define BRIDGE_RTABLE_MAX 100
148 * Spanning tree defaults.
150 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
151 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
152 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
153 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
154 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
155 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
156 #define BSTP_DEFAULT_PATH_COST 55
159 * Timeout (in seconds) for entries learned dynamically.
161 #ifndef BRIDGE_RTABLE_TIMEOUT
162 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
166 * Number of seconds between walks of the route list.
168 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
169 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
172 extern struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
173 extern int (*bridge_output_p)(struct ifnet *, struct mbuf *,
174 struct sockaddr *, struct rtentry *);
175 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
176 extern void (*bridge_detach_p)(struct ifnet *);
178 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
180 int bridge_clone_create(struct if_clone *, int);
181 void bridge_clone_destroy(struct ifnet *);
183 int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
185 static void bridge_init(void *);
186 void bridge_stop(struct ifnet *, int);
187 void bridge_start(struct ifnet *);
189 void bridge_forward(struct bridge_softc *, struct mbuf *m);
191 void bridge_timer(void *);
193 void bridge_broadcast(struct bridge_softc *, struct ifnet *, struct mbuf *,
196 int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
197 struct ifnet *, int, uint8_t);
198 struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
199 void bridge_rttrim(struct bridge_softc *);
200 void bridge_rtage(struct bridge_softc *);
201 void bridge_rtflush(struct bridge_softc *, int);
202 int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
204 int bridge_rtable_init(struct bridge_softc *);
205 void bridge_rtable_fini(struct bridge_softc *);
207 struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
209 int bridge_rtnode_insert(struct bridge_softc *, struct bridge_rtnode *);
210 void bridge_rtnode_destroy(struct bridge_softc *, struct bridge_rtnode *);
212 struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
214 struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
216 void bridge_delete_member(struct bridge_softc *, struct bridge_iflist *);
218 int bridge_ioctl_add(struct bridge_softc *, void *);
219 int bridge_ioctl_del(struct bridge_softc *, void *);
220 int bridge_ioctl_gifflags(struct bridge_softc *, void *);
221 int bridge_ioctl_sifflags(struct bridge_softc *, void *);
222 int bridge_ioctl_scache(struct bridge_softc *, void *);
223 int bridge_ioctl_gcache(struct bridge_softc *, void *);
224 int bridge_ioctl_gifs(struct bridge_softc *, void *);
225 int bridge_ioctl_rts(struct bridge_softc *, void *);
226 int bridge_ioctl_saddr(struct bridge_softc *, void *);
227 int bridge_ioctl_sto(struct bridge_softc *, void *);
228 int bridge_ioctl_gto(struct bridge_softc *, void *);
229 int bridge_ioctl_daddr(struct bridge_softc *, void *);
230 int bridge_ioctl_flush(struct bridge_softc *, void *);
231 int bridge_ioctl_gpri(struct bridge_softc *, void *);
232 int bridge_ioctl_spri(struct bridge_softc *, void *);
233 int bridge_ioctl_ght(struct bridge_softc *, void *);
234 int bridge_ioctl_sht(struct bridge_softc *, void *);
235 int bridge_ioctl_gfd(struct bridge_softc *, void *);
236 int bridge_ioctl_sfd(struct bridge_softc *, void *);
237 int bridge_ioctl_gma(struct bridge_softc *, void *);
238 int bridge_ioctl_sma(struct bridge_softc *, void *);
239 int bridge_ioctl_sifprio(struct bridge_softc *, void *);
240 int bridge_ioctl_sifcost(struct bridge_softc *, void *);
241 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *, int);
242 static int bridge_ip_checkbasic(struct mbuf **mp);
244 static int bridge_ip6_checkbasic(struct mbuf **mp);
247 SYSCTL_DECL(_net_link);
248 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
250 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
251 static int pfil_member = 1; /* run pfil hooks on the member interface */
252 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
253 &pfil_bridge, 0, "Packet filter on the bridge interface");
254 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
255 &pfil_member, 0, "Packet filter on the member interface");
257 struct bridge_control {
258 int (*bc_func)(struct bridge_softc *, void *);
263 #define BC_F_COPYIN 0x01 /* copy arguments in */
264 #define BC_F_COPYOUT 0x02 /* copy arguments out */
265 #define BC_F_SUSER 0x04 /* do super-user check */
267 const struct bridge_control bridge_control_table[] = {
268 { bridge_ioctl_add, sizeof(struct ifbreq),
269 BC_F_COPYIN|BC_F_SUSER },
270 { bridge_ioctl_del, sizeof(struct ifbreq),
271 BC_F_COPYIN|BC_F_SUSER },
273 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
274 BC_F_COPYIN|BC_F_COPYOUT },
275 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
276 BC_F_COPYIN|BC_F_SUSER },
278 { bridge_ioctl_scache, sizeof(struct ifbrparam),
279 BC_F_COPYIN|BC_F_SUSER },
280 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
283 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
284 BC_F_COPYIN|BC_F_COPYOUT },
285 { bridge_ioctl_rts, sizeof(struct ifbaconf),
286 BC_F_COPYIN|BC_F_COPYOUT },
288 { bridge_ioctl_saddr, sizeof(struct ifbareq),
289 BC_F_COPYIN|BC_F_SUSER },
291 { bridge_ioctl_sto, sizeof(struct ifbrparam),
292 BC_F_COPYIN|BC_F_SUSER },
293 { bridge_ioctl_gto, sizeof(struct ifbrparam),
296 { bridge_ioctl_daddr, sizeof(struct ifbareq),
297 BC_F_COPYIN|BC_F_SUSER },
299 { bridge_ioctl_flush, sizeof(struct ifbreq),
300 BC_F_COPYIN|BC_F_SUSER },
302 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
304 { bridge_ioctl_spri, sizeof(struct ifbrparam),
305 BC_F_COPYIN|BC_F_SUSER },
307 { bridge_ioctl_ght, sizeof(struct ifbrparam),
309 { bridge_ioctl_sht, sizeof(struct ifbrparam),
310 BC_F_COPYIN|BC_F_SUSER },
312 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
314 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
315 BC_F_COPYIN|BC_F_SUSER },
317 { bridge_ioctl_gma, sizeof(struct ifbrparam),
319 { bridge_ioctl_sma, sizeof(struct ifbrparam),
320 BC_F_COPYIN|BC_F_SUSER },
322 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
323 BC_F_COPYIN|BC_F_SUSER },
325 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
326 BC_F_COPYIN|BC_F_SUSER },
328 const int bridge_control_table_size =
329 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
331 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
332 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
334 LIST_HEAD(, bridge_softc) bridge_list;
336 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
338 bridge_clone_destroy, 0, IF_MAXUNIT);
341 bridge_modevent(module_t mod, int type, void *data)
346 LIST_INIT(&bridge_list);
347 if_clone_attach(&bridge_cloner);
348 bridge_input_p = bridge_input;
349 bridge_output_p = bridge_output_serialized;
351 bridge_detach_p = bridge_ifdetach;
352 bstp_linkstate_p = bstp_linkstate;
356 if (!LIST_EMPTY(&bridge_list))
358 if_clone_detach(&bridge_cloner);
359 bridge_input_p = NULL;
360 bridge_output_p = NULL;
362 bridge_detach_p = NULL;
363 bstp_linkstate_p = NULL;
372 static moduledata_t bridge_mod = {
378 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
382 * bridge_clone_create:
384 * Create a new bridge instance.
387 bridge_clone_create(struct if_clone *ifc, int unit)
389 struct bridge_softc *sc;
393 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
394 ifp = sc->sc_ifp = &sc->sc_if;
396 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
397 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
398 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
399 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
400 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
401 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
402 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
404 /* Initialize our routing table. */
405 bridge_rtable_init(sc);
407 callout_init(&sc->sc_brcallout);
408 callout_init(&sc->sc_bstpcallout);
410 LIST_INIT(&sc->sc_iflist);
413 if_initname(ifp, ifc->ifc_name, unit);
414 ifp->if_mtu = ETHERMTU;
415 ifp->if_flags = IFF_MULTICAST;
416 ifp->if_ioctl = bridge_ioctl;
417 ifp->if_start = bridge_start;
418 ifp->if_init = bridge_init;
419 ifp->if_type = IFT_BRIDGE;
420 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
421 ifp->if_snd.ifq_maxlen = ifqmaxlen;
422 ifq_set_ready(&ifp->if_snd);
423 ifp->if_hdrlen = ETHER_HDR_LEN;
426 * Generate a random ethernet address and use the private AC:DE:48
430 int rnd = arc4random();
431 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
437 ether_ifattach(ifp, eaddr, NULL);
438 /* Now undo some of the damage... */
439 ifp->if_baudrate = 0;
440 ifp->if_type = IFT_BRIDGE;
443 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
450 * bridge_clone_destroy:
452 * Destroy a bridge instance.
455 bridge_clone_destroy(struct ifnet *ifp)
457 struct bridge_softc *sc = ifp->if_softc;
458 struct bridge_iflist *bif;
460 lwkt_serialize_enter(ifp->if_serializer);
463 ifp->if_flags &= ~IFF_UP;
465 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
466 bridge_delete_member(sc, bif);
468 callout_stop(&sc->sc_brcallout);
469 callout_stop(&sc->sc_bstpcallout);
471 lwkt_serialize_exit(ifp->if_serializer);
474 LIST_REMOVE(sc, sc_list);
480 /* Tear down the routing table. */
481 bridge_rtable_fini(sc);
489 * Handle a control request from the operator.
492 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
494 struct bridge_softc *sc = ifp->if_softc;
495 struct thread *td = curthread;
497 struct ifbreq ifbreq;
498 struct ifbifconf ifbifconf;
499 struct ifbareq ifbareq;
500 struct ifbaconf ifbaconf;
501 struct ifbrparam ifbrparam;
503 struct ifdrv *ifd = (struct ifdrv *) data;
504 const struct bridge_control *bc;
515 if (ifd->ifd_cmd >= bridge_control_table_size) {
519 bc = &bridge_control_table[ifd->ifd_cmd];
521 if (cmd == SIOCGDRVSPEC &&
522 (bc->bc_flags & BC_F_COPYOUT) == 0) {
526 else if (cmd == SIOCSDRVSPEC &&
527 (bc->bc_flags & BC_F_COPYOUT) != 0) {
532 if (bc->bc_flags & BC_F_SUSER) {
538 if (ifd->ifd_len != bc->bc_argsize ||
539 ifd->ifd_len > sizeof(args)) {
544 if (bc->bc_flags & BC_F_COPYIN) {
545 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
550 error = (*bc->bc_func)(sc, &args);
554 if (bc->bc_flags & BC_F_COPYOUT)
555 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
560 if (!(ifp->if_flags & IFF_UP) &&
561 (ifp->if_flags & IFF_RUNNING)) {
563 * If interface is marked down and it is running,
564 * then stop and disable it.
567 } else if ((ifp->if_flags & IFF_UP) &&
568 !(ifp->if_flags & IFF_RUNNING)) {
570 * If interface is marked up and it is stopped, then
578 /* Do not allow the MTU to be changed on the bridge */
584 * drop the lock as ether_ioctl() will call bridge_start() and
585 * cause the lock to be recursed.
587 error = ether_ioctl(ifp, cmd, data);
595 * bridge_lookup_member:
597 * Lookup a bridge member interface.
599 struct bridge_iflist *
600 bridge_lookup_member(struct bridge_softc *sc, const char *name)
602 struct bridge_iflist *bif;
605 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
607 if (strcmp(ifp->if_xname, name) == 0)
615 * bridge_lookup_member_if:
617 * Lookup a bridge member interface by ifnet*.
619 struct bridge_iflist *
620 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
622 struct bridge_iflist *bif;
624 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
625 if (bif->bif_ifp == member_ifp)
633 * bridge_delete_member:
635 * Delete the specified member interface.
638 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif)
640 struct ifnet *ifs = bif->bif_ifp;
642 switch (ifs->if_type) {
646 * Take the interface out of promiscuous mode.
656 panic("bridge_delete_member: impossible");
661 ifs->if_bridge = NULL;
663 LIST_REMOVE(bif, bif_next);
665 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
669 if (sc->sc_ifp->if_flags & IFF_RUNNING)
670 bstp_initialization(sc);
674 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
676 struct ifbreq *req = arg;
677 struct bridge_iflist *bif = NULL;
681 ifs = ifunit(req->ifbr_ifsname);
685 /* Allow the first member to define the MTU */
686 if (LIST_EMPTY(&sc->sc_iflist))
687 sc->sc_ifp->if_mtu = ifs->if_mtu;
688 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
689 if_printf(sc->sc_ifp, "invalid MTU for %s\n", ifs->if_xname);
693 if (ifs->if_bridge == sc)
696 if (ifs->if_bridge != NULL)
699 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT);
703 switch (ifs->if_type) {
707 * Place the interface into promiscuous mode.
709 error = ifpromisc(ifs, 1);
714 case IFT_GIF: /* :^) */
723 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
724 bif->bif_priority = BSTP_DEFAULT_PORT_PRIORITY;
725 bif->bif_path_cost = BSTP_DEFAULT_PATH_COST;
729 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
731 if (sc->sc_ifp->if_flags & IFF_RUNNING)
732 bstp_initialization(sc);
745 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
747 struct ifbreq *req = arg;
748 struct bridge_iflist *bif;
750 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
754 bridge_delete_member(sc, bif);
760 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
762 struct ifbreq *req = arg;
763 struct bridge_iflist *bif;
765 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
769 req->ifbr_ifsflags = bif->bif_flags;
770 req->ifbr_state = bif->bif_state;
771 req->ifbr_priority = bif->bif_priority;
772 req->ifbr_path_cost = bif->bif_path_cost;
773 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
779 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
781 struct ifbreq *req = arg;
782 struct bridge_iflist *bif;
784 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
788 if (req->ifbr_ifsflags & IFBIF_STP) {
789 switch (bif->bif_ifp->if_type) {
791 /* These can do spanning tree. */
795 /* Nothing else can. */
800 bif->bif_flags = req->ifbr_ifsflags;
802 if (sc->sc_ifp->if_flags & IFF_RUNNING)
803 bstp_initialization(sc);
809 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
811 struct ifbrparam *param = arg;
813 sc->sc_brtmax = param->ifbrp_csize;
820 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
822 struct ifbrparam *param = arg;
824 param->ifbrp_csize = sc->sc_brtmax;
830 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
832 struct ifbifconf *bifc = arg;
833 struct bridge_iflist *bif;
835 int count, len, error = 0;
838 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
841 if (bifc->ifbic_len == 0) {
842 bifc->ifbic_len = sizeof(breq) * count;
847 len = bifc->ifbic_len;
848 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
849 if (len < sizeof(breq))
852 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
853 sizeof(breq.ifbr_ifsname));
854 breq.ifbr_ifsflags = bif->bif_flags;
855 breq.ifbr_state = bif->bif_state;
856 breq.ifbr_priority = bif->bif_priority;
857 breq.ifbr_path_cost = bif->bif_path_cost;
858 breq.ifbr_portno = bif->bif_ifp->if_index & 0xff;
859 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
866 bifc->ifbic_len = sizeof(breq) * count;
871 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
873 struct ifbaconf *bac = arg;
874 struct bridge_rtnode *brt;
875 struct ifbareq bareq;
876 int count = 0, error = 0, len;
878 if (bac->ifbac_len == 0)
881 len = bac->ifbac_len;
882 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
883 if (len < sizeof(bareq))
885 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
886 sizeof(bareq.ifba_ifsname));
887 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
888 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
889 time_second < brt->brt_expire)
890 bareq.ifba_expire = brt->brt_expire - time_second;
892 bareq.ifba_expire = 0;
893 bareq.ifba_flags = brt->brt_flags;
895 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
899 len -= sizeof(bareq);
902 bac->ifbac_len = sizeof(bareq) * count;
907 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
909 struct ifbareq *req = arg;
910 struct bridge_iflist *bif;
913 bif = bridge_lookup_member(sc, req->ifba_ifsname);
917 error = bridge_rtupdate(sc, req->ifba_dst, bif->bif_ifp, 1,
924 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
926 struct ifbrparam *param = arg;
928 sc->sc_brttimeout = param->ifbrp_ctime;
934 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
936 struct ifbrparam *param = arg;
938 param->ifbrp_ctime = sc->sc_brttimeout;
944 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
946 struct ifbareq *req = arg;
948 return (bridge_rtdaddr(sc, req->ifba_dst));
952 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
954 struct ifbreq *req = arg;
956 bridge_rtflush(sc, req->ifbr_ifsflags);
962 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
964 struct ifbrparam *param = arg;
966 param->ifbrp_prio = sc->sc_bridge_priority;
972 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
974 struct ifbrparam *param = arg;
976 sc->sc_bridge_priority = param->ifbrp_prio;
978 if (sc->sc_ifp->if_flags & IFF_RUNNING)
979 bstp_initialization(sc);
985 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
987 struct ifbrparam *param = arg;
989 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
995 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
997 struct ifbrparam *param = arg;
999 if (param->ifbrp_hellotime == 0)
1001 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1003 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1004 bstp_initialization(sc);
1010 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1012 struct ifbrparam *param = arg;
1014 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1020 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1022 struct ifbrparam *param = arg;
1024 if (param->ifbrp_fwddelay == 0)
1026 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1028 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1029 bstp_initialization(sc);
1035 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1037 struct ifbrparam *param = arg;
1039 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1045 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1047 struct ifbrparam *param = arg;
1049 if (param->ifbrp_maxage == 0)
1051 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1053 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1054 bstp_initialization(sc);
1060 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1062 struct ifbreq *req = arg;
1063 struct bridge_iflist *bif;
1065 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1069 bif->bif_priority = req->ifbr_priority;
1071 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1072 bstp_initialization(sc);
1078 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1080 struct ifbreq *req = arg;
1081 struct bridge_iflist *bif;
1083 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1087 bif->bif_path_cost = req->ifbr_path_cost;
1089 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1090 bstp_initialization(sc);
1098 * Detach an interface from a bridge. Called when a member
1099 * interface is detaching.
1102 bridge_ifdetach(struct ifnet *ifp)
1104 struct bridge_softc *sc = ifp->if_bridge;
1107 memset(&breq, 0, sizeof(breq));
1108 snprintf(breq.ifbr_ifsname, sizeof(breq.ifbr_ifsname), ifp->if_xname);
1110 lwkt_serialize_enter(ifp->if_serializer);
1111 bridge_ioctl_del(sc, &breq);
1112 lwkt_serialize_exit(ifp->if_serializer);
1118 * Initialize a bridge interface.
1121 bridge_init(void *xsc)
1123 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1124 struct ifnet *ifp = sc->sc_ifp;
1126 if (ifp->if_flags & IFF_RUNNING)
1129 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1132 ifp->if_flags |= IFF_RUNNING;
1133 bstp_initialization(sc);
1140 * Stop the bridge interface.
1143 bridge_stop(struct ifnet *ifp, int disable)
1145 struct bridge_softc *sc = ifp->if_softc;
1147 ASSERT_SERIALIZED(ifp->if_serializer);
1149 if ((ifp->if_flags & IFF_RUNNING) == 0)
1152 callout_stop(&sc->sc_brcallout);
1155 bridge_rtflush(sc, IFBF_FLUSHDYN);
1157 ifp->if_flags &= ~IFF_RUNNING;
1163 * Enqueue a packet on a bridge member interface.
1167 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1169 struct altq_pktattr pktattr;
1172 * Clear any in-bound checksum flags for this packet.
1174 * XXX this seems to mess up the output packet.
1176 /* m->m_pkthdr.csum_flags = 0;*/
1178 while (m->m_type == MT_TAG) {
1179 /* XXX see ether_output_frame for full rules check */
1183 lwkt_serialize_enter(dst_ifp->if_serializer);
1185 if (ifq_is_enabled(&dst_ifp->if_snd))
1186 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
1188 ifq_handoff(dst_ifp, m, &pktattr);
1190 lwkt_serialize_exit(dst_ifp->if_serializer);
1194 * bridge_output_serialized:
1196 * Send output from a bridge member interface. This
1197 * performs the bridging function for locally originated
1200 * The mbuf has the Ethernet header already attached. We must
1201 * enqueue or free the mbuf before returning.
1204 bridge_output_serialized(struct ifnet *ifp, struct mbuf *m,
1205 struct sockaddr *sa, struct rtentry *rt)
1207 struct ether_header *eh;
1208 struct ifnet *dst_if;
1209 struct bridge_softc *sc;
1211 sc = ifp->if_bridge;
1213 ASSERT_SERIALIZED(ifp->if_serializer);
1215 if (m->m_len < ETHER_HDR_LEN) {
1216 m = m_pullup(m, ETHER_HDR_LEN);
1222 * Serialize our bridge interface. We have to get rid of the
1223 * originating interface lock to avoid a deadlock.
1225 lwkt_serialize_exit(ifp->if_serializer);
1226 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1228 eh = mtod(m, struct ether_header *);
1231 * If bridge is down, but the original output interface is up,
1232 * go ahead and send out that interface. Otherwise, the packet
1235 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) {
1241 * If the packet is a multicast, or we don't know a better way to
1242 * get there, send to all interfaces.
1244 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1247 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1248 if (dst_if == NULL) {
1249 struct bridge_iflist *bif;
1253 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1254 dst_if = bif->bif_ifp;
1255 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1259 * If this is not the original output interface,
1260 * and the interface is participating in spanning
1261 * tree, make sure the port is in a state that
1262 * allows forwarding.
1264 if (dst_if != ifp &&
1265 (bif->bif_flags & IFBIF_STP) != 0) {
1266 switch (bif->bif_state) {
1267 case BSTP_IFSTATE_BLOCKING:
1268 case BSTP_IFSTATE_LISTENING:
1269 case BSTP_IFSTATE_DISABLED:
1274 if (LIST_NEXT(bif, bif_next) == NULL) {
1278 mc = m_copypacket(m, MB_DONTWAIT);
1280 sc->sc_ifp->if_oerrors++;
1284 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1285 bridge_enqueue(sc, dst_if, mc);
1286 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1290 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1296 * XXX Spanning tree consideration here?
1299 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1300 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1303 bridge_enqueue(sc, dst_if, m);
1306 lwkt_serialize_enter(ifp->if_serializer);
1313 * Start output on a bridge.
1317 bridge_start(struct ifnet *ifp)
1319 struct bridge_softc *sc;
1321 struct ether_header *eh;
1322 struct ifnet *dst_if;
1326 ifp->if_flags |= IFF_OACTIVE;
1328 m = ifq_dequeue(&ifp->if_snd, NULL);
1334 eh = mtod(m, struct ether_header *);
1337 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1338 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1342 bridge_broadcast(sc, ifp, m, 0);
1344 bridge_enqueue(sc, dst_if, m);
1346 ifp->if_flags &= ~IFF_OACTIVE;
1354 * The forwarding function of the bridge.
1357 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1359 struct bridge_iflist *bif;
1360 struct ifnet *src_if, *dst_if, *ifp;
1361 struct ether_header *eh;
1363 src_if = m->m_pkthdr.rcvif;
1366 ASSERT_SERIALIZED(ifp->if_serializer);
1368 sc->sc_ifp->if_ipackets++;
1369 sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
1372 * Look up the bridge_iflist.
1374 bif = bridge_lookup_member_if(sc, src_if);
1376 /* Interface is not a bridge member (anymore?) */
1381 if (bif->bif_flags & IFBIF_STP) {
1382 switch (bif->bif_state) {
1383 case BSTP_IFSTATE_BLOCKING:
1384 case BSTP_IFSTATE_LISTENING:
1385 case BSTP_IFSTATE_DISABLED:
1391 eh = mtod(m, struct ether_header *);
1394 * Various ifp's are used below, release the serializer for
1395 * the bridge ifp so other ifp serializers can be acquired.
1397 lwkt_serialize_exit(ifp->if_serializer);
1400 * If the interface is learning, and the source
1401 * address is valid and not multicast, record
1404 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1405 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1406 (eh->ether_shost[0] == 0 &&
1407 eh->ether_shost[1] == 0 &&
1408 eh->ether_shost[2] == 0 &&
1409 eh->ether_shost[3] == 0 &&
1410 eh->ether_shost[4] == 0 &&
1411 eh->ether_shost[5] == 0) == 0) {
1412 bridge_rtupdate(sc, eh->ether_shost, src_if, 0, IFBAF_DYNAMIC);
1415 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1416 bif->bif_state == BSTP_IFSTATE_LEARNING) {
1422 * At this point, the port either doesn't participate
1423 * in spanning tree or it is in the forwarding state.
1427 * If the packet is unicast, destined for someone on
1428 * "this" side of the bridge, drop it.
1430 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1431 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1432 if (src_if == dst_if) {
1437 /* ...forward it to all interfaces. */
1438 sc->sc_ifp->if_imcasts++;
1442 /* run the packet filter */
1443 if (inet_pfil_hook.ph_hashooks > 0
1445 || inet6_pfil_hook.ph_hashooks > 0
1448 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1454 if (dst_if == NULL) {
1455 bridge_broadcast(sc, src_if, m, 1);
1460 * At this point, we're dealing with a unicast frame
1461 * going to a different interface.
1463 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
1467 bif = bridge_lookup_member_if(sc, dst_if);
1469 /* Not a member of the bridge (anymore?) */
1474 if (bif->bif_flags & IFBIF_STP) {
1475 switch (bif->bif_state) {
1476 case BSTP_IFSTATE_DISABLED:
1477 case BSTP_IFSTATE_BLOCKING:
1483 if (inet_pfil_hook.ph_hashooks > 0
1485 || inet6_pfil_hook.ph_hashooks > 0
1488 if (bridge_pfil(&m, sc->sc_ifp, dst_if, PFIL_OUT) != 0)
1493 bridge_enqueue(sc, dst_if, m);
1496 * ifp's serializer was held on entry and is expected to be held
1500 lwkt_serialize_enter(ifp->if_serializer);
1506 * Receive input from a member interface. Queue the packet for
1507 * bridging if it is not for us.
1510 bridge_input(struct ifnet *ifp, struct mbuf *m)
1512 struct bridge_softc *sc = ifp->if_bridge;
1513 struct bridge_iflist *bif;
1515 struct ether_header *eh;
1516 struct mbuf *mc, *mc2;
1519 lwkt_serialize_enter(bifp->if_serializer);
1521 if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0)
1524 bif = bridge_lookup_member_if(sc, ifp);
1528 eh = mtod(m, struct ether_header *);
1530 m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
1533 * Tap all packets arriving on the bridge, no matter if
1534 * they are local destinations or not. In is in.
1538 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
1539 #define IFP2ENADDR(ifp) (IFP2AC(ifp)->ac_enaddr)
1540 if (memcmp(eh->ether_dhost, IFP2ENADDR(bifp),
1541 ETHER_ADDR_LEN) == 0) {
1543 * If the packet is for us, set the packets source as the
1544 * bridge, and return the packet back to ether_input for
1548 /* Mark the packet as arriving on the bridge interface */
1549 m->m_pkthdr.rcvif = bifp;
1550 bifp->if_ipackets++;
1555 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1556 /* Tap off 802.1D packets; they do not get forwarded. */
1557 if (memcmp(eh->ether_dhost, bstp_etheraddr,
1558 ETHER_ADDR_LEN) == 0) {
1559 m = bstp_input(ifp, m);
1564 if (bif->bif_flags & IFBIF_STP) {
1565 switch (bif->bif_state) {
1566 case BSTP_IFSTATE_BLOCKING:
1567 case BSTP_IFSTATE_LISTENING:
1568 case BSTP_IFSTATE_DISABLED:
1573 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
1574 sizeof(etherbroadcastaddr)) == 0)
1575 m->m_flags |= M_BCAST;
1577 m->m_flags |= M_MCAST;
1580 * Make a deep copy of the packet and enqueue the copy
1581 * for bridge processing; return the original packet for
1584 mc = m_dup(m, MB_DONTWAIT);
1588 bridge_forward(sc, mc);
1591 * Reinject the mbuf as arriving on the bridge so we have a
1592 * chance at claiming multicast packets. We can not loop back
1593 * here from ether_input as a bridge is never a member of a
1596 KASSERT(bifp->if_bridge == NULL,
1597 ("loop created in bridge_input"));
1598 mc2 = m_copypacket(m, MB_DONTWAIT);
1600 mc2->m_pkthdr.rcvif = bifp;
1601 (*bifp->if_input)(bifp, mc2);
1604 /* Return the original packet for local processing. */
1608 if (bif->bif_flags & IFBIF_STP) {
1609 switch (bif->bif_state) {
1610 case BSTP_IFSTATE_BLOCKING:
1611 case BSTP_IFSTATE_LISTENING:
1612 case BSTP_IFSTATE_DISABLED:
1618 * Unicast. Make sure it's not for us.
1620 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1621 if (bif->bif_ifp->if_type != IFT_ETHER)
1623 /* It is destined for us. */
1624 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
1625 ETHER_ADDR_LEN) == 0) {
1626 if (bif->bif_flags & IFBIF_LEARNING)
1628 eh->ether_shost, ifp, 0, IFBAF_DYNAMIC);
1629 m->m_pkthdr.rcvif = bif->bif_ifp;
1630 if (ifp->if_type == IFT_GIF) {
1631 m->m_flags |= M_PROTO1;
1633 * Avoid an interface ordering deadlock.
1635 lwkt_serialize_exit(bifp->if_serializer);
1636 lwkt_serialize_enter(bif->bif_ifp->if_serializer);
1637 (*bif->bif_ifp->if_input)(bif->bif_ifp, m);
1638 lwkt_serialize_exit(bif->bif_ifp->if_serializer);
1639 lwkt_serialize_enter(bifp->if_serializer);
1645 /* We just received a packet that we sent out. */
1646 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
1647 ETHER_ADDR_LEN) == 0) {
1654 /* Perform the bridge forwarding function. */
1655 bridge_forward(sc, m);
1659 lwkt_serialize_exit(bifp->if_serializer);
1666 * Send a frame to all interfaces that are members of
1667 * the bridge, except for the one on which the packet
1671 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
1672 struct mbuf *m, int runfilt)
1674 struct bridge_iflist *bif;
1676 struct ifnet *dst_if;
1679 /* Filter on the bridge interface before broadcasting */
1680 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1682 || inet6_pfil_hook.ph_hashooks > 0
1685 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
1691 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1692 dst_if = bif->bif_ifp;
1693 if (dst_if == src_if)
1696 if (bif->bif_flags & IFBIF_STP) {
1697 switch (bif->bif_state) {
1698 case BSTP_IFSTATE_BLOCKING:
1699 case BSTP_IFSTATE_DISABLED:
1704 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
1705 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
1708 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1711 if (LIST_NEXT(bif, bif_next) == NULL) {
1715 mc = m_copypacket(m, MB_DONTWAIT);
1717 sc->sc_ifp->if_oerrors++;
1723 * Filter on the output interface. Pass a NULL bridge interface
1724 * pointer so we do not redundantly filter on the bridge for
1725 * each interface we broadcast on.
1727 if (runfilt && (inet_pfil_hook.ph_hashooks > 0
1729 || inet6_pfil_hook.ph_hashooks > 0
1732 if (bridge_pfil(&m, NULL, dst_if, PFIL_OUT) != 0)
1738 bridge_enqueue(sc, dst_if, mc);
1747 * Add a bridge routing entry.
1750 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
1751 struct ifnet *dst_if, int setflags, uint8_t flags)
1753 struct bridge_rtnode *brt;
1757 * A route for this destination might already exist. If so,
1758 * update it, otherwise create a new one.
1760 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
1761 if (sc->sc_brtcnt >= sc->sc_brtmax)
1765 * Allocate a new bridge forwarding node, and
1766 * initialize the expiration time and Ethernet
1769 brt = malloc(sizeof(struct bridge_rtnode), M_DEVBUF, M_NOWAIT|M_ZERO);
1773 brt->brt_expire = time_second + sc->sc_brttimeout;
1774 brt->brt_flags = IFBAF_DYNAMIC;
1775 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
1777 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
1778 free(brt, M_DEVBUF);
1783 brt->brt_ifp = dst_if;
1785 brt->brt_flags = flags;
1786 brt->brt_expire = (flags & IFBAF_STATIC) ? 0 :
1787 time_second + sc->sc_brttimeout;
1796 * Lookup the destination interface for an address.
1799 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
1801 struct bridge_rtnode *brt;
1803 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
1806 return (brt->brt_ifp);
1812 * Trim the routine table so that we have a number
1813 * of routing entries less than or equal to the
1817 bridge_rttrim(struct bridge_softc *sc)
1819 struct bridge_rtnode *brt, *nbrt;
1821 /* Make sure we actually need to do this. */
1822 if (sc->sc_brtcnt <= sc->sc_brtmax)
1825 /* Force an aging cycle; this might trim enough addresses. */
1827 if (sc->sc_brtcnt <= sc->sc_brtmax)
1830 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1831 nbrt = LIST_NEXT(brt, brt_list);
1832 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1833 bridge_rtnode_destroy(sc, brt);
1834 if (sc->sc_brtcnt <= sc->sc_brtmax)
1843 * Aging timer for the bridge.
1846 bridge_timer(void *arg)
1848 struct bridge_softc *sc = arg;
1850 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1854 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1855 callout_reset(&sc->sc_brcallout,
1856 bridge_rtable_prune_period * hz, bridge_timer, sc);
1858 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1864 * Perform an aging cycle.
1867 bridge_rtage(struct bridge_softc *sc)
1869 struct bridge_rtnode *brt, *nbrt;
1871 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1872 nbrt = LIST_NEXT(brt, brt_list);
1873 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
1874 if (time_second >= brt->brt_expire)
1875 bridge_rtnode_destroy(sc, brt);
1883 * Remove all dynamic addresses from the bridge.
1886 bridge_rtflush(struct bridge_softc *sc, int full)
1888 struct bridge_rtnode *brt, *nbrt;
1890 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1891 nbrt = LIST_NEXT(brt, brt_list);
1892 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
1893 bridge_rtnode_destroy(sc, brt);
1900 * Remove an address from the table.
1903 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
1905 struct bridge_rtnode *brt;
1907 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
1910 bridge_rtnode_destroy(sc, brt);
1917 * Delete routes to a speicifc member interface.
1920 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
1922 struct bridge_rtnode *brt, *nbrt;
1924 for (brt = LIST_FIRST(&sc->sc_rtlist); brt != NULL; brt = nbrt) {
1925 nbrt = LIST_NEXT(brt, brt_list);
1926 if (brt->brt_ifp == ifp && (full ||
1927 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
1928 bridge_rtnode_destroy(sc, brt);
1933 * bridge_rtable_init:
1935 * Initialize the route table for this bridge.
1938 bridge_rtable_init(struct bridge_softc *sc)
1942 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
1943 M_DEVBUF, M_NOWAIT);
1944 if (sc->sc_rthash == NULL)
1947 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
1948 LIST_INIT(&sc->sc_rthash[i]);
1950 sc->sc_rthash_key = arc4random();
1952 LIST_INIT(&sc->sc_rtlist);
1958 * bridge_rtable_fini:
1960 * Deconstruct the route table for this bridge.
1963 bridge_rtable_fini(struct bridge_softc *sc)
1966 free(sc->sc_rthash, M_DEVBUF);
1970 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1971 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1973 #define mix(a, b, c) \
1975 a -= b; a -= c; a ^= (c >> 13); \
1976 b -= c; b -= a; b ^= (a << 8); \
1977 c -= a; c -= b; c ^= (b >> 13); \
1978 a -= b; a -= c; a ^= (c >> 12); \
1979 b -= c; b -= a; b ^= (a << 16); \
1980 c -= a; c -= b; c ^= (b >> 5); \
1981 a -= b; a -= c; a ^= (c >> 3); \
1982 b -= c; b -= a; b ^= (a << 10); \
1983 c -= a; c -= b; c ^= (b >> 15); \
1984 } while (/*CONSTCOND*/0)
1986 static __inline uint32_t
1987 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
1989 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2000 return (c & BRIDGE_RTHASH_MASK);
2006 * bridge_rtnode_lookup:
2008 * Look up a bridge route node for the specified destination.
2010 struct bridge_rtnode *
2011 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2013 struct bridge_rtnode *brt;
2017 hash = bridge_rthash(sc, addr);
2018 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2019 dir = memcmp(addr, brt->brt_addr, ETHER_ADDR_LEN);
2030 * bridge_rtnode_insert:
2032 * Insert the specified bridge node into the route table. We
2033 * assume the entry is not already in the table.
2036 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2038 struct bridge_rtnode *lbrt;
2042 hash = bridge_rthash(sc, brt->brt_addr);
2044 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2046 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2051 dir = memcmp(brt->brt_addr, lbrt->brt_addr, ETHER_ADDR_LEN);
2055 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2058 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2059 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2062 lbrt = LIST_NEXT(lbrt, brt_hash);
2063 } while (lbrt != NULL);
2066 panic("bridge_rtnode_insert: impossible");
2070 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2077 * bridge_rtnode_destroy:
2079 * Destroy a bridge rtnode.
2082 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2085 LIST_REMOVE(brt, brt_hash);
2087 LIST_REMOVE(brt, brt_list);
2089 free(brt, M_DEVBUF);
2093 * Send bridge packets through pfil if they are one of the types pfil can deal
2094 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2095 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2099 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2102 struct ether_header *eh1, eh2;
2105 u_int16_t ether_type;
2108 error = -1; /* Default error if not error == 0 */
2110 i = min((*mp)->m_pkthdr.len, max_protohdr);
2111 if ((*mp)->m_len < i) {
2112 *mp = m_pullup(*mp, i);
2114 printf("%s: m_pullup failed\n", __func__);
2119 eh1 = mtod(*mp, struct ether_header *);
2120 ether_type = ntohs(eh1->ether_type);
2123 * Check for SNAP/LLC.
2125 if (ether_type < ETHERMTU) {
2126 struct llc *llc2 = (struct llc *)(eh1 + 1);
2128 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2129 llc2->llc_dsap == LLC_SNAP_LSAP &&
2130 llc2->llc_ssap == LLC_SNAP_LSAP &&
2131 llc2->llc_control == LLC_UI) {
2132 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2138 * If we're trying to filter bridge traffic, don't look at anything
2139 * other than IP and ARP traffic. If the filter doesn't understand
2140 * IPv6, don't allow IPv6 through the bridge either. This is lame
2141 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2142 * but of course we don't have an AppleTalk filter to begin with.
2143 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2146 switch (ether_type) {
2148 case ETHERTYPE_REVARP:
2149 return 0; /* Automatically pass */
2152 case ETHERTYPE_IPV6:
2159 /* Strip off the Ethernet header and keep a copy. */
2160 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2161 m_adj(*mp, ETHER_HDR_LEN);
2163 /* Strip off snap header, if present */
2165 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2166 m_adj(*mp, sizeof(struct llc));
2170 * Check the IP header for alignment and errors
2172 if (dir == PFIL_IN) {
2173 switch (ether_type) {
2175 error = bridge_ip_checkbasic(mp);
2178 case ETHERTYPE_IPV6:
2179 error = bridge_ip6_checkbasic(mp);
2192 * Run the packet through pfil
2198 * before calling the firewall, swap fields the same as
2199 * IP does. here we assume the header is contiguous
2201 ip = mtod(*mp, struct ip *);
2203 ip->ip_len = ntohs(ip->ip_len);
2204 ip->ip_off = ntohs(ip->ip_off);
2207 * Run pfil on the member interface and the bridge, both can
2208 * be skipped by clearing pfil_member or pfil_bridge.
2211 * in_if -> bridge_if -> out_if
2213 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2214 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2217 if (*mp == NULL || error != 0) /* filter may consume */
2220 if (pfil_member && ifp != NULL)
2221 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2224 if (*mp == NULL || error != 0) /* filter may consume */
2227 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2228 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2231 /* Restore ip and the fields ntohs()'d. */
2232 if (*mp != NULL && error == 0) {
2233 ip = mtod(*mp, struct ip *);
2234 ip->ip_len = htons(ip->ip_len);
2235 ip->ip_off = htons(ip->ip_off);
2240 case ETHERTYPE_IPV6 :
2241 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2242 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2245 if (*mp == NULL || error != 0) /* filter may consume */
2248 if (pfil_member && ifp != NULL)
2249 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2252 if (*mp == NULL || error != 0) /* filter may consume */
2255 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2256 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2273 * Finally, put everything back the way it was and return
2276 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
2279 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2282 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
2285 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2296 * Perform basic checks on header size since
2297 * pfil assumes ip_input has already processed
2298 * it for it. Cut-and-pasted from ip_input.c.
2299 * Given how simple the IPv6 version is,
2300 * does the IPv4 version really need to be
2303 * XXX Should we update ipstat here, or not?
2304 * XXX Right now we update ipstat but not
2308 bridge_ip_checkbasic(struct mbuf **mp)
2310 struct mbuf *m = *mp;
2318 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2319 if ((m = m_copyup(m, sizeof(struct ip),
2320 (max_linkhdr + 3) & ~3)) == NULL) {
2321 /* XXXJRT new stat, please */
2322 ipstat.ips_toosmall++;
2327 #ifndef __predict_false
2328 #define __predict_false(x) x
2330 if (__predict_false(m->m_len < sizeof (struct ip))) {
2331 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
2332 ipstat.ips_toosmall++;
2336 ip = mtod(m, struct ip *);
2337 if (ip == NULL) goto bad;
2339 if (ip->ip_v != IPVERSION) {
2340 ipstat.ips_badvers++;
2343 hlen = ip->ip_hl << 2;
2344 if (hlen < sizeof(struct ip)) { /* minimum header length */
2345 ipstat.ips_badhlen++;
2348 if (hlen > m->m_len) {
2349 if ((m = m_pullup(m, hlen)) == 0) {
2350 ipstat.ips_badhlen++;
2353 ip = mtod(m, struct ip *);
2354 if (ip == NULL) goto bad;
2357 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
2358 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
2360 if (hlen == sizeof(struct ip)) {
2361 sum = in_cksum_hdr(ip);
2363 sum = in_cksum(m, hlen);
2367 ipstat.ips_badsum++;
2371 /* Retrieve the packet length. */
2372 len = ntohs(ip->ip_len);
2375 * Check for additional length bogosity
2378 ipstat.ips_badlen++;
2383 * Check that the amount of data in the buffers
2384 * is as at least much as the IP header would have us expect.
2385 * Drop packet if shorter than we expect.
2387 if (m->m_pkthdr.len < len) {
2388 ipstat.ips_tooshort++;
2392 /* Checks out, proceed */
2403 * Same as above, but for IPv6.
2404 * Cut-and-pasted from ip6_input.c.
2405 * XXX Should we update ip6stat, or not?
2408 bridge_ip6_checkbasic(struct mbuf **mp)
2410 struct mbuf *m = *mp;
2411 struct ip6_hdr *ip6;
2414 * If the IPv6 header is not aligned, slurp it up into a new
2415 * mbuf with space for link headers, in the event we forward
2416 * it. Otherwise, if it is aligned, make sure the entire base
2417 * IPv6 header is in the first mbuf of the chain.
2420 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
2421 struct ifnet *inifp = m->m_pkthdr.rcvif;
2422 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
2423 (max_linkhdr + 3) & ~3)) == NULL) {
2424 /* XXXJRT new stat, please */
2425 ip6stat.ip6s_toosmall++;
2426 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2431 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
2432 struct ifnet *inifp = m->m_pkthdr.rcvif;
2433 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
2434 ip6stat.ip6s_toosmall++;
2435 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
2440 ip6 = mtod(m, struct ip6_hdr *);
2442 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
2443 ip6stat.ip6s_badvers++;
2444 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
2448 /* Checks out, proceed */