2 * Copyright (c) 2002 Michael Shalayeff.
3 * Copyright (c) 2003 Ryan McBride.
4 * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org>
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
44 #include <sys/module.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/taskqueue.h>
53 #include <sys/counter.h>
55 #include <net/ethernet.h>
58 #include <net/if_var.h>
59 #include <net/if_dl.h>
60 #include <net/if_llatbl.h>
61 #include <net/if_types.h>
62 #include <net/iso88025.h>
63 #include <net/route.h>
66 #if defined(INET) || defined(INET6)
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_carp.h>
70 #include <netinet/ip.h>
71 #include <machine/in_cksum.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/if_ether.h>
79 #include <netinet/icmp6.h>
80 #include <netinet/ip6.h>
81 #include <netinet6/in6_var.h>
82 #include <netinet6/ip6_var.h>
83 #include <netinet6/scope6_var.h>
84 #include <netinet6/nd6.h>
87 #include <crypto/sha1.h>
89 static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
92 struct ifnet *sc_carpdev; /* Pointer to parent ifnet. */
93 struct ifaddr **sc_ifas; /* Our ifaddrs. */
94 struct sockaddr_dl sc_addr; /* Our link level address. */
95 struct callout sc_ad_tmo; /* Advertising timeout. */
97 struct callout sc_md_tmo; /* Master down timeout. */
100 struct callout sc_md6_tmo; /* XXX: Master down timeout. */
111 enum { INIT = 0, BACKUP, MASTER } sc_state;
113 int sc_sendad_errors;
114 #define CARP_SENDAD_MAX_ERRORS 3
115 int sc_sendad_success;
116 #define CARP_SENDAD_MIN_SUCCESS 3
122 #define CARP_HMAC_PAD 64
123 unsigned char sc_key[CARP_KEY_LEN];
124 unsigned char sc_pad[CARP_HMAC_PAD];
127 TAILQ_ENTRY(carp_softc) sc_list; /* On the carp_if list. */
128 LIST_ENTRY(carp_softc) sc_next; /* On the global list. */
138 TAILQ_HEAD(, carp_softc) cif_vrs;
140 struct ip_moptions cif_imo;
143 struct ip6_moptions cif_im6o;
145 struct ifnet *cif_ifp;
148 #define CIF_PROMISC 0x00000001
153 static int proto_reg[] = {-1, -1};
156 * Brief design of carp(4).
158 * Any carp-capable ifnet may have a list of carp softcs hanging off
159 * its ifp->if_carp pointer. Each softc represents one unique virtual
160 * host id, or vhid. The softc has a back pointer to the ifnet. All
161 * softcs are joined in a global list, which has quite limited use.
163 * Any interface address that takes part in CARP negotiation has a
164 * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
165 * AF_INET or AF_INET6 address.
167 * Although, one can get the softc's backpointer to ifnet and traverse
168 * through its ifp->if_addrhead queue to find all interface addresses
169 * involved in CARP, we keep a growable array of ifaddr pointers. This
170 * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
171 * do calls into the network stack, thus avoiding LORs.
175 * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
176 * callout-driven events and ioctl()s.
178 * To traverse the list of softcs on an ifnet we use CIF_LOCK() or carp_sx.
179 * To traverse the global list we use the mutex carp_mtx.
181 * Known issues with locking:
183 * - Sending ad, we put the pointer to the softc in an mtag, and no reference
184 * counting is done on the softc.
185 * - On module unload we may race (?) with packet processing thread
186 * dereferencing our function pointers.
189 /* Accept incoming CARP packets. */
190 static VNET_DEFINE(int, carp_allow) = 1;
191 #define V_carp_allow VNET(carp_allow)
193 /* Preempt slower nodes. */
194 static VNET_DEFINE(int, carp_preempt) = 0;
195 #define V_carp_preempt VNET(carp_preempt)
198 static VNET_DEFINE(int, carp_log) = 1;
199 #define V_carp_log VNET(carp_log)
201 /* Global advskew demotion. */
202 static VNET_DEFINE(int, carp_demotion) = 0;
203 #define V_carp_demotion VNET(carp_demotion)
205 /* Send error demotion factor. */
206 static VNET_DEFINE(int, carp_senderr_adj) = CARP_MAXSKEW;
207 #define V_carp_senderr_adj VNET(carp_senderr_adj)
209 /* Iface down demotion factor. */
210 static VNET_DEFINE(int, carp_ifdown_adj) = CARP_MAXSKEW;
211 #define V_carp_ifdown_adj VNET(carp_ifdown_adj)
213 static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
215 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP");
216 SYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_VNET | CTLFLAG_RW,
217 &VNET_NAME(carp_allow), 0, "Accept incoming CARP packets");
218 SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
219 &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
220 SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
221 &VNET_NAME(carp_log), 0, "CARP log level");
222 SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
223 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
224 0, 0, carp_demote_adj_sysctl, "I",
225 "Adjust demotion factor (skew of advskew)");
226 SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
227 CTLFLAG_VNET | CTLFLAG_RW,
228 &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
229 SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
230 CTLFLAG_VNET | CTLFLAG_RW,
231 &VNET_NAME(carp_ifdown_adj), 0,
232 "Interface down demotion factor adjustment");
234 VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
235 VNET_PCPUSTAT_SYSINIT(carpstats);
236 VNET_PCPUSTAT_SYSUNINIT(carpstats);
238 #define CARPSTATS_ADD(name, val) \
239 counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
240 sizeof(uint64_t)], (val))
241 #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1)
243 SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
244 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
246 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
248 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
249 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
250 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
251 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
252 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
254 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
255 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
256 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
257 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
258 #define CIF_FREE(cif) do { \
260 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
266 #define CARP_LOG(...) do { \
267 if (V_carp_log > 0) \
268 log(LOG_INFO, "carp: " __VA_ARGS__); \
271 #define CARP_DEBUG(...) do { \
272 if (V_carp_log > 1) \
273 log(LOG_DEBUG, __VA_ARGS__); \
276 #define IFNET_FOREACH_IFA(ifp, ifa) \
277 IF_ADDR_LOCK_ASSERT(ifp); \
278 TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
279 if ((ifa)->ifa_carp != NULL)
281 #define CARP_FOREACH_IFA(sc, ifa) \
282 CARP_LOCK_ASSERT(sc); \
284 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
285 ((ifa) = sc->sc_ifas[_i]) != NULL; \
288 #define IFNET_FOREACH_CARP(ifp, sc) \
289 KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) || \
290 sx_xlocked(&carp_sx), ("cif_vrs not locked")); \
291 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
293 #define DEMOTE_ADVSKEW(sc) \
294 (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \
295 CARP_MAXSKEW : ((sc)->sc_advskew + V_carp_demotion))
297 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
298 static struct carp_softc
299 *carp_alloc(struct ifnet *);
300 static void carp_destroy(struct carp_softc *);
301 static struct carp_if
302 *carp_alloc_if(struct ifnet *);
303 static void carp_free_if(struct carp_if *);
304 static void carp_set_state(struct carp_softc *, int, const char* reason);
305 static void carp_sc_state(struct carp_softc *);
306 static void carp_setrun(struct carp_softc *, sa_family_t);
307 static void carp_master_down(void *);
308 static void carp_master_down_locked(struct carp_softc *,
310 static void carp_send_ad(void *);
311 static void carp_send_ad_locked(struct carp_softc *);
312 static void carp_addroute(struct carp_softc *);
313 static void carp_ifa_addroute(struct ifaddr *);
314 static void carp_delroute(struct carp_softc *);
315 static void carp_ifa_delroute(struct ifaddr *);
316 static void carp_send_ad_all(void *, int);
317 static void carp_demote_adj(int, char *);
319 static LIST_HEAD(, carp_softc) carp_list;
320 static struct mtx carp_mtx;
321 static struct sx carp_sx;
322 static struct task carp_sendall_task =
323 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
326 carp_hmac_prepare(struct carp_softc *sc)
328 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
329 uint8_t vhid = sc->sc_vhid & 0xff;
333 struct in_addr last, cur, in;
336 struct in6_addr last6, cur6, in6;
339 CARP_LOCK_ASSERT(sc);
341 /* Compute ipad from key. */
342 bzero(sc->sc_pad, sizeof(sc->sc_pad));
343 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
344 for (i = 0; i < sizeof(sc->sc_pad); i++)
345 sc->sc_pad[i] ^= 0x36;
347 /* Precompute first part of inner hash. */
348 SHA1Init(&sc->sc_sha1);
349 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
350 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
351 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
352 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
358 cur.s_addr = 0xffffffff;
359 CARP_FOREACH_IFA(sc, ifa) {
360 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
361 if (ifa->ifa_addr->sa_family == AF_INET &&
362 ntohl(in.s_addr) > ntohl(last.s_addr) &&
363 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
364 cur.s_addr = in.s_addr;
369 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
373 memset(&cur6, 0, sizeof(cur6));
377 memset(&cur6, 0xff, sizeof(cur6));
378 CARP_FOREACH_IFA(sc, ifa) {
379 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
380 if (IN6_IS_SCOPE_EMBED(&in6))
381 in6.s6_addr16[1] = 0;
382 if (ifa->ifa_addr->sa_family == AF_INET6 &&
383 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
384 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
390 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
394 /* convert ipad to opad */
395 for (i = 0; i < sizeof(sc->sc_pad); i++)
396 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
400 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
401 unsigned char md[20])
405 CARP_LOCK_ASSERT(sc);
407 /* fetch first half of inner hash */
408 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
410 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
411 SHA1Final(md, &sha1ctx);
415 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
416 SHA1Update(&sha1ctx, md, 20);
417 SHA1Final(md, &sha1ctx);
421 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
422 unsigned char md[20])
424 unsigned char md2[20];
426 CARP_LOCK_ASSERT(sc);
428 carp_hmac_generate(sc, counter, md2);
430 return (bcmp(md, md2, sizeof(md2)));
434 * process input packet.
435 * we have rearranged checks order compared to the rfc,
436 * but it seems more efficient this way or not possible otherwise.
440 carp_input(struct mbuf **mp, int *offp, int proto)
442 struct mbuf *m = *mp;
443 struct ip *ip = mtod(m, struct ip *);
444 struct carp_header *ch;
450 CARPSTATS_INC(carps_ipackets);
454 return (IPPROTO_DONE);
457 /* verify that the IP TTL is 255. */
458 if (ip->ip_ttl != CARP_DFLTTL) {
459 CARPSTATS_INC(carps_badttl);
460 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
462 m->m_pkthdr.rcvif->if_xname);
464 return (IPPROTO_DONE);
467 iplen = ip->ip_hl << 2;
469 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
470 CARPSTATS_INC(carps_badlen);
471 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
472 "on %s\n", __func__, m->m_len - sizeof(struct ip),
473 m->m_pkthdr.rcvif->if_xname);
475 return (IPPROTO_DONE);
478 if (iplen + sizeof(*ch) < m->m_len) {
479 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
480 CARPSTATS_INC(carps_hdrops);
481 CARP_DEBUG("%s: pullup failed\n", __func__);
482 return (IPPROTO_DONE);
484 ip = mtod(m, struct ip *);
486 ch = (struct carp_header *)((char *)ip + iplen);
489 * verify that the received packet length is
490 * equal to the CARP header
492 len = iplen + sizeof(*ch);
493 if (len > m->m_pkthdr.len) {
494 CARPSTATS_INC(carps_badlen);
495 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
497 m->m_pkthdr.rcvif->if_xname);
499 return (IPPROTO_DONE);
502 if ((m = m_pullup(m, len)) == NULL) {
503 CARPSTATS_INC(carps_hdrops);
504 return (IPPROTO_DONE);
506 ip = mtod(m, struct ip *);
507 ch = (struct carp_header *)((char *)ip + iplen);
509 /* verify the CARP checksum */
511 if (in_cksum(m, len - iplen)) {
512 CARPSTATS_INC(carps_badsum);
513 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
514 m->m_pkthdr.rcvif->if_xname);
516 return (IPPROTO_DONE);
520 carp_input_c(m, ch, AF_INET);
521 return (IPPROTO_DONE);
527 carp6_input(struct mbuf **mp, int *offp, int proto)
529 struct mbuf *m = *mp;
530 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
531 struct carp_header *ch;
534 CARPSTATS_INC(carps_ipackets6);
538 return (IPPROTO_DONE);
541 /* check if received on a valid carp interface */
542 if (m->m_pkthdr.rcvif->if_carp == NULL) {
543 CARPSTATS_INC(carps_badif);
544 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
545 __func__, m->m_pkthdr.rcvif->if_xname);
547 return (IPPROTO_DONE);
550 /* verify that the IP TTL is 255 */
551 if (ip6->ip6_hlim != CARP_DFLTTL) {
552 CARPSTATS_INC(carps_badttl);
553 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
554 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
556 return (IPPROTO_DONE);
559 /* verify that we have a complete carp packet */
561 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
563 CARPSTATS_INC(carps_badlen);
564 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
565 return (IPPROTO_DONE);
569 /* verify the CARP checksum */
571 if (in_cksum(m, sizeof(*ch))) {
572 CARPSTATS_INC(carps_badsum);
573 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
574 m->m_pkthdr.rcvif->if_xname);
576 return (IPPROTO_DONE);
580 carp_input_c(m, ch, AF_INET6);
581 return (IPPROTO_DONE);
586 * This routine should not be necessary at all, but some switches
587 * (VMWare ESX vswitches) can echo our own packets back at us,
588 * and we must ignore them or they will cause us to drop out of
591 * We cannot catch all cases of network loops. Instead, what we
592 * do here is catch any packet that arrives with a carp header
593 * with a VHID of 0, that comes from an address that is our own.
594 * These packets are by definition "from us" (even if they are from
595 * a misconfigured host that is pretending to be us).
597 * The VHID test is outside this mini-function.
600 carp_source_is_self(struct mbuf *m, struct ifaddr *ifa, sa_family_t af)
614 ip4 = mtod(m, struct ip *);
615 in4 = ifatoia(ifa)->ia_addr.sin_addr;
616 return (in4.s_addr == ip4->ip_src.s_addr);
620 ip6 = mtod(m, struct ip6_hdr *);
621 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
622 return (memcmp(&in6, &ip6->ip6_src, sizeof(in6)) == 0);
631 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
633 struct ifnet *ifp = m->m_pkthdr.rcvif;
634 struct ifaddr *ifa, *match;
635 struct carp_softc *sc;
636 uint64_t tmp_counter;
637 struct timeval sc_tv, ch_tv;
641 * Verify that the VHID is valid on the receiving interface.
643 * There should be just one match. If there are none
644 * the VHID is not valid and we drop the packet. If
645 * there are multiple VHID matches, take just the first
646 * one, for compatibility with previous code. While we're
647 * scanning, check for obvious loops in the network topology
648 * (these should never happen, and as noted above, we may
649 * miss real loops; this is just a double-check).
654 IFNET_FOREACH_IFA(ifp, ifa) {
655 if (match == NULL && ifa->ifa_carp != NULL &&
656 ifa->ifa_addr->sa_family == af &&
657 ifa->ifa_carp->sc_vhid == ch->carp_vhid)
659 if (ch->carp_vhid == 0 && carp_source_is_self(m, ifa, af))
662 ifa = error ? NULL : match;
665 IF_ADDR_RUNLOCK(ifp);
668 if (error == ELOOP) {
669 CARP_DEBUG("dropping looped packet on interface %s\n",
671 CARPSTATS_INC(carps_badif); /* ??? */
673 CARPSTATS_INC(carps_badvhid);
679 /* verify the CARP version. */
680 if (ch->carp_version != CARP_VERSION) {
681 CARPSTATS_INC(carps_badver);
682 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
693 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
694 CARPSTATS_INC(carps_badauth);
695 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
696 sc->sc_vhid, ifp->if_xname);
700 tmp_counter = ntohl(ch->carp_counter[0]);
701 tmp_counter = tmp_counter<<32;
702 tmp_counter += ntohl(ch->carp_counter[1]);
704 /* XXX Replay protection goes here */
706 sc->sc_init_counter = 0;
707 sc->sc_counter = tmp_counter;
709 sc_tv.tv_sec = sc->sc_advbase;
710 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
711 ch_tv.tv_sec = ch->carp_advbase;
712 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
714 switch (sc->sc_state) {
719 * If we receive an advertisement from a master who's going to
720 * be more frequent than us, go into BACKUP state.
722 if (timevalcmp(&sc_tv, &ch_tv, >) ||
723 timevalcmp(&sc_tv, &ch_tv, ==)) {
724 callout_stop(&sc->sc_ad_tmo);
725 carp_set_state(sc, BACKUP,
726 "more frequent advertisement received");
733 * If we're pre-empting masters who advertise slower than us,
734 * and this one claims to be slower, treat him as down.
736 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
737 carp_master_down_locked(sc,
738 "preempting a slower master");
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_master_down_locked(sc, "master will time out");
754 * Otherwise, we reset the counter and wait for the next
767 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
771 if (sc->sc_init_counter) {
772 /* this could also be seconds since unix epoch */
773 sc->sc_counter = arc4random();
774 sc->sc_counter = sc->sc_counter << 32;
775 sc->sc_counter += arc4random();
779 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
780 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
782 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
784 /* Tag packet for carp_output */
785 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
786 M_NOWAIT)) == NULL) {
788 CARPSTATS_INC(carps_onomem);
791 bcopy(&sc, mtag + 1, sizeof(sc));
792 m_tag_prepend(m, mtag);
798 * To avoid LORs and possible recursions this function shouldn't
799 * be called directly, but scheduled via taskqueue.
802 carp_send_ad_all(void *ctx __unused, int pending __unused)
804 struct carp_softc *sc;
807 LIST_FOREACH(sc, &carp_list, sc_next)
808 if (sc->sc_state == MASTER) {
810 CURVNET_SET(sc->sc_carpdev->if_vnet);
811 carp_send_ad_locked(sc);
815 mtx_unlock(&carp_mtx);
818 /* Send a periodic advertisement, executed in callout context. */
820 carp_send_ad(void *v)
822 struct carp_softc *sc = v;
824 CARP_LOCK_ASSERT(sc);
825 CURVNET_SET(sc->sc_carpdev->if_vnet);
826 carp_send_ad_locked(sc);
832 carp_send_ad_error(struct carp_softc *sc, int error)
836 if (sc->sc_sendad_errors < INT_MAX)
837 sc->sc_sendad_errors++;
838 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
839 static const char fmt[] = "send error %d on %s";
840 char msg[sizeof(fmt) + IFNAMSIZ];
842 sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
843 carp_demote_adj(V_carp_senderr_adj, msg);
845 sc->sc_sendad_success = 0;
847 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS &&
848 ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
849 static const char fmt[] = "send ok on %s";
850 char msg[sizeof(fmt) + IFNAMSIZ];
852 sprintf(msg, fmt, sc->sc_carpdev->if_xname);
853 carp_demote_adj(-V_carp_senderr_adj, msg);
854 sc->sc_sendad_errors = 0;
856 sc->sc_sendad_errors = 0;
861 * Pick the best ifaddr on the given ifp for sending CARP
864 * "Best" here is defined by ifa_preferred(). This function is much
865 * much like ifaof_ifpforaddr() except that we just use ifa_preferred().
867 * (This could be simplified to return the actual address, except that
868 * it has a different format in AF_INET and AF_INET6.)
870 static struct ifaddr *
871 carp_best_ifa(int af, struct ifnet *ifp)
873 struct ifaddr *ifa, *best;
879 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
880 if (ifa->ifa_addr->sa_family == af &&
881 (best == NULL || ifa_preferred(best, ifa)))
884 IF_ADDR_RUNLOCK(ifp);
891 carp_send_ad_locked(struct carp_softc *sc)
893 struct carp_header ch;
896 struct carp_header *ch_ptr;
900 CARP_LOCK_ASSERT(sc);
902 advskew = DEMOTE_ADVSKEW(sc);
903 tv.tv_sec = sc->sc_advbase;
904 tv.tv_usec = advskew * 1000000 / 256;
906 ch.carp_version = CARP_VERSION;
907 ch.carp_type = CARP_ADVERTISEMENT;
908 ch.carp_vhid = sc->sc_vhid;
909 ch.carp_advbase = sc->sc_advbase;
910 ch.carp_advskew = advskew;
911 ch.carp_authlen = 7; /* XXX DEFINE */
912 ch.carp_pad1 = 0; /* must be zero */
915 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
921 m = m_gethdr(M_NOWAIT, MT_DATA);
923 CARPSTATS_INC(carps_onomem);
926 len = sizeof(*ip) + sizeof(ch);
927 m->m_pkthdr.len = len;
928 m->m_pkthdr.rcvif = NULL;
930 M_ALIGN(m, m->m_len);
931 m->m_flags |= M_MCAST;
932 ip = mtod(m, struct ip *);
933 ip->ip_v = IPVERSION;
934 ip->ip_hl = sizeof(*ip) >> 2;
935 ip->ip_tos = IPTOS_LOWDELAY;
936 ip->ip_len = htons(len);
937 ip->ip_off = htons(IP_DF);
938 ip->ip_ttl = CARP_DFLTTL;
939 ip->ip_p = IPPROTO_CARP;
943 ifa = carp_best_ifa(AF_INET, sc->sc_carpdev);
946 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
949 ip->ip_src.s_addr = 0;
950 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
952 ch_ptr = (struct carp_header *)(&ip[1]);
953 bcopy(&ch, ch_ptr, sizeof(ch));
954 if (carp_prepare_ad(m, sc, ch_ptr))
957 m->m_data += sizeof(*ip);
958 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
959 m->m_data -= sizeof(*ip);
961 CARPSTATS_INC(carps_opackets);
963 carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
964 &sc->sc_carpdev->if_carp->cif_imo, NULL));
968 if (sc->sc_naddrs6) {
971 m = m_gethdr(M_NOWAIT, MT_DATA);
973 CARPSTATS_INC(carps_onomem);
976 len = sizeof(*ip6) + sizeof(ch);
977 m->m_pkthdr.len = len;
978 m->m_pkthdr.rcvif = NULL;
980 M_ALIGN(m, m->m_len);
981 m->m_flags |= M_MCAST;
982 ip6 = mtod(m, struct ip6_hdr *);
983 bzero(ip6, sizeof(*ip6));
984 ip6->ip6_vfc |= IPV6_VERSION;
985 ip6->ip6_hlim = CARP_DFLTTL;
986 ip6->ip6_nxt = IPPROTO_CARP;
988 /* set the source address */
989 ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev);
991 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
992 sizeof(struct in6_addr));
995 /* This should never happen with IPv6. */
996 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
998 /* Set the multicast destination. */
999 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1000 ip6->ip6_dst.s6_addr8[15] = 0x12;
1001 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1003 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
1007 ch_ptr = (struct carp_header *)(&ip6[1]);
1008 bcopy(&ch, ch_ptr, sizeof(ch));
1009 if (carp_prepare_ad(m, sc, ch_ptr))
1012 m->m_data += sizeof(*ip6);
1013 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
1014 m->m_data -= sizeof(*ip6);
1016 CARPSTATS_INC(carps_opackets6);
1018 carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
1019 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
1024 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
1028 carp_addroute(struct carp_softc *sc)
1032 CARP_FOREACH_IFA(sc, ifa)
1033 carp_ifa_addroute(ifa);
1037 carp_ifa_addroute(struct ifaddr *ifa)
1040 switch (ifa->ifa_addr->sa_family) {
1043 in_addprefix(ifatoia(ifa), RTF_UP);
1044 ifa_add_loopback_route(ifa,
1045 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
1050 ifa_add_loopback_route(ifa,
1051 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1052 nd6_add_ifa_lle(ifatoia6(ifa));
1059 carp_delroute(struct carp_softc *sc)
1063 CARP_FOREACH_IFA(sc, ifa)
1064 carp_ifa_delroute(ifa);
1068 carp_ifa_delroute(struct ifaddr *ifa)
1071 switch (ifa->ifa_addr->sa_family) {
1074 ifa_del_loopback_route(ifa,
1075 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
1076 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
1081 ifa_del_loopback_route(ifa,
1082 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1083 nd6_rem_ifa_lle(ifatoia6(ifa), 1);
1090 carp_master(struct ifaddr *ifa)
1092 struct carp_softc *sc = ifa->ifa_carp;
1094 return (sc->sc_state == MASTER);
1099 * Broadcast a gratuitous ARP request containing
1100 * the virtual router MAC address for each IP address
1101 * associated with the virtual router.
1104 carp_send_arp(struct carp_softc *sc)
1107 struct in_addr addr;
1109 CARP_FOREACH_IFA(sc, ifa) {
1110 if (ifa->ifa_addr->sa_family != AF_INET)
1112 addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1113 arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr));
1118 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1120 struct carp_softc *sc = ifa->ifa_carp;
1122 if (sc->sc_state == MASTER) {
1123 *enaddr = LLADDR(&sc->sc_addr);
1133 carp_send_na(struct carp_softc *sc)
1135 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1137 struct in6_addr *in6;
1139 CARP_FOREACH_IFA(sc, ifa) {
1140 if (ifa->ifa_addr->sa_family != AF_INET6)
1144 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1145 ND_NA_FLAG_OVERRIDE, 1, NULL);
1146 DELAY(1000); /* XXX */
1151 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1152 * matches and is not a carp address. Returns NULL otherwise.
1155 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1161 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1162 if (ifa->ifa_addr->sa_family != AF_INET6)
1164 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1166 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1172 IF_ADDR_RUNLOCK(ifp);
1178 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1183 IFNET_FOREACH_IFA(ifp, ifa)
1184 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1185 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1186 struct carp_softc *sc = ifa->ifa_carp;
1189 IF_ADDR_RUNLOCK(ifp);
1191 mtag = m_tag_get(PACKET_TAG_CARP,
1192 sizeof(struct carp_softc *), M_NOWAIT);
1194 /* Better a bit than nothing. */
1195 return (LLADDR(&sc->sc_addr));
1197 bcopy(&sc, mtag + 1, sizeof(sc));
1198 m_tag_prepend(m, mtag);
1200 return (LLADDR(&sc->sc_addr));
1202 IF_ADDR_RUNLOCK(ifp);
1209 carp_forus(struct ifnet *ifp, u_char *dhost)
1211 struct carp_softc *sc;
1212 uint8_t *ena = dhost;
1214 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1217 CIF_LOCK(ifp->if_carp);
1218 IFNET_FOREACH_CARP(ifp, sc) {
1220 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1223 CIF_UNLOCK(ifp->if_carp);
1228 CIF_UNLOCK(ifp->if_carp);
1233 /* Master down timeout event, executed in callout context. */
1235 carp_master_down(void *v)
1237 struct carp_softc *sc = v;
1239 CARP_LOCK_ASSERT(sc);
1241 CURVNET_SET(sc->sc_carpdev->if_vnet);
1242 if (sc->sc_state == BACKUP) {
1243 carp_master_down_locked(sc, "master timed out");
1251 carp_master_down_locked(struct carp_softc *sc, const char *reason)
1254 CARP_LOCK_ASSERT(sc);
1256 switch (sc->sc_state) {
1258 carp_set_state(sc, MASTER, reason);
1259 carp_send_ad_locked(sc);
1272 panic("carp: VHID %u@%s: master_down event in %s state\n",
1274 sc->sc_carpdev->if_xname,
1275 sc->sc_state ? "MASTER" : "INIT");
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 CARP_LOCK_ASSERT(sc);
1292 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1293 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1294 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1297 switch (sc->sc_state) {
1299 carp_set_state(sc, BACKUP, "initialization complete");
1303 callout_stop(&sc->sc_ad_tmo);
1304 tv.tv_sec = 3 * sc->sc_advbase;
1305 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1309 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1310 carp_master_down, sc);
1315 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1316 carp_master_down, sc);
1322 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1323 carp_master_down, sc);
1327 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1328 carp_master_down, sc);
1334 tv.tv_sec = sc->sc_advbase;
1335 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1336 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1343 * Setup multicast structures.
1346 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1348 struct ifnet *ifp = cif->cif_ifp;
1355 struct ip_moptions *imo = &cif->cif_imo;
1356 struct in_addr addr;
1358 if (imo->imo_membership)
1361 imo->imo_membership = (struct in_multi **)malloc(
1362 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1364 imo->imo_mfilters = NULL;
1365 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1366 imo->imo_multicast_vif = -1;
1368 addr.s_addr = htonl(INADDR_CARP_GROUP);
1369 if ((error = in_joingroup(ifp, &addr, NULL,
1370 &imo->imo_membership[0])) != 0) {
1371 free(imo->imo_membership, M_CARP);
1374 imo->imo_num_memberships++;
1375 imo->imo_multicast_ifp = ifp;
1376 imo->imo_multicast_ttl = CARP_DFLTTL;
1377 imo->imo_multicast_loop = 0;
1384 struct ip6_moptions *im6o = &cif->cif_im6o;
1385 struct in6_addr in6;
1386 struct in6_multi *in6m;
1388 if (im6o->im6o_membership)
1391 im6o->im6o_membership = (struct in6_multi **)malloc(
1392 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1394 im6o->im6o_mfilters = NULL;
1395 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1396 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1397 im6o->im6o_multicast_ifp = ifp;
1399 /* Join IPv6 CARP multicast group. */
1400 bzero(&in6, sizeof(in6));
1401 in6.s6_addr16[0] = htons(0xff02);
1402 in6.s6_addr8[15] = 0x12;
1403 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1404 free(im6o->im6o_membership, M_CARP);
1408 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1409 free(im6o->im6o_membership, M_CARP);
1412 im6o->im6o_membership[0] = in6m;
1413 im6o->im6o_num_memberships++;
1415 /* Join solicited multicast address. */
1416 bzero(&in6, sizeof(in6));
1417 in6.s6_addr16[0] = htons(0xff02);
1418 in6.s6_addr32[1] = 0;
1419 in6.s6_addr32[2] = htonl(1);
1420 in6.s6_addr32[3] = 0;
1421 in6.s6_addr8[12] = 0xff;
1422 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1423 in6_mc_leave(im6o->im6o_membership[0], NULL);
1424 free(im6o->im6o_membership, M_CARP);
1428 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1429 in6_mc_leave(im6o->im6o_membership[0], NULL);
1430 free(im6o->im6o_membership, M_CARP);
1433 im6o->im6o_membership[1] = in6m;
1434 im6o->im6o_num_memberships++;
1444 * Free multicast structures.
1447 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1450 sx_assert(&carp_sx, SA_XLOCKED);
1455 if (cif->cif_naddrs == 0) {
1456 struct ip_moptions *imo = &cif->cif_imo;
1458 in_leavegroup(imo->imo_membership[0], NULL);
1459 KASSERT(imo->imo_mfilters == NULL,
1460 ("%s: imo_mfilters != NULL", __func__));
1461 free(imo->imo_membership, M_CARP);
1462 imo->imo_membership = NULL;
1469 if (cif->cif_naddrs6 == 0) {
1470 struct ip6_moptions *im6o = &cif->cif_im6o;
1472 in6_mc_leave(im6o->im6o_membership[0], NULL);
1473 in6_mc_leave(im6o->im6o_membership[1], NULL);
1474 KASSERT(im6o->im6o_mfilters == NULL,
1475 ("%s: im6o_mfilters != NULL", __func__));
1476 free(im6o->im6o_membership, M_CARP);
1477 im6o->im6o_membership = NULL;
1485 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1488 struct carp_softc *sc;
1493 switch (sa->sa_family) {
1506 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1510 bcopy(mtag + 1, &sc, sizeof(sc));
1512 /* Set the source MAC address to the Virtual Router MAC Address. */
1513 switch (ifp->if_type) {
1517 struct ether_header *eh;
1519 eh = mtod(m, struct ether_header *);
1520 eh->ether_shost[0] = 0;
1521 eh->ether_shost[1] = 0;
1522 eh->ether_shost[2] = 0x5e;
1523 eh->ether_shost[3] = 0;
1524 eh->ether_shost[4] = 1;
1525 eh->ether_shost[5] = sc->sc_vhid;
1529 struct fddi_header *fh;
1531 fh = mtod(m, struct fddi_header *);
1532 fh->fddi_shost[0] = 0;
1533 fh->fddi_shost[1] = 0;
1534 fh->fddi_shost[2] = 0x5e;
1535 fh->fddi_shost[3] = 0;
1536 fh->fddi_shost[4] = 1;
1537 fh->fddi_shost[5] = sc->sc_vhid;
1540 case IFT_ISO88025: {
1541 struct iso88025_header *th;
1542 th = mtod(m, struct iso88025_header *);
1543 th->iso88025_shost[0] = 3;
1544 th->iso88025_shost[1] = 0;
1545 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1546 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1547 th->iso88025_shost[4] = 0;
1548 th->iso88025_shost[5] = 0;
1552 printf("%s: carp is not supported for the %d interface type\n",
1553 ifp->if_xname, ifp->if_type);
1554 return (EOPNOTSUPP);
1560 static struct carp_softc*
1561 carp_alloc(struct ifnet *ifp)
1563 struct carp_softc *sc;
1564 struct carp_if *cif;
1566 sx_assert(&carp_sx, SA_XLOCKED);
1568 if ((cif = ifp->if_carp) == NULL)
1569 cif = carp_alloc_if(ifp);
1571 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1573 sc->sc_advbase = CARP_DFLTINTV;
1574 sc->sc_vhid = -1; /* required setting */
1575 sc->sc_init_counter = 1;
1576 sc->sc_state = INIT;
1578 sc->sc_ifasiz = sizeof(struct ifaddr *);
1579 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1580 sc->sc_carpdev = ifp;
1584 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1587 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1589 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1592 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1595 mtx_lock(&carp_mtx);
1596 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1597 mtx_unlock(&carp_mtx);
1603 carp_grow_ifas(struct carp_softc *sc)
1605 struct ifaddr **new;
1607 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO);
1609 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1610 free(sc->sc_ifas, M_CARP);
1617 carp_destroy(struct carp_softc *sc)
1619 struct ifnet *ifp = sc->sc_carpdev;
1620 struct carp_if *cif = ifp->if_carp;
1622 sx_assert(&carp_sx, SA_XLOCKED);
1624 if (sc->sc_suppress)
1625 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1629 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1632 mtx_lock(&carp_mtx);
1633 LIST_REMOVE(sc, sc_next);
1634 mtx_unlock(&carp_mtx);
1636 callout_drain(&sc->sc_ad_tmo);
1638 callout_drain(&sc->sc_md_tmo);
1641 callout_drain(&sc->sc_md6_tmo);
1643 CARP_LOCK_DESTROY(sc);
1645 free(sc->sc_ifas, M_CARP);
1649 static struct carp_if*
1650 carp_alloc_if(struct ifnet *ifp)
1652 struct carp_if *cif;
1655 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1657 if ((error = ifpromisc(ifp, 1)) != 0)
1658 printf("%s: ifpromisc(%s) failed: %d\n",
1659 __func__, ifp->if_xname, error);
1661 cif->cif_flags |= CIF_PROMISC;
1665 TAILQ_INIT(&cif->cif_vrs);
1670 IF_ADDR_WUNLOCK(ifp);
1676 carp_free_if(struct carp_if *cif)
1678 struct ifnet *ifp = cif->cif_ifp;
1680 CIF_LOCK_ASSERT(cif);
1681 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1685 ifp->if_carp = NULL;
1686 IF_ADDR_WUNLOCK(ifp);
1688 CIF_LOCK_DESTROY(cif);
1690 if (cif->cif_flags & CIF_PROMISC)
1698 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1702 carpr->carpr_state = sc->sc_state;
1703 carpr->carpr_vhid = sc->sc_vhid;
1704 carpr->carpr_advbase = sc->sc_advbase;
1705 carpr->carpr_advskew = sc->sc_advskew;
1707 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1709 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1714 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1716 struct carpreq carpr;
1718 struct carp_softc *sc = NULL;
1719 int error = 0, locked = 0;
1721 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1724 ifp = ifunit_ref(ifr->ifr_name);
1728 switch (ifp->if_type) {
1740 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1741 error = EADDRNOTAVAIL;
1748 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1750 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1751 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1757 IFNET_FOREACH_CARP(ifp, sc)
1758 if (sc->sc_vhid == carpr.carpr_vhid)
1762 sc = carp_alloc(ifp);
1764 sc->sc_vhid = carpr.carpr_vhid;
1765 LLADDR(&sc->sc_addr)[0] = 0;
1766 LLADDR(&sc->sc_addr)[1] = 0;
1767 LLADDR(&sc->sc_addr)[2] = 0x5e;
1768 LLADDR(&sc->sc_addr)[3] = 0;
1769 LLADDR(&sc->sc_addr)[4] = 1;
1770 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1774 if (carpr.carpr_advbase > 0) {
1775 if (carpr.carpr_advbase > 255 ||
1776 carpr.carpr_advbase < CARP_DFLTINTV) {
1780 sc->sc_advbase = carpr.carpr_advbase;
1782 if (carpr.carpr_advskew >= 255) {
1786 sc->sc_advskew = carpr.carpr_advskew;
1787 if (carpr.carpr_key[0] != '\0') {
1788 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1789 carp_hmac_prepare(sc);
1791 if (sc->sc_state != INIT &&
1792 carpr.carpr_state != sc->sc_state) {
1793 switch (carpr.carpr_state) {
1795 callout_stop(&sc->sc_ad_tmo);
1796 carp_set_state(sc, BACKUP,
1797 "user requested via ifconfig");
1802 carp_master_down_locked(sc,
1803 "user requested via ifconfig");
1815 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1819 if (carpr.carpr_count < 1) {
1823 if (ifp->if_carp == NULL) {
1828 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1829 if (carpr.carpr_vhid != 0) {
1830 IFNET_FOREACH_CARP(ifp, sc)
1831 if (sc->sc_vhid == carpr.carpr_vhid)
1837 carp_carprcp(&carpr, sc, priveleged);
1838 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1843 IFNET_FOREACH_CARP(ifp, sc)
1846 if (count > carpr.carpr_count) {
1847 CIF_UNLOCK(ifp->if_carp);
1853 IFNET_FOREACH_CARP(ifp, sc) {
1854 carp_carprcp(&carpr, sc, priveleged);
1855 carpr.carpr_count = count;
1856 error = copyout(&carpr, ifr->ifr_data +
1857 (i * sizeof(carpr)), sizeof(carpr));
1859 CIF_UNLOCK(ifp->if_carp);
1870 sx_xunlock(&carp_sx);
1881 carp_get_vhid(struct ifaddr *ifa)
1884 if (ifa == NULL || ifa->ifa_carp == NULL)
1887 return (ifa->ifa_carp->sc_vhid);
1891 carp_attach(struct ifaddr *ifa, int vhid)
1893 struct ifnet *ifp = ifa->ifa_ifp;
1894 struct carp_if *cif = ifp->if_carp;
1895 struct carp_softc *sc;
1898 KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa));
1900 switch (ifa->ifa_addr->sa_family) {
1909 return (EPROTOTYPE);
1913 if (ifp->if_carp == NULL) {
1914 sx_xunlock(&carp_sx);
1915 return (ENOPROTOOPT);
1918 IFNET_FOREACH_CARP(ifp, sc)
1919 if (sc->sc_vhid == vhid)
1922 sx_xunlock(&carp_sx);
1926 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1929 sx_xunlock(&carp_sx);
1933 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1934 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1937 switch (ifa->ifa_addr->sa_family) {
1955 sc->sc_ifas[index - 1] = ifa;
1957 carp_hmac_prepare(sc);
1961 sx_xunlock(&carp_sx);
1967 carp_detach(struct ifaddr *ifa, bool keep_cif)
1969 struct ifnet *ifp = ifa->ifa_ifp;
1970 struct carp_if *cif = ifp->if_carp;
1971 struct carp_softc *sc = ifa->ifa_carp;
1974 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1980 index = sc->sc_naddrs + sc->sc_naddrs6;
1981 for (i = 0; i < index; i++)
1982 if (sc->sc_ifas[i] == ifa)
1984 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1985 for (; i < index - 1; i++)
1986 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1987 sc->sc_ifas[index - 1] = NULL;
1989 switch (ifa->ifa_addr->sa_family) {
2004 carp_ifa_delroute(ifa);
2005 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
2007 ifa->ifa_carp = NULL;
2010 carp_hmac_prepare(sc);
2013 if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0)
2021 sx_xunlock(&carp_sx);
2025 carp_set_state(struct carp_softc *sc, int state, const char *reason)
2028 CARP_LOCK_ASSERT(sc);
2030 if (sc->sc_state != state) {
2031 const char *carp_states[] = { CARP_STATES };
2032 char subsys[IFNAMSIZ+5];
2034 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
2035 sc->sc_carpdev->if_xname);
2037 CARP_LOG("%s: %s -> %s (%s)\n", subsys,
2038 carp_states[sc->sc_state], carp_states[state], reason);
2040 sc->sc_state = state;
2042 devctl_notify("CARP", subsys, carp_states[state], NULL);
2047 carp_linkstate(struct ifnet *ifp)
2049 struct carp_softc *sc;
2051 CIF_LOCK(ifp->if_carp);
2052 IFNET_FOREACH_CARP(ifp, sc) {
2057 CIF_UNLOCK(ifp->if_carp);
2061 carp_sc_state(struct carp_softc *sc)
2064 CARP_LOCK_ASSERT(sc);
2066 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2067 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2068 callout_stop(&sc->sc_ad_tmo);
2070 callout_stop(&sc->sc_md_tmo);
2073 callout_stop(&sc->sc_md6_tmo);
2075 carp_set_state(sc, INIT, "hardware interface down");
2077 if (!sc->sc_suppress)
2078 carp_demote_adj(V_carp_ifdown_adj, "interface down");
2079 sc->sc_suppress = 1;
2081 carp_set_state(sc, INIT, "hardware interface up");
2083 if (sc->sc_suppress)
2084 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2085 sc->sc_suppress = 0;
2090 carp_demote_adj(int adj, char *reason)
2092 atomic_add_int(&V_carp_demotion, adj);
2093 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2094 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2098 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2102 new = V_carp_demotion;
2103 error = sysctl_handle_int(oidp, &new, 0, req);
2104 if (error || !req->newptr)
2107 carp_demote_adj(new, "sysctl");
2113 extern struct domain inetdomain;
2114 static struct protosw in_carp_protosw = {
2115 .pr_type = SOCK_RAW,
2116 .pr_domain = &inetdomain,
2117 .pr_protocol = IPPROTO_CARP,
2118 .pr_flags = PR_ATOMIC|PR_ADDR,
2119 .pr_input = carp_input,
2120 .pr_output = rip_output,
2121 .pr_ctloutput = rip_ctloutput,
2122 .pr_usrreqs = &rip_usrreqs
2127 extern struct domain inet6domain;
2128 static struct protosw in6_carp_protosw = {
2129 .pr_type = SOCK_RAW,
2130 .pr_domain = &inet6domain,
2131 .pr_protocol = IPPROTO_CARP,
2132 .pr_flags = PR_ATOMIC|PR_ADDR,
2133 .pr_input = carp6_input,
2134 .pr_output = rip6_output,
2135 .pr_ctloutput = rip6_ctloutput,
2136 .pr_usrreqs = &rip6_usrreqs
2141 carp_mod_cleanup(void)
2145 if (proto_reg[CARP_INET] == 0) {
2146 (void)ipproto_unregister(IPPROTO_CARP);
2147 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2148 proto_reg[CARP_INET] = -1;
2150 carp_iamatch_p = NULL;
2153 if (proto_reg[CARP_INET6] == 0) {
2154 (void)ip6proto_unregister(IPPROTO_CARP);
2155 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2156 proto_reg[CARP_INET6] = -1;
2158 carp_iamatch6_p = NULL;
2159 carp_macmatch6_p = NULL;
2161 carp_ioctl_p = NULL;
2162 carp_attach_p = NULL;
2163 carp_detach_p = NULL;
2164 carp_get_vhid_p = NULL;
2165 carp_linkstate_p = NULL;
2166 carp_forus_p = NULL;
2167 carp_output_p = NULL;
2168 carp_demote_adj_p = NULL;
2169 carp_master_p = NULL;
2170 mtx_unlock(&carp_mtx);
2171 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2172 mtx_destroy(&carp_mtx);
2173 sx_destroy(&carp_sx);
2181 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2182 sx_init(&carp_sx, "carp_sx");
2183 LIST_INIT(&carp_list);
2184 carp_get_vhid_p = carp_get_vhid;
2185 carp_forus_p = carp_forus;
2186 carp_output_p = carp_output;
2187 carp_linkstate_p = carp_linkstate;
2188 carp_ioctl_p = carp_ioctl;
2189 carp_attach_p = carp_attach;
2190 carp_detach_p = carp_detach;
2191 carp_demote_adj_p = carp_demote_adj;
2192 carp_master_p = carp_master;
2194 carp_iamatch6_p = carp_iamatch6;
2195 carp_macmatch6_p = carp_macmatch6;
2196 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2197 (struct protosw *)&in6_carp_protosw);
2198 if (proto_reg[CARP_INET6]) {
2199 printf("carp: error %d attaching to PF_INET6\n",
2200 proto_reg[CARP_INET6]);
2202 return (proto_reg[CARP_INET6]);
2204 err = ip6proto_register(IPPROTO_CARP);
2206 printf("carp: error %d registering with INET6\n", err);
2212 carp_iamatch_p = carp_iamatch;
2213 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2214 if (proto_reg[CARP_INET]) {
2215 printf("carp: error %d attaching to PF_INET\n",
2216 proto_reg[CARP_INET]);
2218 return (proto_reg[CARP_INET]);
2220 err = ipproto_register(IPPROTO_CARP);
2222 printf("carp: error %d registering with INET\n", err);
2231 carp_modevent(module_t mod, int type, void *data)
2235 return carp_mod_load();
2238 mtx_lock(&carp_mtx);
2239 if (LIST_EMPTY(&carp_list))
2242 mtx_unlock(&carp_mtx);
2254 static moduledata_t carp_mod = {
2260 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);