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>
44 #include <sys/sockio.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
49 #include <machine/stdarg.h>
50 #include <crypto/sha1.h>
53 #include <net/ethernet.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <net/if_clone.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/if_ether.h>
70 #include <netinet/icmp6.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/scope6_var.h>
74 #include <netinet6/nd6.h>
77 #include <netinet/ip_carp.h>
79 #define CARP_IFNAME "carp"
80 #define CARP_IS_RUNNING(ifp) \
81 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
84 uint32_t vha_flags; /* CARP_VHAF_ */
85 const struct in_ifaddr *vha_ia; /* carp address */
86 const struct in_ifaddr *vha_iaback; /* backing address */
87 TAILQ_ENTRY(carp_vhaddr) vha_link;
89 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr);
93 struct ifnet *sc_carpdev; /* parent interface */
94 struct carp_vhaddr_list sc_vha_list; /* virtual addr list */
96 const struct in_ifaddr *sc_ia; /* primary iface address v4 */
97 struct ip_moptions sc_imo;
100 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
101 struct ip6_moptions sc_im6o;
103 TAILQ_ENTRY(carp_softc) sc_list;
105 enum { INIT = 0, BACKUP, MASTER }
111 int sc_sendad_errors;
112 #define CARP_SENDAD_MAX_ERRORS 3
113 int sc_sendad_success;
114 #define CARP_SENDAD_MIN_SUCCESS 3
118 int sc_naddrs; /* actually used IPv4 vha */
120 int sc_advbase; /* seconds */
125 #define CARP_HMAC_PAD 64
126 unsigned char sc_key[CARP_KEY_LEN];
127 unsigned char sc_pad[CARP_HMAC_PAD];
130 struct callout sc_ad_tmo; /* advertisement timeout */
131 struct callout sc_md_tmo; /* master down timeout */
132 struct callout sc_md6_tmo; /* master down timeout */
134 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
138 TAILQ_HEAD(, carp_softc) vhif_vrs;
141 enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
143 SYSCTL_DECL(_net_inet_carp);
145 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
146 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
147 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
148 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
149 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
150 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
151 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
152 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
153 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
155 static int carp_suppress_preempt = 0;
156 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
157 &carp_suppress_preempt, 0, "Preemption is suppressed");
159 static struct carpstats carpstats;
160 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
161 &carpstats, carpstats,
162 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
164 #define CARP_LOG(...) do { \
165 if (carp_opts[CARPCTL_LOG] > 0) \
166 log(LOG_INFO, __VA_ARGS__); \
169 #define CARP_DEBUG(...) do { \
170 if (carp_opts[CARPCTL_LOG] > 1) \
171 log(LOG_DEBUG, __VA_ARGS__); \
174 static void carp_hmac_prepare(struct carp_softc *);
175 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
177 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
179 static void carp_setroute(struct carp_softc *, int);
180 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
181 static int carp_clone_create(struct if_clone *, int);
182 static void carp_clone_destroy(struct ifnet *);
183 static void carp_detach(struct carp_softc *, int);
184 static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
185 struct carp_header *);
186 static void carp_send_ad_all(void);
187 static void carp_send_ad_timeout(void *);
188 static void carp_send_ad(struct carp_softc *);
189 static void carp_send_arp(struct carp_softc *);
190 static void carp_master_down_timeout(void *);
191 static void carp_master_down(struct carp_softc *);
192 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
193 static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
195 static void carp_start(struct ifnet *);
196 static void carp_setrun(struct carp_softc *, sa_family_t);
197 static void carp_set_state(struct carp_softc *, int);
199 static void carp_multicast_cleanup(struct carp_softc *);
200 static void carp_add_addr(struct carp_softc *, struct ifaddr *);
201 static void carp_del_addr(struct carp_softc *, struct ifaddr *);
202 static void carp_config_addr(struct carp_softc *, struct ifaddr *);
203 static void carp_link_addrs(struct carp_softc *, struct ifnet *,
205 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *,
208 static int carp_get_vhaddr(struct carp_softc *, struct ifdrv *);
209 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *);
210 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
211 struct ifnet *, const struct in_ifaddr *, int);
212 static void carp_deactivate_vhaddr(struct carp_softc *,
213 struct carp_vhaddr *);
215 static void carp_sc_state(struct carp_softc *);
217 static void carp_send_na(struct carp_softc *);
218 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
219 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
220 static void carp_multicast6_cleanup(struct carp_softc *);
222 static void carp_stop(struct carp_softc *, int);
223 static void carp_reset(struct carp_softc *, int);
225 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
227 static void carp_ifdetach(void *, struct ifnet *);
229 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
231 static LIST_HEAD(, carp_softc) carpif_list;
233 static struct if_clone carp_cloner =
234 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
237 static eventhandler_tag carp_ifdetach_event;
238 static eventhandler_tag carp_ifaddr_event;
241 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
243 struct carp_vhaddr *vha;
244 u_long new_addr, addr;
246 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
249 * Virtual address list is sorted; smaller one first
251 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
253 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
254 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
260 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
262 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
263 vha_new->vha_flags |= CARP_VHAF_ONLIST;
267 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
269 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
270 vha->vha_flags &= ~CARP_VHAF_ONLIST;
271 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
275 carp_hmac_prepare(struct carp_softc *sc)
277 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
278 uint8_t vhid = sc->sc_vhid & 0xff;
279 struct ifaddr_container *ifac;
285 struct carp_vhaddr *vha;
288 /* XXX: possible race here */
290 /* compute ipad from key */
291 bzero(sc->sc_pad, sizeof(sc->sc_pad));
292 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
293 for (i = 0; i < sizeof(sc->sc_pad); i++)
294 sc->sc_pad[i] ^= 0x36;
296 /* precompute first part of inner hash */
297 SHA1Init(&sc->sc_sha1);
298 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
299 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
300 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
301 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
303 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
304 SHA1Update(&sc->sc_sha1,
305 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
306 sizeof(struct in_addr));
310 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
311 struct ifaddr *ifa = ifac->ifa;
313 if (ifa->ifa_addr->sa_family == AF_INET6) {
314 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
315 in6_clearscope(&in6);
316 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
321 /* convert ipad to opad */
322 for (i = 0; i < sizeof(sc->sc_pad); i++)
323 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
327 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
328 unsigned char md[20])
332 /* fetch first half of inner hash */
333 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
335 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
336 SHA1Final(md, &sha1ctx);
340 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
341 SHA1Update(&sha1ctx, md, 20);
342 SHA1Final(md, &sha1ctx);
346 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
347 unsigned char md[20])
349 unsigned char md2[20];
351 carp_hmac_generate(sc, counter, md2);
352 return (bcmp(md, md2, sizeof(md2)));
356 carp_setroute(struct carp_softc *sc, int cmd)
359 struct ifaddr_container *ifac;
362 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
363 struct ifaddr *ifa = ifac->ifa;
365 if (ifa->ifa_addr->sa_family == AF_INET6) {
377 carp_clone_create(struct if_clone *ifc, int unit)
379 struct carp_softc *sc;
382 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
386 sc->sc_advbase = CARP_DFLTINTV;
387 sc->sc_vhid = -1; /* required setting */
389 sc->sc_init_counter = 1;
393 TAILQ_INIT(&sc->sc_vha_list);
396 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
399 callout_init(&sc->sc_ad_tmo);
400 callout_init(&sc->sc_md_tmo);
401 callout_init(&sc->sc_md6_tmo);
404 if_initname(ifp, CARP_IFNAME, unit);
405 ifp->if_mtu = ETHERMTU;
406 ifp->if_flags = IFF_LOOPBACK;
407 ifp->if_ioctl = carp_ioctl;
408 ifp->if_output = carp_looutput;
409 ifp->if_start = carp_start;
410 ifp->if_type = IFT_CARP;
411 ifp->if_snd.ifq_maxlen = ifqmaxlen;
413 if_attach(ifp, NULL);
414 bpfattach(ifp, DLT_NULL, sizeof(u_int));
417 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
424 carp_clone_destroy(struct ifnet *ifp)
426 struct carp_softc *sc = ifp->if_softc;
432 LIST_REMOVE(sc, sc_next);
437 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active\n"));
442 carp_detach(struct carp_softc *sc, int detach)
446 carp_reset(sc, detach);
448 carp_multicast_cleanup(sc);
450 carp_multicast6_cleanup(sc);
453 if (!sc->sc_dead && detach) {
454 struct carp_vhaddr *vha;
456 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
457 carp_deactivate_vhaddr(sc, vha);
458 KKASSERT(sc->sc_naddrs == 0);
461 if (sc->sc_carpdev != NULL) {
462 cif = sc->sc_carpdev->if_carp;
463 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
464 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
465 ifpromisc(sc->sc_carpdev, 0);
466 sc->sc_carpdev->if_carp = NULL;
469 sc->sc_carpdev = NULL;
474 /* Detach an interface from the carp. */
476 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
478 struct carp_if *cif = ifp->if_carp;
479 struct carp_softc *sc;
481 while (ifp->if_carp &&
482 (sc = TAILQ_FIRST(&cif->vhif_vrs)) != NULL)
487 * process input packet.
488 * we have rearranged checks order compared to the rfc,
489 * but it seems more efficient this way or not possible otherwise.
492 carp_input(struct mbuf *m, ...)
494 struct ip *ip = mtod(m, struct ip *);
495 struct carp_header *ch;
500 iphlen = __va_arg(ap, int);
503 carpstats.carps_ipackets++;
505 if (!carp_opts[CARPCTL_ALLOW]) {
510 /* Check if received on a valid carp interface */
511 if (m->m_pkthdr.rcvif->if_carp == NULL) {
512 carpstats.carps_badif++;
513 CARP_LOG("carp_input: packet received on non-carp "
515 m->m_pkthdr.rcvif->if_xname);
520 /* Verify that the IP TTL is CARP_DFLTTL. */
521 if (ip->ip_ttl != CARP_DFLTTL) {
522 carpstats.carps_badttl++;
523 CARP_LOG("carp_input: received ttl %d != %d on %s\n",
524 ip->ip_ttl, CARP_DFLTTL,
525 m->m_pkthdr.rcvif->if_xname);
530 /* Minimal CARP packet size */
531 len = iphlen + sizeof(*ch);
534 * Verify that the received packet length is
535 * not less than the CARP header
537 if (m->m_pkthdr.len < len) {
538 carpstats.carps_badlen++;
539 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
540 m->m_pkthdr.rcvif->if_xname);
545 /* Make sure that CARP header is contiguous */
546 if (len > m->m_len) {
547 m = m_pullup(m, len);
549 carpstats.carps_hdrops++;
550 CARP_LOG("carp_input: m_pullup failed\n");
553 ip = mtod(m, struct ip *);
555 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
557 /* Verify the CARP checksum */
558 if (in_cksum_skip(m, len, iphlen)) {
559 carpstats.carps_badsum++;
560 CARP_LOG("carp_input: checksum failed on %s\n",
561 m->m_pkthdr.rcvif->if_xname);
565 carp_input_c(m, ch, AF_INET);
570 carp6_input(struct mbuf **mp, int *offp, int proto)
572 struct mbuf *m = *mp;
573 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
574 struct carp_header *ch;
577 carpstats.carps_ipackets6++;
579 if (!carp_opts[CARPCTL_ALLOW]) {
581 return (IPPROTO_DONE);
584 /* check if received on a valid carp interface */
585 if (m->m_pkthdr.rcvif->if_carp == NULL) {
586 carpstats.carps_badif++;
587 CARP_LOG("carp6_input: packet received on non-carp "
589 m->m_pkthdr.rcvif->if_xname);
591 return (IPPROTO_DONE);
594 /* verify that the IP TTL is 255 */
595 if (ip6->ip6_hlim != CARP_DFLTTL) {
596 carpstats.carps_badttl++;
597 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
599 m->m_pkthdr.rcvif->if_xname);
601 return (IPPROTO_DONE);
604 /* verify that we have a complete carp packet */
606 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
608 carpstats.carps_badlen++;
609 CARP_LOG("carp6_input: packet size %u too small\n", len);
610 return (IPPROTO_DONE);
613 /* verify the CARP checksum */
614 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
615 carpstats.carps_badsum++;
616 CARP_LOG("carp6_input: checksum failed, on %s\n",
617 m->m_pkthdr.rcvif->if_xname);
619 return (IPPROTO_DONE);
622 carp_input_c(m, ch, AF_INET6);
623 return (IPPROTO_DONE);
628 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
630 struct ifnet *ifp = m->m_pkthdr.rcvif;
632 struct carp_softc *sc;
633 uint64_t tmp_counter;
634 struct timeval sc_tv, ch_tv;
636 /* verify that the VHID is valid on the receiving interface */
637 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
638 if (sc->sc_vhid == ch->carp_vhid)
641 if (!sc || !CARP_IS_RUNNING(&sc->sc_if)) {
642 carpstats.carps_badvhid++;
648 getmicrotime(&cifp->if_lastchange);
650 cifp->if_ibytes += m->m_pkthdr.len;
653 struct ip *ip = mtod(m, struct ip *);
655 /* BPF wants net byte order */
656 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
657 ip->ip_off = htons(ip->ip_off);
658 bpf_mtap(cifp->if_bpf, m);
661 /* verify the CARP version. */
662 if (ch->carp_version != CARP_VERSION) {
663 carpstats.carps_badver++;
665 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
671 /* verify the hash */
672 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
673 carpstats.carps_badauth++;
675 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
680 tmp_counter = ntohl(ch->carp_counter[0]);
681 tmp_counter = tmp_counter<<32;
682 tmp_counter += ntohl(ch->carp_counter[1]);
684 /* XXX Replay protection goes here */
686 sc->sc_init_counter = 0;
687 sc->sc_counter = tmp_counter;
689 sc_tv.tv_sec = sc->sc_advbase;
690 if (carp_suppress_preempt && sc->sc_advskew < 240)
691 sc_tv.tv_usec = 240 * 1000000 / 256;
693 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
694 ch_tv.tv_sec = ch->carp_advbase;
695 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
697 switch (sc->sc_state) {
703 * If we receive an advertisement from a master who's going to
704 * be more frequent than us, go into BACKUP state.
706 if (timevalcmp(&sc_tv, &ch_tv, >) ||
707 timevalcmp(&sc_tv, &ch_tv, ==)) {
708 callout_stop(&sc->sc_ad_tmo);
709 CARP_DEBUG("%s: MASTER -> BACKUP "
710 "(more frequent advertisement received)\n",
712 carp_set_state(sc, BACKUP);
714 carp_setroute(sc, RTM_DELETE);
720 * If we're pre-empting masters who advertise slower than us,
721 * and this one claims to be slower, treat him as down.
723 if (carp_opts[CARPCTL_PREEMPT] &&
724 timevalcmp(&sc_tv, &ch_tv, <)) {
725 CARP_DEBUG("%s: BACKUP -> MASTER "
726 "(preempting a slower master)\n", cifp->if_xname);
727 carp_master_down(sc);
732 * If the master is going to advertise at such a low frequency
733 * that he's guaranteed to time out, we'd might as well just
734 * treat him as timed out now.
736 sc_tv.tv_sec = sc->sc_advbase * 3;
737 if (timevalcmp(&sc_tv, &ch_tv, <)) {
738 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
740 carp_master_down(sc);
745 * Otherwise, we reset the counter and wait for the next
755 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
757 struct ifnet *cifp = &sc->sc_if;
760 if (sc->sc_init_counter) {
761 /* this could also be seconds since unix epoch */
762 sc->sc_counter = karc4random();
763 sc->sc_counter = sc->sc_counter << 32;
764 sc->sc_counter += karc4random();
769 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
770 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
772 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
774 /* Tag packet for carp_output */
775 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), MB_DONTWAIT);
781 bcopy(&cifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
782 m_tag_prepend(m, mtag);
788 carp_send_ad_all(void)
790 struct carp_softc *sc;
792 LIST_FOREACH(sc, &carpif_list, sc_next) {
793 if (sc->sc_carpdev == NULL)
796 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
802 carp_send_ad_timeout(void *xsc)
808 carp_send_ad(struct carp_softc *sc)
810 struct ifnet *cifp = &sc->sc_if;
811 struct carp_header ch;
813 struct carp_header *ch_ptr;
815 int len, advbase, advskew;
817 if (!CARP_IS_RUNNING(cifp)) {
822 advbase = sc->sc_advbase;
823 if (!carp_suppress_preempt || sc->sc_advskew > 240)
824 advskew = sc->sc_advskew;
828 tv.tv_usec = advskew * 1000000 / 256;
831 ch.carp_version = CARP_VERSION;
832 ch.carp_type = CARP_ADVERTISEMENT;
833 ch.carp_vhid = sc->sc_vhid;
834 ch.carp_advbase = advbase;
835 ch.carp_advskew = advskew;
836 ch.carp_authlen = 7; /* XXX DEFINE */
837 ch.carp_pad1 = 0; /* must be zero */
841 if (sc->sc_ia != NULL) {
844 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
847 carpstats.carps_onomem++;
848 /* XXX maybe less ? */
849 if (advbase != 255 || advskew != 255)
850 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
851 carp_send_ad_timeout, sc);
854 len = sizeof(*ip) + sizeof(ch);
855 m->m_pkthdr.len = len;
856 m->m_pkthdr.rcvif = NULL;
858 MH_ALIGN(m, m->m_len);
859 m->m_flags |= M_MCAST;
860 ip = mtod(m, struct ip *);
861 ip->ip_v = IPVERSION;
862 ip->ip_hl = sizeof(*ip) >> 2;
863 ip->ip_tos = IPTOS_LOWDELAY;
865 ip->ip_id = ip_newid();
867 ip->ip_ttl = CARP_DFLTTL;
868 ip->ip_p = IPPROTO_CARP;
870 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
871 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
873 ch_ptr = (struct carp_header *)(&ip[1]);
874 bcopy(&ch, ch_ptr, sizeof(ch));
875 if (carp_prepare_ad(m, sc, ch_ptr))
877 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
879 getmicrotime(&cifp->if_lastchange);
881 cifp->if_obytes += len;
882 carpstats.carps_opackets++;
884 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
886 if (sc->sc_sendad_errors < INT_MAX)
887 sc->sc_sendad_errors++;
888 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
889 carp_suppress_preempt++;
890 if (carp_suppress_preempt == 1) {
894 sc->sc_sendad_success = 0;
896 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
897 if (++sc->sc_sendad_success >=
898 CARP_SENDAD_MIN_SUCCESS) {
899 carp_suppress_preempt--;
900 sc->sc_sendad_errors = 0;
903 sc->sc_sendad_errors = 0;
912 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
915 carpstats.carps_onomem++;
916 /* XXX maybe less ? */
917 if (advbase != 255 || advskew != 255)
918 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
919 carp_send_ad_timeout, sc);
922 len = sizeof(*ip6) + sizeof(ch);
923 m->m_pkthdr.len = len;
924 m->m_pkthdr.rcvif = NULL;
926 MH_ALIGN(m, m->m_len);
927 m->m_flags |= M_MCAST;
928 ip6 = mtod(m, struct ip6_hdr *);
929 bzero(ip6, sizeof(*ip6));
930 ip6->ip6_vfc |= IPV6_VERSION;
931 ip6->ip6_hlim = CARP_DFLTTL;
932 ip6->ip6_nxt = IPPROTO_CARP;
933 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
934 sizeof(struct in6_addr));
935 /* set the multicast destination */
937 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
938 ip6->ip6_dst.s6_addr8[15] = 0x12;
939 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
942 CARP_LOG("%s: in6_setscope failed\n", __func__);
946 ch_ptr = (struct carp_header *)(&ip6[1]);
947 bcopy(&ch, ch_ptr, sizeof(ch));
948 if (carp_prepare_ad(m, sc, ch_ptr))
950 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
952 getmicrotime(&cifp->if_lastchange);
954 cifp->if_obytes += len;
955 carpstats.carps_opackets6++;
957 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
959 if (sc->sc_sendad_errors < INT_MAX)
960 sc->sc_sendad_errors++;
961 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
962 carp_suppress_preempt++;
963 if (carp_suppress_preempt == 1) {
967 sc->sc_sendad_success = 0;
969 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
970 if (++sc->sc_sendad_success >=
971 CARP_SENDAD_MIN_SUCCESS) {
972 carp_suppress_preempt--;
973 sc->sc_sendad_errors = 0;
976 sc->sc_sendad_errors = 0;
982 if (advbase != 255 || advskew != 255)
983 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
984 carp_send_ad_timeout, sc);
988 * Broadcast a gratuitous ARP request containing
989 * the virtual router MAC address for each IP address
990 * associated with the virtual router.
993 carp_send_arp(struct carp_softc *sc)
995 const struct carp_vhaddr *vha;
997 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
998 if (vha->vha_iaback == NULL)
1001 arp_iainit(sc->sc_carpdev, &vha->vha_ia->ia_addr.sin_addr,
1002 IF_LLADDR(&sc->sc_if));
1008 carp_send_na(struct carp_softc *sc)
1010 struct ifaddr_container *ifac;
1011 struct in6_addr *in6;
1012 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1014 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1015 struct ifaddr *ifa = ifac->ifa;
1017 if (ifa->ifa_addr->sa_family != AF_INET6)
1020 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1021 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1022 ND_NA_FLAG_OVERRIDE, 1, NULL);
1023 DELAY(1000); /* XXX */
1028 static __inline const struct carp_vhaddr *
1029 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1031 struct carp_vhaddr *vha;
1033 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1034 if (vha->vha_iaback == NULL)
1037 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1044 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1045 const struct in_addr *isaddr, uint8_t **enaddr)
1047 const struct carp_softc *vh;
1048 int index, count = 0;
1051 * XXX proof of concept implementation.
1052 * We use the source ip to decide which virtual host should
1053 * handle the request. If we're master of that virtual host,
1054 * then we respond, otherwise, just drop the arp packet on
1058 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1059 if (!CARP_IS_RUNNING(&vh->sc_if))
1062 if (carp_find_addr(vh, itaddr) != NULL)
1068 /* this should be a hash, like pf_hash() */
1069 index = ntohl(isaddr->s_addr) % count;
1072 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1073 if (!CARP_IS_RUNNING(&vh->sc_if))
1076 if (carp_find_addr(vh, itaddr) == NULL)
1079 if (count == index) {
1080 if (vh->sc_state == MASTER) {
1081 *enaddr = IF_LLADDR(&vh->sc_if);
1093 carp_iamatch(const void *v, const struct in_addr *itaddr,
1094 const struct in_addr *isaddr, uint8_t **enaddr)
1096 const struct carp_if *cif = v;
1097 const struct carp_softc *vh;
1099 if (carp_opts[CARPCTL_ARPBALANCE])
1100 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1102 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1103 if (!CARP_IS_RUNNING(&vh->sc_if) || vh->sc_state != MASTER)
1106 if (carp_find_addr(vh, itaddr) != NULL) {
1107 *enaddr = IF_LLADDR(&vh->sc_if);
1116 carp_iamatch6(void *v, struct in6_addr *taddr)
1118 struct carp_if *cif = v;
1119 struct carp_softc *vh;
1121 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1122 struct ifaddr_container *ifac;
1124 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1126 struct ifaddr *ifa = ifac->ifa;
1128 if (IN6_ARE_ADDR_EQUAL(taddr,
1129 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1130 CARP_IS_RUNNING(&vh->sc_if) &&
1131 vh->sc_state == MASTER) {
1140 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1143 struct carp_if *cif = v;
1144 struct carp_softc *sc;
1146 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1147 struct ifaddr_container *ifac;
1149 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1151 struct ifaddr *ifa = ifac->ifa;
1153 if (IN6_ARE_ADDR_EQUAL(taddr,
1154 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1155 CARP_IS_RUNNING(&sc->sc_if)) {
1156 struct ifnet *ifp = &sc->sc_if;
1158 mtag = m_tag_get(PACKET_TAG_CARP,
1159 sizeof(struct ifnet *), MB_DONTWAIT);
1161 /* better a bit than nothing */
1162 return (IF_LLADDR(ifp));
1164 bcopy(&ifp, (caddr_t)(mtag + 1),
1165 sizeof(struct ifnet *));
1166 m_tag_prepend(m, mtag);
1168 return (IF_LLADDR(ifp));
1177 carp_forus(const void *v, const void *dhost)
1179 const struct carp_if *cif = v;
1180 const struct carp_softc *vh;
1181 const uint8_t *ena = dhost;
1183 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1186 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1187 const struct ifnet *cifp = &vh->sc_if;
1189 if (CARP_IS_RUNNING(cifp) && vh->sc_state == MASTER &&
1190 !bcmp(dhost, IF_LLADDR(cifp), ETHER_ADDR_LEN))
1197 carp_master_down_timeout(void *xsc)
1199 struct carp_softc *sc = xsc;
1201 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1202 sc->sc_if.if_xname);
1203 carp_master_down(sc);
1207 carp_master_down(struct carp_softc *sc)
1209 switch (sc->sc_state) {
1211 kprintf("%s: master_down event in INIT state\n",
1212 sc->sc_if.if_xname);
1219 carp_set_state(sc, MASTER);
1226 carp_setroute(sc, RTM_ADD);
1232 * When in backup state, af indicates whether to reset the master down timer
1233 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1236 carp_setrun(struct carp_softc *sc, sa_family_t af)
1238 struct ifnet *cifp = &sc->sc_if;
1241 if (sc->sc_carpdev == NULL) {
1242 carp_set_state(sc, INIT);
1246 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1247 (sc->sc_naddrs || sc->sc_naddrs6)) {
1250 carp_setroute(sc, RTM_DELETE);
1254 switch (sc->sc_state) {
1256 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1262 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1264 carp_set_state(sc, MASTER);
1265 carp_setroute(sc, RTM_ADD);
1267 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1268 carp_set_state(sc, BACKUP);
1269 carp_setroute(sc, RTM_DELETE);
1275 callout_stop(&sc->sc_ad_tmo);
1276 tv.tv_sec = 3 * sc->sc_advbase;
1277 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1281 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1282 carp_master_down_timeout, sc);
1287 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1288 carp_master_down_timeout, sc);
1293 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1294 carp_master_down_timeout, sc);
1296 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1297 carp_master_down_timeout, sc);
1303 tv.tv_sec = sc->sc_advbase;
1304 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1305 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1306 carp_send_ad_timeout, sc);
1312 carp_multicast_cleanup(struct carp_softc *sc)
1314 struct ip_moptions *imo = &sc->sc_imo;
1316 if (imo->imo_num_memberships == 0)
1318 KKASSERT(imo->imo_num_memberships == 1);
1320 in_delmulti(imo->imo_membership[0]);
1321 imo->imo_membership[0] = NULL;
1322 imo->imo_num_memberships = 0;
1323 imo->imo_multicast_ifp = NULL;
1328 carp_multicast6_cleanup(struct carp_softc *sc)
1330 struct ip6_moptions *im6o = &sc->sc_im6o;
1332 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1333 struct in6_multi_mship *imm =
1334 LIST_FIRST(&im6o->im6o_memberships);
1336 LIST_REMOVE(imm, i6mm_chain);
1337 in6_leavegroup(imm);
1339 im6o->im6o_multicast_ifp = NULL;
1344 carp_get_vhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1346 const struct carp_vhaddr *vha;
1347 struct ifcarpvhaddr *carpa, *carpa0;
1348 int count, len, error;
1351 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1354 if (ifd->ifd_len == 0) {
1355 ifd->ifd_len = count * sizeof(*carpa);
1357 } else if (count == 0 || ifd->ifd_len < sizeof(*carpa)) {
1361 len = min(ifd->ifd_len, sizeof(*carpa) * count);
1362 KKASSERT(len >= sizeof(*carpa));
1364 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1369 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1370 if (len < sizeof(*carpa))
1373 carpa->carpa_flags = vha->vha_flags;
1374 carpa->carpa_addr.sin_family = AF_INET;
1375 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1377 carpa->carpa_baddr.sin_family = AF_INET;
1378 if (vha->vha_iaback == NULL) {
1379 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1381 carpa->carpa_baddr.sin_addr =
1382 vha->vha_iaback->ia_addr.sin_addr;
1387 len -= sizeof(*carpa);
1389 ifd->ifd_len = sizeof(*carpa) * count;
1390 KKASSERT(ifd->ifd_len > 0);
1392 error = copyout(carpa0, ifd->ifd_data, ifd->ifd_len);
1393 kfree(carpa0, M_TEMP);
1398 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
1401 struct in_ifaddr *ia_if;
1402 struct in_ifaddr_container *iac;
1403 const struct sockaddr_in *sin;
1407 KKASSERT(vha->vha_ia != NULL);
1409 sin = &vha->vha_ia->ia_addr;
1410 iaddr = ntohl(sin->sin_addr.s_addr);
1414 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1415 struct in_ifaddr *ia = iac->ia;
1417 if ((ia->ia_flags & IFA_ROUTE) == 0)
1420 if (ia->ia_ifp->if_type == IFT_CARP)
1423 /* and, yeah, we need a multicast-capable iface too */
1424 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1427 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1428 if (sin->sin_addr.s_addr ==
1429 ia->ia_addr.sin_addr.s_addr)
1433 else if (sc->sc_carpdev != NULL &&
1434 sc->sc_carpdev == ia->ia_ifp)
1439 carp_deactivate_vhaddr(sc, vha);
1443 ifp = ia_if->ia_ifp;
1445 /* XXX Don't allow parent iface to be changed */
1446 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
1449 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
1453 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1455 struct carp_vhaddr *vha_new;
1456 struct in_ifaddr *carp_ia;
1458 struct carp_vhaddr *vha;
1461 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1462 carp_ia = ifatoia(carp_ifa);
1465 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1466 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
1469 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
1470 vha_new->vha_ia = carp_ia;
1471 carp_insert_vhaddr(sc, vha_new);
1473 if (carp_config_vhaddr(sc, vha_new) != 0) {
1475 * If the above configuration fails, it may only mean
1476 * that the new address is problematic. However, the
1477 * carp(4) interface may already have several working
1478 * addresses. Since the expected behaviour of
1479 * SIOC[AS]IFADDR is to put the NIC into working state,
1480 * we try starting the state machine manually here with
1481 * the hope that the carp(4)'s previously working
1482 * addresses still could be brought up.
1484 carp_hmac_prepare(sc);
1485 carp_set_state(sc, INIT);
1491 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1493 struct carp_vhaddr *vha;
1494 struct in_ifaddr *carp_ia;
1496 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1497 carp_ia = ifatoia(carp_ifa);
1499 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1500 KKASSERT(vha->vha_ia != NULL);
1501 if (vha->vha_ia == carp_ia)
1504 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1507 * Remove the vhaddr from the list before deactivating
1508 * the vhaddr, so that the HMAC could be correctly
1509 * updated in carp_deactivate_vhaddr()
1511 carp_remove_vhaddr(sc, vha);
1513 carp_deactivate_vhaddr(sc, vha);
1518 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1520 struct carp_vhaddr *vha;
1521 struct in_ifaddr *carp_ia;
1523 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1524 carp_ia = ifatoia(carp_ifa);
1526 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1527 KKASSERT(vha->vha_ia != NULL);
1528 if (vha->vha_ia == carp_ia)
1531 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1533 /* Remove then reinsert, to keep the vhaddr list sorted */
1534 carp_remove_vhaddr(sc, vha);
1535 carp_insert_vhaddr(sc, vha);
1537 if (carp_config_vhaddr(sc, vha) != 0) {
1538 /* See the comment in carp_add_addr() */
1539 carp_hmac_prepare(sc);
1540 carp_set_state(sc, INIT);
1547 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1550 struct carp_if *cif;
1551 struct in6_ifaddr *ia, *ia_if;
1552 struct ip6_moptions *im6o = &sc->sc_im6o;
1553 struct in6_multi_mship *imm;
1554 struct in6_addr in6;
1557 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1562 /* we have to do it by hands to check we won't match on us */
1563 ia_if = NULL; own = 0;
1564 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1567 for (i = 0; i < 4; i++) {
1568 if ((sin6->sin6_addr.s6_addr32[i] &
1569 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1570 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1571 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1574 /* and, yeah, we need a multicast-capable iface too */
1575 if (ia->ia_ifp != &sc->sc_if &&
1576 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1580 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1581 &ia->ia_addr.sin6_addr))
1587 return (EADDRNOTAVAIL);
1591 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1592 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1593 return (EADDRNOTAVAIL);
1595 if (!sc->sc_naddrs6) {
1596 im6o->im6o_multicast_ifp = ifp;
1598 /* join CARP multicast address */
1599 bzero(&in6, sizeof(in6));
1600 in6.s6_addr16[0] = htons(0xff02);
1601 in6.s6_addr8[15] = 0x12;
1602 if (in6_setscope(&in6, ifp, NULL) != 0)
1604 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1606 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1608 /* join solicited multicast address */
1609 bzero(&in6, sizeof(in6));
1610 in6.s6_addr16[0] = htons(0xff02);
1611 in6.s6_addr32[1] = 0;
1612 in6.s6_addr32[2] = htonl(1);
1613 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1614 in6.s6_addr8[12] = 0xff;
1615 if (in6_setscope(&in6, ifp, NULL) != 0)
1617 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1619 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1622 if (!ifp->if_carp) {
1623 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
1625 if ((error = ifpromisc(ifp, 1))) {
1630 TAILQ_INIT(&cif->vhif_vrs);
1633 struct carp_softc *vr;
1636 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1637 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1644 sc->sc_carpdev = ifp;
1646 { /* XXX prevent endless loop if already in queue */
1647 struct carp_softc *vr, *after = NULL;
1651 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1654 if (vr->sc_vhid < sc->sc_vhid)
1659 /* We're trying to keep things in order */
1661 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1663 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1676 /* clean up multicast memberships */
1677 if (!sc->sc_naddrs6) {
1678 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1679 imm = LIST_FIRST(&im6o->im6o_memberships);
1680 LIST_REMOVE(imm, i6mm_chain);
1681 in6_leavegroup(imm);
1688 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1692 if (!--sc->sc_naddrs6) {
1693 struct carp_if *cif = sc->sc_carpdev->if_carp;
1694 struct ip6_moptions *im6o = &sc->sc_im6o;
1696 callout_stop(&sc->sc_ad_tmo);
1698 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1699 struct in6_multi_mship *imm =
1700 LIST_FIRST(&im6o->im6o_memberships);
1702 LIST_REMOVE(imm, i6mm_chain);
1703 in6_leavegroup(imm);
1705 im6o->im6o_multicast_ifp = NULL;
1706 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1707 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
1708 sc->sc_carpdev->if_carp = NULL;
1709 kfree(cif, M_IFADDR);
1717 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
1719 struct carp_softc *sc = ifp->if_softc, *vr;
1720 struct carpreq carpr;
1723 struct ifaliasreq *ifra;
1725 char devname[IFNAMSIZ];
1728 ifa = (struct ifaddr *)addr;
1729 ifra = (struct ifaliasreq *)addr;
1730 ifr = (struct ifreq *)addr;
1731 ifd = (struct ifdrv *)addr;
1735 switch (ifa->ifa_addr->sa_family) {
1738 ifp->if_flags |= IFF_UP | IFF_RUNNING;
1743 ifp->if_flags |= IFF_UP | IFF_RUNNING;
1744 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1748 error = EAFNOSUPPORT;
1754 switch (ifa->ifa_addr->sa_family) {
1757 panic("SIOCAIFADDR should never be seen\n");
1761 ifp->if_flags |= IFF_UP | IFF_RUNNING;
1762 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1766 error = EAFNOSUPPORT;
1772 switch (ifa->ifa_addr->sa_family) {
1775 panic("SIOCDIFADDR should never be seen\n");
1779 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1783 error = EAFNOSUPPORT;
1789 if (ifp->if_flags & IFF_UP) {
1790 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1791 ifp->if_flags |= IFF_RUNNING;
1792 carp_set_state(sc, INIT);
1795 } else if (ifp->if_flags & IFF_RUNNING) {
1801 error = suser_cred(cr, NULL_CRED_OKAY);
1804 error = copyin(ifr->ifr_data, &carpr, sizeof(carpr));
1809 if ((ifp->if_flags & IFF_RUNNING) &&
1810 sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1811 switch (carpr.carpr_state) {
1813 callout_stop(&sc->sc_ad_tmo);
1814 carp_set_state(sc, BACKUP);
1816 carp_setroute(sc, RTM_DELETE);
1820 carp_master_down(sc);
1827 if (carpr.carpr_vhid > 0) {
1828 if (carpr.carpr_vhid > 255) {
1832 if (sc->sc_carpdev) {
1833 struct carp_if *cif = sc->sc_carpdev->if_carp;
1835 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1837 vr->sc_vhid == carpr.carpr_vhid)
1841 sc->sc_vhid = carpr.carpr_vhid;
1842 IF_LLADDR(ifp)[0] = 0;
1843 IF_LLADDR(ifp)[1] = 0;
1844 IF_LLADDR(ifp)[2] = 0x5e;
1845 IF_LLADDR(ifp)[3] = 0;
1846 IF_LLADDR(ifp)[4] = 1;
1847 IF_LLADDR(ifp)[5] = sc->sc_vhid;
1850 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1851 if (carpr.carpr_advskew >= 255) {
1855 if (carpr.carpr_advbase > 255) {
1859 sc->sc_advbase = carpr.carpr_advbase;
1860 sc->sc_advskew = carpr.carpr_advskew;
1863 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1873 bzero(&carpr, sizeof(carpr));
1874 carpr.carpr_state = sc->sc_state;
1875 carpr.carpr_vhid = sc->sc_vhid;
1876 carpr.carpr_advbase = sc->sc_advbase;
1877 carpr.carpr_advskew = sc->sc_advskew;
1879 error = suser_cred(cr, NULL_CRED_OKAY);
1881 bcopy(sc->sc_key, carpr.carpr_key,
1882 sizeof(carpr.carpr_key));
1885 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1889 switch (ifd->ifd_cmd) {
1891 if (ifd->ifd_len != sizeof(devname))
1905 switch (ifd->ifd_cmd) {
1907 error = carp_get_vhaddr(sc, ifd);
1911 bzero(devname, sizeof(devname));
1912 if (sc->sc_carpdev != NULL) {
1913 strlcpy(devname, sc->sc_carpdev->if_xname,
1916 error = copyout(devname, ifd->ifd_data,
1926 carp_hmac_prepare(sc);
1931 * XXX: this is looutput. We should eventually use it from there.
1934 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1939 M_ASSERTPKTHDR(m); /* check if we have the packet header */
1941 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1943 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1944 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1948 ifp->if_obytes += m->m_pkthdr.len;
1950 /* BPF writes need to be handled specially. */
1951 if (dst->sa_family == AF_UNSPEC) {
1952 bcopy(dst->sa_data, &af, sizeof(af));
1953 dst->sa_family = af;
1957 switch (dst->sa_family) {
1966 return (EAFNOSUPPORT);
1969 return (if_simloop(ifp, m, dst->sa_family, 0));
1973 * Start output on carp interface. This function should never be called.
1976 carp_start(struct ifnet *ifp)
1979 kprintf("%s: start called\n", ifp->if_xname);
1984 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1988 struct carp_softc *sc;
1989 struct ifnet *carp_ifp;
1990 struct ether_header *eh;
1995 switch (sa->sa_family) {
2008 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2012 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2013 sc = carp_ifp->if_softc;
2015 /* Set the source MAC address to Virtual Router MAC Address */
2016 switch (ifp->if_type) {
2019 eh = mtod(m, struct ether_header *);
2020 eh->ether_shost[0] = 0;
2021 eh->ether_shost[1] = 0;
2022 eh->ether_shost[2] = 0x5e;
2023 eh->ether_shost[3] = 0;
2024 eh->ether_shost[4] = 1;
2025 eh->ether_shost[5] = sc->sc_vhid;
2029 if_printf(ifp, "carp is not supported for this "
2030 "interface type\n");
2031 return (EOPNOTSUPP);
2037 carp_set_state(struct carp_softc *sc, int state)
2039 struct ifnet *cifp = &sc->sc_if;
2041 if (sc->sc_state == state)
2043 sc->sc_state = state;
2045 switch (sc->sc_state) {
2047 cifp->if_link_state = LINK_STATE_DOWN;
2051 cifp->if_link_state = LINK_STATE_UP;
2055 cifp->if_link_state = LINK_STATE_UNKNOWN;
2062 carp_carpdev_state(void *v)
2064 struct carp_if *cif = v;
2065 struct carp_softc *sc;
2067 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2072 carp_sc_state(struct carp_softc *sc)
2074 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2075 callout_stop(&sc->sc_ad_tmo);
2076 callout_stop(&sc->sc_md_tmo);
2077 callout_stop(&sc->sc_md6_tmo);
2078 carp_set_state(sc, INIT);
2080 if (!sc->sc_suppress) {
2081 carp_suppress_preempt++;
2082 if (carp_suppress_preempt == 1)
2085 sc->sc_suppress = 1;
2087 carp_set_state(sc, INIT);
2089 if (sc->sc_suppress)
2090 carp_suppress_preempt--;
2091 sc->sc_suppress = 0;
2096 carp_stop(struct carp_softc *sc, int detach)
2098 sc->sc_if.if_flags &= ~IFF_RUNNING;
2100 callout_stop(&sc->sc_ad_tmo);
2101 callout_stop(&sc->sc_md_tmo);
2102 callout_stop(&sc->sc_md6_tmo);
2104 if (!detach && sc->sc_state == MASTER)
2107 if (sc->sc_suppress)
2108 carp_suppress_preempt--;
2109 sc->sc_suppress = 0;
2111 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2112 carp_suppress_preempt--;
2113 sc->sc_sendad_errors = 0;
2114 sc->sc_sendad_success = 0;
2116 carp_set_state(sc, INIT);
2121 carp_reset(struct carp_softc *sc, int detach)
2123 struct ifnet *cifp = &sc->sc_if;
2125 carp_stop(sc, detach);
2126 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2127 cifp->if_flags |= IFF_RUNNING;
2131 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2132 struct ifnet *ifp, const struct in_ifaddr *ia_if, int own)
2134 struct ip_moptions *imo = &sc->sc_imo;
2135 struct carp_if *cif;
2136 struct carp_softc *vr, *after = NULL;
2142 KKASSERT(vha->vha_ia != NULL);
2144 KASSERT(ia_if != NULL, ("NULL backing address\n"));
2145 KASSERT(vha->vha_iaback == NULL, ("%p is already activated\n", vha));
2146 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2147 ("inactive vhaddr %p is the address owner\n", vha));
2149 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2150 ("%s is already on %s\n", sc->sc_if.if_xname,
2151 sc->sc_carpdev->if_xname));
2153 KASSERT(imo->imo_multicast_ifp == NULL ||
2154 imo->imo_multicast_ifp == ifp,
2155 ("%s didn't leave mcast group on %s\n",
2156 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2158 if (imo->imo_num_memberships == 0) {
2159 struct in_addr addr;
2161 addr.s_addr = htonl(INADDR_CARP_GROUP);
2162 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
2164 imo->imo_num_memberships++;
2165 imo->imo_multicast_ifp = ifp;
2166 imo->imo_multicast_ttl = CARP_DFLTTL;
2167 imo->imo_multicast_loop = 0;
2170 if (!ifp->if_carp) {
2171 KASSERT(sc->sc_carpdev == NULL,
2172 ("%s is already on %s\n", sc->sc_if.if_xname,
2173 sc->sc_carpdev->if_xname));
2175 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2177 error = ifpromisc(ifp, 1);
2183 TAILQ_INIT(&cif->vhif_vrs);
2187 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2188 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
2196 if (sc->sc_carpdev != NULL)
2202 sc->sc_carpdev = ifp;
2206 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2209 if (vr->sc_vhid < sc->sc_vhid)
2214 if (assert_onlist) {
2215 KASSERT(onlist, ("%s is not on %s carp list\n",
2216 sc->sc_if.if_xname, ifp->if_xname));
2218 KASSERT(!onlist, ("%s is already on %s carp list\n",
2219 sc->sc_if.if_xname, ifp->if_xname));
2224 /* We're trying to keep things in order */
2226 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2228 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2231 vha->vha_iaback = ia_if;
2235 vha->vha_flags |= CARP_VHAF_OWNER;
2237 /* XXX save user configured advskew? */
2241 carp_hmac_prepare(sc);
2242 carp_set_state(sc, INIT);
2246 carp_multicast_cleanup(sc);
2251 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
2253 KKASSERT(vha->vha_ia != NULL);
2255 carp_hmac_prepare(sc);
2257 if (vha->vha_iaback == NULL) {
2258 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2259 ("inactive vhaddr %p is the address owner\n", vha));
2263 vha->vha_flags &= ~CARP_VHAF_OWNER;
2265 KKASSERT(sc->sc_naddrs > 0);
2266 vha->vha_iaback = NULL;
2268 if (!sc->sc_naddrs) {
2269 if (sc->sc_naddrs6) {
2270 carp_multicast_cleanup(sc);
2279 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2281 struct carp_vhaddr *vha;
2282 struct in_ifaddr *ia_if;
2284 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2285 ia_if = ifatoia(ifa_if);
2287 if ((ia_if->ia_flags & IFA_ROUTE) == 0)
2291 * Test each inactive vhaddr against the newly added address.
2292 * If the newly added address could be the backing address,
2293 * then activate the matching vhaddr.
2295 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2296 const struct in_ifaddr *ia;
2300 if (vha->vha_iaback != NULL)
2304 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr);
2306 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet)
2310 if (ia->ia_addr.sin_addr.s_addr ==
2311 ia_if->ia_addr.sin_addr.s_addr)
2314 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2319 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
2320 struct ifaddr *ifa_if)
2322 struct carp_vhaddr *vha;
2323 struct in_ifaddr *ia_if;
2325 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2326 ia_if = ifatoia(ifa_if);
2329 * Ad src address is deleted; set it to NULL.
2330 * Following loop will try pick up a new ad src address
2331 * if one of the vhaddr could retain its backing address.
2333 if (sc->sc_ia == ia_if)
2337 * Test each active vhaddr against the deleted address.
2338 * If the deleted address is vhaddr address's backing
2339 * address, then deactivate the vhaddr.
2341 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2342 if (vha->vha_iaback == NULL)
2345 if (vha->vha_iaback == ia_if)
2346 carp_deactivate_vhaddr(sc, vha);
2347 else if (sc->sc_ia == NULL)
2348 sc->sc_ia = vha->vha_iaback;
2353 carp_update_addrs(struct carp_softc *sc)
2355 struct carp_vhaddr *vha;
2357 KKASSERT(sc->sc_carpdev == NULL);
2359 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2360 carp_config_vhaddr(sc, vha);
2364 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
2365 enum ifaddr_event event, struct ifaddr *ifa)
2367 struct carp_softc *sc;
2369 if (ifa->ifa_addr->sa_family != AF_INET)
2372 if (ifp->if_type == IFT_CARP) {
2374 * Address is changed on carp(4) interface
2377 case IFADDR_EVENT_ADD:
2378 carp_add_addr(ifp->if_softc, ifa);
2381 case IFADDR_EVENT_CHANGE:
2382 carp_config_addr(ifp->if_softc, ifa);
2385 case IFADDR_EVENT_DELETE:
2386 carp_del_addr(ifp->if_softc, ifa);
2393 * Address is changed on non-carp(4) interface
2395 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2399 LIST_FOREACH(sc, &carpif_list, sc_next) {
2400 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
2401 /* Not the parent iface; skip */
2406 case IFADDR_EVENT_ADD:
2407 carp_link_addrs(sc, ifp, ifa);
2410 case IFADDR_EVENT_DELETE:
2411 if (sc->sc_carpdev != NULL) {
2412 carp_unlink_addrs(sc, ifp, ifa);
2413 if (sc->sc_carpdev == NULL)
2414 carp_update_addrs(sc);
2417 * The carp(4) interface didn't have a
2418 * parent iface, so it is not possible
2419 * that it will contain any address to
2425 case IFADDR_EVENT_CHANGE:
2426 if (sc->sc_carpdev == NULL) {
2428 * The carp(4) interface didn't have a
2429 * parent iface, so it is not possible
2430 * that it will contain any address to
2433 carp_link_addrs(sc, ifp, ifa);
2436 * First try breaking tie with the old
2437 * address. Then see whether we could
2438 * link certain vhaddr to the new address.
2439 * If that fails, i.e. carpdev is NULL,
2440 * we try a global update.
2442 * NOTE: The above order is critical.
2444 carp_unlink_addrs(sc, ifp, ifa);
2445 carp_link_addrs(sc, ifp, ifa);
2446 if (sc->sc_carpdev == NULL)
2447 carp_update_addrs(sc);
2456 carp_modevent(module_t mod, int type, void *data)
2460 LIST_INIT(&carpif_list);
2461 carp_ifdetach_event =
2462 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
2463 EVENTHANDLER_PRI_ANY);
2465 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
2466 EVENTHANDLER_PRI_ANY);
2467 if_clone_attach(&carp_cloner);
2471 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
2472 carp_ifdetach_event);
2473 EVENTHANDLER_DEREGISTER(ifaddr_event,
2475 if_clone_detach(&carp_cloner);
2484 static moduledata_t carp_mod = {
2489 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);