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 $
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 * Bridge's route information is duplicated to each CPUs:
85 * +-----------+ +-----------+ +-----------+ +-----------+
86 * | rtnode | | rtnode | | rtnode | | rtnode |
88 * | dst eaddr | | dst eaddr | | dst eaddr | | dst eaddr |
89 * +-----------+ +-----------+ +-----------+ +-----------+
92 * | | +----------+ | |
96 * +-------------->| timeout |<-------------+
100 * We choose to put timeout and dst_ifp into shared part, so updating
101 * them will be cheaper than using message forwarding. Also there is
102 * not need to use spinlock to protect the updating: timeout and dst_ifp
103 * is not related and specific field's updating order has no importance.
104 * The cache pollution by the share part should not be heavy: in a stable
105 * setup, dst_ifp probably will be not changed in rtnode's life time,
106 * while timeout is refreshed once per second; most of the time, timeout
107 * and dst_ifp are read-only accessed.
110 * Bridge route information installation on bridge_input path:
112 * CPU0 CPU1 CPU2 CPU3
119 * ifnet0<-----------------------+
122 * rtnode exists?(Y)free nmsg :
153 * The netmsgs forwarded between protocol threads and ifnet threads are
154 * allocated with (M_WAITOK|M_NULLOK), so it will not fail under most
155 * cases (route information is too precious to be not installed :).
156 * Since multiple threads may try to install route information for the
157 * same dst eaddr, we look up route information in ifnet0. However, this
158 * looking up only need to be performed on ifnet0, which is the start
159 * point of the route information installation process.
162 * Bridge route information deleting/flushing:
164 * CPU0 CPU1 CPU2 CPU3
168 * find suitable rtnodes,
169 * mark their rtinfo dead
171 * | domsg <------------------------------------------+
174 * V fwdmsg fwdmsg fwdmsg |
175 * ifnet0 --------> ifnet1 --------> ifnet2 --------> ifnet3
176 * delete rtnodes delete rtnodes delete rtnodes delete rtnodes
177 * w/ dead rtinfo w/ dead rtinfo w/ dead rtinfo w/ dead rtinfo
180 * All deleting/flushing operations are serialized by netisr0, so each
181 * operation only reaps the route information marked dead by itself.
184 * Bridge route information adding/deleting/flushing:
185 * Since all operation is serialized by the fixed message flow between
186 * ifnet threads, it is not possible to create corrupted per-cpu route
191 * Percpu member interface list iteration with blocking operation:
192 * Since one bridge could only delete one member interface at a time and
193 * the deleted member interface is not freed after netmsg_service_sync(),
194 * following way is used to make sure that even if the certain member
195 * interface is ripped from the percpu list during the blocking operation,
196 * the iteration still could keep going:
198 * TAILQ_FOREACH_MUTABLE(bif, sc->sc_iflists[mycpuid], bif_next, nbif) {
199 * blocking operation;
200 * blocking operation;
203 * if (nbif != NULL && !nbif->bif_onlist) {
204 * KKASSERT(bif->bif_onlist);
205 * nbif = TAILQ_NEXT(bif, bif_next);
209 * As mentioned above only one member interface could be unlinked from the
210 * percpu member interface list, so either bif or nbif may be not on the list,
211 * but _not_ both. To keep the list iteration, we don't care about bif, but
212 * only nbif. Since removed member interface will only be freed after we
213 * finish our work, it is safe to access any field in an unlinked bif (here
214 * bif_onlist). If nbif is no longer on the list, then bif must be on the
215 * list, so we change nbif to the next element of bif and keep going.
218 #include "opt_inet.h"
219 #include "opt_inet6.h"
221 #include <sys/param.h>
222 #include <sys/mbuf.h>
223 #include <sys/malloc.h>
224 #include <sys/protosw.h>
225 #include <sys/systm.h>
226 #include <sys/time.h>
227 #include <sys/socket.h> /* for net/if.h */
228 #include <sys/sockio.h>
229 #include <sys/ctype.h> /* string functions */
230 #include <sys/kernel.h>
231 #include <sys/random.h>
232 #include <sys/sysctl.h>
233 #include <sys/module.h>
234 #include <sys/proc.h>
235 #include <sys/priv.h>
236 #include <sys/lock.h>
237 #include <sys/thread.h>
238 #include <sys/thread2.h>
239 #include <sys/mpipe.h>
243 #include <net/if_dl.h>
244 #include <net/if_types.h>
245 #include <net/if_var.h>
246 #include <net/pfil.h>
247 #include <net/ifq_var.h>
248 #include <net/if_clone.h>
250 #include <netinet/in.h> /* for struct arpcom */
251 #include <netinet/in_systm.h>
252 #include <netinet/in_var.h>
253 #include <netinet/ip.h>
254 #include <netinet/ip_var.h>
256 #include <netinet/ip6.h>
257 #include <netinet6/ip6_var.h>
259 #include <netinet/if_ether.h> /* for struct arpcom */
260 #include <net/bridge/if_bridgevar.h>
261 #include <net/if_llc.h>
262 #include <net/netmsg2.h>
263 #include <net/netisr2.h>
265 #include <net/route.h>
266 #include <sys/in_cksum.h>
269 * Size of the route hash table. Must be a power of two.
271 #ifndef BRIDGE_RTHASH_SIZE
272 #define BRIDGE_RTHASH_SIZE 1024
275 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
278 * Maximum number of addresses to cache.
280 #ifndef BRIDGE_RTABLE_MAX
281 #define BRIDGE_RTABLE_MAX 100
285 * Spanning tree defaults.
287 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
288 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
289 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
290 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
291 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
292 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
293 #define BSTP_DEFAULT_PATH_COST 55
296 * Timeout (in seconds) for entries learned dynamically.
298 #ifndef BRIDGE_RTABLE_TIMEOUT
299 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
303 * Number of seconds between walks of the route list.
305 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
306 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
310 * List of capabilities to mask on the member interface.
312 #define BRIDGE_IFCAPS_MASK (IFCAP_TXCSUM | IFCAP_TSO)
314 typedef int (*bridge_ctl_t)(struct bridge_softc *, void *);
316 struct netmsg_brctl {
317 struct netmsg_base base;
318 bridge_ctl_t bc_func;
319 struct bridge_softc *bc_sc;
323 struct netmsg_brsaddr {
324 struct netmsg_base base;
325 struct bridge_softc *br_softc;
326 struct ifnet *br_dst_if;
327 struct bridge_rtinfo *br_rtinfo;
329 uint8_t br_dst[ETHER_ADDR_LEN];
333 struct netmsg_braddbif {
334 struct netmsg_base base;
335 struct bridge_softc *br_softc;
336 struct bridge_ifinfo *br_bif_info;
337 struct ifnet *br_bif_ifp;
340 struct netmsg_brdelbif {
341 struct netmsg_base base;
342 struct bridge_softc *br_softc;
343 struct bridge_ifinfo *br_bif_info;
344 struct bridge_iflist_head *br_bif_list;
347 struct netmsg_brsflags {
348 struct netmsg_base base;
349 struct bridge_softc *br_softc;
350 struct bridge_ifinfo *br_bif_info;
351 uint32_t br_bif_flags;
354 eventhandler_tag bridge_detach_cookie = NULL;
356 extern struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
357 extern int (*bridge_output_p)(struct ifnet *, struct mbuf *);
358 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
359 extern struct ifnet *(*bridge_interface_p)(void *if_bridge);
361 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
363 static int bridge_clone_create(struct if_clone *, int, caddr_t);
364 static int bridge_clone_destroy(struct ifnet *);
366 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
367 static void bridge_mutecaps(struct bridge_ifinfo *, struct ifnet *, int);
368 static void bridge_ifdetach(void *, struct ifnet *);
369 static void bridge_init(void *);
370 static int bridge_from_us(struct bridge_softc *, struct ether_header *);
371 static void bridge_stop(struct ifnet *);
372 static void bridge_start(struct ifnet *, struct ifaltq_subque *);
373 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
374 static int bridge_output(struct ifnet *, struct mbuf *);
375 static struct ifnet *bridge_interface(void *if_bridge);
377 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
379 static void bridge_timer_handler(netmsg_t);
380 static void bridge_timer(void *);
382 static void bridge_start_bcast(struct bridge_softc *, struct mbuf *);
383 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
385 static void bridge_span(struct bridge_softc *, struct mbuf *);
387 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
388 struct ifnet *, uint8_t);
389 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
390 static void bridge_rtreap(struct bridge_softc *);
391 static void bridge_rtreap_async(struct bridge_softc *);
392 static void bridge_rttrim(struct bridge_softc *);
393 static int bridge_rtage_finddead(struct bridge_softc *);
394 static void bridge_rtage(struct bridge_softc *);
395 static void bridge_rtflush(struct bridge_softc *, int);
396 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
397 static int bridge_rtsaddr(struct bridge_softc *, const uint8_t *,
398 struct ifnet *, uint8_t);
399 static void bridge_rtmsg_sync(struct bridge_softc *sc);
400 static void bridge_rtreap_handler(netmsg_t);
401 static void bridge_rtinstall_handler(netmsg_t);
402 static int bridge_rtinstall_oncpu(struct bridge_softc *, const uint8_t *,
403 struct ifnet *, int, uint8_t, struct bridge_rtinfo **);
405 static void bridge_rtable_init(struct bridge_softc *);
406 static void bridge_rtable_fini(struct bridge_softc *);
408 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
409 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
411 static void bridge_rtnode_insert(struct bridge_softc *,
412 struct bridge_rtnode *);
413 static void bridge_rtnode_destroy(struct bridge_softc *,
414 struct bridge_rtnode *);
416 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
418 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
420 static struct bridge_iflist *bridge_lookup_member_ifinfo(struct bridge_softc *,
421 struct bridge_ifinfo *);
422 static void bridge_delete_member(struct bridge_softc *,
423 struct bridge_iflist *, int);
424 static void bridge_delete_span(struct bridge_softc *,
425 struct bridge_iflist *);
427 static int bridge_control(struct bridge_softc *, u_long,
428 bridge_ctl_t, void *);
429 static int bridge_ioctl_init(struct bridge_softc *, void *);
430 static int bridge_ioctl_stop(struct bridge_softc *, void *);
431 static int bridge_ioctl_add(struct bridge_softc *, void *);
432 static int bridge_ioctl_del(struct bridge_softc *, void *);
433 static void bridge_ioctl_fillflags(struct bridge_softc *sc,
434 struct bridge_iflist *bif, struct ifbreq *req);
435 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
436 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
437 static int bridge_ioctl_scache(struct bridge_softc *, void *);
438 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
439 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
440 static int bridge_ioctl_rts(struct bridge_softc *, void *);
441 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
442 static int bridge_ioctl_sto(struct bridge_softc *, void *);
443 static int bridge_ioctl_gto(struct bridge_softc *, void *);
444 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
445 static int bridge_ioctl_flush(struct bridge_softc *, void *);
446 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
447 static int bridge_ioctl_spri(struct bridge_softc *, void *);
448 static int bridge_ioctl_reinit(struct bridge_softc *, void *);
449 static int bridge_ioctl_ght(struct bridge_softc *, void *);
450 static int bridge_ioctl_sht(struct bridge_softc *, void *);
451 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
452 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
453 static int bridge_ioctl_gma(struct bridge_softc *, void *);
454 static int bridge_ioctl_sma(struct bridge_softc *, void *);
455 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
456 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
457 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
458 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
459 static int bridge_ioctl_sifbondwght(struct bridge_softc *, void *);
460 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
462 static int bridge_ip_checkbasic(struct mbuf **mp);
464 static int bridge_ip6_checkbasic(struct mbuf **mp);
466 static int bridge_fragment(struct ifnet *, struct mbuf *,
467 struct ether_header *, int, struct llc *);
468 static void bridge_enqueue_handler(netmsg_t);
469 static void bridge_handoff(struct bridge_softc *, struct ifnet *,
472 static void bridge_del_bif_handler(netmsg_t);
473 static void bridge_add_bif_handler(netmsg_t);
474 static void bridge_del_bif(struct bridge_softc *, struct bridge_ifinfo *,
475 struct bridge_iflist_head *);
476 static void bridge_add_bif(struct bridge_softc *, struct bridge_ifinfo *,
479 SYSCTL_DECL(_net_link);
480 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
482 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
483 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
484 static int pfil_member = 1; /* run pfil hooks on the member interface */
485 static int bridge_debug;
486 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
487 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
488 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
489 &pfil_bridge, 0, "Packet filter on the bridge interface");
490 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
491 &pfil_member, 0, "Packet filter on the member interface");
492 SYSCTL_INT(_net_link_bridge, OID_AUTO, debug, CTLFLAG_RW,
493 &bridge_debug, 0, "Bridge debug mode");
495 struct bridge_control_arg {
497 struct ifbreq ifbreq;
498 struct ifbifconf ifbifconf;
499 struct ifbareq ifbareq;
500 struct ifbaconf ifbaconf;
501 struct ifbrparam ifbrparam;
508 struct bridge_control {
509 bridge_ctl_t bc_func;
514 #define BC_F_COPYIN 0x01 /* copy arguments in */
515 #define BC_F_COPYOUT 0x02 /* copy arguments out */
516 #define BC_F_SUSER 0x04 /* do super-user check */
518 const struct bridge_control bridge_control_table[] = {
519 { bridge_ioctl_add, sizeof(struct ifbreq),
520 BC_F_COPYIN|BC_F_SUSER },
521 { bridge_ioctl_del, sizeof(struct ifbreq),
522 BC_F_COPYIN|BC_F_SUSER },
524 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
525 BC_F_COPYIN|BC_F_COPYOUT },
526 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
527 BC_F_COPYIN|BC_F_SUSER },
529 { bridge_ioctl_scache, sizeof(struct ifbrparam),
530 BC_F_COPYIN|BC_F_SUSER },
531 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
534 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
535 BC_F_COPYIN|BC_F_COPYOUT },
536 { bridge_ioctl_rts, sizeof(struct ifbaconf),
537 BC_F_COPYIN|BC_F_COPYOUT },
539 { bridge_ioctl_saddr, sizeof(struct ifbareq),
540 BC_F_COPYIN|BC_F_SUSER },
542 { bridge_ioctl_sto, sizeof(struct ifbrparam),
543 BC_F_COPYIN|BC_F_SUSER },
544 { bridge_ioctl_gto, sizeof(struct ifbrparam),
547 { bridge_ioctl_daddr, sizeof(struct ifbareq),
548 BC_F_COPYIN|BC_F_SUSER },
550 { bridge_ioctl_flush, sizeof(struct ifbreq),
551 BC_F_COPYIN|BC_F_SUSER },
553 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
555 { bridge_ioctl_spri, sizeof(struct ifbrparam),
556 BC_F_COPYIN|BC_F_SUSER },
558 { bridge_ioctl_ght, sizeof(struct ifbrparam),
560 { bridge_ioctl_sht, sizeof(struct ifbrparam),
561 BC_F_COPYIN|BC_F_SUSER },
563 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
565 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
566 BC_F_COPYIN|BC_F_SUSER },
568 { bridge_ioctl_gma, sizeof(struct ifbrparam),
570 { bridge_ioctl_sma, sizeof(struct ifbrparam),
571 BC_F_COPYIN|BC_F_SUSER },
573 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
574 BC_F_COPYIN|BC_F_SUSER },
576 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
577 BC_F_COPYIN|BC_F_SUSER },
579 { bridge_ioctl_addspan, sizeof(struct ifbreq),
580 BC_F_COPYIN|BC_F_SUSER },
581 { bridge_ioctl_delspan, sizeof(struct ifbreq),
582 BC_F_COPYIN|BC_F_SUSER },
584 { bridge_ioctl_sifbondwght, sizeof(struct ifbreq),
585 BC_F_COPYIN|BC_F_SUSER },
588 static const int bridge_control_table_size = NELEM(bridge_control_table);
590 LIST_HEAD(, bridge_softc) bridge_list;
592 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
594 bridge_clone_destroy, 0, IF_MAXUNIT);
597 bridge_modevent(module_t mod, int type, void *data)
601 LIST_INIT(&bridge_list);
602 if_clone_attach(&bridge_cloner);
603 bridge_input_p = bridge_input;
604 bridge_output_p = bridge_output;
605 bridge_interface_p = bridge_interface;
606 bridge_detach_cookie = EVENTHANDLER_REGISTER(
607 ifnet_detach_event, bridge_ifdetach, NULL,
608 EVENTHANDLER_PRI_ANY);
610 bstp_linkstate_p = bstp_linkstate;
614 if (!LIST_EMPTY(&bridge_list))
616 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
617 bridge_detach_cookie);
618 if_clone_detach(&bridge_cloner);
619 bridge_input_p = NULL;
620 bridge_output_p = NULL;
621 bridge_interface_p = NULL;
623 bstp_linkstate_p = NULL;
632 static moduledata_t bridge_mod = {
638 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
642 * bridge_clone_create:
644 * Create a new bridge instance.
647 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
649 struct bridge_softc *sc;
654 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
655 ifp = sc->sc_ifp = &sc->sc_if;
657 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
658 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
659 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
660 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
661 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
662 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
663 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
665 /* Initialize our routing table. */
666 bridge_rtable_init(sc);
668 callout_init(&sc->sc_brcallout);
669 netmsg_init(&sc->sc_brtimemsg, NULL, &netisr_adone_rport,
670 MSGF_DROPABLE, bridge_timer_handler);
671 sc->sc_brtimemsg.lmsg.u.ms_resultp = sc;
673 callout_init(&sc->sc_bstpcallout);
674 netmsg_init(&sc->sc_bstptimemsg, NULL, &netisr_adone_rport,
675 MSGF_DROPABLE, bstp_tick_handler);
676 sc->sc_bstptimemsg.lmsg.u.ms_resultp = sc;
678 /* Initialize per-cpu member iface lists */
679 sc->sc_iflists = kmalloc(sizeof(*sc->sc_iflists) * ncpus,
681 for (cpu = 0; cpu < ncpus; ++cpu)
682 TAILQ_INIT(&sc->sc_iflists[cpu]);
684 TAILQ_INIT(&sc->sc_spanlist);
687 if_initname(ifp, ifc->ifc_name, unit);
688 ifp->if_mtu = ETHERMTU;
689 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
690 ifp->if_ioctl = bridge_ioctl;
691 ifp->if_start = bridge_start;
692 ifp->if_init = bridge_init;
693 ifp->if_type = IFT_ETHER;
694 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
695 ifq_set_ready(&ifp->if_snd);
696 ifp->if_hdrlen = ETHER_HDR_LEN;
699 * Generate a random ethernet address and use the private AC:DE:48
703 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
705 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
707 eaddr[0] &= ~1; /* clear multicast bit */
708 eaddr[0] |= 2; /* set the LAA bit */
710 ether_ifattach(ifp, eaddr, NULL);
711 /* Now undo some of the damage... */
712 ifp->if_baudrate = 0;
713 /*ifp->if_type = IFT_BRIDGE;*/
715 crit_enter(); /* XXX MP */
716 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
723 bridge_delete_dispatch(netmsg_t msg)
725 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
726 struct ifnet *bifp = sc->sc_ifp;
727 struct bridge_iflist *bif;
729 ifnet_serialize_all(bifp);
731 while ((bif = TAILQ_FIRST(&sc->sc_iflists[mycpuid])) != NULL)
732 bridge_delete_member(sc, bif, 0);
734 while ((bif = TAILQ_FIRST(&sc->sc_spanlist)) != NULL)
735 bridge_delete_span(sc, bif);
737 ifnet_deserialize_all(bifp);
739 lwkt_replymsg(&msg->lmsg, 0);
743 * bridge_clone_destroy:
745 * Destroy a bridge instance.
748 bridge_clone_destroy(struct ifnet *ifp)
750 struct bridge_softc *sc = ifp->if_softc;
751 struct netmsg_base msg;
753 ifnet_serialize_all(ifp);
756 ifp->if_flags &= ~IFF_UP;
758 ifnet_deserialize_all(ifp);
760 netmsg_init(&msg, NULL, &curthread->td_msgport,
761 0, bridge_delete_dispatch);
762 msg.lmsg.u.ms_resultp = sc;
763 lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0);
765 crit_enter(); /* XXX MP */
766 LIST_REMOVE(sc, sc_list);
771 /* Tear down the routing table. */
772 bridge_rtable_fini(sc);
774 /* Free per-cpu member iface lists */
775 kfree(sc->sc_iflists, M_DEVBUF);
785 * Handle a control request from the operator.
788 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
790 struct bridge_softc *sc = ifp->if_softc;
791 struct bridge_control_arg args;
792 struct ifdrv *ifd = (struct ifdrv *) data;
793 const struct bridge_control *bc;
796 ASSERT_IFNET_SERIALIZED_ALL(ifp);
805 if (ifd->ifd_cmd >= bridge_control_table_size) {
809 bc = &bridge_control_table[ifd->ifd_cmd];
811 if (cmd == SIOCGDRVSPEC &&
812 (bc->bc_flags & BC_F_COPYOUT) == 0) {
815 } else if (cmd == SIOCSDRVSPEC &&
816 (bc->bc_flags & BC_F_COPYOUT)) {
821 if (bc->bc_flags & BC_F_SUSER) {
822 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
827 if (ifd->ifd_len != bc->bc_argsize ||
828 ifd->ifd_len > sizeof(args.bca_u)) {
833 memset(&args, 0, sizeof(args));
834 if (bc->bc_flags & BC_F_COPYIN) {
835 error = copyin(ifd->ifd_data, &args.bca_u,
841 error = bridge_control(sc, cmd, bc->bc_func, &args);
843 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
847 if (bc->bc_flags & BC_F_COPYOUT) {
848 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
849 if (args.bca_len != 0) {
850 KKASSERT(args.bca_kptr != NULL);
852 error = copyout(args.bca_kptr,
853 args.bca_uptr, args.bca_len);
855 kfree(args.bca_kptr, M_TEMP);
857 KKASSERT(args.bca_kptr == NULL);
860 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
865 if (!(ifp->if_flags & IFF_UP) &&
866 (ifp->if_flags & IFF_RUNNING)) {
868 * If interface is marked down and it is running,
872 } else if ((ifp->if_flags & IFF_UP) &&
873 !(ifp->if_flags & IFF_RUNNING)) {
875 * If interface is marked up and it is stopped, then
882 * If running and link flag state change we have to
883 * reinitialize as well.
885 if ((ifp->if_flags & IFF_RUNNING) &&
886 (ifp->if_flags & (IFF_LINK0|IFF_LINK1|IFF_LINK2)) !=
888 sc->sc_copy_flags = ifp->if_flags &
889 (IFF_LINK0|IFF_LINK1|IFF_LINK2);
890 bridge_control(sc, 0, bridge_ioctl_reinit, NULL);
896 /* Do not allow the MTU to be changed on the bridge */
901 error = ether_ioctl(ifp, cmd, data);
910 * Clear or restore unwanted capabilities on the member interface
913 bridge_mutecaps(struct bridge_ifinfo *bif_info, struct ifnet *ifp, int mute)
917 if (ifp->if_ioctl == NULL)
920 bzero(&ifr, sizeof(ifr));
921 ifr.ifr_reqcap = ifp->if_capenable;
924 /* mask off and save capabilities */
925 bif_info->bifi_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
926 if (bif_info->bifi_mutecap != 0)
927 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
929 /* restore muted capabilities */
930 ifr.ifr_reqcap |= bif_info->bifi_mutecap;
933 if (bif_info->bifi_mutecap != 0) {
934 ifnet_serialize_all(ifp);
935 ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
936 ifnet_deserialize_all(ifp);
941 * bridge_lookup_member:
943 * Lookup a bridge member interface.
945 static struct bridge_iflist *
946 bridge_lookup_member(struct bridge_softc *sc, const char *name)
948 struct bridge_iflist *bif;
950 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
951 if (strcmp(bif->bif_ifp->if_xname, name) == 0)
958 * bridge_lookup_member_if:
960 * Lookup a bridge member interface by ifnet*.
962 static struct bridge_iflist *
963 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
965 struct bridge_iflist *bif;
967 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
968 if (bif->bif_ifp == member_ifp)
975 * bridge_lookup_member_ifinfo:
977 * Lookup a bridge member interface by bridge_ifinfo.
979 static struct bridge_iflist *
980 bridge_lookup_member_ifinfo(struct bridge_softc *sc,
981 struct bridge_ifinfo *bif_info)
983 struct bridge_iflist *bif;
985 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
986 if (bif->bif_info == bif_info)
993 * bridge_delete_member:
995 * Delete the specified member interface.
998 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
1001 struct ifnet *ifs = bif->bif_ifp;
1002 struct ifnet *bifp = sc->sc_ifp;
1003 struct bridge_ifinfo *bif_info = bif->bif_info;
1004 struct bridge_iflist_head saved_bifs;
1006 ASSERT_IFNET_SERIALIZED_ALL(bifp);
1007 KKASSERT(bif_info != NULL);
1009 ifs->if_bridge = NULL;
1012 * Release bridge interface's serializer:
1013 * - To avoid possible dead lock.
1014 * - Various sync operation will block the current thread.
1016 ifnet_deserialize_all(bifp);
1019 switch (ifs->if_type) {
1023 * Take the interface out of promiscuous mode.
1026 bridge_mutecaps(bif_info, ifs, 0);
1033 panic("bridge_delete_member: impossible");
1039 * Remove bifs from percpu linked list.
1041 * Removed bifs are not freed immediately, instead,
1042 * they are saved in saved_bifs. They will be freed
1043 * after we make sure that no one is accessing them,
1044 * i.e. after following netmsg_service_sync()
1046 TAILQ_INIT(&saved_bifs);
1047 bridge_del_bif(sc, bif_info, &saved_bifs);
1050 * Make sure that all protocol threads:
1051 * o see 'ifs' if_bridge is changed
1052 * o know that bif is removed from the percpu linked list
1054 netmsg_service_sync();
1057 * Free the removed bifs
1059 KKASSERT(!TAILQ_EMPTY(&saved_bifs));
1060 while ((bif = TAILQ_FIRST(&saved_bifs)) != NULL) {
1061 TAILQ_REMOVE(&saved_bifs, bif, bif_next);
1062 kfree(bif, M_DEVBUF);
1065 /* See the comment in bridge_ioctl_stop() */
1066 bridge_rtmsg_sync(sc);
1067 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL | IFBF_FLUSHSYNC);
1069 ifnet_serialize_all(bifp);
1071 if (bifp->if_flags & IFF_RUNNING)
1072 bstp_initialization(sc);
1075 * Free the bif_info after bstp_initialization(), so that
1076 * bridge_softc.sc_root_port will not reference a dangling
1079 kfree(bif_info, M_DEVBUF);
1083 * bridge_delete_span:
1085 * Delete the specified span interface.
1088 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1090 KASSERT(bif->bif_ifp->if_bridge == NULL,
1091 ("%s: not a span interface", __func__));
1093 TAILQ_REMOVE(&sc->sc_iflists[mycpuid], bif, bif_next);
1094 kfree(bif, M_DEVBUF);
1098 bridge_ioctl_init(struct bridge_softc *sc, void *arg __unused)
1100 struct ifnet *ifp = sc->sc_ifp;
1102 if (ifp->if_flags & IFF_RUNNING)
1105 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1108 ifp->if_flags |= IFF_RUNNING;
1109 bstp_initialization(sc);
1114 bridge_ioctl_stop(struct bridge_softc *sc, void *arg __unused)
1116 struct ifnet *ifp = sc->sc_ifp;
1118 if ((ifp->if_flags & IFF_RUNNING) == 0)
1121 callout_stop(&sc->sc_brcallout);
1124 lwkt_dropmsg(&sc->sc_brtimemsg.lmsg);
1129 ifp->if_flags &= ~IFF_RUNNING;
1131 ifnet_deserialize_all(ifp);
1133 /* Let everyone know that we are stopped */
1134 netmsg_service_sync();
1137 * Sync ifnetX msgports in the order we forward rtnode
1138 * installation message. This is used to make sure that
1139 * all rtnode installation messages sent by bridge_rtupdate()
1140 * during above netmsg_service_sync() are flushed.
1142 bridge_rtmsg_sync(sc);
1143 bridge_rtflush(sc, IFBF_FLUSHDYN | IFBF_FLUSHSYNC);
1145 ifnet_serialize_all(ifp);
1150 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1152 struct ifbreq *req = arg;
1153 struct bridge_iflist *bif;
1154 struct bridge_ifinfo *bif_info;
1155 struct ifnet *ifs, *bifp;
1159 ASSERT_IFNET_SERIALIZED_ALL(bifp);
1161 ifs = ifunit(req->ifbr_ifsname);
1165 /* If it's in the span list, it can't be a member. */
1166 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1167 if (ifs == bif->bif_ifp)
1170 /* Allow the first Ethernet member to define the MTU */
1171 if (ifs->if_type != IFT_GIF) {
1172 if (TAILQ_EMPTY(&sc->sc_iflists[mycpuid])) {
1173 bifp->if_mtu = ifs->if_mtu;
1174 } else if (bifp->if_mtu != ifs->if_mtu) {
1175 if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname);
1180 if (ifs->if_bridge == sc)
1183 if (ifs->if_bridge != NULL)
1186 bif_info = kmalloc(sizeof(*bif_info), M_DEVBUF, M_WAITOK | M_ZERO);
1187 bif_info->bifi_priority = BSTP_DEFAULT_PORT_PRIORITY;
1188 bif_info->bifi_path_cost = BSTP_DEFAULT_PATH_COST;
1189 bif_info->bifi_ifp = ifs;
1190 bif_info->bifi_bond_weight = 1;
1193 * Release bridge interface's serializer:
1194 * - To avoid possible dead lock.
1195 * - Various sync operation will block the current thread.
1197 ifnet_deserialize_all(bifp);
1199 switch (ifs->if_type) {
1203 * Place the interface into promiscuous mode.
1205 error = ifpromisc(ifs, 1);
1207 ifnet_serialize_all(bifp);
1210 bridge_mutecaps(bif_info, ifs, 1);
1213 case IFT_GIF: /* :^) */
1218 ifnet_serialize_all(bifp);
1223 * Add bifs to percpu linked lists
1225 bridge_add_bif(sc, bif_info, ifs);
1227 ifnet_serialize_all(bifp);
1229 if (bifp->if_flags & IFF_RUNNING)
1230 bstp_initialization(sc);
1235 * Everything has been setup, so let the member interface
1236 * deliver packets to this bridge on its input/output path.
1238 ifs->if_bridge = sc;
1241 if (bif_info != NULL)
1242 kfree(bif_info, M_DEVBUF);
1248 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1250 struct ifbreq *req = arg;
1251 struct bridge_iflist *bif;
1253 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1257 bridge_delete_member(sc, bif, 0);
1263 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1265 struct ifbreq *req = arg;
1266 struct bridge_iflist *bif;
1268 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1271 bridge_ioctl_fillflags(sc, bif, req);
1276 bridge_ioctl_fillflags(struct bridge_softc *sc, struct bridge_iflist *bif,
1279 req->ifbr_ifsflags = bif->bif_flags;
1280 req->ifbr_state = bif->bif_state;
1281 req->ifbr_priority = bif->bif_priority;
1282 req->ifbr_path_cost = bif->bif_path_cost;
1283 req->ifbr_bond_weight = bif->bif_bond_weight;
1284 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1285 if (bif->bif_flags & IFBIF_STP) {
1286 req->ifbr_peer_root = bif->bif_peer_root;
1287 req->ifbr_peer_bridge = bif->bif_peer_bridge;
1288 req->ifbr_peer_cost = bif->bif_peer_cost;
1289 req->ifbr_peer_port = bif->bif_peer_port;
1290 if (bstp_supersedes_port_info(sc, bif)) {
1291 req->ifbr_designated_root = bif->bif_peer_root;
1292 req->ifbr_designated_bridge = bif->bif_peer_bridge;
1293 req->ifbr_designated_cost = bif->bif_peer_cost;
1294 req->ifbr_designated_port = bif->bif_peer_port;
1296 req->ifbr_designated_root = sc->sc_bridge_id;
1297 req->ifbr_designated_bridge = sc->sc_bridge_id;
1298 req->ifbr_designated_cost = bif->bif_path_cost +
1300 req->ifbr_designated_port = bif->bif_port_id;
1303 req->ifbr_peer_root = 0;
1304 req->ifbr_peer_bridge = 0;
1305 req->ifbr_peer_cost = 0;
1306 req->ifbr_peer_port = 0;
1307 req->ifbr_designated_root = 0;
1308 req->ifbr_designated_bridge = 0;
1309 req->ifbr_designated_cost = 0;
1310 req->ifbr_designated_port = 0;
1315 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1317 struct ifbreq *req = arg;
1318 struct bridge_iflist *bif;
1319 struct ifnet *bifp = sc->sc_ifp;
1321 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1325 if (req->ifbr_ifsflags & IFBIF_SPAN) {
1326 /* SPAN is readonly */
1330 if (req->ifbr_ifsflags & IFBIF_STP) {
1331 switch (bif->bif_ifp->if_type) {
1333 /* These can do spanning tree. */
1337 /* Nothing else can. */
1342 bif->bif_flags = (bif->bif_flags & IFBIF_KEEPMASK) |
1343 (req->ifbr_ifsflags & ~IFBIF_KEEPMASK);
1344 if (bifp->if_flags & IFF_RUNNING)
1345 bstp_initialization(sc);
1351 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1353 struct ifbrparam *param = arg;
1354 struct ifnet *ifp = sc->sc_ifp;
1356 sc->sc_brtmax = param->ifbrp_csize;
1358 ifnet_deserialize_all(ifp);
1360 ifnet_serialize_all(ifp);
1366 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1368 struct ifbrparam *param = arg;
1370 param->ifbrp_csize = sc->sc_brtmax;
1376 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1378 struct bridge_control_arg *bc_arg = arg;
1379 struct ifbifconf *bifc = arg;
1380 struct bridge_iflist *bif;
1381 struct ifbreq *breq;
1385 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next)
1387 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1390 if (bifc->ifbic_len == 0) {
1391 bifc->ifbic_len = sizeof(*breq) * count;
1393 } else if (count == 0 || bifc->ifbic_len < sizeof(*breq)) {
1394 bifc->ifbic_len = 0;
1398 len = min(bifc->ifbic_len, sizeof(*breq) * count);
1399 KKASSERT(len >= sizeof(*breq));
1401 breq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1403 bifc->ifbic_len = 0;
1406 bc_arg->bca_kptr = breq;
1409 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1410 if (len < sizeof(*breq))
1413 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1414 sizeof(breq->ifbr_ifsname));
1415 bridge_ioctl_fillflags(sc, bif, breq);
1418 len -= sizeof(*breq);
1420 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1421 if (len < sizeof(*breq))
1424 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1425 sizeof(breq->ifbr_ifsname));
1426 breq->ifbr_ifsflags = bif->bif_flags;
1427 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1430 len -= sizeof(*breq);
1433 bifc->ifbic_len = sizeof(*breq) * count;
1434 KKASSERT(bifc->ifbic_len > 0);
1436 bc_arg->bca_len = bifc->ifbic_len;
1437 bc_arg->bca_uptr = bifc->ifbic_req;
1442 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1444 struct bridge_control_arg *bc_arg = arg;
1445 struct ifbaconf *bac = arg;
1446 struct bridge_rtnode *brt;
1447 struct ifbareq *bareq;
1451 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list)
1454 if (bac->ifbac_len == 0) {
1455 bac->ifbac_len = sizeof(*bareq) * count;
1457 } else if (count == 0 || bac->ifbac_len < sizeof(*bareq)) {
1462 len = min(bac->ifbac_len, sizeof(*bareq) * count);
1463 KKASSERT(len >= sizeof(*bareq));
1465 bareq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1466 if (bareq == NULL) {
1470 bc_arg->bca_kptr = bareq;
1473 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
1474 struct bridge_rtinfo *bri = brt->brt_info;
1477 if (len < sizeof(*bareq))
1480 strlcpy(bareq->ifba_ifsname, bri->bri_ifp->if_xname,
1481 sizeof(bareq->ifba_ifsname));
1482 memcpy(bareq->ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1483 expire = bri->bri_expire;
1484 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1485 time_uptime < expire)
1486 bareq->ifba_expire = expire - time_uptime;
1488 bareq->ifba_expire = 0;
1489 bareq->ifba_flags = bri->bri_flags;
1492 len -= sizeof(*bareq);
1495 bac->ifbac_len = sizeof(*bareq) * count;
1496 KKASSERT(bac->ifbac_len > 0);
1498 bc_arg->bca_len = bac->ifbac_len;
1499 bc_arg->bca_uptr = bac->ifbac_req;
1504 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1506 struct ifbareq *req = arg;
1507 struct bridge_iflist *bif;
1508 struct ifnet *ifp = sc->sc_ifp;
1511 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1513 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1517 ifnet_deserialize_all(ifp);
1518 error = bridge_rtsaddr(sc, req->ifba_dst, bif->bif_ifp,
1520 ifnet_serialize_all(ifp);
1525 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1527 struct ifbrparam *param = arg;
1529 sc->sc_brttimeout = param->ifbrp_ctime;
1535 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1537 struct ifbrparam *param = arg;
1539 param->ifbrp_ctime = sc->sc_brttimeout;
1545 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1547 struct ifbareq *req = arg;
1548 struct ifnet *ifp = sc->sc_ifp;
1551 ifnet_deserialize_all(ifp);
1552 error = bridge_rtdaddr(sc, req->ifba_dst);
1553 ifnet_serialize_all(ifp);
1558 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1560 struct ifbreq *req = arg;
1561 struct ifnet *ifp = sc->sc_ifp;
1563 ifnet_deserialize_all(ifp);
1564 bridge_rtflush(sc, req->ifbr_ifsflags | IFBF_FLUSHSYNC);
1565 ifnet_serialize_all(ifp);
1571 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1573 struct ifbrparam *param = arg;
1575 param->ifbrp_prio = sc->sc_bridge_priority;
1581 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1583 struct ifbrparam *param = arg;
1585 sc->sc_bridge_priority = param->ifbrp_prio;
1587 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1588 bstp_initialization(sc);
1594 bridge_ioctl_reinit(struct bridge_softc *sc, void *arg __unused)
1596 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1597 bstp_initialization(sc);
1602 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1604 struct ifbrparam *param = arg;
1606 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1612 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1614 struct ifbrparam *param = arg;
1616 if (param->ifbrp_hellotime == 0)
1618 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1620 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1621 bstp_initialization(sc);
1627 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1629 struct ifbrparam *param = arg;
1631 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1637 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1639 struct ifbrparam *param = arg;
1641 if (param->ifbrp_fwddelay == 0)
1643 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1645 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1646 bstp_initialization(sc);
1652 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1654 struct ifbrparam *param = arg;
1656 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1662 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1664 struct ifbrparam *param = arg;
1666 if (param->ifbrp_maxage == 0)
1668 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1670 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1671 bstp_initialization(sc);
1677 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1679 struct ifbreq *req = arg;
1680 struct bridge_iflist *bif;
1682 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1686 bif->bif_priority = req->ifbr_priority;
1688 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1689 bstp_initialization(sc);
1695 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1697 struct ifbreq *req = arg;
1698 struct bridge_iflist *bif;
1700 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1704 bif->bif_path_cost = req->ifbr_path_cost;
1706 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1707 bstp_initialization(sc);
1713 bridge_ioctl_sifbondwght(struct bridge_softc *sc, void *arg)
1715 struct ifbreq *req = arg;
1716 struct bridge_iflist *bif;
1718 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1722 bif->bif_bond_weight = req->ifbr_bond_weight;
1724 /* no reinit needed */
1730 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1732 struct ifbreq *req = arg;
1733 struct bridge_iflist *bif;
1735 struct bridge_ifinfo *bif_info;
1737 ifs = ifunit(req->ifbr_ifsname);
1741 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1742 if (ifs == bif->bif_ifp)
1745 if (ifs->if_bridge != NULL)
1748 switch (ifs->if_type) {
1759 * bif_info is needed for bif_flags
1761 bif_info = kmalloc(sizeof(*bif_info), M_DEVBUF, M_WAITOK | M_ZERO);
1762 bif_info->bifi_ifp = ifs;
1764 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
1766 bif->bif_info = bif_info;
1767 bif->bif_flags = IFBIF_SPAN;
1768 /* NOTE: span bif does not need bridge_ifinfo */
1770 TAILQ_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1778 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1780 struct ifbreq *req = arg;
1781 struct bridge_iflist *bif;
1784 ifs = ifunit(req->ifbr_ifsname);
1788 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1789 if (ifs == bif->bif_ifp)
1795 bridge_delete_span(sc, bif);
1797 if (TAILQ_EMPTY(&sc->sc_spanlist))
1804 bridge_ifdetach_dispatch(netmsg_t msg)
1806 struct ifnet *ifp, *bifp;
1807 struct bridge_softc *sc;
1808 struct bridge_iflist *bif;
1810 ifp = msg->lmsg.u.ms_resultp;
1811 sc = ifp->if_bridge;
1813 /* Check if the interface is a bridge member */
1817 ifnet_serialize_all(bifp);
1819 bif = bridge_lookup_member_if(sc, ifp);
1821 bridge_delete_member(sc, bif, 1);
1823 /* XXX Why bif will be NULL? */
1826 ifnet_deserialize_all(bifp);
1830 crit_enter(); /* XXX MP */
1832 /* Check if the interface is a span port */
1833 LIST_FOREACH(sc, &bridge_list, sc_list) {
1836 ifnet_serialize_all(bifp);
1838 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1839 if (ifp == bif->bif_ifp) {
1840 bridge_delete_span(sc, bif);
1844 ifnet_deserialize_all(bifp);
1850 lwkt_replymsg(&msg->lmsg, 0);
1856 * Detach an interface from a bridge. Called when a member
1857 * interface is detaching.
1860 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1862 struct netmsg_base msg;
1864 netmsg_init(&msg, NULL, &curthread->td_msgport,
1865 0, bridge_ifdetach_dispatch);
1866 msg.lmsg.u.ms_resultp = ifp;
1868 lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0);
1874 * Initialize a bridge interface.
1877 bridge_init(void *xsc)
1879 bridge_control(xsc, SIOCSIFFLAGS, bridge_ioctl_init, NULL);
1885 * Stop the bridge interface.
1888 bridge_stop(struct ifnet *ifp)
1890 bridge_control(ifp->if_softc, SIOCSIFFLAGS, bridge_ioctl_stop, NULL);
1894 * Returns TRUE if the packet is being sent 'from us'... from our bridge
1895 * interface or from any member of our bridge interface. This is used
1896 * later on to force the MAC to be the MAC of our bridge interface.
1899 bridge_from_us(struct bridge_softc *sc, struct ether_header *eh)
1901 struct bridge_iflist *bif;
1903 if (memcmp(eh->ether_shost, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN) == 0)
1906 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1907 if (memcmp(eh->ether_shost, IF_LLADDR(bif->bif_ifp),
1908 ETHER_ADDR_LEN) == 0) {
1918 * Enqueue a packet on a bridge member interface.
1922 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1924 struct netmsg_packet *nmp;
1928 nmp = &m->m_hdr.mh_netmsg;
1929 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
1930 0, bridge_enqueue_handler);
1932 nmp->base.lmsg.u.ms_resultp = dst_ifp;
1934 lwkt_sendmsg(netisr_cpuport(mycpuid), &nmp->base.lmsg);
1938 * After looking up dst_if in our forwarding table we still have to
1939 * deal with channel bonding. Find the best interface in the bonding set.
1941 static struct ifnet *
1942 bridge_select_unicast(struct bridge_softc *sc, struct ifnet *dst_if,
1943 int from_blocking, struct mbuf *m)
1945 struct bridge_iflist *bif, *nbif;
1946 struct ifnet *alt_if;
1951 * Unicast, kinda replicates the output side of bridge_output().
1953 * Even though this is a uni-cast packet we may have to select
1954 * an interface from a bonding set.
1956 bif = bridge_lookup_member_if(sc, dst_if);
1958 /* Not a member of the bridge (anymore?) */
1963 * If STP is enabled on the target we are an equal opportunity
1964 * employer and do not necessarily output to dst_if. Instead
1965 * scan available links with the same MAC as the current dst_if
1966 * and choose the best one.
1968 * We also need to do this because arp entries tag onto a particular
1969 * interface and if it happens to be dead then the packets will
1970 * go into a bit bucket.
1972 * If LINK2 is set the matching links are bonded and we-round robin.
1973 * (the MAC address must be the same for the participating links).
1974 * In this case links in a STP FORWARDING or BONDED state are
1975 * allowed for unicast packets.
1977 if (bif->bif_flags & IFBIF_STP) {
1982 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid],
1985 * dst_if may imply a bonding set so we must compare
1988 if (memcmp(IF_LLADDR(bif->bif_ifp),
1990 ETHER_ADDR_LEN) != 0) {
1994 if ((bif->bif_ifp->if_flags & IFF_RUNNING) == 0)
1998 * NOTE: We allow tranmissions through a BLOCKING
1999 * or LEARNING interface only as a last resort.
2000 * We DISALLOW both cases if the receiving
2002 * NOTE: If we send a packet through a learning
2003 * interface the receiving end (if also in
2004 * LEARNING) will throw it away, so this is
2005 * the ultimate last resort.
2007 switch(bif->bif_state) {
2008 case BSTP_IFSTATE_BLOCKING:
2009 if (from_blocking == 0 &&
2010 bif->bif_priority + 256 > alt_priority) {
2011 alt_priority = bif->bif_priority + 256;
2012 alt_if = bif->bif_ifp;
2015 case BSTP_IFSTATE_LEARNING:
2016 if (from_blocking == 0 &&
2017 bif->bif_priority > alt_priority) {
2018 alt_priority = bif->bif_priority;
2019 alt_if = bif->bif_ifp;
2022 case BSTP_IFSTATE_L1BLOCKING:
2023 case BSTP_IFSTATE_LISTENING:
2024 case BSTP_IFSTATE_DISABLED:
2027 /* FORWARDING, BONDED */
2032 * XXX we need to use the toepliz hash or
2033 * something like that instead of
2036 if (sc->sc_ifp->if_flags & IFF_LINK2) {
2037 dst_if = bif->bif_ifp;
2038 if (++bif->bif_bond_count >=
2039 bif->bif_bond_weight) {
2040 bif->bif_bond_count = 0;
2041 TAILQ_REMOVE(&sc->sc_iflists[mycpuid],
2044 &sc->sc_iflists[mycpuid],
2052 * Select best interface in the FORWARDING or
2053 * BONDED set. Well, there shouldn't be any
2054 * in a BONDED state if LINK2 is not set (they
2055 * will all be in a BLOCKING) state, but there
2056 * could be a transitory condition here.
2058 if (bif->bif_priority > priority) {
2059 priority = bif->bif_priority;
2060 dst_if = bif->bif_ifp;
2065 * If no suitable interfaces were found but a suitable
2066 * alternative interface was found, use the alternative
2069 if (priority == 0 && alt_if)
2074 * At this point, we're dealing with a unicast frame
2075 * going to a different interface.
2077 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2086 * Send output from a bridge member interface. This
2087 * performs the bridging function for locally originated
2090 * The mbuf has the Ethernet header already attached. We must
2091 * enqueue or free the mbuf before returning.
2094 bridge_output(struct ifnet *ifp, struct mbuf *m)
2096 struct bridge_softc *sc = ifp->if_bridge;
2097 struct bridge_iflist *bif, *nbif;
2098 struct ether_header *eh;
2099 struct ifnet *dst_if, *alt_if, *bifp;
2103 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2107 * Make sure that we are still a member of a bridge interface.
2118 if (m->m_len < ETHER_HDR_LEN) {
2119 m = m_pullup(m, ETHER_HDR_LEN);
2121 IFNET_STAT_INC(bifp, oerrors, 1);
2125 eh = mtod(m, struct ether_header *);
2126 from_us = bridge_from_us(sc, eh);
2129 * If bridge is down, but the original output interface is up,
2130 * go ahead and send out that interface. Otherwise, the packet
2133 if ((bifp->if_flags & IFF_RUNNING) == 0) {
2139 * If the packet is a multicast, or we don't know a better way to
2140 * get there, send to all interfaces.
2142 if (ETHER_IS_MULTICAST(eh->ether_dhost))
2145 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2147 if (dst_if == NULL) {
2157 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid],
2159 dst_if = bif->bif_ifp;
2161 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2165 * If this is not the original output interface,
2166 * and the interface is participating in spanning
2167 * tree, make sure the port is in a state that
2168 * allows forwarding.
2170 * We keep track of a possible backup IF if we are
2171 * unable to find any interfaces to forward through.
2173 * NOTE: Currently round-robining is not implemented
2174 * across bonded interface groups (needs an
2175 * algorithm to track each group somehow).
2177 * Similarly we track only one alternative
2178 * interface if no suitable interfaces are
2181 if (dst_if != ifp &&
2182 (bif->bif_flags & IFBIF_STP) != 0) {
2183 switch (bif->bif_state) {
2184 case BSTP_IFSTATE_BONDED:
2185 if (bif->bif_priority + 512 >
2188 bif->bif_priority + 512;
2189 alt_if = bif->bif_ifp;
2192 case BSTP_IFSTATE_BLOCKING:
2193 if (bif->bif_priority + 256 >
2196 bif->bif_priority + 256;
2197 alt_if = bif->bif_ifp;
2200 case BSTP_IFSTATE_LEARNING:
2201 if (bif->bif_priority > alt_priority) {
2204 alt_if = bif->bif_ifp;
2207 case BSTP_IFSTATE_L1BLOCKING:
2208 case BSTP_IFSTATE_LISTENING:
2209 case BSTP_IFSTATE_DISABLED:
2217 KKASSERT(used == 0);
2218 if (TAILQ_NEXT(bif, bif_next) == NULL) {
2222 mc = m_copypacket(m, MB_DONTWAIT);
2224 IFNET_STAT_INC(bifp, oerrors, 1);
2230 * If the packet is 'from' us override ether_shost.
2232 bridge_handoff(sc, dst_if, mc, from_us);
2235 if (nbif != NULL && !nbif->bif_onlist) {
2236 KKASSERT(bif->bif_onlist);
2237 nbif = TAILQ_NEXT(bif, bif_next);
2242 * If we couldn't find anything use the backup interface
2245 if (found == 0 && alt_if) {
2246 KKASSERT(used == 0);
2249 bridge_handoff(sc, alt_if, mc, from_us);
2261 dst_if = bridge_select_unicast(sc, dst_if, 0, m);
2268 bridge_handoff(sc, dst_if, m, from_us);
2273 * Returns the bridge interface associated with an ifc.
2274 * Pass ifp->if_bridge (must not be NULL). Used by the ARP
2275 * code to supply the bridge for the is-at info, making
2276 * the bridge responsible for matching local addresses.
2278 * Without this the ARP code will supply bridge member interfaces
2279 * for the is-at which makes it difficult the bridge to fail-over
2280 * interfaces (amoung other things).
2282 static struct ifnet *
2283 bridge_interface(void *if_bridge)
2285 struct bridge_softc *sc = if_bridge;
2286 return (sc->sc_ifp);
2292 * Start output on a bridge.
2295 bridge_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2297 struct bridge_softc *sc = ifp->if_softc;
2299 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2300 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
2302 ifsq_set_oactive(ifsq);
2304 struct ifnet *dst_if = NULL;
2305 struct ether_header *eh;
2308 m = ifsq_dequeue(ifsq);
2313 if (m->m_len < sizeof(*eh)) {
2314 m = m_pullup(m, sizeof(*eh));
2316 IFNET_STAT_INC(ifp, oerrors, 1);
2320 eh = mtod(m, struct ether_header *);
2323 IFNET_STAT_INC(ifp, opackets, 1);
2325 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
2326 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2329 * Multicast or broadcast
2331 if (dst_if == NULL) {
2332 bridge_start_bcast(sc, m);
2339 dst_if = bridge_select_unicast(sc, dst_if, 0, m);
2344 bridge_enqueue(dst_if, m);
2346 ifsq_clr_oactive(ifsq);
2352 * Forward packets received on a bridge interface via the input
2355 * This implements the forwarding function of the bridge.
2358 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
2360 struct bridge_iflist *bif;
2361 struct ifnet *src_if, *dst_if, *ifp;
2362 struct ether_header *eh;
2366 src_if = m->m_pkthdr.rcvif;
2369 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2372 * packet coming in on the bridge is also going out on the bridge,
2373 * but ether code won't adjust output stats for the bridge because
2374 * we are changing the interface to something else.
2376 IFNET_STAT_INC(ifp, opackets, 1);
2377 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
2380 * Look up the bridge_iflist.
2382 bif = bridge_lookup_member_if(sc, src_if);
2384 /* Interface is not a bridge member (anymore?) */
2390 * In spanning tree mode receiving a packet from an interface
2391 * in a BLOCKING state is allowed, it could be a member of last
2392 * resort from the sender's point of view, but forwarding it is
2395 * The sender's spanning tree will eventually sync up and the
2396 * sender will go into a BLOCKING state too (but this still may be
2397 * an interface of last resort during state changes).
2399 if (bif->bif_flags & IFBIF_STP) {
2400 switch (bif->bif_state) {
2401 case BSTP_IFSTATE_L1BLOCKING:
2402 case BSTP_IFSTATE_LISTENING:
2403 case BSTP_IFSTATE_DISABLED:
2407 /* learning, blocking, bonded, forwarding */
2410 from_blocking = (bif->bif_state == BSTP_IFSTATE_BLOCKING);
2415 eh = mtod(m, struct ether_header *);
2418 * If the interface is learning, and the source
2419 * address is valid and not multicast, record
2422 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
2423 from_blocking == 0 &&
2424 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
2425 (eh->ether_shost[0] == 0 &&
2426 eh->ether_shost[1] == 0 &&
2427 eh->ether_shost[2] == 0 &&
2428 eh->ether_shost[3] == 0 &&
2429 eh->ether_shost[4] == 0 &&
2430 eh->ether_shost[5] == 0) == 0) {
2431 bridge_rtupdate(sc, eh->ether_shost, src_if, IFBAF_DYNAMIC);
2435 * Don't forward from an interface in the listening or learning
2436 * state. That is, in the learning state we learn information
2437 * but we throw away the packets.
2439 * We let through packets on interfaces in the blocking state.
2440 * The blocking state is applicable to the send side, not the
2443 if ((bif->bif_flags & IFBIF_STP) != 0 &&
2444 (bif->bif_state == BSTP_IFSTATE_LISTENING ||
2445 bif->bif_state == BSTP_IFSTATE_LEARNING)) {
2451 * At this point, the port either doesn't participate
2452 * in spanning tree or it is in the forwarding state.
2456 * If the packet is unicast, destined for someone on
2457 * "this" side of the bridge, drop it.
2459 * src_if implies the entire bonding set so we have to compare MAC
2460 * addresses and not just if pointers.
2462 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2463 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2464 if (dst_if && memcmp(IF_LLADDR(src_if), IF_LLADDR(dst_if),
2465 ETHER_ADDR_LEN) == 0) {
2470 /* ...forward it to all interfaces. */
2471 IFNET_STAT_INC(ifp, imcasts, 1);
2476 * Brodcast if we do not have forwarding information. However, if
2477 * we received the packet on a blocking interface we do not do this
2478 * (unless you really want to blow up your network).
2480 if (dst_if == NULL) {
2484 bridge_broadcast(sc, src_if, m);
2488 dst_if = bridge_select_unicast(sc, dst_if, from_blocking, m);
2490 if (dst_if == NULL) {
2495 if (inet_pfil_hook.ph_hashooks > 0
2497 || inet6_pfil_hook.ph_hashooks > 0
2500 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2505 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2510 bridge_handoff(sc, dst_if, m, 0);
2516 * Receive input from a member interface. Queue the packet for
2517 * bridging if it is not for us.
2519 static struct mbuf *
2520 bridge_input(struct ifnet *ifp, struct mbuf *m)
2522 struct bridge_softc *sc = ifp->if_bridge;
2523 struct bridge_iflist *bif;
2524 struct ifnet *bifp, *new_ifp;
2525 struct ether_header *eh;
2526 struct mbuf *mc, *mc2;
2528 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2532 * Make sure that we are still a member of a bridge interface.
2540 if ((bifp->if_flags & IFF_RUNNING) == 0)
2544 * Implement support for bridge monitoring. If this flag has been
2545 * set on this interface, discard the packet once we push it through
2546 * the bpf(4) machinery, but before we do, increment various counters
2547 * associated with this bridge.
2549 if (bifp->if_flags & IFF_MONITOR) {
2551 * Change input interface to this bridge
2553 * Update bridge's ifnet statistics
2555 m->m_pkthdr.rcvif = bifp;
2558 IFNET_STAT_INC(bifp, ipackets, 1);
2559 IFNET_STAT_INC(bifp, ibytes, m->m_pkthdr.len);
2560 if (m->m_flags & (M_MCAST | M_BCAST))
2561 IFNET_STAT_INC(bifp, imcasts, 1);
2569 * Handle the ether_header
2571 * In all cases if the packet is destined for us via our MAC
2572 * we must clear BRIDGE_MBUF_TAGGED to ensure that we don't
2573 * repeat the source MAC out the same interface.
2575 * This first test against our bridge MAC is the fast-path.
2577 * NOTE! The bridge interface can serve as an endpoint for
2578 * communication but normally there are no IPs associated
2579 * with it so you cannot route through it. Instead what
2580 * you do is point your default route *THROUGH* the bridge
2581 * to the actual default router for one of the bridged spaces.
2583 * Another possibility is to put all your IP specifications
2584 * on the bridge instead of on the individual interfaces. If
2585 * you do this it should be possible to use the bridge as an
2586 * end point and route (rather than switch) through it using
2587 * the default route or ipfw forwarding rules.
2593 if (m->m_len < ETHER_HDR_LEN) {
2594 m = m_pullup(m, ETHER_HDR_LEN);
2598 eh = mtod(m, struct ether_header *);
2599 m->m_pkthdr.fw_flags |= BRIDGE_MBUF_TAGGED;
2600 bcopy(eh->ether_shost, m->m_pkthdr.ether_br_shost, ETHER_ADDR_LEN);
2602 if ((bridge_debug & 1) &&
2603 (ntohs(eh->ether_type) == ETHERTYPE_ARP ||
2604 ntohs(eh->ether_type) == ETHERTYPE_REVARP)) {
2605 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
2606 "%02x:%02x:%02x:%02x:%02x:%02x type %04x "
2607 "lla %02x:%02x:%02x:%02x:%02x:%02x\n",
2621 ((u_char *)IF_LLADDR(bifp))[0],
2622 ((u_char *)IF_LLADDR(bifp))[1],
2623 ((u_char *)IF_LLADDR(bifp))[2],
2624 ((u_char *)IF_LLADDR(bifp))[3],
2625 ((u_char *)IF_LLADDR(bifp))[4],
2626 ((u_char *)IF_LLADDR(bifp))[5]
2630 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
2632 * If the packet is for us, set the packets source as the
2633 * bridge, and return the packet back to ifnet.if_input for
2636 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
2637 KASSERT(bifp->if_bridge == NULL,
2638 ("loop created in bridge_input"));
2639 if (pfil_member != 0) {
2640 if (inet_pfil_hook.ph_hashooks > 0
2642 || inet6_pfil_hook.ph_hashooks > 0
2645 if (bridge_pfil(&m, NULL, ifp, PFIL_IN) != 0)
2656 * Tap all packets arriving on the bridge, no matter if
2657 * they are local destinations or not. In is in.
2659 * Update bridge's ifnet statistics
2662 IFNET_STAT_INC(bifp, ipackets, 1);
2663 IFNET_STAT_INC(bifp, ibytes, m->m_pkthdr.len);
2664 if (m->m_flags & (M_MCAST | M_BCAST))
2665 IFNET_STAT_INC(bifp, imcasts, 1);
2667 bif = bridge_lookup_member_if(sc, ifp);
2674 if (m->m_flags & (M_BCAST | M_MCAST)) {
2676 * Tap off 802.1D packets; they do not get forwarded.
2678 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2679 ETHER_ADDR_LEN) == 0) {
2680 ifnet_serialize_all(bifp);
2681 bstp_input(sc, bif, m);
2682 ifnet_deserialize_all(bifp);
2684 /* m is freed by bstp_input */
2690 * Other than 802.11d packets, ignore packets if the
2691 * interface is not in a good state.
2693 * NOTE: Broadcast/mcast packets received on a blocking or
2694 * learning interface are allowed for local processing.
2696 * The sending side of a blocked port will stop
2697 * transmitting when a better alternative is found.
2698 * However, later on we will disallow the forwarding
2699 * of bcast/mcsat packets over a blocking interface.
2701 if (bif->bif_flags & IFBIF_STP) {
2702 switch (bif->bif_state) {
2703 case BSTP_IFSTATE_L1BLOCKING:
2704 case BSTP_IFSTATE_LISTENING:
2705 case BSTP_IFSTATE_DISABLED:
2708 /* blocking, learning, bonded, forwarding */
2714 * Make a deep copy of the packet and enqueue the copy
2715 * for bridge processing; return the original packet for
2718 mc = m_dup(m, MB_DONTWAIT);
2723 * It's just too dangerous to allow bcast/mcast over a
2724 * blocked interface, eventually the network will sort
2725 * itself out and a better path will be found.
2727 if ((bif->bif_flags & IFBIF_STP) == 0 ||
2728 bif->bif_state != BSTP_IFSTATE_BLOCKING) {
2729 bridge_forward(sc, mc);
2733 * Reinject the mbuf as arriving on the bridge so we have a
2734 * chance at claiming multicast packets. We can not loop back
2735 * here from ether_input as a bridge is never a member of a
2738 KASSERT(bifp->if_bridge == NULL,
2739 ("loop created in bridge_input"));
2740 mc2 = m_dup(m, MB_DONTWAIT);
2743 /* Keep the layer3 header aligned */
2744 int i = min(mc2->m_pkthdr.len, max_protohdr);
2745 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2750 * Don't tap to bpf(4) again; we have already done
2753 * Leave m_pkthdr.rcvif alone, so ARP replies are
2754 * processed as coming in on the correct interface.
2756 * Clear the bridge flag for local processing in
2757 * case the packet gets routed.
2759 mc2->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
2760 ether_reinput_oncpu(bifp, mc2, 0);
2763 /* Return the original packet for local processing. */
2768 * Input of a unicast packet. We have to allow unicast packets
2769 * input from links in the BLOCKING state as this might be an
2770 * interface of last resort.
2772 * NOTE: We explicitly ignore normal packets received on a link
2773 * in the BLOCKING state. The point of being in that state
2774 * is to avoid getting duplicate packets.
2776 * HOWEVER, if LINK2 is set the normal spanning tree code
2777 * will mark an interface BLOCKING to avoid multi-cast/broadcast
2778 * loops. Unicast packets CAN still loop if we allow the
2779 * case (hence we only do it in LINK2), but it isn't quite as
2780 * bad as a broadcast packet looping.
2782 if (bif->bif_flags & IFBIF_STP) {
2783 switch (bif->bif_state) {
2784 case BSTP_IFSTATE_L1BLOCKING:
2785 case BSTP_IFSTATE_LISTENING:
2786 case BSTP_IFSTATE_DISABLED:
2789 /* blocking, bonded, forwarding, learning */
2795 * Unicast. Make sure it's not for us.
2797 * This loop is MPSAFE; the only blocking operation (bridge_rtupdate)
2798 * is followed by breaking out of the loop.
2800 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2801 if (bif->bif_ifp->if_type != IFT_ETHER)
2805 * It is destined for an interface linked to the bridge.
2806 * We want the bridge itself to take care of link level
2807 * forwarding to member interfaces so reinput on the bridge.
2808 * i.e. if you ping an IP on a target interface associated
2809 * with the bridge, the arp is-at response should indicate
2812 * Only update our addr list when learning if the port
2813 * is not in a blocking state. If it is we still allow
2814 * the packet but we do not try to learn from it.
2816 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
2817 ETHER_ADDR_LEN) == 0) {
2818 if (bif->bif_ifp != ifp) {
2819 /* XXX loop prevention */
2820 m->m_flags |= M_ETHER_BRIDGED;
2822 if ((bif->bif_flags & IFBIF_LEARNING) &&
2823 ((bif->bif_flags & IFBIF_STP) == 0 ||
2824 bif->bif_state != BSTP_IFSTATE_BLOCKING)) {
2825 bridge_rtupdate(sc, eh->ether_shost,
2826 ifp, IFBAF_DYNAMIC);
2828 new_ifp = bifp; /* not bif->bif_ifp */
2829 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
2834 * Ignore received packets that were sent by us.
2836 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
2837 ETHER_ADDR_LEN) == 0) {
2847 * Perform the bridge forwarding function, but disallow bridging
2848 * to interfaces in the blocking state if the packet came in on
2849 * an interface in the blocking state.
2851 bridge_forward(sc, m);
2855 * ether_reinput_oncpu() will reprocess rcvif as
2856 * coming from new_ifp (since we do not specify
2857 * REINPUT_KEEPRCVIF).
2860 if (new_ifp != NULL) {
2862 * Clear the bridge flag for local processing in
2863 * case the packet gets routed.
2865 ether_reinput_oncpu(new_ifp, m, REINPUT_RUNBPF);
2872 * bridge_start_bcast:
2874 * Broadcast the packet sent from bridge to all member
2876 * This is a simplified version of bridge_broadcast(), however,
2877 * this function expects caller to hold bridge's serializer.
2880 bridge_start_bcast(struct bridge_softc *sc, struct mbuf *m)
2882 struct bridge_iflist *bif;
2884 struct ifnet *dst_if, *alt_if, *bifp;
2891 ASSERT_IFNET_SERIALIZED_ALL(bifp);
2894 * Following loop is MPSAFE; nothing is blocking
2897 * NOTE: We transmit through an member in the BLOCKING state only
2903 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2904 dst_if = bif->bif_ifp;
2906 if (bif->bif_flags & IFBIF_STP) {
2907 switch (bif->bif_state) {
2908 case BSTP_IFSTATE_BLOCKING:
2909 if (bif->bif_priority > alt_priority) {
2910 alt_priority = bif->bif_priority;
2911 alt_if = bif->bif_ifp;
2914 case BSTP_IFSTATE_L1BLOCKING:
2915 case BSTP_IFSTATE_DISABLED:
2918 /* listening, learning, bonded, forwarding */
2923 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2924 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2927 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2930 if (TAILQ_NEXT(bif, bif_next) == NULL) {
2934 mc = m_copypacket(m, MB_DONTWAIT);
2936 IFNET_STAT_INC(bifp, oerrors, 1);
2941 bridge_enqueue(dst_if, mc);
2944 if (found == 0 && alt_if) {
2945 KKASSERT(used == 0);
2948 bridge_enqueue(alt_if, mc);
2958 * Send a frame to all interfaces that are members of
2959 * the bridge, except for the one on which the packet
2963 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2966 struct bridge_iflist *bif, *nbif;
2967 struct ether_header *eh;
2969 struct ifnet *dst_if, *alt_if, *bifp;
2977 ASSERT_IFNET_NOT_SERIALIZED_ALL(bifp);
2979 eh = mtod(m, struct ether_header *);
2980 from_us = bridge_from_us(sc, eh);
2982 if (inet_pfil_hook.ph_hashooks > 0
2984 || inet6_pfil_hook.ph_hashooks > 0
2987 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
2992 /* Filter on the bridge interface before broadcasting */
2993 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
3004 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
3005 dst_if = bif->bif_ifp;
3007 if ((dst_if->if_flags & IFF_RUNNING) == 0)
3011 * Don't bounce the packet out the same interface it came
3012 * in on. We have to test MAC addresses because a packet
3013 * can come in a bonded interface and we don't want it to
3014 * be echod out the forwarding interface for the same bonding
3017 if (src_if && memcmp(IF_LLADDR(src_if), IF_LLADDR(dst_if),
3018 ETHER_ADDR_LEN) == 0) {
3023 * Generally speaking we only broadcast through forwarding
3024 * interfaces. If no interfaces are available we select
3025 * a BONDED, BLOCKING, or LEARNING interface to forward
3028 if (bif->bif_flags & IFBIF_STP) {
3029 switch (bif->bif_state) {
3030 case BSTP_IFSTATE_BONDED:
3031 if (bif->bif_priority + 512 > alt_priority) {
3032 alt_priority = bif->bif_priority + 512;
3033 alt_if = bif->bif_ifp;
3036 case BSTP_IFSTATE_BLOCKING:
3037 if (bif->bif_priority + 256 > alt_priority) {
3038 alt_priority = bif->bif_priority + 256;
3039 alt_if = bif->bif_ifp;
3042 case BSTP_IFSTATE_LEARNING:
3043 if (bif->bif_priority > alt_priority) {
3044 alt_priority = bif->bif_priority;
3045 alt_if = bif->bif_ifp;
3048 case BSTP_IFSTATE_L1BLOCKING:
3049 case BSTP_IFSTATE_DISABLED:
3050 case BSTP_IFSTATE_LISTENING:
3058 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
3059 (m->m_flags & (M_BCAST|M_MCAST)) == 0) {
3063 if (TAILQ_NEXT(bif, bif_next) == NULL) {
3067 mc = m_copypacket(m, MB_DONTWAIT);
3069 IFNET_STAT_INC(sc->sc_ifp, oerrors, 1);
3076 * Filter on the output interface. Pass a NULL bridge
3077 * interface pointer so we do not redundantly filter on
3078 * the bridge for each interface we broadcast on.
3080 if (inet_pfil_hook.ph_hashooks > 0
3082 || inet6_pfil_hook.ph_hashooks > 0
3085 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
3090 bridge_handoff(sc, dst_if, mc, from_us);
3092 if (nbif != NULL && !nbif->bif_onlist) {
3093 KKASSERT(bif->bif_onlist);
3094 nbif = TAILQ_NEXT(bif, bif_next);
3098 if (found == 0 && alt_if) {
3099 KKASSERT(used == 0);
3102 bridge_enqueue(alt_if, mc);
3112 * Duplicate a packet out one or more interfaces that are in span mode,
3113 * the original mbuf is unmodified.
3116 bridge_span(struct bridge_softc *sc, struct mbuf *m)
3118 struct bridge_iflist *bif;
3119 struct ifnet *dst_if, *bifp;
3124 ifnet_serialize_all(bifp);
3126 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) {
3127 dst_if = bif->bif_ifp;
3129 if ((dst_if->if_flags & IFF_RUNNING) == 0)
3132 mc = m_copypacket(m, MB_DONTWAIT);
3134 IFNET_STAT_INC(sc->sc_ifp, oerrors, 1);
3137 bridge_enqueue(dst_if, mc);
3140 ifnet_deserialize_all(bifp);
3144 bridge_rtmsg_sync_handler(netmsg_t msg)
3146 ifnet_forwardmsg(&msg->lmsg, mycpuid + 1);
3150 bridge_rtmsg_sync(struct bridge_softc *sc)
3152 struct netmsg_base msg;
3154 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3156 netmsg_init(&msg, NULL, &curthread->td_msgport,
3157 0, bridge_rtmsg_sync_handler);
3158 ifnet_domsg(&msg.lmsg, 0);
3161 static __inline void
3162 bridge_rtinfo_update(struct bridge_rtinfo *bri, struct ifnet *dst_if,
3163 int setflags, uint8_t flags, uint32_t timeo)
3165 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
3166 bri->bri_ifp != dst_if)
3167 bri->bri_ifp = dst_if;
3168 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
3169 bri->bri_expire != time_uptime + timeo)
3170 bri->bri_expire = time_uptime + timeo;
3172 bri->bri_flags = flags;
3176 bridge_rtinstall_oncpu(struct bridge_softc *sc, const uint8_t *dst,
3177 struct ifnet *dst_if, int setflags, uint8_t flags,
3178 struct bridge_rtinfo **bri0)
3180 struct bridge_rtnode *brt;
3181 struct bridge_rtinfo *bri;
3184 brt = bridge_rtnode_lookup(sc, dst);
3187 * rtnode for 'dst' already exists. We inform the
3188 * caller about this by leaving bri0 as NULL. The
3189 * caller will terminate the intallation upon getting
3190 * NULL bri0. However, we still need to update the
3193 KKASSERT(*bri0 == NULL);
3196 bridge_rtinfo_update(brt->brt_info, dst_if, setflags,
3197 flags, sc->sc_brttimeout);
3202 * We only need to check brtcnt on CPU0, since if limit
3203 * is to be exceeded, ENOSPC is returned. Caller knows
3204 * this and will terminate the installation.
3206 if (sc->sc_brtcnt >= sc->sc_brtmax)
3209 KKASSERT(*bri0 == NULL);
3210 bri = kmalloc(sizeof(struct bridge_rtinfo), M_DEVBUF,
3215 bri->bri_flags = IFBAF_DYNAMIC;
3216 bridge_rtinfo_update(bri, dst_if, setflags, flags,
3220 KKASSERT(bri != NULL);
3223 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
3225 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
3226 brt->brt_info = bri;
3228 bridge_rtnode_insert(sc, brt);
3233 bridge_rtinstall_handler(netmsg_t msg)
3235 struct netmsg_brsaddr *brmsg = (struct netmsg_brsaddr *)msg;
3238 error = bridge_rtinstall_oncpu(brmsg->br_softc,
3239 brmsg->br_dst, brmsg->br_dst_if,
3240 brmsg->br_setflags, brmsg->br_flags,
3243 KKASSERT(mycpuid == 0 && brmsg->br_rtinfo == NULL);
3244 lwkt_replymsg(&brmsg->base.lmsg, error);
3246 } else if (brmsg->br_rtinfo == NULL) {
3247 /* rtnode already exists for 'dst' */
3248 KKASSERT(mycpuid == 0);
3249 lwkt_replymsg(&brmsg->base.lmsg, 0);
3252 ifnet_forwardmsg(&brmsg->base.lmsg, mycpuid + 1);
3258 * Add/Update a bridge routing entry.
3261 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
3262 struct ifnet *dst_if, uint8_t flags)
3264 struct bridge_rtnode *brt;
3267 * A route for this destination might already exist. If so,
3268 * update it, otherwise create a new one.
3270 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
3271 struct netmsg_brsaddr *brmsg;
3273 if (sc->sc_brtcnt >= sc->sc_brtmax)
3276 brmsg = kmalloc(sizeof(*brmsg), M_LWKTMSG, M_WAITOK | M_NULLOK);
3280 netmsg_init(&brmsg->base, NULL, &netisr_afree_rport,
3281 0, bridge_rtinstall_handler);
3282 memcpy(brmsg->br_dst, dst, ETHER_ADDR_LEN);
3283 brmsg->br_dst_if = dst_if;
3284 brmsg->br_flags = flags;
3285 brmsg->br_setflags = 0;
3286 brmsg->br_softc = sc;
3287 brmsg->br_rtinfo = NULL;
3289 ifnet_sendmsg(&brmsg->base.lmsg, 0);
3292 bridge_rtinfo_update(brt->brt_info, dst_if, 0, flags,
3298 bridge_rtsaddr(struct bridge_softc *sc, const uint8_t *dst,
3299 struct ifnet *dst_if, uint8_t flags)
3301 struct netmsg_brsaddr brmsg;
3303 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3305 netmsg_init(&brmsg.base, NULL, &curthread->td_msgport,
3306 0, bridge_rtinstall_handler);
3307 memcpy(brmsg.br_dst, dst, ETHER_ADDR_LEN);
3308 brmsg.br_dst_if = dst_if;
3309 brmsg.br_flags = flags;
3310 brmsg.br_setflags = 1;
3311 brmsg.br_softc = sc;
3312 brmsg.br_rtinfo = NULL;
3314 return ifnet_domsg(&brmsg.base.lmsg, 0);
3320 * Lookup the destination interface for an address.
3322 static struct ifnet *
3323 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
3325 struct bridge_rtnode *brt;
3327 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
3329 return brt->brt_info->bri_ifp;
3333 bridge_rtreap_handler(netmsg_t msg)
3335 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
3336 struct bridge_rtnode *brt, *nbrt;
3338 LIST_FOREACH_MUTABLE(brt, &sc->sc_rtlists[mycpuid], brt_list, nbrt) {
3339 if (brt->brt_info->bri_dead)
3340 bridge_rtnode_destroy(sc, brt);
3342 ifnet_forwardmsg(&msg->lmsg, mycpuid + 1);
3346 bridge_rtreap(struct bridge_softc *sc)
3348 struct netmsg_base msg;
3350 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3352 netmsg_init(&msg, NULL, &curthread->td_msgport,
3353 0, bridge_rtreap_handler);
3354 msg.lmsg.u.ms_resultp = sc;
3356 ifnet_domsg(&msg.lmsg, 0);
3360 bridge_rtreap_async(struct bridge_softc *sc)
3362 struct netmsg_base *msg;
3364 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK);
3366 netmsg_init(msg, NULL, &netisr_afree_rport,
3367 0, bridge_rtreap_handler);
3368 msg->lmsg.u.ms_resultp = sc;
3370 ifnet_sendmsg(&msg->lmsg, 0);
3376 * Trim the routine table so that we have a number
3377 * of routing entries less than or equal to the
3381 bridge_rttrim(struct bridge_softc *sc)
3383 struct bridge_rtnode *brt;
3386 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3388 /* Make sure we actually need to do this. */
3389 if (sc->sc_brtcnt <= sc->sc_brtmax)
3393 * Find out how many rtnodes are dead
3395 dead = bridge_rtage_finddead(sc);
3396 KKASSERT(dead <= sc->sc_brtcnt);
3398 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
3399 /* Enough dead rtnodes are found */
3405 * Kill some dynamic rtnodes to meet the brtmax
3407 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3408 struct bridge_rtinfo *bri = brt->brt_info;
3410 if (bri->bri_dead) {
3412 * We have counted this rtnode in
3413 * bridge_rtage_finddead()
3418 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
3421 KKASSERT(dead <= sc->sc_brtcnt);
3423 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
3424 /* Enough rtnodes are collected */
3436 * Aging timer for the bridge.
3439 bridge_timer(void *arg)
3441 struct bridge_softc *sc = arg;
3442 struct netmsg_base *msg;
3444 KKASSERT(mycpuid == BRIDGE_CFGCPU);
3448 if (callout_pending(&sc->sc_brcallout) ||
3449 !callout_active(&sc->sc_brcallout)) {
3453 callout_deactivate(&sc->sc_brcallout);
3455 msg = &sc->sc_brtimemsg;
3456 KKASSERT(msg->lmsg.ms_flags & MSGF_DONE);
3457 lwkt_sendmsg(BRIDGE_CFGPORT, &msg->lmsg);
3463 bridge_timer_handler(netmsg_t msg)
3465 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
3467 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
3471 lwkt_replymsg(&msg->lmsg, 0);
3475 if (sc->sc_ifp->if_flags & IFF_RUNNING) {
3476 callout_reset(&sc->sc_brcallout,
3477 bridge_rtable_prune_period * hz, bridge_timer, sc);
3482 bridge_rtage_finddead(struct bridge_softc *sc)
3484 struct bridge_rtnode *brt;
3487 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3488 struct bridge_rtinfo *bri = brt->brt_info;
3490 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
3491 time_uptime >= bri->bri_expire) {
3494 KKASSERT(dead <= sc->sc_brtcnt);
3503 * Perform an aging cycle.
3506 bridge_rtage(struct bridge_softc *sc)
3508 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3510 if (bridge_rtage_finddead(sc))
3517 * Remove all dynamic addresses from the bridge.
3520 bridge_rtflush(struct bridge_softc *sc, int bf)
3522 struct bridge_rtnode *brt;
3526 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3527 struct bridge_rtinfo *bri = brt->brt_info;
3529 if ((bf & IFBF_FLUSHALL) ||
3530 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
3536 if (bf & IFBF_FLUSHSYNC)
3539 bridge_rtreap_async(sc);
3546 * Remove an address from the table.
3549 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
3551 struct bridge_rtnode *brt;
3553 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3555 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
3558 /* TODO: add a cheaper delete operation */
3559 brt->brt_info->bri_dead = 1;
3567 * Delete routes to a speicifc member interface.
3570 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int bf)
3572 struct bridge_rtnode *brt;
3576 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3577 struct bridge_rtinfo *bri = brt->brt_info;
3579 if (bri->bri_ifp == ifp &&
3580 ((bf & IFBF_FLUSHALL) ||
3581 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) {
3587 if (bf & IFBF_FLUSHSYNC)
3590 bridge_rtreap_async(sc);
3595 * bridge_rtable_init:
3597 * Initialize the route table for this bridge.
3600 bridge_rtable_init(struct bridge_softc *sc)
3605 * Initialize per-cpu hash tables
3607 sc->sc_rthashs = kmalloc(sizeof(*sc->sc_rthashs) * ncpus,
3608 M_DEVBUF, M_WAITOK);
3609 for (cpu = 0; cpu < ncpus; ++cpu) {
3612 sc->sc_rthashs[cpu] =
3613 kmalloc(sizeof(struct bridge_rtnode_head) * BRIDGE_RTHASH_SIZE,
3614 M_DEVBUF, M_WAITOK);
3616 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
3617 LIST_INIT(&sc->sc_rthashs[cpu][i]);
3619 sc->sc_rthash_key = karc4random();
3622 * Initialize per-cpu lists
3624 sc->sc_rtlists = kmalloc(sizeof(struct bridge_rtnode_head) * ncpus,
3625 M_DEVBUF, M_WAITOK);
3626 for (cpu = 0; cpu < ncpus; ++cpu)
3627 LIST_INIT(&sc->sc_rtlists[cpu]);
3631 * bridge_rtable_fini:
3633 * Deconstruct the route table for this bridge.
3636 bridge_rtable_fini(struct bridge_softc *sc)
3641 * Free per-cpu hash tables
3643 for (cpu = 0; cpu < ncpus; ++cpu)
3644 kfree(sc->sc_rthashs[cpu], M_DEVBUF);
3645 kfree(sc->sc_rthashs, M_DEVBUF);
3648 * Free per-cpu lists
3650 kfree(sc->sc_rtlists, M_DEVBUF);
3654 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
3655 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
3657 #define mix(a, b, c) \
3659 a -= b; a -= c; a ^= (c >> 13); \
3660 b -= c; b -= a; b ^= (a << 8); \
3661 c -= a; c -= b; c ^= (b >> 13); \
3662 a -= b; a -= c; a ^= (c >> 12); \
3663 b -= c; b -= a; b ^= (a << 16); \
3664 c -= a; c -= b; c ^= (b >> 5); \
3665 a -= b; a -= c; a ^= (c >> 3); \
3666 b -= c; b -= a; b ^= (a << 10); \
3667 c -= a; c -= b; c ^= (b >> 15); \
3668 } while (/*CONSTCOND*/0)
3670 static __inline uint32_t
3671 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
3673 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
3684 return (c & BRIDGE_RTHASH_MASK);
3690 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
3694 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
3695 d = ((int)a[i]) - ((int)b[i]);
3702 * bridge_rtnode_lookup:
3704 * Look up a bridge route node for the specified destination.
3706 static struct bridge_rtnode *
3707 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
3709 struct bridge_rtnode *brt;
3713 hash = bridge_rthash(sc, addr);
3714 LIST_FOREACH(brt, &sc->sc_rthashs[mycpuid][hash], brt_hash) {
3715 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
3726 * bridge_rtnode_insert:
3728 * Insert the specified bridge node into the route table.
3729 * Caller has to make sure that rtnode does not exist.
3732 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
3734 struct bridge_rtnode *lbrt;
3738 hash = bridge_rthash(sc, brt->brt_addr);
3740 lbrt = LIST_FIRST(&sc->sc_rthashs[mycpuid][hash]);
3742 LIST_INSERT_HEAD(&sc->sc_rthashs[mycpuid][hash],
3748 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
3749 KASSERT(dir != 0, ("rtnode already exist"));
3752 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
3755 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
3756 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
3759 lbrt = LIST_NEXT(lbrt, brt_hash);
3760 } while (lbrt != NULL);
3762 panic("no suitable position found for rtnode");
3764 LIST_INSERT_HEAD(&sc->sc_rtlists[mycpuid], brt, brt_list);
3767 * Update the brtcnt.
3768 * We only need to do it once and we do it on CPU0.
3775 * bridge_rtnode_destroy:
3777 * Destroy a bridge rtnode.
3780 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3782 LIST_REMOVE(brt, brt_hash);
3783 LIST_REMOVE(brt, brt_list);
3785 if (mycpuid + 1 == ncpus) {
3786 /* Free rtinfo associated with rtnode on the last cpu */
3787 kfree(brt->brt_info, M_DEVBUF);
3789 kfree(brt, M_DEVBUF);
3792 /* Update brtcnt only on CPU0 */
3798 bridge_post_pfil(struct mbuf *m)
3800 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED)
3804 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED)
3811 * Send bridge packets through pfil if they are one of the types pfil can deal
3812 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
3813 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3817 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3819 int snap, error, i, hlen;
3820 struct ether_header *eh1, eh2;
3823 u_int16_t ether_type;
3826 error = -1; /* Default error if not error == 0 */
3828 if (pfil_bridge == 0 && pfil_member == 0)
3829 return (0); /* filtering is disabled */
3831 i = min((*mp)->m_pkthdr.len, max_protohdr);
3832 if ((*mp)->m_len < i) {
3833 *mp = m_pullup(*mp, i);
3835 kprintf("%s: m_pullup failed\n", __func__);
3840 eh1 = mtod(*mp, struct ether_header *);
3841 ether_type = ntohs(eh1->ether_type);
3844 * Check for SNAP/LLC.
3846 if (ether_type < ETHERMTU) {
3847 struct llc *llc2 = (struct llc *)(eh1 + 1);
3849 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3850 llc2->llc_dsap == LLC_SNAP_LSAP &&
3851 llc2->llc_ssap == LLC_SNAP_LSAP &&
3852 llc2->llc_control == LLC_UI) {
3853 ether_type = htons(llc2->llc_un.type_snap.ether_type);
3859 * If we're trying to filter bridge traffic, don't look at anything
3860 * other than IP and ARP traffic. If the filter doesn't understand
3861 * IPv6, don't allow IPv6 through the bridge either. This is lame
3862 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3863 * but of course we don't have an AppleTalk filter to begin with.
3864 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3867 switch (ether_type) {
3869 case ETHERTYPE_REVARP:
3870 return (0); /* Automatically pass */
3874 case ETHERTYPE_IPV6:
3880 * Check to see if the user wants to pass non-ip
3881 * packets, these will not be checked by pfil(9)
3882 * and passed unconditionally so the default is to drop.
3888 /* Strip off the Ethernet header and keep a copy. */
3889 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3890 m_adj(*mp, ETHER_HDR_LEN);
3892 /* Strip off snap header, if present */
3894 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3895 m_adj(*mp, sizeof(struct llc));
3899 * Check the IP header for alignment and errors
3901 if (dir == PFIL_IN) {
3902 switch (ether_type) {
3904 error = bridge_ip_checkbasic(mp);
3907 case ETHERTYPE_IPV6:
3908 error = bridge_ip6_checkbasic(mp);
3921 * Run the packet through pfil
3923 switch (ether_type) {
3926 * before calling the firewall, swap fields the same as
3927 * IP does. here we assume the header is contiguous
3929 ip = mtod(*mp, struct ip *);
3931 ip->ip_len = ntohs(ip->ip_len);
3932 ip->ip_off = ntohs(ip->ip_off);
3935 * Run pfil on the member interface and the bridge, both can
3936 * be skipped by clearing pfil_member or pfil_bridge.
3939 * in_if -> bridge_if -> out_if
3941 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) {
3942 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3943 if (*mp == NULL || error != 0) /* filter may consume */
3945 error = bridge_post_pfil(*mp);
3950 if (pfil_member && ifp != NULL) {
3951 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp, dir);
3952 if (*mp == NULL || error != 0) /* filter may consume */
3954 error = bridge_post_pfil(*mp);
3959 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) {
3960 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3961 if (*mp == NULL || error != 0) /* filter may consume */
3963 error = bridge_post_pfil(*mp);
3968 /* check if we need to fragment the packet */
3969 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
3970 i = (*mp)->m_pkthdr.len;
3971 if (i > ifp->if_mtu) {
3972 error = bridge_fragment(ifp, *mp, &eh2, snap,
3978 /* Recalculate the ip checksum and restore byte ordering */
3979 ip = mtod(*mp, struct ip *);
3980 hlen = ip->ip_hl << 2;
3981 if (hlen < sizeof(struct ip))
3983 if (hlen > (*mp)->m_len) {
3984 if ((*mp = m_pullup(*mp, hlen)) == NULL)
3986 ip = mtod(*mp, struct ip *);
3990 ip->ip_len = htons(ip->ip_len);
3991 ip->ip_off = htons(ip->ip_off);
3993 if (hlen == sizeof(struct ip))
3994 ip->ip_sum = in_cksum_hdr(ip);
3996 ip->ip_sum = in_cksum(*mp, hlen);
4000 case ETHERTYPE_IPV6:
4001 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
4002 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
4005 if (*mp == NULL || error != 0) /* filter may consume */
4008 if (pfil_member && ifp != NULL)
4009 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
4012 if (*mp == NULL || error != 0) /* filter may consume */
4015 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
4016 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
4033 * Finally, put everything back the way it was and return
4036 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
4039 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
4042 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
4045 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
4056 * Perform basic checks on header size since
4057 * pfil assumes ip_input has already processed
4058 * it for it. Cut-and-pasted from ip_input.c.
4059 * Given how simple the IPv6 version is,
4060 * does the IPv4 version really need to be
4063 * XXX Should we update ipstat here, or not?
4064 * XXX Right now we update ipstat but not
4068 bridge_ip_checkbasic(struct mbuf **mp)
4070 struct mbuf *m = *mp;
4078 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
4079 if ((m = m_copyup(m, sizeof(struct ip),
4080 (max_linkhdr + 3) & ~3)) == NULL) {
4081 /* XXXJRT new stat, please */
4082 ipstat.ips_toosmall++;
4087 #ifndef __predict_false
4088 #define __predict_false(x) x
4090 if (__predict_false(m->m_len < sizeof (struct ip))) {
4091 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
4092 ipstat.ips_toosmall++;
4096 ip = mtod(m, struct ip *);
4097 if (ip == NULL) goto bad;
4099 if (ip->ip_v != IPVERSION) {
4100 ipstat.ips_badvers++;
4103 hlen = ip->ip_hl << 2;
4104 if (hlen < sizeof(struct ip)) { /* minimum header length */
4105 ipstat.ips_badhlen++;
4108 if (hlen > m->m_len) {
4109 if ((m = m_pullup(m, hlen)) == NULL) {
4110 ipstat.ips_badhlen++;
4113 ip = mtod(m, struct ip *);
4114 if (ip == NULL) goto bad;
4117 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
4118 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
4120 if (hlen == sizeof(struct ip)) {
4121 sum = in_cksum_hdr(ip);
4123 sum = in_cksum(m, hlen);
4127 ipstat.ips_badsum++;
4131 /* Retrieve the packet length. */
4132 len = ntohs(ip->ip_len);
4135 * Check for additional length bogosity
4138 ipstat.ips_badlen++;
4143 * Check that the amount of data in the buffers
4144 * is as at least much as the IP header would have us expect.
4145 * Drop packet if shorter than we expect.
4147 if (m->m_pkthdr.len < len) {
4148 ipstat.ips_tooshort++;
4152 /* Checks out, proceed */
4163 * Same as above, but for IPv6.
4164 * Cut-and-pasted from ip6_input.c.
4165 * XXX Should we update ip6stat, or not?
4168 bridge_ip6_checkbasic(struct mbuf **mp)
4170 struct mbuf *m = *mp;
4171 struct ip6_hdr *ip6;
4174 * If the IPv6 header is not aligned, slurp it up into a new
4175 * mbuf with space for link headers, in the event we forward
4176 * it. Otherwise, if it is aligned, make sure the entire base
4177 * IPv6 header is in the first mbuf of the chain.
4180 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
4181 struct ifnet *inifp = m->m_pkthdr.rcvif;
4182 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
4183 (max_linkhdr + 3) & ~3)) == NULL) {
4184 /* XXXJRT new stat, please */
4185 ip6stat.ip6s_toosmall++;
4186 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
4191 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
4192 struct ifnet *inifp = m->m_pkthdr.rcvif;
4193 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
4194 ip6stat.ip6s_toosmall++;
4195 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
4200 ip6 = mtod(m, struct ip6_hdr *);
4202 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
4203 ip6stat.ip6s_badvers++;
4204 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
4208 /* Checks out, proceed */
4221 * Return a fragmented mbuf chain.
4224 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
4225 int snap, struct llc *llc)
4231 if (m->m_len < sizeof(struct ip) &&
4232 (m = m_pullup(m, sizeof(struct ip))) == NULL)
4234 ip = mtod(m, struct ip *);
4236 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
4241 /* walk the chain and re-add the Ethernet header */
4242 for (m0 = m; m0; m0 = m0->m_nextpkt) {
4245 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
4250 bcopy(llc, mtod(m0, caddr_t),
4251 sizeof(struct llc));
4253 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
4258 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
4264 ipstat.ips_fragmented++;
4275 bridge_enqueue_handler(netmsg_t msg)
4277 struct netmsg_packet *nmp;
4278 struct ifnet *dst_ifp;
4283 dst_ifp = nmp->base.lmsg.u.ms_resultp;
4286 bridge_handoff(dst_ifp->if_bridge, dst_ifp, m, 1);
4290 bridge_handoff(struct bridge_softc *sc, struct ifnet *dst_ifp,
4291 struct mbuf *m, int from_us)
4299 /* We may be sending a fragment so traverse the mbuf */
4301 struct altq_pktattr pktattr;
4304 m->m_nextpkt = NULL;
4307 * If being sent from our host override ether_shost
4308 * with the bridge MAC. This is mandatory for ARP
4309 * so things don't get confused. In particular we
4310 * don't want ARPs to get associated with link interfaces
4311 * under the bridge which might or might not stay valid.
4313 * Also override ether_shost when relaying a packet out
4314 * the same interface it came in on, due to multi-homed
4315 * addresses & default routes, otherwise switches will
4316 * get very confused.
4318 * Otherwise if we are in transparent mode.
4320 if (from_us || m->m_pkthdr.rcvif == dst_ifp) {
4322 offsetof(struct ether_header, ether_shost),
4323 ETHER_ADDR_LEN, IF_LLADDR(sc->sc_ifp));
4324 } else if ((bifp->if_flags & IFF_LINK0) &&
4325 (m->m_pkthdr.fw_flags & BRIDGE_MBUF_TAGGED)) {
4327 offsetof(struct ether_header, ether_shost),
4329 m->m_pkthdr.ether_br_shost);
4330 } /* else retain shost */
4332 if (ifq_is_enabled(&dst_ifp->if_snd))
4333 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
4335 ifq_dispatch(dst_ifp, m, &pktattr);
4340 bridge_control_dispatch(netmsg_t msg)
4342 struct netmsg_brctl *bc_msg = (struct netmsg_brctl *)msg;
4343 struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
4346 ifnet_serialize_all(bifp);
4347 error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
4348 ifnet_deserialize_all(bifp);
4350 lwkt_replymsg(&bc_msg->base.lmsg, error);
4354 bridge_control(struct bridge_softc *sc, u_long cmd,
4355 bridge_ctl_t bc_func, void *bc_arg)
4357 struct ifnet *bifp = sc->sc_ifp;
4358 struct netmsg_brctl bc_msg;
4361 ASSERT_IFNET_SERIALIZED_ALL(bifp);
4363 bzero(&bc_msg, sizeof(bc_msg));
4365 netmsg_init(&bc_msg.base, NULL, &curthread->td_msgport,
4366 0, bridge_control_dispatch);
4367 bc_msg.bc_func = bc_func;
4369 bc_msg.bc_arg = bc_arg;
4371 ifnet_deserialize_all(bifp);
4372 error = lwkt_domsg(BRIDGE_CFGPORT, &bc_msg.base.lmsg, 0);
4373 ifnet_serialize_all(bifp);
4378 bridge_add_bif_handler(netmsg_t msg)
4380 struct netmsg_braddbif *amsg = (struct netmsg_braddbif *)msg;
4381 struct bridge_softc *sc;
4382 struct bridge_iflist *bif;
4384 sc = amsg->br_softc;
4386 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
4387 bif->bif_ifp = amsg->br_bif_ifp;
4388 bif->bif_onlist = 1;
4389 bif->bif_info = amsg->br_bif_info;
4392 * runs through bif_info
4394 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
4396 TAILQ_INSERT_HEAD(&sc->sc_iflists[mycpuid], bif, bif_next);
4398 ifnet_forwardmsg(&amsg->base.lmsg, mycpuid + 1);
4402 bridge_add_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
4405 struct netmsg_braddbif amsg;
4407 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
4409 netmsg_init(&amsg.base, NULL, &curthread->td_msgport,
4410 0, bridge_add_bif_handler);
4412 amsg.br_bif_info = bif_info;
4413 amsg.br_bif_ifp = ifp;
4415 ifnet_domsg(&amsg.base.lmsg, 0);
4419 bridge_del_bif_handler(netmsg_t msg)
4421 struct netmsg_brdelbif *dmsg = (struct netmsg_brdelbif *)msg;
4422 struct bridge_softc *sc;
4423 struct bridge_iflist *bif;
4425 sc = dmsg->br_softc;
4428 * Locate the bif associated with the br_bif_info
4429 * on the current CPU
4431 bif = bridge_lookup_member_ifinfo(sc, dmsg->br_bif_info);
4432 KKASSERT(bif != NULL && bif->bif_onlist);
4434 /* Remove the bif from the current CPU's iflist */
4435 bif->bif_onlist = 0;
4436 TAILQ_REMOVE(dmsg->br_bif_list, bif, bif_next);
4438 /* Save the removed bif for later freeing */
4439 TAILQ_INSERT_HEAD(dmsg->br_bif_list, bif, bif_next);
4441 ifnet_forwardmsg(&dmsg->base.lmsg, mycpuid + 1);
4445 bridge_del_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
4446 struct bridge_iflist_head *saved_bifs)
4448 struct netmsg_brdelbif dmsg;
4450 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
4452 netmsg_init(&dmsg.base, NULL, &curthread->td_msgport,
4453 0, bridge_del_bif_handler);
4455 dmsg.br_bif_info = bif_info;
4456 dmsg.br_bif_list = saved_bifs;
4458 ifnet_domsg(&dmsg.base.lmsg, 0);