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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include "opt_inet6.h"
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/malloc.h>
43 #include <sys/module.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/signalvar.h>
50 #include <sys/filio.h>
51 #include <sys/sockio.h>
53 #include <sys/socket.h>
54 #include <sys/vnode.h>
55 #include <sys/vimage.h>
57 #include <machine/stdarg.h>
60 #include <net/ethernet.h>
62 #include <net/iso88025.h>
64 #include <net/if_clone.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/if_ether.h>
76 #include <machine/in_cksum.h>
77 #include <netinet/vinet.h>
81 #include <netinet/icmp6.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/scope6_var.h>
85 #include <netinet6/nd6.h>
86 #include <netinet6/vinet6.h>
89 #include <crypto/sha1.h>
90 #include <netinet/ip_carp.h>
92 #define CARP_IFNAME "carp"
93 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
94 SYSCTL_DECL(_net_inet_carp);
97 struct ifnet *sc_ifp; /* Interface clue */
98 struct ifnet *sc_carpdev; /* Pointer to parent interface */
99 struct in_ifaddr *sc_ia; /* primary iface address */
100 struct ip_moptions sc_imo;
102 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
103 struct ip6_moptions sc_im6o;
105 TAILQ_ENTRY(carp_softc) sc_list;
107 enum { INIT = 0, BACKUP, MASTER } sc_state;
112 int sc_sendad_errors;
113 #define CARP_SENDAD_MAX_ERRORS 3
114 int sc_sendad_success;
115 #define CARP_SENDAD_MIN_SUCCESS 3
121 int sc_advbase; /* seconds */
123 u_int64_t sc_counter;
126 #define CARP_HMAC_PAD 64
127 unsigned char sc_key[CARP_KEY_LEN];
128 unsigned char sc_pad[CARP_HMAC_PAD];
131 struct callout sc_ad_tmo; /* advertisement timeout */
132 struct callout sc_md_tmo; /* master down timeout */
133 struct callout sc_md6_tmo; /* master down timeout */
135 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
137 #define SC2IFP(sc) ((sc)->sc_ifp)
139 int carp_suppress_preempt = 0;
140 int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
141 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
142 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
143 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
144 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
145 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
146 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
147 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
148 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
149 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
150 &carp_suppress_preempt, 0, "Preemption is suppressed");
152 struct carpstats carpstats;
153 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
154 &carpstats, carpstats,
155 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
158 TAILQ_HEAD(, carp_softc) vhif_vrs;
161 struct ifnet *vhif_ifp;
165 /* Get carp_if from softc. Valid after carp_set_addr{,6}. */
166 #define SC2CIF(sc) ((struct carp_if *)(sc)->sc_carpdev->if_carp)
168 /* lock per carp_if queue */
169 #define CARP_LOCK_INIT(cif) mtx_init(&(cif)->vhif_mtx, "carp_if", \
171 #define CARP_LOCK_DESTROY(cif) mtx_destroy(&(cif)->vhif_mtx)
172 #define CARP_LOCK_ASSERT(cif) mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
173 #define CARP_LOCK(cif) mtx_lock(&(cif)->vhif_mtx)
174 #define CARP_UNLOCK(cif) mtx_unlock(&(cif)->vhif_mtx)
176 #define CARP_SCLOCK(sc) mtx_lock(&SC2CIF(sc)->vhif_mtx)
177 #define CARP_SCUNLOCK(sc) mtx_unlock(&SC2CIF(sc)->vhif_mtx)
178 #define CARP_SCLOCK_ASSERT(sc) mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
180 #define CARP_LOG(...) do { \
181 if (carp_opts[CARPCTL_LOG] > 0) \
182 log(LOG_INFO, __VA_ARGS__); \
185 #define CARP_DEBUG(...) do { \
186 if (carp_opts[CARPCTL_LOG] > 1) \
187 log(LOG_DEBUG, __VA_ARGS__); \
190 static void carp_hmac_prepare(struct carp_softc *);
191 static void carp_hmac_generate(struct carp_softc *, u_int32_t *,
193 static int carp_hmac_verify(struct carp_softc *, u_int32_t *,
195 static void carp_setroute(struct carp_softc *, int);
196 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
197 static int carp_clone_create(struct if_clone *, int, caddr_t);
198 static void carp_clone_destroy(struct ifnet *);
199 static void carpdetach(struct carp_softc *, int);
200 static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
201 struct carp_header *);
202 static void carp_send_ad_all(void);
203 static void carp_send_ad(void *);
204 static void carp_send_ad_locked(struct carp_softc *);
205 static void carp_send_arp(struct carp_softc *);
206 static void carp_master_down(void *);
207 static void carp_master_down_locked(struct carp_softc *);
208 static int carp_ioctl(struct ifnet *, u_long, caddr_t);
209 static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
211 static void carp_start(struct ifnet *);
212 static void carp_setrun(struct carp_softc *, sa_family_t);
213 static void carp_set_state(struct carp_softc *, int);
214 static int carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
215 enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
217 static void carp_multicast_cleanup(struct carp_softc *);
218 static int carp_set_addr(struct carp_softc *, struct sockaddr_in *);
219 static int carp_del_addr(struct carp_softc *, struct sockaddr_in *);
220 static void carp_carpdev_state_locked(struct carp_if *);
221 static void carp_sc_state_locked(struct carp_softc *);
223 static void carp_send_na(struct carp_softc *);
224 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
225 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
226 static void carp_multicast6_cleanup(struct carp_softc *);
229 static LIST_HEAD(, carp_softc) carpif_list;
230 static struct mtx carp_mtx;
231 IFC_SIMPLE_DECLARE(carp, 0);
233 static eventhandler_tag if_detach_event_tag;
235 static __inline u_int16_t
236 carp_cksum(struct mbuf *m, int len)
238 return (in_cksum(m, len));
242 carp_hmac_prepare(struct carp_softc *sc)
244 u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
245 u_int8_t vhid = sc->sc_vhid & 0xff;
249 struct in_addr last, cur, in;
252 struct in6_addr last6, cur6, in6;
258 /* XXX: possible race here */
260 /* compute ipad from key */
261 bzero(sc->sc_pad, sizeof(sc->sc_pad));
262 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
263 for (i = 0; i < sizeof(sc->sc_pad); i++)
264 sc->sc_pad[i] ^= 0x36;
266 /* precompute first part of inner hash */
267 SHA1Init(&sc->sc_sha1);
268 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
269 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
270 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
271 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
277 cur.s_addr = 0xffffffff;
278 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
279 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
280 if (ifa->ifa_addr->sa_family == AF_INET &&
281 ntohl(in.s_addr) > ntohl(last.s_addr) &&
282 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
283 cur.s_addr = in.s_addr;
288 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
292 memset(&cur6, 0, sizeof(cur6));
296 memset(&cur6, 0xff, sizeof(cur6));
297 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
298 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
299 if (IN6_IS_SCOPE_EMBED(&in6))
300 in6.s6_addr16[1] = 0;
301 if (ifa->ifa_addr->sa_family == AF_INET6 &&
302 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
303 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
309 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
313 /* convert ipad to opad */
314 for (i = 0; i < sizeof(sc->sc_pad); i++)
315 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
322 carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
323 unsigned char md[20])
327 /* fetch first half of inner hash */
328 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
330 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
331 SHA1Final(md, &sha1ctx);
335 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
336 SHA1Update(&sha1ctx, md, 20);
337 SHA1Final(md, &sha1ctx);
341 carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
342 unsigned char md[20])
344 unsigned char md2[20];
346 CARP_SCLOCK_ASSERT(sc);
348 carp_hmac_generate(sc, counter, md2);
350 return (bcmp(md, md2, sizeof(md2)));
354 carp_setroute(struct carp_softc *sc, int cmd)
360 CARP_SCLOCK_ASSERT(sc);
363 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
364 if (ifa->ifa_addr->sa_family == AF_INET &&
365 sc->sc_carpdev != NULL) {
366 int count = carp_addrcount(
367 (struct carp_if *)sc->sc_carpdev->if_carp,
368 ifatoia(ifa), CARP_COUNT_MASTER);
370 if ((cmd == RTM_ADD && count == 1) ||
371 (cmd == RTM_DELETE && count == 0))
372 rtinit(ifa, cmd, RTF_UP | RTF_HOST);
379 carp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
382 struct carp_softc *sc;
385 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
386 ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
392 sc->sc_flags_backup = 0;
394 sc->sc_advbase = CARP_DFLTINTV;
395 sc->sc_vhid = -1; /* required setting */
397 sc->sc_init_counter = 1;
398 sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
400 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
402 sc->sc_imo.imo_membership = (struct in_multi **)malloc(
403 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
405 sc->sc_imo.imo_mfilters = NULL;
406 sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
407 sc->sc_imo.imo_multicast_vif = -1;
409 callout_init(&sc->sc_ad_tmo, CALLOUT_MPSAFE);
410 callout_init(&sc->sc_md_tmo, CALLOUT_MPSAFE);
411 callout_init(&sc->sc_md6_tmo, CALLOUT_MPSAFE);
414 if_initname(ifp, CARP_IFNAME, unit);
415 ifp->if_mtu = ETHERMTU;
416 ifp->if_flags = IFF_LOOPBACK;
417 ifp->if_ioctl = carp_ioctl;
418 ifp->if_output = carp_looutput;
419 ifp->if_start = carp_start;
420 ifp->if_type = IFT_CARP;
421 ifp->if_snd.ifq_maxlen = ifqmaxlen;
424 bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
426 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
427 mtx_unlock(&carp_mtx);
432 carp_clone_destroy(struct ifnet *ifp)
434 struct carp_softc *sc = ifp->if_softc;
438 carpdetach(sc, 1); /* Returns unlocked. */
441 LIST_REMOVE(sc, sc_next);
442 mtx_unlock(&carp_mtx);
445 if_free_type(ifp, IFT_ETHER);
446 free(sc->sc_imo.imo_membership, M_CARP);
451 * This function can be called on CARP interface destroy path,
452 * and in case of the removal of the underlying interface as
453 * well. We differentiate these two cases. In the latter case
454 * we do not cleanup our multicast memberships, since they
455 * are already freed. Also, in the latter case we do not
456 * release the lock on return, because the function will be
457 * called once more, for another CARP instance on the same
461 carpdetach(struct carp_softc *sc, int unlock)
465 callout_stop(&sc->sc_ad_tmo);
466 callout_stop(&sc->sc_md_tmo);
467 callout_stop(&sc->sc_md6_tmo);
470 carp_suppress_preempt--;
473 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
474 carp_suppress_preempt--;
475 sc->sc_sendad_errors = 0;
477 carp_set_state(sc, INIT);
478 SC2IFP(sc)->if_flags &= ~IFF_UP;
481 carp_multicast_cleanup(sc);
483 carp_multicast6_cleanup(sc);
486 if (sc->sc_carpdev != NULL) {
487 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
488 CARP_LOCK_ASSERT(cif);
489 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
490 if (!--cif->vhif_nvrs) {
491 ifpromisc(sc->sc_carpdev, 0);
492 sc->sc_carpdev->if_carp = NULL;
493 CARP_LOCK_DESTROY(cif);
497 sc->sc_carpdev = NULL;
501 /* Detach an interface from the carp. */
503 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
505 struct carp_if *cif = (struct carp_if *)ifp->if_carp;
506 struct carp_softc *sc, *nextsc;
512 * XXX: At the end of for() cycle the lock will be destroyed.
515 for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
516 nextsc = TAILQ_NEXT(sc, sc_list);
522 * process input packet.
523 * we have rearranged checks order compared to the rfc,
524 * but it seems more efficient this way or not possible otherwise.
527 carp_input(struct mbuf *m, int hlen)
529 struct ip *ip = mtod(m, struct ip *);
530 struct carp_header *ch;
533 carpstats.carps_ipackets++;
535 if (!carp_opts[CARPCTL_ALLOW]) {
540 /* check if received on a valid carp interface */
541 if (m->m_pkthdr.rcvif->if_carp == NULL) {
542 carpstats.carps_badif++;
543 CARP_LOG("carp_input: packet received on non-carp "
545 m->m_pkthdr.rcvif->if_xname);
550 /* verify that the IP TTL is 255. */
551 if (ip->ip_ttl != CARP_DFLTTL) {
552 carpstats.carps_badttl++;
553 CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
555 m->m_pkthdr.rcvif->if_xname);
560 iplen = ip->ip_hl << 2;
562 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
563 carpstats.carps_badlen++;
564 CARP_LOG("carp_input: received len %zd < "
565 "sizeof(struct carp_header)\n",
566 m->m_len - sizeof(struct ip));
571 if (iplen + sizeof(*ch) < m->m_len) {
572 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
573 carpstats.carps_hdrops++;
574 CARP_LOG("carp_input: pullup failed\n");
577 ip = mtod(m, struct ip *);
579 ch = (struct carp_header *)((char *)ip + iplen);
582 * verify that the received packet length is
583 * equal to the CARP header
585 len = iplen + sizeof(*ch);
586 if (len > m->m_pkthdr.len) {
587 carpstats.carps_badlen++;
588 CARP_LOG("carp_input: packet too short %d on %s\n",
590 m->m_pkthdr.rcvif->if_xname);
595 if ((m = m_pullup(m, len)) == NULL) {
596 carpstats.carps_hdrops++;
599 ip = mtod(m, struct ip *);
600 ch = (struct carp_header *)((char *)ip + iplen);
602 /* verify the CARP checksum */
604 if (carp_cksum(m, len - iplen)) {
605 carpstats.carps_badsum++;
606 CARP_LOG("carp_input: checksum failed on %s\n",
607 m->m_pkthdr.rcvif->if_xname);
613 carp_input_c(m, ch, AF_INET);
618 carp6_input(struct mbuf **mp, int *offp, int proto)
620 struct mbuf *m = *mp;
621 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
622 struct carp_header *ch;
625 carpstats.carps_ipackets6++;
627 if (!carp_opts[CARPCTL_ALLOW]) {
629 return (IPPROTO_DONE);
632 /* check if received on a valid carp interface */
633 if (m->m_pkthdr.rcvif->if_carp == NULL) {
634 carpstats.carps_badif++;
635 CARP_LOG("carp6_input: packet received on non-carp "
637 m->m_pkthdr.rcvif->if_xname);
639 return (IPPROTO_DONE);
642 /* verify that the IP TTL is 255 */
643 if (ip6->ip6_hlim != CARP_DFLTTL) {
644 carpstats.carps_badttl++;
645 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
647 m->m_pkthdr.rcvif->if_xname);
649 return (IPPROTO_DONE);
652 /* verify that we have a complete carp packet */
654 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
656 carpstats.carps_badlen++;
657 CARP_LOG("carp6_input: packet size %u too small\n", len);
658 return (IPPROTO_DONE);
662 /* verify the CARP checksum */
664 if (carp_cksum(m, sizeof(*ch))) {
665 carpstats.carps_badsum++;
666 CARP_LOG("carp6_input: checksum failed, on %s\n",
667 m->m_pkthdr.rcvif->if_xname);
669 return (IPPROTO_DONE);
673 carp_input_c(m, ch, AF_INET6);
674 return (IPPROTO_DONE);
679 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
681 struct ifnet *ifp = m->m_pkthdr.rcvif;
682 struct carp_softc *sc;
683 u_int64_t tmp_counter;
684 struct timeval sc_tv, ch_tv;
686 /* verify that the VHID is valid on the receiving interface */
687 CARP_LOCK(ifp->if_carp);
688 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
689 if (sc->sc_vhid == ch->carp_vhid)
692 if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
693 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
694 carpstats.carps_badvhid++;
695 CARP_UNLOCK(ifp->if_carp);
700 getmicrotime(&SC2IFP(sc)->if_lastchange);
701 SC2IFP(sc)->if_ipackets++;
702 SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
704 if (bpf_peers_present(SC2IFP(sc)->if_bpf)) {
705 struct ip *ip = mtod(m, struct ip *);
708 /* BPF wants net byte order */
709 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
710 ip->ip_off = htons(ip->ip_off);
711 bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
714 /* verify the CARP version. */
715 if (ch->carp_version != CARP_VERSION) {
716 carpstats.carps_badver++;
717 SC2IFP(sc)->if_ierrors++;
718 CARP_UNLOCK(ifp->if_carp);
719 CARP_LOG("%s; invalid version %d\n",
720 SC2IFP(sc)->if_xname,
726 /* verify the hash */
727 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
728 carpstats.carps_badauth++;
729 SC2IFP(sc)->if_ierrors++;
730 CARP_UNLOCK(ifp->if_carp);
731 CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
736 tmp_counter = ntohl(ch->carp_counter[0]);
737 tmp_counter = tmp_counter<<32;
738 tmp_counter += ntohl(ch->carp_counter[1]);
740 /* XXX Replay protection goes here */
742 sc->sc_init_counter = 0;
743 sc->sc_counter = tmp_counter;
745 sc_tv.tv_sec = sc->sc_advbase;
746 if (carp_suppress_preempt && sc->sc_advskew < 240)
747 sc_tv.tv_usec = 240 * 1000000 / 256;
749 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
750 ch_tv.tv_sec = ch->carp_advbase;
751 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
753 switch (sc->sc_state) {
758 * If we receive an advertisement from a master who's going to
759 * be more frequent than us, go into BACKUP state.
761 if (timevalcmp(&sc_tv, &ch_tv, >) ||
762 timevalcmp(&sc_tv, &ch_tv, ==)) {
763 callout_stop(&sc->sc_ad_tmo);
764 CARP_DEBUG("%s: MASTER -> BACKUP "
765 "(more frequent advertisement received)\n",
766 SC2IFP(sc)->if_xname);
767 carp_set_state(sc, BACKUP);
769 carp_setroute(sc, RTM_DELETE);
774 * If we're pre-empting masters who advertise slower than us,
775 * and this one claims to be slower, treat him as down.
777 if (carp_opts[CARPCTL_PREEMPT] &&
778 timevalcmp(&sc_tv, &ch_tv, <)) {
779 CARP_DEBUG("%s: BACKUP -> MASTER "
780 "(preempting a slower master)\n",
781 SC2IFP(sc)->if_xname);
782 carp_master_down_locked(sc);
787 * If the master is going to advertise at such a low frequency
788 * that he's guaranteed to time out, we'd might as well just
789 * treat him as timed out now.
791 sc_tv.tv_sec = sc->sc_advbase * 3;
792 if (timevalcmp(&sc_tv, &ch_tv, <)) {
793 CARP_DEBUG("%s: BACKUP -> MASTER "
794 "(master timed out)\n",
795 SC2IFP(sc)->if_xname);
796 carp_master_down_locked(sc);
801 * Otherwise, we reset the counter and wait for the next
808 CARP_UNLOCK(ifp->if_carp);
815 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
818 struct ifnet *ifp = SC2IFP(sc);
820 if (sc->sc_init_counter) {
821 /* this could also be seconds since unix epoch */
822 sc->sc_counter = arc4random();
823 sc->sc_counter = sc->sc_counter << 32;
824 sc->sc_counter += arc4random();
828 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
829 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
831 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
833 /* Tag packet for carp_output */
834 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
837 SC2IFP(sc)->if_oerrors++;
840 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
841 m_tag_prepend(m, mtag);
847 carp_send_ad_all(void)
849 struct carp_softc *sc;
852 LIST_FOREACH(sc, &carpif_list, sc_next) {
853 if (sc->sc_carpdev == NULL)
856 if ((SC2IFP(sc)->if_flags & IFF_UP) &&
857 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
858 sc->sc_state == MASTER)
859 carp_send_ad_locked(sc);
862 mtx_unlock(&carp_mtx);
866 carp_send_ad(void *v)
868 struct carp_softc *sc = v;
871 carp_send_ad_locked(sc);
876 carp_send_ad_locked(struct carp_softc *sc)
878 struct carp_header ch;
880 struct carp_header *ch_ptr;
882 int len, advbase, advskew;
884 CARP_SCLOCK_ASSERT(sc);
886 /* bow out if we've lost our UPness or RUNNINGuiness */
887 if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
888 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
892 advbase = sc->sc_advbase;
893 if (!carp_suppress_preempt || sc->sc_advskew > 240)
894 advskew = sc->sc_advskew;
898 tv.tv_usec = advskew * 1000000 / 256;
901 ch.carp_version = CARP_VERSION;
902 ch.carp_type = CARP_ADVERTISEMENT;
903 ch.carp_vhid = sc->sc_vhid;
904 ch.carp_advbase = advbase;
905 ch.carp_advskew = advskew;
906 ch.carp_authlen = 7; /* XXX DEFINE */
907 ch.carp_pad1 = 0; /* must be zero */
911 INIT_VNET_INET(curvnet);
915 MGETHDR(m, M_DONTWAIT, MT_HEADER);
917 SC2IFP(sc)->if_oerrors++;
918 carpstats.carps_onomem++;
919 /* XXX maybe less ? */
920 if (advbase != 255 || advskew != 255)
921 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
925 len = sizeof(*ip) + sizeof(ch);
926 m->m_pkthdr.len = len;
927 m->m_pkthdr.rcvif = NULL;
929 MH_ALIGN(m, m->m_len);
930 m->m_flags |= M_MCAST;
931 ip = mtod(m, struct ip *);
932 ip->ip_v = IPVERSION;
933 ip->ip_hl = sizeof(*ip) >> 2;
934 ip->ip_tos = IPTOS_LOWDELAY;
936 ip->ip_id = ip_newid();
938 ip->ip_ttl = CARP_DFLTTL;
939 ip->ip_p = IPPROTO_CARP;
941 ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
942 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
944 ch_ptr = (struct carp_header *)(&ip[1]);
945 bcopy(&ch, ch_ptr, sizeof(ch));
946 if (carp_prepare_ad(m, sc, ch_ptr))
949 m->m_data += sizeof(*ip);
950 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
951 m->m_data -= sizeof(*ip);
953 getmicrotime(&SC2IFP(sc)->if_lastchange);
954 SC2IFP(sc)->if_opackets++;
955 SC2IFP(sc)->if_obytes += len;
956 carpstats.carps_opackets++;
958 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
959 SC2IFP(sc)->if_oerrors++;
960 if (sc->sc_sendad_errors < INT_MAX)
961 sc->sc_sendad_errors++;
962 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
963 carp_suppress_preempt++;
964 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;
987 MGETHDR(m, M_DONTWAIT, MT_HEADER);
989 SC2IFP(sc)->if_oerrors++;
990 carpstats.carps_onomem++;
991 /* XXX maybe less ? */
992 if (advbase != 255 || advskew != 255)
993 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
997 len = sizeof(*ip6) + sizeof(ch);
998 m->m_pkthdr.len = len;
999 m->m_pkthdr.rcvif = NULL;
1001 MH_ALIGN(m, m->m_len);
1002 m->m_flags |= M_MCAST;
1003 ip6 = mtod(m, struct ip6_hdr *);
1004 bzero(ip6, sizeof(*ip6));
1005 ip6->ip6_vfc |= IPV6_VERSION;
1006 ip6->ip6_hlim = CARP_DFLTTL;
1007 ip6->ip6_nxt = IPPROTO_CARP;
1008 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1009 sizeof(struct in6_addr));
1010 /* set the multicast destination */
1012 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1013 ip6->ip6_dst.s6_addr8[15] = 0x12;
1014 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1015 SC2IFP(sc)->if_oerrors++;
1017 CARP_LOG("%s: in6_setscope failed\n", __func__);
1021 ch_ptr = (struct carp_header *)(&ip6[1]);
1022 bcopy(&ch, ch_ptr, sizeof(ch));
1023 if (carp_prepare_ad(m, sc, ch_ptr))
1026 m->m_data += sizeof(*ip6);
1027 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
1028 m->m_data -= sizeof(*ip6);
1030 getmicrotime(&SC2IFP(sc)->if_lastchange);
1031 SC2IFP(sc)->if_opackets++;
1032 SC2IFP(sc)->if_obytes += len;
1033 carpstats.carps_opackets6++;
1035 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1036 SC2IFP(sc)->if_oerrors++;
1037 if (sc->sc_sendad_errors < INT_MAX)
1038 sc->sc_sendad_errors++;
1039 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1040 carp_suppress_preempt++;
1041 if (carp_suppress_preempt == 1) {
1047 sc->sc_sendad_success = 0;
1049 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1050 if (++sc->sc_sendad_success >=
1051 CARP_SENDAD_MIN_SUCCESS) {
1052 carp_suppress_preempt--;
1053 sc->sc_sendad_errors = 0;
1056 sc->sc_sendad_errors = 0;
1061 if (advbase != 255 || advskew != 255)
1062 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1068 * Broadcast a gratuitous ARP request containing
1069 * the virtual router MAC address for each IP address
1070 * associated with the virtual router.
1073 carp_send_arp(struct carp_softc *sc)
1077 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1079 if (ifa->ifa_addr->sa_family != AF_INET)
1082 /* arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
1083 arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1085 DELAY(1000); /* XXX */
1091 carp_send_na(struct carp_softc *sc)
1094 struct in6_addr *in6;
1095 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1097 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1099 if (ifa->ifa_addr->sa_family != AF_INET6)
1102 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1103 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1104 ND_NA_FLAG_OVERRIDE, 1, NULL);
1105 DELAY(1000); /* XXX */
1111 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1113 struct carp_softc *vh;
1117 CARP_LOCK_ASSERT(cif);
1119 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1120 if ((type == CARP_COUNT_RUNNING &&
1121 (SC2IFP(vh)->if_flags & IFF_UP) &&
1122 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1123 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1124 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1126 if (ifa->ifa_addr->sa_family == AF_INET &&
1127 ia->ia_addr.sin_addr.s_addr ==
1128 ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1137 carp_iamatch(void *v, struct in_ifaddr *ia,
1138 struct in_addr *isaddr, u_int8_t **enaddr)
1140 struct carp_if *cif = v;
1141 struct carp_softc *vh;
1142 int index, count = 0;
1147 if (carp_opts[CARPCTL_ARPBALANCE]) {
1149 * XXX proof of concept implementation.
1150 * We use the source ip to decide which virtual host should
1151 * handle the request. If we're master of that virtual host,
1152 * then we respond, otherwise, just drop the arp packet on
1155 count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1157 /* should never reach this */
1162 /* this should be a hash, like pf_hash() */
1163 index = ntohl(isaddr->s_addr) % count;
1166 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1167 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1168 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1169 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1171 if (ifa->ifa_addr->sa_family ==
1173 ia->ia_addr.sin_addr.s_addr ==
1174 ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1175 if (count == index) {
1178 *enaddr = IF_LLADDR(vh->sc_ifp);
1192 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1193 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1194 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1195 ia->ia_ifp == SC2IFP(vh) &&
1196 vh->sc_state == MASTER) {
1197 *enaddr = IF_LLADDR(vh->sc_ifp);
1209 carp_iamatch6(void *v, struct in6_addr *taddr)
1211 struct carp_if *cif = v;
1212 struct carp_softc *vh;
1216 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1217 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1218 if (IN6_ARE_ADDR_EQUAL(taddr,
1219 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1220 (SC2IFP(vh)->if_flags & IFF_UP) &&
1221 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1222 vh->sc_state == MASTER) {
1234 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1237 struct carp_if *cif = v;
1238 struct carp_softc *sc;
1242 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1243 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1244 if (IN6_ARE_ADDR_EQUAL(taddr,
1245 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1246 (SC2IFP(sc)->if_flags & IFF_UP) &&
1247 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1248 struct ifnet *ifp = SC2IFP(sc);
1249 mtag = m_tag_get(PACKET_TAG_CARP,
1250 sizeof(struct ifnet *), M_NOWAIT);
1252 /* better a bit than nothing */
1254 return (IF_LLADDR(sc->sc_ifp));
1256 bcopy(&ifp, (caddr_t)(mtag + 1),
1257 sizeof(struct ifnet *));
1258 m_tag_prepend(m, mtag);
1261 return (IF_LLADDR(sc->sc_ifp));
1272 carp_forus(void *v, void *dhost)
1274 struct carp_if *cif = v;
1275 struct carp_softc *vh;
1276 u_int8_t *ena = dhost;
1278 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1282 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1283 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1284 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1285 vh->sc_state == MASTER &&
1286 !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1288 return (SC2IFP(vh));
1296 carp_master_down(void *v)
1298 struct carp_softc *sc = v;
1301 carp_master_down_locked(sc);
1306 carp_master_down_locked(struct carp_softc *sc)
1309 CARP_SCLOCK_ASSERT(sc);
1311 switch (sc->sc_state) {
1313 printf("%s: master_down event in INIT state\n",
1314 SC2IFP(sc)->if_xname);
1319 carp_set_state(sc, MASTER);
1320 carp_send_ad_locked(sc);
1326 carp_setroute(sc, RTM_ADD);
1332 * When in backup state, af indicates whether to reset the master down timer
1333 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1336 carp_setrun(struct carp_softc *sc, sa_family_t af)
1340 if (sc->sc_carpdev == NULL) {
1341 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1342 carp_set_state(sc, INIT);
1345 CARP_SCLOCK_ASSERT(sc);
1347 if (SC2IFP(sc)->if_flags & IFF_UP &&
1348 sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1349 SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1351 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1352 carp_setroute(sc, RTM_DELETE);
1356 switch (sc->sc_state) {
1358 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1359 carp_send_ad_locked(sc);
1364 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1365 SC2IFP(sc)->if_xname);
1366 carp_set_state(sc, MASTER);
1367 carp_setroute(sc, RTM_ADD);
1369 CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1370 carp_set_state(sc, BACKUP);
1371 carp_setroute(sc, RTM_DELETE);
1376 callout_stop(&sc->sc_ad_tmo);
1377 tv.tv_sec = 3 * sc->sc_advbase;
1378 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1382 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1383 carp_master_down, sc);
1388 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1389 carp_master_down, sc);
1394 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1395 carp_master_down, sc);
1397 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1398 carp_master_down, sc);
1403 tv.tv_sec = sc->sc_advbase;
1404 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1405 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1412 carp_multicast_cleanup(struct carp_softc *sc)
1414 struct ip_moptions *imo = &sc->sc_imo;
1415 u_int16_t n = imo->imo_num_memberships;
1417 /* Clean up our own multicast memberships */
1419 if (imo->imo_membership[n] != NULL) {
1420 in_delmulti(imo->imo_membership[n]);
1421 imo->imo_membership[n] = NULL;
1424 KASSERT(imo->imo_mfilters == NULL,
1425 ("%s: imo_mfilters != NULL", __func__));
1426 imo->imo_num_memberships = 0;
1427 imo->imo_multicast_ifp = NULL;
1432 carp_multicast6_cleanup(struct carp_softc *sc)
1434 struct ip6_moptions *im6o = &sc->sc_im6o;
1436 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1437 struct in6_multi_mship *imm =
1438 LIST_FIRST(&im6o->im6o_memberships);
1440 LIST_REMOVE(imm, i6mm_chain);
1441 in6_leavegroup(imm);
1443 im6o->im6o_multicast_ifp = NULL;
1448 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1450 INIT_VNET_INET(curvnet);
1452 struct carp_if *cif;
1453 struct in_ifaddr *ia, *ia_if;
1454 struct ip_moptions *imo = &sc->sc_imo;
1455 struct in_addr addr;
1456 u_long iaddr = htonl(sin->sin_addr.s_addr);
1459 if (sin->sin_addr.s_addr == 0) {
1460 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1461 carp_set_state(sc, INIT);
1463 SC2IFP(sc)->if_flags |= IFF_UP;
1472 /* we have to do it by hands to check we won't match on us */
1473 ia_if = NULL; own = 0;
1474 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1475 /* and, yeah, we need a multicast-capable iface too */
1476 if (ia->ia_ifp != SC2IFP(sc) &&
1477 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1478 (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1481 if (sin->sin_addr.s_addr ==
1482 ia->ia_addr.sin_addr.s_addr)
1488 return (EADDRNOTAVAIL);
1493 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1494 (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1495 return (EADDRNOTAVAIL);
1497 if (imo->imo_num_memberships == 0) {
1498 addr.s_addr = htonl(INADDR_CARP_GROUP);
1499 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1501 imo->imo_num_memberships++;
1502 imo->imo_multicast_ifp = ifp;
1503 imo->imo_multicast_ttl = CARP_DFLTTL;
1504 imo->imo_multicast_loop = 0;
1507 if (!ifp->if_carp) {
1509 cif = malloc(sizeof(*cif), M_CARP,
1515 if ((error = ifpromisc(ifp, 1))) {
1520 CARP_LOCK_INIT(cif);
1522 cif->vhif_ifp = ifp;
1523 TAILQ_INIT(&cif->vhif_vrs);
1527 struct carp_softc *vr;
1529 cif = (struct carp_if *)ifp->if_carp;
1531 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1532 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1539 sc->sc_carpdev = ifp;
1541 { /* XXX prevent endless loop if already in queue */
1542 struct carp_softc *vr, *after = NULL;
1544 cif = (struct carp_if *)ifp->if_carp;
1546 /* XXX: cif should not change, right? So we still hold the lock */
1547 CARP_LOCK_ASSERT(cif);
1549 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1552 if (vr->sc_vhid < sc->sc_vhid)
1557 /* We're trying to keep things in order */
1558 if (after == NULL) {
1559 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1561 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1568 SC2IFP(sc)->if_flags |= IFF_UP;
1571 carp_sc_state_locked(sc);
1579 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1584 carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1588 if (!--sc->sc_naddrs) {
1589 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1590 struct ip_moptions *imo = &sc->sc_imo;
1593 callout_stop(&sc->sc_ad_tmo);
1594 SC2IFP(sc)->if_flags &= ~IFF_UP;
1595 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1597 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1598 imo->imo_multicast_ifp = NULL;
1599 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1600 if (!--cif->vhif_nvrs) {
1601 sc->sc_carpdev->if_carp = NULL;
1602 CARP_LOCK_DESTROY(cif);
1603 free(cif, M_IFADDR);
1614 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1616 INIT_VNET_INET6(curvnet);
1618 struct carp_if *cif;
1619 struct in6_ifaddr *ia, *ia_if;
1620 struct ip6_moptions *im6o = &sc->sc_im6o;
1621 struct in6_multi_mship *imm;
1622 struct in6_addr in6;
1625 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1626 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1627 carp_set_state(sc, INIT);
1629 SC2IFP(sc)->if_flags |= IFF_UP;
1638 /* we have to do it by hands to check we won't match on us */
1639 ia_if = NULL; own = 0;
1640 for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) {
1643 for (i = 0; i < 4; i++) {
1644 if ((sin6->sin6_addr.s6_addr32[i] &
1645 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1646 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1647 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1650 /* and, yeah, we need a multicast-capable iface too */
1651 if (ia->ia_ifp != SC2IFP(sc) &&
1652 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1656 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1657 &ia->ia_addr.sin6_addr))
1663 return (EADDRNOTAVAIL);
1667 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1668 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1669 return (EADDRNOTAVAIL);
1671 if (!sc->sc_naddrs6) {
1672 im6o->im6o_multicast_ifp = ifp;
1674 /* join CARP multicast address */
1675 bzero(&in6, sizeof(in6));
1676 in6.s6_addr16[0] = htons(0xff02);
1677 in6.s6_addr8[15] = 0x12;
1678 if (in6_setscope(&in6, ifp, NULL) != 0)
1680 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1682 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1684 /* join solicited multicast address */
1685 bzero(&in6, sizeof(in6));
1686 in6.s6_addr16[0] = htons(0xff02);
1687 in6.s6_addr32[1] = 0;
1688 in6.s6_addr32[2] = htonl(1);
1689 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1690 in6.s6_addr8[12] = 0xff;
1691 if (in6_setscope(&in6, ifp, NULL) != 0)
1693 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1695 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1698 if (!ifp->if_carp) {
1699 cif = malloc(sizeof(*cif), M_CARP,
1705 if ((error = ifpromisc(ifp, 1))) {
1710 CARP_LOCK_INIT(cif);
1712 cif->vhif_ifp = ifp;
1713 TAILQ_INIT(&cif->vhif_vrs);
1717 struct carp_softc *vr;
1719 cif = (struct carp_if *)ifp->if_carp;
1721 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1722 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1729 sc->sc_carpdev = ifp;
1731 { /* XXX prevent endless loop if already in queue */
1732 struct carp_softc *vr, *after = NULL;
1734 cif = (struct carp_if *)ifp->if_carp;
1735 CARP_LOCK_ASSERT(cif);
1737 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1740 if (vr->sc_vhid < sc->sc_vhid)
1745 /* We're trying to keep things in order */
1746 if (after == NULL) {
1747 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1749 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1756 SC2IFP(sc)->if_flags |= IFF_UP;
1759 carp_sc_state_locked(sc);
1767 /* clean up multicast memberships */
1768 if (!sc->sc_naddrs6) {
1769 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1770 imm = LIST_FIRST(&im6o->im6o_memberships);
1771 LIST_REMOVE(imm, i6mm_chain);
1772 in6_leavegroup(imm);
1779 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1783 if (!--sc->sc_naddrs6) {
1784 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1785 struct ip6_moptions *im6o = &sc->sc_im6o;
1788 callout_stop(&sc->sc_ad_tmo);
1789 SC2IFP(sc)->if_flags &= ~IFF_UP;
1790 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1792 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1793 struct in6_multi_mship *imm =
1794 LIST_FIRST(&im6o->im6o_memberships);
1796 LIST_REMOVE(imm, i6mm_chain);
1797 in6_leavegroup(imm);
1799 im6o->im6o_multicast_ifp = NULL;
1800 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1801 if (!--cif->vhif_nvrs) {
1802 CARP_LOCK_DESTROY(cif);
1803 sc->sc_carpdev->if_carp = NULL;
1804 free(cif, M_IFADDR);
1814 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1816 struct carp_softc *sc = ifp->if_softc, *vr;
1817 struct carpreq carpr;
1820 struct ifaliasreq *ifra;
1821 int locked = 0, error = 0;
1823 ifa = (struct ifaddr *)addr;
1824 ifra = (struct ifaliasreq *)addr;
1825 ifr = (struct ifreq *)addr;
1829 switch (ifa->ifa_addr->sa_family) {
1832 SC2IFP(sc)->if_flags |= IFF_UP;
1833 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1834 sizeof(struct sockaddr));
1835 error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1840 SC2IFP(sc)->if_flags |= IFF_UP;
1841 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1845 error = EAFNOSUPPORT;
1851 switch (ifa->ifa_addr->sa_family) {
1854 SC2IFP(sc)->if_flags |= IFF_UP;
1855 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1856 sizeof(struct sockaddr));
1857 error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1862 SC2IFP(sc)->if_flags |= IFF_UP;
1863 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1867 error = EAFNOSUPPORT;
1873 switch (ifa->ifa_addr->sa_family) {
1876 error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1881 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1885 error = EAFNOSUPPORT;
1891 if (sc->sc_carpdev) {
1895 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1896 callout_stop(&sc->sc_ad_tmo);
1897 callout_stop(&sc->sc_md_tmo);
1898 callout_stop(&sc->sc_md6_tmo);
1899 if (sc->sc_state == MASTER)
1900 carp_send_ad_locked(sc);
1901 carp_set_state(sc, INIT);
1903 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1904 SC2IFP(sc)->if_flags |= IFF_UP;
1910 error = priv_check(curthread, PRIV_NETINET_CARP);
1913 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1916 if (sc->sc_carpdev) {
1920 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1921 switch (carpr.carpr_state) {
1923 callout_stop(&sc->sc_ad_tmo);
1924 carp_set_state(sc, BACKUP);
1926 carp_setroute(sc, RTM_DELETE);
1929 carp_master_down_locked(sc);
1935 if (carpr.carpr_vhid > 0) {
1936 if (carpr.carpr_vhid > 255) {
1940 if (sc->sc_carpdev) {
1941 struct carp_if *cif;
1942 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1943 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1945 vr->sc_vhid == carpr.carpr_vhid) {
1949 if (error == EEXIST)
1952 sc->sc_vhid = carpr.carpr_vhid;
1953 IF_LLADDR(sc->sc_ifp)[0] = 0;
1954 IF_LLADDR(sc->sc_ifp)[1] = 0;
1955 IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1956 IF_LLADDR(sc->sc_ifp)[3] = 0;
1957 IF_LLADDR(sc->sc_ifp)[4] = 1;
1958 IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1961 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1962 if (carpr.carpr_advskew >= 255) {
1966 if (carpr.carpr_advbase > 255) {
1970 sc->sc_advbase = carpr.carpr_advbase;
1971 sc->sc_advskew = carpr.carpr_advskew;
1974 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1984 /* XXX: lockless read */
1985 bzero(&carpr, sizeof(carpr));
1986 carpr.carpr_state = sc->sc_state;
1987 carpr.carpr_vhid = sc->sc_vhid;
1988 carpr.carpr_advbase = sc->sc_advbase;
1989 carpr.carpr_advskew = sc->sc_advskew;
1990 error = priv_check(curthread, PRIV_NETINET_CARP);
1992 bcopy(sc->sc_key, carpr.carpr_key,
1993 sizeof(carpr.carpr_key));
1994 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
2004 carp_hmac_prepare(sc);
2010 * XXX: this is looutput. We should eventually use it from there.
2013 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2018 M_ASSERTPKTHDR(m); /* check if we have the packet header */
2020 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2022 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
2023 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
2027 ifp->if_obytes += m->m_pkthdr.len;
2029 /* BPF writes need to be handled specially. */
2030 if (dst->sa_family == AF_UNSPEC) {
2031 bcopy(dst->sa_data, &af, sizeof(af));
2032 dst->sa_family = af;
2036 switch (dst->sa_family) {
2043 printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
2045 return (EAFNOSUPPORT);
2048 return(if_simloop(ifp, m, dst->sa_family, 0));
2052 * Start output on carp interface. This function should never be called.
2055 carp_start(struct ifnet *ifp)
2058 printf("%s: start called\n", ifp->if_xname);
2063 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2067 struct carp_softc *sc;
2068 struct ifnet *carp_ifp;
2073 switch (sa->sa_family) {
2086 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2090 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2091 sc = carp_ifp->if_softc;
2093 /* Set the source MAC address to Virtual Router MAC Address */
2094 switch (ifp->if_type) {
2097 struct ether_header *eh;
2099 eh = mtod(m, struct ether_header *);
2100 eh->ether_shost[0] = 0;
2101 eh->ether_shost[1] = 0;
2102 eh->ether_shost[2] = 0x5e;
2103 eh->ether_shost[3] = 0;
2104 eh->ether_shost[4] = 1;
2105 eh->ether_shost[5] = sc->sc_vhid;
2109 struct fddi_header *fh;
2111 fh = mtod(m, struct fddi_header *);
2112 fh->fddi_shost[0] = 0;
2113 fh->fddi_shost[1] = 0;
2114 fh->fddi_shost[2] = 0x5e;
2115 fh->fddi_shost[3] = 0;
2116 fh->fddi_shost[4] = 1;
2117 fh->fddi_shost[5] = sc->sc_vhid;
2120 case IFT_ISO88025: {
2121 struct iso88025_header *th;
2122 th = mtod(m, struct iso88025_header *);
2123 th->iso88025_shost[0] = 3;
2124 th->iso88025_shost[1] = 0;
2125 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2126 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2127 th->iso88025_shost[4] = 0;
2128 th->iso88025_shost[5] = 0;
2132 printf("%s: carp is not supported for this interface type\n",
2134 return (EOPNOTSUPP);
2141 carp_set_state(struct carp_softc *sc, int state)
2146 CARP_SCLOCK_ASSERT(sc);
2148 if (sc->sc_state == state)
2151 sc->sc_state = state;
2154 link_state = LINK_STATE_DOWN;
2157 link_state = LINK_STATE_UP;
2160 link_state = LINK_STATE_UNKNOWN;
2163 if_link_state_change(SC2IFP(sc), link_state);
2167 carp_carpdev_state(void *v)
2169 struct carp_if *cif = v;
2172 carp_carpdev_state_locked(cif);
2177 carp_carpdev_state_locked(struct carp_if *cif)
2179 struct carp_softc *sc;
2181 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2182 carp_sc_state_locked(sc);
2186 carp_sc_state_locked(struct carp_softc *sc)
2188 CARP_SCLOCK_ASSERT(sc);
2190 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2191 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2192 sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2193 SC2IFP(sc)->if_flags &= ~IFF_UP;
2194 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2195 callout_stop(&sc->sc_ad_tmo);
2196 callout_stop(&sc->sc_md_tmo);
2197 callout_stop(&sc->sc_md6_tmo);
2198 carp_set_state(sc, INIT);
2200 if (!sc->sc_suppress) {
2201 carp_suppress_preempt++;
2202 if (carp_suppress_preempt == 1) {
2208 sc->sc_suppress = 1;
2210 SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2211 carp_set_state(sc, INIT);
2213 if (sc->sc_suppress)
2214 carp_suppress_preempt--;
2215 sc->sc_suppress = 0;
2222 carp_modevent(module_t mod, int type, void *data)
2226 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2227 carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
2228 if (if_detach_event_tag == NULL)
2230 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2231 LIST_INIT(&carpif_list);
2232 if_clone_attach(&carp_cloner);
2236 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2237 if_clone_detach(&carp_cloner);
2238 mtx_destroy(&carp_mtx);
2248 static moduledata_t carp_mod = {
2254 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);