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>
56 #include <machine/stdarg.h>
59 #include <net/ethernet.h>
61 #include <net/iso88025.h>
63 #include <net/if_clone.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #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 IF_ADDR_LOCK(SC2IFP(sc));
277 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
278 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
279 if (ifa->ifa_addr->sa_family == AF_INET &&
280 ntohl(in.s_addr) > ntohl(last.s_addr) &&
281 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
282 cur.s_addr = in.s_addr;
286 IF_ADDR_UNLOCK(SC2IFP(sc));
288 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
292 memset(&cur6, 0, sizeof(cur6));
296 memset(&cur6, 0xff, sizeof(cur6));
297 IF_ADDR_LOCK(SC2IFP(sc));
298 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
299 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
300 if (IN6_IS_SCOPE_EMBED(&in6))
301 in6.s6_addr16[1] = 0;
302 if (ifa->ifa_addr->sa_family == AF_INET6 &&
303 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
304 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
309 IF_ADDR_UNLOCK(SC2IFP(sc));
311 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
315 /* convert ipad to opad */
316 for (i = 0; i < sizeof(sc->sc_pad); i++)
317 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
324 carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
325 unsigned char md[20])
329 /* fetch first half of inner hash */
330 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
332 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
333 SHA1Final(md, &sha1ctx);
337 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
338 SHA1Update(&sha1ctx, md, 20);
339 SHA1Final(md, &sha1ctx);
343 carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
344 unsigned char md[20])
346 unsigned char md2[20];
348 CARP_SCLOCK_ASSERT(sc);
350 carp_hmac_generate(sc, counter, md2);
352 return (bcmp(md, md2, sizeof(md2)));
356 carp_setroute(struct carp_softc *sc, int cmd)
362 CARP_SCLOCK_ASSERT(sc);
365 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
366 if (ifa->ifa_addr->sa_family == AF_INET &&
367 sc->sc_carpdev != NULL) {
368 int count = carp_addrcount(
369 (struct carp_if *)sc->sc_carpdev->if_carp,
370 ifatoia(ifa), CARP_COUNT_MASTER);
372 if ((cmd == RTM_ADD && count == 1) ||
373 (cmd == RTM_DELETE && count == 0))
374 rtinit(ifa, cmd, RTF_UP | RTF_HOST);
381 carp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
384 struct carp_softc *sc;
387 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
388 ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
394 sc->sc_flags_backup = 0;
396 sc->sc_advbase = CARP_DFLTINTV;
397 sc->sc_vhid = -1; /* required setting */
399 sc->sc_init_counter = 1;
400 sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
401 sc->sc_imo.imo_membership = (struct in_multi **)malloc(
402 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
404 sc->sc_imo.imo_mfilters = NULL;
405 sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
406 sc->sc_imo.imo_multicast_vif = -1;
408 sc->sc_im6o.im6o_membership = (struct in6_multi **)malloc(
409 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
411 sc->sc_im6o.im6o_mfilters = NULL;
412 sc->sc_im6o.im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
413 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
416 callout_init(&sc->sc_ad_tmo, CALLOUT_MPSAFE);
417 callout_init(&sc->sc_md_tmo, CALLOUT_MPSAFE);
418 callout_init(&sc->sc_md6_tmo, CALLOUT_MPSAFE);
421 if_initname(ifp, CARP_IFNAME, unit);
422 ifp->if_mtu = ETHERMTU;
423 ifp->if_flags = IFF_LOOPBACK;
424 ifp->if_ioctl = carp_ioctl;
425 ifp->if_output = carp_looutput;
426 ifp->if_start = carp_start;
427 ifp->if_type = IFT_CARP;
428 ifp->if_snd.ifq_maxlen = ifqmaxlen;
431 bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
433 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
434 mtx_unlock(&carp_mtx);
439 carp_clone_destroy(struct ifnet *ifp)
441 struct carp_softc *sc = ifp->if_softc;
445 carpdetach(sc, 1); /* Returns unlocked. */
448 LIST_REMOVE(sc, sc_next);
449 mtx_unlock(&carp_mtx);
452 if_free_type(ifp, IFT_ETHER);
453 free(sc->sc_imo.imo_membership, M_CARP);
455 free(sc->sc_im6o.im6o_membership, M_CARP);
461 * This function can be called on CARP interface destroy path,
462 * and in case of the removal of the underlying interface as
463 * well. We differentiate these two cases. In the latter case
464 * we do not cleanup our multicast memberships, since they
465 * are already freed. Also, in the latter case we do not
466 * release the lock on return, because the function will be
467 * called once more, for another CARP instance on the same
471 carpdetach(struct carp_softc *sc, int unlock)
475 callout_stop(&sc->sc_ad_tmo);
476 callout_stop(&sc->sc_md_tmo);
477 callout_stop(&sc->sc_md6_tmo);
480 carp_suppress_preempt--;
483 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
484 carp_suppress_preempt--;
485 sc->sc_sendad_errors = 0;
487 carp_set_state(sc, INIT);
488 SC2IFP(sc)->if_flags &= ~IFF_UP;
491 carp_multicast_cleanup(sc);
493 carp_multicast6_cleanup(sc);
496 if (sc->sc_carpdev != NULL) {
497 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
498 CARP_LOCK_ASSERT(cif);
499 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
500 if (!--cif->vhif_nvrs) {
501 ifpromisc(sc->sc_carpdev, 0);
502 sc->sc_carpdev->if_carp = NULL;
503 CARP_LOCK_DESTROY(cif);
507 sc->sc_carpdev = NULL;
511 /* Detach an interface from the carp. */
513 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
515 struct carp_if *cif = (struct carp_if *)ifp->if_carp;
516 struct carp_softc *sc, *nextsc;
522 * XXX: At the end of for() cycle the lock will be destroyed.
525 for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
526 nextsc = TAILQ_NEXT(sc, sc_list);
532 * process input packet.
533 * we have rearranged checks order compared to the rfc,
534 * but it seems more efficient this way or not possible otherwise.
537 carp_input(struct mbuf *m, int hlen)
539 struct ip *ip = mtod(m, struct ip *);
540 struct carp_header *ch;
543 CARPSTATS_INC(carps_ipackets);
545 if (!carp_opts[CARPCTL_ALLOW]) {
550 /* check if received on a valid carp interface */
551 if (m->m_pkthdr.rcvif->if_carp == NULL) {
552 CARPSTATS_INC(carps_badif);
553 CARP_DEBUG("carp_input: packet received on non-carp "
555 m->m_pkthdr.rcvif->if_xname);
560 /* verify that the IP TTL is 255. */
561 if (ip->ip_ttl != CARP_DFLTTL) {
562 CARPSTATS_INC(carps_badttl);
563 CARP_DEBUG("carp_input: received ttl %d != 255 on %s\n",
565 m->m_pkthdr.rcvif->if_xname);
570 iplen = ip->ip_hl << 2;
572 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
573 CARPSTATS_INC(carps_badlen);
574 CARP_DEBUG("carp_input: received len %zd < "
575 "sizeof(struct carp_header) on %s\n",
576 m->m_len - sizeof(struct ip),
577 m->m_pkthdr.rcvif->if_xname);
582 if (iplen + sizeof(*ch) < m->m_len) {
583 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
584 CARPSTATS_INC(carps_hdrops);
585 CARP_DEBUG("carp_input: pullup failed\n");
588 ip = mtod(m, struct ip *);
590 ch = (struct carp_header *)((char *)ip + iplen);
593 * verify that the received packet length is
594 * equal to the CARP header
596 len = iplen + sizeof(*ch);
597 if (len > m->m_pkthdr.len) {
598 CARPSTATS_INC(carps_badlen);
599 CARP_DEBUG("carp_input: packet too short %d on %s\n",
601 m->m_pkthdr.rcvif->if_xname);
606 if ((m = m_pullup(m, len)) == NULL) {
607 CARPSTATS_INC(carps_hdrops);
610 ip = mtod(m, struct ip *);
611 ch = (struct carp_header *)((char *)ip + iplen);
613 /* verify the CARP checksum */
615 if (carp_cksum(m, len - iplen)) {
616 CARPSTATS_INC(carps_badsum);
617 CARP_DEBUG("carp_input: checksum failed on %s\n",
618 m->m_pkthdr.rcvif->if_xname);
624 carp_input_c(m, ch, AF_INET);
629 carp6_input(struct mbuf **mp, int *offp, int proto)
631 struct mbuf *m = *mp;
632 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
633 struct carp_header *ch;
636 CARPSTATS_INC(carps_ipackets6);
638 if (!carp_opts[CARPCTL_ALLOW]) {
640 return (IPPROTO_DONE);
643 /* check if received on a valid carp interface */
644 if (m->m_pkthdr.rcvif->if_carp == NULL) {
645 CARPSTATS_INC(carps_badif);
646 CARP_DEBUG("carp6_input: packet received on non-carp "
648 m->m_pkthdr.rcvif->if_xname);
650 return (IPPROTO_DONE);
653 /* verify that the IP TTL is 255 */
654 if (ip6->ip6_hlim != CARP_DFLTTL) {
655 CARPSTATS_INC(carps_badttl);
656 CARP_DEBUG("carp6_input: received ttl %d != 255 on %s\n",
658 m->m_pkthdr.rcvif->if_xname);
660 return (IPPROTO_DONE);
663 /* verify that we have a complete carp packet */
665 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
667 CARPSTATS_INC(carps_badlen);
668 CARP_DEBUG("carp6_input: packet size %u too small\n", len);
669 return (IPPROTO_DONE);
673 /* verify the CARP checksum */
675 if (carp_cksum(m, sizeof(*ch))) {
676 CARPSTATS_INC(carps_badsum);
677 CARP_DEBUG("carp6_input: checksum failed, on %s\n",
678 m->m_pkthdr.rcvif->if_xname);
680 return (IPPROTO_DONE);
684 carp_input_c(m, ch, AF_INET6);
685 return (IPPROTO_DONE);
690 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
692 struct ifnet *ifp = m->m_pkthdr.rcvif;
693 struct carp_softc *sc;
694 u_int64_t tmp_counter;
695 struct timeval sc_tv, ch_tv;
697 /* verify that the VHID is valid on the receiving interface */
698 CARP_LOCK(ifp->if_carp);
699 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
700 if (sc->sc_vhid == ch->carp_vhid)
703 if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
704 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
705 CARPSTATS_INC(carps_badvhid);
706 CARP_UNLOCK(ifp->if_carp);
711 getmicrotime(&SC2IFP(sc)->if_lastchange);
712 SC2IFP(sc)->if_ipackets++;
713 SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
715 if (bpf_peers_present(SC2IFP(sc)->if_bpf)) {
716 struct ip *ip = mtod(m, struct ip *);
719 /* BPF wants net byte order */
720 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
721 ip->ip_off = htons(ip->ip_off);
722 bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
725 /* verify the CARP version. */
726 if (ch->carp_version != CARP_VERSION) {
727 CARPSTATS_INC(carps_badver);
728 SC2IFP(sc)->if_ierrors++;
729 CARP_UNLOCK(ifp->if_carp);
730 CARP_DEBUG("%s; invalid version %d\n",
731 SC2IFP(sc)->if_xname,
737 /* verify the hash */
738 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
739 CARPSTATS_INC(carps_badauth);
740 SC2IFP(sc)->if_ierrors++;
741 CARP_UNLOCK(ifp->if_carp);
742 CARP_DEBUG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
747 tmp_counter = ntohl(ch->carp_counter[0]);
748 tmp_counter = tmp_counter<<32;
749 tmp_counter += ntohl(ch->carp_counter[1]);
751 /* XXX Replay protection goes here */
753 sc->sc_init_counter = 0;
754 sc->sc_counter = tmp_counter;
756 sc_tv.tv_sec = sc->sc_advbase;
757 if (carp_suppress_preempt && sc->sc_advskew < 240)
758 sc_tv.tv_usec = 240 * 1000000 / 256;
760 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
761 ch_tv.tv_sec = ch->carp_advbase;
762 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
764 switch (sc->sc_state) {
769 * If we receive an advertisement from a master who's going to
770 * be more frequent than us, go into BACKUP state.
772 if (timevalcmp(&sc_tv, &ch_tv, >) ||
773 timevalcmp(&sc_tv, &ch_tv, ==)) {
774 callout_stop(&sc->sc_ad_tmo);
775 CARP_LOG("%s: MASTER -> BACKUP "
776 "(more frequent advertisement received)\n",
777 SC2IFP(sc)->if_xname);
778 carp_set_state(sc, BACKUP);
780 carp_setroute(sc, RTM_DELETE);
785 * If we're pre-empting masters who advertise slower than us,
786 * and this one claims to be slower, treat him as down.
788 if (carp_opts[CARPCTL_PREEMPT] &&
789 timevalcmp(&sc_tv, &ch_tv, <)) {
790 CARP_LOG("%s: BACKUP -> MASTER "
791 "(preempting a slower master)\n",
792 SC2IFP(sc)->if_xname);
793 carp_master_down_locked(sc);
798 * If the master is going to advertise at such a low frequency
799 * that he's guaranteed to time out, we'd might as well just
800 * treat him as timed out now.
802 sc_tv.tv_sec = sc->sc_advbase * 3;
803 if (timevalcmp(&sc_tv, &ch_tv, <)) {
804 CARP_LOG("%s: BACKUP -> MASTER "
805 "(master timed out)\n",
806 SC2IFP(sc)->if_xname);
807 carp_master_down_locked(sc);
812 * Otherwise, we reset the counter and wait for the next
819 CARP_UNLOCK(ifp->if_carp);
826 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
829 struct ifnet *ifp = SC2IFP(sc);
831 if (sc->sc_init_counter) {
832 /* this could also be seconds since unix epoch */
833 sc->sc_counter = arc4random();
834 sc->sc_counter = sc->sc_counter << 32;
835 sc->sc_counter += arc4random();
839 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
840 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
842 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
844 /* Tag packet for carp_output */
845 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
848 SC2IFP(sc)->if_oerrors++;
851 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
852 m_tag_prepend(m, mtag);
858 carp_send_ad_all(void)
860 struct carp_softc *sc;
863 LIST_FOREACH(sc, &carpif_list, sc_next) {
864 if (sc->sc_carpdev == NULL)
867 if ((SC2IFP(sc)->if_flags & IFF_UP) &&
868 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
869 sc->sc_state == MASTER)
870 carp_send_ad_locked(sc);
873 mtx_unlock(&carp_mtx);
877 carp_send_ad(void *v)
879 struct carp_softc *sc = v;
882 carp_send_ad_locked(sc);
887 carp_send_ad_locked(struct carp_softc *sc)
889 struct carp_header ch;
891 struct carp_header *ch_ptr;
893 int len, advbase, advskew;
895 CARP_SCLOCK_ASSERT(sc);
897 /* bow out if we've lost our UPness or RUNNINGuiness */
898 if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
899 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
903 advbase = sc->sc_advbase;
904 if (!carp_suppress_preempt || sc->sc_advskew > 240)
905 advskew = sc->sc_advskew;
909 tv.tv_usec = advskew * 1000000 / 256;
912 ch.carp_version = CARP_VERSION;
913 ch.carp_type = CARP_ADVERTISEMENT;
914 ch.carp_vhid = sc->sc_vhid;
915 ch.carp_advbase = advbase;
916 ch.carp_advskew = advskew;
917 ch.carp_authlen = 7; /* XXX DEFINE */
918 ch.carp_pad1 = 0; /* must be zero */
925 MGETHDR(m, M_DONTWAIT, MT_HEADER);
927 SC2IFP(sc)->if_oerrors++;
928 CARPSTATS_INC(carps_onomem);
929 /* XXX maybe less ? */
930 if (advbase != 255 || advskew != 255)
931 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
935 len = sizeof(*ip) + sizeof(ch);
936 m->m_pkthdr.len = len;
937 m->m_pkthdr.rcvif = NULL;
939 MH_ALIGN(m, m->m_len);
940 m->m_flags |= M_MCAST;
941 ip = mtod(m, struct ip *);
942 ip->ip_v = IPVERSION;
943 ip->ip_hl = sizeof(*ip) >> 2;
944 ip->ip_tos = IPTOS_LOWDELAY;
946 ip->ip_id = ip_newid();
948 ip->ip_ttl = CARP_DFLTTL;
949 ip->ip_p = IPPROTO_CARP;
951 ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
952 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
954 ch_ptr = (struct carp_header *)(&ip[1]);
955 bcopy(&ch, ch_ptr, sizeof(ch));
956 if (carp_prepare_ad(m, sc, ch_ptr))
959 m->m_data += sizeof(*ip);
960 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
961 m->m_data -= sizeof(*ip);
963 getmicrotime(&SC2IFP(sc)->if_lastchange);
964 SC2IFP(sc)->if_opackets++;
965 SC2IFP(sc)->if_obytes += len;
966 CARPSTATS_INC(carps_opackets);
968 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
969 SC2IFP(sc)->if_oerrors++;
970 if (sc->sc_sendad_errors < INT_MAX)
971 sc->sc_sendad_errors++;
972 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
973 carp_suppress_preempt++;
974 if (carp_suppress_preempt == 1) {
980 sc->sc_sendad_success = 0;
982 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
983 if (++sc->sc_sendad_success >=
984 CARP_SENDAD_MIN_SUCCESS) {
985 carp_suppress_preempt--;
986 sc->sc_sendad_errors = 0;
989 sc->sc_sendad_errors = 0;
997 MGETHDR(m, M_DONTWAIT, MT_HEADER);
999 SC2IFP(sc)->if_oerrors++;
1000 CARPSTATS_INC(carps_onomem);
1001 /* XXX maybe less ? */
1002 if (advbase != 255 || advskew != 255)
1003 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1007 len = sizeof(*ip6) + sizeof(ch);
1008 m->m_pkthdr.len = len;
1009 m->m_pkthdr.rcvif = NULL;
1011 MH_ALIGN(m, m->m_len);
1012 m->m_flags |= M_MCAST;
1013 ip6 = mtod(m, struct ip6_hdr *);
1014 bzero(ip6, sizeof(*ip6));
1015 ip6->ip6_vfc |= IPV6_VERSION;
1016 ip6->ip6_hlim = CARP_DFLTTL;
1017 ip6->ip6_nxt = IPPROTO_CARP;
1018 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1019 sizeof(struct in6_addr));
1020 /* set the multicast destination */
1022 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1023 ip6->ip6_dst.s6_addr8[15] = 0x12;
1024 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1025 SC2IFP(sc)->if_oerrors++;
1027 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
1031 ch_ptr = (struct carp_header *)(&ip6[1]);
1032 bcopy(&ch, ch_ptr, sizeof(ch));
1033 if (carp_prepare_ad(m, sc, ch_ptr))
1036 m->m_data += sizeof(*ip6);
1037 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
1038 m->m_data -= sizeof(*ip6);
1040 getmicrotime(&SC2IFP(sc)->if_lastchange);
1041 SC2IFP(sc)->if_opackets++;
1042 SC2IFP(sc)->if_obytes += len;
1043 CARPSTATS_INC(carps_opackets6);
1045 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1046 SC2IFP(sc)->if_oerrors++;
1047 if (sc->sc_sendad_errors < INT_MAX)
1048 sc->sc_sendad_errors++;
1049 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1050 carp_suppress_preempt++;
1051 if (carp_suppress_preempt == 1) {
1057 sc->sc_sendad_success = 0;
1059 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1060 if (++sc->sc_sendad_success >=
1061 CARP_SENDAD_MIN_SUCCESS) {
1062 carp_suppress_preempt--;
1063 sc->sc_sendad_errors = 0;
1066 sc->sc_sendad_errors = 0;
1071 if (advbase != 255 || advskew != 255)
1072 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1078 * Broadcast a gratuitous ARP request containing
1079 * the virtual router MAC address for each IP address
1080 * associated with the virtual router.
1083 carp_send_arp(struct carp_softc *sc)
1087 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1089 if (ifa->ifa_addr->sa_family != AF_INET)
1092 /* arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
1093 arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1095 DELAY(1000); /* XXX */
1101 carp_send_na(struct carp_softc *sc)
1104 struct in6_addr *in6;
1105 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1107 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1109 if (ifa->ifa_addr->sa_family != AF_INET6)
1112 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1113 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1114 ND_NA_FLAG_OVERRIDE, 1, NULL);
1115 DELAY(1000); /* XXX */
1121 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1123 struct carp_softc *vh;
1127 CARP_LOCK_ASSERT(cif);
1129 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1130 if ((type == CARP_COUNT_RUNNING &&
1131 (SC2IFP(vh)->if_flags & IFF_UP) &&
1132 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1133 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1134 IF_ADDR_LOCK(SC2IFP(vh));
1135 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1137 if (ifa->ifa_addr->sa_family == AF_INET &&
1138 ia->ia_addr.sin_addr.s_addr ==
1139 ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1142 IF_ADDR_UNLOCK(SC2IFP(vh));
1149 carp_iamatch(void *v, struct in_ifaddr *ia,
1150 struct in_addr *isaddr, u_int8_t **enaddr)
1152 struct carp_if *cif = v;
1153 struct carp_softc *vh;
1154 int index, count = 0;
1159 if (carp_opts[CARPCTL_ARPBALANCE]) {
1161 * XXX proof of concept implementation.
1162 * We use the source ip to decide which virtual host should
1163 * handle the request. If we're master of that virtual host,
1164 * then we respond, otherwise, just drop the arp packet on
1167 count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1169 /* should never reach this */
1174 /* this should be a hash, like pf_hash() */
1175 index = ntohl(isaddr->s_addr) % count;
1178 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1179 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1180 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1181 IF_ADDR_LOCK(SC2IFP(vh));
1182 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1184 if (ifa->ifa_addr->sa_family ==
1186 ia->ia_addr.sin_addr.s_addr ==
1187 ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1188 if (count == index) {
1191 *enaddr = IF_LLADDR(vh->sc_ifp);
1192 IF_ADDR_UNLOCK(SC2IFP(vh));
1196 IF_ADDR_UNLOCK(SC2IFP(vh));
1204 IF_ADDR_UNLOCK(SC2IFP(vh));
1208 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1209 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1210 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1211 ia->ia_ifp == SC2IFP(vh) &&
1212 vh->sc_state == MASTER) {
1213 *enaddr = IF_LLADDR(vh->sc_ifp);
1225 carp_iamatch6(void *v, struct in6_addr *taddr)
1227 struct carp_if *cif = v;
1228 struct carp_softc *vh;
1232 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1233 IF_ADDR_LOCK(SC2IFP(vh));
1234 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1235 if (IN6_ARE_ADDR_EQUAL(taddr,
1236 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1237 (SC2IFP(vh)->if_flags & IFF_UP) &&
1238 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1239 vh->sc_state == MASTER) {
1241 IF_ADDR_UNLOCK(SC2IFP(vh));
1246 IF_ADDR_UNLOCK(SC2IFP(vh));
1254 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1257 struct carp_if *cif = v;
1258 struct carp_softc *sc;
1262 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1263 IF_ADDR_LOCK(SC2IFP(sc));
1264 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1265 if (IN6_ARE_ADDR_EQUAL(taddr,
1266 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1267 (SC2IFP(sc)->if_flags & IFF_UP) &&
1268 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1269 struct ifnet *ifp = SC2IFP(sc);
1270 mtag = m_tag_get(PACKET_TAG_CARP,
1271 sizeof(struct ifnet *), M_NOWAIT);
1273 /* better a bit than nothing */
1274 IF_ADDR_UNLOCK(SC2IFP(sc));
1276 return (IF_LLADDR(sc->sc_ifp));
1278 bcopy(&ifp, (caddr_t)(mtag + 1),
1279 sizeof(struct ifnet *));
1280 m_tag_prepend(m, mtag);
1282 IF_ADDR_UNLOCK(SC2IFP(sc));
1284 return (IF_LLADDR(sc->sc_ifp));
1287 IF_ADDR_UNLOCK(SC2IFP(sc));
1296 carp_forus(void *v, void *dhost)
1298 struct carp_if *cif = v;
1299 struct carp_softc *vh;
1300 u_int8_t *ena = dhost;
1302 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1306 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1307 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1308 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1309 vh->sc_state == MASTER &&
1310 !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1312 return (SC2IFP(vh));
1320 carp_master_down(void *v)
1322 struct carp_softc *sc = v;
1325 carp_master_down_locked(sc);
1330 carp_master_down_locked(struct carp_softc *sc)
1333 CARP_SCLOCK_ASSERT(sc);
1335 switch (sc->sc_state) {
1337 printf("%s: master_down event in INIT state\n",
1338 SC2IFP(sc)->if_xname);
1343 carp_set_state(sc, MASTER);
1344 carp_send_ad_locked(sc);
1350 carp_setroute(sc, RTM_ADD);
1356 * When in backup state, af indicates whether to reset the master down timer
1357 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1360 carp_setrun(struct carp_softc *sc, sa_family_t af)
1364 if (sc->sc_carpdev == NULL) {
1365 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1366 carp_set_state(sc, INIT);
1369 CARP_SCLOCK_ASSERT(sc);
1371 if (SC2IFP(sc)->if_flags & IFF_UP &&
1372 sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1373 SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1375 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1376 carp_setroute(sc, RTM_DELETE);
1380 switch (sc->sc_state) {
1382 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1383 carp_send_ad_locked(sc);
1388 CARP_LOG("%s: INIT -> MASTER (preempting)\n",
1389 SC2IFP(sc)->if_xname);
1390 carp_set_state(sc, MASTER);
1391 carp_setroute(sc, RTM_ADD);
1393 CARP_LOG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1394 carp_set_state(sc, BACKUP);
1395 carp_setroute(sc, RTM_DELETE);
1400 callout_stop(&sc->sc_ad_tmo);
1401 tv.tv_sec = 3 * sc->sc_advbase;
1402 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1406 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1407 carp_master_down, sc);
1412 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1413 carp_master_down, sc);
1418 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1419 carp_master_down, sc);
1421 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1422 carp_master_down, sc);
1427 tv.tv_sec = sc->sc_advbase;
1428 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1429 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1436 carp_multicast_cleanup(struct carp_softc *sc)
1438 struct ip_moptions *imo = &sc->sc_imo;
1439 u_int16_t n = imo->imo_num_memberships;
1441 /* Clean up our own multicast memberships */
1443 if (imo->imo_membership[n] != NULL) {
1444 in_delmulti(imo->imo_membership[n]);
1445 imo->imo_membership[n] = NULL;
1448 KASSERT(imo->imo_mfilters == NULL,
1449 ("%s: imo_mfilters != NULL", __func__));
1450 imo->imo_num_memberships = 0;
1451 imo->imo_multicast_ifp = NULL;
1456 carp_multicast6_cleanup(struct carp_softc *sc)
1458 struct ip6_moptions *im6o = &sc->sc_im6o;
1459 u_int16_t n = im6o->im6o_num_memberships;
1462 if (im6o->im6o_membership[n] != NULL) {
1463 in6_mc_leave(im6o->im6o_membership[n], NULL);
1464 im6o->im6o_membership[n] = NULL;
1467 KASSERT(im6o->im6o_mfilters == NULL,
1468 ("%s: im6o_mfilters != NULL", __func__));
1469 im6o->im6o_num_memberships = 0;
1470 im6o->im6o_multicast_ifp = NULL;
1475 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1478 struct carp_if *cif;
1479 struct in_ifaddr *ia, *ia_if;
1480 struct ip_moptions *imo = &sc->sc_imo;
1481 struct in_addr addr;
1482 u_long iaddr = htonl(sin->sin_addr.s_addr);
1485 if (sin->sin_addr.s_addr == 0) {
1486 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1487 carp_set_state(sc, INIT);
1489 SC2IFP(sc)->if_flags |= IFF_UP;
1498 /* we have to do it by hands to check we won't match on us */
1499 ia_if = NULL; own = 0;
1501 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1502 /* and, yeah, we need a multicast-capable iface too */
1503 if (ia->ia_ifp != SC2IFP(sc) &&
1504 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1505 (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1508 if (sin->sin_addr.s_addr ==
1509 ia->ia_addr.sin_addr.s_addr)
1515 IN_IFADDR_RUNLOCK();
1516 return (EADDRNOTAVAIL);
1520 ifa_ref(&ia->ia_ifa);
1521 IN_IFADDR_RUNLOCK();
1525 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1526 (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp)) {
1527 ifa_free(&ia->ia_ifa);
1528 return (EADDRNOTAVAIL);
1531 if (imo->imo_num_memberships == 0) {
1532 addr.s_addr = htonl(INADDR_CARP_GROUP);
1533 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) ==
1535 ifa_free(&ia->ia_ifa);
1538 imo->imo_num_memberships++;
1539 imo->imo_multicast_ifp = ifp;
1540 imo->imo_multicast_ttl = CARP_DFLTTL;
1541 imo->imo_multicast_loop = 0;
1544 if (!ifp->if_carp) {
1546 cif = malloc(sizeof(*cif), M_CARP,
1552 if ((error = ifpromisc(ifp, 1))) {
1557 CARP_LOCK_INIT(cif);
1559 cif->vhif_ifp = ifp;
1560 TAILQ_INIT(&cif->vhif_vrs);
1564 struct carp_softc *vr;
1566 cif = (struct carp_if *)ifp->if_carp;
1568 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1569 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1576 sc->sc_carpdev = ifp;
1578 { /* XXX prevent endless loop if already in queue */
1579 struct carp_softc *vr, *after = NULL;
1581 cif = (struct carp_if *)ifp->if_carp;
1583 /* XXX: cif should not change, right? So we still hold the lock */
1584 CARP_LOCK_ASSERT(cif);
1586 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1589 if (vr->sc_vhid < sc->sc_vhid)
1594 /* We're trying to keep things in order */
1595 if (after == NULL) {
1596 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1598 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1605 SC2IFP(sc)->if_flags |= IFF_UP;
1608 carp_sc_state_locked(sc);
1612 ifa_free(&ia->ia_ifa); /* XXXRW: should hold reference for softc. */
1617 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1618 ifa_free(&ia->ia_ifa);
1623 carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1627 if (!--sc->sc_naddrs) {
1628 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1629 struct ip_moptions *imo = &sc->sc_imo;
1632 callout_stop(&sc->sc_ad_tmo);
1633 SC2IFP(sc)->if_flags &= ~IFF_UP;
1634 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1636 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1637 imo->imo_multicast_ifp = NULL;
1638 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1639 if (!--cif->vhif_nvrs) {
1640 sc->sc_carpdev->if_carp = NULL;
1641 CARP_LOCK_DESTROY(cif);
1653 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1656 struct carp_if *cif;
1657 struct in6_ifaddr *ia, *ia_if;
1658 struct ip6_moptions *im6o = &sc->sc_im6o;
1659 struct in6_addr in6;
1664 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1665 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1666 carp_set_state(sc, INIT);
1668 SC2IFP(sc)->if_flags |= IFF_UP;
1677 /* we have to do it by hands to check we won't match on us */
1678 ia_if = NULL; own = 0;
1680 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1683 for (i = 0; i < 4; i++) {
1684 if ((sin6->sin6_addr.s6_addr32[i] &
1685 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1686 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1687 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1690 /* and, yeah, we need a multicast-capable iface too */
1691 if (ia->ia_ifp != SC2IFP(sc) &&
1692 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1696 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1697 &ia->ia_addr.sin6_addr))
1703 IN6_IFADDR_RUNLOCK();
1704 return (EADDRNOTAVAIL);
1707 ifa_ref(&ia->ia_ifa);
1708 IN6_IFADDR_RUNLOCK();
1711 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1712 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp)) {
1713 ifa_free(&ia->ia_ifa);
1714 return (EADDRNOTAVAIL);
1717 if (!sc->sc_naddrs6) {
1718 struct in6_multi *in6m;
1720 im6o->im6o_multicast_ifp = ifp;
1722 /* join CARP multicast address */
1723 bzero(&in6, sizeof(in6));
1724 in6.s6_addr16[0] = htons(0xff02);
1725 in6.s6_addr8[15] = 0x12;
1726 if (in6_setscope(&in6, ifp, NULL) != 0)
1729 error = in6_mc_join(ifp, &in6, NULL, &in6m, 0);
1732 im6o->im6o_membership[0] = in6m;
1733 im6o->im6o_num_memberships++;
1735 /* join solicited multicast address */
1736 bzero(&in6, sizeof(in6));
1737 in6.s6_addr16[0] = htons(0xff02);
1738 in6.s6_addr32[1] = 0;
1739 in6.s6_addr32[2] = htonl(1);
1740 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1741 in6.s6_addr8[12] = 0xff;
1742 if (in6_setscope(&in6, ifp, NULL) != 0)
1745 error = in6_mc_join(ifp, &in6, NULL, &in6m, 0);
1748 im6o->im6o_membership[1] = in6m;
1749 im6o->im6o_num_memberships++;
1752 if (!ifp->if_carp) {
1753 cif = malloc(sizeof(*cif), M_CARP,
1759 if ((error = ifpromisc(ifp, 1))) {
1764 CARP_LOCK_INIT(cif);
1766 cif->vhif_ifp = ifp;
1767 TAILQ_INIT(&cif->vhif_vrs);
1771 struct carp_softc *vr;
1773 cif = (struct carp_if *)ifp->if_carp;
1775 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1776 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1783 sc->sc_carpdev = ifp;
1785 { /* XXX prevent endless loop if already in queue */
1786 struct carp_softc *vr, *after = NULL;
1788 cif = (struct carp_if *)ifp->if_carp;
1789 CARP_LOCK_ASSERT(cif);
1791 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1794 if (vr->sc_vhid < sc->sc_vhid)
1799 /* We're trying to keep things in order */
1800 if (after == NULL) {
1801 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1803 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1810 SC2IFP(sc)->if_flags |= IFF_UP;
1813 carp_sc_state_locked(sc);
1817 ifa_free(&ia->ia_ifa); /* XXXRW: should hold reference for softc. */
1822 if (!sc->sc_naddrs6)
1823 carp_multicast6_cleanup(sc);
1824 ifa_free(&ia->ia_ifa);
1829 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1833 if (!--sc->sc_naddrs6) {
1834 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1837 callout_stop(&sc->sc_ad_tmo);
1838 SC2IFP(sc)->if_flags &= ~IFF_UP;
1839 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1841 carp_multicast6_cleanup(sc);
1842 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1843 if (!--cif->vhif_nvrs) {
1844 CARP_LOCK_DESTROY(cif);
1845 sc->sc_carpdev->if_carp = NULL;
1856 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1858 struct carp_softc *sc = ifp->if_softc, *vr;
1859 struct carpreq carpr;
1862 struct ifaliasreq *ifra;
1863 int locked = 0, error = 0;
1865 ifa = (struct ifaddr *)addr;
1866 ifra = (struct ifaliasreq *)addr;
1867 ifr = (struct ifreq *)addr;
1871 switch (ifa->ifa_addr->sa_family) {
1874 SC2IFP(sc)->if_flags |= IFF_UP;
1875 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1876 sizeof(struct sockaddr));
1877 error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1882 SC2IFP(sc)->if_flags |= IFF_UP;
1883 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1887 error = EAFNOSUPPORT;
1893 switch (ifa->ifa_addr->sa_family) {
1896 SC2IFP(sc)->if_flags |= IFF_UP;
1897 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1898 sizeof(struct sockaddr));
1899 error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1904 SC2IFP(sc)->if_flags |= IFF_UP;
1905 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1909 error = EAFNOSUPPORT;
1915 switch (ifa->ifa_addr->sa_family) {
1918 error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1923 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1927 error = EAFNOSUPPORT;
1933 if (sc->sc_carpdev) {
1937 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1938 callout_stop(&sc->sc_ad_tmo);
1939 callout_stop(&sc->sc_md_tmo);
1940 callout_stop(&sc->sc_md6_tmo);
1941 if (sc->sc_state == MASTER)
1942 carp_send_ad_locked(sc);
1943 carp_set_state(sc, INIT);
1945 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1946 SC2IFP(sc)->if_flags |= IFF_UP;
1952 error = priv_check(curthread, PRIV_NETINET_CARP);
1955 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1958 if (sc->sc_carpdev) {
1962 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1963 switch (carpr.carpr_state) {
1965 callout_stop(&sc->sc_ad_tmo);
1966 carp_set_state(sc, BACKUP);
1968 carp_setroute(sc, RTM_DELETE);
1971 carp_master_down_locked(sc);
1977 if (carpr.carpr_vhid > 0) {
1978 if (carpr.carpr_vhid > 255) {
1982 if (sc->sc_carpdev) {
1983 struct carp_if *cif;
1984 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1985 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1987 vr->sc_vhid == carpr.carpr_vhid) {
1991 if (error == EEXIST)
1994 sc->sc_vhid = carpr.carpr_vhid;
1995 IF_LLADDR(sc->sc_ifp)[0] = 0;
1996 IF_LLADDR(sc->sc_ifp)[1] = 0;
1997 IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1998 IF_LLADDR(sc->sc_ifp)[3] = 0;
1999 IF_LLADDR(sc->sc_ifp)[4] = 1;
2000 IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
2003 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
2004 if (carpr.carpr_advskew >= 255) {
2008 if (carpr.carpr_advbase > 255) {
2012 sc->sc_advbase = carpr.carpr_advbase;
2013 sc->sc_advskew = carpr.carpr_advskew;
2016 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
2026 /* XXX: lockless read */
2027 bzero(&carpr, sizeof(carpr));
2028 carpr.carpr_state = sc->sc_state;
2029 carpr.carpr_vhid = sc->sc_vhid;
2030 carpr.carpr_advbase = sc->sc_advbase;
2031 carpr.carpr_advskew = sc->sc_advskew;
2032 error = priv_check(curthread, PRIV_NETINET_CARP);
2034 bcopy(sc->sc_key, carpr.carpr_key,
2035 sizeof(carpr.carpr_key));
2036 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
2046 carp_hmac_prepare(sc);
2052 * XXX: this is looutput. We should eventually use it from there.
2055 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2059 struct rtentry *rt = NULL;
2061 M_ASSERTPKTHDR(m); /* check if we have the packet header */
2065 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2067 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
2068 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
2072 ifp->if_obytes += m->m_pkthdr.len;
2074 /* BPF writes need to be handled specially. */
2075 if (dst->sa_family == AF_UNSPEC) {
2076 bcopy(dst->sa_data, &af, sizeof(af));
2077 dst->sa_family = af;
2081 switch (dst->sa_family) {
2088 printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
2090 return (EAFNOSUPPORT);
2093 return(if_simloop(ifp, m, dst->sa_family, 0));
2097 * Start output on carp interface. This function should never be called.
2100 carp_start(struct ifnet *ifp)
2103 printf("%s: start called\n", ifp->if_xname);
2108 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2112 struct carp_softc *sc;
2113 struct ifnet *carp_ifp;
2118 switch (sa->sa_family) {
2131 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2135 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2136 sc = carp_ifp->if_softc;
2138 /* Set the source MAC address to Virtual Router MAC Address */
2139 switch (ifp->if_type) {
2142 struct ether_header *eh;
2144 eh = mtod(m, struct ether_header *);
2145 eh->ether_shost[0] = 0;
2146 eh->ether_shost[1] = 0;
2147 eh->ether_shost[2] = 0x5e;
2148 eh->ether_shost[3] = 0;
2149 eh->ether_shost[4] = 1;
2150 eh->ether_shost[5] = sc->sc_vhid;
2154 struct fddi_header *fh;
2156 fh = mtod(m, struct fddi_header *);
2157 fh->fddi_shost[0] = 0;
2158 fh->fddi_shost[1] = 0;
2159 fh->fddi_shost[2] = 0x5e;
2160 fh->fddi_shost[3] = 0;
2161 fh->fddi_shost[4] = 1;
2162 fh->fddi_shost[5] = sc->sc_vhid;
2165 case IFT_ISO88025: {
2166 struct iso88025_header *th;
2167 th = mtod(m, struct iso88025_header *);
2168 th->iso88025_shost[0] = 3;
2169 th->iso88025_shost[1] = 0;
2170 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2171 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2172 th->iso88025_shost[4] = 0;
2173 th->iso88025_shost[5] = 0;
2177 printf("%s: carp is not supported for this interface type\n",
2179 return (EOPNOTSUPP);
2186 carp_set_state(struct carp_softc *sc, int state)
2191 CARP_SCLOCK_ASSERT(sc);
2193 if (sc->sc_state == state)
2196 sc->sc_state = state;
2199 link_state = LINK_STATE_DOWN;
2202 link_state = LINK_STATE_UP;
2205 link_state = LINK_STATE_UNKNOWN;
2208 if_link_state_change(SC2IFP(sc), link_state);
2212 carp_carpdev_state(void *v)
2214 struct carp_if *cif = v;
2217 carp_carpdev_state_locked(cif);
2222 carp_carpdev_state_locked(struct carp_if *cif)
2224 struct carp_softc *sc;
2226 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2227 carp_sc_state_locked(sc);
2231 carp_sc_state_locked(struct carp_softc *sc)
2233 CARP_SCLOCK_ASSERT(sc);
2235 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2236 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2237 sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2238 SC2IFP(sc)->if_flags &= ~IFF_UP;
2239 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2240 callout_stop(&sc->sc_ad_tmo);
2241 callout_stop(&sc->sc_md_tmo);
2242 callout_stop(&sc->sc_md6_tmo);
2243 carp_set_state(sc, INIT);
2245 if (!sc->sc_suppress) {
2246 carp_suppress_preempt++;
2247 if (carp_suppress_preempt == 1) {
2253 sc->sc_suppress = 1;
2255 SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2256 carp_set_state(sc, INIT);
2258 if (sc->sc_suppress)
2259 carp_suppress_preempt--;
2260 sc->sc_suppress = 0;
2267 carp_modevent(module_t mod, int type, void *data)
2271 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2272 carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
2273 if (if_detach_event_tag == NULL)
2275 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2276 LIST_INIT(&carpif_list);
2277 if_clone_attach(&carp_cloner);
2281 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2282 if_clone_detach(&carp_cloner);
2283 mtx_destroy(&carp_mtx);
2293 static moduledata_t carp_mod = {
2299 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);