2 * Copyright 2001 Wasabi Systems, Inc.
5 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Wasabi Systems, Inc.
19 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
20 * or promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by Jason L. Wright
51 * 4. The name of the author may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
56 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
57 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
63 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
66 * $OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp $
67 * $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $
68 * $FreeBSD: src/sys/net/if_bridge.c,v 1.26 2005/10/13 23:05:55 thompsa Exp $
72 * Network interface bridge support.
76 * - Currently only supports Ethernet-like interfaces (Ethernet,
77 * 802.11, VLANs on Ethernet, etc.) Figure out a nice way
78 * to bridge other types of interfaces (FDDI-FDDI, and maybe
79 * consider heterogenous bridges).
82 * Bridge's route information is duplicated to each CPUs:
85 * +-----------+ +-----------+ +-----------+ +-----------+
86 * | rtnode | | rtnode | | rtnode | | rtnode |
88 * | dst eaddr | | dst eaddr | | dst eaddr | | dst eaddr |
89 * +-----------+ +-----------+ +-----------+ +-----------+
92 * | | +----------+ | |
96 * +-------------->| timeout |<-------------+
100 * We choose to put timeout and dst_ifp into shared part, so updating
101 * them will be cheaper than using message forwarding. Also there is
102 * not need to use spinlock to protect the updating: timeout and dst_ifp
103 * is not related and specific field's updating order has no importance.
104 * The cache pollution by the share part should not be heavy: in a stable
105 * setup, dst_ifp probably will be not changed in rtnode's life time,
106 * while timeout is refreshed once per second; most of the time, timeout
107 * and dst_ifp are read-only accessed.
110 * Bridge route information installation on bridge_input path:
112 * CPU0 CPU1 CPU2 CPU3
119 * ifnet0<-----------------------+
122 * rtnode exists?(Y)free nmsg :
153 * The netmsgs forwarded between protocol threads and ifnet threads are
154 * allocated with (M_WAITOK|M_NULLOK), so it will not fail under most
155 * cases (route information is too precious to be not installed :).
156 * Since multiple threads may try to install route information for the
157 * same dst eaddr, we look up route information in ifnet0. However, this
158 * looking up only need to be performed on ifnet0, which is the start
159 * point of the route information installation process.
162 * Bridge route information deleting/flushing:
164 * CPU0 CPU1 CPU2 CPU3
168 * find suitable rtnodes,
169 * mark their rtinfo dead
171 * | domsg <------------------------------------------+
174 * V fwdmsg fwdmsg fwdmsg |
175 * ifnet0 --------> ifnet1 --------> ifnet2 --------> ifnet3
176 * delete rtnodes delete rtnodes delete rtnodes delete rtnodes
177 * w/ dead rtinfo w/ dead rtinfo w/ dead rtinfo w/ dead rtinfo
180 * All deleting/flushing operations are serialized by netisr0, so each
181 * operation only reaps the route information marked dead by itself.
184 * Bridge route information adding/deleting/flushing:
185 * Since all operation is serialized by the fixed message flow between
186 * ifnet threads, it is not possible to create corrupted per-cpu route
191 * Percpu member interface list iteration with blocking operation:
192 * Since one bridge could only delete one member interface at a time and
193 * the deleted member interface is not freed after netmsg_service_sync(),
194 * following way is used to make sure that even if the certain member
195 * interface is ripped from the percpu list during the blocking operation,
196 * the iteration still could keep going:
198 * LIST_FOREACH_MUTABLE(bif, sc->sc_iflists[mycpuid], bif_next, nbif) {
199 * blocking operation;
200 * blocking operation;
203 * if (nbif != NULL && !nbif->bif_onlist) {
204 * KKASSERT(bif->bif_onlist);
205 * nbif = LIST_NEXT(bif, bif_next);
209 * As mentioned above only one member interface could be unlinked from the
210 * percpu member interface list, so either bif or nbif may be not on the list,
211 * but _not_ both. To keep the list iteration, we don't care about bif, but
212 * only nbif. Since removed member interface will only be freed after we
213 * finish our work, it is safe to access any field in an unlinked bif (here
214 * bif_onlist). If nbif is no longer on the list, then bif must be on the
215 * list, so we change nbif to the next element of bif and keep going.
218 #include "opt_inet.h"
219 #include "opt_inet6.h"
221 #include <sys/param.h>
222 #include <sys/mbuf.h>
223 #include <sys/malloc.h>
224 #include <sys/protosw.h>
225 #include <sys/systm.h>
226 #include <sys/time.h>
227 #include <sys/socket.h> /* for net/if.h */
228 #include <sys/sockio.h>
229 #include <sys/ctype.h> /* string functions */
230 #include <sys/kernel.h>
231 #include <sys/random.h>
232 #include <sys/sysctl.h>
233 #include <sys/module.h>
234 #include <sys/proc.h>
235 #include <sys/priv.h>
236 #include <sys/lock.h>
237 #include <sys/thread.h>
238 #include <sys/thread2.h>
239 #include <sys/mpipe.h>
243 #include <net/if_dl.h>
244 #include <net/if_types.h>
245 #include <net/if_var.h>
246 #include <net/pfil.h>
247 #include <net/ifq_var.h>
248 #include <net/if_clone.h>
250 #include <netinet/in.h> /* for struct arpcom */
251 #include <netinet/in_systm.h>
252 #include <netinet/in_var.h>
253 #include <netinet/ip.h>
254 #include <netinet/ip_var.h>
256 #include <netinet/ip6.h>
257 #include <netinet6/ip6_var.h>
259 #include <netinet/if_ether.h> /* for struct arpcom */
260 #include <net/bridge/if_bridgevar.h>
261 #include <net/if_llc.h>
262 #include <net/netmsg2.h>
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_base base;
317 bridge_ctl_t bc_func;
318 struct bridge_softc *bc_sc;
322 struct netmsg_brsaddr {
323 struct netmsg_base base;
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_base base;
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_base base;
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_base base;
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, caddr_t);
362 static int 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(netmsg_t);
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_rtreap_async(struct bridge_softc *);
388 static void bridge_rttrim(struct bridge_softc *);
389 static int bridge_rtage_finddead(struct bridge_softc *);
390 static void bridge_rtage(struct bridge_softc *);
391 static void bridge_rtflush(struct bridge_softc *, int);
392 static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *);
393 static int bridge_rtsaddr(struct bridge_softc *, const uint8_t *,
394 struct ifnet *, uint8_t);
395 static void bridge_rtmsg_sync(struct bridge_softc *sc);
396 static void bridge_rtreap_handler(netmsg_t);
397 static void bridge_rtinstall_handler(netmsg_t);
398 static int bridge_rtinstall_oncpu(struct bridge_softc *, const uint8_t *,
399 struct ifnet *, int, uint8_t, struct bridge_rtinfo **);
401 static void bridge_rtable_init(struct bridge_softc *);
402 static void bridge_rtable_fini(struct bridge_softc *);
404 static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *);
405 static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *,
407 static void bridge_rtnode_insert(struct bridge_softc *,
408 struct bridge_rtnode *);
409 static void bridge_rtnode_destroy(struct bridge_softc *,
410 struct bridge_rtnode *);
412 static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *,
414 static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *,
416 static struct bridge_iflist *bridge_lookup_member_ifinfo(struct bridge_softc *,
417 struct bridge_ifinfo *);
418 static void bridge_delete_member(struct bridge_softc *,
419 struct bridge_iflist *, int);
420 static void bridge_delete_span(struct bridge_softc *,
421 struct bridge_iflist *);
423 static int bridge_control(struct bridge_softc *, u_long,
424 bridge_ctl_t, void *);
425 static int bridge_ioctl_init(struct bridge_softc *, void *);
426 static int bridge_ioctl_stop(struct bridge_softc *, void *);
427 static int bridge_ioctl_add(struct bridge_softc *, void *);
428 static int bridge_ioctl_del(struct bridge_softc *, void *);
429 static int bridge_ioctl_gifflags(struct bridge_softc *, void *);
430 static int bridge_ioctl_sifflags(struct bridge_softc *, void *);
431 static int bridge_ioctl_scache(struct bridge_softc *, void *);
432 static int bridge_ioctl_gcache(struct bridge_softc *, void *);
433 static int bridge_ioctl_gifs(struct bridge_softc *, void *);
434 static int bridge_ioctl_rts(struct bridge_softc *, void *);
435 static int bridge_ioctl_saddr(struct bridge_softc *, void *);
436 static int bridge_ioctl_sto(struct bridge_softc *, void *);
437 static int bridge_ioctl_gto(struct bridge_softc *, void *);
438 static int bridge_ioctl_daddr(struct bridge_softc *, void *);
439 static int bridge_ioctl_flush(struct bridge_softc *, void *);
440 static int bridge_ioctl_gpri(struct bridge_softc *, void *);
441 static int bridge_ioctl_spri(struct bridge_softc *, void *);
442 static int bridge_ioctl_ght(struct bridge_softc *, void *);
443 static int bridge_ioctl_sht(struct bridge_softc *, void *);
444 static int bridge_ioctl_gfd(struct bridge_softc *, void *);
445 static int bridge_ioctl_sfd(struct bridge_softc *, void *);
446 static int bridge_ioctl_gma(struct bridge_softc *, void *);
447 static int bridge_ioctl_sma(struct bridge_softc *, void *);
448 static int bridge_ioctl_sifprio(struct bridge_softc *, void *);
449 static int bridge_ioctl_sifcost(struct bridge_softc *, void *);
450 static int bridge_ioctl_addspan(struct bridge_softc *, void *);
451 static int bridge_ioctl_delspan(struct bridge_softc *, void *);
452 static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *,
454 static int bridge_ip_checkbasic(struct mbuf **mp);
456 static int bridge_ip6_checkbasic(struct mbuf **mp);
458 static int bridge_fragment(struct ifnet *, struct mbuf *,
459 struct ether_header *, int, struct llc *);
460 static void bridge_enqueue_handler(netmsg_t);
461 static void bridge_handoff(struct ifnet *, struct mbuf *, int);
463 static void bridge_del_bif_handler(netmsg_t);
464 static void bridge_add_bif_handler(netmsg_t);
465 static void bridge_set_bifflags_handler(netmsg_t);
466 static void bridge_del_bif(struct bridge_softc *, struct bridge_ifinfo *,
467 struct bridge_iflist_head *);
468 static void bridge_add_bif(struct bridge_softc *, struct bridge_ifinfo *,
470 static void bridge_set_bifflags(struct bridge_softc *,
471 struct bridge_ifinfo *, uint32_t);
473 SYSCTL_DECL(_net_link);
474 SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW, 0, "Bridge");
476 static int pfil_onlyip = 1; /* only pass IP[46] packets when pfil is enabled */
477 static int pfil_bridge = 1; /* run pfil hooks on the bridge interface */
478 static int pfil_member = 1; /* run pfil hooks on the member interface */
479 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_onlyip, CTLFLAG_RW,
480 &pfil_onlyip, 0, "Only pass IP packets when pfil is enabled");
481 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_bridge, CTLFLAG_RW,
482 &pfil_bridge, 0, "Packet filter on the bridge interface");
483 SYSCTL_INT(_net_link_bridge, OID_AUTO, pfil_member, CTLFLAG_RW,
484 &pfil_member, 0, "Packet filter on the member interface");
486 struct bridge_control_arg {
488 struct ifbreq ifbreq;
489 struct ifbifconf ifbifconf;
490 struct ifbareq ifbareq;
491 struct ifbaconf ifbaconf;
492 struct ifbrparam ifbrparam;
499 struct bridge_control {
500 bridge_ctl_t bc_func;
505 #define BC_F_COPYIN 0x01 /* copy arguments in */
506 #define BC_F_COPYOUT 0x02 /* copy arguments out */
507 #define BC_F_SUSER 0x04 /* do super-user check */
509 const struct bridge_control bridge_control_table[] = {
510 { bridge_ioctl_add, sizeof(struct ifbreq),
511 BC_F_COPYIN|BC_F_SUSER },
512 { bridge_ioctl_del, sizeof(struct ifbreq),
513 BC_F_COPYIN|BC_F_SUSER },
515 { bridge_ioctl_gifflags, sizeof(struct ifbreq),
516 BC_F_COPYIN|BC_F_COPYOUT },
517 { bridge_ioctl_sifflags, sizeof(struct ifbreq),
518 BC_F_COPYIN|BC_F_SUSER },
520 { bridge_ioctl_scache, sizeof(struct ifbrparam),
521 BC_F_COPYIN|BC_F_SUSER },
522 { bridge_ioctl_gcache, sizeof(struct ifbrparam),
525 { bridge_ioctl_gifs, sizeof(struct ifbifconf),
526 BC_F_COPYIN|BC_F_COPYOUT },
527 { bridge_ioctl_rts, sizeof(struct ifbaconf),
528 BC_F_COPYIN|BC_F_COPYOUT },
530 { bridge_ioctl_saddr, sizeof(struct ifbareq),
531 BC_F_COPYIN|BC_F_SUSER },
533 { bridge_ioctl_sto, sizeof(struct ifbrparam),
534 BC_F_COPYIN|BC_F_SUSER },
535 { bridge_ioctl_gto, sizeof(struct ifbrparam),
538 { bridge_ioctl_daddr, sizeof(struct ifbareq),
539 BC_F_COPYIN|BC_F_SUSER },
541 { bridge_ioctl_flush, sizeof(struct ifbreq),
542 BC_F_COPYIN|BC_F_SUSER },
544 { bridge_ioctl_gpri, sizeof(struct ifbrparam),
546 { bridge_ioctl_spri, sizeof(struct ifbrparam),
547 BC_F_COPYIN|BC_F_SUSER },
549 { bridge_ioctl_ght, sizeof(struct ifbrparam),
551 { bridge_ioctl_sht, sizeof(struct ifbrparam),
552 BC_F_COPYIN|BC_F_SUSER },
554 { bridge_ioctl_gfd, sizeof(struct ifbrparam),
556 { bridge_ioctl_sfd, sizeof(struct ifbrparam),
557 BC_F_COPYIN|BC_F_SUSER },
559 { bridge_ioctl_gma, sizeof(struct ifbrparam),
561 { bridge_ioctl_sma, sizeof(struct ifbrparam),
562 BC_F_COPYIN|BC_F_SUSER },
564 { bridge_ioctl_sifprio, sizeof(struct ifbreq),
565 BC_F_COPYIN|BC_F_SUSER },
567 { bridge_ioctl_sifcost, sizeof(struct ifbreq),
568 BC_F_COPYIN|BC_F_SUSER },
570 { bridge_ioctl_addspan, sizeof(struct ifbreq),
571 BC_F_COPYIN|BC_F_SUSER },
572 { bridge_ioctl_delspan, sizeof(struct ifbreq),
573 BC_F_COPYIN|BC_F_SUSER },
575 static const int bridge_control_table_size = NELEM(bridge_control_table);
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, caddr_t param __unused)
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, NULL, &netisr_adone_rport,
655 MSGF_DROPABLE, bridge_timer_handler);
656 sc->sc_brtimemsg.lmsg.u.ms_resultp = sc;
658 callout_init(&sc->sc_bstpcallout);
659 netmsg_init(&sc->sc_bstptimemsg, NULL, &netisr_adone_rport,
660 MSGF_DROPABLE, bstp_tick_handler);
661 sc->sc_bstptimemsg.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 ifq_set_ready(&ifp->if_snd);
681 ifp->if_hdrlen = ETHER_HDR_LEN;
684 * Generate a random ethernet address and use the private AC:DE:48
688 bcopy(&rnd, &eaddr[0], 4); /* ETHER_ADDR_LEN == 6 */
690 bcopy(&rnd, &eaddr[2], 4); /* ETHER_ADDR_LEN == 6 */
692 eaddr[0] &= ~1; /* clear multicast bit */
693 eaddr[0] |= 2; /* set the LAA bit */
695 ether_ifattach(ifp, eaddr, NULL);
696 /* Now undo some of the damage... */
697 ifp->if_baudrate = 0;
698 ifp->if_type = IFT_BRIDGE;
700 crit_enter(); /* XXX MP */
701 LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
708 bridge_delete_dispatch(netmsg_t msg)
710 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
711 struct ifnet *bifp = sc->sc_ifp;
712 struct bridge_iflist *bif;
714 ifnet_serialize_all(bifp);
716 while ((bif = LIST_FIRST(&sc->sc_iflists[mycpuid])) != NULL)
717 bridge_delete_member(sc, bif, 0);
719 while ((bif = LIST_FIRST(&sc->sc_spanlist)) != NULL)
720 bridge_delete_span(sc, bif);
722 ifnet_deserialize_all(bifp);
724 lwkt_replymsg(&msg->lmsg, 0);
728 * bridge_clone_destroy:
730 * Destroy a bridge instance.
733 bridge_clone_destroy(struct ifnet *ifp)
735 struct bridge_softc *sc = ifp->if_softc;
736 struct netmsg_base msg;
738 ifnet_serialize_all(ifp);
741 ifp->if_flags &= ~IFF_UP;
743 ifnet_deserialize_all(ifp);
745 netmsg_init(&msg, NULL, &curthread->td_msgport,
746 0, bridge_delete_dispatch);
747 msg.lmsg.u.ms_resultp = sc;
748 lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0);
750 crit_enter(); /* XXX MP */
751 LIST_REMOVE(sc, sc_list);
756 /* Tear down the routing table. */
757 bridge_rtable_fini(sc);
759 /* Free per-cpu member iface lists */
760 kfree(sc->sc_iflists, M_DEVBUF);
770 * Handle a control request from the operator.
773 bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
775 struct bridge_softc *sc = ifp->if_softc;
776 struct bridge_control_arg args;
777 struct ifdrv *ifd = (struct ifdrv *) data;
778 const struct bridge_control *bc;
781 ASSERT_IFNET_SERIALIZED_ALL(ifp);
790 if (ifd->ifd_cmd >= bridge_control_table_size) {
794 bc = &bridge_control_table[ifd->ifd_cmd];
796 if (cmd == SIOCGDRVSPEC &&
797 (bc->bc_flags & BC_F_COPYOUT) == 0) {
800 } else if (cmd == SIOCSDRVSPEC &&
801 (bc->bc_flags & BC_F_COPYOUT)) {
806 if (bc->bc_flags & BC_F_SUSER) {
807 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
812 if (ifd->ifd_len != bc->bc_argsize ||
813 ifd->ifd_len > sizeof(args.bca_u)) {
818 memset(&args, 0, sizeof(args));
819 if (bc->bc_flags & BC_F_COPYIN) {
820 error = copyin(ifd->ifd_data, &args.bca_u,
826 error = bridge_control(sc, cmd, bc->bc_func, &args);
828 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
832 if (bc->bc_flags & BC_F_COPYOUT) {
833 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
834 if (args.bca_len != 0) {
835 KKASSERT(args.bca_kptr != NULL);
837 error = copyout(args.bca_kptr,
838 args.bca_uptr, args.bca_len);
840 kfree(args.bca_kptr, M_TEMP);
842 KKASSERT(args.bca_kptr == NULL);
845 KKASSERT(args.bca_len == 0 && args.bca_kptr == NULL);
850 if (!(ifp->if_flags & IFF_UP) &&
851 (ifp->if_flags & IFF_RUNNING)) {
853 * If interface is marked down and it is running,
857 } else if ((ifp->if_flags & IFF_UP) &&
858 !(ifp->if_flags & IFF_RUNNING)) {
860 * If interface is marked up and it is stopped, then
868 /* Do not allow the MTU to be changed on the bridge */
873 error = ether_ioctl(ifp, cmd, data);
882 * Clear or restore unwanted capabilities on the member interface
885 bridge_mutecaps(struct bridge_ifinfo *bif_info, struct ifnet *ifp, int mute)
890 if (ifp->if_ioctl == NULL)
893 bzero(&ifr, sizeof(ifr));
894 ifr.ifr_reqcap = ifp->if_capenable;
897 /* mask off and save capabilities */
898 bif_info->bifi_mutecap = ifr.ifr_reqcap & BRIDGE_IFCAPS_MASK;
899 if (bif_info->bifi_mutecap != 0)
900 ifr.ifr_reqcap &= ~BRIDGE_IFCAPS_MASK;
902 /* restore muted capabilities */
903 ifr.ifr_reqcap |= bif_info->bifi_mutecap;
906 if (bif_info->bifi_mutecap != 0) {
907 ifnet_serialize_all(ifp);
908 error = ifp->if_ioctl(ifp, SIOCSIFCAP, (caddr_t)&ifr, NULL);
909 ifnet_deserialize_all(ifp);
914 * bridge_lookup_member:
916 * Lookup a bridge member interface.
918 static struct bridge_iflist *
919 bridge_lookup_member(struct bridge_softc *sc, const char *name)
921 struct bridge_iflist *bif;
923 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
924 if (strcmp(bif->bif_ifp->if_xname, name) == 0)
931 * bridge_lookup_member_if:
933 * Lookup a bridge member interface by ifnet*.
935 static struct bridge_iflist *
936 bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp)
938 struct bridge_iflist *bif;
940 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
941 if (bif->bif_ifp == member_ifp)
948 * bridge_lookup_member_ifinfo:
950 * Lookup a bridge member interface by bridge_ifinfo.
952 static struct bridge_iflist *
953 bridge_lookup_member_ifinfo(struct bridge_softc *sc,
954 struct bridge_ifinfo *bif_info)
956 struct bridge_iflist *bif;
958 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
959 if (bif->bif_info == bif_info)
966 * bridge_delete_member:
968 * Delete the specified member interface.
971 bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif,
974 struct ifnet *ifs = bif->bif_ifp;
975 struct ifnet *bifp = sc->sc_ifp;
976 struct bridge_ifinfo *bif_info = bif->bif_info;
977 struct bridge_iflist_head saved_bifs;
979 ASSERT_IFNET_SERIALIZED_ALL(bifp);
980 KKASSERT(bif_info != NULL);
982 ifs->if_bridge = NULL;
985 * Release bridge interface's serializer:
986 * - To avoid possible dead lock.
987 * - Various sync operation will block the current thread.
989 ifnet_deserialize_all(bifp);
992 switch (ifs->if_type) {
996 * Take the interface out of promiscuous mode.
999 bridge_mutecaps(bif_info, ifs, 0);
1006 panic("bridge_delete_member: impossible");
1012 * Remove bifs from percpu linked list.
1014 * Removed bifs are not freed immediately, instead,
1015 * they are saved in saved_bifs. They will be freed
1016 * after we make sure that no one is accessing them,
1017 * i.e. after following netmsg_service_sync()
1019 LIST_INIT(&saved_bifs);
1020 bridge_del_bif(sc, bif_info, &saved_bifs);
1023 * Make sure that all protocol threads:
1024 * o see 'ifs' if_bridge is changed
1025 * o know that bif is removed from the percpu linked list
1027 netmsg_service_sync();
1030 * Free the removed bifs
1032 KKASSERT(!LIST_EMPTY(&saved_bifs));
1033 while ((bif = LIST_FIRST(&saved_bifs)) != NULL) {
1034 LIST_REMOVE(bif, bif_next);
1035 kfree(bif, M_DEVBUF);
1038 /* See the comment in bridge_ioctl_stop() */
1039 bridge_rtmsg_sync(sc);
1040 bridge_rtdelete(sc, ifs, IFBF_FLUSHALL | IFBF_FLUSHSYNC);
1042 ifnet_serialize_all(bifp);
1044 if (bifp->if_flags & IFF_RUNNING)
1045 bstp_initialization(sc);
1048 * Free the bif_info after bstp_initialization(), so that
1049 * bridge_softc.sc_root_port will not reference a dangling
1052 kfree(bif_info, M_DEVBUF);
1056 * bridge_delete_span:
1058 * Delete the specified span interface.
1061 bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif)
1063 KASSERT(bif->bif_ifp->if_bridge == NULL,
1064 ("%s: not a span interface", __func__));
1066 LIST_REMOVE(bif, bif_next);
1067 kfree(bif, M_DEVBUF);
1071 bridge_ioctl_init(struct bridge_softc *sc, void *arg __unused)
1073 struct ifnet *ifp = sc->sc_ifp;
1075 if (ifp->if_flags & IFF_RUNNING)
1078 callout_reset(&sc->sc_brcallout, bridge_rtable_prune_period * hz,
1081 ifp->if_flags |= IFF_RUNNING;
1082 bstp_initialization(sc);
1087 bridge_ioctl_stop(struct bridge_softc *sc, void *arg __unused)
1089 struct ifnet *ifp = sc->sc_ifp;
1090 struct lwkt_msg *lmsg;
1092 if ((ifp->if_flags & IFF_RUNNING) == 0)
1095 callout_stop(&sc->sc_brcallout);
1098 lmsg = &sc->sc_brtimemsg.lmsg;
1099 if ((lmsg->ms_flags & MSGF_DONE) == 0) {
1100 /* Pending to be processed; drop it */
1107 ifp->if_flags &= ~IFF_RUNNING;
1109 ifnet_deserialize_all(ifp);
1111 /* Let everyone know that we are stopped */
1112 netmsg_service_sync();
1115 * Sync ifnetX msgports in the order we forward rtnode
1116 * installation message. This is used to make sure that
1117 * all rtnode installation messages sent by bridge_rtupdate()
1118 * during above netmsg_service_sync() are flushed.
1120 bridge_rtmsg_sync(sc);
1121 bridge_rtflush(sc, IFBF_FLUSHDYN | IFBF_FLUSHSYNC);
1123 ifnet_serialize_all(ifp);
1128 bridge_ioctl_add(struct bridge_softc *sc, void *arg)
1130 struct ifbreq *req = arg;
1131 struct bridge_iflist *bif;
1132 struct bridge_ifinfo *bif_info;
1133 struct ifnet *ifs, *bifp;
1137 ASSERT_IFNET_SERIALIZED_ALL(bifp);
1139 ifs = ifunit(req->ifbr_ifsname);
1143 /* If it's in the span list, it can't be a member. */
1144 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1145 if (ifs == bif->bif_ifp)
1148 /* Allow the first Ethernet member to define the MTU */
1149 if (ifs->if_type != IFT_GIF) {
1150 if (LIST_EMPTY(&sc->sc_iflists[mycpuid])) {
1151 bifp->if_mtu = ifs->if_mtu;
1152 } else if (bifp->if_mtu != ifs->if_mtu) {
1153 if_printf(bifp, "invalid MTU for %s\n", ifs->if_xname);
1158 if (ifs->if_bridge == sc)
1161 if (ifs->if_bridge != NULL)
1164 bif_info = kmalloc(sizeof(*bif_info), M_DEVBUF, M_WAITOK | M_ZERO);
1165 bif_info->bifi_priority = BSTP_DEFAULT_PORT_PRIORITY;
1166 bif_info->bifi_path_cost = BSTP_DEFAULT_PATH_COST;
1167 bif_info->bifi_ifp = ifs;
1170 * Release bridge interface's serializer:
1171 * - To avoid possible dead lock.
1172 * - Various sync operation will block the current thread.
1174 ifnet_deserialize_all(bifp);
1176 switch (ifs->if_type) {
1180 * Place the interface into promiscuous mode.
1182 error = ifpromisc(ifs, 1);
1184 ifnet_serialize_all(bifp);
1187 bridge_mutecaps(bif_info, ifs, 1);
1190 case IFT_GIF: /* :^) */
1195 ifnet_serialize_all(bifp);
1200 * Add bifs to percpu linked lists
1202 bridge_add_bif(sc, bif_info, ifs);
1204 ifnet_serialize_all(bifp);
1206 if (bifp->if_flags & IFF_RUNNING)
1207 bstp_initialization(sc);
1212 * Everything has been setup, so let the member interface
1213 * deliver packets to this bridge on its input/output path.
1215 ifs->if_bridge = sc;
1218 if (bif_info != NULL)
1219 kfree(bif_info, M_DEVBUF);
1225 bridge_ioctl_del(struct bridge_softc *sc, void *arg)
1227 struct ifbreq *req = arg;
1228 struct bridge_iflist *bif;
1230 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1234 bridge_delete_member(sc, bif, 0);
1240 bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg)
1242 struct ifbreq *req = arg;
1243 struct bridge_iflist *bif;
1245 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1249 req->ifbr_ifsflags = bif->bif_flags;
1250 req->ifbr_state = bif->bif_state;
1251 req->ifbr_priority = bif->bif_priority;
1252 req->ifbr_path_cost = bif->bif_path_cost;
1253 req->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1259 bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg)
1261 struct ifbreq *req = arg;
1262 struct bridge_iflist *bif;
1263 struct ifnet *bifp = sc->sc_ifp;
1265 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1269 if (req->ifbr_ifsflags & IFBIF_SPAN) {
1270 /* SPAN is readonly */
1274 if (req->ifbr_ifsflags & IFBIF_STP) {
1275 switch (bif->bif_ifp->if_type) {
1277 /* These can do spanning tree. */
1281 /* Nothing else can. */
1286 ifnet_deserialize_all(bifp);
1287 bridge_set_bifflags(sc, bif->bif_info, req->ifbr_ifsflags);
1288 ifnet_serialize_all(bifp);
1290 if (bifp->if_flags & IFF_RUNNING)
1291 bstp_initialization(sc);
1297 bridge_ioctl_scache(struct bridge_softc *sc, void *arg)
1299 struct ifbrparam *param = arg;
1300 struct ifnet *ifp = sc->sc_ifp;
1302 sc->sc_brtmax = param->ifbrp_csize;
1304 ifnet_deserialize_all(ifp);
1306 ifnet_serialize_all(ifp);
1312 bridge_ioctl_gcache(struct bridge_softc *sc, void *arg)
1314 struct ifbrparam *param = arg;
1316 param->ifbrp_csize = sc->sc_brtmax;
1322 bridge_ioctl_gifs(struct bridge_softc *sc, void *arg)
1324 struct bridge_control_arg *bc_arg = arg;
1325 struct ifbifconf *bifc = arg;
1326 struct bridge_iflist *bif;
1327 struct ifbreq *breq;
1331 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next)
1333 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1336 if (bifc->ifbic_len == 0) {
1337 bifc->ifbic_len = sizeof(*breq) * count;
1339 } else if (count == 0 || bifc->ifbic_len < sizeof(*breq)) {
1340 bifc->ifbic_len = 0;
1344 len = min(bifc->ifbic_len, sizeof(*breq) * count);
1345 KKASSERT(len >= sizeof(*breq));
1347 breq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1349 bifc->ifbic_len = 0;
1352 bc_arg->bca_kptr = breq;
1355 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
1356 if (len < sizeof(*breq))
1359 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1360 sizeof(breq->ifbr_ifsname));
1361 breq->ifbr_ifsflags = bif->bif_flags;
1362 breq->ifbr_state = bif->bif_state;
1363 breq->ifbr_priority = bif->bif_priority;
1364 breq->ifbr_path_cost = bif->bif_path_cost;
1365 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1368 len -= sizeof(*breq);
1370 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
1371 if (len < sizeof(*breq))
1374 strlcpy(breq->ifbr_ifsname, bif->bif_ifp->if_xname,
1375 sizeof(breq->ifbr_ifsname));
1376 breq->ifbr_ifsflags = bif->bif_flags;
1377 breq->ifbr_portno = bif->bif_ifp->if_index & 0xff;
1380 len -= sizeof(*breq);
1383 bifc->ifbic_len = sizeof(*breq) * count;
1384 KKASSERT(bifc->ifbic_len > 0);
1386 bc_arg->bca_len = bifc->ifbic_len;
1387 bc_arg->bca_uptr = bifc->ifbic_req;
1392 bridge_ioctl_rts(struct bridge_softc *sc, void *arg)
1394 struct bridge_control_arg *bc_arg = arg;
1395 struct ifbaconf *bac = arg;
1396 struct bridge_rtnode *brt;
1397 struct ifbareq *bareq;
1401 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list)
1404 if (bac->ifbac_len == 0) {
1405 bac->ifbac_len = sizeof(*bareq) * count;
1407 } else if (count == 0 || bac->ifbac_len < sizeof(*bareq)) {
1412 len = min(bac->ifbac_len, sizeof(*bareq) * count);
1413 KKASSERT(len >= sizeof(*bareq));
1415 bareq = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1416 if (bareq == NULL) {
1420 bc_arg->bca_kptr = bareq;
1423 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
1424 struct bridge_rtinfo *bri = brt->brt_info;
1425 unsigned long expire;
1427 if (len < sizeof(*bareq))
1430 strlcpy(bareq->ifba_ifsname, bri->bri_ifp->if_xname,
1431 sizeof(bareq->ifba_ifsname));
1432 memcpy(bareq->ifba_dst, brt->brt_addr, sizeof(brt->brt_addr));
1433 expire = bri->bri_expire;
1434 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
1435 time_second < expire)
1436 bareq->ifba_expire = expire - time_second;
1438 bareq->ifba_expire = 0;
1439 bareq->ifba_flags = bri->bri_flags;
1442 len -= sizeof(*bareq);
1445 bac->ifbac_len = sizeof(*bareq) * count;
1446 KKASSERT(bac->ifbac_len > 0);
1448 bc_arg->bca_len = bac->ifbac_len;
1449 bc_arg->bca_uptr = bac->ifbac_req;
1454 bridge_ioctl_saddr(struct bridge_softc *sc, void *arg)
1456 struct ifbareq *req = arg;
1457 struct bridge_iflist *bif;
1458 struct ifnet *ifp = sc->sc_ifp;
1461 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1463 bif = bridge_lookup_member(sc, req->ifba_ifsname);
1467 ifnet_deserialize_all(ifp);
1468 error = bridge_rtsaddr(sc, req->ifba_dst, bif->bif_ifp,
1470 ifnet_serialize_all(ifp);
1475 bridge_ioctl_sto(struct bridge_softc *sc, void *arg)
1477 struct ifbrparam *param = arg;
1479 sc->sc_brttimeout = param->ifbrp_ctime;
1485 bridge_ioctl_gto(struct bridge_softc *sc, void *arg)
1487 struct ifbrparam *param = arg;
1489 param->ifbrp_ctime = sc->sc_brttimeout;
1495 bridge_ioctl_daddr(struct bridge_softc *sc, void *arg)
1497 struct ifbareq *req = arg;
1498 struct ifnet *ifp = sc->sc_ifp;
1501 ifnet_deserialize_all(ifp);
1502 error = bridge_rtdaddr(sc, req->ifba_dst);
1503 ifnet_serialize_all(ifp);
1508 bridge_ioctl_flush(struct bridge_softc *sc, void *arg)
1510 struct ifbreq *req = arg;
1511 struct ifnet *ifp = sc->sc_ifp;
1513 ifnet_deserialize_all(ifp);
1514 bridge_rtflush(sc, req->ifbr_ifsflags | IFBF_FLUSHSYNC);
1515 ifnet_serialize_all(ifp);
1521 bridge_ioctl_gpri(struct bridge_softc *sc, void *arg)
1523 struct ifbrparam *param = arg;
1525 param->ifbrp_prio = sc->sc_bridge_priority;
1531 bridge_ioctl_spri(struct bridge_softc *sc, void *arg)
1533 struct ifbrparam *param = arg;
1535 sc->sc_bridge_priority = param->ifbrp_prio;
1537 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1538 bstp_initialization(sc);
1544 bridge_ioctl_ght(struct bridge_softc *sc, void *arg)
1546 struct ifbrparam *param = arg;
1548 param->ifbrp_hellotime = sc->sc_bridge_hello_time >> 8;
1554 bridge_ioctl_sht(struct bridge_softc *sc, void *arg)
1556 struct ifbrparam *param = arg;
1558 if (param->ifbrp_hellotime == 0)
1560 sc->sc_bridge_hello_time = param->ifbrp_hellotime << 8;
1562 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1563 bstp_initialization(sc);
1569 bridge_ioctl_gfd(struct bridge_softc *sc, void *arg)
1571 struct ifbrparam *param = arg;
1573 param->ifbrp_fwddelay = sc->sc_bridge_forward_delay >> 8;
1579 bridge_ioctl_sfd(struct bridge_softc *sc, void *arg)
1581 struct ifbrparam *param = arg;
1583 if (param->ifbrp_fwddelay == 0)
1585 sc->sc_bridge_forward_delay = param->ifbrp_fwddelay << 8;
1587 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1588 bstp_initialization(sc);
1594 bridge_ioctl_gma(struct bridge_softc *sc, void *arg)
1596 struct ifbrparam *param = arg;
1598 param->ifbrp_maxage = sc->sc_bridge_max_age >> 8;
1604 bridge_ioctl_sma(struct bridge_softc *sc, void *arg)
1606 struct ifbrparam *param = arg;
1608 if (param->ifbrp_maxage == 0)
1610 sc->sc_bridge_max_age = param->ifbrp_maxage << 8;
1612 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1613 bstp_initialization(sc);
1619 bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg)
1621 struct ifbreq *req = arg;
1622 struct bridge_iflist *bif;
1624 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1628 bif->bif_priority = req->ifbr_priority;
1630 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1631 bstp_initialization(sc);
1637 bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg)
1639 struct ifbreq *req = arg;
1640 struct bridge_iflist *bif;
1642 bif = bridge_lookup_member(sc, req->ifbr_ifsname);
1646 bif->bif_path_cost = req->ifbr_path_cost;
1648 if (sc->sc_ifp->if_flags & IFF_RUNNING)
1649 bstp_initialization(sc);
1655 bridge_ioctl_addspan(struct bridge_softc *sc, void *arg)
1657 struct ifbreq *req = arg;
1658 struct bridge_iflist *bif;
1661 ifs = ifunit(req->ifbr_ifsname);
1665 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1666 if (ifs == bif->bif_ifp)
1669 if (ifs->if_bridge != NULL)
1672 switch (ifs->if_type) {
1682 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
1684 bif->bif_flags = IFBIF_SPAN;
1685 /* NOTE: span bif does not need bridge_ifinfo */
1687 LIST_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next);
1695 bridge_ioctl_delspan(struct bridge_softc *sc, void *arg)
1697 struct ifbreq *req = arg;
1698 struct bridge_iflist *bif;
1701 ifs = ifunit(req->ifbr_ifsname);
1705 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1706 if (ifs == bif->bif_ifp)
1712 bridge_delete_span(sc, bif);
1714 if (LIST_EMPTY(&sc->sc_spanlist))
1721 bridge_ifdetach_dispatch(netmsg_t msg)
1723 struct ifnet *ifp, *bifp;
1724 struct bridge_softc *sc;
1725 struct bridge_iflist *bif;
1727 ifp = msg->lmsg.u.ms_resultp;
1728 sc = ifp->if_bridge;
1730 /* Check if the interface is a bridge member */
1734 ifnet_serialize_all(bifp);
1736 bif = bridge_lookup_member_if(sc, ifp);
1738 bridge_delete_member(sc, bif, 1);
1740 /* XXX Why bif will be NULL? */
1743 ifnet_deserialize_all(bifp);
1747 crit_enter(); /* XXX MP */
1749 /* Check if the interface is a span port */
1750 LIST_FOREACH(sc, &bridge_list, sc_list) {
1753 ifnet_serialize_all(bifp);
1755 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next)
1756 if (ifp == bif->bif_ifp) {
1757 bridge_delete_span(sc, bif);
1761 ifnet_deserialize_all(bifp);
1767 lwkt_replymsg(&msg->lmsg, 0);
1773 * Detach an interface from a bridge. Called when a member
1774 * interface is detaching.
1777 bridge_ifdetach(void *arg __unused, struct ifnet *ifp)
1779 struct netmsg_base msg;
1781 netmsg_init(&msg, NULL, &curthread->td_msgport,
1782 0, bridge_ifdetach_dispatch);
1783 msg.lmsg.u.ms_resultp = ifp;
1785 lwkt_domsg(BRIDGE_CFGPORT, &msg.lmsg, 0);
1791 * Initialize a bridge interface.
1794 bridge_init(void *xsc)
1796 bridge_control(xsc, SIOCSIFFLAGS, bridge_ioctl_init, NULL);
1802 * Stop the bridge interface.
1805 bridge_stop(struct ifnet *ifp)
1807 bridge_control(ifp->if_softc, SIOCSIFFLAGS, bridge_ioctl_stop, NULL);
1813 * Enqueue a packet on a bridge member interface.
1817 bridge_enqueue(struct ifnet *dst_ifp, struct mbuf *m)
1819 struct netmsg_packet *nmp;
1821 nmp = &m->m_hdr.mh_netmsg;
1822 netmsg_init(&nmp->base, NULL, &netisr_apanic_rport,
1823 0, bridge_enqueue_handler);
1825 nmp->base.lmsg.u.ms_resultp = dst_ifp;
1827 lwkt_sendmsg(ifnet_portfn(mycpu->gd_cpuid), &nmp->base.lmsg);
1833 * Send output from a bridge member interface. This
1834 * performs the bridging function for locally originated
1837 * The mbuf has the Ethernet header already attached. We must
1838 * enqueue or free the mbuf before returning.
1841 bridge_output(struct ifnet *ifp, struct mbuf *m)
1843 struct bridge_softc *sc = ifp->if_bridge;
1844 struct ether_header *eh;
1845 struct ifnet *dst_if, *bifp;
1848 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
1851 * Make sure that we are still a member of a bridge interface.
1859 if (m->m_len < ETHER_HDR_LEN) {
1860 m = m_pullup(m, ETHER_HDR_LEN);
1864 eh = mtod(m, struct ether_header *);
1866 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0)
1872 * If bridge is down, but the original output interface is up,
1873 * go ahead and send out that interface. Otherwise, the packet
1876 if ((bifp->if_flags & IFF_RUNNING) == 0) {
1882 * If the packet is a multicast, or we don't know a better way to
1883 * get there, send to all interfaces.
1885 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1888 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1889 if (dst_if == NULL) {
1890 struct bridge_iflist *bif, *nbif;
1897 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid],
1899 dst_if = bif->bif_ifp;
1900 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1904 * If this is not the original output interface,
1905 * and the interface is participating in spanning
1906 * tree, make sure the port is in a state that
1907 * allows forwarding.
1909 if (dst_if != ifp &&
1910 (bif->bif_flags & IFBIF_STP) != 0) {
1911 switch (bif->bif_state) {
1912 case BSTP_IFSTATE_BLOCKING:
1913 case BSTP_IFSTATE_LISTENING:
1914 case BSTP_IFSTATE_DISABLED:
1919 if (LIST_NEXT(bif, bif_next) == NULL) {
1923 mc = m_copypacket(m, MB_DONTWAIT);
1931 * If the packet is 'from' us override ether_shost.
1933 bridge_handoff(dst_if, mc, from_us);
1935 if (nbif != NULL && !nbif->bif_onlist) {
1936 KKASSERT(bif->bif_onlist);
1937 nbif = LIST_NEXT(bif, bif_next);
1947 * XXX Spanning tree consideration here?
1951 if ((dst_if->if_flags & IFF_RUNNING) == 0)
1954 bridge_handoff(dst_if, m, from_us);
1961 * Start output on a bridge.
1964 bridge_start(struct ifnet *ifp)
1966 struct bridge_softc *sc = ifp->if_softc;
1968 ASSERT_IFNET_SERIALIZED_TX(ifp);
1970 ifp->if_flags |= IFF_OACTIVE;
1972 struct ifnet *dst_if = NULL;
1973 struct ether_header *eh;
1976 m = ifq_dequeue(&ifp->if_snd, NULL);
1980 if (m->m_len < sizeof(*eh)) {
1981 m = m_pullup(m, sizeof(*eh));
1987 eh = mtod(m, struct ether_header *);
1992 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0)
1993 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
1996 bridge_start_bcast(sc, m);
1998 bridge_enqueue(dst_if, m);
2000 ifp->if_flags &= ~IFF_OACTIVE;
2006 * Forward packets received on a bridge interface via the input
2009 * The forwarding function of the bridge.
2012 bridge_forward(struct bridge_softc *sc, struct mbuf *m)
2014 struct bridge_iflist *bif;
2015 struct ifnet *src_if, *dst_if, *ifp;
2016 struct ether_header *eh;
2018 src_if = m->m_pkthdr.rcvif;
2021 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2024 ifp->if_ibytes += m->m_pkthdr.len;
2027 * Look up the bridge_iflist.
2029 bif = bridge_lookup_member_if(sc, src_if);
2031 /* Interface is not a bridge member (anymore?) */
2036 if (bif->bif_flags & IFBIF_STP) {
2037 switch (bif->bif_state) {
2038 case BSTP_IFSTATE_BLOCKING:
2039 case BSTP_IFSTATE_LISTENING:
2040 case BSTP_IFSTATE_DISABLED:
2046 eh = mtod(m, struct ether_header *);
2049 * If the interface is learning, and the source
2050 * address is valid and not multicast, record
2053 if ((bif->bif_flags & IFBIF_LEARNING) != 0 &&
2054 ETHER_IS_MULTICAST(eh->ether_shost) == 0 &&
2055 (eh->ether_shost[0] == 0 &&
2056 eh->ether_shost[1] == 0 &&
2057 eh->ether_shost[2] == 0 &&
2058 eh->ether_shost[3] == 0 &&
2059 eh->ether_shost[4] == 0 &&
2060 eh->ether_shost[5] == 0) == 0)
2061 bridge_rtupdate(sc, eh->ether_shost, src_if, IFBAF_DYNAMIC);
2063 if ((bif->bif_flags & IFBIF_STP) != 0 &&
2064 bif->bif_state == BSTP_IFSTATE_LEARNING) {
2070 * At this point, the port either doesn't participate
2071 * in spanning tree or it is in the forwarding state.
2075 * If the packet is unicast, destined for someone on
2076 * "this" side of the bridge, drop it.
2078 if ((m->m_flags & (M_BCAST|M_MCAST)) == 0) {
2079 dst_if = bridge_rtlookup(sc, eh->ether_dhost);
2080 if (src_if == dst_if) {
2085 /* ...forward it to all interfaces. */
2090 if (dst_if == NULL) {
2091 bridge_broadcast(sc, src_if, m);
2096 * At this point, we're dealing with a unicast frame
2097 * going to a different interface.
2099 if ((dst_if->if_flags & IFF_RUNNING) == 0) {
2103 bif = bridge_lookup_member_if(sc, dst_if);
2105 /* Not a member of the bridge (anymore?) */
2110 if (bif->bif_flags & IFBIF_STP) {
2111 switch (bif->bif_state) {
2112 case BSTP_IFSTATE_DISABLED:
2113 case BSTP_IFSTATE_BLOCKING:
2119 if (inet_pfil_hook.ph_hashooks > 0
2121 || inet6_pfil_hook.ph_hashooks > 0
2124 if (bridge_pfil(&m, ifp, src_if, PFIL_IN) != 0)
2129 if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
2134 bridge_handoff(dst_if, m, 0);
2140 * Receive input from a member interface. Queue the packet for
2141 * bridging if it is not for us.
2143 static struct mbuf *
2144 bridge_input(struct ifnet *ifp, struct mbuf *m)
2146 struct bridge_softc *sc = ifp->if_bridge;
2147 struct bridge_iflist *bif;
2148 struct ifnet *bifp, *new_ifp;
2149 struct ether_header *eh;
2150 struct mbuf *mc, *mc2;
2152 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
2155 * Make sure that we are still a member of a bridge interface.
2163 if ((bifp->if_flags & IFF_RUNNING) == 0)
2167 * Implement support for bridge monitoring. If this flag has been
2168 * set on this interface, discard the packet once we push it through
2169 * the bpf(4) machinery, but before we do, increment various counters
2170 * associated with this bridge.
2172 if (bifp->if_flags & IFF_MONITOR) {
2173 /* Change input interface to this bridge */
2174 m->m_pkthdr.rcvif = bifp;
2178 /* Update bridge's ifnet statistics */
2179 bifp->if_ipackets++;
2180 bifp->if_ibytes += m->m_pkthdr.len;
2181 if (m->m_flags & (M_MCAST | M_BCAST))
2190 * Handle the ether_header
2192 eh = mtod(m, struct ether_header *);
2193 m->m_pkthdr.fw_flags |= BRIDGE_MBUF_TAGGED;
2194 bcopy(eh, &m->m_pkthdr.br.ether, sizeof(*eh));
2196 if (memcmp(eh->ether_dhost, IF_LLADDR(bifp), ETHER_ADDR_LEN) == 0) {
2198 * If the packet is for us, set the packets source as the
2199 * bridge, and return the packet back to ifnet.if_input for
2202 KASSERT(bifp->if_bridge == NULL,
2203 ("loop created in bridge_input"));
2204 if (pfil_member != 0) {
2205 if (inet_pfil_hook.ph_hashooks > 0
2207 || inet6_pfil_hook.ph_hashooks > 0
2210 if (bridge_pfil(&m, NULL, ifp, PFIL_IN) != 0)
2221 * Tap all packets arriving on the bridge, no matter if
2222 * they are local destinations or not. In is in.
2226 bif = bridge_lookup_member_if(sc, ifp);
2233 if (m->m_flags & (M_BCAST | M_MCAST)) {
2234 /* Tap off 802.1D packets; they do not get forwarded. */
2235 if (memcmp(eh->ether_dhost, bstp_etheraddr,
2236 ETHER_ADDR_LEN) == 0) {
2237 ifnet_serialize_all(bifp);
2238 bstp_input(sc, bif, m);
2239 ifnet_deserialize_all(bifp);
2241 /* m is freed by bstp_input */
2246 if (bif->bif_flags & IFBIF_STP) {
2247 switch (bif->bif_state) {
2248 case BSTP_IFSTATE_BLOCKING:
2249 case BSTP_IFSTATE_LISTENING:
2250 case BSTP_IFSTATE_DISABLED:
2256 * Make a deep copy of the packet and enqueue the copy
2257 * for bridge processing; return the original packet for
2260 mc = m_dup(m, MB_DONTWAIT);
2264 bridge_forward(sc, mc);
2267 * Reinject the mbuf as arriving on the bridge so we have a
2268 * chance at claiming multicast packets. We can not loop back
2269 * here from ether_input as a bridge is never a member of a
2272 KASSERT(bifp->if_bridge == NULL,
2273 ("loop created in bridge_input"));
2274 mc2 = m_dup(m, MB_DONTWAIT);
2277 /* Keep the layer3 header aligned */
2278 int i = min(mc2->m_pkthdr.len, max_protohdr);
2279 mc2 = m_copyup(mc2, i, ETHER_ALIGN);
2284 * Don't tap to bpf(4) again; we have already done
2287 * Leave m_pkthdr.rcvif alone, so ARP replies are
2288 * processed as coming in on the correct interface.
2290 ether_reinput_oncpu(bifp, mc2, REINPUT_KEEPRCVIF);
2293 /* Return the original packet for local processing. */
2297 if (bif->bif_flags & IFBIF_STP) {
2298 switch (bif->bif_state) {
2299 case BSTP_IFSTATE_BLOCKING:
2300 case BSTP_IFSTATE_LISTENING:
2301 case BSTP_IFSTATE_DISABLED:
2307 * Unicast. Make sure it's not for us.
2309 * This loop is MPSAFE; the only blocking operation (bridge_rtupdate)
2310 * is followed by breaking out of the loop.
2312 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2313 if (bif->bif_ifp->if_type != IFT_ETHER)
2317 * It is destined for us. Reinput on the same interface
2318 * it came in on so things like ARP responses get assigned
2319 * to the correct member (the incoming interface) and not
2320 * to the member which happens to have the matching dhost.
2322 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_dhost,
2323 ETHER_ADDR_LEN) == 0) {
2324 if (bif->bif_ifp != ifp) {
2325 /* XXX loop prevention */
2326 m->m_flags |= M_ETHER_BRIDGED;
2327 new_ifp = bif->bif_ifp;
2329 if (bif->bif_flags & IFBIF_LEARNING) {
2330 bridge_rtupdate(sc, eh->ether_shost,
2331 ifp, IFBAF_DYNAMIC);
2336 /* We just received a packet that we sent out. */
2337 if (memcmp(IF_LLADDR(bif->bif_ifp), eh->ether_shost,
2338 ETHER_ADDR_LEN) == 0) {
2345 /* Perform the bridge forwarding function. */
2346 bridge_forward(sc, m);
2350 * Leave m_pkthdr.rcvif alone, so ARP replies are
2351 * processed as coming in on the correct interface.
2354 if (new_ifp != NULL) {
2355 ether_reinput_oncpu(new_ifp, m,
2356 REINPUT_KEEPRCVIF|REINPUT_RUNBPF);
2363 * bridge_start_bcast:
2365 * Broadcast the packet sent from bridge to all member
2367 * This is a simplified version of bridge_broadcast(), however,
2368 * this function expects caller to hold bridge's serializer.
2371 bridge_start_bcast(struct bridge_softc *sc, struct mbuf *m)
2373 struct bridge_iflist *bif;
2375 struct ifnet *dst_if, *bifp;
2379 ASSERT_IFNET_SERIALIZED_ALL(bifp);
2382 * Following loop is MPSAFE; nothing is blocking
2385 LIST_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) {
2386 dst_if = bif->bif_ifp;
2388 if (bif->bif_flags & IFBIF_STP) {
2389 switch (bif->bif_state) {
2390 case BSTP_IFSTATE_BLOCKING:
2391 case BSTP_IFSTATE_DISABLED:
2396 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2397 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2400 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2403 if (LIST_NEXT(bif, bif_next) == NULL) {
2407 mc = m_copypacket(m, MB_DONTWAIT);
2413 bridge_enqueue(dst_if, mc);
2422 * Send a frame to all interfaces that are members of
2423 * the bridge, except for the one on which the packet
2427 bridge_broadcast(struct bridge_softc *sc, struct ifnet *src_if,
2430 struct bridge_iflist *bif, *nbif;
2431 struct ether_header *eh;
2433 struct ifnet *dst_if, *bifp;
2438 ASSERT_IFNET_NOT_SERIALIZED_ALL(bifp);
2440 eh = mtod(m, struct ether_header *);
2441 if (memcmp(eh->ether_dhost, IF_LLADDR(src_if), ETHER_ADDR_LEN) == 0)
2446 if (inet_pfil_hook.ph_hashooks > 0
2448 || inet6_pfil_hook.ph_hashooks > 0
2451 if (bridge_pfil(&m, bifp, src_if, PFIL_IN) != 0)
2456 /* Filter on the bridge interface before broadcasting */
2457 if (bridge_pfil(&m, bifp, NULL, PFIL_OUT) != 0)
2463 LIST_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) {
2464 dst_if = bif->bif_ifp;
2465 if (dst_if == src_if)
2468 if (bif->bif_flags & IFBIF_STP) {
2469 switch (bif->bif_state) {
2470 case BSTP_IFSTATE_BLOCKING:
2471 case BSTP_IFSTATE_DISABLED:
2476 if ((bif->bif_flags & IFBIF_DISCOVER) == 0 &&
2477 (m->m_flags & (M_BCAST|M_MCAST)) == 0)
2480 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2483 if (LIST_NEXT(bif, bif_next) == NULL) {
2487 mc = m_copypacket(m, MB_DONTWAIT);
2489 sc->sc_ifp->if_oerrors++;
2495 * Filter on the output interface. Pass a NULL bridge
2496 * interface pointer so we do not redundantly filter on
2497 * the bridge for each interface we broadcast on.
2499 if (inet_pfil_hook.ph_hashooks > 0
2501 || inet6_pfil_hook.ph_hashooks > 0
2504 if (bridge_pfil(&mc, NULL, dst_if, PFIL_OUT) != 0)
2509 bridge_handoff(dst_if, mc, from_us);
2511 if (nbif != NULL && !nbif->bif_onlist) {
2512 KKASSERT(bif->bif_onlist);
2513 nbif = LIST_NEXT(bif, bif_next);
2523 * Duplicate a packet out one or more interfaces that are in span mode,
2524 * the original mbuf is unmodified.
2527 bridge_span(struct bridge_softc *sc, struct mbuf *m)
2529 struct bridge_iflist *bif;
2530 struct ifnet *dst_if, *bifp;
2534 ifnet_serialize_all(bifp);
2536 LIST_FOREACH(bif, &sc->sc_spanlist, bif_next) {
2537 dst_if = bif->bif_ifp;
2539 if ((dst_if->if_flags & IFF_RUNNING) == 0)
2542 mc = m_copypacket(m, MB_DONTWAIT);
2544 sc->sc_ifp->if_oerrors++;
2547 bridge_enqueue(dst_if, mc);
2550 ifnet_deserialize_all(bifp);
2554 bridge_rtmsg_sync_handler(netmsg_t msg)
2556 ifnet_forwardmsg(&msg->lmsg, mycpuid + 1);
2560 bridge_rtmsg_sync(struct bridge_softc *sc)
2562 struct netmsg_base msg;
2564 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2566 netmsg_init(&msg, NULL, &curthread->td_msgport,
2567 0, bridge_rtmsg_sync_handler);
2568 ifnet_domsg(&msg.lmsg, 0);
2571 static __inline void
2572 bridge_rtinfo_update(struct bridge_rtinfo *bri, struct ifnet *dst_if,
2573 int setflags, uint8_t flags, uint32_t timeo)
2575 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2576 bri->bri_ifp != dst_if)
2577 bri->bri_ifp = dst_if;
2578 if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2579 bri->bri_expire != time_second + timeo)
2580 bri->bri_expire = time_second + timeo;
2582 bri->bri_flags = flags;
2586 bridge_rtinstall_oncpu(struct bridge_softc *sc, const uint8_t *dst,
2587 struct ifnet *dst_if, int setflags, uint8_t flags,
2588 struct bridge_rtinfo **bri0)
2590 struct bridge_rtnode *brt;
2591 struct bridge_rtinfo *bri;
2594 brt = bridge_rtnode_lookup(sc, dst);
2597 * rtnode for 'dst' already exists. We inform the
2598 * caller about this by leaving bri0 as NULL. The
2599 * caller will terminate the intallation upon getting
2600 * NULL bri0. However, we still need to update the
2603 KKASSERT(*bri0 == NULL);
2606 bridge_rtinfo_update(brt->brt_info, dst_if, setflags,
2607 flags, sc->sc_brttimeout);
2612 * We only need to check brtcnt on CPU0, since if limit
2613 * is to be exceeded, ENOSPC is returned. Caller knows
2614 * this and will terminate the installation.
2616 if (sc->sc_brtcnt >= sc->sc_brtmax)
2619 KKASSERT(*bri0 == NULL);
2620 bri = kmalloc(sizeof(struct bridge_rtinfo), M_DEVBUF,
2625 bri->bri_flags = IFBAF_DYNAMIC;
2626 bridge_rtinfo_update(bri, dst_if, setflags, flags,
2630 KKASSERT(bri != NULL);
2633 brt = kmalloc(sizeof(struct bridge_rtnode), M_DEVBUF,
2635 memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN);
2636 brt->brt_info = bri;
2638 bridge_rtnode_insert(sc, brt);
2643 bridge_rtinstall_handler(netmsg_t msg)
2645 struct netmsg_brsaddr *brmsg = (struct netmsg_brsaddr *)msg;
2648 error = bridge_rtinstall_oncpu(brmsg->br_softc,
2649 brmsg->br_dst, brmsg->br_dst_if,
2650 brmsg->br_setflags, brmsg->br_flags,
2653 KKASSERT(mycpuid == 0 && brmsg->br_rtinfo == NULL);
2654 lwkt_replymsg(&brmsg->base.lmsg, error);
2656 } else if (brmsg->br_rtinfo == NULL) {
2657 /* rtnode already exists for 'dst' */
2658 KKASSERT(mycpuid == 0);
2659 lwkt_replymsg(&brmsg->base.lmsg, 0);
2662 ifnet_forwardmsg(&brmsg->base.lmsg, mycpuid + 1);
2668 * Add/Update a bridge routing entry.
2671 bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst,
2672 struct ifnet *dst_if, uint8_t flags)
2674 struct bridge_rtnode *brt;
2677 * A route for this destination might already exist. If so,
2678 * update it, otherwise create a new one.
2680 if ((brt = bridge_rtnode_lookup(sc, dst)) == NULL) {
2681 struct netmsg_brsaddr *brmsg;
2683 if (sc->sc_brtcnt >= sc->sc_brtmax)
2686 brmsg = kmalloc(sizeof(*brmsg), M_LWKTMSG, M_WAITOK | M_NULLOK);
2690 netmsg_init(&brmsg->base, NULL, &netisr_afree_rport,
2691 0, bridge_rtinstall_handler);
2692 memcpy(brmsg->br_dst, dst, ETHER_ADDR_LEN);
2693 brmsg->br_dst_if = dst_if;
2694 brmsg->br_flags = flags;
2695 brmsg->br_setflags = 0;
2696 brmsg->br_softc = sc;
2697 brmsg->br_rtinfo = NULL;
2699 ifnet_sendmsg(&brmsg->base.lmsg, 0);
2702 bridge_rtinfo_update(brt->brt_info, dst_if, 0, flags,
2708 bridge_rtsaddr(struct bridge_softc *sc, const uint8_t *dst,
2709 struct ifnet *dst_if, uint8_t flags)
2711 struct netmsg_brsaddr brmsg;
2713 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2715 netmsg_init(&brmsg.base, NULL, &curthread->td_msgport,
2716 0, bridge_rtinstall_handler);
2717 memcpy(brmsg.br_dst, dst, ETHER_ADDR_LEN);
2718 brmsg.br_dst_if = dst_if;
2719 brmsg.br_flags = flags;
2720 brmsg.br_setflags = 1;
2721 brmsg.br_softc = sc;
2722 brmsg.br_rtinfo = NULL;
2724 return ifnet_domsg(&brmsg.base.lmsg, 0);
2730 * Lookup the destination interface for an address.
2732 static struct ifnet *
2733 bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr)
2735 struct bridge_rtnode *brt;
2737 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2739 return brt->brt_info->bri_ifp;
2743 bridge_rtreap_handler(netmsg_t msg)
2745 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
2746 struct bridge_rtnode *brt, *nbrt;
2748 LIST_FOREACH_MUTABLE(brt, &sc->sc_rtlists[mycpuid], brt_list, nbrt) {
2749 if (brt->brt_info->bri_dead)
2750 bridge_rtnode_destroy(sc, brt);
2752 ifnet_forwardmsg(&msg->lmsg, mycpuid + 1);
2756 bridge_rtreap(struct bridge_softc *sc)
2758 struct netmsg_base msg;
2760 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2762 netmsg_init(&msg, NULL, &curthread->td_msgport,
2763 0, bridge_rtreap_handler);
2764 msg.lmsg.u.ms_resultp = sc;
2766 ifnet_domsg(&msg.lmsg, 0);
2770 bridge_rtreap_async(struct bridge_softc *sc)
2772 struct netmsg_base *msg;
2774 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK);
2776 netmsg_init(msg, NULL, &netisr_afree_rport,
2777 0, bridge_rtreap_handler);
2778 msg->lmsg.u.ms_resultp = sc;
2780 ifnet_sendmsg(&msg->lmsg, 0);
2786 * Trim the routine table so that we have a number
2787 * of routing entries less than or equal to the
2791 bridge_rttrim(struct bridge_softc *sc)
2793 struct bridge_rtnode *brt;
2796 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2798 /* Make sure we actually need to do this. */
2799 if (sc->sc_brtcnt <= sc->sc_brtmax)
2803 * Find out how many rtnodes are dead
2805 dead = bridge_rtage_finddead(sc);
2806 KKASSERT(dead <= sc->sc_brtcnt);
2808 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
2809 /* Enough dead rtnodes are found */
2815 * Kill some dynamic rtnodes to meet the brtmax
2817 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2818 struct bridge_rtinfo *bri = brt->brt_info;
2820 if (bri->bri_dead) {
2822 * We have counted this rtnode in
2823 * bridge_rtage_finddead()
2828 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2831 KKASSERT(dead <= sc->sc_brtcnt);
2833 if (sc->sc_brtcnt - dead <= sc->sc_brtmax) {
2834 /* Enough rtnodes are collected */
2846 * Aging timer for the bridge.
2849 bridge_timer(void *arg)
2851 struct bridge_softc *sc = arg;
2852 struct netmsg_base *msg;
2854 KKASSERT(mycpuid == BRIDGE_CFGCPU);
2858 if (callout_pending(&sc->sc_brcallout) ||
2859 !callout_active(&sc->sc_brcallout)) {
2863 callout_deactivate(&sc->sc_brcallout);
2865 msg = &sc->sc_brtimemsg;
2866 KKASSERT(msg->lmsg.ms_flags & MSGF_DONE);
2867 lwkt_sendmsg(BRIDGE_CFGPORT, &msg->lmsg);
2873 bridge_timer_handler(netmsg_t msg)
2875 struct bridge_softc *sc = msg->lmsg.u.ms_resultp;
2877 KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT);
2881 lwkt_replymsg(&msg->lmsg, 0);
2885 if (sc->sc_ifp->if_flags & IFF_RUNNING) {
2886 callout_reset(&sc->sc_brcallout,
2887 bridge_rtable_prune_period * hz, bridge_timer, sc);
2892 bridge_rtage_finddead(struct bridge_softc *sc)
2894 struct bridge_rtnode *brt;
2897 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2898 struct bridge_rtinfo *bri = brt->brt_info;
2900 if ((bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC &&
2901 time_second >= bri->bri_expire) {
2904 KKASSERT(dead <= sc->sc_brtcnt);
2913 * Perform an aging cycle.
2916 bridge_rtage(struct bridge_softc *sc)
2918 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2920 if (bridge_rtage_finddead(sc))
2927 * Remove all dynamic addresses from the bridge.
2930 bridge_rtflush(struct bridge_softc *sc, int bf)
2932 struct bridge_rtnode *brt;
2936 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2937 struct bridge_rtinfo *bri = brt->brt_info;
2939 if ((bf & IFBF_FLUSHALL) ||
2940 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
2946 if (bf & IFBF_FLUSHSYNC)
2949 bridge_rtreap_async(sc);
2956 * Remove an address from the table.
2959 bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr)
2961 struct bridge_rtnode *brt;
2963 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
2965 if ((brt = bridge_rtnode_lookup(sc, addr)) == NULL)
2968 /* TODO: add a cheaper delete operation */
2969 brt->brt_info->bri_dead = 1;
2977 * Delete routes to a speicifc member interface.
2980 bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int bf)
2982 struct bridge_rtnode *brt;
2986 LIST_FOREACH(brt, &sc->sc_rtlists[mycpuid], brt_list) {
2987 struct bridge_rtinfo *bri = brt->brt_info;
2989 if (bri->bri_ifp == ifp &&
2990 ((bf & IFBF_FLUSHALL) ||
2991 (bri->bri_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) {
2997 if (bf & IFBF_FLUSHSYNC)
3000 bridge_rtreap_async(sc);
3005 * bridge_rtable_init:
3007 * Initialize the route table for this bridge.
3010 bridge_rtable_init(struct bridge_softc *sc)
3015 * Initialize per-cpu hash tables
3017 sc->sc_rthashs = kmalloc(sizeof(*sc->sc_rthashs) * ncpus,
3018 M_DEVBUF, M_WAITOK);
3019 for (cpu = 0; cpu < ncpus; ++cpu) {
3022 sc->sc_rthashs[cpu] =
3023 kmalloc(sizeof(struct bridge_rtnode_head) * BRIDGE_RTHASH_SIZE,
3024 M_DEVBUF, M_WAITOK);
3026 for (i = 0; i < BRIDGE_RTHASH_SIZE; i++)
3027 LIST_INIT(&sc->sc_rthashs[cpu][i]);
3029 sc->sc_rthash_key = karc4random();
3032 * Initialize per-cpu lists
3034 sc->sc_rtlists = kmalloc(sizeof(struct bridge_rtnode_head) * ncpus,
3035 M_DEVBUF, M_WAITOK);
3036 for (cpu = 0; cpu < ncpus; ++cpu)
3037 LIST_INIT(&sc->sc_rtlists[cpu]);
3041 * bridge_rtable_fini:
3043 * Deconstruct the route table for this bridge.
3046 bridge_rtable_fini(struct bridge_softc *sc)
3051 * Free per-cpu hash tables
3053 for (cpu = 0; cpu < ncpus; ++cpu)
3054 kfree(sc->sc_rthashs[cpu], M_DEVBUF);
3055 kfree(sc->sc_rthashs, M_DEVBUF);
3058 * Free per-cpu lists
3060 kfree(sc->sc_rtlists, M_DEVBUF);
3064 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
3065 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
3067 #define mix(a, b, c) \
3069 a -= b; a -= c; a ^= (c >> 13); \
3070 b -= c; b -= a; b ^= (a << 8); \
3071 c -= a; c -= b; c ^= (b >> 13); \
3072 a -= b; a -= c; a ^= (c >> 12); \
3073 b -= c; b -= a; b ^= (a << 16); \
3074 c -= a; c -= b; c ^= (b >> 5); \
3075 a -= b; a -= c; a ^= (c >> 3); \
3076 b -= c; b -= a; b ^= (a << 10); \
3077 c -= a; c -= b; c ^= (b >> 15); \
3078 } while (/*CONSTCOND*/0)
3080 static __inline uint32_t
3081 bridge_rthash(struct bridge_softc *sc, const uint8_t *addr)
3083 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key;
3094 return (c & BRIDGE_RTHASH_MASK);
3100 bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b)
3104 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) {
3105 d = ((int)a[i]) - ((int)b[i]);
3112 * bridge_rtnode_lookup:
3114 * Look up a bridge route node for the specified destination.
3116 static struct bridge_rtnode *
3117 bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr)
3119 struct bridge_rtnode *brt;
3123 hash = bridge_rthash(sc, addr);
3124 LIST_FOREACH(brt, &sc->sc_rthashs[mycpuid][hash], brt_hash) {
3125 dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr);
3136 * bridge_rtnode_insert:
3138 * Insert the specified bridge node into the route table.
3139 * Caller has to make sure that rtnode does not exist.
3142 bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt)
3144 struct bridge_rtnode *lbrt;
3148 hash = bridge_rthash(sc, brt->brt_addr);
3150 lbrt = LIST_FIRST(&sc->sc_rthashs[mycpuid][hash]);
3152 LIST_INSERT_HEAD(&sc->sc_rthashs[mycpuid][hash], brt, brt_hash);
3157 dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr);
3158 KASSERT(dir != 0, ("rtnode already exist\n"));
3161 LIST_INSERT_BEFORE(lbrt, brt, brt_hash);
3164 if (LIST_NEXT(lbrt, brt_hash) == NULL) {
3165 LIST_INSERT_AFTER(lbrt, brt, brt_hash);
3168 lbrt = LIST_NEXT(lbrt, brt_hash);
3169 } while (lbrt != NULL);
3171 panic("no suitable position found for rtnode\n");
3173 LIST_INSERT_HEAD(&sc->sc_rtlists[mycpuid], brt, brt_list);
3176 * Update the brtcnt.
3177 * We only need to do it once and we do it on CPU0.
3184 * bridge_rtnode_destroy:
3186 * Destroy a bridge rtnode.
3189 bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt)
3191 LIST_REMOVE(brt, brt_hash);
3192 LIST_REMOVE(brt, brt_list);
3194 if (mycpuid + 1 == ncpus) {
3195 /* Free rtinfo associated with rtnode on the last cpu */
3196 kfree(brt->brt_info, M_DEVBUF);
3198 kfree(brt, M_DEVBUF);
3201 /* Update brtcnt only on CPU0 */
3207 bridge_post_pfil(struct mbuf *m)
3209 if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED)
3213 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED)
3220 * Send bridge packets through pfil if they are one of the types pfil can deal
3221 * with, or if they are ARP or REVARP. (pfil will pass ARP and REVARP without
3222 * question.) If *bifp or *ifp are NULL then packet filtering is skipped for
3226 bridge_pfil(struct mbuf **mp, struct ifnet *bifp, struct ifnet *ifp, int dir)
3228 int snap, error, i, hlen;
3229 struct ether_header *eh1, eh2;
3232 u_int16_t ether_type;
3235 error = -1; /* Default error if not error == 0 */
3237 if (pfil_bridge == 0 && pfil_member == 0)
3238 return (0); /* filtering is disabled */
3240 i = min((*mp)->m_pkthdr.len, max_protohdr);
3241 if ((*mp)->m_len < i) {
3242 *mp = m_pullup(*mp, i);
3244 kprintf("%s: m_pullup failed\n", __func__);
3249 eh1 = mtod(*mp, struct ether_header *);
3250 ether_type = ntohs(eh1->ether_type);
3253 * Check for SNAP/LLC.
3255 if (ether_type < ETHERMTU) {
3256 struct llc *llc2 = (struct llc *)(eh1 + 1);
3258 if ((*mp)->m_len >= ETHER_HDR_LEN + 8 &&
3259 llc2->llc_dsap == LLC_SNAP_LSAP &&
3260 llc2->llc_ssap == LLC_SNAP_LSAP &&
3261 llc2->llc_control == LLC_UI) {
3262 ether_type = htons(llc2->llc_un.type_snap.ether_type);
3268 * If we're trying to filter bridge traffic, don't look at anything
3269 * other than IP and ARP traffic. If the filter doesn't understand
3270 * IPv6, don't allow IPv6 through the bridge either. This is lame
3271 * since if we really wanted, say, an AppleTalk filter, we are hosed,
3272 * but of course we don't have an AppleTalk filter to begin with.
3273 * (Note that since pfil doesn't understand ARP it will pass *ALL*
3276 switch (ether_type) {
3278 case ETHERTYPE_REVARP:
3279 return (0); /* Automatically pass */
3283 case ETHERTYPE_IPV6:
3289 * Check to see if the user wants to pass non-ip
3290 * packets, these will not be checked by pfil(9)
3291 * and passed unconditionally so the default is to drop.
3297 /* Strip off the Ethernet header and keep a copy. */
3298 m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t) &eh2);
3299 m_adj(*mp, ETHER_HDR_LEN);
3301 /* Strip off snap header, if present */
3303 m_copydata(*mp, 0, sizeof(struct llc), (caddr_t) &llc1);
3304 m_adj(*mp, sizeof(struct llc));
3308 * Check the IP header for alignment and errors
3310 if (dir == PFIL_IN) {
3311 switch (ether_type) {
3313 error = bridge_ip_checkbasic(mp);
3316 case ETHERTYPE_IPV6:
3317 error = bridge_ip6_checkbasic(mp);
3330 * Run the packet through pfil
3332 switch (ether_type) {
3335 * before calling the firewall, swap fields the same as
3336 * IP does. here we assume the header is contiguous
3338 ip = mtod(*mp, struct ip *);
3340 ip->ip_len = ntohs(ip->ip_len);
3341 ip->ip_off = ntohs(ip->ip_off);
3344 * Run pfil on the member interface and the bridge, both can
3345 * be skipped by clearing pfil_member or pfil_bridge.
3348 * in_if -> bridge_if -> out_if
3350 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL) {
3351 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3352 if (*mp == NULL || error != 0) /* filter may consume */
3354 error = bridge_post_pfil(*mp);
3359 if (pfil_member && ifp != NULL) {
3360 error = pfil_run_hooks(&inet_pfil_hook, mp, ifp, dir);
3361 if (*mp == NULL || error != 0) /* filter may consume */
3363 error = bridge_post_pfil(*mp);
3368 if (pfil_bridge && dir == PFIL_IN && bifp != NULL) {
3369 error = pfil_run_hooks(&inet_pfil_hook, mp, bifp, dir);
3370 if (*mp == NULL || error != 0) /* filter may consume */
3372 error = bridge_post_pfil(*mp);
3377 /* check if we need to fragment the packet */
3378 if (pfil_member && ifp != NULL && dir == PFIL_OUT) {
3379 i = (*mp)->m_pkthdr.len;
3380 if (i > ifp->if_mtu) {
3381 error = bridge_fragment(ifp, *mp, &eh2, snap,
3387 /* Recalculate the ip checksum and restore byte ordering */
3388 ip = mtod(*mp, struct ip *);
3389 hlen = ip->ip_hl << 2;
3390 if (hlen < sizeof(struct ip))
3392 if (hlen > (*mp)->m_len) {
3393 if ((*mp = m_pullup(*mp, hlen)) == 0)
3395 ip = mtod(*mp, struct ip *);
3399 ip->ip_len = htons(ip->ip_len);
3400 ip->ip_off = htons(ip->ip_off);
3402 if (hlen == sizeof(struct ip))
3403 ip->ip_sum = in_cksum_hdr(ip);
3405 ip->ip_sum = in_cksum(*mp, hlen);
3409 case ETHERTYPE_IPV6:
3410 if (pfil_bridge && dir == PFIL_OUT && bifp != NULL)
3411 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3414 if (*mp == NULL || error != 0) /* filter may consume */
3417 if (pfil_member && ifp != NULL)
3418 error = pfil_run_hooks(&inet6_pfil_hook, mp, ifp,
3421 if (*mp == NULL || error != 0) /* filter may consume */
3424 if (pfil_bridge && dir == PFIL_IN && bifp != NULL)
3425 error = pfil_run_hooks(&inet6_pfil_hook, mp, bifp,
3442 * Finally, put everything back the way it was and return
3445 M_PREPEND(*mp, sizeof(struct llc), MB_DONTWAIT);
3448 bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc));
3451 M_PREPEND(*mp, ETHER_HDR_LEN, MB_DONTWAIT);
3454 bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN);
3465 * Perform basic checks on header size since
3466 * pfil assumes ip_input has already processed
3467 * it for it. Cut-and-pasted from ip_input.c.
3468 * Given how simple the IPv6 version is,
3469 * does the IPv4 version really need to be
3472 * XXX Should we update ipstat here, or not?
3473 * XXX Right now we update ipstat but not
3477 bridge_ip_checkbasic(struct mbuf **mp)
3479 struct mbuf *m = *mp;
3487 if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3488 if ((m = m_copyup(m, sizeof(struct ip),
3489 (max_linkhdr + 3) & ~3)) == NULL) {
3490 /* XXXJRT new stat, please */
3491 ipstat.ips_toosmall++;
3496 #ifndef __predict_false
3497 #define __predict_false(x) x
3499 if (__predict_false(m->m_len < sizeof (struct ip))) {
3500 if ((m = m_pullup(m, sizeof (struct ip))) == NULL) {
3501 ipstat.ips_toosmall++;
3505 ip = mtod(m, struct ip *);
3506 if (ip == NULL) goto bad;
3508 if (ip->ip_v != IPVERSION) {
3509 ipstat.ips_badvers++;
3512 hlen = ip->ip_hl << 2;
3513 if (hlen < sizeof(struct ip)) { /* minimum header length */
3514 ipstat.ips_badhlen++;
3517 if (hlen > m->m_len) {
3518 if ((m = m_pullup(m, hlen)) == 0) {
3519 ipstat.ips_badhlen++;
3522 ip = mtod(m, struct ip *);
3523 if (ip == NULL) goto bad;
3526 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
3527 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
3529 if (hlen == sizeof(struct ip)) {
3530 sum = in_cksum_hdr(ip);
3532 sum = in_cksum(m, hlen);
3536 ipstat.ips_badsum++;
3540 /* Retrieve the packet length. */
3541 len = ntohs(ip->ip_len);
3544 * Check for additional length bogosity
3547 ipstat.ips_badlen++;
3552 * Check that the amount of data in the buffers
3553 * is as at least much as the IP header would have us expect.
3554 * Drop packet if shorter than we expect.
3556 if (m->m_pkthdr.len < len) {
3557 ipstat.ips_tooshort++;
3561 /* Checks out, proceed */
3572 * Same as above, but for IPv6.
3573 * Cut-and-pasted from ip6_input.c.
3574 * XXX Should we update ip6stat, or not?
3577 bridge_ip6_checkbasic(struct mbuf **mp)
3579 struct mbuf *m = *mp;
3580 struct ip6_hdr *ip6;
3583 * If the IPv6 header is not aligned, slurp it up into a new
3584 * mbuf with space for link headers, in the event we forward
3585 * it. Otherwise, if it is aligned, make sure the entire base
3586 * IPv6 header is in the first mbuf of the chain.
3589 if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
3590 struct ifnet *inifp = m->m_pkthdr.rcvif;
3591 if ((m = m_copyup(m, sizeof(struct ip6_hdr),
3592 (max_linkhdr + 3) & ~3)) == NULL) {
3593 /* XXXJRT new stat, please */
3594 ip6stat.ip6s_toosmall++;
3595 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3600 if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
3601 struct ifnet *inifp = m->m_pkthdr.rcvif;
3602 if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
3603 ip6stat.ip6s_toosmall++;
3604 in6_ifstat_inc(inifp, ifs6_in_hdrerr);
3609 ip6 = mtod(m, struct ip6_hdr *);
3611 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
3612 ip6stat.ip6s_badvers++;
3613 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
3617 /* Checks out, proceed */
3630 * Return a fragmented mbuf chain.
3633 bridge_fragment(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh,
3634 int snap, struct llc *llc)
3640 if (m->m_len < sizeof(struct ip) &&
3641 (m = m_pullup(m, sizeof(struct ip))) == NULL)
3643 ip = mtod(m, struct ip *);
3645 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist,
3650 /* walk the chain and re-add the Ethernet header */
3651 for (m0 = m; m0; m0 = m0->m_nextpkt) {
3654 M_PREPEND(m0, sizeof(struct llc), MB_DONTWAIT);
3659 bcopy(llc, mtod(m0, caddr_t),
3660 sizeof(struct llc));
3662 M_PREPEND(m0, ETHER_HDR_LEN, MB_DONTWAIT);
3667 bcopy(eh, mtod(m0, caddr_t), ETHER_HDR_LEN);
3673 ipstat.ips_fragmented++;
3684 bridge_enqueue_handler(netmsg_t msg)
3686 struct netmsg_packet *nmp;
3687 struct ifnet *dst_ifp;
3692 dst_ifp = nmp->base.lmsg.u.ms_resultp;
3694 bridge_handoff(dst_ifp, m, 1);
3698 bridge_handoff(struct ifnet *dst_ifp, struct mbuf *m, int from_us)
3703 bifp = ((struct bridge_softc *)dst_ifp->if_bridge)->sc_ifp;
3705 /* We may be sending a fragment so traverse the mbuf */
3707 struct altq_pktattr pktattr;
3710 m->m_nextpkt = NULL;
3713 * If being sent from our host override ether_shost
3714 * so any replies go the correct interface. This is
3715 * mandatory or ARP replies will wind up on the wrong
3718 * Otherwise if we are in transparent mode
3722 offsetof(struct ether_header, ether_shost),
3723 ETHER_ADDR_LEN, IF_LLADDR(dst_ifp));
3724 } else if ((bifp->if_flags & IFF_LINK0) &&
3725 (m->m_pkthdr.fw_flags & BRIDGE_MBUF_TAGGED)) {
3727 offsetof(struct ether_header, ether_shost),
3729 m->m_pkthdr.br.ether.ether_shost);
3732 if (ifq_is_enabled(&dst_ifp->if_snd))
3733 altq_etherclassify(&dst_ifp->if_snd, m, &pktattr);
3735 ifq_dispatch(dst_ifp, m, &pktattr);
3740 bridge_control_dispatch(netmsg_t msg)
3742 struct netmsg_brctl *bc_msg = (struct netmsg_brctl *)msg;
3743 struct ifnet *bifp = bc_msg->bc_sc->sc_ifp;
3746 ifnet_serialize_all(bifp);
3747 error = bc_msg->bc_func(bc_msg->bc_sc, bc_msg->bc_arg);
3748 ifnet_deserialize_all(bifp);
3750 lwkt_replymsg(&bc_msg->base.lmsg, error);
3754 bridge_control(struct bridge_softc *sc, u_long cmd,
3755 bridge_ctl_t bc_func, void *bc_arg)
3757 struct ifnet *bifp = sc->sc_ifp;
3758 struct netmsg_brctl bc_msg;
3761 ASSERT_IFNET_SERIALIZED_ALL(bifp);
3763 bzero(&bc_msg, sizeof(bc_msg));
3765 netmsg_init(&bc_msg.base, NULL, &curthread->td_msgport,
3766 0, bridge_control_dispatch);
3767 bc_msg.bc_func = bc_func;
3769 bc_msg.bc_arg = bc_arg;
3771 ifnet_deserialize_all(bifp);
3772 error = lwkt_domsg(BRIDGE_CFGPORT, &bc_msg.base.lmsg, 0);
3773 ifnet_serialize_all(bifp);
3778 bridge_add_bif_handler(netmsg_t msg)
3780 struct netmsg_braddbif *amsg = (struct netmsg_braddbif *)msg;
3781 struct bridge_softc *sc;
3782 struct bridge_iflist *bif;
3784 sc = amsg->br_softc;
3786 bif = kmalloc(sizeof(*bif), M_DEVBUF, M_WAITOK | M_ZERO);
3787 bif->bif_ifp = amsg->br_bif_ifp;
3788 bif->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
3789 bif->bif_onlist = 1;
3790 bif->bif_info = amsg->br_bif_info;
3792 LIST_INSERT_HEAD(&sc->sc_iflists[mycpuid], bif, bif_next);
3794 ifnet_forwardmsg(&amsg->base.lmsg, mycpuid + 1);
3798 bridge_add_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3801 struct netmsg_braddbif amsg;
3803 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3805 netmsg_init(&amsg.base, NULL, &curthread->td_msgport,
3806 0, bridge_add_bif_handler);
3808 amsg.br_bif_info = bif_info;
3809 amsg.br_bif_ifp = ifp;
3811 ifnet_domsg(&amsg.base.lmsg, 0);
3815 bridge_del_bif_handler(netmsg_t msg)
3817 struct netmsg_brdelbif *dmsg = (struct netmsg_brdelbif *)msg;
3818 struct bridge_softc *sc;
3819 struct bridge_iflist *bif;
3821 sc = dmsg->br_softc;
3824 * Locate the bif associated with the br_bif_info
3825 * on the current CPU
3827 bif = bridge_lookup_member_ifinfo(sc, dmsg->br_bif_info);
3828 KKASSERT(bif != NULL && bif->bif_onlist);
3830 /* Remove the bif from the current CPU's iflist */
3831 bif->bif_onlist = 0;
3832 LIST_REMOVE(bif, bif_next);
3834 /* Save the removed bif for later freeing */
3835 LIST_INSERT_HEAD(dmsg->br_bif_list, bif, bif_next);
3837 ifnet_forwardmsg(&dmsg->base.lmsg, mycpuid + 1);
3841 bridge_del_bif(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3842 struct bridge_iflist_head *saved_bifs)
3844 struct netmsg_brdelbif dmsg;
3846 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3848 netmsg_init(&dmsg.base, NULL, &curthread->td_msgport,
3849 0, bridge_del_bif_handler);
3851 dmsg.br_bif_info = bif_info;
3852 dmsg.br_bif_list = saved_bifs;
3854 ifnet_domsg(&dmsg.base.lmsg, 0);
3858 bridge_set_bifflags_handler(netmsg_t msg)
3860 struct netmsg_brsflags *smsg = (struct netmsg_brsflags *)msg;
3861 struct bridge_softc *sc;
3862 struct bridge_iflist *bif;
3864 sc = smsg->br_softc;
3867 * Locate the bif associated with the br_bif_info
3868 * on the current CPU
3870 bif = bridge_lookup_member_ifinfo(sc, smsg->br_bif_info);
3871 KKASSERT(bif != NULL && bif->bif_onlist);
3873 bif->bif_flags = smsg->br_bif_flags;
3875 ifnet_forwardmsg(&smsg->base.lmsg, mycpuid + 1);
3879 bridge_set_bifflags(struct bridge_softc *sc, struct bridge_ifinfo *bif_info,
3882 struct netmsg_brsflags smsg;
3884 ASSERT_IFNET_NOT_SERIALIZED_ALL(sc->sc_ifp);
3886 netmsg_init(&smsg.base, NULL, &curthread->td_msgport,
3887 0, bridge_set_bifflags_handler);
3889 smsg.br_bif_info = bif_info;
3890 smsg.br_bif_flags = bif_flags;
3892 ifnet_domsg(&smsg.base.lmsg, 0);