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>
40 #include <sys/devctl.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/malloc.h>
46 #include <sys/module.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 #include <sys/syslog.h>
53 #include <sys/taskqueue.h>
54 #include <sys/counter.h>
56 #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_private.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
152 * Brief design of carp(4).
154 * Any carp-capable ifnet may have a list of carp softcs hanging off
155 * its ifp->if_carp pointer. Each softc represents one unique virtual
156 * host id, or vhid. The softc has a back pointer to the ifnet. All
157 * softcs are joined in a global list, which has quite limited use.
159 * Any interface address that takes part in CARP negotiation has a
160 * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
161 * AF_INET or AF_INET6 address.
163 * Although, one can get the softc's backpointer to ifnet and traverse
164 * through its ifp->if_addrhead queue to find all interface addresses
165 * involved in CARP, we keep a growable array of ifaddr pointers. This
166 * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
167 * do calls into the network stack, thus avoiding LORs.
171 * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
172 * callout-driven events and ioctl()s.
174 * To traverse the list of softcs on an ifnet we use CIF_LOCK() or carp_sx.
175 * To traverse the global list we use the mutex carp_mtx.
177 * Known issues with locking:
179 * - Sending ad, we put the pointer to the softc in an mtag, and no reference
180 * counting is done on the softc.
181 * - On module unload we may race (?) with packet processing thread
182 * dereferencing our function pointers.
185 /* Accept incoming CARP packets. */
186 VNET_DEFINE_STATIC(int, carp_allow) = 1;
187 #define V_carp_allow VNET(carp_allow)
189 /* Set DSCP in outgoing CARP packets. */
190 VNET_DEFINE_STATIC(int, carp_dscp) = 56;
191 #define V_carp_dscp VNET(carp_dscp)
193 /* Preempt slower nodes. */
194 VNET_DEFINE_STATIC(int, carp_preempt) = 0;
195 #define V_carp_preempt VNET(carp_preempt)
198 VNET_DEFINE_STATIC(int, carp_log) = 1;
199 #define V_carp_log VNET(carp_log)
201 /* Global advskew demotion. */
202 VNET_DEFINE_STATIC(int, carp_demotion) = 0;
203 #define V_carp_demotion VNET(carp_demotion)
205 /* Send error demotion factor. */
206 VNET_DEFINE_STATIC(int, carp_senderr_adj) = CARP_MAXSKEW;
207 #define V_carp_senderr_adj VNET(carp_senderr_adj)
209 /* Iface down demotion factor. */
210 VNET_DEFINE_STATIC(int, carp_ifdown_adj) = CARP_MAXSKEW;
211 #define V_carp_ifdown_adj VNET(carp_ifdown_adj)
213 static int carp_allow_sysctl(SYSCTL_HANDLER_ARGS);
214 static int carp_dscp_sysctl(SYSCTL_HANDLER_ARGS);
215 static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
217 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
219 SYSCTL_PROC(_net_inet_carp, OID_AUTO, allow,
220 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
221 &VNET_NAME(carp_allow), 0, carp_allow_sysctl, "I",
222 "Accept incoming CARP packets");
223 SYSCTL_PROC(_net_inet_carp, OID_AUTO, dscp,
224 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
225 0, 0, carp_dscp_sysctl, "I",
226 "DSCP value for carp packets");
227 SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
228 &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
229 SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
230 &VNET_NAME(carp_log), 0, "CARP log level");
231 SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
232 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
233 0, 0, carp_demote_adj_sysctl, "I",
234 "Adjust demotion factor (skew of advskew)");
235 SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
236 CTLFLAG_VNET | CTLFLAG_RW,
237 &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
238 SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
239 CTLFLAG_VNET | CTLFLAG_RW,
240 &VNET_NAME(carp_ifdown_adj), 0,
241 "Interface down demotion factor adjustment");
243 VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
244 VNET_PCPUSTAT_SYSINIT(carpstats);
245 VNET_PCPUSTAT_SYSUNINIT(carpstats);
247 #define CARPSTATS_ADD(name, val) \
248 counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
249 sizeof(uint64_t)], (val))
250 #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1)
252 SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
253 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
255 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
257 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
258 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
259 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
260 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
261 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
263 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
264 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
265 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
266 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
267 #define CIF_FREE(cif) do { \
269 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
275 #define CARP_LOG(...) do { \
276 if (V_carp_log > 0) \
277 log(LOG_INFO, "carp: " __VA_ARGS__); \
280 #define CARP_DEBUG(...) do { \
281 if (V_carp_log > 1) \
282 log(LOG_DEBUG, __VA_ARGS__); \
285 #define IFNET_FOREACH_IFA(ifp, ifa) \
286 CK_STAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
287 if ((ifa)->ifa_carp != NULL)
289 #define CARP_FOREACH_IFA(sc, ifa) \
290 CARP_LOCK_ASSERT(sc); \
292 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
293 ((ifa) = sc->sc_ifas[_i]) != NULL; \
296 #define IFNET_FOREACH_CARP(ifp, sc) \
297 KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) || \
298 sx_xlocked(&carp_sx), ("cif_vrs not locked")); \
299 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
301 #define DEMOTE_ADVSKEW(sc) \
302 (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \
304 (((sc)->sc_advskew + V_carp_demotion < 0) ? \
305 0 : ((sc)->sc_advskew + V_carp_demotion)))
307 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
308 static struct carp_softc
309 *carp_alloc(struct ifnet *);
310 static void carp_destroy(struct carp_softc *);
311 static struct carp_if
312 *carp_alloc_if(struct ifnet *);
313 static void carp_free_if(struct carp_if *);
314 static void carp_set_state(struct carp_softc *, int, const char* reason);
315 static void carp_sc_state(struct carp_softc *);
316 static void carp_setrun(struct carp_softc *, sa_family_t);
317 static void carp_master_down(void *);
318 static void carp_master_down_locked(struct carp_softc *,
320 static void carp_send_ad(void *);
321 static void carp_send_ad_locked(struct carp_softc *);
322 static void carp_addroute(struct carp_softc *);
323 static void carp_ifa_addroute(struct ifaddr *);
324 static void carp_delroute(struct carp_softc *);
325 static void carp_ifa_delroute(struct ifaddr *);
326 static void carp_send_ad_all(void *, int);
327 static void carp_demote_adj(int, char *);
329 static LIST_HEAD(, carp_softc) carp_list;
330 static struct mtx carp_mtx;
331 static struct sx carp_sx;
332 static struct task carp_sendall_task =
333 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
336 carp_hmac_prepare(struct carp_softc *sc)
338 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
339 uint8_t vhid = sc->sc_vhid & 0xff;
343 struct in_addr last, cur, in;
346 struct in6_addr last6, cur6, in6;
349 CARP_LOCK_ASSERT(sc);
351 /* Compute ipad from key. */
352 bzero(sc->sc_pad, sizeof(sc->sc_pad));
353 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
354 for (i = 0; i < sizeof(sc->sc_pad); i++)
355 sc->sc_pad[i] ^= 0x36;
357 /* Precompute first part of inner hash. */
358 SHA1Init(&sc->sc_sha1);
359 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
360 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
361 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
362 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
368 cur.s_addr = 0xffffffff;
369 CARP_FOREACH_IFA(sc, ifa) {
370 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
371 if (ifa->ifa_addr->sa_family == AF_INET &&
372 ntohl(in.s_addr) > ntohl(last.s_addr) &&
373 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
374 cur.s_addr = in.s_addr;
379 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
383 memset(&cur6, 0, sizeof(cur6));
387 memset(&cur6, 0xff, sizeof(cur6));
388 CARP_FOREACH_IFA(sc, ifa) {
389 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
390 if (IN6_IS_SCOPE_EMBED(&in6))
391 in6.s6_addr16[1] = 0;
392 if (ifa->ifa_addr->sa_family == AF_INET6 &&
393 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
394 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
400 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
404 /* convert ipad to opad */
405 for (i = 0; i < sizeof(sc->sc_pad); i++)
406 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
410 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
411 unsigned char md[20])
415 CARP_LOCK_ASSERT(sc);
417 /* fetch first half of inner hash */
418 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
420 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
421 SHA1Final(md, &sha1ctx);
425 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
426 SHA1Update(&sha1ctx, md, 20);
427 SHA1Final(md, &sha1ctx);
431 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
432 unsigned char md[20])
434 unsigned char md2[20];
436 CARP_LOCK_ASSERT(sc);
438 carp_hmac_generate(sc, counter, md2);
440 return (bcmp(md, md2, sizeof(md2)));
444 * process input packet.
445 * we have rearranged checks order compared to the rfc,
446 * but it seems more efficient this way or not possible otherwise.
450 carp_input(struct mbuf **mp, int *offp, int proto)
452 struct mbuf *m = *mp;
453 struct ip *ip = mtod(m, struct ip *);
454 struct carp_header *ch;
460 CARPSTATS_INC(carps_ipackets);
464 return (IPPROTO_DONE);
467 /* verify that the IP TTL is 255. */
468 if (ip->ip_ttl != CARP_DFLTTL) {
469 CARPSTATS_INC(carps_badttl);
470 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
472 m->m_pkthdr.rcvif->if_xname);
474 return (IPPROTO_DONE);
477 iplen = ip->ip_hl << 2;
479 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
480 CARPSTATS_INC(carps_badlen);
481 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
482 "on %s\n", __func__, m->m_len - sizeof(struct ip),
483 m->m_pkthdr.rcvif->if_xname);
485 return (IPPROTO_DONE);
488 if (iplen + sizeof(*ch) < m->m_len) {
489 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
490 CARPSTATS_INC(carps_hdrops);
491 CARP_DEBUG("%s: pullup failed\n", __func__);
492 return (IPPROTO_DONE);
494 ip = mtod(m, struct ip *);
496 ch = (struct carp_header *)((char *)ip + iplen);
499 * verify that the received packet length is
500 * equal to the CARP header
502 len = iplen + sizeof(*ch);
503 if (len > m->m_pkthdr.len) {
504 CARPSTATS_INC(carps_badlen);
505 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
507 m->m_pkthdr.rcvif->if_xname);
509 return (IPPROTO_DONE);
512 if ((m = m_pullup(m, len)) == NULL) {
513 CARPSTATS_INC(carps_hdrops);
514 return (IPPROTO_DONE);
516 ip = mtod(m, struct ip *);
517 ch = (struct carp_header *)((char *)ip + iplen);
519 /* verify the CARP checksum */
521 if (in_cksum(m, len - iplen)) {
522 CARPSTATS_INC(carps_badsum);
523 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
524 m->m_pkthdr.rcvif->if_xname);
526 return (IPPROTO_DONE);
530 carp_input_c(m, ch, AF_INET);
531 return (IPPROTO_DONE);
537 carp6_input(struct mbuf **mp, int *offp, int proto)
539 struct mbuf *m = *mp;
540 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
541 struct carp_header *ch;
544 CARPSTATS_INC(carps_ipackets6);
548 return (IPPROTO_DONE);
551 /* check if received on a valid carp interface */
552 if (m->m_pkthdr.rcvif->if_carp == NULL) {
553 CARPSTATS_INC(carps_badif);
554 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
555 __func__, m->m_pkthdr.rcvif->if_xname);
557 return (IPPROTO_DONE);
560 /* verify that the IP TTL is 255 */
561 if (ip6->ip6_hlim != CARP_DFLTTL) {
562 CARPSTATS_INC(carps_badttl);
563 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
564 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
566 return (IPPROTO_DONE);
569 /* verify that we have a complete carp packet */
570 if (m->m_len < *offp + sizeof(*ch)) {
572 m = m_pullup(m, *offp + sizeof(*ch));
574 CARPSTATS_INC(carps_badlen);
575 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
576 return (IPPROTO_DONE);
579 ch = (struct carp_header *)(mtod(m, char *) + *offp);
581 /* verify the CARP checksum */
583 if (in_cksum(m, sizeof(*ch))) {
584 CARPSTATS_INC(carps_badsum);
585 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
586 m->m_pkthdr.rcvif->if_xname);
588 return (IPPROTO_DONE);
592 carp_input_c(m, ch, AF_INET6);
593 return (IPPROTO_DONE);
598 * This routine should not be necessary at all, but some switches
599 * (VMWare ESX vswitches) can echo our own packets back at us,
600 * and we must ignore them or they will cause us to drop out of
603 * We cannot catch all cases of network loops. Instead, what we
604 * do here is catch any packet that arrives with a carp header
605 * with a VHID of 0, that comes from an address that is our own.
606 * These packets are by definition "from us" (even if they are from
607 * a misconfigured host that is pretending to be us).
609 * The VHID test is outside this mini-function.
612 carp_source_is_self(struct mbuf *m, struct ifaddr *ifa, sa_family_t af)
626 ip4 = mtod(m, struct ip *);
627 in4 = ifatoia(ifa)->ia_addr.sin_addr;
628 return (in4.s_addr == ip4->ip_src.s_addr);
632 ip6 = mtod(m, struct ip6_hdr *);
633 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
634 return (memcmp(&in6, &ip6->ip6_src, sizeof(in6)) == 0);
643 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
645 struct ifnet *ifp = m->m_pkthdr.rcvif;
646 struct ifaddr *ifa, *match;
647 struct carp_softc *sc;
648 uint64_t tmp_counter;
649 struct timeval sc_tv, ch_tv;
655 * Verify that the VHID is valid on the receiving interface.
657 * There should be just one match. If there are none
658 * the VHID is not valid and we drop the packet. If
659 * there are multiple VHID matches, take just the first
660 * one, for compatibility with previous code. While we're
661 * scanning, check for obvious loops in the network topology
662 * (these should never happen, and as noted above, we may
663 * miss real loops; this is just a double-check).
667 IFNET_FOREACH_IFA(ifp, ifa) {
668 if (match == NULL && ifa->ifa_carp != NULL &&
669 ifa->ifa_addr->sa_family == af &&
670 ifa->ifa_carp->sc_vhid == ch->carp_vhid)
672 if (ch->carp_vhid == 0 && carp_source_is_self(m, ifa, af))
675 ifa = error ? NULL : match;
680 if (error == ELOOP) {
681 CARP_DEBUG("dropping looped packet on interface %s\n",
683 CARPSTATS_INC(carps_badif); /* ??? */
685 CARPSTATS_INC(carps_badvhid);
691 /* verify the CARP version. */
692 if (ch->carp_version != CARP_VERSION) {
693 CARPSTATS_INC(carps_badver);
694 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
705 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
706 CARPSTATS_INC(carps_badauth);
707 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
708 sc->sc_vhid, ifp->if_xname);
712 tmp_counter = ntohl(ch->carp_counter[0]);
713 tmp_counter = tmp_counter<<32;
714 tmp_counter += ntohl(ch->carp_counter[1]);
716 /* XXX Replay protection goes here */
718 sc->sc_init_counter = 0;
719 sc->sc_counter = tmp_counter;
721 sc_tv.tv_sec = sc->sc_advbase;
722 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
723 ch_tv.tv_sec = ch->carp_advbase;
724 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
726 switch (sc->sc_state) {
731 * If we receive an advertisement from a master who's going to
732 * be more frequent than us, go into BACKUP state.
734 if (timevalcmp(&sc_tv, &ch_tv, >) ||
735 timevalcmp(&sc_tv, &ch_tv, ==)) {
736 callout_stop(&sc->sc_ad_tmo);
737 carp_set_state(sc, BACKUP,
738 "more frequent advertisement received");
745 * If we're pre-empting masters who advertise slower than us,
746 * and this one claims to be slower, treat him as down.
748 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
749 carp_master_down_locked(sc,
750 "preempting a slower master");
755 * If the master is going to advertise at such a low frequency
756 * that he's guaranteed to time out, we'd might as well just
757 * treat him as timed out now.
759 sc_tv.tv_sec = sc->sc_advbase * 3;
760 if (timevalcmp(&sc_tv, &ch_tv, <)) {
761 carp_master_down_locked(sc, "master will time out");
766 * Otherwise, we reset the counter and wait for the next
779 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
783 if (sc->sc_init_counter) {
784 /* this could also be seconds since unix epoch */
785 sc->sc_counter = arc4random();
786 sc->sc_counter = sc->sc_counter << 32;
787 sc->sc_counter += arc4random();
791 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
792 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
794 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
796 /* Tag packet for carp_output */
797 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
798 M_NOWAIT)) == NULL) {
800 CARPSTATS_INC(carps_onomem);
803 bcopy(&sc, mtag + 1, sizeof(sc));
804 m_tag_prepend(m, mtag);
810 * To avoid LORs and possible recursions this function shouldn't
811 * be called directly, but scheduled via taskqueue.
814 carp_send_ad_all(void *ctx __unused, int pending __unused)
816 struct carp_softc *sc;
817 struct epoch_tracker et;
821 LIST_FOREACH(sc, &carp_list, sc_next)
822 if (sc->sc_state == MASTER) {
824 CURVNET_SET(sc->sc_carpdev->if_vnet);
825 carp_send_ad_locked(sc);
829 mtx_unlock(&carp_mtx);
833 /* Send a periodic advertisement, executed in callout context. */
835 carp_send_ad(void *v)
837 struct carp_softc *sc = v;
838 struct epoch_tracker et;
841 CARP_LOCK_ASSERT(sc);
842 CURVNET_SET(sc->sc_carpdev->if_vnet);
843 carp_send_ad_locked(sc);
850 carp_send_ad_error(struct carp_softc *sc, int error)
854 * We track errors and successfull sends with this logic:
855 * - Any error resets success counter to 0.
856 * - MAX_ERRORS triggers demotion.
857 * - MIN_SUCCESS successes resets error counter to 0.
858 * - MIN_SUCCESS reverts demotion, if it was triggered before.
861 if (sc->sc_sendad_errors < INT_MAX)
862 sc->sc_sendad_errors++;
863 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
864 static const char fmt[] = "send error %d on %s";
865 char msg[sizeof(fmt) + IFNAMSIZ];
867 sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
868 carp_demote_adj(V_carp_senderr_adj, msg);
870 sc->sc_sendad_success = 0;
871 } else if (sc->sc_sendad_errors > 0) {
872 if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
873 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
874 static const char fmt[] = "send ok on %s";
875 char msg[sizeof(fmt) + IFNAMSIZ];
877 sprintf(msg, fmt, sc->sc_carpdev->if_xname);
878 carp_demote_adj(-V_carp_senderr_adj, msg);
880 sc->sc_sendad_errors = 0;
886 * Pick the best ifaddr on the given ifp for sending CARP
889 * "Best" here is defined by ifa_preferred(). This function is much
890 * much like ifaof_ifpforaddr() except that we just use ifa_preferred().
892 * (This could be simplified to return the actual address, except that
893 * it has a different format in AF_INET and AF_INET6.)
895 static struct ifaddr *
896 carp_best_ifa(int af, struct ifnet *ifp)
898 struct ifaddr *ifa, *best;
905 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
906 if (ifa->ifa_addr->sa_family == af &&
907 (best == NULL || ifa_preferred(best, ifa)))
916 carp_send_ad_locked(struct carp_softc *sc)
918 struct carp_header ch;
921 struct carp_header *ch_ptr;
926 CARP_LOCK_ASSERT(sc);
928 advskew = DEMOTE_ADVSKEW(sc);
929 tv.tv_sec = sc->sc_advbase;
930 tv.tv_usec = advskew * 1000000 / 256;
932 ch.carp_version = CARP_VERSION;
933 ch.carp_type = CARP_ADVERTISEMENT;
934 ch.carp_vhid = sc->sc_vhid;
935 ch.carp_advbase = sc->sc_advbase;
936 ch.carp_advskew = advskew;
937 ch.carp_authlen = 7; /* XXX DEFINE */
938 ch.carp_pad1 = 0; /* must be zero */
941 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
947 m = m_gethdr(M_NOWAIT, MT_DATA);
949 CARPSTATS_INC(carps_onomem);
952 len = sizeof(*ip) + sizeof(ch);
953 m->m_pkthdr.len = len;
954 m->m_pkthdr.rcvif = NULL;
956 M_ALIGN(m, m->m_len);
957 m->m_flags |= M_MCAST;
958 ip = mtod(m, struct ip *);
959 ip->ip_v = IPVERSION;
960 ip->ip_hl = sizeof(*ip) >> 2;
961 ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET;
962 ip->ip_len = htons(len);
963 ip->ip_off = htons(IP_DF);
964 ip->ip_ttl = CARP_DFLTTL;
965 ip->ip_p = IPPROTO_CARP;
969 ifa = carp_best_ifa(AF_INET, sc->sc_carpdev);
972 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
975 ip->ip_src.s_addr = 0;
976 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
978 ch_ptr = (struct carp_header *)(&ip[1]);
979 bcopy(&ch, ch_ptr, sizeof(ch));
980 if (carp_prepare_ad(m, sc, ch_ptr))
983 m->m_data += sizeof(*ip);
984 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
985 m->m_data -= sizeof(*ip);
987 CARPSTATS_INC(carps_opackets);
989 carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
990 &sc->sc_carpdev->if_carp->cif_imo, NULL));
994 if (sc->sc_naddrs6) {
997 m = m_gethdr(M_NOWAIT, MT_DATA);
999 CARPSTATS_INC(carps_onomem);
1002 len = sizeof(*ip6) + sizeof(ch);
1003 m->m_pkthdr.len = len;
1004 m->m_pkthdr.rcvif = NULL;
1006 M_ALIGN(m, m->m_len);
1007 m->m_flags |= M_MCAST;
1008 ip6 = mtod(m, struct ip6_hdr *);
1009 bzero(ip6, sizeof(*ip6));
1010 ip6->ip6_vfc |= IPV6_VERSION;
1011 /* Traffic class isn't defined in ip6 struct instead
1012 * it gets offset into flowid field */
1013 ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN +
1014 IPTOS_DSCP_OFFSET));
1015 ip6->ip6_hlim = CARP_DFLTTL;
1016 ip6->ip6_nxt = IPPROTO_CARP;
1018 /* set the source address */
1019 ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev);
1021 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
1022 sizeof(struct in6_addr));
1025 /* This should never happen with IPv6. */
1026 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
1028 /* Set the multicast destination. */
1029 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1030 ip6->ip6_dst.s6_addr8[15] = 0x12;
1031 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1033 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
1037 ch_ptr = (struct carp_header *)(&ip6[1]);
1038 bcopy(&ch, ch_ptr, sizeof(ch));
1039 if (carp_prepare_ad(m, sc, ch_ptr))
1042 m->m_data += sizeof(*ip6);
1043 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
1044 m->m_data -= sizeof(*ip6);
1046 CARPSTATS_INC(carps_opackets6);
1048 carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
1049 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
1054 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
1058 carp_addroute(struct carp_softc *sc)
1062 CARP_FOREACH_IFA(sc, ifa)
1063 carp_ifa_addroute(ifa);
1067 carp_ifa_addroute(struct ifaddr *ifa)
1070 switch (ifa->ifa_addr->sa_family) {
1073 in_addprefix(ifatoia(ifa));
1074 ifa_add_loopback_route(ifa,
1075 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
1080 ifa_add_loopback_route(ifa,
1081 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1082 nd6_add_ifa_lle(ifatoia6(ifa));
1089 carp_delroute(struct carp_softc *sc)
1093 CARP_FOREACH_IFA(sc, ifa)
1094 carp_ifa_delroute(ifa);
1098 carp_ifa_delroute(struct ifaddr *ifa)
1101 switch (ifa->ifa_addr->sa_family) {
1104 ifa_del_loopback_route(ifa,
1105 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
1106 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
1111 ifa_del_loopback_route(ifa,
1112 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1113 nd6_rem_ifa_lle(ifatoia6(ifa), 1);
1120 carp_master(struct ifaddr *ifa)
1122 struct carp_softc *sc = ifa->ifa_carp;
1124 return (sc->sc_state == MASTER);
1129 * Broadcast a gratuitous ARP request containing
1130 * the virtual router MAC address for each IP address
1131 * associated with the virtual router.
1134 carp_send_arp(struct carp_softc *sc)
1137 struct in_addr addr;
1141 CARP_FOREACH_IFA(sc, ifa) {
1142 if (ifa->ifa_addr->sa_family != AF_INET)
1144 addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1145 arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr));
1150 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1152 struct carp_softc *sc = ifa->ifa_carp;
1154 if (sc->sc_state == MASTER) {
1155 *enaddr = LLADDR(&sc->sc_addr);
1165 carp_send_na(struct carp_softc *sc)
1167 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1169 struct in6_addr *in6;
1171 CARP_FOREACH_IFA(sc, ifa) {
1172 if (ifa->ifa_addr->sa_family != AF_INET6)
1176 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1177 ND_NA_FLAG_OVERRIDE, 1, NULL);
1178 DELAY(1000); /* XXX */
1183 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1184 * matches and is not a carp address. Returns NULL otherwise.
1187 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1194 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1195 if (ifa->ifa_addr->sa_family != AF_INET6)
1197 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1199 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1210 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1216 IFNET_FOREACH_IFA(ifp, ifa)
1217 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1218 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1219 struct carp_softc *sc = ifa->ifa_carp;
1222 mtag = m_tag_get(PACKET_TAG_CARP,
1223 sizeof(struct carp_softc *), M_NOWAIT);
1225 /* Better a bit than nothing. */
1226 return (LLADDR(&sc->sc_addr));
1228 bcopy(&sc, mtag + 1, sizeof(sc));
1229 m_tag_prepend(m, mtag);
1231 return (LLADDR(&sc->sc_addr));
1239 carp_forus(struct ifnet *ifp, u_char *dhost)
1241 struct carp_softc *sc;
1242 uint8_t *ena = dhost;
1244 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1247 CIF_LOCK(ifp->if_carp);
1248 IFNET_FOREACH_CARP(ifp, sc) {
1250 * CARP_LOCK() is not here, since would protect nothing, but
1251 * cause deadlock with if_bridge, calling this under its lock.
1253 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1255 CIF_UNLOCK(ifp->if_carp);
1259 CIF_UNLOCK(ifp->if_carp);
1264 /* Master down timeout event, executed in callout context. */
1266 carp_master_down(void *v)
1268 struct carp_softc *sc = v;
1269 struct epoch_tracker et;
1271 NET_EPOCH_ENTER(et);
1272 CARP_LOCK_ASSERT(sc);
1274 CURVNET_SET(sc->sc_carpdev->if_vnet);
1275 if (sc->sc_state == BACKUP) {
1276 carp_master_down_locked(sc, "master timed out");
1285 carp_master_down_locked(struct carp_softc *sc, const char *reason)
1289 CARP_LOCK_ASSERT(sc);
1291 switch (sc->sc_state) {
1293 carp_set_state(sc, MASTER, reason);
1294 carp_send_ad_locked(sc);
1307 panic("carp: VHID %u@%s: master_down event in %s state\n",
1309 sc->sc_carpdev->if_xname,
1310 sc->sc_state ? "MASTER" : "INIT");
1317 * When in backup state, af indicates whether to reset the master down timer
1318 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1321 carp_setrun(struct carp_softc *sc, sa_family_t af)
1325 CARP_LOCK_ASSERT(sc);
1327 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1328 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1329 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) ||
1333 switch (sc->sc_state) {
1335 carp_set_state(sc, BACKUP, "initialization complete");
1339 callout_stop(&sc->sc_ad_tmo);
1340 tv.tv_sec = 3 * sc->sc_advbase;
1341 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1345 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1346 carp_master_down, sc);
1351 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1352 carp_master_down, sc);
1358 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1359 carp_master_down, sc);
1363 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1364 carp_master_down, sc);
1370 tv.tv_sec = sc->sc_advbase;
1371 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1372 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1379 * Setup multicast structures.
1382 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1384 struct ifnet *ifp = cif->cif_ifp;
1391 struct ip_moptions *imo = &cif->cif_imo;
1392 struct in_mfilter *imf;
1393 struct in_addr addr;
1395 if (ip_mfilter_first(&imo->imo_head) != NULL)
1398 imf = ip_mfilter_alloc(M_WAITOK, 0, 0);
1399 ip_mfilter_init(&imo->imo_head);
1400 imo->imo_multicast_vif = -1;
1402 addr.s_addr = htonl(INADDR_CARP_GROUP);
1403 if ((error = in_joingroup(ifp, &addr, NULL,
1404 &imf->imf_inm)) != 0) {
1405 ip_mfilter_free(imf);
1409 ip_mfilter_insert(&imo->imo_head, imf);
1410 imo->imo_multicast_ifp = ifp;
1411 imo->imo_multicast_ttl = CARP_DFLTTL;
1412 imo->imo_multicast_loop = 0;
1419 struct ip6_moptions *im6o = &cif->cif_im6o;
1420 struct in6_mfilter *im6f[2];
1421 struct in6_addr in6;
1423 if (ip6_mfilter_first(&im6o->im6o_head))
1426 im6f[0] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
1427 im6f[1] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
1429 ip6_mfilter_init(&im6o->im6o_head);
1430 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1431 im6o->im6o_multicast_ifp = ifp;
1433 /* Join IPv6 CARP multicast group. */
1434 bzero(&in6, sizeof(in6));
1435 in6.s6_addr16[0] = htons(0xff02);
1436 in6.s6_addr8[15] = 0x12;
1437 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1438 ip6_mfilter_free(im6f[0]);
1439 ip6_mfilter_free(im6f[1]);
1442 if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[0]->im6f_in6m, 0)) != 0) {
1443 ip6_mfilter_free(im6f[0]);
1444 ip6_mfilter_free(im6f[1]);
1448 /* Join solicited multicast address. */
1449 bzero(&in6, sizeof(in6));
1450 in6.s6_addr16[0] = htons(0xff02);
1451 in6.s6_addr32[1] = 0;
1452 in6.s6_addr32[2] = htonl(1);
1453 in6.s6_addr32[3] = 0;
1454 in6.s6_addr8[12] = 0xff;
1456 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1457 ip6_mfilter_free(im6f[0]);
1458 ip6_mfilter_free(im6f[1]);
1462 if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[1]->im6f_in6m, 0)) != 0) {
1463 in6_leavegroup(im6f[0]->im6f_in6m, NULL);
1464 ip6_mfilter_free(im6f[0]);
1465 ip6_mfilter_free(im6f[1]);
1468 ip6_mfilter_insert(&im6o->im6o_head, im6f[0]);
1469 ip6_mfilter_insert(&im6o->im6o_head, im6f[1]);
1479 * Free multicast structures.
1482 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1485 struct ip_moptions *imo = &cif->cif_imo;
1486 struct in_mfilter *imf;
1489 struct ip6_moptions *im6o = &cif->cif_im6o;
1490 struct in6_mfilter *im6f;
1492 sx_assert(&carp_sx, SA_XLOCKED);
1497 if (cif->cif_naddrs != 0)
1500 while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
1501 ip_mfilter_remove(&imo->imo_head, imf);
1502 in_leavegroup(imf->imf_inm, NULL);
1503 ip_mfilter_free(imf);
1509 if (cif->cif_naddrs6 != 0)
1512 while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) {
1513 ip6_mfilter_remove(&im6o->im6o_head, im6f);
1514 in6_leavegroup(im6f->im6f_in6m, NULL);
1515 ip6_mfilter_free(im6f);
1523 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1526 struct carp_softc *sc;
1531 switch (sa->sa_family) {
1544 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1548 bcopy(mtag + 1, &sc, sizeof(sc));
1550 /* Set the source MAC address to the Virtual Router MAC Address. */
1551 switch (ifp->if_type) {
1555 struct ether_header *eh;
1557 eh = mtod(m, struct ether_header *);
1558 eh->ether_shost[0] = 0;
1559 eh->ether_shost[1] = 0;
1560 eh->ether_shost[2] = 0x5e;
1561 eh->ether_shost[3] = 0;
1562 eh->ether_shost[4] = 1;
1563 eh->ether_shost[5] = sc->sc_vhid;
1567 printf("%s: carp is not supported for the %d interface type\n",
1568 ifp->if_xname, ifp->if_type);
1569 return (EOPNOTSUPP);
1575 static struct carp_softc*
1576 carp_alloc(struct ifnet *ifp)
1578 struct carp_softc *sc;
1579 struct carp_if *cif;
1581 sx_assert(&carp_sx, SA_XLOCKED);
1583 if ((cif = ifp->if_carp) == NULL)
1584 cif = carp_alloc_if(ifp);
1586 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1588 sc->sc_advbase = CARP_DFLTINTV;
1589 sc->sc_vhid = -1; /* required setting */
1590 sc->sc_init_counter = 1;
1591 sc->sc_state = INIT;
1593 sc->sc_ifasiz = sizeof(struct ifaddr *);
1594 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1595 sc->sc_carpdev = ifp;
1599 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1602 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1604 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1607 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1610 mtx_lock(&carp_mtx);
1611 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1612 mtx_unlock(&carp_mtx);
1618 carp_grow_ifas(struct carp_softc *sc)
1620 struct ifaddr **new;
1622 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO);
1624 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1625 free(sc->sc_ifas, M_CARP);
1632 carp_destroy(struct carp_softc *sc)
1634 struct ifnet *ifp = sc->sc_carpdev;
1635 struct carp_if *cif = ifp->if_carp;
1637 sx_assert(&carp_sx, SA_XLOCKED);
1639 if (sc->sc_suppress)
1640 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1644 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1647 mtx_lock(&carp_mtx);
1648 LIST_REMOVE(sc, sc_next);
1649 mtx_unlock(&carp_mtx);
1651 callout_drain(&sc->sc_ad_tmo);
1653 callout_drain(&sc->sc_md_tmo);
1656 callout_drain(&sc->sc_md6_tmo);
1658 CARP_LOCK_DESTROY(sc);
1660 free(sc->sc_ifas, M_CARP);
1664 static struct carp_if*
1665 carp_alloc_if(struct ifnet *ifp)
1667 struct carp_if *cif;
1670 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1672 if ((error = ifpromisc(ifp, 1)) != 0)
1673 printf("%s: ifpromisc(%s) failed: %d\n",
1674 __func__, ifp->if_xname, error);
1676 cif->cif_flags |= CIF_PROMISC;
1680 TAILQ_INIT(&cif->cif_vrs);
1685 IF_ADDR_WUNLOCK(ifp);
1691 carp_free_if(struct carp_if *cif)
1693 struct ifnet *ifp = cif->cif_ifp;
1695 CIF_LOCK_ASSERT(cif);
1696 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1700 ifp->if_carp = NULL;
1701 IF_ADDR_WUNLOCK(ifp);
1703 CIF_LOCK_DESTROY(cif);
1705 if (cif->cif_flags & CIF_PROMISC)
1713 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1717 carpr->carpr_state = sc->sc_state;
1718 carpr->carpr_vhid = sc->sc_vhid;
1719 carpr->carpr_advbase = sc->sc_advbase;
1720 carpr->carpr_advskew = sc->sc_advskew;
1722 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1724 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1729 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1731 struct epoch_tracker et;
1732 struct carpreq carpr;
1734 struct carp_softc *sc = NULL;
1735 int error = 0, locked = 0;
1737 if ((error = copyin(ifr_data_get_ptr(ifr), &carpr, sizeof carpr)))
1740 ifp = ifunit_ref(ifr->ifr_name);
1744 switch (ifp->if_type) {
1754 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1755 error = EADDRNOTAVAIL;
1762 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1764 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1765 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1771 IFNET_FOREACH_CARP(ifp, sc)
1772 if (sc->sc_vhid == carpr.carpr_vhid)
1776 sc = carp_alloc(ifp);
1778 sc->sc_vhid = carpr.carpr_vhid;
1779 LLADDR(&sc->sc_addr)[0] = 0;
1780 LLADDR(&sc->sc_addr)[1] = 0;
1781 LLADDR(&sc->sc_addr)[2] = 0x5e;
1782 LLADDR(&sc->sc_addr)[3] = 0;
1783 LLADDR(&sc->sc_addr)[4] = 1;
1784 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1788 if (carpr.carpr_advbase > 0) {
1789 if (carpr.carpr_advbase > 255 ||
1790 carpr.carpr_advbase < CARP_DFLTINTV) {
1794 sc->sc_advbase = carpr.carpr_advbase;
1796 if (carpr.carpr_advskew >= 255) {
1800 sc->sc_advskew = carpr.carpr_advskew;
1801 if (carpr.carpr_key[0] != '\0') {
1802 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1803 carp_hmac_prepare(sc);
1805 if (sc->sc_state != INIT &&
1806 carpr.carpr_state != sc->sc_state) {
1807 switch (carpr.carpr_state) {
1809 callout_stop(&sc->sc_ad_tmo);
1810 carp_set_state(sc, BACKUP,
1811 "user requested via ifconfig");
1816 NET_EPOCH_ENTER(et);
1817 carp_master_down_locked(sc,
1818 "user requested via ifconfig");
1831 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1835 if (carpr.carpr_count < 1) {
1839 if (ifp->if_carp == NULL) {
1844 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1845 if (carpr.carpr_vhid != 0) {
1846 IFNET_FOREACH_CARP(ifp, sc)
1847 if (sc->sc_vhid == carpr.carpr_vhid)
1853 carp_carprcp(&carpr, sc, priveleged);
1854 error = copyout(&carpr, ifr_data_get_ptr(ifr),
1860 IFNET_FOREACH_CARP(ifp, sc)
1863 if (count > carpr.carpr_count) {
1864 CIF_UNLOCK(ifp->if_carp);
1870 IFNET_FOREACH_CARP(ifp, sc) {
1871 carp_carprcp(&carpr, sc, priveleged);
1872 carpr.carpr_count = count;
1873 error = copyout(&carpr,
1874 (char *)ifr_data_get_ptr(ifr) +
1875 (i * sizeof(carpr)), sizeof(carpr));
1877 CIF_UNLOCK(ifp->if_carp);
1888 sx_xunlock(&carp_sx);
1899 carp_get_vhid(struct ifaddr *ifa)
1902 if (ifa == NULL || ifa->ifa_carp == NULL)
1905 return (ifa->ifa_carp->sc_vhid);
1909 carp_attach(struct ifaddr *ifa, int vhid)
1911 struct ifnet *ifp = ifa->ifa_ifp;
1912 struct carp_if *cif = ifp->if_carp;
1913 struct carp_softc *sc;
1916 KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa));
1918 switch (ifa->ifa_addr->sa_family) {
1927 return (EPROTOTYPE);
1931 if (ifp->if_carp == NULL) {
1932 sx_xunlock(&carp_sx);
1933 return (ENOPROTOOPT);
1936 IFNET_FOREACH_CARP(ifp, sc)
1937 if (sc->sc_vhid == vhid)
1940 sx_xunlock(&carp_sx);
1944 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1947 sx_xunlock(&carp_sx);
1951 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1952 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1955 switch (ifa->ifa_addr->sa_family) {
1973 sc->sc_ifas[index - 1] = ifa;
1975 carp_hmac_prepare(sc);
1979 sx_xunlock(&carp_sx);
1985 carp_detach(struct ifaddr *ifa, bool keep_cif)
1987 struct ifnet *ifp = ifa->ifa_ifp;
1988 struct carp_if *cif = ifp->if_carp;
1989 struct carp_softc *sc = ifa->ifa_carp;
1992 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1998 index = sc->sc_naddrs + sc->sc_naddrs6;
1999 for (i = 0; i < index; i++)
2000 if (sc->sc_ifas[i] == ifa)
2002 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
2003 for (; i < index - 1; i++)
2004 sc->sc_ifas[i] = sc->sc_ifas[i+1];
2005 sc->sc_ifas[index - 1] = NULL;
2007 switch (ifa->ifa_addr->sa_family) {
2022 carp_ifa_delroute(ifa);
2023 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
2025 ifa->ifa_carp = NULL;
2028 carp_hmac_prepare(sc);
2031 if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0)
2039 sx_xunlock(&carp_sx);
2043 carp_set_state(struct carp_softc *sc, int state, const char *reason)
2046 CARP_LOCK_ASSERT(sc);
2048 if (sc->sc_state != state) {
2049 const char *carp_states[] = { CARP_STATES };
2050 char subsys[IFNAMSIZ+5];
2052 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
2053 sc->sc_carpdev->if_xname);
2055 CARP_LOG("%s: %s -> %s (%s)\n", subsys,
2056 carp_states[sc->sc_state], carp_states[state], reason);
2058 sc->sc_state = state;
2060 devctl_notify("CARP", subsys, carp_states[state], NULL);
2065 carp_linkstate(struct ifnet *ifp)
2067 struct carp_softc *sc;
2069 CIF_LOCK(ifp->if_carp);
2070 IFNET_FOREACH_CARP(ifp, sc) {
2075 CIF_UNLOCK(ifp->if_carp);
2079 carp_sc_state(struct carp_softc *sc)
2082 CARP_LOCK_ASSERT(sc);
2084 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2085 !(sc->sc_carpdev->if_flags & IFF_UP) ||
2087 callout_stop(&sc->sc_ad_tmo);
2089 callout_stop(&sc->sc_md_tmo);
2092 callout_stop(&sc->sc_md6_tmo);
2094 carp_set_state(sc, INIT, "hardware interface down");
2096 if (!sc->sc_suppress)
2097 carp_demote_adj(V_carp_ifdown_adj, "interface down");
2098 sc->sc_suppress = 1;
2100 carp_set_state(sc, INIT, "hardware interface up");
2102 if (sc->sc_suppress)
2103 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2104 sc->sc_suppress = 0;
2109 carp_demote_adj(int adj, char *reason)
2111 atomic_add_int(&V_carp_demotion, adj);
2112 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2113 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2117 carp_allow_sysctl(SYSCTL_HANDLER_ARGS)
2120 struct carp_softc *sc;
2123 error = sysctl_handle_int(oidp, &new, 0, req);
2124 if (error || !req->newptr)
2127 if (V_carp_allow != new) {
2130 mtx_lock(&carp_mtx);
2131 LIST_FOREACH(sc, &carp_list, sc_next) {
2133 if (curvnet == sc->sc_carpdev->if_vnet)
2137 mtx_unlock(&carp_mtx);
2144 carp_dscp_sysctl(SYSCTL_HANDLER_ARGS)
2149 error = sysctl_handle_int(oidp, &new, 0, req);
2150 if (error || !req->newptr)
2153 if (new < 0 || new > 63)
2162 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2166 new = V_carp_demotion;
2167 error = sysctl_handle_int(oidp, &new, 0, req);
2168 if (error || !req->newptr)
2171 carp_demote_adj(new, "sysctl");
2177 carp_mod_cleanup(void)
2181 (void)ipproto_unregister(IPPROTO_CARP);
2182 carp_iamatch_p = NULL;
2185 (void)ip6proto_unregister(IPPROTO_CARP);
2186 carp_iamatch6_p = NULL;
2187 carp_macmatch6_p = NULL;
2189 carp_ioctl_p = NULL;
2190 carp_attach_p = NULL;
2191 carp_detach_p = NULL;
2192 carp_get_vhid_p = NULL;
2193 carp_linkstate_p = NULL;
2194 carp_forus_p = NULL;
2195 carp_output_p = NULL;
2196 carp_demote_adj_p = NULL;
2197 carp_master_p = NULL;
2198 mtx_unlock(&carp_mtx);
2199 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2200 mtx_destroy(&carp_mtx);
2201 sx_destroy(&carp_sx);
2205 ipcarp_sysinit(void)
2208 /* Load allow as tunable so to postpone carp start after module load */
2209 TUNABLE_INT_FETCH("net.inet.carp.allow", &V_carp_allow);
2211 VNET_SYSINIT(ip_carp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipcarp_sysinit, NULL);
2218 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2219 sx_init(&carp_sx, "carp_sx");
2220 LIST_INIT(&carp_list);
2221 carp_get_vhid_p = carp_get_vhid;
2222 carp_forus_p = carp_forus;
2223 carp_output_p = carp_output;
2224 carp_linkstate_p = carp_linkstate;
2225 carp_ioctl_p = carp_ioctl;
2226 carp_attach_p = carp_attach;
2227 carp_detach_p = carp_detach;
2228 carp_demote_adj_p = carp_demote_adj;
2229 carp_master_p = carp_master;
2231 carp_iamatch6_p = carp_iamatch6;
2232 carp_macmatch6_p = carp_macmatch6;
2233 err = ip6proto_register(IPPROTO_CARP, carp6_input, NULL);
2235 printf("carp: error %d registering with INET6\n", err);
2241 carp_iamatch_p = carp_iamatch;
2242 err = ipproto_register(IPPROTO_CARP, carp_input, NULL);
2244 printf("carp: error %d registering with INET\n", err);
2253 carp_modevent(module_t mod, int type, void *data)
2257 return carp_mod_load();
2260 mtx_lock(&carp_mtx);
2261 if (LIST_EMPTY(&carp_list))
2264 mtx_unlock(&carp_mtx);
2276 static moduledata_t carp_mod = {
2282 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);