2 * Copyright 2001 Wasabi Systems, Inc.
5 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Wasabi Systems, Inc.
19 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
20 * or promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by Jason L. Wright
51 * 4. The name of the author may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
56 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
57 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
63 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
66 * $OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp $
67 * $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $
68 * $FreeBSD: src/sys/net/if_bridge.c,v 1.26 2005/10/13 23:05:55 thompsa Exp $
69 * $DragonFly: src/sys/net/bridge/if_bridge.c,v 1.59 2008/11/23 02:58:26 sephe Exp $
73 * Network interface bridge support.
77 * - Currently only supports Ethernet-like interfaces (Ethernet,
78 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
79 * to bridge other types of interfaces (FDDI-FDDI, and maybe
80 * consider heterogenous bridges).
83 * Bridge's route information is duplicated to each CPUs:
86 * +-----------+ +-----------+ +-----------+ +-----------+
87 * | rtnode | | rtnode | | rtnode | | rtnode |
89 * | dst eaddr | | dst eaddr | | dst eaddr | | dst eaddr |
90 * +-----------+ +-----------+ +-----------+ +-----------+
93 * | | +----------+ | |
97 * +-------------->| timeout |<-------------+
101 * We choose to put timeout and dst_ifp into shared part, so updating
102 * them will be cheaper than using message forwarding. Also there is
103 * not need to use spinlock to protect the updating: timeout and dst_ifp
104 * is not related and specific field's updating order has no importance.
105 * The cache pollution by the share part should not be heavy: in a stable
106 * setup, dst_ifp probably will be not changed in rtnode's life time,
107 * while timeout is refreshed once per second; most of the time, timeout
108 * and dst_ifp are read-only accessed.
111 * Bridge route information installation on bridge_input path:
113 * CPU0 CPU1 CPU2 CPU3
120 * ifnet0<-----------------------+
123 * rtnode exists?(Y)free nmsg :
154 * The netmsgs forwarded between protocol threads and ifnet threads are
155 * allocated with (M_WAITOK|M_NULLOK), so it will not fail under most
156 * cases (route information is too precious to be not installed :).
157 * Since multiple threads may try to install route information for the
158 * same dst eaddr, we look up route information in ifnet0. However, this
159 * looking up only need to be performed on ifnet0, which is the start
160 * point of the route information installation process.
163 * Bridge route information deleting/flushing:
165 * CPU0 CPU1 CPU2 CPU3
169 * find suitable rtnodes,
170 * mark their rtinfo dead
172 * | domsg <------------------------------------------+
175 * V fwdmsg fwdmsg fwdmsg |
176 * ifnet0 --------> ifnet1 --------> ifnet2 --------> ifnet3
177 * delete rtnodes delete rtnodes delete rtnodes delete rtnodes
178 * w/ dead rtinfo 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 * ifnet threads, it is not possible to create corrupted per-cpu route
192 * Percpu member interface list iteration with blocking operation:
193 * Since one bridge could only delete one member interface at a time and
194 * the deleted member interface is not freed after netmsg_service_sync(),
195 * following way is used to make sure that even if the certain member
196 * interface is ripped from the percpu list during the blocking operation,
197 * the iteration still could keep going:
199 * LIST_FOREACH_MUTABLE(bif, sc->sc_iflists[mycpuid], bif_next, nbif) {
200 * blocking operation;
201 * blocking operation;
204 * if (nbif != NULL && !nbif->bif_onlist) {
205 * KKASSERT(bif->bif_onlist);
206 * nbif = LIST_NEXT(bif, bif_next);
210 * As mentioned above only one member interface could be unlinked from the
211 * percpu member interface list, so either bif or nbif may be not on the list,
212 * but _not_ both. To keep the list iteration, we don't care about bif, but
213 * only nbif. Since removed member interface will only be freed after we
214 * finish our work, it is safe to access any field in an unlinked bif (here
215 * bif_onlist). If nbif is no longer on the list, then bif must be on the
216 * list, so we change nbif to the next element of bif and keep going.
219 #include "opt_inet.h"
220 #include "opt_inet6.h"
222 #include <sys/param.h>
223 #include <sys/mbuf.h>
224 #include <sys/malloc.h>
225 #include <sys/protosw.h>
226 #include <sys/systm.h>
227 #include <sys/time.h>
228 #include <sys/socket.h> /* for net/if.h */
229 #include <sys/sockio.h>
230 #include <sys/ctype.h> /* string functions */
231 #include <sys/kernel.h>
232 #include <sys/random.h>
233 #include <sys/sysctl.h>
234 #include <sys/module.h>
235 #include <sys/proc.h>
236 #include <sys/lock.h>
237 #include <sys/thread.h>
238 #include <sys/thread2.h>
239 #include <sys/mpipe.h>
243 #include <net/if_dl.h>
244 #include <net/if_types.h>
245 #include <net/if_var.h>
246 #include <net/pfil.h>
247 #include <net/ifq_var.h>
248 #include <net/if_clone.h>
250 #include <netinet/in.h> /* for struct arpcom */
251 #include <netinet/in_systm.h>
252 #include <netinet/in_var.h>
253 #include <netinet/ip.h>
254 #include <netinet/ip_var.h>
256 #include <netinet/ip6.h>
257 #include <netinet6/ip6_var.h>
259 #include <netinet/if_ether.h> /* for struct arpcom */
260 #include <net/bridge/if_bridgevar.h>
261 #include <net/if_llc.h>
262 #include <net/netmsg2.h>
264 #include <net/route.h>
265 #include <sys/in_cksum.h>
268 * Size of the route hash table. Must be a power of two.
270 #ifndef BRIDGE_RTHASH_SIZE
271 #define BRIDGE_RTHASH_SIZE 1024
274 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
277 * Maximum number of addresses to cache.
279 #ifndef BRIDGE_RTABLE_MAX
280 #define BRIDGE_RTABLE_MAX 100
284 * Spanning tree defaults.
286 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
287 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
288 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
289 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
290 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
291 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
292 #define BSTP_DEFAULT_PATH_COST 55
295 * Timeout (in seconds) for entries learned dynamically.
297 #ifndef BRIDGE_RTABLE_TIMEOUT
298 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
302 * Number of seconds between walks of the route list.
304 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
305 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
309 * List of capabilities to mask on the member interface.
311 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM
313 typedef int (*bridge_ctl_t)(struct bridge_softc *, void *);
315 struct netmsg_brctl {
316 struct netmsg bc_nmsg;
317 bridge_ctl_t bc_func;
318 struct bridge_softc *bc_sc;
322 struct netmsg_brsaddr {
323 struct netmsg br_nmsg;
324 struct bridge_softc *br_softc;
325 struct ifnet *br_dst_if;
326 struct bridge_rtinfo *br_rtinfo;
328 uint8_t br_dst[ETHER_ADDR_LEN];
332 struct netmsg_braddbif {
333 struct netmsg br_nmsg;
334 struct bridge_softc *br_softc;
335 struct bridge_ifinfo *br_bif_info;
336 struct ifnet *br_bif_ifp;
339 struct netmsg_brdelbif {
340 struct netmsg br_nmsg;
341 struct bridge_softc *br_softc;
342 struct bridge_ifinfo *br_bif_info;
343 struct bridge_iflist_head *br_bif_list;
346 struct netmsg_brsflags {
347 struct netmsg br_nmsg;
348 struct bridge_softc *br_softc;
349 struct bridge_ifinfo *br_bif_info;
350 uint32_t br_bif_flags;
353 eventhandler_tag bridge_detach_cookie = NULL;
355 extern struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
356 extern int (*bridge_output_p)(struct ifnet *, struct mbuf *);
357 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
359 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
361 static int bridge_clone_create(struct if_clone *, int);
362 static void bridge_clone_destroy(struct ifnet *);
364 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
365 static void bridge_mutecaps(struct bridge_ifinfo *, struct ifnet *, int);
366 static void bridge_ifdetach(void *, struct ifnet *);
367 static void bridge_init(void *);
368 static void bridge_stop(struct ifnet *);
369 static void bridge_start(struct ifnet *);
370 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
371 static int bridge_output(struct ifnet *, struct mbuf *);
373 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
375 static void bridge_timer_handler(struct netmsg *);
376 static void bridge_timer(void *);
378 static void bridge_start_bcast(struct bridge_softc *, struct mbuf *);
379 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
381 static void bridge_span(struct bridge_softc *, struct mbuf *);
383 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
384 struct ifnet *, uint8_t);
385 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
386 static void bridge_rtreap(struct bridge_softc *);
387 static void bridge_rttrim(struct bridge_softc *);
388 static int bridge_rtage_finddead(struct bridge_softc *);
389 static void bridge_rtage(struct bridge_softc *);
390 static void bridge_rtflush(struct bridge_softc *, int);
391 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
392 static int bridge_rtsaddr(struct bridge_softc *, const uint8_t *,
393 struct ifnet *, uint8_t);
394 static void bridge_rtmsg_sync(struct bridge_softc *sc);
395 static void bridge_rtreap_handler(struct netmsg *);
396 static void bridge_rtinstall_handler(struct netmsg *);
397 static int bridge_rtinstall_oncpu(struct bridge_softc *, const uint8_t *,
398 struct ifnet *, int, uint8_t, struct bridge_rtinfo **);
400 static void bridge_rtable_init(struct bridge_softc *);
401 static void bridge_rtable_fini(struct bridge_softc *);
403 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
404 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
406 static void bridge_rtnode_insert(struct bridge_softc *,
407 struct bridge_rtnode *);
408 static void bridge_rtnode_destroy(struct bridge_softc *,
409 struct bridge_rtnode *);
411 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
413 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
415 static struct bridge_iflist *bridge_lookup_member_ifinfo(struct bridge_softc *,
416 struct bridge_ifinfo *);
417 static void bridge_delete_member(struct bridge_softc *,
418 struct bridge_iflist *, int);
419 static void bridge_delete_span(struct bridge_softc *,
420 struct bridge_iflist *);
422 static int bridge_control(struct bridge_softc *, u_long,
423 bridge_ctl_t, void *);
424 static int bridge_ioctl_init(struct bridge_softc *, void *);
425 static int bridge_ioctl_stop(struct bridge_softc *, void *);
426 static int bridge_ioctl_add(struct bridge_softc *, void *);
427 static int bridge_ioctl_del(struct bridge_softc *, void *);
428 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
429 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
430 static int bridge_ioctl_scache(struct bridge_softc *, void *);
431 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
432 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
433 static int bridge_ioctl_rts(struct bridge_softc *, void *);
434 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
435 static int bridge_ioctl_sto(struct bridge_softc *, void *);
436 static int bridge_ioctl_gto(struct bridge_softc *, void *);
437 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
438 static int bridge_ioctl_flush(struct bridge_softc *, void *);
439 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
440 static int bridge_ioctl_spri(struct bridge_softc *, void *);
441 static int bridge_ioctl_ght(struct bridge_softc *, void *);
442 static int bridge_ioctl_sht(struct bridge_softc *, void *);
443 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
444 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
445 static int bridge_ioctl_gma(struct bridge_softc *, void *);
446 static int bridge_ioctl_sma(struct bridge_softc *, void *);
447 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
448 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
449 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
450 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
451 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
453 static int bridge_ip_checkbasic(struct mbuf **mp);
455 static int bridge_ip6_checkbasic(struct mbuf **mp);
457 static int bridge_fragment(struct ifnet *, struct mbuf *,
458 struct ether_header *, int, struct llc *);
459 static void bridge_enqueue_handler(struct netmsg *);
460 static void bridge_handoff(struct ifnet *, struct mbuf *);
462 static void bridge_del_bif_handler(struct netmsg *);
463 static void bridge_add_bif_handler(struct netmsg *);
464 static void bridge_set_bifflags_handler(struct netmsg *);
465 static void bridge_del_bif(struct bridge_softc *, struct bridge_ifinfo *,
466 struct bridge_iflist_head *);
467 static void bridge_add_bif(struct bridge_softc *, struct bridge_ifinfo *,
469 static void bridge_set_bifflags(struct bridge_softc *,
470 struct bridge_ifinfo *, uint32_t);
472 SYSCTL_DECL(_net_link);
473 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
475 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
476 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
477 static int pfil_member = 1; /* run pfil hooks on the member interface */
478 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
479 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
480 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
481 &pfil_bridge, 0, "Packet filter on the bridge interface");
482 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
483 &pfil_member, 0, "Packet filter on the member interface");
485 struct bridge_control_arg {
487 struct ifbreq ifbreq;
488 struct ifbifconf ifbifconf;
489 struct ifbareq ifbareq;
490 struct ifbaconf ifbaconf;
491 struct ifbrparam ifbrparam;
498 struct bridge_control {
499 bridge_ctl_t bc_func;
504 #define BC_F_COPYIN 0x01 /* copy arguments in */
505 #define BC_F_COPYOUT 0x02 /* copy arguments out */
506 #define BC_F_SUSER 0x04 /* do super-user check */
508 const struct bridge_control bridge_control_table[] = {
509 { bridge_ioctl_add, sizeof(struct ifbreq),
510 BC_F_COPYIN|BC_F_SUSER },
511 { bridge_ioctl_del, sizeof(struct ifbreq),
512 BC_F_COPYIN|BC_F_SUSER },
514 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
515 BC_F_COPYIN|BC_F_COPYOUT },
516 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
517 BC_F_COPYIN|BC_F_SUSER },
519 { bridge_ioctl_scache, sizeof(struct ifbrparam),
520 BC_F_COPYIN|BC_F_SUSER },
521 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
524 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
525 BC_F_COPYIN|BC_F_COPYOUT },
526 { bridge_ioctl_rts, sizeof(struct ifbaconf),
527 BC_F_COPYIN|BC_F_COPYOUT },
529 { bridge_ioctl_saddr, sizeof(struct ifbareq),
530 BC_F_COPYIN|BC_F_SUSER },
532 { bridge_ioctl_sto, sizeof(struct ifbrparam),
533 BC_F_COPYIN|BC_F_SUSER },
534 { bridge_ioctl_gto, sizeof(struct ifbrparam),
537 { bridge_ioctl_daddr, sizeof(struct ifbareq),
538 BC_F_COPYIN|BC_F_SUSER },
540 { bridge_ioctl_flush, sizeof(struct ifbreq),
541 BC_F_COPYIN|BC_F_SUSER },
543 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
545 { bridge_ioctl_spri, sizeof(struct ifbrparam),
546 BC_F_COPYIN|BC_F_SUSER },
548 { bridge_ioctl_ght, sizeof(struct ifbrparam),
550 { bridge_ioctl_sht, sizeof(struct ifbrparam),
551 BC_F_COPYIN|BC_F_SUSER },
553 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
555 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
556 BC_F_COPYIN|BC_F_SUSER },
558 { bridge_ioctl_gma, sizeof(struct ifbrparam),
560 { bridge_ioctl_sma, sizeof(struct ifbrparam),
561 BC_F_COPYIN|BC_F_SUSER },
563 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
564 BC_F_COPYIN|BC_F_SUSER },
566 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
567 BC_F_COPYIN|BC_F_SUSER },
569 { bridge_ioctl_addspan, sizeof(struct ifbreq),
570 BC_F_COPYIN|BC_F_SUSER },
571 { bridge_ioctl_delspan, sizeof(struct ifbreq),
572 BC_F_COPYIN|BC_F_SUSER },
574 static const int bridge_control_table_size =
575 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
577 LIST_HEAD(, bridge_softc) bridge_list;
579 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
581 bridge_clone_destroy, 0, IF_MAXUNIT);
584 bridge_modevent(module_t mod, int type, void *data)
588 LIST_INIT(&bridge_list);
589 if_clone_attach(&bridge_cloner);
590 bridge_input_p = bridge_input;
591 bridge_output_p = bridge_output;
592 bridge_detach_cookie = EVENTHANDLER_REGISTER(
593 ifnet_detach_event, bridge_ifdetach, NULL,
594 EVENTHANDLER_PRI_ANY);
596 bstp_linkstate_p = bstp_linkstate;
600 if (!LIST_EMPTY(&bridge_list))
602 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
603 bridge_detach_cookie);
604 if_clone_detach(&bridge_cloner);
605 bridge_input_p = NULL;
606 bridge_output_p = NULL;
608 bstp_linkstate_p = NULL;
617 static moduledata_t bridge_mod = {
623 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
627 * bridge_clone_create:
629 * Create a new bridge instance.
632 bridge_clone_create(struct if_clone *ifc, int unit)
634 struct bridge_softc *sc;
639 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
640 ifp = sc->sc_ifp = &sc->sc_if;
642 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
643 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
644 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
645 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
646 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
647 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
648 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
650 /* Initialize our routing table. */
651 bridge_rtable_init(sc);
653 callout_init(&sc->sc_brcallout);
654 netmsg_init(&sc->sc_brtimemsg, &netisr_adone_rport,
655 MSGF_DROPABLE, bridge_timer_handler);
656 sc->sc_brtimemsg.nm_lmsg.u.ms_resultp = sc;
658 callout_init(&sc->sc_bstpcallout);
659 netmsg_init(&sc->sc_bstptimemsg, &netisr_adone_rport,
660 MSGF_DROPABLE, bstp_tick_handler);
661 sc->sc_bstptimemsg.nm_lmsg.u.ms_resultp = sc;
663 /* Initialize per-cpu member iface lists */
664 sc->sc_iflists = kmalloc(sizeof(*sc->sc_iflists) * ncpus,
666 for (cpu = 0; cpu < ncpus; ++cpu)
667 LIST_INIT(&sc->sc_iflists[cpu]);
669 LIST_INIT(&sc->sc_spanlist);
672 if_initname(ifp, ifc->ifc_name, unit);
673 ifp->if_mtu = ETHERMTU;
674 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
675 ifp->if_ioctl = bridge_ioctl;
676 ifp->if_start = bridge_start;
677 ifp->if_init = bridge_init;
678 ifp->if_type = IFT_BRIDGE;
679 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
680 ifp->if_snd.ifq_maxlen = ifqmaxlen;
681 ifq_set_ready(&ifp->if_snd);
682 ifp->if_hdrlen = ETHER_HDR_LEN;
685 * Generate a random ethernet address and use the private AC:DE:48
689 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
691 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
693 eaddr[0] &= ~1; /* clear multicast bit */
694 eaddr[0] |= 2; /* set the LAA bit */
696 ether_ifattach(ifp, eaddr, NULL);
697 /* Now undo some of the damage... */
698 ifp->if_baudrate = 0;
699 ifp->if_type = IFT_BRIDGE;
701 crit_enter(); /* XXX MP */
702 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
709 bridge_delete_dispatch(struct netmsg *nmsg)
711 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
712 struct bridge_softc *sc = lmsg->u.ms_resultp;
713 struct ifnet *bifp = sc->sc_ifp;
714 struct bridge_iflist *bif;
716 lwkt_serialize_enter(bifp->if_serializer);
718 while ((bif = LIST_FIRST(&sc->sc_iflists[mycpuid])) != NULL)
719 bridge_delete_member(sc, bif, 0);
721 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL)
722 bridge_delete_span(sc, bif);
724 lwkt_serialize_exit(bifp->if_serializer);
726 lwkt_replymsg(lmsg, 0);
730 * bridge_clone_destroy:
732 * Destroy a bridge instance.
735 bridge_clone_destroy(struct ifnet *ifp)
737 struct bridge_softc *sc = ifp->if_softc;
738 struct lwkt_msg *lmsg;
741 lwkt_serialize_enter(ifp->if_serializer);
744 ifp->if_flags &= ~IFF_UP;
746 lwkt_serialize_exit(ifp->if_serializer);
748 netmsg_init(&nmsg, &curthread->td_msgport, 0, bridge_delete_dispatch);
749 lmsg = &nmsg.nm_lmsg;
750 lmsg->u.ms_resultp = sc;
751 lwkt_domsg(BRIDGE_CFGPORT, lmsg, 0);
753 crit_enter(); /* XXX MP */
754 LIST_REMOVE(sc, sc_list);
759 /* Tear down the routing table. */
760 bridge_rtable_fini(sc);
762 /* Free per-cpu member iface lists */
763 kfree(sc->sc_iflists, M_DEVBUF);
771 * Handle a control request from the operator.
774 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
776 struct bridge_softc *sc = ifp->if_softc;
777 struct bridge_control_arg args;
778 struct ifdrv *ifd = (struct ifdrv *) data;
779 const struct bridge_control *bc;
782 ASSERT_SERIALIZED(ifp->if_serializer);
791 if (ifd->ifd_cmd >= bridge_control_table_size) {
795 bc = &bridge_control_table[ifd->ifd_cmd];
797 if (cmd == SIOCGDRVSPEC &&
798 (bc->bc_flags & BC_F_COPYOUT) == 0) {
801 } else if (cmd == SIOCSDRVSPEC &&
802 (bc->bc_flags & BC_F_COPYOUT)) {
807 if (bc->bc_flags & BC_F_SUSER) {
808 error = suser_cred(cr, NULL_CRED_OKAY);
813 if (ifd->ifd_len != bc->bc_argsize ||
814 ifd->ifd_len > sizeof(args.bca_u)) {
819 memset(&args, 0, sizeof(args));
820 if (bc->bc_flags & BC_F_COPYIN) {
821 error = copyin(ifd->ifd_data, &args.bca_u,
827 error = bridge_control(sc, cmd, bc->bc_func, &args);
829 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
833 if (bc->bc_flags & BC_F_COPYOUT) {
834 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
835 if (args.bca_len != 0) {
836 KKASSERT(args.bca_kptr != NULL);
838 error = copyout(args.bca_kptr,
839 args.bca_uptr, args.bca_len);
841 kfree(args.bca_kptr, M_TEMP);
843 KKASSERT(args.bca_kptr == NULL);
846 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
851 if (!(ifp->if_flags & IFF_UP) &&
852 (ifp->if_flags & IFF_RUNNING)) {
854 * If interface is marked down and it is running,
858 } else if ((ifp->if_flags & IFF_UP) &&
859 !(ifp->if_flags & IFF_RUNNING)) {
861 * If interface is marked up and it is stopped, then
869 /* Do not allow the MTU to be changed on the bridge */
874 error = ether_ioctl(ifp, cmd, data);
883 * Clear or restore unwanted capabilities on the member interface
886 bridge_mutecaps(struct bridge_ifinfo *bif_info, struct ifnet *ifp, int mute)
891 if (ifp->if_ioctl == NULL)
894 bzero(&ifr, sizeof(ifr));
895 ifr.ifr_reqcap = ifp->if_capenable;
898 /* mask off and save capabilities */
899 bif_info->bifi_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
900 if (bif_info->bifi_mutecap != 0)
901 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
903 /* restore muted capabilities */
904 ifr.ifr_reqcap |= bif_info->bifi_mutecap;
907 if (bif_info->bifi_mutecap != 0) {
908 lwkt_serialize_enter(ifp->if_serializer);
909 error = ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
910 lwkt_serialize_exit(ifp->if_serializer);
915 * bridge_lookup_member:
917 * Lookup a bridge member interface.
919 static struct bridge_iflist *
920 bridge_lookup_member(struct bridge_softc *sc, const char *name)
922 struct bridge_iflist *bif;
924 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
925 if (strcmp(bif->bif_ifp->if_xname, name) == 0)
932 * bridge_lookup_member_if:
934 * Lookup a bridge member interface by ifnet*.
936 static struct bridge_iflist *
937 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
939 struct bridge_iflist *bif;
941 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
942 if (bif->bif_ifp == member_ifp)
949 * bridge_lookup_member_ifinfo:
951 * Lookup a bridge member interface by bridge_ifinfo.
953 static struct bridge_iflist *
954 bridge_lookup_member_ifinfo(struct bridge_softc *sc,
955 struct bridge_ifinfo *bif_info)
957 struct bridge_iflist *bif;
959 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
960 if (bif->bif_info == bif_info)
967 * bridge_delete_member:
969 * Delete the specified member interface.
972 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
975 struct ifnet *ifs = bif->bif_ifp;
976 struct ifnet *bifp = sc->sc_ifp;
977 struct bridge_ifinfo *bif_info = bif->bif_info;
978 struct bridge_iflist_head saved_bifs;
980 ASSERT_SERIALIZED(bifp->if_serializer);
981 KKASSERT(bif_info != NULL);
983 ifs->if_bridge = NULL;
986 * Release bridge interface's serializer:
987 * - To avoid possible dead lock.
988 * - Various sync operation will block the current thread.
990 lwkt_serialize_exit(bifp->if_serializer);
993 switch (ifs->if_type) {
997 * Take the interface out of promiscuous mode.
1000 bridge_mutecaps(bif_info, ifs, 0);
1007 panic("bridge_delete_member: impossible");
1013 * Remove bifs from percpu linked list.
1015 * Removed bifs are not freed immediately, instead,
1016 * they are saved in saved_bifs. They will be freed
1017 * after we make sure that no one is accessing them,
1018 * i.e. after following netmsg_service_sync()
1020 LIST_INIT(&saved_bifs);
1021 bridge_del_bif(sc, bif_info, &saved_bifs);
1024 * Make sure that all protocol threads:
1025 * o see 'ifs' if_bridge is changed
1026 * o know that bif is removed from the percpu linked list
1028 netmsg_service_sync();
1031 * Free the removed bifs
1033 KKASSERT(!LIST_EMPTY(&saved_bifs));
1034 while ((bif = LIST_FIRST(&saved_bifs)) != NULL) {
1035 LIST_REMOVE(bif, bif_next);
1036 kfree(bif, M_DEVBUF);
1039 /* See the comment in bridge_ioctl_stop() */
1040 bridge_rtmsg_sync(sc);
1041 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
1043 lwkt_serialize_enter(bifp->if_serializer);
1045 if (bifp->if_flags & IFF_RUNNING)
1046 bstp_initialization(sc);
1049 * Free the bif_info after bstp_initialization(), so that
1050 * bridge_softc.sc_root_port will not reference a dangling
1053 kfree(bif_info, M_DEVBUF);
1057 * bridge_delete_span:
1059 * Delete the specified span interface.
1062 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1064 KASSERT(bif->bif_ifp->if_bridge == NULL,
1065 ("%s: not a span interface", __func__));
1067 LIST_REMOVE(bif, bif_next);
1068 kfree(bif, M_DEVBUF);
1072 bridge_ioctl_init(struct bridge_softc *sc, void *arg __unused)
1074 struct ifnet *ifp = sc->sc_ifp;
1076 if (ifp->if_flags & IFF_RUNNING)
1079 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1082 ifp->if_flags |= IFF_RUNNING;
1083 bstp_initialization(sc);
1088 bridge_ioctl_stop(struct bridge_softc *sc, void *arg __unused)
1090 struct ifnet *ifp = sc->sc_ifp;
1091 struct lwkt_msg *lmsg;
1093 if ((ifp->if_flags & IFF_RUNNING) == 0)
1096 callout_stop(&sc->sc_brcallout);
1099 lmsg = &sc->sc_brtimemsg.nm_lmsg;
1100 if ((lmsg->ms_flags & MSGF_DONE) == 0) {
1101 /* Pending to be processed; drop it */
1108 ifp->if_flags &= ~IFF_RUNNING;
1110 lwkt_serialize_exit(ifp->if_serializer);
1112 /* Let everyone know that we are stopped */
1113 netmsg_service_sync();
1116 * Sync ifnetX msgports in the order we forward rtnode
1117 * installation message. This is used to make sure that
1118 * all rtnode installation messages sent by bridge_rtupdate()
1119 * during above netmsg_service_sync() are flushed.
1121 bridge_rtmsg_sync(sc);
1122 bridge_rtflush(sc, IFBF_FLUSHDYN);
1124 lwkt_serialize_enter(ifp->if_serializer);
1129 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1131 struct ifbreq *req = arg;
1132 struct bridge_iflist *bif;
1133 struct bridge_ifinfo *bif_info;
1134 struct ifnet *ifs, *bifp;
1138 ASSERT_SERIALIZED(bifp->if_serializer);
1140 ifs = ifunit(req->ifbr_ifsname);
1144 /* If it's in the span list, it can't be a member. */
1145 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1146 if (ifs == bif->bif_ifp)
1149 /* Allow the first Ethernet member to define the MTU */
1150 if (ifs->if_type != IFT_GIF) {
1151 if (LIST_EMPTY(&sc->sc_iflists[mycpuid])) {
1152 bifp->if_mtu = ifs->if_mtu;
1153 } else if (bifp->if_mtu != ifs->if_mtu) {
1154 if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname);
1159 if (ifs->if_bridge == sc)
1162 if (ifs->if_bridge != NULL)
1165 bif_info = kmalloc(sizeof(*bif_info), M_DEVBUF, M_WAITOK | M_ZERO);
1166 bif_info->bifi_priority = BSTP_DEFAULT_PORT_PRIORITY;
1167 bif_info->bifi_path_cost = BSTP_DEFAULT_PATH_COST;
1168 bif_info->bifi_ifp = ifs;
1171 * Release bridge interface's serializer:
1172 * - To avoid possible dead lock.
1173 * - Various sync operation will block the current thread.
1175 lwkt_serialize_exit(bifp->if_serializer);
1177 switch (ifs->if_type) {
1181 * Place the interface into promiscuous mode.
1183 error = ifpromisc(ifs, 1);
1185 lwkt_serialize_enter(bifp->if_serializer);
1188 bridge_mutecaps(bif_info, ifs, 1);
1191 case IFT_GIF: /* :^) */
1196 lwkt_serialize_enter(bifp->if_serializer);
1201 * Add bifs to percpu linked lists
1203 bridge_add_bif(sc, bif_info, ifs);
1205 lwkt_serialize_enter(bifp->if_serializer);
1207 if (bifp->if_flags & IFF_RUNNING)
1208 bstp_initialization(sc);
1213 * Everything has been setup, so let the member interface
1214 * deliver packets to this bridge on its input/output path.
1216 ifs->if_bridge = sc;
1219 if (bif_info != NULL)
1220 kfree(bif_info, M_DEVBUF);
1226 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1228 struct ifbreq *req = arg;
1229 struct bridge_iflist *bif;
1231 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1235 bridge_delete_member(sc, bif, 0);
1241 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1243 struct ifbreq *req = arg;
1244 struct bridge_iflist *bif;
1246 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1250 req->ifbr_ifsflags = bif->bif_flags;
1251 req->ifbr_state = bif->bif_state;
1252 req->ifbr_priority = bif->bif_priority;
1253 req->ifbr_path_cost = bif->bif_path_cost;
1254 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1260 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1262 struct ifbreq *req = arg;
1263 struct bridge_iflist *bif;
1264 struct ifnet *bifp = sc->sc_ifp;
1266 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1270 if (req->ifbr_ifsflags & IFBIF_SPAN) {
1271 /* SPAN is readonly */
1275 if (req->ifbr_ifsflags & IFBIF_STP) {
1276 switch (bif->bif_ifp->if_type) {
1278 /* These can do spanning tree. */
1282 /* Nothing else can. */
1287 lwkt_serialize_exit(bifp->if_serializer);
1288 bridge_set_bifflags(sc, bif->bif_info, req->ifbr_ifsflags);
1289 lwkt_serialize_enter(bifp->if_serializer);
1291 if (bifp->if_flags & IFF_RUNNING)
1292 bstp_initialization(sc);
1298 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1300 struct ifbrparam *param = arg;
1301 struct ifnet *ifp = sc->sc_ifp;
1303 sc->sc_brtmax = param->ifbrp_csize;
1305 lwkt_serialize_exit(ifp->if_serializer);
1307 lwkt_serialize_enter(ifp->if_serializer);
1313 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1315 struct ifbrparam *param = arg;
1317 param->ifbrp_csize = sc->sc_brtmax;
1323 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1325 struct bridge_control_arg *bc_arg = arg;
1326 struct ifbifconf *bifc = arg;
1327 struct bridge_iflist *bif;
1328 struct ifbreq *breq;
1332 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next)
1334 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1337 if (bifc->ifbic_len == 0) {
1338 bifc->ifbic_len = sizeof(*breq) * count;
1340 } else if (count == 0 || bifc->ifbic_len < sizeof(*breq)) {
1341 bifc->ifbic_len = 0;
1345 len = min(bifc->ifbic_len, sizeof(*breq) * count);
1346 KKASSERT(len >= sizeof(*breq));
1348 breq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1350 bifc->ifbic_len = 0;
1353 bc_arg->bca_kptr = breq;
1356 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1357 if (len < sizeof(*breq))
1360 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1361 sizeof(breq->ifbr_ifsname));
1362 breq->ifbr_ifsflags = bif->bif_flags;
1363 breq->ifbr_state = bif->bif_state;
1364 breq->ifbr_priority = bif->bif_priority;
1365 breq->ifbr_path_cost = bif->bif_path_cost;
1366 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1369 len -= sizeof(*breq);
1371 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1372 if (len < sizeof(*breq))
1375 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1376 sizeof(breq->ifbr_ifsname));
1377 breq->ifbr_ifsflags = bif->bif_flags;
1378 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1381 len -= sizeof(*breq);
1384 bifc->ifbic_len = sizeof(*breq) * count;
1385 KKASSERT(bifc->ifbic_len > 0);
1387 bc_arg->bca_len = bifc->ifbic_len;
1388 bc_arg->bca_uptr = bifc->ifbic_req;
1393 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1395 struct bridge_control_arg *bc_arg = arg;
1396 struct ifbaconf *bac = arg;
1397 struct bridge_rtnode *brt;
1398 struct ifbareq *bareq;
1402 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list)
1405 if (bac->ifbac_len == 0) {
1406 bac->ifbac_len = sizeof(*bareq) * count;
1408 } else if (count == 0 || bac->ifbac_len < sizeof(*bareq)) {
1413 len = min(bac->ifbac_len, sizeof(*bareq) * count);
1414 KKASSERT(len >= sizeof(*bareq));
1416 bareq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1417 if (bareq == NULL) {
1421 bc_arg->bca_kptr = bareq;
1424 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
1425 struct bridge_rtinfo *bri = brt->brt_info;
1426 unsigned long expire;
1428 if (len < sizeof(*bareq))
1431 strlcpy(bareq->ifba_ifsname, bri->bri_ifp->if_xname,
1432 sizeof(bareq->ifba_ifsname));
1433 memcpy(bareq->ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1434 expire = bri->bri_expire;
1435 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1436 time_second < expire)
1437 bareq->ifba_expire = expire - time_second;
1439 bareq->ifba_expire = 0;
1440 bareq->ifba_flags = bri->bri_flags;
1443 len -= sizeof(*bareq);
1446 bac->ifbac_len = sizeof(*bareq) * count;
1447 KKASSERT(bac->ifbac_len > 0);
1449 bc_arg->bca_len = bac->ifbac_len;
1450 bc_arg->bca_uptr = bac->ifbac_req;
1455 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1457 struct ifbareq *req = arg;
1458 struct bridge_iflist *bif;
1459 struct ifnet *ifp = sc->sc_ifp;
1462 ASSERT_SERIALIZED(ifp->if_serializer);
1464 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1468 lwkt_serialize_exit(ifp->if_serializer);
1469 error = bridge_rtsaddr(sc, req->ifba_dst, bif->bif_ifp,
1471 lwkt_serialize_enter(ifp->if_serializer);
1476 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1478 struct ifbrparam *param = arg;
1480 sc->sc_brttimeout = param->ifbrp_ctime;
1486 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1488 struct ifbrparam *param = arg;
1490 param->ifbrp_ctime = sc->sc_brttimeout;
1496 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1498 struct ifbareq *req = arg;
1499 struct ifnet *ifp = sc->sc_ifp;
1502 lwkt_serialize_exit(ifp->if_serializer);
1503 error = bridge_rtdaddr(sc, req->ifba_dst);
1504 lwkt_serialize_enter(ifp->if_serializer);
1509 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1511 struct ifbreq *req = arg;
1512 struct ifnet *ifp = sc->sc_ifp;
1514 lwkt_serialize_exit(ifp->if_serializer);
1515 bridge_rtflush(sc, req->ifbr_ifsflags);
1516 lwkt_serialize_enter(ifp->if_serializer);
1522 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1524 struct ifbrparam *param = arg;
1526 param->ifbrp_prio = sc->sc_bridge_priority;
1532 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1534 struct ifbrparam *param = arg;
1536 sc->sc_bridge_priority = param->ifbrp_prio;
1538 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1539 bstp_initialization(sc);
1545 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1547 struct ifbrparam *param = arg;
1549 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1555 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1557 struct ifbrparam *param = arg;
1559 if (param->ifbrp_hellotime == 0)
1561 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1563 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1564 bstp_initialization(sc);
1570 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1572 struct ifbrparam *param = arg;
1574 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1580 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1582 struct ifbrparam *param = arg;
1584 if (param->ifbrp_fwddelay == 0)
1586 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1588 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1589 bstp_initialization(sc);
1595 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1597 struct ifbrparam *param = arg;
1599 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1605 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1607 struct ifbrparam *param = arg;
1609 if (param->ifbrp_maxage == 0)
1611 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1613 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1614 bstp_initialization(sc);
1620 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1622 struct ifbreq *req = arg;
1623 struct bridge_iflist *bif;
1625 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1629 bif->bif_priority = req->ifbr_priority;
1631 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1632 bstp_initialization(sc);
1638 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1640 struct ifbreq *req = arg;
1641 struct bridge_iflist *bif;
1643 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1647 bif->bif_path_cost = req->ifbr_path_cost;
1649 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1650 bstp_initialization(sc);
1656 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1658 struct ifbreq *req = arg;
1659 struct bridge_iflist *bif;
1662 ifs = ifunit(req->ifbr_ifsname);
1666 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1667 if (ifs == bif->bif_ifp)
1670 if (ifs->if_bridge != NULL)
1673 switch (ifs->if_type) {
1683 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
1685 bif->bif_flags = IFBIF_SPAN;
1686 /* NOTE: span bif does not need bridge_ifinfo */
1688 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1696 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1698 struct ifbreq *req = arg;
1699 struct bridge_iflist *bif;
1702 ifs = ifunit(req->ifbr_ifsname);
1706 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1707 if (ifs == bif->bif_ifp)
1713 bridge_delete_span(sc, bif);
1715 if (LIST_EMPTY(&sc->sc_spanlist))
1722 bridge_ifdetach_dispatch(struct netmsg *nmsg)
1724 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
1725 struct ifnet *ifp, *bifp;
1726 struct bridge_softc *sc;
1727 struct bridge_iflist *bif;
1729 ifp = lmsg->u.ms_resultp;
1730 sc = ifp->if_bridge;
1732 /* Check if the interface is a bridge member */
1736 lwkt_serialize_enter(bifp->if_serializer);
1738 bif = bridge_lookup_member_if(sc, ifp);
1740 bridge_delete_member(sc, bif, 1);
1742 /* XXX Why bif will be NULL? */
1745 lwkt_serialize_exit(bifp->if_serializer);
1749 crit_enter(); /* XXX MP */
1751 /* Check if the interface is a span port */
1752 LIST_FOREACH(sc, &bridge_list, sc_list) {
1755 lwkt_serialize_enter(bifp->if_serializer);
1757 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1758 if (ifp == bif->bif_ifp) {
1759 bridge_delete_span(sc, bif);
1763 lwkt_serialize_exit(bifp->if_serializer);
1769 lwkt_replymsg(lmsg, 0);
1775 * Detach an interface from a bridge. Called when a member
1776 * interface is detaching.
1779 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1781 struct lwkt_msg *lmsg;
1784 netmsg_init(&nmsg, &curthread->td_msgport, 0, bridge_ifdetach_dispatch);
1785 lmsg = &nmsg.nm_lmsg;
1786 lmsg->u.ms_resultp = ifp;
1788 lwkt_domsg(BRIDGE_CFGPORT, lmsg, 0);
1794 * Initialize a bridge interface.
1797 bridge_init(void *xsc)
1799 bridge_control(xsc, SIOCSIFFLAGS, bridge_ioctl_init, NULL);
1805 * Stop the bridge interface.
1808 bridge_stop(struct ifnet *ifp)
1810 bridge_control(ifp->if_softc, SIOCSIFFLAGS, bridge_ioctl_stop, NULL);
1816 * Enqueue a packet on a bridge member interface.
1820 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1822 struct netmsg_packet *nmp;
1825 nmp = &m->m_hdr.mh_netmsg;
1826 netmsg_init(&nmp->nm_netmsg, &netisr_apanic_rport, 0,
1827 bridge_enqueue_handler);
1829 nmp->nm_netmsg.nm_lmsg.u.ms_resultp = dst_ifp;
1831 if (curthread->td_flags & TDF_NETWORK)
1832 port = &curthread->td_msgport;
1834 port = cpu_portfn(mycpuid);
1835 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
1841 * Send output from a bridge member interface. This
1842 * performs the bridging function for locally originated
1845 * The mbuf has the Ethernet header already attached. We must
1846 * enqueue or free the mbuf before returning.
1849 bridge_output(struct ifnet *ifp, struct mbuf *m)
1851 struct bridge_softc *sc = ifp->if_bridge;
1852 struct ether_header *eh;
1853 struct ifnet *dst_if, *bifp;
1855 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
1858 * Make sure that we are still a member of a bridge interface.
1866 if (m->m_len < ETHER_HDR_LEN) {
1867 m = m_pullup(m, ETHER_HDR_LEN);
1871 eh = mtod(m, struct ether_header *);
1874 * If bridge is down, but the original output interface is up,
1875 * go ahead and send out that interface. Otherwise, the packet
1878 if ((bifp->if_flags & IFF_RUNNING) == 0) {
1884 * If the packet is a multicast, or we don't know a better way to
1885 * get there, send to all interfaces.
1887 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1890 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1891 if (dst_if == NULL) {
1892 struct bridge_iflist *bif, *nbif;
1899 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid],
1901 dst_if = bif->bif_ifp;
1902 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1906 * If this is not the original output interface,
1907 * and the interface is participating in spanning
1908 * tree, make sure the port is in a state that
1909 * allows forwarding.
1911 if (dst_if != ifp &&
1912 (bif->bif_flags & IFBIF_STP) != 0) {
1913 switch (bif->bif_state) {
1914 case BSTP_IFSTATE_BLOCKING:
1915 case BSTP_IFSTATE_LISTENING:
1916 case BSTP_IFSTATE_DISABLED:
1921 if (LIST_NEXT(bif, bif_next) == NULL) {
1925 mc = m_copypacket(m, MB_DONTWAIT);
1931 bridge_handoff(dst_if, mc);
1933 if (nbif != NULL && !nbif->bif_onlist) {
1934 KKASSERT(bif->bif_onlist);
1935 nbif = LIST_NEXT(bif, bif_next);
1945 * XXX Spanning tree consideration here?
1949 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1952 bridge_handoff(dst_if, m);
1959 * Start output on a bridge.
1963 bridge_start(struct ifnet *ifp)
1965 struct bridge_softc *sc = ifp->if_softc;
1967 ASSERT_SERIALIZED(ifp->if_serializer);
1969 ifp->if_flags |= IFF_OACTIVE;
1971 struct ifnet *dst_if = NULL;
1972 struct ether_header *eh;
1975 m = ifq_dequeue(&ifp->if_snd, NULL);
1979 if (m->m_len < sizeof(*eh)) {
1980 m = m_pullup(m, sizeof(*eh));
1986 eh = mtod(m, struct ether_header *);
1991 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1992 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1995 bridge_start_bcast(sc, m);
1997 bridge_enqueue(dst_if, m);
1999 ifp->if_flags &= ~IFF_OACTIVE;
2005 * The forwarding function of the bridge.
2008 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
2010 struct bridge_iflist *bif;
2011 struct ifnet *src_if, *dst_if, *ifp;
2012 struct ether_header *eh;
2014 src_if = m->m_pkthdr.rcvif;
2017 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
2020 ifp->if_ibytes += m->m_pkthdr.len;
2023 * Look up the bridge_iflist.
2025 bif = bridge_lookup_member_if(sc, src_if);
2027 /* Interface is not a bridge member (anymore?) */
2032 if (bif->bif_flags & IFBIF_STP) {
2033 switch (bif->bif_state) {
2034 case BSTP_IFSTATE_BLOCKING:
2035 case BSTP_IFSTATE_LISTENING:
2036 case BSTP_IFSTATE_DISABLED:
2042 eh = mtod(m, struct ether_header *);
2045 * If the interface is learning, and the source
2046 * address is valid and not multicast, record
2049 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
2050 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
2051 (eh->ether_shost[0] == 0 &&
2052 eh->ether_shost[1] == 0 &&
2053 eh->ether_shost[2] == 0 &&
2054 eh->ether_shost[3] == 0 &&
2055 eh->ether_shost[4] == 0 &&
2056 eh->ether_shost[5] == 0) == 0)
2057 bridge_rtupdate(sc, eh->ether_shost, src_if, IFBAF_DYNAMIC);
2059 if ((bif->bif_flags & IFBIF_STP) != 0 &&
2060 bif->bif_state == BSTP_IFSTATE_LEARNING) {
2066 * At this point, the port either doesn't participate
2067 * in spanning tree or it is in the forwarding state.
2071 * If the packet is unicast, destined for someone on
2072 * "this" side of the bridge, drop it.
2074 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2075 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2076 if (src_if == dst_if) {
2081 /* ...forward it to all interfaces. */
2086 if (dst_if == NULL) {
2087 bridge_broadcast(sc, src_if, m);
2092 * At this point, we're dealing with a unicast frame
2093 * going to a different interface.
2095 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
2099 bif = bridge_lookup_member_if(sc, dst_if);
2101 /* Not a member of the bridge (anymore?) */
2106 if (bif->bif_flags & IFBIF_STP) {
2107 switch (bif->bif_state) {
2108 case BSTP_IFSTATE_DISABLED:
2109 case BSTP_IFSTATE_BLOCKING:
2115 if (inet_pfil_hook.ph_hashooks > 0
2117 || inet6_pfil_hook.ph_hashooks > 0
2120 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2125 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2130 bridge_handoff(dst_if, m);
2136 * Receive input from a member interface. Queue the packet for
2137 * bridging if it is not for us.
2139 static struct mbuf *
2140 bridge_input(struct ifnet *ifp, struct mbuf *m)
2142 struct bridge_softc *sc = ifp->if_bridge;
2143 struct bridge_iflist *bif;
2144 struct ifnet *bifp, *new_ifp;
2145 struct ether_header *eh;
2146 struct mbuf *mc, *mc2;
2148 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
2151 * Make sure that we are still a member of a bridge interface.
2159 if ((bifp->if_flags & IFF_RUNNING) == 0)
2163 * Implement support for bridge monitoring. If this flag has been
2164 * set on this interface, discard the packet once we push it through
2165 * the bpf(4) machinery, but before we do, increment various counters
2166 * associated with this bridge.
2168 if (bifp->if_flags & IFF_MONITOR) {
2169 /* Change input interface to this bridge */
2170 m->m_pkthdr.rcvif = bifp;
2174 /* Update bridge's ifnet statistics */
2175 bifp->if_ipackets++;
2176 bifp->if_ibytes += m->m_pkthdr.len;
2177 if (m->m_flags & (M_MCAST | M_BCAST))
2185 eh = mtod(m, struct ether_header *);
2187 m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
2189 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
2191 * If the packet is for us, set the packets source as the
2192 * bridge, and return the packet back to ifnet.if_input for
2195 KASSERT(bifp->if_bridge == NULL,
2196 ("loop created in bridge_input"));
2202 * Tap all packets arriving on the bridge, no matter if
2203 * they are local destinations or not. In is in.
2207 bif = bridge_lookup_member_if(sc, ifp);
2214 if (m->m_flags & (M_BCAST | M_MCAST)) {
2215 /* Tap off 802.1D packets; they do not get forwarded. */
2216 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2217 ETHER_ADDR_LEN) == 0) {
2218 lwkt_serialize_enter(bifp->if_serializer);
2219 bstp_input(sc, bif, m);
2220 lwkt_serialize_exit(bifp->if_serializer);
2222 /* m is freed by bstp_input */
2227 if (bif->bif_flags & IFBIF_STP) {
2228 switch (bif->bif_state) {
2229 case BSTP_IFSTATE_BLOCKING:
2230 case BSTP_IFSTATE_LISTENING:
2231 case BSTP_IFSTATE_DISABLED:
2237 * Make a deep copy of the packet and enqueue the copy
2238 * for bridge processing; return the original packet for
2241 mc = m_dup(m, MB_DONTWAIT);
2245 bridge_forward(sc, mc);
2248 * Reinject the mbuf as arriving on the bridge so we have a
2249 * chance at claiming multicast packets. We can not loop back
2250 * here from ether_input as a bridge is never a member of a
2253 KASSERT(bifp->if_bridge == NULL,
2254 ("loop created in bridge_input"));
2255 mc2 = m_dup(m, MB_DONTWAIT);
2258 /* Keep the layer3 header aligned */
2259 int i = min(mc2->m_pkthdr.len, max_protohdr);
2260 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2265 * Don't tap to bpf(4) again; we have
2266 * already done the tapping.
2268 ether_reinput_oncpu(bifp, mc2, 0);
2271 /* Return the original packet for local processing. */
2275 if (bif->bif_flags & IFBIF_STP) {
2276 switch (bif->bif_state) {
2277 case BSTP_IFSTATE_BLOCKING:
2278 case BSTP_IFSTATE_LISTENING:
2279 case BSTP_IFSTATE_DISABLED:
2285 * Unicast. Make sure it's not for us.
2287 * This loop is MPSAFE; the only blocking operation (bridge_rtupdate)
2288 * is followed by breaking out of the loop.
2290 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2291 if (bif->bif_ifp->if_type != IFT_ETHER)
2294 /* It is destined for us. */
2295 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
2296 ETHER_ADDR_LEN) == 0) {
2297 if (bif->bif_ifp != ifp) {
2298 /* XXX loop prevention */
2299 m->m_flags |= M_PROTO1;
2300 new_ifp = bif->bif_ifp;
2302 if (bif->bif_flags & IFBIF_LEARNING) {
2303 bridge_rtupdate(sc, eh->ether_shost,
2304 ifp, IFBAF_DYNAMIC);
2309 /* We just received a packet that we sent out. */
2310 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
2311 ETHER_ADDR_LEN) == 0) {
2318 /* Perform the bridge forwarding function. */
2319 bridge_forward(sc, m);
2322 if (new_ifp != NULL) {
2323 ether_reinput_oncpu(new_ifp, m, 1);
2330 * bridge_start_bcast:
2332 * Broadcast the packet sent from bridge to all member
2334 * This is a simplified version of bridge_broadcast(), however,
2335 * this function expects caller to hold bridge's serializer.
2338 bridge_start_bcast(struct bridge_softc *sc, struct mbuf *m)
2340 struct bridge_iflist *bif;
2342 struct ifnet *dst_if, *bifp;
2346 ASSERT_SERIALIZED(bifp->if_serializer);
2349 * Following loop is MPSAFE; nothing is blocking
2352 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2353 dst_if = bif->bif_ifp;
2355 if (bif->bif_flags & IFBIF_STP) {
2356 switch (bif->bif_state) {
2357 case BSTP_IFSTATE_BLOCKING:
2358 case BSTP_IFSTATE_DISABLED:
2363 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2364 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2367 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2370 if (LIST_NEXT(bif, bif_next) == NULL) {
2374 mc = m_copypacket(m, MB_DONTWAIT);
2380 bridge_enqueue(dst_if, mc);
2389 * Send a frame to all interfaces that are members of
2390 * the bridge, except for the one on which the packet
2394 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2397 struct bridge_iflist *bif, *nbif;
2399 struct ifnet *dst_if, *bifp;
2403 ASSERT_NOT_SERIALIZED(bifp->if_serializer);
2405 if (inet_pfil_hook.ph_hashooks > 0
2407 || inet6_pfil_hook.ph_hashooks > 0
2410 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
2415 /* Filter on the bridge interface before broadcasting */
2416 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
2422 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
2423 dst_if = bif->bif_ifp;
2424 if (dst_if == src_if)
2427 if (bif->bif_flags & IFBIF_STP) {
2428 switch (bif->bif_state) {
2429 case BSTP_IFSTATE_BLOCKING:
2430 case BSTP_IFSTATE_DISABLED:
2435 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2436 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2439 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2442 if (LIST_NEXT(bif, bif_next) == NULL) {
2446 mc = m_copypacket(m, MB_DONTWAIT);
2448 sc->sc_ifp->if_oerrors++;
2454 * Filter on the output interface. Pass a NULL bridge
2455 * interface pointer so we do not redundantly filter on
2456 * the bridge for each interface we broadcast on.
2458 if (inet_pfil_hook.ph_hashooks > 0
2460 || inet6_pfil_hook.ph_hashooks > 0
2463 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2468 bridge_handoff(dst_if, mc);
2470 if (nbif != NULL && !nbif->bif_onlist) {
2471 KKASSERT(bif->bif_onlist);
2472 nbif = LIST_NEXT(bif, bif_next);
2482 * Duplicate a packet out one or more interfaces that are in span mode,
2483 * the original mbuf is unmodified.
2486 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2488 struct bridge_iflist *bif;
2489 struct ifnet *dst_if, *bifp;
2493 lwkt_serialize_enter(bifp->if_serializer);
2495 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2496 dst_if = bif->bif_ifp;
2498 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2501 mc = m_copypacket(m, MB_DONTWAIT);
2503 sc->sc_ifp->if_oerrors++;
2506 bridge_enqueue(dst_if, mc);
2509 lwkt_serialize_exit(bifp->if_serializer);
2513 bridge_rtmsg_sync_handler(struct netmsg *nmsg)
2515 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2519 bridge_rtmsg_sync(struct bridge_softc *sc)
2523 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2525 netmsg_init(&nmsg, &curthread->td_msgport, 0,
2526 bridge_rtmsg_sync_handler);
2527 ifnet_domsg(&nmsg.nm_lmsg, 0);
2530 static __inline void
2531 bridge_rtinfo_update(struct bridge_rtinfo *bri, struct ifnet *dst_if,
2532 int setflags, uint8_t flags, uint32_t timeo)
2534 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2535 bri->bri_ifp != dst_if)
2536 bri->bri_ifp = dst_if;
2537 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2538 bri->bri_expire != time_second + timeo)
2539 bri->bri_expire = time_second + timeo;
2541 bri->bri_flags = flags;
2545 bridge_rtinstall_oncpu(struct bridge_softc *sc, const uint8_t *dst,
2546 struct ifnet *dst_if, int setflags, uint8_t flags,
2547 struct bridge_rtinfo **bri0)
2549 struct bridge_rtnode *brt;
2550 struct bridge_rtinfo *bri;
2553 brt = bridge_rtnode_lookup(sc, dst);
2556 * rtnode for 'dst' already exists. We inform the
2557 * caller about this by leaving bri0 as NULL. The
2558 * caller will terminate the intallation upon getting
2559 * NULL bri0. However, we still need to update the
2562 KKASSERT(*bri0 == NULL);
2565 bridge_rtinfo_update(brt->brt_info, dst_if, setflags,
2566 flags, sc->sc_brttimeout);
2571 * We only need to check brtcnt on CPU0, since if limit
2572 * is to be exceeded, ENOSPC is returned. Caller knows
2573 * this and will terminate the installation.
2575 if (sc->sc_brtcnt >= sc->sc_brtmax)
2578 KKASSERT(*bri0 == NULL);
2579 bri = kmalloc(sizeof(struct bridge_rtinfo), M_DEVBUF,
2584 bri->bri_flags = IFBAF_DYNAMIC;
2585 bridge_rtinfo_update(bri, dst_if, setflags, flags,
2589 KKASSERT(bri != NULL);
2592 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2594 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2595 brt->brt_info = bri;
2597 bridge_rtnode_insert(sc, brt);
2602 bridge_rtinstall_handler(struct netmsg *nmsg)
2604 struct netmsg_brsaddr *brmsg = (struct netmsg_brsaddr *)nmsg;
2607 error = bridge_rtinstall_oncpu(brmsg->br_softc,
2608 brmsg->br_dst, brmsg->br_dst_if,
2609 brmsg->br_setflags, brmsg->br_flags,
2612 KKASSERT(mycpuid == 0 && brmsg->br_rtinfo == NULL);
2613 lwkt_replymsg(&nmsg->nm_lmsg, error);
2615 } else if (brmsg->br_rtinfo == NULL) {
2616 /* rtnode already exists for 'dst' */
2617 KKASSERT(mycpuid == 0);
2618 lwkt_replymsg(&nmsg->nm_lmsg, 0);
2621 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2627 * Add/Update a bridge routing entry.
2630 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2631 struct ifnet *dst_if, uint8_t flags)
2633 struct bridge_rtnode *brt;
2636 * A route for this destination might already exist. If so,
2637 * update it, otherwise create a new one.
2639 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2640 struct netmsg_brsaddr *brmsg;
2642 if (sc->sc_brtcnt >= sc->sc_brtmax)
2645 brmsg = kmalloc(sizeof(*brmsg), M_LWKTMSG, M_WAITOK | M_NULLOK);
2649 netmsg_init(&brmsg->br_nmsg, &netisr_afree_rport, 0,
2650 bridge_rtinstall_handler);
2651 memcpy(brmsg->br_dst, dst, ETHER_ADDR_LEN);
2652 brmsg->br_dst_if = dst_if;
2653 brmsg->br_flags = flags;
2654 brmsg->br_setflags = 0;
2655 brmsg->br_softc = sc;
2656 brmsg->br_rtinfo = NULL;
2658 ifnet_sendmsg(&brmsg->br_nmsg.nm_lmsg, 0);
2661 bridge_rtinfo_update(brt->brt_info, dst_if, 0, flags,
2667 bridge_rtsaddr(struct bridge_softc *sc, const uint8_t *dst,
2668 struct ifnet *dst_if, uint8_t flags)
2670 struct netmsg_brsaddr brmsg;
2672 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2674 netmsg_init(&brmsg.br_nmsg, &curthread->td_msgport, 0,
2675 bridge_rtinstall_handler);
2676 memcpy(brmsg.br_dst, dst, ETHER_ADDR_LEN);
2677 brmsg.br_dst_if = dst_if;
2678 brmsg.br_flags = flags;
2679 brmsg.br_setflags = 1;
2680 brmsg.br_softc = sc;
2681 brmsg.br_rtinfo = NULL;
2683 return ifnet_domsg(&brmsg.br_nmsg.nm_lmsg, 0);
2689 * Lookup the destination interface for an address.
2691 static struct ifnet *
2692 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2694 struct bridge_rtnode *brt;
2696 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2698 return brt->brt_info->bri_ifp;
2702 bridge_rtreap_handler(struct netmsg *nmsg)
2704 struct bridge_softc *sc = nmsg->nm_lmsg.u.ms_resultp;
2705 struct bridge_rtnode *brt, *nbrt;
2707 LIST_FOREACH_MUTABLE(brt, &sc->sc_rtlists[mycpuid], brt_list, nbrt) {
2708 if (brt->brt_info->bri_dead)
2709 bridge_rtnode_destroy(sc, brt);
2711 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2715 bridge_rtreap(struct bridge_softc *sc)
2719 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2721 netmsg_init(&nmsg, &curthread->td_msgport, 0, bridge_rtreap_handler);
2722 nmsg.nm_lmsg.u.ms_resultp = sc;
2724 ifnet_domsg(&nmsg.nm_lmsg, 0);
2730 * Trim the routine table so that we have a number
2731 * of routing entries less than or equal to the
2735 bridge_rttrim(struct bridge_softc *sc)
2737 struct bridge_rtnode *brt;
2740 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2742 /* Make sure we actually need to do this. */
2743 if (sc->sc_brtcnt <= sc->sc_brtmax)
2747 * Find out how many rtnodes are dead
2749 dead = bridge_rtage_finddead(sc);
2750 KKASSERT(dead <= sc->sc_brtcnt);
2752 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
2753 /* Enough dead rtnodes are found */
2759 * Kill some dynamic rtnodes to meet the brtmax
2761 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2762 struct bridge_rtinfo *bri = brt->brt_info;
2764 if (bri->bri_dead) {
2766 * We have counted this rtnode in
2767 * bridge_rtage_finddead()
2772 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2775 KKASSERT(dead <= sc->sc_brtcnt);
2777 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
2778 /* Enough rtnodes are collected */
2790 * Aging timer for the bridge.
2793 bridge_timer(void *arg)
2795 struct bridge_softc *sc = arg;
2796 struct lwkt_msg *lmsg;
2798 KKASSERT(mycpuid == BRIDGE_CFGCPU);
2802 if (callout_pending(&sc->sc_brcallout) ||
2803 !callout_active(&sc->sc_brcallout)) {
2807 callout_deactivate(&sc->sc_brcallout);
2809 lmsg = &sc->sc_brtimemsg.nm_lmsg;
2810 KKASSERT(lmsg->ms_flags & MSGF_DONE);
2811 lwkt_sendmsg(BRIDGE_CFGPORT, lmsg);
2817 bridge_timer_handler(struct netmsg *nmsg)
2819 struct bridge_softc *sc = nmsg->nm_lmsg.u.ms_resultp;
2821 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
2825 lwkt_replymsg(&nmsg->nm_lmsg, 0);
2829 if (sc->sc_ifp->if_flags & IFF_RUNNING) {
2830 callout_reset(&sc->sc_brcallout,
2831 bridge_rtable_prune_period * hz, bridge_timer, sc);
2836 bridge_rtage_finddead(struct bridge_softc *sc)
2838 struct bridge_rtnode *brt;
2841 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2842 struct bridge_rtinfo *bri = brt->brt_info;
2844 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2845 time_second >= bri->bri_expire) {
2848 KKASSERT(dead <= sc->sc_brtcnt);
2857 * Perform an aging cycle.
2860 bridge_rtage(struct bridge_softc *sc)
2862 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2864 if (bridge_rtage_finddead(sc))
2871 * Remove all dynamic addresses from the bridge.
2874 bridge_rtflush(struct bridge_softc *sc, int full)
2876 struct bridge_rtnode *brt;
2879 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2882 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2883 struct bridge_rtinfo *bri = brt->brt_info;
2886 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2898 * Remove an address from the table.
2901 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2903 struct bridge_rtnode *brt;
2905 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2907 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2910 /* TODO: add a cheaper delete operation */
2911 brt->brt_info->bri_dead = 1;
2919 * Delete routes to a speicifc member interface.
2922 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2924 struct bridge_rtnode *brt;
2927 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2930 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2931 struct bridge_rtinfo *bri = brt->brt_info;
2933 if (bri->bri_ifp == ifp &&
2935 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) {
2945 * bridge_rtable_init:
2947 * Initialize the route table for this bridge.
2950 bridge_rtable_init(struct bridge_softc *sc)
2955 * Initialize per-cpu hash tables
2957 sc->sc_rthashs = kmalloc(sizeof(*sc->sc_rthashs) * ncpus,
2958 M_DEVBUF, M_WAITOK);
2959 for (cpu = 0; cpu < ncpus; ++cpu) {
2962 sc->sc_rthashs[cpu] =
2963 kmalloc(sizeof(struct bridge_rtnode_head) * BRIDGE_RTHASH_SIZE,
2964 M_DEVBUF, M_WAITOK);
2966 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2967 LIST_INIT(&sc->sc_rthashs[cpu][i]);
2969 sc->sc_rthash_key = karc4random();
2972 * Initialize per-cpu lists
2974 sc->sc_rtlists = kmalloc(sizeof(struct bridge_rtnode_head) * ncpus,
2975 M_DEVBUF, M_WAITOK);
2976 for (cpu = 0; cpu < ncpus; ++cpu)
2977 LIST_INIT(&sc->sc_rtlists[cpu]);
2981 * bridge_rtable_fini:
2983 * Deconstruct the route table for this bridge.
2986 bridge_rtable_fini(struct bridge_softc *sc)
2991 * Free per-cpu hash tables
2993 for (cpu = 0; cpu < ncpus; ++cpu)
2994 kfree(sc->sc_rthashs[cpu], M_DEVBUF);
2995 kfree(sc->sc_rthashs, M_DEVBUF);
2998 * Free per-cpu lists
3000 kfree(sc->sc_rtlists, M_DEVBUF);
3004 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
3005 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
3007 #define mix(a, b, c) \
3009 a -= b; a -= c; a ^= (c >> 13); \
3010 b -= c; b -= a; b ^= (a << 8); \
3011 c -= a; c -= b; c ^= (b >> 13); \
3012 a -= b; a -= c; a ^= (c >> 12); \
3013 b -= c; b -= a; b ^= (a << 16); \
3014 c -= a; c -= b; c ^= (b >> 5); \
3015 a -= b; a -= c; a ^= (c >> 3); \
3016 b -= c; b -= a; b ^= (a << 10); \
3017 c -= a; c -= b; c ^= (b >> 15); \
3018 } while (/*CONSTCOND*/0)
3020 static __inline uint32_t
3021 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
3023 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
3034 return (c & BRIDGE_RTHASH_MASK);
3040 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
3044 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
3045 d = ((int)a[i]) - ((int)b[i]);
3052 * bridge_rtnode_lookup:
3054 * Look up a bridge route node for the specified destination.
3056 static struct bridge_rtnode *
3057 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
3059 struct bridge_rtnode *brt;
3063 hash = bridge_rthash(sc, addr);
3064 LIST_FOREACH(brt, &sc->sc_rthashs[mycpuid][hash], brt_hash) {
3065 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
3076 * bridge_rtnode_insert:
3078 * Insert the specified bridge node into the route table.
3079 * Caller has to make sure that rtnode does not exist.
3082 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
3084 struct bridge_rtnode *lbrt;
3088 hash = bridge_rthash(sc, brt->brt_addr);
3090 lbrt = LIST_FIRST(&sc->sc_rthashs[mycpuid][hash]);
3092 LIST_INSERT_HEAD(&sc->sc_rthashs[mycpuid][hash], brt, brt_hash);
3097 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
3098 KASSERT(dir != 0, ("rtnode already exist\n"));
3101 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
3104 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
3105 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
3108 lbrt = LIST_NEXT(lbrt, brt_hash);
3109 } while (lbrt != NULL);
3111 panic("no suitable position found for rtnode\n");
3113 LIST_INSERT_HEAD(&sc->sc_rtlists[mycpuid], brt, brt_list);
3116 * Update the brtcnt.
3117 * We only need to do it once and we do it on CPU0.
3124 * bridge_rtnode_destroy:
3126 * Destroy a bridge rtnode.
3129 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3131 LIST_REMOVE(brt, brt_hash);
3132 LIST_REMOVE(brt, brt_list);
3134 if (mycpuid + 1 == ncpus) {
3135 /* Free rtinfo associated with rtnode on the last cpu */
3136 kfree(brt->brt_info, M_DEVBUF);
3138 kfree(brt, M_DEVBUF);
3141 /* Update brtcnt only on CPU0 */
3147 bridge_post_pfil(struct mbuf *m)
3149 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED)
3153 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED)
3160 * Send bridge packets through pfil if they are one of the types pfil can deal
3161 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
3162 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3166 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3168 int snap, error, i, hlen;
3169 struct ether_header *eh1, eh2;
3172 u_int16_t ether_type;
3175 error = -1; /* Default error if not error == 0 */
3177 if (pfil_bridge == 0 && pfil_member == 0)
3178 return (0); /* filtering is disabled */
3180 i = min((*mp)->m_pkthdr.len, max_protohdr);
3181 if ((*mp)->m_len < i) {
3182 *mp = m_pullup(*mp, i);
3184 kprintf("%s: m_pullup failed\n", __func__);
3189 eh1 = mtod(*mp, struct ether_header *);
3190 ether_type = ntohs(eh1->ether_type);
3193 * Check for SNAP/LLC.
3195 if (ether_type < ETHERMTU) {
3196 struct llc *llc2 = (struct llc *)(eh1 + 1);
3198 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3199 llc2->llc_dsap == LLC_SNAP_LSAP &&
3200 llc2->llc_ssap == LLC_SNAP_LSAP &&
3201 llc2->llc_control == LLC_UI) {
3202 ether_type = htons(llc2->llc_un.type_snap.ether_type);
3208 * If we're trying to filter bridge traffic, don't look at anything
3209 * other than IP and ARP traffic. If the filter doesn't understand
3210 * IPv6, don't allow IPv6 through the bridge either. This is lame
3211 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3212 * but of course we don't have an AppleTalk filter to begin with.
3213 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3216 switch (ether_type) {
3218 case ETHERTYPE_REVARP:
3219 return (0); /* Automatically pass */
3223 case ETHERTYPE_IPV6:
3229 * Check to see if the user wants to pass non-ip
3230 * packets, these will not be checked by pfil(9)
3231 * and passed unconditionally so the default is to drop.
3237 /* Strip off the Ethernet header and keep a copy. */
3238 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3239 m_adj(*mp, ETHER_HDR_LEN);
3241 /* Strip off snap header, if present */
3243 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3244 m_adj(*mp, sizeof(struct llc));
3248 * Check the IP header for alignment and errors
3250 if (dir == PFIL_IN) {
3251 switch (ether_type) {
3253 error = bridge_ip_checkbasic(mp);
3256 case ETHERTYPE_IPV6:
3257 error = bridge_ip6_checkbasic(mp);
3270 * Run the packet through pfil
3272 switch (ether_type) {
3275 * before calling the firewall, swap fields the same as
3276 * IP does. here we assume the header is contiguous
3278 ip = mtod(*mp, struct ip *);
3280 ip->ip_len = ntohs(ip->ip_len);
3281 ip->ip_off = ntohs(ip->ip_off);
3284 * Run pfil on the member interface and the bridge, both can
3285 * be skipped by clearing pfil_member or pfil_bridge.
3288 * in_if -> bridge_if -> out_if
3290 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) {
3291 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3292 if (*mp == NULL || error != 0) /* filter may consume */
3294 error = bridge_post_pfil(*mp);
3299 if (pfil_member && ifp != NULL) {
3300 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp, dir);
3301 if (*mp == NULL || error != 0) /* filter may consume */
3303 error = bridge_post_pfil(*mp);
3308 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) {
3309 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3310 if (*mp == NULL || error != 0) /* filter may consume */
3312 error = bridge_post_pfil(*mp);
3317 /* check if we need to fragment the packet */
3318 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
3319 i = (*mp)->m_pkthdr.len;
3320 if (i > ifp->if_mtu) {
3321 error = bridge_fragment(ifp, *mp, &eh2, snap,
3327 /* Recalculate the ip checksum and restore byte ordering */
3328 ip = mtod(*mp, struct ip *);
3329 hlen = ip->ip_hl << 2;
3330 if (hlen < sizeof(struct ip))
3332 if (hlen > (*mp)->m_len) {
3333 if ((*mp = m_pullup(*mp, hlen)) == 0)
3335 ip = mtod(*mp, struct ip *);
3339 ip->ip_len = htons(ip->ip_len);
3340 ip->ip_off = htons(ip->ip_off);
3342 if (hlen == sizeof(struct ip))
3343 ip->ip_sum = in_cksum_hdr(ip);
3345 ip->ip_sum = in_cksum(*mp, hlen);
3349 case ETHERTYPE_IPV6:
3350 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3351 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3354 if (*mp == NULL || error != 0) /* filter may consume */
3357 if (pfil_member && ifp != NULL)
3358 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
3361 if (*mp == NULL || error != 0) /* filter may consume */
3364 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3365 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3382 * Finally, put everything back the way it was and return
3385 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
3388 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
3391 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
3394 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3405 * Perform basic checks on header size since
3406 * pfil assumes ip_input has already processed
3407 * it for it. Cut-and-pasted from ip_input.c.
3408 * Given how simple the IPv6 version is,
3409 * does the IPv4 version really need to be
3412 * XXX Should we update ipstat here, or not?
3413 * XXX Right now we update ipstat but not
3417 bridge_ip_checkbasic(struct mbuf **mp)
3419 struct mbuf *m = *mp;
3427 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3428 if ((m = m_copyup(m, sizeof(struct ip),
3429 (max_linkhdr + 3) & ~3)) == NULL) {
3430 /* XXXJRT new stat, please */
3431 ipstat.ips_toosmall++;
3436 #ifndef __predict_false
3437 #define __predict_false(x) x
3439 if (__predict_false(m->m_len < sizeof (struct ip))) {
3440 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3441 ipstat.ips_toosmall++;
3445 ip = mtod(m, struct ip *);
3446 if (ip == NULL) goto bad;
3448 if (ip->ip_v != IPVERSION) {
3449 ipstat.ips_badvers++;
3452 hlen = ip->ip_hl << 2;
3453 if (hlen < sizeof(struct ip)) { /* minimum header length */
3454 ipstat.ips_badhlen++;
3457 if (hlen > m->m_len) {
3458 if ((m = m_pullup(m, hlen)) == 0) {
3459 ipstat.ips_badhlen++;
3462 ip = mtod(m, struct ip *);
3463 if (ip == NULL) goto bad;
3466 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3467 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3469 if (hlen == sizeof(struct ip)) {
3470 sum = in_cksum_hdr(ip);
3472 sum = in_cksum(m, hlen);
3476 ipstat.ips_badsum++;
3480 /* Retrieve the packet length. */
3481 len = ntohs(ip->ip_len);
3484 * Check for additional length bogosity
3487 ipstat.ips_badlen++;
3492 * Check that the amount of data in the buffers
3493 * is as at least much as the IP header would have us expect.
3494 * Drop packet if shorter than we expect.
3496 if (m->m_pkthdr.len < len) {
3497 ipstat.ips_tooshort++;
3501 /* Checks out, proceed */
3512 * Same as above, but for IPv6.
3513 * Cut-and-pasted from ip6_input.c.
3514 * XXX Should we update ip6stat, or not?
3517 bridge_ip6_checkbasic(struct mbuf **mp)
3519 struct mbuf *m = *mp;
3520 struct ip6_hdr *ip6;
3523 * If the IPv6 header is not aligned, slurp it up into a new
3524 * mbuf with space for link headers, in the event we forward
3525 * it. Otherwise, if it is aligned, make sure the entire base
3526 * IPv6 header is in the first mbuf of the chain.
3529 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3530 struct ifnet *inifp = m->m_pkthdr.rcvif;
3531 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3532 (max_linkhdr + 3) & ~3)) == NULL) {
3533 /* XXXJRT new stat, please */
3534 ip6stat.ip6s_toosmall++;
3535 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3540 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3541 struct ifnet *inifp = m->m_pkthdr.rcvif;
3542 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3543 ip6stat.ip6s_toosmall++;
3544 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3549 ip6 = mtod(m, struct ip6_hdr *);
3551 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3552 ip6stat.ip6s_badvers++;
3553 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3557 /* Checks out, proceed */
3570 * Return a fragmented mbuf chain.
3573 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
3574 int snap, struct llc *llc)
3580 if (m->m_len < sizeof(struct ip) &&
3581 (m = m_pullup(m, sizeof(struct ip))) == NULL)
3583 ip = mtod(m, struct ip *);
3585 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
3590 /* walk the chain and re-add the Ethernet header */
3591 for (m0 = m; m0; m0 = m0->m_nextpkt) {
3594 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
3599 bcopy(llc, mtod(m0, caddr_t),
3600 sizeof(struct llc));
3602 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
3607 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
3613 ipstat.ips_fragmented++;
3624 bridge_enqueue_handler(struct netmsg *nmsg)
3626 struct netmsg_packet *nmp;
3627 struct ifnet *dst_ifp;
3630 nmp = (struct netmsg_packet *)nmsg;
3632 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
3634 bridge_handoff(dst_ifp, m);
3638 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m)
3642 /* We may be sending a fragment so traverse the mbuf */
3644 struct altq_pktattr pktattr;
3647 m->m_nextpkt = NULL;
3649 if (ifq_is_enabled(&dst_ifp->if_snd))
3650 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
3652 ifq_dispatch(dst_ifp, m, &pktattr);
3657 bridge_control_dispatch(struct netmsg *nmsg)
3659 struct netmsg_brctl *bc_msg = (struct netmsg_brctl *)nmsg;
3660 struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
3663 lwkt_serialize_enter(bifp->if_serializer);
3664 error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
3665 lwkt_serialize_exit(bifp->if_serializer);
3667 lwkt_replymsg(&nmsg->nm_lmsg, error);
3671 bridge_control(struct bridge_softc *sc, u_long cmd,
3672 bridge_ctl_t bc_func, void *bc_arg)
3674 struct ifnet *bifp = sc->sc_ifp;
3675 struct netmsg_brctl bc_msg;
3676 struct netmsg *nmsg;
3679 ASSERT_SERIALIZED(bifp->if_serializer);
3681 bzero(&bc_msg, sizeof(bc_msg));
3682 nmsg = &bc_msg.bc_nmsg;
3684 netmsg_init(nmsg, &curthread->td_msgport, 0, bridge_control_dispatch);
3685 bc_msg.bc_func = bc_func;
3687 bc_msg.bc_arg = bc_arg;
3689 lwkt_serialize_exit(bifp->if_serializer);
3690 error = lwkt_domsg(BRIDGE_CFGPORT, &nmsg->nm_lmsg, 0);
3691 lwkt_serialize_enter(bifp->if_serializer);
3696 bridge_add_bif_handler(struct netmsg *nmsg)
3698 struct netmsg_braddbif *amsg = (struct netmsg_braddbif *)nmsg;
3699 struct bridge_softc *sc;
3700 struct bridge_iflist *bif;
3702 sc = amsg->br_softc;
3704 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
3705 bif->bif_ifp = amsg->br_bif_ifp;
3706 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
3707 bif->bif_onlist = 1;
3708 bif->bif_info = amsg->br_bif_info;
3710 LIST_INSERT_HEAD(&sc->sc_iflists[mycpuid], bif, bif_next);
3712 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3716 bridge_add_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3719 struct netmsg_braddbif amsg;
3721 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
3723 netmsg_init(&amsg.br_nmsg, &curthread->td_msgport, 0,
3724 bridge_add_bif_handler);
3726 amsg.br_bif_info = bif_info;
3727 amsg.br_bif_ifp = ifp;
3729 ifnet_domsg(&amsg.br_nmsg.nm_lmsg, 0);
3733 bridge_del_bif_handler(struct netmsg *nmsg)
3735 struct netmsg_brdelbif *dmsg = (struct netmsg_brdelbif *)nmsg;
3736 struct bridge_softc *sc;
3737 struct bridge_iflist *bif;
3739 sc = dmsg->br_softc;
3742 * Locate the bif associated with the br_bif_info
3743 * on the current CPU
3745 bif = bridge_lookup_member_ifinfo(sc, dmsg->br_bif_info);
3746 KKASSERT(bif != NULL && bif->bif_onlist);
3748 /* Remove the bif from the current CPU's iflist */
3749 bif->bif_onlist = 0;
3750 LIST_REMOVE(bif, bif_next);
3752 /* Save the removed bif for later freeing */
3753 LIST_INSERT_HEAD(dmsg->br_bif_list, bif, bif_next);
3755 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3759 bridge_del_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3760 struct bridge_iflist_head *saved_bifs)
3762 struct netmsg_brdelbif dmsg;
3764 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
3766 netmsg_init(&dmsg.br_nmsg, &curthread->td_msgport, 0,
3767 bridge_del_bif_handler);
3769 dmsg.br_bif_info = bif_info;
3770 dmsg.br_bif_list = saved_bifs;
3772 ifnet_domsg(&dmsg.br_nmsg.nm_lmsg, 0);
3776 bridge_set_bifflags_handler(struct netmsg *nmsg)
3778 struct netmsg_brsflags *smsg = (struct netmsg_brsflags *)nmsg;
3779 struct bridge_softc *sc;
3780 struct bridge_iflist *bif;
3782 sc = smsg->br_softc;
3785 * Locate the bif associated with the br_bif_info
3786 * on the current CPU
3788 bif = bridge_lookup_member_ifinfo(sc, smsg->br_bif_info);
3789 KKASSERT(bif != NULL && bif->bif_onlist);
3791 bif->bif_flags = smsg->br_bif_flags;
3793 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3797 bridge_set_bifflags(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3800 struct netmsg_brsflags smsg;
3802 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
3804 netmsg_init(&smsg.br_nmsg, &curthread->td_msgport, 0,
3805 bridge_set_bifflags_handler);
3807 smsg.br_bif_info = bif_info;
3808 smsg.br_bif_flags = bif_flags;
3810 ifnet_domsg(&smsg.br_nmsg.nm_lmsg, 0);