/* * Copyright (c) 2002 Michael Shalayeff. All rights reserved. * Copyright (c) 2003 Ryan McBride. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ /* * $FreeBSD: src/sys/netinet/ip_carp.c,v 1.48 2007/02/02 09:39:09 glebius Exp $ */ #include "opt_carp.h" #include "opt_inet.h" #include "opt_inet6.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef INET #include #include #include #include #include #include #endif #ifdef INET6 #include #include #include #include #include #endif #include #define CARP_IFNAME "carp" #define CARP_IS_RUNNING(ifp) \ (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) struct carp_vhaddr { uint32_t vha_flags; /* CARP_VHAF_ */ struct in_ifaddr *vha_ia; /* carp address */ struct in_ifaddr *vha_iaback; /* backing address */ TAILQ_ENTRY(carp_vhaddr) vha_link; }; TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr); struct carp_softc { struct arpcom arpcom; struct ifnet *sc_carpdev; /* parent interface */ struct carp_vhaddr_list sc_vha_list; /* virtual addr list */ const struct in_ifaddr *sc_ia; /* primary iface address v4 */ struct ip_moptions sc_imo; #ifdef INET6 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */ struct ip6_moptions sc_im6o; #endif /* INET6 */ TAILQ_ENTRY(carp_softc) sc_list; enum { INIT = 0, BACKUP, MASTER } sc_state; int sc_dead; int sc_suppress; int sc_sendad_errors; #define CARP_SENDAD_MAX_ERRORS 3 int sc_sendad_success; #define CARP_SENDAD_MIN_SUCCESS 3 int sc_vhid; int sc_advskew; int sc_naddrs; /* actually used IPv4 vha */ int sc_naddrs6; int sc_advbase; /* seconds */ int sc_init_counter; uint64_t sc_counter; /* authentication */ #define CARP_HMAC_PAD 64 unsigned char sc_key[CARP_KEY_LEN]; unsigned char sc_pad[CARP_HMAC_PAD]; SHA1_CTX sc_sha1; struct callout sc_ad_tmo; /* advertisement timeout */ struct callout sc_md_tmo; /* master down timeout */ struct callout sc_md6_tmo; /* master down timeout */ LIST_ENTRY(carp_softc) sc_next; /* Interface clue */ }; #define sc_if arpcom.ac_if struct carp_if { TAILQ_HEAD(, carp_softc) vhif_vrs; }; struct netmsg_carp { struct netmsg_base base; struct ifnet *nc_carpdev; struct carp_softc *nc_softc; void *nc_data; size_t nc_datalen; }; SYSCTL_DECL(_net_inet_carp); static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */ SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW, &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets"); SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW, &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode"); SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW, &carp_opts[CARPCTL_LOG], 0, "log bad carp packets"); SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW, &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses"); static int carp_suppress_preempt = 0; SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD, &carp_suppress_preempt, 0, "Preemption is suppressed"); static struct carpstats carpstats; SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW, &carpstats, carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)"); #define CARP_LOG(...) do { \ if (carp_opts[CARPCTL_LOG] > 0) \ log(LOG_INFO, __VA_ARGS__); \ } while (0) #define CARP_DEBUG(...) do { \ if (carp_opts[CARPCTL_LOG] > 1) \ log(LOG_DEBUG, __VA_ARGS__); \ } while (0) static struct lwkt_token carp_tok = LWKT_TOKEN_INITIALIZER(carp_token); static void carp_hmac_prepare(struct carp_softc *); static void carp_hmac_generate(struct carp_softc *, uint32_t *, unsigned char *); static int carp_hmac_verify(struct carp_softc *, uint32_t *, unsigned char *); static void carp_setroute(struct carp_softc *, int); static void carp_proto_input_c(struct carp_softc *, struct mbuf *, struct carp_header *, sa_family_t); static int carp_clone_create(struct if_clone *, int, caddr_t); static int carp_clone_destroy(struct ifnet *); static void carp_detach(struct carp_softc *, int, boolean_t); static void carp_prepare_ad(struct carp_softc *, struct carp_header *); static void carp_send_ad_all(void); static void carp_send_ad_timeout(void *); static void carp_send_ad(struct carp_softc *); static void carp_send_arp(struct carp_softc *); static void carp_master_down_timeout(void *); static void carp_master_down(struct carp_softc *); static void carp_setrun(struct carp_softc *, sa_family_t); static void carp_set_state(struct carp_softc *, int); static struct ifnet *carp_forus(struct carp_if *, const uint8_t *); static void carp_init(void *); static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *); static int carp_output(struct ifnet *, struct mbuf *, struct sockaddr *, struct rtentry *); static void carp_start(struct ifnet *); static void carp_serialize(struct ifnet *, enum ifnet_serialize); static void carp_deserialize(struct ifnet *, enum ifnet_serialize); static int carp_tryserialize(struct ifnet *, enum ifnet_serialize); #ifdef INVARIANTS static void carp_serialize_assert(struct ifnet *, enum ifnet_serialize, boolean_t); #endif static void carp_multicast_cleanup(struct carp_softc *); static void carp_add_addr(struct carp_softc *, struct ifaddr *); static void carp_del_addr(struct carp_softc *, struct ifaddr *); static void carp_config_addr(struct carp_softc *, struct ifaddr *); static void carp_link_addrs(struct carp_softc *, struct ifnet *, struct ifaddr *); static void carp_unlink_addrs(struct carp_softc *, struct ifnet *, struct ifaddr *); static void carp_update_addrs(struct carp_softc *, struct ifaddr *); static int carp_get_vhaddr(struct carp_softc *, struct ifdrv *); static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *, struct in_ifaddr *); static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *, struct ifnet *, struct in_ifaddr *, int); static void carp_deactivate_vhaddr(struct carp_softc *, struct carp_vhaddr *, boolean_t); static int carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *); static void carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *, boolean_t); static void carp_sc_state(struct carp_softc *); #ifdef INET6 static void carp_send_na(struct carp_softc *); #ifdef notyet static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *); static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *); #endif static void carp_multicast6_cleanup(struct carp_softc *); #endif static void carp_stop(struct carp_softc *, int); static void carp_suspend(struct carp_softc *, int); static void carp_ioctl_stop(struct carp_softc *); static int carp_ioctl_setvh(struct carp_softc *, struct carpreq *); static void carp_ioctl_getvh(struct carp_softc *, struct carpreq *); static void carp_ioctl_getdevname(struct carp_softc *, char *); static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event, struct ifaddr *); static void carp_ifdetach(void *, struct ifnet *); static void carp_ifdetach_dispatch(netmsg_t); static void carp_clone_destroy_dispatch(netmsg_t); static void carp_init_dispatch(netmsg_t); static void carp_ioctl_stop_dispatch(netmsg_t); static void carp_ioctl_setvh_dispatch(netmsg_t); static void carp_ioctl_getvh_dispatch(netmsg_t); static void carp_ioctl_getdevname_dispatch(netmsg_t); static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces"); static LIST_HEAD(, carp_softc) carpif_list; static struct if_clone carp_cloner = IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy, 0, IF_MAXUNIT); static uint8_t carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 }; static eventhandler_tag carp_ifdetach_event; static eventhandler_tag carp_ifaddr_event; static __inline void carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new) { struct carp_vhaddr *vha; u_long new_addr, addr; KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0); /* * Virtual address list is sorted; smaller one first */ new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr); if (addr > new_addr) break; } if (vha == NULL) TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link); else TAILQ_INSERT_BEFORE(vha, vha_new, vha_link); vha_new->vha_flags |= CARP_VHAF_ONLIST; } static __inline void carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha) { KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST); vha->vha_flags &= ~CARP_VHAF_ONLIST; TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link); } static void carp_hmac_prepare(struct carp_softc *sc) { uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT; uint8_t vhid = sc->sc_vhid & 0xff; int i; #ifdef INET6 struct ifaddr_container *ifac; struct in6_addr in6; #endif #ifdef INET struct carp_vhaddr *vha; #endif /* XXX: possible race here */ /* compute ipad from key */ bzero(sc->sc_pad, sizeof(sc->sc_pad)); bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key)); for (i = 0; i < sizeof(sc->sc_pad); i++) sc->sc_pad[i] ^= 0x36; /* precompute first part of inner hash */ SHA1Init(&sc->sc_sha1); SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad)); SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version)); SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type)); SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid)); #ifdef INET TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { SHA1Update(&sc->sc_sha1, (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr, sizeof(struct in_addr)); } #endif /* INET */ #ifdef INET6 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (ifa->ifa_addr->sa_family == AF_INET6) { in6 = ifatoia6(ifa)->ia_addr.sin6_addr; in6_clearscope(&in6); SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6)); } } #endif /* INET6 */ /* convert ipad to opad */ for (i = 0; i < sizeof(sc->sc_pad); i++) sc->sc_pad[i] ^= 0x36 ^ 0x5c; } static void carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2], unsigned char md[20]) { SHA1_CTX sha1ctx; /* fetch first half of inner hash */ bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter)); SHA1Final(md, &sha1ctx); /* outer hash */ SHA1Init(&sha1ctx); SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad)); SHA1Update(&sha1ctx, md, 20); SHA1Final(md, &sha1ctx); } static int carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2], unsigned char md[20]) { unsigned char md2[20]; carp_hmac_generate(sc, counter, md2); return (bcmp(md, md2, sizeof(md2))); } static void carp_setroute(struct carp_softc *sc, int cmd) { #ifdef INET6 struct ifaddr_container *ifac; #endif struct carp_vhaddr *vha; KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (vha->vha_iaback == NULL) continue; if (cmd == RTM_DELETE) carp_delroute_vhaddr(sc, vha, FALSE); else carp_addroute_vhaddr(sc, vha); } #ifdef INET6 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (ifa->ifa_addr->sa_family == AF_INET6) { if (cmd == RTM_ADD) in6_ifaddloop(ifa); else in6_ifremloop(ifa); } } #endif /* INET6 */ } static int carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused) { struct carp_softc *sc; struct ifnet *ifp; sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO); ifp = &sc->sc_if; sc->sc_suppress = 0; sc->sc_advbase = CARP_DFLTINTV; sc->sc_vhid = -1; /* required setting */ sc->sc_advskew = 0; sc->sc_init_counter = 1; sc->sc_naddrs = 0; sc->sc_naddrs6 = 0; TAILQ_INIT(&sc->sc_vha_list); #ifdef INET6 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL; #endif callout_init_mp(&sc->sc_ad_tmo); callout_init_mp(&sc->sc_md_tmo); callout_init_mp(&sc->sc_md6_tmo); if_initname(ifp, CARP_IFNAME, unit); ifp->if_softc = sc; ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; ifp->if_init = carp_init; ifp->if_ioctl = carp_ioctl; ifp->if_start = carp_start; ifp->if_serialize = carp_serialize; ifp->if_deserialize = carp_deserialize; ifp->if_tryserialize = carp_tryserialize; #ifdef INVARIANTS ifp->if_serialize_assert = carp_serialize_assert; #endif ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); ifq_set_ready(&ifp->if_snd); ether_ifattach(ifp, carp_etheraddr, NULL); ifp->if_type = IFT_CARP; ifp->if_output = carp_output; carp_gettok(); LIST_INSERT_HEAD(&carpif_list, sc, sc_next); carp_reltok(); return (0); } static void carp_clone_destroy_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; carp_gettok(); sc->sc_dead = 1; carp_detach(sc, 1, FALSE); carp_reltok(); lwkt_replymsg(&cmsg->base.lmsg, 0); } static int carp_clone_destroy(struct ifnet *ifp) { struct carp_softc *sc = ifp->if_softc; struct netmsg_carp cmsg; bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_clone_destroy_dispatch); cmsg.nc_softc = sc; lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0); carp_gettok(); LIST_REMOVE(sc, sc_next); carp_reltok(); bpfdetach(ifp); if_detach(ifp); KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active\n")); kfree(sc, M_CARP); return 0; } static void carp_detach(struct carp_softc *sc, int detach, boolean_t del_iaback) { struct carp_if *cif; carp_suspend(sc, detach); carp_multicast_cleanup(sc); #ifdef INET6 carp_multicast6_cleanup(sc); #endif if (!sc->sc_dead && detach) { struct carp_vhaddr *vha; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) carp_deactivate_vhaddr(sc, vha, del_iaback); KKASSERT(sc->sc_naddrs == 0); } if (sc->sc_carpdev != NULL) { cif = sc->sc_carpdev->if_carp; TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); if (TAILQ_EMPTY(&cif->vhif_vrs)) { ifpromisc(sc->sc_carpdev, 0); sc->sc_carpdev->if_carp = NULL; kfree(cif, M_CARP); } sc->sc_carpdev = NULL; sc->sc_ia = NULL; } } static void carp_ifdetach_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct ifnet *ifp = cmsg->nc_carpdev; struct carp_if *cif = ifp->if_carp; struct carp_softc *sc; carp_gettok(); while (ifp->if_carp && (sc = TAILQ_FIRST(&cif->vhif_vrs)) != NULL) carp_detach(sc, 1, TRUE); carp_reltok(); lwkt_replymsg(&cmsg->base.lmsg, 0); } /* Detach an interface from the carp. */ static void carp_ifdetach(void *arg __unused, struct ifnet *ifp) { struct netmsg_carp cmsg; ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ifdetach_dispatch); cmsg.nc_carpdev = ifp; lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0); } /* * process input packet. * we have rearranged checks order compared to the rfc, * but it seems more efficient this way or not possible otherwise. */ int carp_proto_input(struct mbuf **mp, int *offp, int proto) { struct mbuf *m = *mp; struct ip *ip = mtod(m, struct ip *); struct ifnet *ifp = m->m_pkthdr.rcvif; struct carp_header *ch; struct carp_softc *sc; int len, iphlen; carp_gettok(); iphlen = *offp; *mp = NULL; carpstats.carps_ipackets++; if (!carp_opts[CARPCTL_ALLOW]) { m_freem(m); goto back; } /* Check if received on a valid carp interface */ if (ifp->if_type != IFT_CARP) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: packet received on non-carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } if (!CARP_IS_RUNNING(ifp)) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: packet received on stopped carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } sc = ifp->if_softc; if (sc->sc_carpdev == NULL) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: packet received on defunc carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: non-mcast packet on " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } /* Verify that the IP TTL is CARP_DFLTTL. */ if (ip->ip_ttl != CARP_DFLTTL) { carpstats.carps_badttl++; CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n", ip->ip_ttl, CARP_DFLTTL, ifp->if_xname); m_freem(m); goto back; } /* Minimal CARP packet size */ len = iphlen + sizeof(*ch); /* * Verify that the received packet length is * not less than the CARP header */ if (m->m_pkthdr.len < len) { carpstats.carps_badlen++; CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len, ifp->if_xname); m_freem(m); goto back; } /* Make sure that CARP header is contiguous */ if (len > m->m_len) { m = m_pullup(m, len); if (m == NULL) { carpstats.carps_hdrops++; CARP_LOG("carp_proto_input: m_pullup failed\n"); goto back; } ip = mtod(m, struct ip *); } ch = (struct carp_header *)((uint8_t *)ip + iphlen); /* Verify the CARP checksum */ if (in_cksum_skip(m, len, iphlen)) { carpstats.carps_badsum++; CARP_LOG("carp_proto_input: checksum failed on %s\n", ifp->if_xname); m_freem(m); goto back; } carp_proto_input_c(sc, m, ch, AF_INET); back: carp_reltok(); return(IPPROTO_DONE); } #ifdef INET6 int carp6_proto_input(struct mbuf **mp, int *offp, int proto) { struct mbuf *m = *mp; struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); struct ifnet *ifp = m->m_pkthdr.rcvif; struct carp_header *ch; struct carp_softc *sc; u_int len; carp_gettok(); carpstats.carps_ipackets6++; if (!carp_opts[CARPCTL_ALLOW]) { m_freem(m); goto back; } /* check if received on a valid carp interface */ if (ifp->if_type != IFT_CARP) { carpstats.carps_badif++; CARP_LOG("carp6_proto_input: packet received on non-carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } if (!CARP_IS_RUNNING(ifp)) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: packet received on stopped carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } sc = ifp->if_softc; if (sc->sc_carpdev == NULL) { carpstats.carps_badif++; CARP_LOG("carp6_proto_input: packet received on defunc-carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } /* verify that the IP TTL is 255 */ if (ip6->ip6_hlim != CARP_DFLTTL) { carpstats.carps_badttl++; CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n", ip6->ip6_hlim, ifp->if_xname); m_freem(m); goto back; } /* verify that we have a complete carp packet */ len = m->m_len; IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch)); if (ch == NULL) { carpstats.carps_badlen++; CARP_LOG("carp6_proto_input: packet size %u too small\n", len); goto back; } /* verify the CARP checksum */ if (in_cksum_range(m, 0, *offp, sizeof(*ch))) { carpstats.carps_badsum++; CARP_LOG("carp6_proto_input: checksum failed, on %s\n", ifp->if_xname); m_freem(m); goto back; } carp_proto_input_c(sc, m, ch, AF_INET6); back: carp_reltok(); return (IPPROTO_DONE); } #endif /* INET6 */ static void carp_proto_input_c(struct carp_softc *sc, struct mbuf *m, struct carp_header *ch, sa_family_t af) { struct ifnet *cifp; uint64_t tmp_counter; struct timeval sc_tv, ch_tv; if (sc->sc_vhid != ch->carp_vhid) { /* * CARP uses multicast, however, multicast packets * are tapped to all CARP interfaces on the physical * interface receiving the CARP packets, so we don't * update any stats here. */ m_freem(m); return; } cifp = &sc->sc_if; /* verify the CARP version. */ if (ch->carp_version != CARP_VERSION) { carpstats.carps_badver++; CARP_LOG("%s; invalid version %d\n", cifp->if_xname, ch->carp_version); m_freem(m); return; } /* verify the hash */ if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { carpstats.carps_badauth++; CARP_LOG("%s: incorrect hash\n", cifp->if_xname); m_freem(m); return; } tmp_counter = ntohl(ch->carp_counter[0]); tmp_counter = tmp_counter<<32; tmp_counter += ntohl(ch->carp_counter[1]); /* XXX Replay protection goes here */ sc->sc_init_counter = 0; sc->sc_counter = tmp_counter; sc_tv.tv_sec = sc->sc_advbase; if (carp_suppress_preempt && sc->sc_advskew < 240) sc_tv.tv_usec = 240 * 1000000 / 256; else sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256; ch_tv.tv_sec = ch->carp_advbase; ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; switch (sc->sc_state) { case INIT: break; case MASTER: /* * If we receive an advertisement from a master who's going to * be more frequent than us, go into BACKUP state. */ if (timevalcmp(&sc_tv, &ch_tv, >) || timevalcmp(&sc_tv, &ch_tv, ==)) { callout_stop(&sc->sc_ad_tmo); CARP_DEBUG("%s: MASTER -> BACKUP " "(more frequent advertisement received)\n", cifp->if_xname); carp_set_state(sc, BACKUP); carp_setrun(sc, 0); carp_setroute(sc, RTM_DELETE); } break; case BACKUP: /* * If we're pre-empting masters who advertise slower than us, * and this one claims to be slower, treat him as down. */ if (carp_opts[CARPCTL_PREEMPT] && timevalcmp(&sc_tv, &ch_tv, <)) { CARP_DEBUG("%s: BACKUP -> MASTER " "(preempting a slower master)\n", cifp->if_xname); carp_master_down(sc); break; } /* * If the master is going to advertise at such a low frequency * that he's guaranteed to time out, we'd might as well just * treat him as timed out now. */ sc_tv.tv_sec = sc->sc_advbase * 3; if (timevalcmp(&sc_tv, &ch_tv, <)) { CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n", cifp->if_xname); carp_master_down(sc); break; } /* * Otherwise, we reset the counter and wait for the next * advertisement. */ carp_setrun(sc, af); break; } m_freem(m); } struct mbuf * carp_input(void *v, struct mbuf *m) { struct carp_if *cif = v; struct ether_header *eh; struct carp_softc *sc; struct ifnet *ifp; ASSERT_LWKT_TOKEN_HELD(&carp_tok); eh = mtod(m, struct ether_header *); ifp = carp_forus(cif, eh->ether_dhost); if (ifp != NULL) { ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF); return NULL; } if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) return m; /* * XXX Should really check the list of multicast addresses * for each CARP interface _before_ copying. */ TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { struct mbuf *m0; if ((sc->sc_if.if_flags & IFF_UP) == 0) continue; m0 = m_dup(m, MB_DONTWAIT); if (m0 == NULL) continue; ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF); } return m; } static void carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch) { if (sc->sc_init_counter) { /* this could also be seconds since unix epoch */ sc->sc_counter = karc4random(); sc->sc_counter = sc->sc_counter << 32; sc->sc_counter += karc4random(); } else { sc->sc_counter++; } ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff); ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff); carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); } static void carp_send_ad_all(void) { struct carp_softc *sc; LIST_FOREACH(sc, &carpif_list, sc_next) { if (sc->sc_carpdev == NULL) continue; if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER) carp_send_ad(sc); } } static void carp_send_ad_timeout(void *xsc) { carp_gettok(); carp_send_ad(xsc); carp_reltok(); } static void carp_send_ad(struct carp_softc *sc) { struct ifnet *cifp = &sc->sc_if; struct carp_header ch; struct timeval tv; struct carp_header *ch_ptr; struct mbuf *m; int len, advbase, advskew; if (!CARP_IS_RUNNING(cifp)) { /* Bow out */ advbase = 255; advskew = 255; } else { advbase = sc->sc_advbase; if (!carp_suppress_preempt || sc->sc_advskew > 240) advskew = sc->sc_advskew; else advskew = 240; tv.tv_sec = advbase; tv.tv_usec = advskew * 1000000 / 256; } ch.carp_version = CARP_VERSION; ch.carp_type = CARP_ADVERTISEMENT; ch.carp_vhid = sc->sc_vhid; ch.carp_advbase = advbase; ch.carp_advskew = advskew; ch.carp_authlen = 7; /* XXX DEFINE */ ch.carp_pad1 = 0; /* must be zero */ ch.carp_cksum = 0; #ifdef INET if (sc->sc_ia != NULL) { struct ip *ip; MGETHDR(m, MB_DONTWAIT, MT_HEADER); if (m == NULL) { cifp->if_oerrors++; carpstats.carps_onomem++; /* XXX maybe less ? */ if (advbase != 255 || advskew != 255) callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), carp_send_ad_timeout, sc); return; } len = sizeof(*ip) + sizeof(ch); m->m_pkthdr.len = len; m->m_pkthdr.rcvif = NULL; m->m_len = len; MH_ALIGN(m, m->m_len); m->m_flags |= M_MCAST; ip = mtod(m, struct ip *); ip->ip_v = IPVERSION; ip->ip_hl = sizeof(*ip) >> 2; ip->ip_tos = IPTOS_LOWDELAY; ip->ip_len = len; ip->ip_id = ip_newid(); ip->ip_off = IP_DF; ip->ip_ttl = CARP_DFLTTL; ip->ip_p = IPPROTO_CARP; ip->ip_sum = 0; ip->ip_src = sc->sc_ia->ia_addr.sin_addr; ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP); ch_ptr = (struct carp_header *)(&ip[1]); bcopy(&ch, ch_ptr, sizeof(ch)); carp_prepare_ad(sc, ch_ptr); ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip)); getmicrotime(&cifp->if_lastchange); cifp->if_opackets++; cifp->if_obytes += len; carpstats.carps_opackets++; if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) { cifp->if_oerrors++; if (sc->sc_sendad_errors < INT_MAX) sc->sc_sendad_errors++; if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { carp_suppress_preempt++; if (carp_suppress_preempt == 1) { carp_send_ad_all(); } } sc->sc_sendad_success = 0; } else { if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) { carp_suppress_preempt--; sc->sc_sendad_errors = 0; } } else { sc->sc_sendad_errors = 0; } } } #endif /* INET */ #ifdef INET6 if (sc->sc_ia6) { struct ip6_hdr *ip6; MGETHDR(m, MB_DONTWAIT, MT_HEADER); if (m == NULL) { cifp->if_oerrors++; carpstats.carps_onomem++; /* XXX maybe less ? */ if (advbase != 255 || advskew != 255) callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), carp_send_ad_timeout, sc); return; } len = sizeof(*ip6) + sizeof(ch); m->m_pkthdr.len = len; m->m_pkthdr.rcvif = NULL; m->m_len = len; MH_ALIGN(m, m->m_len); m->m_flags |= M_MCAST; ip6 = mtod(m, struct ip6_hdr *); bzero(ip6, sizeof(*ip6)); ip6->ip6_vfc |= IPV6_VERSION; ip6->ip6_hlim = CARP_DFLTTL; ip6->ip6_nxt = IPPROTO_CARP; bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src, sizeof(struct in6_addr)); /* set the multicast destination */ ip6->ip6_dst.s6_addr16[0] = htons(0xff02); ip6->ip6_dst.s6_addr8[15] = 0x12; if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) { cifp->if_oerrors++; m_freem(m); CARP_LOG("%s: in6_setscope failed\n", __func__); return; } ch_ptr = (struct carp_header *)(&ip6[1]); bcopy(&ch, ch_ptr, sizeof(ch)); carp_prepare_ad(sc, ch_ptr); ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6)); getmicrotime(&cifp->if_lastchange); cifp->if_opackets++; cifp->if_obytes += len; carpstats.carps_opackets6++; if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) { cifp->if_oerrors++; if (sc->sc_sendad_errors < INT_MAX) sc->sc_sendad_errors++; if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { carp_suppress_preempt++; if (carp_suppress_preempt == 1) { carp_send_ad_all(); } } sc->sc_sendad_success = 0; } else { if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) { carp_suppress_preempt--; sc->sc_sendad_errors = 0; } } else { sc->sc_sendad_errors = 0; } } } #endif /* INET6 */ if (advbase != 255 || advskew != 255) callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), carp_send_ad_timeout, sc); } /* * Broadcast a gratuitous ARP request containing * the virtual router MAC address for each IP address * associated with the virtual router. */ static void carp_send_arp(struct carp_softc *sc) { const struct carp_vhaddr *vha; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (vha->vha_iaback == NULL) continue; arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa); } } #ifdef INET6 static void carp_send_na(struct carp_softc *sc) { struct ifaddr_container *ifac; struct in6_addr *in6; static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (ifa->ifa_addr->sa_family != AF_INET6) continue; in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; nd6_na_output(sc->sc_carpdev, &mcast, in6, ND_NA_FLAG_OVERRIDE, 1, NULL); DELAY(1000); /* XXX */ } } #endif /* INET6 */ static __inline const struct carp_vhaddr * carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr) { struct carp_vhaddr *vha; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (vha->vha_iaback == NULL) continue; if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr) return vha; } return NULL; } #ifdef notyet static int carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr, const struct in_addr *isaddr, uint8_t **enaddr) { const struct carp_softc *vh; int index, count = 0; /* * XXX proof of concept implementation. * We use the source ip to decide which virtual host should * handle the request. If we're master of that virtual host, * then we respond, otherwise, just drop the arp packet on * the floor. */ TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { if (!CARP_IS_RUNNING(&vh->sc_if)) continue; if (carp_find_addr(vh, itaddr) != NULL) count++; } if (count == 0) return 0; /* this should be a hash, like pf_hash() */ index = ntohl(isaddr->s_addr) % count; count = 0; TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { if (!CARP_IS_RUNNING(&vh->sc_if)) continue; if (carp_find_addr(vh, itaddr) == NULL) continue; if (count == index) { if (vh->sc_state == MASTER) { *enaddr = IF_LLADDR(&vh->sc_if); return 1; } else { return 0; } } count++; } return 0; } #endif int carp_iamatch(const struct in_ifaddr *ia) { const struct carp_softc *sc = ia->ia_ifp->if_softc; ASSERT_LWKT_TOKEN_HELD(&carp_tok); #ifdef notyet if (carp_opts[CARPCTL_ARPBALANCE]) return carp_iamatch_balance(cif, itaddr, isaddr, enaddr); #endif if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER) return 0; return 1; } #ifdef INET6 struct ifaddr * carp_iamatch6(void *v, struct in6_addr *taddr) { struct carp_if *cif = v; struct carp_softc *vh; ASSERT_LWKT_TOKEN_HELD(&carp_tok); TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { struct ifaddr_container *ifac; TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (IN6_ARE_ADDR_EQUAL(taddr, &ifatoia6(ifa)->ia_addr.sin6_addr) && CARP_IS_RUNNING(&vh->sc_if) && vh->sc_state == MASTER) { return (ifa); } } } return (NULL); } void * carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr) { struct m_tag *mtag; struct carp_if *cif = v; struct carp_softc *sc; ASSERT_LWKT_TOKEN_HELD(&carp_tok); TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { struct ifaddr_container *ifac; TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (IN6_ARE_ADDR_EQUAL(taddr, &ifatoia6(ifa)->ia_addr.sin6_addr) && CARP_IS_RUNNING(&sc->sc_if)) { struct ifnet *ifp = &sc->sc_if; mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), MB_DONTWAIT); if (mtag == NULL) { /* better a bit than nothing */ return (IF_LLADDR(ifp)); } bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *)); m_tag_prepend(m, mtag); return (IF_LLADDR(ifp)); } } } return (NULL); } #endif static struct ifnet * carp_forus(struct carp_if *cif, const uint8_t *dhost) { struct carp_softc *sc; ASSERT_LWKT_TOKEN_HELD(&carp_tok); if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0) return NULL; TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { struct ifnet *ifp = &sc->sc_if; if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER && !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN)) return ifp; } return NULL; } static void carp_master_down_timeout(void *xsc) { struct carp_softc *sc = xsc; CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n", sc->sc_if.if_xname); carp_gettok(); carp_master_down(sc); carp_reltok(); } static void carp_master_down(struct carp_softc *sc) { switch (sc->sc_state) { case INIT: kprintf("%s: master_down event in INIT state\n", sc->sc_if.if_xname); break; case MASTER: break; case BACKUP: carp_set_state(sc, MASTER); carp_send_ad(sc); carp_send_arp(sc); #ifdef INET6 carp_send_na(sc); #endif /* INET6 */ carp_setrun(sc, 0); carp_setroute(sc, RTM_ADD); break; } } /* * When in backup state, af indicates whether to reset the master down timer * for v4 or v6. If it's set to zero, reset the ones which are already pending. */ static void carp_setrun(struct carp_softc *sc, sa_family_t af) { struct ifnet *cifp = &sc->sc_if; struct timeval tv; if (sc->sc_carpdev == NULL) { carp_set_state(sc, INIT); return; } if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6)) { /* Nothing */ } else { carp_setroute(sc, RTM_DELETE); return; } switch (sc->sc_state) { case INIT: if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) { carp_send_ad(sc); carp_send_arp(sc); #ifdef INET6 carp_send_na(sc); #endif /* INET6 */ CARP_DEBUG("%s: INIT -> MASTER (preempting)\n", cifp->if_xname); carp_set_state(sc, MASTER); carp_setroute(sc, RTM_ADD); } else { CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname); carp_set_state(sc, BACKUP); carp_setroute(sc, RTM_DELETE); carp_setrun(sc, 0); } break; case BACKUP: callout_stop(&sc->sc_ad_tmo); tv.tv_sec = 3 * sc->sc_advbase; tv.tv_usec = sc->sc_advskew * 1000000 / 256; switch (af) { #ifdef INET case AF_INET: callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv), carp_master_down_timeout, sc); break; #endif /* INET */ #ifdef INET6 case AF_INET6: callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv), carp_master_down_timeout, sc); break; #endif /* INET6 */ default: if (sc->sc_naddrs) callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv), carp_master_down_timeout, sc); if (sc->sc_naddrs6) callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv), carp_master_down_timeout, sc); break; } break; case MASTER: tv.tv_sec = sc->sc_advbase; tv.tv_usec = sc->sc_advskew * 1000000 / 256; callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), carp_send_ad_timeout, sc); break; } } static void carp_multicast_cleanup(struct carp_softc *sc) { struct ip_moptions *imo = &sc->sc_imo; if (imo->imo_num_memberships == 0) return; KKASSERT(imo->imo_num_memberships == 1); in_delmulti(imo->imo_membership[0]); imo->imo_membership[0] = NULL; imo->imo_num_memberships = 0; imo->imo_multicast_ifp = NULL; } #ifdef INET6 static void carp_multicast6_cleanup(struct carp_softc *sc) { struct ip6_moptions *im6o = &sc->sc_im6o; while (!LIST_EMPTY(&im6o->im6o_memberships)) { struct in6_multi_mship *imm = LIST_FIRST(&im6o->im6o_memberships); LIST_REMOVE(imm, i6mm_chain); in6_leavegroup(imm); } im6o->im6o_multicast_ifp = NULL; } #endif static int carp_get_vhaddr(struct carp_softc *sc, struct ifdrv *ifd) { const struct carp_vhaddr *vha; struct ifcarpvhaddr *carpa, *carpa0; int count, len, error; count = 0; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) ++count; if (ifd->ifd_len == 0) { ifd->ifd_len = count * sizeof(*carpa); return 0; } else if (count == 0 || ifd->ifd_len < sizeof(*carpa)) { ifd->ifd_len = 0; return 0; } len = min(ifd->ifd_len, sizeof(*carpa) * count); KKASSERT(len >= sizeof(*carpa)); carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO); if (carpa == NULL) return ENOMEM; count = 0; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (len < sizeof(*carpa)) break; carpa->carpa_flags = vha->vha_flags; carpa->carpa_addr.sin_family = AF_INET; carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr; carpa->carpa_baddr.sin_family = AF_INET; if (vha->vha_iaback == NULL) { carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY; } else { carpa->carpa_baddr.sin_addr = vha->vha_iaback->ia_addr.sin_addr; } ++carpa; ++count; len -= sizeof(*carpa); } ifd->ifd_len = sizeof(*carpa) * count; KKASSERT(ifd->ifd_len > 0); error = copyout(carpa0, ifd->ifd_data, ifd->ifd_len); kfree(carpa0, M_TEMP); return error; } static int carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, struct in_ifaddr *ia_del) { struct ifnet *ifp; struct in_ifaddr *ia_if; struct in_ifaddr_container *iac; const struct sockaddr_in *sin; u_long iaddr; int own; KKASSERT(vha->vha_ia != NULL); sin = &vha->vha_ia->ia_addr; iaddr = ntohl(sin->sin_addr.s_addr); ia_if = NULL; own = 0; TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) { struct in_ifaddr *ia = iac->ia; if (ia == ia_del) continue; if (ia->ia_ifp->if_type == IFT_CARP) continue; if ((ia->ia_ifp->if_flags & IFF_UP) == 0) continue; /* and, yeah, we need a multicast-capable iface too */ if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0) continue; if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) { if (sin->sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr) own = 1; if (ia_if == NULL) ia_if = ia; else if (sc->sc_carpdev != NULL && sc->sc_carpdev == ia->ia_ifp) ia_if = ia; } } carp_deactivate_vhaddr(sc, vha, FALSE); if (!ia_if) return ENOENT; ifp = ia_if->ia_ifp; /* XXX Don't allow parent iface to be changed */ if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) return EEXIST; return carp_activate_vhaddr(sc, vha, ifp, ia_if, own); } static void carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa) { struct carp_vhaddr *vha_new; struct in_ifaddr *carp_ia; #ifdef INVARIANTS struct carp_vhaddr *vha; #endif KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET); carp_ia = ifatoia(carp_ifa); #ifdef INVARIANTS TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia); #endif vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO); vha_new->vha_ia = carp_ia; carp_insert_vhaddr(sc, vha_new); if (carp_config_vhaddr(sc, vha_new, NULL) != 0) { /* * If the above configuration fails, it may only mean * that the new address is problematic. However, the * carp(4) interface may already have several working * addresses. Since the expected behaviour of * SIOC[AS]IFADDR is to put the NIC into working state, * we try starting the state machine manually here with * the hope that the carp(4)'s previously working * addresses still could be brought up. */ carp_hmac_prepare(sc); carp_set_state(sc, INIT); carp_setrun(sc, 0); } } static void carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa) { struct carp_vhaddr *vha; struct in_ifaddr *carp_ia; KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET); carp_ia = ifatoia(carp_ifa); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { KKASSERT(vha->vha_ia != NULL); if (vha->vha_ia == carp_ia) break; } KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa)); /* * Remove the vhaddr from the list before deactivating * the vhaddr, so that the HMAC could be correctly * updated in carp_deactivate_vhaddr() */ carp_remove_vhaddr(sc, vha); carp_deactivate_vhaddr(sc, vha, FALSE); kfree(vha, M_CARP); } static void carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa) { struct carp_vhaddr *vha; struct in_ifaddr *carp_ia; KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET); carp_ia = ifatoia(carp_ifa); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { KKASSERT(vha->vha_ia != NULL); if (vha->vha_ia == carp_ia) break; } KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa)); /* Remove then reinsert, to keep the vhaddr list sorted */ carp_remove_vhaddr(sc, vha); carp_insert_vhaddr(sc, vha); if (carp_config_vhaddr(sc, vha, NULL) != 0) { /* See the comment in carp_add_addr() */ carp_hmac_prepare(sc); carp_set_state(sc, INIT); carp_setrun(sc, 0); } } #ifdef notyet #ifdef INET6 static int carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) { struct ifnet *ifp; struct carp_if *cif; struct in6_ifaddr *ia, *ia_if; struct ip6_moptions *im6o = &sc->sc_im6o; struct in6_multi_mship *imm; struct in6_addr in6; int own, error; if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { carp_setrun(sc, 0); return (0); } /* we have to do it by hands to check we won't match on us */ ia_if = NULL; own = 0; for (ia = in6_ifaddr; ia; ia = ia->ia_next) { int i; for (i = 0; i < 4; i++) { if ((sin6->sin6_addr.s6_addr32[i] & ia->ia_prefixmask.sin6_addr.s6_addr32[i]) != (ia->ia_addr.sin6_addr.s6_addr32[i] & ia->ia_prefixmask.sin6_addr.s6_addr32[i])) break; } /* and, yeah, we need a multicast-capable iface too */ if (ia->ia_ifp != &sc->sc_if && (ia->ia_ifp->if_flags & IFF_MULTICAST) && (i == 4)) { if (!ia_if) ia_if = ia; if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ia->ia_addr.sin6_addr)) own++; } } if (!ia_if) return (EADDRNOTAVAIL); ia = ia_if; ifp = ia->ia_ifp; if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 || (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp)) return (EADDRNOTAVAIL); if (!sc->sc_naddrs6) { im6o->im6o_multicast_ifp = ifp; /* join CARP multicast address */ bzero(&in6, sizeof(in6)); in6.s6_addr16[0] = htons(0xff02); in6.s6_addr8[15] = 0x12; if (in6_setscope(&in6, ifp, NULL) != 0) goto cleanup; if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL) goto cleanup; LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); /* join solicited multicast address */ bzero(&in6, sizeof(in6)); in6.s6_addr16[0] = htons(0xff02); in6.s6_addr32[1] = 0; in6.s6_addr32[2] = htonl(1); in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3]; in6.s6_addr8[12] = 0xff; if (in6_setscope(&in6, ifp, NULL) != 0) goto cleanup; if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL) goto cleanup; LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); } if (!ifp->if_carp) { cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO); if ((error = ifpromisc(ifp, 1))) { kfree(cif, M_CARP); goto cleanup; } TAILQ_INIT(&cif->vhif_vrs); ifp->if_carp = cif; } else { struct carp_softc *vr; cif = ifp->if_carp; TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { if (vr != sc && vr->sc_vhid == sc->sc_vhid) { error = EINVAL; goto cleanup; } } } sc->sc_ia6 = ia; sc->sc_carpdev = ifp; { /* XXX prevent endless loop if already in queue */ struct carp_softc *vr, *after = NULL; int myself = 0; cif = ifp->if_carp; TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { if (vr == sc) myself = 1; if (vr->sc_vhid < sc->sc_vhid) after = vr; } if (!myself) { /* We're trying to keep things in order */ if (after == NULL) TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); else TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list); } } sc->sc_naddrs6++; if (own) sc->sc_advskew = 0; carp_sc_state(sc); carp_setrun(sc, 0); return (0); cleanup: /* clean up multicast memberships */ if (!sc->sc_naddrs6) { while (!LIST_EMPTY(&im6o->im6o_memberships)) { imm = LIST_FIRST(&im6o->im6o_memberships); LIST_REMOVE(imm, i6mm_chain); in6_leavegroup(imm); } } return (error); } static int carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) { int error = 0; if (!--sc->sc_naddrs6) { struct carp_if *cif = sc->sc_carpdev->if_carp; struct ip6_moptions *im6o = &sc->sc_im6o; callout_stop(&sc->sc_ad_tmo); sc->sc_vhid = -1; while (!LIST_EMPTY(&im6o->im6o_memberships)) { struct in6_multi_mship *imm = LIST_FIRST(&im6o->im6o_memberships); LIST_REMOVE(imm, i6mm_chain); in6_leavegroup(imm); } im6o->im6o_multicast_ifp = NULL; TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); if (TAILQ_EMPTY(&cif->vhif_vrs)) { sc->sc_carpdev->if_carp = NULL; kfree(cif, M_IFADDR); } } return (error); } #endif /* INET6 */ #endif static int carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr) { struct carp_softc *sc = ifp->if_softc; struct carpreq carpr; struct ifaddr *ifa; struct ifreq *ifr; struct ifaliasreq *ifra; struct ifdrv *ifd; char devname[IFNAMSIZ]; int error = 0; ASSERT_IFNET_SERIALIZED_ALL(ifp); carp_gettok(); ifa = (struct ifaddr *)addr; ifra = (struct ifaliasreq *)addr; ifr = (struct ifreq *)addr; ifd = (struct ifdrv *)addr; switch (cmd) { case SIOCSIFFLAGS: if (ifp->if_flags & IFF_UP) { if ((ifp->if_flags & IFF_RUNNING) == 0) carp_init(sc); } else if (ifp->if_flags & IFF_RUNNING) { carp_ioctl_stop(sc); } break; case SIOCSVH: error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY); if (error) break; error = copyin(ifr->ifr_data, &carpr, sizeof(carpr)); if (error) break; error = carp_ioctl_setvh(sc, &carpr); break; case SIOCGVH: carp_ioctl_getvh(sc, &carpr); error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY); if (error) bzero(carpr.carpr_key, sizeof(carpr.carpr_key)); error = copyout(&carpr, ifr->ifr_data, sizeof(carpr)); break; case SIOCGDRVSPEC: switch (ifd->ifd_cmd) { case CARPGDEVNAME: if (ifd->ifd_len != sizeof(devname)) error = EINVAL; break; case CARPGVHADDR: break; default: error = EINVAL; break; } if (error) break; switch (ifd->ifd_cmd) { case CARPGVHADDR: error = carp_get_vhaddr(sc, ifd); break; case CARPGDEVNAME: carp_ioctl_getdevname(sc, devname); error = copyout(devname, ifd->ifd_data, sizeof(devname)); break; } break; default: error = ether_ioctl(ifp, cmd, addr); break; } carp_reltok(); return error; } static void carp_ioctl_stop_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; carp_gettok(); carp_stop(sc, 0); carp_reltok(); lwkt_replymsg(&cmsg->base.lmsg, 0); } static void carp_ioctl_stop(struct carp_softc *sc) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_stop_dispatch); cmsg.nc_softc = sc; lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0); ifnet_serialize_all(ifp); } static void carp_ioctl_setvh_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc, *vr; struct ifnet *ifp = &sc->arpcom.ac_if; const struct carpreq *carpr = cmsg->nc_data; int error; carp_gettok(); error = 1; if ((ifp->if_flags & IFF_RUNNING) && sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) { switch (carpr->carpr_state) { case BACKUP: callout_stop(&sc->sc_ad_tmo); carp_set_state(sc, BACKUP); carp_setrun(sc, 0); carp_setroute(sc, RTM_DELETE); break; case MASTER: carp_master_down(sc); break; default: break; } } if (carpr->carpr_vhid > 0) { if (carpr->carpr_vhid > 255) { error = EINVAL; goto back; } if (sc->sc_carpdev) { struct carp_if *cif = sc->sc_carpdev->if_carp; TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { if (vr != sc && vr->sc_vhid == carpr->carpr_vhid) { error = EEXIST; goto back; } } } sc->sc_vhid = carpr->carpr_vhid; IF_LLADDR(ifp)[5] = sc->sc_vhid; bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); error--; } if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) { if (carpr->carpr_advskew >= 255) { error = EINVAL; goto back; } if (carpr->carpr_advbase > 255) { error = EINVAL; goto back; } sc->sc_advbase = carpr->carpr_advbase; sc->sc_advskew = carpr->carpr_advskew; error--; } bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key)); if (error > 0) { error = EINVAL; } else { error = 0; carp_setrun(sc, 0); } back: carp_hmac_prepare(sc); carp_gettok(); lwkt_replymsg(&cmsg->base.lmsg, error); } static int carp_ioctl_setvh(struct carp_softc *sc, struct carpreq *carpr) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; int error; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_setvh_dispatch); cmsg.nc_softc = sc; cmsg.nc_data = carpr; error = lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0); ifnet_serialize_all(ifp); return error; } static void carp_ioctl_getvh_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; struct carpreq *carpr = cmsg->nc_data; carp_gettok(); carpr->carpr_state = sc->sc_state; carpr->carpr_vhid = sc->sc_vhid; carpr->carpr_advbase = sc->sc_advbase; carpr->carpr_advskew = sc->sc_advskew; bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key)); carp_reltok(); lwkt_replymsg(&cmsg->base.lmsg, 0); } static void carp_ioctl_getvh(struct carp_softc *sc, struct carpreq *carpr) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_getvh_dispatch); cmsg.nc_softc = sc; cmsg.nc_data = carpr; lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0); ifnet_serialize_all(ifp); } static void carp_ioctl_getdevname_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; char *devname = cmsg->nc_data; bzero(devname, sizeof(devname)); carp_gettok(); if (sc->sc_carpdev != NULL) strlcpy(devname, sc->sc_carpdev->if_xname, sizeof(devname)); carp_reltok(); lwkt_replymsg(&cmsg->base.lmsg, 0); } static void carp_ioctl_getdevname(struct carp_softc *sc, char *devname) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_getdevname_dispatch); cmsg.nc_softc = sc; cmsg.nc_data = devname; lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0); ifnet_serialize_all(ifp); } static void carp_init_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; carp_gettok(); sc->sc_if.if_flags |= IFF_RUNNING; carp_hmac_prepare(sc); carp_set_state(sc, INIT); carp_setrun(sc, 0); carp_reltok(); lwkt_replymsg(&cmsg->base.lmsg, 0); } static void carp_init(void *xsc) { struct carp_softc *sc = xsc; struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_init_dispatch); cmsg.nc_softc = sc; lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0); ifnet_serialize_all(ifp); } static int carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, struct rtentry *rt) { struct carp_softc *sc = ifp->if_softc; int error = 0; carp_gettok(); if (sc->sc_carpdev) { /* * NOTE: * CARP's ifp is passed to backing device's * if_output method. */ sc->sc_carpdev->if_output(ifp, m, dst, rt); } else { m_freem(m); error = ENETUNREACH; } carp_reltok(); return error; } /* * Start output on carp interface. This function should never be called. */ static void carp_start(struct ifnet *ifp) { panic("%s: start called\n", ifp->if_xname); } static void carp_serialize(struct ifnet *ifp __unused, enum ifnet_serialize slz __unused) { } static void carp_deserialize(struct ifnet *ifp __unused, enum ifnet_serialize slz __unused) { } static int carp_tryserialize(struct ifnet *ifp __unused, enum ifnet_serialize slz __unused) { return 1; } #ifdef INVARIANTS static void carp_serialize_assert(struct ifnet *ifp __unused, enum ifnet_serialize slz __unused, boolean_t serialized __unused) { } #endif /* INVARIANTS */ static void carp_set_state(struct carp_softc *sc, int state) { struct ifnet *cifp = &sc->sc_if; if (sc->sc_state == state) return; sc->sc_state = state; switch (sc->sc_state) { case BACKUP: cifp->if_link_state = LINK_STATE_DOWN; break; case MASTER: cifp->if_link_state = LINK_STATE_UP; break; default: cifp->if_link_state = LINK_STATE_UNKNOWN; break; } rt_ifmsg(cifp); } void carp_group_demote_adj(struct ifnet *ifp, int adj) { struct ifg_list *ifgl; int *dm; carp_gettok(); TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL)) continue; dm = &ifgl->ifgl_group->ifg_carp_demoted; if (*dm + adj >= 0) *dm += adj; else *dm = 0; if (adj > 0 && *dm == 1) carp_send_ad_all(); CARP_LOG("%s demoted group %s to %d", ifp->if_xname, ifgl->ifgl_group->ifg_group, *dm); } carp_reltok(); } void carp_carpdev_state(void *v) { struct carp_if *cif = v; struct carp_softc *sc; carp_gettok(); TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) carp_sc_state(sc); carp_reltok(); } static void carp_sc_state(struct carp_softc *sc) { if (!(sc->sc_carpdev->if_flags & IFF_UP)) { callout_stop(&sc->sc_ad_tmo); callout_stop(&sc->sc_md_tmo); callout_stop(&sc->sc_md6_tmo); carp_set_state(sc, INIT); carp_setrun(sc, 0); if (!sc->sc_suppress) { carp_suppress_preempt++; if (carp_suppress_preempt == 1) carp_send_ad_all(); } sc->sc_suppress = 1; } else { carp_set_state(sc, INIT); carp_setrun(sc, 0); if (sc->sc_suppress) carp_suppress_preempt--; sc->sc_suppress = 0; } } static void carp_stop(struct carp_softc *sc, int detach) { sc->sc_if.if_flags &= ~IFF_RUNNING; callout_stop(&sc->sc_ad_tmo); callout_stop(&sc->sc_md_tmo); callout_stop(&sc->sc_md6_tmo); if (!detach && sc->sc_state == MASTER) carp_send_ad(sc); if (sc->sc_suppress) carp_suppress_preempt--; sc->sc_suppress = 0; if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) carp_suppress_preempt--; sc->sc_sendad_errors = 0; sc->sc_sendad_success = 0; carp_set_state(sc, INIT); carp_setrun(sc, 0); } static void carp_suspend(struct carp_softc *sc, int detach) { struct ifnet *cifp = &sc->sc_if; carp_stop(sc, detach); /* Retain the running state, if we are not dead yet */ if (!sc->sc_dead && (cifp->if_flags & IFF_UP)) cifp->if_flags |= IFF_RUNNING; } static int carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, struct ifnet *ifp, struct in_ifaddr *ia_if, int own) { struct ip_moptions *imo = &sc->sc_imo; struct carp_if *cif; struct carp_softc *vr, *after = NULL; int onlist, error; #ifdef INVARIANTS int assert_onlist; #endif KKASSERT(vha->vha_ia != NULL); KASSERT(ia_if != NULL, ("NULL backing address\n")); KASSERT(vha->vha_iaback == NULL, ("%p is already activated\n", vha)); KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0, ("inactive vhaddr %p is the address owner\n", vha)); KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp, ("%s is already on %s\n", sc->sc_if.if_xname, sc->sc_carpdev->if_xname)); if (!ifp->if_carp) { KASSERT(sc->sc_carpdev == NULL, ("%s is already on %s\n", sc->sc_if.if_xname, sc->sc_carpdev->if_xname)); cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO); error = ifpromisc(ifp, 1); if (error) { kfree(cif, M_CARP); return error; } TAILQ_INIT(&cif->vhif_vrs); ifp->if_carp = cif; } else { cif = ifp->if_carp; TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { if (vr != sc && vr->sc_vhid == sc->sc_vhid) return EINVAL; } } #ifdef INVARIANTS if (sc->sc_carpdev != NULL) assert_onlist = 1; else assert_onlist = 0; #endif sc->sc_ia = ia_if; sc->sc_carpdev = ifp; cif = ifp->if_carp; onlist = 0; TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { if (vr == sc) onlist = 1; if (vr->sc_vhid < sc->sc_vhid) after = vr; } #ifdef INVARIANTS if (assert_onlist) { KASSERT(onlist, ("%s is not on %s carp list\n", sc->sc_if.if_xname, ifp->if_xname)); } else { KASSERT(!onlist, ("%s is already on %s carp list\n", sc->sc_if.if_xname, ifp->if_xname)); } #endif if (!onlist) { /* We're trying to keep things in order */ if (after == NULL) TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); else TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list); } vha->vha_iaback = ia_if; sc->sc_naddrs++; if (own) { vha->vha_flags |= CARP_VHAF_OWNER; /* XXX save user configured advskew? */ sc->sc_advskew = 0; } carp_addroute_vhaddr(sc, vha); /* * Join the multicast group only after the backing interface * has been hooked with the CARP interface. */ KASSERT(imo->imo_multicast_ifp == NULL || imo->imo_multicast_ifp == &sc->sc_if, ("%s didn't leave mcast group on %s\n", sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname)); if (imo->imo_num_memberships == 0) { struct in_addr addr; addr.s_addr = htonl(INADDR_CARP_GROUP); imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if); if (imo->imo_membership[0] == NULL) { carp_deactivate_vhaddr(sc, vha, FALSE); return ENOBUFS; } imo->imo_num_memberships++; imo->imo_multicast_ifp = &sc->sc_if; imo->imo_multicast_ttl = CARP_DFLTTL; imo->imo_multicast_loop = 0; } carp_hmac_prepare(sc); carp_set_state(sc, INIT); carp_setrun(sc, 0); return 0; } static void carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, boolean_t del_iaback) { KKASSERT(vha->vha_ia != NULL); carp_hmac_prepare(sc); if (vha->vha_iaback == NULL) { KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0, ("inactive vhaddr %p is the address owner\n", vha)); return; } vha->vha_flags &= ~CARP_VHAF_OWNER; carp_delroute_vhaddr(sc, vha, del_iaback); KKASSERT(sc->sc_naddrs > 0); vha->vha_iaback = NULL; sc->sc_naddrs--; if (!sc->sc_naddrs) { if (sc->sc_naddrs6) { carp_multicast_cleanup(sc); sc->sc_ia = NULL; } else { carp_detach(sc, 0, del_iaback); } } } static void carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if) { struct carp_vhaddr *vha; struct in_ifaddr *ia_if; KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET); ia_if = ifatoia(ifa_if); /* * Test each inactive vhaddr against the newly added address. * If the newly added address could be the backing address, * then activate the matching vhaddr. */ TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { const struct in_ifaddr *ia; u_long iaddr; int own; if (vha->vha_iaback != NULL) continue; ia = vha->vha_ia; iaddr = ntohl(ia->ia_addr.sin_addr.s_addr); if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet) continue; own = 0; if (ia->ia_addr.sin_addr.s_addr == ia_if->ia_addr.sin_addr.s_addr) own = 1; carp_activate_vhaddr(sc, vha, ifp, ia_if, own); } } static void carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if) { struct carp_vhaddr *vha; struct in_ifaddr *ia_if; KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET); ia_if = ifatoia(ifa_if); /* * Ad src address is deleted; set it to NULL. * Following loop will try pick up a new ad src address * if one of the vhaddr could retain its backing address. */ if (sc->sc_ia == ia_if) sc->sc_ia = NULL; /* * Test each active vhaddr against the deleted address. * If the deleted address is vhaddr address's backing * address, then deactivate the vhaddr. */ TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (vha->vha_iaback == NULL) continue; if (vha->vha_iaback == ia_if) carp_deactivate_vhaddr(sc, vha, TRUE); else if (sc->sc_ia == NULL) sc->sc_ia = vha->vha_iaback; } } static void carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del) { struct carp_vhaddr *vha; KKASSERT(sc->sc_carpdev == NULL); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) carp_config_vhaddr(sc, vha, ifatoia(ifa_del)); } static void carp_ifaddr(void *arg __unused, struct ifnet *ifp, enum ifaddr_event event, struct ifaddr *ifa) { struct carp_softc *sc; carp_gettok(); if (ifa->ifa_addr->sa_family != AF_INET) goto back; KASSERT(&curthread->td_msgport == cpu_portfn(0), ("not in netisr0")); if (ifp->if_type == IFT_CARP) { /* * Address is changed on carp(4) interface */ switch (event) { case IFADDR_EVENT_ADD: carp_add_addr(ifp->if_softc, ifa); break; case IFADDR_EVENT_CHANGE: carp_config_addr(ifp->if_softc, ifa); break; case IFADDR_EVENT_DELETE: carp_del_addr(ifp->if_softc, ifa); break; } goto back; } /* * Address is changed on non-carp(4) interface */ if ((ifp->if_flags & IFF_MULTICAST) == 0) goto back; LIST_FOREACH(sc, &carpif_list, sc_next) { if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) { /* Not the parent iface; skip */ continue; } switch (event) { case IFADDR_EVENT_ADD: carp_link_addrs(sc, ifp, ifa); break; case IFADDR_EVENT_DELETE: if (sc->sc_carpdev != NULL) { carp_unlink_addrs(sc, ifp, ifa); if (sc->sc_carpdev == NULL) { /* * We no longer have the parent * interface, however, certain * virtual addresses, which are * not used because they can't * match the previous parent * interface's addresses, may now * match different interface's * addresses. */ carp_update_addrs(sc, ifa); } } else { /* * The carp(4) interface didn't have a * parent iface, so it is not possible * that it will contain any address to * be unlinked. */ } break; case IFADDR_EVENT_CHANGE: if (sc->sc_carpdev == NULL) { /* * The carp(4) interface didn't have a * parent iface, so it is not possible * that it will contain any address to * be updated. */ carp_link_addrs(sc, ifp, ifa); } else { /* * First try breaking tie with the old * address. Then see whether we could * link certain vhaddr to the new address. * If that fails, i.e. carpdev is NULL, * we try a global update. * * NOTE: The above order is critical. */ carp_unlink_addrs(sc, ifp, ifa); carp_link_addrs(sc, ifp, ifa); if (sc->sc_carpdev == NULL) { /* * See the comment in the above * IFADDR_EVENT_DELETE block. */ carp_update_addrs(sc, NULL); } } break; } } back: carp_reltok(); } void carp_proto_ctlinput(netmsg_t msg) { int cmd = msg->ctlinput.nm_cmd; struct sockaddr *sa = msg->ctlinput.nm_arg; struct in_ifaddr_container *iac; carp_gettok(); TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) { struct in_ifaddr *ia = iac->ia; struct ifnet *ifp = ia->ia_ifp; if (ifp->if_type == IFT_CARP) continue; if (ia->ia_ifa.ifa_addr == sa) { if (cmd == PRC_IFDOWN) { carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE, &ia->ia_ifa); } else if (cmd == PRC_IFUP) { carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD, &ia->ia_ifa); } break; } } carp_reltok(); lwkt_replymsg(&msg->lmsg, 0); } void carp_gettok(void) { lwkt_gettoken(&carp_tok); } void carp_reltok(void) { lwkt_reltoken(&carp_tok); } struct ifnet * carp_parent(struct ifnet *cifp) { struct carp_softc *sc; ASSERT_LWKT_TOKEN_HELD(&carp_tok); KKASSERT(cifp->if_type == IFT_CARP); sc = cifp->if_softc; return sc->sc_carpdev; } #define rtinitflags(x) \ (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \ ? RTF_HOST : 0) static int carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha) { struct in_ifaddr *ia, *iaback; int error; if (sc->sc_state != MASTER) return 0; ia = vha->vha_ia; KKASSERT(ia != NULL); iaback = vha->vha_iaback; KKASSERT(iaback != NULL); rtinit(&iaback->ia_ifa, RTM_DELETE, rtinitflags(iaback)); in_ifadown(&iaback->ia_ifa, 1); iaback->ia_flags &= ~IFA_ROUTE; error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP); if (!error) ia->ia_flags |= IFA_ROUTE; return error; } static void carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, boolean_t del_iaback) { struct in_ifaddr *ia, *iaback; ia = vha->vha_ia; KKASSERT(ia != NULL); iaback = vha->vha_iaback; KKASSERT(iaback != NULL); rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia)); in_ifadown(&ia->ia_ifa, 1); ia->ia_flags &= ~IFA_ROUTE; if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) { int error; error = rtinit(&iaback->ia_ifa, RTM_ADD, rtinitflags(iaback) | RTF_UP); if (!error) iaback->ia_flags |= IFA_ROUTE; } } static int carp_modevent(module_t mod, int type, void *data) { switch (type) { case MOD_LOAD: LIST_INIT(&carpif_list); carp_ifdetach_event = EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY); carp_ifaddr_event = EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL, EVENTHANDLER_PRI_FIRST); if_clone_attach(&carp_cloner); break; case MOD_UNLOAD: EVENTHANDLER_DEREGISTER(ifnet_detach_event, carp_ifdetach_event); EVENTHANDLER_DEREGISTER(ifaddr_event, carp_ifaddr_event); if_clone_detach(&carp_cloner); break; default: return (EINVAL); } return (0); } static moduledata_t carp_mod = { "carp", carp_modevent, 0 }; DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);