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 */
195 static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
197 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP");
198 SYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW, &carp_allow, 0,
199 "Accept incoming CARP packets");
200 SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW, &carp_preempt, 0,
201 "High-priority backup preemption mode");
202 SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW, &carp_log, 0,
204 SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion, CTLTYPE_INT|CTLFLAG_RW,
205 0, 0, carp_demote_adj_sysctl, "I",
206 "Adjust demotion factor (skew of advskew)");
207 SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW,
208 &carp_senderr_adj, 0, "Send error demotion factor adjustment");
209 SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW,
210 &carp_ifdown_adj, 0, "Interface down demotion factor adjustment");
212 static struct carpstats carpstats;
213 SYSCTL_STRUCT(_net_inet_carp, OID_AUTO, stats, CTLFLAG_RW, &carpstats,
214 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
216 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
218 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
219 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
220 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
221 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
222 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
224 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
225 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
226 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
227 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
228 #define CIF_FREE(cif) do { \
229 CIF_LOCK_ASSERT(cif); \
230 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
236 #define CARP_LOG(...) do { \
238 log(LOG_INFO, "carp: " __VA_ARGS__); \
241 #define CARP_DEBUG(...) do { \
243 log(LOG_DEBUG, __VA_ARGS__); \
246 #define IFNET_FOREACH_IFA(ifp, ifa) \
247 IF_ADDR_LOCK_ASSERT(ifp); \
248 TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
249 if ((ifa)->ifa_carp != NULL)
251 #define CARP_FOREACH_IFA(sc, ifa) \
252 CARP_LOCK_ASSERT(sc); \
254 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
255 ((ifa) = sc->sc_ifas[_i]) != NULL; \
258 #define IFNET_FOREACH_CARP(ifp, sc) \
259 CIF_LOCK_ASSERT(ifp->if_carp); \
260 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
262 #define DEMOTE_ADVSKEW(sc) \
263 (((sc)->sc_advskew + carp_demotion > CARP_MAXSKEW) ? \
264 CARP_MAXSKEW : ((sc)->sc_advskew + carp_demotion))
266 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
267 static struct carp_softc
268 *carp_alloc(struct ifnet *);
269 static void carp_detach_locked(struct ifaddr *);
270 static void carp_destroy(struct carp_softc *);
271 static struct carp_if
272 *carp_alloc_if(struct ifnet *);
273 static void carp_free_if(struct carp_if *);
274 static void carp_set_state(struct carp_softc *, int);
275 static void carp_sc_state(struct carp_softc *);
276 static void carp_setrun(struct carp_softc *, sa_family_t);
277 static void carp_master_down(void *);
278 static void carp_master_down_locked(struct carp_softc *);
279 static void carp_send_ad(void *);
280 static void carp_send_ad_locked(struct carp_softc *);
281 static void carp_addroute(struct carp_softc *);
282 static void carp_ifa_addroute(struct ifaddr *);
283 static void carp_delroute(struct carp_softc *);
284 static void carp_ifa_delroute(struct ifaddr *);
285 static void carp_send_ad_all(void *, int);
286 static void carp_demote_adj(int, char *);
288 static LIST_HEAD(, carp_softc) carp_list;
289 static struct mtx carp_mtx;
290 static struct task carp_sendall_task =
291 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
294 carp_hmac_prepare(struct carp_softc *sc)
296 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
297 uint8_t vhid = sc->sc_vhid & 0xff;
301 struct in_addr last, cur, in;
304 struct in6_addr last6, cur6, in6;
307 CARP_LOCK_ASSERT(sc);
309 /* Compute ipad from key. */
310 bzero(sc->sc_pad, sizeof(sc->sc_pad));
311 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
312 for (i = 0; i < sizeof(sc->sc_pad); i++)
313 sc->sc_pad[i] ^= 0x36;
315 /* Precompute first part of inner hash. */
316 SHA1Init(&sc->sc_sha1);
317 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
318 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
319 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
320 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
326 cur.s_addr = 0xffffffff;
327 CARP_FOREACH_IFA(sc, ifa) {
328 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
329 if (ifa->ifa_addr->sa_family == AF_INET &&
330 ntohl(in.s_addr) > ntohl(last.s_addr) &&
331 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
332 cur.s_addr = in.s_addr;
337 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
341 memset(&cur6, 0, sizeof(cur6));
345 memset(&cur6, 0xff, sizeof(cur6));
346 CARP_FOREACH_IFA(sc, ifa) {
347 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
348 if (IN6_IS_SCOPE_EMBED(&in6))
349 in6.s6_addr16[1] = 0;
350 if (ifa->ifa_addr->sa_family == AF_INET6 &&
351 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
352 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
358 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
362 /* convert ipad to opad */
363 for (i = 0; i < sizeof(sc->sc_pad); i++)
364 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
368 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
369 unsigned char md[20])
373 CARP_LOCK_ASSERT(sc);
375 /* fetch first half of inner hash */
376 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
378 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
379 SHA1Final(md, &sha1ctx);
383 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
384 SHA1Update(&sha1ctx, md, 20);
385 SHA1Final(md, &sha1ctx);
389 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
390 unsigned char md[20])
392 unsigned char md2[20];
394 CARP_LOCK_ASSERT(sc);
396 carp_hmac_generate(sc, counter, md2);
398 return (bcmp(md, md2, sizeof(md2)));
402 * process input packet.
403 * we have rearranged checks order compared to the rfc,
404 * but it seems more efficient this way or not possible otherwise.
408 carp_input(struct mbuf *m, int hlen)
410 struct ip *ip = mtod(m, struct ip *);
411 struct carp_header *ch;
414 CARPSTATS_INC(carps_ipackets);
421 /* verify that the IP TTL is 255. */
422 if (ip->ip_ttl != CARP_DFLTTL) {
423 CARPSTATS_INC(carps_badttl);
424 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
426 m->m_pkthdr.rcvif->if_xname);
431 iplen = ip->ip_hl << 2;
433 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
434 CARPSTATS_INC(carps_badlen);
435 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
436 "on %s\n", __func__, m->m_len - sizeof(struct ip),
437 m->m_pkthdr.rcvif->if_xname);
442 if (iplen + sizeof(*ch) < m->m_len) {
443 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
444 CARPSTATS_INC(carps_hdrops);
445 CARP_DEBUG("%s: pullup failed\n", __func__);
448 ip = mtod(m, struct ip *);
450 ch = (struct carp_header *)((char *)ip + iplen);
453 * verify that the received packet length is
454 * equal to the CARP header
456 len = iplen + sizeof(*ch);
457 if (len > m->m_pkthdr.len) {
458 CARPSTATS_INC(carps_badlen);
459 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
461 m->m_pkthdr.rcvif->if_xname);
466 if ((m = m_pullup(m, len)) == NULL) {
467 CARPSTATS_INC(carps_hdrops);
470 ip = mtod(m, struct ip *);
471 ch = (struct carp_header *)((char *)ip + iplen);
473 /* verify the CARP checksum */
475 if (in_cksum(m, len - iplen)) {
476 CARPSTATS_INC(carps_badsum);
477 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
478 m->m_pkthdr.rcvif->if_xname);
484 carp_input_c(m, ch, AF_INET);
490 carp6_input(struct mbuf **mp, int *offp, int proto)
492 struct mbuf *m = *mp;
493 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
494 struct carp_header *ch;
497 CARPSTATS_INC(carps_ipackets6);
501 return (IPPROTO_DONE);
504 /* check if received on a valid carp interface */
505 if (m->m_pkthdr.rcvif->if_carp == NULL) {
506 CARPSTATS_INC(carps_badif);
507 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
508 __func__, m->m_pkthdr.rcvif->if_xname);
510 return (IPPROTO_DONE);
513 /* verify that the IP TTL is 255 */
514 if (ip6->ip6_hlim != CARP_DFLTTL) {
515 CARPSTATS_INC(carps_badttl);
516 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
517 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
519 return (IPPROTO_DONE);
522 /* verify that we have a complete carp packet */
524 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
526 CARPSTATS_INC(carps_badlen);
527 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
528 return (IPPROTO_DONE);
532 /* verify the CARP checksum */
534 if (in_cksum(m, sizeof(*ch))) {
535 CARPSTATS_INC(carps_badsum);
536 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
537 m->m_pkthdr.rcvif->if_xname);
539 return (IPPROTO_DONE);
543 carp_input_c(m, ch, AF_INET6);
544 return (IPPROTO_DONE);
549 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
551 struct ifnet *ifp = m->m_pkthdr.rcvif;
553 struct carp_softc *sc;
554 uint64_t tmp_counter;
555 struct timeval sc_tv, ch_tv;
557 /* verify that the VHID is valid on the receiving interface */
559 IFNET_FOREACH_IFA(ifp, ifa)
560 if (ifa->ifa_addr->sa_family == af &&
561 ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
565 IF_ADDR_RUNLOCK(ifp);
568 CARPSTATS_INC(carps_badvhid);
573 /* verify the CARP version. */
574 if (ch->carp_version != CARP_VERSION) {
575 CARPSTATS_INC(carps_badver);
576 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
587 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
588 CARPSTATS_INC(carps_badauth);
589 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
590 sc->sc_vhid, ifp->if_xname);
594 tmp_counter = ntohl(ch->carp_counter[0]);
595 tmp_counter = tmp_counter<<32;
596 tmp_counter += ntohl(ch->carp_counter[1]);
598 /* XXX Replay protection goes here */
600 sc->sc_init_counter = 0;
601 sc->sc_counter = tmp_counter;
603 sc_tv.tv_sec = sc->sc_advbase;
604 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
605 ch_tv.tv_sec = ch->carp_advbase;
606 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
608 switch (sc->sc_state) {
613 * If we receive an advertisement from a master who's going to
614 * be more frequent than us, go into BACKUP state.
616 if (timevalcmp(&sc_tv, &ch_tv, >) ||
617 timevalcmp(&sc_tv, &ch_tv, ==)) {
618 callout_stop(&sc->sc_ad_tmo);
619 CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
620 "(more frequent advertisement received)\n",
622 sc->sc_carpdev->if_xname);
623 carp_set_state(sc, BACKUP);
630 * If we're pre-empting masters who advertise slower than us,
631 * and this one claims to be slower, treat him as down.
633 if (carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
634 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
635 "(preempting a slower master)\n",
637 sc->sc_carpdev->if_xname);
638 carp_master_down_locked(sc);
643 * If the master is going to advertise at such a low frequency
644 * that he's guaranteed to time out, we'd might as well just
645 * treat him as timed out now.
647 sc_tv.tv_sec = sc->sc_advbase * 3;
648 if (timevalcmp(&sc_tv, &ch_tv, <)) {
649 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
650 "(master timed out)\n",
652 sc->sc_carpdev->if_xname);
653 carp_master_down_locked(sc);
658 * Otherwise, we reset the counter and wait for the next
671 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
675 if (sc->sc_init_counter) {
676 /* this could also be seconds since unix epoch */
677 sc->sc_counter = arc4random();
678 sc->sc_counter = sc->sc_counter << 32;
679 sc->sc_counter += arc4random();
683 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
684 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
686 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
688 /* Tag packet for carp_output */
689 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
690 M_NOWAIT)) == NULL) {
692 CARPSTATS_INC(carps_onomem);
695 bcopy(&sc, mtag + 1, sizeof(sc));
696 m_tag_prepend(m, mtag);
702 * To avoid LORs and possible recursions this function shouldn't
703 * be called directly, but scheduled via taskqueue.
706 carp_send_ad_all(void *ctx __unused, int pending __unused)
708 struct carp_softc *sc;
711 LIST_FOREACH(sc, &carp_list, sc_next)
712 if (sc->sc_state == MASTER) {
714 CURVNET_SET(sc->sc_carpdev->if_vnet);
715 carp_send_ad_locked(sc);
719 mtx_unlock(&carp_mtx);
722 /* Send a periodic advertisement, executed in callout context. */
724 carp_send_ad(void *v)
726 struct carp_softc *sc = v;
728 CARP_LOCK_ASSERT(sc);
729 CURVNET_SET(sc->sc_carpdev->if_vnet);
730 carp_send_ad_locked(sc);
736 carp_send_ad_locked(struct carp_softc *sc)
738 struct carp_header ch;
742 struct carp_header *ch_ptr;
746 CARP_LOCK_ASSERT(sc);
748 advskew = DEMOTE_ADVSKEW(sc);
749 tv.tv_sec = sc->sc_advbase;
750 tv.tv_usec = advskew * 1000000 / 256;
752 ch.carp_version = CARP_VERSION;
753 ch.carp_type = CARP_ADVERTISEMENT;
754 ch.carp_vhid = sc->sc_vhid;
755 ch.carp_advbase = sc->sc_advbase;
756 ch.carp_advskew = advskew;
757 ch.carp_authlen = 7; /* XXX DEFINE */
758 ch.carp_pad1 = 0; /* must be zero */
761 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
767 m = m_gethdr(M_NOWAIT, MT_DATA);
769 CARPSTATS_INC(carps_onomem);
772 len = sizeof(*ip) + sizeof(ch);
773 m->m_pkthdr.len = len;
774 m->m_pkthdr.rcvif = NULL;
776 MH_ALIGN(m, m->m_len);
777 m->m_flags |= M_MCAST;
778 ip = mtod(m, struct ip *);
779 ip->ip_v = IPVERSION;
780 ip->ip_hl = sizeof(*ip) >> 2;
781 ip->ip_tos = IPTOS_LOWDELAY;
782 ip->ip_len = htons(len);
783 ip->ip_id = ip_newid();
784 ip->ip_off = htons(IP_DF);
785 ip->ip_ttl = CARP_DFLTTL;
786 ip->ip_p = IPPROTO_CARP;
789 bzero(&sa, sizeof(sa));
790 sa.sa_family = AF_INET;
791 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
794 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
797 ip->ip_src.s_addr = 0;
798 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
800 ch_ptr = (struct carp_header *)(&ip[1]);
801 bcopy(&ch, ch_ptr, sizeof(ch));
802 if (carp_prepare_ad(m, sc, ch_ptr))
805 m->m_data += sizeof(*ip);
806 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
807 m->m_data -= sizeof(*ip);
809 CARPSTATS_INC(carps_opackets);
811 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
812 &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
813 if (sc->sc_sendad_errors < INT_MAX)
814 sc->sc_sendad_errors++;
815 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
816 carp_demote_adj(carp_senderr_adj, "send error");
817 sc->sc_sendad_success = 0;
819 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
820 if (++sc->sc_sendad_success >=
821 CARP_SENDAD_MIN_SUCCESS) {
822 carp_demote_adj(-carp_senderr_adj,
824 sc->sc_sendad_errors = 0;
827 sc->sc_sendad_errors = 0;
832 if (sc->sc_naddrs6) {
835 m = m_gethdr(M_NOWAIT, MT_DATA);
837 CARPSTATS_INC(carps_onomem);
840 len = sizeof(*ip6) + sizeof(ch);
841 m->m_pkthdr.len = len;
842 m->m_pkthdr.rcvif = NULL;
844 MH_ALIGN(m, m->m_len);
845 m->m_flags |= M_MCAST;
846 ip6 = mtod(m, struct ip6_hdr *);
847 bzero(ip6, sizeof(*ip6));
848 ip6->ip6_vfc |= IPV6_VERSION;
849 ip6->ip6_hlim = CARP_DFLTTL;
850 ip6->ip6_nxt = IPPROTO_CARP;
851 bzero(&sa, sizeof(sa));
853 /* set the source address */
854 sa.sa_family = AF_INET6;
855 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
857 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
858 sizeof(struct in6_addr));
861 /* This should never happen with IPv6. */
862 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
864 /* Set the multicast destination. */
865 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
866 ip6->ip6_dst.s6_addr8[15] = 0x12;
867 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
869 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
873 ch_ptr = (struct carp_header *)(&ip6[1]);
874 bcopy(&ch, ch_ptr, sizeof(ch));
875 if (carp_prepare_ad(m, sc, ch_ptr))
878 m->m_data += sizeof(*ip6);
879 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
880 m->m_data -= sizeof(*ip6);
882 CARPSTATS_INC(carps_opackets6);
884 if (ip6_output(m, NULL, NULL, 0,
885 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
886 if (sc->sc_sendad_errors < INT_MAX)
887 sc->sc_sendad_errors++;
888 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
889 carp_demote_adj(carp_senderr_adj,
891 sc->sc_sendad_success = 0;
893 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
894 if (++sc->sc_sendad_success >=
895 CARP_SENDAD_MIN_SUCCESS) {
896 carp_demote_adj(-carp_senderr_adj,
898 sc->sc_sendad_errors = 0;
901 sc->sc_sendad_errors = 0;
907 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
911 carp_addroute(struct carp_softc *sc)
915 CARP_FOREACH_IFA(sc, ifa)
916 carp_ifa_addroute(ifa);
920 carp_ifa_addroute(struct ifaddr *ifa)
923 switch (ifa->ifa_addr->sa_family) {
926 in_addprefix(ifatoia(ifa), RTF_UP);
927 ifa_add_loopback_route(ifa,
928 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
933 ifa_add_loopback_route(ifa,
934 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
942 carp_delroute(struct carp_softc *sc)
946 CARP_FOREACH_IFA(sc, ifa)
947 carp_ifa_delroute(ifa);
951 carp_ifa_delroute(struct ifaddr *ifa)
954 switch (ifa->ifa_addr->sa_family) {
957 ifa_del_loopback_route(ifa,
958 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
959 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
964 ifa_del_loopback_route(ifa,
965 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
973 carp_master(struct ifaddr *ifa)
975 struct carp_softc *sc = ifa->ifa_carp;
977 return (sc->sc_state == MASTER);
982 * Broadcast a gratuitous ARP request containing
983 * the virtual router MAC address for each IP address
984 * associated with the virtual router.
987 carp_send_arp(struct carp_softc *sc)
991 CARP_FOREACH_IFA(sc, ifa)
992 if (ifa->ifa_addr->sa_family == AF_INET)
993 arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
997 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
999 struct carp_softc *sc = ifa->ifa_carp;
1001 if (sc->sc_state == MASTER) {
1002 *enaddr = LLADDR(&sc->sc_addr);
1012 carp_send_na(struct carp_softc *sc)
1014 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1016 struct in6_addr *in6;
1018 CARP_FOREACH_IFA(sc, ifa) {
1019 if (ifa->ifa_addr->sa_family != AF_INET6)
1023 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1024 ND_NA_FLAG_OVERRIDE, 1, NULL);
1025 DELAY(1000); /* XXX */
1030 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1031 * matches and is not a carp address. Returns NULL otherwise.
1034 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1040 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1041 if (ifa->ifa_addr->sa_family != AF_INET6)
1043 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1045 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1051 IF_ADDR_RUNLOCK(ifp);
1057 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1062 IFNET_FOREACH_IFA(ifp, ifa)
1063 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1064 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1065 struct carp_softc *sc = ifa->ifa_carp;
1068 IF_ADDR_RUNLOCK(ifp);
1070 mtag = m_tag_get(PACKET_TAG_CARP,
1071 sizeof(struct carp_softc *), M_NOWAIT);
1073 /* Better a bit than nothing. */
1074 return (LLADDR(&sc->sc_addr));
1076 bcopy(&sc, mtag + 1, sizeof(sc));
1077 m_tag_prepend(m, mtag);
1079 return (LLADDR(&sc->sc_addr));
1081 IF_ADDR_RUNLOCK(ifp);
1088 carp_forus(struct ifnet *ifp, u_char *dhost)
1090 struct carp_softc *sc;
1091 uint8_t *ena = dhost;
1093 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1096 CIF_LOCK(ifp->if_carp);
1097 IFNET_FOREACH_CARP(ifp, sc) {
1099 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1102 CIF_UNLOCK(ifp->if_carp);
1107 CIF_UNLOCK(ifp->if_carp);
1112 /* Master down timeout event, executed in callout context. */
1114 carp_master_down(void *v)
1116 struct carp_softc *sc = v;
1118 CARP_LOCK_ASSERT(sc);
1120 CURVNET_SET(sc->sc_carpdev->if_vnet);
1121 if (sc->sc_state == BACKUP) {
1122 CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
1124 sc->sc_carpdev->if_xname);
1125 carp_master_down_locked(sc);
1133 carp_master_down_locked(struct carp_softc *sc)
1136 CARP_LOCK_ASSERT(sc);
1138 switch (sc->sc_state) {
1140 carp_set_state(sc, MASTER);
1141 carp_send_ad_locked(sc);
1154 panic("carp: VHID %u@%s: master_down event in %s state\n",
1156 sc->sc_carpdev->if_xname,
1157 sc->sc_state ? "MASTER" : "INIT");
1164 * When in backup state, af indicates whether to reset the master down timer
1165 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1168 carp_setrun(struct carp_softc *sc, sa_family_t af)
1172 CARP_LOCK_ASSERT(sc);
1174 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1175 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1176 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1179 switch (sc->sc_state) {
1181 CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1183 sc->sc_carpdev->if_xname);
1184 carp_set_state(sc, BACKUP);
1188 callout_stop(&sc->sc_ad_tmo);
1189 tv.tv_sec = 3 * sc->sc_advbase;
1190 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1194 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1195 carp_master_down, sc);
1200 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1201 carp_master_down, sc);
1207 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1208 carp_master_down, sc);
1212 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1213 carp_master_down, sc);
1219 tv.tv_sec = sc->sc_advbase;
1220 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1221 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1228 * Setup multicast structures.
1231 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1233 struct ifnet *ifp = cif->cif_ifp;
1236 CIF_LOCK_ASSERT(cif);
1242 struct ip_moptions *imo = &cif->cif_imo;
1243 struct in_addr addr;
1245 if (imo->imo_membership)
1248 imo->imo_membership = (struct in_multi **)malloc(
1249 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1251 if (imo->imo_membership == NULL)
1253 imo->imo_mfilters = NULL;
1254 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1255 imo->imo_multicast_vif = -1;
1257 addr.s_addr = htonl(INADDR_CARP_GROUP);
1258 if ((error = in_joingroup(ifp, &addr, NULL,
1259 &imo->imo_membership[0])) != 0) {
1260 free(imo->imo_membership, M_CARP);
1263 imo->imo_num_memberships++;
1264 imo->imo_multicast_ifp = ifp;
1265 imo->imo_multicast_ttl = CARP_DFLTTL;
1266 imo->imo_multicast_loop = 0;
1273 struct ip6_moptions *im6o = &cif->cif_im6o;
1274 struct in6_addr in6;
1275 struct in6_multi *in6m;
1277 if (im6o->im6o_membership)
1280 im6o->im6o_membership = (struct in6_multi **)malloc(
1281 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1283 if (im6o->im6o_membership == NULL)
1285 im6o->im6o_mfilters = NULL;
1286 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1287 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1288 im6o->im6o_multicast_ifp = ifp;
1290 /* Join IPv6 CARP multicast group. */
1291 bzero(&in6, sizeof(in6));
1292 in6.s6_addr16[0] = htons(0xff02);
1293 in6.s6_addr8[15] = 0x12;
1294 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1295 free(im6o->im6o_membership, M_CARP);
1299 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1300 free(im6o->im6o_membership, M_CARP);
1303 im6o->im6o_membership[0] = in6m;
1304 im6o->im6o_num_memberships++;
1306 /* Join solicited multicast address. */
1307 bzero(&in6, sizeof(in6));
1308 in6.s6_addr16[0] = htons(0xff02);
1309 in6.s6_addr32[1] = 0;
1310 in6.s6_addr32[2] = htonl(1);
1311 in6.s6_addr32[3] = 0;
1312 in6.s6_addr8[12] = 0xff;
1313 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1314 in6_mc_leave(im6o->im6o_membership[0], NULL);
1315 free(im6o->im6o_membership, M_CARP);
1319 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1320 in6_mc_leave(im6o->im6o_membership[0], NULL);
1321 free(im6o->im6o_membership, M_CARP);
1324 im6o->im6o_membership[1] = in6m;
1325 im6o->im6o_num_memberships++;
1335 * Free multicast structures.
1338 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1341 CIF_LOCK_ASSERT(cif);
1345 if (cif->cif_naddrs == 0) {
1346 struct ip_moptions *imo = &cif->cif_imo;
1348 in_leavegroup(imo->imo_membership[0], NULL);
1349 KASSERT(imo->imo_mfilters == NULL,
1350 ("%s: imo_mfilters != NULL", __func__));
1351 free(imo->imo_membership, M_CARP);
1352 imo->imo_membership = NULL;
1359 if (cif->cif_naddrs6 == 0) {
1360 struct ip6_moptions *im6o = &cif->cif_im6o;
1362 in6_mc_leave(im6o->im6o_membership[0], NULL);
1363 in6_mc_leave(im6o->im6o_membership[1], NULL);
1364 KASSERT(im6o->im6o_mfilters == NULL,
1365 ("%s: im6o_mfilters != NULL", __func__));
1366 free(im6o->im6o_membership, M_CARP);
1367 im6o->im6o_membership = NULL;
1375 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1378 struct carp_softc *sc;
1383 switch (sa->sa_family) {
1396 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1400 bcopy(mtag + 1, &sc, sizeof(sc));
1402 /* Set the source MAC address to the Virtual Router MAC Address. */
1403 switch (ifp->if_type) {
1407 struct ether_header *eh;
1409 eh = mtod(m, struct ether_header *);
1410 eh->ether_shost[0] = 0;
1411 eh->ether_shost[1] = 0;
1412 eh->ether_shost[2] = 0x5e;
1413 eh->ether_shost[3] = 0;
1414 eh->ether_shost[4] = 1;
1415 eh->ether_shost[5] = sc->sc_vhid;
1419 struct fddi_header *fh;
1421 fh = mtod(m, struct fddi_header *);
1422 fh->fddi_shost[0] = 0;
1423 fh->fddi_shost[1] = 0;
1424 fh->fddi_shost[2] = 0x5e;
1425 fh->fddi_shost[3] = 0;
1426 fh->fddi_shost[4] = 1;
1427 fh->fddi_shost[5] = sc->sc_vhid;
1430 case IFT_ISO88025: {
1431 struct iso88025_header *th;
1432 th = mtod(m, struct iso88025_header *);
1433 th->iso88025_shost[0] = 3;
1434 th->iso88025_shost[1] = 0;
1435 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1436 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1437 th->iso88025_shost[4] = 0;
1438 th->iso88025_shost[5] = 0;
1442 printf("%s: carp is not supported for the %d interface type\n",
1443 ifp->if_xname, ifp->if_type);
1444 return (EOPNOTSUPP);
1450 static struct carp_softc*
1451 carp_alloc(struct ifnet *ifp)
1453 struct carp_softc *sc;
1454 struct carp_if *cif;
1456 if ((cif = ifp->if_carp) == NULL) {
1457 cif = carp_alloc_if(ifp);
1462 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1464 sc->sc_advbase = CARP_DFLTINTV;
1465 sc->sc_vhid = -1; /* required setting */
1466 sc->sc_init_counter = 1;
1467 sc->sc_state = INIT;
1469 sc->sc_ifasiz = sizeof(struct ifaddr *);
1470 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1471 sc->sc_carpdev = ifp;
1475 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1478 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1480 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1483 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1486 mtx_lock(&carp_mtx);
1487 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1488 mtx_unlock(&carp_mtx);
1494 carp_grow_ifas(struct carp_softc *sc)
1496 struct ifaddr **new;
1498 CARP_LOCK_ASSERT(sc);
1500 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1503 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1504 free(sc->sc_ifas, M_CARP);
1512 carp_destroy(struct carp_softc *sc)
1514 struct ifnet *ifp = sc->sc_carpdev;
1515 struct carp_if *cif = ifp->if_carp;
1517 CIF_LOCK_ASSERT(cif);
1519 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1521 mtx_lock(&carp_mtx);
1522 LIST_REMOVE(sc, sc_next);
1523 mtx_unlock(&carp_mtx);
1526 if (sc->sc_suppress)
1527 carp_demote_adj(-carp_ifdown_adj, "vhid removed");
1528 callout_drain(&sc->sc_ad_tmo);
1530 callout_drain(&sc->sc_md_tmo);
1533 callout_drain(&sc->sc_md6_tmo);
1535 CARP_LOCK_DESTROY(sc);
1537 free(sc->sc_ifas, M_CARP);
1541 static struct carp_if*
1542 carp_alloc_if(struct ifnet *ifp)
1544 struct carp_if *cif;
1546 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1548 if (ifpromisc(ifp, 1) != 0)
1553 TAILQ_INIT(&cif->cif_vrs);
1558 IF_ADDR_WUNLOCK(ifp);
1569 carp_free_if(struct carp_if *cif)
1571 struct ifnet *ifp = cif->cif_ifp;
1573 CIF_LOCK_ASSERT(cif);
1574 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1578 ifp->if_carp = NULL;
1580 IF_ADDR_WUNLOCK(ifp);
1582 CIF_LOCK_DESTROY(cif);
1590 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1594 carpr->carpr_state = sc->sc_state;
1595 carpr->carpr_vhid = sc->sc_vhid;
1596 carpr->carpr_advbase = sc->sc_advbase;
1597 carpr->carpr_advskew = sc->sc_advskew;
1599 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1601 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1606 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1608 struct carpreq carpr;
1610 struct carp_softc *sc = NULL;
1611 int error = 0, locked = 0;
1613 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1616 ifp = ifunit_ref(ifr->ifr_name);
1620 switch (ifp->if_type) {
1632 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1633 error = EADDRNOTAVAIL;
1639 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1641 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1642 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1648 CIF_LOCK(ifp->if_carp);
1649 IFNET_FOREACH_CARP(ifp, sc)
1650 if (sc->sc_vhid == carpr.carpr_vhid)
1652 CIF_UNLOCK(ifp->if_carp);
1655 sc = carp_alloc(ifp);
1657 error = EINVAL; /* XXX: ifpromisc failed */
1662 sc->sc_vhid = carpr.carpr_vhid;
1663 LLADDR(&sc->sc_addr)[0] = 0;
1664 LLADDR(&sc->sc_addr)[1] = 0;
1665 LLADDR(&sc->sc_addr)[2] = 0x5e;
1666 LLADDR(&sc->sc_addr)[3] = 0;
1667 LLADDR(&sc->sc_addr)[4] = 1;
1668 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1672 if (carpr.carpr_advbase > 0) {
1673 if (carpr.carpr_advbase > 255 ||
1674 carpr.carpr_advbase < CARP_DFLTINTV) {
1678 sc->sc_advbase = carpr.carpr_advbase;
1680 if (carpr.carpr_advskew > 0) {
1681 if (carpr.carpr_advskew >= 255) {
1685 sc->sc_advskew = carpr.carpr_advskew;
1687 if (carpr.carpr_key[0] != '\0') {
1688 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1689 carp_hmac_prepare(sc);
1691 if (sc->sc_state != INIT &&
1692 carpr.carpr_state != sc->sc_state) {
1693 switch (carpr.carpr_state) {
1695 callout_stop(&sc->sc_ad_tmo);
1696 carp_set_state(sc, BACKUP);
1701 carp_master_down_locked(sc);
1713 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1717 if (carpr.carpr_count < 1) {
1721 if (ifp->if_carp == NULL) {
1726 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1727 if (carpr.carpr_vhid != 0) {
1728 CIF_LOCK(ifp->if_carp);
1729 IFNET_FOREACH_CARP(ifp, sc)
1730 if (sc->sc_vhid == carpr.carpr_vhid)
1732 CIF_UNLOCK(ifp->if_carp);
1737 carp_carprcp(&carpr, sc, priveleged);
1738 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1743 CIF_LOCK(ifp->if_carp);
1744 IFNET_FOREACH_CARP(ifp, sc)
1747 if (count > carpr.carpr_count) {
1748 CIF_UNLOCK(ifp->if_carp);
1754 IFNET_FOREACH_CARP(ifp, sc) {
1755 carp_carprcp(&carpr, sc, priveleged);
1756 carpr.carpr_count = count;
1757 error = copyout(&carpr, ifr->ifr_data +
1758 (i * sizeof(carpr)), sizeof(carpr));
1760 CIF_UNLOCK(ifp->if_carp);
1765 CIF_UNLOCK(ifp->if_carp);
1782 carp_get_vhid(struct ifaddr *ifa)
1785 if (ifa == NULL || ifa->ifa_carp == NULL)
1788 return (ifa->ifa_carp->sc_vhid);
1792 carp_attach(struct ifaddr *ifa, int vhid)
1794 struct ifnet *ifp = ifa->ifa_ifp;
1795 struct carp_if *cif = ifp->if_carp;
1796 struct carp_softc *sc;
1799 if (ifp->if_carp == NULL)
1800 return (ENOPROTOOPT);
1802 switch (ifa->ifa_addr->sa_family) {
1811 return (EPROTOTYPE);
1815 IFNET_FOREACH_CARP(ifp, sc)
1816 if (sc->sc_vhid == vhid)
1823 if (ifa->ifa_carp) {
1824 if (ifa->ifa_carp->sc_vhid != vhid)
1825 carp_detach_locked(ifa);
1832 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1839 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1840 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1841 if ((error = carp_grow_ifas(sc)) != 0) {
1842 carp_multicast_cleanup(cif,
1843 ifa->ifa_addr->sa_family);
1849 switch (ifa->ifa_addr->sa_family) {
1865 sc->sc_ifas[index - 1] = ifa;
1868 carp_hmac_prepare(sc);
1878 carp_detach(struct ifaddr *ifa)
1880 struct ifnet *ifp = ifa->ifa_ifp;
1881 struct carp_if *cif = ifp->if_carp;
1884 carp_detach_locked(ifa);
1889 carp_detach_locked(struct ifaddr *ifa)
1891 struct ifnet *ifp = ifa->ifa_ifp;
1892 struct carp_if *cif = ifp->if_carp;
1893 struct carp_softc *sc = ifa->ifa_carp;
1896 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1898 CIF_LOCK_ASSERT(cif);
1902 index = sc->sc_naddrs + sc->sc_naddrs6;
1903 for (i = 0; i < index; i++)
1904 if (sc->sc_ifas[i] == ifa)
1906 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1907 for (; i < index - 1; i++)
1908 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1909 sc->sc_ifas[index - 1] = NULL;
1911 switch (ifa->ifa_addr->sa_family) {
1926 carp_ifa_delroute(ifa);
1927 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1929 ifa->ifa_carp = NULL;
1932 carp_hmac_prepare(sc);
1935 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1943 carp_set_state(struct carp_softc *sc, int state)
1946 CARP_LOCK_ASSERT(sc);
1948 if (sc->sc_state != state) {
1949 const char *carp_states[] = { CARP_STATES };
1950 char subsys[IFNAMSIZ+5];
1952 sc->sc_state = state;
1954 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1955 sc->sc_carpdev->if_xname);
1956 devctl_notify("CARP", subsys, carp_states[state], NULL);
1961 carp_linkstate(struct ifnet *ifp)
1963 struct carp_softc *sc;
1965 CIF_LOCK(ifp->if_carp);
1966 IFNET_FOREACH_CARP(ifp, sc) {
1971 CIF_UNLOCK(ifp->if_carp);
1975 carp_sc_state(struct carp_softc *sc)
1978 CARP_LOCK_ASSERT(sc);
1980 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1981 !(sc->sc_carpdev->if_flags & IFF_UP)) {
1982 callout_stop(&sc->sc_ad_tmo);
1984 callout_stop(&sc->sc_md_tmo);
1987 callout_stop(&sc->sc_md6_tmo);
1989 carp_set_state(sc, INIT);
1991 if (!sc->sc_suppress)
1992 carp_demote_adj(carp_ifdown_adj, "interface down");
1993 sc->sc_suppress = 1;
1995 carp_set_state(sc, INIT);
1997 if (sc->sc_suppress)
1998 carp_demote_adj(-carp_ifdown_adj, "interface up");
1999 sc->sc_suppress = 0;
2004 carp_demote_adj(int adj, char *reason)
2006 atomic_add_int(&carp_demotion, adj);
2007 CARP_LOG("demoted by %d to %d (%s)\n", adj, carp_demotion, reason);
2008 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2012 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2016 new = carp_demotion;
2017 error = sysctl_handle_int(oidp, &new, 0, req);
2018 if (error || !req->newptr)
2021 carp_demote_adj(new, "sysctl");
2027 extern struct domain inetdomain;
2028 static struct protosw in_carp_protosw = {
2029 .pr_type = SOCK_RAW,
2030 .pr_domain = &inetdomain,
2031 .pr_protocol = IPPROTO_CARP,
2032 .pr_flags = PR_ATOMIC|PR_ADDR,
2033 .pr_input = carp_input,
2034 .pr_output = (pr_output_t *)rip_output,
2035 .pr_ctloutput = rip_ctloutput,
2036 .pr_usrreqs = &rip_usrreqs
2041 extern struct domain inet6domain;
2042 static struct ip6protosw in6_carp_protosw = {
2043 .pr_type = SOCK_RAW,
2044 .pr_domain = &inet6domain,
2045 .pr_protocol = IPPROTO_CARP,
2046 .pr_flags = PR_ATOMIC|PR_ADDR,
2047 .pr_input = carp6_input,
2048 .pr_output = rip6_output,
2049 .pr_ctloutput = rip6_ctloutput,
2050 .pr_usrreqs = &rip6_usrreqs
2055 carp_mod_cleanup(void)
2059 if (proto_reg[CARP_INET] == 0) {
2060 (void)ipproto_unregister(IPPROTO_CARP);
2061 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2062 proto_reg[CARP_INET] = -1;
2064 carp_iamatch_p = NULL;
2067 if (proto_reg[CARP_INET6] == 0) {
2068 (void)ip6proto_unregister(IPPROTO_CARP);
2069 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2070 proto_reg[CARP_INET6] = -1;
2072 carp_iamatch6_p = NULL;
2073 carp_macmatch6_p = NULL;
2075 carp_ioctl_p = NULL;
2076 carp_attach_p = NULL;
2077 carp_detach_p = NULL;
2078 carp_get_vhid_p = NULL;
2079 carp_linkstate_p = NULL;
2080 carp_forus_p = NULL;
2081 carp_output_p = NULL;
2082 carp_demote_adj_p = NULL;
2083 carp_master_p = NULL;
2084 mtx_unlock(&carp_mtx);
2085 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2086 mtx_destroy(&carp_mtx);
2094 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2095 LIST_INIT(&carp_list);
2096 carp_get_vhid_p = carp_get_vhid;
2097 carp_forus_p = carp_forus;
2098 carp_output_p = carp_output;
2099 carp_linkstate_p = carp_linkstate;
2100 carp_ioctl_p = carp_ioctl;
2101 carp_attach_p = carp_attach;
2102 carp_detach_p = carp_detach;
2103 carp_demote_adj_p = carp_demote_adj;
2104 carp_master_p = carp_master;
2106 carp_iamatch6_p = carp_iamatch6;
2107 carp_macmatch6_p = carp_macmatch6;
2108 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2109 (struct protosw *)&in6_carp_protosw);
2110 if (proto_reg[CARP_INET6]) {
2111 printf("carp: error %d attaching to PF_INET6\n",
2112 proto_reg[CARP_INET6]);
2114 return (proto_reg[CARP_INET6]);
2116 err = ip6proto_register(IPPROTO_CARP);
2118 printf("carp: error %d registering with INET6\n", err);
2124 carp_iamatch_p = carp_iamatch;
2125 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2126 if (proto_reg[CARP_INET]) {
2127 printf("carp: error %d attaching to PF_INET\n",
2128 proto_reg[CARP_INET]);
2130 return (proto_reg[CARP_INET]);
2132 err = ipproto_register(IPPROTO_CARP);
2134 printf("carp: error %d registering with INET\n", err);
2143 carp_modevent(module_t mod, int type, void *data)
2147 return carp_mod_load();
2150 mtx_lock(&carp_mtx);
2151 if (LIST_EMPTY(&carp_list))
2154 mtx_unlock(&carp_mtx);
2166 static moduledata_t carp_mod = {
2172 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);