2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3 * Copyright (c) 2003 Ryan McBride. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/sys/netinet/ip_carp.c,v 1.48 2007/02/02 09:39:09 glebius Exp $
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/in_cksum.h>
38 #include <sys/limits.h>
39 #include <sys/malloc.h>
41 #include <sys/msgport2.h>
45 #include <sys/sockio.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/thread.h>
51 #include <machine/stdarg.h>
52 #include <crypto/sha1.h>
55 #include <net/ethernet.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 #include <net/if_clone.h>
61 #include <net/if_var.h>
62 #include <net/ifq_var.h>
63 #include <net/netmsg2.h>
64 #include <net/netisr2.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/if_ether.h>
76 #include <netinet/icmp6.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet6/scope6_var.h>
80 #include <netinet6/nd6.h>
83 #include <netinet/ip_carp.h>
86 * Note about carp's MP safe approach:
88 * Brief: carp_softc (softc), carp_softc_container (scc)
90 * - All configuration operation, e.g. ioctl, add/delete inet addresses
91 * is serialized by netisr0; not by carp's serializer
93 * - Backing interface's if_carp and carp_softc's relationship:
96 * if_carp -->| carp_if |
102 * | scc |-->| softc |
108 * | scc |-->| softc |
112 * - if_carp creation, modification and deletion all happen in netisr0,
113 * as stated previously. Since if_carp is accessed by multiple netisrs,
114 * the modification to if_carp is conducted in the following way:
118 * 1) Duplicate the old carp_if to new carp_if (ncif), and insert the
119 * to-be-added carp_softc to the new carp_if (ncif):
124 * +---------+ +---------+
125 * | carp_if | | carp_if |
126 * +---------+ +---------+
130 * +-----+ | | +-----+
131 * | scc |---->| softc |<----| scc |
132 * +-----+ | | +-----+
136 * +-----+ | | +-----+
137 * | scc |---->| softc |<----| scc |
138 * +-----+ | | +-----+
143 * | softc |<----| scc |
147 * 2) Switch save if_carp into ocif and switch if_carp to ncif:
152 * +---------+ +---------+
153 * | carp_if | | carp_if |
154 * +---------+ +---------+
158 * +-----+ | | +-----+
159 * | scc |---->| softc |<----| scc |
160 * +-----+ | | +-----+
164 * +-----+ | | +-----+
165 * | scc |---->| softc |<----| scc |
166 * +-----+ | | +-----+
171 * | softc |<----| scc |
175 * 3) Run netmsg_service_sync(), which will make sure that
176 * ocif is no longer accessed (all network operations
177 * are happened only in network threads).
178 * 4) Free ocif -- only carp_if and scc are freed.
181 * Removing carp_softc:
183 * 1) Duplicate the old carp_if to new carp_if (ncif); the to-be-deleted
184 * carp_softc will not be duplicated.
189 * +---------+ +---------+
190 * | carp_if | | carp_if |
191 * +---------+ +---------+
195 * +-----+ | | +-----+
196 * | scc |---->| softc |<----| scc |
197 * +-----+ | | +-----+
202 * | scc |---->| softc | |
207 * +-----+ | | +-----+
208 * | scc |---->| softc |<----| scc |
209 * +-----+ | | +-----+
212 * 2) Switch save if_carp into ocif and switch if_carp to ncif:
217 * +---------+ +---------+
218 * | carp_if | | carp_if |
219 * +---------+ +---------+
223 * +-----+ | | +-----+
224 * | scc |---->| softc |<----| scc |
225 * +-----+ | | +-----+
230 * | scc |---->| softc | |
235 * +-----+ | | +-----+
236 * | scc |---->| softc |<----| scc |
237 * +-----+ | | +-----+
240 * 3) Run netmsg_service_sync(), which will make sure that
241 * ocif is no longer accessed (all network operations
242 * are happened only in network threads).
243 * 4) Free ocif -- only carp_if and scc are freed.
245 * - if_carp accessing:
246 * The accessing code should cache the if_carp in a local temporary
247 * variable and accessing the temporary variable along the code path
248 * instead of accessing if_carp later on.
251 #define CARP_IFNAME "carp"
252 #define CARP_IS_RUNNING(ifp) \
253 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
258 uint32_t vha_flags; /* CARP_VHAF_ */
259 struct in_ifaddr *vha_ia; /* carp address */
260 struct in_ifaddr *vha_iaback; /* backing address */
261 TAILQ_ENTRY(carp_vhaddr) vha_link;
263 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr);
266 struct netmsg_base base;
267 struct ifnet *nc_carpdev;
268 struct carp_softc *nc_softc;
274 struct arpcom arpcom;
275 struct ifnet *sc_carpdev; /* parent interface */
276 struct carp_vhaddr_list sc_vha_list; /* virtual addr list */
278 const struct in_ifaddr *sc_ia; /* primary iface address v4 */
279 struct ip_moptions sc_imo;
282 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
283 struct ip6_moptions sc_im6o;
286 enum { INIT = 0, BACKUP, MASTER }
292 int sc_sendad_errors;
293 #define CARP_SENDAD_MAX_ERRORS 3
294 int sc_sendad_success;
295 #define CARP_SENDAD_MIN_SUCCESS 3
299 int sc_naddrs; /* actually used IPv4 vha */
301 int sc_advbase; /* seconds */
306 #define CARP_HMAC_PAD 64
307 unsigned char sc_key[CARP_KEY_LEN];
308 unsigned char sc_pad[CARP_HMAC_PAD];
311 struct callout sc_ad_tmo; /* advertisement timeout */
312 struct netmsg_carp sc_ad_msg; /* adv timeout netmsg */
313 struct callout sc_md_tmo; /* ip4 master down timeout */
314 struct callout sc_md6_tmo; /* ip6 master down timeout */
315 struct netmsg_carp sc_md_msg; /* master down timeout netmsg */
317 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
320 #define sc_if arpcom.ac_if
322 struct carp_softc_container {
323 TAILQ_ENTRY(carp_softc_container) scc_link;
324 struct carp_softc *scc_softc;
326 TAILQ_HEAD(carp_if, carp_softc_container);
328 SYSCTL_DECL(_net_inet_carp);
330 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
331 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
332 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
333 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
334 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
335 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
336 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
337 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
338 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
340 static int carp_suppress_preempt = 0;
341 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
342 &carp_suppress_preempt, 0, "Preemption is suppressed");
344 static struct carpstats carpstats;
345 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
346 &carpstats, carpstats,
347 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
349 #define CARP_LOG(...) do { \
350 if (carp_opts[CARPCTL_LOG] > 0) \
351 log(LOG_INFO, __VA_ARGS__); \
354 #define CARP_DEBUG(...) do { \
355 if (carp_opts[CARPCTL_LOG] > 1) \
356 log(LOG_DEBUG, __VA_ARGS__); \
359 static struct lwkt_token carp_listtok = LWKT_TOKEN_INITIALIZER(carp_list_token);
361 static void carp_hmac_prepare(struct carp_softc *);
362 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
364 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
366 static void carp_setroute(struct carp_softc *, int);
367 static void carp_proto_input_c(struct carp_softc *, struct mbuf *,
368 struct carp_header *, sa_family_t);
369 static int carp_clone_create(struct if_clone *, int, caddr_t);
370 static int carp_clone_destroy(struct ifnet *);
371 static void carp_detach(struct carp_softc *, boolean_t, boolean_t);
372 static void carp_prepare_ad(struct carp_softc *, struct carp_header *);
373 static void carp_send_ad_all(void);
374 static void carp_send_ad_timeout(void *);
375 static void carp_send_ad(struct carp_softc *);
376 static void carp_send_arp(struct carp_softc *);
377 static void carp_master_down_timeout(void *);
378 static void carp_master_down(struct carp_softc *);
379 static void carp_setrun(struct carp_softc *, sa_family_t);
380 static void carp_set_state(struct carp_softc *, int);
381 static struct ifnet *carp_forus(struct carp_if *, const uint8_t *);
383 static void carp_init(void *);
384 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
385 static int carp_output(struct ifnet *, struct mbuf *, struct sockaddr *,
387 static void carp_start(struct ifnet *, struct ifaltq_subque *);
389 static void carp_multicast_cleanup(struct carp_softc *);
390 static void carp_add_addr(struct carp_softc *, struct ifaddr *);
391 static void carp_del_addr(struct carp_softc *, struct ifaddr *);
392 static void carp_config_addr(struct carp_softc *, struct ifaddr *);
393 static void carp_link_addrs(struct carp_softc *, struct ifnet *,
395 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *,
397 static void carp_update_addrs(struct carp_softc *, struct ifaddr *);
399 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *,
401 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
402 struct ifnet *, struct in_ifaddr *, int);
403 static void carp_deactivate_vhaddr(struct carp_softc *,
404 struct carp_vhaddr *, boolean_t);
405 static int carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *);
406 static void carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *,
410 static void carp_sc_state(struct carp_softc *);
413 static void carp_send_na(struct carp_softc *);
415 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
416 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
418 static void carp_multicast6_cleanup(struct carp_softc *);
420 static void carp_stop(struct carp_softc *, boolean_t);
421 static void carp_suspend(struct carp_softc *, boolean_t);
422 static void carp_ioctl_stop(struct carp_softc *);
423 static int carp_ioctl_setvh(struct carp_softc *, void *, struct ucred *);
424 static void carp_ioctl_ifcap(struct carp_softc *, int);
425 static int carp_ioctl_getvh(struct carp_softc *, void *, struct ucred *);
426 static int carp_ioctl_getdevname(struct carp_softc *, struct ifdrv *);
427 static int carp_ioctl_getvhaddr(struct carp_softc *, struct ifdrv *);
429 static struct carp_if *carp_if_remove(struct carp_if *, struct carp_softc *);
430 static struct carp_if *carp_if_insert(struct carp_if *, struct carp_softc *);
431 static void carp_if_free(struct carp_if *);
433 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
435 static void carp_ifdetach(void *, struct ifnet *);
437 static void carp_ifdetach_dispatch(netmsg_t);
438 static void carp_clone_destroy_dispatch(netmsg_t);
439 static void carp_init_dispatch(netmsg_t);
440 static void carp_ioctl_stop_dispatch(netmsg_t);
441 static void carp_ioctl_setvh_dispatch(netmsg_t);
442 static void carp_ioctl_ifcap_dispatch(netmsg_t);
443 static void carp_ioctl_getvh_dispatch(netmsg_t);
444 static void carp_ioctl_getdevname_dispatch(netmsg_t);
445 static void carp_ioctl_getvhaddr_dispatch(netmsg_t);
446 static void carp_send_ad_timeout_dispatch(netmsg_t);
447 static void carp_master_down_timeout_dispatch(netmsg_t);
449 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
451 static LIST_HEAD(, carp_softc) carpif_list;
453 static struct if_clone carp_cloner =
454 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
457 static const uint8_t carp_etheraddr[ETHER_ADDR_LEN] =
458 { 0, 0, 0x5e, 0, 1, 0 };
460 static eventhandler_tag carp_ifdetach_event;
461 static eventhandler_tag carp_ifaddr_event;
464 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
466 struct carp_vhaddr *vha;
467 u_long new_addr, addr;
469 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
472 * Virtual address list is sorted; smaller one first
474 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
476 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
477 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
483 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
485 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
486 vha_new->vha_flags |= CARP_VHAF_ONLIST;
490 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
492 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
493 vha->vha_flags &= ~CARP_VHAF_ONLIST;
494 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
498 carp_hmac_prepare(struct carp_softc *sc)
500 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
501 uint8_t vhid = sc->sc_vhid & 0xff;
504 struct ifaddr_container *ifac;
508 struct carp_vhaddr *vha;
511 /* XXX: possible race here */
513 /* compute ipad from key */
514 bzero(sc->sc_pad, sizeof(sc->sc_pad));
515 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
516 for (i = 0; i < sizeof(sc->sc_pad); i++)
517 sc->sc_pad[i] ^= 0x36;
519 /* precompute first part of inner hash */
520 SHA1Init(&sc->sc_sha1);
521 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
522 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
523 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
524 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
526 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
527 SHA1Update(&sc->sc_sha1,
528 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
529 sizeof(struct in_addr));
533 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
534 struct ifaddr *ifa = ifac->ifa;
536 if (ifa->ifa_addr->sa_family == AF_INET6) {
537 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
538 in6_clearscope(&in6);
539 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
544 /* convert ipad to opad */
545 for (i = 0; i < sizeof(sc->sc_pad); i++)
546 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
550 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
551 unsigned char md[20])
555 /* fetch first half of inner hash */
556 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
558 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
559 SHA1Final(md, &sha1ctx);
563 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
564 SHA1Update(&sha1ctx, md, 20);
565 SHA1Final(md, &sha1ctx);
569 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
570 unsigned char md[20])
572 unsigned char md2[20];
574 carp_hmac_generate(sc, counter, md2);
575 return (bcmp(md, md2, sizeof(md2)));
579 carp_setroute(struct carp_softc *sc, int cmd)
582 struct ifaddr_container *ifac;
584 struct carp_vhaddr *vha;
586 KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD);
588 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
589 if (vha->vha_iaback == NULL)
591 if (cmd == RTM_DELETE)
592 carp_delroute_vhaddr(sc, vha, FALSE);
594 carp_addroute_vhaddr(sc, vha);
598 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
599 struct ifaddr *ifa = ifac->ifa;
601 if (ifa->ifa_addr->sa_family == AF_INET6) {
612 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
614 struct carp_softc *sc;
617 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
621 sc->sc_advbase = CARP_DFLTINTV;
622 sc->sc_vhid = -1; /* required setting */
624 sc->sc_init_counter = 1;
628 TAILQ_INIT(&sc->sc_vha_list);
631 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
634 callout_init_mp(&sc->sc_ad_tmo);
635 netmsg_init(&sc->sc_ad_msg.base, NULL, &netisr_adone_rport,
636 MSGF_DROPABLE | MSGF_PRIORITY, carp_send_ad_timeout_dispatch);
637 sc->sc_ad_msg.nc_softc = sc;
639 callout_init_mp(&sc->sc_md_tmo);
640 callout_init_mp(&sc->sc_md6_tmo);
641 netmsg_init(&sc->sc_md_msg.base, NULL, &netisr_adone_rport,
642 MSGF_DROPABLE | MSGF_PRIORITY, carp_master_down_timeout_dispatch);
643 sc->sc_md_msg.nc_softc = sc;
645 if_initname(ifp, CARP_IFNAME, unit);
647 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
648 ifp->if_init = carp_init;
649 ifp->if_ioctl = carp_ioctl;
650 ifp->if_start = carp_start;
652 ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_TSO;
653 ifp->if_capenable = ifp->if_capabilities;
655 * Leave if_hwassist as it is; if_hwassist will be
656 * setup when this carp interface has parent.
659 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
660 ifq_set_ready(&ifp->if_snd);
662 ether_ifattach(ifp, carp_etheraddr, NULL);
664 ifp->if_type = IFT_CARP;
665 ifp->if_output = carp_output;
667 lwkt_gettoken(&carp_listtok);
668 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
669 lwkt_reltoken(&carp_listtok);
675 carp_clone_destroy_dispatch(netmsg_t msg)
677 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
678 struct carp_softc *sc = cmsg->nc_softc;
681 carp_detach(sc, TRUE, FALSE);
683 callout_stop_sync(&sc->sc_ad_tmo);
684 callout_stop_sync(&sc->sc_md_tmo);
685 callout_stop_sync(&sc->sc_md6_tmo);
688 lwkt_dropmsg(&sc->sc_ad_msg.base.lmsg);
689 lwkt_dropmsg(&sc->sc_md_msg.base.lmsg);
692 lwkt_replymsg(&cmsg->base.lmsg, 0);
696 carp_clone_destroy(struct ifnet *ifp)
698 struct carp_softc *sc = ifp->if_softc;
699 struct netmsg_carp cmsg;
701 bzero(&cmsg, sizeof(cmsg));
702 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
703 carp_clone_destroy_dispatch);
706 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
708 lwkt_gettoken(&carp_listtok);
709 LIST_REMOVE(sc, sc_next);
710 lwkt_reltoken(&carp_listtok);
715 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active"));
721 static struct carp_if *
722 carp_if_remove(struct carp_if *ocif, struct carp_softc *sc)
724 struct carp_softc_container *oscc, *scc;
731 TAILQ_FOREACH(oscc, ocif, scc_link) {
734 if (oscc->scc_softc == sc)
738 KASSERT(found, ("%s carp_softc is not on carp_if", __func__));
741 /* Last one is going to be unlinked */
745 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
748 TAILQ_FOREACH(oscc, ocif, scc_link) {
749 if (oscc->scc_softc == sc)
752 scc = kmalloc(sizeof(*scc), M_CARP, M_WAITOK | M_ZERO);
753 scc->scc_softc = oscc->scc_softc;
754 TAILQ_INSERT_TAIL(cif, scc, scc_link);
760 static struct carp_if *
761 carp_if_insert(struct carp_if *ocif, struct carp_softc *sc)
763 struct carp_softc_container *oscc;
768 TAILQ_FOREACH(oscc, ocif, scc_link) {
769 if (oscc->scc_softc == sc)
775 if (sc->sc_carpdev != NULL) {
776 KASSERT(onlist, ("%s is not on %s carp list",
777 sc->sc_if.if_xname, sc->sc_carpdev->if_xname));
779 KASSERT(!onlist, ("%s is already on carp list",
780 sc->sc_if.if_xname));
786 struct carp_softc_container *new_scc, *scc;
789 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
792 new_scc = kmalloc(sizeof(*new_scc), M_CARP, M_WAITOK | M_ZERO);
793 new_scc->scc_softc = sc;
796 TAILQ_FOREACH(oscc, ocif, scc_link) {
798 oscc->scc_softc->sc_vhid > sc->sc_vhid) {
799 TAILQ_INSERT_TAIL(cif, new_scc,
804 scc = kmalloc(sizeof(*scc), M_CARP,
806 scc->scc_softc = oscc->scc_softc;
807 TAILQ_INSERT_TAIL(cif, scc, scc_link);
811 TAILQ_INSERT_TAIL(cif, new_scc, scc_link);
820 carp_if_free(struct carp_if *cif)
822 struct carp_softc_container *scc;
824 while ((scc = TAILQ_FIRST(cif)) != NULL) {
825 TAILQ_REMOVE(cif, scc, scc_link);
832 carp_detach(struct carp_softc *sc, boolean_t detach, boolean_t del_iaback)
834 carp_suspend(sc, detach);
836 carp_multicast_cleanup(sc);
838 carp_multicast6_cleanup(sc);
841 if (!sc->sc_dead && detach) {
842 struct carp_vhaddr *vha;
844 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
845 carp_deactivate_vhaddr(sc, vha, del_iaback);
846 KKASSERT(sc->sc_naddrs == 0);
849 if (sc->sc_carpdev != NULL) {
850 struct ifnet *ifp = sc->sc_carpdev;
851 struct carp_if *ocif = ifp->if_carp;
853 ifp->if_carp = carp_if_remove(ocif, sc);
854 KASSERT(ifp->if_carp != ocif,
855 ("%s carp_if_remove failed", __func__));
857 sc->sc_carpdev = NULL;
859 sc->arpcom.ac_if.if_hwassist = 0;
862 * Make sure that all protocol threads see the
863 * sc_carpdev and if_carp changes
865 netmsg_service_sync();
867 if (ifp->if_carp == NULL) {
869 * No more carp interfaces using
870 * ifp as the backing interface,
871 * move it out of promiscous mode.
877 * The old carp list could be safely free now,
878 * since no one can access it.
885 carp_ifdetach_dispatch(netmsg_t msg)
887 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
888 struct ifnet *ifp = cmsg->nc_carpdev;
890 while (ifp->if_carp) {
891 struct carp_softc_container *scc;
893 scc = TAILQ_FIRST((struct carp_if *)(ifp->if_carp));
894 carp_detach(scc->scc_softc, TRUE, TRUE);
896 lwkt_replymsg(&cmsg->base.lmsg, 0);
899 /* Detach an interface from the carp. */
901 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
903 struct netmsg_carp cmsg;
905 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
907 bzero(&cmsg, sizeof(cmsg));
908 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
909 carp_ifdetach_dispatch);
910 cmsg.nc_carpdev = ifp;
912 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
916 * process input packet.
917 * we have rearranged checks order compared to the rfc,
918 * but it seems more efficient this way or not possible otherwise.
921 carp_proto_input(struct mbuf **mp, int *offp, int proto)
923 struct mbuf *m = *mp;
924 struct ip *ip = mtod(m, struct ip *);
925 struct ifnet *ifp = m->m_pkthdr.rcvif;
926 struct carp_header *ch;
927 struct carp_softc *sc;
933 carpstats.carps_ipackets++;
935 if (!carp_opts[CARPCTL_ALLOW]) {
940 /* Check if received on a valid carp interface */
941 if (ifp->if_type != IFT_CARP) {
942 carpstats.carps_badif++;
943 CARP_LOG("carp_proto_input: packet received on non-carp "
944 "interface: %s\n", ifp->if_xname);
949 if (!CARP_IS_RUNNING(ifp)) {
950 carpstats.carps_badif++;
951 CARP_LOG("carp_proto_input: packet received on stopped carp "
952 "interface: %s\n", ifp->if_xname);
958 if (sc->sc_carpdev == NULL) {
959 carpstats.carps_badif++;
960 CARP_LOG("carp_proto_input: packet received on defunc carp "
961 "interface: %s\n", ifp->if_xname);
966 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
967 carpstats.carps_badif++;
968 CARP_LOG("carp_proto_input: non-mcast packet on "
969 "interface: %s\n", ifp->if_xname);
974 /* Verify that the IP TTL is CARP_DFLTTL. */
975 if (ip->ip_ttl != CARP_DFLTTL) {
976 carpstats.carps_badttl++;
977 CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n",
978 ip->ip_ttl, CARP_DFLTTL, ifp->if_xname);
983 /* Minimal CARP packet size */
984 len = iphlen + sizeof(*ch);
987 * Verify that the received packet length is
988 * not less than the CARP header
990 if (m->m_pkthdr.len < len) {
991 carpstats.carps_badlen++;
992 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
998 /* Make sure that CARP header is contiguous */
999 if (len > m->m_len) {
1000 m = m_pullup(m, len);
1002 carpstats.carps_hdrops++;
1003 CARP_LOG("carp_proto_input: m_pullup failed\n");
1006 ip = mtod(m, struct ip *);
1008 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
1010 /* Verify the CARP checksum */
1011 if (in_cksum_skip(m, len, iphlen)) {
1012 carpstats.carps_badsum++;
1013 CARP_LOG("carp_proto_input: checksum failed on %s\n",
1018 carp_proto_input_c(sc, m, ch, AF_INET);
1020 return(IPPROTO_DONE);
1025 carp6_proto_input(struct mbuf **mp, int *offp, int proto)
1027 struct mbuf *m = *mp;
1028 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1029 struct ifnet *ifp = m->m_pkthdr.rcvif;
1030 struct carp_header *ch;
1031 struct carp_softc *sc;
1034 carpstats.carps_ipackets6++;
1036 if (!carp_opts[CARPCTL_ALLOW]) {
1041 /* check if received on a valid carp interface */
1042 if (ifp->if_type != IFT_CARP) {
1043 carpstats.carps_badif++;
1044 CARP_LOG("carp6_proto_input: packet received on non-carp "
1045 "interface: %s\n", ifp->if_xname);
1050 if (!CARP_IS_RUNNING(ifp)) {
1051 carpstats.carps_badif++;
1052 CARP_LOG("carp_proto_input: packet received on stopped carp "
1053 "interface: %s\n", ifp->if_xname);
1059 if (sc->sc_carpdev == NULL) {
1060 carpstats.carps_badif++;
1061 CARP_LOG("carp6_proto_input: packet received on defunc-carp "
1062 "interface: %s\n", ifp->if_xname);
1067 /* verify that the IP TTL is 255 */
1068 if (ip6->ip6_hlim != CARP_DFLTTL) {
1069 carpstats.carps_badttl++;
1070 CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n",
1071 ip6->ip6_hlim, ifp->if_xname);
1076 /* verify that we have a complete carp packet */
1078 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
1080 carpstats.carps_badlen++;
1081 CARP_LOG("carp6_proto_input: packet size %u too small\n", len);
1085 /* verify the CARP checksum */
1086 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
1087 carpstats.carps_badsum++;
1088 CARP_LOG("carp6_proto_input: checksum failed, on %s\n",
1094 carp_proto_input_c(sc, m, ch, AF_INET6);
1096 return (IPPROTO_DONE);
1101 carp_proto_input_c(struct carp_softc *sc, struct mbuf *m,
1102 struct carp_header *ch, sa_family_t af)
1105 uint64_t tmp_counter;
1106 struct timeval sc_tv, ch_tv;
1108 if (sc->sc_vhid != ch->carp_vhid) {
1110 * CARP uses multicast, however, multicast packets
1111 * are tapped to all CARP interfaces on the physical
1112 * interface receiving the CARP packets, so we don't
1113 * update any stats here.
1120 /* verify the CARP version. */
1121 if (ch->carp_version != CARP_VERSION) {
1122 carpstats.carps_badver++;
1123 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
1129 /* verify the hash */
1130 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
1131 carpstats.carps_badauth++;
1132 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
1137 tmp_counter = ntohl(ch->carp_counter[0]);
1138 tmp_counter = tmp_counter<<32;
1139 tmp_counter += ntohl(ch->carp_counter[1]);
1141 /* XXX Replay protection goes here */
1143 sc->sc_init_counter = 0;
1144 sc->sc_counter = tmp_counter;
1146 sc_tv.tv_sec = sc->sc_advbase;
1147 if (carp_suppress_preempt && sc->sc_advskew < 240)
1148 sc_tv.tv_usec = 240 * 1000000 / 256;
1150 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1151 ch_tv.tv_sec = ch->carp_advbase;
1152 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
1154 switch (sc->sc_state) {
1160 * If we receive an advertisement from a master who's going to
1161 * be more frequent than us, go into BACKUP state.
1163 if (timevalcmp(&sc_tv, &ch_tv, >) ||
1164 timevalcmp(&sc_tv, &ch_tv, ==)) {
1165 callout_stop(&sc->sc_ad_tmo);
1166 CARP_DEBUG("%s: MASTER -> BACKUP "
1167 "(more frequent advertisement received)\n",
1169 carp_set_state(sc, BACKUP);
1171 carp_setroute(sc, RTM_DELETE);
1177 * If we're pre-empting masters who advertise slower than us,
1178 * and this one claims to be slower, treat him as down.
1180 if (carp_opts[CARPCTL_PREEMPT] &&
1181 timevalcmp(&sc_tv, &ch_tv, <)) {
1182 CARP_DEBUG("%s: BACKUP -> MASTER "
1183 "(preempting a slower master)\n", cifp->if_xname);
1184 carp_master_down(sc);
1189 * If the master is going to advertise at such a low frequency
1190 * that he's guaranteed to time out, we'd might as well just
1191 * treat him as timed out now.
1193 sc_tv.tv_sec = sc->sc_advbase * 3;
1194 if (timevalcmp(&sc_tv, &ch_tv, <)) {
1195 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1197 carp_master_down(sc);
1202 * Otherwise, we reset the counter and wait for the next
1205 carp_setrun(sc, af);
1212 carp_input(void *v, struct mbuf *m)
1214 struct carp_if *cif = v;
1215 struct ether_header *eh;
1216 struct carp_softc_container *scc;
1219 eh = mtod(m, struct ether_header *);
1221 ifp = carp_forus(cif, eh->ether_dhost);
1223 ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF);
1227 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
1231 * XXX Should really check the list of multicast addresses
1232 * for each CARP interface _before_ copying.
1234 TAILQ_FOREACH(scc, cif, scc_link) {
1235 struct carp_softc *sc = scc->scc_softc;
1238 if ((sc->sc_if.if_flags & IFF_UP) == 0)
1241 m0 = m_dup(m, MB_DONTWAIT);
1245 ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF);
1251 carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch)
1253 if (sc->sc_init_counter) {
1254 /* this could also be seconds since unix epoch */
1255 sc->sc_counter = karc4random();
1256 sc->sc_counter = sc->sc_counter << 32;
1257 sc->sc_counter += karc4random();
1262 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
1263 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
1265 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
1269 carp_send_ad_all(void)
1271 struct carp_softc *sc;
1273 LIST_FOREACH(sc, &carpif_list, sc_next) {
1274 if (sc->sc_carpdev == NULL)
1277 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
1283 carp_send_ad_timeout(void *xsc)
1285 struct carp_softc *sc = xsc;
1286 struct netmsg_carp *cmsg = &sc->sc_ad_msg;
1288 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d",
1289 __func__, mycpuid));
1292 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1293 lwkt_sendmsg(netisr_cpuport(0), &cmsg->base.lmsg);
1298 carp_send_ad_timeout_dispatch(netmsg_t msg)
1300 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1301 struct carp_softc *sc = cmsg->nc_softc;
1305 lwkt_replymsg(&cmsg->base.lmsg, 0);
1312 carp_send_ad(struct carp_softc *sc)
1314 struct ifnet *cifp = &sc->sc_if;
1315 struct carp_header ch;
1317 struct carp_header *ch_ptr;
1319 int len, advbase, advskew;
1321 if (!CARP_IS_RUNNING(cifp)) {
1326 advbase = sc->sc_advbase;
1327 if (!carp_suppress_preempt || sc->sc_advskew > 240)
1328 advskew = sc->sc_advskew;
1331 tv.tv_sec = advbase;
1332 tv.tv_usec = advskew * 1000000 / 256;
1335 ch.carp_version = CARP_VERSION;
1336 ch.carp_type = CARP_ADVERTISEMENT;
1337 ch.carp_vhid = sc->sc_vhid;
1338 ch.carp_advbase = advbase;
1339 ch.carp_advskew = advskew;
1340 ch.carp_authlen = 7; /* XXX DEFINE */
1341 ch.carp_pad1 = 0; /* must be zero */
1345 if (sc->sc_ia != NULL) {
1348 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1350 IFNET_STAT_INC(cifp, oerrors, 1);
1351 carpstats.carps_onomem++;
1352 /* XXX maybe less ? */
1353 if (advbase != 255 || advskew != 255)
1354 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1355 carp_send_ad_timeout, sc);
1358 len = sizeof(*ip) + sizeof(ch);
1359 m->m_pkthdr.len = len;
1360 m->m_pkthdr.rcvif = NULL;
1362 MH_ALIGN(m, m->m_len);
1363 m->m_flags |= M_MCAST;
1364 ip = mtod(m, struct ip *);
1365 ip->ip_v = IPVERSION;
1366 ip->ip_hl = sizeof(*ip) >> 2;
1367 ip->ip_tos = IPTOS_LOWDELAY;
1369 ip->ip_id = ip_newid();
1371 ip->ip_ttl = CARP_DFLTTL;
1372 ip->ip_p = IPPROTO_CARP;
1374 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
1375 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
1377 ch_ptr = (struct carp_header *)(&ip[1]);
1378 bcopy(&ch, ch_ptr, sizeof(ch));
1379 carp_prepare_ad(sc, ch_ptr);
1380 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
1382 getmicrotime(&cifp->if_lastchange);
1383 IFNET_STAT_INC(cifp, opackets, 1);
1384 IFNET_STAT_INC(cifp, obytes, len);
1385 carpstats.carps_opackets++;
1387 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
1388 IFNET_STAT_INC(cifp, oerrors, 1);
1389 if (sc->sc_sendad_errors < INT_MAX)
1390 sc->sc_sendad_errors++;
1391 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1392 carp_suppress_preempt++;
1393 if (carp_suppress_preempt == 1) {
1397 sc->sc_sendad_success = 0;
1399 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1400 if (++sc->sc_sendad_success >=
1401 CARP_SENDAD_MIN_SUCCESS) {
1402 carp_suppress_preempt--;
1403 sc->sc_sendad_errors = 0;
1406 sc->sc_sendad_errors = 0;
1413 struct ip6_hdr *ip6;
1415 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1417 IFNET_STAT_INC(cifp, oerrors, 1);
1418 carpstats.carps_onomem++;
1419 /* XXX maybe less ? */
1420 if (advbase != 255 || advskew != 255)
1421 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1422 carp_send_ad_timeout, sc);
1425 len = sizeof(*ip6) + sizeof(ch);
1426 m->m_pkthdr.len = len;
1427 m->m_pkthdr.rcvif = NULL;
1429 MH_ALIGN(m, m->m_len);
1430 m->m_flags |= M_MCAST;
1431 ip6 = mtod(m, struct ip6_hdr *);
1432 bzero(ip6, sizeof(*ip6));
1433 ip6->ip6_vfc |= IPV6_VERSION;
1434 ip6->ip6_hlim = CARP_DFLTTL;
1435 ip6->ip6_nxt = IPPROTO_CARP;
1436 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1437 sizeof(struct in6_addr));
1438 /* set the multicast destination */
1440 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1441 ip6->ip6_dst.s6_addr8[15] = 0x12;
1442 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1443 IFNET_STAT_INC(cifp, oerrors, 1);
1445 CARP_LOG("%s: in6_setscope failed\n", __func__);
1449 ch_ptr = (struct carp_header *)(&ip6[1]);
1450 bcopy(&ch, ch_ptr, sizeof(ch));
1451 carp_prepare_ad(sc, ch_ptr);
1452 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
1454 getmicrotime(&cifp->if_lastchange);
1455 IFNET_STAT_INC(cifp, opackets, 1);
1456 IFNET_STAT_INC(cifp, obytes, len);
1457 carpstats.carps_opackets6++;
1459 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1460 IFNET_STAT_INC(cifp, oerrors, 1);
1461 if (sc->sc_sendad_errors < INT_MAX)
1462 sc->sc_sendad_errors++;
1463 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1464 carp_suppress_preempt++;
1465 if (carp_suppress_preempt == 1) {
1469 sc->sc_sendad_success = 0;
1471 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1472 if (++sc->sc_sendad_success >=
1473 CARP_SENDAD_MIN_SUCCESS) {
1474 carp_suppress_preempt--;
1475 sc->sc_sendad_errors = 0;
1478 sc->sc_sendad_errors = 0;
1484 if (advbase != 255 || advskew != 255)
1485 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1486 carp_send_ad_timeout, sc);
1490 * Broadcast a gratuitous ARP request containing
1491 * the virtual router MAC address for each IP address
1492 * associated with the virtual router.
1495 carp_send_arp(struct carp_softc *sc)
1497 const struct carp_vhaddr *vha;
1499 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1500 if (vha->vha_iaback == NULL)
1502 arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa);
1508 carp_send_na(struct carp_softc *sc)
1510 struct ifaddr_container *ifac;
1511 struct in6_addr *in6;
1512 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1514 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1515 struct ifaddr *ifa = ifac->ifa;
1517 if (ifa->ifa_addr->sa_family != AF_INET6)
1520 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1521 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1522 ND_NA_FLAG_OVERRIDE, 1, NULL);
1523 DELAY(1000); /* XXX */
1528 static __inline const struct carp_vhaddr *
1529 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1531 struct carp_vhaddr *vha;
1533 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1534 if (vha->vha_iaback == NULL)
1537 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1545 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1546 const struct in_addr *isaddr, uint8_t **enaddr)
1548 const struct carp_softc *vh;
1549 int index, count = 0;
1552 * XXX proof of concept implementation.
1553 * We use the source ip to decide which virtual host should
1554 * handle the request. If we're master of that virtual host,
1555 * then we respond, otherwise, just drop the arp packet on
1559 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1560 if (!CARP_IS_RUNNING(&vh->sc_if))
1563 if (carp_find_addr(vh, itaddr) != NULL)
1569 /* this should be a hash, like pf_hash() */
1570 index = ntohl(isaddr->s_addr) % count;
1573 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1574 if (!CARP_IS_RUNNING(&vh->sc_if))
1577 if (carp_find_addr(vh, itaddr) == NULL)
1580 if (count == index) {
1581 if (vh->sc_state == MASTER) {
1582 *enaddr = IF_LLADDR(&vh->sc_if);
1595 carp_iamatch(const struct in_ifaddr *ia)
1597 const struct carp_softc *sc = ia->ia_ifp->if_softc;
1599 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1600 ("not in netisr0"));
1603 if (carp_opts[CARPCTL_ARPBALANCE])
1604 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1607 if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER)
1615 carp_iamatch6(void *v, struct in6_addr *taddr)
1618 struct carp_if *cif = v;
1619 struct carp_softc *vh;
1621 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1622 struct ifaddr_container *ifac;
1624 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1626 struct ifaddr *ifa = ifac->ifa;
1628 if (IN6_ARE_ADDR_EQUAL(taddr,
1629 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1630 CARP_IS_RUNNING(&vh->sc_if) &&
1631 vh->sc_state == MASTER) {
1641 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1645 struct carp_if *cif = v;
1646 struct carp_softc *sc;
1648 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1649 struct ifaddr_container *ifac;
1651 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1653 struct ifaddr *ifa = ifac->ifa;
1655 if (IN6_ARE_ADDR_EQUAL(taddr,
1656 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1657 CARP_IS_RUNNING(&sc->sc_if)) {
1658 struct ifnet *ifp = &sc->sc_if;
1660 mtag = m_tag_get(PACKET_TAG_CARP,
1661 sizeof(struct ifnet *), MB_DONTWAIT);
1663 /* better a bit than nothing */
1664 return (IF_LLADDR(ifp));
1666 bcopy(&ifp, (caddr_t)(mtag + 1),
1667 sizeof(struct ifnet *));
1668 m_tag_prepend(m, mtag);
1670 return (IF_LLADDR(ifp));
1679 static struct ifnet *
1680 carp_forus(struct carp_if *cif, const uint8_t *dhost)
1682 struct carp_softc_container *scc;
1684 if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0)
1687 TAILQ_FOREACH(scc, cif, scc_link) {
1688 struct carp_softc *sc = scc->scc_softc;
1689 struct ifnet *ifp = &sc->sc_if;
1691 if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER &&
1692 !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN))
1699 carp_master_down_timeout(void *xsc)
1701 struct carp_softc *sc = xsc;
1702 struct netmsg_carp *cmsg = &sc->sc_md_msg;
1704 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d",
1705 __func__, mycpuid));
1708 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1709 lwkt_sendmsg(netisr_cpuport(0), &cmsg->base.lmsg);
1714 carp_master_down_timeout_dispatch(netmsg_t msg)
1716 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1717 struct carp_softc *sc = cmsg->nc_softc;
1721 lwkt_replymsg(&cmsg->base.lmsg, 0);
1724 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1725 sc->sc_if.if_xname);
1726 carp_master_down(sc);
1730 carp_master_down(struct carp_softc *sc)
1732 switch (sc->sc_state) {
1734 kprintf("%s: master_down event in INIT state\n",
1735 sc->sc_if.if_xname);
1742 carp_set_state(sc, MASTER);
1749 carp_setroute(sc, RTM_ADD);
1755 * When in backup state, af indicates whether to reset the master down timer
1756 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1759 carp_setrun(struct carp_softc *sc, sa_family_t af)
1761 struct ifnet *cifp = &sc->sc_if;
1764 if (sc->sc_carpdev == NULL) {
1765 carp_set_state(sc, INIT);
1769 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1770 (sc->sc_naddrs || sc->sc_naddrs6)) {
1773 carp_setroute(sc, RTM_DELETE);
1777 switch (sc->sc_state) {
1779 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1785 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1787 carp_set_state(sc, MASTER);
1788 carp_setroute(sc, RTM_ADD);
1790 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1791 carp_set_state(sc, BACKUP);
1792 carp_setroute(sc, RTM_DELETE);
1798 callout_stop(&sc->sc_ad_tmo);
1799 tv.tv_sec = 3 * sc->sc_advbase;
1800 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1804 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1805 carp_master_down_timeout, sc);
1810 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1811 carp_master_down_timeout, sc);
1816 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1817 carp_master_down_timeout, sc);
1819 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1820 carp_master_down_timeout, sc);
1826 tv.tv_sec = sc->sc_advbase;
1827 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1828 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1829 carp_send_ad_timeout, sc);
1835 carp_multicast_cleanup(struct carp_softc *sc)
1837 struct ip_moptions *imo = &sc->sc_imo;
1839 if (imo->imo_num_memberships == 0)
1841 KKASSERT(imo->imo_num_memberships == 1);
1843 in_delmulti(imo->imo_membership[0]);
1844 imo->imo_membership[0] = NULL;
1845 imo->imo_num_memberships = 0;
1846 imo->imo_multicast_ifp = NULL;
1851 carp_multicast6_cleanup(struct carp_softc *sc)
1853 struct ip6_moptions *im6o = &sc->sc_im6o;
1855 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1856 struct in6_multi_mship *imm =
1857 LIST_FIRST(&im6o->im6o_memberships);
1859 LIST_REMOVE(imm, i6mm_chain);
1860 in6_leavegroup(imm);
1862 im6o->im6o_multicast_ifp = NULL;
1867 carp_ioctl_getvhaddr_dispatch(netmsg_t msg)
1869 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1870 struct carp_softc *sc = cmsg->nc_softc;
1871 const struct carp_vhaddr *vha;
1872 struct ifcarpvhaddr *carpa, *carpa0;
1873 int count, len, error = 0;
1876 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1879 if (cmsg->nc_datalen == 0) {
1880 cmsg->nc_datalen = count * sizeof(*carpa);
1882 } else if (count == 0 || cmsg->nc_datalen < sizeof(*carpa)) {
1883 cmsg->nc_datalen = 0;
1886 len = min(cmsg->nc_datalen, sizeof(*carpa) * count);
1887 KKASSERT(len >= sizeof(*carpa));
1889 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1890 if (carpa == NULL) {
1896 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1897 if (len < sizeof(*carpa))
1900 carpa->carpa_flags = vha->vha_flags;
1901 carpa->carpa_addr.sin_family = AF_INET;
1902 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1904 carpa->carpa_baddr.sin_family = AF_INET;
1905 if (vha->vha_iaback == NULL) {
1906 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1908 carpa->carpa_baddr.sin_addr =
1909 vha->vha_iaback->ia_addr.sin_addr;
1914 len -= sizeof(*carpa);
1916 cmsg->nc_datalen = sizeof(*carpa) * count;
1917 KKASSERT(cmsg->nc_datalen > 0);
1919 cmsg->nc_data = carpa0;
1922 lwkt_replymsg(&cmsg->base.lmsg, error);
1926 carp_ioctl_getvhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1928 struct ifnet *ifp = &sc->arpcom.ac_if;
1929 struct netmsg_carp cmsg;
1932 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1933 ifnet_deserialize_all(ifp);
1935 bzero(&cmsg, sizeof(cmsg));
1936 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
1937 carp_ioctl_getvhaddr_dispatch);
1939 cmsg.nc_datalen = ifd->ifd_len;
1941 error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
1944 if (cmsg.nc_data != NULL) {
1945 error = copyout(cmsg.nc_data, ifd->ifd_data,
1947 kfree(cmsg.nc_data, M_TEMP);
1949 ifd->ifd_len = cmsg.nc_datalen;
1951 KASSERT(cmsg.nc_data == NULL,
1952 ("%s temp vhaddr is alloc upon error", __func__));
1955 ifnet_serialize_all(ifp);
1960 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
1961 struct in_ifaddr *ia_del)
1964 struct in_ifaddr *ia_if;
1965 const struct in_ifaddr *ia_vha;
1966 struct in_ifaddr_container *iac;
1967 int own, ia_match_carpdev;
1969 KKASSERT(vha->vha_ia != NULL);
1970 ia_vha = vha->vha_ia;
1974 ia_match_carpdev = 0;
1975 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1976 struct in_ifaddr *ia = iac->ia;
1981 if (ia->ia_ifp->if_type == IFT_CARP)
1984 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1987 /* and, yeah, we need a multicast-capable iface too */
1988 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1991 if (ia_vha->ia_subnetmask == ia->ia_subnetmask &&
1992 ia_vha->ia_subnet == ia->ia_subnet) {
1993 if (ia_vha->ia_addr.sin_addr.s_addr ==
1994 ia->ia_addr.sin_addr.s_addr)
1996 if (ia_if == NULL) {
1998 } else if (sc->sc_carpdev != NULL &&
1999 sc->sc_carpdev == ia->ia_ifp) {
2001 if (ia_if->ia_flags & IFA_ROUTE) {
2003 * Address with prefix route
2008 ia_match_carpdev = 1;
2009 } else if (!ia_match_carpdev) {
2010 if (ia->ia_flags & IFA_ROUTE) {
2012 * Address with prefix route
2013 * is prefered over others.
2021 carp_deactivate_vhaddr(sc, vha, FALSE);
2025 ifp = ia_if->ia_ifp;
2027 /* XXX Don't allow parent iface to be changed */
2028 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
2031 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2035 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2037 struct carp_vhaddr *vha_new;
2038 struct in_ifaddr *carp_ia;
2040 struct carp_vhaddr *vha;
2043 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2044 carp_ia = ifatoia(carp_ifa);
2047 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2048 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
2051 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
2052 vha_new->vha_ia = carp_ia;
2053 carp_insert_vhaddr(sc, vha_new);
2055 if (carp_config_vhaddr(sc, vha_new, NULL) != 0) {
2057 * If the above configuration fails, it may only mean
2058 * that the new address is problematic. However, the
2059 * carp(4) interface may already have several working
2060 * addresses. Since the expected behaviour of
2061 * SIOC[AS]IFADDR is to put the NIC into working state,
2062 * we try starting the state machine manually here with
2063 * the hope that the carp(4)'s previously working
2064 * addresses still could be brought up.
2066 carp_hmac_prepare(sc);
2067 carp_set_state(sc, INIT);
2073 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2075 struct carp_vhaddr *vha;
2076 struct in_ifaddr *carp_ia;
2078 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2079 carp_ia = ifatoia(carp_ifa);
2081 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2082 KKASSERT(vha->vha_ia != NULL);
2083 if (vha->vha_ia == carp_ia)
2086 KASSERT(vha != NULL, ("no corresponding vhaddr %p", carp_ifa));
2089 * Remove the vhaddr from the list before deactivating
2090 * the vhaddr, so that the HMAC could be correctly
2091 * updated in carp_deactivate_vhaddr()
2093 carp_remove_vhaddr(sc, vha);
2095 carp_deactivate_vhaddr(sc, vha, FALSE);
2100 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2102 struct carp_vhaddr *vha;
2103 struct in_ifaddr *carp_ia;
2105 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2106 carp_ia = ifatoia(carp_ifa);
2108 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2109 KKASSERT(vha->vha_ia != NULL);
2110 if (vha->vha_ia == carp_ia)
2113 KASSERT(vha != NULL, ("no corresponding vhaddr %p", carp_ifa));
2115 /* Remove then reinsert, to keep the vhaddr list sorted */
2116 carp_remove_vhaddr(sc, vha);
2117 carp_insert_vhaddr(sc, vha);
2119 if (carp_config_vhaddr(sc, vha, NULL) != 0) {
2120 /* See the comment in carp_add_addr() */
2121 carp_hmac_prepare(sc);
2122 carp_set_state(sc, INIT);
2131 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
2134 struct carp_if *cif;
2135 struct in6_ifaddr *ia, *ia_if;
2136 struct ip6_moptions *im6o = &sc->sc_im6o;
2137 struct in6_multi_mship *imm;
2138 struct in6_addr in6;
2141 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2146 /* we have to do it by hands to check we won't match on us */
2147 ia_if = NULL; own = 0;
2148 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
2151 for (i = 0; i < 4; i++) {
2152 if ((sin6->sin6_addr.s6_addr32[i] &
2153 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
2154 (ia->ia_addr.sin6_addr.s6_addr32[i] &
2155 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
2158 /* and, yeah, we need a multicast-capable iface too */
2159 if (ia->ia_ifp != &sc->sc_if &&
2160 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
2164 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2165 &ia->ia_addr.sin6_addr))
2171 return (EADDRNOTAVAIL);
2175 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
2176 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
2177 return (EADDRNOTAVAIL);
2179 if (!sc->sc_naddrs6) {
2180 im6o->im6o_multicast_ifp = ifp;
2182 /* join CARP multicast address */
2183 bzero(&in6, sizeof(in6));
2184 in6.s6_addr16[0] = htons(0xff02);
2185 in6.s6_addr8[15] = 0x12;
2186 if (in6_setscope(&in6, ifp, NULL) != 0)
2188 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2190 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2192 /* join solicited multicast address */
2193 bzero(&in6, sizeof(in6));
2194 in6.s6_addr16[0] = htons(0xff02);
2195 in6.s6_addr32[1] = 0;
2196 in6.s6_addr32[2] = htonl(1);
2197 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
2198 in6.s6_addr8[12] = 0xff;
2199 if (in6_setscope(&in6, ifp, NULL) != 0)
2201 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2203 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2207 if (!ifp->if_carp) {
2208 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2210 if ((error = ifpromisc(ifp, 1))) {
2215 TAILQ_INIT(&cif->vhif_vrs);
2218 struct carp_softc *vr;
2221 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2222 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
2230 sc->sc_carpdev = ifp;
2233 { /* XXX prevent endless loop if already in queue */
2234 struct carp_softc *vr, *after = NULL;
2238 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2241 if (vr->sc_vhid < sc->sc_vhid)
2246 /* We're trying to keep things in order */
2248 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2250 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2264 /* clean up multicast memberships */
2265 if (!sc->sc_naddrs6) {
2266 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2267 imm = LIST_FIRST(&im6o->im6o_memberships);
2268 LIST_REMOVE(imm, i6mm_chain);
2269 in6_leavegroup(imm);
2276 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
2280 if (!--sc->sc_naddrs6) {
2281 struct carp_if *cif = sc->sc_carpdev->if_carp;
2282 struct ip6_moptions *im6o = &sc->sc_im6o;
2284 callout_stop(&sc->sc_ad_tmo);
2286 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2287 struct in6_multi_mship *imm =
2288 LIST_FIRST(&im6o->im6o_memberships);
2290 LIST_REMOVE(imm, i6mm_chain);
2291 in6_leavegroup(imm);
2293 im6o->im6o_multicast_ifp = NULL;
2295 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
2296 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
2297 sc->sc_carpdev->if_carp = NULL;
2298 kfree(cif, M_IFADDR);
2309 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
2311 struct carp_softc *sc = ifp->if_softc;
2312 struct ifreq *ifr = (struct ifreq *)addr;
2313 struct ifdrv *ifd = (struct ifdrv *)addr;
2316 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2320 if (ifp->if_flags & IFF_UP) {
2321 if ((ifp->if_flags & IFF_RUNNING) == 0)
2323 } else if (ifp->if_flags & IFF_RUNNING) {
2324 carp_ioctl_stop(sc);
2329 carp_ioctl_ifcap(sc, ifr->ifr_reqcap);
2333 error = carp_ioctl_setvh(sc, ifr->ifr_data, cr);
2337 error = carp_ioctl_getvh(sc, ifr->ifr_data, cr);
2341 switch (ifd->ifd_cmd) {
2343 error = carp_ioctl_getdevname(sc, ifd);
2347 error = carp_ioctl_getvhaddr(sc, ifd);
2357 error = ether_ioctl(ifp, cmd, addr);
2365 carp_ioctl_stop_dispatch(netmsg_t msg)
2367 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2368 struct carp_softc *sc = cmsg->nc_softc;
2370 carp_stop(sc, FALSE);
2371 lwkt_replymsg(&cmsg->base.lmsg, 0);
2375 carp_ioctl_stop(struct carp_softc *sc)
2377 struct ifnet *ifp = &sc->arpcom.ac_if;
2378 struct netmsg_carp cmsg;
2380 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2382 ifnet_deserialize_all(ifp);
2384 bzero(&cmsg, sizeof(cmsg));
2385 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2386 carp_ioctl_stop_dispatch);
2389 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2391 ifnet_serialize_all(ifp);
2395 carp_ioctl_setvh_dispatch(netmsg_t msg)
2397 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2398 struct carp_softc *sc = cmsg->nc_softc;
2399 struct ifnet *ifp = &sc->arpcom.ac_if;
2400 const struct carpreq *carpr = cmsg->nc_data;
2404 if ((ifp->if_flags & IFF_RUNNING) &&
2405 sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) {
2406 switch (carpr->carpr_state) {
2408 callout_stop(&sc->sc_ad_tmo);
2409 carp_set_state(sc, BACKUP);
2411 carp_setroute(sc, RTM_DELETE);
2415 carp_master_down(sc);
2422 if (carpr->carpr_vhid > 0) {
2423 if (carpr->carpr_vhid > 255) {
2427 if (sc->sc_carpdev) {
2428 struct carp_if *cif = sc->sc_carpdev->if_carp;
2429 struct carp_softc_container *scc;
2431 TAILQ_FOREACH(scc, cif, scc_link) {
2432 struct carp_softc *vr = scc->scc_softc;
2435 vr->sc_vhid == carpr->carpr_vhid) {
2441 sc->sc_vhid = carpr->carpr_vhid;
2443 IF_LLADDR(ifp)[5] = sc->sc_vhid;
2444 bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr,
2449 if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) {
2450 if (carpr->carpr_advskew >= 255) {
2454 if (carpr->carpr_advbase > 255) {
2458 sc->sc_advbase = carpr->carpr_advbase;
2459 sc->sc_advskew = carpr->carpr_advskew;
2462 bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key));
2470 carp_hmac_prepare(sc);
2472 lwkt_replymsg(&cmsg->base.lmsg, error);
2476 carp_ioctl_setvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2478 struct ifnet *ifp = &sc->arpcom.ac_if;
2479 struct netmsg_carp cmsg;
2480 struct carpreq carpr;
2483 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2484 ifnet_deserialize_all(ifp);
2486 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2490 error = copyin(udata, &carpr, sizeof(carpr));
2494 bzero(&cmsg, sizeof(cmsg));
2495 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2496 carp_ioctl_setvh_dispatch);
2498 cmsg.nc_data = &carpr;
2500 error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2503 ifnet_serialize_all(ifp);
2508 carp_ioctl_ifcap_dispatch(netmsg_t msg)
2510 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2511 struct carp_softc *sc = cmsg->nc_softc;
2512 struct ifnet *ifp = &sc->arpcom.ac_if;
2513 int reqcap = *((const int *)(cmsg->nc_data));
2516 mask = reqcap ^ ifp->if_capenable;
2517 if (mask & IFCAP_TXCSUM) {
2518 ifp->if_capenable ^= IFCAP_TXCSUM;
2519 if ((ifp->if_capenable & IFCAP_TXCSUM) &&
2520 sc->sc_carpdev != NULL) {
2522 (sc->sc_carpdev->if_hwassist &
2523 (CSUM_IP | CSUM_UDP | CSUM_TCP));
2525 ifp->if_hwassist &= ~(CSUM_IP | CSUM_UDP | CSUM_TCP);
2528 if (mask & IFCAP_TSO) {
2529 ifp->if_capenable ^= IFCAP_TSO;
2530 if ((ifp->if_capenable & IFCAP_TSO) &&
2531 sc->sc_carpdev != NULL) {
2533 (sc->sc_carpdev->if_hwassist & CSUM_TSO);
2535 ifp->if_hwassist &= ~CSUM_TSO;
2539 lwkt_replymsg(&cmsg->base.lmsg, 0);
2543 carp_ioctl_ifcap(struct carp_softc *sc, int reqcap)
2545 struct ifnet *ifp = &sc->arpcom.ac_if;
2546 struct netmsg_carp cmsg;
2548 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2549 ifnet_deserialize_all(ifp);
2551 bzero(&cmsg, sizeof(cmsg));
2552 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2553 carp_ioctl_ifcap_dispatch);
2555 cmsg.nc_data = &reqcap;
2557 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2559 ifnet_serialize_all(ifp);
2563 carp_ioctl_getvh_dispatch(netmsg_t msg)
2565 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2566 struct carp_softc *sc = cmsg->nc_softc;
2567 struct carpreq *carpr = cmsg->nc_data;
2569 carpr->carpr_state = sc->sc_state;
2570 carpr->carpr_vhid = sc->sc_vhid;
2571 carpr->carpr_advbase = sc->sc_advbase;
2572 carpr->carpr_advskew = sc->sc_advskew;
2573 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
2575 lwkt_replymsg(&cmsg->base.lmsg, 0);
2579 carp_ioctl_getvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2581 struct ifnet *ifp = &sc->arpcom.ac_if;
2582 struct netmsg_carp cmsg;
2583 struct carpreq carpr;
2586 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2587 ifnet_deserialize_all(ifp);
2589 bzero(&cmsg, sizeof(cmsg));
2590 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2591 carp_ioctl_getvh_dispatch);
2593 cmsg.nc_data = &carpr;
2595 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2597 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2599 bzero(carpr.carpr_key, sizeof(carpr.carpr_key));
2601 error = copyout(&carpr, udata, sizeof(carpr));
2603 ifnet_serialize_all(ifp);
2608 carp_ioctl_getdevname_dispatch(netmsg_t msg)
2610 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2611 struct carp_softc *sc = cmsg->nc_softc;
2612 char *devname = cmsg->nc_data;
2614 bzero(devname, IFNAMSIZ);
2615 if (sc->sc_carpdev != NULL)
2616 strlcpy(devname, sc->sc_carpdev->if_xname, IFNAMSIZ);
2618 lwkt_replymsg(&cmsg->base.lmsg, 0);
2622 carp_ioctl_getdevname(struct carp_softc *sc, struct ifdrv *ifd)
2624 struct ifnet *ifp = &sc->arpcom.ac_if;
2625 struct netmsg_carp cmsg;
2626 char devname[IFNAMSIZ];
2629 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2631 if (ifd->ifd_len != sizeof(devname))
2634 ifnet_deserialize_all(ifp);
2636 bzero(&cmsg, sizeof(cmsg));
2637 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2638 carp_ioctl_getdevname_dispatch);
2640 cmsg.nc_data = devname;
2642 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2644 error = copyout(devname, ifd->ifd_data, sizeof(devname));
2646 ifnet_serialize_all(ifp);
2651 carp_init_dispatch(netmsg_t msg)
2653 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2654 struct carp_softc *sc = cmsg->nc_softc;
2656 sc->sc_if.if_flags |= IFF_RUNNING;
2657 carp_hmac_prepare(sc);
2658 carp_set_state(sc, INIT);
2661 lwkt_replymsg(&cmsg->base.lmsg, 0);
2665 carp_init(void *xsc)
2667 struct carp_softc *sc = xsc;
2668 struct ifnet *ifp = &sc->arpcom.ac_if;
2669 struct netmsg_carp cmsg;
2671 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2673 ifnet_deserialize_all(ifp);
2675 bzero(&cmsg, sizeof(cmsg));
2676 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2677 carp_init_dispatch);
2680 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2682 ifnet_serialize_all(ifp);
2686 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2689 struct carp_softc *sc = ifp->if_softc;
2690 struct ifnet *carpdev;
2693 carpdev = sc->sc_carpdev;
2694 if (carpdev != NULL) {
2695 if (m->m_flags & M_MCAST)
2696 IFNET_STAT_INC(ifp, omcasts, 1);
2697 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + ETHER_HDR_LEN);
2698 IFNET_STAT_INC(ifp, opackets, 1);
2702 * CARP's ifp is passed to backing device's
2705 carpdev->if_output(ifp, m, dst, rt);
2707 IFNET_STAT_INC(ifp, oerrors, 1);
2709 error = ENETUNREACH;
2715 * Start output on carp interface. This function should never be called.
2718 carp_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
2720 panic("%s: start called", ifp->if_xname);
2724 carp_set_state(struct carp_softc *sc, int state)
2726 struct ifnet *cifp = &sc->sc_if;
2728 if (sc->sc_state == state)
2730 sc->sc_state = state;
2732 switch (sc->sc_state) {
2734 cifp->if_link_state = LINK_STATE_DOWN;
2738 cifp->if_link_state = LINK_STATE_UP;
2742 cifp->if_link_state = LINK_STATE_UNKNOWN;
2749 carp_group_demote_adj(struct ifnet *ifp, int adj)
2751 struct ifg_list *ifgl;
2754 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2755 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2757 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2764 if (adj > 0 && *dm == 1)
2766 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2767 ifgl->ifgl_group->ifg_group, *dm);
2773 carp_carpdev_state(void *v)
2775 struct carp_if *cif = v;
2776 struct carp_softc *sc;
2778 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2783 carp_sc_state(struct carp_softc *sc)
2785 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2786 callout_stop(&sc->sc_ad_tmo);
2787 callout_stop(&sc->sc_md_tmo);
2788 callout_stop(&sc->sc_md6_tmo);
2789 carp_set_state(sc, INIT);
2791 if (!sc->sc_suppress) {
2792 carp_suppress_preempt++;
2793 if (carp_suppress_preempt == 1)
2796 sc->sc_suppress = 1;
2798 carp_set_state(sc, INIT);
2800 if (sc->sc_suppress)
2801 carp_suppress_preempt--;
2802 sc->sc_suppress = 0;
2808 carp_stop(struct carp_softc *sc, boolean_t detach)
2810 sc->sc_if.if_flags &= ~IFF_RUNNING;
2812 callout_stop(&sc->sc_ad_tmo);
2813 callout_stop(&sc->sc_md_tmo);
2814 callout_stop(&sc->sc_md6_tmo);
2816 if (!detach && sc->sc_state == MASTER)
2819 if (sc->sc_suppress)
2820 carp_suppress_preempt--;
2821 sc->sc_suppress = 0;
2823 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2824 carp_suppress_preempt--;
2825 sc->sc_sendad_errors = 0;
2826 sc->sc_sendad_success = 0;
2828 carp_set_state(sc, INIT);
2833 carp_suspend(struct carp_softc *sc, boolean_t detach)
2835 struct ifnet *cifp = &sc->sc_if;
2837 carp_stop(sc, detach);
2839 /* Retain the running state, if we are not dead yet */
2840 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2841 cifp->if_flags |= IFF_RUNNING;
2845 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2846 struct ifnet *ifp, struct in_ifaddr *ia_if, int own)
2848 struct ip_moptions *imo = &sc->sc_imo;
2849 struct carp_if *ocif = ifp->if_carp;
2852 KKASSERT(vha->vha_ia != NULL);
2854 KASSERT(ia_if != NULL, ("NULL backing address"));
2855 KASSERT(vha->vha_iaback == NULL, ("%p is already activated", vha));
2856 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2857 ("inactive vhaddr %p is the address owner", vha));
2859 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2860 ("%s is already on %s", sc->sc_if.if_xname,
2861 sc->sc_carpdev->if_xname));
2864 KASSERT(sc->sc_carpdev == NULL,
2865 ("%s is already on %s", sc->sc_if.if_xname,
2866 sc->sc_carpdev->if_xname));
2868 error = ifpromisc(ifp, 1);
2872 struct carp_softc_container *scc;
2874 TAILQ_FOREACH(scc, ocif, scc_link) {
2875 struct carp_softc *vr = scc->scc_softc;
2877 if (vr != sc && vr->sc_vhid == sc->sc_vhid)
2882 ifp->if_carp = carp_if_insert(ocif, sc);
2883 KASSERT(ifp->if_carp != NULL, ("%s carp_if_insert failed", __func__));
2886 sc->sc_carpdev = ifp;
2887 sc->arpcom.ac_if.if_hwassist = 0;
2888 if (sc->arpcom.ac_if.if_capenable & IFCAP_TXCSUM) {
2889 sc->arpcom.ac_if.if_hwassist |=
2890 (ifp->if_hwassist & (CSUM_IP | CSUM_UDP | CSUM_TCP));
2892 if (sc->arpcom.ac_if.if_capenable & IFCAP_TSO)
2893 sc->arpcom.ac_if.if_hwassist |= (ifp->if_hwassist & CSUM_TSO);
2896 * Make sure that all protocol threads see the sc_carpdev and
2899 netmsg_service_sync();
2901 if (ocif != NULL && ifp->if_carp != ocif) {
2903 * The old carp list could be safely free now,
2904 * since no one can access it.
2909 vha->vha_iaback = ia_if;
2913 vha->vha_flags |= CARP_VHAF_OWNER;
2915 /* XXX save user configured advskew? */
2919 carp_addroute_vhaddr(sc, vha);
2922 * Join the multicast group only after the backing interface
2923 * has been hooked with the CARP interface.
2925 KASSERT(imo->imo_multicast_ifp == NULL ||
2926 imo->imo_multicast_ifp == &sc->sc_if,
2927 ("%s didn't leave mcast group on %s",
2928 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2930 if (imo->imo_num_memberships == 0) {
2931 struct in_addr addr;
2933 addr.s_addr = htonl(INADDR_CARP_GROUP);
2934 imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if);
2935 if (imo->imo_membership[0] == NULL) {
2936 carp_deactivate_vhaddr(sc, vha, FALSE);
2940 imo->imo_num_memberships++;
2941 imo->imo_multicast_ifp = &sc->sc_if;
2942 imo->imo_multicast_ttl = CARP_DFLTTL;
2943 imo->imo_multicast_loop = 0;
2946 carp_hmac_prepare(sc);
2947 carp_set_state(sc, INIT);
2953 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2954 boolean_t del_iaback)
2956 KKASSERT(vha->vha_ia != NULL);
2958 carp_hmac_prepare(sc);
2960 if (vha->vha_iaback == NULL) {
2961 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2962 ("inactive vhaddr %p is the address owner", vha));
2966 vha->vha_flags &= ~CARP_VHAF_OWNER;
2967 carp_delroute_vhaddr(sc, vha, del_iaback);
2969 KKASSERT(sc->sc_naddrs > 0);
2970 vha->vha_iaback = NULL;
2972 if (!sc->sc_naddrs) {
2973 if (sc->sc_naddrs6) {
2974 carp_multicast_cleanup(sc);
2977 carp_detach(sc, FALSE, del_iaback);
2983 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2985 struct carp_vhaddr *vha;
2986 struct in_ifaddr *ia_if;
2988 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2989 ia_if = ifatoia(ifa_if);
2992 * Test each inactive vhaddr against the newly added address.
2993 * If the newly added address could be the backing address,
2994 * then activate the matching vhaddr.
2996 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2997 const struct in_ifaddr *ia;
3000 if (vha->vha_iaback != NULL)
3004 if (ia->ia_subnetmask != ia_if->ia_subnetmask ||
3005 ia->ia_subnet != ia_if->ia_subnet)
3009 if (ia->ia_addr.sin_addr.s_addr ==
3010 ia_if->ia_addr.sin_addr.s_addr)
3013 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
3018 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
3019 struct ifaddr *ifa_if)
3021 struct carp_vhaddr *vha;
3022 struct in_ifaddr *ia_if;
3024 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
3025 ia_if = ifatoia(ifa_if);
3028 * Ad src address is deleted; set it to NULL.
3029 * Following loop will try pick up a new ad src address
3030 * if one of the vhaddr could retain its backing address.
3032 if (sc->sc_ia == ia_if)
3036 * Test each active vhaddr against the deleted address.
3037 * If the deleted address is vhaddr address's backing
3038 * address, then deactivate the vhaddr.
3040 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
3041 if (vha->vha_iaback == NULL)
3044 if (vha->vha_iaback == ia_if)
3045 carp_deactivate_vhaddr(sc, vha, TRUE);
3046 else if (sc->sc_ia == NULL)
3047 sc->sc_ia = vha->vha_iaback;
3052 carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del)
3054 struct carp_vhaddr *vha;
3056 KKASSERT(sc->sc_carpdev == NULL);
3058 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
3059 carp_config_vhaddr(sc, vha, ifatoia(ifa_del));
3063 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
3064 enum ifaddr_event event, struct ifaddr *ifa)
3066 struct carp_softc *sc;
3068 if (ifa->ifa_addr->sa_family != AF_INET)
3071 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
3072 ("not in netisr0"));
3074 if (ifp->if_type == IFT_CARP) {
3076 * Address is changed on carp(4) interface
3079 case IFADDR_EVENT_ADD:
3080 carp_add_addr(ifp->if_softc, ifa);
3083 case IFADDR_EVENT_CHANGE:
3084 carp_config_addr(ifp->if_softc, ifa);
3087 case IFADDR_EVENT_DELETE:
3088 carp_del_addr(ifp->if_softc, ifa);
3095 * Address is changed on non-carp(4) interface
3097 if ((ifp->if_flags & IFF_MULTICAST) == 0)
3100 LIST_FOREACH(sc, &carpif_list, sc_next) {
3101 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
3102 /* Not the parent iface; skip */
3107 case IFADDR_EVENT_ADD:
3108 carp_link_addrs(sc, ifp, ifa);
3111 case IFADDR_EVENT_DELETE:
3112 if (sc->sc_carpdev != NULL) {
3113 carp_unlink_addrs(sc, ifp, ifa);
3114 if (sc->sc_carpdev == NULL) {
3116 * We no longer have the parent
3117 * interface, however, certain
3118 * virtual addresses, which are
3119 * not used because they can't
3120 * match the previous parent
3121 * interface's addresses, may now
3122 * match different interface's
3125 carp_update_addrs(sc, ifa);
3129 * The carp(4) interface didn't have a
3130 * parent iface, so it is not possible
3131 * that it will contain any address to
3137 case IFADDR_EVENT_CHANGE:
3138 if (sc->sc_carpdev == NULL) {
3140 * The carp(4) interface didn't have a
3141 * parent iface, so it is not possible
3142 * that it will contain any address to
3145 carp_link_addrs(sc, ifp, ifa);
3148 * First try breaking tie with the old
3149 * address. Then see whether we could
3150 * link certain vhaddr to the new address.
3151 * If that fails, i.e. carpdev is NULL,
3152 * we try a global update.
3154 * NOTE: The above order is critical.
3156 carp_unlink_addrs(sc, ifp, ifa);
3157 carp_link_addrs(sc, ifp, ifa);
3158 if (sc->sc_carpdev == NULL) {
3160 * See the comment in the above
3161 * IFADDR_EVENT_DELETE block.
3163 carp_update_addrs(sc, NULL);
3172 carp_proto_ctlinput(netmsg_t msg)
3174 int cmd = msg->ctlinput.nm_cmd;
3175 struct sockaddr *sa = msg->ctlinput.nm_arg;
3176 struct in_ifaddr_container *iac;
3178 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
3179 struct in_ifaddr *ia = iac->ia;
3180 struct ifnet *ifp = ia->ia_ifp;
3182 if (ifp->if_type == IFT_CARP)
3185 if (ia->ia_ifa.ifa_addr == sa) {
3186 if (cmd == PRC_IFDOWN) {
3187 carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE,
3189 } else if (cmd == PRC_IFUP) {
3190 carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD,
3197 lwkt_replymsg(&msg->lmsg, 0);
3201 carp_parent(struct ifnet *cifp)
3203 struct carp_softc *sc;
3205 KKASSERT(cifp->if_type == IFT_CARP);
3206 sc = cifp->if_softc;
3208 return sc->sc_carpdev;
3211 #define rtinitflags(x) \
3212 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
3216 carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
3218 struct in_ifaddr *ia, *iaback;
3220 if (sc->sc_state != MASTER)
3224 KKASSERT(ia != NULL);
3226 iaback = vha->vha_iaback;
3227 KKASSERT(iaback != NULL);
3229 return rtchange(&iaback->ia_ifa, &ia->ia_ifa);
3233 carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
3234 boolean_t del_iaback)
3236 struct in_ifaddr *ia, *iaback;
3239 KKASSERT(ia != NULL);
3241 iaback = vha->vha_iaback;
3242 KKASSERT(iaback != NULL);
3244 if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) {
3245 rtchange(&ia->ia_ifa, &iaback->ia_ifa);
3249 rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia));
3250 in_ifadown_force(&ia->ia_ifa, 1);
3251 ia->ia_flags &= ~IFA_ROUTE;
3255 carp_modevent(module_t mod, int type, void *data)
3259 LIST_INIT(&carpif_list);
3260 carp_ifdetach_event =
3261 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
3262 EVENTHANDLER_PRI_ANY);
3264 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
3265 EVENTHANDLER_PRI_FIRST);
3266 if_clone_attach(&carp_cloner);
3270 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
3271 carp_ifdetach_event);
3272 EVENTHANDLER_DEREGISTER(ifaddr_event,
3274 if_clone_detach(&carp_cloner);
3283 static moduledata_t carp_mod = {
3288 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);