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 * netisr0<-----------------------+
122 * rtnode exists?(Y)free nmsg :
129 * +---------->netisr1 :
135 * (w/ nmsg's rtinfo) :
144 * +---------->netisr3
153 * The netmsgs forwarded between netisr2 are allocated with
154 * (M_WAITOK|M_NULLOK), so it will not fail under most cases (route
155 * information is too precious to be not installed :). Since multiple
156 * netisrs may try to install route information for the same dst eaddr,
157 * we look up route information in netisr0. However, this looking up
158 * only need to be performed on netisr0, which is the start point of
159 * 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 <-------------------------------------------+
172 * : delete rtnodes | replymsg
175 * : fwdmsg fwdmsg fwdmsg |
176 * :----------> netisr1 --------> netisr2 --------> netisr3
177 * delete rtnodes delete rtnodes delete rtnodes
178 * w/ dead rtinfo w/ dead rtinfo w/ dead rtinfo
181 * All deleting/flushing operations are serialized by netisr0, so each
182 * operation only reaps the route information marked dead by itself.
185 * Bridge route information adding/deleting/flushing:
186 * Since all operation is serialized by the fixed message flow between
187 * netisrs, it is not possible to create corrupted per-cpu route
192 * XXX This no longer applies.
193 * Percpu member interface list iteration with blocking operation:
194 * Since one bridge could only delete one member interface at a time and
195 * the deleted member interface is not freed after netmsg_service_sync(),
196 * following way is used to make sure that even if the certain member
197 * interface is ripped from the percpu list during the blocking operation,
198 * the iteration still could keep going:
200 * TAILQ_FOREACH_MUTABLE(bif, sc->sc_iflists[mycpuid], bif_next, nbif) {
201 * blocking operation;
202 * blocking operation;
205 * if (nbif != NULL && !nbif->bif_onlist) {
206 * KKASSERT(bif->bif_onlist);
207 * nbif = TAILQ_NEXT(bif, bif_next);
211 * As mentioned above only one member interface could be unlinked from the
212 * percpu member interface list, so either bif or nbif may be not on the list,
213 * but _not_ both. To keep the list iteration, we don't care about bif, but
214 * only nbif. Since removed member interface will only be freed after we
215 * finish our work, it is safe to access any field in an unlinked bif (here
216 * bif_onlist). If nbif is no longer on the list, then bif must be on the
217 * list, so we change nbif to the next element of bif and keep going.
220 #include "opt_inet.h"
221 #include "opt_inet6.h"
223 #include <sys/param.h>
224 #include <sys/mbuf.h>
225 #include <sys/malloc.h>
226 #include <sys/protosw.h>
227 #include <sys/systm.h>
228 #include <sys/time.h>
229 #include <sys/socket.h> /* for net/if.h */
230 #include <sys/sockio.h>
231 #include <sys/ctype.h> /* string functions */
232 #include <sys/kernel.h>
233 #include <sys/random.h>
234 #include <sys/sysctl.h>
235 #include <sys/module.h>
236 #include <sys/proc.h>
237 #include <sys/priv.h>
238 #include <sys/lock.h>
239 #include <sys/thread.h>
240 #include <sys/thread2.h>
241 #include <sys/mpipe.h>
245 #include <net/if_dl.h>
246 #include <net/if_types.h>
247 #include <net/if_var.h>
248 #include <net/pfil.h>
249 #include <net/ifq_var.h>
250 #include <net/if_clone.h>
252 #include <netinet/in.h> /* for struct arpcom */
253 #include <netinet/in_systm.h>
254 #include <netinet/in_var.h>
255 #include <netinet/ip.h>
256 #include <netinet/ip_var.h>
258 #include <netinet/ip6.h>
259 #include <netinet6/ip6_var.h>
261 #include <netinet/if_ether.h> /* for struct arpcom */
262 #include <net/bridge/if_bridgevar.h>
263 #include <net/if_llc.h>
264 #include <net/netmsg2.h>
265 #include <net/netisr2.h>
267 #include <net/route.h>
268 #include <sys/in_cksum.h>
271 * Size of the route hash table. Must be a power of two.
273 #ifndef BRIDGE_RTHASH_SIZE
274 #define BRIDGE_RTHASH_SIZE 1024
277 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
280 * Maximum number of addresses to cache.
282 #ifndef BRIDGE_RTABLE_MAX
283 #define BRIDGE_RTABLE_MAX 4096
287 * Spanning tree defaults.
289 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
290 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
291 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
292 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
293 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
294 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
295 #define BSTP_DEFAULT_PATH_COST 55
298 * Timeout (in seconds) for entries learned dynamically.
300 #ifndef BRIDGE_RTABLE_TIMEOUT
301 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
305 * Number of seconds between walks of the route list.
307 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
308 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
312 * List of capabilities to mask on the member interface.
314 #define BRIDGE_IFCAPS_MASK (IFCAP_TXCSUM | IFCAP_TSO)
316 typedef int (*bridge_ctl_t)(struct bridge_softc *, void *);
318 struct netmsg_brctl {
319 struct netmsg_base base;
320 bridge_ctl_t bc_func;
321 struct bridge_softc *bc_sc;
325 struct netmsg_brsaddr {
326 struct netmsg_base base;
327 struct bridge_softc *br_softc;
328 struct ifnet *br_dst_if;
329 struct bridge_rtinfo *br_rtinfo;
331 uint8_t br_dst[ETHER_ADDR_LEN];
335 struct netmsg_braddbif {
336 struct netmsg_base base;
337 struct bridge_softc *br_softc;
338 struct bridge_ifinfo *br_bif_info;
339 struct ifnet *br_bif_ifp;
342 struct netmsg_brdelbif {
343 struct netmsg_base base;
344 struct bridge_softc *br_softc;
345 struct bridge_ifinfo *br_bif_info;
346 struct bridge_iflist_head *br_bif_list;
349 struct netmsg_brsflags {
350 struct netmsg_base base;
351 struct bridge_softc *br_softc;
352 struct bridge_ifinfo *br_bif_info;
353 uint32_t br_bif_flags;
356 eventhandler_tag bridge_detach_cookie = NULL;
358 extern struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
359 extern int (*bridge_output_p)(struct ifnet *, struct mbuf *);
360 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
361 extern struct ifnet *(*bridge_interface_p)(void *if_bridge);
363 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
365 static int bridge_clone_create(struct if_clone *, int, caddr_t);
366 static int bridge_clone_destroy(struct ifnet *);
368 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
369 static void bridge_mutecaps(struct bridge_ifinfo *, struct ifnet *, int);
370 static void bridge_ifdetach(void *, struct ifnet *);
371 static void bridge_init(void *);
372 static int bridge_from_us(struct bridge_softc *, struct ether_header *);
373 static void bridge_stop(struct ifnet *);
374 static void bridge_start(struct ifnet *, struct ifaltq_subque *);
375 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
376 static int bridge_output(struct ifnet *, struct mbuf *);
377 static struct ifnet *bridge_interface(void *if_bridge);
379 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
381 static void bridge_timer_handler(netmsg_t);
382 static void bridge_timer(void *);
384 static void bridge_start_bcast(struct bridge_softc *, struct mbuf *);
385 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
387 static void bridge_span(struct bridge_softc *, struct mbuf *);
389 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
390 struct ifnet *, uint8_t);
391 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
392 static void bridge_rtreap(struct bridge_softc *);
393 static void bridge_rtreap_async(struct bridge_softc *);
394 static void bridge_rttrim(struct bridge_softc *);
395 static int bridge_rtage_finddead(struct bridge_softc *);
396 static void bridge_rtage(struct bridge_softc *);
397 static void bridge_rtflush(struct bridge_softc *, int);
398 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
399 static int bridge_rtsaddr(struct bridge_softc *, const uint8_t *,
400 struct ifnet *, uint8_t);
401 static void bridge_rtmsg_sync(struct bridge_softc *sc);
402 static void bridge_rtreap_handler(netmsg_t);
403 static void bridge_rtinstall_handler(netmsg_t);
404 static int bridge_rtinstall_oncpu(struct bridge_softc *, const uint8_t *,
405 struct ifnet *, int, uint8_t, struct bridge_rtinfo **);
407 static void bridge_rtable_init(struct bridge_softc *);
408 static void bridge_rtable_fini(struct bridge_softc *);
410 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
411 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
413 static void bridge_rtnode_insert(struct bridge_softc *,
414 struct bridge_rtnode *);
415 static void bridge_rtnode_destroy(struct bridge_softc *,
416 struct bridge_rtnode *);
418 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
420 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
422 static struct bridge_iflist *bridge_lookup_member_ifinfo(struct bridge_softc *,
423 struct bridge_ifinfo *);
424 static void bridge_delete_member(struct bridge_softc *,
425 struct bridge_iflist *, int);
426 static void bridge_delete_span(struct bridge_softc *,
427 struct bridge_iflist *);
429 static int bridge_control(struct bridge_softc *, u_long,
430 bridge_ctl_t, void *);
431 static int bridge_ioctl_init(struct bridge_softc *, void *);
432 static int bridge_ioctl_stop(struct bridge_softc *, void *);
433 static int bridge_ioctl_add(struct bridge_softc *, void *);
434 static int bridge_ioctl_del(struct bridge_softc *, void *);
435 static void bridge_ioctl_fillflags(struct bridge_softc *sc,
436 struct bridge_iflist *bif, struct ifbreq *req);
437 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
438 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
439 static int bridge_ioctl_scache(struct bridge_softc *, void *);
440 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
441 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
442 static int bridge_ioctl_rts(struct bridge_softc *, void *);
443 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
444 static int bridge_ioctl_sto(struct bridge_softc *, void *);
445 static int bridge_ioctl_gto(struct bridge_softc *, void *);
446 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
447 static int bridge_ioctl_flush(struct bridge_softc *, void *);
448 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
449 static int bridge_ioctl_spri(struct bridge_softc *, void *);
450 static int bridge_ioctl_reinit(struct bridge_softc *, void *);
451 static int bridge_ioctl_ght(struct bridge_softc *, void *);
452 static int bridge_ioctl_sht(struct bridge_softc *, void *);
453 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
454 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
455 static int bridge_ioctl_gma(struct bridge_softc *, void *);
456 static int bridge_ioctl_sma(struct bridge_softc *, void *);
457 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
458 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
459 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
460 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
461 static int bridge_ioctl_sifbondwght(struct bridge_softc *, void *);
462 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
464 static int bridge_ip_checkbasic(struct mbuf **mp);
466 static int bridge_ip6_checkbasic(struct mbuf **mp);
468 static int bridge_fragment(struct ifnet *, struct mbuf *,
469 struct ether_header *, int, struct llc *);
470 static void bridge_enqueue_handler(netmsg_t);
471 static void bridge_handoff(struct bridge_softc *, struct ifnet *,
474 static void bridge_del_bif_handler(netmsg_t);
475 static void bridge_add_bif_handler(netmsg_t);
476 static void bridge_del_bif(struct bridge_softc *, struct bridge_ifinfo *,
477 struct bridge_iflist_head *);
478 static void bridge_add_bif(struct bridge_softc *, struct bridge_ifinfo *,
481 SYSCTL_DECL(_net_link);
482 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
484 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
485 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
486 static int pfil_member = 1; /* run pfil hooks on the member interface */
487 static int bridge_debug;
488 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
489 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
490 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
491 &pfil_bridge, 0, "Packet filter on the bridge interface");
492 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
493 &pfil_member, 0, "Packet filter on the member interface");
494 SYSCTL_INT(_net_link_bridge, OID_AUTO, debug, CTLFLAG_RW,
495 &bridge_debug, 0, "Bridge debug mode");
497 struct bridge_control_arg {
499 struct ifbreq ifbreq;
500 struct ifbifconf ifbifconf;
501 struct ifbareq ifbareq;
502 struct ifbaconf ifbaconf;
503 struct ifbrparam ifbrparam;
510 struct bridge_control {
511 bridge_ctl_t bc_func;
516 #define BC_F_COPYIN 0x01 /* copy arguments in */
517 #define BC_F_COPYOUT 0x02 /* copy arguments out */
518 #define BC_F_SUSER 0x04 /* do super-user check */
520 const struct bridge_control bridge_control_table[] = {
521 { bridge_ioctl_add, sizeof(struct ifbreq),
522 BC_F_COPYIN|BC_F_SUSER },
523 { bridge_ioctl_del, sizeof(struct ifbreq),
524 BC_F_COPYIN|BC_F_SUSER },
526 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
527 BC_F_COPYIN|BC_F_COPYOUT },
528 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
529 BC_F_COPYIN|BC_F_SUSER },
531 { bridge_ioctl_scache, sizeof(struct ifbrparam),
532 BC_F_COPYIN|BC_F_SUSER },
533 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
536 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
537 BC_F_COPYIN|BC_F_COPYOUT },
538 { bridge_ioctl_rts, sizeof(struct ifbaconf),
539 BC_F_COPYIN|BC_F_COPYOUT },
541 { bridge_ioctl_saddr, sizeof(struct ifbareq),
542 BC_F_COPYIN|BC_F_SUSER },
544 { bridge_ioctl_sto, sizeof(struct ifbrparam),
545 BC_F_COPYIN|BC_F_SUSER },
546 { bridge_ioctl_gto, sizeof(struct ifbrparam),
549 { bridge_ioctl_daddr, sizeof(struct ifbareq),
550 BC_F_COPYIN|BC_F_SUSER },
552 { bridge_ioctl_flush, sizeof(struct ifbreq),
553 BC_F_COPYIN|BC_F_SUSER },
555 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
557 { bridge_ioctl_spri, sizeof(struct ifbrparam),
558 BC_F_COPYIN|BC_F_SUSER },
560 { bridge_ioctl_ght, sizeof(struct ifbrparam),
562 { bridge_ioctl_sht, sizeof(struct ifbrparam),
563 BC_F_COPYIN|BC_F_SUSER },
565 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
567 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
568 BC_F_COPYIN|BC_F_SUSER },
570 { bridge_ioctl_gma, sizeof(struct ifbrparam),
572 { bridge_ioctl_sma, sizeof(struct ifbrparam),
573 BC_F_COPYIN|BC_F_SUSER },
575 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
576 BC_F_COPYIN|BC_F_SUSER },
578 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
579 BC_F_COPYIN|BC_F_SUSER },
581 { bridge_ioctl_addspan, sizeof(struct ifbreq),
582 BC_F_COPYIN|BC_F_SUSER },
583 { bridge_ioctl_delspan, sizeof(struct ifbreq),
584 BC_F_COPYIN|BC_F_SUSER },
586 { bridge_ioctl_sifbondwght, sizeof(struct ifbreq),
587 BC_F_COPYIN|BC_F_SUSER },
590 static const int bridge_control_table_size = NELEM(bridge_control_table);
592 LIST_HEAD(, bridge_softc) bridge_list;
594 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
596 bridge_clone_destroy, 0, IF_MAXUNIT);
599 bridge_modevent(module_t mod, int type, void *data)
603 LIST_INIT(&bridge_list);
604 if_clone_attach(&bridge_cloner);
605 bridge_input_p = bridge_input;
606 bridge_output_p = bridge_output;
607 bridge_interface_p = bridge_interface;
608 bridge_detach_cookie = EVENTHANDLER_REGISTER(
609 ifnet_detach_event, bridge_ifdetach, NULL,
610 EVENTHANDLER_PRI_ANY);
612 bstp_linkstate_p = bstp_linkstate;
616 if (!LIST_EMPTY(&bridge_list))
618 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
619 bridge_detach_cookie);
620 if_clone_detach(&bridge_cloner);
621 bridge_input_p = NULL;
622 bridge_output_p = NULL;
623 bridge_interface_p = NULL;
625 bstp_linkstate_p = NULL;
634 static moduledata_t bridge_mod = {
640 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
644 * bridge_clone_create:
646 * Create a new bridge instance.
649 bridge_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
651 struct bridge_softc *sc;
656 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
657 ifp = sc->sc_ifp = &sc->sc_if;
659 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
660 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
661 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
662 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
663 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
664 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
665 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
667 /* Initialize our routing table. */
668 bridge_rtable_init(sc);
670 callout_init_mp(&sc->sc_brcallout);
671 netmsg_init(&sc->sc_brtimemsg, NULL, &netisr_adone_rport,
672 MSGF_DROPABLE, bridge_timer_handler);
673 sc->sc_brtimemsg.lmsg.u.ms_resultp = sc;
675 callout_init_mp(&sc->sc_bstpcallout);
676 netmsg_init(&sc->sc_bstptimemsg, NULL, &netisr_adone_rport,
677 MSGF_DROPABLE, bstp_tick_handler);
678 sc->sc_bstptimemsg.lmsg.u.ms_resultp = sc;
680 /* Initialize per-cpu member iface lists */
681 sc->sc_iflists = kmalloc(sizeof(*sc->sc_iflists) * ncpus,
683 for (cpu = 0; cpu < ncpus; ++cpu)
684 TAILQ_INIT(&sc->sc_iflists[cpu]);
686 TAILQ_INIT(&sc->sc_spanlist);
689 if_initname(ifp, ifc->ifc_name, unit);
690 ifp->if_mtu = ETHERMTU;
691 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
692 ifp->if_ioctl = bridge_ioctl;
693 ifp->if_start = bridge_start;
694 ifp->if_init = bridge_init;
695 ifp->if_type = IFT_ETHER;
696 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
697 ifq_set_ready(&ifp->if_snd);
698 ifp->if_hdrlen = ETHER_HDR_LEN;
701 * Generate a random ethernet address and use the private AC:DE:48
705 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
707 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
709 eaddr[0] &= ~1; /* clear multicast bit */
710 eaddr[0] |= 2; /* set the LAA bit */
712 ether_ifattach(ifp, eaddr, NULL);
713 /* Now undo some of the damage... */
714 ifp->if_baudrate = 0;
715 /*ifp->if_type = IFT_BRIDGE;*/
717 crit_enter(); /* XXX MP */
718 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
725 bridge_delete_dispatch(netmsg_t msg)
727 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
728 struct ifnet *bifp = sc->sc_ifp;
729 struct bridge_iflist *bif;
731 ifnet_serialize_all(bifp);
733 while ((bif = TAILQ_FIRST(&sc->sc_iflists[mycpuid])) != NULL)
734 bridge_delete_member(sc, bif, 0);
736 while ((bif = TAILQ_FIRST(&sc->sc_spanlist)) != NULL)
737 bridge_delete_span(sc, bif);
739 ifnet_deserialize_all(bifp);
741 lwkt_replymsg(&msg->lmsg, 0);
745 * bridge_clone_destroy:
747 * Destroy a bridge instance.
750 bridge_clone_destroy(struct ifnet *ifp)
752 struct bridge_softc *sc = ifp->if_softc;
753 struct netmsg_base msg;
755 ifnet_serialize_all(ifp);
758 ifp->if_flags &= ~IFF_UP;
760 ifnet_deserialize_all(ifp);
762 netmsg_init(&msg, NULL, &curthread->td_msgport,
763 0, bridge_delete_dispatch);
764 msg.lmsg.u.ms_resultp = sc;
765 lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0);
767 crit_enter(); /* XXX MP */
768 LIST_REMOVE(sc, sc_list);
773 /* Tear down the routing table. */
774 bridge_rtable_fini(sc);
776 /* Free per-cpu member iface lists */
777 kfree(sc->sc_iflists, M_DEVBUF);
787 * Handle a control request from the operator.
790 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
792 struct bridge_softc *sc = ifp->if_softc;
793 struct bridge_control_arg args;
794 struct ifdrv *ifd = (struct ifdrv *) data;
795 const struct bridge_control *bc;
798 ASSERT_IFNET_SERIALIZED_ALL(ifp);
807 if (ifd->ifd_cmd >= bridge_control_table_size) {
811 bc = &bridge_control_table[ifd->ifd_cmd];
813 if (cmd == SIOCGDRVSPEC &&
814 (bc->bc_flags & BC_F_COPYOUT) == 0) {
817 } else if (cmd == SIOCSDRVSPEC &&
818 (bc->bc_flags & BC_F_COPYOUT)) {
823 if (bc->bc_flags & BC_F_SUSER) {
824 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
829 if (ifd->ifd_len != bc->bc_argsize ||
830 ifd->ifd_len > sizeof(args.bca_u)) {
835 memset(&args, 0, sizeof(args));
836 if (bc->bc_flags & BC_F_COPYIN) {
837 error = copyin(ifd->ifd_data, &args.bca_u,
843 error = bridge_control(sc, cmd, bc->bc_func, &args);
845 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
849 if (bc->bc_flags & BC_F_COPYOUT) {
850 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
851 if (args.bca_len != 0) {
852 KKASSERT(args.bca_kptr != NULL);
854 error = copyout(args.bca_kptr,
855 args.bca_uptr, args.bca_len);
857 kfree(args.bca_kptr, M_TEMP);
859 KKASSERT(args.bca_kptr == NULL);
862 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
867 if (!(ifp->if_flags & IFF_UP) &&
868 (ifp->if_flags & IFF_RUNNING)) {
870 * If interface is marked down and it is running,
874 } else if ((ifp->if_flags & IFF_UP) &&
875 !(ifp->if_flags & IFF_RUNNING)) {
877 * If interface is marked up and it is stopped, then
884 * If running and link flag state change we have to
885 * reinitialize as well.
887 if ((ifp->if_flags & IFF_RUNNING) &&
888 (ifp->if_flags & (IFF_LINK0|IFF_LINK1|IFF_LINK2)) !=
890 sc->sc_copy_flags = ifp->if_flags &
891 (IFF_LINK0|IFF_LINK1|IFF_LINK2);
892 bridge_control(sc, 0, bridge_ioctl_reinit, NULL);
898 /* Do not allow the MTU to be changed on the bridge */
903 error = ether_ioctl(ifp, cmd, data);
912 * Clear or restore unwanted capabilities on the member interface
915 bridge_mutecaps(struct bridge_ifinfo *bif_info, struct ifnet *ifp, int mute)
919 if (ifp->if_ioctl == NULL)
922 bzero(&ifr, sizeof(ifr));
923 ifr.ifr_reqcap = ifp->if_capenable;
926 /* mask off and save capabilities */
927 bif_info->bifi_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
928 if (bif_info->bifi_mutecap != 0)
929 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
931 /* restore muted capabilities */
932 ifr.ifr_reqcap |= bif_info->bifi_mutecap;
935 if (bif_info->bifi_mutecap != 0) {
936 ifnet_serialize_all(ifp);
937 ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
938 ifnet_deserialize_all(ifp);
943 * bridge_lookup_member:
945 * Lookup a bridge member interface.
947 static struct bridge_iflist *
948 bridge_lookup_member(struct bridge_softc *sc, const char *name)
950 struct bridge_iflist *bif;
952 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
953 if (strcmp(bif->bif_ifp->if_xname, name) == 0)
960 * bridge_lookup_member_if:
962 * Lookup a bridge member interface by ifnet*.
964 static struct bridge_iflist *
965 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
967 struct bridge_iflist *bif;
969 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
970 if (bif->bif_ifp == member_ifp)
977 * bridge_lookup_member_ifinfo:
979 * Lookup a bridge member interface by bridge_ifinfo.
981 static struct bridge_iflist *
982 bridge_lookup_member_ifinfo(struct bridge_softc *sc,
983 struct bridge_ifinfo *bif_info)
985 struct bridge_iflist *bif;
987 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
988 if (bif->bif_info == bif_info)
995 * bridge_delete_member:
997 * Delete the specified member interface.
1000 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
1003 struct ifnet *ifs = bif->bif_ifp;
1004 struct ifnet *bifp = sc->sc_ifp;
1005 struct bridge_ifinfo *bif_info = bif->bif_info;
1006 struct bridge_iflist_head saved_bifs;
1008 ASSERT_IFNET_SERIALIZED_ALL(bifp);
1009 KKASSERT(bif_info != NULL);
1011 ifs->if_bridge = NULL;
1014 * Release bridge interface's serializer:
1015 * - To avoid possible dead lock.
1016 * - Various sync operation will block the current thread.
1018 ifnet_deserialize_all(bifp);
1021 switch (ifs->if_type) {
1025 * Take the interface out of promiscuous mode.
1028 bridge_mutecaps(bif_info, ifs, 0);
1035 panic("bridge_delete_member: impossible");
1041 * Remove bifs from percpu linked list.
1043 * Removed bifs are not freed immediately, instead,
1044 * they are saved in saved_bifs. They will be freed
1045 * after we make sure that no one is accessing them,
1046 * i.e. after following netmsg_service_sync()
1048 TAILQ_INIT(&saved_bifs);
1049 bridge_del_bif(sc, bif_info, &saved_bifs);
1052 * Make sure that all protocol threads:
1053 * o see 'ifs' if_bridge is changed
1054 * o know that bif is removed from the percpu linked list
1056 netmsg_service_sync();
1059 * Free the removed bifs
1061 KKASSERT(!TAILQ_EMPTY(&saved_bifs));
1062 while ((bif = TAILQ_FIRST(&saved_bifs)) != NULL) {
1063 TAILQ_REMOVE(&saved_bifs, bif, bif_next);
1064 kfree(bif, M_DEVBUF);
1067 /* See the comment in bridge_ioctl_stop() */
1068 bridge_rtmsg_sync(sc);
1069 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL | IFBF_FLUSHSYNC);
1071 ifnet_serialize_all(bifp);
1073 if (bifp->if_flags & IFF_RUNNING)
1074 bstp_initialization(sc);
1077 * Free the bif_info after bstp_initialization(), so that
1078 * bridge_softc.sc_root_port will not reference a dangling
1081 kfree(bif_info, M_DEVBUF);
1085 * bridge_delete_span:
1087 * Delete the specified span interface.
1090 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1092 KASSERT(bif->bif_ifp->if_bridge == NULL,
1093 ("%s: not a span interface", __func__));
1095 TAILQ_REMOVE(&sc->sc_iflists[mycpuid], bif, bif_next);
1096 kfree(bif, M_DEVBUF);
1100 bridge_ioctl_init(struct bridge_softc *sc, void *arg __unused)
1102 struct ifnet *ifp = sc->sc_ifp;
1104 if (ifp->if_flags & IFF_RUNNING)
1107 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1110 ifp->if_flags |= IFF_RUNNING;
1111 bstp_initialization(sc);
1116 bridge_ioctl_stop(struct bridge_softc *sc, void *arg __unused)
1118 struct ifnet *ifp = sc->sc_ifp;
1120 if ((ifp->if_flags & IFF_RUNNING) == 0)
1123 callout_stop(&sc->sc_brcallout);
1126 lwkt_dropmsg(&sc->sc_brtimemsg.lmsg);
1131 ifp->if_flags &= ~IFF_RUNNING;
1133 ifnet_deserialize_all(ifp);
1135 /* Let everyone know that we are stopped */
1136 netmsg_service_sync();
1139 * Sync ifnetX msgports in the order we forward rtnode
1140 * installation message. This is used to make sure that
1141 * all rtnode installation messages sent by bridge_rtupdate()
1142 * during above netmsg_service_sync() are flushed.
1144 bridge_rtmsg_sync(sc);
1145 bridge_rtflush(sc, IFBF_FLUSHDYN | IFBF_FLUSHSYNC);
1147 ifnet_serialize_all(ifp);
1152 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1154 struct ifbreq *req = arg;
1155 struct bridge_iflist *bif;
1156 struct bridge_ifinfo *bif_info;
1157 struct ifnet *ifs, *bifp;
1161 ASSERT_IFNET_SERIALIZED_ALL(bifp);
1163 ifs = ifunit_netisr(req->ifbr_ifsname);
1167 /* If it's in the span list, it can't be a member. */
1168 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1169 if (ifs == bif->bif_ifp)
1172 /* Allow the first Ethernet member to define the MTU */
1173 if (ifs->if_type != IFT_GIF) {
1174 if (TAILQ_EMPTY(&sc->sc_iflists[mycpuid])) {
1175 bifp->if_mtu = ifs->if_mtu;
1176 } else if (bifp->if_mtu != ifs->if_mtu) {
1177 if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname);
1182 if (ifs->if_bridge == sc)
1185 if (ifs->if_bridge != NULL)
1188 bif_info = kmalloc(sizeof(*bif_info), M_DEVBUF, M_WAITOK | M_ZERO);
1189 bif_info->bifi_priority = BSTP_DEFAULT_PORT_PRIORITY;
1190 bif_info->bifi_path_cost = BSTP_DEFAULT_PATH_COST;
1191 bif_info->bifi_ifp = ifs;
1192 bif_info->bifi_bond_weight = 1;
1195 * Release bridge interface's serializer:
1196 * - To avoid possible dead lock.
1197 * - Various sync operation will block the current thread.
1199 ifnet_deserialize_all(bifp);
1201 switch (ifs->if_type) {
1205 * Place the interface into promiscuous mode.
1207 error = ifpromisc(ifs, 1);
1209 ifnet_serialize_all(bifp);
1212 bridge_mutecaps(bif_info, ifs, 1);
1215 case IFT_GIF: /* :^) */
1220 ifnet_serialize_all(bifp);
1225 * Add bifs to percpu linked lists
1227 bridge_add_bif(sc, bif_info, ifs);
1229 ifnet_serialize_all(bifp);
1231 if (bifp->if_flags & IFF_RUNNING)
1232 bstp_initialization(sc);
1237 * Everything has been setup, so let the member interface
1238 * deliver packets to this bridge on its input/output path.
1240 ifs->if_bridge = sc;
1243 if (bif_info != NULL)
1244 kfree(bif_info, M_DEVBUF);
1250 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1252 struct ifbreq *req = arg;
1253 struct bridge_iflist *bif;
1255 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1259 bridge_delete_member(sc, bif, 0);
1265 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1267 struct ifbreq *req = arg;
1268 struct bridge_iflist *bif;
1270 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1273 bridge_ioctl_fillflags(sc, bif, req);
1278 bridge_ioctl_fillflags(struct bridge_softc *sc, struct bridge_iflist *bif,
1281 req->ifbr_ifsflags = bif->bif_flags;
1282 req->ifbr_state = bif->bif_state;
1283 req->ifbr_priority = bif->bif_priority;
1284 req->ifbr_path_cost = bif->bif_path_cost;
1285 req->ifbr_bond_weight = bif->bif_bond_weight;
1286 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1287 if (bif->bif_flags & IFBIF_STP) {
1288 req->ifbr_peer_root = bif->bif_peer_root;
1289 req->ifbr_peer_bridge = bif->bif_peer_bridge;
1290 req->ifbr_peer_cost = bif->bif_peer_cost;
1291 req->ifbr_peer_port = bif->bif_peer_port;
1292 if (bstp_supersedes_port_info(sc, bif)) {
1293 req->ifbr_designated_root = bif->bif_peer_root;
1294 req->ifbr_designated_bridge = bif->bif_peer_bridge;
1295 req->ifbr_designated_cost = bif->bif_peer_cost;
1296 req->ifbr_designated_port = bif->bif_peer_port;
1298 req->ifbr_designated_root = sc->sc_bridge_id;
1299 req->ifbr_designated_bridge = sc->sc_bridge_id;
1300 req->ifbr_designated_cost = bif->bif_path_cost +
1302 req->ifbr_designated_port = bif->bif_port_id;
1305 req->ifbr_peer_root = 0;
1306 req->ifbr_peer_bridge = 0;
1307 req->ifbr_peer_cost = 0;
1308 req->ifbr_peer_port = 0;
1309 req->ifbr_designated_root = 0;
1310 req->ifbr_designated_bridge = 0;
1311 req->ifbr_designated_cost = 0;
1312 req->ifbr_designated_port = 0;
1317 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1319 struct ifbreq *req = arg;
1320 struct bridge_iflist *bif;
1321 struct ifnet *bifp = sc->sc_ifp;
1323 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1327 if (req->ifbr_ifsflags & IFBIF_SPAN) {
1328 /* SPAN is readonly */
1332 if (req->ifbr_ifsflags & IFBIF_STP) {
1333 switch (bif->bif_ifp->if_type) {
1335 /* These can do spanning tree. */
1339 /* Nothing else can. */
1344 bif->bif_flags = (bif->bif_flags & IFBIF_KEEPMASK) |
1345 (req->ifbr_ifsflags & ~IFBIF_KEEPMASK);
1346 if (bifp->if_flags & IFF_RUNNING)
1347 bstp_initialization(sc);
1353 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1355 struct ifbrparam *param = arg;
1356 struct ifnet *ifp = sc->sc_ifp;
1358 sc->sc_brtmax = param->ifbrp_csize;
1360 ifnet_deserialize_all(ifp);
1362 ifnet_serialize_all(ifp);
1368 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1370 struct ifbrparam *param = arg;
1372 param->ifbrp_csize = sc->sc_brtmax;
1378 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1380 struct bridge_control_arg *bc_arg = arg;
1381 struct ifbifconf *bifc = arg;
1382 struct bridge_iflist *bif;
1383 struct ifbreq *breq;
1387 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next)
1389 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1392 if (bifc->ifbic_len == 0) {
1393 bifc->ifbic_len = sizeof(*breq) * count;
1395 } else if (count == 0 || bifc->ifbic_len < sizeof(*breq)) {
1396 bifc->ifbic_len = 0;
1400 len = min(bifc->ifbic_len, sizeof(*breq) * count);
1401 KKASSERT(len >= sizeof(*breq));
1403 breq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1405 bifc->ifbic_len = 0;
1408 bc_arg->bca_kptr = breq;
1411 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1412 if (len < sizeof(*breq))
1415 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1416 sizeof(breq->ifbr_ifsname));
1417 bridge_ioctl_fillflags(sc, bif, breq);
1420 len -= sizeof(*breq);
1422 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1423 if (len < sizeof(*breq))
1426 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1427 sizeof(breq->ifbr_ifsname));
1428 breq->ifbr_ifsflags = bif->bif_flags;
1429 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1432 len -= sizeof(*breq);
1435 bifc->ifbic_len = sizeof(*breq) * count;
1436 KKASSERT(bifc->ifbic_len > 0);
1438 bc_arg->bca_len = bifc->ifbic_len;
1439 bc_arg->bca_uptr = bifc->ifbic_req;
1444 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1446 struct bridge_control_arg *bc_arg = arg;
1447 struct ifbaconf *bac = arg;
1448 struct bridge_rtnode *brt;
1449 struct ifbareq *bareq;
1453 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list)
1456 if (bac->ifbac_len == 0) {
1457 bac->ifbac_len = sizeof(*bareq) * count;
1459 } else if (count == 0 || bac->ifbac_len < sizeof(*bareq)) {
1464 len = min(bac->ifbac_len, sizeof(*bareq) * count);
1465 KKASSERT(len >= sizeof(*bareq));
1467 bareq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1468 if (bareq == NULL) {
1472 bc_arg->bca_kptr = bareq;
1475 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
1476 struct bridge_rtinfo *bri = brt->brt_info;
1479 if (len < sizeof(*bareq))
1482 strlcpy(bareq->ifba_ifsname, bri->bri_ifp->if_xname,
1483 sizeof(bareq->ifba_ifsname));
1484 memcpy(bareq->ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1485 expire = bri->bri_expire;
1486 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1487 time_uptime < expire)
1488 bareq->ifba_expire = expire - time_uptime;
1490 bareq->ifba_expire = 0;
1491 bareq->ifba_flags = bri->bri_flags;
1494 len -= sizeof(*bareq);
1497 bac->ifbac_len = sizeof(*bareq) * count;
1498 KKASSERT(bac->ifbac_len > 0);
1500 bc_arg->bca_len = bac->ifbac_len;
1501 bc_arg->bca_uptr = bac->ifbac_req;
1506 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1508 struct ifbareq *req = arg;
1509 struct bridge_iflist *bif;
1510 struct ifnet *ifp = sc->sc_ifp;
1513 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1515 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1519 ifnet_deserialize_all(ifp);
1520 error = bridge_rtsaddr(sc, req->ifba_dst, bif->bif_ifp,
1522 ifnet_serialize_all(ifp);
1527 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1529 struct ifbrparam *param = arg;
1531 sc->sc_brttimeout = param->ifbrp_ctime;
1537 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1539 struct ifbrparam *param = arg;
1541 param->ifbrp_ctime = sc->sc_brttimeout;
1547 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1549 struct ifbareq *req = arg;
1550 struct ifnet *ifp = sc->sc_ifp;
1553 ifnet_deserialize_all(ifp);
1554 error = bridge_rtdaddr(sc, req->ifba_dst);
1555 ifnet_serialize_all(ifp);
1560 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1562 struct ifbreq *req = arg;
1563 struct ifnet *ifp = sc->sc_ifp;
1565 ifnet_deserialize_all(ifp);
1566 bridge_rtflush(sc, req->ifbr_ifsflags | IFBF_FLUSHSYNC);
1567 ifnet_serialize_all(ifp);
1573 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1575 struct ifbrparam *param = arg;
1577 param->ifbrp_prio = sc->sc_bridge_priority;
1583 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1585 struct ifbrparam *param = arg;
1587 sc->sc_bridge_priority = param->ifbrp_prio;
1589 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1590 bstp_initialization(sc);
1596 bridge_ioctl_reinit(struct bridge_softc *sc, void *arg __unused)
1598 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1599 bstp_initialization(sc);
1604 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1606 struct ifbrparam *param = arg;
1608 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1614 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1616 struct ifbrparam *param = arg;
1618 if (param->ifbrp_hellotime == 0)
1620 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1622 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1623 bstp_initialization(sc);
1629 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1631 struct ifbrparam *param = arg;
1633 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1639 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1641 struct ifbrparam *param = arg;
1643 if (param->ifbrp_fwddelay == 0)
1645 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1647 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1648 bstp_initialization(sc);
1654 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1656 struct ifbrparam *param = arg;
1658 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1664 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1666 struct ifbrparam *param = arg;
1668 if (param->ifbrp_maxage == 0)
1670 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1672 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1673 bstp_initialization(sc);
1679 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1681 struct ifbreq *req = arg;
1682 struct bridge_iflist *bif;
1684 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1688 bif->bif_priority = req->ifbr_priority;
1690 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1691 bstp_initialization(sc);
1697 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1699 struct ifbreq *req = arg;
1700 struct bridge_iflist *bif;
1702 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1706 bif->bif_path_cost = req->ifbr_path_cost;
1708 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1709 bstp_initialization(sc);
1715 bridge_ioctl_sifbondwght(struct bridge_softc *sc, void *arg)
1717 struct ifbreq *req = arg;
1718 struct bridge_iflist *bif;
1720 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1724 bif->bif_bond_weight = req->ifbr_bond_weight;
1726 /* no reinit needed */
1732 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1734 struct ifbreq *req = arg;
1735 struct bridge_iflist *bif;
1737 struct bridge_ifinfo *bif_info;
1739 ifs = ifunit_netisr(req->ifbr_ifsname);
1743 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1744 if (ifs == bif->bif_ifp)
1747 if (ifs->if_bridge != NULL)
1750 switch (ifs->if_type) {
1761 * bif_info is needed for bif_flags
1763 bif_info = kmalloc(sizeof(*bif_info), M_DEVBUF, M_WAITOK | M_ZERO);
1764 bif_info->bifi_ifp = ifs;
1766 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
1768 bif->bif_info = bif_info;
1769 bif->bif_flags = IFBIF_SPAN;
1770 /* NOTE: span bif does not need bridge_ifinfo */
1772 TAILQ_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1780 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1782 struct ifbreq *req = arg;
1783 struct bridge_iflist *bif;
1786 ifs = ifunit_netisr(req->ifbr_ifsname);
1790 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1791 if (ifs == bif->bif_ifp)
1797 bridge_delete_span(sc, bif);
1799 if (TAILQ_EMPTY(&sc->sc_spanlist))
1806 bridge_ifdetach_dispatch(netmsg_t msg)
1808 struct ifnet *ifp, *bifp;
1809 struct bridge_softc *sc;
1810 struct bridge_iflist *bif;
1812 ifp = msg->lmsg.u.ms_resultp;
1813 sc = ifp->if_bridge;
1815 /* Check if the interface is a bridge member */
1819 ifnet_serialize_all(bifp);
1821 bif = bridge_lookup_member_if(sc, ifp);
1823 bridge_delete_member(sc, bif, 1);
1825 /* XXX Why bif will be NULL? */
1828 ifnet_deserialize_all(bifp);
1832 crit_enter(); /* XXX MP */
1834 /* Check if the interface is a span port */
1835 LIST_FOREACH(sc, &bridge_list, sc_list) {
1838 ifnet_serialize_all(bifp);
1840 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next)
1841 if (ifp == bif->bif_ifp) {
1842 bridge_delete_span(sc, bif);
1846 ifnet_deserialize_all(bifp);
1852 lwkt_replymsg(&msg->lmsg, 0);
1858 * Detach an interface from a bridge. Called when a member
1859 * interface is detaching.
1862 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1864 struct netmsg_base msg;
1866 netmsg_init(&msg, NULL, &curthread->td_msgport,
1867 0, bridge_ifdetach_dispatch);
1868 msg.lmsg.u.ms_resultp = ifp;
1870 lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0);
1876 * Initialize a bridge interface.
1879 bridge_init(void *xsc)
1881 bridge_control(xsc, SIOCSIFFLAGS, bridge_ioctl_init, NULL);
1887 * Stop the bridge interface.
1890 bridge_stop(struct ifnet *ifp)
1892 bridge_control(ifp->if_softc, SIOCSIFFLAGS, bridge_ioctl_stop, NULL);
1896 * Returns TRUE if the packet is being sent 'from us'... from our bridge
1897 * interface or from any member of our bridge interface. This is used
1898 * later on to force the MAC to be the MAC of our bridge interface.
1901 bridge_from_us(struct bridge_softc *sc, struct ether_header *eh)
1903 struct bridge_iflist *bif;
1905 if (memcmp(eh->ether_shost, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN) == 0)
1908 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1909 if (memcmp(eh->ether_shost, IF_LLADDR(bif->bif_ifp),
1910 ETHER_ADDR_LEN) == 0) {
1920 * Enqueue a packet on a bridge member interface.
1924 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1926 struct netmsg_packet *nmp;
1930 nmp = &m->m_hdr.mh_netmsg;
1931 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
1932 0, bridge_enqueue_handler);
1934 nmp->base.lmsg.u.ms_resultp = dst_ifp;
1936 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), &nmp->base.lmsg);
1940 * After looking up dst_if in our forwarding table we still have to
1941 * deal with channel bonding. Find the best interface in the bonding set.
1943 static struct ifnet *
1944 bridge_select_unicast(struct bridge_softc *sc, struct ifnet *dst_if,
1945 int from_blocking, struct mbuf *m)
1947 struct bridge_iflist *bif, *nbif;
1948 struct ifnet *alt_if;
1953 * Unicast, kinda replicates the output side of bridge_output().
1955 * Even though this is a uni-cast packet we may have to select
1956 * an interface from a bonding set.
1958 bif = bridge_lookup_member_if(sc, dst_if);
1960 /* Not a member of the bridge (anymore?) */
1965 * If STP is enabled on the target we are an equal opportunity
1966 * employer and do not necessarily output to dst_if. Instead
1967 * scan available links with the same MAC as the current dst_if
1968 * and choose the best one.
1970 * We also need to do this because arp entries tag onto a particular
1971 * interface and if it happens to be dead then the packets will
1972 * go into a bit bucket.
1974 * If LINK2 is set the matching links are bonded and we-round robin.
1975 * (the MAC address must be the same for the participating links).
1976 * In this case links in a STP FORWARDING or BONDED state are
1977 * allowed for unicast packets.
1979 if (bif->bif_flags & IFBIF_STP) {
1984 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid],
1987 * dst_if may imply a bonding set so we must compare
1990 if (memcmp(IF_LLADDR(bif->bif_ifp),
1992 ETHER_ADDR_LEN) != 0) {
1996 if ((bif->bif_ifp->if_flags & IFF_RUNNING) == 0)
2000 * NOTE: We allow tranmissions through a BLOCKING
2001 * or LEARNING interface only as a last resort.
2002 * We DISALLOW both cases if the receiving
2004 * NOTE: If we send a packet through a learning
2005 * interface the receiving end (if also in
2006 * LEARNING) will throw it away, so this is
2007 * the ultimate last resort.
2009 switch(bif->bif_state) {
2010 case BSTP_IFSTATE_BLOCKING:
2011 if (from_blocking == 0 &&
2012 bif->bif_priority + 256 > alt_priority) {
2013 alt_priority = bif->bif_priority + 256;
2014 alt_if = bif->bif_ifp;
2017 case BSTP_IFSTATE_LEARNING:
2018 if (from_blocking == 0 &&
2019 bif->bif_priority > alt_priority) {
2020 alt_priority = bif->bif_priority;
2021 alt_if = bif->bif_ifp;
2024 case BSTP_IFSTATE_L1BLOCKING:
2025 case BSTP_IFSTATE_LISTENING:
2026 case BSTP_IFSTATE_DISABLED:
2029 /* FORWARDING, BONDED */
2034 * XXX we need to use the toepliz hash or
2035 * something like that instead of
2038 if (sc->sc_ifp->if_flags & IFF_LINK2) {
2039 dst_if = bif->bif_ifp;
2040 if (++bif->bif_bond_count >=
2041 bif->bif_bond_weight) {
2042 bif->bif_bond_count = 0;
2043 TAILQ_REMOVE(&sc->sc_iflists[mycpuid],
2046 &sc->sc_iflists[mycpuid],
2054 * Select best interface in the FORWARDING or
2055 * BONDED set. Well, there shouldn't be any
2056 * in a BONDED state if LINK2 is not set (they
2057 * will all be in a BLOCKING) state, but there
2058 * could be a transitory condition here.
2060 if (bif->bif_priority > priority) {
2061 priority = bif->bif_priority;
2062 dst_if = bif->bif_ifp;
2067 * If no suitable interfaces were found but a suitable
2068 * alternative interface was found, use the alternative
2071 if (priority == 0 && alt_if)
2076 * At this point, we're dealing with a unicast frame
2077 * going to a different interface.
2079 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2088 * Send output from a bridge member interface. This
2089 * performs the bridging function for locally originated
2092 * The mbuf has the Ethernet header already attached. We must
2093 * enqueue or free the mbuf before returning.
2096 bridge_output(struct ifnet *ifp, struct mbuf *m)
2098 struct bridge_softc *sc = ifp->if_bridge;
2099 struct bridge_iflist *bif, *nbif;
2100 struct ether_header *eh;
2101 struct ifnet *dst_if, *alt_if, *bifp;
2105 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2109 * Make sure that we are still a member of a bridge interface.
2120 if (m->m_len < ETHER_HDR_LEN) {
2121 m = m_pullup(m, ETHER_HDR_LEN);
2123 IFNET_STAT_INC(bifp, oerrors, 1);
2127 eh = mtod(m, struct ether_header *);
2128 from_us = bridge_from_us(sc, eh);
2131 * If bridge is down, but the original output interface is up,
2132 * go ahead and send out that interface. Otherwise, the packet
2135 if ((bifp->if_flags & IFF_RUNNING) == 0) {
2141 * If the packet is a multicast, or we don't know a better way to
2142 * get there, send to all interfaces.
2144 if (ETHER_IS_MULTICAST(eh->ether_dhost))
2147 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2149 if (dst_if == NULL) {
2159 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid],
2161 dst_if = bif->bif_ifp;
2163 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2167 * If this is not the original output interface,
2168 * and the interface is participating in spanning
2169 * tree, make sure the port is in a state that
2170 * allows forwarding.
2172 * We keep track of a possible backup IF if we are
2173 * unable to find any interfaces to forward through.
2175 * NOTE: Currently round-robining is not implemented
2176 * across bonded interface groups (needs an
2177 * algorithm to track each group somehow).
2179 * Similarly we track only one alternative
2180 * interface if no suitable interfaces are
2183 if (dst_if != ifp &&
2184 (bif->bif_flags & IFBIF_STP) != 0) {
2185 switch (bif->bif_state) {
2186 case BSTP_IFSTATE_BONDED:
2187 if (bif->bif_priority + 512 >
2190 bif->bif_priority + 512;
2191 alt_if = bif->bif_ifp;
2194 case BSTP_IFSTATE_BLOCKING:
2195 if (bif->bif_priority + 256 >
2198 bif->bif_priority + 256;
2199 alt_if = bif->bif_ifp;
2202 case BSTP_IFSTATE_LEARNING:
2203 if (bif->bif_priority > alt_priority) {
2206 alt_if = bif->bif_ifp;
2209 case BSTP_IFSTATE_L1BLOCKING:
2210 case BSTP_IFSTATE_LISTENING:
2211 case BSTP_IFSTATE_DISABLED:
2219 KKASSERT(used == 0);
2220 if (TAILQ_NEXT(bif, bif_next) == NULL) {
2224 mc = m_copypacket(m, M_NOWAIT);
2226 IFNET_STAT_INC(bifp, oerrors, 1);
2232 * If the packet is 'from' us override ether_shost.
2234 bridge_handoff(sc, dst_if, mc, from_us);
2237 if (nbif != NULL && !nbif->bif_onlist) {
2238 KKASSERT(bif->bif_onlist);
2239 nbif = TAILQ_NEXT(bif, bif_next);
2244 * If we couldn't find anything use the backup interface
2247 if (found == 0 && alt_if) {
2248 KKASSERT(used == 0);
2251 bridge_handoff(sc, alt_if, mc, from_us);
2263 dst_if = bridge_select_unicast(sc, dst_if, 0, m);
2270 bridge_handoff(sc, dst_if, m, from_us);
2275 * Returns the bridge interface associated with an ifc.
2276 * Pass ifp->if_bridge (must not be NULL). Used by the ARP
2277 * code to supply the bridge for the is-at info, making
2278 * the bridge responsible for matching local addresses.
2280 * Without this the ARP code will supply bridge member interfaces
2281 * for the is-at which makes it difficult the bridge to fail-over
2282 * interfaces (amoung other things).
2284 static struct ifnet *
2285 bridge_interface(void *if_bridge)
2287 struct bridge_softc *sc = if_bridge;
2288 return (sc->sc_ifp);
2294 * Start output on a bridge.
2297 bridge_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2299 struct bridge_softc *sc = ifp->if_softc;
2301 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2302 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
2304 ifsq_set_oactive(ifsq);
2306 struct ifnet *dst_if = NULL;
2307 struct ether_header *eh;
2310 m = ifsq_dequeue(ifsq);
2315 if (m->m_len < sizeof(*eh)) {
2316 m = m_pullup(m, sizeof(*eh));
2318 IFNET_STAT_INC(ifp, oerrors, 1);
2322 eh = mtod(m, struct ether_header *);
2325 IFNET_STAT_INC(ifp, opackets, 1);
2327 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
2328 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2331 * Multicast or broadcast
2333 if (dst_if == NULL) {
2334 bridge_start_bcast(sc, m);
2341 dst_if = bridge_select_unicast(sc, dst_if, 0, m);
2346 bridge_enqueue(dst_if, m);
2348 ifsq_clr_oactive(ifsq);
2354 * Forward packets received on a bridge interface via the input
2357 * This implements the forwarding function of the bridge.
2360 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
2362 struct bridge_iflist *bif;
2363 struct ifnet *src_if, *dst_if, *ifp;
2364 struct ether_header *eh;
2368 src_if = m->m_pkthdr.rcvif;
2371 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2374 * packet coming in on the bridge is also going out on the bridge,
2375 * but ether code won't adjust output stats for the bridge because
2376 * we are changing the interface to something else.
2378 IFNET_STAT_INC(ifp, opackets, 1);
2379 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
2382 * Look up the bridge_iflist.
2384 bif = bridge_lookup_member_if(sc, src_if);
2386 /* Interface is not a bridge member (anymore?) */
2392 * In spanning tree mode receiving a packet from an interface
2393 * in a BLOCKING state is allowed, it could be a member of last
2394 * resort from the sender's point of view, but forwarding it is
2397 * The sender's spanning tree will eventually sync up and the
2398 * sender will go into a BLOCKING state too (but this still may be
2399 * an interface of last resort during state changes).
2401 if (bif->bif_flags & IFBIF_STP) {
2402 switch (bif->bif_state) {
2403 case BSTP_IFSTATE_L1BLOCKING:
2404 case BSTP_IFSTATE_LISTENING:
2405 case BSTP_IFSTATE_DISABLED:
2409 /* learning, blocking, bonded, forwarding */
2412 from_blocking = (bif->bif_state == BSTP_IFSTATE_BLOCKING);
2417 eh = mtod(m, struct ether_header *);
2420 * If the interface is learning, and the source
2421 * address is valid and not multicast, record
2424 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
2425 from_blocking == 0 &&
2426 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
2427 (eh->ether_shost[0] == 0 &&
2428 eh->ether_shost[1] == 0 &&
2429 eh->ether_shost[2] == 0 &&
2430 eh->ether_shost[3] == 0 &&
2431 eh->ether_shost[4] == 0 &&
2432 eh->ether_shost[5] == 0) == 0) {
2433 bridge_rtupdate(sc, eh->ether_shost, src_if, IFBAF_DYNAMIC);
2437 * Don't forward from an interface in the listening or learning
2438 * state. That is, in the learning state we learn information
2439 * but we throw away the packets.
2441 * We let through packets on interfaces in the blocking state.
2442 * The blocking state is applicable to the send side, not the
2445 if ((bif->bif_flags & IFBIF_STP) != 0 &&
2446 (bif->bif_state == BSTP_IFSTATE_LISTENING ||
2447 bif->bif_state == BSTP_IFSTATE_LEARNING)) {
2453 * At this point, the port either doesn't participate
2454 * in spanning tree or it is in the forwarding state.
2458 * If the packet is unicast, destined for someone on
2459 * "this" side of the bridge, drop it.
2461 * src_if implies the entire bonding set so we have to compare MAC
2462 * addresses and not just if pointers.
2464 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2465 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2466 if (dst_if && memcmp(IF_LLADDR(src_if), IF_LLADDR(dst_if),
2467 ETHER_ADDR_LEN) == 0) {
2472 /* ...forward it to all interfaces. */
2473 IFNET_STAT_INC(ifp, imcasts, 1);
2478 * Brodcast if we do not have forwarding information. However, if
2479 * we received the packet on a blocking interface we do not do this
2480 * (unless you really want to blow up your network).
2482 if (dst_if == NULL) {
2486 bridge_broadcast(sc, src_if, m);
2490 dst_if = bridge_select_unicast(sc, dst_if, from_blocking, m);
2492 if (dst_if == NULL) {
2497 if (inet_pfil_hook.ph_hashooks > 0
2499 || inet6_pfil_hook.ph_hashooks > 0
2502 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2507 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2512 bridge_handoff(sc, dst_if, m, 0);
2518 * Receive input from a member interface. Queue the packet for
2519 * bridging if it is not for us.
2521 static struct mbuf *
2522 bridge_input(struct ifnet *ifp, struct mbuf *m)
2524 struct bridge_softc *sc = ifp->if_bridge;
2525 struct bridge_iflist *bif;
2526 struct ifnet *bifp, *new_ifp;
2527 struct ether_header *eh;
2528 struct mbuf *mc, *mc2;
2530 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2534 * Make sure that we are still a member of a bridge interface.
2542 if ((bifp->if_flags & IFF_RUNNING) == 0)
2546 * Implement support for bridge monitoring. If this flag has been
2547 * set on this interface, discard the packet once we push it through
2548 * the bpf(4) machinery, but before we do, increment various counters
2549 * associated with this bridge.
2551 if (bifp->if_flags & IFF_MONITOR) {
2553 * Change input interface to this bridge
2555 * Update bridge's ifnet statistics
2557 m->m_pkthdr.rcvif = bifp;
2560 IFNET_STAT_INC(bifp, ipackets, 1);
2561 IFNET_STAT_INC(bifp, ibytes, m->m_pkthdr.len);
2562 if (m->m_flags & (M_MCAST | M_BCAST))
2563 IFNET_STAT_INC(bifp, imcasts, 1);
2571 * Handle the ether_header
2573 * In all cases if the packet is destined for us via our MAC
2574 * we must clear BRIDGE_MBUF_TAGGED to ensure that we don't
2575 * repeat the source MAC out the same interface.
2577 * This first test against our bridge MAC is the fast-path.
2579 * NOTE! The bridge interface can serve as an endpoint for
2580 * communication but normally there are no IPs associated
2581 * with it so you cannot route through it. Instead what
2582 * you do is point your default route *THROUGH* the bridge
2583 * to the actual default router for one of the bridged spaces.
2585 * Another possibility is to put all your IP specifications
2586 * on the bridge instead of on the individual interfaces. If
2587 * you do this it should be possible to use the bridge as an
2588 * end point and route (rather than switch) through it using
2589 * the default route or ipfw forwarding rules.
2595 if (m->m_len < ETHER_HDR_LEN) {
2596 m = m_pullup(m, ETHER_HDR_LEN);
2600 eh = mtod(m, struct ether_header *);
2601 m->m_pkthdr.fw_flags |= BRIDGE_MBUF_TAGGED;
2602 bcopy(eh->ether_shost, m->m_pkthdr.ether_br_shost, ETHER_ADDR_LEN);
2604 if ((bridge_debug & 1) &&
2605 (ntohs(eh->ether_type) == ETHERTYPE_ARP ||
2606 ntohs(eh->ether_type) == ETHERTYPE_REVARP)) {
2607 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
2608 "%02x:%02x:%02x:%02x:%02x:%02x type %04x "
2609 "lla %02x:%02x:%02x:%02x:%02x:%02x\n",
2623 ((u_char *)IF_LLADDR(bifp))[0],
2624 ((u_char *)IF_LLADDR(bifp))[1],
2625 ((u_char *)IF_LLADDR(bifp))[2],
2626 ((u_char *)IF_LLADDR(bifp))[3],
2627 ((u_char *)IF_LLADDR(bifp))[4],
2628 ((u_char *)IF_LLADDR(bifp))[5]
2633 * If the packet is for us, set the packets source as the
2634 * bridge, and return the packet back to ifnet.if_input for
2637 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
2639 * We must still record the source interface in our
2640 * addr cache, otherwise our bridge won't know where
2641 * to send responses and will broadcast them.
2643 bif = bridge_lookup_member_if(sc, ifp);
2644 if ((bif->bif_flags & IFBIF_LEARNING) &&
2645 ((bif->bif_flags & IFBIF_STP) == 0 ||
2646 bif->bif_state != BSTP_IFSTATE_BLOCKING)) {
2647 bridge_rtupdate(sc, eh->ether_shost,
2648 ifp, IFBAF_DYNAMIC);
2652 * Perform pfil hooks.
2654 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
2655 KASSERT(bifp->if_bridge == NULL,
2656 ("loop created in bridge_input"));
2657 if (pfil_member != 0) {
2658 if (inet_pfil_hook.ph_hashooks > 0
2660 || inet6_pfil_hook.ph_hashooks > 0
2663 if (bridge_pfil(&m, NULL, ifp, PFIL_IN) != 0)
2671 * Set new_ifp and skip to the end. This will trigger code
2672 * to reinput the packet and run it into our stack.
2679 * Tap all packets arriving on the bridge, no matter if
2680 * they are local destinations or not. In is in.
2682 * Update bridge's ifnet statistics
2685 IFNET_STAT_INC(bifp, ipackets, 1);
2686 IFNET_STAT_INC(bifp, ibytes, m->m_pkthdr.len);
2687 if (m->m_flags & (M_MCAST | M_BCAST))
2688 IFNET_STAT_INC(bifp, imcasts, 1);
2690 bif = bridge_lookup_member_if(sc, ifp);
2697 if (m->m_flags & (M_BCAST | M_MCAST)) {
2699 * Tap off 802.1D packets; they do not get forwarded.
2701 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2702 ETHER_ADDR_LEN) == 0) {
2703 ifnet_serialize_all(bifp);
2704 bstp_input(sc, bif, m);
2705 ifnet_deserialize_all(bifp);
2707 /* m is freed by bstp_input */
2713 * Other than 802.11d packets, ignore packets if the
2714 * interface is not in a good state.
2716 * NOTE: Broadcast/mcast packets received on a blocking or
2717 * learning interface are allowed for local processing.
2719 * The sending side of a blocked port will stop
2720 * transmitting when a better alternative is found.
2721 * However, later on we will disallow the forwarding
2722 * of bcast/mcsat packets over a blocking interface.
2724 if (bif->bif_flags & IFBIF_STP) {
2725 switch (bif->bif_state) {
2726 case BSTP_IFSTATE_L1BLOCKING:
2727 case BSTP_IFSTATE_LISTENING:
2728 case BSTP_IFSTATE_DISABLED:
2731 /* blocking, learning, bonded, forwarding */
2737 * Make a deep copy of the packet and enqueue the copy
2738 * for bridge processing; return the original packet for
2741 mc = m_dup(m, M_NOWAIT);
2746 * It's just too dangerous to allow bcast/mcast over a
2747 * blocked interface, eventually the network will sort
2748 * itself out and a better path will be found.
2750 if ((bif->bif_flags & IFBIF_STP) == 0 ||
2751 bif->bif_state != BSTP_IFSTATE_BLOCKING) {
2752 bridge_forward(sc, mc);
2756 * Reinject the mbuf as arriving on the bridge so we have a
2757 * chance at claiming multicast packets. We can not loop back
2758 * here from ether_input as a bridge is never a member of a
2761 KASSERT(bifp->if_bridge == NULL,
2762 ("loop created in bridge_input"));
2763 mc2 = m_dup(m, M_NOWAIT);
2766 /* Keep the layer3 header aligned */
2767 int i = min(mc2->m_pkthdr.len, max_protohdr);
2768 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2773 * Don't tap to bpf(4) again; we have already done
2776 * Leave m_pkthdr.rcvif alone, so ARP replies are
2777 * processed as coming in on the correct interface.
2779 * Clear the bridge flag for local processing in
2780 * case the packet gets routed.
2782 mc2->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
2783 ether_reinput_oncpu(bifp, mc2, 0);
2786 /* Return the original packet for local processing. */
2791 * Input of a unicast packet. We have to allow unicast packets
2792 * input from links in the BLOCKING state as this might be an
2793 * interface of last resort.
2795 * NOTE: We explicitly ignore normal packets received on a link
2796 * in the BLOCKING state. The point of being in that state
2797 * is to avoid getting duplicate packets.
2799 * HOWEVER, if LINK2 is set the normal spanning tree code
2800 * will mark an interface BLOCKING to avoid multi-cast/broadcast
2801 * loops. Unicast packets CAN still loop if we allow the
2802 * case (hence we only do it in LINK2), but it isn't quite as
2803 * bad as a broadcast packet looping.
2805 if (bif->bif_flags & IFBIF_STP) {
2806 switch (bif->bif_state) {
2807 case BSTP_IFSTATE_L1BLOCKING:
2808 case BSTP_IFSTATE_LISTENING:
2809 case BSTP_IFSTATE_DISABLED:
2812 /* blocking, bonded, forwarding, learning */
2818 * Unicast. Make sure it's not for us.
2820 * This loop is MPSAFE; the only blocking operation (bridge_rtupdate)
2821 * is followed by breaking out of the loop.
2823 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2824 if (bif->bif_ifp->if_type != IFT_ETHER)
2828 * It is destined for an interface linked to the bridge.
2829 * We want the bridge itself to take care of link level
2830 * forwarding to member interfaces so reinput on the bridge.
2831 * i.e. if you ping an IP on a target interface associated
2832 * with the bridge, the arp is-at response should indicate
2835 * Only update our addr list when learning if the port
2836 * is not in a blocking state. If it is we still allow
2837 * the packet but we do not try to learn from it.
2839 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
2840 ETHER_ADDR_LEN) == 0) {
2841 if (bif->bif_ifp != ifp) {
2842 /* XXX loop prevention */
2843 m->m_flags |= M_ETHER_BRIDGED;
2845 if ((bif->bif_flags & IFBIF_LEARNING) &&
2846 ((bif->bif_flags & IFBIF_STP) == 0 ||
2847 bif->bif_state != BSTP_IFSTATE_BLOCKING)) {
2848 bridge_rtupdate(sc, eh->ether_shost,
2849 ifp, IFBAF_DYNAMIC);
2851 new_ifp = bifp; /* not bif->bif_ifp */
2852 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
2857 * Ignore received packets that were sent by us.
2859 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
2860 ETHER_ADDR_LEN) == 0) {
2870 * Perform the bridge forwarding function, but disallow bridging
2871 * to interfaces in the blocking state if the packet came in on
2872 * an interface in the blocking state.
2874 * (bridge_forward also updates the addr cache).
2876 bridge_forward(sc, m);
2880 * ether_reinput_oncpu() will reprocess rcvif as
2881 * coming from new_ifp (since we do not specify
2882 * REINPUT_KEEPRCVIF).
2885 if (new_ifp != NULL) {
2887 * Clear the bridge flag for local processing in
2888 * case the packet gets routed.
2890 ether_reinput_oncpu(new_ifp, m, REINPUT_RUNBPF);
2897 * bridge_start_bcast:
2899 * Broadcast the packet sent from bridge to all member
2901 * This is a simplified version of bridge_broadcast(), however,
2902 * this function expects caller to hold bridge's serializer.
2905 bridge_start_bcast(struct bridge_softc *sc, struct mbuf *m)
2907 struct bridge_iflist *bif;
2909 struct ifnet *dst_if, *alt_if, *bifp;
2916 ASSERT_IFNET_SERIALIZED_ALL(bifp);
2919 * Following loop is MPSAFE; nothing is blocking
2922 * NOTE: We transmit through an member in the BLOCKING state only
2928 TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2929 dst_if = bif->bif_ifp;
2931 if (bif->bif_flags & IFBIF_STP) {
2932 switch (bif->bif_state) {
2933 case BSTP_IFSTATE_BLOCKING:
2934 if (bif->bif_priority > alt_priority) {
2935 alt_priority = bif->bif_priority;
2936 alt_if = bif->bif_ifp;
2939 case BSTP_IFSTATE_L1BLOCKING:
2940 case BSTP_IFSTATE_DISABLED:
2943 /* listening, learning, bonded, forwarding */
2948 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2949 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2952 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2955 if (TAILQ_NEXT(bif, bif_next) == NULL) {
2959 mc = m_copypacket(m, M_NOWAIT);
2961 IFNET_STAT_INC(bifp, oerrors, 1);
2966 bridge_enqueue(dst_if, mc);
2969 if (found == 0 && alt_if) {
2970 KKASSERT(used == 0);
2973 bridge_enqueue(alt_if, mc);
2983 * Send a frame to all interfaces that are members of
2984 * the bridge, except for the one on which the packet
2988 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2991 struct bridge_iflist *bif, *nbif;
2992 struct ether_header *eh;
2994 struct ifnet *dst_if, *alt_if, *bifp;
3002 ASSERT_IFNET_NOT_SERIALIZED_ALL(bifp);
3004 eh = mtod(m, struct ether_header *);
3005 from_us = bridge_from_us(sc, eh);
3007 if (inet_pfil_hook.ph_hashooks > 0
3009 || inet6_pfil_hook.ph_hashooks > 0
3012 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
3017 /* Filter on the bridge interface before broadcasting */
3018 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
3029 TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
3030 dst_if = bif->bif_ifp;
3032 if ((dst_if->if_flags & IFF_RUNNING) == 0)
3036 * Don't bounce the packet out the same interface it came
3037 * in on. We have to test MAC addresses because a packet
3038 * can come in a bonded interface and we don't want it to
3039 * be echod out the forwarding interface for the same bonding
3042 if (src_if && memcmp(IF_LLADDR(src_if), IF_LLADDR(dst_if),
3043 ETHER_ADDR_LEN) == 0) {
3048 * Generally speaking we only broadcast through forwarding
3049 * interfaces. If no interfaces are available we select
3050 * a BONDED, BLOCKING, or LEARNING interface to forward
3053 if (bif->bif_flags & IFBIF_STP) {
3054 switch (bif->bif_state) {
3055 case BSTP_IFSTATE_BONDED:
3056 if (bif->bif_priority + 512 > alt_priority) {
3057 alt_priority = bif->bif_priority + 512;
3058 alt_if = bif->bif_ifp;
3061 case BSTP_IFSTATE_BLOCKING:
3062 if (bif->bif_priority + 256 > alt_priority) {
3063 alt_priority = bif->bif_priority + 256;
3064 alt_if = bif->bif_ifp;
3067 case BSTP_IFSTATE_LEARNING:
3068 if (bif->bif_priority > alt_priority) {
3069 alt_priority = bif->bif_priority;
3070 alt_if = bif->bif_ifp;
3073 case BSTP_IFSTATE_L1BLOCKING:
3074 case BSTP_IFSTATE_DISABLED:
3075 case BSTP_IFSTATE_LISTENING:
3083 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
3084 (m->m_flags & (M_BCAST|M_MCAST)) == 0) {
3088 if (TAILQ_NEXT(bif, bif_next) == NULL) {
3092 mc = m_copypacket(m, M_NOWAIT);
3094 IFNET_STAT_INC(sc->sc_ifp, oerrors, 1);
3101 * Filter on the output interface. Pass a NULL bridge
3102 * interface pointer so we do not redundantly filter on
3103 * the bridge for each interface we broadcast on.
3105 if (inet_pfil_hook.ph_hashooks > 0
3107 || inet6_pfil_hook.ph_hashooks > 0
3110 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
3115 bridge_handoff(sc, dst_if, mc, from_us);
3117 if (nbif != NULL && !nbif->bif_onlist) {
3118 KKASSERT(bif->bif_onlist);
3119 nbif = TAILQ_NEXT(bif, bif_next);
3123 if (found == 0 && alt_if) {
3124 KKASSERT(used == 0);
3127 bridge_enqueue(alt_if, mc);
3137 * Duplicate a packet out one or more interfaces that are in span mode,
3138 * the original mbuf is unmodified.
3141 bridge_span(struct bridge_softc *sc, struct mbuf *m)
3143 struct bridge_iflist *bif;
3144 struct ifnet *dst_if, *bifp;
3149 ifnet_serialize_all(bifp);
3151 TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) {
3152 dst_if = bif->bif_ifp;
3154 if ((dst_if->if_flags & IFF_RUNNING) == 0)
3157 mc = m_copypacket(m, M_NOWAIT);
3159 IFNET_STAT_INC(sc->sc_ifp, oerrors, 1);
3162 bridge_enqueue(dst_if, mc);
3165 ifnet_deserialize_all(bifp);
3169 bridge_rtmsg_sync_handler(netmsg_t msg)
3171 netisr_forwardmsg(&msg->base, mycpuid + 1);
3175 bridge_rtmsg_sync(struct bridge_softc *sc)
3177 struct netmsg_base msg;
3179 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3181 /* XXX use netmsg_service_sync */
3182 netmsg_init(&msg, NULL, &curthread->td_msgport,
3183 0, bridge_rtmsg_sync_handler);
3184 netisr_domsg(&msg, 0);
3187 static __inline void
3188 bridge_rtinfo_update(struct bridge_rtinfo *bri, struct ifnet *dst_if,
3189 int setflags, uint8_t flags, uint32_t timeo)
3191 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
3192 bri->bri_ifp != dst_if)
3193 bri->bri_ifp = dst_if;
3194 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
3195 bri->bri_expire != time_uptime + timeo)
3196 bri->bri_expire = time_uptime + timeo;
3198 bri->bri_flags = flags;
3202 bridge_rtinstall_oncpu(struct bridge_softc *sc, const uint8_t *dst,
3203 struct ifnet *dst_if, int setflags, uint8_t flags,
3204 struct bridge_rtinfo **bri0)
3206 struct bridge_rtnode *brt;
3207 struct bridge_rtinfo *bri;
3210 brt = bridge_rtnode_lookup(sc, dst);
3213 * rtnode for 'dst' already exists. We inform the
3214 * caller about this by leaving bri0 as NULL. The
3215 * caller will terminate the intallation upon getting
3216 * NULL bri0. However, we still need to update the
3219 KKASSERT(*bri0 == NULL);
3222 bridge_rtinfo_update(brt->brt_info, dst_if, setflags,
3223 flags, sc->sc_brttimeout);
3228 * We only need to check brtcnt on CPU0, since if limit
3229 * is to be exceeded, ENOSPC is returned. Caller knows
3230 * this and will terminate the installation.
3232 if (sc->sc_brtcnt >= sc->sc_brtmax)
3235 KKASSERT(*bri0 == NULL);
3236 bri = kmalloc(sizeof(struct bridge_rtinfo), M_DEVBUF,
3241 bri->bri_flags = IFBAF_DYNAMIC;
3242 bridge_rtinfo_update(bri, dst_if, setflags, flags,
3246 KKASSERT(bri != NULL);
3249 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
3251 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
3252 brt->brt_info = bri;
3254 bridge_rtnode_insert(sc, brt);
3259 bridge_rtinstall_handler(netmsg_t msg)
3261 struct netmsg_brsaddr *brmsg = (struct netmsg_brsaddr *)msg;
3264 error = bridge_rtinstall_oncpu(brmsg->br_softc,
3265 brmsg->br_dst, brmsg->br_dst_if,
3266 brmsg->br_setflags, brmsg->br_flags,
3269 KKASSERT(mycpuid == 0 && brmsg->br_rtinfo == NULL);
3270 netisr_replymsg(&brmsg->base, error);
3272 } else if (brmsg->br_rtinfo == NULL) {
3273 /* rtnode already exists for 'dst' */
3274 KKASSERT(mycpuid == 0);
3275 netisr_replymsg(&brmsg->base, 0);
3278 netisr_forwardmsg(&brmsg->base, mycpuid + 1);
3284 * Add/Update a bridge routing entry.
3287 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
3288 struct ifnet *dst_if, uint8_t flags)
3290 struct bridge_rtnode *brt;
3293 * A route for this destination might already exist. If so,
3294 * update it, otherwise create a new one.
3296 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
3297 struct netmsg_brsaddr *brmsg;
3299 if (sc->sc_brtcnt >= sc->sc_brtmax)
3302 brmsg = kmalloc(sizeof(*brmsg), M_LWKTMSG, M_WAITOK | M_NULLOK);
3306 netmsg_init(&brmsg->base, NULL, &netisr_afree_rport,
3307 0, bridge_rtinstall_handler);
3308 memcpy(brmsg->br_dst, dst, ETHER_ADDR_LEN);
3309 brmsg->br_dst_if = dst_if;
3310 brmsg->br_flags = flags;
3311 brmsg->br_setflags = 0;
3312 brmsg->br_softc = sc;
3313 brmsg->br_rtinfo = NULL;
3315 netisr_sendmsg(&brmsg->base, 0);
3318 bridge_rtinfo_update(brt->brt_info, dst_if, 0, flags,
3324 bridge_rtsaddr(struct bridge_softc *sc, const uint8_t *dst,
3325 struct ifnet *dst_if, uint8_t flags)
3327 struct netmsg_brsaddr brmsg;
3329 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3331 netmsg_init(&brmsg.base, NULL, &curthread->td_msgport,
3332 0, bridge_rtinstall_handler);
3333 memcpy(brmsg.br_dst, dst, ETHER_ADDR_LEN);
3334 brmsg.br_dst_if = dst_if;
3335 brmsg.br_flags = flags;
3336 brmsg.br_setflags = 1;
3337 brmsg.br_softc = sc;
3338 brmsg.br_rtinfo = NULL;
3340 return netisr_domsg(&brmsg.base, 0);
3346 * Lookup the destination interface for an address.
3348 static struct ifnet *
3349 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
3351 struct bridge_rtnode *brt;
3353 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
3355 return brt->brt_info->bri_ifp;
3359 bridge_rtreap_handler(netmsg_t msg)
3361 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
3362 struct bridge_rtnode *brt, *nbrt;
3364 LIST_FOREACH_MUTABLE(brt, &sc->sc_rtlists[mycpuid], brt_list, nbrt) {
3365 if (brt->brt_info->bri_dead)
3366 bridge_rtnode_destroy(sc, brt);
3368 netisr_forwardmsg(&msg->base, mycpuid + 1);
3372 bridge_rtreap(struct bridge_softc *sc)
3374 struct netmsg_base msg;
3376 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3378 netmsg_init(&msg, NULL, &curthread->td_msgport,
3379 0, bridge_rtreap_handler);
3380 msg.lmsg.u.ms_resultp = sc;
3382 netisr_domsg(&msg, 0);
3386 bridge_rtreap_async(struct bridge_softc *sc)
3388 struct netmsg_base *msg;
3390 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK);
3392 netmsg_init(msg, NULL, &netisr_afree_rport,
3393 0, bridge_rtreap_handler);
3394 msg->lmsg.u.ms_resultp = sc;
3396 netisr_sendmsg(msg, 0);
3402 * Trim the routine table so that we have a number
3403 * of routing entries less than or equal to the
3407 bridge_rttrim(struct bridge_softc *sc)
3409 struct bridge_rtnode *brt;
3412 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3414 /* Make sure we actually need to do this. */
3415 if (sc->sc_brtcnt <= sc->sc_brtmax)
3419 * Find out how many rtnodes are dead
3421 dead = bridge_rtage_finddead(sc);
3422 KKASSERT(dead <= sc->sc_brtcnt);
3424 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
3425 /* Enough dead rtnodes are found */
3431 * Kill some dynamic rtnodes to meet the brtmax
3433 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3434 struct bridge_rtinfo *bri = brt->brt_info;
3436 if (bri->bri_dead) {
3438 * We have counted this rtnode in
3439 * bridge_rtage_finddead()
3444 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
3447 KKASSERT(dead <= sc->sc_brtcnt);
3449 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
3450 /* Enough rtnodes are collected */
3462 * Aging timer for the bridge.
3465 bridge_timer(void *arg)
3467 struct bridge_softc *sc = arg;
3468 struct netmsg_base *msg;
3470 KKASSERT(mycpuid == BRIDGE_CFGCPU);
3474 if (callout_pending(&sc->sc_brcallout) ||
3475 !callout_active(&sc->sc_brcallout)) {
3479 callout_deactivate(&sc->sc_brcallout);
3481 msg = &sc->sc_brtimemsg;
3482 KKASSERT(msg->lmsg.ms_flags & MSGF_DONE);
3483 lwkt_sendmsg_oncpu(BRIDGE_CFGPORT, &msg->lmsg);
3489 bridge_timer_handler(netmsg_t msg)
3491 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
3493 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
3497 lwkt_replymsg(&msg->lmsg, 0);
3501 if (sc->sc_ifp->if_flags & IFF_RUNNING) {
3502 callout_reset(&sc->sc_brcallout,
3503 bridge_rtable_prune_period * hz, bridge_timer, sc);
3508 bridge_rtage_finddead(struct bridge_softc *sc)
3510 struct bridge_rtnode *brt;
3513 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3514 struct bridge_rtinfo *bri = brt->brt_info;
3516 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
3517 time_uptime >= bri->bri_expire) {
3520 KKASSERT(dead <= sc->sc_brtcnt);
3529 * Perform an aging cycle.
3532 bridge_rtage(struct bridge_softc *sc)
3534 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3536 if (bridge_rtage_finddead(sc))
3543 * Remove all dynamic addresses from the bridge.
3546 bridge_rtflush(struct bridge_softc *sc, int bf)
3548 struct bridge_rtnode *brt;
3552 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3553 struct bridge_rtinfo *bri = brt->brt_info;
3555 if ((bf & IFBF_FLUSHALL) ||
3556 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
3562 if (bf & IFBF_FLUSHSYNC)
3565 bridge_rtreap_async(sc);
3572 * Remove an address from the table.
3575 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
3577 struct bridge_rtnode *brt;
3579 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3581 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
3584 /* TODO: add a cheaper delete operation */
3585 brt->brt_info->bri_dead = 1;
3593 * Delete routes to a speicifc member interface.
3596 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int bf)
3598 struct bridge_rtnode *brt;
3602 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
3603 struct bridge_rtinfo *bri = brt->brt_info;
3605 if (bri->bri_ifp == ifp &&
3606 ((bf & IFBF_FLUSHALL) ||
3607 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) {
3613 if (bf & IFBF_FLUSHSYNC)
3616 bridge_rtreap_async(sc);
3621 * bridge_rtable_init:
3623 * Initialize the route table for this bridge.
3626 bridge_rtable_init(struct bridge_softc *sc)
3631 * Initialize per-cpu hash tables
3633 sc->sc_rthashs = kmalloc(sizeof(*sc->sc_rthashs) * ncpus,
3634 M_DEVBUF, M_WAITOK);
3635 for (cpu = 0; cpu < ncpus; ++cpu) {
3638 sc->sc_rthashs[cpu] =
3639 kmalloc(sizeof(struct bridge_rtnode_head) * BRIDGE_RTHASH_SIZE,
3640 M_DEVBUF, M_WAITOK);
3642 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
3643 LIST_INIT(&sc->sc_rthashs[cpu][i]);
3645 sc->sc_rthash_key = karc4random();
3648 * Initialize per-cpu lists
3650 sc->sc_rtlists = kmalloc(sizeof(struct bridge_rtnode_head) * ncpus,
3651 M_DEVBUF, M_WAITOK);
3652 for (cpu = 0; cpu < ncpus; ++cpu)
3653 LIST_INIT(&sc->sc_rtlists[cpu]);
3657 * bridge_rtable_fini:
3659 * Deconstruct the route table for this bridge.
3662 bridge_rtable_fini(struct bridge_softc *sc)
3667 * Free per-cpu hash tables
3669 for (cpu = 0; cpu < ncpus; ++cpu)
3670 kfree(sc->sc_rthashs[cpu], M_DEVBUF);
3671 kfree(sc->sc_rthashs, M_DEVBUF);
3674 * Free per-cpu lists
3676 kfree(sc->sc_rtlists, M_DEVBUF);
3680 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
3681 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
3683 #define mix(a, b, c) \
3685 a -= b; a -= c; a ^= (c >> 13); \
3686 b -= c; b -= a; b ^= (a << 8); \
3687 c -= a; c -= b; c ^= (b >> 13); \
3688 a -= b; a -= c; a ^= (c >> 12); \
3689 b -= c; b -= a; b ^= (a << 16); \
3690 c -= a; c -= b; c ^= (b >> 5); \
3691 a -= b; a -= c; a ^= (c >> 3); \
3692 b -= c; b -= a; b ^= (a << 10); \
3693 c -= a; c -= b; c ^= (b >> 15); \
3694 } while (/*CONSTCOND*/0)
3696 static __inline uint32_t
3697 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
3699 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
3710 return (c & BRIDGE_RTHASH_MASK);
3716 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
3720 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
3721 d = ((int)a[i]) - ((int)b[i]);
3728 * bridge_rtnode_lookup:
3730 * Look up a bridge route node for the specified destination.
3732 static struct bridge_rtnode *
3733 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
3735 struct bridge_rtnode *brt;
3739 hash = bridge_rthash(sc, addr);
3740 LIST_FOREACH(brt, &sc->sc_rthashs[mycpuid][hash], brt_hash) {
3741 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
3752 * bridge_rtnode_insert:
3754 * Insert the specified bridge node into the route table.
3755 * Caller has to make sure that rtnode does not exist.
3758 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
3760 struct bridge_rtnode *lbrt;
3764 hash = bridge_rthash(sc, brt->brt_addr);
3766 lbrt = LIST_FIRST(&sc->sc_rthashs[mycpuid][hash]);
3768 LIST_INSERT_HEAD(&sc->sc_rthashs[mycpuid][hash],
3774 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
3775 KASSERT(dir != 0, ("rtnode already exist"));
3778 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
3781 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
3782 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
3785 lbrt = LIST_NEXT(lbrt, brt_hash);
3786 } while (lbrt != NULL);
3788 panic("no suitable position found for rtnode");
3790 LIST_INSERT_HEAD(&sc->sc_rtlists[mycpuid], brt, brt_list);
3793 * Update the brtcnt.
3794 * We only need to do it once and we do it on CPU0.
3801 * bridge_rtnode_destroy:
3803 * Destroy a bridge rtnode.
3806 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3808 LIST_REMOVE(brt, brt_hash);
3809 LIST_REMOVE(brt, brt_list);
3811 if (mycpuid + 1 == ncpus) {
3812 /* Free rtinfo associated with rtnode on the last cpu */
3813 kfree(brt->brt_info, M_DEVBUF);
3815 kfree(brt, M_DEVBUF);
3818 /* Update brtcnt only on CPU0 */
3824 bridge_post_pfil(struct mbuf *m)
3826 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED)
3830 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED)
3837 * Send bridge packets through pfil if they are one of the types pfil can deal
3838 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
3839 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3843 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3845 int snap, error, i, hlen;
3846 struct ether_header *eh1, eh2;
3849 u_int16_t ether_type;
3852 error = -1; /* Default error if not error == 0 */
3854 if (pfil_bridge == 0 && pfil_member == 0)
3855 return (0); /* filtering is disabled */
3857 i = min((*mp)->m_pkthdr.len, max_protohdr);
3858 if ((*mp)->m_len < i) {
3859 *mp = m_pullup(*mp, i);
3861 kprintf("%s: m_pullup failed\n", __func__);
3866 eh1 = mtod(*mp, struct ether_header *);
3867 ether_type = ntohs(eh1->ether_type);
3870 * Check for SNAP/LLC.
3872 if (ether_type < ETHERMTU) {
3873 struct llc *llc2 = (struct llc *)(eh1 + 1);
3875 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3876 llc2->llc_dsap == LLC_SNAP_LSAP &&
3877 llc2->llc_ssap == LLC_SNAP_LSAP &&
3878 llc2->llc_control == LLC_UI) {
3879 ether_type = htons(llc2->llc_un.type_snap.ether_type);
3885 * If we're trying to filter bridge traffic, don't look at anything
3886 * other than IP and ARP traffic. If the filter doesn't understand
3887 * IPv6, don't allow IPv6 through the bridge either. This is lame
3888 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3889 * but of course we don't have an AppleTalk filter to begin with.
3890 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3893 switch (ether_type) {
3895 case ETHERTYPE_REVARP:
3896 return (0); /* Automatically pass */
3900 case ETHERTYPE_IPV6:
3906 * Check to see if the user wants to pass non-ip
3907 * packets, these will not be checked by pfil(9)
3908 * and passed unconditionally so the default is to drop.
3914 /* Strip off the Ethernet header and keep a copy. */
3915 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3916 m_adj(*mp, ETHER_HDR_LEN);
3918 /* Strip off snap header, if present */
3920 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3921 m_adj(*mp, sizeof(struct llc));
3925 * Check the IP header for alignment and errors
3927 if (dir == PFIL_IN) {
3928 switch (ether_type) {
3930 error = bridge_ip_checkbasic(mp);
3933 case ETHERTYPE_IPV6:
3934 error = bridge_ip6_checkbasic(mp);
3947 * Run the packet through pfil
3949 switch (ether_type) {
3952 * before calling the firewall, swap fields the same as
3953 * IP does. here we assume the header is contiguous
3955 ip = mtod(*mp, struct ip *);
3957 ip->ip_len = ntohs(ip->ip_len);
3958 ip->ip_off = ntohs(ip->ip_off);
3961 * Run pfil on the member interface and the bridge, both can
3962 * be skipped by clearing pfil_member or pfil_bridge.
3965 * in_if -> bridge_if -> out_if
3967 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) {
3968 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3969 if (*mp == NULL || error != 0) /* filter may consume */
3971 error = bridge_post_pfil(*mp);
3976 if (pfil_member && ifp != NULL) {
3977 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp, dir);
3978 if (*mp == NULL || error != 0) /* filter may consume */
3980 error = bridge_post_pfil(*mp);
3985 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) {
3986 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3987 if (*mp == NULL || error != 0) /* filter may consume */
3989 error = bridge_post_pfil(*mp);
3994 /* check if we need to fragment the packet */
3995 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
3996 i = (*mp)->m_pkthdr.len;
3997 if (i > ifp->if_mtu) {
3998 error = bridge_fragment(ifp, *mp, &eh2, snap,
4004 /* Recalculate the ip checksum and restore byte ordering */
4005 ip = mtod(*mp, struct ip *);
4006 hlen = ip->ip_hl << 2;
4007 if (hlen < sizeof(struct ip))
4009 if (hlen > (*mp)->m_len) {
4010 if ((*mp = m_pullup(*mp, hlen)) == NULL)
4012 ip = mtod(*mp, struct ip *);
4016 ip->ip_len = htons(ip->ip_len);
4017 ip->ip_off = htons(ip->ip_off);
4019 if (hlen == sizeof(struct ip))
4020 ip->ip_sum = in_cksum_hdr(ip);
4022 ip->ip_sum = in_cksum(*mp, hlen);
4026 case ETHERTYPE_IPV6:
4027 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
4028 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
4031 if (*mp == NULL || error != 0) /* filter may consume */
4034 if (pfil_member && ifp != NULL)
4035 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
4038 if (*mp == NULL || error != 0) /* filter may consume */
4041 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
4042 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
4059 * Finally, put everything back the way it was and return
4062 M_PREPEND(*mp, sizeof(struct llc), M_NOWAIT);
4065 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
4068 M_PREPEND(*mp, ETHER_HDR_LEN, M_NOWAIT);
4071 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
4082 * Perform basic checks on header size since
4083 * pfil assumes ip_input has already processed
4084 * it for it. Cut-and-pasted from ip_input.c.
4085 * Given how simple the IPv6 version is,
4086 * does the IPv4 version really need to be
4089 * XXX Should we update ipstat here, or not?
4090 * XXX Right now we update ipstat but not
4094 bridge_ip_checkbasic(struct mbuf **mp)
4096 struct mbuf *m = *mp;
4104 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
4105 if ((m = m_copyup(m, sizeof(struct ip),
4106 (max_linkhdr + 3) & ~3)) == NULL) {
4107 /* XXXJRT new stat, please */
4108 ipstat.ips_toosmall++;
4113 #ifndef __predict_false
4114 #define __predict_false(x) x
4116 if (__predict_false(m->m_len < sizeof (struct ip))) {
4117 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
4118 ipstat.ips_toosmall++;
4122 ip = mtod(m, struct ip *);
4123 if (ip == NULL) goto bad;
4125 if (ip->ip_v != IPVERSION) {
4126 ipstat.ips_badvers++;
4129 hlen = ip->ip_hl << 2;
4130 if (hlen < sizeof(struct ip)) { /* minimum header length */
4131 ipstat.ips_badhlen++;
4134 if (hlen > m->m_len) {
4135 if ((m = m_pullup(m, hlen)) == NULL) {
4136 ipstat.ips_badhlen++;
4139 ip = mtod(m, struct ip *);
4140 if (ip == NULL) goto bad;
4143 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
4144 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
4146 if (hlen == sizeof(struct ip)) {
4147 sum = in_cksum_hdr(ip);
4149 sum = in_cksum(m, hlen);
4153 ipstat.ips_badsum++;
4157 /* Retrieve the packet length. */
4158 len = ntohs(ip->ip_len);
4161 * Check for additional length bogosity
4164 ipstat.ips_badlen++;
4169 * Check that the amount of data in the buffers
4170 * is as at least much as the IP header would have us expect.
4171 * Drop packet if shorter than we expect.
4173 if (m->m_pkthdr.len < len) {
4174 ipstat.ips_tooshort++;
4178 /* Checks out, proceed */
4189 * Same as above, but for IPv6.
4190 * Cut-and-pasted from ip6_input.c.
4191 * XXX Should we update ip6stat, or not?
4194 bridge_ip6_checkbasic(struct mbuf **mp)
4196 struct mbuf *m = *mp;
4197 struct ip6_hdr *ip6;
4200 * If the IPv6 header is not aligned, slurp it up into a new
4201 * mbuf with space for link headers, in the event we forward
4202 * it. Otherwise, if it is aligned, make sure the entire base
4203 * IPv6 header is in the first mbuf of the chain.
4206 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
4207 struct ifnet *inifp = m->m_pkthdr.rcvif;
4208 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
4209 (max_linkhdr + 3) & ~3)) == NULL) {
4210 /* XXXJRT new stat, please */
4211 ip6stat.ip6s_toosmall++;
4212 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
4217 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
4218 struct ifnet *inifp = m->m_pkthdr.rcvif;
4219 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
4220 ip6stat.ip6s_toosmall++;
4221 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
4226 ip6 = mtod(m, struct ip6_hdr *);
4228 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
4229 ip6stat.ip6s_badvers++;
4230 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
4234 /* Checks out, proceed */
4247 * Return a fragmented mbuf chain.
4250 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
4251 int snap, struct llc *llc)
4257 if (m->m_len < sizeof(struct ip) &&
4258 (m = m_pullup(m, sizeof(struct ip))) == NULL)
4260 ip = mtod(m, struct ip *);
4262 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
4267 /* walk the chain and re-add the Ethernet header */
4268 for (m0 = m; m0; m0 = m0->m_nextpkt) {
4271 M_PREPEND(m0, sizeof(struct llc), M_NOWAIT);
4276 bcopy(llc, mtod(m0, caddr_t),
4277 sizeof(struct llc));
4279 M_PREPEND(m0, ETHER_HDR_LEN, M_NOWAIT);
4284 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
4290 ipstat.ips_fragmented++;
4301 bridge_enqueue_handler(netmsg_t msg)
4303 struct netmsg_packet *nmp;
4304 struct ifnet *dst_ifp;
4309 dst_ifp = nmp->base.lmsg.u.ms_resultp;
4312 bridge_handoff(dst_ifp->if_bridge, dst_ifp, m, 1);
4316 bridge_handoff(struct bridge_softc *sc, struct ifnet *dst_ifp,
4317 struct mbuf *m, int from_us)
4325 /* We may be sending a fragment so traverse the mbuf */
4327 struct altq_pktattr pktattr;
4330 m->m_nextpkt = NULL;
4333 * If being sent from our host override ether_shost
4334 * with the bridge MAC. This is mandatory for ARP
4335 * so things don't get confused. In particular we
4336 * don't want ARPs to get associated with link interfaces
4337 * under the bridge which might or might not stay valid.
4339 * Also override ether_shost when relaying a packet out
4340 * the same interface it came in on, due to multi-homed
4341 * addresses & default routes, otherwise switches will
4342 * get very confused.
4344 * Otherwise if we are in transparent mode.
4346 if (from_us || m->m_pkthdr.rcvif == dst_ifp) {
4348 offsetof(struct ether_header, ether_shost),
4349 ETHER_ADDR_LEN, IF_LLADDR(sc->sc_ifp));
4350 } else if ((bifp->if_flags & IFF_LINK0) &&
4351 (m->m_pkthdr.fw_flags & BRIDGE_MBUF_TAGGED)) {
4353 offsetof(struct ether_header, ether_shost),
4355 m->m_pkthdr.ether_br_shost);
4356 } /* else retain shost */
4358 if (ifq_is_enabled(&dst_ifp->if_snd))
4359 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
4361 ifq_dispatch(dst_ifp, m, &pktattr);
4366 bridge_control_dispatch(netmsg_t msg)
4368 struct netmsg_brctl *bc_msg = (struct netmsg_brctl *)msg;
4369 struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
4372 ifnet_serialize_all(bifp);
4373 error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
4374 ifnet_deserialize_all(bifp);
4376 lwkt_replymsg(&bc_msg->base.lmsg, error);
4380 bridge_control(struct bridge_softc *sc, u_long cmd,
4381 bridge_ctl_t bc_func, void *bc_arg)
4383 struct ifnet *bifp = sc->sc_ifp;
4384 struct netmsg_brctl bc_msg;
4387 ASSERT_IFNET_SERIALIZED_ALL(bifp);
4389 bzero(&bc_msg, sizeof(bc_msg));
4391 netmsg_init(&bc_msg.base, NULL, &curthread->td_msgport,
4392 0, bridge_control_dispatch);
4393 bc_msg.bc_func = bc_func;
4395 bc_msg.bc_arg = bc_arg;
4397 ifnet_deserialize_all(bifp);
4398 error = lwkt_domsg(BRIDGE_CFGPORT, &bc_msg.base.lmsg, 0);
4399 ifnet_serialize_all(bifp);
4404 bridge_add_bif_handler(netmsg_t msg)
4406 struct netmsg_braddbif *amsg = (struct netmsg_braddbif *)msg;
4407 struct bridge_softc *sc;
4408 struct bridge_iflist *bif;
4410 sc = amsg->br_softc;
4412 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
4413 bif->bif_ifp = amsg->br_bif_ifp;
4414 bif->bif_onlist = 1;
4415 bif->bif_info = amsg->br_bif_info;
4418 * runs through bif_info
4420 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
4422 TAILQ_INSERT_HEAD(&sc->sc_iflists[mycpuid], bif, bif_next);
4424 netisr_forwardmsg(&amsg->base, mycpuid + 1);
4428 bridge_add_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
4431 struct netmsg_braddbif amsg;
4433 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
4435 netmsg_init(&amsg.base, NULL, &curthread->td_msgport,
4436 0, bridge_add_bif_handler);
4438 amsg.br_bif_info = bif_info;
4439 amsg.br_bif_ifp = ifp;
4441 netisr_domsg(&amsg.base, 0);
4445 bridge_del_bif_handler(netmsg_t msg)
4447 struct netmsg_brdelbif *dmsg = (struct netmsg_brdelbif *)msg;
4448 struct bridge_softc *sc;
4449 struct bridge_iflist *bif;
4451 sc = dmsg->br_softc;
4454 * Locate the bif associated with the br_bif_info
4455 * on the current CPU
4457 bif = bridge_lookup_member_ifinfo(sc, dmsg->br_bif_info);
4458 KKASSERT(bif != NULL && bif->bif_onlist);
4460 /* Remove the bif from the current CPU's iflist */
4461 bif->bif_onlist = 0;
4462 TAILQ_REMOVE(dmsg->br_bif_list, bif, bif_next);
4464 /* Save the removed bif for later freeing */
4465 TAILQ_INSERT_HEAD(dmsg->br_bif_list, bif, bif_next);
4467 netisr_forwardmsg(&dmsg->base, mycpuid + 1);
4471 bridge_del_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
4472 struct bridge_iflist_head *saved_bifs)
4474 struct netmsg_brdelbif dmsg;
4476 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
4478 netmsg_init(&dmsg.base, NULL, &curthread->td_msgport,
4479 0, bridge_del_bif_handler);
4481 dmsg.br_bif_info = bif_info;
4482 dmsg.br_bif_list = saved_bifs;
4484 netisr_domsg(&dmsg.base, 0);