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_types.h>
62 #include <net/route.h>
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip_carp.h>
69 #include <netinet/ip.h>
70 #include <machine/in_cksum.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/if_ether.h>
78 #include <netinet/icmp6.h>
79 #include <netinet/ip6.h>
80 #include <netinet6/in6_var.h>
81 #include <netinet6/ip6_var.h>
82 #include <netinet6/scope6_var.h>
83 #include <netinet6/nd6.h>
86 #include <crypto/sha1.h>
88 static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
91 struct ifnet *sc_carpdev; /* Pointer to parent ifnet. */
92 struct ifaddr **sc_ifas; /* Our ifaddrs. */
93 struct sockaddr_dl sc_addr; /* Our link level address. */
94 struct callout sc_ad_tmo; /* Advertising timeout. */
96 struct callout sc_md_tmo; /* Master down timeout. */
99 struct callout sc_md6_tmo; /* XXX: Master down timeout. */
110 enum { INIT = 0, BACKUP, MASTER } sc_state;
112 int sc_sendad_errors;
113 #define CARP_SENDAD_MAX_ERRORS 3
114 int sc_sendad_success;
115 #define CARP_SENDAD_MIN_SUCCESS 3
121 #define CARP_HMAC_PAD 64
122 unsigned char sc_key[CARP_KEY_LEN];
123 unsigned char sc_pad[CARP_HMAC_PAD];
126 TAILQ_ENTRY(carp_softc) sc_list; /* On the carp_if list. */
127 LIST_ENTRY(carp_softc) sc_next; /* On the global list. */
137 TAILQ_HEAD(, carp_softc) cif_vrs;
139 struct ip_moptions cif_imo;
142 struct ip6_moptions cif_im6o;
144 struct ifnet *cif_ifp;
147 #define CIF_PROMISC 0x00000001
151 * Brief design of carp(4).
153 * Any carp-capable ifnet may have a list of carp softcs hanging off
154 * its ifp->if_carp pointer. Each softc represents one unique virtual
155 * host id, or vhid. The softc has a back pointer to the ifnet. All
156 * softcs are joined in a global list, which has quite limited use.
158 * Any interface address that takes part in CARP negotiation has a
159 * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
160 * AF_INET or AF_INET6 address.
162 * Although, one can get the softc's backpointer to ifnet and traverse
163 * through its ifp->if_addrhead queue to find all interface addresses
164 * involved in CARP, we keep a growable array of ifaddr pointers. This
165 * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
166 * do calls into the network stack, thus avoiding LORs.
170 * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
171 * callout-driven events and ioctl()s.
173 * To traverse the list of softcs on an ifnet we use CIF_LOCK() or carp_sx.
174 * To traverse the global list we use the mutex carp_mtx.
176 * Known issues with locking:
178 * - Sending ad, we put the pointer to the softc in an mtag, and no reference
179 * counting is done on the softc.
180 * - On module unload we may race (?) with packet processing thread
181 * dereferencing our function pointers.
184 /* Accept incoming CARP packets. */
185 VNET_DEFINE_STATIC(int, carp_allow) = 1;
186 #define V_carp_allow VNET(carp_allow)
188 /* Set DSCP in outgoing CARP packets. */
189 VNET_DEFINE_STATIC(int, carp_dscp) = 56;
190 #define V_carp_dscp VNET(carp_dscp)
192 /* Preempt slower nodes. */
193 VNET_DEFINE_STATIC(int, carp_preempt) = 0;
194 #define V_carp_preempt VNET(carp_preempt)
197 VNET_DEFINE_STATIC(int, carp_log) = 1;
198 #define V_carp_log VNET(carp_log)
200 /* Global advskew demotion. */
201 VNET_DEFINE_STATIC(int, carp_demotion) = 0;
202 #define V_carp_demotion VNET(carp_demotion)
204 /* Send error demotion factor. */
205 VNET_DEFINE_STATIC(int, carp_senderr_adj) = CARP_MAXSKEW;
206 #define V_carp_senderr_adj VNET(carp_senderr_adj)
208 /* Iface down demotion factor. */
209 VNET_DEFINE_STATIC(int, carp_ifdown_adj) = CARP_MAXSKEW;
210 #define V_carp_ifdown_adj VNET(carp_ifdown_adj)
212 static int carp_allow_sysctl(SYSCTL_HANDLER_ARGS);
213 static int carp_dscp_sysctl(SYSCTL_HANDLER_ARGS);
214 static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
216 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
218 SYSCTL_PROC(_net_inet_carp, OID_AUTO, allow,
219 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
220 0, 0, carp_allow_sysctl, "I",
221 "Accept incoming CARP packets");
222 SYSCTL_PROC(_net_inet_carp, OID_AUTO, dscp,
223 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
224 0, 0, carp_dscp_sysctl, "I",
225 "DSCP value for carp packets");
226 SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
227 &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
228 SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
229 &VNET_NAME(carp_log), 0, "CARP log level");
230 SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
231 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
232 0, 0, carp_demote_adj_sysctl, "I",
233 "Adjust demotion factor (skew of advskew)");
234 SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
235 CTLFLAG_VNET | CTLFLAG_RW,
236 &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
237 SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
238 CTLFLAG_VNET | CTLFLAG_RW,
239 &VNET_NAME(carp_ifdown_adj), 0,
240 "Interface down demotion factor adjustment");
242 VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
243 VNET_PCPUSTAT_SYSINIT(carpstats);
244 VNET_PCPUSTAT_SYSUNINIT(carpstats);
246 #define CARPSTATS_ADD(name, val) \
247 counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
248 sizeof(uint64_t)], (val))
249 #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1)
251 SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
252 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
254 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
256 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
257 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
258 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
259 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
260 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
262 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
263 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
264 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
265 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
266 #define CIF_FREE(cif) do { \
268 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
274 #define CARP_LOG(...) do { \
275 if (V_carp_log > 0) \
276 log(LOG_INFO, "carp: " __VA_ARGS__); \
279 #define CARP_DEBUG(...) do { \
280 if (V_carp_log > 1) \
281 log(LOG_DEBUG, __VA_ARGS__); \
284 #define IFNET_FOREACH_IFA(ifp, ifa) \
285 CK_STAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
286 if ((ifa)->ifa_carp != NULL)
288 #define CARP_FOREACH_IFA(sc, ifa) \
289 CARP_LOCK_ASSERT(sc); \
291 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
292 ((ifa) = sc->sc_ifas[_i]) != NULL; \
295 #define IFNET_FOREACH_CARP(ifp, sc) \
296 KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) || \
297 sx_xlocked(&carp_sx), ("cif_vrs not locked")); \
298 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
300 #define DEMOTE_ADVSKEW(sc) \
301 (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \
303 (((sc)->sc_advskew + V_carp_demotion < 0) ? \
304 0 : ((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 */
569 if (m->m_len < *offp + sizeof(*ch)) {
571 m = m_pullup(m, *offp + sizeof(*ch));
573 CARPSTATS_INC(carps_badlen);
574 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
575 return (IPPROTO_DONE);
578 ch = (struct carp_header *)(mtod(m, char *) + *offp);
580 /* verify the CARP checksum */
582 if (in_cksum(m, sizeof(*ch))) {
583 CARPSTATS_INC(carps_badsum);
584 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
585 m->m_pkthdr.rcvif->if_xname);
587 return (IPPROTO_DONE);
591 carp_input_c(m, ch, AF_INET6);
592 return (IPPROTO_DONE);
597 * This routine should not be necessary at all, but some switches
598 * (VMWare ESX vswitches) can echo our own packets back at us,
599 * and we must ignore them or they will cause us to drop out of
602 * We cannot catch all cases of network loops. Instead, what we
603 * do here is catch any packet that arrives with a carp header
604 * with a VHID of 0, that comes from an address that is our own.
605 * These packets are by definition "from us" (even if they are from
606 * a misconfigured host that is pretending to be us).
608 * The VHID test is outside this mini-function.
611 carp_source_is_self(struct mbuf *m, struct ifaddr *ifa, sa_family_t af)
625 ip4 = mtod(m, struct ip *);
626 in4 = ifatoia(ifa)->ia_addr.sin_addr;
627 return (in4.s_addr == ip4->ip_src.s_addr);
631 ip6 = mtod(m, struct ip6_hdr *);
632 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
633 return (memcmp(&in6, &ip6->ip6_src, sizeof(in6)) == 0);
642 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
644 struct ifnet *ifp = m->m_pkthdr.rcvif;
645 struct ifaddr *ifa, *match;
646 struct carp_softc *sc;
647 uint64_t tmp_counter;
648 struct timeval sc_tv, ch_tv;
654 * Verify that the VHID is valid on the receiving interface.
656 * There should be just one match. If there are none
657 * the VHID is not valid and we drop the packet. If
658 * there are multiple VHID matches, take just the first
659 * one, for compatibility with previous code. While we're
660 * scanning, check for obvious loops in the network topology
661 * (these should never happen, and as noted above, we may
662 * miss real loops; this is just a double-check).
666 IFNET_FOREACH_IFA(ifp, ifa) {
667 if (match == NULL && ifa->ifa_carp != NULL &&
668 ifa->ifa_addr->sa_family == af &&
669 ifa->ifa_carp->sc_vhid == ch->carp_vhid)
671 if (ch->carp_vhid == 0 && carp_source_is_self(m, ifa, af))
674 ifa = error ? NULL : match;
679 if (error == ELOOP) {
680 CARP_DEBUG("dropping looped packet on interface %s\n",
682 CARPSTATS_INC(carps_badif); /* ??? */
684 CARPSTATS_INC(carps_badvhid);
690 /* verify the CARP version. */
691 if (ch->carp_version != CARP_VERSION) {
692 CARPSTATS_INC(carps_badver);
693 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
704 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
705 CARPSTATS_INC(carps_badauth);
706 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
707 sc->sc_vhid, ifp->if_xname);
711 tmp_counter = ntohl(ch->carp_counter[0]);
712 tmp_counter = tmp_counter<<32;
713 tmp_counter += ntohl(ch->carp_counter[1]);
715 /* XXX Replay protection goes here */
717 sc->sc_init_counter = 0;
718 sc->sc_counter = tmp_counter;
720 sc_tv.tv_sec = sc->sc_advbase;
721 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
722 ch_tv.tv_sec = ch->carp_advbase;
723 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
725 switch (sc->sc_state) {
730 * If we receive an advertisement from a master who's going to
731 * be more frequent than us, go into BACKUP state.
733 if (timevalcmp(&sc_tv, &ch_tv, >) ||
734 timevalcmp(&sc_tv, &ch_tv, ==)) {
735 callout_stop(&sc->sc_ad_tmo);
736 carp_set_state(sc, BACKUP,
737 "more frequent advertisement received");
744 * If we're pre-empting masters who advertise slower than us,
745 * and this one claims to be slower, treat him as down.
747 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
748 carp_master_down_locked(sc,
749 "preempting a slower master");
754 * If the master is going to advertise at such a low frequency
755 * that he's guaranteed to time out, we'd might as well just
756 * treat him as timed out now.
758 sc_tv.tv_sec = sc->sc_advbase * 3;
759 if (timevalcmp(&sc_tv, &ch_tv, <)) {
760 carp_master_down_locked(sc, "master will time out");
765 * Otherwise, we reset the counter and wait for the next
778 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
782 if (sc->sc_init_counter) {
783 /* this could also be seconds since unix epoch */
784 sc->sc_counter = arc4random();
785 sc->sc_counter = sc->sc_counter << 32;
786 sc->sc_counter += arc4random();
790 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
791 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
793 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
795 /* Tag packet for carp_output */
796 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
797 M_NOWAIT)) == NULL) {
799 CARPSTATS_INC(carps_onomem);
802 bcopy(&sc, mtag + 1, sizeof(sc));
803 m_tag_prepend(m, mtag);
809 * To avoid LORs and possible recursions this function shouldn't
810 * be called directly, but scheduled via taskqueue.
813 carp_send_ad_all(void *ctx __unused, int pending __unused)
815 struct carp_softc *sc;
816 struct epoch_tracker et;
820 LIST_FOREACH(sc, &carp_list, sc_next)
821 if (sc->sc_state == MASTER) {
823 CURVNET_SET(sc->sc_carpdev->if_vnet);
824 carp_send_ad_locked(sc);
828 mtx_unlock(&carp_mtx);
832 /* Send a periodic advertisement, executed in callout context. */
834 carp_send_ad(void *v)
836 struct carp_softc *sc = v;
837 struct epoch_tracker et;
840 CARP_LOCK_ASSERT(sc);
841 CURVNET_SET(sc->sc_carpdev->if_vnet);
842 carp_send_ad_locked(sc);
849 carp_send_ad_error(struct carp_softc *sc, int error)
853 * We track errors and successfull sends with this logic:
854 * - Any error resets success counter to 0.
855 * - MAX_ERRORS triggers demotion.
856 * - MIN_SUCCESS successes resets error counter to 0.
857 * - MIN_SUCCESS reverts demotion, if it was triggered before.
860 if (sc->sc_sendad_errors < INT_MAX)
861 sc->sc_sendad_errors++;
862 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
863 static const char fmt[] = "send error %d on %s";
864 char msg[sizeof(fmt) + IFNAMSIZ];
866 sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
867 carp_demote_adj(V_carp_senderr_adj, msg);
869 sc->sc_sendad_success = 0;
870 } else if (sc->sc_sendad_errors > 0) {
871 if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
872 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
873 static const char fmt[] = "send ok on %s";
874 char msg[sizeof(fmt) + IFNAMSIZ];
876 sprintf(msg, fmt, sc->sc_carpdev->if_xname);
877 carp_demote_adj(-V_carp_senderr_adj, msg);
879 sc->sc_sendad_errors = 0;
885 * Pick the best ifaddr on the given ifp for sending CARP
888 * "Best" here is defined by ifa_preferred(). This function is much
889 * much like ifaof_ifpforaddr() except that we just use ifa_preferred().
891 * (This could be simplified to return the actual address, except that
892 * it has a different format in AF_INET and AF_INET6.)
894 static struct ifaddr *
895 carp_best_ifa(int af, struct ifnet *ifp)
897 struct ifaddr *ifa, *best;
904 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
905 if (ifa->ifa_addr->sa_family == af &&
906 (best == NULL || ifa_preferred(best, ifa)))
915 carp_send_ad_locked(struct carp_softc *sc)
917 struct carp_header ch;
920 struct carp_header *ch_ptr;
925 CARP_LOCK_ASSERT(sc);
927 advskew = DEMOTE_ADVSKEW(sc);
928 tv.tv_sec = sc->sc_advbase;
929 tv.tv_usec = advskew * 1000000 / 256;
931 ch.carp_version = CARP_VERSION;
932 ch.carp_type = CARP_ADVERTISEMENT;
933 ch.carp_vhid = sc->sc_vhid;
934 ch.carp_advbase = sc->sc_advbase;
935 ch.carp_advskew = advskew;
936 ch.carp_authlen = 7; /* XXX DEFINE */
937 ch.carp_pad1 = 0; /* must be zero */
940 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
946 m = m_gethdr(M_NOWAIT, MT_DATA);
948 CARPSTATS_INC(carps_onomem);
951 len = sizeof(*ip) + sizeof(ch);
952 m->m_pkthdr.len = len;
953 m->m_pkthdr.rcvif = NULL;
955 M_ALIGN(m, m->m_len);
956 m->m_flags |= M_MCAST;
957 ip = mtod(m, struct ip *);
958 ip->ip_v = IPVERSION;
959 ip->ip_hl = sizeof(*ip) >> 2;
960 ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET;
961 ip->ip_len = htons(len);
962 ip->ip_off = htons(IP_DF);
963 ip->ip_ttl = CARP_DFLTTL;
964 ip->ip_p = IPPROTO_CARP;
968 ifa = carp_best_ifa(AF_INET, sc->sc_carpdev);
971 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
974 ip->ip_src.s_addr = 0;
975 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
977 ch_ptr = (struct carp_header *)(&ip[1]);
978 bcopy(&ch, ch_ptr, sizeof(ch));
979 if (carp_prepare_ad(m, sc, ch_ptr))
982 m->m_data += sizeof(*ip);
983 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
984 m->m_data -= sizeof(*ip);
986 CARPSTATS_INC(carps_opackets);
988 carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
989 &sc->sc_carpdev->if_carp->cif_imo, NULL));
993 if (sc->sc_naddrs6) {
996 m = m_gethdr(M_NOWAIT, MT_DATA);
998 CARPSTATS_INC(carps_onomem);
1001 len = sizeof(*ip6) + sizeof(ch);
1002 m->m_pkthdr.len = len;
1003 m->m_pkthdr.rcvif = NULL;
1005 M_ALIGN(m, m->m_len);
1006 m->m_flags |= M_MCAST;
1007 ip6 = mtod(m, struct ip6_hdr *);
1008 bzero(ip6, sizeof(*ip6));
1009 ip6->ip6_vfc |= IPV6_VERSION;
1010 /* Traffic class isn't defined in ip6 struct instead
1011 * it gets offset into flowid field */
1012 ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN +
1013 IPTOS_DSCP_OFFSET));
1014 ip6->ip6_hlim = CARP_DFLTTL;
1015 ip6->ip6_nxt = IPPROTO_CARP;
1017 /* set the source address */
1018 ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev);
1020 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
1021 sizeof(struct in6_addr));
1024 /* This should never happen with IPv6. */
1025 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
1027 /* Set the multicast destination. */
1028 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1029 ip6->ip6_dst.s6_addr8[15] = 0x12;
1030 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1032 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
1036 ch_ptr = (struct carp_header *)(&ip6[1]);
1037 bcopy(&ch, ch_ptr, sizeof(ch));
1038 if (carp_prepare_ad(m, sc, ch_ptr))
1041 m->m_data += sizeof(*ip6);
1042 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
1043 m->m_data -= sizeof(*ip6);
1045 CARPSTATS_INC(carps_opackets6);
1047 carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
1048 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
1053 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
1057 carp_addroute(struct carp_softc *sc)
1061 CARP_FOREACH_IFA(sc, ifa)
1062 carp_ifa_addroute(ifa);
1066 carp_ifa_addroute(struct ifaddr *ifa)
1069 switch (ifa->ifa_addr->sa_family) {
1072 in_addprefix(ifatoia(ifa));
1073 ifa_add_loopback_route(ifa,
1074 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
1079 ifa_add_loopback_route(ifa,
1080 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1081 nd6_add_ifa_lle(ifatoia6(ifa));
1088 carp_delroute(struct carp_softc *sc)
1092 CARP_FOREACH_IFA(sc, ifa)
1093 carp_ifa_delroute(ifa);
1097 carp_ifa_delroute(struct ifaddr *ifa)
1100 switch (ifa->ifa_addr->sa_family) {
1103 ifa_del_loopback_route(ifa,
1104 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
1105 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
1110 ifa_del_loopback_route(ifa,
1111 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1112 nd6_rem_ifa_lle(ifatoia6(ifa), 1);
1119 carp_master(struct ifaddr *ifa)
1121 struct carp_softc *sc = ifa->ifa_carp;
1123 return (sc->sc_state == MASTER);
1128 * Broadcast a gratuitous ARP request containing
1129 * the virtual router MAC address for each IP address
1130 * associated with the virtual router.
1133 carp_send_arp(struct carp_softc *sc)
1136 struct in_addr addr;
1140 CARP_FOREACH_IFA(sc, ifa) {
1141 if (ifa->ifa_addr->sa_family != AF_INET)
1143 addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1144 arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr));
1149 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1151 struct carp_softc *sc = ifa->ifa_carp;
1153 if (sc->sc_state == MASTER) {
1154 *enaddr = LLADDR(&sc->sc_addr);
1164 carp_send_na(struct carp_softc *sc)
1166 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1168 struct in6_addr *in6;
1170 CARP_FOREACH_IFA(sc, ifa) {
1171 if (ifa->ifa_addr->sa_family != AF_INET6)
1175 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1176 ND_NA_FLAG_OVERRIDE, 1, NULL);
1177 DELAY(1000); /* XXX */
1182 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1183 * matches and is not a carp address. Returns NULL otherwise.
1186 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1193 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1194 if (ifa->ifa_addr->sa_family != AF_INET6)
1196 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1198 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1209 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1215 IFNET_FOREACH_IFA(ifp, ifa)
1216 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1217 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1218 struct carp_softc *sc = ifa->ifa_carp;
1221 mtag = m_tag_get(PACKET_TAG_CARP,
1222 sizeof(struct carp_softc *), M_NOWAIT);
1224 /* Better a bit than nothing. */
1225 return (LLADDR(&sc->sc_addr));
1227 bcopy(&sc, mtag + 1, sizeof(sc));
1228 m_tag_prepend(m, mtag);
1230 return (LLADDR(&sc->sc_addr));
1238 carp_forus(struct ifnet *ifp, u_char *dhost)
1240 struct carp_softc *sc;
1241 uint8_t *ena = dhost;
1243 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1246 CIF_LOCK(ifp->if_carp);
1247 IFNET_FOREACH_CARP(ifp, sc) {
1249 * CARP_LOCK() is not here, since would protect nothing, but
1250 * cause deadlock with if_bridge, calling this under its lock.
1252 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1254 CIF_UNLOCK(ifp->if_carp);
1258 CIF_UNLOCK(ifp->if_carp);
1263 /* Master down timeout event, executed in callout context. */
1265 carp_master_down(void *v)
1267 struct carp_softc *sc = v;
1268 struct epoch_tracker et;
1270 NET_EPOCH_ENTER(et);
1271 CARP_LOCK_ASSERT(sc);
1273 CURVNET_SET(sc->sc_carpdev->if_vnet);
1274 if (sc->sc_state == BACKUP) {
1275 carp_master_down_locked(sc, "master timed out");
1284 carp_master_down_locked(struct carp_softc *sc, const char *reason)
1288 CARP_LOCK_ASSERT(sc);
1290 switch (sc->sc_state) {
1292 carp_set_state(sc, MASTER, reason);
1293 carp_send_ad_locked(sc);
1306 panic("carp: VHID %u@%s: master_down event in %s state\n",
1308 sc->sc_carpdev->if_xname,
1309 sc->sc_state ? "MASTER" : "INIT");
1316 * When in backup state, af indicates whether to reset the master down timer
1317 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1320 carp_setrun(struct carp_softc *sc, sa_family_t af)
1324 CARP_LOCK_ASSERT(sc);
1326 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1327 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1328 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) ||
1332 switch (sc->sc_state) {
1334 carp_set_state(sc, BACKUP, "initialization complete");
1338 callout_stop(&sc->sc_ad_tmo);
1339 tv.tv_sec = 3 * sc->sc_advbase;
1340 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1344 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1345 carp_master_down, sc);
1350 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1351 carp_master_down, sc);
1357 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1358 carp_master_down, sc);
1362 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1363 carp_master_down, sc);
1369 tv.tv_sec = sc->sc_advbase;
1370 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1371 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1378 * Setup multicast structures.
1381 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1383 struct ifnet *ifp = cif->cif_ifp;
1390 struct ip_moptions *imo = &cif->cif_imo;
1391 struct in_mfilter *imf;
1392 struct in_addr addr;
1394 if (ip_mfilter_first(&imo->imo_head) != NULL)
1397 imf = ip_mfilter_alloc(M_WAITOK, 0, 0);
1398 ip_mfilter_init(&imo->imo_head);
1399 imo->imo_multicast_vif = -1;
1401 addr.s_addr = htonl(INADDR_CARP_GROUP);
1402 if ((error = in_joingroup(ifp, &addr, NULL,
1403 &imf->imf_inm)) != 0) {
1404 ip_mfilter_free(imf);
1408 ip_mfilter_insert(&imo->imo_head, imf);
1409 imo->imo_multicast_ifp = ifp;
1410 imo->imo_multicast_ttl = CARP_DFLTTL;
1411 imo->imo_multicast_loop = 0;
1418 struct ip6_moptions *im6o = &cif->cif_im6o;
1419 struct in6_mfilter *im6f[2];
1420 struct in6_addr in6;
1422 if (ip6_mfilter_first(&im6o->im6o_head))
1425 im6f[0] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
1426 im6f[1] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
1428 ip6_mfilter_init(&im6o->im6o_head);
1429 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1430 im6o->im6o_multicast_ifp = ifp;
1432 /* Join IPv6 CARP multicast group. */
1433 bzero(&in6, sizeof(in6));
1434 in6.s6_addr16[0] = htons(0xff02);
1435 in6.s6_addr8[15] = 0x12;
1436 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1437 ip6_mfilter_free(im6f[0]);
1438 ip6_mfilter_free(im6f[1]);
1441 if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[0]->im6f_in6m, 0)) != 0) {
1442 ip6_mfilter_free(im6f[0]);
1443 ip6_mfilter_free(im6f[1]);
1447 /* Join solicited multicast address. */
1448 bzero(&in6, sizeof(in6));
1449 in6.s6_addr16[0] = htons(0xff02);
1450 in6.s6_addr32[1] = 0;
1451 in6.s6_addr32[2] = htonl(1);
1452 in6.s6_addr32[3] = 0;
1453 in6.s6_addr8[12] = 0xff;
1455 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1456 ip6_mfilter_free(im6f[0]);
1457 ip6_mfilter_free(im6f[1]);
1461 if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[1]->im6f_in6m, 0)) != 0) {
1462 in6_leavegroup(im6f[0]->im6f_in6m, NULL);
1463 ip6_mfilter_free(im6f[0]);
1464 ip6_mfilter_free(im6f[1]);
1467 ip6_mfilter_insert(&im6o->im6o_head, im6f[0]);
1468 ip6_mfilter_insert(&im6o->im6o_head, im6f[1]);
1478 * Free multicast structures.
1481 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1484 struct ip_moptions *imo = &cif->cif_imo;
1485 struct in_mfilter *imf;
1488 struct ip6_moptions *im6o = &cif->cif_im6o;
1489 struct in6_mfilter *im6f;
1491 sx_assert(&carp_sx, SA_XLOCKED);
1496 if (cif->cif_naddrs != 0)
1499 while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
1500 ip_mfilter_remove(&imo->imo_head, imf);
1501 in_leavegroup(imf->imf_inm, NULL);
1502 ip_mfilter_free(imf);
1508 if (cif->cif_naddrs6 != 0)
1511 while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) {
1512 ip6_mfilter_remove(&im6o->im6o_head, im6f);
1513 in6_leavegroup(im6f->im6f_in6m, NULL);
1514 ip6_mfilter_free(im6f);
1522 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1525 struct carp_softc *sc;
1530 switch (sa->sa_family) {
1543 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1547 bcopy(mtag + 1, &sc, sizeof(sc));
1549 /* Set the source MAC address to the Virtual Router MAC Address. */
1550 switch (ifp->if_type) {
1554 struct ether_header *eh;
1556 eh = mtod(m, struct ether_header *);
1557 eh->ether_shost[0] = 0;
1558 eh->ether_shost[1] = 0;
1559 eh->ether_shost[2] = 0x5e;
1560 eh->ether_shost[3] = 0;
1561 eh->ether_shost[4] = 1;
1562 eh->ether_shost[5] = sc->sc_vhid;
1566 printf("%s: carp is not supported for the %d interface type\n",
1567 ifp->if_xname, ifp->if_type);
1568 return (EOPNOTSUPP);
1574 static struct carp_softc*
1575 carp_alloc(struct ifnet *ifp)
1577 struct carp_softc *sc;
1578 struct carp_if *cif;
1580 sx_assert(&carp_sx, SA_XLOCKED);
1582 if ((cif = ifp->if_carp) == NULL)
1583 cif = carp_alloc_if(ifp);
1585 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1587 sc->sc_advbase = CARP_DFLTINTV;
1588 sc->sc_vhid = -1; /* required setting */
1589 sc->sc_init_counter = 1;
1590 sc->sc_state = INIT;
1592 sc->sc_ifasiz = sizeof(struct ifaddr *);
1593 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1594 sc->sc_carpdev = ifp;
1598 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1601 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1603 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1606 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1609 mtx_lock(&carp_mtx);
1610 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1611 mtx_unlock(&carp_mtx);
1617 carp_grow_ifas(struct carp_softc *sc)
1619 struct ifaddr **new;
1621 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO);
1623 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1624 free(sc->sc_ifas, M_CARP);
1631 carp_destroy(struct carp_softc *sc)
1633 struct ifnet *ifp = sc->sc_carpdev;
1634 struct carp_if *cif = ifp->if_carp;
1636 sx_assert(&carp_sx, SA_XLOCKED);
1638 if (sc->sc_suppress)
1639 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1643 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1646 mtx_lock(&carp_mtx);
1647 LIST_REMOVE(sc, sc_next);
1648 mtx_unlock(&carp_mtx);
1650 callout_drain(&sc->sc_ad_tmo);
1652 callout_drain(&sc->sc_md_tmo);
1655 callout_drain(&sc->sc_md6_tmo);
1657 CARP_LOCK_DESTROY(sc);
1659 free(sc->sc_ifas, M_CARP);
1663 static struct carp_if*
1664 carp_alloc_if(struct ifnet *ifp)
1666 struct carp_if *cif;
1669 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1671 if ((error = ifpromisc(ifp, 1)) != 0)
1672 printf("%s: ifpromisc(%s) failed: %d\n",
1673 __func__, ifp->if_xname, error);
1675 cif->cif_flags |= CIF_PROMISC;
1679 TAILQ_INIT(&cif->cif_vrs);
1684 IF_ADDR_WUNLOCK(ifp);
1690 carp_free_if(struct carp_if *cif)
1692 struct ifnet *ifp = cif->cif_ifp;
1694 CIF_LOCK_ASSERT(cif);
1695 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1699 ifp->if_carp = NULL;
1700 IF_ADDR_WUNLOCK(ifp);
1702 CIF_LOCK_DESTROY(cif);
1704 if (cif->cif_flags & CIF_PROMISC)
1712 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1716 carpr->carpr_state = sc->sc_state;
1717 carpr->carpr_vhid = sc->sc_vhid;
1718 carpr->carpr_advbase = sc->sc_advbase;
1719 carpr->carpr_advskew = sc->sc_advskew;
1721 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1723 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1728 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1730 struct carpreq carpr;
1732 struct carp_softc *sc = NULL;
1733 int error = 0, locked = 0;
1735 if ((error = copyin(ifr_data_get_ptr(ifr), &carpr, sizeof carpr)))
1738 ifp = ifunit_ref(ifr->ifr_name);
1742 switch (ifp->if_type) {
1752 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1753 error = EADDRNOTAVAIL;
1760 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1762 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1763 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1769 IFNET_FOREACH_CARP(ifp, sc)
1770 if (sc->sc_vhid == carpr.carpr_vhid)
1774 sc = carp_alloc(ifp);
1776 sc->sc_vhid = carpr.carpr_vhid;
1777 LLADDR(&sc->sc_addr)[0] = 0;
1778 LLADDR(&sc->sc_addr)[1] = 0;
1779 LLADDR(&sc->sc_addr)[2] = 0x5e;
1780 LLADDR(&sc->sc_addr)[3] = 0;
1781 LLADDR(&sc->sc_addr)[4] = 1;
1782 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1786 if (carpr.carpr_advbase > 0) {
1787 if (carpr.carpr_advbase > 255 ||
1788 carpr.carpr_advbase < CARP_DFLTINTV) {
1792 sc->sc_advbase = carpr.carpr_advbase;
1794 if (carpr.carpr_advskew >= 255) {
1798 sc->sc_advskew = carpr.carpr_advskew;
1799 if (carpr.carpr_key[0] != '\0') {
1800 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1801 carp_hmac_prepare(sc);
1803 if (sc->sc_state != INIT &&
1804 carpr.carpr_state != sc->sc_state) {
1805 switch (carpr.carpr_state) {
1807 callout_stop(&sc->sc_ad_tmo);
1808 carp_set_state(sc, BACKUP,
1809 "user requested via ifconfig");
1814 carp_master_down_locked(sc,
1815 "user requested via ifconfig");
1827 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1831 if (carpr.carpr_count < 1) {
1835 if (ifp->if_carp == NULL) {
1840 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1841 if (carpr.carpr_vhid != 0) {
1842 IFNET_FOREACH_CARP(ifp, sc)
1843 if (sc->sc_vhid == carpr.carpr_vhid)
1849 carp_carprcp(&carpr, sc, priveleged);
1850 error = copyout(&carpr, ifr_data_get_ptr(ifr),
1856 IFNET_FOREACH_CARP(ifp, sc)
1859 if (count > carpr.carpr_count) {
1860 CIF_UNLOCK(ifp->if_carp);
1866 IFNET_FOREACH_CARP(ifp, sc) {
1867 carp_carprcp(&carpr, sc, priveleged);
1868 carpr.carpr_count = count;
1869 error = copyout(&carpr,
1870 (char *)ifr_data_get_ptr(ifr) +
1871 (i * sizeof(carpr)), sizeof(carpr));
1873 CIF_UNLOCK(ifp->if_carp);
1884 sx_xunlock(&carp_sx);
1895 carp_get_vhid(struct ifaddr *ifa)
1898 if (ifa == NULL || ifa->ifa_carp == NULL)
1901 return (ifa->ifa_carp->sc_vhid);
1905 carp_attach(struct ifaddr *ifa, int vhid)
1907 struct ifnet *ifp = ifa->ifa_ifp;
1908 struct carp_if *cif = ifp->if_carp;
1909 struct carp_softc *sc;
1912 KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa));
1914 switch (ifa->ifa_addr->sa_family) {
1923 return (EPROTOTYPE);
1927 if (ifp->if_carp == NULL) {
1928 sx_xunlock(&carp_sx);
1929 return (ENOPROTOOPT);
1932 IFNET_FOREACH_CARP(ifp, sc)
1933 if (sc->sc_vhid == vhid)
1936 sx_xunlock(&carp_sx);
1940 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1943 sx_xunlock(&carp_sx);
1947 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1948 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1951 switch (ifa->ifa_addr->sa_family) {
1969 sc->sc_ifas[index - 1] = ifa;
1971 carp_hmac_prepare(sc);
1975 sx_xunlock(&carp_sx);
1981 carp_detach(struct ifaddr *ifa, bool keep_cif)
1983 struct ifnet *ifp = ifa->ifa_ifp;
1984 struct carp_if *cif = ifp->if_carp;
1985 struct carp_softc *sc = ifa->ifa_carp;
1988 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1994 index = sc->sc_naddrs + sc->sc_naddrs6;
1995 for (i = 0; i < index; i++)
1996 if (sc->sc_ifas[i] == ifa)
1998 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1999 for (; i < index - 1; i++)
2000 sc->sc_ifas[i] = sc->sc_ifas[i+1];
2001 sc->sc_ifas[index - 1] = NULL;
2003 switch (ifa->ifa_addr->sa_family) {
2018 carp_ifa_delroute(ifa);
2019 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
2021 ifa->ifa_carp = NULL;
2024 carp_hmac_prepare(sc);
2027 if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0)
2035 sx_xunlock(&carp_sx);
2039 carp_set_state(struct carp_softc *sc, int state, const char *reason)
2042 CARP_LOCK_ASSERT(sc);
2044 if (sc->sc_state != state) {
2045 const char *carp_states[] = { CARP_STATES };
2046 char subsys[IFNAMSIZ+5];
2048 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
2049 sc->sc_carpdev->if_xname);
2051 CARP_LOG("%s: %s -> %s (%s)\n", subsys,
2052 carp_states[sc->sc_state], carp_states[state], reason);
2054 sc->sc_state = state;
2056 devctl_notify("CARP", subsys, carp_states[state], NULL);
2061 carp_linkstate(struct ifnet *ifp)
2063 struct carp_softc *sc;
2065 CIF_LOCK(ifp->if_carp);
2066 IFNET_FOREACH_CARP(ifp, sc) {
2071 CIF_UNLOCK(ifp->if_carp);
2075 carp_sc_state(struct carp_softc *sc)
2078 CARP_LOCK_ASSERT(sc);
2080 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2081 !(sc->sc_carpdev->if_flags & IFF_UP) ||
2083 callout_stop(&sc->sc_ad_tmo);
2085 callout_stop(&sc->sc_md_tmo);
2088 callout_stop(&sc->sc_md6_tmo);
2090 carp_set_state(sc, INIT, "hardware interface down");
2092 if (!sc->sc_suppress)
2093 carp_demote_adj(V_carp_ifdown_adj, "interface down");
2094 sc->sc_suppress = 1;
2096 carp_set_state(sc, INIT, "hardware interface up");
2098 if (sc->sc_suppress)
2099 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2100 sc->sc_suppress = 0;
2105 carp_demote_adj(int adj, char *reason)
2107 atomic_add_int(&V_carp_demotion, adj);
2108 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2109 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2113 carp_allow_sysctl(SYSCTL_HANDLER_ARGS)
2116 struct carp_softc *sc;
2119 error = sysctl_handle_int(oidp, &new, 0, req);
2120 if (error || !req->newptr)
2123 if (V_carp_allow != new) {
2126 mtx_lock(&carp_mtx);
2127 LIST_FOREACH(sc, &carp_list, sc_next) {
2129 if (curvnet == sc->sc_carpdev->if_vnet)
2133 mtx_unlock(&carp_mtx);
2140 carp_dscp_sysctl(SYSCTL_HANDLER_ARGS)
2145 error = sysctl_handle_int(oidp, &new, 0, req);
2146 if (error || !req->newptr)
2149 if (new < 0 || new > 63)
2158 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2162 new = V_carp_demotion;
2163 error = sysctl_handle_int(oidp, &new, 0, req);
2164 if (error || !req->newptr)
2167 carp_demote_adj(new, "sysctl");
2173 carp_mod_cleanup(void)
2177 (void)ipproto_unregister(IPPROTO_CARP);
2178 carp_iamatch_p = NULL;
2181 (void)ip6proto_unregister(IPPROTO_CARP);
2182 carp_iamatch6_p = NULL;
2183 carp_macmatch6_p = NULL;
2185 carp_ioctl_p = NULL;
2186 carp_attach_p = NULL;
2187 carp_detach_p = NULL;
2188 carp_get_vhid_p = NULL;
2189 carp_linkstate_p = NULL;
2190 carp_forus_p = NULL;
2191 carp_output_p = NULL;
2192 carp_demote_adj_p = NULL;
2193 carp_master_p = NULL;
2194 mtx_unlock(&carp_mtx);
2195 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2196 mtx_destroy(&carp_mtx);
2197 sx_destroy(&carp_sx);
2205 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2206 sx_init(&carp_sx, "carp_sx");
2207 LIST_INIT(&carp_list);
2208 carp_get_vhid_p = carp_get_vhid;
2209 carp_forus_p = carp_forus;
2210 carp_output_p = carp_output;
2211 carp_linkstate_p = carp_linkstate;
2212 carp_ioctl_p = carp_ioctl;
2213 carp_attach_p = carp_attach;
2214 carp_detach_p = carp_detach;
2215 carp_demote_adj_p = carp_demote_adj;
2216 carp_master_p = carp_master;
2218 carp_iamatch6_p = carp_iamatch6;
2219 carp_macmatch6_p = carp_macmatch6;
2220 err = ip6proto_register(IPPROTO_CARP, carp6_input, NULL);
2222 printf("carp: error %d registering with INET6\n", err);
2228 carp_iamatch_p = carp_iamatch;
2229 err = ipproto_register(IPPROTO_CARP, carp_input, NULL);
2231 printf("carp: error %d registering with INET\n", err);
2240 carp_modevent(module_t mod, int type, void *data)
2244 return carp_mod_load();
2247 mtx_lock(&carp_mtx);
2248 if (LIST_EMPTY(&carp_list))
2251 mtx_unlock(&carp_mtx);
2263 static moduledata_t carp_mod = {
2269 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);