2 * Copyright (c) 2002 Michael Shalayeff.
3 * Copyright (c) 2003 Ryan McBride.
4 * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
44 #include <sys/module.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/taskqueue.h>
54 #include <net/ethernet.h>
57 #include <net/if_dl.h>
58 #include <net/if_llatbl.h>
59 #include <net/if_types.h>
60 #include <net/iso88025.h>
61 #include <net/route.h>
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_carp.h>
68 #include <netinet/ip.h>
69 #include <machine/in_cksum.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/if_ether.h>
77 #include <netinet/icmp6.h>
78 #include <netinet/ip6.h>
79 #include <netinet6/ip6protosw.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;
150 static int proto_reg[] = {-1, -1};
153 * Brief design of carp(4).
155 * Any carp-capable ifnet may have a list of carp softcs hanging off
156 * its ifp->if_carp pointer. Each softc represents one unique virtual
157 * host id, or vhid. The softc has a back pointer to the ifnet. All
158 * softcs are joined in a global list, which has quite limited use.
160 * Any interface address that takes part in CARP negotiation has a
161 * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
162 * AF_INET or AF_INET6 address.
164 * Although, one can get the softc's backpointer to ifnet and traverse
165 * through its ifp->if_addrhead queue to find all interface addresses
166 * involved in CARP, we keep a growable array of ifaddr pointers. This
167 * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
168 * do calls into the network stack, thus avoiding LORs.
172 * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
173 * callout-driven events and ioctl()s.
175 * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to
176 * traverse the global list we use the mutex carp_mtx.
178 * Known issues with locking:
180 * - There is no protection for races between two ioctl() requests,
181 * neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all
182 * interface ioctl()s should be serialized right in net/if.c.
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 static int carp_allow = 1; /* Accept incoming CARP packets. */
190 static int carp_preempt = 0; /* Preempt slower nodes. */
191 static int carp_log = 1; /* Log level. */
192 static int carp_demotion = 0; /* Global advskew demotion. */
193 static int carp_senderr_adj = CARP_MAXSKEW; /* Send error demotion factor */
194 static int carp_ifdown_adj = CARP_MAXSKEW; /* Iface down demotion factor */
196 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP");
197 SYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW, &carp_allow, 0,
198 "Accept incoming CARP packets");
199 SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW, &carp_preempt, 0,
200 "High-priority backup preemption mode");
201 SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW, &carp_log, 0,
203 SYSCTL_INT(_net_inet_carp, OID_AUTO, demotion, CTLFLAG_RW, &carp_demotion, 0,
204 "Demotion factor (skew of advskew)");
205 SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW,
206 &carp_senderr_adj, 0, "Send error demotion factor adjustment");
207 SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW,
208 &carp_ifdown_adj, 0, "Interface down demotion factor adjustment");
210 static struct carpstats carpstats;
211 SYSCTL_STRUCT(_net_inet_carp, OID_AUTO, stats, CTLFLAG_RW, &carpstats,
212 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
214 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
216 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
217 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
218 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
219 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
220 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
222 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
223 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
224 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
225 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
226 #define CIF_FREE(cif) do { \
227 CIF_LOCK_ASSERT(cif); \
228 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
234 #define CARP_LOG(...) do { \
236 log(LOG_INFO, "carp: " __VA_ARGS__); \
239 #define CARP_DEBUG(...) do { \
241 log(LOG_DEBUG, __VA_ARGS__); \
244 #define IFNET_FOREACH_IFA(ifp, ifa) \
245 IF_ADDR_LOCK_ASSERT(ifp); \
246 TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
247 if ((ifa)->ifa_carp != NULL)
249 #define CARP_FOREACH_IFA(sc, ifa) \
250 CARP_LOCK_ASSERT(sc); \
252 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
253 ((ifa) = sc->sc_ifas[_i]) != NULL; \
256 #define IFNET_FOREACH_CARP(ifp, sc) \
257 CIF_LOCK_ASSERT(ifp->if_carp); \
258 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
260 #define DEMOTE_ADVSKEW(sc) \
261 (((sc)->sc_advskew + carp_demotion > CARP_MAXSKEW) ? \
262 CARP_MAXSKEW : ((sc)->sc_advskew + carp_demotion))
264 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
265 static struct carp_softc
266 *carp_alloc(struct ifnet *);
267 static void carp_detach_locked(struct ifaddr *);
268 static void carp_destroy(struct carp_softc *);
269 static struct carp_if
270 *carp_alloc_if(struct ifnet *);
271 static void carp_free_if(struct carp_if *);
272 static void carp_set_state(struct carp_softc *, int);
273 static void carp_sc_state(struct carp_softc *);
274 static void carp_setrun(struct carp_softc *, sa_family_t);
275 static void carp_master_down(void *);
276 static void carp_master_down_locked(struct carp_softc *);
277 static void carp_send_ad(void *);
278 static void carp_send_ad_locked(struct carp_softc *);
279 static void carp_addroute(struct carp_softc *);
280 static void carp_ifa_addroute(struct ifaddr *);
281 static void carp_delroute(struct carp_softc *);
282 static void carp_ifa_delroute(struct ifaddr *);
283 static void carp_send_ad_all(void *, int);
284 static void carp_demote_adj(int, char *);
286 static LIST_HEAD(, carp_softc) carp_list;
287 static struct mtx carp_mtx;
288 static struct task carp_sendall_task =
289 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
291 static __inline uint16_t
292 carp_cksum(struct mbuf *m, int len)
294 return (in_cksum(m, len));
298 carp_hmac_prepare(struct carp_softc *sc)
300 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
301 uint8_t vhid = sc->sc_vhid & 0xff;
305 struct in_addr last, cur, in;
308 struct in6_addr last6, cur6, in6;
311 CARP_LOCK_ASSERT(sc);
313 /* Compute ipad from key. */
314 bzero(sc->sc_pad, sizeof(sc->sc_pad));
315 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
316 for (i = 0; i < sizeof(sc->sc_pad); i++)
317 sc->sc_pad[i] ^= 0x36;
319 /* Precompute first part of inner hash. */
320 SHA1Init(&sc->sc_sha1);
321 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
322 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
323 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
324 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
330 cur.s_addr = 0xffffffff;
331 CARP_FOREACH_IFA(sc, ifa) {
332 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
333 if (ifa->ifa_addr->sa_family == AF_INET &&
334 ntohl(in.s_addr) > ntohl(last.s_addr) &&
335 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
336 cur.s_addr = in.s_addr;
341 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
345 memset(&cur6, 0, sizeof(cur6));
349 memset(&cur6, 0xff, sizeof(cur6));
350 CARP_FOREACH_IFA(sc, ifa) {
351 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
352 if (IN6_IS_SCOPE_EMBED(&in6))
353 in6.s6_addr16[1] = 0;
354 if (ifa->ifa_addr->sa_family == AF_INET6 &&
355 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
356 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
362 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
366 /* convert ipad to opad */
367 for (i = 0; i < sizeof(sc->sc_pad); i++)
368 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
372 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
373 unsigned char md[20])
377 CARP_LOCK_ASSERT(sc);
379 /* fetch first half of inner hash */
380 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
382 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
383 SHA1Final(md, &sha1ctx);
387 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
388 SHA1Update(&sha1ctx, md, 20);
389 SHA1Final(md, &sha1ctx);
393 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
394 unsigned char md[20])
396 unsigned char md2[20];
398 CARP_LOCK_ASSERT(sc);
400 carp_hmac_generate(sc, counter, md2);
402 return (bcmp(md, md2, sizeof(md2)));
406 * process input packet.
407 * we have rearranged checks order compared to the rfc,
408 * but it seems more efficient this way or not possible otherwise.
412 carp_input(struct mbuf *m, int hlen)
414 struct ip *ip = mtod(m, struct ip *);
415 struct carp_header *ch;
418 CARPSTATS_INC(carps_ipackets);
425 /* verify that the IP TTL is 255. */
426 if (ip->ip_ttl != CARP_DFLTTL) {
427 CARPSTATS_INC(carps_badttl);
428 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
430 m->m_pkthdr.rcvif->if_xname);
435 iplen = ip->ip_hl << 2;
437 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
438 CARPSTATS_INC(carps_badlen);
439 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
440 "on %s\n", __func__, m->m_len - sizeof(struct ip),
441 m->m_pkthdr.rcvif->if_xname);
446 if (iplen + sizeof(*ch) < m->m_len) {
447 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
448 CARPSTATS_INC(carps_hdrops);
449 CARP_DEBUG("%s: pullup failed\n", __func__);
452 ip = mtod(m, struct ip *);
454 ch = (struct carp_header *)((char *)ip + iplen);
457 * verify that the received packet length is
458 * equal to the CARP header
460 len = iplen + sizeof(*ch);
461 if (len > m->m_pkthdr.len) {
462 CARPSTATS_INC(carps_badlen);
463 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
465 m->m_pkthdr.rcvif->if_xname);
470 if ((m = m_pullup(m, len)) == NULL) {
471 CARPSTATS_INC(carps_hdrops);
474 ip = mtod(m, struct ip *);
475 ch = (struct carp_header *)((char *)ip + iplen);
477 /* verify the CARP checksum */
479 if (carp_cksum(m, len - iplen)) {
480 CARPSTATS_INC(carps_badsum);
481 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
482 m->m_pkthdr.rcvif->if_xname);
488 carp_input_c(m, ch, AF_INET);
494 carp6_input(struct mbuf **mp, int *offp, int proto)
496 struct mbuf *m = *mp;
497 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
498 struct carp_header *ch;
501 CARPSTATS_INC(carps_ipackets6);
505 return (IPPROTO_DONE);
508 /* check if received on a valid carp interface */
509 if (m->m_pkthdr.rcvif->if_carp == NULL) {
510 CARPSTATS_INC(carps_badif);
511 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
512 __func__, m->m_pkthdr.rcvif->if_xname);
514 return (IPPROTO_DONE);
517 /* verify that the IP TTL is 255 */
518 if (ip6->ip6_hlim != CARP_DFLTTL) {
519 CARPSTATS_INC(carps_badttl);
520 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
521 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
523 return (IPPROTO_DONE);
526 /* verify that we have a complete carp packet */
528 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
530 CARPSTATS_INC(carps_badlen);
531 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
532 return (IPPROTO_DONE);
536 /* verify the CARP checksum */
538 if (carp_cksum(m, sizeof(*ch))) {
539 CARPSTATS_INC(carps_badsum);
540 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
541 m->m_pkthdr.rcvif->if_xname);
543 return (IPPROTO_DONE);
547 carp_input_c(m, ch, AF_INET6);
548 return (IPPROTO_DONE);
553 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
555 struct ifnet *ifp = m->m_pkthdr.rcvif;
557 struct carp_softc *sc;
558 uint64_t tmp_counter;
559 struct timeval sc_tv, ch_tv;
561 /* verify that the VHID is valid on the receiving interface */
563 IFNET_FOREACH_IFA(ifp, ifa)
564 if (ifa->ifa_addr->sa_family == af &&
565 ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
569 IF_ADDR_RUNLOCK(ifp);
572 CARPSTATS_INC(carps_badvhid);
577 /* verify the CARP version. */
578 if (ch->carp_version != CARP_VERSION) {
579 CARPSTATS_INC(carps_badver);
580 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
591 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
592 CARPSTATS_INC(carps_badauth);
593 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
594 sc->sc_vhid, ifp->if_xname);
598 tmp_counter = ntohl(ch->carp_counter[0]);
599 tmp_counter = tmp_counter<<32;
600 tmp_counter += ntohl(ch->carp_counter[1]);
602 /* XXX Replay protection goes here */
604 sc->sc_init_counter = 0;
605 sc->sc_counter = tmp_counter;
607 sc_tv.tv_sec = sc->sc_advbase;
608 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
609 ch_tv.tv_sec = ch->carp_advbase;
610 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
612 switch (sc->sc_state) {
617 * If we receive an advertisement from a master who's going to
618 * be more frequent than us, go into BACKUP state.
620 if (timevalcmp(&sc_tv, &ch_tv, >) ||
621 timevalcmp(&sc_tv, &ch_tv, ==)) {
622 callout_stop(&sc->sc_ad_tmo);
623 CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
624 "(more frequent advertisement received)\n",
626 sc->sc_carpdev->if_xname);
627 carp_set_state(sc, BACKUP);
634 * If we're pre-empting masters who advertise slower than us,
635 * and this one claims to be slower, treat him as down.
637 if (carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
638 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
639 "(preempting a slower master)\n",
641 sc->sc_carpdev->if_xname);
642 carp_master_down_locked(sc);
647 * If the master is going to advertise at such a low frequency
648 * that he's guaranteed to time out, we'd might as well just
649 * treat him as timed out now.
651 sc_tv.tv_sec = sc->sc_advbase * 3;
652 if (timevalcmp(&sc_tv, &ch_tv, <)) {
653 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
654 "(master timed out)\n",
656 sc->sc_carpdev->if_xname);
657 carp_master_down_locked(sc);
662 * Otherwise, we reset the counter and wait for the next
675 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
679 if (sc->sc_init_counter) {
680 /* this could also be seconds since unix epoch */
681 sc->sc_counter = arc4random();
682 sc->sc_counter = sc->sc_counter << 32;
683 sc->sc_counter += arc4random();
687 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
688 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
690 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
692 /* Tag packet for carp_output */
693 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
694 M_NOWAIT)) == NULL) {
696 CARPSTATS_INC(carps_onomem);
699 bcopy(&sc, mtag + 1, sizeof(sc));
700 m_tag_prepend(m, mtag);
706 * To avoid LORs and possible recursions this function shouldn't
707 * be called directly, but scheduled via taskqueue.
710 carp_send_ad_all(void *ctx __unused, int pending __unused)
712 struct carp_softc *sc;
715 LIST_FOREACH(sc, &carp_list, sc_next)
716 if (sc->sc_state == MASTER) {
718 CURVNET_SET(sc->sc_carpdev->if_vnet);
719 carp_send_ad_locked(sc);
723 mtx_unlock(&carp_mtx);
726 /* Send a periodic advertisement, executed in callout context. */
728 carp_send_ad(void *v)
730 struct carp_softc *sc = v;
732 CARP_LOCK_ASSERT(sc);
733 CURVNET_SET(sc->sc_carpdev->if_vnet);
734 carp_send_ad_locked(sc);
740 carp_send_ad_locked(struct carp_softc *sc)
742 struct carp_header ch;
746 struct carp_header *ch_ptr;
750 CARP_LOCK_ASSERT(sc);
752 advskew = DEMOTE_ADVSKEW(sc);
753 tv.tv_sec = sc->sc_advbase;
754 tv.tv_usec = advskew * 1000000 / 256;
756 ch.carp_version = CARP_VERSION;
757 ch.carp_type = CARP_ADVERTISEMENT;
758 ch.carp_vhid = sc->sc_vhid;
759 ch.carp_advbase = sc->sc_advbase;
760 ch.carp_advskew = advskew;
761 ch.carp_authlen = 7; /* XXX DEFINE */
762 ch.carp_pad1 = 0; /* must be zero */
765 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
771 MGETHDR(m, M_NOWAIT, MT_HEADER);
773 CARPSTATS_INC(carps_onomem);
776 len = sizeof(*ip) + sizeof(ch);
777 m->m_pkthdr.len = len;
778 m->m_pkthdr.rcvif = NULL;
780 MH_ALIGN(m, m->m_len);
781 m->m_flags |= M_MCAST;
782 ip = mtod(m, struct ip *);
783 ip->ip_v = IPVERSION;
784 ip->ip_hl = sizeof(*ip) >> 2;
785 ip->ip_tos = IPTOS_LOWDELAY;
787 ip->ip_id = ip_newid();
789 ip->ip_ttl = CARP_DFLTTL;
790 ip->ip_p = IPPROTO_CARP;
793 bzero(&sa, sizeof(sa));
794 sa.sa_family = AF_INET;
795 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
798 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
801 ip->ip_src.s_addr = 0;
802 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
804 ch_ptr = (struct carp_header *)(&ip[1]);
805 bcopy(&ch, ch_ptr, sizeof(ch));
806 if (carp_prepare_ad(m, sc, ch_ptr))
809 m->m_data += sizeof(*ip);
810 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
811 m->m_data -= sizeof(*ip);
813 CARPSTATS_INC(carps_opackets);
815 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
816 &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
817 if (sc->sc_sendad_errors < INT_MAX)
818 sc->sc_sendad_errors++;
819 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
820 carp_demote_adj(carp_senderr_adj, "send error");
821 sc->sc_sendad_success = 0;
823 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
824 if (++sc->sc_sendad_success >=
825 CARP_SENDAD_MIN_SUCCESS) {
826 carp_demote_adj(-carp_senderr_adj,
828 sc->sc_sendad_errors = 0;
831 sc->sc_sendad_errors = 0;
836 if (sc->sc_naddrs6) {
839 MGETHDR(m, M_NOWAIT, MT_HEADER);
841 CARPSTATS_INC(carps_onomem);
844 len = sizeof(*ip6) + sizeof(ch);
845 m->m_pkthdr.len = len;
846 m->m_pkthdr.rcvif = NULL;
848 MH_ALIGN(m, m->m_len);
849 m->m_flags |= M_MCAST;
850 ip6 = mtod(m, struct ip6_hdr *);
851 bzero(ip6, sizeof(*ip6));
852 ip6->ip6_vfc |= IPV6_VERSION;
853 ip6->ip6_hlim = CARP_DFLTTL;
854 ip6->ip6_nxt = IPPROTO_CARP;
855 bzero(&sa, sizeof(sa));
857 /* set the source address */
858 sa.sa_family = AF_INET6;
859 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
861 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
862 sizeof(struct in6_addr));
865 /* This should never happen with IPv6. */
866 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
868 /* Set the multicast destination. */
869 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
870 ip6->ip6_dst.s6_addr8[15] = 0x12;
871 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
873 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
877 ch_ptr = (struct carp_header *)(&ip6[1]);
878 bcopy(&ch, ch_ptr, sizeof(ch));
879 if (carp_prepare_ad(m, sc, ch_ptr))
882 m->m_data += sizeof(*ip6);
883 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
884 m->m_data -= sizeof(*ip6);
886 CARPSTATS_INC(carps_opackets6);
888 if (ip6_output(m, NULL, NULL, 0,
889 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
890 if (sc->sc_sendad_errors < INT_MAX)
891 sc->sc_sendad_errors++;
892 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
893 carp_demote_adj(carp_senderr_adj,
895 sc->sc_sendad_success = 0;
897 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
898 if (++sc->sc_sendad_success >=
899 CARP_SENDAD_MIN_SUCCESS) {
900 carp_demote_adj(-carp_senderr_adj,
902 sc->sc_sendad_errors = 0;
905 sc->sc_sendad_errors = 0;
911 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
915 carp_addroute(struct carp_softc *sc)
919 CARP_FOREACH_IFA(sc, ifa)
920 carp_ifa_addroute(ifa);
924 carp_ifa_addroute(struct ifaddr *ifa)
927 switch (ifa->ifa_addr->sa_family) {
930 in_addprefix(ifatoia(ifa), RTF_UP);
931 ifa_add_loopback_route(ifa,
932 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
937 ifa_add_loopback_route(ifa,
938 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
946 carp_delroute(struct carp_softc *sc)
950 CARP_FOREACH_IFA(sc, ifa)
951 carp_ifa_delroute(ifa);
955 carp_ifa_delroute(struct ifaddr *ifa)
958 switch (ifa->ifa_addr->sa_family) {
961 ifa_del_loopback_route(ifa,
962 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
963 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
968 ifa_del_loopback_route(ifa,
969 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
978 * Broadcast a gratuitous ARP request containing
979 * the virtual router MAC address for each IP address
980 * associated with the virtual router.
983 carp_send_arp(struct carp_softc *sc)
987 CARP_FOREACH_IFA(sc, ifa)
988 if (ifa->ifa_addr->sa_family == AF_INET)
989 arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
993 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
995 struct carp_softc *sc = ifa->ifa_carp;
997 if (sc->sc_state == MASTER) {
998 *enaddr = LLADDR(&sc->sc_addr);
1008 carp_send_na(struct carp_softc *sc)
1010 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1012 struct in6_addr *in6;
1014 CARP_FOREACH_IFA(sc, ifa) {
1015 if (ifa->ifa_addr->sa_family != AF_INET6)
1019 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1020 ND_NA_FLAG_OVERRIDE, 1, NULL);
1021 DELAY(1000); /* XXX */
1026 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1027 * matches and is not a carp address. Returns NULL otherwise.
1030 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1036 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1037 if (ifa->ifa_addr->sa_family != AF_INET6)
1039 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1041 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1047 IF_ADDR_RUNLOCK(ifp);
1053 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1058 IFNET_FOREACH_IFA(ifp, ifa)
1059 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1060 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1061 struct carp_softc *sc = ifa->ifa_carp;
1064 IF_ADDR_RUNLOCK(ifp);
1066 mtag = m_tag_get(PACKET_TAG_CARP,
1067 sizeof(struct carp_softc *), M_NOWAIT);
1069 /* Better a bit than nothing. */
1070 return (LLADDR(&sc->sc_addr));
1072 bcopy(&sc, mtag + 1, sizeof(sc));
1073 m_tag_prepend(m, mtag);
1075 return (LLADDR(&sc->sc_addr));
1077 IF_ADDR_RUNLOCK(ifp);
1084 carp_forus(struct ifnet *ifp, u_char *dhost)
1086 struct carp_softc *sc;
1087 uint8_t *ena = dhost;
1089 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1092 CIF_LOCK(ifp->if_carp);
1093 IFNET_FOREACH_CARP(ifp, sc) {
1095 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1098 CIF_UNLOCK(ifp->if_carp);
1103 CIF_UNLOCK(ifp->if_carp);
1108 /* Master down timeout event, executed in callout context. */
1110 carp_master_down(void *v)
1112 struct carp_softc *sc = v;
1114 CARP_LOCK_ASSERT(sc);
1116 CURVNET_SET(sc->sc_carpdev->if_vnet);
1117 if (sc->sc_state == BACKUP) {
1118 CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
1120 sc->sc_carpdev->if_xname);
1121 carp_master_down_locked(sc);
1129 carp_master_down_locked(struct carp_softc *sc)
1132 CARP_LOCK_ASSERT(sc);
1134 switch (sc->sc_state) {
1136 carp_set_state(sc, MASTER);
1137 carp_send_ad_locked(sc);
1150 panic("carp: VHID %u@%s: master_down event in %s state\n",
1152 sc->sc_carpdev->if_xname,
1153 sc->sc_state ? "MASTER" : "INIT");
1160 * When in backup state, af indicates whether to reset the master down timer
1161 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1164 carp_setrun(struct carp_softc *sc, sa_family_t af)
1168 CARP_LOCK_ASSERT(sc);
1170 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1171 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1172 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1175 switch (sc->sc_state) {
1177 CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1179 sc->sc_carpdev->if_xname);
1180 carp_set_state(sc, BACKUP);
1184 callout_stop(&sc->sc_ad_tmo);
1185 tv.tv_sec = 3 * sc->sc_advbase;
1186 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1190 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1191 carp_master_down, sc);
1196 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1197 carp_master_down, sc);
1203 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1204 carp_master_down, sc);
1208 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1209 carp_master_down, sc);
1215 tv.tv_sec = sc->sc_advbase;
1216 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1217 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1224 * Setup multicast structures.
1227 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1229 struct ifnet *ifp = cif->cif_ifp;
1232 CIF_LOCK_ASSERT(cif);
1238 struct ip_moptions *imo = &cif->cif_imo;
1239 struct in_addr addr;
1241 if (imo->imo_membership)
1244 imo->imo_membership = (struct in_multi **)malloc(
1245 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1247 if (imo->imo_membership == NULL)
1249 imo->imo_mfilters = NULL;
1250 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1251 imo->imo_multicast_vif = -1;
1253 addr.s_addr = htonl(INADDR_CARP_GROUP);
1254 if ((error = in_joingroup(ifp, &addr, NULL,
1255 &imo->imo_membership[0])) != 0) {
1256 free(imo->imo_membership, M_CARP);
1259 imo->imo_num_memberships++;
1260 imo->imo_multicast_ifp = ifp;
1261 imo->imo_multicast_ttl = CARP_DFLTTL;
1262 imo->imo_multicast_loop = 0;
1269 struct ip6_moptions *im6o = &cif->cif_im6o;
1270 struct in6_addr in6;
1271 struct in6_multi *in6m;
1273 if (im6o->im6o_membership)
1276 im6o->im6o_membership = (struct in6_multi **)malloc(
1277 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1279 if (im6o->im6o_membership == NULL)
1281 im6o->im6o_mfilters = NULL;
1282 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1283 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1284 im6o->im6o_multicast_ifp = ifp;
1286 /* Join IPv6 CARP multicast group. */
1287 bzero(&in6, sizeof(in6));
1288 in6.s6_addr16[0] = htons(0xff02);
1289 in6.s6_addr8[15] = 0x12;
1290 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1291 free(im6o->im6o_membership, M_CARP);
1295 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1296 free(im6o->im6o_membership, M_CARP);
1299 im6o->im6o_membership[0] = in6m;
1300 im6o->im6o_num_memberships++;
1302 /* Join solicited multicast address. */
1303 bzero(&in6, sizeof(in6));
1304 in6.s6_addr16[0] = htons(0xff02);
1305 in6.s6_addr32[1] = 0;
1306 in6.s6_addr32[2] = htonl(1);
1307 in6.s6_addr32[3] = 0;
1308 in6.s6_addr8[12] = 0xff;
1309 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1310 in6_mc_leave(im6o->im6o_membership[0], NULL);
1311 free(im6o->im6o_membership, M_CARP);
1315 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1316 in6_mc_leave(im6o->im6o_membership[0], NULL);
1317 free(im6o->im6o_membership, M_CARP);
1320 im6o->im6o_membership[1] = in6m;
1321 im6o->im6o_num_memberships++;
1331 * Free multicast structures.
1334 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1337 CIF_LOCK_ASSERT(cif);
1341 if (cif->cif_naddrs == 0) {
1342 struct ip_moptions *imo = &cif->cif_imo;
1344 in_leavegroup(imo->imo_membership[0], NULL);
1345 KASSERT(imo->imo_mfilters == NULL,
1346 ("%s: imo_mfilters != NULL", __func__));
1347 free(imo->imo_membership, M_CARP);
1348 imo->imo_membership = NULL;
1355 if (cif->cif_naddrs6 == 0) {
1356 struct ip6_moptions *im6o = &cif->cif_im6o;
1358 in6_mc_leave(im6o->im6o_membership[0], NULL);
1359 in6_mc_leave(im6o->im6o_membership[1], NULL);
1360 KASSERT(im6o->im6o_mfilters == NULL,
1361 ("%s: im6o_mfilters != NULL", __func__));
1362 free(im6o->im6o_membership, M_CARP);
1363 im6o->im6o_membership = NULL;
1371 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa)
1374 struct carp_softc *sc;
1379 switch (sa->sa_family) {
1392 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1396 bcopy(mtag + 1, &sc, sizeof(sc));
1398 /* Set the source MAC address to the Virtual Router MAC Address. */
1399 switch (ifp->if_type) {
1403 struct ether_header *eh;
1405 eh = mtod(m, struct ether_header *);
1406 eh->ether_shost[0] = 0;
1407 eh->ether_shost[1] = 0;
1408 eh->ether_shost[2] = 0x5e;
1409 eh->ether_shost[3] = 0;
1410 eh->ether_shost[4] = 1;
1411 eh->ether_shost[5] = sc->sc_vhid;
1415 struct fddi_header *fh;
1417 fh = mtod(m, struct fddi_header *);
1418 fh->fddi_shost[0] = 0;
1419 fh->fddi_shost[1] = 0;
1420 fh->fddi_shost[2] = 0x5e;
1421 fh->fddi_shost[3] = 0;
1422 fh->fddi_shost[4] = 1;
1423 fh->fddi_shost[5] = sc->sc_vhid;
1426 case IFT_ISO88025: {
1427 struct iso88025_header *th;
1428 th = mtod(m, struct iso88025_header *);
1429 th->iso88025_shost[0] = 3;
1430 th->iso88025_shost[1] = 0;
1431 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1432 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1433 th->iso88025_shost[4] = 0;
1434 th->iso88025_shost[5] = 0;
1438 printf("%s: carp is not supported for the %d interface type\n",
1439 ifp->if_xname, ifp->if_type);
1440 return (EOPNOTSUPP);
1446 static struct carp_softc*
1447 carp_alloc(struct ifnet *ifp)
1449 struct carp_softc *sc;
1450 struct carp_if *cif;
1452 if ((cif = ifp->if_carp) == NULL) {
1453 cif = carp_alloc_if(ifp);
1458 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1460 sc->sc_advbase = CARP_DFLTINTV;
1461 sc->sc_vhid = -1; /* required setting */
1462 sc->sc_init_counter = 1;
1463 sc->sc_state = INIT;
1465 sc->sc_ifasiz = sizeof(struct ifaddr *);
1466 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1467 sc->sc_carpdev = ifp;
1471 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1474 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1476 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1479 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1482 mtx_lock(&carp_mtx);
1483 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1484 mtx_unlock(&carp_mtx);
1490 carp_grow_ifas(struct carp_softc *sc)
1492 struct ifaddr **new;
1494 CARP_LOCK_ASSERT(sc);
1496 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1499 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1500 free(sc->sc_ifas, M_CARP);
1508 carp_destroy(struct carp_softc *sc)
1510 struct ifnet *ifp = sc->sc_carpdev;
1511 struct carp_if *cif = ifp->if_carp;
1513 CIF_LOCK_ASSERT(cif);
1515 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1517 mtx_lock(&carp_mtx);
1518 LIST_REMOVE(sc, sc_next);
1519 mtx_unlock(&carp_mtx);
1522 if (sc->sc_suppress)
1523 carp_demote_adj(-carp_ifdown_adj, "vhid removed");
1524 callout_drain(&sc->sc_ad_tmo);
1526 callout_drain(&sc->sc_md_tmo);
1529 callout_drain(&sc->sc_md6_tmo);
1531 CARP_LOCK_DESTROY(sc);
1533 free(sc->sc_ifas, M_CARP);
1537 static struct carp_if*
1538 carp_alloc_if(struct ifnet *ifp)
1540 struct carp_if *cif;
1542 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1544 if (ifpromisc(ifp, 1) != 0)
1549 TAILQ_INIT(&cif->cif_vrs);
1554 IF_ADDR_WUNLOCK(ifp);
1565 carp_free_if(struct carp_if *cif)
1567 struct ifnet *ifp = cif->cif_ifp;
1569 CIF_LOCK_ASSERT(cif);
1570 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1574 ifp->if_carp = NULL;
1576 IF_ADDR_WUNLOCK(ifp);
1578 CIF_LOCK_DESTROY(cif);
1586 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1590 carpr->carpr_state = sc->sc_state;
1591 carpr->carpr_vhid = sc->sc_vhid;
1592 carpr->carpr_advbase = sc->sc_advbase;
1593 carpr->carpr_advskew = sc->sc_advskew;
1595 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1597 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1602 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1604 struct carpreq carpr;
1606 struct carp_softc *sc = NULL;
1607 int error = 0, locked = 0;
1609 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1612 ifp = ifunit_ref(ifr->ifr_name);
1616 switch (ifp->if_type) {
1628 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1629 error = EADDRNOTAVAIL;
1635 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1637 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1638 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1644 CIF_LOCK(ifp->if_carp);
1645 IFNET_FOREACH_CARP(ifp, sc)
1646 if (sc->sc_vhid == carpr.carpr_vhid)
1648 CIF_UNLOCK(ifp->if_carp);
1651 sc = carp_alloc(ifp);
1653 error = EINVAL; /* XXX: ifpromisc failed */
1658 sc->sc_vhid = carpr.carpr_vhid;
1659 LLADDR(&sc->sc_addr)[0] = 0;
1660 LLADDR(&sc->sc_addr)[1] = 0;
1661 LLADDR(&sc->sc_addr)[2] = 0x5e;
1662 LLADDR(&sc->sc_addr)[3] = 0;
1663 LLADDR(&sc->sc_addr)[4] = 1;
1664 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1668 if (carpr.carpr_advbase > 0) {
1669 if (carpr.carpr_advbase > 255 ||
1670 carpr.carpr_advbase < CARP_DFLTINTV) {
1674 sc->sc_advbase = carpr.carpr_advbase;
1676 if (carpr.carpr_advskew > 0) {
1677 if (carpr.carpr_advskew >= 255) {
1681 sc->sc_advskew = carpr.carpr_advskew;
1683 if (carpr.carpr_key[0] != '\0') {
1684 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1685 carp_hmac_prepare(sc);
1687 if (sc->sc_state != INIT &&
1688 carpr.carpr_state != sc->sc_state) {
1689 switch (carpr.carpr_state) {
1691 callout_stop(&sc->sc_ad_tmo);
1692 carp_set_state(sc, BACKUP);
1697 carp_master_down_locked(sc);
1709 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1713 if (carpr.carpr_count < 1) {
1717 if (ifp->if_carp == NULL) {
1722 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1723 if (carpr.carpr_vhid != 0) {
1724 CIF_LOCK(ifp->if_carp);
1725 IFNET_FOREACH_CARP(ifp, sc)
1726 if (sc->sc_vhid == carpr.carpr_vhid)
1728 CIF_UNLOCK(ifp->if_carp);
1733 carp_carprcp(&carpr, sc, priveleged);
1734 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1739 CIF_LOCK(ifp->if_carp);
1740 IFNET_FOREACH_CARP(ifp, sc)
1743 if (count > carpr.carpr_count) {
1744 CIF_UNLOCK(ifp->if_carp);
1750 IFNET_FOREACH_CARP(ifp, sc) {
1751 carp_carprcp(&carpr, sc, priveleged);
1752 carpr.carpr_count = count;
1753 error = copyout(&carpr, ifr->ifr_data +
1754 (i * sizeof(carpr)), sizeof(carpr));
1756 CIF_UNLOCK(ifp->if_carp);
1761 CIF_UNLOCK(ifp->if_carp);
1778 carp_get_vhid(struct ifaddr *ifa)
1781 if (ifa == NULL || ifa->ifa_carp == NULL)
1784 return (ifa->ifa_carp->sc_vhid);
1788 carp_attach(struct ifaddr *ifa, int vhid)
1790 struct ifnet *ifp = ifa->ifa_ifp;
1791 struct carp_if *cif = ifp->if_carp;
1792 struct carp_softc *sc;
1795 if (ifp->if_carp == NULL)
1796 return (ENOPROTOOPT);
1798 switch (ifa->ifa_addr->sa_family) {
1807 return (EPROTOTYPE);
1811 IFNET_FOREACH_CARP(ifp, sc)
1812 if (sc->sc_vhid == vhid)
1819 if (ifa->ifa_carp) {
1820 if (ifa->ifa_carp->sc_vhid != vhid)
1821 carp_detach_locked(ifa);
1828 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1835 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1836 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1837 if ((error = carp_grow_ifas(sc)) != 0) {
1838 carp_multicast_cleanup(cif,
1839 ifa->ifa_addr->sa_family);
1845 switch (ifa->ifa_addr->sa_family) {
1861 sc->sc_ifas[index - 1] = ifa;
1864 carp_hmac_prepare(sc);
1874 carp_detach(struct ifaddr *ifa)
1876 struct ifnet *ifp = ifa->ifa_ifp;
1877 struct carp_if *cif = ifp->if_carp;
1880 carp_detach_locked(ifa);
1885 carp_detach_locked(struct ifaddr *ifa)
1887 struct ifnet *ifp = ifa->ifa_ifp;
1888 struct carp_if *cif = ifp->if_carp;
1889 struct carp_softc *sc = ifa->ifa_carp;
1892 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1894 CIF_LOCK_ASSERT(cif);
1898 index = sc->sc_naddrs + sc->sc_naddrs6;
1899 for (i = 0; i < index; i++)
1900 if (sc->sc_ifas[i] == ifa)
1902 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1903 for (; i < index - 1; i++)
1904 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1905 sc->sc_ifas[index - 1] = NULL;
1907 switch (ifa->ifa_addr->sa_family) {
1922 carp_ifa_delroute(ifa);
1923 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1925 ifa->ifa_carp = NULL;
1928 carp_hmac_prepare(sc);
1931 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1939 carp_set_state(struct carp_softc *sc, int state)
1942 CARP_LOCK_ASSERT(sc);
1944 if (sc->sc_state != state) {
1945 const char *carp_states[] = { CARP_STATES };
1946 char subsys[IFNAMSIZ+5];
1948 sc->sc_state = state;
1950 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1951 sc->sc_carpdev->if_xname);
1952 devctl_notify("CARP", subsys, carp_states[state], NULL);
1957 carp_linkstate(struct ifnet *ifp)
1959 struct carp_softc *sc;
1961 CIF_LOCK(ifp->if_carp);
1962 IFNET_FOREACH_CARP(ifp, sc) {
1967 CIF_UNLOCK(ifp->if_carp);
1971 carp_sc_state(struct carp_softc *sc)
1974 CARP_LOCK_ASSERT(sc);
1976 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1977 !(sc->sc_carpdev->if_flags & IFF_UP)) {
1978 callout_stop(&sc->sc_ad_tmo);
1980 callout_stop(&sc->sc_md_tmo);
1983 callout_stop(&sc->sc_md6_tmo);
1985 carp_set_state(sc, INIT);
1987 if (!sc->sc_suppress)
1988 carp_demote_adj(carp_ifdown_adj, "interface down");
1989 sc->sc_suppress = 1;
1991 carp_set_state(sc, INIT);
1993 if (sc->sc_suppress)
1994 carp_demote_adj(-carp_ifdown_adj, "interface up");
1995 sc->sc_suppress = 0;
2000 carp_demote_adj(int adj, char *reason)
2002 carp_demotion += adj;
2003 CARP_LOG("demoted by %d to %d (%s)\n", adj, carp_demotion, reason);
2004 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2008 extern struct domain inetdomain;
2009 static struct protosw in_carp_protosw = {
2010 .pr_type = SOCK_RAW,
2011 .pr_domain = &inetdomain,
2012 .pr_protocol = IPPROTO_CARP,
2013 .pr_flags = PR_ATOMIC|PR_ADDR,
2014 .pr_input = carp_input,
2015 .pr_output = (pr_output_t *)rip_output,
2016 .pr_ctloutput = rip_ctloutput,
2017 .pr_usrreqs = &rip_usrreqs
2022 extern struct domain inet6domain;
2023 static struct ip6protosw in6_carp_protosw = {
2024 .pr_type = SOCK_RAW,
2025 .pr_domain = &inet6domain,
2026 .pr_protocol = IPPROTO_CARP,
2027 .pr_flags = PR_ATOMIC|PR_ADDR,
2028 .pr_input = carp6_input,
2029 .pr_output = rip6_output,
2030 .pr_ctloutput = rip6_ctloutput,
2031 .pr_usrreqs = &rip6_usrreqs
2036 carp_mod_cleanup(void)
2040 if (proto_reg[CARP_INET] == 0) {
2041 (void)ipproto_unregister(IPPROTO_CARP);
2042 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2043 proto_reg[CARP_INET] = -1;
2045 carp_iamatch_p = NULL;
2048 if (proto_reg[CARP_INET6] == 0) {
2049 (void)ip6proto_unregister(IPPROTO_CARP);
2050 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2051 proto_reg[CARP_INET6] = -1;
2053 carp_iamatch6_p = NULL;
2054 carp_macmatch6_p = NULL;
2056 carp_ioctl_p = NULL;
2057 carp_attach_p = NULL;
2058 carp_detach_p = NULL;
2059 carp_get_vhid_p = NULL;
2060 carp_linkstate_p = NULL;
2061 carp_forus_p = NULL;
2062 carp_output_p = NULL;
2063 carp_demote_adj_p = NULL;
2064 mtx_unlock(&carp_mtx);
2065 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2066 mtx_destroy(&carp_mtx);
2074 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2075 LIST_INIT(&carp_list);
2076 carp_get_vhid_p = carp_get_vhid;
2077 carp_forus_p = carp_forus;
2078 carp_output_p = carp_output;
2079 carp_linkstate_p = carp_linkstate;
2080 carp_ioctl_p = carp_ioctl;
2081 carp_attach_p = carp_attach;
2082 carp_detach_p = carp_detach;
2083 carp_demote_adj_p = carp_demote_adj;
2085 carp_iamatch6_p = carp_iamatch6;
2086 carp_macmatch6_p = carp_macmatch6;
2087 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2088 (struct protosw *)&in6_carp_protosw);
2089 if (proto_reg[CARP_INET6]) {
2090 printf("carp: error %d attaching to PF_INET6\n",
2091 proto_reg[CARP_INET6]);
2093 return (proto_reg[CARP_INET6]);
2095 err = ip6proto_register(IPPROTO_CARP);
2097 printf("carp: error %d registering with INET6\n", err);
2103 carp_iamatch_p = carp_iamatch;
2104 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2105 if (proto_reg[CARP_INET]) {
2106 printf("carp: error %d attaching to PF_INET\n",
2107 proto_reg[CARP_INET]);
2109 return (proto_reg[CARP_INET]);
2111 err = ipproto_register(IPPROTO_CARP);
2113 printf("carp: error %d registering with INET\n", err);
2122 carp_modevent(module_t mod, int type, void *data)
2126 return carp_mod_load();
2129 mtx_lock(&carp_mtx);
2130 if (LIST_EMPTY(&carp_list))
2133 mtx_unlock(&carp_mtx);
2145 static moduledata_t carp_mod = {
2151 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);