4 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
5 * Copyright (c) 2003 Ryan McBride. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
32 #include "opt_inet6.h"
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
40 #include <sys/malloc.h>
42 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/signalvar.h>
48 #include <sys/filio.h>
49 #include <sys/sockio.h>
51 #include <sys/socket.h>
52 #include <sys/vnode.h>
54 #include <machine/stdarg.h>
57 #include <net/ethernet.h>
59 #include <net/iso88025.h>
61 #include <net/if_clone.h>
62 #include <net/if_dl.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/if_ether.h>
73 #include <machine/in_cksum.h>
77 #include <netinet/icmp6.h>
78 #include <netinet/ip6.h>
79 #include <netinet6/ip6_var.h>
80 #include <netinet6/scope6_var.h>
81 #include <netinet6/nd6.h>
84 #include <crypto/sha1.h>
85 #include <netinet/ip_carp.h>
87 #define CARP_IFNAME "carp"
88 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
89 SYSCTL_DECL(_net_inet_carp);
92 struct ifnet *sc_ifp; /* Interface clue */
93 struct ifnet *sc_carpdev; /* Pointer to parent interface */
94 struct in_ifaddr *sc_ia; /* primary iface address */
95 struct ip_moptions sc_imo;
97 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
98 struct ip6_moptions sc_im6o;
100 TAILQ_ENTRY(carp_softc) sc_list;
102 enum { INIT = 0, BACKUP, MASTER } sc_state;
107 int sc_sendad_errors;
108 #define CARP_SENDAD_MAX_ERRORS 3
109 int sc_sendad_success;
110 #define CARP_SENDAD_MIN_SUCCESS 3
116 int sc_advbase; /* seconds */
118 u_int64_t sc_counter;
121 #define CARP_HMAC_PAD 64
122 unsigned char sc_key[CARP_KEY_LEN];
123 unsigned char sc_pad[CARP_HMAC_PAD];
126 struct callout sc_ad_tmo; /* advertisement timeout */
127 struct callout sc_md_tmo; /* master down timeout */
128 struct callout sc_md6_tmo; /* master down timeout */
130 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
132 #define SC2IFP(sc) ((sc)->sc_ifp)
134 int carp_suppress_preempt = 0;
135 int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
136 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
137 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
138 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
139 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
140 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
141 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
142 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
143 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
144 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
145 &carp_suppress_preempt, 0, "Preemption is suppressed");
147 struct carpstats carpstats;
148 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
149 &carpstats, carpstats,
150 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
153 TAILQ_HEAD(, carp_softc) vhif_vrs;
156 struct ifnet *vhif_ifp;
160 /* Get carp_if from softc. Valid after carp_set_addr{,6}. */
161 #define SC2CIF(sc) ((struct carp_if *)(sc)->sc_carpdev->if_carp)
163 /* lock per carp_if queue */
164 #define CARP_LOCK_INIT(cif) mtx_init(&(cif)->vhif_mtx, "carp_if", \
166 #define CARP_LOCK_DESTROY(cif) mtx_destroy(&(cif)->vhif_mtx)
167 #define CARP_LOCK_ASSERT(cif) mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
168 #define CARP_LOCK(cif) mtx_lock(&(cif)->vhif_mtx)
169 #define CARP_UNLOCK(cif) mtx_unlock(&(cif)->vhif_mtx)
171 #define CARP_SCLOCK(sc) mtx_lock(&SC2CIF(sc)->vhif_mtx)
172 #define CARP_SCUNLOCK(sc) mtx_unlock(&SC2CIF(sc)->vhif_mtx)
173 #define CARP_SCLOCK_ASSERT(sc) mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
175 #define CARP_LOG(...) do { \
176 if (carp_opts[CARPCTL_LOG] > 0) \
177 log(LOG_INFO, __VA_ARGS__); \
180 #define CARP_DEBUG(...) do { \
181 if (carp_opts[CARPCTL_LOG] > 1) \
182 log(LOG_DEBUG, __VA_ARGS__); \
185 static void carp_hmac_prepare(struct carp_softc *);
186 static void carp_hmac_generate(struct carp_softc *, u_int32_t *,
188 static int carp_hmac_verify(struct carp_softc *, u_int32_t *,
190 static void carp_setroute(struct carp_softc *, int);
191 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
192 static int carp_clone_create(struct if_clone *, int);
193 static void carp_clone_destroy(struct ifnet *);
194 static void carpdetach(struct carp_softc *);
195 static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
196 struct carp_header *);
197 static void carp_send_ad_all(void);
198 static void carp_send_ad(void *);
199 static void carp_send_ad_locked(struct carp_softc *);
200 static void carp_send_arp(struct carp_softc *);
201 static void carp_master_down(void *);
202 static void carp_master_down_locked(struct carp_softc *);
203 static int carp_ioctl(struct ifnet *, u_long, caddr_t);
204 static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
206 static void carp_start(struct ifnet *);
207 static void carp_setrun(struct carp_softc *, sa_family_t);
208 static void carp_set_state(struct carp_softc *, int);
209 static int carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
210 enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
212 static void carp_multicast_cleanup(struct carp_softc *);
213 static int carp_set_addr(struct carp_softc *, struct sockaddr_in *);
214 static int carp_del_addr(struct carp_softc *, struct sockaddr_in *);
215 static void carp_carpdev_state_locked(struct carp_if *);
216 static void carp_sc_state_locked(struct carp_softc *);
218 static void carp_send_na(struct carp_softc *);
219 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
220 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
223 static LIST_HEAD(, carp_softc) carpif_list;
224 static struct mtx carp_mtx;
225 IFC_SIMPLE_DECLARE(carp, 0);
227 static eventhandler_tag if_detach_event_tag;
229 static __inline u_int16_t
230 carp_cksum(struct mbuf *m, int len)
232 return (in_cksum(m, len));
236 carp_hmac_prepare(struct carp_softc *sc)
238 u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
239 u_int8_t vhid = sc->sc_vhid & 0xff;
249 /* XXX: possible race here */
251 /* compute ipad from key */
252 bzero(sc->sc_pad, sizeof(sc->sc_pad));
253 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
254 for (i = 0; i < sizeof(sc->sc_pad); i++)
255 sc->sc_pad[i] ^= 0x36;
257 /* precompute first part of inner hash */
258 SHA1Init(&sc->sc_sha1);
259 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
260 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
261 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
262 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
264 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
265 if (ifa->ifa_addr->sa_family == AF_INET)
266 SHA1Update(&sc->sc_sha1,
267 (void *)&ifatoia(ifa)->ia_addr.sin_addr.s_addr,
268 sizeof(struct in_addr));
272 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
273 if (ifa->ifa_addr->sa_family == AF_INET6) {
274 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
275 in6_clearscope(&in6);
276 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
281 /* convert ipad to opad */
282 for (i = 0; i < sizeof(sc->sc_pad); i++)
283 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
290 carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
291 unsigned char md[20])
295 /* fetch first half of inner hash */
296 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
298 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
299 SHA1Final(md, &sha1ctx);
303 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
304 SHA1Update(&sha1ctx, md, 20);
305 SHA1Final(md, &sha1ctx);
309 carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
310 unsigned char md[20])
312 unsigned char md2[20];
314 CARP_SCLOCK_ASSERT(sc);
316 carp_hmac_generate(sc, counter, md2);
318 return (bcmp(md, md2, sizeof(md2)));
322 carp_setroute(struct carp_softc *sc, int cmd)
328 CARP_SCLOCK_ASSERT(sc);
331 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
332 if (ifa->ifa_addr->sa_family == AF_INET &&
333 sc->sc_carpdev != NULL) {
334 int count = carp_addrcount(
335 (struct carp_if *)sc->sc_carpdev->if_carp,
336 ifatoia(ifa), CARP_COUNT_MASTER);
338 if ((cmd == RTM_ADD && count == 1) ||
339 (cmd == RTM_DELETE && count == 0))
340 rtinit(ifa, cmd, RTF_UP | RTF_HOST);
343 if (ifa->ifa_addr->sa_family == AF_INET6) {
355 carp_clone_create(struct if_clone *ifc, int unit)
358 struct carp_softc *sc;
361 MALLOC(sc, struct carp_softc *, sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
362 ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
368 sc->sc_flags_backup = 0;
370 sc->sc_advbase = CARP_DFLTINTV;
371 sc->sc_vhid = -1; /* required setting */
373 sc->sc_init_counter = 1;
374 sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
376 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
379 callout_init(&sc->sc_ad_tmo, NET_CALLOUT_MPSAFE);
380 callout_init(&sc->sc_md_tmo, NET_CALLOUT_MPSAFE);
381 callout_init(&sc->sc_md6_tmo, NET_CALLOUT_MPSAFE);
384 if_initname(ifp, CARP_IFNAME, unit);
385 ifp->if_mtu = ETHERMTU;
386 ifp->if_flags = IFF_LOOPBACK;
387 ifp->if_ioctl = carp_ioctl;
388 ifp->if_output = carp_looutput;
389 ifp->if_start = carp_start;
390 ifp->if_type = IFT_CARP;
391 ifp->if_snd.ifq_maxlen = ifqmaxlen;
394 bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
396 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
397 mtx_unlock(&carp_mtx);
402 carp_clone_destroy(struct ifnet *ifp)
404 struct carp_softc *sc = ifp->if_softc;
413 LIST_REMOVE(sc, sc_next);
414 mtx_unlock(&carp_mtx);
417 if_free_type(ifp, IFT_ETHER);
422 carpdetach(struct carp_softc *sc)
426 callout_stop(&sc->sc_ad_tmo);
427 callout_stop(&sc->sc_md_tmo);
428 callout_stop(&sc->sc_md6_tmo);
431 carp_suppress_preempt--;
434 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
435 carp_suppress_preempt--;
436 sc->sc_sendad_errors = 0;
438 carp_set_state(sc, INIT);
439 SC2IFP(sc)->if_flags &= ~IFF_UP;
441 carp_multicast_cleanup(sc);
443 if (sc->sc_carpdev != NULL) {
444 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
445 CARP_LOCK_ASSERT(cif);
446 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
447 if (!--cif->vhif_nvrs) {
448 ifpromisc(sc->sc_carpdev, 0);
449 sc->sc_carpdev->if_carp = NULL;
450 CARP_LOCK_DESTROY(cif);
454 sc->sc_carpdev = NULL;
457 /* Detach an interface from the carp. */
459 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
461 struct carp_if *cif = (struct carp_if *)ifp->if_carp;
462 struct carp_softc *sc, *nextsc;
468 * XXX: At the end of for() cycle the lock will be destroyed.
471 for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
472 nextsc = TAILQ_NEXT(sc, sc_list);
478 * process input packet.
479 * we have rearranged checks order compared to the rfc,
480 * but it seems more efficient this way or not possible otherwise.
483 carp_input(struct mbuf *m, int hlen)
485 struct ip *ip = mtod(m, struct ip *);
486 struct carp_header *ch;
489 carpstats.carps_ipackets++;
491 if (!carp_opts[CARPCTL_ALLOW]) {
496 /* check if received on a valid carp interface */
497 if (m->m_pkthdr.rcvif->if_carp == NULL) {
498 carpstats.carps_badif++;
499 CARP_LOG("carp_input: packet received on non-carp "
501 m->m_pkthdr.rcvif->if_xname);
506 /* verify that the IP TTL is 255. */
507 if (ip->ip_ttl != CARP_DFLTTL) {
508 carpstats.carps_badttl++;
509 CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
511 m->m_pkthdr.rcvif->if_xname);
516 iplen = ip->ip_hl << 2;
518 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
519 carpstats.carps_badlen++;
520 CARP_LOG("carp_input: received len %zd < "
521 "sizeof(struct carp_header)\n",
522 m->m_len - sizeof(struct ip));
527 if (iplen + sizeof(*ch) < m->m_len) {
528 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
529 carpstats.carps_hdrops++;
530 CARP_LOG("carp_input: pullup failed\n");
533 ip = mtod(m, struct ip *);
535 ch = (struct carp_header *)((char *)ip + iplen);
538 * verify that the received packet length is
539 * equal to the CARP header
541 len = iplen + sizeof(*ch);
542 if (len > m->m_pkthdr.len) {
543 carpstats.carps_badlen++;
544 CARP_LOG("carp_input: packet too short %d on %s\n",
546 m->m_pkthdr.rcvif->if_xname);
551 if ((m = m_pullup(m, len)) == NULL) {
552 carpstats.carps_hdrops++;
555 ip = mtod(m, struct ip *);
556 ch = (struct carp_header *)((char *)ip + iplen);
558 /* verify the CARP checksum */
560 if (carp_cksum(m, len - iplen)) {
561 carpstats.carps_badsum++;
562 CARP_LOG("carp_input: checksum failed on %s\n",
563 m->m_pkthdr.rcvif->if_xname);
569 carp_input_c(m, ch, AF_INET);
574 carp6_input(struct mbuf **mp, int *offp, int proto)
576 struct mbuf *m = *mp;
577 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
578 struct carp_header *ch;
581 carpstats.carps_ipackets6++;
583 if (!carp_opts[CARPCTL_ALLOW]) {
585 return (IPPROTO_DONE);
588 /* check if received on a valid carp interface */
589 if (m->m_pkthdr.rcvif->if_carp == NULL) {
590 carpstats.carps_badif++;
591 CARP_LOG("carp6_input: packet received on non-carp "
593 m->m_pkthdr.rcvif->if_xname);
595 return (IPPROTO_DONE);
598 /* verify that the IP TTL is 255 */
599 if (ip6->ip6_hlim != CARP_DFLTTL) {
600 carpstats.carps_badttl++;
601 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
603 m->m_pkthdr.rcvif->if_xname);
605 return (IPPROTO_DONE);
608 /* verify that we have a complete carp packet */
610 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
612 carpstats.carps_badlen++;
613 CARP_LOG("carp6_input: packet size %u too small\n", len);
614 return (IPPROTO_DONE);
618 /* verify the CARP checksum */
620 if (carp_cksum(m, sizeof(*ch))) {
621 carpstats.carps_badsum++;
622 CARP_LOG("carp6_input: checksum failed, on %s\n",
623 m->m_pkthdr.rcvif->if_xname);
625 return (IPPROTO_DONE);
629 carp_input_c(m, ch, AF_INET6);
630 return (IPPROTO_DONE);
635 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
637 struct ifnet *ifp = m->m_pkthdr.rcvif;
638 struct carp_softc *sc;
639 u_int64_t tmp_counter;
640 struct timeval sc_tv, ch_tv;
642 /* verify that the VHID is valid on the receiving interface */
643 CARP_LOCK(ifp->if_carp);
644 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
645 if (sc->sc_vhid == ch->carp_vhid)
648 if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
649 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
650 carpstats.carps_badvhid++;
651 CARP_UNLOCK(ifp->if_carp);
656 getmicrotime(&SC2IFP(sc)->if_lastchange);
657 SC2IFP(sc)->if_ipackets++;
658 SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
660 if (bpf_peers_present(SC2IFP(sc)->if_bpf)) {
661 struct ip *ip = mtod(m, struct ip *);
664 /* BPF wants net byte order */
665 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
666 ip->ip_off = htons(ip->ip_off);
667 bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
670 /* verify the CARP version. */
671 if (ch->carp_version != CARP_VERSION) {
672 carpstats.carps_badver++;
673 SC2IFP(sc)->if_ierrors++;
674 CARP_UNLOCK(ifp->if_carp);
675 CARP_LOG("%s; invalid version %d\n",
676 SC2IFP(sc)->if_xname,
682 /* verify the hash */
683 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
684 carpstats.carps_badauth++;
685 SC2IFP(sc)->if_ierrors++;
686 CARP_UNLOCK(ifp->if_carp);
687 CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
692 tmp_counter = ntohl(ch->carp_counter[0]);
693 tmp_counter = tmp_counter<<32;
694 tmp_counter += ntohl(ch->carp_counter[1]);
696 /* XXX Replay protection goes here */
698 sc->sc_init_counter = 0;
699 sc->sc_counter = tmp_counter;
701 sc_tv.tv_sec = sc->sc_advbase;
702 if (carp_suppress_preempt && sc->sc_advskew < 240)
703 sc_tv.tv_usec = 240 * 1000000 / 256;
705 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
706 ch_tv.tv_sec = ch->carp_advbase;
707 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
709 switch (sc->sc_state) {
714 * If we receive an advertisement from a master who's going to
715 * be more frequent than us, go into BACKUP state.
717 if (timevalcmp(&sc_tv, &ch_tv, >) ||
718 timevalcmp(&sc_tv, &ch_tv, ==)) {
719 callout_stop(&sc->sc_ad_tmo);
720 CARP_DEBUG("%s: MASTER -> BACKUP "
721 "(more frequent advertisement received)\n",
722 SC2IFP(sc)->if_xname);
723 carp_set_state(sc, BACKUP);
725 carp_setroute(sc, RTM_DELETE);
730 * If we're pre-empting masters who advertise slower than us,
731 * and this one claims to be slower, treat him as down.
733 if (carp_opts[CARPCTL_PREEMPT] &&
734 timevalcmp(&sc_tv, &ch_tv, <)) {
735 CARP_DEBUG("%s: BACKUP -> MASTER "
736 "(preempting a slower master)\n",
737 SC2IFP(sc)->if_xname);
738 carp_master_down_locked(sc);
743 * If the master is going to advertise at such a low frequency
744 * that he's guaranteed to time out, we'd might as well just
745 * treat him as timed out now.
747 sc_tv.tv_sec = sc->sc_advbase * 3;
748 if (timevalcmp(&sc_tv, &ch_tv, <)) {
749 CARP_DEBUG("%s: BACKUP -> MASTER "
750 "(master timed out)\n",
751 SC2IFP(sc)->if_xname);
752 carp_master_down_locked(sc);
757 * Otherwise, we reset the counter and wait for the next
764 CARP_UNLOCK(ifp->if_carp);
771 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
774 struct ifnet *ifp = SC2IFP(sc);
776 if (sc->sc_init_counter) {
777 /* this could also be seconds since unix epoch */
778 sc->sc_counter = arc4random();
779 sc->sc_counter = sc->sc_counter << 32;
780 sc->sc_counter += arc4random();
784 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
785 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
787 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
789 /* Tag packet for carp_output */
790 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
793 SC2IFP(sc)->if_oerrors++;
796 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
797 m_tag_prepend(m, mtag);
803 carp_send_ad_all(void)
805 struct carp_softc *sc;
808 LIST_FOREACH(sc, &carpif_list, sc_next) {
809 if (sc->sc_carpdev == NULL)
812 if ((SC2IFP(sc)->if_flags & IFF_UP) &&
813 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
814 sc->sc_state == MASTER)
815 carp_send_ad_locked(sc);
818 mtx_unlock(&carp_mtx);
822 carp_send_ad(void *v)
824 struct carp_softc *sc = v;
827 carp_send_ad_locked(sc);
832 carp_send_ad_locked(struct carp_softc *sc)
834 struct carp_header ch;
836 struct carp_header *ch_ptr;
838 int len, advbase, advskew;
840 CARP_SCLOCK_ASSERT(sc);
842 /* bow out if we've lost our UPness or RUNNINGuiness */
843 if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
844 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
848 advbase = sc->sc_advbase;
849 if (!carp_suppress_preempt || sc->sc_advskew > 240)
850 advskew = sc->sc_advskew;
854 tv.tv_usec = advskew * 1000000 / 256;
857 ch.carp_version = CARP_VERSION;
858 ch.carp_type = CARP_ADVERTISEMENT;
859 ch.carp_vhid = sc->sc_vhid;
860 ch.carp_advbase = advbase;
861 ch.carp_advskew = advskew;
862 ch.carp_authlen = 7; /* XXX DEFINE */
863 ch.carp_pad1 = 0; /* must be zero */
870 MGETHDR(m, M_DONTWAIT, MT_HEADER);
872 SC2IFP(sc)->if_oerrors++;
873 carpstats.carps_onomem++;
874 /* XXX maybe less ? */
875 if (advbase != 255 || advskew != 255)
876 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
880 len = sizeof(*ip) + sizeof(ch);
881 m->m_pkthdr.len = len;
882 m->m_pkthdr.rcvif = NULL;
884 MH_ALIGN(m, m->m_len);
885 m->m_flags |= M_MCAST;
886 ip = mtod(m, struct ip *);
887 ip->ip_v = IPVERSION;
888 ip->ip_hl = sizeof(*ip) >> 2;
889 ip->ip_tos = IPTOS_LOWDELAY;
891 ip->ip_id = ip_newid();
893 ip->ip_ttl = CARP_DFLTTL;
894 ip->ip_p = IPPROTO_CARP;
896 ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
897 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
899 ch_ptr = (struct carp_header *)(&ip[1]);
900 bcopy(&ch, ch_ptr, sizeof(ch));
901 if (carp_prepare_ad(m, sc, ch_ptr))
904 m->m_data += sizeof(*ip);
905 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
906 m->m_data -= sizeof(*ip);
908 getmicrotime(&SC2IFP(sc)->if_lastchange);
909 SC2IFP(sc)->if_opackets++;
910 SC2IFP(sc)->if_obytes += len;
911 carpstats.carps_opackets++;
913 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
914 SC2IFP(sc)->if_oerrors++;
915 if (sc->sc_sendad_errors < INT_MAX)
916 sc->sc_sendad_errors++;
917 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
918 carp_suppress_preempt++;
919 if (carp_suppress_preempt == 1) {
925 sc->sc_sendad_success = 0;
927 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
928 if (++sc->sc_sendad_success >=
929 CARP_SENDAD_MIN_SUCCESS) {
930 carp_suppress_preempt--;
931 sc->sc_sendad_errors = 0;
934 sc->sc_sendad_errors = 0;
942 MGETHDR(m, M_DONTWAIT, MT_HEADER);
944 SC2IFP(sc)->if_oerrors++;
945 carpstats.carps_onomem++;
946 /* XXX maybe less ? */
947 if (advbase != 255 || advskew != 255)
948 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
952 len = sizeof(*ip6) + sizeof(ch);
953 m->m_pkthdr.len = len;
954 m->m_pkthdr.rcvif = NULL;
956 MH_ALIGN(m, m->m_len);
957 m->m_flags |= M_MCAST;
958 ip6 = mtod(m, struct ip6_hdr *);
959 bzero(ip6, sizeof(*ip6));
960 ip6->ip6_vfc |= IPV6_VERSION;
961 ip6->ip6_hlim = CARP_DFLTTL;
962 ip6->ip6_nxt = IPPROTO_CARP;
963 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
964 sizeof(struct in6_addr));
965 /* set the multicast destination */
967 ip6->ip6_dst.s6_addr8[0] = 0xff;
968 ip6->ip6_dst.s6_addr8[1] = 0x02;
969 ip6->ip6_dst.s6_addr8[15] = 0x12;
971 ch_ptr = (struct carp_header *)(&ip6[1]);
972 bcopy(&ch, ch_ptr, sizeof(ch));
973 if (carp_prepare_ad(m, sc, ch_ptr))
976 m->m_data += sizeof(*ip6);
977 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
978 m->m_data -= sizeof(*ip6);
980 getmicrotime(&SC2IFP(sc)->if_lastchange);
981 SC2IFP(sc)->if_opackets++;
982 SC2IFP(sc)->if_obytes += len;
983 carpstats.carps_opackets6++;
985 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
986 SC2IFP(sc)->if_oerrors++;
987 if (sc->sc_sendad_errors < INT_MAX)
988 sc->sc_sendad_errors++;
989 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
990 carp_suppress_preempt++;
991 if (carp_suppress_preempt == 1) {
997 sc->sc_sendad_success = 0;
999 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1000 if (++sc->sc_sendad_success >=
1001 CARP_SENDAD_MIN_SUCCESS) {
1002 carp_suppress_preempt--;
1003 sc->sc_sendad_errors = 0;
1006 sc->sc_sendad_errors = 0;
1011 if (advbase != 255 || advskew != 255)
1012 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1018 * Broadcast a gratuitous ARP request containing
1019 * the virtual router MAC address for each IP address
1020 * associated with the virtual router.
1023 carp_send_arp(struct carp_softc *sc)
1027 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1029 if (ifa->ifa_addr->sa_family != AF_INET)
1032 /* arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
1033 arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1035 DELAY(1000); /* XXX */
1041 carp_send_na(struct carp_softc *sc)
1044 struct in6_addr *in6;
1045 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1047 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1049 if (ifa->ifa_addr->sa_family != AF_INET6)
1052 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1053 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1054 ND_NA_FLAG_OVERRIDE, 1, NULL);
1055 DELAY(1000); /* XXX */
1061 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1063 struct carp_softc *vh;
1067 CARP_LOCK_ASSERT(cif);
1069 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1070 if ((type == CARP_COUNT_RUNNING &&
1071 (SC2IFP(vh)->if_flags & IFF_UP) &&
1072 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1073 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1074 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1076 if (ifa->ifa_addr->sa_family == AF_INET &&
1077 ia->ia_addr.sin_addr.s_addr ==
1078 ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1087 carp_iamatch(void *v, struct in_ifaddr *ia,
1088 struct in_addr *isaddr, u_int8_t **enaddr)
1090 struct carp_if *cif = v;
1091 struct carp_softc *vh;
1092 int index, count = 0;
1097 if (carp_opts[CARPCTL_ARPBALANCE]) {
1099 * XXX proof of concept implementation.
1100 * We use the source ip to decide which virtual host should
1101 * handle the request. If we're master of that virtual host,
1102 * then we respond, otherwise, just drop the arp packet on
1105 count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1107 /* should never reach this */
1112 /* this should be a hash, like pf_hash() */
1113 index = ntohl(isaddr->s_addr) % count;
1116 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1117 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1118 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1119 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1121 if (ifa->ifa_addr->sa_family ==
1123 ia->ia_addr.sin_addr.s_addr ==
1124 ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1125 if (count == index) {
1128 *enaddr = IF_LLADDR(vh->sc_ifp);
1142 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1143 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1144 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1145 ia->ia_ifp == SC2IFP(vh) &&
1146 vh->sc_state == MASTER) {
1147 *enaddr = IF_LLADDR(vh->sc_ifp);
1159 carp_iamatch6(void *v, struct in6_addr *taddr)
1161 struct carp_if *cif = v;
1162 struct carp_softc *vh;
1166 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1167 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1168 if (IN6_ARE_ADDR_EQUAL(taddr,
1169 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1170 (SC2IFP(vh)->if_flags & IFF_UP) &&
1171 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1172 vh->sc_state == MASTER) {
1184 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1187 struct carp_if *cif = v;
1188 struct carp_softc *sc;
1192 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1193 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1194 if (IN6_ARE_ADDR_EQUAL(taddr,
1195 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1196 (SC2IFP(sc)->if_flags & IFF_UP) &&
1197 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1198 struct ifnet *ifp = SC2IFP(sc);
1199 mtag = m_tag_get(PACKET_TAG_CARP,
1200 sizeof(struct ifnet *), M_NOWAIT);
1202 /* better a bit than nothing */
1204 return (IF_LLADDR(sc->sc_ifp));
1206 bcopy(&ifp, (caddr_t)(mtag + 1),
1207 sizeof(struct ifnet *));
1208 m_tag_prepend(m, mtag);
1211 return (IF_LLADDR(sc->sc_ifp));
1222 carp_forus(void *v, void *dhost)
1224 struct carp_if *cif = v;
1225 struct carp_softc *vh;
1226 u_int8_t *ena = dhost;
1228 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1232 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1233 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1234 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1235 vh->sc_state == MASTER &&
1236 !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1238 return (SC2IFP(vh));
1246 carp_master_down(void *v)
1248 struct carp_softc *sc = v;
1251 carp_master_down_locked(sc);
1256 carp_master_down_locked(struct carp_softc *sc)
1259 CARP_SCLOCK_ASSERT(sc);
1261 switch (sc->sc_state) {
1263 printf("%s: master_down event in INIT state\n",
1264 SC2IFP(sc)->if_xname);
1269 carp_set_state(sc, MASTER);
1270 carp_send_ad_locked(sc);
1276 carp_setroute(sc, RTM_ADD);
1282 * When in backup state, af indicates whether to reset the master down timer
1283 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1286 carp_setrun(struct carp_softc *sc, sa_family_t af)
1290 if (sc->sc_carpdev == NULL) {
1291 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1292 carp_set_state(sc, INIT);
1295 CARP_SCLOCK_ASSERT(sc);
1297 if (SC2IFP(sc)->if_flags & IFF_UP &&
1298 sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1299 SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1301 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1302 carp_setroute(sc, RTM_DELETE);
1306 switch (sc->sc_state) {
1308 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1309 carp_send_ad_locked(sc);
1314 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1315 SC2IFP(sc)->if_xname);
1316 carp_set_state(sc, MASTER);
1317 carp_setroute(sc, RTM_ADD);
1319 CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1320 carp_set_state(sc, BACKUP);
1321 carp_setroute(sc, RTM_DELETE);
1326 callout_stop(&sc->sc_ad_tmo);
1327 tv.tv_sec = 3 * sc->sc_advbase;
1328 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1332 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1333 carp_master_down, sc);
1338 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1339 carp_master_down, sc);
1344 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1345 carp_master_down, sc);
1347 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1348 carp_master_down, sc);
1353 tv.tv_sec = sc->sc_advbase;
1354 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1355 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1362 carp_multicast_cleanup(struct carp_softc *sc)
1364 struct ip_moptions *imo = &sc->sc_imo;
1366 struct ip6_moptions *im6o = &sc->sc_im6o;
1368 u_int16_t n = imo->imo_num_memberships;
1370 /* Clean up our own multicast memberships */
1372 if (imo->imo_membership[n] != NULL) {
1373 in_delmulti(imo->imo_membership[n]);
1374 imo->imo_membership[n] = NULL;
1377 imo->imo_num_memberships = 0;
1378 imo->imo_multicast_ifp = NULL;
1381 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1382 struct in6_multi_mship *imm =
1383 LIST_FIRST(&im6o->im6o_memberships);
1385 LIST_REMOVE(imm, i6mm_chain);
1386 in6_leavegroup(imm);
1388 im6o->im6o_multicast_ifp = NULL;
1393 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1396 struct carp_if *cif;
1397 struct in_ifaddr *ia, *ia_if;
1398 struct ip_moptions *imo = &sc->sc_imo;
1399 struct in_addr addr;
1400 u_long iaddr = htonl(sin->sin_addr.s_addr);
1403 if (sin->sin_addr.s_addr == 0) {
1404 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1405 carp_set_state(sc, INIT);
1407 SC2IFP(sc)->if_flags |= IFF_UP;
1412 /* we have to do it by hands to check we won't match on us */
1413 ia_if = NULL; own = 0;
1414 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
1415 /* and, yeah, we need a multicast-capable iface too */
1416 if (ia->ia_ifp != SC2IFP(sc) &&
1417 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1418 (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1421 if (sin->sin_addr.s_addr ==
1422 ia->ia_addr.sin_addr.s_addr)
1428 return (EADDRNOTAVAIL);
1433 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1434 (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1435 return (EADDRNOTAVAIL);
1437 if (imo->imo_num_memberships == 0) {
1438 addr.s_addr = htonl(INADDR_CARP_GROUP);
1439 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1441 imo->imo_num_memberships++;
1442 imo->imo_multicast_ifp = ifp;
1443 imo->imo_multicast_ttl = CARP_DFLTTL;
1444 imo->imo_multicast_loop = 0;
1447 if (!ifp->if_carp) {
1449 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1455 if ((error = ifpromisc(ifp, 1))) {
1460 CARP_LOCK_INIT(cif);
1462 cif->vhif_ifp = ifp;
1463 TAILQ_INIT(&cif->vhif_vrs);
1467 struct carp_softc *vr;
1469 cif = (struct carp_if *)ifp->if_carp;
1471 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1472 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1479 sc->sc_carpdev = ifp;
1481 { /* XXX prevent endless loop if already in queue */
1482 struct carp_softc *vr, *after = NULL;
1484 cif = (struct carp_if *)ifp->if_carp;
1486 /* XXX: cif should not change, right? So we still hold the lock */
1487 CARP_LOCK_ASSERT(cif);
1489 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1492 if (vr->sc_vhid < sc->sc_vhid)
1497 /* We're trying to keep things in order */
1498 if (after == NULL) {
1499 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1501 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1508 SC2IFP(sc)->if_flags |= IFF_UP;
1511 carp_sc_state_locked(sc);
1519 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1524 carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1528 if (!--sc->sc_naddrs) {
1529 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1530 struct ip_moptions *imo = &sc->sc_imo;
1533 callout_stop(&sc->sc_ad_tmo);
1534 SC2IFP(sc)->if_flags &= ~IFF_UP;
1535 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1537 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1538 imo->imo_multicast_ifp = NULL;
1539 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1540 if (!--cif->vhif_nvrs) {
1541 sc->sc_carpdev->if_carp = NULL;
1542 CARP_LOCK_DESTROY(cif);
1543 FREE(cif, M_IFADDR);
1554 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1557 struct carp_if *cif;
1558 struct in6_ifaddr *ia, *ia_if;
1559 struct ip6_moptions *im6o = &sc->sc_im6o;
1560 struct in6_multi_mship *imm;
1561 struct in6_addr in6;
1564 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1565 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1566 carp_set_state(sc, INIT);
1568 SC2IFP(sc)->if_flags |= IFF_UP;
1573 /* we have to do it by hands to check we won't match on us */
1574 ia_if = NULL; own = 0;
1575 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1578 for (i = 0; i < 4; i++) {
1579 if ((sin6->sin6_addr.s6_addr32[i] &
1580 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1581 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1582 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1585 /* and, yeah, we need a multicast-capable iface too */
1586 if (ia->ia_ifp != SC2IFP(sc) &&
1587 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1591 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1592 &ia->ia_addr.sin6_addr))
1598 return (EADDRNOTAVAIL);
1602 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1603 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1604 return (EADDRNOTAVAIL);
1606 if (!sc->sc_naddrs6) {
1607 im6o->im6o_multicast_ifp = ifp;
1609 /* join CARP multicast address */
1610 bzero(&in6, sizeof(in6));
1611 in6.s6_addr16[0] = htons(0xff02);
1612 in6.s6_addr8[15] = 0x12;
1613 if (in6_setscope(&in6, ifp, NULL) != 0)
1615 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1617 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1619 /* join solicited multicast address */
1620 bzero(&in6, sizeof(in6));
1621 in6.s6_addr16[0] = htons(0xff02);
1622 in6.s6_addr32[1] = 0;
1623 in6.s6_addr32[2] = htonl(1);
1624 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1625 in6.s6_addr8[12] = 0xff;
1626 if (in6_setscope(&in6, ifp, NULL) != 0)
1628 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1630 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1633 if (!ifp->if_carp) {
1634 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1640 if ((error = ifpromisc(ifp, 1))) {
1645 CARP_LOCK_INIT(cif);
1647 cif->vhif_ifp = ifp;
1648 TAILQ_INIT(&cif->vhif_vrs);
1652 struct carp_softc *vr;
1654 cif = (struct carp_if *)ifp->if_carp;
1656 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1657 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1664 sc->sc_carpdev = ifp;
1666 { /* XXX prevent endless loop if already in queue */
1667 struct carp_softc *vr, *after = NULL;
1669 cif = (struct carp_if *)ifp->if_carp;
1670 CARP_LOCK_ASSERT(cif);
1672 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1675 if (vr->sc_vhid < sc->sc_vhid)
1680 /* We're trying to keep things in order */
1681 if (after == NULL) {
1682 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1684 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1691 SC2IFP(sc)->if_flags |= IFF_UP;
1694 carp_sc_state_locked(sc);
1702 /* clean up multicast memberships */
1703 if (!sc->sc_naddrs6) {
1704 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1705 imm = LIST_FIRST(&im6o->im6o_memberships);
1706 LIST_REMOVE(imm, i6mm_chain);
1707 in6_leavegroup(imm);
1714 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1718 if (!--sc->sc_naddrs6) {
1719 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1720 struct ip6_moptions *im6o = &sc->sc_im6o;
1723 callout_stop(&sc->sc_ad_tmo);
1724 SC2IFP(sc)->if_flags &= ~IFF_UP;
1725 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1727 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1728 struct in6_multi_mship *imm =
1729 LIST_FIRST(&im6o->im6o_memberships);
1731 LIST_REMOVE(imm, i6mm_chain);
1732 in6_leavegroup(imm);
1734 im6o->im6o_multicast_ifp = NULL;
1735 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1736 if (!--cif->vhif_nvrs) {
1737 CARP_LOCK_DESTROY(cif);
1738 sc->sc_carpdev->if_carp = NULL;
1739 FREE(cif, M_IFADDR);
1749 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1751 struct carp_softc *sc = ifp->if_softc, *vr;
1752 struct carpreq carpr;
1755 struct ifaliasreq *ifra;
1756 int locked = 0, error = 0;
1758 ifa = (struct ifaddr *)addr;
1759 ifra = (struct ifaliasreq *)addr;
1760 ifr = (struct ifreq *)addr;
1764 switch (ifa->ifa_addr->sa_family) {
1767 SC2IFP(sc)->if_flags |= IFF_UP;
1768 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1769 sizeof(struct sockaddr));
1770 error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1775 SC2IFP(sc)->if_flags |= IFF_UP;
1776 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1780 error = EAFNOSUPPORT;
1786 switch (ifa->ifa_addr->sa_family) {
1789 SC2IFP(sc)->if_flags |= IFF_UP;
1790 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1791 sizeof(struct sockaddr));
1792 error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1797 SC2IFP(sc)->if_flags |= IFF_UP;
1798 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1802 error = EAFNOSUPPORT;
1808 switch (ifa->ifa_addr->sa_family) {
1811 error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1816 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1820 error = EAFNOSUPPORT;
1826 if (sc->sc_carpdev) {
1830 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1831 callout_stop(&sc->sc_ad_tmo);
1832 callout_stop(&sc->sc_md_tmo);
1833 callout_stop(&sc->sc_md6_tmo);
1834 if (sc->sc_state == MASTER)
1835 carp_send_ad_locked(sc);
1836 carp_set_state(sc, INIT);
1838 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1839 SC2IFP(sc)->if_flags |= IFF_UP;
1845 if ((error = suser(curthread)) != 0)
1847 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1850 if (sc->sc_carpdev) {
1854 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1855 switch (carpr.carpr_state) {
1857 callout_stop(&sc->sc_ad_tmo);
1858 carp_set_state(sc, BACKUP);
1860 carp_setroute(sc, RTM_DELETE);
1863 carp_master_down_locked(sc);
1869 if (carpr.carpr_vhid > 0) {
1870 if (carpr.carpr_vhid > 255) {
1874 if (sc->sc_carpdev) {
1875 struct carp_if *cif;
1876 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1877 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1879 vr->sc_vhid == carpr.carpr_vhid)
1882 sc->sc_vhid = carpr.carpr_vhid;
1883 IF_LLADDR(sc->sc_ifp)[0] = 0;
1884 IF_LLADDR(sc->sc_ifp)[1] = 0;
1885 IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1886 IF_LLADDR(sc->sc_ifp)[3] = 0;
1887 IF_LLADDR(sc->sc_ifp)[4] = 1;
1888 IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1891 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1892 if (carpr.carpr_advskew >= 255) {
1896 if (carpr.carpr_advbase > 255) {
1900 sc->sc_advbase = carpr.carpr_advbase;
1901 sc->sc_advskew = carpr.carpr_advskew;
1904 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1914 /* XXX: lockless read */
1915 bzero(&carpr, sizeof(carpr));
1916 carpr.carpr_state = sc->sc_state;
1917 carpr.carpr_vhid = sc->sc_vhid;
1918 carpr.carpr_advbase = sc->sc_advbase;
1919 carpr.carpr_advskew = sc->sc_advskew;
1920 if (suser(curthread) == 0)
1921 bcopy(sc->sc_key, carpr.carpr_key,
1922 sizeof(carpr.carpr_key));
1923 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1933 carp_hmac_prepare(sc);
1939 * XXX: this is looutput. We should eventually use it from there.
1942 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1947 M_ASSERTPKTHDR(m); /* check if we have the packet header */
1949 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1951 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1952 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1956 ifp->if_obytes += m->m_pkthdr.len;
1958 /* BPF writes need to be handled specially. */
1959 if (dst->sa_family == AF_UNSPEC) {
1960 bcopy(dst->sa_data, &af, sizeof(af));
1961 dst->sa_family = af;
1965 switch (dst->sa_family) {
1972 printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
1974 return (EAFNOSUPPORT);
1977 return(if_simloop(ifp, m, dst->sa_family, 0));
1981 * Start output on carp interface. This function should never be called.
1984 carp_start(struct ifnet *ifp)
1987 printf("%s: start called\n", ifp->if_xname);
1992 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1996 struct carp_softc *sc;
1997 struct ifnet *carp_ifp;
2002 switch (sa->sa_family) {
2015 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2019 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2020 sc = carp_ifp->if_softc;
2022 /* Set the source MAC address to Virtual Router MAC Address */
2023 switch (ifp->if_type) {
2026 struct ether_header *eh;
2028 eh = mtod(m, struct ether_header *);
2029 eh->ether_shost[0] = 0;
2030 eh->ether_shost[1] = 0;
2031 eh->ether_shost[2] = 0x5e;
2032 eh->ether_shost[3] = 0;
2033 eh->ether_shost[4] = 1;
2034 eh->ether_shost[5] = sc->sc_vhid;
2038 struct fddi_header *fh;
2040 fh = mtod(m, struct fddi_header *);
2041 fh->fddi_shost[0] = 0;
2042 fh->fddi_shost[1] = 0;
2043 fh->fddi_shost[2] = 0x5e;
2044 fh->fddi_shost[3] = 0;
2045 fh->fddi_shost[4] = 1;
2046 fh->fddi_shost[5] = sc->sc_vhid;
2049 case IFT_ISO88025: {
2050 struct iso88025_header *th;
2051 th = mtod(m, struct iso88025_header *);
2052 th->iso88025_shost[0] = 3;
2053 th->iso88025_shost[1] = 0;
2054 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2055 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2056 th->iso88025_shost[4] = 0;
2057 th->iso88025_shost[5] = 0;
2061 printf("%s: carp is not supported for this interface type\n",
2063 return (EOPNOTSUPP);
2070 carp_set_state(struct carp_softc *sc, int state)
2074 CARP_SCLOCK_ASSERT(sc);
2076 if (sc->sc_state == state)
2079 sc->sc_state = state;
2082 SC2IFP(sc)->if_link_state = LINK_STATE_DOWN;
2085 SC2IFP(sc)->if_link_state = LINK_STATE_UP;
2088 SC2IFP(sc)->if_link_state = LINK_STATE_UNKNOWN;
2091 rt_ifmsg(SC2IFP(sc));
2095 carp_carpdev_state(void *v)
2097 struct carp_if *cif = v;
2100 carp_carpdev_state_locked(cif);
2105 carp_carpdev_state_locked(struct carp_if *cif)
2107 struct carp_softc *sc;
2109 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2110 carp_sc_state_locked(sc);
2114 carp_sc_state_locked(struct carp_softc *sc)
2116 CARP_SCLOCK_ASSERT(sc);
2118 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2119 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2120 sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2121 SC2IFP(sc)->if_flags &= ~IFF_UP;
2122 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2123 callout_stop(&sc->sc_ad_tmo);
2124 callout_stop(&sc->sc_md_tmo);
2125 callout_stop(&sc->sc_md6_tmo);
2126 carp_set_state(sc, INIT);
2128 if (!sc->sc_suppress) {
2129 carp_suppress_preempt++;
2130 if (carp_suppress_preempt == 1) {
2136 sc->sc_suppress = 1;
2138 SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2139 carp_set_state(sc, INIT);
2141 if (sc->sc_suppress)
2142 carp_suppress_preempt--;
2143 sc->sc_suppress = 0;
2150 carp_modevent(module_t mod, int type, void *data)
2154 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2155 carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
2156 if (if_detach_event_tag == NULL)
2158 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2159 LIST_INIT(&carpif_list);
2160 if_clone_attach(&carp_cloner);
2164 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2165 if_clone_detach(&carp_cloner);
2166 mtx_destroy(&carp_mtx);
2176 static moduledata_t carp_mod = {
2182 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);