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 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
81 static MALLOC_DEFINE(M_IFNET, "IFNET", "IFNET CARP?");
82 SYSCTL_DECL(_net_inet_carp);
85 struct ifnet *sc_ifp; /* Interface clue */
86 struct ifnet *sc_carpdev; /* parent interface */
87 struct in_ifaddr *sc_ia; /* primary iface address */
88 struct ip_moptions sc_imo;
90 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
91 struct ip6_moptions sc_im6o;
93 TAILQ_ENTRY(carp_softc) sc_list;
95 enum { INIT = 0, BACKUP, MASTER }
101 int sc_sendad_errors;
102 #define CARP_SENDAD_MAX_ERRORS 3
103 int sc_sendad_success;
104 #define CARP_SENDAD_MIN_SUCCESS 3
110 int sc_advbase; /* seconds */
115 #define CARP_HMAC_PAD 64
116 unsigned char sc_key[CARP_KEY_LEN];
117 unsigned char sc_pad[CARP_HMAC_PAD];
120 struct callout sc_ad_tmo; /* advertisement timeout */
121 struct callout sc_md_tmo; /* master down timeout */
122 struct callout sc_md6_tmo; /* master down timeout */
124 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
126 #define SC2IFP(sc) ((sc)->sc_ifp)
129 TAILQ_HEAD(, carp_softc) vhif_vrs;
132 struct ifnet *vhif_ifp;
135 enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
137 int carp_suppress_preempt = 0;
138 int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
139 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
140 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
141 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
142 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
143 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
144 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
145 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
146 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
147 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
148 &carp_suppress_preempt, 0, "Preemption is suppressed");
150 struct carpstats carpstats;
151 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
152 &carpstats, carpstats,
153 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
155 /* Get carp_if from softc. Valid after carp_set_addr{,6}. */
156 #define SC2CIF(sc) ((struct carp_if *)(sc)->sc_carpdev->if_carp)
158 #define CARP_LOG(...) do { \
159 if (carp_opts[CARPCTL_LOG] > 0) \
160 log(LOG_INFO, __VA_ARGS__); \
163 #define CARP_DEBUG(...) do { \
164 if (carp_opts[CARPCTL_LOG] > 1) \
165 log(LOG_DEBUG, __VA_ARGS__); \
168 static void carp_hmac_prepare(struct carp_softc *);
169 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
171 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
173 static void carp_setroute(struct carp_softc *, int);
174 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
175 static int carp_clone_create(struct if_clone *, int);
176 static void carp_clone_destroy(struct ifnet *);
177 static void carpdetach(struct carp_softc *, int);
178 static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
179 struct carp_header *);
180 static void carp_send_ad_all(void);
181 static void carp_send_ad(void *);
182 static void carp_send_ad_locked(struct carp_softc *);
183 static void carp_send_arp(struct carp_softc *);
184 static void carp_master_down(void *);
185 static void carp_master_down_locked(struct carp_softc *);
186 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
187 static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
189 static void carp_start(struct ifnet *);
190 static void carp_setrun(struct carp_softc *, sa_family_t);
191 static void carp_set_state(struct carp_softc *, int);
192 static int carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
194 static void carp_multicast_cleanup(struct carp_softc *);
195 static int carp_set_addr(struct carp_softc *, struct sockaddr_in *);
196 static int carp_del_addr(struct carp_softc *, struct sockaddr_in *);
197 static void carp_carpdev_state_locked(struct carp_if *);
198 static void carp_sc_state_locked(struct carp_softc *);
200 static void carp_send_na(struct carp_softc *);
201 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
202 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
203 static void carp_multicast6_cleanup(struct carp_softc *);
206 static LIST_HEAD(, carp_softc) carpif_list;
208 static struct if_clone carp_cloner =
209 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
212 static eventhandler_tag carp_ifdetach_event;
214 static __inline uint16_t
215 carp_cksum(struct mbuf *m, int len)
217 return (in_cksum(m, len));
221 carp_hmac_prepare(struct carp_softc *sc)
223 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
224 uint8_t vhid = sc->sc_vhid & 0xff;
225 struct ifaddr_container *ifac;
231 /* XXX: possible race here */
233 /* compute ipad from key */
234 bzero(sc->sc_pad, sizeof(sc->sc_pad));
235 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
236 for (i = 0; i < sizeof(sc->sc_pad); i++)
237 sc->sc_pad[i] ^= 0x36;
239 /* precompute first part of inner hash */
240 SHA1Init(&sc->sc_sha1);
241 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
242 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
243 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
244 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
246 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
247 struct ifaddr *ifa = ifac->ifa;
249 if (ifa->ifa_addr->sa_family == AF_INET)
250 SHA1Update(&sc->sc_sha1,
251 (void *)&ifatoia(ifa)->ia_addr.sin_addr.s_addr,
252 sizeof(struct in_addr));
256 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
257 struct ifaddr *ifa = ifac->ifa;
259 if (ifa->ifa_addr->sa_family == AF_INET6) {
260 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
261 in6_clearscope(&in6);
262 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
267 /* convert ipad to opad */
268 for (i = 0; i < sizeof(sc->sc_pad); i++)
269 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
273 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
274 unsigned char md[20])
278 /* fetch first half of inner hash */
279 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
281 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
282 SHA1Final(md, &sha1ctx);
286 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
287 SHA1Update(&sha1ctx, md, 20);
288 SHA1Final(md, &sha1ctx);
292 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
293 unsigned char md[20])
295 unsigned char md2[20];
297 carp_hmac_generate(sc, counter, md2);
299 return (bcmp(md, md2, sizeof(md2)));
303 carp_setroute(struct carp_softc *sc, int cmd)
305 struct ifaddr_container *ifac;
308 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
309 struct ifaddr *ifa = ifac->ifa;
311 if (ifa->ifa_addr->sa_family == AF_INET &&
312 sc->sc_carpdev != NULL) {
313 int count = carp_addrcount(
314 (struct carp_if *)sc->sc_carpdev->if_carp,
315 ifatoia(ifa), CARP_COUNT_MASTER);
317 if ((cmd == RTM_ADD && count == 1) ||
318 (cmd == RTM_DELETE && count == 0))
319 rtinit(ifa, cmd, RTF_UP | RTF_HOST);
322 if (ifa->ifa_addr->sa_family == AF_INET6) {
335 carp_clone_create(struct if_clone *ifc, int unit)
338 struct carp_softc *sc;
341 MALLOC(sc, struct carp_softc *, sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
342 ifp = SC2IFP(sc) = kmalloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
344 sc->sc_flags_backup = 0;
346 sc->sc_advbase = CARP_DFLTINTV;
347 sc->sc_vhid = -1; /* required setting */
349 sc->sc_init_counter = 1;
350 sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
353 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
356 /* sc->sc_imo.imo_membership = kmalloc((sizeof(struct in_multi) * IP_MAX_MEMBERSHIPS), M_CARP,M_WAITOK);*/
358 sc->sc_imo.imo_max_memberships = IP_MAX_MEMBERSHIPS;
359 sc->sc_imo.imo_multicast_vif = -1;
361 callout_init(&sc->sc_ad_tmo);
362 callout_init(&sc->sc_md_tmo);
363 callout_init(&sc->sc_md6_tmo);
366 if_initname(ifp, CARP_IFNAME, unit);
367 ifp->if_mtu = ETHERMTU;
368 ifp->if_flags = IFF_LOOPBACK;
369 ifp->if_ioctl = carp_ioctl;
370 ifp->if_output = carp_looutput;
371 ifp->if_start = carp_start;
372 ifp->if_type = IFT_CARP;
373 ifp->if_snd.ifq_maxlen = ifqmaxlen;
375 if_attach(ifp, NULL);
376 bpfattach(ifp, DLT_NULL, sizeof(u_int));
379 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
386 carp_clone_destroy(struct ifnet *ifp)
388 struct carp_softc *sc = ifp->if_softc;
390 carpdetach(sc, 1); /* Returns unlocked. */
393 LIST_REMOVE(sc, sc_next);
397 /* if_free_type(ifp, IFT_ETHER);*/
398 /* kfree(sc->sc_imo.imo_membership, M_CARP); */
403 * This function can be called on CARP interface destroy path,
404 * and in case of the removal of the underlying interface as
405 * well. We differentiate these two cases. In the latter case
406 * we do not cleanup our multicast memberships, since they
407 * are already freed. Also, in the latter case we do not
408 * release the lock on return, because the function will be
409 * called once more, for another CARP instance on the same
413 carpdetach(struct carp_softc *sc, int unlock)
417 callout_stop(&sc->sc_ad_tmo);
418 callout_stop(&sc->sc_md_tmo);
419 callout_stop(&sc->sc_md6_tmo);
422 carp_suppress_preempt--;
425 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
426 carp_suppress_preempt--;
427 sc->sc_sendad_errors = 0;
429 carp_set_state(sc, INIT);
430 SC2IFP(sc)->if_flags &= ~IFF_UP;
433 carp_multicast_cleanup(sc);
435 carp_multicast6_cleanup(sc);
438 if (sc->sc_carpdev != NULL) {
439 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
440 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
441 if (!--cif->vhif_nvrs) {
442 ifpromisc(sc->sc_carpdev, 0);
443 sc->sc_carpdev->if_carp = NULL;
446 sc->sc_carpdev = NULL;
450 /* Detach an interface from the carp. */
452 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
454 struct carp_if *cif = (struct carp_if *)ifp->if_carp;
455 struct carp_softc *sc, *nextsc;
461 * XXX: At the end of for() cycle the lock will be destroyed.
463 for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
464 nextsc = TAILQ_NEXT(sc, sc_list);
470 * process input packet.
471 * we have rearranged checks order compared to the rfc,
472 * but it seems more efficient this way or not possible otherwise.
475 carp_input(struct mbuf *m, ...)
477 struct ip *ip = mtod(m, struct ip *);
478 struct carp_header *ch;
479 int iplen, len, hlen;
483 hlen = __va_arg(ap, int);
486 carpstats.carps_ipackets++;
488 if (!carp_opts[CARPCTL_ALLOW]) {
493 /* check if received on a valid carp interface */
494 if (m->m_pkthdr.rcvif->if_carp == NULL) {
495 carpstats.carps_badif++;
496 CARP_LOG("carp_input: packet received on non-carp "
498 m->m_pkthdr.rcvif->if_xname);
503 /* verify that the IP TTL is 255. */
504 if (ip->ip_ttl != CARP_DFLTTL) {
505 carpstats.carps_badttl++;
506 CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
508 m->m_pkthdr.rcvif->if_xname);
513 iplen = ip->ip_hl << 2;
515 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
516 carpstats.carps_badlen++;
517 CARP_LOG("carp_input: received len %zd < "
518 "sizeof(struct carp_header)\n",
519 m->m_len - sizeof(struct ip));
524 if (iplen + sizeof(*ch) < m->m_len) {
525 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
526 carpstats.carps_hdrops++;
527 CARP_LOG("carp_input: pullup failed\n");
530 ip = mtod(m, struct ip *);
532 ch = (struct carp_header *)((char *)ip + iplen);
535 * verify that the received packet length is
536 * equal to the CARP header
538 len = iplen + sizeof(*ch);
539 if (len > m->m_pkthdr.len) {
540 carpstats.carps_badlen++;
541 CARP_LOG("carp_input: packet too short %d on %s\n",
543 m->m_pkthdr.rcvif->if_xname);
548 if ((m = m_pullup(m, len)) == NULL) {
549 carpstats.carps_hdrops++;
552 ip = mtod(m, struct ip *);
553 ch = (struct carp_header *)((char *)ip + iplen);
555 /* verify the CARP checksum */
557 if (carp_cksum(m, len - iplen)) {
558 carpstats.carps_badsum++;
559 CARP_LOG("carp_input: checksum failed on %s\n",
560 m->m_pkthdr.rcvif->if_xname);
566 carp_input_c(m, ch, AF_INET);
571 carp6_input(struct mbuf **mp, int *offp, int proto)
573 struct mbuf *m = *mp;
574 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
575 struct carp_header *ch;
578 carpstats.carps_ipackets6++;
580 if (!carp_opts[CARPCTL_ALLOW]) {
582 return (IPPROTO_DONE);
585 /* check if received on a valid carp interface */
586 if (m->m_pkthdr.rcvif->if_carp == NULL) {
587 carpstats.carps_badif++;
588 CARP_LOG("carp6_input: packet received on non-carp "
590 m->m_pkthdr.rcvif->if_xname);
592 return (IPPROTO_DONE);
595 /* verify that the IP TTL is 255 */
596 if (ip6->ip6_hlim != CARP_DFLTTL) {
597 carpstats.carps_badttl++;
598 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
600 m->m_pkthdr.rcvif->if_xname);
602 return (IPPROTO_DONE);
605 /* verify that we have a complete carp packet */
607 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
609 carpstats.carps_badlen++;
610 CARP_LOG("carp6_input: packet size %u too small\n", len);
611 return (IPPROTO_DONE);
615 /* verify the CARP checksum */
617 if (carp_cksum(m, sizeof(*ch))) {
618 carpstats.carps_badsum++;
619 CARP_LOG("carp6_input: checksum failed, on %s\n",
620 m->m_pkthdr.rcvif->if_xname);
622 return (IPPROTO_DONE);
626 carp_input_c(m, ch, AF_INET6);
627 return (IPPROTO_DONE);
632 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
634 struct ifnet *ifp = m->m_pkthdr.rcvif;
635 struct carp_softc *sc;
636 uint64_t tmp_counter;
637 struct timeval sc_tv, ch_tv;
639 /* verify that the VHID is valid on the receiving interface */
640 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
641 if (sc->sc_vhid == ch->carp_vhid)
644 if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) && (SC2IFP(sc)->if_flags & IFF_RUNNING))) {
645 carpstats.carps_badvhid++;
650 getmicrotime(&SC2IFP(sc)->if_lastchange);
651 SC2IFP(sc)->if_ipackets++;
652 SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
654 if (SC2IFP(sc)->if_bpf) {
655 struct ip *ip = mtod(m, struct ip *);
657 /* BPF wants net byte order */
658 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
659 ip->ip_off = htons(ip->ip_off);
660 bpf_mtap(SC2IFP(sc)->if_bpf, m);
663 /* verify the CARP version. */
664 if (ch->carp_version != CARP_VERSION) {
665 carpstats.carps_badver++;
666 SC2IFP(sc)->if_ierrors++;
667 CARP_LOG("%s; invalid version %d\n",
668 SC2IFP(sc)->if_xname,
674 /* verify the hash */
675 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
676 carpstats.carps_badauth++;
677 SC2IFP(sc)->if_ierrors++;
678 CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
683 tmp_counter = ntohl(ch->carp_counter[0]);
684 tmp_counter = tmp_counter<<32;
685 tmp_counter += ntohl(ch->carp_counter[1]);
687 /* XXX Replay protection goes here */
689 sc->sc_init_counter = 0;
690 sc->sc_counter = tmp_counter;
692 sc_tv.tv_sec = sc->sc_advbase;
693 if (carp_suppress_preempt && sc->sc_advskew < 240)
694 sc_tv.tv_usec = 240 * 1000000 / 256;
696 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
697 ch_tv.tv_sec = ch->carp_advbase;
698 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
700 switch (sc->sc_state) {
705 * If we receive an advertisement from a master who's going to
706 * be more frequent than us, go into BACKUP state.
708 if (timevalcmp(&sc_tv, &ch_tv, >) ||
709 timevalcmp(&sc_tv, &ch_tv, ==)) {
710 callout_stop(&sc->sc_ad_tmo);
711 CARP_DEBUG("%s: MASTER -> BACKUP "
712 "(more frequent advertisement received)\n",
713 SC2IFP(sc)->if_xname);
714 carp_set_state(sc, BACKUP);
716 carp_setroute(sc, RTM_DELETE);
721 * If we're pre-empting masters who advertise slower than us,
722 * and this one claims to be slower, treat him as down.
724 if (carp_opts[CARPCTL_PREEMPT] &&
725 timevalcmp(&sc_tv, &ch_tv, <)) {
726 CARP_DEBUG("%s: BACKUP -> MASTER "
727 "(preempting a slower master)\n",
728 SC2IFP(sc)->if_xname);
729 carp_master_down_locked(sc);
734 * If the master is going to advertise at such a low frequency
735 * that he's guaranteed to time out, we'd might as well just
736 * treat him as timed out now.
738 sc_tv.tv_sec = sc->sc_advbase * 3;
739 if (timevalcmp(&sc_tv, &ch_tv, <)) {
740 CARP_DEBUG("%s: BACKUP -> MASTER "
741 "(master timed out)\n",
742 SC2IFP(sc)->if_xname);
743 carp_master_down_locked(sc);
748 * Otherwise, we reset the counter and wait for the next
759 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
762 struct ifnet *ifp = SC2IFP(sc);
764 if (sc->sc_init_counter) {
765 /* this could also be seconds since unix epoch */
766 sc->sc_counter = karc4random();
767 sc->sc_counter = sc->sc_counter << 32;
768 sc->sc_counter += karc4random();
772 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
773 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
775 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
777 /* Tag packet for carp_output */
778 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), MB_DONTWAIT);
781 SC2IFP(sc)->if_oerrors++;
784 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
785 m_tag_prepend(m, mtag);
791 carp_send_ad_all(void)
793 struct carp_softc *sc;
795 LIST_FOREACH(sc, &carpif_list, sc_next) {
796 if (sc->sc_carpdev == NULL)
798 if ((SC2IFP(sc)->if_flags & IFF_UP) && (SC2IFP(sc)->if_flags & IFF_RUNNING) &&
799 sc->sc_state == MASTER)
800 carp_send_ad_locked(sc);
805 carp_send_ad(void *v)
807 struct carp_softc *sc = v;
809 carp_send_ad_locked(sc);
813 carp_send_ad_locked(struct carp_softc *sc)
815 struct carp_header ch;
817 struct carp_header *ch_ptr;
819 int len, advbase, advskew;
822 /* bow out if we've lost our UPness or RUNNINGuiness */
823 if (!((SC2IFP(sc)->if_flags & IFF_UP) && (SC2IFP(sc)->if_flags & IFF_RUNNING))) {
827 advbase = sc->sc_advbase;
828 if (!carp_suppress_preempt || sc->sc_advskew > 240)
829 advskew = sc->sc_advskew;
833 tv.tv_usec = advskew * 1000000 / 256;
836 ch.carp_version = CARP_VERSION;
837 ch.carp_type = CARP_ADVERTISEMENT;
838 ch.carp_vhid = sc->sc_vhid;
839 ch.carp_advbase = advbase;
840 ch.carp_advskew = advskew;
841 ch.carp_authlen = 7; /* XXX DEFINE */
842 ch.carp_pad1 = 0; /* must be zero */
849 MGETHDR(m, M_NOWAIT, MT_HEADER);
851 SC2IFP(sc)->if_oerrors++;
852 carpstats.carps_onomem++;
853 /* XXX maybe less ? */
854 if (advbase != 255 || advskew != 255)
855 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
859 len = sizeof(*ip) + sizeof(ch);
860 m->m_pkthdr.len = len;
861 m->m_pkthdr.rcvif = NULL;
863 MH_ALIGN(m, m->m_len);
864 m->m_flags |= M_MCAST;
865 ip = mtod(m, struct ip *);
866 ip->ip_v = IPVERSION;
867 ip->ip_hl = sizeof(*ip) >> 2;
868 ip->ip_tos = IPTOS_LOWDELAY;
870 ip->ip_id = ip_newid();
872 ip->ip_ttl = CARP_DFLTTL;
873 ip->ip_p = IPPROTO_CARP;
875 ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
876 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
878 ch_ptr = (struct carp_header *)(&ip[1]);
879 bcopy(&ch, ch_ptr, sizeof(ch));
880 if (carp_prepare_ad(m, sc, ch_ptr))
883 m->m_data += sizeof(*ip);
884 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
885 m->m_data -= sizeof(*ip);
887 getmicrotime(&SC2IFP(sc)->if_lastchange);
888 SC2IFP(sc)->if_opackets++;
889 SC2IFP(sc)->if_obytes += len;
890 carpstats.carps_opackets++;
892 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
893 SC2IFP(sc)->if_oerrors++;
894 if (sc->sc_sendad_errors < INT_MAX)
895 sc->sc_sendad_errors++;
896 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
897 carp_suppress_preempt++;
898 if (carp_suppress_preempt == 1) {
902 sc->sc_sendad_success = 0;
904 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
905 if (++sc->sc_sendad_success >=
906 CARP_SENDAD_MIN_SUCCESS) {
907 carp_suppress_preempt--;
908 sc->sc_sendad_errors = 0;
911 sc->sc_sendad_errors = 0;
919 MGETHDR(m, M_NOWAIT, MT_HEADER);
921 SC2IFP(sc)->if_oerrors++;
922 carpstats.carps_onomem++;
923 /* XXX maybe less ? */
924 if (advbase != 255 || advskew != 255)
925 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
929 len = sizeof(*ip6) + sizeof(ch);
930 m->m_pkthdr.len = len;
931 m->m_pkthdr.rcvif = NULL;
933 MH_ALIGN(m, m->m_len);
934 m->m_flags |= M_MCAST;
935 ip6 = mtod(m, struct ip6_hdr *);
936 bzero(ip6, sizeof(*ip6));
937 ip6->ip6_vfc |= IPV6_VERSION;
938 ip6->ip6_hlim = CARP_DFLTTL;
939 ip6->ip6_nxt = IPPROTO_CARP;
940 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
941 sizeof(struct in6_addr));
942 /* set the multicast destination */
944 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
945 ip6->ip6_dst.s6_addr8[15] = 0x12;
946 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
947 SC2IFP(sc)->if_oerrors++;
949 CARP_LOG("%s: in6_setscope failed\n", __func__);
953 ch_ptr = (struct carp_header *)(&ip6[1]);
954 bcopy(&ch, ch_ptr, sizeof(ch));
955 if (carp_prepare_ad(m, sc, ch_ptr))
958 m->m_data += sizeof(*ip6);
959 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
960 m->m_data -= sizeof(*ip6);
962 getmicrotime(&SC2IFP(sc)->if_lastchange);
963 SC2IFP(sc)->if_opackets++;
964 SC2IFP(sc)->if_obytes += len;
965 carpstats.carps_opackets6++;
967 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
968 SC2IFP(sc)->if_oerrors++;
969 if (sc->sc_sendad_errors < INT_MAX)
970 sc->sc_sendad_errors++;
971 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
972 carp_suppress_preempt++;
973 if (carp_suppress_preempt == 1) {
977 sc->sc_sendad_success = 0;
979 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
980 if (++sc->sc_sendad_success >=
981 CARP_SENDAD_MIN_SUCCESS) {
982 carp_suppress_preempt--;
983 sc->sc_sendad_errors = 0;
986 sc->sc_sendad_errors = 0;
991 if (advbase != 255 || advskew != 255)
992 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
998 * Broadcast a gratuitous ARP request containing
999 * the virtual router MAC address for each IP address
1000 * associated with the virtual router.
1003 carp_send_arp(struct carp_softc *sc)
1005 struct ifaddr_container *ifac;
1007 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
1008 struct ifaddr *ifa = ifac->ifa;
1010 if (ifa->ifa_addr->sa_family != AF_INET)
1012 arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1014 DELAY(1000); /* XXX */
1020 carp_send_na(struct carp_softc *sc)
1022 struct ifaddr_container *ifac;
1023 struct in6_addr *in6;
1024 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1026 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
1027 struct ifaddr *ifa = ifac->ifa;
1029 if (ifa->ifa_addr->sa_family != AF_INET6)
1032 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1033 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1034 ND_NA_FLAG_OVERRIDE, 1, NULL);
1035 DELAY(1000); /* XXX */
1041 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1043 struct carp_softc *vh;
1046 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1047 if ((type == CARP_COUNT_RUNNING &&
1048 (SC2IFP(vh)->if_flags & IFF_UP) && (SC2IFP(vh)->if_flags & IFF_RUNNING)) ||
1049 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1050 struct ifaddr_container *ifac;
1052 TAILQ_FOREACH(ifac, &SC2IFP(vh)->if_addrheads[mycpuid],
1054 struct ifaddr *ifa = ifac->ifa;
1056 if (ifa->ifa_addr->sa_family == AF_INET &&
1057 ia->ia_addr.sin_addr.s_addr ==
1058 ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1067 carp_iamatch(void *v, struct in_ifaddr *ia,
1068 struct in_addr *isaddr, uint8_t **enaddr)
1070 struct carp_if *cif = v;
1071 struct carp_softc *vh;
1072 int index, count = 0;
1074 if (carp_opts[CARPCTL_ARPBALANCE]) {
1076 * XXX proof of concept implementation.
1077 * We use the source ip to decide which virtual host should
1078 * handle the request. If we're master of that virtual host,
1079 * then we respond, otherwise, just drop the arp packet on
1082 count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1084 /* should never reach this */
1088 /* this should be a hash, like pf_hash() */
1089 index = ntohl(isaddr->s_addr) % count;
1092 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1093 if ((SC2IFP(vh)->if_flags & IFF_UP) && (SC2IFP(vh)->if_flags & IFF_RUNNING)) {
1094 struct ifaddr_container *ifac;
1096 TAILQ_FOREACH(ifac, &SC2IFP(vh)->if_addrheads[mycpuid], ifa_link) {
1097 struct ifaddr *ifa = ifac->ifa;
1099 if (ifa->ifa_addr->sa_family ==
1101 ia->ia_addr.sin_addr.s_addr ==
1102 ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1103 if (count == index) {
1106 *enaddr = IF_LLADDR(vh->sc_ifp);
1118 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1119 if ((SC2IFP(vh)->if_flags & IFF_UP) && (SC2IFP(vh)->if_flags & IFF_RUNNING) &&
1120 vh->sc_state == MASTER) {
1121 *enaddr = IF_LLADDR(vh->sc_ifp);
1131 carp_iamatch6(void *v, struct in6_addr *taddr)
1133 struct carp_if *cif = v;
1134 struct carp_softc *vh;
1136 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1137 struct ifaddr_container *ifac;
1139 TAILQ_FOREACH(ifac, &SC2IFP(vh)->if_addrheads[mycpuid], ifa_link) {
1140 struct ifaddr *ifa = ifac->ifa;
1142 if (IN6_ARE_ADDR_EQUAL(taddr,
1143 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1144 (SC2IFP(vh)->if_flags & IFF_UP) && (SC2IFP(vh)->if_flags & IFF_RUNNING) &&
1145 vh->sc_state == MASTER) {
1155 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1158 struct carp_if *cif = v;
1159 struct carp_softc *sc;
1161 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1162 struct ifaddr_container *ifac;
1164 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
1165 struct ifaddr *ifa = ifac->ifa;
1167 if (IN6_ARE_ADDR_EQUAL(taddr,
1168 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1169 (SC2IFP(sc)->if_flags & IFF_UP) && (SC2IFP(sc)->if_flags & IFF_RUNNING)) {
1170 struct ifnet *ifp = SC2IFP(sc);
1171 mtag = m_tag_get(PACKET_TAG_CARP,
1172 sizeof(struct ifnet *), MB_DONTWAIT);
1174 /* better a bit than nothing */
1175 return (IF_LLADDR(sc->sc_ifp));
1177 bcopy(&ifp, (caddr_t)(mtag + 1),
1178 sizeof(struct ifnet *));
1179 m_tag_prepend(m, mtag);
1181 return (IF_LLADDR(sc->sc_ifp));
1190 carp_forus(void *v, void *dhost)
1192 struct carp_if *cif = v;
1193 struct carp_softc *vh;
1194 uint8_t *ena = dhost;
1197 * XXX: See here for check on MAC adr is not for virtual use
1201 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1206 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1207 if ((SC2IFP(vh)->if_flags & IFF_UP) && (SC2IFP(vh)->if_flags & IFF_RUNNING) &&
1208 vh->sc_state == MASTER &&
1209 !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1210 return (SC2IFP(vh));
1216 carp_master_down(void *v)
1218 struct carp_softc *sc = v;
1220 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1221 carp_master_down_locked(sc);
1222 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1226 carp_master_down_locked(struct carp_softc *sc)
1228 switch (sc->sc_state) {
1230 kprintf("%s: master_down event in INIT state\n",
1231 SC2IFP(sc)->if_xname);
1236 carp_set_state(sc, MASTER);
1237 carp_send_ad_locked(sc);
1243 carp_setroute(sc, RTM_ADD);
1249 * When in backup state, af indicates whether to reset the master down timer
1250 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1253 carp_setrun(struct carp_softc *sc, sa_family_t af)
1257 if (sc->sc_carpdev == NULL) {
1258 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
1259 carp_set_state(sc, INIT);
1263 if (SC2IFP(sc)->if_flags & IFF_UP &&
1264 sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1265 SC2IFP(sc)->if_flags |= IFF_RUNNING;
1267 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
1268 carp_setroute(sc, RTM_DELETE);
1272 switch (sc->sc_state) {
1274 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1275 carp_send_ad_locked(sc);
1280 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1281 SC2IFP(sc)->if_xname);
1282 carp_set_state(sc, MASTER);
1283 carp_setroute(sc, RTM_ADD);
1285 CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1286 carp_set_state(sc, BACKUP);
1287 carp_setroute(sc, RTM_DELETE);
1292 callout_stop(&sc->sc_ad_tmo);
1293 tv.tv_sec = 3 * sc->sc_advbase;
1294 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1298 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1299 carp_master_down, sc);
1304 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1305 carp_master_down, sc);
1310 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1311 carp_master_down, sc);
1313 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1314 carp_master_down, sc);
1319 tv.tv_sec = sc->sc_advbase;
1320 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1321 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1328 carp_multicast_cleanup(struct carp_softc *sc)
1330 struct ip_moptions *imo = &sc->sc_imo;
1331 uint16_t n = imo->imo_num_memberships;
1333 /* Clean up our own multicast memberships */
1335 if (imo->imo_membership[n] != NULL) {
1336 in_delmulti(imo->imo_membership[n]);
1337 imo->imo_membership[n] = NULL;
1340 imo->imo_num_memberships = 0;
1341 imo->imo_multicast_ifp = NULL;
1346 carp_multicast6_cleanup(struct carp_softc *sc)
1348 struct ip6_moptions *im6o = &sc->sc_im6o;
1350 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1351 struct in6_multi_mship *imm =
1352 LIST_FIRST(&im6o->im6o_memberships);
1354 LIST_REMOVE(imm, i6mm_chain);
1355 in6_leavegroup(imm);
1357 im6o->im6o_multicast_ifp = NULL;
1362 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1365 struct carp_if *cif;
1366 struct in_ifaddr *ia, *ia_if;
1367 struct in_ifaddr_container *iac;
1368 struct ip_moptions *imo = &sc->sc_imo;
1369 struct in_addr addr;
1370 u_long iaddr = htonl(sin->sin_addr.s_addr);
1373 if (sin->sin_addr.s_addr == 0)
1375 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1377 carp_set_state(sc, INIT);
1381 SC2IFP(sc)->if_flags |= IFF_UP;
1386 /* we have to do it by hands to check we won't match on us */
1387 ia_if = NULL; own = 0;
1388 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1391 /* and, yeah, we need a multicast-capable iface too */
1392 if (ia->ia_ifp != SC2IFP(sc) &&
1393 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1394 (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1397 if (sin->sin_addr.s_addr ==
1398 ia->ia_addr.sin_addr.s_addr)
1405 return (EADDRNOTAVAIL);
1410 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1411 (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1412 return (EADDRNOTAVAIL);
1414 if (imo->imo_num_memberships == 0) {
1415 addr.s_addr = htonl(INADDR_CARP_GROUP);
1416 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1418 imo->imo_num_memberships++;
1419 imo->imo_multicast_ifp = ifp;
1420 imo->imo_multicast_ttl = CARP_DFLTTL;
1421 imo->imo_multicast_loop = 0;
1424 if (!ifp->if_carp) {
1426 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1428 if ((error = ifpromisc(ifp, 1))) {
1433 cif->vhif_ifp = ifp;
1434 TAILQ_INIT(&cif->vhif_vrs);
1438 struct carp_softc *vr;
1440 cif = (struct carp_if *)ifp->if_carp;
1441 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1442 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1448 sc->sc_carpdev = ifp;
1450 { /* XXX prevent endless loop if already in queue */
1451 struct carp_softc *vr, *after = NULL;
1453 cif = (struct carp_if *)ifp->if_carp;
1455 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1458 if (vr->sc_vhid < sc->sc_vhid)
1463 /* We're trying to keep things in order */
1464 if (after == NULL) {
1465 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1467 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1474 SC2IFP(sc)->if_flags |= IFF_UP;
1479 carp_sc_state_locked(sc);
1485 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1491 carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1495 if (!--sc->sc_naddrs) {
1496 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1497 struct ip_moptions *imo = &sc->sc_imo;
1499 callout_stop(&sc->sc_ad_tmo);
1500 SC2IFP(sc)->if_flags &= ~IFF_UP;
1501 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
1503 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1504 imo->imo_multicast_ifp = NULL;
1505 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1506 if (!--cif->vhif_nvrs) {
1507 sc->sc_carpdev->if_carp = NULL;
1508 FREE(cif, M_IFADDR);
1517 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1520 struct carp_if *cif;
1521 struct in6_ifaddr *ia, *ia_if;
1522 struct ip6_moptions *im6o = &sc->sc_im6o;
1523 struct in6_multi_mship *imm;
1524 struct in6_addr in6;
1527 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1528 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1529 carp_set_state(sc, INIT);
1531 SC2IFP(sc)->if_flags |= IFF_UP;
1536 /* we have to do it by hands to check we won't match on us */
1537 ia_if = NULL; own = 0;
1538 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1541 for (i = 0; i < 4; i++) {
1542 if ((sin6->sin6_addr.s6_addr32[i] &
1543 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1544 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1545 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1548 /* and, yeah, we need a multicast-capable iface too */
1549 if (ia->ia_ifp != SC2IFP(sc) &&
1550 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1554 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1555 &ia->ia_addr.sin6_addr))
1561 return (EADDRNOTAVAIL);
1565 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1566 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1567 return (EADDRNOTAVAIL);
1569 if (!sc->sc_naddrs6) {
1570 im6o->im6o_multicast_ifp = ifp;
1572 /* join CARP multicast address */
1573 bzero(&in6, sizeof(in6));
1574 in6.s6_addr16[0] = htons(0xff02);
1575 in6.s6_addr8[15] = 0x12;
1576 if (in6_setscope(&in6, ifp, NULL) != 0)
1578 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1580 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1582 /* join solicited multicast address */
1583 bzero(&in6, sizeof(in6));
1584 in6.s6_addr16[0] = htons(0xff02);
1585 in6.s6_addr32[1] = 0;
1586 in6.s6_addr32[2] = htonl(1);
1587 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1588 in6.s6_addr8[12] = 0xff;
1589 if (in6_setscope(&in6, ifp, NULL) != 0)
1591 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1593 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1596 if (!ifp->if_carp) {
1597 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1599 if ((error = ifpromisc(ifp, 1))) {
1604 cif->vhif_ifp = ifp;
1605 TAILQ_INIT(&cif->vhif_vrs);
1609 struct carp_softc *vr;
1611 cif = (struct carp_if *)ifp->if_carp;
1612 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1613 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1619 sc->sc_carpdev = ifp;
1621 { /* XXX prevent endless loop if already in queue */
1622 struct carp_softc *vr, *after = NULL;
1624 cif = (struct carp_if *)ifp->if_carp;
1626 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1629 if (vr->sc_vhid < sc->sc_vhid)
1634 /* We're trying to keep things in order */
1635 if (after == NULL) {
1636 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1638 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1645 SC2IFP(sc)->if_flags |= IFF_UP;
1648 carp_sc_state_locked(sc);
1654 /* clean up multicast memberships */
1655 if (!sc->sc_naddrs6) {
1656 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1657 imm = LIST_FIRST(&im6o->im6o_memberships);
1658 LIST_REMOVE(imm, i6mm_chain);
1659 in6_leavegroup(imm);
1666 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1670 if (!--sc->sc_naddrs6) {
1671 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1672 struct ip6_moptions *im6o = &sc->sc_im6o;
1674 callout_stop(&sc->sc_ad_tmo);
1675 SC2IFP(sc)->if_flags &= ~IFF_UP;
1676 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
1678 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1679 struct in6_multi_mship *imm =
1680 LIST_FIRST(&im6o->im6o_memberships);
1682 LIST_REMOVE(imm, i6mm_chain);
1683 in6_leavegroup(imm);
1685 im6o->im6o_multicast_ifp = NULL;
1686 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1687 if (!--cif->vhif_nvrs) {
1688 sc->sc_carpdev->if_carp = NULL;
1689 FREE(cif, M_IFADDR);
1698 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *creds)
1700 struct carp_softc *sc = ifp->if_softc, *vr;
1701 struct carpreq carpr;
1704 struct ifaliasreq *ifra;
1705 int locked = 0, error = 0;
1707 ifa = (struct ifaddr *)addr;
1708 ifra = (struct ifaliasreq *)addr;
1709 ifr = (struct ifreq *)addr;
1714 switch (ifa->ifa_addr->sa_family) {
1717 SC2IFP(sc)->if_flags |= IFF_UP;
1718 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1719 sizeof(struct sockaddr));
1720 error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1725 SC2IFP(sc)->if_flags |= IFF_UP;
1726 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1730 error = EAFNOSUPPORT;
1736 switch (ifa->ifa_addr->sa_family) {
1739 SC2IFP(sc)->if_flags |= IFF_UP;
1740 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1741 sizeof(struct sockaddr));
1742 error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1747 SC2IFP(sc)->if_flags |= IFF_UP;
1748 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1752 error = EAFNOSUPPORT;
1758 switch (ifa->ifa_addr->sa_family) {
1761 error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1766 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1770 error = EAFNOSUPPORT;
1776 if (sc->sc_carpdev) {
1779 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1780 callout_stop(&sc->sc_ad_tmo);
1781 callout_stop(&sc->sc_md_tmo);
1782 callout_stop(&sc->sc_md6_tmo);
1783 if (sc->sc_state == MASTER)
1784 carp_send_ad_locked(sc);
1785 carp_set_state(sc, INIT);
1787 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1788 SC2IFP(sc)->if_flags |= IFF_UP;
1794 error = suser(curthread);
1797 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1800 if (sc->sc_carpdev) {
1803 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1804 switch (carpr.carpr_state) {
1806 callout_stop(&sc->sc_ad_tmo);
1807 carp_set_state(sc, BACKUP);
1809 carp_setroute(sc, RTM_DELETE);
1812 carp_master_down_locked(sc);
1818 if (carpr.carpr_vhid > 0) {
1819 if (carpr.carpr_vhid > 255) {
1823 if (sc->sc_carpdev) {
1824 struct carp_if *cif;
1825 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1826 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1828 vr->sc_vhid == carpr.carpr_vhid)
1831 sc->sc_vhid = carpr.carpr_vhid;
1832 IF_LLADDR(sc->sc_ifp)[0] = 0;
1833 IF_LLADDR(sc->sc_ifp)[1] = 0;
1834 IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1835 IF_LLADDR(sc->sc_ifp)[3] = 0;
1836 IF_LLADDR(sc->sc_ifp)[4] = 1;
1837 IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1840 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1841 if (carpr.carpr_advskew >= 255) {
1845 if (carpr.carpr_advbase > 255) {
1849 sc->sc_advbase = carpr.carpr_advbase;
1850 sc->sc_advskew = carpr.carpr_advskew;
1853 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1863 /* XXX: lockless read */
1864 bzero(&carpr, sizeof(carpr));
1865 carpr.carpr_state = sc->sc_state;
1866 carpr.carpr_vhid = sc->sc_vhid;
1867 carpr.carpr_advbase = sc->sc_advbase;
1868 carpr.carpr_advskew = sc->sc_advskew;
1869 error = suser(curthread);
1871 bcopy(sc->sc_key, carpr.carpr_key,
1872 sizeof(carpr.carpr_key));
1873 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1880 carp_hmac_prepare(sc);
1886 * XXX: this is looutput. We should eventually use it from there.
1889 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1894 M_ASSERTPKTHDR(m); /* check if we have the packet header */
1896 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1898 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1899 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1903 ifp->if_obytes += m->m_pkthdr.len;
1905 /* BPF writes need to be handled specially. */
1906 if (dst->sa_family == AF_UNSPEC) {
1907 bcopy(dst->sa_data, &af, sizeof(af));
1908 dst->sa_family = af;
1912 switch (dst->sa_family) {
1920 return (EAFNOSUPPORT);
1923 return(if_simloop(ifp, m, dst->sa_family, 0));
1927 * Start output on carp interface. This function should never be called.
1930 carp_start(struct ifnet *ifp)
1933 kprintf("%s: start called\n", ifp->if_xname);
1938 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1942 struct carp_softc *sc;
1943 struct ifnet *carp_ifp;
1948 switch (sa->sa_family) {
1961 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1965 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
1966 sc = carp_ifp->if_softc;
1968 /* Set the source MAC address to Virtual Router MAC Address */
1969 switch (ifp->if_type) {
1972 struct ether_header *eh;
1974 eh = mtod(m, struct ether_header *);
1975 eh->ether_shost[0] = 0;
1976 eh->ether_shost[1] = 0;
1977 eh->ether_shost[2] = 0x5e;
1978 eh->ether_shost[3] = 0;
1979 eh->ether_shost[4] = 1;
1980 eh->ether_shost[5] = sc->sc_vhid;
1984 kprintf("%s: carp is not supported for this interface type\n",
1986 return (EOPNOTSUPP);
1994 carp_set_state(struct carp_softc *sc, int state)
1996 if (sc->sc_state == state)
1999 sc->sc_state = state;
2002 SC2IFP(sc)->if_link_state = LINK_STATE_DOWN;
2005 SC2IFP(sc)->if_link_state = LINK_STATE_UP;
2008 SC2IFP(sc)->if_link_state = LINK_STATE_UNKNOWN;
2011 rt_ifmsg(SC2IFP(sc));
2015 carp_carpdev_state(void *v)
2017 struct carp_if *cif = v;
2019 carp_carpdev_state_locked(cif);
2023 carp_carpdev_state_locked(struct carp_if *cif)
2025 struct carp_softc *sc;
2027 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2028 carp_sc_state_locked(sc);
2032 carp_sc_state_locked(struct carp_softc *sc)
2034 if ( !(sc->sc_carpdev->if_flags & IFF_UP)) {
2035 sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2036 SC2IFP(sc)->if_flags &= ~IFF_UP;
2037 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
2038 callout_stop(&sc->sc_ad_tmo);
2039 callout_stop(&sc->sc_md_tmo);
2040 callout_stop(&sc->sc_md6_tmo);
2041 carp_set_state(sc, INIT);
2043 if (!sc->sc_suppress) {
2044 carp_suppress_preempt++;
2045 if (carp_suppress_preempt == 1) {
2049 sc->sc_suppress = 1;
2051 SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2052 carp_set_state(sc, INIT);
2054 if (sc->sc_suppress)
2055 carp_suppress_preempt--;
2056 sc->sc_suppress = 0;
2063 carp_modevent(module_t mod, int type, void *data)
2067 LIST_INIT(&carpif_list);
2068 carp_ifdetach_event =
2069 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
2070 EVENTHANDLER_PRI_ANY);
2071 if_clone_attach(&carp_cloner);
2075 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
2076 carp_ifdetach_event);
2077 if_clone_detach(&carp_cloner);
2087 static moduledata_t carp_mod = {
2092 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);