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"
83 struct ifnet *sc_ifp; /* compat shim */
84 struct ifnet *sc_carpdev; /* parent interface */
85 struct in_ifaddr *sc_ia; /* primary iface address */
86 struct ip_moptions sc_imo;
88 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
89 struct ip6_moptions sc_im6o;
91 TAILQ_ENTRY(carp_softc) sc_list;
93 enum { INIT = 0, BACKUP, MASTER }
100 #define CARP_SENDAD_MAX_ERRORS 3
101 int sc_sendad_success;
102 #define CARP_SENDAD_MIN_SUCCESS 3
108 int sc_advbase; /* seconds */
113 #define CARP_HMAC_PAD 64
114 unsigned char sc_key[CARP_KEY_LEN];
115 unsigned char sc_pad[CARP_HMAC_PAD];
118 struct callout sc_ad_tmo; /* advertisement timeout */
119 struct callout sc_md_tmo; /* master down timeout */
120 struct callout sc_md6_tmo; /* master down timeout */
122 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
124 #define SC2IFP(sc) ((sc)->sc_ifp)
127 TAILQ_HEAD(, carp_softc) vhif_vrs;
130 struct ifnet *vhif_ifp;
133 enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
135 SYSCTL_DECL(_net_inet_carp);
137 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
138 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
139 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
140 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
141 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
142 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
143 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
144 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
145 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
147 static int carp_suppress_preempt = 0;
148 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
149 &carp_suppress_preempt, 0, "Preemption is suppressed");
151 static struct carpstats carpstats;
152 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
153 &carpstats, carpstats,
154 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
156 /* Get carp_if from softc. Valid after carp_set_addr{,6}. */
157 #define SC2CIF(sc) ((struct carp_if *)(sc)->sc_carpdev->if_carp)
159 #define CARP_LOG(...) do { \
160 if (carp_opts[CARPCTL_LOG] > 0) \
161 log(LOG_INFO, __VA_ARGS__); \
164 #define CARP_DEBUG(...) do { \
165 if (carp_opts[CARPCTL_LOG] > 1) \
166 log(LOG_DEBUG, __VA_ARGS__); \
169 static void carp_hmac_prepare(struct carp_softc *);
170 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
172 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
174 static void carp_setroute(struct carp_softc *, int);
175 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
176 static int carp_clone_create(struct if_clone *, int);
177 static void carp_clone_destroy(struct ifnet *);
178 static void carpdetach(struct carp_softc *, int);
179 static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
180 struct carp_header *);
181 static void carp_send_ad_all(void);
182 static void carp_send_ad(void *);
183 static void carp_send_ad_locked(struct carp_softc *);
184 static void carp_send_arp(struct carp_softc *);
185 static void carp_master_down(void *);
186 static void carp_master_down_locked(struct carp_softc *);
187 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
188 static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
190 static void carp_start(struct ifnet *);
191 static void carp_setrun(struct carp_softc *, sa_family_t);
192 static void carp_set_state(struct carp_softc *, int);
193 static int carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
195 static void carp_multicast_cleanup(struct carp_softc *);
196 static int carp_set_addr(struct carp_softc *, struct sockaddr_in *);
197 static int carp_del_addr(struct carp_softc *, struct sockaddr_in *);
198 static void carp_carpdev_state_locked(struct carp_if *);
199 static void carp_sc_state_locked(struct carp_softc *);
201 static void carp_send_na(struct carp_softc *);
202 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
203 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
204 static void carp_multicast6_cleanup(struct carp_softc *);
207 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
209 static LIST_HEAD(, carp_softc) carpif_list;
211 static struct if_clone carp_cloner =
212 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
215 static eventhandler_tag carp_ifdetach_event;
217 static __inline uint16_t
218 carp_cksum(struct mbuf *m, int len)
220 return (in_cksum(m, len));
224 carp_hmac_prepare(struct carp_softc *sc)
226 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
227 uint8_t vhid = sc->sc_vhid & 0xff;
228 struct ifaddr_container *ifac;
234 /* XXX: possible race here */
236 /* compute ipad from key */
237 bzero(sc->sc_pad, sizeof(sc->sc_pad));
238 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
239 for (i = 0; i < sizeof(sc->sc_pad); i++)
240 sc->sc_pad[i] ^= 0x36;
242 /* precompute first part of inner hash */
243 SHA1Init(&sc->sc_sha1);
244 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
245 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
246 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
247 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
249 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
250 struct ifaddr *ifa = ifac->ifa;
252 if (ifa->ifa_addr->sa_family == AF_INET)
253 SHA1Update(&sc->sc_sha1,
254 (void *)&ifatoia(ifa)->ia_addr.sin_addr.s_addr,
255 sizeof(struct in_addr));
259 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
260 struct ifaddr *ifa = ifac->ifa;
262 if (ifa->ifa_addr->sa_family == AF_INET6) {
263 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
264 in6_clearscope(&in6);
265 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
270 /* convert ipad to opad */
271 for (i = 0; i < sizeof(sc->sc_pad); i++)
272 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
276 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
277 unsigned char md[20])
281 /* fetch first half of inner hash */
282 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
284 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
285 SHA1Final(md, &sha1ctx);
289 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
290 SHA1Update(&sha1ctx, md, 20);
291 SHA1Final(md, &sha1ctx);
295 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
296 unsigned char md[20])
298 unsigned char md2[20];
300 carp_hmac_generate(sc, counter, md2);
301 return (bcmp(md, md2, sizeof(md2)));
305 carp_setroute(struct carp_softc *sc, int cmd)
307 struct ifaddr_container *ifac;
310 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
311 struct ifaddr *ifa = ifac->ifa;
313 if (ifa->ifa_addr->sa_family == AF_INET &&
314 sc->sc_carpdev != NULL) {
315 int count = carp_addrcount(
316 (struct carp_if *)sc->sc_carpdev->if_carp,
317 ifatoia(ifa), CARP_COUNT_MASTER);
319 if ((cmd == RTM_ADD && count == 1) ||
320 (cmd == RTM_DELETE && count == 0))
321 rtinit(ifa, cmd, RTF_UP | RTF_HOST);
324 if (ifa->ifa_addr->sa_family == AF_INET6) {
336 carp_clone_create(struct if_clone *ifc, int unit)
338 struct carp_softc *sc;
341 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
342 ifp = sc->sc_ifp = &sc->sc_if;
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;
353 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
356 callout_init(&sc->sc_ad_tmo);
357 callout_init(&sc->sc_md_tmo);
358 callout_init(&sc->sc_md6_tmo);
361 if_initname(ifp, CARP_IFNAME, unit);
362 ifp->if_mtu = ETHERMTU;
363 ifp->if_flags = IFF_LOOPBACK;
364 ifp->if_ioctl = carp_ioctl;
365 ifp->if_output = carp_looutput;
366 ifp->if_start = carp_start;
367 ifp->if_type = IFT_CARP;
368 ifp->if_snd.ifq_maxlen = ifqmaxlen;
370 if_attach(ifp, NULL);
371 bpfattach(ifp, DLT_NULL, sizeof(u_int));
374 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
381 carp_clone_destroy(struct ifnet *ifp)
383 struct carp_softc *sc = ifp->if_softc;
388 LIST_REMOVE(sc, sc_next);
396 * This function can be called on CARP interface destroy path,
397 * and in case of the removal of the underlying interface as
398 * well. We differentiate these two cases. In the latter case
399 * we do not cleanup our multicast memberships, since they
403 carpdetach(struct carp_softc *sc, int unlock)
407 callout_stop(&sc->sc_ad_tmo);
408 callout_stop(&sc->sc_md_tmo);
409 callout_stop(&sc->sc_md6_tmo);
412 carp_suppress_preempt--;
415 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
416 carp_suppress_preempt--;
417 sc->sc_sendad_errors = 0;
419 carp_set_state(sc, INIT);
420 SC2IFP(sc)->if_flags &= ~IFF_UP;
423 carp_multicast_cleanup(sc);
425 carp_multicast6_cleanup(sc);
428 if (sc->sc_carpdev != NULL) {
429 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
430 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
431 if (!--cif->vhif_nvrs) {
432 ifpromisc(sc->sc_carpdev, 0);
433 sc->sc_carpdev->if_carp = NULL;
436 sc->sc_carpdev = NULL;
440 /* Detach an interface from the carp. */
442 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
444 struct carp_if *cif = (struct carp_if *)ifp->if_carp;
445 struct carp_softc *sc, *nextsc;
450 for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
451 nextsc = TAILQ_NEXT(sc, sc_list);
457 * process input packet.
458 * we have rearranged checks order compared to the rfc,
459 * but it seems more efficient this way or not possible otherwise.
462 carp_input(struct mbuf *m, ...)
464 struct ip *ip = mtod(m, struct ip *);
465 struct carp_header *ch;
466 int iplen, len, hlen;
470 hlen = __va_arg(ap, int);
473 carpstats.carps_ipackets++;
475 if (!carp_opts[CARPCTL_ALLOW]) {
480 /* check if received on a valid carp interface */
481 if (m->m_pkthdr.rcvif->if_carp == NULL) {
482 carpstats.carps_badif++;
483 CARP_LOG("carp_input: packet received on non-carp "
485 m->m_pkthdr.rcvif->if_xname);
490 /* verify that the IP TTL is 255. */
491 if (ip->ip_ttl != CARP_DFLTTL) {
492 carpstats.carps_badttl++;
493 CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
495 m->m_pkthdr.rcvif->if_xname);
500 iplen = ip->ip_hl << 2;
502 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
503 carpstats.carps_badlen++;
504 CARP_LOG("carp_input: received len %zd < "
505 "sizeof(struct carp_header)\n",
506 m->m_len - sizeof(struct ip));
511 if (iplen + sizeof(*ch) < m->m_len) {
512 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
513 carpstats.carps_hdrops++;
514 CARP_LOG("carp_input: pullup failed\n");
517 ip = mtod(m, struct ip *);
519 ch = (struct carp_header *)((char *)ip + iplen);
522 * verify that the received packet length is
523 * equal to the CARP header
525 len = iplen + sizeof(*ch);
526 if (len > m->m_pkthdr.len) {
527 carpstats.carps_badlen++;
528 CARP_LOG("carp_input: packet too short %d on %s\n",
530 m->m_pkthdr.rcvif->if_xname);
535 if ((m = m_pullup(m, len)) == NULL) {
536 carpstats.carps_hdrops++;
539 ip = mtod(m, struct ip *);
540 ch = (struct carp_header *)((char *)ip + iplen);
542 /* verify the CARP checksum */
544 if (carp_cksum(m, len - iplen)) {
545 carpstats.carps_badsum++;
546 CARP_LOG("carp_input: checksum failed on %s\n",
547 m->m_pkthdr.rcvif->if_xname);
553 carp_input_c(m, ch, AF_INET);
558 carp6_input(struct mbuf **mp, int *offp, int proto)
560 struct mbuf *m = *mp;
561 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
562 struct carp_header *ch;
565 carpstats.carps_ipackets6++;
567 if (!carp_opts[CARPCTL_ALLOW]) {
569 return (IPPROTO_DONE);
572 /* check if received on a valid carp interface */
573 if (m->m_pkthdr.rcvif->if_carp == NULL) {
574 carpstats.carps_badif++;
575 CARP_LOG("carp6_input: packet received on non-carp "
577 m->m_pkthdr.rcvif->if_xname);
579 return (IPPROTO_DONE);
582 /* verify that the IP TTL is 255 */
583 if (ip6->ip6_hlim != CARP_DFLTTL) {
584 carpstats.carps_badttl++;
585 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
587 m->m_pkthdr.rcvif->if_xname);
589 return (IPPROTO_DONE);
592 /* verify that we have a complete carp packet */
594 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
596 carpstats.carps_badlen++;
597 CARP_LOG("carp6_input: packet size %u too small\n", len);
598 return (IPPROTO_DONE);
601 /* verify the CARP checksum */
603 if (carp_cksum(m, sizeof(*ch))) {
604 carpstats.carps_badsum++;
605 CARP_LOG("carp6_input: checksum failed, on %s\n",
606 m->m_pkthdr.rcvif->if_xname);
608 return (IPPROTO_DONE);
612 carp_input_c(m, ch, AF_INET6);
613 return (IPPROTO_DONE);
618 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
620 struct ifnet *ifp = m->m_pkthdr.rcvif;
621 struct carp_softc *sc;
622 uint64_t tmp_counter;
623 struct timeval sc_tv, ch_tv;
625 /* verify that the VHID is valid on the receiving interface */
626 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
627 if (sc->sc_vhid == ch->carp_vhid)
631 !((SC2IFP(sc)->if_flags & IFF_UP) &&
632 (SC2IFP(sc)->if_flags & IFF_RUNNING))) {
633 carpstats.carps_badvhid++;
638 getmicrotime(&SC2IFP(sc)->if_lastchange);
639 SC2IFP(sc)->if_ipackets++;
640 SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
642 if (SC2IFP(sc)->if_bpf) {
643 struct ip *ip = mtod(m, struct ip *);
645 /* BPF wants net byte order */
646 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
647 ip->ip_off = htons(ip->ip_off);
648 bpf_mtap(SC2IFP(sc)->if_bpf, m);
651 /* verify the CARP version. */
652 if (ch->carp_version != CARP_VERSION) {
653 carpstats.carps_badver++;
654 SC2IFP(sc)->if_ierrors++;
655 CARP_LOG("%s; invalid version %d\n",
656 SC2IFP(sc)->if_xname,
662 /* verify the hash */
663 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
664 carpstats.carps_badauth++;
665 SC2IFP(sc)->if_ierrors++;
666 CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
671 tmp_counter = ntohl(ch->carp_counter[0]);
672 tmp_counter = tmp_counter<<32;
673 tmp_counter += ntohl(ch->carp_counter[1]);
675 /* XXX Replay protection goes here */
677 sc->sc_init_counter = 0;
678 sc->sc_counter = tmp_counter;
680 sc_tv.tv_sec = sc->sc_advbase;
681 if (carp_suppress_preempt && sc->sc_advskew < 240)
682 sc_tv.tv_usec = 240 * 1000000 / 256;
684 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
685 ch_tv.tv_sec = ch->carp_advbase;
686 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
688 switch (sc->sc_state) {
694 * If we receive an advertisement from a master who's going to
695 * be more frequent than us, go into BACKUP state.
697 if (timevalcmp(&sc_tv, &ch_tv, >) ||
698 timevalcmp(&sc_tv, &ch_tv, ==)) {
699 callout_stop(&sc->sc_ad_tmo);
700 CARP_DEBUG("%s: MASTER -> BACKUP "
701 "(more frequent advertisement received)\n",
702 SC2IFP(sc)->if_xname);
703 carp_set_state(sc, BACKUP);
705 carp_setroute(sc, RTM_DELETE);
711 * If we're pre-empting masters who advertise slower than us,
712 * and this one claims to be slower, treat him as down.
714 if (carp_opts[CARPCTL_PREEMPT] &&
715 timevalcmp(&sc_tv, &ch_tv, <)) {
716 CARP_DEBUG("%s: BACKUP -> MASTER "
717 "(preempting a slower master)\n",
718 SC2IFP(sc)->if_xname);
719 carp_master_down_locked(sc);
724 * If the master is going to advertise at such a low frequency
725 * that he's guaranteed to time out, we'd might as well just
726 * treat him as timed out now.
728 sc_tv.tv_sec = sc->sc_advbase * 3;
729 if (timevalcmp(&sc_tv, &ch_tv, <)) {
730 CARP_DEBUG("%s: BACKUP -> MASTER "
731 "(master timed out)\n",
732 SC2IFP(sc)->if_xname);
733 carp_master_down_locked(sc);
738 * Otherwise, we reset the counter and wait for the next
748 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
751 struct ifnet *ifp = SC2IFP(sc);
753 if (sc->sc_init_counter) {
754 /* this could also be seconds since unix epoch */
755 sc->sc_counter = karc4random();
756 sc->sc_counter = sc->sc_counter << 32;
757 sc->sc_counter += karc4random();
762 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
763 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
765 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
767 /* Tag packet for carp_output */
768 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), MB_DONTWAIT);
771 SC2IFP(sc)->if_oerrors++;
774 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
775 m_tag_prepend(m, mtag);
781 carp_send_ad_all(void)
783 struct carp_softc *sc;
785 LIST_FOREACH(sc, &carpif_list, sc_next) {
786 if (sc->sc_carpdev == NULL)
789 if ((SC2IFP(sc)->if_flags & IFF_UP) &&
790 (SC2IFP(sc)->if_flags & IFF_RUNNING) &&
791 sc->sc_state == MASTER)
792 carp_send_ad_locked(sc);
797 carp_send_ad(void *v)
799 struct carp_softc *sc = v;
801 carp_send_ad_locked(sc);
805 carp_send_ad_locked(struct carp_softc *sc)
807 struct carp_header ch;
809 struct carp_header *ch_ptr;
811 int len, advbase, advskew;
813 /* bow out if we've lost our UPness or RUNNINGuiness */
814 if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
815 (SC2IFP(sc)->if_flags & IFF_RUNNING))) {
819 advbase = sc->sc_advbase;
820 if (!carp_suppress_preempt || sc->sc_advskew > 240)
821 advskew = sc->sc_advskew;
825 tv.tv_usec = advskew * 1000000 / 256;
828 ch.carp_version = CARP_VERSION;
829 ch.carp_type = CARP_ADVERTISEMENT;
830 ch.carp_vhid = sc->sc_vhid;
831 ch.carp_advbase = advbase;
832 ch.carp_advskew = advskew;
833 ch.carp_authlen = 7; /* XXX DEFINE */
834 ch.carp_pad1 = 0; /* must be zero */
841 MGETHDR(m, M_NOWAIT, MT_HEADER);
843 SC2IFP(sc)->if_oerrors++;
844 carpstats.carps_onomem++;
845 /* XXX maybe less ? */
846 if (advbase != 255 || advskew != 255)
847 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
851 len = sizeof(*ip) + sizeof(ch);
852 m->m_pkthdr.len = len;
853 m->m_pkthdr.rcvif = NULL;
855 MH_ALIGN(m, m->m_len);
856 m->m_flags |= M_MCAST;
857 ip = mtod(m, struct ip *);
858 ip->ip_v = IPVERSION;
859 ip->ip_hl = sizeof(*ip) >> 2;
860 ip->ip_tos = IPTOS_LOWDELAY;
862 ip->ip_id = ip_newid();
864 ip->ip_ttl = CARP_DFLTTL;
865 ip->ip_p = IPPROTO_CARP;
867 ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
868 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
870 ch_ptr = (struct carp_header *)(&ip[1]);
871 bcopy(&ch, ch_ptr, sizeof(ch));
872 if (carp_prepare_ad(m, sc, ch_ptr))
875 m->m_data += sizeof(*ip);
876 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
877 m->m_data -= sizeof(*ip);
879 getmicrotime(&SC2IFP(sc)->if_lastchange);
880 SC2IFP(sc)->if_opackets++;
881 SC2IFP(sc)->if_obytes += len;
882 carpstats.carps_opackets++;
884 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
885 SC2IFP(sc)->if_oerrors++;
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, M_NOWAIT, MT_HEADER);
914 SC2IFP(sc)->if_oerrors++;
915 carpstats.carps_onomem++;
916 /* XXX maybe less ? */
917 if (advbase != 255 || advskew != 255)
918 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
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) {
940 SC2IFP(sc)->if_oerrors++;
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))
951 m->m_data += sizeof(*ip6);
952 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
953 m->m_data -= sizeof(*ip6);
955 getmicrotime(&SC2IFP(sc)->if_lastchange);
956 SC2IFP(sc)->if_opackets++;
957 SC2IFP(sc)->if_obytes += len;
958 carpstats.carps_opackets6++;
960 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
961 SC2IFP(sc)->if_oerrors++;
962 if (sc->sc_sendad_errors < INT_MAX)
963 sc->sc_sendad_errors++;
964 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
965 carp_suppress_preempt++;
966 if (carp_suppress_preempt == 1) {
970 sc->sc_sendad_success = 0;
972 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
973 if (++sc->sc_sendad_success >=
974 CARP_SENDAD_MIN_SUCCESS) {
975 carp_suppress_preempt--;
976 sc->sc_sendad_errors = 0;
979 sc->sc_sendad_errors = 0;
985 if (advbase != 255 || advskew != 255)
986 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
991 * Broadcast a gratuitous ARP request containing
992 * the virtual router MAC address for each IP address
993 * associated with the virtual router.
996 carp_send_arp(struct carp_softc *sc)
998 struct ifaddr_container *ifac;
1000 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
1001 struct ifaddr *ifa = ifac->ifa;
1003 if (ifa->ifa_addr->sa_family != AF_INET)
1005 arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1007 DELAY(1000); /* XXX */
1013 carp_send_na(struct carp_softc *sc)
1015 struct ifaddr_container *ifac;
1016 struct in6_addr *in6;
1017 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1019 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid], ifa_link) {
1020 struct ifaddr *ifa = ifac->ifa;
1022 if (ifa->ifa_addr->sa_family != AF_INET6)
1025 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1026 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1027 ND_NA_FLAG_OVERRIDE, 1, NULL);
1028 DELAY(1000); /* XXX */
1034 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1036 struct carp_softc *vh;
1039 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1040 if ((type == CARP_COUNT_RUNNING &&
1041 (SC2IFP(vh)->if_flags & IFF_UP) &&
1042 (SC2IFP(vh)->if_flags & IFF_RUNNING)) ||
1043 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1044 struct ifaddr_container *ifac;
1046 TAILQ_FOREACH(ifac, &SC2IFP(vh)->if_addrheads[mycpuid],
1048 struct ifaddr *ifa = ifac->ifa;
1050 if (ifa->ifa_addr->sa_family == AF_INET &&
1051 ia->ia_addr.sin_addr.s_addr ==
1052 ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1061 carp_iamatch(void *v, struct in_ifaddr *ia,
1062 struct in_addr *isaddr, uint8_t **enaddr)
1064 struct carp_if *cif = v;
1065 struct carp_softc *vh;
1066 int index, count = 0;
1068 if (carp_opts[CARPCTL_ARPBALANCE]) {
1070 * XXX proof of concept implementation.
1071 * We use the source ip to decide which virtual host should
1072 * handle the request. If we're master of that virtual host,
1073 * then we respond, otherwise, just drop the arp packet on
1076 count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1078 /* should never reach this */
1082 /* this should be a hash, like pf_hash() */
1083 index = ntohl(isaddr->s_addr) % count;
1086 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1087 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1088 (SC2IFP(vh)->if_flags & IFF_RUNNING)) {
1089 struct ifaddr_container *ifac;
1092 &SC2IFP(vh)->if_addrheads[mycpuid], ifa_link) {
1093 struct ifaddr *ifa = ifac->ifa;
1095 if (ifa->ifa_addr->sa_family ==
1097 ia->ia_addr.sin_addr.s_addr ==
1098 ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1099 if (count == index) {
1100 if (vh->sc_state == MASTER) {
1101 *enaddr = IF_LLADDR(vh->sc_ifp);
1113 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1114 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1115 (SC2IFP(vh)->if_flags & IFF_RUNNING) &&
1116 vh->sc_state == MASTER) {
1117 *enaddr = IF_LLADDR(vh->sc_ifp);
1127 carp_iamatch6(void *v, struct in6_addr *taddr)
1129 struct carp_if *cif = v;
1130 struct carp_softc *vh;
1132 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1133 struct ifaddr_container *ifac;
1135 TAILQ_FOREACH(ifac, &SC2IFP(vh)->if_addrheads[mycpuid],
1137 struct ifaddr *ifa = ifac->ifa;
1139 if (IN6_ARE_ADDR_EQUAL(taddr,
1140 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1141 (SC2IFP(vh)->if_flags & IFF_UP) &&
1142 (SC2IFP(vh)->if_flags & IFF_RUNNING) &&
1143 vh->sc_state == MASTER) {
1152 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1155 struct carp_if *cif = v;
1156 struct carp_softc *sc;
1158 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1159 struct ifaddr_container *ifac;
1161 TAILQ_FOREACH(ifac, &SC2IFP(sc)->if_addrheads[mycpuid],
1163 struct ifaddr *ifa = ifac->ifa;
1165 if (IN6_ARE_ADDR_EQUAL(taddr,
1166 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1167 (SC2IFP(sc)->if_flags & IFF_UP) &&
1168 (SC2IFP(sc)->if_flags & IFF_RUNNING)) {
1169 struct ifnet *ifp = SC2IFP(sc);
1170 mtag = m_tag_get(PACKET_TAG_CARP,
1171 sizeof(struct ifnet *), MB_DONTWAIT);
1173 /* better a bit than nothing */
1174 return (IF_LLADDR(sc->sc_ifp));
1176 bcopy(&ifp, (caddr_t)(mtag + 1),
1177 sizeof(struct ifnet *));
1178 m_tag_prepend(m, mtag);
1180 return (IF_LLADDR(sc->sc_ifp));
1189 carp_forus(void *v, void *dhost)
1191 struct carp_if *cif = v;
1192 struct carp_softc *vh;
1193 uint8_t *ena = dhost;
1196 * XXX: See here for check on MAC adr is not for virtual use
1200 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1203 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1204 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1205 (SC2IFP(vh)->if_flags & IFF_RUNNING) &&
1206 vh->sc_state == MASTER &&
1207 !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1208 return (SC2IFP(vh));
1215 carp_master_down(void *v)
1217 struct carp_softc *sc = v;
1219 lwkt_serialize_enter(sc->sc_ifp->if_serializer);
1220 carp_master_down_locked(sc);
1221 lwkt_serialize_exit(sc->sc_ifp->if_serializer);
1225 carp_master_down_locked(struct carp_softc *sc)
1227 switch (sc->sc_state) {
1229 kprintf("%s: master_down event in INIT state\n",
1230 SC2IFP(sc)->if_xname);
1237 carp_set_state(sc, MASTER);
1238 carp_send_ad_locked(sc);
1244 carp_setroute(sc, RTM_ADD);
1250 * When in backup state, af indicates whether to reset the master down timer
1251 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1254 carp_setrun(struct carp_softc *sc, sa_family_t af)
1258 if (sc->sc_carpdev == NULL) {
1259 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
1260 carp_set_state(sc, INIT);
1264 if (SC2IFP(sc)->if_flags & IFF_UP &&
1265 sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6)) {
1266 SC2IFP(sc)->if_flags |= IFF_RUNNING;
1268 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
1269 carp_setroute(sc, RTM_DELETE);
1273 switch (sc->sc_state) {
1275 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1276 carp_send_ad_locked(sc);
1281 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1282 SC2IFP(sc)->if_xname);
1283 carp_set_state(sc, MASTER);
1284 carp_setroute(sc, RTM_ADD);
1286 CARP_DEBUG("%s: INIT -> BACKUP\n",
1287 SC2IFP(sc)->if_xname);
1288 carp_set_state(sc, BACKUP);
1289 carp_setroute(sc, RTM_DELETE);
1295 callout_stop(&sc->sc_ad_tmo);
1296 tv.tv_sec = 3 * sc->sc_advbase;
1297 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1301 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1302 carp_master_down, sc);
1307 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1308 carp_master_down, sc);
1313 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1314 carp_master_down, sc);
1316 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1317 carp_master_down, sc);
1323 tv.tv_sec = sc->sc_advbase;
1324 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1325 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1332 carp_multicast_cleanup(struct carp_softc *sc)
1334 struct ip_moptions *imo = &sc->sc_imo;
1335 uint16_t n = imo->imo_num_memberships;
1337 /* Clean up our own multicast memberships */
1339 if (imo->imo_membership[n] != NULL) {
1340 in_delmulti(imo->imo_membership[n]);
1341 imo->imo_membership[n] = NULL;
1344 imo->imo_num_memberships = 0;
1345 imo->imo_multicast_ifp = NULL;
1350 carp_multicast6_cleanup(struct carp_softc *sc)
1352 struct ip6_moptions *im6o = &sc->sc_im6o;
1354 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1355 struct in6_multi_mship *imm =
1356 LIST_FIRST(&im6o->im6o_memberships);
1358 LIST_REMOVE(imm, i6mm_chain);
1359 in6_leavegroup(imm);
1361 im6o->im6o_multicast_ifp = NULL;
1366 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1369 struct carp_if *cif;
1370 struct in_ifaddr *ia, *ia_if;
1371 struct in_ifaddr_container *iac;
1372 struct ip_moptions *imo = &sc->sc_imo;
1373 struct in_addr addr;
1374 u_long iaddr = htonl(sin->sin_addr.s_addr);
1377 if (sin->sin_addr.s_addr == 0) {
1378 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1379 carp_set_state(sc, INIT);
1381 SC2IFP(sc)->if_flags |= IFF_UP;
1385 /* we have to do it by hands to check we won't match on us */
1386 ia_if = NULL; own = 0;
1387 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1390 /* and, yeah, we need a multicast-capable iface too */
1391 if (ia->ia_ifp != SC2IFP(sc) &&
1392 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1393 (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1396 if (sin->sin_addr.s_addr ==
1397 ia->ia_addr.sin_addr.s_addr)
1403 return (EADDRNOTAVAIL);
1408 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1409 (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1410 return (EADDRNOTAVAIL);
1412 if (imo->imo_num_memberships == 0) {
1413 addr.s_addr = htonl(INADDR_CARP_GROUP);
1414 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1416 imo->imo_num_memberships++;
1417 imo->imo_multicast_ifp = ifp;
1418 imo->imo_multicast_ttl = CARP_DFLTTL;
1419 imo->imo_multicast_loop = 0;
1422 if (!ifp->if_carp) {
1423 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1425 if ((error = ifpromisc(ifp, 1))) {
1430 cif->vhif_ifp = ifp;
1431 TAILQ_INIT(&cif->vhif_vrs);
1434 struct carp_softc *vr;
1436 cif = (struct carp_if *)ifp->if_carp;
1437 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1438 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1445 sc->sc_carpdev = ifp;
1447 { /* XXX prevent endless loop if already in queue */
1448 struct carp_softc *vr, *after = NULL;
1450 cif = (struct carp_if *)ifp->if_carp;
1452 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1455 if (vr->sc_vhid < sc->sc_vhid)
1460 /* We're trying to keep things in order */
1462 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1464 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1470 SC2IFP(sc)->if_flags |= IFF_UP;
1474 carp_sc_state_locked(sc);
1480 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1485 carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1489 if (!--sc->sc_naddrs) {
1490 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1491 struct ip_moptions *imo = &sc->sc_imo;
1493 callout_stop(&sc->sc_ad_tmo);
1494 SC2IFP(sc)->if_flags &= ~IFF_UP;
1495 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
1497 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1498 imo->imo_multicast_ifp = NULL;
1499 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1500 if (!--cif->vhif_nvrs) {
1501 sc->sc_carpdev->if_carp = NULL;
1502 FREE(cif, M_IFADDR);
1510 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1513 struct carp_if *cif;
1514 struct in6_ifaddr *ia, *ia_if;
1515 struct ip6_moptions *im6o = &sc->sc_im6o;
1516 struct in6_multi_mship *imm;
1517 struct in6_addr in6;
1520 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1521 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1522 carp_set_state(sc, INIT);
1524 SC2IFP(sc)->if_flags |= IFF_UP;
1529 /* we have to do it by hands to check we won't match on us */
1530 ia_if = NULL; own = 0;
1531 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1534 for (i = 0; i < 4; i++) {
1535 if ((sin6->sin6_addr.s6_addr32[i] &
1536 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1537 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1538 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1541 /* and, yeah, we need a multicast-capable iface too */
1542 if (ia->ia_ifp != SC2IFP(sc) &&
1543 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1547 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1548 &ia->ia_addr.sin6_addr))
1554 return (EADDRNOTAVAIL);
1558 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1559 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1560 return (EADDRNOTAVAIL);
1562 if (!sc->sc_naddrs6) {
1563 im6o->im6o_multicast_ifp = ifp;
1565 /* join CARP multicast address */
1566 bzero(&in6, sizeof(in6));
1567 in6.s6_addr16[0] = htons(0xff02);
1568 in6.s6_addr8[15] = 0x12;
1569 if (in6_setscope(&in6, ifp, NULL) != 0)
1571 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1573 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1575 /* join solicited multicast address */
1576 bzero(&in6, sizeof(in6));
1577 in6.s6_addr16[0] = htons(0xff02);
1578 in6.s6_addr32[1] = 0;
1579 in6.s6_addr32[2] = htonl(1);
1580 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1581 in6.s6_addr8[12] = 0xff;
1582 if (in6_setscope(&in6, ifp, NULL) != 0)
1584 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1586 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1589 if (!ifp->if_carp) {
1590 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1592 if ((error = ifpromisc(ifp, 1))) {
1597 cif->vhif_ifp = ifp;
1598 TAILQ_INIT(&cif->vhif_vrs);
1601 struct carp_softc *vr;
1603 cif = (struct carp_if *)ifp->if_carp;
1604 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1605 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1612 sc->sc_carpdev = ifp;
1614 { /* XXX prevent endless loop if already in queue */
1615 struct carp_softc *vr, *after = NULL;
1617 cif = (struct carp_if *)ifp->if_carp;
1619 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1622 if (vr->sc_vhid < sc->sc_vhid)
1627 /* We're trying to keep things in order */
1629 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1631 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1637 SC2IFP(sc)->if_flags |= IFF_UP;
1640 carp_sc_state_locked(sc);
1646 /* clean up multicast memberships */
1647 if (!sc->sc_naddrs6) {
1648 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1649 imm = LIST_FIRST(&im6o->im6o_memberships);
1650 LIST_REMOVE(imm, i6mm_chain);
1651 in6_leavegroup(imm);
1658 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1662 if (!--sc->sc_naddrs6) {
1663 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1664 struct ip6_moptions *im6o = &sc->sc_im6o;
1666 callout_stop(&sc->sc_ad_tmo);
1667 SC2IFP(sc)->if_flags &= ~IFF_UP;
1668 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
1670 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1671 struct in6_multi_mship *imm =
1672 LIST_FIRST(&im6o->im6o_memberships);
1674 LIST_REMOVE(imm, i6mm_chain);
1675 in6_leavegroup(imm);
1677 im6o->im6o_multicast_ifp = NULL;
1678 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1679 if (!--cif->vhif_nvrs) {
1680 sc->sc_carpdev->if_carp = NULL;
1681 FREE(cif, M_IFADDR);
1689 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
1691 struct carp_softc *sc = ifp->if_softc, *vr;
1692 struct carpreq carpr;
1695 struct ifaliasreq *ifra;
1698 ifa = (struct ifaddr *)addr;
1699 ifra = (struct ifaliasreq *)addr;
1700 ifr = (struct ifreq *)addr;
1704 switch (ifa->ifa_addr->sa_family) {
1707 SC2IFP(sc)->if_flags |= IFF_UP;
1708 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1709 sizeof(struct sockaddr));
1710 error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1715 SC2IFP(sc)->if_flags |= IFF_UP;
1716 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1720 error = EAFNOSUPPORT;
1726 switch (ifa->ifa_addr->sa_family) {
1729 SC2IFP(sc)->if_flags |= IFF_UP;
1730 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1731 sizeof(struct sockaddr));
1732 error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1737 SC2IFP(sc)->if_flags |= IFF_UP;
1738 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1742 error = EAFNOSUPPORT;
1748 switch (ifa->ifa_addr->sa_family) {
1751 error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1756 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1760 error = EAFNOSUPPORT;
1766 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1767 callout_stop(&sc->sc_ad_tmo);
1768 callout_stop(&sc->sc_md_tmo);
1769 callout_stop(&sc->sc_md6_tmo);
1770 if (sc->sc_state == MASTER)
1771 carp_send_ad_locked(sc);
1772 carp_set_state(sc, INIT);
1774 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1775 SC2IFP(sc)->if_flags |= IFF_UP;
1781 error = suser_cred(cr, NULL_CRED_OKAY);
1784 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1787 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1788 switch (carpr.carpr_state) {
1790 callout_stop(&sc->sc_ad_tmo);
1791 carp_set_state(sc, BACKUP);
1793 carp_setroute(sc, RTM_DELETE);
1797 carp_master_down_locked(sc);
1804 if (carpr.carpr_vhid > 0) {
1805 if (carpr.carpr_vhid > 255) {
1809 if (sc->sc_carpdev) {
1810 struct carp_if *cif;
1811 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1812 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1814 vr->sc_vhid == carpr.carpr_vhid)
1818 sc->sc_vhid = carpr.carpr_vhid;
1819 IF_LLADDR(sc->sc_ifp)[0] = 0;
1820 IF_LLADDR(sc->sc_ifp)[1] = 0;
1821 IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1822 IF_LLADDR(sc->sc_ifp)[3] = 0;
1823 IF_LLADDR(sc->sc_ifp)[4] = 1;
1824 IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1827 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1828 if (carpr.carpr_advskew >= 255) {
1832 if (carpr.carpr_advbase > 255) {
1836 sc->sc_advbase = carpr.carpr_advbase;
1837 sc->sc_advskew = carpr.carpr_advskew;
1840 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1850 bzero(&carpr, sizeof(carpr));
1851 carpr.carpr_state = sc->sc_state;
1852 carpr.carpr_vhid = sc->sc_vhid;
1853 carpr.carpr_advbase = sc->sc_advbase;
1854 carpr.carpr_advskew = sc->sc_advskew;
1855 error = suser_cred(cr, NULL_CRED_OKAY);
1857 bcopy(sc->sc_key, carpr.carpr_key,
1858 sizeof(carpr.carpr_key));
1860 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1866 carp_hmac_prepare(sc);
1871 * XXX: this is looutput. We should eventually use it from there.
1874 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1879 M_ASSERTPKTHDR(m); /* check if we have the packet header */
1881 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1883 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1884 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1888 ifp->if_obytes += m->m_pkthdr.len;
1890 /* BPF writes need to be handled specially. */
1891 if (dst->sa_family == AF_UNSPEC) {
1892 bcopy(dst->sa_data, &af, sizeof(af));
1893 dst->sa_family = af;
1897 switch (dst->sa_family) {
1906 return (EAFNOSUPPORT);
1909 return (if_simloop(ifp, m, dst->sa_family, 0));
1913 * Start output on carp interface. This function should never be called.
1916 carp_start(struct ifnet *ifp)
1919 kprintf("%s: start called\n", ifp->if_xname);
1924 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1928 struct carp_softc *sc;
1929 struct ifnet *carp_ifp;
1930 struct ether_header *eh;
1935 switch (sa->sa_family) {
1948 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1952 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
1953 sc = carp_ifp->if_softc;
1955 /* Set the source MAC address to Virtual Router MAC Address */
1956 switch (ifp->if_type) {
1959 eh = mtod(m, struct ether_header *);
1960 eh->ether_shost[0] = 0;
1961 eh->ether_shost[1] = 0;
1962 eh->ether_shost[2] = 0x5e;
1963 eh->ether_shost[3] = 0;
1964 eh->ether_shost[4] = 1;
1965 eh->ether_shost[5] = sc->sc_vhid;
1969 if_printf(ifp, "carp is not supported for this "
1970 "interface type\n");
1971 return (EOPNOTSUPP);
1977 carp_set_state(struct carp_softc *sc, int state)
1979 if (sc->sc_state == state)
1982 sc->sc_state = state;
1985 SC2IFP(sc)->if_link_state = LINK_STATE_DOWN;
1989 SC2IFP(sc)->if_link_state = LINK_STATE_UP;
1993 SC2IFP(sc)->if_link_state = LINK_STATE_UNKNOWN;
1996 rt_ifmsg(SC2IFP(sc));
2000 carp_carpdev_state(void *v)
2002 struct carp_if *cif = v;
2004 carp_carpdev_state_locked(cif);
2008 carp_carpdev_state_locked(struct carp_if *cif)
2010 struct carp_softc *sc;
2012 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2013 carp_sc_state_locked(sc);
2017 carp_sc_state_locked(struct carp_softc *sc)
2019 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2020 sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2021 SC2IFP(sc)->if_flags &= ~IFF_UP;
2022 SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
2023 callout_stop(&sc->sc_ad_tmo);
2024 callout_stop(&sc->sc_md_tmo);
2025 callout_stop(&sc->sc_md6_tmo);
2026 carp_set_state(sc, INIT);
2028 if (!sc->sc_suppress) {
2029 carp_suppress_preempt++;
2030 if (carp_suppress_preempt == 1)
2033 sc->sc_suppress = 1;
2035 SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2036 carp_set_state(sc, INIT);
2038 if (sc->sc_suppress)
2039 carp_suppress_preempt--;
2040 sc->sc_suppress = 0;
2045 carp_modevent(module_t mod, int type, void *data)
2049 LIST_INIT(&carpif_list);
2050 carp_ifdetach_event =
2051 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
2052 EVENTHANDLER_PRI_ANY);
2053 if_clone_attach(&carp_cloner);
2057 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
2058 carp_ifdetach_event);
2059 if_clone_detach(&carp_cloner);
2068 static moduledata_t carp_mod = {
2073 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);