2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3 * Copyright (c) 2003 Ryan McBride. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/sys/netinet/ip_carp.c,v 1.48 2007/02/02 09:39:09 glebius Exp $
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/in_cksum.h>
38 #include <sys/limits.h>
39 #include <sys/malloc.h>
44 #include <sys/sockio.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
48 #include <sys/thread.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 struct lwkt_token carp_tok = LWKT_TOKEN_INITIALIZER(carp_token);
177 static void carp_hmac_prepare(struct carp_softc *);
178 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
180 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
182 static void carp_setroute(struct carp_softc *, int);
183 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
184 static int carp_clone_create(struct if_clone *, int, caddr_t);
185 static int carp_clone_destroy(struct ifnet *);
186 static void carp_detach(struct carp_softc *, int);
187 static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
188 struct carp_header *);
189 static void carp_send_ad_all(void);
190 static void carp_send_ad_timeout(void *);
191 static void carp_send_ad(struct carp_softc *);
192 static void carp_send_arp(struct carp_softc *);
193 static void carp_master_down_timeout(void *);
194 static void carp_master_down(struct carp_softc *);
195 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
196 static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
198 static void carp_start(struct ifnet *);
199 static void carp_setrun(struct carp_softc *, sa_family_t);
200 static void carp_set_state(struct carp_softc *, int);
202 static void carp_multicast_cleanup(struct carp_softc *);
203 static void carp_add_addr(struct carp_softc *, struct ifaddr *);
204 static void carp_del_addr(struct carp_softc *, struct ifaddr *);
205 static void carp_config_addr(struct carp_softc *, struct ifaddr *);
206 static void carp_link_addrs(struct carp_softc *, struct ifnet *,
208 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *,
211 static int carp_get_vhaddr(struct carp_softc *, struct ifdrv *);
212 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *);
213 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
214 struct ifnet *, const struct in_ifaddr *, int);
215 static void carp_deactivate_vhaddr(struct carp_softc *,
216 struct carp_vhaddr *);
218 static void carp_sc_state(struct carp_softc *);
220 static void carp_send_na(struct carp_softc *);
221 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
222 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
223 static void carp_multicast6_cleanup(struct carp_softc *);
225 static void carp_stop(struct carp_softc *, int);
226 static void carp_reset(struct carp_softc *, int);
228 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
230 static void carp_ifdetach(void *, struct ifnet *);
232 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
234 static LIST_HEAD(, carp_softc) carpif_list;
236 static struct if_clone carp_cloner =
237 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
240 static eventhandler_tag carp_ifdetach_event;
241 static eventhandler_tag carp_ifaddr_event;
244 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
246 struct carp_vhaddr *vha;
247 u_long new_addr, addr;
249 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
252 * Virtual address list is sorted; smaller one first
254 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
256 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
257 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
263 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
265 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
266 vha_new->vha_flags |= CARP_VHAF_ONLIST;
270 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
272 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
273 vha->vha_flags &= ~CARP_VHAF_ONLIST;
274 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
278 carp_hmac_prepare(struct carp_softc *sc)
280 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
281 uint8_t vhid = sc->sc_vhid & 0xff;
284 struct ifaddr_container *ifac;
288 struct carp_vhaddr *vha;
291 /* XXX: possible race here */
293 /* compute ipad from key */
294 bzero(sc->sc_pad, sizeof(sc->sc_pad));
295 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
296 for (i = 0; i < sizeof(sc->sc_pad); i++)
297 sc->sc_pad[i] ^= 0x36;
299 /* precompute first part of inner hash */
300 SHA1Init(&sc->sc_sha1);
301 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
302 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
303 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
304 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
306 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
307 SHA1Update(&sc->sc_sha1,
308 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
309 sizeof(struct in_addr));
313 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
314 struct ifaddr *ifa = ifac->ifa;
316 if (ifa->ifa_addr->sa_family == AF_INET6) {
317 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
318 in6_clearscope(&in6);
319 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
324 /* convert ipad to opad */
325 for (i = 0; i < sizeof(sc->sc_pad); i++)
326 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
330 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
331 unsigned char md[20])
335 /* fetch first half of inner hash */
336 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
338 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
339 SHA1Final(md, &sha1ctx);
343 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
344 SHA1Update(&sha1ctx, md, 20);
345 SHA1Final(md, &sha1ctx);
349 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
350 unsigned char md[20])
352 unsigned char md2[20];
354 carp_hmac_generate(sc, counter, md2);
355 return (bcmp(md, md2, sizeof(md2)));
359 carp_setroute(struct carp_softc *sc, int cmd)
362 struct ifaddr_container *ifac;
364 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
365 struct ifaddr *ifa = ifac->ifa;
367 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);
440 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active\n"));
447 carp_detach(struct carp_softc *sc, int detach)
451 carp_reset(sc, detach);
453 carp_multicast_cleanup(sc);
455 carp_multicast6_cleanup(sc);
458 if (!sc->sc_dead && detach) {
459 struct carp_vhaddr *vha;
461 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
462 carp_deactivate_vhaddr(sc, vha);
463 KKASSERT(sc->sc_naddrs == 0);
466 if (sc->sc_carpdev != NULL) {
467 cif = sc->sc_carpdev->if_carp;
468 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
469 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
470 ifpromisc(sc->sc_carpdev, 0);
471 sc->sc_carpdev->if_carp = NULL;
474 sc->sc_carpdev = NULL;
479 /* Detach an interface from the carp. */
481 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
483 struct carp_if *cif = ifp->if_carp;
484 struct carp_softc *sc;
488 while (ifp->if_carp &&
489 (sc = TAILQ_FIRST(&cif->vhif_vrs)) != NULL)
496 * process input packet.
497 * we have rearranged checks order compared to the rfc,
498 * but it seems more efficient this way or not possible otherwise.
501 carp_input(struct mbuf **mp, int *offp, int proto)
503 struct mbuf *m = *mp;
504 struct ip *ip = mtod(m, struct ip *);
505 struct carp_header *ch;
513 carpstats.carps_ipackets++;
515 if (!carp_opts[CARPCTL_ALLOW]) {
520 /* Check if received on a valid carp interface */
521 if (m->m_pkthdr.rcvif->if_carp == NULL) {
522 carpstats.carps_badif++;
523 CARP_LOG("carp_input: packet received on non-carp "
525 m->m_pkthdr.rcvif->if_xname);
530 /* Verify that the IP TTL is CARP_DFLTTL. */
531 if (ip->ip_ttl != CARP_DFLTTL) {
532 carpstats.carps_badttl++;
533 CARP_LOG("carp_input: received ttl %d != %d on %s\n",
534 ip->ip_ttl, CARP_DFLTTL,
535 m->m_pkthdr.rcvif->if_xname);
540 /* Minimal CARP packet size */
541 len = iphlen + sizeof(*ch);
544 * Verify that the received packet length is
545 * not less than the CARP header
547 if (m->m_pkthdr.len < len) {
548 carpstats.carps_badlen++;
549 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
550 m->m_pkthdr.rcvif->if_xname);
555 /* Make sure that CARP header is contiguous */
556 if (len > m->m_len) {
557 m = m_pullup(m, len);
559 carpstats.carps_hdrops++;
560 CARP_LOG("carp_input: m_pullup failed\n");
563 ip = mtod(m, struct ip *);
565 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
567 /* Verify the CARP checksum */
568 if (in_cksum_skip(m, len, iphlen)) {
569 carpstats.carps_badsum++;
570 CARP_LOG("carp_input: checksum failed on %s\n",
571 m->m_pkthdr.rcvif->if_xname);
575 carp_input_c(m, ch, AF_INET);
579 return(IPPROTO_DONE);
584 carp6_input(struct mbuf **mp, int *offp, int proto)
586 struct mbuf *m = *mp;
587 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
588 struct carp_header *ch;
593 carpstats.carps_ipackets6++;
595 if (!carp_opts[CARPCTL_ALLOW]) {
600 /* check if received on a valid carp interface */
601 if (m->m_pkthdr.rcvif->if_carp == NULL) {
602 carpstats.carps_badif++;
603 CARP_LOG("carp6_input: packet received on non-carp "
605 m->m_pkthdr.rcvif->if_xname);
610 /* verify that the IP TTL is 255 */
611 if (ip6->ip6_hlim != CARP_DFLTTL) {
612 carpstats.carps_badttl++;
613 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
615 m->m_pkthdr.rcvif->if_xname);
620 /* verify that we have a complete carp packet */
622 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
624 carpstats.carps_badlen++;
625 CARP_LOG("carp6_input: packet size %u too small\n", len);
629 /* verify the CARP checksum */
630 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
631 carpstats.carps_badsum++;
632 CARP_LOG("carp6_input: checksum failed, on %s\n",
633 m->m_pkthdr.rcvif->if_xname);
638 carp_input_c(m, ch, AF_INET6);
641 return (IPPROTO_DONE);
646 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
648 struct ifnet *ifp = m->m_pkthdr.rcvif;
650 struct carp_softc *sc;
651 uint64_t tmp_counter;
652 struct timeval sc_tv, ch_tv;
654 /* verify that the VHID is valid on the receiving interface */
655 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
656 if (sc->sc_vhid == ch->carp_vhid)
659 if (!sc || !CARP_IS_RUNNING(&sc->sc_if)) {
660 carpstats.carps_badvhid++;
666 getmicrotime(&cifp->if_lastchange);
668 cifp->if_ibytes += m->m_pkthdr.len;
671 struct ip *ip = mtod(m, struct ip *);
673 /* BPF wants net byte order */
674 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
675 ip->ip_off = htons(ip->ip_off);
676 bpf_mtap(cifp->if_bpf, m);
679 /* verify the CARP version. */
680 if (ch->carp_version != CARP_VERSION) {
681 carpstats.carps_badver++;
683 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
689 /* verify the hash */
690 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
691 carpstats.carps_badauth++;
693 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
698 tmp_counter = ntohl(ch->carp_counter[0]);
699 tmp_counter = tmp_counter<<32;
700 tmp_counter += ntohl(ch->carp_counter[1]);
702 /* XXX Replay protection goes here */
704 sc->sc_init_counter = 0;
705 sc->sc_counter = tmp_counter;
707 sc_tv.tv_sec = sc->sc_advbase;
708 if (carp_suppress_preempt && sc->sc_advskew < 240)
709 sc_tv.tv_usec = 240 * 1000000 / 256;
711 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
712 ch_tv.tv_sec = ch->carp_advbase;
713 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
715 switch (sc->sc_state) {
721 * If we receive an advertisement from a master who's going to
722 * be more frequent than us, go into BACKUP state.
724 if (timevalcmp(&sc_tv, &ch_tv, >) ||
725 timevalcmp(&sc_tv, &ch_tv, ==)) {
726 callout_stop(&sc->sc_ad_tmo);
727 CARP_DEBUG("%s: MASTER -> BACKUP "
728 "(more frequent advertisement received)\n",
730 carp_set_state(sc, BACKUP);
732 carp_setroute(sc, RTM_DELETE);
738 * If we're pre-empting masters who advertise slower than us,
739 * and this one claims to be slower, treat him as down.
741 if (carp_opts[CARPCTL_PREEMPT] &&
742 timevalcmp(&sc_tv, &ch_tv, <)) {
743 CARP_DEBUG("%s: BACKUP -> MASTER "
744 "(preempting a slower master)\n", cifp->if_xname);
745 carp_master_down(sc);
750 * If the master is going to advertise at such a low frequency
751 * that he's guaranteed to time out, we'd might as well just
752 * treat him as timed out now.
754 sc_tv.tv_sec = sc->sc_advbase * 3;
755 if (timevalcmp(&sc_tv, &ch_tv, <)) {
756 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
758 carp_master_down(sc);
763 * Otherwise, we reset the counter and wait for the next
773 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
775 struct ifnet *cifp = &sc->sc_if;
778 if (sc->sc_init_counter) {
779 /* this could also be seconds since unix epoch */
780 sc->sc_counter = karc4random();
781 sc->sc_counter = sc->sc_counter << 32;
782 sc->sc_counter += karc4random();
787 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
788 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
790 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
792 /* Tag packet for carp_output */
793 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), MB_DONTWAIT);
799 bcopy(&cifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
800 m_tag_prepend(m, mtag);
806 carp_send_ad_all(void)
808 struct carp_softc *sc;
810 LIST_FOREACH(sc, &carpif_list, sc_next) {
811 if (sc->sc_carpdev == NULL)
814 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
820 carp_send_ad_timeout(void *xsc)
826 carp_send_ad(struct carp_softc *sc)
828 struct ifnet *cifp = &sc->sc_if;
829 struct carp_header ch;
831 struct carp_header *ch_ptr;
833 int len, advbase, advskew;
835 if (!CARP_IS_RUNNING(cifp)) {
840 advbase = sc->sc_advbase;
841 if (!carp_suppress_preempt || sc->sc_advskew > 240)
842 advskew = sc->sc_advskew;
846 tv.tv_usec = advskew * 1000000 / 256;
849 ch.carp_version = CARP_VERSION;
850 ch.carp_type = CARP_ADVERTISEMENT;
851 ch.carp_vhid = sc->sc_vhid;
852 ch.carp_advbase = advbase;
853 ch.carp_advskew = advskew;
854 ch.carp_authlen = 7; /* XXX DEFINE */
855 ch.carp_pad1 = 0; /* must be zero */
859 if (sc->sc_ia != NULL) {
862 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
865 carpstats.carps_onomem++;
866 /* XXX maybe less ? */
867 if (advbase != 255 || advskew != 255)
868 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
869 carp_send_ad_timeout, sc);
872 len = sizeof(*ip) + sizeof(ch);
873 m->m_pkthdr.len = len;
874 m->m_pkthdr.rcvif = NULL;
876 MH_ALIGN(m, m->m_len);
877 m->m_flags |= M_MCAST;
878 ip = mtod(m, struct ip *);
879 ip->ip_v = IPVERSION;
880 ip->ip_hl = sizeof(*ip) >> 2;
881 ip->ip_tos = IPTOS_LOWDELAY;
883 ip->ip_id = ip_newid();
885 ip->ip_ttl = CARP_DFLTTL;
886 ip->ip_p = IPPROTO_CARP;
888 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
889 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
891 ch_ptr = (struct carp_header *)(&ip[1]);
892 bcopy(&ch, ch_ptr, sizeof(ch));
893 if (carp_prepare_ad(m, sc, ch_ptr))
895 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
897 getmicrotime(&cifp->if_lastchange);
899 cifp->if_obytes += len;
900 carpstats.carps_opackets++;
902 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
904 if (sc->sc_sendad_errors < INT_MAX)
905 sc->sc_sendad_errors++;
906 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
907 carp_suppress_preempt++;
908 if (carp_suppress_preempt == 1) {
912 sc->sc_sendad_success = 0;
914 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
915 if (++sc->sc_sendad_success >=
916 CARP_SENDAD_MIN_SUCCESS) {
917 carp_suppress_preempt--;
918 sc->sc_sendad_errors = 0;
921 sc->sc_sendad_errors = 0;
930 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
933 carpstats.carps_onomem++;
934 /* XXX maybe less ? */
935 if (advbase != 255 || advskew != 255)
936 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
937 carp_send_ad_timeout, sc);
940 len = sizeof(*ip6) + sizeof(ch);
941 m->m_pkthdr.len = len;
942 m->m_pkthdr.rcvif = NULL;
944 MH_ALIGN(m, m->m_len);
945 m->m_flags |= M_MCAST;
946 ip6 = mtod(m, struct ip6_hdr *);
947 bzero(ip6, sizeof(*ip6));
948 ip6->ip6_vfc |= IPV6_VERSION;
949 ip6->ip6_hlim = CARP_DFLTTL;
950 ip6->ip6_nxt = IPPROTO_CARP;
951 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
952 sizeof(struct in6_addr));
953 /* set the multicast destination */
955 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
956 ip6->ip6_dst.s6_addr8[15] = 0x12;
957 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
960 CARP_LOG("%s: in6_setscope failed\n", __func__);
964 ch_ptr = (struct carp_header *)(&ip6[1]);
965 bcopy(&ch, ch_ptr, sizeof(ch));
966 if (carp_prepare_ad(m, sc, ch_ptr))
968 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
970 getmicrotime(&cifp->if_lastchange);
972 cifp->if_obytes += len;
973 carpstats.carps_opackets6++;
975 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
977 if (sc->sc_sendad_errors < INT_MAX)
978 sc->sc_sendad_errors++;
979 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
980 carp_suppress_preempt++;
981 if (carp_suppress_preempt == 1) {
985 sc->sc_sendad_success = 0;
987 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
988 if (++sc->sc_sendad_success >=
989 CARP_SENDAD_MIN_SUCCESS) {
990 carp_suppress_preempt--;
991 sc->sc_sendad_errors = 0;
994 sc->sc_sendad_errors = 0;
1000 if (advbase != 255 || advskew != 255)
1001 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1002 carp_send_ad_timeout, sc);
1006 * Broadcast a gratuitous ARP request containing
1007 * the virtual router MAC address for each IP address
1008 * associated with the virtual router.
1011 carp_send_arp(struct carp_softc *sc)
1013 const struct carp_vhaddr *vha;
1015 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1016 if (vha->vha_iaback == NULL)
1019 arp_iainit(sc->sc_carpdev, &vha->vha_ia->ia_addr.sin_addr,
1020 IF_LLADDR(&sc->sc_if));
1026 carp_send_na(struct carp_softc *sc)
1028 struct ifaddr_container *ifac;
1029 struct in6_addr *in6;
1030 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1032 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1033 struct ifaddr *ifa = ifac->ifa;
1035 if (ifa->ifa_addr->sa_family != AF_INET6)
1038 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1039 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1040 ND_NA_FLAG_OVERRIDE, 1, NULL);
1041 DELAY(1000); /* XXX */
1046 static __inline const struct carp_vhaddr *
1047 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1049 struct carp_vhaddr *vha;
1051 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1052 if (vha->vha_iaback == NULL)
1055 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1062 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1063 const struct in_addr *isaddr, uint8_t **enaddr)
1065 const struct carp_softc *vh;
1066 int index, count = 0;
1069 * XXX proof of concept implementation.
1070 * We use the source ip to decide which virtual host should
1071 * handle the request. If we're master of that virtual host,
1072 * then we respond, otherwise, just drop the arp packet on
1076 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1077 if (!CARP_IS_RUNNING(&vh->sc_if))
1080 if (carp_find_addr(vh, itaddr) != NULL)
1086 /* this should be a hash, like pf_hash() */
1087 index = ntohl(isaddr->s_addr) % count;
1090 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1091 if (!CARP_IS_RUNNING(&vh->sc_if))
1094 if (carp_find_addr(vh, itaddr) == NULL)
1097 if (count == index) {
1098 if (vh->sc_state == MASTER) {
1099 *enaddr = IF_LLADDR(&vh->sc_if);
1111 carp_iamatch(const void *v, const struct in_addr *itaddr,
1112 const struct in_addr *isaddr, uint8_t **enaddr)
1114 const struct carp_if *cif = v;
1115 const struct carp_softc *vh;
1117 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1119 if (carp_opts[CARPCTL_ARPBALANCE])
1120 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1122 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1123 if (!CARP_IS_RUNNING(&vh->sc_if) || vh->sc_state != MASTER)
1126 if (carp_find_addr(vh, itaddr) != NULL) {
1127 *enaddr = IF_LLADDR(&vh->sc_if);
1136 carp_iamatch6(void *v, struct in6_addr *taddr)
1138 struct carp_if *cif = v;
1139 struct carp_softc *vh;
1141 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1143 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1144 struct ifaddr_container *ifac;
1146 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1148 struct ifaddr *ifa = ifac->ifa;
1150 if (IN6_ARE_ADDR_EQUAL(taddr,
1151 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1152 CARP_IS_RUNNING(&vh->sc_if) &&
1153 vh->sc_state == MASTER) {
1162 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1165 struct carp_if *cif = v;
1166 struct carp_softc *sc;
1168 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1170 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1171 struct ifaddr_container *ifac;
1173 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1175 struct ifaddr *ifa = ifac->ifa;
1177 if (IN6_ARE_ADDR_EQUAL(taddr,
1178 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1179 CARP_IS_RUNNING(&sc->sc_if)) {
1180 struct ifnet *ifp = &sc->sc_if;
1182 mtag = m_tag_get(PACKET_TAG_CARP,
1183 sizeof(struct ifnet *), MB_DONTWAIT);
1185 /* better a bit than nothing */
1186 return (IF_LLADDR(ifp));
1188 bcopy(&ifp, (caddr_t)(mtag + 1),
1189 sizeof(struct ifnet *));
1190 m_tag_prepend(m, mtag);
1192 return (IF_LLADDR(ifp));
1201 carp_forus(const void *v, const void *dhost)
1203 const struct carp_if *cif = v;
1204 const struct carp_softc *vh;
1205 const uint8_t *ena = dhost;
1207 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1209 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1212 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1213 const struct ifnet *cifp = &vh->sc_if;
1215 if (CARP_IS_RUNNING(cifp) && vh->sc_state == MASTER &&
1216 !bcmp(dhost, IF_LLADDR(cifp), ETHER_ADDR_LEN))
1223 carp_master_down_timeout(void *xsc)
1225 struct carp_softc *sc = xsc;
1227 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1228 sc->sc_if.if_xname);
1229 carp_master_down(sc);
1233 carp_master_down(struct carp_softc *sc)
1235 switch (sc->sc_state) {
1237 kprintf("%s: master_down event in INIT state\n",
1238 sc->sc_if.if_xname);
1245 carp_set_state(sc, MASTER);
1252 carp_setroute(sc, RTM_ADD);
1258 * When in backup state, af indicates whether to reset the master down timer
1259 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1262 carp_setrun(struct carp_softc *sc, sa_family_t af)
1264 struct ifnet *cifp = &sc->sc_if;
1267 if (sc->sc_carpdev == NULL) {
1268 carp_set_state(sc, INIT);
1272 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1273 (sc->sc_naddrs || sc->sc_naddrs6)) {
1276 carp_setroute(sc, RTM_DELETE);
1280 switch (sc->sc_state) {
1282 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1288 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1290 carp_set_state(sc, MASTER);
1291 carp_setroute(sc, RTM_ADD);
1293 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1294 carp_set_state(sc, BACKUP);
1295 carp_setroute(sc, RTM_DELETE);
1301 callout_stop(&sc->sc_ad_tmo);
1302 tv.tv_sec = 3 * sc->sc_advbase;
1303 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1307 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1308 carp_master_down_timeout, sc);
1313 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1314 carp_master_down_timeout, sc);
1319 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1320 carp_master_down_timeout, sc);
1322 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1323 carp_master_down_timeout, sc);
1329 tv.tv_sec = sc->sc_advbase;
1330 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1331 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1332 carp_send_ad_timeout, sc);
1338 carp_multicast_cleanup(struct carp_softc *sc)
1340 struct ip_moptions *imo = &sc->sc_imo;
1342 if (imo->imo_num_memberships == 0)
1344 KKASSERT(imo->imo_num_memberships == 1);
1346 in_delmulti(imo->imo_membership[0]);
1347 imo->imo_membership[0] = NULL;
1348 imo->imo_num_memberships = 0;
1349 imo->imo_multicast_ifp = NULL;
1354 carp_multicast6_cleanup(struct carp_softc *sc)
1356 struct ip6_moptions *im6o = &sc->sc_im6o;
1358 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1359 struct in6_multi_mship *imm =
1360 LIST_FIRST(&im6o->im6o_memberships);
1362 LIST_REMOVE(imm, i6mm_chain);
1363 in6_leavegroup(imm);
1365 im6o->im6o_multicast_ifp = NULL;
1370 carp_get_vhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1372 const struct carp_vhaddr *vha;
1373 struct ifcarpvhaddr *carpa, *carpa0;
1374 int count, len, error;
1377 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1380 if (ifd->ifd_len == 0) {
1381 ifd->ifd_len = count * sizeof(*carpa);
1383 } else if (count == 0 || ifd->ifd_len < sizeof(*carpa)) {
1387 len = min(ifd->ifd_len, sizeof(*carpa) * count);
1388 KKASSERT(len >= sizeof(*carpa));
1390 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1395 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1396 if (len < sizeof(*carpa))
1399 carpa->carpa_flags = vha->vha_flags;
1400 carpa->carpa_addr.sin_family = AF_INET;
1401 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1403 carpa->carpa_baddr.sin_family = AF_INET;
1404 if (vha->vha_iaback == NULL) {
1405 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1407 carpa->carpa_baddr.sin_addr =
1408 vha->vha_iaback->ia_addr.sin_addr;
1413 len -= sizeof(*carpa);
1415 ifd->ifd_len = sizeof(*carpa) * count;
1416 KKASSERT(ifd->ifd_len > 0);
1418 error = copyout(carpa0, ifd->ifd_data, ifd->ifd_len);
1419 kfree(carpa0, M_TEMP);
1424 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
1427 struct in_ifaddr *ia_if;
1428 struct in_ifaddr_container *iac;
1429 const struct sockaddr_in *sin;
1433 KKASSERT(vha->vha_ia != NULL);
1435 sin = &vha->vha_ia->ia_addr;
1436 iaddr = ntohl(sin->sin_addr.s_addr);
1440 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1441 struct in_ifaddr *ia = iac->ia;
1443 if ((ia->ia_flags & IFA_ROUTE) == 0)
1446 if (ia->ia_ifp->if_type == IFT_CARP)
1449 /* and, yeah, we need a multicast-capable iface too */
1450 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1453 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1454 if (sin->sin_addr.s_addr ==
1455 ia->ia_addr.sin_addr.s_addr)
1459 else if (sc->sc_carpdev != NULL &&
1460 sc->sc_carpdev == ia->ia_ifp)
1465 carp_deactivate_vhaddr(sc, vha);
1469 ifp = ia_if->ia_ifp;
1471 /* XXX Don't allow parent iface to be changed */
1472 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
1475 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
1479 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1481 struct carp_vhaddr *vha_new;
1482 struct in_ifaddr *carp_ia;
1484 struct carp_vhaddr *vha;
1487 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1488 carp_ia = ifatoia(carp_ifa);
1491 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1492 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
1495 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
1496 vha_new->vha_ia = carp_ia;
1497 carp_insert_vhaddr(sc, vha_new);
1499 if (carp_config_vhaddr(sc, vha_new) != 0) {
1501 * If the above configuration fails, it may only mean
1502 * that the new address is problematic. However, the
1503 * carp(4) interface may already have several working
1504 * addresses. Since the expected behaviour of
1505 * SIOC[AS]IFADDR is to put the NIC into working state,
1506 * we try starting the state machine manually here with
1507 * the hope that the carp(4)'s previously working
1508 * addresses still could be brought up.
1510 carp_hmac_prepare(sc);
1511 carp_set_state(sc, INIT);
1517 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1519 struct carp_vhaddr *vha;
1520 struct in_ifaddr *carp_ia;
1522 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1523 carp_ia = ifatoia(carp_ifa);
1525 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1526 KKASSERT(vha->vha_ia != NULL);
1527 if (vha->vha_ia == carp_ia)
1530 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1533 * Remove the vhaddr from the list before deactivating
1534 * the vhaddr, so that the HMAC could be correctly
1535 * updated in carp_deactivate_vhaddr()
1537 carp_remove_vhaddr(sc, vha);
1539 carp_deactivate_vhaddr(sc, vha);
1544 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1546 struct carp_vhaddr *vha;
1547 struct in_ifaddr *carp_ia;
1549 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1550 carp_ia = ifatoia(carp_ifa);
1552 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1553 KKASSERT(vha->vha_ia != NULL);
1554 if (vha->vha_ia == carp_ia)
1557 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1559 /* Remove then reinsert, to keep the vhaddr list sorted */
1560 carp_remove_vhaddr(sc, vha);
1561 carp_insert_vhaddr(sc, vha);
1563 if (carp_config_vhaddr(sc, vha) != 0) {
1564 /* See the comment in carp_add_addr() */
1565 carp_hmac_prepare(sc);
1566 carp_set_state(sc, INIT);
1573 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1576 struct carp_if *cif;
1577 struct in6_ifaddr *ia, *ia_if;
1578 struct ip6_moptions *im6o = &sc->sc_im6o;
1579 struct in6_multi_mship *imm;
1580 struct in6_addr in6;
1583 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1588 /* we have to do it by hands to check we won't match on us */
1589 ia_if = NULL; own = 0;
1590 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1593 for (i = 0; i < 4; i++) {
1594 if ((sin6->sin6_addr.s6_addr32[i] &
1595 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1596 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1597 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1600 /* and, yeah, we need a multicast-capable iface too */
1601 if (ia->ia_ifp != &sc->sc_if &&
1602 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1606 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1607 &ia->ia_addr.sin6_addr))
1613 return (EADDRNOTAVAIL);
1617 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1618 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1619 return (EADDRNOTAVAIL);
1621 if (!sc->sc_naddrs6) {
1622 im6o->im6o_multicast_ifp = ifp;
1624 /* join CARP multicast address */
1625 bzero(&in6, sizeof(in6));
1626 in6.s6_addr16[0] = htons(0xff02);
1627 in6.s6_addr8[15] = 0x12;
1628 if (in6_setscope(&in6, ifp, NULL) != 0)
1630 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1632 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1634 /* join solicited multicast address */
1635 bzero(&in6, sizeof(in6));
1636 in6.s6_addr16[0] = htons(0xff02);
1637 in6.s6_addr32[1] = 0;
1638 in6.s6_addr32[2] = htonl(1);
1639 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1640 in6.s6_addr8[12] = 0xff;
1641 if (in6_setscope(&in6, ifp, NULL) != 0)
1643 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1645 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1648 if (!ifp->if_carp) {
1649 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
1651 if ((error = ifpromisc(ifp, 1))) {
1656 TAILQ_INIT(&cif->vhif_vrs);
1659 struct carp_softc *vr;
1662 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1663 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1670 sc->sc_carpdev = ifp;
1672 { /* XXX prevent endless loop if already in queue */
1673 struct carp_softc *vr, *after = NULL;
1677 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1680 if (vr->sc_vhid < sc->sc_vhid)
1685 /* We're trying to keep things in order */
1687 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1689 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1702 /* clean up multicast memberships */
1703 if (!sc->sc_naddrs6) {
1704 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1705 imm = LIST_FIRST(&im6o->im6o_memberships);
1706 LIST_REMOVE(imm, i6mm_chain);
1707 in6_leavegroup(imm);
1714 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1718 if (!--sc->sc_naddrs6) {
1719 struct carp_if *cif = sc->sc_carpdev->if_carp;
1720 struct ip6_moptions *im6o = &sc->sc_im6o;
1722 callout_stop(&sc->sc_ad_tmo);
1724 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1725 struct in6_multi_mship *imm =
1726 LIST_FIRST(&im6o->im6o_memberships);
1728 LIST_REMOVE(imm, i6mm_chain);
1729 in6_leavegroup(imm);
1731 im6o->im6o_multicast_ifp = NULL;
1732 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1733 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
1734 sc->sc_carpdev->if_carp = NULL;
1735 kfree(cif, M_IFADDR);
1743 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
1745 struct carp_softc *sc = ifp->if_softc, *vr;
1746 struct carpreq carpr;
1749 struct ifaliasreq *ifra;
1751 char devname[IFNAMSIZ];
1756 ifa = (struct ifaddr *)addr;
1757 ifra = (struct ifaliasreq *)addr;
1758 ifr = (struct ifreq *)addr;
1759 ifd = (struct ifdrv *)addr;
1763 switch (ifa->ifa_addr->sa_family) {
1766 ifp->if_flags |= IFF_UP | IFF_RUNNING;
1771 ifp->if_flags |= IFF_UP | IFF_RUNNING;
1772 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1776 error = EAFNOSUPPORT;
1782 switch (ifa->ifa_addr->sa_family) {
1785 panic("SIOCAIFADDR should never be seen\n");
1789 ifp->if_flags |= IFF_UP | IFF_RUNNING;
1790 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1794 error = EAFNOSUPPORT;
1800 switch (ifa->ifa_addr->sa_family) {
1803 panic("SIOCDIFADDR should never be seen\n");
1807 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1811 error = EAFNOSUPPORT;
1817 if (ifp->if_flags & IFF_UP) {
1818 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1819 ifp->if_flags |= IFF_RUNNING;
1820 carp_set_state(sc, INIT);
1823 } else if (ifp->if_flags & IFF_RUNNING) {
1829 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1832 error = copyin(ifr->ifr_data, &carpr, sizeof(carpr));
1837 if ((ifp->if_flags & IFF_RUNNING) &&
1838 sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1839 switch (carpr.carpr_state) {
1841 callout_stop(&sc->sc_ad_tmo);
1842 carp_set_state(sc, BACKUP);
1844 carp_setroute(sc, RTM_DELETE);
1848 carp_master_down(sc);
1855 if (carpr.carpr_vhid > 0) {
1856 if (carpr.carpr_vhid > 255) {
1860 if (sc->sc_carpdev) {
1861 struct carp_if *cif = sc->sc_carpdev->if_carp;
1863 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1865 vr->sc_vhid == carpr.carpr_vhid) {
1871 sc->sc_vhid = carpr.carpr_vhid;
1872 IF_LLADDR(ifp)[0] = 0;
1873 IF_LLADDR(ifp)[1] = 0;
1874 IF_LLADDR(ifp)[2] = 0x5e;
1875 IF_LLADDR(ifp)[3] = 0;
1876 IF_LLADDR(ifp)[4] = 1;
1877 IF_LLADDR(ifp)[5] = sc->sc_vhid;
1880 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1881 if (carpr.carpr_advskew >= 255) {
1885 if (carpr.carpr_advbase > 255) {
1889 sc->sc_advbase = carpr.carpr_advbase;
1890 sc->sc_advskew = carpr.carpr_advskew;
1893 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1903 bzero(&carpr, sizeof(carpr));
1904 carpr.carpr_state = sc->sc_state;
1905 carpr.carpr_vhid = sc->sc_vhid;
1906 carpr.carpr_advbase = sc->sc_advbase;
1907 carpr.carpr_advskew = sc->sc_advskew;
1908 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1910 bcopy(sc->sc_key, carpr.carpr_key,
1911 sizeof(carpr.carpr_key));
1914 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1918 switch (ifd->ifd_cmd) {
1920 if (ifd->ifd_len != sizeof(devname))
1934 switch (ifd->ifd_cmd) {
1936 error = carp_get_vhaddr(sc, ifd);
1940 bzero(devname, sizeof(devname));
1941 if (sc->sc_carpdev != NULL) {
1942 strlcpy(devname, sc->sc_carpdev->if_xname,
1945 error = copyout(devname, ifd->ifd_data,
1955 carp_hmac_prepare(sc);
1962 * XXX: this is looutput. We should eventually use it from there.
1965 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1970 M_ASSERTPKTHDR(m); /* check if we have the packet header */
1972 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1974 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1975 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1979 ifp->if_obytes += m->m_pkthdr.len;
1981 /* BPF writes need to be handled specially. */
1982 if (dst->sa_family == AF_UNSPEC) {
1983 bcopy(dst->sa_data, &af, sizeof(af));
1984 dst->sa_family = af;
1988 switch (dst->sa_family) {
1996 return (EAFNOSUPPORT);
1999 return (if_simloop(ifp, m, dst->sa_family, 0));
2003 * Start output on carp interface. This function should never be called.
2006 carp_start(struct ifnet *ifp)
2009 kprintf("%s: start called\n", ifp->if_xname);
2014 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2018 struct carp_softc *sc;
2019 struct ifnet *carp_ifp;
2020 struct ether_header *eh;
2022 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
2027 switch (sa->sa_family) {
2040 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2044 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2045 sc = carp_ifp->if_softc;
2047 /* Set the source MAC address to Virtual Router MAC Address */
2048 switch (ifp->if_type) {
2051 eh = mtod(m, struct ether_header *);
2052 eh->ether_shost[0] = 0;
2053 eh->ether_shost[1] = 0;
2054 eh->ether_shost[2] = 0x5e;
2055 eh->ether_shost[3] = 0;
2056 eh->ether_shost[4] = 1;
2057 eh->ether_shost[5] = sc->sc_vhid;
2061 if_printf(ifp, "carp is not supported for this "
2062 "interface type\n");
2063 return (EOPNOTSUPP);
2069 carp_set_state(struct carp_softc *sc, int state)
2071 struct ifnet *cifp = &sc->sc_if;
2073 if (sc->sc_state == state)
2075 sc->sc_state = state;
2077 switch (sc->sc_state) {
2079 cifp->if_link_state = LINK_STATE_DOWN;
2083 cifp->if_link_state = LINK_STATE_UP;
2087 cifp->if_link_state = LINK_STATE_UNKNOWN;
2094 carp_group_demote_adj(struct ifnet *ifp, int adj)
2096 struct ifg_list *ifgl;
2101 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2102 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2104 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2111 if (adj > 0 && *dm == 1)
2113 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2114 ifgl->ifgl_group->ifg_group, *dm);
2121 carp_carpdev_state(void *v)
2123 struct carp_if *cif = v;
2124 struct carp_softc *sc;
2128 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2135 carp_sc_state(struct carp_softc *sc)
2137 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2138 callout_stop(&sc->sc_ad_tmo);
2139 callout_stop(&sc->sc_md_tmo);
2140 callout_stop(&sc->sc_md6_tmo);
2141 carp_set_state(sc, INIT);
2143 if (!sc->sc_suppress) {
2144 carp_suppress_preempt++;
2145 if (carp_suppress_preempt == 1)
2148 sc->sc_suppress = 1;
2150 carp_set_state(sc, INIT);
2152 if (sc->sc_suppress)
2153 carp_suppress_preempt--;
2154 sc->sc_suppress = 0;
2159 carp_stop(struct carp_softc *sc, int detach)
2161 sc->sc_if.if_flags &= ~IFF_RUNNING;
2163 callout_stop(&sc->sc_ad_tmo);
2164 callout_stop(&sc->sc_md_tmo);
2165 callout_stop(&sc->sc_md6_tmo);
2167 if (!detach && sc->sc_state == MASTER)
2170 if (sc->sc_suppress)
2171 carp_suppress_preempt--;
2172 sc->sc_suppress = 0;
2174 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2175 carp_suppress_preempt--;
2176 sc->sc_sendad_errors = 0;
2177 sc->sc_sendad_success = 0;
2179 carp_set_state(sc, INIT);
2184 carp_reset(struct carp_softc *sc, int detach)
2186 struct ifnet *cifp = &sc->sc_if;
2188 carp_stop(sc, detach);
2189 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2190 cifp->if_flags |= IFF_RUNNING;
2194 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2195 struct ifnet *ifp, const struct in_ifaddr *ia_if, int own)
2197 struct ip_moptions *imo = &sc->sc_imo;
2198 struct carp_if *cif;
2199 struct carp_softc *vr, *after = NULL;
2205 KKASSERT(vha->vha_ia != NULL);
2207 KASSERT(ia_if != NULL, ("NULL backing address\n"));
2208 KASSERT(vha->vha_iaback == NULL, ("%p is already activated\n", vha));
2209 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2210 ("inactive vhaddr %p is the address owner\n", vha));
2212 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2213 ("%s is already on %s\n", sc->sc_if.if_xname,
2214 sc->sc_carpdev->if_xname));
2216 KASSERT(imo->imo_multicast_ifp == NULL ||
2217 imo->imo_multicast_ifp == ifp,
2218 ("%s didn't leave mcast group on %s\n",
2219 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2221 if (imo->imo_num_memberships == 0) {
2222 struct in_addr addr;
2224 addr.s_addr = htonl(INADDR_CARP_GROUP);
2225 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
2227 imo->imo_num_memberships++;
2228 imo->imo_multicast_ifp = ifp;
2229 imo->imo_multicast_ttl = CARP_DFLTTL;
2230 imo->imo_multicast_loop = 0;
2233 if (!ifp->if_carp) {
2234 KASSERT(sc->sc_carpdev == NULL,
2235 ("%s is already on %s\n", sc->sc_if.if_xname,
2236 sc->sc_carpdev->if_xname));
2238 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2240 error = ifpromisc(ifp, 1);
2246 TAILQ_INIT(&cif->vhif_vrs);
2250 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2251 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
2259 if (sc->sc_carpdev != NULL)
2265 sc->sc_carpdev = ifp;
2269 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2272 if (vr->sc_vhid < sc->sc_vhid)
2277 if (assert_onlist) {
2278 KASSERT(onlist, ("%s is not on %s carp list\n",
2279 sc->sc_if.if_xname, ifp->if_xname));
2281 KASSERT(!onlist, ("%s is already on %s carp list\n",
2282 sc->sc_if.if_xname, ifp->if_xname));
2287 /* We're trying to keep things in order */
2289 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2291 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2294 vha->vha_iaback = ia_if;
2298 vha->vha_flags |= CARP_VHAF_OWNER;
2300 /* XXX save user configured advskew? */
2304 carp_hmac_prepare(sc);
2305 carp_set_state(sc, INIT);
2309 carp_multicast_cleanup(sc);
2314 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
2316 KKASSERT(vha->vha_ia != NULL);
2318 carp_hmac_prepare(sc);
2320 if (vha->vha_iaback == NULL) {
2321 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2322 ("inactive vhaddr %p is the address owner\n", vha));
2326 vha->vha_flags &= ~CARP_VHAF_OWNER;
2328 KKASSERT(sc->sc_naddrs > 0);
2329 vha->vha_iaback = NULL;
2331 if (!sc->sc_naddrs) {
2332 if (sc->sc_naddrs6) {
2333 carp_multicast_cleanup(sc);
2342 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2344 struct carp_vhaddr *vha;
2345 struct in_ifaddr *ia_if;
2347 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2348 ia_if = ifatoia(ifa_if);
2350 if ((ia_if->ia_flags & IFA_ROUTE) == 0)
2354 * Test each inactive vhaddr against the newly added address.
2355 * If the newly added address could be the backing address,
2356 * then activate the matching vhaddr.
2358 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2359 const struct in_ifaddr *ia;
2363 if (vha->vha_iaback != NULL)
2367 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr);
2369 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet)
2373 if (ia->ia_addr.sin_addr.s_addr ==
2374 ia_if->ia_addr.sin_addr.s_addr)
2377 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2382 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
2383 struct ifaddr *ifa_if)
2385 struct carp_vhaddr *vha;
2386 struct in_ifaddr *ia_if;
2388 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2389 ia_if = ifatoia(ifa_if);
2392 * Ad src address is deleted; set it to NULL.
2393 * Following loop will try pick up a new ad src address
2394 * if one of the vhaddr could retain its backing address.
2396 if (sc->sc_ia == ia_if)
2400 * Test each active vhaddr against the deleted address.
2401 * If the deleted address is vhaddr address's backing
2402 * address, then deactivate the vhaddr.
2404 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2405 if (vha->vha_iaback == NULL)
2408 if (vha->vha_iaback == ia_if)
2409 carp_deactivate_vhaddr(sc, vha);
2410 else if (sc->sc_ia == NULL)
2411 sc->sc_ia = vha->vha_iaback;
2416 carp_update_addrs(struct carp_softc *sc)
2418 struct carp_vhaddr *vha;
2420 KKASSERT(sc->sc_carpdev == NULL);
2422 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2423 carp_config_vhaddr(sc, vha);
2427 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
2428 enum ifaddr_event event, struct ifaddr *ifa)
2430 struct carp_softc *sc;
2434 if (ifa->ifa_addr->sa_family != AF_INET)
2437 if (ifp->if_type == IFT_CARP) {
2439 * Address is changed on carp(4) interface
2442 case IFADDR_EVENT_ADD:
2443 carp_add_addr(ifp->if_softc, ifa);
2446 case IFADDR_EVENT_CHANGE:
2447 carp_config_addr(ifp->if_softc, ifa);
2450 case IFADDR_EVENT_DELETE:
2451 carp_del_addr(ifp->if_softc, ifa);
2458 * Address is changed on non-carp(4) interface
2460 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2463 LIST_FOREACH(sc, &carpif_list, sc_next) {
2464 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
2465 /* Not the parent iface; skip */
2470 case IFADDR_EVENT_ADD:
2471 carp_link_addrs(sc, ifp, ifa);
2474 case IFADDR_EVENT_DELETE:
2475 if (sc->sc_carpdev != NULL) {
2476 carp_unlink_addrs(sc, ifp, ifa);
2477 if (sc->sc_carpdev == NULL) {
2479 * We no longer have the parent
2480 * interface, however, certain
2481 * virtual addresses, which are
2482 * not used because they can't
2483 * match the previous parent
2484 * interface's addresses, may now
2485 * match different interface's
2488 carp_update_addrs(sc);
2492 * The carp(4) interface didn't have a
2493 * parent iface, so it is not possible
2494 * that it will contain any address to
2500 case IFADDR_EVENT_CHANGE:
2501 if (sc->sc_carpdev == NULL) {
2503 * The carp(4) interface didn't have a
2504 * parent iface, so it is not possible
2505 * that it will contain any address to
2508 carp_link_addrs(sc, ifp, ifa);
2511 * First try breaking tie with the old
2512 * address. Then see whether we could
2513 * link certain vhaddr to the new address.
2514 * If that fails, i.e. carpdev is NULL,
2515 * we try a global update.
2517 * NOTE: The above order is critical.
2519 carp_unlink_addrs(sc, ifp, ifa);
2520 carp_link_addrs(sc, ifp, ifa);
2521 if (sc->sc_carpdev == NULL) {
2523 * See the comment in the above
2524 * IFADDR_EVENT_DELETE block.
2526 carp_update_addrs(sc);
2540 lwkt_gettoken(&carp_tok);
2546 lwkt_reltoken(&carp_tok);
2550 carp_modevent(module_t mod, int type, void *data)
2554 LIST_INIT(&carpif_list);
2555 carp_ifdetach_event =
2556 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
2557 EVENTHANDLER_PRI_ANY);
2559 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
2560 EVENTHANDLER_PRI_ANY);
2561 if_clone_attach(&carp_cloner);
2565 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
2566 carp_ifdetach_event);
2567 EVENTHANDLER_DEREGISTER(ifaddr_event,
2569 if_clone_detach(&carp_cloner);
2578 static moduledata_t carp_mod = {
2583 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);