1 /* $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $ */
4 * Copyright 2001 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
39 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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.
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.
68 * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp
72 * Network interface bridge support.
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).
82 #include <sys/cdefs.h>
83 __FBSDID("$FreeBSD$");
86 #include "opt_inet6.h"
89 #include <sys/param.h>
91 #include <sys/malloc.h>
92 #include <sys/protosw.h>
93 #include <sys/systm.h>
95 #include <sys/socket.h> /* for net/if.h */
96 #include <sys/sockio.h>
97 #include <sys/ctype.h> /* string functions */
98 #include <sys/kernel.h>
99 #include <sys/random.h>
100 #include <sys/syslog.h>
101 #include <sys/sysctl.h>
103 #include <sys/module.h>
104 #include <sys/priv.h>
105 #include <sys/proc.h>
106 #include <sys/lock.h>
107 #include <sys/mutex.h>
111 #include <net/if_clone.h>
112 #include <net/if_dl.h>
113 #include <net/if_types.h>
114 #include <net/if_var.h>
115 #include <net/pfil.h>
117 #include <netinet/in.h> /* for struct arpcom */
118 #include <netinet/in_systm.h>
119 #include <netinet/in_var.h>
120 #include <netinet/ip.h>
121 #include <netinet/ip_var.h>
123 #include <netinet/ip6.h>
124 #include <netinet6/ip6_var.h>
127 #include <netinet/ip_carp.h>
129 #include <machine/in_cksum.h>
130 #include <netinet/if_ether.h> /* for struct arpcom */
131 #include <net/bridgestp.h>
132 #include <net/if_bridgevar.h>
133 #include <net/if_llc.h>
135 #include <net/route.h>
136 #include <netinet/ip_fw.h>
137 #include <netinet/ip_dummynet.h>
140 * Size of the route hash table. Must be a power of two.
142 #ifndef BRIDGE_RTHASH_SIZE
143 #define BRIDGE_RTHASH_SIZE 1024
146 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
149 * Maximum number of addresses to cache.
151 #ifndef BRIDGE_RTABLE_MAX
152 #define BRIDGE_RTABLE_MAX 100
156 * Timeout (in seconds) for entries learned dynamically.
158 #ifndef BRIDGE_RTABLE_TIMEOUT
159 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
163 * Number of seconds between walks of the route list.
165 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
166 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
170 * List of capabilities to mask on the member interface.
172 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM
175 * Bridge interface list entry.
177 struct bridge_iflist {
178 LIST_ENTRY(bridge_iflist) bif_next;
179 struct ifnet *bif_ifp; /* member if */
180 struct bstp_port bif_stp; /* STP state */
181 uint32_t bif_flags; /* member if flags */
182 int bif_mutecap; /* member muted caps */
188 struct bridge_rtnode {
189 LIST_ENTRY(bridge_rtnode) brt_hash; /* hash table linkage */
190 LIST_ENTRY(bridge_rtnode) brt_list; /* list linkage */
191 struct ifnet *brt_ifp; /* destination if */
192 unsigned long brt_expire; /* expiration time */
193 uint8_t brt_flags; /* address flags */
194 uint8_t brt_addr[ETHER_ADDR_LEN];
198 * Software state for each bridge.
200 struct bridge_softc {
201 struct ifnet *sc_ifp; /* make this an interface */
202 LIST_ENTRY(bridge_softc) sc_list;
205 uint32_t sc_brtmax; /* max # of addresses */
206 uint32_t sc_brtcnt; /* cur. # of addresses */
207 uint32_t sc_brttimeout; /* rt timeout in seconds */
208 struct callout sc_brcallout; /* bridge callout */
209 uint32_t sc_iflist_ref; /* refcount for sc_iflist */
210 uint32_t sc_iflist_xcnt; /* refcount for sc_iflist */
211 LIST_HEAD(, bridge_iflist) sc_iflist; /* member interface list */
212 LIST_HEAD(, bridge_rtnode) *sc_rthash; /* our forwarding table */
213 LIST_HEAD(, bridge_rtnode) sc_rtlist; /* list version of above */
214 uint32_t sc_rthash_key; /* key for hash */
215 LIST_HEAD(, bridge_iflist) sc_spanlist; /* span ports list */
216 struct bstp_state sc_stp; /* STP state */
217 uint32_t sc_brtexceeded; /* # of cache drops */
220 static struct mtx bridge_list_mtx;
221 eventhandler_tag bridge_detach_cookie = NULL;
223 int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
225 uma_zone_t bridge_rtnode_zone;
227 static int bridge_clone_create(struct if_clone *, int, caddr_t);
228 static void bridge_clone_destroy(struct ifnet *);
230 static int bridge_ioctl(struct ifnet *, u_long, caddr_t);
231 static void bridge_mutecaps(struct bridge_iflist *, int);
232 static void bridge_ifdetach(void *arg __unused, struct ifnet *);
233 static void bridge_init(void *);
234 static void bridge_dummynet(struct mbuf *, struct ifnet *);
235 static void bridge_stop(struct ifnet *, int);
236 static void bridge_start(struct ifnet *);
237 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
238 static int bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
240 static void bridge_enqueue(struct bridge_softc *, struct ifnet *,
242 static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
244 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
246 static void bridge_timer(void *);
248 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
250 static void bridge_span(struct bridge_softc *, struct mbuf *);
252 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
253 struct bridge_iflist *, int, uint8_t);
254 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
255 static void bridge_rttrim(struct bridge_softc *);
256 static void bridge_rtage(struct bridge_softc *);
257 static void bridge_rtflush(struct bridge_softc *, int);
258 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
260 static int bridge_rtable_init(struct bridge_softc *);
261 static void bridge_rtable_fini(struct bridge_softc *);
263 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
264 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
266 static int bridge_rtnode_insert(struct bridge_softc *,
267 struct bridge_rtnode *);
268 static void bridge_rtnode_destroy(struct bridge_softc *,
269 struct bridge_rtnode *);
270 static void bridge_rtable_expire(struct ifnet *, int);
271 static void bridge_state_change(struct ifnet *, int);
273 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
275 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
277 static void bridge_delete_member(struct bridge_softc *,
278 struct bridge_iflist *, int);
279 static void bridge_delete_span(struct bridge_softc *,
280 struct bridge_iflist *);
282 static int bridge_ioctl_add(struct bridge_softc *, void *);
283 static int bridge_ioctl_del(struct bridge_softc *, void *);
284 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
285 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
286 static int bridge_ioctl_scache(struct bridge_softc *, void *);
287 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
288 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
289 static int bridge_ioctl_rts(struct bridge_softc *, void *);
290 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
291 static int bridge_ioctl_sto(struct bridge_softc *, void *);
292 static int bridge_ioctl_gto(struct bridge_softc *, void *);
293 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
294 static int bridge_ioctl_flush(struct bridge_softc *, void *);
295 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
296 static int bridge_ioctl_spri(struct bridge_softc *, void *);
297 static int bridge_ioctl_ght(struct bridge_softc *, void *);
298 static int bridge_ioctl_sht(struct bridge_softc *, void *);
299 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
300 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
301 static int bridge_ioctl_gma(struct bridge_softc *, void *);
302 static int bridge_ioctl_sma(struct bridge_softc *, void *);
303 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
304 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
305 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
306 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
307 static int bridge_ioctl_gbparam(struct bridge_softc *, void *);
308 static int bridge_ioctl_grte(struct bridge_softc *, void *);
309 static int bridge_ioctl_gifsstp(struct bridge_softc *, void *);
310 static int bridge_ioctl_sproto(struct bridge_softc *, void *);
311 static int bridge_ioctl_stxhc(struct bridge_softc *, void *);
312 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
314 static int bridge_ip_checkbasic(struct mbuf **mp);
316 static int bridge_ip6_checkbasic(struct mbuf **mp);
318 static int bridge_fragment(struct ifnet *, struct mbuf *,
319 struct ether_header *, int, struct llc *);
321 static struct bstp_cb_ops bridge_ops = {
322 .bcb_state = bridge_state_change,
323 .bcb_rtage = bridge_rtable_expire
326 SYSCTL_DECL(_net_link);
327 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
329 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
330 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
331 static int pfil_member = 1; /* run pfil hooks on the member interface */
332 static int pfil_ipfw = 0; /* layer2 filter with ipfw */
333 static int pfil_ipfw_arp = 0; /* layer2 filter with ipfw */
334 static int log_stp = 0; /* log STP state changes */
335 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
336 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
337 SYSCTL_INT(_net_link_bridge, OID_AUTO, ipfw_arp, CTLFLAG_RW,
338 &pfil_ipfw_arp, 0, "Filter ARP packets through IPFW layer2");
339 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
340 &pfil_bridge, 0, "Packet filter on the bridge interface");
341 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
342 &pfil_member, 0, "Packet filter on the member interface");
343 SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, CTLFLAG_RW,
344 &log_stp, 0, "Log STP state changes");
346 struct bridge_control {
347 int (*bc_func)(struct bridge_softc *, void *);
352 #define BC_F_COPYIN 0x01 /* copy arguments in */
353 #define BC_F_COPYOUT 0x02 /* copy arguments out */
354 #define BC_F_SUSER 0x04 /* do super-user check */
356 const struct bridge_control bridge_control_table[] = {
357 { bridge_ioctl_add, sizeof(struct ifbreq),
358 BC_F_COPYIN|BC_F_SUSER },
359 { bridge_ioctl_del, sizeof(struct ifbreq),
360 BC_F_COPYIN|BC_F_SUSER },
362 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
363 BC_F_COPYIN|BC_F_COPYOUT },
364 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
365 BC_F_COPYIN|BC_F_SUSER },
367 { bridge_ioctl_scache, sizeof(struct ifbrparam),
368 BC_F_COPYIN|BC_F_SUSER },
369 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
372 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
373 BC_F_COPYIN|BC_F_COPYOUT },
374 { bridge_ioctl_rts, sizeof(struct ifbaconf),
375 BC_F_COPYIN|BC_F_COPYOUT },
377 { bridge_ioctl_saddr, sizeof(struct ifbareq),
378 BC_F_COPYIN|BC_F_SUSER },
380 { bridge_ioctl_sto, sizeof(struct ifbrparam),
381 BC_F_COPYIN|BC_F_SUSER },
382 { bridge_ioctl_gto, sizeof(struct ifbrparam),
385 { bridge_ioctl_daddr, sizeof(struct ifbareq),
386 BC_F_COPYIN|BC_F_SUSER },
388 { bridge_ioctl_flush, sizeof(struct ifbreq),
389 BC_F_COPYIN|BC_F_SUSER },
391 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
393 { bridge_ioctl_spri, sizeof(struct ifbrparam),
394 BC_F_COPYIN|BC_F_SUSER },
396 { bridge_ioctl_ght, sizeof(struct ifbrparam),
398 { bridge_ioctl_sht, sizeof(struct ifbrparam),
399 BC_F_COPYIN|BC_F_SUSER },
401 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
403 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
404 BC_F_COPYIN|BC_F_SUSER },
406 { bridge_ioctl_gma, sizeof(struct ifbrparam),
408 { bridge_ioctl_sma, sizeof(struct ifbrparam),
409 BC_F_COPYIN|BC_F_SUSER },
411 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
412 BC_F_COPYIN|BC_F_SUSER },
414 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
415 BC_F_COPYIN|BC_F_SUSER },
417 { bridge_ioctl_addspan, sizeof(struct ifbreq),
418 BC_F_COPYIN|BC_F_SUSER },
419 { bridge_ioctl_delspan, sizeof(struct ifbreq),
420 BC_F_COPYIN|BC_F_SUSER },
422 { bridge_ioctl_gbparam, sizeof(struct ifbropreq),
425 { bridge_ioctl_grte, sizeof(struct ifbrparam),
428 { bridge_ioctl_gifsstp, sizeof(struct ifbpstpconf),
429 BC_F_COPYIN|BC_F_COPYOUT },
431 { bridge_ioctl_sproto, sizeof(struct ifbrparam),
432 BC_F_COPYIN|BC_F_SUSER },
434 { bridge_ioctl_stxhc, sizeof(struct ifbrparam),
435 BC_F_COPYIN|BC_F_SUSER },
437 const int bridge_control_table_size =
438 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
440 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
441 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
443 LIST_HEAD(, bridge_softc) bridge_list;
445 IFC_SIMPLE_DECLARE(bridge, 0);
448 bridge_modevent(module_t mod, int type, void *data)
453 mtx_init(&bridge_list_mtx, "if_bridge list", NULL, MTX_DEF);
454 if_clone_attach(&bridge_cloner);
455 bridge_rtnode_zone = uma_zcreate("bridge_rtnode",
456 sizeof(struct bridge_rtnode), NULL, NULL, NULL, NULL,
458 LIST_INIT(&bridge_list);
459 bridge_input_p = bridge_input;
460 bridge_output_p = bridge_output;
461 bridge_dn_p = bridge_dummynet;
462 bridge_detach_cookie = EVENTHANDLER_REGISTER(
463 ifnet_departure_event, bridge_ifdetach, NULL,
464 EVENTHANDLER_PRI_ANY);
467 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
468 bridge_detach_cookie);
469 if_clone_detach(&bridge_cloner);
470 uma_zdestroy(bridge_rtnode_zone);
471 bridge_input_p = NULL;
472 bridge_output_p = NULL;
474 mtx_destroy(&bridge_list_mtx);
482 static moduledata_t bridge_mod = {
488 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
489 MODULE_DEPEND(if_bridge, bridgestp, 1, 1, 1);
492 * handler for net.link.bridge.pfil_ipfw
495 sysctl_pfil_ipfw(SYSCTL_HANDLER_ARGS)
497 int enable = pfil_ipfw;
500 error = sysctl_handle_int(oidp, &enable, 0, req);
501 enable = (enable) ? 1 : 0;
503 if (enable != pfil_ipfw) {
507 * Disable pfil so that ipfw doesnt run twice, if the user
508 * really wants both then they can re-enable pfil_bridge and/or
509 * pfil_member. Also allow non-ip packets as ipfw can filter by
521 SYSCTL_PROC(_net_link_bridge, OID_AUTO, ipfw, CTLTYPE_INT|CTLFLAG_RW,
522 &pfil_ipfw, 0, &sysctl_pfil_ipfw, "I", "Layer2 filter with IPFW");
525 * bridge_clone_create:
527 * Create a new bridge instance.
530 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t params)
532 struct bridge_softc *sc, *sc2;
533 struct ifnet *bifp, *ifp;
537 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
538 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
544 BRIDGE_LOCK_INIT(sc);
545 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
546 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
548 /* Initialize our routing table. */
549 bridge_rtable_init(sc);
551 callout_init_mtx(&sc->sc_brcallout, &sc->sc_mtx, 0);
553 LIST_INIT(&sc->sc_iflist);
554 LIST_INIT(&sc->sc_spanlist);
557 if_initname(ifp, ifc->ifc_name, unit);
558 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
559 ifp->if_ioctl = bridge_ioctl;
560 ifp->if_start = bridge_start;
561 ifp->if_init = bridge_init;
562 ifp->if_type = IFT_BRIDGE;
563 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
564 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
565 IFQ_SET_READY(&ifp->if_snd);
568 * Generate a random ethernet address with a locally administered
571 * Since we are using random ethernet addresses for the bridge, it is
572 * possible that we might have address collisions, so make sure that
573 * this hardware address isn't already in use on another bridge.
575 for (retry = 1; retry != 0;) {
576 arc4rand(eaddr, ETHER_ADDR_LEN, 1);
577 eaddr[0] &= ~1; /* clear multicast bit */
578 eaddr[0] |= 2; /* set the LAA bit */
580 mtx_lock(&bridge_list_mtx);
581 LIST_FOREACH(sc2, &bridge_list, sc_list) {
583 if (memcmp(eaddr, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0)
586 mtx_unlock(&bridge_list_mtx);
589 bstp_attach(&sc->sc_stp, &bridge_ops);
590 ether_ifattach(ifp, eaddr);
591 /* Now undo some of the damage... */
592 ifp->if_baudrate = 0;
593 ifp->if_type = IFT_BRIDGE;
595 mtx_lock(&bridge_list_mtx);
596 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
597 mtx_unlock(&bridge_list_mtx);
603 * bridge_clone_destroy:
605 * Destroy a bridge instance.
608 bridge_clone_destroy(struct ifnet *ifp)
610 struct bridge_softc *sc = ifp->if_softc;
611 struct bridge_iflist *bif;
616 ifp->if_flags &= ~IFF_UP;
618 while ((bif = LIST_FIRST(&sc->sc_iflist)) != NULL)
619 bridge_delete_member(sc, bif, 0);
621 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL) {
622 bridge_delete_span(sc, bif);
627 callout_drain(&sc->sc_brcallout);
629 mtx_lock(&bridge_list_mtx);
630 LIST_REMOVE(sc, sc_list);
631 mtx_unlock(&bridge_list_mtx);
633 bstp_detach(&sc->sc_stp);
635 if_free_type(ifp, IFT_ETHER);
637 /* Tear down the routing table. */
638 bridge_rtable_fini(sc);
640 BRIDGE_LOCK_DESTROY(sc);
647 * Handle a control request from the operator.
650 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
652 struct bridge_softc *sc = ifp->if_softc;
653 struct thread *td = curthread;
655 struct ifbreq ifbreq;
656 struct ifbifconf ifbifconf;
657 struct ifbareq ifbareq;
658 struct ifbaconf ifbaconf;
659 struct ifbrparam ifbrparam;
660 struct ifbropreq ifbropreq;
662 struct ifdrv *ifd = (struct ifdrv *) data;
663 const struct bridge_control *bc;
676 if (ifd->ifd_cmd >= bridge_control_table_size) {
680 bc = &bridge_control_table[ifd->ifd_cmd];
682 if (cmd == SIOCGDRVSPEC &&
683 (bc->bc_flags & BC_F_COPYOUT) == 0) {
687 else if (cmd == SIOCSDRVSPEC &&
688 (bc->bc_flags & BC_F_COPYOUT) != 0) {
693 if (bc->bc_flags & BC_F_SUSER) {
694 error = priv_check(td, PRIV_NET_BRIDGE);
699 if (ifd->ifd_len != bc->bc_argsize ||
700 ifd->ifd_len > sizeof(args)) {
705 bzero(&args, sizeof(args));
706 if (bc->bc_flags & BC_F_COPYIN) {
707 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
712 error = (*bc->bc_func)(sc, &args);
716 if (bc->bc_flags & BC_F_COPYOUT)
717 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
722 if (!(ifp->if_flags & IFF_UP) &&
723 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
725 * If interface is marked down and it is running,
726 * then stop and disable it.
729 } else if ((ifp->if_flags & IFF_UP) &&
730 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
732 * If interface is marked up and it is stopped, then
741 /* Do not allow the MTU to be changed on the bridge */
747 * drop the lock as ether_ioctl() will call bridge_start() and
748 * cause the lock to be recursed.
751 error = ether_ioctl(ifp, cmd, data);
755 if (BRIDGE_LOCKED(sc))
764 * Clear or restore unwanted capabilities on the member interface
767 bridge_mutecaps(struct bridge_iflist *bif, int mute)
769 struct ifnet *ifp = bif->bif_ifp;
773 if (ifp->if_ioctl == NULL)
776 bzero(&ifr, sizeof(ifr));
777 ifr.ifr_reqcap = ifp->if_capenable;
780 /* mask off and save capabilities */
781 bif->bif_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
782 if (bif->bif_mutecap != 0)
783 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
785 /* restore muted capabilities */
786 ifr.ifr_reqcap |= bif->bif_mutecap;
789 if (bif->bif_mutecap != 0) {
791 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr);
792 IFF_UNLOCKGIANT(ifp);
797 * bridge_lookup_member:
799 * Lookup a bridge member interface.
801 static struct bridge_iflist *
802 bridge_lookup_member(struct bridge_softc *sc, const char *name)
804 struct bridge_iflist *bif;
807 BRIDGE_LOCK_ASSERT(sc);
809 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
811 if (strcmp(ifp->if_xname, name) == 0)
819 * bridge_lookup_member_if:
821 * Lookup a bridge member interface by ifnet*.
823 static struct bridge_iflist *
824 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
826 struct bridge_iflist *bif;
828 BRIDGE_LOCK_ASSERT(sc);
830 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
831 if (bif->bif_ifp == member_ifp)
839 * bridge_delete_member:
841 * Delete the specified member interface.
844 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
847 struct ifnet *ifs = bif->bif_ifp;
849 BRIDGE_LOCK_ASSERT(sc);
852 switch (ifs->if_type) {
856 * Take the interface out of promiscuous mode.
858 (void) ifpromisc(ifs, 0);
859 bridge_mutecaps(bif, 0);
867 panic("bridge_delete_member: impossible");
873 if (bif->bif_flags & IFBIF_STP)
874 bstp_disable(&bif->bif_stp);
876 ifs->if_bridge = NULL;
878 LIST_REMOVE(bif, bif_next);
881 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
884 bstp_destroy(&bif->bif_stp); /* prepare to free */
890 * bridge_delete_span:
892 * Delete the specified span interface.
895 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
897 BRIDGE_LOCK_ASSERT(sc);
899 KASSERT(bif->bif_ifp->if_bridge == NULL,
900 ("%s: not a span interface", __func__));
902 LIST_REMOVE(bif, bif_next);
907 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
909 struct ifbreq *req = arg;
910 struct bridge_iflist *bif = NULL;
914 ifs = ifunit(req->ifbr_ifsname);
918 /* If it's in the span list, it can't be a member. */
919 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
920 if (ifs == bif->bif_ifp)
923 /* Allow the first Ethernet member to define the MTU */
924 if (ifs->if_type != IFT_GIF) {
925 if (LIST_EMPTY(&sc->sc_iflist))
926 sc->sc_ifp->if_mtu = ifs->if_mtu;
927 else if (sc->sc_ifp->if_mtu != ifs->if_mtu) {
928 if_printf(sc->sc_ifp, "invalid MTU for %s\n",
934 if (ifs->if_bridge == sc)
937 if (ifs->if_bridge != NULL)
940 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
945 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
947 switch (ifs->if_type) {
951 * Place the interface into promiscuous mode.
953 error = ifpromisc(ifs, 1);
957 bridge_mutecaps(bif, 1);
969 bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp);
973 * NOTE: insert_***HEAD*** should be safe for the traversals.
975 LIST_INSERT_HEAD(&sc->sc_iflist, bif, bif_next);
986 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
988 struct ifbreq *req = arg;
989 struct bridge_iflist *bif;
991 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
995 bridge_delete_member(sc, bif, 0);
1001 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1003 struct ifbreq *req = arg;
1004 struct bridge_iflist *bif;
1005 struct bstp_port *bp;
1007 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1012 req->ifbr_ifsflags = bif->bif_flags;
1013 req->ifbr_state = bp->bp_state;
1014 req->ifbr_priority = bp->bp_priority;
1015 req->ifbr_path_cost = bp->bp_path_cost;
1016 req->ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1017 req->ifbr_proto = bp->bp_protover;
1018 req->ifbr_role = bp->bp_role;
1019 req->ifbr_stpflags = bp->bp_flags;
1021 /* Copy STP state options as flags */
1022 if (bp->bp_operedge)
1023 req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
1024 if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
1025 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
1026 if (bp->bp_ptp_link)
1027 req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
1028 if (bp->bp_flags & BSTP_PORT_AUTOPTP)
1029 req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
1030 if (bp->bp_flags & BSTP_PORT_ADMEDGE)
1031 req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE;
1032 if (bp->bp_flags & BSTP_PORT_ADMCOST)
1033 req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST;
1038 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1040 struct ifbreq *req = arg;
1041 struct bridge_iflist *bif;
1042 struct bstp_port *bp;
1045 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1050 if (req->ifbr_ifsflags & IFBIF_SPAN)
1051 /* SPAN is readonly */
1054 if (req->ifbr_ifsflags & IFBIF_STP) {
1055 if ((bif->bif_flags & IFBIF_STP) == 0) {
1056 error = bstp_enable(&bif->bif_stp);
1061 if ((bif->bif_flags & IFBIF_STP) != 0)
1062 bstp_disable(&bif->bif_stp);
1065 /* Pass on STP flags */
1066 bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0);
1067 bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0);
1068 bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0);
1069 bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0);
1071 /* Save the bits relating to the bridge */
1072 bif->bif_flags = req->ifbr_ifsflags & IFBIFMASK;
1078 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1080 struct ifbrparam *param = arg;
1082 sc->sc_brtmax = param->ifbrp_csize;
1089 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1091 struct ifbrparam *param = arg;
1093 param->ifbrp_csize = sc->sc_brtmax;
1099 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1101 struct ifbifconf *bifc = arg;
1102 struct bridge_iflist *bif;
1104 int count, len, error = 0;
1107 LIST_FOREACH(bif, &sc->sc_iflist, bif_next)
1109 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1112 if (bifc->ifbic_len == 0) {
1113 bifc->ifbic_len = sizeof(breq) * count;
1118 len = bifc->ifbic_len;
1119 bzero(&breq, sizeof(breq));
1120 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1121 if (len < sizeof(breq))
1124 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1125 sizeof(breq.ifbr_ifsname));
1126 /* Fill in the ifbreq structure */
1127 error = bridge_ioctl_gifflags(sc, &breq);
1130 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
1134 len -= sizeof(breq);
1136 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1137 if (len < sizeof(breq))
1140 strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname,
1141 sizeof(breq.ifbr_ifsname));
1142 breq.ifbr_ifsflags = bif->bif_flags;
1143 breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff;
1144 error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq));
1148 len -= sizeof(breq);
1151 bifc->ifbic_len = sizeof(breq) * count;
1156 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1158 struct ifbaconf *bac = arg;
1159 struct bridge_rtnode *brt;
1160 struct ifbareq bareq;
1161 int count = 0, error = 0, len;
1163 if (bac->ifbac_len == 0)
1166 len = bac->ifbac_len;
1167 bzero(&bareq, sizeof(bareq));
1168 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
1169 if (len < sizeof(bareq))
1171 strlcpy(bareq.ifba_ifsname, brt->brt_ifp->if_xname,
1172 sizeof(bareq.ifba_ifsname));
1173 memcpy(bareq.ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1174 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1175 time_uptime < brt->brt_expire)
1176 bareq.ifba_expire = brt->brt_expire - time_uptime;
1178 bareq.ifba_expire = 0;
1179 bareq.ifba_flags = brt->brt_flags;
1181 error = copyout(&bareq, bac->ifbac_req + count, sizeof(bareq));
1185 len -= sizeof(bareq);
1188 bac->ifbac_len = sizeof(bareq) * count;
1193 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1195 struct ifbareq *req = arg;
1196 struct bridge_iflist *bif;
1199 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1203 error = bridge_rtupdate(sc, req->ifba_dst, bif, 1,
1210 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1212 struct ifbrparam *param = arg;
1214 sc->sc_brttimeout = param->ifbrp_ctime;
1219 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1221 struct ifbrparam *param = arg;
1223 param->ifbrp_ctime = sc->sc_brttimeout;
1228 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1230 struct ifbareq *req = arg;
1232 return (bridge_rtdaddr(sc, req->ifba_dst));
1236 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1238 struct ifbreq *req = arg;
1240 bridge_rtflush(sc, req->ifbr_ifsflags);
1245 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1247 struct ifbrparam *param = arg;
1248 struct bstp_state *bs = &sc->sc_stp;
1250 param->ifbrp_prio = bs->bs_bridge_priority;
1255 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1257 struct ifbrparam *param = arg;
1259 return (bstp_set_priority(&sc->sc_stp, param->ifbrp_prio));
1263 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1265 struct ifbrparam *param = arg;
1266 struct bstp_state *bs = &sc->sc_stp;
1268 param->ifbrp_hellotime = bs->bs_bridge_htime >> 8;
1273 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1275 struct ifbrparam *param = arg;
1277 return (bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime));
1281 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1283 struct ifbrparam *param = arg;
1284 struct bstp_state *bs = &sc->sc_stp;
1286 param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8;
1291 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1293 struct ifbrparam *param = arg;
1295 return (bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay));
1299 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1301 struct ifbrparam *param = arg;
1302 struct bstp_state *bs = &sc->sc_stp;
1304 param->ifbrp_maxage = bs->bs_bridge_max_age >> 8;
1309 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1311 struct ifbrparam *param = arg;
1313 return (bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage));
1317 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1319 struct ifbreq *req = arg;
1320 struct bridge_iflist *bif;
1322 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1326 return (bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority));
1330 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1332 struct ifbreq *req = arg;
1333 struct bridge_iflist *bif;
1335 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1339 return (bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost));
1343 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1345 struct ifbreq *req = arg;
1346 struct bridge_iflist *bif = NULL;
1349 ifs = ifunit(req->ifbr_ifsname);
1353 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1354 if (ifs == bif->bif_ifp)
1357 if (ifs->if_bridge != NULL)
1360 switch (ifs->if_type) {
1369 bif = malloc(sizeof(*bif), M_DEVBUF, M_NOWAIT|M_ZERO);
1374 bif->bif_flags = IFBIF_SPAN;
1376 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1382 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1384 struct ifbreq *req = arg;
1385 struct bridge_iflist *bif;
1388 ifs = ifunit(req->ifbr_ifsname);
1392 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1393 if (ifs == bif->bif_ifp)
1399 bridge_delete_span(sc, bif);
1405 bridge_ioctl_gbparam(struct bridge_softc *sc, void *arg)
1407 struct ifbropreq *req = arg;
1408 struct bstp_state *bs = &sc->sc_stp;
1409 struct bstp_port *root_port;
1411 req->ifbop_maxage = bs->bs_bridge_max_age >> 8;
1412 req->ifbop_hellotime = bs->bs_bridge_htime >> 8;
1413 req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8;
1415 root_port = bs->bs_root_port;
1416 if (root_port == NULL)
1417 req->ifbop_root_port = 0;
1419 req->ifbop_root_port = root_port->bp_ifp->if_index;
1421 req->ifbop_holdcount = bs->bs_txholdcount;
1422 req->ifbop_priority = bs->bs_bridge_priority;
1423 req->ifbop_protocol = bs->bs_protover;
1424 req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost;
1425 req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id;
1426 req->ifbop_designated_root = bs->bs_root_pv.pv_root_id;
1427 req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id;
1428 req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec;
1429 req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec;
1435 bridge_ioctl_grte(struct bridge_softc *sc, void *arg)
1437 struct ifbrparam *param = arg;
1439 param->ifbrp_cexceeded = sc->sc_brtexceeded;
1444 bridge_ioctl_gifsstp(struct bridge_softc *sc, void *arg)
1446 struct ifbpstpconf *bifstp = arg;
1447 struct bridge_iflist *bif;
1448 struct bstp_port *bp;
1449 struct ifbpstpreq bpreq;
1450 int count, len, error = 0;
1453 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1454 if ((bif->bif_flags & IFBIF_STP) != 0)
1458 if (bifstp->ifbpstp_len == 0) {
1459 bifstp->ifbpstp_len = sizeof(bpreq) * count;
1464 len = bifstp->ifbpstp_len;
1465 bzero(&bpreq, sizeof(bpreq));
1466 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1467 if (len < sizeof(bpreq))
1470 if ((bif->bif_flags & IFBIF_STP) == 0)
1474 bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff;
1475 bpreq.ifbp_fwd_trans = bp->bp_forward_transitions;
1476 bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost;
1477 bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id;
1478 bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id;
1479 bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id;
1481 error = copyout(&bpreq, bifstp->ifbpstp_req + count,
1487 len -= sizeof(bpreq);
1490 bifstp->ifbpstp_len = sizeof(bpreq) * count;
1495 bridge_ioctl_sproto(struct bridge_softc *sc, void *arg)
1497 struct ifbrparam *param = arg;
1499 return (bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto));
1503 bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg)
1505 struct ifbrparam *param = arg;
1507 return (bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc));
1513 * Detach an interface from a bridge. Called when a member
1514 * interface is detaching.
1517 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1519 struct bridge_softc *sc = ifp->if_bridge;
1520 struct bridge_iflist *bif;
1522 /* Check if the interface is a bridge member */
1526 bif = bridge_lookup_member_if(sc, ifp);
1528 bridge_delete_member(sc, bif, 1);
1534 /* Check if the interface is a span port */
1535 mtx_lock(&bridge_list_mtx);
1536 LIST_FOREACH(sc, &bridge_list, sc_list) {
1538 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1539 if (ifp == bif->bif_ifp) {
1540 bridge_delete_span(sc, bif);
1546 mtx_unlock(&bridge_list_mtx);
1552 * Initialize a bridge interface.
1555 bridge_init(void *xsc)
1557 struct bridge_softc *sc = (struct bridge_softc *)xsc;
1558 struct ifnet *ifp = sc->sc_ifp;
1560 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1564 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1567 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1568 bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */
1576 * Stop the bridge interface.
1579 bridge_stop(struct ifnet *ifp, int disable)
1581 struct bridge_softc *sc = ifp->if_softc;
1583 BRIDGE_LOCK_ASSERT(sc);
1585 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1588 callout_stop(&sc->sc_brcallout);
1589 bstp_stop(&sc->sc_stp);
1591 bridge_rtflush(sc, IFBF_FLUSHDYN);
1593 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1599 * Enqueue a packet on a bridge member interface.
1603 bridge_enqueue(struct bridge_softc *sc, struct ifnet *dst_ifp, struct mbuf *m)
1609 len = m->m_pkthdr.len;
1610 mflags = m->m_flags;
1612 /* We may be sending a fragment so traverse the mbuf */
1615 m->m_nextpkt = NULL;
1618 IFQ_ENQUEUE(&dst_ifp->if_snd, m, err);
1623 sc->sc_ifp->if_opackets++;
1624 sc->sc_ifp->if_obytes += len;
1626 dst_ifp->if_obytes += len;
1628 if (mflags & M_MCAST) {
1629 sc->sc_ifp->if_omcasts++;
1630 dst_ifp->if_omcasts++;
1634 if ((dst_ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0)
1635 (*dst_ifp->if_start)(dst_ifp);
1641 * Receive a queued packet from dummynet and pass it on to the output
1644 * The mbuf has the Ethernet header already attached.
1647 bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
1649 struct bridge_softc *sc;
1651 sc = ifp->if_bridge;
1654 * The packet didnt originate from a member interface. This should only
1655 * ever happen if a member interface is removed while packets are
1663 if (PFIL_HOOKED(&inet_pfil_hook)
1665 || PFIL_HOOKED(&inet6_pfil_hook)
1668 if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
1674 bridge_enqueue(sc, ifp, m);
1680 * Send output from a bridge member interface. This
1681 * performs the bridging function for locally originated
1684 * The mbuf has the Ethernet header already attached. We must
1685 * enqueue or free the mbuf before returning.
1688 bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1691 struct ether_header *eh;
1692 struct ifnet *dst_if;
1693 struct bridge_softc *sc;
1695 if (m->m_len < ETHER_HDR_LEN) {
1696 m = m_pullup(m, ETHER_HDR_LEN);
1701 eh = mtod(m, struct ether_header *);
1702 sc = ifp->if_bridge;
1707 * If bridge is down, but the original output interface is up,
1708 * go ahead and send out that interface. Otherwise, the packet
1711 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1717 * If the packet is a multicast, or we don't know a better way to
1718 * get there, send to all interfaces.
1720 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1723 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1724 if (dst_if == NULL) {
1725 struct bridge_iflist *bif;
1727 int error = 0, used = 0;
1731 BRIDGE_LOCK2REF(sc, error);
1737 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
1738 dst_if = bif->bif_ifp;
1740 if (dst_if->if_type == IFT_GIF)
1742 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
1746 * If this is not the original output interface,
1747 * and the interface is participating in spanning
1748 * tree, make sure the port is in a state that
1749 * allows forwarding.
1751 if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) &&
1752 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
1755 if (LIST_NEXT(bif, bif_next) == NULL) {
1759 mc = m_copypacket(m, M_DONTWAIT);
1761 sc->sc_ifp->if_oerrors++;
1766 bridge_enqueue(sc, dst_if, mc);
1776 * XXX Spanning tree consideration here?
1780 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1787 bridge_enqueue(sc, dst_if, m);
1794 * Start output on a bridge.
1798 bridge_start(struct ifnet *ifp)
1800 struct bridge_softc *sc;
1802 struct ether_header *eh;
1803 struct ifnet *dst_if;
1807 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1809 IFQ_DEQUEUE(&ifp->if_snd, m);
1814 eh = mtod(m, struct ether_header *);
1818 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1819 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1823 bridge_broadcast(sc, ifp, m, 0);
1826 bridge_enqueue(sc, dst_if, m);
1829 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1835 * The forwarding function of the bridge.
1837 * NOTE: Releases the lock on return.
1840 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1842 struct bridge_iflist *bif;
1843 struct ifnet *src_if, *dst_if, *ifp;
1844 struct ether_header *eh;
1846 src_if = m->m_pkthdr.rcvif;
1849 sc->sc_ifp->if_ipackets++;
1850 sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
1853 * Look up the bridge_iflist.
1855 bif = bridge_lookup_member_if(sc, src_if);
1857 /* Interface is not a bridge member (anymore?) */
1863 if ((bif->bif_flags & IFBIF_STP) &&
1864 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
1870 eh = mtod(m, struct ether_header *);
1873 * If the interface is learning, and the source
1874 * address is valid and not multicast, record
1877 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
1878 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
1879 (eh->ether_shost[0] == 0 &&
1880 eh->ether_shost[1] == 0 &&
1881 eh->ether_shost[2] == 0 &&
1882 eh->ether_shost[3] == 0 &&
1883 eh->ether_shost[4] == 0 &&
1884 eh->ether_shost[5] == 0) == 0) {
1885 (void) bridge_rtupdate(sc, eh->ether_shost,
1886 bif, 0, IFBAF_DYNAMIC);
1889 if ((bif->bif_flags & IFBIF_STP) != 0 &&
1890 bif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING) {
1897 * At this point, the port either doesn't participate
1898 * in spanning tree or it is in the forwarding state.
1902 * If the packet is unicast, destined for someone on
1903 * "this" side of the bridge, drop it.
1905 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
1906 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1907 if (src_if == dst_if) {
1913 /* ...forward it to all interfaces. */
1914 sc->sc_ifp->if_imcasts++;
1919 * If we have a destination interface which is a member of our bridge,
1920 * OR this is a unicast packet, push it through the bpf(4) machinery.
1921 * For broadcast or multicast packets, don't bother because it will
1922 * be reinjected into ether_input. We do this before we pass the packets
1923 * through the pfil(9) framework, as it is possible that pfil(9) will
1924 * drop the packet, or possibly modify it, making it difficult to debug
1925 * firewall issues on the bridge.
1927 if (dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0)
1930 /* run the packet filter */
1931 if (PFIL_HOOKED(&inet_pfil_hook)
1933 || PFIL_HOOKED(&inet6_pfil_hook)
1937 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
1944 if (dst_if == NULL) {
1945 bridge_broadcast(sc, src_if, m, 1);
1950 * At this point, we're dealing with a unicast frame
1951 * going to a different interface.
1953 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1958 bif = bridge_lookup_member_if(sc, dst_if);
1960 /* Not a member of the bridge (anymore?) */
1966 if ((bif->bif_flags & IFBIF_STP) &&
1967 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
1975 if (PFIL_HOOKED(&inet_pfil_hook)
1977 || PFIL_HOOKED(&inet6_pfil_hook)
1980 if (bridge_pfil(&m, sc->sc_ifp, dst_if, PFIL_OUT) != 0)
1986 bridge_enqueue(sc, dst_if, m);
1992 * Receive input from a member interface. Queue the packet for
1993 * bridging if it is not for us.
1995 static struct mbuf *
1996 bridge_input(struct ifnet *ifp, struct mbuf *m)
1998 struct bridge_softc *sc = ifp->if_bridge;
1999 struct bridge_iflist *bif, *bif2;
2001 struct ether_header *eh;
2002 struct mbuf *mc, *mc2;
2004 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2010 * Implement support for bridge monitoring. If this flag has been
2011 * set on this interface, discard the packet once we push it through
2012 * the bpf(4) machinery, but before we do, increment the byte and
2013 * packet counters associated with this interface.
2015 if ((bifp->if_flags & IFF_MONITOR) != 0) {
2016 m->m_pkthdr.rcvif = bifp;
2018 bifp->if_ipackets++;
2019 bifp->if_ibytes += m->m_pkthdr.len;
2024 bif = bridge_lookup_member_if(sc, ifp);
2030 eh = mtod(m, struct ether_header *);
2032 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp),
2033 ETHER_ADDR_LEN) == 0) {
2035 * If the packet is for us, set the packets source as the
2036 * bridge, and return the packet back to ether_input for
2040 /* Note where to send the reply to */
2041 if (bif->bif_flags & IFBIF_LEARNING)
2042 (void) bridge_rtupdate(sc,
2043 eh->ether_shost, bif, 0, IFBAF_DYNAMIC);
2045 /* Mark the packet as arriving on the bridge interface */
2046 m->m_pkthdr.rcvif = bifp;
2048 bifp->if_ipackets++;
2056 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
2057 /* Tap off 802.1D packets; they do not get forwarded. */
2058 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2059 ETHER_ADDR_LEN) == 0) {
2060 m = bstp_input(&bif->bif_stp, ifp, m);
2067 if ((bif->bif_flags & IFBIF_STP) &&
2068 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2073 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
2074 sizeof(etherbroadcastaddr)) == 0)
2075 m->m_flags |= M_BCAST;
2077 m->m_flags |= M_MCAST;
2080 * Make a deep copy of the packet and enqueue the copy
2081 * for bridge processing; return the original packet for
2084 mc = m_dup(m, M_DONTWAIT);
2090 /* Perform the bridge forwarding function with the copy. */
2091 bridge_forward(sc, mc);
2094 * Reinject the mbuf as arriving on the bridge so we have a
2095 * chance at claiming multicast packets. We can not loop back
2096 * here from ether_input as a bridge is never a member of a
2099 KASSERT(bifp->if_bridge == NULL,
2100 ("loop created in bridge_input"));
2101 mc2 = m_dup(m, M_DONTWAIT);
2103 /* Keep the layer3 header aligned */
2104 int i = min(mc2->m_pkthdr.len, max_protohdr);
2105 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2108 mc2->m_pkthdr.rcvif = bifp;
2109 (*bifp->if_input)(bifp, mc2);
2112 /* Return the original packet for local processing. */
2116 if ((bif->bif_flags & IFBIF_STP) &&
2117 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) {
2123 * Unicast. Make sure it's not for us.
2125 LIST_FOREACH(bif2, &sc->sc_iflist, bif_next) {
2126 if (bif2->bif_ifp->if_type == IFT_GIF)
2128 /* It is destined for us. */
2129 if (memcmp(IF_LLADDR(bif2->bif_ifp), eh->ether_dhost,
2130 ETHER_ADDR_LEN) == 0
2132 || (bif2->bif_ifp->if_carp
2133 && carp_forus(bif2->bif_ifp->if_carp, eh->ether_dhost))
2136 if (bif->bif_flags & IFBIF_LEARNING)
2137 (void) bridge_rtupdate(sc,
2138 eh->ether_shost, bif, 0, IFBAF_DYNAMIC);
2139 m->m_pkthdr.rcvif = bif2->bif_ifp;
2144 /* We just received a packet that we sent out. */
2145 if (memcmp(IF_LLADDR(bif2->bif_ifp), eh->ether_shost,
2146 ETHER_ADDR_LEN) == 0
2148 || (bif2->bif_ifp->if_carp
2149 && carp_forus(bif2->bif_ifp->if_carp, eh->ether_shost))
2158 /* Perform the bridge forwarding function. */
2159 bridge_forward(sc, m);
2167 * Send a frame to all interfaces that are members of
2168 * the bridge, except for the one on which the packet
2171 * NOTE: Releases the lock on return.
2174 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2175 struct mbuf *m, int runfilt)
2177 struct bridge_iflist *bif;
2179 struct ifnet *dst_if;
2180 int error = 0, used = 0, i;
2182 BRIDGE_LOCK2REF(sc, error);
2188 /* Filter on the bridge interface before broadcasting */
2189 if (runfilt && (PFIL_HOOKED(&inet_pfil_hook)
2191 || PFIL_HOOKED(&inet6_pfil_hook)
2194 if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
2200 LIST_FOREACH(bif, &sc->sc_iflist, bif_next) {
2201 dst_if = bif->bif_ifp;
2202 if (dst_if == src_if)
2205 if ((bif->bif_flags & IFBIF_STP) &&
2206 bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING)
2209 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2210 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2213 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2216 if (LIST_NEXT(bif, bif_next) == NULL) {
2220 mc = m_dup(m, M_DONTWAIT);
2222 sc->sc_ifp->if_oerrors++;
2228 * Filter on the output interface. Pass a NULL bridge interface
2229 * pointer so we do not redundantly filter on the bridge for
2230 * each interface we broadcast on.
2232 if (runfilt && (PFIL_HOOKED(&inet_pfil_hook)
2234 || PFIL_HOOKED(&inet6_pfil_hook)
2238 /* Keep the layer3 header aligned */
2239 i = min(mc->m_pkthdr.len, max_protohdr);
2240 mc = m_copyup(mc, i, ETHER_ALIGN);
2242 sc->sc_ifp->if_oerrors++;
2246 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2252 bridge_enqueue(sc, dst_if, mc);
2264 * Duplicate a packet out one or more interfaces that are in span mode,
2265 * the original mbuf is unmodified.
2268 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2270 struct bridge_iflist *bif;
2271 struct ifnet *dst_if;
2274 if (LIST_EMPTY(&sc->sc_spanlist))
2277 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2278 dst_if = bif->bif_ifp;
2280 if ((dst_if->if_drv_flags & IFF_DRV_RUNNING) == 0)
2283 mc = m_copypacket(m, M_DONTWAIT);
2285 sc->sc_ifp->if_oerrors++;
2289 bridge_enqueue(sc, dst_if, mc);
2296 * Add a bridge routing entry.
2299 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2300 struct bridge_iflist *bif, int setflags, uint8_t flags)
2302 struct bridge_rtnode *brt;
2303 struct ifnet *dst_if = bif->bif_ifp;
2306 BRIDGE_LOCK_ASSERT(sc);
2309 * A route for this destination might already exist. If so,
2310 * update it, otherwise create a new one.
2312 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2313 if (sc->sc_brtcnt >= sc->sc_brtmax) {
2314 sc->sc_brtexceeded++;
2319 * Allocate a new bridge forwarding node, and
2320 * initialize the expiration time and Ethernet
2323 brt = uma_zalloc(bridge_rtnode_zone, M_NOWAIT | M_ZERO);
2327 if (bif->bif_flags & IFBIF_STICKY)
2328 brt->brt_flags = IFBAF_STICKY;
2330 brt->brt_flags = IFBAF_DYNAMIC;
2332 brt->brt_ifp = dst_if;
2333 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2335 if ((error = bridge_rtnode_insert(sc, brt)) != 0) {
2336 uma_zfree(bridge_rtnode_zone, brt);
2341 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2342 brt->brt_ifp = dst_if;
2343 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2344 brt->brt_expire = time_uptime + sc->sc_brttimeout;
2346 brt->brt_flags = flags;
2354 * Lookup the destination interface for an address.
2356 static struct ifnet *
2357 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2359 struct bridge_rtnode *brt;
2361 BRIDGE_LOCK_ASSERT(sc);
2363 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2366 return (brt->brt_ifp);
2372 * Trim the routine table so that we have a number
2373 * of routing entries less than or equal to the
2377 bridge_rttrim(struct bridge_softc *sc)
2379 struct bridge_rtnode *brt, *nbrt;
2381 BRIDGE_LOCK_ASSERT(sc);
2383 /* Make sure we actually need to do this. */
2384 if (sc->sc_brtcnt <= sc->sc_brtmax)
2387 /* Force an aging cycle; this might trim enough addresses. */
2389 if (sc->sc_brtcnt <= sc->sc_brtmax)
2392 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2393 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2394 bridge_rtnode_destroy(sc, brt);
2395 if (sc->sc_brtcnt <= sc->sc_brtmax)
2404 * Aging timer for the bridge.
2407 bridge_timer(void *arg)
2409 struct bridge_softc *sc = arg;
2411 BRIDGE_LOCK_ASSERT(sc);
2415 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
2416 callout_reset(&sc->sc_brcallout,
2417 bridge_rtable_prune_period * hz, bridge_timer, sc);
2423 * Perform an aging cycle.
2426 bridge_rtage(struct bridge_softc *sc)
2428 struct bridge_rtnode *brt, *nbrt;
2430 BRIDGE_LOCK_ASSERT(sc);
2432 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2433 if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2434 if (time_uptime >= brt->brt_expire)
2435 bridge_rtnode_destroy(sc, brt);
2443 * Remove all dynamic addresses from the bridge.
2446 bridge_rtflush(struct bridge_softc *sc, int full)
2448 struct bridge_rtnode *brt, *nbrt;
2450 BRIDGE_LOCK_ASSERT(sc);
2452 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2453 if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2454 bridge_rtnode_destroy(sc, brt);
2461 * Remove an address from the table.
2464 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2466 struct bridge_rtnode *brt;
2468 BRIDGE_LOCK_ASSERT(sc);
2470 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2473 bridge_rtnode_destroy(sc, brt);
2480 * Delete routes to a speicifc member interface.
2483 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2485 struct bridge_rtnode *brt, *nbrt;
2487 BRIDGE_LOCK_ASSERT(sc);
2489 LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) {
2490 if (brt->brt_ifp == ifp && (full ||
2491 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC))
2492 bridge_rtnode_destroy(sc, brt);
2497 * bridge_rtable_init:
2499 * Initialize the route table for this bridge.
2502 bridge_rtable_init(struct bridge_softc *sc)
2506 sc->sc_rthash = malloc(sizeof(*sc->sc_rthash) * BRIDGE_RTHASH_SIZE,
2507 M_DEVBUF, M_NOWAIT);
2508 if (sc->sc_rthash == NULL)
2511 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2512 LIST_INIT(&sc->sc_rthash[i]);
2514 sc->sc_rthash_key = arc4random();
2516 LIST_INIT(&sc->sc_rtlist);
2522 * bridge_rtable_fini:
2524 * Deconstruct the route table for this bridge.
2527 bridge_rtable_fini(struct bridge_softc *sc)
2530 free(sc->sc_rthash, M_DEVBUF);
2534 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2535 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2537 #define mix(a, b, c) \
2539 a -= b; a -= c; a ^= (c >> 13); \
2540 b -= c; b -= a; b ^= (a << 8); \
2541 c -= a; c -= b; c ^= (b >> 13); \
2542 a -= b; a -= c; a ^= (c >> 12); \
2543 b -= c; b -= a; b ^= (a << 16); \
2544 c -= a; c -= b; c ^= (b >> 5); \
2545 a -= b; a -= c; a ^= (c >> 3); \
2546 b -= c; b -= a; b ^= (a << 10); \
2547 c -= a; c -= b; c ^= (b >> 15); \
2548 } while (/*CONSTCOND*/0)
2550 static __inline uint32_t
2551 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2553 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
2564 return (c & BRIDGE_RTHASH_MASK);
2570 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
2574 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
2575 d = ((int)a[i]) - ((int)b[i]);
2582 * bridge_rtnode_lookup:
2584 * Look up a bridge route node for the specified destination.
2586 static struct bridge_rtnode *
2587 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
2589 struct bridge_rtnode *brt;
2593 BRIDGE_LOCK_ASSERT(sc);
2595 hash = bridge_rthash(sc, addr);
2596 LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) {
2597 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
2608 * bridge_rtnode_insert:
2610 * Insert the specified bridge node into the route table. We
2611 * assume the entry is not already in the table.
2614 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
2616 struct bridge_rtnode *lbrt;
2620 BRIDGE_LOCK_ASSERT(sc);
2622 hash = bridge_rthash(sc, brt->brt_addr);
2624 lbrt = LIST_FIRST(&sc->sc_rthash[hash]);
2626 LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash);
2631 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
2635 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
2638 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
2639 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
2642 lbrt = LIST_NEXT(lbrt, brt_hash);
2643 } while (lbrt != NULL);
2646 panic("bridge_rtnode_insert: impossible");
2650 LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list);
2657 * bridge_rtnode_destroy:
2659 * Destroy a bridge rtnode.
2662 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
2664 BRIDGE_LOCK_ASSERT(sc);
2666 LIST_REMOVE(brt, brt_hash);
2668 LIST_REMOVE(brt, brt_list);
2670 uma_zfree(bridge_rtnode_zone, brt);
2674 * bridge_rtable_expire:
2676 * Set the expiry time for all routes on an interface.
2679 bridge_rtable_expire(struct ifnet *ifp, int age)
2681 struct bridge_softc *sc = ifp->if_bridge;
2682 struct bridge_rtnode *brt;
2687 * If the age is zero then flush, otherwise set all the expiry times to
2688 * age for the interface
2691 bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN);
2693 LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) {
2694 /* Cap the expiry time to 'age' */
2695 if (brt->brt_ifp == ifp &&
2696 brt->brt_expire > time_uptime + age &&
2697 (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
2698 brt->brt_expire = time_uptime + age;
2705 * bridge_state_change:
2707 * Callback from the bridgestp code when a port changes states.
2710 bridge_state_change(struct ifnet *ifp, int state)
2712 struct bridge_softc *sc = ifp->if_bridge;
2713 static const char *stpstates[] = {
2723 log(LOG_NOTICE, "%s: state changed to %s on %s\n",
2724 sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname);
2728 * Send bridge packets through pfil if they are one of the types pfil can deal
2729 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
2730 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
2734 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
2736 int snap, error, i, hlen;
2737 struct ether_header *eh1, eh2;
2738 struct ip_fw_args args;
2741 u_int16_t ether_type;
2744 error = -1; /* Default error if not error == 0 */
2746 /* we may return with the IP fields swapped, ensure its not shared */
2747 KASSERT(M_WRITABLE(*mp), ("%s: modifying a shared mbuf", __func__));
2749 if (pfil_bridge == 0 && pfil_member == 0 && pfil_ipfw == 0)
2750 return (0); /* filtering is disabled */
2752 i = min((*mp)->m_pkthdr.len, max_protohdr);
2753 if ((*mp)->m_len < i) {
2754 *mp = m_pullup(*mp, i);
2756 printf("%s: m_pullup failed\n", __func__);
2761 eh1 = mtod(*mp, struct ether_header *);
2762 ether_type = ntohs(eh1->ether_type);
2765 * Check for SNAP/LLC.
2767 if (ether_type < ETHERMTU) {
2768 struct llc *llc2 = (struct llc *)(eh1 + 1);
2770 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
2771 llc2->llc_dsap == LLC_SNAP_LSAP &&
2772 llc2->llc_ssap == LLC_SNAP_LSAP &&
2773 llc2->llc_control == LLC_UI) {
2774 ether_type = htons(llc2->llc_un.type_snap.ether_type);
2780 * If we're trying to filter bridge traffic, don't look at anything
2781 * other than IP and ARP traffic. If the filter doesn't understand
2782 * IPv6, don't allow IPv6 through the bridge either. This is lame
2783 * since if we really wanted, say, an AppleTalk filter, we are hosed,
2784 * but of course we don't have an AppleTalk filter to begin with.
2785 * (Note that since pfil doesn't understand ARP it will pass *ALL*
2788 switch (ether_type) {
2790 case ETHERTYPE_REVARP:
2791 if (pfil_ipfw_arp == 0)
2792 return (0); /* Automatically pass */
2797 case ETHERTYPE_IPV6:
2802 * Check to see if the user wants to pass non-ip
2803 * packets, these will not be checked by pfil(9) and
2804 * passed unconditionally so the default is to drop.
2810 /* Strip off the Ethernet header and keep a copy. */
2811 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
2812 m_adj(*mp, ETHER_HDR_LEN);
2814 /* Strip off snap header, if present */
2816 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
2817 m_adj(*mp, sizeof(struct llc));
2821 * Check the IP header for alignment and errors
2823 if (dir == PFIL_IN) {
2824 switch (ether_type) {
2826 error = bridge_ip_checkbasic(mp);
2829 case ETHERTYPE_IPV6:
2830 error = bridge_ip6_checkbasic(mp);
2840 if (IPFW_LOADED && pfil_ipfw != 0 && dir == PFIL_OUT && ifp != NULL) {
2842 args.rule = ip_dn_claim_rule(*mp);
2843 if (args.rule != NULL && fw_one_pass)
2844 goto ipfwpass; /* packet already partially processed */
2848 args.next_hop = NULL;
2850 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
2851 i = ip_fw_chk_ptr(&args);
2857 if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
2859 /* put the Ethernet header back on */
2860 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
2863 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
2866 * Pass the pkt to dummynet, which consumes it. The
2867 * packet will return to us via bridge_dummynet().
2870 ip_dn_io_ptr(*mp, DN_TO_IFB_FWD, &args);
2874 if (i != IP_FW_PASS) /* drop */
2882 * Run the packet through pfil
2884 switch (ether_type) {
2887 * before calling the firewall, swap fields the same as
2888 * IP does. here we assume the header is contiguous
2890 ip = mtod(*mp, struct ip *);
2892 ip->ip_len = ntohs(ip->ip_len);
2893 ip->ip_off = ntohs(ip->ip_off);
2896 * Run pfil on the member interface and the bridge, both can
2897 * be skipped by clearing pfil_member or pfil_bridge.
2900 * in_if -> bridge_if -> out_if
2902 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2903 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2906 if (*mp == NULL || error != 0) /* filter may consume */
2909 if (pfil_member && ifp != NULL)
2910 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp,
2913 if (*mp == NULL || error != 0) /* filter may consume */
2916 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2917 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp,
2920 if (*mp == NULL || error != 0) /* filter may consume */
2923 /* check if we need to fragment the packet */
2924 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
2925 i = (*mp)->m_pkthdr.len;
2926 if (i > ifp->if_mtu) {
2927 error = bridge_fragment(ifp, *mp, &eh2, snap,
2933 /* Recalculate the ip checksum and restore byte ordering */
2934 ip = mtod(*mp, struct ip *);
2935 hlen = ip->ip_hl << 2;
2936 if (hlen < sizeof(struct ip))
2938 if (hlen > (*mp)->m_len) {
2939 if ((*mp = m_pullup(*mp, hlen)) == 0)
2941 ip = mtod(*mp, struct ip *);
2945 ip->ip_len = htons(ip->ip_len);
2946 ip->ip_off = htons(ip->ip_off);
2948 if (hlen == sizeof(struct ip))
2949 ip->ip_sum = in_cksum_hdr(ip);
2951 ip->ip_sum = in_cksum(*mp, hlen);
2955 case ETHERTYPE_IPV6:
2956 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
2957 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2960 if (*mp == NULL || error != 0) /* filter may consume */
2963 if (pfil_member && ifp != NULL)
2964 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
2967 if (*mp == NULL || error != 0) /* filter may consume */
2970 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
2971 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
2988 * Finally, put everything back the way it was and return
2991 M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT);
2994 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
2997 M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT);
3000 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3011 * Perform basic checks on header size since
3012 * pfil assumes ip_input has already processed
3013 * it for it. Cut-and-pasted from ip_input.c.
3014 * Given how simple the IPv6 version is,
3015 * does the IPv4 version really need to be
3018 * XXX Should we update ipstat here, or not?
3019 * XXX Right now we update ipstat but not
3023 bridge_ip_checkbasic(struct mbuf **mp)
3025 struct mbuf *m = *mp;
3033 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3034 if ((m = m_copyup(m, sizeof(struct ip),
3035 (max_linkhdr + 3) & ~3)) == NULL) {
3036 /* XXXJRT new stat, please */
3037 ipstat.ips_toosmall++;
3040 } else if (__predict_false(m->m_len < sizeof (struct ip))) {
3041 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3042 ipstat.ips_toosmall++;
3046 ip = mtod(m, struct ip *);
3047 if (ip == NULL) goto bad;
3049 if (ip->ip_v != IPVERSION) {
3050 ipstat.ips_badvers++;
3053 hlen = ip->ip_hl << 2;
3054 if (hlen < sizeof(struct ip)) { /* minimum header length */
3055 ipstat.ips_badhlen++;
3058 if (hlen > m->m_len) {
3059 if ((m = m_pullup(m, hlen)) == 0) {
3060 ipstat.ips_badhlen++;
3063 ip = mtod(m, struct ip *);
3064 if (ip == NULL) goto bad;
3067 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3068 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3070 if (hlen == sizeof(struct ip)) {
3071 sum = in_cksum_hdr(ip);
3073 sum = in_cksum(m, hlen);
3077 ipstat.ips_badsum++;
3081 /* Retrieve the packet length. */
3082 len = ntohs(ip->ip_len);
3085 * Check for additional length bogosity
3088 ipstat.ips_badlen++;
3093 * Check that the amount of data in the buffers
3094 * is as at least much as the IP header would have us expect.
3095 * Drop packet if shorter than we expect.
3097 if (m->m_pkthdr.len < len) {
3098 ipstat.ips_tooshort++;
3102 /* Checks out, proceed */
3113 * Same as above, but for IPv6.
3114 * Cut-and-pasted from ip6_input.c.
3115 * XXX Should we update ip6stat, or not?
3118 bridge_ip6_checkbasic(struct mbuf **mp)
3120 struct mbuf *m = *mp;
3121 struct ip6_hdr *ip6;
3124 * If the IPv6 header is not aligned, slurp it up into a new
3125 * mbuf with space for link headers, in the event we forward
3126 * it. Otherwise, if it is aligned, make sure the entire base
3127 * IPv6 header is in the first mbuf of the chain.
3129 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3130 struct ifnet *inifp = m->m_pkthdr.rcvif;
3131 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3132 (max_linkhdr + 3) & ~3)) == NULL) {
3133 /* XXXJRT new stat, please */
3134 ip6stat.ip6s_toosmall++;
3135 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3138 } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3139 struct ifnet *inifp = m->m_pkthdr.rcvif;
3140 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3141 ip6stat.ip6s_toosmall++;
3142 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3147 ip6 = mtod(m, struct ip6_hdr *);
3149 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3150 ip6stat.ip6s_badvers++;
3151 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3155 /* Checks out, proceed */
3168 * Return a fragmented mbuf chain.
3171 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
3172 int snap, struct llc *llc)
3178 if (m->m_len < sizeof(struct ip) &&
3179 (m = m_pullup(m, sizeof(struct ip))) == NULL)
3181 ip = mtod(m, struct ip *);
3183 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
3188 /* walk the chain and re-add the Ethernet header */
3189 for (m0 = m; m0; m0 = m0->m_nextpkt) {
3192 M_PREPEND(m0, sizeof(struct llc), M_DONTWAIT);
3197 bcopy(llc, mtod(m0, caddr_t),
3198 sizeof(struct llc));
3200 M_PREPEND(m0, ETHER_HDR_LEN, M_DONTWAIT);
3205 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
3211 ipstat.ips_fragmented++;