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.57 2008/11/22 09:54:28 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
191 #include "opt_inet.h"
192 #include "opt_inet6.h"
194 #include <sys/param.h>
195 #include <sys/mbuf.h>
196 #include <sys/malloc.h>
197 #include <sys/protosw.h>
198 #include <sys/systm.h>
199 #include <sys/time.h>
200 #include <sys/socket.h> /* for net/if.h */
201 #include <sys/sockio.h>
202 #include <sys/ctype.h> /* string functions */
203 #include <sys/kernel.h>
204 #include <sys/random.h>
205 #include <sys/sysctl.h>
206 #include <sys/module.h>
207 #include <sys/proc.h>
208 #include <sys/lock.h>
209 #include <sys/thread.h>
210 #include <sys/thread2.h>
211 #include <sys/mpipe.h>
215 #include <net/if_dl.h>
216 #include <net/if_types.h>
217 #include <net/if_var.h>
218 #include <net/pfil.h>
219 #include <net/ifq_var.h>
220 #include <net/if_clone.h>
222 #include <netinet/in.h> /* for struct arpcom */
223 #include <netinet/in_systm.h>
224 #include <netinet/in_var.h>
225 #include <netinet/ip.h>
226 #include <netinet/ip_var.h>
228 #include <netinet/ip6.h>
229 #include <netinet6/ip6_var.h>
231 #include <netinet/if_ether.h> /* for struct arpcom */
232 #include <net/bridge/if_bridgevar.h>
233 #include <net/if_llc.h>
234 #include <net/netmsg2.h>
236 #include <net/route.h>
237 #include <sys/in_cksum.h>
240 * Size of the route hash table. Must be a power of two.
242 #ifndef BRIDGE_RTHASH_SIZE
243 #define BRIDGE_RTHASH_SIZE 1024
246 #define BRIDGE_RTHASH_MASK (BRIDGE_RTHASH_SIZE - 1)
249 * Maximum number of addresses to cache.
251 #ifndef BRIDGE_RTABLE_MAX
252 #define BRIDGE_RTABLE_MAX 100
256 * Spanning tree defaults.
258 #define BSTP_DEFAULT_MAX_AGE (20 * 256)
259 #define BSTP_DEFAULT_HELLO_TIME (2 * 256)
260 #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
261 #define BSTP_DEFAULT_HOLD_TIME (1 * 256)
262 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
263 #define BSTP_DEFAULT_PORT_PRIORITY 0x80
264 #define BSTP_DEFAULT_PATH_COST 55
267 * Timeout (in seconds) for entries learned dynamically.
269 #ifndef BRIDGE_RTABLE_TIMEOUT
270 #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */
274 * Number of seconds between walks of the route list.
276 #ifndef BRIDGE_RTABLE_PRUNE_PERIOD
277 #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60)
281 * List of capabilities to mask on the member interface.
283 #define BRIDGE_IFCAPS_MASK IFCAP_TXCSUM
285 typedef int (*bridge_ctl_t)(struct bridge_softc *, void *);
287 struct netmsg_brctl {
288 struct netmsg bc_nmsg;
289 bridge_ctl_t bc_func;
290 struct bridge_softc *bc_sc;
294 struct netmsg_brsaddr {
295 struct netmsg br_nmsg;
296 struct bridge_softc *br_softc;
297 struct ifnet *br_dst_if;
298 struct bridge_rtinfo *br_rtinfo;
300 uint8_t br_dst[ETHER_ADDR_LEN];
304 struct netmsg_braddbif {
305 struct netmsg br_nmsg;
306 struct bridge_softc *br_softc;
307 struct bridge_ifinfo *br_bif_info;
308 struct ifnet *br_bif_ifp;
311 struct netmsg_brdelbif {
312 struct netmsg br_nmsg;
313 struct bridge_softc *br_softc;
314 struct bridge_ifinfo *br_bif_info;
315 struct bridge_iflist_head *br_bif_list;
318 struct netmsg_brsflags {
319 struct netmsg br_nmsg;
320 struct bridge_softc *br_softc;
321 struct bridge_ifinfo *br_bif_info;
322 uint32_t br_bif_flags;
325 eventhandler_tag bridge_detach_cookie = NULL;
327 extern struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
328 extern int (*bridge_output_p)(struct ifnet *, struct mbuf *);
329 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
331 static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD;
333 static int bridge_clone_create(struct if_clone *, int);
334 static void bridge_clone_destroy(struct ifnet *);
336 static int bridge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
337 static void bridge_mutecaps(struct bridge_ifinfo *, struct ifnet *, int);
338 static void bridge_ifdetach(void *, struct ifnet *);
339 static void bridge_init(void *);
340 static void bridge_stop(struct ifnet *);
341 static void bridge_start(struct ifnet *);
342 static struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
343 static int bridge_output(struct ifnet *, struct mbuf *);
345 static void bridge_forward(struct bridge_softc *, struct mbuf *m);
347 static void bridge_timer_handler(struct netmsg *);
348 static void bridge_timer(void *);
350 static void bridge_start_bcast(struct bridge_softc *, struct mbuf *);
351 static void bridge_broadcast(struct bridge_softc *, struct ifnet *,
353 static void bridge_span(struct bridge_softc *, struct mbuf *);
355 static int bridge_rtupdate(struct bridge_softc *, const uint8_t *,
356 struct ifnet *, uint8_t);
357 static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *);
358 static void bridge_rtreap(struct bridge_softc *);
359 static void bridge_rttrim(struct bridge_softc *);
360 static int bridge_rtage_finddead(struct bridge_softc *);
361 static void bridge_rtage(struct bridge_softc *);
362 static void bridge_rtflush(struct bridge_softc *, int);
363 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
364 static int bridge_rtsaddr(struct bridge_softc *, const uint8_t *,
365 struct ifnet *, uint8_t);
366 static void bridge_rtmsg_sync(struct bridge_softc *sc);
367 static void bridge_rtreap_handler(struct netmsg *);
368 static void bridge_rtinstall_handler(struct netmsg *);
369 static int bridge_rtinstall_oncpu(struct bridge_softc *, const uint8_t *,
370 struct ifnet *, int, uint8_t, struct bridge_rtinfo **);
372 static void bridge_rtable_init(struct bridge_softc *);
373 static void bridge_rtable_fini(struct bridge_softc *);
375 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
376 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
378 static void bridge_rtnode_insert(struct bridge_softc *,
379 struct bridge_rtnode *);
380 static void bridge_rtnode_destroy(struct bridge_softc *,
381 struct bridge_rtnode *);
383 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
385 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
387 static struct bridge_iflist *bridge_lookup_member_ifinfo(struct bridge_softc *,
388 struct bridge_ifinfo *);
389 static void bridge_delete_member(struct bridge_softc *,
390 struct bridge_iflist *, int);
391 static void bridge_delete_span(struct bridge_softc *,
392 struct bridge_iflist *);
394 static int bridge_control(struct bridge_softc *, u_long,
395 bridge_ctl_t, void *);
396 static int bridge_ioctl_init(struct bridge_softc *, void *);
397 static int bridge_ioctl_stop(struct bridge_softc *, void *);
398 static int bridge_ioctl_add(struct bridge_softc *, void *);
399 static int bridge_ioctl_del(struct bridge_softc *, void *);
400 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
401 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
402 static int bridge_ioctl_scache(struct bridge_softc *, void *);
403 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
404 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
405 static int bridge_ioctl_rts(struct bridge_softc *, void *);
406 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
407 static int bridge_ioctl_sto(struct bridge_softc *, void *);
408 static int bridge_ioctl_gto(struct bridge_softc *, void *);
409 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
410 static int bridge_ioctl_flush(struct bridge_softc *, void *);
411 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
412 static int bridge_ioctl_spri(struct bridge_softc *, void *);
413 static int bridge_ioctl_ght(struct bridge_softc *, void *);
414 static int bridge_ioctl_sht(struct bridge_softc *, void *);
415 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
416 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
417 static int bridge_ioctl_gma(struct bridge_softc *, void *);
418 static int bridge_ioctl_sma(struct bridge_softc *, void *);
419 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
420 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
421 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
422 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
423 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
425 static int bridge_ip_checkbasic(struct mbuf **mp);
427 static int bridge_ip6_checkbasic(struct mbuf **mp);
429 static int bridge_fragment(struct ifnet *, struct mbuf *,
430 struct ether_header *, int, struct llc *);
431 static void bridge_enqueue_handler(struct netmsg *);
432 static void bridge_handoff(struct ifnet *, struct mbuf *);
434 static void bridge_del_bif_handler(struct netmsg *);
435 static void bridge_add_bif_handler(struct netmsg *);
436 static void bridge_set_bifflags_handler(struct netmsg *);
437 static void bridge_del_bif(struct bridge_softc *, struct bridge_ifinfo *,
438 struct bridge_iflist_head *);
439 static void bridge_add_bif(struct bridge_softc *, struct bridge_ifinfo *,
441 static void bridge_set_bifflags(struct bridge_softc *,
442 struct bridge_ifinfo *, uint32_t);
444 SYSCTL_DECL(_net_link);
445 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
447 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
448 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
449 static int pfil_member = 1; /* run pfil hooks on the member interface */
450 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
451 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
452 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
453 &pfil_bridge, 0, "Packet filter on the bridge interface");
454 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
455 &pfil_member, 0, "Packet filter on the member interface");
457 struct bridge_control_arg {
459 struct ifbreq ifbreq;
460 struct ifbifconf ifbifconf;
461 struct ifbareq ifbareq;
462 struct ifbaconf ifbaconf;
463 struct ifbrparam ifbrparam;
470 struct bridge_control {
471 bridge_ctl_t bc_func;
476 #define BC_F_COPYIN 0x01 /* copy arguments in */
477 #define BC_F_COPYOUT 0x02 /* copy arguments out */
478 #define BC_F_SUSER 0x04 /* do super-user check */
480 const struct bridge_control bridge_control_table[] = {
481 { bridge_ioctl_add, sizeof(struct ifbreq),
482 BC_F_COPYIN|BC_F_SUSER },
483 { bridge_ioctl_del, sizeof(struct ifbreq),
484 BC_F_COPYIN|BC_F_SUSER },
486 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
487 BC_F_COPYIN|BC_F_COPYOUT },
488 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
489 BC_F_COPYIN|BC_F_SUSER },
491 { bridge_ioctl_scache, sizeof(struct ifbrparam),
492 BC_F_COPYIN|BC_F_SUSER },
493 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
496 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
497 BC_F_COPYIN|BC_F_COPYOUT },
498 { bridge_ioctl_rts, sizeof(struct ifbaconf),
499 BC_F_COPYIN|BC_F_COPYOUT },
501 { bridge_ioctl_saddr, sizeof(struct ifbareq),
502 BC_F_COPYIN|BC_F_SUSER },
504 { bridge_ioctl_sto, sizeof(struct ifbrparam),
505 BC_F_COPYIN|BC_F_SUSER },
506 { bridge_ioctl_gto, sizeof(struct ifbrparam),
509 { bridge_ioctl_daddr, sizeof(struct ifbareq),
510 BC_F_COPYIN|BC_F_SUSER },
512 { bridge_ioctl_flush, sizeof(struct ifbreq),
513 BC_F_COPYIN|BC_F_SUSER },
515 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
517 { bridge_ioctl_spri, sizeof(struct ifbrparam),
518 BC_F_COPYIN|BC_F_SUSER },
520 { bridge_ioctl_ght, sizeof(struct ifbrparam),
522 { bridge_ioctl_sht, sizeof(struct ifbrparam),
523 BC_F_COPYIN|BC_F_SUSER },
525 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
527 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
528 BC_F_COPYIN|BC_F_SUSER },
530 { bridge_ioctl_gma, sizeof(struct ifbrparam),
532 { bridge_ioctl_sma, sizeof(struct ifbrparam),
533 BC_F_COPYIN|BC_F_SUSER },
535 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
536 BC_F_COPYIN|BC_F_SUSER },
538 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
539 BC_F_COPYIN|BC_F_SUSER },
541 { bridge_ioctl_addspan, sizeof(struct ifbreq),
542 BC_F_COPYIN|BC_F_SUSER },
543 { bridge_ioctl_delspan, sizeof(struct ifbreq),
544 BC_F_COPYIN|BC_F_SUSER },
546 static const int bridge_control_table_size =
547 sizeof(bridge_control_table) / sizeof(bridge_control_table[0]);
549 LIST_HEAD(, bridge_softc) bridge_list;
551 struct if_clone bridge_cloner = IF_CLONE_INITIALIZER("bridge",
553 bridge_clone_destroy, 0, IF_MAXUNIT);
556 bridge_modevent(module_t mod, int type, void *data)
560 LIST_INIT(&bridge_list);
561 if_clone_attach(&bridge_cloner);
562 bridge_input_p = bridge_input;
563 bridge_output_p = bridge_output;
564 bridge_detach_cookie = EVENTHANDLER_REGISTER(
565 ifnet_detach_event, bridge_ifdetach, NULL,
566 EVENTHANDLER_PRI_ANY);
568 bstp_linkstate_p = bstp_linkstate;
572 if (!LIST_EMPTY(&bridge_list))
574 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
575 bridge_detach_cookie);
576 if_clone_detach(&bridge_cloner);
577 bridge_input_p = NULL;
578 bridge_output_p = NULL;
580 bstp_linkstate_p = NULL;
589 static moduledata_t bridge_mod = {
595 DECLARE_MODULE(if_bridge, bridge_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
599 * bridge_clone_create:
601 * Create a new bridge instance.
604 bridge_clone_create(struct if_clone *ifc, int unit)
606 struct bridge_softc *sc;
611 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
612 ifp = sc->sc_ifp = &sc->sc_if;
614 sc->sc_brtmax = BRIDGE_RTABLE_MAX;
615 sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
616 sc->sc_bridge_max_age = BSTP_DEFAULT_MAX_AGE;
617 sc->sc_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME;
618 sc->sc_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY;
619 sc->sc_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY;
620 sc->sc_hold_time = BSTP_DEFAULT_HOLD_TIME;
622 /* Initialize our routing table. */
623 bridge_rtable_init(sc);
625 callout_init(&sc->sc_brcallout);
626 netmsg_init(&sc->sc_brtimemsg, &netisr_adone_rport,
627 MSGF_DROPABLE | MSGF_PRIORITY, bridge_timer_handler);
628 sc->sc_brtimemsg.nm_lmsg.u.ms_resultp = sc;
630 callout_init(&sc->sc_bstpcallout);
631 netmsg_init(&sc->sc_bstptimemsg, &netisr_adone_rport,
632 MSGF_DROPABLE | MSGF_PRIORITY, bstp_tick_handler);
633 sc->sc_bstptimemsg.nm_lmsg.u.ms_resultp = sc;
635 /* Initialize per-cpu member iface lists */
636 sc->sc_iflists = kmalloc(sizeof(*sc->sc_iflists) * ncpus,
638 for (cpu = 0; cpu < ncpus; ++cpu)
639 LIST_INIT(&sc->sc_iflists[cpu]);
641 LIST_INIT(&sc->sc_spanlist);
644 if_initname(ifp, ifc->ifc_name, unit);
645 ifp->if_mtu = ETHERMTU;
646 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST;
647 ifp->if_ioctl = bridge_ioctl;
648 ifp->if_start = bridge_start;
649 ifp->if_init = bridge_init;
650 ifp->if_type = IFT_BRIDGE;
651 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
652 ifp->if_snd.ifq_maxlen = ifqmaxlen;
653 ifq_set_ready(&ifp->if_snd);
654 ifp->if_hdrlen = ETHER_HDR_LEN;
657 * Generate a random ethernet address and use the private AC:DE:48
661 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
663 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
665 eaddr[0] &= ~1; /* clear multicast bit */
666 eaddr[0] |= 2; /* set the LAA bit */
668 ether_ifattach(ifp, eaddr, NULL);
669 /* Now undo some of the damage... */
670 ifp->if_baudrate = 0;
671 ifp->if_type = IFT_BRIDGE;
673 crit_enter(); /* XXX MP */
674 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
681 bridge_delete_dispatch(struct netmsg *nmsg)
683 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
684 struct bridge_softc *sc = lmsg->u.ms_resultp;
685 struct ifnet *bifp = sc->sc_ifp;
686 struct bridge_iflist *bif;
688 lwkt_serialize_enter(bifp->if_serializer);
690 while ((bif = LIST_FIRST(&sc->sc_iflists[mycpuid])) != NULL)
691 bridge_delete_member(sc, bif, 0);
693 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL)
694 bridge_delete_span(sc, bif);
696 lwkt_serialize_exit(bifp->if_serializer);
698 lwkt_replymsg(lmsg, 0);
702 * bridge_clone_destroy:
704 * Destroy a bridge instance.
707 bridge_clone_destroy(struct ifnet *ifp)
709 struct bridge_softc *sc = ifp->if_softc;
710 struct lwkt_msg *lmsg;
713 lwkt_serialize_enter(ifp->if_serializer);
716 ifp->if_flags &= ~IFF_UP;
718 lwkt_serialize_exit(ifp->if_serializer);
720 netmsg_init(&nmsg, &curthread->td_msgport, 0, bridge_delete_dispatch);
721 lmsg = &nmsg.nm_lmsg;
722 lmsg->u.ms_resultp = sc;
723 lwkt_domsg(BRIDGE_CFGPORT, lmsg, 0);
725 crit_enter(); /* XXX MP */
726 LIST_REMOVE(sc, sc_list);
731 /* Tear down the routing table. */
732 bridge_rtable_fini(sc);
734 /* Free per-cpu member iface lists */
735 kfree(sc->sc_iflists, M_DEVBUF);
743 * Handle a control request from the operator.
746 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
748 struct bridge_softc *sc = ifp->if_softc;
749 struct bridge_control_arg args;
750 struct ifdrv *ifd = (struct ifdrv *) data;
751 const struct bridge_control *bc;
754 ASSERT_SERIALIZED(ifp->if_serializer);
763 if (ifd->ifd_cmd >= bridge_control_table_size) {
767 bc = &bridge_control_table[ifd->ifd_cmd];
769 if (cmd == SIOCGDRVSPEC &&
770 (bc->bc_flags & BC_F_COPYOUT) == 0) {
773 } else if (cmd == SIOCSDRVSPEC &&
774 (bc->bc_flags & BC_F_COPYOUT)) {
779 if (bc->bc_flags & BC_F_SUSER) {
780 error = suser_cred(cr, NULL_CRED_OKAY);
785 if (ifd->ifd_len != bc->bc_argsize ||
786 ifd->ifd_len > sizeof(args.bca_u)) {
791 memset(&args, 0, sizeof(args));
792 if (bc->bc_flags & BC_F_COPYIN) {
793 error = copyin(ifd->ifd_data, &args.bca_u,
799 error = bridge_control(sc, cmd, bc->bc_func, &args);
801 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
805 if (bc->bc_flags & BC_F_COPYOUT) {
806 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
807 if (args.bca_len != 0) {
808 KKASSERT(args.bca_kptr != NULL);
810 error = copyout(args.bca_kptr,
811 args.bca_uptr, args.bca_len);
813 kfree(args.bca_kptr, M_TEMP);
815 KKASSERT(args.bca_kptr == NULL);
818 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
823 if (!(ifp->if_flags & IFF_UP) &&
824 (ifp->if_flags & IFF_RUNNING)) {
826 * If interface is marked down and it is running,
830 } else if ((ifp->if_flags & IFF_UP) &&
831 !(ifp->if_flags & IFF_RUNNING)) {
833 * If interface is marked up and it is stopped, then
841 /* Do not allow the MTU to be changed on the bridge */
846 error = ether_ioctl(ifp, cmd, data);
855 * Clear or restore unwanted capabilities on the member interface
858 bridge_mutecaps(struct bridge_ifinfo *bif_info, struct ifnet *ifp, int mute)
863 if (ifp->if_ioctl == NULL)
866 bzero(&ifr, sizeof(ifr));
867 ifr.ifr_reqcap = ifp->if_capenable;
870 /* mask off and save capabilities */
871 bif_info->bifi_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
872 if (bif_info->bifi_mutecap != 0)
873 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
875 /* restore muted capabilities */
876 ifr.ifr_reqcap |= bif_info->bifi_mutecap;
879 if (bif_info->bifi_mutecap != 0) {
880 lwkt_serialize_enter(ifp->if_serializer);
881 error = ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
882 lwkt_serialize_exit(ifp->if_serializer);
887 * bridge_lookup_member:
889 * Lookup a bridge member interface.
891 static struct bridge_iflist *
892 bridge_lookup_member(struct bridge_softc *sc, const char *name)
894 struct bridge_iflist *bif;
896 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
897 if (strcmp(bif->bif_ifp->if_xname, name) == 0)
904 * bridge_lookup_member_if:
906 * Lookup a bridge member interface by ifnet*.
908 static struct bridge_iflist *
909 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
911 struct bridge_iflist *bif;
913 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
914 if (bif->bif_ifp == member_ifp)
921 * bridge_lookup_member_ifinfo:
923 * Lookup a bridge member interface by bridge_ifinfo.
925 static struct bridge_iflist *
926 bridge_lookup_member_ifinfo(struct bridge_softc *sc,
927 struct bridge_ifinfo *bif_info)
929 struct bridge_iflist *bif;
931 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
932 if (bif->bif_info == bif_info)
939 * bridge_delete_member:
941 * Delete the specified member interface.
944 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
947 struct ifnet *ifs = bif->bif_ifp;
948 struct ifnet *bifp = sc->sc_ifp;
949 struct bridge_ifinfo *bif_info = bif->bif_info;
950 struct bridge_iflist_head saved_bifs;
952 ASSERT_SERIALIZED(bifp->if_serializer);
953 KKASSERT(bif_info != NULL);
955 ifs->if_bridge = NULL;
958 * Release bridge interface's serializer:
959 * - To avoid possible dead lock.
960 * - Various sync operation will block the current thread.
962 lwkt_serialize_exit(bifp->if_serializer);
965 switch (ifs->if_type) {
969 * Take the interface out of promiscuous mode.
972 bridge_mutecaps(bif_info, ifs, 0);
979 panic("bridge_delete_member: impossible");
985 * Remove bifs from percpu linked list.
987 * Removed bifs are not freed immediately, instead,
988 * they are saved in saved_bifs. They will be freed
989 * after we make sure that no one is accessing them,
990 * i.e. after following netmsg_service_sync()
992 LIST_INIT(&saved_bifs);
993 bridge_del_bif(sc, bif_info, &saved_bifs);
996 * Make sure that all protocol threads:
997 * o see 'ifs' if_bridge is changed
998 * o know that bif is removed from the percpu linked list
1000 netmsg_service_sync();
1003 * Free the removed bifs
1005 KKASSERT(!LIST_EMPTY(&saved_bifs));
1006 while ((bif = LIST_FIRST(&saved_bifs)) != NULL) {
1007 LIST_REMOVE(bif, bif_next);
1008 kfree(bif, M_DEVBUF);
1011 /* See the comment in bridge_ioctl_stop() */
1012 bridge_rtmsg_sync(sc);
1013 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL);
1015 lwkt_serialize_enter(bifp->if_serializer);
1017 if (bifp->if_flags & IFF_RUNNING)
1018 bstp_initialization(sc);
1021 * Free the bif_info after bstp_initialization(), so that
1022 * bridge_softc.sc_root_port will not reference a dangling
1025 kfree(bif_info, M_DEVBUF);
1029 * bridge_delete_span:
1031 * Delete the specified span interface.
1034 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1036 KASSERT(bif->bif_ifp->if_bridge == NULL,
1037 ("%s: not a span interface", __func__));
1039 LIST_REMOVE(bif, bif_next);
1040 kfree(bif, M_DEVBUF);
1044 bridge_ioctl_init(struct bridge_softc *sc, void *arg __unused)
1046 struct ifnet *ifp = sc->sc_ifp;
1048 if (ifp->if_flags & IFF_RUNNING)
1051 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1054 ifp->if_flags |= IFF_RUNNING;
1055 bstp_initialization(sc);
1060 bridge_ioctl_stop(struct bridge_softc *sc, void *arg __unused)
1062 struct ifnet *ifp = sc->sc_ifp;
1063 struct lwkt_msg *lmsg;
1065 if ((ifp->if_flags & IFF_RUNNING) == 0)
1068 callout_stop(&sc->sc_brcallout);
1071 lmsg = &sc->sc_brtimemsg.nm_lmsg;
1072 if ((lmsg->ms_flags & MSGF_DONE) == 0) {
1073 /* Pending to be processed; drop it */
1080 ifp->if_flags &= ~IFF_RUNNING;
1082 lwkt_serialize_exit(ifp->if_serializer);
1084 /* Let everyone know that we are stopped */
1085 netmsg_service_sync();
1088 * Sync ifnetX msgports in the order we forward rtnode
1089 * installation message. This is used to make sure that
1090 * all rtnode installation messages sent by bridge_rtupdate()
1091 * during above netmsg_service_sync() are flushed.
1093 bridge_rtmsg_sync(sc);
1094 bridge_rtflush(sc, IFBF_FLUSHDYN);
1096 lwkt_serialize_enter(ifp->if_serializer);
1101 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1103 struct ifbreq *req = arg;
1104 struct bridge_iflist *bif;
1105 struct bridge_ifinfo *bif_info;
1106 struct ifnet *ifs, *bifp;
1110 ASSERT_SERIALIZED(bifp->if_serializer);
1112 ifs = ifunit(req->ifbr_ifsname);
1116 /* If it's in the span list, it can't be a member. */
1117 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1118 if (ifs == bif->bif_ifp)
1121 /* Allow the first Ethernet member to define the MTU */
1122 if (ifs->if_type != IFT_GIF) {
1123 if (LIST_EMPTY(&sc->sc_iflists[mycpuid])) {
1124 bifp->if_mtu = ifs->if_mtu;
1125 } else if (bifp->if_mtu != ifs->if_mtu) {
1126 if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname);
1131 if (ifs->if_bridge == sc)
1134 if (ifs->if_bridge != NULL)
1137 bif_info = kmalloc(sizeof(*bif_info), M_DEVBUF, M_WAITOK | M_ZERO);
1138 bif_info->bifi_priority = BSTP_DEFAULT_PORT_PRIORITY;
1139 bif_info->bifi_path_cost = BSTP_DEFAULT_PATH_COST;
1140 bif_info->bifi_ifp = ifs;
1143 * Release bridge interface's serializer:
1144 * - To avoid possible dead lock.
1145 * - Various sync operation will block the current thread.
1147 lwkt_serialize_exit(bifp->if_serializer);
1149 switch (ifs->if_type) {
1153 * Place the interface into promiscuous mode.
1155 error = ifpromisc(ifs, 1);
1157 lwkt_serialize_enter(bifp->if_serializer);
1160 bridge_mutecaps(bif_info, ifs, 1);
1163 case IFT_GIF: /* :^) */
1168 lwkt_serialize_enter(bifp->if_serializer);
1173 * Add bifs to percpu linked lists
1175 bridge_add_bif(sc, bif_info, ifs);
1177 lwkt_serialize_enter(bifp->if_serializer);
1179 if (bifp->if_flags & IFF_RUNNING)
1180 bstp_initialization(sc);
1185 * Everything has been setup, so let the member interface
1186 * deliver packets to this bridge on its input/output path.
1188 ifs->if_bridge = sc;
1191 if (bif_info != NULL)
1192 kfree(bif_info, M_DEVBUF);
1198 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1200 struct ifbreq *req = arg;
1201 struct bridge_iflist *bif;
1203 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1207 bridge_delete_member(sc, bif, 0);
1213 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1215 struct ifbreq *req = arg;
1216 struct bridge_iflist *bif;
1218 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1222 req->ifbr_ifsflags = bif->bif_flags;
1223 req->ifbr_state = bif->bif_state;
1224 req->ifbr_priority = bif->bif_priority;
1225 req->ifbr_path_cost = bif->bif_path_cost;
1226 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1232 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1234 struct ifbreq *req = arg;
1235 struct bridge_iflist *bif;
1236 struct ifnet *bifp = sc->sc_ifp;
1238 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1242 if (req->ifbr_ifsflags & IFBIF_SPAN) {
1243 /* SPAN is readonly */
1247 if (req->ifbr_ifsflags & IFBIF_STP) {
1248 switch (bif->bif_ifp->if_type) {
1250 /* These can do spanning tree. */
1254 /* Nothing else can. */
1259 lwkt_serialize_exit(bifp->if_serializer);
1260 bridge_set_bifflags(sc, bif->bif_info, req->ifbr_ifsflags);
1261 lwkt_serialize_enter(bifp->if_serializer);
1263 if (bifp->if_flags & IFF_RUNNING)
1264 bstp_initialization(sc);
1270 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1272 struct ifbrparam *param = arg;
1273 struct ifnet *ifp = sc->sc_ifp;
1275 sc->sc_brtmax = param->ifbrp_csize;
1277 lwkt_serialize_exit(ifp->if_serializer);
1279 lwkt_serialize_enter(ifp->if_serializer);
1285 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1287 struct ifbrparam *param = arg;
1289 param->ifbrp_csize = sc->sc_brtmax;
1295 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1297 struct bridge_control_arg *bc_arg = arg;
1298 struct ifbifconf *bifc = arg;
1299 struct bridge_iflist *bif;
1300 struct ifbreq *breq;
1304 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next)
1306 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1309 if (bifc->ifbic_len == 0) {
1310 bifc->ifbic_len = sizeof(*breq) * count;
1312 } else if (count == 0 || bifc->ifbic_len < sizeof(*breq)) {
1313 bifc->ifbic_len = 0;
1317 len = min(bifc->ifbic_len, sizeof(*breq) * count);
1318 KKASSERT(len >= sizeof(*breq));
1320 breq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1322 bifc->ifbic_len = 0;
1325 bc_arg->bca_kptr = breq;
1328 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1329 if (len < sizeof(*breq))
1332 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1333 sizeof(breq->ifbr_ifsname));
1334 breq->ifbr_ifsflags = bif->bif_flags;
1335 breq->ifbr_state = bif->bif_state;
1336 breq->ifbr_priority = bif->bif_priority;
1337 breq->ifbr_path_cost = bif->bif_path_cost;
1338 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1341 len -= sizeof(*breq);
1343 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1344 if (len < sizeof(*breq))
1347 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1348 sizeof(breq->ifbr_ifsname));
1349 breq->ifbr_ifsflags = bif->bif_flags;
1350 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1353 len -= sizeof(*breq);
1356 bifc->ifbic_len = sizeof(*breq) * count;
1357 KKASSERT(bifc->ifbic_len > 0);
1359 bc_arg->bca_len = bifc->ifbic_len;
1360 bc_arg->bca_uptr = bifc->ifbic_req;
1365 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1367 struct bridge_control_arg *bc_arg = arg;
1368 struct ifbaconf *bac = arg;
1369 struct bridge_rtnode *brt;
1370 struct ifbareq *bareq;
1374 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list)
1377 if (bac->ifbac_len == 0) {
1378 bac->ifbac_len = sizeof(*bareq) * count;
1380 } else if (count == 0 || bac->ifbac_len < sizeof(*bareq)) {
1385 len = min(bac->ifbac_len, sizeof(*bareq) * count);
1386 KKASSERT(len >= sizeof(*bareq));
1388 bareq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1389 if (bareq == NULL) {
1393 bc_arg->bca_kptr = bareq;
1396 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
1397 struct bridge_rtinfo *bri = brt->brt_info;
1398 unsigned long expire;
1400 if (len < sizeof(*bareq))
1403 strlcpy(bareq->ifba_ifsname, bri->bri_ifp->if_xname,
1404 sizeof(bareq->ifba_ifsname));
1405 memcpy(bareq->ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1406 expire = bri->bri_expire;
1407 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1408 time_second < expire)
1409 bareq->ifba_expire = expire - time_second;
1411 bareq->ifba_expire = 0;
1412 bareq->ifba_flags = bri->bri_flags;
1415 len -= sizeof(*bareq);
1418 bac->ifbac_len = sizeof(*bareq) * count;
1419 KKASSERT(bac->ifbac_len > 0);
1421 bc_arg->bca_len = bac->ifbac_len;
1422 bc_arg->bca_uptr = bac->ifbac_req;
1427 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1429 struct ifbareq *req = arg;
1430 struct bridge_iflist *bif;
1431 struct ifnet *ifp = sc->sc_ifp;
1434 ASSERT_SERIALIZED(ifp->if_serializer);
1436 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1440 lwkt_serialize_exit(ifp->if_serializer);
1441 error = bridge_rtsaddr(sc, req->ifba_dst, bif->bif_ifp,
1443 lwkt_serialize_enter(ifp->if_serializer);
1448 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1450 struct ifbrparam *param = arg;
1452 sc->sc_brttimeout = param->ifbrp_ctime;
1458 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1460 struct ifbrparam *param = arg;
1462 param->ifbrp_ctime = sc->sc_brttimeout;
1468 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1470 struct ifbareq *req = arg;
1471 struct ifnet *ifp = sc->sc_ifp;
1474 lwkt_serialize_exit(ifp->if_serializer);
1475 error = bridge_rtdaddr(sc, req->ifba_dst);
1476 lwkt_serialize_enter(ifp->if_serializer);
1481 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1483 struct ifbreq *req = arg;
1484 struct ifnet *ifp = sc->sc_ifp;
1486 lwkt_serialize_exit(ifp->if_serializer);
1487 bridge_rtflush(sc, req->ifbr_ifsflags);
1488 lwkt_serialize_enter(ifp->if_serializer);
1494 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1496 struct ifbrparam *param = arg;
1498 param->ifbrp_prio = sc->sc_bridge_priority;
1504 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1506 struct ifbrparam *param = arg;
1508 sc->sc_bridge_priority = param->ifbrp_prio;
1510 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1511 bstp_initialization(sc);
1517 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1519 struct ifbrparam *param = arg;
1521 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1527 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1529 struct ifbrparam *param = arg;
1531 if (param->ifbrp_hellotime == 0)
1533 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1535 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1536 bstp_initialization(sc);
1542 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1544 struct ifbrparam *param = arg;
1546 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1552 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1554 struct ifbrparam *param = arg;
1556 if (param->ifbrp_fwddelay == 0)
1558 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1560 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1561 bstp_initialization(sc);
1567 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1569 struct ifbrparam *param = arg;
1571 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1577 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1579 struct ifbrparam *param = arg;
1581 if (param->ifbrp_maxage == 0)
1583 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1585 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1586 bstp_initialization(sc);
1592 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1594 struct ifbreq *req = arg;
1595 struct bridge_iflist *bif;
1597 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1601 bif->bif_priority = req->ifbr_priority;
1603 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1604 bstp_initialization(sc);
1610 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1612 struct ifbreq *req = arg;
1613 struct bridge_iflist *bif;
1615 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1619 bif->bif_path_cost = req->ifbr_path_cost;
1621 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1622 bstp_initialization(sc);
1628 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1630 struct ifbreq *req = arg;
1631 struct bridge_iflist *bif;
1634 ifs = ifunit(req->ifbr_ifsname);
1638 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1639 if (ifs == bif->bif_ifp)
1642 if (ifs->if_bridge != NULL)
1645 switch (ifs->if_type) {
1655 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
1657 bif->bif_flags = IFBIF_SPAN;
1658 /* NOTE: span bif does not need bridge_ifinfo */
1660 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1668 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1670 struct ifbreq *req = arg;
1671 struct bridge_iflist *bif;
1674 ifs = ifunit(req->ifbr_ifsname);
1678 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1679 if (ifs == bif->bif_ifp)
1685 bridge_delete_span(sc, bif);
1687 if (LIST_EMPTY(&sc->sc_spanlist))
1694 bridge_ifdetach_dispatch(struct netmsg *nmsg)
1696 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
1697 struct ifnet *ifp, *bifp;
1698 struct bridge_softc *sc;
1699 struct bridge_iflist *bif;
1701 ifp = lmsg->u.ms_resultp;
1702 sc = ifp->if_bridge;
1704 /* Check if the interface is a bridge member */
1708 lwkt_serialize_enter(bifp->if_serializer);
1710 bif = bridge_lookup_member_if(sc, ifp);
1712 bridge_delete_member(sc, bif, 1);
1714 /* XXX Why bif will be NULL? */
1717 lwkt_serialize_exit(bifp->if_serializer);
1721 crit_enter(); /* XXX MP */
1723 /* Check if the interface is a span port */
1724 LIST_FOREACH(sc, &bridge_list, sc_list) {
1727 lwkt_serialize_enter(bifp->if_serializer);
1729 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1730 if (ifp == bif->bif_ifp) {
1731 bridge_delete_span(sc, bif);
1735 lwkt_serialize_exit(bifp->if_serializer);
1741 lwkt_replymsg(lmsg, 0);
1747 * Detach an interface from a bridge. Called when a member
1748 * interface is detaching.
1751 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1753 struct lwkt_msg *lmsg;
1756 netmsg_init(&nmsg, &curthread->td_msgport, 0, bridge_ifdetach_dispatch);
1757 lmsg = &nmsg.nm_lmsg;
1758 lmsg->u.ms_resultp = ifp;
1760 lwkt_domsg(BRIDGE_CFGPORT, lmsg, 0);
1766 * Initialize a bridge interface.
1769 bridge_init(void *xsc)
1771 bridge_control(xsc, SIOCSIFFLAGS, bridge_ioctl_init, NULL);
1777 * Stop the bridge interface.
1780 bridge_stop(struct ifnet *ifp)
1782 bridge_control(ifp->if_softc, SIOCSIFFLAGS, bridge_ioctl_stop, NULL);
1788 * Enqueue a packet on a bridge member interface.
1792 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1794 struct netmsg_packet *nmp;
1797 nmp = &m->m_hdr.mh_netmsg;
1798 netmsg_init(&nmp->nm_netmsg, &netisr_apanic_rport, 0,
1799 bridge_enqueue_handler);
1801 nmp->nm_netmsg.nm_lmsg.u.ms_resultp = dst_ifp;
1803 if (curthread->td_flags & TDF_NETWORK)
1804 port = &curthread->td_msgport;
1806 port = cpu_portfn(mycpuid);
1807 lwkt_sendmsg(port, &nmp->nm_netmsg.nm_lmsg);
1813 * Send output from a bridge member interface. This
1814 * performs the bridging function for locally originated
1817 * The mbuf has the Ethernet header already attached. We must
1818 * enqueue or free the mbuf before returning.
1821 bridge_output(struct ifnet *ifp, struct mbuf *m)
1823 struct bridge_softc *sc = ifp->if_bridge;
1824 struct ether_header *eh;
1825 struct ifnet *dst_if, *bifp;
1827 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
1830 * Make sure that we are still a member of a bridge interface.
1838 if (m->m_len < ETHER_HDR_LEN) {
1839 m = m_pullup(m, ETHER_HDR_LEN);
1843 eh = mtod(m, struct ether_header *);
1846 * If bridge is down, but the original output interface is up,
1847 * go ahead and send out that interface. Otherwise, the packet
1850 if ((bifp->if_flags & IFF_RUNNING) == 0) {
1856 * If the packet is a multicast, or we don't know a better way to
1857 * get there, send to all interfaces.
1859 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1862 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1863 if (dst_if == NULL) {
1864 struct bridge_iflist *bif, *nbif;
1871 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid],
1873 dst_if = bif->bif_ifp;
1874 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1878 * If this is not the original output interface,
1879 * and the interface is participating in spanning
1880 * tree, make sure the port is in a state that
1881 * allows forwarding.
1883 if (dst_if != ifp &&
1884 (bif->bif_flags & IFBIF_STP) != 0) {
1885 switch (bif->bif_state) {
1886 case BSTP_IFSTATE_BLOCKING:
1887 case BSTP_IFSTATE_LISTENING:
1888 case BSTP_IFSTATE_DISABLED:
1893 if (LIST_NEXT(bif, bif_next) == NULL) {
1897 mc = m_copypacket(m, MB_DONTWAIT);
1903 bridge_handoff(dst_if, mc);
1905 if (nbif != NULL && !nbif->bif_onlist) {
1906 KKASSERT(bif->bif_onlist);
1907 nbif = LIST_NEXT(bif, bif_next);
1917 * XXX Spanning tree consideration here?
1921 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1924 bridge_handoff(dst_if, m);
1931 * Start output on a bridge.
1935 bridge_start(struct ifnet *ifp)
1937 struct bridge_softc *sc = ifp->if_softc;
1939 ASSERT_SERIALIZED(ifp->if_serializer);
1941 ifp->if_flags |= IFF_OACTIVE;
1943 struct ifnet *dst_if = NULL;
1944 struct ether_header *eh;
1947 m = ifq_dequeue(&ifp->if_snd, NULL);
1951 if (m->m_len < sizeof(*eh)) {
1952 m = m_pullup(m, sizeof(*eh));
1958 eh = mtod(m, struct ether_header *);
1963 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1964 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1967 bridge_start_bcast(sc, m);
1969 bridge_enqueue(dst_if, m);
1971 ifp->if_flags &= ~IFF_OACTIVE;
1977 * The forwarding function of the bridge.
1980 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
1982 struct bridge_iflist *bif;
1983 struct ifnet *src_if, *dst_if, *ifp;
1984 struct ether_header *eh;
1986 src_if = m->m_pkthdr.rcvif;
1989 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
1992 ifp->if_ibytes += m->m_pkthdr.len;
1995 * Look up the bridge_iflist.
1997 bif = bridge_lookup_member_if(sc, src_if);
1999 /* Interface is not a bridge member (anymore?) */
2004 if (bif->bif_flags & IFBIF_STP) {
2005 switch (bif->bif_state) {
2006 case BSTP_IFSTATE_BLOCKING:
2007 case BSTP_IFSTATE_LISTENING:
2008 case BSTP_IFSTATE_DISABLED:
2014 eh = mtod(m, struct ether_header *);
2017 * If the interface is learning, and the source
2018 * address is valid and not multicast, record
2021 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
2022 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
2023 (eh->ether_shost[0] == 0 &&
2024 eh->ether_shost[1] == 0 &&
2025 eh->ether_shost[2] == 0 &&
2026 eh->ether_shost[3] == 0 &&
2027 eh->ether_shost[4] == 0 &&
2028 eh->ether_shost[5] == 0) == 0)
2029 bridge_rtupdate(sc, eh->ether_shost, src_if, IFBAF_DYNAMIC);
2031 if ((bif->bif_flags & IFBIF_STP) != 0 &&
2032 bif->bif_state == BSTP_IFSTATE_LEARNING) {
2038 * At this point, the port either doesn't participate
2039 * in spanning tree or it is in the forwarding state.
2043 * If the packet is unicast, destined for someone on
2044 * "this" side of the bridge, drop it.
2046 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2047 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2048 if (src_if == dst_if) {
2053 /* ...forward it to all interfaces. */
2058 if (dst_if == NULL) {
2059 bridge_broadcast(sc, src_if, m);
2064 * At this point, we're dealing with a unicast frame
2065 * going to a different interface.
2067 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
2071 bif = bridge_lookup_member_if(sc, dst_if);
2073 /* Not a member of the bridge (anymore?) */
2078 if (bif->bif_flags & IFBIF_STP) {
2079 switch (bif->bif_state) {
2080 case BSTP_IFSTATE_DISABLED:
2081 case BSTP_IFSTATE_BLOCKING:
2087 if (inet_pfil_hook.ph_hashooks > 0
2089 || inet6_pfil_hook.ph_hashooks > 0
2092 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2097 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2102 bridge_handoff(dst_if, m);
2108 * Receive input from a member interface. Queue the packet for
2109 * bridging if it is not for us.
2111 static struct mbuf *
2112 bridge_input(struct ifnet *ifp, struct mbuf *m)
2114 struct bridge_softc *sc = ifp->if_bridge;
2115 struct bridge_iflist *bif;
2116 struct ifnet *bifp, *new_ifp;
2117 struct ether_header *eh;
2118 struct mbuf *mc, *mc2;
2120 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
2123 * Make sure that we are still a member of a bridge interface.
2131 if ((bifp->if_flags & IFF_RUNNING) == 0)
2135 * Implement support for bridge monitoring. If this flag has been
2136 * set on this interface, discard the packet once we push it through
2137 * the bpf(4) machinery, but before we do, increment various counters
2138 * associated with this bridge.
2140 if (bifp->if_flags & IFF_MONITOR) {
2141 /* Change input interface to this bridge */
2142 m->m_pkthdr.rcvif = bifp;
2146 /* Update bridge's ifnet statistics */
2147 bifp->if_ipackets++;
2148 bifp->if_ibytes += m->m_pkthdr.len;
2149 if (m->m_flags & (M_MCAST | M_BCAST))
2157 eh = mtod(m, struct ether_header *);
2159 m->m_flags &= ~M_PROTO1; /* XXX Hack - loop prevention */
2161 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
2163 * If the packet is for us, set the packets source as the
2164 * bridge, and return the packet back to ifnet.if_input for
2167 KASSERT(bifp->if_bridge == NULL,
2168 ("loop created in bridge_input"));
2174 * Tap all packets arriving on the bridge, no matter if
2175 * they are local destinations or not. In is in.
2179 bif = bridge_lookup_member_if(sc, ifp);
2186 if (m->m_flags & (M_BCAST | M_MCAST)) {
2187 /* Tap off 802.1D packets; they do not get forwarded. */
2188 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2189 ETHER_ADDR_LEN) == 0) {
2190 lwkt_serialize_enter(bifp->if_serializer);
2191 bstp_input(sc, bif, m);
2192 lwkt_serialize_exit(bifp->if_serializer);
2194 /* m is freed by bstp_input */
2199 if (bif->bif_flags & IFBIF_STP) {
2200 switch (bif->bif_state) {
2201 case BSTP_IFSTATE_BLOCKING:
2202 case BSTP_IFSTATE_LISTENING:
2203 case BSTP_IFSTATE_DISABLED:
2209 * Make a deep copy of the packet and enqueue the copy
2210 * for bridge processing; return the original packet for
2213 mc = m_dup(m, MB_DONTWAIT);
2217 bridge_forward(sc, mc);
2220 * Reinject the mbuf as arriving on the bridge so we have a
2221 * chance at claiming multicast packets. We can not loop back
2222 * here from ether_input as a bridge is never a member of a
2225 KASSERT(bifp->if_bridge == NULL,
2226 ("loop created in bridge_input"));
2227 mc2 = m_dup(m, MB_DONTWAIT);
2230 /* Keep the layer3 header aligned */
2231 int i = min(mc2->m_pkthdr.len, max_protohdr);
2232 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2237 * Don't tap to bpf(4) again; we have
2238 * already done the tapping.
2240 ether_reinput_oncpu(bifp, mc2, 0);
2243 /* Return the original packet for local processing. */
2247 if (bif->bif_flags & IFBIF_STP) {
2248 switch (bif->bif_state) {
2249 case BSTP_IFSTATE_BLOCKING:
2250 case BSTP_IFSTATE_LISTENING:
2251 case BSTP_IFSTATE_DISABLED:
2257 * Unicast. Make sure it's not for us.
2259 * This loop is MPSAFE; the only blocking operation (bridge_rtupdate)
2260 * is followed by breaking out of the loop.
2262 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2263 if (bif->bif_ifp->if_type != IFT_ETHER)
2266 /* It is destined for us. */
2267 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
2268 ETHER_ADDR_LEN) == 0) {
2269 if (bif->bif_ifp != ifp) {
2270 /* XXX loop prevention */
2271 m->m_flags |= M_PROTO1;
2272 new_ifp = bif->bif_ifp;
2274 if (bif->bif_flags & IFBIF_LEARNING) {
2275 bridge_rtupdate(sc, eh->ether_shost,
2276 ifp, IFBAF_DYNAMIC);
2281 /* We just received a packet that we sent out. */
2282 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
2283 ETHER_ADDR_LEN) == 0) {
2290 /* Perform the bridge forwarding function. */
2291 bridge_forward(sc, m);
2294 if (new_ifp != NULL) {
2295 ether_reinput_oncpu(new_ifp, m, 1);
2302 * bridge_start_bcast:
2304 * Broadcast the packet sent from bridge to all member
2306 * This is a simplified version of bridge_broadcast(), however,
2307 * this function expects caller to hold bridge's serializer.
2310 bridge_start_bcast(struct bridge_softc *sc, struct mbuf *m)
2312 struct bridge_iflist *bif;
2314 struct ifnet *dst_if, *bifp;
2318 ASSERT_SERIALIZED(bifp->if_serializer);
2321 * Following loop is MPSAFE; nothing is blocking
2324 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2325 dst_if = bif->bif_ifp;
2327 if (bif->bif_flags & IFBIF_STP) {
2328 switch (bif->bif_state) {
2329 case BSTP_IFSTATE_BLOCKING:
2330 case BSTP_IFSTATE_DISABLED:
2335 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2336 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2339 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2342 if (LIST_NEXT(bif, bif_next) == NULL) {
2346 mc = m_copypacket(m, MB_DONTWAIT);
2352 bridge_enqueue(dst_if, mc);
2361 * Send a frame to all interfaces that are members of
2362 * the bridge, except for the one on which the packet
2366 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2369 struct bridge_iflist *bif, *nbif;
2371 struct ifnet *dst_if, *bifp;
2375 ASSERT_NOT_SERIALIZED(bifp->if_serializer);
2377 if (inet_pfil_hook.ph_hashooks > 0
2379 || inet6_pfil_hook.ph_hashooks > 0
2382 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
2387 /* Filter on the bridge interface before broadcasting */
2388 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
2394 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
2395 dst_if = bif->bif_ifp;
2396 if (dst_if == src_if)
2399 if (bif->bif_flags & IFBIF_STP) {
2400 switch (bif->bif_state) {
2401 case BSTP_IFSTATE_BLOCKING:
2402 case BSTP_IFSTATE_DISABLED:
2407 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2408 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2411 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2414 if (LIST_NEXT(bif, bif_next) == NULL) {
2418 mc = m_copypacket(m, MB_DONTWAIT);
2420 sc->sc_ifp->if_oerrors++;
2426 * Filter on the output interface. Pass a NULL bridge
2427 * interface pointer so we do not redundantly filter on
2428 * the bridge for each interface we broadcast on.
2430 if (inet_pfil_hook.ph_hashooks > 0
2432 || inet6_pfil_hook.ph_hashooks > 0
2435 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2440 bridge_handoff(dst_if, mc);
2442 if (nbif != NULL && !nbif->bif_onlist) {
2443 KKASSERT(bif->bif_onlist);
2444 nbif = LIST_NEXT(bif, bif_next);
2454 * Duplicate a packet out one or more interfaces that are in span mode,
2455 * the original mbuf is unmodified.
2458 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2460 struct bridge_iflist *bif;
2461 struct ifnet *dst_if, *bifp;
2465 lwkt_serialize_enter(bifp->if_serializer);
2467 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2468 dst_if = bif->bif_ifp;
2470 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2473 mc = m_copypacket(m, MB_DONTWAIT);
2475 sc->sc_ifp->if_oerrors++;
2478 bridge_enqueue(dst_if, mc);
2481 lwkt_serialize_exit(bifp->if_serializer);
2485 bridge_rtmsg_sync_handler(struct netmsg *nmsg)
2487 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2491 bridge_rtmsg_sync(struct bridge_softc *sc)
2495 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2497 netmsg_init(&nmsg, &curthread->td_msgport, 0,
2498 bridge_rtmsg_sync_handler);
2499 ifnet_domsg(&nmsg.nm_lmsg, 0);
2502 static __inline void
2503 bridge_rtinfo_update(struct bridge_rtinfo *bri, struct ifnet *dst_if,
2504 int setflags, uint8_t flags, uint32_t timeo)
2506 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2507 bri->bri_ifp != dst_if)
2508 bri->bri_ifp = dst_if;
2509 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2510 bri->bri_expire != time_second + timeo)
2511 bri->bri_expire = time_second + timeo;
2513 bri->bri_flags = flags;
2517 bridge_rtinstall_oncpu(struct bridge_softc *sc, const uint8_t *dst,
2518 struct ifnet *dst_if, int setflags, uint8_t flags,
2519 struct bridge_rtinfo **bri0)
2521 struct bridge_rtnode *brt;
2522 struct bridge_rtinfo *bri;
2525 brt = bridge_rtnode_lookup(sc, dst);
2528 * rtnode for 'dst' already exists. We inform the
2529 * caller about this by leaving bri0 as NULL. The
2530 * caller will terminate the intallation upon getting
2531 * NULL bri0. However, we still need to update the
2534 KKASSERT(*bri0 == NULL);
2537 bridge_rtinfo_update(brt->brt_info, dst_if, setflags,
2538 flags, sc->sc_brttimeout);
2543 * We only need to check brtcnt on CPU0, since if limit
2544 * is to be exceeded, ENOSPC is returned. Caller knows
2545 * this and will terminate the installation.
2547 if (sc->sc_brtcnt >= sc->sc_brtmax)
2550 KKASSERT(*bri0 == NULL);
2551 bri = kmalloc(sizeof(struct bridge_rtinfo), M_DEVBUF,
2556 bri->bri_flags = IFBAF_DYNAMIC;
2557 bridge_rtinfo_update(bri, dst_if, setflags, flags,
2561 KKASSERT(bri != NULL);
2564 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2566 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2567 brt->brt_info = bri;
2569 bridge_rtnode_insert(sc, brt);
2574 bridge_rtinstall_handler(struct netmsg *nmsg)
2576 struct netmsg_brsaddr *brmsg = (struct netmsg_brsaddr *)nmsg;
2579 error = bridge_rtinstall_oncpu(brmsg->br_softc,
2580 brmsg->br_dst, brmsg->br_dst_if,
2581 brmsg->br_setflags, brmsg->br_flags,
2584 KKASSERT(mycpuid == 0 && brmsg->br_rtinfo == NULL);
2585 lwkt_replymsg(&nmsg->nm_lmsg, error);
2587 } else if (brmsg->br_rtinfo == NULL) {
2588 /* rtnode already exists for 'dst' */
2589 KKASSERT(mycpuid == 0);
2590 lwkt_replymsg(&nmsg->nm_lmsg, 0);
2593 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2599 * Add/Update a bridge routing entry.
2602 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2603 struct ifnet *dst_if, uint8_t flags)
2605 struct bridge_rtnode *brt;
2608 * A route for this destination might already exist. If so,
2609 * update it, otherwise create a new one.
2611 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2612 struct netmsg_brsaddr *brmsg;
2614 if (sc->sc_brtcnt >= sc->sc_brtmax)
2617 brmsg = kmalloc(sizeof(*brmsg), M_LWKTMSG, M_WAITOK | M_NULLOK);
2621 netmsg_init(&brmsg->br_nmsg, &netisr_afree_rport, 0,
2622 bridge_rtinstall_handler);
2623 memcpy(brmsg->br_dst, dst, ETHER_ADDR_LEN);
2624 brmsg->br_dst_if = dst_if;
2625 brmsg->br_flags = flags;
2626 brmsg->br_setflags = 0;
2627 brmsg->br_softc = sc;
2628 brmsg->br_rtinfo = NULL;
2630 ifnet_sendmsg(&brmsg->br_nmsg.nm_lmsg, 0);
2633 bridge_rtinfo_update(brt->brt_info, dst_if, 0, flags,
2639 bridge_rtsaddr(struct bridge_softc *sc, const uint8_t *dst,
2640 struct ifnet *dst_if, uint8_t flags)
2642 struct netmsg_brsaddr brmsg;
2644 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2646 netmsg_init(&brmsg.br_nmsg, &curthread->td_msgport, MSGF_PRIORITY,
2647 bridge_rtinstall_handler);
2648 memcpy(brmsg.br_dst, dst, ETHER_ADDR_LEN);
2649 brmsg.br_dst_if = dst_if;
2650 brmsg.br_flags = flags;
2651 brmsg.br_setflags = 1;
2652 brmsg.br_softc = sc;
2653 brmsg.br_rtinfo = NULL;
2655 return ifnet_domsg(&brmsg.br_nmsg.nm_lmsg, 0);
2661 * Lookup the destination interface for an address.
2663 static struct ifnet *
2664 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2666 struct bridge_rtnode *brt;
2668 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2670 return brt->brt_info->bri_ifp;
2674 bridge_rtreap_handler(struct netmsg *nmsg)
2676 struct bridge_softc *sc = nmsg->nm_lmsg.u.ms_resultp;
2677 struct bridge_rtnode *brt, *nbrt;
2679 LIST_FOREACH_MUTABLE(brt, &sc->sc_rtlists[mycpuid], brt_list, nbrt) {
2680 if (brt->brt_info->bri_dead)
2681 bridge_rtnode_destroy(sc, brt);
2683 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2687 bridge_rtreap(struct bridge_softc *sc)
2691 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2693 netmsg_init(&nmsg, &curthread->td_msgport, 0, bridge_rtreap_handler);
2694 nmsg.nm_lmsg.u.ms_resultp = sc;
2696 ifnet_domsg(&nmsg.nm_lmsg, 0);
2702 * Trim the routine table so that we have a number
2703 * of routing entries less than or equal to the
2707 bridge_rttrim(struct bridge_softc *sc)
2709 struct bridge_rtnode *brt;
2712 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2714 /* Make sure we actually need to do this. */
2715 if (sc->sc_brtcnt <= sc->sc_brtmax)
2719 * Find out how many rtnodes are dead
2721 dead = bridge_rtage_finddead(sc);
2722 KKASSERT(dead <= sc->sc_brtcnt);
2724 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
2725 /* Enough dead rtnodes are found */
2731 * Kill some dynamic rtnodes to meet the brtmax
2733 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2734 struct bridge_rtinfo *bri = brt->brt_info;
2736 if (bri->bri_dead) {
2738 * We have counted this rtnode in
2739 * bridge_rtage_finddead()
2744 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2747 KKASSERT(dead <= sc->sc_brtcnt);
2749 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
2750 /* Enough rtnodes are collected */
2762 * Aging timer for the bridge.
2765 bridge_timer(void *arg)
2767 struct bridge_softc *sc = arg;
2768 struct lwkt_msg *lmsg;
2770 KKASSERT(mycpuid == BRIDGE_CFGCPU);
2774 if (callout_pending(&sc->sc_brcallout) ||
2775 !callout_active(&sc->sc_brcallout)) {
2779 callout_deactivate(&sc->sc_brcallout);
2781 lmsg = &sc->sc_brtimemsg.nm_lmsg;
2782 KKASSERT(lmsg->ms_flags & MSGF_DONE);
2783 lwkt_sendmsg(BRIDGE_CFGPORT, lmsg);
2789 bridge_timer_handler(struct netmsg *nmsg)
2791 struct bridge_softc *sc = nmsg->nm_lmsg.u.ms_resultp;
2793 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
2797 lwkt_replymsg(&nmsg->nm_lmsg, 0);
2801 if (sc->sc_ifp->if_flags & IFF_RUNNING) {
2802 callout_reset(&sc->sc_brcallout,
2803 bridge_rtable_prune_period * hz, bridge_timer, sc);
2808 bridge_rtage_finddead(struct bridge_softc *sc)
2810 struct bridge_rtnode *brt;
2813 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2814 struct bridge_rtinfo *bri = brt->brt_info;
2816 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2817 time_second >= bri->bri_expire) {
2820 KKASSERT(dead <= sc->sc_brtcnt);
2829 * Perform an aging cycle.
2832 bridge_rtage(struct bridge_softc *sc)
2834 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2836 if (bridge_rtage_finddead(sc))
2843 * Remove all dynamic addresses from the bridge.
2846 bridge_rtflush(struct bridge_softc *sc, int full)
2848 struct bridge_rtnode *brt;
2851 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2854 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2855 struct bridge_rtinfo *bri = brt->brt_info;
2858 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2870 * Remove an address from the table.
2873 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2875 struct bridge_rtnode *brt;
2877 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2879 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2882 /* TODO: add a cheaper delete operation */
2883 brt->brt_info->bri_dead = 1;
2891 * Delete routes to a speicifc member interface.
2894 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full)
2896 struct bridge_rtnode *brt;
2899 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
2902 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2903 struct bridge_rtinfo *bri = brt->brt_info;
2905 if (bri->bri_ifp == ifp &&
2907 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) {
2917 * bridge_rtable_init:
2919 * Initialize the route table for this bridge.
2922 bridge_rtable_init(struct bridge_softc *sc)
2927 * Initialize per-cpu hash tables
2929 sc->sc_rthashs = kmalloc(sizeof(*sc->sc_rthashs) * ncpus,
2930 M_DEVBUF, M_WAITOK);
2931 for (cpu = 0; cpu < ncpus; ++cpu) {
2934 sc->sc_rthashs[cpu] =
2935 kmalloc(sizeof(struct bridge_rtnode_head) * BRIDGE_RTHASH_SIZE,
2936 M_DEVBUF, M_WAITOK);
2938 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
2939 LIST_INIT(&sc->sc_rthashs[cpu][i]);
2941 sc->sc_rthash_key = karc4random();
2944 * Initialize per-cpu lists
2946 sc->sc_rtlists = kmalloc(sizeof(struct bridge_rtnode_head) * ncpus,
2947 M_DEVBUF, M_WAITOK);
2948 for (cpu = 0; cpu < ncpus; ++cpu)
2949 LIST_INIT(&sc->sc_rtlists[cpu]);
2953 * bridge_rtable_fini:
2955 * Deconstruct the route table for this bridge.
2958 bridge_rtable_fini(struct bridge_softc *sc)
2963 * Free per-cpu hash tables
2965 for (cpu = 0; cpu < ncpus; ++cpu)
2966 kfree(sc->sc_rthashs[cpu], M_DEVBUF);
2967 kfree(sc->sc_rthashs, M_DEVBUF);
2970 * Free per-cpu lists
2972 kfree(sc->sc_rtlists, M_DEVBUF);
2976 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2977 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2979 #define mix(a, b, c) \
2981 a -= b; a -= c; a ^= (c >> 13); \
2982 b -= c; b -= a; b ^= (a << 8); \
2983 c -= a; c -= b; c ^= (b >> 13); \
2984 a -= b; a -= c; a ^= (c >> 12); \
2985 b -= c; b -= a; b ^= (a << 16); \
2986 c -= a; c -= b; c ^= (b >> 5); \
2987 a -= b; a -= c; a ^= (c >> 3); \
2988 b -= c; b -= a; b ^= (a << 10); \
2989 c -= a; c -= b; c ^= (b >> 15); \
2990 } while (/*CONSTCOND*/0)
2992 static __inline uint32_t
2993 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
2995 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
3006 return (c & BRIDGE_RTHASH_MASK);
3012 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
3016 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
3017 d = ((int)a[i]) - ((int)b[i]);
3024 * bridge_rtnode_lookup:
3026 * Look up a bridge route node for the specified destination.
3028 static struct bridge_rtnode *
3029 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
3031 struct bridge_rtnode *brt;
3035 hash = bridge_rthash(sc, addr);
3036 LIST_FOREACH(brt, &sc->sc_rthashs[mycpuid][hash], brt_hash) {
3037 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
3048 * bridge_rtnode_insert:
3050 * Insert the specified bridge node into the route table.
3051 * Caller has to make sure that rtnode does not exist.
3054 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
3056 struct bridge_rtnode *lbrt;
3060 hash = bridge_rthash(sc, brt->brt_addr);
3062 lbrt = LIST_FIRST(&sc->sc_rthashs[mycpuid][hash]);
3064 LIST_INSERT_HEAD(&sc->sc_rthashs[mycpuid][hash], brt, brt_hash);
3069 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
3070 KASSERT(dir != 0, ("rtnode already exist\n"));
3073 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
3076 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
3077 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
3080 lbrt = LIST_NEXT(lbrt, brt_hash);
3081 } while (lbrt != NULL);
3083 panic("no suitable position found for rtnode\n");
3085 LIST_INSERT_HEAD(&sc->sc_rtlists[mycpuid], brt, brt_list);
3088 * Update the brtcnt.
3089 * We only need to do it once and we do it on CPU0.
3096 * bridge_rtnode_destroy:
3098 * Destroy a bridge rtnode.
3101 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3103 LIST_REMOVE(brt, brt_hash);
3104 LIST_REMOVE(brt, brt_list);
3106 if (mycpuid + 1 == ncpus) {
3107 /* Free rtinfo associated with rtnode on the last cpu */
3108 kfree(brt->brt_info, M_DEVBUF);
3110 kfree(brt, M_DEVBUF);
3113 /* Update brtcnt only on CPU0 */
3119 bridge_post_pfil(struct mbuf *m)
3121 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED)
3125 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED)
3132 * Send bridge packets through pfil if they are one of the types pfil can deal
3133 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
3134 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3138 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3140 int snap, error, i, hlen;
3141 struct ether_header *eh1, eh2;
3144 u_int16_t ether_type;
3147 error = -1; /* Default error if not error == 0 */
3149 if (pfil_bridge == 0 && pfil_member == 0)
3150 return (0); /* filtering is disabled */
3152 i = min((*mp)->m_pkthdr.len, max_protohdr);
3153 if ((*mp)->m_len < i) {
3154 *mp = m_pullup(*mp, i);
3156 kprintf("%s: m_pullup failed\n", __func__);
3161 eh1 = mtod(*mp, struct ether_header *);
3162 ether_type = ntohs(eh1->ether_type);
3165 * Check for SNAP/LLC.
3167 if (ether_type < ETHERMTU) {
3168 struct llc *llc2 = (struct llc *)(eh1 + 1);
3170 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3171 llc2->llc_dsap == LLC_SNAP_LSAP &&
3172 llc2->llc_ssap == LLC_SNAP_LSAP &&
3173 llc2->llc_control == LLC_UI) {
3174 ether_type = htons(llc2->llc_un.type_snap.ether_type);
3180 * If we're trying to filter bridge traffic, don't look at anything
3181 * other than IP and ARP traffic. If the filter doesn't understand
3182 * IPv6, don't allow IPv6 through the bridge either. This is lame
3183 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3184 * but of course we don't have an AppleTalk filter to begin with.
3185 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3188 switch (ether_type) {
3190 case ETHERTYPE_REVARP:
3191 return (0); /* Automatically pass */
3195 case ETHERTYPE_IPV6:
3201 * Check to see if the user wants to pass non-ip
3202 * packets, these will not be checked by pfil(9)
3203 * and passed unconditionally so the default is to drop.
3209 /* Strip off the Ethernet header and keep a copy. */
3210 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3211 m_adj(*mp, ETHER_HDR_LEN);
3213 /* Strip off snap header, if present */
3215 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3216 m_adj(*mp, sizeof(struct llc));
3220 * Check the IP header for alignment and errors
3222 if (dir == PFIL_IN) {
3223 switch (ether_type) {
3225 error = bridge_ip_checkbasic(mp);
3228 case ETHERTYPE_IPV6:
3229 error = bridge_ip6_checkbasic(mp);
3242 * Run the packet through pfil
3244 switch (ether_type) {
3247 * before calling the firewall, swap fields the same as
3248 * IP does. here we assume the header is contiguous
3250 ip = mtod(*mp, struct ip *);
3252 ip->ip_len = ntohs(ip->ip_len);
3253 ip->ip_off = ntohs(ip->ip_off);
3256 * Run pfil on the member interface and the bridge, both can
3257 * be skipped by clearing pfil_member or pfil_bridge.
3260 * in_if -> bridge_if -> out_if
3262 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) {
3263 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3264 if (*mp == NULL || error != 0) /* filter may consume */
3266 error = bridge_post_pfil(*mp);
3271 if (pfil_member && ifp != NULL) {
3272 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp, dir);
3273 if (*mp == NULL || error != 0) /* filter may consume */
3275 error = bridge_post_pfil(*mp);
3280 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) {
3281 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3282 if (*mp == NULL || error != 0) /* filter may consume */
3284 error = bridge_post_pfil(*mp);
3289 /* check if we need to fragment the packet */
3290 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
3291 i = (*mp)->m_pkthdr.len;
3292 if (i > ifp->if_mtu) {
3293 error = bridge_fragment(ifp, *mp, &eh2, snap,
3299 /* Recalculate the ip checksum and restore byte ordering */
3300 ip = mtod(*mp, struct ip *);
3301 hlen = ip->ip_hl << 2;
3302 if (hlen < sizeof(struct ip))
3304 if (hlen > (*mp)->m_len) {
3305 if ((*mp = m_pullup(*mp, hlen)) == 0)
3307 ip = mtod(*mp, struct ip *);
3311 ip->ip_len = htons(ip->ip_len);
3312 ip->ip_off = htons(ip->ip_off);
3314 if (hlen == sizeof(struct ip))
3315 ip->ip_sum = in_cksum_hdr(ip);
3317 ip->ip_sum = in_cksum(*mp, hlen);
3321 case ETHERTYPE_IPV6:
3322 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3323 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3326 if (*mp == NULL || error != 0) /* filter may consume */
3329 if (pfil_member && ifp != NULL)
3330 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
3333 if (*mp == NULL || error != 0) /* filter may consume */
3336 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3337 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3354 * Finally, put everything back the way it was and return
3357 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
3360 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
3363 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
3366 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3377 * Perform basic checks on header size since
3378 * pfil assumes ip_input has already processed
3379 * it for it. Cut-and-pasted from ip_input.c.
3380 * Given how simple the IPv6 version is,
3381 * does the IPv4 version really need to be
3384 * XXX Should we update ipstat here, or not?
3385 * XXX Right now we update ipstat but not
3389 bridge_ip_checkbasic(struct mbuf **mp)
3391 struct mbuf *m = *mp;
3399 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3400 if ((m = m_copyup(m, sizeof(struct ip),
3401 (max_linkhdr + 3) & ~3)) == NULL) {
3402 /* XXXJRT new stat, please */
3403 ipstat.ips_toosmall++;
3408 #ifndef __predict_false
3409 #define __predict_false(x) x
3411 if (__predict_false(m->m_len < sizeof (struct ip))) {
3412 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3413 ipstat.ips_toosmall++;
3417 ip = mtod(m, struct ip *);
3418 if (ip == NULL) goto bad;
3420 if (ip->ip_v != IPVERSION) {
3421 ipstat.ips_badvers++;
3424 hlen = ip->ip_hl << 2;
3425 if (hlen < sizeof(struct ip)) { /* minimum header length */
3426 ipstat.ips_badhlen++;
3429 if (hlen > m->m_len) {
3430 if ((m = m_pullup(m, hlen)) == 0) {
3431 ipstat.ips_badhlen++;
3434 ip = mtod(m, struct ip *);
3435 if (ip == NULL) goto bad;
3438 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3439 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3441 if (hlen == sizeof(struct ip)) {
3442 sum = in_cksum_hdr(ip);
3444 sum = in_cksum(m, hlen);
3448 ipstat.ips_badsum++;
3452 /* Retrieve the packet length. */
3453 len = ntohs(ip->ip_len);
3456 * Check for additional length bogosity
3459 ipstat.ips_badlen++;
3464 * Check that the amount of data in the buffers
3465 * is as at least much as the IP header would have us expect.
3466 * Drop packet if shorter than we expect.
3468 if (m->m_pkthdr.len < len) {
3469 ipstat.ips_tooshort++;
3473 /* Checks out, proceed */
3484 * Same as above, but for IPv6.
3485 * Cut-and-pasted from ip6_input.c.
3486 * XXX Should we update ip6stat, or not?
3489 bridge_ip6_checkbasic(struct mbuf **mp)
3491 struct mbuf *m = *mp;
3492 struct ip6_hdr *ip6;
3495 * If the IPv6 header is not aligned, slurp it up into a new
3496 * mbuf with space for link headers, in the event we forward
3497 * it. Otherwise, if it is aligned, make sure the entire base
3498 * IPv6 header is in the first mbuf of the chain.
3501 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3502 struct ifnet *inifp = m->m_pkthdr.rcvif;
3503 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3504 (max_linkhdr + 3) & ~3)) == NULL) {
3505 /* XXXJRT new stat, please */
3506 ip6stat.ip6s_toosmall++;
3507 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3512 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3513 struct ifnet *inifp = m->m_pkthdr.rcvif;
3514 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3515 ip6stat.ip6s_toosmall++;
3516 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3521 ip6 = mtod(m, struct ip6_hdr *);
3523 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3524 ip6stat.ip6s_badvers++;
3525 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3529 /* Checks out, proceed */
3542 * Return a fragmented mbuf chain.
3545 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
3546 int snap, struct llc *llc)
3552 if (m->m_len < sizeof(struct ip) &&
3553 (m = m_pullup(m, sizeof(struct ip))) == NULL)
3555 ip = mtod(m, struct ip *);
3557 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
3562 /* walk the chain and re-add the Ethernet header */
3563 for (m0 = m; m0; m0 = m0->m_nextpkt) {
3566 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
3571 bcopy(llc, mtod(m0, caddr_t),
3572 sizeof(struct llc));
3574 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
3579 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
3585 ipstat.ips_fragmented++;
3596 bridge_enqueue_handler(struct netmsg *nmsg)
3598 struct netmsg_packet *nmp;
3599 struct ifnet *dst_ifp;
3602 nmp = (struct netmsg_packet *)nmsg;
3604 dst_ifp = nmp->nm_netmsg.nm_lmsg.u.ms_resultp;
3606 bridge_handoff(dst_ifp, m);
3610 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m)
3614 /* We may be sending a fragment so traverse the mbuf */
3616 struct altq_pktattr pktattr;
3619 m->m_nextpkt = NULL;
3621 if (ifq_is_enabled(&dst_ifp->if_snd))
3622 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
3624 ifq_dispatch(dst_ifp, m, &pktattr);
3629 bridge_control_dispatch(struct netmsg *nmsg)
3631 struct netmsg_brctl *bc_msg = (struct netmsg_brctl *)nmsg;
3632 struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
3635 lwkt_serialize_enter(bifp->if_serializer);
3636 error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
3637 lwkt_serialize_exit(bifp->if_serializer);
3639 lwkt_replymsg(&nmsg->nm_lmsg, error);
3643 bridge_control(struct bridge_softc *sc, u_long cmd,
3644 bridge_ctl_t bc_func, void *bc_arg)
3646 struct ifnet *bifp = sc->sc_ifp;
3647 struct netmsg_brctl bc_msg;
3648 struct netmsg *nmsg;
3651 ASSERT_SERIALIZED(bifp->if_serializer);
3653 bzero(&bc_msg, sizeof(bc_msg));
3654 nmsg = &bc_msg.bc_nmsg;
3656 netmsg_init(nmsg, &curthread->td_msgport, 0, bridge_control_dispatch);
3657 bc_msg.bc_func = bc_func;
3659 bc_msg.bc_arg = bc_arg;
3661 lwkt_serialize_exit(bifp->if_serializer);
3662 error = lwkt_domsg(BRIDGE_CFGPORT, &nmsg->nm_lmsg, 0);
3663 lwkt_serialize_enter(bifp->if_serializer);
3668 bridge_add_bif_handler(struct netmsg *nmsg)
3670 struct netmsg_braddbif *amsg = (struct netmsg_braddbif *)nmsg;
3671 struct bridge_softc *sc;
3672 struct bridge_iflist *bif;
3674 sc = amsg->br_softc;
3676 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
3677 bif->bif_ifp = amsg->br_bif_ifp;
3678 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
3679 bif->bif_onlist = 1;
3680 bif->bif_info = amsg->br_bif_info;
3682 LIST_INSERT_HEAD(&sc->sc_iflists[mycpuid], bif, bif_next);
3684 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3688 bridge_add_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3691 struct netmsg_braddbif amsg;
3693 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
3695 netmsg_init(&amsg.br_nmsg, &curthread->td_msgport, 0,
3696 bridge_add_bif_handler);
3698 amsg.br_bif_info = bif_info;
3699 amsg.br_bif_ifp = ifp;
3701 ifnet_domsg(&amsg.br_nmsg.nm_lmsg, 0);
3705 bridge_del_bif_handler(struct netmsg *nmsg)
3707 struct netmsg_brdelbif *dmsg = (struct netmsg_brdelbif *)nmsg;
3708 struct bridge_softc *sc;
3709 struct bridge_iflist *bif;
3711 sc = dmsg->br_softc;
3714 * Locate the bif associated with the br_bif_info
3715 * on the current CPU
3717 bif = bridge_lookup_member_ifinfo(sc, dmsg->br_bif_info);
3718 KKASSERT(bif != NULL && bif->bif_onlist);
3720 /* Remove the bif from the current CPU's iflist */
3721 bif->bif_onlist = 0;
3722 LIST_REMOVE(bif, bif_next);
3724 /* Save the removed bif for later freeing */
3725 LIST_INSERT_HEAD(dmsg->br_bif_list, bif, bif_next);
3727 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3731 bridge_del_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3732 struct bridge_iflist_head *saved_bifs)
3734 struct netmsg_brdelbif dmsg;
3736 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
3738 netmsg_init(&dmsg.br_nmsg, &curthread->td_msgport, 0,
3739 bridge_del_bif_handler);
3741 dmsg.br_bif_info = bif_info;
3742 dmsg.br_bif_list = saved_bifs;
3744 ifnet_domsg(&dmsg.br_nmsg.nm_lmsg, 0);
3748 bridge_set_bifflags_handler(struct netmsg *nmsg)
3750 struct netmsg_brsflags *smsg = (struct netmsg_brsflags *)nmsg;
3751 struct bridge_softc *sc;
3752 struct bridge_iflist *bif;
3754 sc = smsg->br_softc;
3757 * Locate the bif associated with the br_bif_info
3758 * on the current CPU
3760 bif = bridge_lookup_member_ifinfo(sc, smsg->br_bif_info);
3761 KKASSERT(bif != NULL && bif->bif_onlist);
3763 bif->bif_flags = smsg->br_bif_flags;
3765 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3769 bridge_set_bifflags(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3772 struct netmsg_brsflags smsg;
3774 ASSERT_NOT_SERIALIZED(sc->sc_ifp->if_serializer);
3776 netmsg_init(&smsg.br_nmsg, &curthread->td_msgport, 0,
3777 bridge_set_bifflags_handler);
3779 smsg.br_bif_info = bif_info;
3780 smsg.br_bif_flags = bif_flags;
3782 ifnet_domsg(&smsg.br_nmsg.nm_lmsg, 0);