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>
80 #include <netinet/icmp6.h>
81 #include <netinet/ip6.h>
82 #include <netinet6/ip6_var.h>
83 #include <netinet6/scope6_var.h>
84 #include <netinet6/nd6.h>
87 #include <crypto/sha1.h>
88 #include <netinet/ip_carp.h>
90 #define CARP_IFNAME "carp"
91 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
92 SYSCTL_DECL(_net_inet_carp);
95 struct ifnet *sc_ifp; /* Interface clue */
96 struct ifnet *sc_carpdev; /* Pointer to parent interface */
97 struct in_ifaddr *sc_ia; /* primary iface address */
98 struct ip_moptions sc_imo;
100 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
101 struct ip6_moptions sc_im6o;
103 TAILQ_ENTRY(carp_softc) sc_list;
105 enum { INIT = 0, BACKUP, MASTER } sc_state;
110 int sc_sendad_errors;
111 #define CARP_SENDAD_MAX_ERRORS 3
112 int sc_sendad_success;
113 #define CARP_SENDAD_MIN_SUCCESS 3
119 int sc_advbase; /* seconds */
121 u_int64_t sc_counter;
124 #define CARP_HMAC_PAD 64
125 unsigned char sc_key[CARP_KEY_LEN];
126 unsigned char sc_pad[CARP_HMAC_PAD];
129 struct callout sc_ad_tmo; /* advertisement timeout */
130 struct callout sc_md_tmo; /* master down timeout */
131 struct callout sc_md6_tmo; /* master down timeout */
133 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
135 #define SC2IFP(sc) ((sc)->sc_ifp)
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)");
156 TAILQ_HEAD(, carp_softc) vhif_vrs;
159 struct ifnet *vhif_ifp;
163 /* Get carp_if from softc. Valid after carp_set_addr{,6}. */
164 #define SC2CIF(sc) ((struct carp_if *)(sc)->sc_carpdev->if_carp)
166 /* lock per carp_if queue */
167 #define CARP_LOCK_INIT(cif) mtx_init(&(cif)->vhif_mtx, "carp_if", \
169 #define CARP_LOCK_DESTROY(cif) mtx_destroy(&(cif)->vhif_mtx)
170 #define CARP_LOCK_ASSERT(cif) mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
171 #define CARP_LOCK(cif) mtx_lock(&(cif)->vhif_mtx)
172 #define CARP_UNLOCK(cif) mtx_unlock(&(cif)->vhif_mtx)
174 #define CARP_SCLOCK(sc) mtx_lock(&SC2CIF(sc)->vhif_mtx)
175 #define CARP_SCUNLOCK(sc) mtx_unlock(&SC2CIF(sc)->vhif_mtx)
176 #define CARP_SCLOCK_ASSERT(sc) mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
178 #define CARP_LOG(...) do { \
179 if (carp_opts[CARPCTL_LOG] > 0) \
180 log(LOG_INFO, __VA_ARGS__); \
183 #define CARP_DEBUG(...) do { \
184 if (carp_opts[CARPCTL_LOG] > 1) \
185 log(LOG_DEBUG, __VA_ARGS__); \
188 static void carp_hmac_prepare(struct carp_softc *);
189 static void carp_hmac_generate(struct carp_softc *, u_int32_t *,
191 static int carp_hmac_verify(struct carp_softc *, u_int32_t *,
193 static void carp_setroute(struct carp_softc *, int);
194 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
195 static int carp_clone_create(struct if_clone *, int, caddr_t);
196 static void carp_clone_destroy(struct ifnet *);
197 static void carpdetach(struct carp_softc *, int);
198 static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
199 struct carp_header *);
200 static void carp_send_ad_all(void);
201 static void carp_send_ad(void *);
202 static void carp_send_ad_locked(struct carp_softc *);
203 static void carp_send_arp(struct carp_softc *);
204 static void carp_master_down(void *);
205 static void carp_master_down_locked(struct carp_softc *);
206 static int carp_ioctl(struct ifnet *, u_long, caddr_t);
207 static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
209 static void carp_start(struct ifnet *);
210 static void carp_setrun(struct carp_softc *, sa_family_t);
211 static void carp_set_state(struct carp_softc *, int);
212 static int carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
213 enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
215 static void carp_multicast_cleanup(struct carp_softc *);
216 static int carp_set_addr(struct carp_softc *, struct sockaddr_in *);
217 static int carp_del_addr(struct carp_softc *, struct sockaddr_in *);
218 static void carp_carpdev_state_locked(struct carp_if *);
219 static void carp_sc_state_locked(struct carp_softc *);
221 static void carp_send_na(struct carp_softc *);
222 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
223 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
224 static void carp_multicast6_cleanup(struct carp_softc *);
227 static LIST_HEAD(, carp_softc) carpif_list;
228 static struct mtx carp_mtx;
229 IFC_SIMPLE_DECLARE(carp, 0);
231 static eventhandler_tag if_detach_event_tag;
233 static __inline u_int16_t
234 carp_cksum(struct mbuf *m, int len)
236 return (in_cksum(m, len));
240 carp_hmac_prepare(struct carp_softc *sc)
242 u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
243 u_int8_t vhid = sc->sc_vhid & 0xff;
247 struct in_addr last, cur, in;
250 struct in6_addr last6, cur6, in6;
256 /* XXX: possible race here */
258 /* compute ipad from key */
259 bzero(sc->sc_pad, sizeof(sc->sc_pad));
260 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
261 for (i = 0; i < sizeof(sc->sc_pad); i++)
262 sc->sc_pad[i] ^= 0x36;
264 /* precompute first part of inner hash */
265 SHA1Init(&sc->sc_sha1);
266 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
267 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
268 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
269 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
275 cur.s_addr = 0xffffffff;
276 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
277 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
278 if (ifa->ifa_addr->sa_family == AF_INET &&
279 ntohl(in.s_addr) > ntohl(last.s_addr) &&
280 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
281 cur.s_addr = in.s_addr;
286 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
290 memset(&cur6, 0, sizeof(cur6));
294 memset(&cur6, 0xff, sizeof(cur6));
295 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
296 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
297 if (IN6_IS_SCOPE_EMBED(&in6))
298 in6.s6_addr16[1] = 0;
299 if (ifa->ifa_addr->sa_family == AF_INET6 &&
300 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
301 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
307 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
311 /* convert ipad to opad */
312 for (i = 0; i < sizeof(sc->sc_pad); i++)
313 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
320 carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
321 unsigned char md[20])
325 /* fetch first half of inner hash */
326 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
328 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
329 SHA1Final(md, &sha1ctx);
333 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
334 SHA1Update(&sha1ctx, md, 20);
335 SHA1Final(md, &sha1ctx);
339 carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
340 unsigned char md[20])
342 unsigned char md2[20];
344 CARP_SCLOCK_ASSERT(sc);
346 carp_hmac_generate(sc, counter, md2);
348 return (bcmp(md, md2, sizeof(md2)));
352 carp_setroute(struct carp_softc *sc, int cmd)
358 CARP_SCLOCK_ASSERT(sc);
361 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
362 if (ifa->ifa_addr->sa_family == AF_INET &&
363 sc->sc_carpdev != NULL) {
364 int count = carp_addrcount(
365 (struct carp_if *)sc->sc_carpdev->if_carp,
366 ifatoia(ifa), CARP_COUNT_MASTER);
368 if ((cmd == RTM_ADD && count == 1) ||
369 (cmd == RTM_DELETE && count == 0))
370 rtinit(ifa, cmd, RTF_UP | RTF_HOST);
373 if (ifa->ifa_addr->sa_family == AF_INET6) {
385 carp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
388 struct carp_softc *sc;
391 MALLOC(sc, struct carp_softc *, sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
392 ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
398 sc->sc_flags_backup = 0;
400 sc->sc_advbase = CARP_DFLTINTV;
401 sc->sc_vhid = -1; /* required setting */
403 sc->sc_init_counter = 1;
404 sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
406 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
408 sc->sc_imo.imo_membership = (struct in_multi **)malloc(
409 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
411 sc->sc_imo.imo_mfilters = NULL;
412 sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
413 sc->sc_imo.imo_multicast_vif = -1;
415 callout_init(&sc->sc_ad_tmo, CALLOUT_MPSAFE);
416 callout_init(&sc->sc_md_tmo, CALLOUT_MPSAFE);
417 callout_init(&sc->sc_md6_tmo, CALLOUT_MPSAFE);
420 if_initname(ifp, CARP_IFNAME, unit);
421 ifp->if_mtu = ETHERMTU;
422 ifp->if_flags = IFF_LOOPBACK;
423 ifp->if_ioctl = carp_ioctl;
424 ifp->if_output = carp_looutput;
425 ifp->if_start = carp_start;
426 ifp->if_type = IFT_CARP;
427 ifp->if_snd.ifq_maxlen = ifqmaxlen;
430 bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
432 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
433 mtx_unlock(&carp_mtx);
438 carp_clone_destroy(struct ifnet *ifp)
440 struct carp_softc *sc = ifp->if_softc;
444 carpdetach(sc, 1); /* Returns unlocked. */
447 LIST_REMOVE(sc, sc_next);
448 mtx_unlock(&carp_mtx);
451 if_free_type(ifp, IFT_ETHER);
452 free(sc->sc_imo.imo_membership, M_CARP);
457 * This function can be called on CARP interface destroy path,
458 * and in case of the removal of the underlying interface as
459 * well. We differentiate these two cases. In the latter case
460 * we do not cleanup our multicast memberships, since they
461 * are already freed. Also, in the latter case we do not
462 * release the lock on return, because the function will be
463 * called once more, for another CARP instance on the same
467 carpdetach(struct carp_softc *sc, int unlock)
471 callout_stop(&sc->sc_ad_tmo);
472 callout_stop(&sc->sc_md_tmo);
473 callout_stop(&sc->sc_md6_tmo);
476 carp_suppress_preempt--;
479 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
480 carp_suppress_preempt--;
481 sc->sc_sendad_errors = 0;
483 carp_set_state(sc, INIT);
484 SC2IFP(sc)->if_flags &= ~IFF_UP;
487 carp_multicast_cleanup(sc);
489 carp_multicast6_cleanup(sc);
492 if (sc->sc_carpdev != NULL) {
493 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
494 CARP_LOCK_ASSERT(cif);
495 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
496 if (!--cif->vhif_nvrs) {
497 ifpromisc(sc->sc_carpdev, 0);
498 sc->sc_carpdev->if_carp = NULL;
499 CARP_LOCK_DESTROY(cif);
503 sc->sc_carpdev = NULL;
507 /* Detach an interface from the carp. */
509 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
511 struct carp_if *cif = (struct carp_if *)ifp->if_carp;
512 struct carp_softc *sc, *nextsc;
518 * XXX: At the end of for() cycle the lock will be destroyed.
521 for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
522 nextsc = TAILQ_NEXT(sc, sc_list);
528 * process input packet.
529 * we have rearranged checks order compared to the rfc,
530 * but it seems more efficient this way or not possible otherwise.
533 carp_input(struct mbuf *m, int hlen)
535 struct ip *ip = mtod(m, struct ip *);
536 struct carp_header *ch;
539 carpstats.carps_ipackets++;
541 if (!carp_opts[CARPCTL_ALLOW]) {
546 /* check if received on a valid carp interface */
547 if (m->m_pkthdr.rcvif->if_carp == NULL) {
548 carpstats.carps_badif++;
549 CARP_LOG("carp_input: packet received on non-carp "
551 m->m_pkthdr.rcvif->if_xname);
556 /* verify that the IP TTL is 255. */
557 if (ip->ip_ttl != CARP_DFLTTL) {
558 carpstats.carps_badttl++;
559 CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
561 m->m_pkthdr.rcvif->if_xname);
566 iplen = ip->ip_hl << 2;
568 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
569 carpstats.carps_badlen++;
570 CARP_LOG("carp_input: received len %zd < "
571 "sizeof(struct carp_header)\n",
572 m->m_len - sizeof(struct ip));
577 if (iplen + sizeof(*ch) < m->m_len) {
578 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
579 carpstats.carps_hdrops++;
580 CARP_LOG("carp_input: pullup failed\n");
583 ip = mtod(m, struct ip *);
585 ch = (struct carp_header *)((char *)ip + iplen);
588 * verify that the received packet length is
589 * equal to the CARP header
591 len = iplen + sizeof(*ch);
592 if (len > m->m_pkthdr.len) {
593 carpstats.carps_badlen++;
594 CARP_LOG("carp_input: packet too short %d on %s\n",
596 m->m_pkthdr.rcvif->if_xname);
601 if ((m = m_pullup(m, len)) == NULL) {
602 carpstats.carps_hdrops++;
605 ip = mtod(m, struct ip *);
606 ch = (struct carp_header *)((char *)ip + iplen);
608 /* verify the CARP checksum */
610 if (carp_cksum(m, len - iplen)) {
611 carpstats.carps_badsum++;
612 CARP_LOG("carp_input: checksum failed on %s\n",
613 m->m_pkthdr.rcvif->if_xname);
619 carp_input_c(m, ch, AF_INET);
624 carp6_input(struct mbuf **mp, int *offp, int proto)
626 struct mbuf *m = *mp;
627 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
628 struct carp_header *ch;
631 carpstats.carps_ipackets6++;
633 if (!carp_opts[CARPCTL_ALLOW]) {
635 return (IPPROTO_DONE);
638 /* check if received on a valid carp interface */
639 if (m->m_pkthdr.rcvif->if_carp == NULL) {
640 carpstats.carps_badif++;
641 CARP_LOG("carp6_input: packet received on non-carp "
643 m->m_pkthdr.rcvif->if_xname);
645 return (IPPROTO_DONE);
648 /* verify that the IP TTL is 255 */
649 if (ip6->ip6_hlim != CARP_DFLTTL) {
650 carpstats.carps_badttl++;
651 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
653 m->m_pkthdr.rcvif->if_xname);
655 return (IPPROTO_DONE);
658 /* verify that we have a complete carp packet */
660 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
662 carpstats.carps_badlen++;
663 CARP_LOG("carp6_input: packet size %u too small\n", len);
664 return (IPPROTO_DONE);
668 /* verify the CARP checksum */
670 if (carp_cksum(m, sizeof(*ch))) {
671 carpstats.carps_badsum++;
672 CARP_LOG("carp6_input: checksum failed, on %s\n",
673 m->m_pkthdr.rcvif->if_xname);
675 return (IPPROTO_DONE);
679 carp_input_c(m, ch, AF_INET6);
680 return (IPPROTO_DONE);
685 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
687 struct ifnet *ifp = m->m_pkthdr.rcvif;
688 struct carp_softc *sc;
689 u_int64_t tmp_counter;
690 struct timeval sc_tv, ch_tv;
692 /* verify that the VHID is valid on the receiving interface */
693 CARP_LOCK(ifp->if_carp);
694 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
695 if (sc->sc_vhid == ch->carp_vhid)
698 if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
699 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
700 carpstats.carps_badvhid++;
701 CARP_UNLOCK(ifp->if_carp);
706 getmicrotime(&SC2IFP(sc)->if_lastchange);
707 SC2IFP(sc)->if_ipackets++;
708 SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
710 if (bpf_peers_present(SC2IFP(sc)->if_bpf)) {
711 struct ip *ip = mtod(m, struct ip *);
714 /* BPF wants net byte order */
715 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
716 ip->ip_off = htons(ip->ip_off);
717 bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
720 /* verify the CARP version. */
721 if (ch->carp_version != CARP_VERSION) {
722 carpstats.carps_badver++;
723 SC2IFP(sc)->if_ierrors++;
724 CARP_UNLOCK(ifp->if_carp);
725 CARP_LOG("%s; invalid version %d\n",
726 SC2IFP(sc)->if_xname,
732 /* verify the hash */
733 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
734 carpstats.carps_badauth++;
735 SC2IFP(sc)->if_ierrors++;
736 CARP_UNLOCK(ifp->if_carp);
737 CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
742 tmp_counter = ntohl(ch->carp_counter[0]);
743 tmp_counter = tmp_counter<<32;
744 tmp_counter += ntohl(ch->carp_counter[1]);
746 /* XXX Replay protection goes here */
748 sc->sc_init_counter = 0;
749 sc->sc_counter = tmp_counter;
751 sc_tv.tv_sec = sc->sc_advbase;
752 if (carp_suppress_preempt && sc->sc_advskew < 240)
753 sc_tv.tv_usec = 240 * 1000000 / 256;
755 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
756 ch_tv.tv_sec = ch->carp_advbase;
757 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
759 switch (sc->sc_state) {
764 * If we receive an advertisement from a master who's going to
765 * be more frequent than us, go into BACKUP state.
767 if (timevalcmp(&sc_tv, &ch_tv, >) ||
768 timevalcmp(&sc_tv, &ch_tv, ==)) {
769 callout_stop(&sc->sc_ad_tmo);
770 CARP_DEBUG("%s: MASTER -> BACKUP "
771 "(more frequent advertisement received)\n",
772 SC2IFP(sc)->if_xname);
773 carp_set_state(sc, BACKUP);
775 carp_setroute(sc, RTM_DELETE);
780 * If we're pre-empting masters who advertise slower than us,
781 * and this one claims to be slower, treat him as down.
783 if (carp_opts[CARPCTL_PREEMPT] &&
784 timevalcmp(&sc_tv, &ch_tv, <)) {
785 CARP_DEBUG("%s: BACKUP -> MASTER "
786 "(preempting a slower master)\n",
787 SC2IFP(sc)->if_xname);
788 carp_master_down_locked(sc);
793 * If the master is going to advertise at such a low frequency
794 * that he's guaranteed to time out, we'd might as well just
795 * treat him as timed out now.
797 sc_tv.tv_sec = sc->sc_advbase * 3;
798 if (timevalcmp(&sc_tv, &ch_tv, <)) {
799 CARP_DEBUG("%s: BACKUP -> MASTER "
800 "(master timed out)\n",
801 SC2IFP(sc)->if_xname);
802 carp_master_down_locked(sc);
807 * Otherwise, we reset the counter and wait for the next
814 CARP_UNLOCK(ifp->if_carp);
821 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
824 struct ifnet *ifp = SC2IFP(sc);
826 if (sc->sc_init_counter) {
827 /* this could also be seconds since unix epoch */
828 sc->sc_counter = arc4random();
829 sc->sc_counter = sc->sc_counter << 32;
830 sc->sc_counter += arc4random();
834 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
835 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
837 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
839 /* Tag packet for carp_output */
840 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
843 SC2IFP(sc)->if_oerrors++;
846 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
847 m_tag_prepend(m, mtag);
853 carp_send_ad_all(void)
855 struct carp_softc *sc;
858 LIST_FOREACH(sc, &carpif_list, sc_next) {
859 if (sc->sc_carpdev == NULL)
862 if ((SC2IFP(sc)->if_flags & IFF_UP) &&
863 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
864 sc->sc_state == MASTER)
865 carp_send_ad_locked(sc);
868 mtx_unlock(&carp_mtx);
872 carp_send_ad(void *v)
874 struct carp_softc *sc = v;
877 carp_send_ad_locked(sc);
882 carp_send_ad_locked(struct carp_softc *sc)
884 struct carp_header ch;
886 struct carp_header *ch_ptr;
888 int len, advbase, advskew;
890 CARP_SCLOCK_ASSERT(sc);
892 /* bow out if we've lost our UPness or RUNNINGuiness */
893 if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
894 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
898 advbase = sc->sc_advbase;
899 if (!carp_suppress_preempt || sc->sc_advskew > 240)
900 advskew = sc->sc_advskew;
904 tv.tv_usec = advskew * 1000000 / 256;
907 ch.carp_version = CARP_VERSION;
908 ch.carp_type = CARP_ADVERTISEMENT;
909 ch.carp_vhid = sc->sc_vhid;
910 ch.carp_advbase = advbase;
911 ch.carp_advskew = advskew;
912 ch.carp_authlen = 7; /* XXX DEFINE */
913 ch.carp_pad1 = 0; /* must be zero */
920 MGETHDR(m, M_DONTWAIT, MT_HEADER);
922 SC2IFP(sc)->if_oerrors++;
923 carpstats.carps_onomem++;
924 /* XXX maybe less ? */
925 if (advbase != 255 || advskew != 255)
926 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
930 len = sizeof(*ip) + sizeof(ch);
931 m->m_pkthdr.len = len;
932 m->m_pkthdr.rcvif = NULL;
934 MH_ALIGN(m, m->m_len);
935 m->m_flags |= M_MCAST;
936 ip = mtod(m, struct ip *);
937 ip->ip_v = IPVERSION;
938 ip->ip_hl = sizeof(*ip) >> 2;
939 ip->ip_tos = IPTOS_LOWDELAY;
941 ip->ip_id = ip_newid();
943 ip->ip_ttl = CARP_DFLTTL;
944 ip->ip_p = IPPROTO_CARP;
946 ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
947 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
949 ch_ptr = (struct carp_header *)(&ip[1]);
950 bcopy(&ch, ch_ptr, sizeof(ch));
951 if (carp_prepare_ad(m, sc, ch_ptr))
954 m->m_data += sizeof(*ip);
955 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
956 m->m_data -= sizeof(*ip);
958 getmicrotime(&SC2IFP(sc)->if_lastchange);
959 SC2IFP(sc)->if_opackets++;
960 SC2IFP(sc)->if_obytes += len;
961 carpstats.carps_opackets++;
963 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
964 SC2IFP(sc)->if_oerrors++;
965 if (sc->sc_sendad_errors < INT_MAX)
966 sc->sc_sendad_errors++;
967 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
968 carp_suppress_preempt++;
969 if (carp_suppress_preempt == 1) {
975 sc->sc_sendad_success = 0;
977 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
978 if (++sc->sc_sendad_success >=
979 CARP_SENDAD_MIN_SUCCESS) {
980 carp_suppress_preempt--;
981 sc->sc_sendad_errors = 0;
984 sc->sc_sendad_errors = 0;
992 MGETHDR(m, M_DONTWAIT, MT_HEADER);
994 SC2IFP(sc)->if_oerrors++;
995 carpstats.carps_onomem++;
996 /* XXX maybe less ? */
997 if (advbase != 255 || advskew != 255)
998 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1002 len = sizeof(*ip6) + sizeof(ch);
1003 m->m_pkthdr.len = len;
1004 m->m_pkthdr.rcvif = NULL;
1006 MH_ALIGN(m, m->m_len);
1007 m->m_flags |= M_MCAST;
1008 ip6 = mtod(m, struct ip6_hdr *);
1009 bzero(ip6, sizeof(*ip6));
1010 ip6->ip6_vfc |= IPV6_VERSION;
1011 ip6->ip6_hlim = CARP_DFLTTL;
1012 ip6->ip6_nxt = IPPROTO_CARP;
1013 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1014 sizeof(struct in6_addr));
1015 /* set the multicast destination */
1017 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1018 ip6->ip6_dst.s6_addr8[15] = 0x12;
1019 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1020 SC2IFP(sc)->if_oerrors++;
1022 CARP_LOG("%s: in6_setscope failed\n", __func__);
1026 ch_ptr = (struct carp_header *)(&ip6[1]);
1027 bcopy(&ch, ch_ptr, sizeof(ch));
1028 if (carp_prepare_ad(m, sc, ch_ptr))
1031 m->m_data += sizeof(*ip6);
1032 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
1033 m->m_data -= sizeof(*ip6);
1035 getmicrotime(&SC2IFP(sc)->if_lastchange);
1036 SC2IFP(sc)->if_opackets++;
1037 SC2IFP(sc)->if_obytes += len;
1038 carpstats.carps_opackets6++;
1040 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1041 SC2IFP(sc)->if_oerrors++;
1042 if (sc->sc_sendad_errors < INT_MAX)
1043 sc->sc_sendad_errors++;
1044 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1045 carp_suppress_preempt++;
1046 if (carp_suppress_preempt == 1) {
1052 sc->sc_sendad_success = 0;
1054 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1055 if (++sc->sc_sendad_success >=
1056 CARP_SENDAD_MIN_SUCCESS) {
1057 carp_suppress_preempt--;
1058 sc->sc_sendad_errors = 0;
1061 sc->sc_sendad_errors = 0;
1066 if (advbase != 255 || advskew != 255)
1067 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1073 * Broadcast a gratuitous ARP request containing
1074 * the virtual router MAC address for each IP address
1075 * associated with the virtual router.
1078 carp_send_arp(struct carp_softc *sc)
1082 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1084 if (ifa->ifa_addr->sa_family != AF_INET)
1087 /* arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
1088 arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1090 DELAY(1000); /* XXX */
1096 carp_send_na(struct carp_softc *sc)
1099 struct in6_addr *in6;
1100 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1102 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1104 if (ifa->ifa_addr->sa_family != AF_INET6)
1107 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1108 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1109 ND_NA_FLAG_OVERRIDE, 1, NULL);
1110 DELAY(1000); /* XXX */
1116 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1118 struct carp_softc *vh;
1122 CARP_LOCK_ASSERT(cif);
1124 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1125 if ((type == CARP_COUNT_RUNNING &&
1126 (SC2IFP(vh)->if_flags & IFF_UP) &&
1127 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1128 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1129 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1131 if (ifa->ifa_addr->sa_family == AF_INET &&
1132 ia->ia_addr.sin_addr.s_addr ==
1133 ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1142 carp_iamatch(void *v, struct in_ifaddr *ia,
1143 struct in_addr *isaddr, u_int8_t **enaddr)
1145 struct carp_if *cif = v;
1146 struct carp_softc *vh;
1147 int index, count = 0;
1152 if (carp_opts[CARPCTL_ARPBALANCE]) {
1154 * XXX proof of concept implementation.
1155 * We use the source ip to decide which virtual host should
1156 * handle the request. If we're master of that virtual host,
1157 * then we respond, otherwise, just drop the arp packet on
1160 count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1162 /* should never reach this */
1167 /* this should be a hash, like pf_hash() */
1168 index = ntohl(isaddr->s_addr) % count;
1171 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1172 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1173 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1174 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1176 if (ifa->ifa_addr->sa_family ==
1178 ia->ia_addr.sin_addr.s_addr ==
1179 ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1180 if (count == index) {
1183 *enaddr = IF_LLADDR(vh->sc_ifp);
1197 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1198 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1199 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1200 ia->ia_ifp == SC2IFP(vh) &&
1201 vh->sc_state == MASTER) {
1202 *enaddr = IF_LLADDR(vh->sc_ifp);
1214 carp_iamatch6(void *v, struct in6_addr *taddr)
1216 struct carp_if *cif = v;
1217 struct carp_softc *vh;
1221 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1222 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1223 if (IN6_ARE_ADDR_EQUAL(taddr,
1224 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1225 (SC2IFP(vh)->if_flags & IFF_UP) &&
1226 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1227 vh->sc_state == MASTER) {
1239 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1242 struct carp_if *cif = v;
1243 struct carp_softc *sc;
1247 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1248 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1249 if (IN6_ARE_ADDR_EQUAL(taddr,
1250 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1251 (SC2IFP(sc)->if_flags & IFF_UP) &&
1252 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1253 struct ifnet *ifp = SC2IFP(sc);
1254 mtag = m_tag_get(PACKET_TAG_CARP,
1255 sizeof(struct ifnet *), M_NOWAIT);
1257 /* better a bit than nothing */
1259 return (IF_LLADDR(sc->sc_ifp));
1261 bcopy(&ifp, (caddr_t)(mtag + 1),
1262 sizeof(struct ifnet *));
1263 m_tag_prepend(m, mtag);
1266 return (IF_LLADDR(sc->sc_ifp));
1277 carp_forus(void *v, void *dhost)
1279 struct carp_if *cif = v;
1280 struct carp_softc *vh;
1281 u_int8_t *ena = dhost;
1283 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1287 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1288 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1289 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1290 vh->sc_state == MASTER &&
1291 !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1293 return (SC2IFP(vh));
1301 carp_master_down(void *v)
1303 struct carp_softc *sc = v;
1306 carp_master_down_locked(sc);
1311 carp_master_down_locked(struct carp_softc *sc)
1314 CARP_SCLOCK_ASSERT(sc);
1316 switch (sc->sc_state) {
1318 printf("%s: master_down event in INIT state\n",
1319 SC2IFP(sc)->if_xname);
1324 carp_set_state(sc, MASTER);
1325 carp_send_ad_locked(sc);
1331 carp_setroute(sc, RTM_ADD);
1337 * When in backup state, af indicates whether to reset the master down timer
1338 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1341 carp_setrun(struct carp_softc *sc, sa_family_t af)
1345 if (sc->sc_carpdev == NULL) {
1346 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1347 carp_set_state(sc, INIT);
1350 CARP_SCLOCK_ASSERT(sc);
1352 if (SC2IFP(sc)->if_flags & IFF_UP &&
1353 sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1354 SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1356 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1357 carp_setroute(sc, RTM_DELETE);
1361 switch (sc->sc_state) {
1363 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1364 carp_send_ad_locked(sc);
1369 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1370 SC2IFP(sc)->if_xname);
1371 carp_set_state(sc, MASTER);
1372 carp_setroute(sc, RTM_ADD);
1374 CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1375 carp_set_state(sc, BACKUP);
1376 carp_setroute(sc, RTM_DELETE);
1381 callout_stop(&sc->sc_ad_tmo);
1382 tv.tv_sec = 3 * sc->sc_advbase;
1383 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1387 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1388 carp_master_down, sc);
1393 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1394 carp_master_down, sc);
1399 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1400 carp_master_down, sc);
1402 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1403 carp_master_down, sc);
1408 tv.tv_sec = sc->sc_advbase;
1409 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1410 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1417 carp_multicast_cleanup(struct carp_softc *sc)
1419 struct ip_moptions *imo = &sc->sc_imo;
1420 u_int16_t n = imo->imo_num_memberships;
1422 /* Clean up our own multicast memberships */
1424 if (imo->imo_membership[n] != NULL) {
1425 in_delmulti(imo->imo_membership[n]);
1426 imo->imo_membership[n] = NULL;
1429 KASSERT(imo->imo_mfilters == NULL,
1430 ("%s: imo_mfilters != NULL", __func__));
1431 imo->imo_num_memberships = 0;
1432 imo->imo_multicast_ifp = NULL;
1437 carp_multicast6_cleanup(struct carp_softc *sc)
1439 struct ip6_moptions *im6o = &sc->sc_im6o;
1441 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1442 struct in6_multi_mship *imm =
1443 LIST_FIRST(&im6o->im6o_memberships);
1445 LIST_REMOVE(imm, i6mm_chain);
1446 in6_leavegroup(imm);
1448 im6o->im6o_multicast_ifp = NULL;
1453 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1456 struct carp_if *cif;
1457 struct in_ifaddr *ia, *ia_if;
1458 struct ip_moptions *imo = &sc->sc_imo;
1459 struct in_addr addr;
1460 u_long iaddr = htonl(sin->sin_addr.s_addr);
1463 if (sin->sin_addr.s_addr == 0) {
1464 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1465 carp_set_state(sc, INIT);
1467 SC2IFP(sc)->if_flags |= IFF_UP;
1476 /* we have to do it by hands to check we won't match on us */
1477 ia_if = NULL; own = 0;
1478 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1479 /* and, yeah, we need a multicast-capable iface too */
1480 if (ia->ia_ifp != SC2IFP(sc) &&
1481 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1482 (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1485 if (sin->sin_addr.s_addr ==
1486 ia->ia_addr.sin_addr.s_addr)
1492 return (EADDRNOTAVAIL);
1497 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1498 (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1499 return (EADDRNOTAVAIL);
1501 if (imo->imo_num_memberships == 0) {
1502 addr.s_addr = htonl(INADDR_CARP_GROUP);
1503 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1505 imo->imo_num_memberships++;
1506 imo->imo_multicast_ifp = ifp;
1507 imo->imo_multicast_ttl = CARP_DFLTTL;
1508 imo->imo_multicast_loop = 0;
1511 if (!ifp->if_carp) {
1513 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1519 if ((error = ifpromisc(ifp, 1))) {
1524 CARP_LOCK_INIT(cif);
1526 cif->vhif_ifp = ifp;
1527 TAILQ_INIT(&cif->vhif_vrs);
1531 struct carp_softc *vr;
1533 cif = (struct carp_if *)ifp->if_carp;
1535 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1536 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1543 sc->sc_carpdev = ifp;
1545 { /* XXX prevent endless loop if already in queue */
1546 struct carp_softc *vr, *after = NULL;
1548 cif = (struct carp_if *)ifp->if_carp;
1550 /* XXX: cif should not change, right? So we still hold the lock */
1551 CARP_LOCK_ASSERT(cif);
1553 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1556 if (vr->sc_vhid < sc->sc_vhid)
1561 /* We're trying to keep things in order */
1562 if (after == NULL) {
1563 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1565 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1572 SC2IFP(sc)->if_flags |= IFF_UP;
1575 carp_sc_state_locked(sc);
1583 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1588 carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1592 if (!--sc->sc_naddrs) {
1593 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1594 struct ip_moptions *imo = &sc->sc_imo;
1597 callout_stop(&sc->sc_ad_tmo);
1598 SC2IFP(sc)->if_flags &= ~IFF_UP;
1599 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1601 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1602 imo->imo_multicast_ifp = NULL;
1603 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1604 if (!--cif->vhif_nvrs) {
1605 sc->sc_carpdev->if_carp = NULL;
1606 CARP_LOCK_DESTROY(cif);
1607 FREE(cif, M_IFADDR);
1618 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1621 struct carp_if *cif;
1622 struct in6_ifaddr *ia, *ia_if;
1623 struct ip6_moptions *im6o = &sc->sc_im6o;
1624 struct in6_multi_mship *imm;
1625 struct in6_addr in6;
1628 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1629 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1630 carp_set_state(sc, INIT);
1632 SC2IFP(sc)->if_flags |= IFF_UP;
1641 /* we have to do it by hands to check we won't match on us */
1642 ia_if = NULL; own = 0;
1643 for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) {
1646 for (i = 0; i < 4; i++) {
1647 if ((sin6->sin6_addr.s6_addr32[i] &
1648 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1649 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1650 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1653 /* and, yeah, we need a multicast-capable iface too */
1654 if (ia->ia_ifp != SC2IFP(sc) &&
1655 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1659 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1660 &ia->ia_addr.sin6_addr))
1666 return (EADDRNOTAVAIL);
1670 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1671 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1672 return (EADDRNOTAVAIL);
1674 if (!sc->sc_naddrs6) {
1675 im6o->im6o_multicast_ifp = ifp;
1677 /* join CARP multicast address */
1678 bzero(&in6, sizeof(in6));
1679 in6.s6_addr16[0] = htons(0xff02);
1680 in6.s6_addr8[15] = 0x12;
1681 if (in6_setscope(&in6, ifp, NULL) != 0)
1683 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1685 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1687 /* join solicited multicast address */
1688 bzero(&in6, sizeof(in6));
1689 in6.s6_addr16[0] = htons(0xff02);
1690 in6.s6_addr32[1] = 0;
1691 in6.s6_addr32[2] = htonl(1);
1692 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1693 in6.s6_addr8[12] = 0xff;
1694 if (in6_setscope(&in6, ifp, NULL) != 0)
1696 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1698 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1701 if (!ifp->if_carp) {
1702 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1708 if ((error = ifpromisc(ifp, 1))) {
1713 CARP_LOCK_INIT(cif);
1715 cif->vhif_ifp = ifp;
1716 TAILQ_INIT(&cif->vhif_vrs);
1720 struct carp_softc *vr;
1722 cif = (struct carp_if *)ifp->if_carp;
1724 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1725 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1732 sc->sc_carpdev = ifp;
1734 { /* XXX prevent endless loop if already in queue */
1735 struct carp_softc *vr, *after = NULL;
1737 cif = (struct carp_if *)ifp->if_carp;
1738 CARP_LOCK_ASSERT(cif);
1740 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1743 if (vr->sc_vhid < sc->sc_vhid)
1748 /* We're trying to keep things in order */
1749 if (after == NULL) {
1750 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1752 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1759 SC2IFP(sc)->if_flags |= IFF_UP;
1762 carp_sc_state_locked(sc);
1770 /* clean up multicast memberships */
1771 if (!sc->sc_naddrs6) {
1772 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1773 imm = LIST_FIRST(&im6o->im6o_memberships);
1774 LIST_REMOVE(imm, i6mm_chain);
1775 in6_leavegroup(imm);
1782 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1786 if (!--sc->sc_naddrs6) {
1787 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1788 struct ip6_moptions *im6o = &sc->sc_im6o;
1791 callout_stop(&sc->sc_ad_tmo);
1792 SC2IFP(sc)->if_flags &= ~IFF_UP;
1793 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1795 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1796 struct in6_multi_mship *imm =
1797 LIST_FIRST(&im6o->im6o_memberships);
1799 LIST_REMOVE(imm, i6mm_chain);
1800 in6_leavegroup(imm);
1802 im6o->im6o_multicast_ifp = NULL;
1803 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1804 if (!--cif->vhif_nvrs) {
1805 CARP_LOCK_DESTROY(cif);
1806 sc->sc_carpdev->if_carp = NULL;
1807 FREE(cif, M_IFADDR);
1817 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1819 struct carp_softc *sc = ifp->if_softc, *vr;
1820 struct carpreq carpr;
1823 struct ifaliasreq *ifra;
1824 int locked = 0, error = 0;
1826 ifa = (struct ifaddr *)addr;
1827 ifra = (struct ifaliasreq *)addr;
1828 ifr = (struct ifreq *)addr;
1832 switch (ifa->ifa_addr->sa_family) {
1835 SC2IFP(sc)->if_flags |= IFF_UP;
1836 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1837 sizeof(struct sockaddr));
1838 error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1843 SC2IFP(sc)->if_flags |= IFF_UP;
1844 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1848 error = EAFNOSUPPORT;
1854 switch (ifa->ifa_addr->sa_family) {
1857 SC2IFP(sc)->if_flags |= IFF_UP;
1858 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1859 sizeof(struct sockaddr));
1860 error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1865 SC2IFP(sc)->if_flags |= IFF_UP;
1866 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1870 error = EAFNOSUPPORT;
1876 switch (ifa->ifa_addr->sa_family) {
1879 error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1884 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1888 error = EAFNOSUPPORT;
1894 if (sc->sc_carpdev) {
1898 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1899 callout_stop(&sc->sc_ad_tmo);
1900 callout_stop(&sc->sc_md_tmo);
1901 callout_stop(&sc->sc_md6_tmo);
1902 if (sc->sc_state == MASTER)
1903 carp_send_ad_locked(sc);
1904 carp_set_state(sc, INIT);
1906 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1907 SC2IFP(sc)->if_flags |= IFF_UP;
1913 error = priv_check(curthread, PRIV_NETINET_CARP);
1916 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1919 if (sc->sc_carpdev) {
1923 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1924 switch (carpr.carpr_state) {
1926 callout_stop(&sc->sc_ad_tmo);
1927 carp_set_state(sc, BACKUP);
1929 carp_setroute(sc, RTM_DELETE);
1932 carp_master_down_locked(sc);
1938 if (carpr.carpr_vhid > 0) {
1939 if (carpr.carpr_vhid > 255) {
1943 if (sc->sc_carpdev) {
1944 struct carp_if *cif;
1945 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1946 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1948 vr->sc_vhid == carpr.carpr_vhid) {
1952 if (error == EEXIST)
1955 sc->sc_vhid = carpr.carpr_vhid;
1956 IF_LLADDR(sc->sc_ifp)[0] = 0;
1957 IF_LLADDR(sc->sc_ifp)[1] = 0;
1958 IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1959 IF_LLADDR(sc->sc_ifp)[3] = 0;
1960 IF_LLADDR(sc->sc_ifp)[4] = 1;
1961 IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1964 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1965 if (carpr.carpr_advskew >= 255) {
1969 if (carpr.carpr_advbase > 255) {
1973 sc->sc_advbase = carpr.carpr_advbase;
1974 sc->sc_advskew = carpr.carpr_advskew;
1977 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1987 /* XXX: lockless read */
1988 bzero(&carpr, sizeof(carpr));
1989 carpr.carpr_state = sc->sc_state;
1990 carpr.carpr_vhid = sc->sc_vhid;
1991 carpr.carpr_advbase = sc->sc_advbase;
1992 carpr.carpr_advskew = sc->sc_advskew;
1993 error = priv_check(curthread, PRIV_NETINET_CARP);
1995 bcopy(sc->sc_key, carpr.carpr_key,
1996 sizeof(carpr.carpr_key));
1997 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
2007 carp_hmac_prepare(sc);
2013 * XXX: this is looutput. We should eventually use it from there.
2016 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2021 M_ASSERTPKTHDR(m); /* check if we have the packet header */
2023 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2025 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
2026 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
2030 ifp->if_obytes += m->m_pkthdr.len;
2032 /* BPF writes need to be handled specially. */
2033 if (dst->sa_family == AF_UNSPEC) {
2034 bcopy(dst->sa_data, &af, sizeof(af));
2035 dst->sa_family = af;
2039 switch (dst->sa_family) {
2046 printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
2048 return (EAFNOSUPPORT);
2051 return(if_simloop(ifp, m, dst->sa_family, 0));
2055 * Start output on carp interface. This function should never be called.
2058 carp_start(struct ifnet *ifp)
2061 printf("%s: start called\n", ifp->if_xname);
2066 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2070 struct carp_softc *sc;
2071 struct ifnet *carp_ifp;
2076 switch (sa->sa_family) {
2089 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2093 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2094 sc = carp_ifp->if_softc;
2096 /* Set the source MAC address to Virtual Router MAC Address */
2097 switch (ifp->if_type) {
2100 struct ether_header *eh;
2102 eh = mtod(m, struct ether_header *);
2103 eh->ether_shost[0] = 0;
2104 eh->ether_shost[1] = 0;
2105 eh->ether_shost[2] = 0x5e;
2106 eh->ether_shost[3] = 0;
2107 eh->ether_shost[4] = 1;
2108 eh->ether_shost[5] = sc->sc_vhid;
2112 struct fddi_header *fh;
2114 fh = mtod(m, struct fddi_header *);
2115 fh->fddi_shost[0] = 0;
2116 fh->fddi_shost[1] = 0;
2117 fh->fddi_shost[2] = 0x5e;
2118 fh->fddi_shost[3] = 0;
2119 fh->fddi_shost[4] = 1;
2120 fh->fddi_shost[5] = sc->sc_vhid;
2123 case IFT_ISO88025: {
2124 struct iso88025_header *th;
2125 th = mtod(m, struct iso88025_header *);
2126 th->iso88025_shost[0] = 3;
2127 th->iso88025_shost[1] = 0;
2128 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2129 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2130 th->iso88025_shost[4] = 0;
2131 th->iso88025_shost[5] = 0;
2135 printf("%s: carp is not supported for this interface type\n",
2137 return (EOPNOTSUPP);
2144 carp_set_state(struct carp_softc *sc, int state)
2148 CARP_SCLOCK_ASSERT(sc);
2150 if (sc->sc_state == state)
2153 sc->sc_state = state;
2156 SC2IFP(sc)->if_link_state = LINK_STATE_DOWN;
2159 SC2IFP(sc)->if_link_state = LINK_STATE_UP;
2162 SC2IFP(sc)->if_link_state = LINK_STATE_UNKNOWN;
2165 rt_ifmsg(SC2IFP(sc));
2169 carp_carpdev_state(void *v)
2171 struct carp_if *cif = v;
2174 carp_carpdev_state_locked(cif);
2179 carp_carpdev_state_locked(struct carp_if *cif)
2181 struct carp_softc *sc;
2183 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2184 carp_sc_state_locked(sc);
2188 carp_sc_state_locked(struct carp_softc *sc)
2190 CARP_SCLOCK_ASSERT(sc);
2192 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2193 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2194 sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2195 SC2IFP(sc)->if_flags &= ~IFF_UP;
2196 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2197 callout_stop(&sc->sc_ad_tmo);
2198 callout_stop(&sc->sc_md_tmo);
2199 callout_stop(&sc->sc_md6_tmo);
2200 carp_set_state(sc, INIT);
2202 if (!sc->sc_suppress) {
2203 carp_suppress_preempt++;
2204 if (carp_suppress_preempt == 1) {
2210 sc->sc_suppress = 1;
2212 SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2213 carp_set_state(sc, INIT);
2215 if (sc->sc_suppress)
2216 carp_suppress_preempt--;
2217 sc->sc_suppress = 0;
2224 carp_modevent(module_t mod, int type, void *data)
2228 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2229 carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
2230 if (if_detach_event_tag == NULL)
2232 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2233 LIST_INIT(&carpif_list);
2234 if_clone_attach(&carp_cloner);
2238 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2239 if_clone_detach(&carp_cloner);
2240 mtx_destroy(&carp_mtx);
2250 static moduledata_t carp_mod = {
2256 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);