2 * Copyright (c) 2002 Michael Shalayeff.
3 * Copyright (c) 2003 Ryan McBride.
4 * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
44 #include <sys/module.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/taskqueue.h>
53 #include <sys/counter.h>
55 #include <net/ethernet.h>
58 #include <net/if_dl.h>
59 #include <net/if_llatbl.h>
60 #include <net/if_types.h>
61 #include <net/iso88025.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/ip6protosw.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;
151 static int proto_reg[] = {-1, -1};
154 * Brief design of carp(4).
156 * Any carp-capable ifnet may have a list of carp softcs hanging off
157 * its ifp->if_carp pointer. Each softc represents one unique virtual
158 * host id, or vhid. The softc has a back pointer to the ifnet. All
159 * softcs are joined in a global list, which has quite limited use.
161 * Any interface address that takes part in CARP negotiation has a
162 * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
163 * AF_INET or AF_INET6 address.
165 * Although, one can get the softc's backpointer to ifnet and traverse
166 * through its ifp->if_addrhead queue to find all interface addresses
167 * involved in CARP, we keep a growable array of ifaddr pointers. This
168 * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
169 * do calls into the network stack, thus avoiding LORs.
173 * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
174 * callout-driven events and ioctl()s.
176 * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to
177 * traverse the global list we use the mutex carp_mtx.
179 * Known issues with locking:
181 * - There is no protection for races between two ioctl() requests,
182 * neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all
183 * interface ioctl()s should be serialized right in net/if.c.
184 * - Sending ad, we put the pointer to the softc in an mtag, and no reference
185 * counting is done on the softc.
186 * - On module unload we may race (?) with packet processing thread
187 * dereferencing our function pointers.
190 /* Accept incoming CARP packets. */
191 static VNET_DEFINE(int, carp_allow) = 1;
192 #define V_carp_allow VNET(carp_allow)
194 /* Preempt slower nodes. */
195 static VNET_DEFINE(int, carp_preempt) = 0;
196 #define V_carp_preempt VNET(carp_preempt)
199 static VNET_DEFINE(int, carp_log) = 1;
200 #define V_carp_log VNET(carp_log)
202 /* Global advskew demotion. */
203 static VNET_DEFINE(int, carp_demotion) = 0;
204 #define V_carp_demotion VNET(carp_demotion)
206 /* Send error demotion factor. */
207 static VNET_DEFINE(int, carp_senderr_adj) = CARP_MAXSKEW;
208 #define V_carp_senderr_adj VNET(carp_senderr_adj)
210 /* Iface down demotion factor. */
211 static VNET_DEFINE(int, carp_ifdown_adj) = CARP_MAXSKEW;
212 #define V_carp_ifdown_adj VNET(carp_ifdown_adj)
214 static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
216 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP");
217 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW,
218 &VNET_NAME(carp_allow), 0, "Accept incoming CARP packets");
219 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW,
220 &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
221 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW,
222 &VNET_NAME(carp_log), 0, "CARP log level");
223 SYSCTL_VNET_PROC(_net_inet_carp, OID_AUTO, demotion, CTLTYPE_INT|CTLFLAG_RW,
224 0, 0, carp_demote_adj_sysctl, "I",
225 "Adjust demotion factor (skew of advskew)");
226 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW,
227 &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
228 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW,
229 &VNET_NAME(carp_ifdown_adj), 0,
230 "Interface down demotion factor adjustment");
232 VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
233 VNET_PCPUSTAT_SYSINIT(carpstats);
234 VNET_PCPUSTAT_SYSUNINIT(carpstats);
236 #define CARPSTATS_ADD(name, val) \
237 counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
238 sizeof(uint64_t)], (val))
239 #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1)
241 SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
242 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
244 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
246 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
247 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
248 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
249 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
250 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
252 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
253 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
254 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
255 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
256 #define CIF_FREE(cif) do { \
257 CIF_LOCK_ASSERT(cif); \
258 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
264 #define CARP_LOG(...) do { \
265 if (V_carp_log > 0) \
266 log(LOG_INFO, "carp: " __VA_ARGS__); \
269 #define CARP_DEBUG(...) do { \
270 if (V_carp_log > 1) \
271 log(LOG_DEBUG, __VA_ARGS__); \
274 #define IFNET_FOREACH_IFA(ifp, ifa) \
275 IF_ADDR_LOCK_ASSERT(ifp); \
276 TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
277 if ((ifa)->ifa_carp != NULL)
279 #define CARP_FOREACH_IFA(sc, ifa) \
280 CARP_LOCK_ASSERT(sc); \
282 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
283 ((ifa) = sc->sc_ifas[_i]) != NULL; \
286 #define IFNET_FOREACH_CARP(ifp, sc) \
287 CIF_LOCK_ASSERT(ifp->if_carp); \
288 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
290 #define DEMOTE_ADVSKEW(sc) \
291 (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \
292 CARP_MAXSKEW : ((sc)->sc_advskew + V_carp_demotion))
294 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
295 static struct carp_softc
296 *carp_alloc(struct ifnet *);
297 static void carp_detach_locked(struct ifaddr *);
298 static void carp_destroy(struct carp_softc *);
299 static struct carp_if
300 *carp_alloc_if(struct ifnet *);
301 static void carp_free_if(struct carp_if *);
302 static void carp_set_state(struct carp_softc *, int);
303 static void carp_sc_state(struct carp_softc *);
304 static void carp_setrun(struct carp_softc *, sa_family_t);
305 static void carp_master_down(void *);
306 static void carp_master_down_locked(struct carp_softc *);
307 static void carp_send_ad(void *);
308 static void carp_send_ad_locked(struct carp_softc *);
309 static void carp_addroute(struct carp_softc *);
310 static void carp_ifa_addroute(struct ifaddr *);
311 static void carp_delroute(struct carp_softc *);
312 static void carp_ifa_delroute(struct ifaddr *);
313 static void carp_send_ad_all(void *, int);
314 static void carp_demote_adj(int, char *);
316 static LIST_HEAD(, carp_softc) carp_list;
317 static struct mtx carp_mtx;
318 static struct task carp_sendall_task =
319 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
322 carp_hmac_prepare(struct carp_softc *sc)
324 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
325 uint8_t vhid = sc->sc_vhid & 0xff;
329 struct in_addr last, cur, in;
332 struct in6_addr last6, cur6, in6;
335 CARP_LOCK_ASSERT(sc);
337 /* Compute ipad from key. */
338 bzero(sc->sc_pad, sizeof(sc->sc_pad));
339 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
340 for (i = 0; i < sizeof(sc->sc_pad); i++)
341 sc->sc_pad[i] ^= 0x36;
343 /* Precompute first part of inner hash. */
344 SHA1Init(&sc->sc_sha1);
345 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
346 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
347 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
348 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
354 cur.s_addr = 0xffffffff;
355 CARP_FOREACH_IFA(sc, ifa) {
356 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
357 if (ifa->ifa_addr->sa_family == AF_INET &&
358 ntohl(in.s_addr) > ntohl(last.s_addr) &&
359 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
360 cur.s_addr = in.s_addr;
365 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
369 memset(&cur6, 0, sizeof(cur6));
373 memset(&cur6, 0xff, sizeof(cur6));
374 CARP_FOREACH_IFA(sc, ifa) {
375 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
376 if (IN6_IS_SCOPE_EMBED(&in6))
377 in6.s6_addr16[1] = 0;
378 if (ifa->ifa_addr->sa_family == AF_INET6 &&
379 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
380 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
386 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
390 /* convert ipad to opad */
391 for (i = 0; i < sizeof(sc->sc_pad); i++)
392 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
396 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
397 unsigned char md[20])
401 CARP_LOCK_ASSERT(sc);
403 /* fetch first half of inner hash */
404 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
406 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
407 SHA1Final(md, &sha1ctx);
411 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
412 SHA1Update(&sha1ctx, md, 20);
413 SHA1Final(md, &sha1ctx);
417 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
418 unsigned char md[20])
420 unsigned char md2[20];
422 CARP_LOCK_ASSERT(sc);
424 carp_hmac_generate(sc, counter, md2);
426 return (bcmp(md, md2, sizeof(md2)));
430 * process input packet.
431 * we have rearranged checks order compared to the rfc,
432 * but it seems more efficient this way or not possible otherwise.
436 carp_input(struct mbuf *m, int hlen)
438 struct ip *ip = mtod(m, struct ip *);
439 struct carp_header *ch;
442 CARPSTATS_INC(carps_ipackets);
449 /* verify that the IP TTL is 255. */
450 if (ip->ip_ttl != CARP_DFLTTL) {
451 CARPSTATS_INC(carps_badttl);
452 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
454 m->m_pkthdr.rcvif->if_xname);
459 iplen = ip->ip_hl << 2;
461 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
462 CARPSTATS_INC(carps_badlen);
463 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
464 "on %s\n", __func__, m->m_len - sizeof(struct ip),
465 m->m_pkthdr.rcvif->if_xname);
470 if (iplen + sizeof(*ch) < m->m_len) {
471 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
472 CARPSTATS_INC(carps_hdrops);
473 CARP_DEBUG("%s: pullup failed\n", __func__);
476 ip = mtod(m, struct ip *);
478 ch = (struct carp_header *)((char *)ip + iplen);
481 * verify that the received packet length is
482 * equal to the CARP header
484 len = iplen + sizeof(*ch);
485 if (len > m->m_pkthdr.len) {
486 CARPSTATS_INC(carps_badlen);
487 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
489 m->m_pkthdr.rcvif->if_xname);
494 if ((m = m_pullup(m, len)) == NULL) {
495 CARPSTATS_INC(carps_hdrops);
498 ip = mtod(m, struct ip *);
499 ch = (struct carp_header *)((char *)ip + iplen);
501 /* verify the CARP checksum */
503 if (in_cksum(m, len - iplen)) {
504 CARPSTATS_INC(carps_badsum);
505 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
506 m->m_pkthdr.rcvif->if_xname);
512 carp_input_c(m, ch, AF_INET);
518 carp6_input(struct mbuf **mp, int *offp, int proto)
520 struct mbuf *m = *mp;
521 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
522 struct carp_header *ch;
525 CARPSTATS_INC(carps_ipackets6);
529 return (IPPROTO_DONE);
532 /* check if received on a valid carp interface */
533 if (m->m_pkthdr.rcvif->if_carp == NULL) {
534 CARPSTATS_INC(carps_badif);
535 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
536 __func__, m->m_pkthdr.rcvif->if_xname);
538 return (IPPROTO_DONE);
541 /* verify that the IP TTL is 255 */
542 if (ip6->ip6_hlim != CARP_DFLTTL) {
543 CARPSTATS_INC(carps_badttl);
544 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
545 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
547 return (IPPROTO_DONE);
550 /* verify that we have a complete carp packet */
552 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
554 CARPSTATS_INC(carps_badlen);
555 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
556 return (IPPROTO_DONE);
560 /* verify the CARP checksum */
562 if (in_cksum(m, sizeof(*ch))) {
563 CARPSTATS_INC(carps_badsum);
564 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
565 m->m_pkthdr.rcvif->if_xname);
567 return (IPPROTO_DONE);
571 carp_input_c(m, ch, AF_INET6);
572 return (IPPROTO_DONE);
577 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
579 struct ifnet *ifp = m->m_pkthdr.rcvif;
581 struct carp_softc *sc;
582 uint64_t tmp_counter;
583 struct timeval sc_tv, ch_tv;
585 /* verify that the VHID is valid on the receiving interface */
587 IFNET_FOREACH_IFA(ifp, ifa)
588 if (ifa->ifa_addr->sa_family == af &&
589 ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
593 IF_ADDR_RUNLOCK(ifp);
596 CARPSTATS_INC(carps_badvhid);
601 /* verify the CARP version. */
602 if (ch->carp_version != CARP_VERSION) {
603 CARPSTATS_INC(carps_badver);
604 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
615 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
616 CARPSTATS_INC(carps_badauth);
617 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
618 sc->sc_vhid, ifp->if_xname);
622 tmp_counter = ntohl(ch->carp_counter[0]);
623 tmp_counter = tmp_counter<<32;
624 tmp_counter += ntohl(ch->carp_counter[1]);
626 /* XXX Replay protection goes here */
628 sc->sc_init_counter = 0;
629 sc->sc_counter = tmp_counter;
631 sc_tv.tv_sec = sc->sc_advbase;
632 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
633 ch_tv.tv_sec = ch->carp_advbase;
634 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
636 switch (sc->sc_state) {
641 * If we receive an advertisement from a master who's going to
642 * be more frequent than us, go into BACKUP state.
644 if (timevalcmp(&sc_tv, &ch_tv, >) ||
645 timevalcmp(&sc_tv, &ch_tv, ==)) {
646 callout_stop(&sc->sc_ad_tmo);
647 CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
648 "(more frequent advertisement received)\n",
650 sc->sc_carpdev->if_xname);
651 carp_set_state(sc, BACKUP);
658 * If we're pre-empting masters who advertise slower than us,
659 * and this one claims to be slower, treat him as down.
661 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
662 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
663 "(preempting a slower master)\n",
665 sc->sc_carpdev->if_xname);
666 carp_master_down_locked(sc);
671 * If the master is going to advertise at such a low frequency
672 * that he's guaranteed to time out, we'd might as well just
673 * treat him as timed out now.
675 sc_tv.tv_sec = sc->sc_advbase * 3;
676 if (timevalcmp(&sc_tv, &ch_tv, <)) {
677 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
678 "(master timed out)\n",
680 sc->sc_carpdev->if_xname);
681 carp_master_down_locked(sc);
686 * Otherwise, we reset the counter and wait for the next
699 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
703 if (sc->sc_init_counter) {
704 /* this could also be seconds since unix epoch */
705 sc->sc_counter = arc4random();
706 sc->sc_counter = sc->sc_counter << 32;
707 sc->sc_counter += arc4random();
711 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
712 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
714 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
716 /* Tag packet for carp_output */
717 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
718 M_NOWAIT)) == NULL) {
720 CARPSTATS_INC(carps_onomem);
723 bcopy(&sc, mtag + 1, sizeof(sc));
724 m_tag_prepend(m, mtag);
730 * To avoid LORs and possible recursions this function shouldn't
731 * be called directly, but scheduled via taskqueue.
734 carp_send_ad_all(void *ctx __unused, int pending __unused)
736 struct carp_softc *sc;
739 LIST_FOREACH(sc, &carp_list, sc_next)
740 if (sc->sc_state == MASTER) {
742 CURVNET_SET(sc->sc_carpdev->if_vnet);
743 carp_send_ad_locked(sc);
747 mtx_unlock(&carp_mtx);
750 /* Send a periodic advertisement, executed in callout context. */
752 carp_send_ad(void *v)
754 struct carp_softc *sc = v;
756 CARP_LOCK_ASSERT(sc);
757 CURVNET_SET(sc->sc_carpdev->if_vnet);
758 carp_send_ad_locked(sc);
764 carp_send_ad_error(struct carp_softc *sc, int error)
768 if (sc->sc_sendad_errors < INT_MAX)
769 sc->sc_sendad_errors++;
770 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
771 static const char fmt[] = "send error %d on %s";
772 char msg[sizeof(fmt) + IFNAMSIZ];
774 sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
775 carp_demote_adj(V_carp_senderr_adj, msg);
777 sc->sc_sendad_success = 0;
779 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS &&
780 ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
781 static const char fmt[] = "send ok on %s";
782 char msg[sizeof(fmt) + IFNAMSIZ];
784 sprintf(msg, fmt, sc->sc_carpdev->if_xname);
785 carp_demote_adj(-V_carp_senderr_adj, msg);
786 sc->sc_sendad_errors = 0;
788 sc->sc_sendad_errors = 0;
793 carp_send_ad_locked(struct carp_softc *sc)
795 struct carp_header ch;
799 struct carp_header *ch_ptr;
803 CARP_LOCK_ASSERT(sc);
805 advskew = DEMOTE_ADVSKEW(sc);
806 tv.tv_sec = sc->sc_advbase;
807 tv.tv_usec = advskew * 1000000 / 256;
809 ch.carp_version = CARP_VERSION;
810 ch.carp_type = CARP_ADVERTISEMENT;
811 ch.carp_vhid = sc->sc_vhid;
812 ch.carp_advbase = sc->sc_advbase;
813 ch.carp_advskew = advskew;
814 ch.carp_authlen = 7; /* XXX DEFINE */
815 ch.carp_pad1 = 0; /* must be zero */
818 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
824 m = m_gethdr(M_NOWAIT, MT_DATA);
826 CARPSTATS_INC(carps_onomem);
829 len = sizeof(*ip) + sizeof(ch);
830 m->m_pkthdr.len = len;
831 m->m_pkthdr.rcvif = NULL;
833 MH_ALIGN(m, m->m_len);
834 m->m_flags |= M_MCAST;
835 ip = mtod(m, struct ip *);
836 ip->ip_v = IPVERSION;
837 ip->ip_hl = sizeof(*ip) >> 2;
838 ip->ip_tos = IPTOS_LOWDELAY;
839 ip->ip_len = htons(len);
840 ip->ip_id = ip_newid();
841 ip->ip_off = htons(IP_DF);
842 ip->ip_ttl = CARP_DFLTTL;
843 ip->ip_p = IPPROTO_CARP;
846 bzero(&sa, sizeof(sa));
847 sa.sa_family = AF_INET;
848 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
851 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
854 ip->ip_src.s_addr = 0;
855 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
857 ch_ptr = (struct carp_header *)(&ip[1]);
858 bcopy(&ch, ch_ptr, sizeof(ch));
859 if (carp_prepare_ad(m, sc, ch_ptr))
862 m->m_data += sizeof(*ip);
863 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
864 m->m_data -= sizeof(*ip);
866 CARPSTATS_INC(carps_opackets);
868 carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
869 &sc->sc_carpdev->if_carp->cif_imo, NULL));
873 if (sc->sc_naddrs6) {
876 m = m_gethdr(M_NOWAIT, MT_DATA);
878 CARPSTATS_INC(carps_onomem);
881 len = sizeof(*ip6) + sizeof(ch);
882 m->m_pkthdr.len = len;
883 m->m_pkthdr.rcvif = NULL;
885 MH_ALIGN(m, m->m_len);
886 m->m_flags |= M_MCAST;
887 ip6 = mtod(m, struct ip6_hdr *);
888 bzero(ip6, sizeof(*ip6));
889 ip6->ip6_vfc |= IPV6_VERSION;
890 ip6->ip6_hlim = CARP_DFLTTL;
891 ip6->ip6_nxt = IPPROTO_CARP;
892 bzero(&sa, sizeof(sa));
894 /* set the source address */
895 sa.sa_family = AF_INET6;
896 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
898 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
899 sizeof(struct in6_addr));
902 /* This should never happen with IPv6. */
903 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
905 /* Set the multicast destination. */
906 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
907 ip6->ip6_dst.s6_addr8[15] = 0x12;
908 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
910 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
914 ch_ptr = (struct carp_header *)(&ip6[1]);
915 bcopy(&ch, ch_ptr, sizeof(ch));
916 if (carp_prepare_ad(m, sc, ch_ptr))
919 m->m_data += sizeof(*ip6);
920 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
921 m->m_data -= sizeof(*ip6);
923 CARPSTATS_INC(carps_opackets6);
925 carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
926 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
931 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
935 carp_addroute(struct carp_softc *sc)
939 CARP_FOREACH_IFA(sc, ifa)
940 carp_ifa_addroute(ifa);
944 carp_ifa_addroute(struct ifaddr *ifa)
947 switch (ifa->ifa_addr->sa_family) {
950 in_addprefix(ifatoia(ifa), RTF_UP);
951 ifa_add_loopback_route(ifa,
952 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
957 ifa_add_loopback_route(ifa,
958 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
966 carp_delroute(struct carp_softc *sc)
970 CARP_FOREACH_IFA(sc, ifa)
971 carp_ifa_delroute(ifa);
975 carp_ifa_delroute(struct ifaddr *ifa)
978 switch (ifa->ifa_addr->sa_family) {
981 ifa_del_loopback_route(ifa,
982 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
983 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
988 ifa_del_loopback_route(ifa,
989 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
997 carp_master(struct ifaddr *ifa)
999 struct carp_softc *sc = ifa->ifa_carp;
1001 return (sc->sc_state == MASTER);
1006 * Broadcast a gratuitous ARP request containing
1007 * the virtual router MAC address for each IP address
1008 * associated with the virtual router.
1011 carp_send_arp(struct carp_softc *sc)
1015 CARP_FOREACH_IFA(sc, ifa)
1016 if (ifa->ifa_addr->sa_family == AF_INET)
1017 arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
1021 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1023 struct carp_softc *sc = ifa->ifa_carp;
1025 if (sc->sc_state == MASTER) {
1026 *enaddr = LLADDR(&sc->sc_addr);
1036 carp_send_na(struct carp_softc *sc)
1038 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1040 struct in6_addr *in6;
1042 CARP_FOREACH_IFA(sc, ifa) {
1043 if (ifa->ifa_addr->sa_family != AF_INET6)
1047 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1048 ND_NA_FLAG_OVERRIDE, 1, NULL);
1049 DELAY(1000); /* XXX */
1054 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1055 * matches and is not a carp address. Returns NULL otherwise.
1058 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1064 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1065 if (ifa->ifa_addr->sa_family != AF_INET6)
1067 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1069 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1075 IF_ADDR_RUNLOCK(ifp);
1081 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1086 IFNET_FOREACH_IFA(ifp, ifa)
1087 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1088 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1089 struct carp_softc *sc = ifa->ifa_carp;
1092 IF_ADDR_RUNLOCK(ifp);
1094 mtag = m_tag_get(PACKET_TAG_CARP,
1095 sizeof(struct carp_softc *), M_NOWAIT);
1097 /* Better a bit than nothing. */
1098 return (LLADDR(&sc->sc_addr));
1100 bcopy(&sc, mtag + 1, sizeof(sc));
1101 m_tag_prepend(m, mtag);
1103 return (LLADDR(&sc->sc_addr));
1105 IF_ADDR_RUNLOCK(ifp);
1112 carp_forus(struct ifnet *ifp, u_char *dhost)
1114 struct carp_softc *sc;
1115 uint8_t *ena = dhost;
1117 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1120 CIF_LOCK(ifp->if_carp);
1121 IFNET_FOREACH_CARP(ifp, sc) {
1123 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1126 CIF_UNLOCK(ifp->if_carp);
1131 CIF_UNLOCK(ifp->if_carp);
1136 /* Master down timeout event, executed in callout context. */
1138 carp_master_down(void *v)
1140 struct carp_softc *sc = v;
1142 CARP_LOCK_ASSERT(sc);
1144 CURVNET_SET(sc->sc_carpdev->if_vnet);
1145 if (sc->sc_state == BACKUP) {
1146 CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
1148 sc->sc_carpdev->if_xname);
1149 carp_master_down_locked(sc);
1157 carp_master_down_locked(struct carp_softc *sc)
1160 CARP_LOCK_ASSERT(sc);
1162 switch (sc->sc_state) {
1164 carp_set_state(sc, MASTER);
1165 carp_send_ad_locked(sc);
1178 panic("carp: VHID %u@%s: master_down event in %s state\n",
1180 sc->sc_carpdev->if_xname,
1181 sc->sc_state ? "MASTER" : "INIT");
1188 * When in backup state, af indicates whether to reset the master down timer
1189 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1192 carp_setrun(struct carp_softc *sc, sa_family_t af)
1196 CARP_LOCK_ASSERT(sc);
1198 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1199 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1200 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1203 switch (sc->sc_state) {
1205 CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1207 sc->sc_carpdev->if_xname);
1208 carp_set_state(sc, BACKUP);
1212 callout_stop(&sc->sc_ad_tmo);
1213 tv.tv_sec = 3 * sc->sc_advbase;
1214 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1218 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1219 carp_master_down, sc);
1224 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1225 carp_master_down, sc);
1231 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1232 carp_master_down, sc);
1236 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1237 carp_master_down, sc);
1243 tv.tv_sec = sc->sc_advbase;
1244 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1245 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1252 * Setup multicast structures.
1255 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1257 struct ifnet *ifp = cif->cif_ifp;
1260 CIF_LOCK_ASSERT(cif);
1266 struct ip_moptions *imo = &cif->cif_imo;
1267 struct in_addr addr;
1269 if (imo->imo_membership)
1272 imo->imo_membership = (struct in_multi **)malloc(
1273 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1275 if (imo->imo_membership == NULL)
1277 imo->imo_mfilters = NULL;
1278 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1279 imo->imo_multicast_vif = -1;
1281 addr.s_addr = htonl(INADDR_CARP_GROUP);
1282 if ((error = in_joingroup(ifp, &addr, NULL,
1283 &imo->imo_membership[0])) != 0) {
1284 free(imo->imo_membership, M_CARP);
1287 imo->imo_num_memberships++;
1288 imo->imo_multicast_ifp = ifp;
1289 imo->imo_multicast_ttl = CARP_DFLTTL;
1290 imo->imo_multicast_loop = 0;
1297 struct ip6_moptions *im6o = &cif->cif_im6o;
1298 struct in6_addr in6;
1299 struct in6_multi *in6m;
1301 if (im6o->im6o_membership)
1304 im6o->im6o_membership = (struct in6_multi **)malloc(
1305 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1307 if (im6o->im6o_membership == NULL)
1309 im6o->im6o_mfilters = NULL;
1310 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1311 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1312 im6o->im6o_multicast_ifp = ifp;
1314 /* Join IPv6 CARP multicast group. */
1315 bzero(&in6, sizeof(in6));
1316 in6.s6_addr16[0] = htons(0xff02);
1317 in6.s6_addr8[15] = 0x12;
1318 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1319 free(im6o->im6o_membership, M_CARP);
1323 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1324 free(im6o->im6o_membership, M_CARP);
1327 im6o->im6o_membership[0] = in6m;
1328 im6o->im6o_num_memberships++;
1330 /* Join solicited multicast address. */
1331 bzero(&in6, sizeof(in6));
1332 in6.s6_addr16[0] = htons(0xff02);
1333 in6.s6_addr32[1] = 0;
1334 in6.s6_addr32[2] = htonl(1);
1335 in6.s6_addr32[3] = 0;
1336 in6.s6_addr8[12] = 0xff;
1337 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1338 in6_mc_leave(im6o->im6o_membership[0], NULL);
1339 free(im6o->im6o_membership, M_CARP);
1343 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1344 in6_mc_leave(im6o->im6o_membership[0], NULL);
1345 free(im6o->im6o_membership, M_CARP);
1348 im6o->im6o_membership[1] = in6m;
1349 im6o->im6o_num_memberships++;
1359 * Free multicast structures.
1362 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1365 CIF_LOCK_ASSERT(cif);
1369 if (cif->cif_naddrs == 0) {
1370 struct ip_moptions *imo = &cif->cif_imo;
1372 in_leavegroup(imo->imo_membership[0], NULL);
1373 KASSERT(imo->imo_mfilters == NULL,
1374 ("%s: imo_mfilters != NULL", __func__));
1375 free(imo->imo_membership, M_CARP);
1376 imo->imo_membership = NULL;
1383 if (cif->cif_naddrs6 == 0) {
1384 struct ip6_moptions *im6o = &cif->cif_im6o;
1386 in6_mc_leave(im6o->im6o_membership[0], NULL);
1387 in6_mc_leave(im6o->im6o_membership[1], NULL);
1388 KASSERT(im6o->im6o_mfilters == NULL,
1389 ("%s: im6o_mfilters != NULL", __func__));
1390 free(im6o->im6o_membership, M_CARP);
1391 im6o->im6o_membership = NULL;
1399 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1402 struct carp_softc *sc;
1407 switch (sa->sa_family) {
1420 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1424 bcopy(mtag + 1, &sc, sizeof(sc));
1426 /* Set the source MAC address to the Virtual Router MAC Address. */
1427 switch (ifp->if_type) {
1431 struct ether_header *eh;
1433 eh = mtod(m, struct ether_header *);
1434 eh->ether_shost[0] = 0;
1435 eh->ether_shost[1] = 0;
1436 eh->ether_shost[2] = 0x5e;
1437 eh->ether_shost[3] = 0;
1438 eh->ether_shost[4] = 1;
1439 eh->ether_shost[5] = sc->sc_vhid;
1443 struct fddi_header *fh;
1445 fh = mtod(m, struct fddi_header *);
1446 fh->fddi_shost[0] = 0;
1447 fh->fddi_shost[1] = 0;
1448 fh->fddi_shost[2] = 0x5e;
1449 fh->fddi_shost[3] = 0;
1450 fh->fddi_shost[4] = 1;
1451 fh->fddi_shost[5] = sc->sc_vhid;
1454 case IFT_ISO88025: {
1455 struct iso88025_header *th;
1456 th = mtod(m, struct iso88025_header *);
1457 th->iso88025_shost[0] = 3;
1458 th->iso88025_shost[1] = 0;
1459 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1460 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1461 th->iso88025_shost[4] = 0;
1462 th->iso88025_shost[5] = 0;
1466 printf("%s: carp is not supported for the %d interface type\n",
1467 ifp->if_xname, ifp->if_type);
1468 return (EOPNOTSUPP);
1474 static struct carp_softc*
1475 carp_alloc(struct ifnet *ifp)
1477 struct carp_softc *sc;
1478 struct carp_if *cif;
1480 if ((cif = ifp->if_carp) == NULL) {
1481 cif = carp_alloc_if(ifp);
1486 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1488 sc->sc_advbase = CARP_DFLTINTV;
1489 sc->sc_vhid = -1; /* required setting */
1490 sc->sc_init_counter = 1;
1491 sc->sc_state = INIT;
1493 sc->sc_ifasiz = sizeof(struct ifaddr *);
1494 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1495 sc->sc_carpdev = ifp;
1499 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1502 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1504 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1507 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1510 mtx_lock(&carp_mtx);
1511 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1512 mtx_unlock(&carp_mtx);
1518 carp_grow_ifas(struct carp_softc *sc)
1520 struct ifaddr **new;
1522 CARP_LOCK_ASSERT(sc);
1524 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1527 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1528 free(sc->sc_ifas, M_CARP);
1536 carp_destroy(struct carp_softc *sc)
1538 struct ifnet *ifp = sc->sc_carpdev;
1539 struct carp_if *cif = ifp->if_carp;
1541 CIF_LOCK_ASSERT(cif);
1543 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1545 mtx_lock(&carp_mtx);
1546 LIST_REMOVE(sc, sc_next);
1547 mtx_unlock(&carp_mtx);
1550 if (sc->sc_suppress)
1551 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1552 callout_drain(&sc->sc_ad_tmo);
1554 callout_drain(&sc->sc_md_tmo);
1557 callout_drain(&sc->sc_md6_tmo);
1559 CARP_LOCK_DESTROY(sc);
1561 free(sc->sc_ifas, M_CARP);
1565 static struct carp_if*
1566 carp_alloc_if(struct ifnet *ifp)
1568 struct carp_if *cif;
1570 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1572 if (ifpromisc(ifp, 1) != 0)
1577 TAILQ_INIT(&cif->cif_vrs);
1582 IF_ADDR_WUNLOCK(ifp);
1593 carp_free_if(struct carp_if *cif)
1595 struct ifnet *ifp = cif->cif_ifp;
1597 CIF_LOCK_ASSERT(cif);
1598 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1602 ifp->if_carp = NULL;
1603 IF_ADDR_WUNLOCK(ifp);
1605 CIF_LOCK_DESTROY(cif);
1614 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1618 carpr->carpr_state = sc->sc_state;
1619 carpr->carpr_vhid = sc->sc_vhid;
1620 carpr->carpr_advbase = sc->sc_advbase;
1621 carpr->carpr_advskew = sc->sc_advskew;
1623 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1625 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1630 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1632 struct carpreq carpr;
1634 struct carp_softc *sc = NULL;
1635 int error = 0, locked = 0;
1637 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1640 ifp = ifunit_ref(ifr->ifr_name);
1644 switch (ifp->if_type) {
1656 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1657 error = EADDRNOTAVAIL;
1663 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1665 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1666 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1672 CIF_LOCK(ifp->if_carp);
1673 IFNET_FOREACH_CARP(ifp, sc)
1674 if (sc->sc_vhid == carpr.carpr_vhid)
1676 CIF_UNLOCK(ifp->if_carp);
1679 sc = carp_alloc(ifp);
1681 error = EINVAL; /* XXX: ifpromisc failed */
1686 sc->sc_vhid = carpr.carpr_vhid;
1687 LLADDR(&sc->sc_addr)[0] = 0;
1688 LLADDR(&sc->sc_addr)[1] = 0;
1689 LLADDR(&sc->sc_addr)[2] = 0x5e;
1690 LLADDR(&sc->sc_addr)[3] = 0;
1691 LLADDR(&sc->sc_addr)[4] = 1;
1692 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1696 if (carpr.carpr_advbase > 0) {
1697 if (carpr.carpr_advbase > 255 ||
1698 carpr.carpr_advbase < CARP_DFLTINTV) {
1702 sc->sc_advbase = carpr.carpr_advbase;
1704 if (carpr.carpr_advskew >= 255) {
1708 sc->sc_advskew = carpr.carpr_advskew;
1709 if (carpr.carpr_key[0] != '\0') {
1710 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1711 carp_hmac_prepare(sc);
1713 if (sc->sc_state != INIT &&
1714 carpr.carpr_state != sc->sc_state) {
1715 switch (carpr.carpr_state) {
1717 callout_stop(&sc->sc_ad_tmo);
1718 carp_set_state(sc, BACKUP);
1723 carp_master_down_locked(sc);
1735 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1739 if (carpr.carpr_count < 1) {
1743 if (ifp->if_carp == NULL) {
1748 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1749 if (carpr.carpr_vhid != 0) {
1750 CIF_LOCK(ifp->if_carp);
1751 IFNET_FOREACH_CARP(ifp, sc)
1752 if (sc->sc_vhid == carpr.carpr_vhid)
1754 CIF_UNLOCK(ifp->if_carp);
1759 carp_carprcp(&carpr, sc, priveleged);
1760 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1765 CIF_LOCK(ifp->if_carp);
1766 IFNET_FOREACH_CARP(ifp, sc)
1769 if (count > carpr.carpr_count) {
1770 CIF_UNLOCK(ifp->if_carp);
1776 IFNET_FOREACH_CARP(ifp, sc) {
1777 carp_carprcp(&carpr, sc, priveleged);
1778 carpr.carpr_count = count;
1779 error = copyout(&carpr, ifr->ifr_data +
1780 (i * sizeof(carpr)), sizeof(carpr));
1782 CIF_UNLOCK(ifp->if_carp);
1787 CIF_UNLOCK(ifp->if_carp);
1804 carp_get_vhid(struct ifaddr *ifa)
1807 if (ifa == NULL || ifa->ifa_carp == NULL)
1810 return (ifa->ifa_carp->sc_vhid);
1814 carp_attach(struct ifaddr *ifa, int vhid)
1816 struct ifnet *ifp = ifa->ifa_ifp;
1817 struct carp_if *cif = ifp->if_carp;
1818 struct carp_softc *sc;
1821 if (ifp->if_carp == NULL)
1822 return (ENOPROTOOPT);
1824 switch (ifa->ifa_addr->sa_family) {
1833 return (EPROTOTYPE);
1837 IFNET_FOREACH_CARP(ifp, sc)
1838 if (sc->sc_vhid == vhid)
1845 if (ifa->ifa_carp) {
1846 if (ifa->ifa_carp->sc_vhid != vhid)
1847 carp_detach_locked(ifa);
1854 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1861 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1862 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1863 if ((error = carp_grow_ifas(sc)) != 0) {
1864 carp_multicast_cleanup(cif,
1865 ifa->ifa_addr->sa_family);
1871 switch (ifa->ifa_addr->sa_family) {
1887 sc->sc_ifas[index - 1] = ifa;
1890 carp_hmac_prepare(sc);
1900 carp_detach(struct ifaddr *ifa)
1902 struct ifnet *ifp = ifa->ifa_ifp;
1903 struct carp_if *cif = ifp->if_carp;
1906 carp_detach_locked(ifa);
1911 carp_detach_locked(struct ifaddr *ifa)
1913 struct ifnet *ifp = ifa->ifa_ifp;
1914 struct carp_if *cif = ifp->if_carp;
1915 struct carp_softc *sc = ifa->ifa_carp;
1918 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1920 CIF_LOCK_ASSERT(cif);
1924 index = sc->sc_naddrs + sc->sc_naddrs6;
1925 for (i = 0; i < index; i++)
1926 if (sc->sc_ifas[i] == ifa)
1928 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1929 for (; i < index - 1; i++)
1930 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1931 sc->sc_ifas[index - 1] = NULL;
1933 switch (ifa->ifa_addr->sa_family) {
1948 carp_ifa_delroute(ifa);
1949 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1951 ifa->ifa_carp = NULL;
1954 carp_hmac_prepare(sc);
1957 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1965 carp_set_state(struct carp_softc *sc, int state)
1968 CARP_LOCK_ASSERT(sc);
1970 if (sc->sc_state != state) {
1971 const char *carp_states[] = { CARP_STATES };
1972 char subsys[IFNAMSIZ+5];
1974 sc->sc_state = state;
1976 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1977 sc->sc_carpdev->if_xname);
1978 devctl_notify("CARP", subsys, carp_states[state], NULL);
1983 carp_linkstate(struct ifnet *ifp)
1985 struct carp_softc *sc;
1987 CIF_LOCK(ifp->if_carp);
1988 IFNET_FOREACH_CARP(ifp, sc) {
1993 CIF_UNLOCK(ifp->if_carp);
1997 carp_sc_state(struct carp_softc *sc)
2000 CARP_LOCK_ASSERT(sc);
2002 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2003 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2004 callout_stop(&sc->sc_ad_tmo);
2006 callout_stop(&sc->sc_md_tmo);
2009 callout_stop(&sc->sc_md6_tmo);
2011 carp_set_state(sc, INIT);
2013 if (!sc->sc_suppress)
2014 carp_demote_adj(V_carp_ifdown_adj, "interface down");
2015 sc->sc_suppress = 1;
2017 carp_set_state(sc, INIT);
2019 if (sc->sc_suppress)
2020 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2021 sc->sc_suppress = 0;
2026 carp_demote_adj(int adj, char *reason)
2028 atomic_add_int(&V_carp_demotion, adj);
2029 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2030 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2034 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2038 new = V_carp_demotion;
2039 error = sysctl_handle_int(oidp, &new, 0, req);
2040 if (error || !req->newptr)
2043 carp_demote_adj(new, "sysctl");
2049 extern struct domain inetdomain;
2050 static struct protosw in_carp_protosw = {
2051 .pr_type = SOCK_RAW,
2052 .pr_domain = &inetdomain,
2053 .pr_protocol = IPPROTO_CARP,
2054 .pr_flags = PR_ATOMIC|PR_ADDR,
2055 .pr_input = carp_input,
2056 .pr_output = (pr_output_t *)rip_output,
2057 .pr_ctloutput = rip_ctloutput,
2058 .pr_usrreqs = &rip_usrreqs
2063 extern struct domain inet6domain;
2064 static struct ip6protosw in6_carp_protosw = {
2065 .pr_type = SOCK_RAW,
2066 .pr_domain = &inet6domain,
2067 .pr_protocol = IPPROTO_CARP,
2068 .pr_flags = PR_ATOMIC|PR_ADDR,
2069 .pr_input = carp6_input,
2070 .pr_output = rip6_output,
2071 .pr_ctloutput = rip6_ctloutput,
2072 .pr_usrreqs = &rip6_usrreqs
2077 carp_mod_cleanup(void)
2081 if (proto_reg[CARP_INET] == 0) {
2082 (void)ipproto_unregister(IPPROTO_CARP);
2083 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2084 proto_reg[CARP_INET] = -1;
2086 carp_iamatch_p = NULL;
2089 if (proto_reg[CARP_INET6] == 0) {
2090 (void)ip6proto_unregister(IPPROTO_CARP);
2091 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2092 proto_reg[CARP_INET6] = -1;
2094 carp_iamatch6_p = NULL;
2095 carp_macmatch6_p = NULL;
2097 carp_ioctl_p = NULL;
2098 carp_attach_p = NULL;
2099 carp_detach_p = NULL;
2100 carp_get_vhid_p = NULL;
2101 carp_linkstate_p = NULL;
2102 carp_forus_p = NULL;
2103 carp_output_p = NULL;
2104 carp_demote_adj_p = NULL;
2105 carp_master_p = NULL;
2106 mtx_unlock(&carp_mtx);
2107 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2108 mtx_destroy(&carp_mtx);
2116 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2117 LIST_INIT(&carp_list);
2118 carp_get_vhid_p = carp_get_vhid;
2119 carp_forus_p = carp_forus;
2120 carp_output_p = carp_output;
2121 carp_linkstate_p = carp_linkstate;
2122 carp_ioctl_p = carp_ioctl;
2123 carp_attach_p = carp_attach;
2124 carp_detach_p = carp_detach;
2125 carp_demote_adj_p = carp_demote_adj;
2126 carp_master_p = carp_master;
2128 carp_iamatch6_p = carp_iamatch6;
2129 carp_macmatch6_p = carp_macmatch6;
2130 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2131 (struct protosw *)&in6_carp_protosw);
2132 if (proto_reg[CARP_INET6]) {
2133 printf("carp: error %d attaching to PF_INET6\n",
2134 proto_reg[CARP_INET6]);
2136 return (proto_reg[CARP_INET6]);
2138 err = ip6proto_register(IPPROTO_CARP);
2140 printf("carp: error %d registering with INET6\n", err);
2146 carp_iamatch_p = carp_iamatch;
2147 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2148 if (proto_reg[CARP_INET]) {
2149 printf("carp: error %d attaching to PF_INET\n",
2150 proto_reg[CARP_INET]);
2152 return (proto_reg[CARP_INET]);
2154 err = ipproto_register(IPPROTO_CARP);
2156 printf("carp: error %d registering with INET\n", err);
2165 carp_modevent(module_t mod, int type, void *data)
2169 return carp_mod_load();
2172 mtx_lock(&carp_mtx);
2173 if (LIST_EMPTY(&carp_list))
2176 mtx_unlock(&carp_mtx);
2188 static moduledata_t carp_mod = {
2194 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);