2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Michael Shalayeff.
5 * Copyright (c) 2003 Ryan McBride.
6 * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/malloc.h>
46 #include <sys/module.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/taskqueue.h>
55 #include <sys/counter.h>
57 #include <net/ethernet.h>
59 #include <net/if_var.h>
60 #include <net/if_dl.h>
61 #include <net/if_llatbl.h>
62 #include <net/if_types.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 VNET_DEFINE_STATIC(int, carp_allow) = 1;
191 #define V_carp_allow VNET(carp_allow)
193 /* Set DSCP in outgoing CARP packets. */
194 VNET_DEFINE_STATIC(int, carp_dscp) = 56;
195 #define V_carp_dscp VNET(carp_dscp)
197 /* Preempt slower nodes. */
198 VNET_DEFINE_STATIC(int, carp_preempt) = 0;
199 #define V_carp_preempt VNET(carp_preempt)
202 VNET_DEFINE_STATIC(int, carp_log) = 1;
203 #define V_carp_log VNET(carp_log)
205 /* Global advskew demotion. */
206 VNET_DEFINE_STATIC(int, carp_demotion) = 0;
207 #define V_carp_demotion VNET(carp_demotion)
209 /* Send error demotion factor. */
210 VNET_DEFINE_STATIC(int, carp_senderr_adj) = CARP_MAXSKEW;
211 #define V_carp_senderr_adj VNET(carp_senderr_adj)
213 /* Iface down demotion factor. */
214 VNET_DEFINE_STATIC(int, carp_ifdown_adj) = CARP_MAXSKEW;
215 #define V_carp_ifdown_adj VNET(carp_ifdown_adj)
217 static int carp_allow_sysctl(SYSCTL_HANDLER_ARGS);
218 static int carp_dscp_sysctl(SYSCTL_HANDLER_ARGS);
219 static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
221 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP");
222 SYSCTL_PROC(_net_inet_carp, OID_AUTO, allow,
223 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 0, 0, carp_allow_sysctl, "I",
224 "Accept incoming CARP packets");
225 SYSCTL_PROC(_net_inet_carp, OID_AUTO, dscp,
226 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 0, 0, carp_dscp_sysctl, "I",
227 "DSCP value for carp packets");
228 SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
229 &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
230 SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
231 &VNET_NAME(carp_log), 0, "CARP log level");
232 SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
233 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
234 0, 0, carp_demote_adj_sysctl, "I",
235 "Adjust demotion factor (skew of advskew)");
236 SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
237 CTLFLAG_VNET | CTLFLAG_RW,
238 &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
239 SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
240 CTLFLAG_VNET | CTLFLAG_RW,
241 &VNET_NAME(carp_ifdown_adj), 0,
242 "Interface down demotion factor adjustment");
244 VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
245 VNET_PCPUSTAT_SYSINIT(carpstats);
246 VNET_PCPUSTAT_SYSUNINIT(carpstats);
248 #define CARPSTATS_ADD(name, val) \
249 counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
250 sizeof(uint64_t)], (val))
251 #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1)
253 SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
254 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
256 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
258 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
259 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
260 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
261 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
262 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
264 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
265 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
266 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
267 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
268 #define CIF_FREE(cif) do { \
270 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
276 #define CARP_LOG(...) do { \
277 if (V_carp_log > 0) \
278 log(LOG_INFO, "carp: " __VA_ARGS__); \
281 #define CARP_DEBUG(...) do { \
282 if (V_carp_log > 1) \
283 log(LOG_DEBUG, __VA_ARGS__); \
286 #define IFNET_FOREACH_IFA(ifp, ifa) \
287 CK_STAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
288 if ((ifa)->ifa_carp != NULL)
290 #define CARP_FOREACH_IFA(sc, ifa) \
291 CARP_LOCK_ASSERT(sc); \
293 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
294 ((ifa) = sc->sc_ifas[_i]) != NULL; \
297 #define IFNET_FOREACH_CARP(ifp, sc) \
298 KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) || \
299 sx_xlocked(&carp_sx), ("cif_vrs not locked")); \
300 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
302 #define DEMOTE_ADVSKEW(sc) \
303 (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \
304 CARP_MAXSKEW : ((sc)->sc_advskew + V_carp_demotion))
306 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
307 static struct carp_softc
308 *carp_alloc(struct ifnet *);
309 static void carp_destroy(struct carp_softc *);
310 static struct carp_if
311 *carp_alloc_if(struct ifnet *);
312 static void carp_free_if(struct carp_if *);
313 static void carp_set_state(struct carp_softc *, int, const char* reason);
314 static void carp_sc_state(struct carp_softc *);
315 static void carp_setrun(struct carp_softc *, sa_family_t);
316 static void carp_master_down(void *);
317 static void carp_master_down_locked(struct carp_softc *,
319 static void carp_send_ad(void *);
320 static void carp_send_ad_locked(struct carp_softc *);
321 static void carp_addroute(struct carp_softc *);
322 static void carp_ifa_addroute(struct ifaddr *);
323 static void carp_delroute(struct carp_softc *);
324 static void carp_ifa_delroute(struct ifaddr *);
325 static void carp_send_ad_all(void *, int);
326 static void carp_demote_adj(int, char *);
328 static LIST_HEAD(, carp_softc) carp_list;
329 static struct mtx carp_mtx;
330 static struct sx carp_sx;
331 static struct task carp_sendall_task =
332 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
335 carp_hmac_prepare(struct carp_softc *sc)
337 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
338 uint8_t vhid = sc->sc_vhid & 0xff;
342 struct in_addr last, cur, in;
345 struct in6_addr last6, cur6, in6;
348 CARP_LOCK_ASSERT(sc);
350 /* Compute ipad from key. */
351 bzero(sc->sc_pad, sizeof(sc->sc_pad));
352 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
353 for (i = 0; i < sizeof(sc->sc_pad); i++)
354 sc->sc_pad[i] ^= 0x36;
356 /* Precompute first part of inner hash. */
357 SHA1Init(&sc->sc_sha1);
358 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
359 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
360 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
361 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
367 cur.s_addr = 0xffffffff;
368 CARP_FOREACH_IFA(sc, ifa) {
369 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
370 if (ifa->ifa_addr->sa_family == AF_INET &&
371 ntohl(in.s_addr) > ntohl(last.s_addr) &&
372 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
373 cur.s_addr = in.s_addr;
378 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
382 memset(&cur6, 0, sizeof(cur6));
386 memset(&cur6, 0xff, sizeof(cur6));
387 CARP_FOREACH_IFA(sc, ifa) {
388 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
389 if (IN6_IS_SCOPE_EMBED(&in6))
390 in6.s6_addr16[1] = 0;
391 if (ifa->ifa_addr->sa_family == AF_INET6 &&
392 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
393 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
399 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
403 /* convert ipad to opad */
404 for (i = 0; i < sizeof(sc->sc_pad); i++)
405 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
409 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
410 unsigned char md[20])
414 CARP_LOCK_ASSERT(sc);
416 /* fetch first half of inner hash */
417 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
419 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
420 SHA1Final(md, &sha1ctx);
424 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
425 SHA1Update(&sha1ctx, md, 20);
426 SHA1Final(md, &sha1ctx);
430 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
431 unsigned char md[20])
433 unsigned char md2[20];
435 CARP_LOCK_ASSERT(sc);
437 carp_hmac_generate(sc, counter, md2);
439 return (bcmp(md, md2, sizeof(md2)));
443 * process input packet.
444 * we have rearranged checks order compared to the rfc,
445 * but it seems more efficient this way or not possible otherwise.
449 carp_input(struct mbuf **mp, int *offp, int proto)
451 struct mbuf *m = *mp;
452 struct ip *ip = mtod(m, struct ip *);
453 struct carp_header *ch;
459 CARPSTATS_INC(carps_ipackets);
463 return (IPPROTO_DONE);
466 /* verify that the IP TTL is 255. */
467 if (ip->ip_ttl != CARP_DFLTTL) {
468 CARPSTATS_INC(carps_badttl);
469 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
471 m->m_pkthdr.rcvif->if_xname);
473 return (IPPROTO_DONE);
476 iplen = ip->ip_hl << 2;
478 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
479 CARPSTATS_INC(carps_badlen);
480 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
481 "on %s\n", __func__, m->m_len - sizeof(struct ip),
482 m->m_pkthdr.rcvif->if_xname);
484 return (IPPROTO_DONE);
487 if (iplen + sizeof(*ch) < m->m_len) {
488 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
489 CARPSTATS_INC(carps_hdrops);
490 CARP_DEBUG("%s: pullup failed\n", __func__);
491 return (IPPROTO_DONE);
493 ip = mtod(m, struct ip *);
495 ch = (struct carp_header *)((char *)ip + iplen);
498 * verify that the received packet length is
499 * equal to the CARP header
501 len = iplen + sizeof(*ch);
502 if (len > m->m_pkthdr.len) {
503 CARPSTATS_INC(carps_badlen);
504 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
506 m->m_pkthdr.rcvif->if_xname);
508 return (IPPROTO_DONE);
511 if ((m = m_pullup(m, len)) == NULL) {
512 CARPSTATS_INC(carps_hdrops);
513 return (IPPROTO_DONE);
515 ip = mtod(m, struct ip *);
516 ch = (struct carp_header *)((char *)ip + iplen);
518 /* verify the CARP checksum */
520 if (in_cksum(m, len - iplen)) {
521 CARPSTATS_INC(carps_badsum);
522 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
523 m->m_pkthdr.rcvif->if_xname);
525 return (IPPROTO_DONE);
529 carp_input_c(m, ch, AF_INET);
530 return (IPPROTO_DONE);
536 carp6_input(struct mbuf **mp, int *offp, int proto)
538 struct mbuf *m = *mp;
539 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
540 struct carp_header *ch;
543 CARPSTATS_INC(carps_ipackets6);
547 return (IPPROTO_DONE);
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("%s: packet received on non-carp interface: %s\n",
554 __func__, m->m_pkthdr.rcvif->if_xname);
556 return (IPPROTO_DONE);
559 /* verify that the IP TTL is 255 */
560 if (ip6->ip6_hlim != CARP_DFLTTL) {
561 CARPSTATS_INC(carps_badttl);
562 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
563 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
565 return (IPPROTO_DONE);
568 /* verify that we have a complete carp packet */
570 m = m_pullup(m, *offp + sizeof(*ch));
572 CARPSTATS_INC(carps_badlen);
573 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
574 return (IPPROTO_DONE);
576 ch = (struct carp_header *)(mtod(m, caddr_t) + *offp);
579 /* verify the CARP checksum */
581 if (in_cksum(m, sizeof(*ch))) {
582 CARPSTATS_INC(carps_badsum);
583 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
584 m->m_pkthdr.rcvif->if_xname);
586 return (IPPROTO_DONE);
590 carp_input_c(m, ch, AF_INET6);
591 return (IPPROTO_DONE);
596 * This routine should not be necessary at all, but some switches
597 * (VMWare ESX vswitches) can echo our own packets back at us,
598 * and we must ignore them or they will cause us to drop out of
601 * We cannot catch all cases of network loops. Instead, what we
602 * do here is catch any packet that arrives with a carp header
603 * with a VHID of 0, that comes from an address that is our own.
604 * These packets are by definition "from us" (even if they are from
605 * a misconfigured host that is pretending to be us).
607 * The VHID test is outside this mini-function.
610 carp_source_is_self(struct mbuf *m, struct ifaddr *ifa, sa_family_t af)
624 ip4 = mtod(m, struct ip *);
625 in4 = ifatoia(ifa)->ia_addr.sin_addr;
626 return (in4.s_addr == ip4->ip_src.s_addr);
630 ip6 = mtod(m, struct ip6_hdr *);
631 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
632 return (memcmp(&in6, &ip6->ip6_src, sizeof(in6)) == 0);
641 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
643 struct ifnet *ifp = m->m_pkthdr.rcvif;
644 struct ifaddr *ifa, *match;
645 struct carp_softc *sc;
646 uint64_t tmp_counter;
647 struct timeval sc_tv, ch_tv;
653 * Verify that the VHID is valid on the receiving interface.
655 * There should be just one match. If there are none
656 * the VHID is not valid and we drop the packet. If
657 * there are multiple VHID matches, take just the first
658 * one, for compatibility with previous code. While we're
659 * scanning, check for obvious loops in the network topology
660 * (these should never happen, and as noted above, we may
661 * miss real loops; this is just a double-check).
665 IFNET_FOREACH_IFA(ifp, ifa) {
666 if (match == NULL && ifa->ifa_carp != NULL &&
667 ifa->ifa_addr->sa_family == af &&
668 ifa->ifa_carp->sc_vhid == ch->carp_vhid)
670 if (ch->carp_vhid == 0 && carp_source_is_self(m, ifa, af))
673 ifa = error ? NULL : match;
678 if (error == ELOOP) {
679 CARP_DEBUG("dropping looped packet on interface %s\n",
681 CARPSTATS_INC(carps_badif); /* ??? */
683 CARPSTATS_INC(carps_badvhid);
689 /* verify the CARP version. */
690 if (ch->carp_version != CARP_VERSION) {
691 CARPSTATS_INC(carps_badver);
692 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
703 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
704 CARPSTATS_INC(carps_badauth);
705 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
706 sc->sc_vhid, ifp->if_xname);
710 tmp_counter = ntohl(ch->carp_counter[0]);
711 tmp_counter = tmp_counter<<32;
712 tmp_counter += ntohl(ch->carp_counter[1]);
714 /* XXX Replay protection goes here */
716 sc->sc_init_counter = 0;
717 sc->sc_counter = tmp_counter;
719 sc_tv.tv_sec = sc->sc_advbase;
720 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
721 ch_tv.tv_sec = ch->carp_advbase;
722 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
724 switch (sc->sc_state) {
729 * If we receive an advertisement from a master who's going to
730 * be more frequent than us, go into BACKUP state.
732 if (timevalcmp(&sc_tv, &ch_tv, >) ||
733 timevalcmp(&sc_tv, &ch_tv, ==)) {
734 callout_stop(&sc->sc_ad_tmo);
735 carp_set_state(sc, BACKUP,
736 "more frequent advertisement received");
743 * If we're pre-empting masters who advertise slower than us,
744 * and this one claims to be slower, treat him as down.
746 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
747 carp_master_down_locked(sc,
748 "preempting a slower master");
753 * If the master is going to advertise at such a low frequency
754 * that he's guaranteed to time out, we'd might as well just
755 * treat him as timed out now.
757 sc_tv.tv_sec = sc->sc_advbase * 3;
758 if (timevalcmp(&sc_tv, &ch_tv, <)) {
759 carp_master_down_locked(sc, "master will time out");
764 * Otherwise, we reset the counter and wait for the next
777 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
781 if (sc->sc_init_counter) {
782 /* this could also be seconds since unix epoch */
783 sc->sc_counter = arc4random();
784 sc->sc_counter = sc->sc_counter << 32;
785 sc->sc_counter += arc4random();
789 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
790 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
792 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
794 /* Tag packet for carp_output */
795 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
796 M_NOWAIT)) == NULL) {
798 CARPSTATS_INC(carps_onomem);
801 bcopy(&sc, mtag + 1, sizeof(sc));
802 m_tag_prepend(m, mtag);
808 * To avoid LORs and possible recursions this function shouldn't
809 * be called directly, but scheduled via taskqueue.
812 carp_send_ad_all(void *ctx __unused, int pending __unused)
814 struct carp_softc *sc;
817 LIST_FOREACH(sc, &carp_list, sc_next)
818 if (sc->sc_state == MASTER) {
820 CURVNET_SET(sc->sc_carpdev->if_vnet);
821 carp_send_ad_locked(sc);
825 mtx_unlock(&carp_mtx);
828 /* Send a periodic advertisement, executed in callout context. */
830 carp_send_ad(void *v)
832 struct carp_softc *sc = v;
834 CARP_LOCK_ASSERT(sc);
835 CURVNET_SET(sc->sc_carpdev->if_vnet);
836 carp_send_ad_locked(sc);
842 carp_send_ad_error(struct carp_softc *sc, int error)
846 if (sc->sc_sendad_errors < INT_MAX)
847 sc->sc_sendad_errors++;
848 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
849 static const char fmt[] = "send error %d on %s";
850 char msg[sizeof(fmt) + IFNAMSIZ];
852 sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
853 carp_demote_adj(V_carp_senderr_adj, msg);
855 sc->sc_sendad_success = 0;
857 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS &&
858 ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
859 static const char fmt[] = "send ok on %s";
860 char msg[sizeof(fmt) + IFNAMSIZ];
862 sprintf(msg, fmt, sc->sc_carpdev->if_xname);
863 carp_demote_adj(-V_carp_senderr_adj, msg);
864 sc->sc_sendad_errors = 0;
866 sc->sc_sendad_errors = 0;
871 * Pick the best ifaddr on the given ifp for sending CARP
874 * "Best" here is defined by ifa_preferred(). This function is much
875 * much like ifaof_ifpforaddr() except that we just use ifa_preferred().
877 * (This could be simplified to return the actual address, except that
878 * it has a different format in AF_INET and AF_INET6.)
880 static struct ifaddr *
881 carp_best_ifa(int af, struct ifnet *ifp)
883 struct ifaddr *ifa, *best;
890 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
891 if (ifa->ifa_addr->sa_family == af &&
892 (best == NULL || ifa_preferred(best, ifa)))
901 carp_send_ad_locked(struct carp_softc *sc)
903 struct carp_header ch;
906 struct carp_header *ch_ptr;
910 CARP_LOCK_ASSERT(sc);
912 advskew = DEMOTE_ADVSKEW(sc);
913 tv.tv_sec = sc->sc_advbase;
914 tv.tv_usec = advskew * 1000000 / 256;
916 ch.carp_version = CARP_VERSION;
917 ch.carp_type = CARP_ADVERTISEMENT;
918 ch.carp_vhid = sc->sc_vhid;
919 ch.carp_advbase = sc->sc_advbase;
920 ch.carp_advskew = advskew;
921 ch.carp_authlen = 7; /* XXX DEFINE */
922 ch.carp_pad1 = 0; /* must be zero */
925 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
931 m = m_gethdr(M_NOWAIT, MT_DATA);
933 CARPSTATS_INC(carps_onomem);
936 len = sizeof(*ip) + sizeof(ch);
937 m->m_pkthdr.len = len;
938 m->m_pkthdr.rcvif = NULL;
940 M_ALIGN(m, m->m_len);
941 m->m_flags |= M_MCAST;
942 ip = mtod(m, struct ip *);
943 ip->ip_v = IPVERSION;
944 ip->ip_hl = sizeof(*ip) >> 2;
945 ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET;
946 ip->ip_len = htons(len);
947 ip->ip_off = htons(IP_DF);
948 ip->ip_ttl = CARP_DFLTTL;
949 ip->ip_p = IPPROTO_CARP;
953 ifa = carp_best_ifa(AF_INET, sc->sc_carpdev);
956 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
959 ip->ip_src.s_addr = 0;
960 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
962 ch_ptr = (struct carp_header *)(&ip[1]);
963 bcopy(&ch, ch_ptr, sizeof(ch));
964 if (carp_prepare_ad(m, sc, ch_ptr))
967 m->m_data += sizeof(*ip);
968 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
969 m->m_data -= sizeof(*ip);
971 CARPSTATS_INC(carps_opackets);
973 carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
974 &sc->sc_carpdev->if_carp->cif_imo, NULL));
978 if (sc->sc_naddrs6) {
981 m = m_gethdr(M_NOWAIT, MT_DATA);
983 CARPSTATS_INC(carps_onomem);
986 len = sizeof(*ip6) + sizeof(ch);
987 m->m_pkthdr.len = len;
988 m->m_pkthdr.rcvif = NULL;
990 M_ALIGN(m, m->m_len);
991 m->m_flags |= M_MCAST;
992 ip6 = mtod(m, struct ip6_hdr *);
993 bzero(ip6, sizeof(*ip6));
994 ip6->ip6_vfc |= IPV6_VERSION;
995 /* Traffic class isn't defined in ip6 struct instead
996 * it gets offset into flowid field */
997 ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN +
999 ip6->ip6_hlim = CARP_DFLTTL;
1000 ip6->ip6_nxt = IPPROTO_CARP;
1002 /* set the source address */
1003 ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev);
1005 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
1006 sizeof(struct in6_addr));
1009 /* This should never happen with IPv6. */
1010 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
1012 /* Set the multicast destination. */
1013 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1014 ip6->ip6_dst.s6_addr8[15] = 0x12;
1015 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1017 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
1021 ch_ptr = (struct carp_header *)(&ip6[1]);
1022 bcopy(&ch, ch_ptr, sizeof(ch));
1023 if (carp_prepare_ad(m, sc, ch_ptr))
1026 m->m_data += sizeof(*ip6);
1027 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
1028 m->m_data -= sizeof(*ip6);
1030 CARPSTATS_INC(carps_opackets6);
1032 carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
1033 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
1038 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
1042 carp_addroute(struct carp_softc *sc)
1046 CARP_FOREACH_IFA(sc, ifa)
1047 carp_ifa_addroute(ifa);
1051 carp_ifa_addroute(struct ifaddr *ifa)
1054 switch (ifa->ifa_addr->sa_family) {
1057 in_addprefix(ifatoia(ifa), RTF_UP);
1058 ifa_add_loopback_route(ifa,
1059 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
1064 ifa_add_loopback_route(ifa,
1065 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1066 nd6_add_ifa_lle(ifatoia6(ifa));
1073 carp_delroute(struct carp_softc *sc)
1077 CARP_FOREACH_IFA(sc, ifa)
1078 carp_ifa_delroute(ifa);
1082 carp_ifa_delroute(struct ifaddr *ifa)
1085 switch (ifa->ifa_addr->sa_family) {
1088 ifa_del_loopback_route(ifa,
1089 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
1090 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
1095 ifa_del_loopback_route(ifa,
1096 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1097 nd6_rem_ifa_lle(ifatoia6(ifa), 1);
1104 carp_master(struct ifaddr *ifa)
1106 struct carp_softc *sc = ifa->ifa_carp;
1108 return (sc->sc_state == MASTER);
1113 * Broadcast a gratuitous ARP request containing
1114 * the virtual router MAC address for each IP address
1115 * associated with the virtual router.
1118 carp_send_arp(struct carp_softc *sc)
1121 struct in_addr addr;
1123 CARP_FOREACH_IFA(sc, ifa) {
1124 if (ifa->ifa_addr->sa_family != AF_INET)
1126 addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1127 arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr));
1132 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1134 struct carp_softc *sc = ifa->ifa_carp;
1136 if (sc->sc_state == MASTER) {
1137 *enaddr = LLADDR(&sc->sc_addr);
1147 carp_send_na(struct carp_softc *sc)
1149 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1151 struct in6_addr *in6;
1153 CARP_FOREACH_IFA(sc, ifa) {
1154 if (ifa->ifa_addr->sa_family != AF_INET6)
1158 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1159 ND_NA_FLAG_OVERRIDE, 1, NULL);
1160 DELAY(1000); /* XXX */
1165 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1166 * matches and is not a carp address. Returns NULL otherwise.
1169 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1176 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1177 if (ifa->ifa_addr->sa_family != AF_INET6)
1179 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1181 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1192 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1198 IFNET_FOREACH_IFA(ifp, ifa)
1199 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1200 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1201 struct carp_softc *sc = ifa->ifa_carp;
1204 mtag = m_tag_get(PACKET_TAG_CARP,
1205 sizeof(struct carp_softc *), M_NOWAIT);
1207 /* Better a bit than nothing. */
1208 return (LLADDR(&sc->sc_addr));
1210 bcopy(&sc, mtag + 1, sizeof(sc));
1211 m_tag_prepend(m, mtag);
1213 return (LLADDR(&sc->sc_addr));
1221 carp_forus(struct ifnet *ifp, u_char *dhost)
1223 struct carp_softc *sc;
1224 uint8_t *ena = dhost;
1226 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1229 CIF_LOCK(ifp->if_carp);
1230 IFNET_FOREACH_CARP(ifp, sc) {
1232 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1235 CIF_UNLOCK(ifp->if_carp);
1240 CIF_UNLOCK(ifp->if_carp);
1245 /* Master down timeout event, executed in callout context. */
1247 carp_master_down(void *v)
1249 struct carp_softc *sc = v;
1251 CARP_LOCK_ASSERT(sc);
1253 CURVNET_SET(sc->sc_carpdev->if_vnet);
1254 if (sc->sc_state == BACKUP) {
1255 carp_master_down_locked(sc, "master timed out");
1263 carp_master_down_locked(struct carp_softc *sc, const char *reason)
1266 CARP_LOCK_ASSERT(sc);
1268 switch (sc->sc_state) {
1270 carp_set_state(sc, MASTER, reason);
1271 carp_send_ad_locked(sc);
1284 panic("carp: VHID %u@%s: master_down event in %s state\n",
1286 sc->sc_carpdev->if_xname,
1287 sc->sc_state ? "MASTER" : "INIT");
1294 * When in backup state, af indicates whether to reset the master down timer
1295 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1298 carp_setrun(struct carp_softc *sc, sa_family_t af)
1302 CARP_LOCK_ASSERT(sc);
1304 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1305 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1306 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) ||
1310 switch (sc->sc_state) {
1312 carp_set_state(sc, BACKUP, "initialization complete");
1316 callout_stop(&sc->sc_ad_tmo);
1317 tv.tv_sec = 3 * sc->sc_advbase;
1318 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1322 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1323 carp_master_down, sc);
1328 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1329 carp_master_down, sc);
1335 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1336 carp_master_down, sc);
1340 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1341 carp_master_down, sc);
1347 tv.tv_sec = sc->sc_advbase;
1348 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1349 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1356 * Setup multicast structures.
1359 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1361 struct ifnet *ifp = cif->cif_ifp;
1368 struct ip_moptions *imo = &cif->cif_imo;
1369 struct in_mfilter *imf;
1370 struct in_addr addr;
1372 if (ip_mfilter_first(&imo->imo_head) != NULL)
1375 imf = ip_mfilter_alloc(M_WAITOK, 0, 0);
1376 ip_mfilter_init(&imo->imo_head);
1377 imo->imo_multicast_vif = -1;
1379 addr.s_addr = htonl(INADDR_CARP_GROUP);
1380 if ((error = in_joingroup(ifp, &addr, NULL,
1381 &imf->imf_inm)) != 0) {
1382 ip_mfilter_free(imf);
1386 ip_mfilter_insert(&imo->imo_head, imf);
1387 imo->imo_multicast_ifp = ifp;
1388 imo->imo_multicast_ttl = CARP_DFLTTL;
1389 imo->imo_multicast_loop = 0;
1396 struct ip6_moptions *im6o = &cif->cif_im6o;
1397 struct in6_mfilter *im6f[2];
1398 struct in6_addr in6;
1400 if (ip6_mfilter_first(&im6o->im6o_head))
1403 im6f[0] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
1404 im6f[1] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
1406 ip6_mfilter_init(&im6o->im6o_head);
1407 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1408 im6o->im6o_multicast_ifp = ifp;
1410 /* Join IPv6 CARP multicast group. */
1411 bzero(&in6, sizeof(in6));
1412 in6.s6_addr16[0] = htons(0xff02);
1413 in6.s6_addr8[15] = 0x12;
1414 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1415 ip6_mfilter_free(im6f[0]);
1416 ip6_mfilter_free(im6f[1]);
1419 if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[0]->im6f_in6m, 0)) != 0) {
1420 ip6_mfilter_free(im6f[0]);
1421 ip6_mfilter_free(im6f[1]);
1425 /* Join solicited multicast address. */
1426 bzero(&in6, sizeof(in6));
1427 in6.s6_addr16[0] = htons(0xff02);
1428 in6.s6_addr32[1] = 0;
1429 in6.s6_addr32[2] = htonl(1);
1430 in6.s6_addr32[3] = 0;
1431 in6.s6_addr8[12] = 0xff;
1433 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1434 ip6_mfilter_free(im6f[0]);
1435 ip6_mfilter_free(im6f[1]);
1439 if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[1]->im6f_in6m, 0)) != 0) {
1440 in6_leavegroup(im6f[0]->im6f_in6m, NULL);
1441 ip6_mfilter_free(im6f[0]);
1442 ip6_mfilter_free(im6f[1]);
1445 ip6_mfilter_insert(&im6o->im6o_head, im6f[0]);
1446 ip6_mfilter_insert(&im6o->im6o_head, im6f[1]);
1456 * Free multicast structures.
1459 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1462 struct ip_moptions *imo = &cif->cif_imo;
1463 struct in_mfilter *imf;
1466 struct ip6_moptions *im6o = &cif->cif_im6o;
1467 struct in6_mfilter *im6f;
1469 sx_assert(&carp_sx, SA_XLOCKED);
1474 if (cif->cif_naddrs != 0)
1477 while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
1478 ip_mfilter_remove(&imo->imo_head, imf);
1479 in_leavegroup(imf->imf_inm, NULL);
1480 ip_mfilter_free(imf);
1486 if (cif->cif_naddrs6 != 0)
1489 while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) {
1490 ip6_mfilter_remove(&im6o->im6o_head, im6f);
1491 in6_leavegroup(im6f->im6f_in6m, NULL);
1492 ip6_mfilter_free(im6f);
1500 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1503 struct carp_softc *sc;
1508 switch (sa->sa_family) {
1521 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1525 bcopy(mtag + 1, &sc, sizeof(sc));
1527 /* Set the source MAC address to the Virtual Router MAC Address. */
1528 switch (ifp->if_type) {
1532 struct ether_header *eh;
1534 eh = mtod(m, struct ether_header *);
1535 eh->ether_shost[0] = 0;
1536 eh->ether_shost[1] = 0;
1537 eh->ether_shost[2] = 0x5e;
1538 eh->ether_shost[3] = 0;
1539 eh->ether_shost[4] = 1;
1540 eh->ether_shost[5] = sc->sc_vhid;
1544 printf("%s: carp is not supported for the %d interface type\n",
1545 ifp->if_xname, ifp->if_type);
1546 return (EOPNOTSUPP);
1552 static struct carp_softc*
1553 carp_alloc(struct ifnet *ifp)
1555 struct carp_softc *sc;
1556 struct carp_if *cif;
1558 sx_assert(&carp_sx, SA_XLOCKED);
1560 if ((cif = ifp->if_carp) == NULL)
1561 cif = carp_alloc_if(ifp);
1563 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1565 sc->sc_advbase = CARP_DFLTINTV;
1566 sc->sc_vhid = -1; /* required setting */
1567 sc->sc_init_counter = 1;
1568 sc->sc_state = INIT;
1570 sc->sc_ifasiz = sizeof(struct ifaddr *);
1571 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1572 sc->sc_carpdev = ifp;
1576 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1579 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1581 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1584 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1587 mtx_lock(&carp_mtx);
1588 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1589 mtx_unlock(&carp_mtx);
1595 carp_grow_ifas(struct carp_softc *sc)
1597 struct ifaddr **new;
1599 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO);
1601 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1602 free(sc->sc_ifas, M_CARP);
1609 carp_destroy(struct carp_softc *sc)
1611 struct ifnet *ifp = sc->sc_carpdev;
1612 struct carp_if *cif = ifp->if_carp;
1614 sx_assert(&carp_sx, SA_XLOCKED);
1616 if (sc->sc_suppress)
1617 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1621 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1624 mtx_lock(&carp_mtx);
1625 LIST_REMOVE(sc, sc_next);
1626 mtx_unlock(&carp_mtx);
1628 callout_drain(&sc->sc_ad_tmo);
1630 callout_drain(&sc->sc_md_tmo);
1633 callout_drain(&sc->sc_md6_tmo);
1635 CARP_LOCK_DESTROY(sc);
1637 free(sc->sc_ifas, M_CARP);
1641 static struct carp_if*
1642 carp_alloc_if(struct ifnet *ifp)
1644 struct carp_if *cif;
1647 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1649 if ((error = ifpromisc(ifp, 1)) != 0)
1650 printf("%s: ifpromisc(%s) failed: %d\n",
1651 __func__, ifp->if_xname, error);
1653 cif->cif_flags |= CIF_PROMISC;
1657 TAILQ_INIT(&cif->cif_vrs);
1662 IF_ADDR_WUNLOCK(ifp);
1668 carp_free_if(struct carp_if *cif)
1670 struct ifnet *ifp = cif->cif_ifp;
1672 CIF_LOCK_ASSERT(cif);
1673 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1677 ifp->if_carp = NULL;
1678 IF_ADDR_WUNLOCK(ifp);
1680 CIF_LOCK_DESTROY(cif);
1682 if (cif->cif_flags & CIF_PROMISC)
1690 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1694 carpr->carpr_state = sc->sc_state;
1695 carpr->carpr_vhid = sc->sc_vhid;
1696 carpr->carpr_advbase = sc->sc_advbase;
1697 carpr->carpr_advskew = sc->sc_advskew;
1699 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1701 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1706 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1708 struct carpreq carpr;
1710 struct carp_softc *sc = NULL;
1711 int error = 0, locked = 0;
1713 if ((error = copyin(ifr_data_get_ptr(ifr), &carpr, sizeof carpr)))
1716 ifp = ifunit_ref(ifr->ifr_name);
1720 switch (ifp->if_type) {
1730 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1731 error = EADDRNOTAVAIL;
1738 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1740 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1741 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1747 IFNET_FOREACH_CARP(ifp, sc)
1748 if (sc->sc_vhid == carpr.carpr_vhid)
1752 sc = carp_alloc(ifp);
1754 sc->sc_vhid = carpr.carpr_vhid;
1755 LLADDR(&sc->sc_addr)[0] = 0;
1756 LLADDR(&sc->sc_addr)[1] = 0;
1757 LLADDR(&sc->sc_addr)[2] = 0x5e;
1758 LLADDR(&sc->sc_addr)[3] = 0;
1759 LLADDR(&sc->sc_addr)[4] = 1;
1760 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1764 if (carpr.carpr_advbase > 0) {
1765 if (carpr.carpr_advbase > 255 ||
1766 carpr.carpr_advbase < CARP_DFLTINTV) {
1770 sc->sc_advbase = carpr.carpr_advbase;
1772 if (carpr.carpr_advskew >= 255) {
1776 sc->sc_advskew = carpr.carpr_advskew;
1777 if (carpr.carpr_key[0] != '\0') {
1778 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1779 carp_hmac_prepare(sc);
1781 if (sc->sc_state != INIT &&
1782 carpr.carpr_state != sc->sc_state) {
1783 switch (carpr.carpr_state) {
1785 callout_stop(&sc->sc_ad_tmo);
1786 carp_set_state(sc, BACKUP,
1787 "user requested via ifconfig");
1792 carp_master_down_locked(sc,
1793 "user requested via ifconfig");
1805 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1809 if (carpr.carpr_count < 1) {
1813 if (ifp->if_carp == NULL) {
1818 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1819 if (carpr.carpr_vhid != 0) {
1820 IFNET_FOREACH_CARP(ifp, sc)
1821 if (sc->sc_vhid == carpr.carpr_vhid)
1827 carp_carprcp(&carpr, sc, priveleged);
1828 error = copyout(&carpr, ifr_data_get_ptr(ifr),
1834 IFNET_FOREACH_CARP(ifp, sc)
1837 if (count > carpr.carpr_count) {
1838 CIF_UNLOCK(ifp->if_carp);
1844 IFNET_FOREACH_CARP(ifp, sc) {
1845 carp_carprcp(&carpr, sc, priveleged);
1846 carpr.carpr_count = count;
1847 error = copyout(&carpr,
1848 (caddr_t)ifr_data_get_ptr(ifr) +
1849 (i * sizeof(carpr)), sizeof(carpr));
1851 CIF_UNLOCK(ifp->if_carp);
1862 sx_xunlock(&carp_sx);
1873 carp_get_vhid(struct ifaddr *ifa)
1876 if (ifa == NULL || ifa->ifa_carp == NULL)
1879 return (ifa->ifa_carp->sc_vhid);
1883 carp_attach(struct ifaddr *ifa, int vhid)
1885 struct ifnet *ifp = ifa->ifa_ifp;
1886 struct carp_if *cif = ifp->if_carp;
1887 struct carp_softc *sc;
1890 KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa));
1892 switch (ifa->ifa_addr->sa_family) {
1901 return (EPROTOTYPE);
1905 if (ifp->if_carp == NULL) {
1906 sx_xunlock(&carp_sx);
1907 return (ENOPROTOOPT);
1910 IFNET_FOREACH_CARP(ifp, sc)
1911 if (sc->sc_vhid == vhid)
1914 sx_xunlock(&carp_sx);
1918 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1921 sx_xunlock(&carp_sx);
1925 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1926 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1929 switch (ifa->ifa_addr->sa_family) {
1947 sc->sc_ifas[index - 1] = ifa;
1949 carp_hmac_prepare(sc);
1953 sx_xunlock(&carp_sx);
1959 carp_detach(struct ifaddr *ifa, bool keep_cif)
1961 struct ifnet *ifp = ifa->ifa_ifp;
1962 struct carp_if *cif = ifp->if_carp;
1963 struct carp_softc *sc = ifa->ifa_carp;
1966 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1972 index = sc->sc_naddrs + sc->sc_naddrs6;
1973 for (i = 0; i < index; i++)
1974 if (sc->sc_ifas[i] == ifa)
1976 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1977 for (; i < index - 1; i++)
1978 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1979 sc->sc_ifas[index - 1] = NULL;
1981 switch (ifa->ifa_addr->sa_family) {
1996 carp_ifa_delroute(ifa);
1997 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1999 ifa->ifa_carp = NULL;
2002 carp_hmac_prepare(sc);
2005 if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0)
2013 sx_xunlock(&carp_sx);
2017 carp_set_state(struct carp_softc *sc, int state, const char *reason)
2020 CARP_LOCK_ASSERT(sc);
2022 if (sc->sc_state != state) {
2023 const char *carp_states[] = { CARP_STATES };
2024 char subsys[IFNAMSIZ+5];
2026 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
2027 sc->sc_carpdev->if_xname);
2029 CARP_LOG("%s: %s -> %s (%s)\n", subsys,
2030 carp_states[sc->sc_state], carp_states[state], reason);
2032 sc->sc_state = state;
2034 devctl_notify("CARP", subsys, carp_states[state], NULL);
2039 carp_linkstate(struct ifnet *ifp)
2041 struct carp_softc *sc;
2043 CIF_LOCK(ifp->if_carp);
2044 IFNET_FOREACH_CARP(ifp, sc) {
2049 CIF_UNLOCK(ifp->if_carp);
2053 carp_sc_state(struct carp_softc *sc)
2056 CARP_LOCK_ASSERT(sc);
2058 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2059 !(sc->sc_carpdev->if_flags & IFF_UP) ||
2061 callout_stop(&sc->sc_ad_tmo);
2063 callout_stop(&sc->sc_md_tmo);
2066 callout_stop(&sc->sc_md6_tmo);
2068 carp_set_state(sc, INIT, "hardware interface down");
2070 if (!sc->sc_suppress)
2071 carp_demote_adj(V_carp_ifdown_adj, "interface down");
2072 sc->sc_suppress = 1;
2074 carp_set_state(sc, INIT, "hardware interface up");
2076 if (sc->sc_suppress)
2077 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2078 sc->sc_suppress = 0;
2083 carp_demote_adj(int adj, char *reason)
2085 atomic_add_int(&V_carp_demotion, adj);
2086 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2087 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2091 carp_allow_sysctl(SYSCTL_HANDLER_ARGS)
2094 struct carp_softc *sc;
2097 error = sysctl_handle_int(oidp, &new, 0, req);
2098 if (error || !req->newptr)
2101 if (V_carp_allow != new) {
2104 mtx_lock(&carp_mtx);
2105 LIST_FOREACH(sc, &carp_list, sc_next) {
2107 if (curvnet == sc->sc_carpdev->if_vnet)
2111 mtx_unlock(&carp_mtx);
2118 carp_dscp_sysctl(SYSCTL_HANDLER_ARGS)
2123 error = sysctl_handle_int(oidp, &new, 0, req);
2124 if (error || !req->newptr)
2127 if (new < 0 || new > 63)
2136 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2140 new = V_carp_demotion;
2141 error = sysctl_handle_int(oidp, &new, 0, req);
2142 if (error || !req->newptr)
2145 carp_demote_adj(new, "sysctl");
2151 extern struct domain inetdomain;
2152 static struct protosw in_carp_protosw = {
2153 .pr_type = SOCK_RAW,
2154 .pr_domain = &inetdomain,
2155 .pr_protocol = IPPROTO_CARP,
2156 .pr_flags = PR_ATOMIC|PR_ADDR,
2157 .pr_input = carp_input,
2158 .pr_output = rip_output,
2159 .pr_ctloutput = rip_ctloutput,
2160 .pr_usrreqs = &rip_usrreqs
2165 extern struct domain inet6domain;
2166 static struct protosw in6_carp_protosw = {
2167 .pr_type = SOCK_RAW,
2168 .pr_domain = &inet6domain,
2169 .pr_protocol = IPPROTO_CARP,
2170 .pr_flags = PR_ATOMIC|PR_ADDR,
2171 .pr_input = carp6_input,
2172 .pr_output = rip6_output,
2173 .pr_ctloutput = rip6_ctloutput,
2174 .pr_usrreqs = &rip6_usrreqs
2179 carp_mod_cleanup(void)
2183 if (proto_reg[CARP_INET] == 0) {
2184 (void)ipproto_unregister(IPPROTO_CARP);
2185 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2186 proto_reg[CARP_INET] = -1;
2188 carp_iamatch_p = NULL;
2191 if (proto_reg[CARP_INET6] == 0) {
2192 (void)ip6proto_unregister(IPPROTO_CARP);
2193 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2194 proto_reg[CARP_INET6] = -1;
2196 carp_iamatch6_p = NULL;
2197 carp_macmatch6_p = NULL;
2199 carp_ioctl_p = NULL;
2200 carp_attach_p = NULL;
2201 carp_detach_p = NULL;
2202 carp_get_vhid_p = NULL;
2203 carp_linkstate_p = NULL;
2204 carp_forus_p = NULL;
2205 carp_output_p = NULL;
2206 carp_demote_adj_p = NULL;
2207 carp_master_p = NULL;
2208 mtx_unlock(&carp_mtx);
2209 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2210 mtx_destroy(&carp_mtx);
2211 sx_destroy(&carp_sx);
2219 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2220 sx_init(&carp_sx, "carp_sx");
2221 LIST_INIT(&carp_list);
2222 carp_get_vhid_p = carp_get_vhid;
2223 carp_forus_p = carp_forus;
2224 carp_output_p = carp_output;
2225 carp_linkstate_p = carp_linkstate;
2226 carp_ioctl_p = carp_ioctl;
2227 carp_attach_p = carp_attach;
2228 carp_detach_p = carp_detach;
2229 carp_demote_adj_p = carp_demote_adj;
2230 carp_master_p = carp_master;
2232 carp_iamatch6_p = carp_iamatch6;
2233 carp_macmatch6_p = carp_macmatch6;
2234 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2235 (struct protosw *)&in6_carp_protosw);
2236 if (proto_reg[CARP_INET6]) {
2237 printf("carp: error %d attaching to PF_INET6\n",
2238 proto_reg[CARP_INET6]);
2240 return (proto_reg[CARP_INET6]);
2242 err = ip6proto_register(IPPROTO_CARP);
2244 printf("carp: error %d registering with INET6\n", err);
2250 carp_iamatch_p = carp_iamatch;
2251 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2252 if (proto_reg[CARP_INET]) {
2253 printf("carp: error %d attaching to PF_INET\n",
2254 proto_reg[CARP_INET]);
2256 return (proto_reg[CARP_INET]);
2258 err = ipproto_register(IPPROTO_CARP);
2260 printf("carp: error %d registering with INET\n", err);
2269 carp_modevent(module_t mod, int type, void *data)
2273 return carp_mod_load();
2276 mtx_lock(&carp_mtx);
2277 if (LIST_EMPTY(&carp_list))
2280 mtx_unlock(&carp_mtx);
2292 static moduledata_t carp_mod = {
2298 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);