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 $
28 * $DragonFly: src/sys/netinet/ip_carp.c,v 1.10 2008/07/27 10:06:57 sephe Exp $
33 #include "opt_inet6.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/in_cksum.h>
39 #include <sys/limits.h>
40 #include <sys/malloc.h>
45 #include <sys/sockio.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
50 #include <machine/stdarg.h>
51 #include <crypto/sha1.h>
54 #include <net/ethernet.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 #include <net/if_clone.h>
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/if_ether.h>
71 #include <netinet/icmp6.h>
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/scope6_var.h>
75 #include <netinet6/nd6.h>
78 #include <netinet/ip_carp.h>
80 #define CARP_IFNAME "carp"
81 #define CARP_IS_RUNNING(ifp) \
82 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
85 uint32_t vha_flags; /* CARP_VHAF_ */
86 const struct in_ifaddr *vha_ia; /* carp address */
87 const struct in_ifaddr *vha_iaback; /* backing address */
88 TAILQ_ENTRY(carp_vhaddr) vha_link;
90 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr);
94 struct ifnet *sc_carpdev; /* parent interface */
95 struct carp_vhaddr_list sc_vha_list; /* virtual addr list */
97 const struct in_ifaddr *sc_ia; /* primary iface address v4 */
98 struct ip_moptions sc_imo;
101 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
102 struct ip6_moptions sc_im6o;
104 TAILQ_ENTRY(carp_softc) sc_list;
106 enum { INIT = 0, BACKUP, MASTER }
112 int sc_sendad_errors;
113 #define CARP_SENDAD_MAX_ERRORS 3
114 int sc_sendad_success;
115 #define CARP_SENDAD_MIN_SUCCESS 3
119 int sc_naddrs; /* actually used IPv4 vha */
121 int sc_advbase; /* seconds */
126 #define CARP_HMAC_PAD 64
127 unsigned char sc_key[CARP_KEY_LEN];
128 unsigned char sc_pad[CARP_HMAC_PAD];
131 struct callout sc_ad_tmo; /* advertisement timeout */
132 struct callout sc_md_tmo; /* master down timeout */
133 struct callout sc_md6_tmo; /* master down timeout */
135 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
139 TAILQ_HEAD(, carp_softc) vhif_vrs;
142 enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
144 SYSCTL_DECL(_net_inet_carp);
146 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
147 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
148 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
149 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
150 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
151 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
152 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
153 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
154 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
156 static int carp_suppress_preempt = 0;
157 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
158 &carp_suppress_preempt, 0, "Preemption is suppressed");
160 static struct carpstats carpstats;
161 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
162 &carpstats, carpstats,
163 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
165 #define CARP_LOG(...) do { \
166 if (carp_opts[CARPCTL_LOG] > 0) \
167 log(LOG_INFO, __VA_ARGS__); \
170 #define CARP_DEBUG(...) do { \
171 if (carp_opts[CARPCTL_LOG] > 1) \
172 log(LOG_DEBUG, __VA_ARGS__); \
175 static void carp_hmac_prepare(struct carp_softc *);
176 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
178 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
180 static void carp_setroute(struct carp_softc *, int);
181 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
182 static int carp_clone_create(struct if_clone *, int, caddr_t);
183 static int carp_clone_destroy(struct ifnet *);
184 static void carp_detach(struct carp_softc *, int);
185 static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
186 struct carp_header *);
187 static void carp_send_ad_all(void);
188 static void carp_send_ad_timeout(void *);
189 static void carp_send_ad(struct carp_softc *);
190 static void carp_send_arp(struct carp_softc *);
191 static void carp_master_down_timeout(void *);
192 static void carp_master_down(struct carp_softc *);
193 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
194 static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
196 static void carp_start(struct ifnet *);
197 static void carp_setrun(struct carp_softc *, sa_family_t);
198 static void carp_set_state(struct carp_softc *, int);
200 static void carp_multicast_cleanup(struct carp_softc *);
201 static void carp_add_addr(struct carp_softc *, struct ifaddr *);
202 static void carp_del_addr(struct carp_softc *, struct ifaddr *);
203 static void carp_config_addr(struct carp_softc *, struct ifaddr *);
204 static void carp_link_addrs(struct carp_softc *, struct ifnet *,
206 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *,
209 static int carp_get_vhaddr(struct carp_softc *, struct ifdrv *);
210 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *);
211 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
212 struct ifnet *, const struct in_ifaddr *, int);
213 static void carp_deactivate_vhaddr(struct carp_softc *,
214 struct carp_vhaddr *);
216 static void carp_sc_state(struct carp_softc *);
218 static void carp_send_na(struct carp_softc *);
219 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
220 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
221 static void carp_multicast6_cleanup(struct carp_softc *);
223 static void carp_stop(struct carp_softc *, int);
224 static void carp_reset(struct carp_softc *, int);
226 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
228 static void carp_ifdetach(void *, struct ifnet *);
230 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
232 static LIST_HEAD(, carp_softc) carpif_list;
234 static struct if_clone carp_cloner =
235 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
238 static eventhandler_tag carp_ifdetach_event;
239 static eventhandler_tag carp_ifaddr_event;
242 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
244 struct carp_vhaddr *vha;
245 u_long new_addr, addr;
247 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
250 * Virtual address list is sorted; smaller one first
252 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
254 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
255 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
261 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
263 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
264 vha_new->vha_flags |= CARP_VHAF_ONLIST;
268 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
270 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
271 vha->vha_flags &= ~CARP_VHAF_ONLIST;
272 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
276 carp_hmac_prepare(struct carp_softc *sc)
278 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
279 uint8_t vhid = sc->sc_vhid & 0xff;
282 struct ifaddr_container *ifac;
286 struct carp_vhaddr *vha;
289 /* XXX: possible race here */
291 /* compute ipad from key */
292 bzero(sc->sc_pad, sizeof(sc->sc_pad));
293 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
294 for (i = 0; i < sizeof(sc->sc_pad); i++)
295 sc->sc_pad[i] ^= 0x36;
297 /* precompute first part of inner hash */
298 SHA1Init(&sc->sc_sha1);
299 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
300 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
301 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
302 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
304 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
305 SHA1Update(&sc->sc_sha1,
306 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
307 sizeof(struct in_addr));
311 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
312 struct ifaddr *ifa = ifac->ifa;
314 if (ifa->ifa_addr->sa_family == AF_INET6) {
315 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
316 in6_clearscope(&in6);
317 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
322 /* convert ipad to opad */
323 for (i = 0; i < sizeof(sc->sc_pad); i++)
324 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
328 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
329 unsigned char md[20])
333 /* fetch first half of inner hash */
334 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
336 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
337 SHA1Final(md, &sha1ctx);
341 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
342 SHA1Update(&sha1ctx, md, 20);
343 SHA1Final(md, &sha1ctx);
347 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
348 unsigned char md[20])
350 unsigned char md2[20];
352 carp_hmac_generate(sc, counter, md2);
353 return (bcmp(md, md2, sizeof(md2)));
357 carp_setroute(struct carp_softc *sc, int cmd)
360 struct ifaddr_container *ifac;
363 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
364 struct ifaddr *ifa = ifac->ifa;
366 if (ifa->ifa_addr->sa_family == AF_INET6) {
378 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
380 struct carp_softc *sc;
383 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
387 sc->sc_advbase = CARP_DFLTINTV;
388 sc->sc_vhid = -1; /* required setting */
390 sc->sc_init_counter = 1;
394 TAILQ_INIT(&sc->sc_vha_list);
397 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
400 callout_init(&sc->sc_ad_tmo);
401 callout_init(&sc->sc_md_tmo);
402 callout_init(&sc->sc_md6_tmo);
405 if_initname(ifp, CARP_IFNAME, unit);
406 ifp->if_mtu = ETHERMTU;
407 ifp->if_flags = IFF_LOOPBACK;
408 ifp->if_ioctl = carp_ioctl;
409 ifp->if_output = carp_looutput;
410 ifp->if_start = carp_start;
411 ifp->if_type = IFT_CARP;
412 ifp->if_snd.ifq_maxlen = ifqmaxlen;
414 if_attach(ifp, NULL);
415 bpfattach(ifp, DLT_NULL, sizeof(u_int));
418 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
425 carp_clone_destroy(struct ifnet *ifp)
427 struct carp_softc *sc = ifp->if_softc;
433 LIST_REMOVE(sc, sc_next);
438 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active\n"));
445 carp_detach(struct carp_softc *sc, int detach)
449 carp_reset(sc, detach);
451 carp_multicast_cleanup(sc);
453 carp_multicast6_cleanup(sc);
456 if (!sc->sc_dead && detach) {
457 struct carp_vhaddr *vha;
459 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
460 carp_deactivate_vhaddr(sc, vha);
461 KKASSERT(sc->sc_naddrs == 0);
464 if (sc->sc_carpdev != NULL) {
465 cif = sc->sc_carpdev->if_carp;
466 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
467 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
468 ifpromisc(sc->sc_carpdev, 0);
469 sc->sc_carpdev->if_carp = NULL;
472 sc->sc_carpdev = NULL;
477 /* Detach an interface from the carp. */
479 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
481 struct carp_if *cif = ifp->if_carp;
482 struct carp_softc *sc;
484 while (ifp->if_carp &&
485 (sc = TAILQ_FIRST(&cif->vhif_vrs)) != NULL)
490 * process input packet.
491 * we have rearranged checks order compared to the rfc,
492 * but it seems more efficient this way or not possible otherwise.
495 carp_input(struct mbuf **mp, int *offp, int proto)
497 struct mbuf *m = *mp;
498 struct ip *ip = mtod(m, struct ip *);
499 struct carp_header *ch;
505 carpstats.carps_ipackets++;
507 if (!carp_opts[CARPCTL_ALLOW]) {
509 return(IPPROTO_DONE);
512 /* Check if received on a valid carp interface */
513 if (m->m_pkthdr.rcvif->if_carp == NULL) {
514 carpstats.carps_badif++;
515 CARP_LOG("carp_input: packet received on non-carp "
517 m->m_pkthdr.rcvif->if_xname);
519 return(IPPROTO_DONE);
522 /* Verify that the IP TTL is CARP_DFLTTL. */
523 if (ip->ip_ttl != CARP_DFLTTL) {
524 carpstats.carps_badttl++;
525 CARP_LOG("carp_input: received ttl %d != %d on %s\n",
526 ip->ip_ttl, CARP_DFLTTL,
527 m->m_pkthdr.rcvif->if_xname);
529 return(IPPROTO_DONE);
532 /* Minimal CARP packet size */
533 len = iphlen + sizeof(*ch);
536 * Verify that the received packet length is
537 * not less than the CARP header
539 if (m->m_pkthdr.len < len) {
540 carpstats.carps_badlen++;
541 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
542 m->m_pkthdr.rcvif->if_xname);
544 return(IPPROTO_DONE);
547 /* Make sure that CARP header is contiguous */
548 if (len > m->m_len) {
549 m = m_pullup(m, len);
551 carpstats.carps_hdrops++;
552 CARP_LOG("carp_input: m_pullup failed\n");
553 return(IPPROTO_DONE);
555 ip = mtod(m, struct ip *);
557 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
559 /* Verify the CARP checksum */
560 if (in_cksum_skip(m, len, iphlen)) {
561 carpstats.carps_badsum++;
562 CARP_LOG("carp_input: checksum failed on %s\n",
563 m->m_pkthdr.rcvif->if_xname);
565 return(IPPROTO_DONE);
567 carp_input_c(m, ch, AF_INET);
568 return(IPPROTO_DONE);
573 carp6_input(struct mbuf **mp, int *offp, int proto)
575 struct mbuf *m = *mp;
576 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
577 struct carp_header *ch;
580 carpstats.carps_ipackets6++;
582 if (!carp_opts[CARPCTL_ALLOW]) {
584 return (IPPROTO_DONE);
587 /* check if received on a valid carp interface */
588 if (m->m_pkthdr.rcvif->if_carp == NULL) {
589 carpstats.carps_badif++;
590 CARP_LOG("carp6_input: packet received on non-carp "
592 m->m_pkthdr.rcvif->if_xname);
594 return (IPPROTO_DONE);
597 /* verify that the IP TTL is 255 */
598 if (ip6->ip6_hlim != CARP_DFLTTL) {
599 carpstats.carps_badttl++;
600 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
602 m->m_pkthdr.rcvif->if_xname);
604 return (IPPROTO_DONE);
607 /* verify that we have a complete carp packet */
609 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
611 carpstats.carps_badlen++;
612 CARP_LOG("carp6_input: packet size %u too small\n", len);
613 return (IPPROTO_DONE);
616 /* verify the CARP checksum */
617 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
618 carpstats.carps_badsum++;
619 CARP_LOG("carp6_input: checksum failed, on %s\n",
620 m->m_pkthdr.rcvif->if_xname);
622 return (IPPROTO_DONE);
625 carp_input_c(m, ch, AF_INET6);
626 return (IPPROTO_DONE);
631 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
633 struct ifnet *ifp = m->m_pkthdr.rcvif;
635 struct carp_softc *sc;
636 uint64_t tmp_counter;
637 struct timeval sc_tv, ch_tv;
639 /* verify that the VHID is valid on the receiving interface */
640 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
641 if (sc->sc_vhid == ch->carp_vhid)
644 if (!sc || !CARP_IS_RUNNING(&sc->sc_if)) {
645 carpstats.carps_badvhid++;
651 getmicrotime(&cifp->if_lastchange);
653 cifp->if_ibytes += m->m_pkthdr.len;
656 struct ip *ip = mtod(m, struct ip *);
658 /* BPF wants net byte order */
659 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
660 ip->ip_off = htons(ip->ip_off);
661 bpf_mtap(cifp->if_bpf, m);
664 /* verify the CARP version. */
665 if (ch->carp_version != CARP_VERSION) {
666 carpstats.carps_badver++;
668 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
674 /* verify the hash */
675 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
676 carpstats.carps_badauth++;
678 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
683 tmp_counter = ntohl(ch->carp_counter[0]);
684 tmp_counter = tmp_counter<<32;
685 tmp_counter += ntohl(ch->carp_counter[1]);
687 /* XXX Replay protection goes here */
689 sc->sc_init_counter = 0;
690 sc->sc_counter = tmp_counter;
692 sc_tv.tv_sec = sc->sc_advbase;
693 if (carp_suppress_preempt && sc->sc_advskew < 240)
694 sc_tv.tv_usec = 240 * 1000000 / 256;
696 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
697 ch_tv.tv_sec = ch->carp_advbase;
698 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
700 switch (sc->sc_state) {
706 * If we receive an advertisement from a master who's going to
707 * be more frequent than us, go into BACKUP state.
709 if (timevalcmp(&sc_tv, &ch_tv, >) ||
710 timevalcmp(&sc_tv, &ch_tv, ==)) {
711 callout_stop(&sc->sc_ad_tmo);
712 CARP_DEBUG("%s: MASTER -> BACKUP "
713 "(more frequent advertisement received)\n",
715 carp_set_state(sc, BACKUP);
717 carp_setroute(sc, RTM_DELETE);
723 * If we're pre-empting masters who advertise slower than us,
724 * and this one claims to be slower, treat him as down.
726 if (carp_opts[CARPCTL_PREEMPT] &&
727 timevalcmp(&sc_tv, &ch_tv, <)) {
728 CARP_DEBUG("%s: BACKUP -> MASTER "
729 "(preempting a slower master)\n", cifp->if_xname);
730 carp_master_down(sc);
735 * If the master is going to advertise at such a low frequency
736 * that he's guaranteed to time out, we'd might as well just
737 * treat him as timed out now.
739 sc_tv.tv_sec = sc->sc_advbase * 3;
740 if (timevalcmp(&sc_tv, &ch_tv, <)) {
741 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
743 carp_master_down(sc);
748 * Otherwise, we reset the counter and wait for the next
758 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
760 struct ifnet *cifp = &sc->sc_if;
763 if (sc->sc_init_counter) {
764 /* this could also be seconds since unix epoch */
765 sc->sc_counter = karc4random();
766 sc->sc_counter = sc->sc_counter << 32;
767 sc->sc_counter += karc4random();
772 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
773 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
775 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
777 /* Tag packet for carp_output */
778 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), MB_DONTWAIT);
784 bcopy(&cifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
785 m_tag_prepend(m, mtag);
791 carp_send_ad_all(void)
793 struct carp_softc *sc;
795 LIST_FOREACH(sc, &carpif_list, sc_next) {
796 if (sc->sc_carpdev == NULL)
799 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
805 carp_send_ad_timeout(void *xsc)
811 carp_send_ad(struct carp_softc *sc)
813 struct ifnet *cifp = &sc->sc_if;
814 struct carp_header ch;
816 struct carp_header *ch_ptr;
818 int len, advbase, advskew;
820 if (!CARP_IS_RUNNING(cifp)) {
825 advbase = sc->sc_advbase;
826 if (!carp_suppress_preempt || sc->sc_advskew > 240)
827 advskew = sc->sc_advskew;
831 tv.tv_usec = advskew * 1000000 / 256;
834 ch.carp_version = CARP_VERSION;
835 ch.carp_type = CARP_ADVERTISEMENT;
836 ch.carp_vhid = sc->sc_vhid;
837 ch.carp_advbase = advbase;
838 ch.carp_advskew = advskew;
839 ch.carp_authlen = 7; /* XXX DEFINE */
840 ch.carp_pad1 = 0; /* must be zero */
844 if (sc->sc_ia != NULL) {
847 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
850 carpstats.carps_onomem++;
851 /* XXX maybe less ? */
852 if (advbase != 255 || advskew != 255)
853 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
854 carp_send_ad_timeout, sc);
857 len = sizeof(*ip) + sizeof(ch);
858 m->m_pkthdr.len = len;
859 m->m_pkthdr.rcvif = NULL;
861 MH_ALIGN(m, m->m_len);
862 m->m_flags |= M_MCAST;
863 ip = mtod(m, struct ip *);
864 ip->ip_v = IPVERSION;
865 ip->ip_hl = sizeof(*ip) >> 2;
866 ip->ip_tos = IPTOS_LOWDELAY;
868 ip->ip_id = ip_newid();
870 ip->ip_ttl = CARP_DFLTTL;
871 ip->ip_p = IPPROTO_CARP;
873 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
874 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
876 ch_ptr = (struct carp_header *)(&ip[1]);
877 bcopy(&ch, ch_ptr, sizeof(ch));
878 if (carp_prepare_ad(m, sc, ch_ptr))
880 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
882 getmicrotime(&cifp->if_lastchange);
884 cifp->if_obytes += len;
885 carpstats.carps_opackets++;
887 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
889 if (sc->sc_sendad_errors < INT_MAX)
890 sc->sc_sendad_errors++;
891 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
892 carp_suppress_preempt++;
893 if (carp_suppress_preempt == 1) {
897 sc->sc_sendad_success = 0;
899 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
900 if (++sc->sc_sendad_success >=
901 CARP_SENDAD_MIN_SUCCESS) {
902 carp_suppress_preempt--;
903 sc->sc_sendad_errors = 0;
906 sc->sc_sendad_errors = 0;
915 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
918 carpstats.carps_onomem++;
919 /* XXX maybe less ? */
920 if (advbase != 255 || advskew != 255)
921 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
922 carp_send_ad_timeout, sc);
925 len = sizeof(*ip6) + sizeof(ch);
926 m->m_pkthdr.len = len;
927 m->m_pkthdr.rcvif = NULL;
929 MH_ALIGN(m, m->m_len);
930 m->m_flags |= M_MCAST;
931 ip6 = mtod(m, struct ip6_hdr *);
932 bzero(ip6, sizeof(*ip6));
933 ip6->ip6_vfc |= IPV6_VERSION;
934 ip6->ip6_hlim = CARP_DFLTTL;
935 ip6->ip6_nxt = IPPROTO_CARP;
936 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
937 sizeof(struct in6_addr));
938 /* set the multicast destination */
940 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
941 ip6->ip6_dst.s6_addr8[15] = 0x12;
942 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
945 CARP_LOG("%s: in6_setscope failed\n", __func__);
949 ch_ptr = (struct carp_header *)(&ip6[1]);
950 bcopy(&ch, ch_ptr, sizeof(ch));
951 if (carp_prepare_ad(m, sc, ch_ptr))
953 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
955 getmicrotime(&cifp->if_lastchange);
957 cifp->if_obytes += len;
958 carpstats.carps_opackets6++;
960 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
962 if (sc->sc_sendad_errors < INT_MAX)
963 sc->sc_sendad_errors++;
964 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
965 carp_suppress_preempt++;
966 if (carp_suppress_preempt == 1) {
970 sc->sc_sendad_success = 0;
972 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
973 if (++sc->sc_sendad_success >=
974 CARP_SENDAD_MIN_SUCCESS) {
975 carp_suppress_preempt--;
976 sc->sc_sendad_errors = 0;
979 sc->sc_sendad_errors = 0;
985 if (advbase != 255 || advskew != 255)
986 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
987 carp_send_ad_timeout, sc);
991 * Broadcast a gratuitous ARP request containing
992 * the virtual router MAC address for each IP address
993 * associated with the virtual router.
996 carp_send_arp(struct carp_softc *sc)
998 const struct carp_vhaddr *vha;
1000 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1001 if (vha->vha_iaback == NULL)
1004 arp_iainit(sc->sc_carpdev, &vha->vha_ia->ia_addr.sin_addr,
1005 IF_LLADDR(&sc->sc_if));
1011 carp_send_na(struct carp_softc *sc)
1013 struct ifaddr_container *ifac;
1014 struct in6_addr *in6;
1015 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1017 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1018 struct ifaddr *ifa = ifac->ifa;
1020 if (ifa->ifa_addr->sa_family != AF_INET6)
1023 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1024 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1025 ND_NA_FLAG_OVERRIDE, 1, NULL);
1026 DELAY(1000); /* XXX */
1031 static __inline const struct carp_vhaddr *
1032 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1034 struct carp_vhaddr *vha;
1036 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1037 if (vha->vha_iaback == NULL)
1040 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1047 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1048 const struct in_addr *isaddr, uint8_t **enaddr)
1050 const struct carp_softc *vh;
1051 int index, count = 0;
1054 * XXX proof of concept implementation.
1055 * We use the source ip to decide which virtual host should
1056 * handle the request. If we're master of that virtual host,
1057 * then we respond, otherwise, just drop the arp packet on
1061 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1062 if (!CARP_IS_RUNNING(&vh->sc_if))
1065 if (carp_find_addr(vh, itaddr) != NULL)
1071 /* this should be a hash, like pf_hash() */
1072 index = ntohl(isaddr->s_addr) % count;
1075 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1076 if (!CARP_IS_RUNNING(&vh->sc_if))
1079 if (carp_find_addr(vh, itaddr) == NULL)
1082 if (count == index) {
1083 if (vh->sc_state == MASTER) {
1084 *enaddr = IF_LLADDR(&vh->sc_if);
1096 carp_iamatch(const void *v, const struct in_addr *itaddr,
1097 const struct in_addr *isaddr, uint8_t **enaddr)
1099 const struct carp_if *cif = v;
1100 const struct carp_softc *vh;
1102 if (carp_opts[CARPCTL_ARPBALANCE])
1103 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1105 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1106 if (!CARP_IS_RUNNING(&vh->sc_if) || vh->sc_state != MASTER)
1109 if (carp_find_addr(vh, itaddr) != NULL) {
1110 *enaddr = IF_LLADDR(&vh->sc_if);
1119 carp_iamatch6(void *v, struct in6_addr *taddr)
1121 struct carp_if *cif = v;
1122 struct carp_softc *vh;
1124 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1125 struct ifaddr_container *ifac;
1127 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1129 struct ifaddr *ifa = ifac->ifa;
1131 if (IN6_ARE_ADDR_EQUAL(taddr,
1132 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1133 CARP_IS_RUNNING(&vh->sc_if) &&
1134 vh->sc_state == MASTER) {
1143 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1146 struct carp_if *cif = v;
1147 struct carp_softc *sc;
1149 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1150 struct ifaddr_container *ifac;
1152 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1154 struct ifaddr *ifa = ifac->ifa;
1156 if (IN6_ARE_ADDR_EQUAL(taddr,
1157 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1158 CARP_IS_RUNNING(&sc->sc_if)) {
1159 struct ifnet *ifp = &sc->sc_if;
1161 mtag = m_tag_get(PACKET_TAG_CARP,
1162 sizeof(struct ifnet *), MB_DONTWAIT);
1164 /* better a bit than nothing */
1165 return (IF_LLADDR(ifp));
1167 bcopy(&ifp, (caddr_t)(mtag + 1),
1168 sizeof(struct ifnet *));
1169 m_tag_prepend(m, mtag);
1171 return (IF_LLADDR(ifp));
1180 carp_forus(const void *v, const void *dhost)
1182 const struct carp_if *cif = v;
1183 const struct carp_softc *vh;
1184 const uint8_t *ena = dhost;
1186 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1189 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1190 const struct ifnet *cifp = &vh->sc_if;
1192 if (CARP_IS_RUNNING(cifp) && vh->sc_state == MASTER &&
1193 !bcmp(dhost, IF_LLADDR(cifp), ETHER_ADDR_LEN))
1200 carp_master_down_timeout(void *xsc)
1202 struct carp_softc *sc = xsc;
1204 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1205 sc->sc_if.if_xname);
1206 carp_master_down(sc);
1210 carp_master_down(struct carp_softc *sc)
1212 switch (sc->sc_state) {
1214 kprintf("%s: master_down event in INIT state\n",
1215 sc->sc_if.if_xname);
1222 carp_set_state(sc, MASTER);
1229 carp_setroute(sc, RTM_ADD);
1235 * When in backup state, af indicates whether to reset the master down timer
1236 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1239 carp_setrun(struct carp_softc *sc, sa_family_t af)
1241 struct ifnet *cifp = &sc->sc_if;
1244 if (sc->sc_carpdev == NULL) {
1245 carp_set_state(sc, INIT);
1249 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1250 (sc->sc_naddrs || sc->sc_naddrs6)) {
1253 carp_setroute(sc, RTM_DELETE);
1257 switch (sc->sc_state) {
1259 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1265 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1267 carp_set_state(sc, MASTER);
1268 carp_setroute(sc, RTM_ADD);
1270 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1271 carp_set_state(sc, BACKUP);
1272 carp_setroute(sc, RTM_DELETE);
1278 callout_stop(&sc->sc_ad_tmo);
1279 tv.tv_sec = 3 * sc->sc_advbase;
1280 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1284 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1285 carp_master_down_timeout, sc);
1290 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1291 carp_master_down_timeout, sc);
1296 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1297 carp_master_down_timeout, sc);
1299 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1300 carp_master_down_timeout, sc);
1306 tv.tv_sec = sc->sc_advbase;
1307 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1308 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1309 carp_send_ad_timeout, sc);
1315 carp_multicast_cleanup(struct carp_softc *sc)
1317 struct ip_moptions *imo = &sc->sc_imo;
1319 if (imo->imo_num_memberships == 0)
1321 KKASSERT(imo->imo_num_memberships == 1);
1323 in_delmulti(imo->imo_membership[0]);
1324 imo->imo_membership[0] = NULL;
1325 imo->imo_num_memberships = 0;
1326 imo->imo_multicast_ifp = NULL;
1331 carp_multicast6_cleanup(struct carp_softc *sc)
1333 struct ip6_moptions *im6o = &sc->sc_im6o;
1335 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1336 struct in6_multi_mship *imm =
1337 LIST_FIRST(&im6o->im6o_memberships);
1339 LIST_REMOVE(imm, i6mm_chain);
1340 in6_leavegroup(imm);
1342 im6o->im6o_multicast_ifp = NULL;
1347 carp_get_vhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1349 const struct carp_vhaddr *vha;
1350 struct ifcarpvhaddr *carpa, *carpa0;
1351 int count, len, error;
1354 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1357 if (ifd->ifd_len == 0) {
1358 ifd->ifd_len = count * sizeof(*carpa);
1360 } else if (count == 0 || ifd->ifd_len < sizeof(*carpa)) {
1364 len = min(ifd->ifd_len, sizeof(*carpa) * count);
1365 KKASSERT(len >= sizeof(*carpa));
1367 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1372 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1373 if (len < sizeof(*carpa))
1376 carpa->carpa_flags = vha->vha_flags;
1377 carpa->carpa_addr.sin_family = AF_INET;
1378 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1380 carpa->carpa_baddr.sin_family = AF_INET;
1381 if (vha->vha_iaback == NULL) {
1382 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1384 carpa->carpa_baddr.sin_addr =
1385 vha->vha_iaback->ia_addr.sin_addr;
1390 len -= sizeof(*carpa);
1392 ifd->ifd_len = sizeof(*carpa) * count;
1393 KKASSERT(ifd->ifd_len > 0);
1395 error = copyout(carpa0, ifd->ifd_data, ifd->ifd_len);
1396 kfree(carpa0, M_TEMP);
1401 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
1404 struct in_ifaddr *ia_if;
1405 struct in_ifaddr_container *iac;
1406 const struct sockaddr_in *sin;
1410 KKASSERT(vha->vha_ia != NULL);
1412 sin = &vha->vha_ia->ia_addr;
1413 iaddr = ntohl(sin->sin_addr.s_addr);
1417 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1418 struct in_ifaddr *ia = iac->ia;
1420 if ((ia->ia_flags & IFA_ROUTE) == 0)
1423 if (ia->ia_ifp->if_type == IFT_CARP)
1426 /* and, yeah, we need a multicast-capable iface too */
1427 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1430 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1431 if (sin->sin_addr.s_addr ==
1432 ia->ia_addr.sin_addr.s_addr)
1436 else if (sc->sc_carpdev != NULL &&
1437 sc->sc_carpdev == ia->ia_ifp)
1442 carp_deactivate_vhaddr(sc, vha);
1446 ifp = ia_if->ia_ifp;
1448 /* XXX Don't allow parent iface to be changed */
1449 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
1452 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
1456 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1458 struct carp_vhaddr *vha_new;
1459 struct in_ifaddr *carp_ia;
1461 struct carp_vhaddr *vha;
1464 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1465 carp_ia = ifatoia(carp_ifa);
1468 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1469 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
1472 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
1473 vha_new->vha_ia = carp_ia;
1474 carp_insert_vhaddr(sc, vha_new);
1476 if (carp_config_vhaddr(sc, vha_new) != 0) {
1478 * If the above configuration fails, it may only mean
1479 * that the new address is problematic. However, the
1480 * carp(4) interface may already have several working
1481 * addresses. Since the expected behaviour of
1482 * SIOC[AS]IFADDR is to put the NIC into working state,
1483 * we try starting the state machine manually here with
1484 * the hope that the carp(4)'s previously working
1485 * addresses still could be brought up.
1487 carp_hmac_prepare(sc);
1488 carp_set_state(sc, INIT);
1494 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1496 struct carp_vhaddr *vha;
1497 struct in_ifaddr *carp_ia;
1499 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1500 carp_ia = ifatoia(carp_ifa);
1502 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1503 KKASSERT(vha->vha_ia != NULL);
1504 if (vha->vha_ia == carp_ia)
1507 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1510 * Remove the vhaddr from the list before deactivating
1511 * the vhaddr, so that the HMAC could be correctly
1512 * updated in carp_deactivate_vhaddr()
1514 carp_remove_vhaddr(sc, vha);
1516 carp_deactivate_vhaddr(sc, vha);
1521 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1523 struct carp_vhaddr *vha;
1524 struct in_ifaddr *carp_ia;
1526 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1527 carp_ia = ifatoia(carp_ifa);
1529 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1530 KKASSERT(vha->vha_ia != NULL);
1531 if (vha->vha_ia == carp_ia)
1534 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1536 /* Remove then reinsert, to keep the vhaddr list sorted */
1537 carp_remove_vhaddr(sc, vha);
1538 carp_insert_vhaddr(sc, vha);
1540 if (carp_config_vhaddr(sc, vha) != 0) {
1541 /* See the comment in carp_add_addr() */
1542 carp_hmac_prepare(sc);
1543 carp_set_state(sc, INIT);
1550 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1553 struct carp_if *cif;
1554 struct in6_ifaddr *ia, *ia_if;
1555 struct ip6_moptions *im6o = &sc->sc_im6o;
1556 struct in6_multi_mship *imm;
1557 struct in6_addr in6;
1560 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1565 /* we have to do it by hands to check we won't match on us */
1566 ia_if = NULL; own = 0;
1567 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1570 for (i = 0; i < 4; i++) {
1571 if ((sin6->sin6_addr.s6_addr32[i] &
1572 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1573 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1574 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1577 /* and, yeah, we need a multicast-capable iface too */
1578 if (ia->ia_ifp != &sc->sc_if &&
1579 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1583 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1584 &ia->ia_addr.sin6_addr))
1590 return (EADDRNOTAVAIL);
1594 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1595 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1596 return (EADDRNOTAVAIL);
1598 if (!sc->sc_naddrs6) {
1599 im6o->im6o_multicast_ifp = ifp;
1601 /* join CARP multicast address */
1602 bzero(&in6, sizeof(in6));
1603 in6.s6_addr16[0] = htons(0xff02);
1604 in6.s6_addr8[15] = 0x12;
1605 if (in6_setscope(&in6, ifp, NULL) != 0)
1607 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1609 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1611 /* join solicited multicast address */
1612 bzero(&in6, sizeof(in6));
1613 in6.s6_addr16[0] = htons(0xff02);
1614 in6.s6_addr32[1] = 0;
1615 in6.s6_addr32[2] = htonl(1);
1616 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1617 in6.s6_addr8[12] = 0xff;
1618 if (in6_setscope(&in6, ifp, NULL) != 0)
1620 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1622 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1625 if (!ifp->if_carp) {
1626 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
1628 if ((error = ifpromisc(ifp, 1))) {
1633 TAILQ_INIT(&cif->vhif_vrs);
1636 struct carp_softc *vr;
1639 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1640 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1647 sc->sc_carpdev = ifp;
1649 { /* XXX prevent endless loop if already in queue */
1650 struct carp_softc *vr, *after = NULL;
1654 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1657 if (vr->sc_vhid < sc->sc_vhid)
1662 /* We're trying to keep things in order */
1664 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1666 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1679 /* clean up multicast memberships */
1680 if (!sc->sc_naddrs6) {
1681 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1682 imm = LIST_FIRST(&im6o->im6o_memberships);
1683 LIST_REMOVE(imm, i6mm_chain);
1684 in6_leavegroup(imm);
1691 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1695 if (!--sc->sc_naddrs6) {
1696 struct carp_if *cif = sc->sc_carpdev->if_carp;
1697 struct ip6_moptions *im6o = &sc->sc_im6o;
1699 callout_stop(&sc->sc_ad_tmo);
1701 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1702 struct in6_multi_mship *imm =
1703 LIST_FIRST(&im6o->im6o_memberships);
1705 LIST_REMOVE(imm, i6mm_chain);
1706 in6_leavegroup(imm);
1708 im6o->im6o_multicast_ifp = NULL;
1709 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1710 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
1711 sc->sc_carpdev->if_carp = NULL;
1712 kfree(cif, M_IFADDR);
1720 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
1722 struct carp_softc *sc = ifp->if_softc, *vr;
1723 struct carpreq carpr;
1726 struct ifaliasreq *ifra;
1728 char devname[IFNAMSIZ];
1731 ifa = (struct ifaddr *)addr;
1732 ifra = (struct ifaliasreq *)addr;
1733 ifr = (struct ifreq *)addr;
1734 ifd = (struct ifdrv *)addr;
1738 switch (ifa->ifa_addr->sa_family) {
1741 ifp->if_flags |= IFF_UP | IFF_RUNNING;
1746 ifp->if_flags |= IFF_UP | IFF_RUNNING;
1747 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1751 error = EAFNOSUPPORT;
1757 switch (ifa->ifa_addr->sa_family) {
1760 panic("SIOCAIFADDR should never be seen\n");
1764 ifp->if_flags |= IFF_UP | IFF_RUNNING;
1765 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1769 error = EAFNOSUPPORT;
1775 switch (ifa->ifa_addr->sa_family) {
1778 panic("SIOCDIFADDR should never be seen\n");
1782 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1786 error = EAFNOSUPPORT;
1792 if (ifp->if_flags & IFF_UP) {
1793 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1794 ifp->if_flags |= IFF_RUNNING;
1795 carp_set_state(sc, INIT);
1798 } else if (ifp->if_flags & IFF_RUNNING) {
1804 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1807 error = copyin(ifr->ifr_data, &carpr, sizeof(carpr));
1812 if ((ifp->if_flags & IFF_RUNNING) &&
1813 sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1814 switch (carpr.carpr_state) {
1816 callout_stop(&sc->sc_ad_tmo);
1817 carp_set_state(sc, BACKUP);
1819 carp_setroute(sc, RTM_DELETE);
1823 carp_master_down(sc);
1830 if (carpr.carpr_vhid > 0) {
1831 if (carpr.carpr_vhid > 255) {
1835 if (sc->sc_carpdev) {
1836 struct carp_if *cif = sc->sc_carpdev->if_carp;
1838 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1840 vr->sc_vhid == carpr.carpr_vhid)
1844 sc->sc_vhid = carpr.carpr_vhid;
1845 IF_LLADDR(ifp)[0] = 0;
1846 IF_LLADDR(ifp)[1] = 0;
1847 IF_LLADDR(ifp)[2] = 0x5e;
1848 IF_LLADDR(ifp)[3] = 0;
1849 IF_LLADDR(ifp)[4] = 1;
1850 IF_LLADDR(ifp)[5] = sc->sc_vhid;
1853 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1854 if (carpr.carpr_advskew >= 255) {
1858 if (carpr.carpr_advbase > 255) {
1862 sc->sc_advbase = carpr.carpr_advbase;
1863 sc->sc_advskew = carpr.carpr_advskew;
1866 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1876 bzero(&carpr, sizeof(carpr));
1877 carpr.carpr_state = sc->sc_state;
1878 carpr.carpr_vhid = sc->sc_vhid;
1879 carpr.carpr_advbase = sc->sc_advbase;
1880 carpr.carpr_advskew = sc->sc_advskew;
1881 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1883 bcopy(sc->sc_key, carpr.carpr_key,
1884 sizeof(carpr.carpr_key));
1887 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1891 switch (ifd->ifd_cmd) {
1893 if (ifd->ifd_len != sizeof(devname))
1907 switch (ifd->ifd_cmd) {
1909 error = carp_get_vhaddr(sc, ifd);
1913 bzero(devname, sizeof(devname));
1914 if (sc->sc_carpdev != NULL) {
1915 strlcpy(devname, sc->sc_carpdev->if_xname,
1918 error = copyout(devname, ifd->ifd_data,
1928 carp_hmac_prepare(sc);
1933 * XXX: this is looutput. We should eventually use it from there.
1936 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1941 M_ASSERTPKTHDR(m); /* check if we have the packet header */
1943 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1945 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1946 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1950 ifp->if_obytes += m->m_pkthdr.len;
1952 /* BPF writes need to be handled specially. */
1953 if (dst->sa_family == AF_UNSPEC) {
1954 bcopy(dst->sa_data, &af, sizeof(af));
1955 dst->sa_family = af;
1959 switch (dst->sa_family) {
1968 return (EAFNOSUPPORT);
1971 return (if_simloop(ifp, m, dst->sa_family, 0));
1975 * Start output on carp interface. This function should never be called.
1978 carp_start(struct ifnet *ifp)
1981 kprintf("%s: start called\n", ifp->if_xname);
1986 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1990 struct carp_softc *sc;
1991 struct ifnet *carp_ifp;
1992 struct ether_header *eh;
1997 switch (sa->sa_family) {
2010 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2014 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2015 sc = carp_ifp->if_softc;
2017 /* Set the source MAC address to Virtual Router MAC Address */
2018 switch (ifp->if_type) {
2021 eh = mtod(m, struct ether_header *);
2022 eh->ether_shost[0] = 0;
2023 eh->ether_shost[1] = 0;
2024 eh->ether_shost[2] = 0x5e;
2025 eh->ether_shost[3] = 0;
2026 eh->ether_shost[4] = 1;
2027 eh->ether_shost[5] = sc->sc_vhid;
2031 if_printf(ifp, "carp is not supported for this "
2032 "interface type\n");
2033 return (EOPNOTSUPP);
2039 carp_set_state(struct carp_softc *sc, int state)
2041 struct ifnet *cifp = &sc->sc_if;
2043 if (sc->sc_state == state)
2045 sc->sc_state = state;
2047 switch (sc->sc_state) {
2049 cifp->if_link_state = LINK_STATE_DOWN;
2053 cifp->if_link_state = LINK_STATE_UP;
2057 cifp->if_link_state = LINK_STATE_UNKNOWN;
2064 carp_group_demote_adj(struct ifnet *ifp, int adj)
2066 struct ifg_list *ifgl;
2069 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2070 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2072 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2079 if (adj > 0 && *dm == 1)
2081 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2082 ifgl->ifgl_group->ifg_group, *dm);
2087 carp_carpdev_state(void *v)
2089 struct carp_if *cif = v;
2090 struct carp_softc *sc;
2092 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2097 carp_sc_state(struct carp_softc *sc)
2099 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2100 callout_stop(&sc->sc_ad_tmo);
2101 callout_stop(&sc->sc_md_tmo);
2102 callout_stop(&sc->sc_md6_tmo);
2103 carp_set_state(sc, INIT);
2105 if (!sc->sc_suppress) {
2106 carp_suppress_preempt++;
2107 if (carp_suppress_preempt == 1)
2110 sc->sc_suppress = 1;
2112 carp_set_state(sc, INIT);
2114 if (sc->sc_suppress)
2115 carp_suppress_preempt--;
2116 sc->sc_suppress = 0;
2121 carp_stop(struct carp_softc *sc, int detach)
2123 sc->sc_if.if_flags &= ~IFF_RUNNING;
2125 callout_stop(&sc->sc_ad_tmo);
2126 callout_stop(&sc->sc_md_tmo);
2127 callout_stop(&sc->sc_md6_tmo);
2129 if (!detach && sc->sc_state == MASTER)
2132 if (sc->sc_suppress)
2133 carp_suppress_preempt--;
2134 sc->sc_suppress = 0;
2136 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2137 carp_suppress_preempt--;
2138 sc->sc_sendad_errors = 0;
2139 sc->sc_sendad_success = 0;
2141 carp_set_state(sc, INIT);
2146 carp_reset(struct carp_softc *sc, int detach)
2148 struct ifnet *cifp = &sc->sc_if;
2150 carp_stop(sc, detach);
2151 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2152 cifp->if_flags |= IFF_RUNNING;
2156 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2157 struct ifnet *ifp, const struct in_ifaddr *ia_if, int own)
2159 struct ip_moptions *imo = &sc->sc_imo;
2160 struct carp_if *cif;
2161 struct carp_softc *vr, *after = NULL;
2167 KKASSERT(vha->vha_ia != NULL);
2169 KASSERT(ia_if != NULL, ("NULL backing address\n"));
2170 KASSERT(vha->vha_iaback == NULL, ("%p is already activated\n", vha));
2171 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2172 ("inactive vhaddr %p is the address owner\n", vha));
2174 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2175 ("%s is already on %s\n", sc->sc_if.if_xname,
2176 sc->sc_carpdev->if_xname));
2178 KASSERT(imo->imo_multicast_ifp == NULL ||
2179 imo->imo_multicast_ifp == ifp,
2180 ("%s didn't leave mcast group on %s\n",
2181 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2183 if (imo->imo_num_memberships == 0) {
2184 struct in_addr addr;
2186 addr.s_addr = htonl(INADDR_CARP_GROUP);
2187 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
2189 imo->imo_num_memberships++;
2190 imo->imo_multicast_ifp = ifp;
2191 imo->imo_multicast_ttl = CARP_DFLTTL;
2192 imo->imo_multicast_loop = 0;
2195 if (!ifp->if_carp) {
2196 KASSERT(sc->sc_carpdev == NULL,
2197 ("%s is already on %s\n", sc->sc_if.if_xname,
2198 sc->sc_carpdev->if_xname));
2200 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2202 error = ifpromisc(ifp, 1);
2208 TAILQ_INIT(&cif->vhif_vrs);
2212 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2213 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
2221 if (sc->sc_carpdev != NULL)
2227 sc->sc_carpdev = ifp;
2231 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2234 if (vr->sc_vhid < sc->sc_vhid)
2239 if (assert_onlist) {
2240 KASSERT(onlist, ("%s is not on %s carp list\n",
2241 sc->sc_if.if_xname, ifp->if_xname));
2243 KASSERT(!onlist, ("%s is already on %s carp list\n",
2244 sc->sc_if.if_xname, ifp->if_xname));
2249 /* We're trying to keep things in order */
2251 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2253 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2256 vha->vha_iaback = ia_if;
2260 vha->vha_flags |= CARP_VHAF_OWNER;
2262 /* XXX save user configured advskew? */
2266 carp_hmac_prepare(sc);
2267 carp_set_state(sc, INIT);
2271 carp_multicast_cleanup(sc);
2276 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
2278 KKASSERT(vha->vha_ia != NULL);
2280 carp_hmac_prepare(sc);
2282 if (vha->vha_iaback == NULL) {
2283 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2284 ("inactive vhaddr %p is the address owner\n", vha));
2288 vha->vha_flags &= ~CARP_VHAF_OWNER;
2290 KKASSERT(sc->sc_naddrs > 0);
2291 vha->vha_iaback = NULL;
2293 if (!sc->sc_naddrs) {
2294 if (sc->sc_naddrs6) {
2295 carp_multicast_cleanup(sc);
2304 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2306 struct carp_vhaddr *vha;
2307 struct in_ifaddr *ia_if;
2309 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2310 ia_if = ifatoia(ifa_if);
2312 if ((ia_if->ia_flags & IFA_ROUTE) == 0)
2316 * Test each inactive vhaddr against the newly added address.
2317 * If the newly added address could be the backing address,
2318 * then activate the matching vhaddr.
2320 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2321 const struct in_ifaddr *ia;
2325 if (vha->vha_iaback != NULL)
2329 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr);
2331 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet)
2335 if (ia->ia_addr.sin_addr.s_addr ==
2336 ia_if->ia_addr.sin_addr.s_addr)
2339 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2344 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
2345 struct ifaddr *ifa_if)
2347 struct carp_vhaddr *vha;
2348 struct in_ifaddr *ia_if;
2350 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2351 ia_if = ifatoia(ifa_if);
2354 * Ad src address is deleted; set it to NULL.
2355 * Following loop will try pick up a new ad src address
2356 * if one of the vhaddr could retain its backing address.
2358 if (sc->sc_ia == ia_if)
2362 * Test each active vhaddr against the deleted address.
2363 * If the deleted address is vhaddr address's backing
2364 * address, then deactivate the vhaddr.
2366 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2367 if (vha->vha_iaback == NULL)
2370 if (vha->vha_iaback == ia_if)
2371 carp_deactivate_vhaddr(sc, vha);
2372 else if (sc->sc_ia == NULL)
2373 sc->sc_ia = vha->vha_iaback;
2378 carp_update_addrs(struct carp_softc *sc)
2380 struct carp_vhaddr *vha;
2382 KKASSERT(sc->sc_carpdev == NULL);
2384 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2385 carp_config_vhaddr(sc, vha);
2389 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
2390 enum ifaddr_event event, struct ifaddr *ifa)
2392 struct carp_softc *sc;
2394 if (ifa->ifa_addr->sa_family != AF_INET)
2397 if (ifp->if_type == IFT_CARP) {
2399 * Address is changed on carp(4) interface
2402 case IFADDR_EVENT_ADD:
2403 carp_add_addr(ifp->if_softc, ifa);
2406 case IFADDR_EVENT_CHANGE:
2407 carp_config_addr(ifp->if_softc, ifa);
2410 case IFADDR_EVENT_DELETE:
2411 carp_del_addr(ifp->if_softc, ifa);
2418 * Address is changed on non-carp(4) interface
2420 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2424 LIST_FOREACH(sc, &carpif_list, sc_next) {
2425 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
2426 /* Not the parent iface; skip */
2431 case IFADDR_EVENT_ADD:
2432 carp_link_addrs(sc, ifp, ifa);
2435 case IFADDR_EVENT_DELETE:
2436 if (sc->sc_carpdev != NULL) {
2437 carp_unlink_addrs(sc, ifp, ifa);
2438 if (sc->sc_carpdev == NULL)
2439 carp_update_addrs(sc);
2442 * The carp(4) interface didn't have a
2443 * parent iface, so it is not possible
2444 * that it will contain any address to
2450 case IFADDR_EVENT_CHANGE:
2451 if (sc->sc_carpdev == NULL) {
2453 * The carp(4) interface didn't have a
2454 * parent iface, so it is not possible
2455 * that it will contain any address to
2458 carp_link_addrs(sc, ifp, ifa);
2461 * First try breaking tie with the old
2462 * address. Then see whether we could
2463 * link certain vhaddr to the new address.
2464 * If that fails, i.e. carpdev is NULL,
2465 * we try a global update.
2467 * NOTE: The above order is critical.
2469 carp_unlink_addrs(sc, ifp, ifa);
2470 carp_link_addrs(sc, ifp, ifa);
2471 if (sc->sc_carpdev == NULL)
2472 carp_update_addrs(sc);
2481 carp_modevent(module_t mod, int type, void *data)
2485 LIST_INIT(&carpif_list);
2486 carp_ifdetach_event =
2487 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
2488 EVENTHANDLER_PRI_ANY);
2490 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
2491 EVENTHANDLER_PRI_ANY);
2492 if_clone_attach(&carp_cloner);
2496 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
2497 carp_ifdetach_event);
2498 EVENTHANDLER_DEREGISTER(ifaddr_event,
2500 if_clone_detach(&carp_cloner);
2509 static moduledata_t carp_mod = {
2514 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);