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)
227 #define CARP_LOG(...) do { \
229 log(LOG_INFO, "carp: " __VA_ARGS__); \
232 #define CARP_DEBUG(...) do { \
234 log(LOG_DEBUG, __VA_ARGS__); \
237 #define IFNET_FOREACH_IFA(ifp, ifa) \
238 IF_ADDR_LOCK_ASSERT(ifp); \
239 TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
240 if ((ifa)->ifa_carp != NULL)
242 #define CARP_FOREACH_IFA(sc, ifa) \
243 CARP_LOCK_ASSERT(sc); \
245 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
246 ((ifa) = sc->sc_ifas[_i]) != NULL; \
249 #define IFNET_FOREACH_CARP(ifp, sc) \
250 CIF_LOCK_ASSERT(ifp->if_carp); \
251 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
253 #define DEMOTE_ADVSKEW(sc) \
254 (((sc)->sc_advskew + carp_demotion > CARP_MAXSKEW) ? \
255 CARP_MAXSKEW : ((sc)->sc_advskew + carp_demotion))
257 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
258 static struct carp_softc
259 *carp_alloc(struct ifnet *);
260 static void carp_destroy(struct carp_softc *);
261 static struct carp_if
262 *carp_alloc_if(struct ifnet *);
263 static void carp_free_if(struct carp_if *);
264 static void carp_set_state(struct carp_softc *, int);
265 static void carp_sc_state(struct carp_softc *);
266 static void carp_setrun(struct carp_softc *, sa_family_t);
267 static void carp_master_down(void *);
268 static void carp_master_down_locked(struct carp_softc *);
269 static void carp_send_ad(void *);
270 static void carp_send_ad_locked(struct carp_softc *);
271 static void carp_addroute(struct carp_softc *);
272 static void carp_ifa_addroute(struct ifaddr *);
273 static void carp_delroute(struct carp_softc *);
274 static void carp_ifa_delroute(struct ifaddr *);
275 static void carp_send_ad_all(void *, int);
276 static void carp_demote_adj(int, char *);
278 static LIST_HEAD(, carp_softc) carp_list;
279 static struct mtx carp_mtx;
280 static struct task carp_sendall_task =
281 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
283 static __inline uint16_t
284 carp_cksum(struct mbuf *m, int len)
286 return (in_cksum(m, len));
290 carp_hmac_prepare(struct carp_softc *sc)
292 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
293 uint8_t vhid = sc->sc_vhid & 0xff;
297 struct in_addr last, cur, in;
300 struct in6_addr last6, cur6, in6;
303 CARP_LOCK_ASSERT(sc);
305 /* Compute ipad from key. */
306 bzero(sc->sc_pad, sizeof(sc->sc_pad));
307 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
308 for (i = 0; i < sizeof(sc->sc_pad); i++)
309 sc->sc_pad[i] ^= 0x36;
311 /* Precompute first part of inner hash. */
312 SHA1Init(&sc->sc_sha1);
313 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
314 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
315 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
316 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
322 cur.s_addr = 0xffffffff;
323 CARP_FOREACH_IFA(sc, ifa) {
324 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
325 if (ifa->ifa_addr->sa_family == AF_INET &&
326 ntohl(in.s_addr) > ntohl(last.s_addr) &&
327 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
328 cur.s_addr = in.s_addr;
333 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
337 memset(&cur6, 0, sizeof(cur6));
341 memset(&cur6, 0xff, sizeof(cur6));
342 CARP_FOREACH_IFA(sc, ifa) {
343 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
344 if (IN6_IS_SCOPE_EMBED(&in6))
345 in6.s6_addr16[1] = 0;
346 if (ifa->ifa_addr->sa_family == AF_INET6 &&
347 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
348 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
354 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
358 /* convert ipad to opad */
359 for (i = 0; i < sizeof(sc->sc_pad); i++)
360 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
364 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
365 unsigned char md[20])
369 CARP_LOCK_ASSERT(sc);
371 /* fetch first half of inner hash */
372 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
374 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
375 SHA1Final(md, &sha1ctx);
379 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
380 SHA1Update(&sha1ctx, md, 20);
381 SHA1Final(md, &sha1ctx);
385 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
386 unsigned char md[20])
388 unsigned char md2[20];
390 CARP_LOCK_ASSERT(sc);
392 carp_hmac_generate(sc, counter, md2);
394 return (bcmp(md, md2, sizeof(md2)));
398 * process input packet.
399 * we have rearranged checks order compared to the rfc,
400 * but it seems more efficient this way or not possible otherwise.
404 carp_input(struct mbuf *m, int hlen)
406 struct ip *ip = mtod(m, struct ip *);
407 struct carp_header *ch;
410 CARPSTATS_INC(carps_ipackets);
417 /* verify that the IP TTL is 255. */
418 if (ip->ip_ttl != CARP_DFLTTL) {
419 CARPSTATS_INC(carps_badttl);
420 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
422 m->m_pkthdr.rcvif->if_xname);
427 iplen = ip->ip_hl << 2;
429 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
430 CARPSTATS_INC(carps_badlen);
431 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
432 "on %s\n", __func__, m->m_len - sizeof(struct ip),
433 m->m_pkthdr.rcvif->if_xname);
438 if (iplen + sizeof(*ch) < m->m_len) {
439 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
440 CARPSTATS_INC(carps_hdrops);
441 CARP_DEBUG("%s: pullup failed\n", __func__);
444 ip = mtod(m, struct ip *);
446 ch = (struct carp_header *)((char *)ip + iplen);
449 * verify that the received packet length is
450 * equal to the CARP header
452 len = iplen + sizeof(*ch);
453 if (len > m->m_pkthdr.len) {
454 CARPSTATS_INC(carps_badlen);
455 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
457 m->m_pkthdr.rcvif->if_xname);
462 if ((m = m_pullup(m, len)) == NULL) {
463 CARPSTATS_INC(carps_hdrops);
466 ip = mtod(m, struct ip *);
467 ch = (struct carp_header *)((char *)ip + iplen);
469 /* verify the CARP checksum */
471 if (carp_cksum(m, len - iplen)) {
472 CARPSTATS_INC(carps_badsum);
473 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
474 m->m_pkthdr.rcvif->if_xname);
480 carp_input_c(m, ch, AF_INET);
486 carp6_input(struct mbuf **mp, int *offp, int proto)
488 struct mbuf *m = *mp;
489 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
490 struct carp_header *ch;
493 CARPSTATS_INC(carps_ipackets6);
497 return (IPPROTO_DONE);
500 /* check if received on a valid carp interface */
501 if (m->m_pkthdr.rcvif->if_carp == NULL) {
502 CARPSTATS_INC(carps_badif);
503 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
504 __func__, m->m_pkthdr.rcvif->if_xname);
506 return (IPPROTO_DONE);
509 /* verify that the IP TTL is 255 */
510 if (ip6->ip6_hlim != CARP_DFLTTL) {
511 CARPSTATS_INC(carps_badttl);
512 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
513 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
515 return (IPPROTO_DONE);
518 /* verify that we have a complete carp packet */
520 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
522 CARPSTATS_INC(carps_badlen);
523 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
524 return (IPPROTO_DONE);
528 /* verify the CARP checksum */
530 if (carp_cksum(m, sizeof(*ch))) {
531 CARPSTATS_INC(carps_badsum);
532 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
533 m->m_pkthdr.rcvif->if_xname);
535 return (IPPROTO_DONE);
539 carp_input_c(m, ch, AF_INET6);
540 return (IPPROTO_DONE);
545 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
547 struct ifnet *ifp = m->m_pkthdr.rcvif;
549 struct carp_softc *sc;
550 uint64_t tmp_counter;
551 struct timeval sc_tv, ch_tv;
553 /* verify that the VHID is valid on the receiving interface */
555 IFNET_FOREACH_IFA(ifp, ifa)
556 if (ifa->ifa_addr->sa_family == af &&
557 ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
561 IF_ADDR_RUNLOCK(ifp);
564 CARPSTATS_INC(carps_badvhid);
569 /* verify the CARP version. */
570 if (ch->carp_version != CARP_VERSION) {
571 CARPSTATS_INC(carps_badver);
572 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
583 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
584 CARPSTATS_INC(carps_badauth);
585 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
586 sc->sc_vhid, ifp->if_xname);
590 tmp_counter = ntohl(ch->carp_counter[0]);
591 tmp_counter = tmp_counter<<32;
592 tmp_counter += ntohl(ch->carp_counter[1]);
594 /* XXX Replay protection goes here */
596 sc->sc_init_counter = 0;
597 sc->sc_counter = tmp_counter;
599 sc_tv.tv_sec = sc->sc_advbase;
600 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
601 ch_tv.tv_sec = ch->carp_advbase;
602 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
604 switch (sc->sc_state) {
609 * If we receive an advertisement from a master who's going to
610 * be more frequent than us, go into BACKUP state.
612 if (timevalcmp(&sc_tv, &ch_tv, >) ||
613 timevalcmp(&sc_tv, &ch_tv, ==)) {
614 callout_stop(&sc->sc_ad_tmo);
615 CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
616 "(more frequent advertisement received)\n",
618 sc->sc_carpdev->if_xname);
619 carp_set_state(sc, BACKUP);
626 * If we're pre-empting masters who advertise slower than us,
627 * and this one claims to be slower, treat him as down.
629 if (carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
630 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
631 "(preempting a slower master)\n",
633 sc->sc_carpdev->if_xname);
634 carp_master_down_locked(sc);
639 * If the master is going to advertise at such a low frequency
640 * that he's guaranteed to time out, we'd might as well just
641 * treat him as timed out now.
643 sc_tv.tv_sec = sc->sc_advbase * 3;
644 if (timevalcmp(&sc_tv, &ch_tv, <)) {
645 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
646 "(master timed out)\n",
648 sc->sc_carpdev->if_xname);
649 carp_master_down_locked(sc);
654 * Otherwise, we reset the counter and wait for the next
667 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
671 if (sc->sc_init_counter) {
672 /* this could also be seconds since unix epoch */
673 sc->sc_counter = arc4random();
674 sc->sc_counter = sc->sc_counter << 32;
675 sc->sc_counter += arc4random();
679 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
680 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
682 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
684 /* Tag packet for carp_output */
685 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
686 M_NOWAIT)) == NULL) {
688 CARPSTATS_INC(carps_onomem);
691 bcopy(&sc, (caddr_t)(mtag + 1), sizeof(struct carp_softc *));
692 m_tag_prepend(m, mtag);
698 * To avoid LORs and possible recursions this function shouldn't
699 * be called directly, but scheduled via taskqueue.
702 carp_send_ad_all(void *ctx __unused, int pending __unused)
704 struct carp_softc *sc;
707 LIST_FOREACH(sc, &carp_list, sc_next)
708 if (sc->sc_state == MASTER) {
710 carp_send_ad_locked(sc);
713 mtx_unlock(&carp_mtx);
717 carp_send_ad(void *v)
719 struct carp_softc *sc = v;
721 CARP_LOCK_ASSERT(sc);
722 carp_send_ad_locked(sc);
727 carp_send_ad_locked(struct carp_softc *sc)
729 struct carp_header ch;
733 struct carp_header *ch_ptr;
737 CARP_LOCK_ASSERT(sc);
739 advskew = DEMOTE_ADVSKEW(sc);
740 tv.tv_sec = sc->sc_advbase;
741 tv.tv_usec = advskew * 1000000 / 256;
743 ch.carp_version = CARP_VERSION;
744 ch.carp_type = CARP_ADVERTISEMENT;
745 ch.carp_vhid = sc->sc_vhid;
746 ch.carp_advbase = sc->sc_advbase;
747 ch.carp_advskew = advskew;
748 ch.carp_authlen = 7; /* XXX DEFINE */
749 ch.carp_pad1 = 0; /* must be zero */
752 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
758 MGETHDR(m, M_NOWAIT, MT_HEADER);
760 CARPSTATS_INC(carps_onomem);
761 /* XXX maybe less ? */
762 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
766 len = sizeof(*ip) + sizeof(ch);
767 m->m_pkthdr.len = len;
768 m->m_pkthdr.rcvif = NULL;
770 MH_ALIGN(m, m->m_len);
771 m->m_flags |= M_MCAST;
772 ip = mtod(m, struct ip *);
773 ip->ip_v = IPVERSION;
774 ip->ip_hl = sizeof(*ip) >> 2;
775 ip->ip_tos = IPTOS_LOWDELAY;
777 ip->ip_id = ip_newid();
779 ip->ip_ttl = CARP_DFLTTL;
780 ip->ip_p = IPPROTO_CARP;
783 bzero(&sa, sizeof(sa));
784 sa.sa_family = AF_INET;
785 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
788 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
791 ip->ip_src.s_addr = 0;
792 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
794 ch_ptr = (struct carp_header *)(&ip[1]);
795 bcopy(&ch, ch_ptr, sizeof(ch));
796 if (carp_prepare_ad(m, sc, ch_ptr))
799 m->m_data += sizeof(*ip);
800 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
801 m->m_data -= sizeof(*ip);
803 CARPSTATS_INC(carps_opackets);
805 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
806 &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
807 if (sc->sc_sendad_errors < INT_MAX)
808 sc->sc_sendad_errors++;
809 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
810 carp_demote_adj(carp_senderr_adj, "send error");
811 sc->sc_sendad_success = 0;
813 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
814 if (++sc->sc_sendad_success >=
815 CARP_SENDAD_MIN_SUCCESS) {
816 carp_demote_adj(-carp_senderr_adj,
818 sc->sc_sendad_errors = 0;
821 sc->sc_sendad_errors = 0;
826 if (sc->sc_naddrs6) {
829 MGETHDR(m, M_NOWAIT, MT_HEADER);
831 CARPSTATS_INC(carps_onomem);
832 /* XXX maybe less ? */
833 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
837 len = sizeof(*ip6) + sizeof(ch);
838 m->m_pkthdr.len = len;
839 m->m_pkthdr.rcvif = NULL;
841 MH_ALIGN(m, m->m_len);
842 m->m_flags |= M_MCAST;
843 ip6 = mtod(m, struct ip6_hdr *);
844 bzero(ip6, sizeof(*ip6));
845 ip6->ip6_vfc |= IPV6_VERSION;
846 ip6->ip6_hlim = CARP_DFLTTL;
847 ip6->ip6_nxt = IPPROTO_CARP;
848 bzero(&sa, sizeof(sa));
850 /* set the source address */
851 sa.sa_family = AF_INET6;
852 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
854 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
855 sizeof(struct in6_addr));
858 /* This should never happen with IPv6. */
859 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
861 /* Set the multicast destination. */
862 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
863 ip6->ip6_dst.s6_addr8[15] = 0x12;
864 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
866 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
870 ch_ptr = (struct carp_header *)(&ip6[1]);
871 bcopy(&ch, ch_ptr, sizeof(ch));
872 if (carp_prepare_ad(m, sc, ch_ptr))
875 m->m_data += sizeof(*ip6);
876 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
877 m->m_data -= sizeof(*ip6);
879 CARPSTATS_INC(carps_opackets6);
881 if (ip6_output(m, NULL, NULL, 0,
882 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
883 if (sc->sc_sendad_errors < INT_MAX)
884 sc->sc_sendad_errors++;
885 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
886 carp_demote_adj(carp_senderr_adj,
888 sc->sc_sendad_success = 0;
890 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
891 if (++sc->sc_sendad_success >=
892 CARP_SENDAD_MIN_SUCCESS) {
893 carp_demote_adj(-carp_senderr_adj,
895 sc->sc_sendad_errors = 0;
898 sc->sc_sendad_errors = 0;
903 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
907 carp_addroute(struct carp_softc *sc)
911 CARP_FOREACH_IFA(sc, ifa)
912 carp_ifa_addroute(ifa);
916 carp_ifa_addroute(struct ifaddr *ifa)
919 switch (ifa->ifa_addr->sa_family) {
922 in_addprefix(ifatoia(ifa), RTF_UP);
923 ifa_add_loopback_route(ifa,
924 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
929 ifa_add_loopback_route(ifa,
930 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
938 carp_delroute(struct carp_softc *sc)
942 CARP_FOREACH_IFA(sc, ifa)
943 carp_ifa_delroute(ifa);
947 carp_ifa_delroute(struct ifaddr *ifa)
950 switch (ifa->ifa_addr->sa_family) {
953 ifa_del_loopback_route(ifa,
954 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
955 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
960 ifa_del_loopback_route(ifa,
961 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
970 * Broadcast a gratuitous ARP request containing
971 * the virtual router MAC address for each IP address
972 * associated with the virtual router.
975 carp_send_arp(struct carp_softc *sc)
979 CARP_FOREACH_IFA(sc, ifa)
980 if (ifa->ifa_addr->sa_family == AF_INET)
981 arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
985 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
987 struct carp_softc *sc = ifa->ifa_carp;
989 if (sc->sc_state == MASTER) {
990 *enaddr = LLADDR(&sc->sc_addr);
1000 carp_send_na(struct carp_softc *sc)
1002 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1004 struct in6_addr *in6;
1006 CARP_FOREACH_IFA(sc, ifa) {
1007 if (ifa->ifa_addr->sa_family != AF_INET6)
1011 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1012 ND_NA_FLAG_OVERRIDE, 1, NULL);
1013 DELAY(1000); /* XXX */
1018 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1023 IFNET_FOREACH_IFA(ifp, ifa)
1024 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1025 ifa->ifa_carp->sc_state == MASTER &&
1026 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1028 IF_ADDR_RUNLOCK(ifp);
1031 IF_ADDR_RUNLOCK(ifp);
1037 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1042 IFNET_FOREACH_IFA(ifp, ifa)
1043 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1044 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1045 struct carp_softc *sc = ifa->ifa_carp;
1048 IF_ADDR_RUNLOCK(ifp);
1050 mtag = m_tag_get(PACKET_TAG_CARP,
1051 sizeof(struct ifnet *), M_NOWAIT);
1053 /* Better a bit than nothing. */
1054 return (LLADDR(&sc->sc_addr));
1056 bcopy(&ifp, (caddr_t)(mtag + 1),
1057 sizeof(struct ifnet *));
1058 m_tag_prepend(m, mtag);
1060 return (LLADDR(&sc->sc_addr));
1062 IF_ADDR_RUNLOCK(ifp);
1069 carp_forus(struct ifnet *ifp, u_char *dhost)
1071 struct carp_softc *sc;
1072 uint8_t *ena = dhost;
1074 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1077 CIF_LOCK(ifp->if_carp);
1078 IFNET_FOREACH_CARP(ifp, sc) {
1080 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1083 CIF_UNLOCK(ifp->if_carp);
1088 CIF_UNLOCK(ifp->if_carp);
1094 carp_master_down(void *v)
1096 struct carp_softc *sc = v;
1098 CARP_LOCK_ASSERT(sc);
1100 if (sc->sc_state == BACKUP) {
1101 CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
1103 sc->sc_carpdev->if_xname);
1104 carp_master_down_locked(sc);
1111 carp_master_down_locked(struct carp_softc *sc)
1114 CARP_LOCK_ASSERT(sc);
1116 switch (sc->sc_state) {
1118 carp_set_state(sc, MASTER);
1119 carp_send_ad_locked(sc);
1132 panic("carp: VHID %u@%s: master_down event in %s state\n",
1134 sc->sc_carpdev->if_xname,
1135 sc->sc_state ? "MASTER" : "INIT");
1142 * When in backup state, af indicates whether to reset the master down timer
1143 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1146 carp_setrun(struct carp_softc *sc, sa_family_t af)
1150 CARP_LOCK_ASSERT(sc);
1152 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1153 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1154 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1157 switch (sc->sc_state) {
1159 CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1161 sc->sc_carpdev->if_xname);
1162 carp_set_state(sc, BACKUP);
1166 callout_stop(&sc->sc_ad_tmo);
1167 tv.tv_sec = 3 * sc->sc_advbase;
1168 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1172 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1173 carp_master_down, sc);
1178 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1179 carp_master_down, sc);
1185 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1186 carp_master_down, sc);
1190 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1191 carp_master_down, sc);
1197 tv.tv_sec = sc->sc_advbase;
1198 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1199 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1206 * Setup multicast structures.
1209 carp_multicast_setup(struct carp_softc *sc, sa_family_t sa)
1211 struct ifnet *ifp = sc->sc_carpdev;
1212 struct carp_if *cif = ifp->if_carp;
1219 struct ip_moptions *imo = &cif->cif_imo;
1220 struct in_addr addr;
1222 if (imo->imo_membership)
1225 imo->imo_membership = (struct in_multi **)malloc(
1226 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1228 imo->imo_mfilters = NULL;
1229 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1230 imo->imo_multicast_vif = -1;
1232 addr.s_addr = htonl(INADDR_CARP_GROUP);
1233 if ((error = in_joingroup(ifp, &addr, NULL,
1234 &imo->imo_membership[0])) != 0) {
1235 free(imo->imo_membership, M_CARP);
1238 imo->imo_num_memberships++;
1239 imo->imo_multicast_ifp = ifp;
1240 imo->imo_multicast_ttl = CARP_DFLTTL;
1241 imo->imo_multicast_loop = 0;
1248 struct ip6_moptions *im6o = &cif->cif_im6o;
1249 struct in6_addr in6;
1250 struct in6_multi *in6m;
1252 if (im6o->im6o_membership)
1255 im6o->im6o_membership = (struct in6_multi **)malloc(
1256 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1258 im6o->im6o_mfilters = NULL;
1259 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1260 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1261 im6o->im6o_multicast_ifp = ifp;
1263 /* Join IPv6 CARP multicast group. */
1264 bzero(&in6, sizeof(in6));
1265 in6.s6_addr16[0] = htons(0xff02);
1266 in6.s6_addr8[15] = 0x12;
1267 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1268 free(im6o->im6o_membership, M_CARP);
1272 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1273 free(im6o->im6o_membership, M_CARP);
1276 im6o->im6o_membership[0] = in6m;
1277 im6o->im6o_num_memberships++;
1279 /* Join solicited multicast address. */
1280 bzero(&in6, sizeof(in6));
1281 in6.s6_addr16[0] = htons(0xff02);
1282 in6.s6_addr32[1] = 0;
1283 in6.s6_addr32[2] = htonl(1);
1284 in6.s6_addr32[3] = 0;
1285 in6.s6_addr8[12] = 0xff;
1286 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1287 in6_mc_leave(im6o->im6o_membership[0], NULL);
1288 free(im6o->im6o_membership, M_CARP);
1292 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1293 in6_mc_leave(im6o->im6o_membership[0], NULL);
1294 free(im6o->im6o_membership, M_CARP);
1297 im6o->im6o_membership[1] = in6m;
1298 im6o->im6o_num_memberships++;
1308 * Free multicast structures.
1311 carp_multicast_cleanup(struct carp_softc *sc, sa_family_t sa)
1313 struct ifnet *ifp = sc->sc_carpdev;
1314 struct carp_if *cif = ifp->if_carp;
1319 if (sc->sc_naddrs == 0) {
1320 struct ip_moptions *imo = &cif->cif_imo;
1322 in_leavegroup(imo->imo_membership[0], NULL);
1323 KASSERT(imo->imo_mfilters == NULL,
1324 ("%s: imo_mfilters != NULL", __func__));
1325 free(imo->imo_membership, M_CARP);
1326 imo->imo_membership = NULL;
1333 if (sc->sc_naddrs6 == 0) {
1334 struct ip6_moptions *im6o = &cif->cif_im6o;
1336 in6_mc_leave(im6o->im6o_membership[0], NULL);
1337 in6_mc_leave(im6o->im6o_membership[1], NULL);
1338 KASSERT(im6o->im6o_mfilters == NULL,
1339 ("%s: im6o_mfilters != NULL", __func__));
1340 free(im6o->im6o_membership, M_CARP);
1341 im6o->im6o_membership = NULL;
1349 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa)
1352 struct carp_softc *sc;
1357 switch (sa->sa_family) {
1370 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1374 bcopy(mtag + 1, &sc, sizeof(struct carp_softc *));
1376 /* Set the source MAC address to the Virtual Router MAC Address. */
1377 switch (ifp->if_type) {
1380 struct ether_header *eh;
1382 eh = mtod(m, struct ether_header *);
1383 eh->ether_shost[0] = 0;
1384 eh->ether_shost[1] = 0;
1385 eh->ether_shost[2] = 0x5e;
1386 eh->ether_shost[3] = 0;
1387 eh->ether_shost[4] = 1;
1388 eh->ether_shost[5] = sc->sc_vhid;
1392 struct fddi_header *fh;
1394 fh = mtod(m, struct fddi_header *);
1395 fh->fddi_shost[0] = 0;
1396 fh->fddi_shost[1] = 0;
1397 fh->fddi_shost[2] = 0x5e;
1398 fh->fddi_shost[3] = 0;
1399 fh->fddi_shost[4] = 1;
1400 fh->fddi_shost[5] = sc->sc_vhid;
1403 case IFT_ISO88025: {
1404 struct iso88025_header *th;
1405 th = mtod(m, struct iso88025_header *);
1406 th->iso88025_shost[0] = 3;
1407 th->iso88025_shost[1] = 0;
1408 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1409 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1410 th->iso88025_shost[4] = 0;
1411 th->iso88025_shost[5] = 0;
1415 printf("%s: carp is not supported for the %d interface type\n",
1416 ifp->if_xname, ifp->if_type);
1417 return (EOPNOTSUPP);
1423 static struct carp_softc*
1424 carp_alloc(struct ifnet *ifp)
1426 struct carp_softc *sc;
1427 struct carp_if *cif;
1429 if ((cif = ifp->if_carp) == NULL) {
1430 cif = carp_alloc_if(ifp);
1435 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1437 sc->sc_advbase = CARP_DFLTINTV;
1438 sc->sc_vhid = -1; /* required setting */
1439 sc->sc_init_counter = 1;
1440 sc->sc_state = INIT;
1442 sc->sc_ifasiz = sizeof(struct ifaddr *);
1443 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1444 sc->sc_carpdev = ifp;
1448 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1451 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1453 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1456 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1459 mtx_lock(&carp_mtx);
1460 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1461 mtx_unlock(&carp_mtx);
1467 carp_grow_ifas(struct carp_softc *sc)
1469 struct ifaddr **new;
1471 CARP_LOCK_ASSERT(sc);
1473 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1476 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1477 free(sc->sc_ifas, M_CARP);
1485 carp_destroy(struct carp_softc *sc)
1487 struct ifnet *ifp = sc->sc_carpdev;
1488 struct carp_if *cif = ifp->if_carp;
1491 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1492 if (TAILQ_EMPTY(&cif->cif_vrs))
1497 mtx_lock(&carp_mtx);
1498 LIST_REMOVE(sc, sc_next);
1499 mtx_unlock(&carp_mtx);
1502 if (sc->sc_suppress)
1503 carp_demote_adj(-carp_ifdown_adj, "vhid removed");
1504 callout_drain(&sc->sc_ad_tmo);
1506 callout_drain(&sc->sc_md_tmo);
1509 callout_drain(&sc->sc_md6_tmo);
1511 CARP_LOCK_DESTROY(sc);
1513 free(sc->sc_ifas, M_CARP);
1517 static struct carp_if*
1518 carp_alloc_if(struct ifnet *ifp)
1520 struct carp_if *cif;
1522 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1524 if (ifpromisc(ifp, 1) != 0)
1529 TAILQ_INIT(&cif->cif_vrs);
1534 IF_ADDR_WUNLOCK(ifp);
1545 carp_free_if(struct carp_if *cif)
1547 struct ifnet *ifp = cif->cif_ifp;
1549 CIF_LOCK_ASSERT(cif);
1550 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1554 ifp->if_carp = NULL;
1556 IF_ADDR_WUNLOCK(ifp);
1558 CIF_LOCK_DESTROY(cif);
1566 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1570 carpr->carpr_state = sc->sc_state;
1571 carpr->carpr_vhid = sc->sc_vhid;
1572 carpr->carpr_advbase = sc->sc_advbase;
1573 carpr->carpr_advskew = sc->sc_advskew;
1575 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1577 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1582 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1584 struct carpreq carpr;
1586 struct carp_softc *sc = NULL;
1587 int error = 0, locked = 0;
1589 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1592 ifp = ifunit_ref(ifr->ifr_name);
1596 switch (ifp->if_type) {
1607 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1608 error = EADDRNOTAVAIL;
1614 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1616 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1617 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1623 CIF_LOCK(ifp->if_carp);
1624 IFNET_FOREACH_CARP(ifp, sc)
1625 if (sc->sc_vhid == carpr.carpr_vhid)
1627 CIF_UNLOCK(ifp->if_carp);
1630 sc = carp_alloc(ifp);
1632 error = EINVAL; /* XXX: ifpromisc failed */
1637 sc->sc_vhid = carpr.carpr_vhid;
1638 LLADDR(&sc->sc_addr)[0] = 0;
1639 LLADDR(&sc->sc_addr)[1] = 0;
1640 LLADDR(&sc->sc_addr)[2] = 0x5e;
1641 LLADDR(&sc->sc_addr)[3] = 0;
1642 LLADDR(&sc->sc_addr)[4] = 1;
1643 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1647 if (carpr.carpr_advbase > 0) {
1648 if (carpr.carpr_advbase > 255 ||
1649 carpr.carpr_advbase < CARP_DFLTINTV) {
1653 sc->sc_advbase = carpr.carpr_advbase;
1655 if (carpr.carpr_advskew > 0) {
1656 if (carpr.carpr_advskew >= 255) {
1660 sc->sc_advskew = carpr.carpr_advskew;
1662 if (carpr.carpr_key[0] != '\0') {
1663 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1664 carp_hmac_prepare(sc);
1666 if (sc->sc_state != INIT &&
1667 carpr.carpr_state != sc->sc_state) {
1668 switch (carpr.carpr_state) {
1670 callout_stop(&sc->sc_ad_tmo);
1671 carp_set_state(sc, BACKUP);
1676 carp_master_down_locked(sc);
1688 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1692 if (carpr.carpr_count < 1) {
1696 if (ifp->if_carp == NULL) {
1701 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1702 if (carpr.carpr_vhid != 0) {
1703 CIF_LOCK(ifp->if_carp);
1704 IFNET_FOREACH_CARP(ifp, sc)
1705 if (sc->sc_vhid == carpr.carpr_vhid)
1707 CIF_UNLOCK(ifp->if_carp);
1712 carp_carprcp(&carpr, sc, priveleged);
1713 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1718 CIF_LOCK(ifp->if_carp);
1719 IFNET_FOREACH_CARP(ifp, sc)
1722 if (count > carpr.carpr_count) {
1723 CIF_UNLOCK(ifp->if_carp);
1729 IFNET_FOREACH_CARP(ifp, sc) {
1730 carp_carprcp(&carpr, sc, priveleged);
1731 carpr.carpr_count = count;
1732 error = copyout(&carpr, ifr->ifr_data +
1733 (i * sizeof(carpr)), sizeof(carpr));
1735 CIF_UNLOCK(ifp->if_carp);
1740 CIF_UNLOCK(ifp->if_carp);
1757 carp_get_vhid(struct ifaddr *ifa)
1760 if (ifa == NULL || ifa->ifa_carp == NULL)
1763 return (ifa->ifa_carp->sc_vhid);
1767 carp_attach(struct ifaddr *ifa, int vhid)
1769 struct ifnet *ifp = ifa->ifa_ifp;
1770 struct carp_softc *sc;
1773 if (ifp->if_carp == NULL)
1774 return (ENOPROTOOPT);
1776 switch (ifa->ifa_addr->sa_family) {
1785 return (EPROTOTYPE);
1788 CIF_LOCK(ifp->if_carp);
1789 IFNET_FOREACH_CARP(ifp, sc)
1790 if (sc->sc_vhid == vhid)
1792 CIF_UNLOCK(ifp->if_carp);
1796 if (ifa->ifa_carp) {
1797 if (ifa->ifa_carp->sc_vhid != vhid)
1803 error = carp_multicast_setup(sc, ifa->ifa_addr->sa_family);
1808 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1809 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1810 if ((error = carp_grow_ifas(sc)) != 0) {
1811 carp_multicast_cleanup(sc,
1812 ifa->ifa_addr->sa_family);
1817 switch (ifa->ifa_addr->sa_family) {
1831 sc->sc_ifas[index - 1] = ifa;
1834 carp_hmac_prepare(sc);
1843 carp_detach(struct ifaddr *ifa)
1845 struct carp_softc *sc = ifa->ifa_carp;
1848 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1853 index = sc->sc_naddrs + sc->sc_naddrs6;
1854 for (i = 0; i < index; i++)
1855 if (sc->sc_ifas[i] == ifa)
1857 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1858 for (; i < index - 1; i++)
1859 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1860 sc->sc_ifas[index - 1] = NULL;
1862 switch (ifa->ifa_addr->sa_family) {
1875 carp_ifa_delroute(ifa);
1876 carp_multicast_cleanup(sc, ifa->ifa_addr->sa_family);
1878 ifa->ifa_carp = NULL;
1881 carp_hmac_prepare(sc);
1884 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1892 carp_set_state(struct carp_softc *sc, int state)
1895 CARP_LOCK_ASSERT(sc);
1897 if (sc->sc_state != state) {
1898 const char *carp_states[] = { CARP_STATES };
1899 char subsys[IFNAMSIZ+5];
1901 sc->sc_state = state;
1903 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1904 sc->sc_carpdev->if_xname);
1905 devctl_notify("CARP", subsys, carp_states[state], NULL);
1910 carp_linkstate(struct ifnet *ifp)
1912 struct carp_softc *sc;
1914 CIF_LOCK(ifp->if_carp);
1915 IFNET_FOREACH_CARP(ifp, sc) {
1920 CIF_UNLOCK(ifp->if_carp);
1924 carp_sc_state(struct carp_softc *sc)
1927 CARP_LOCK_ASSERT(sc);
1929 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1930 !(sc->sc_carpdev->if_flags & IFF_UP)) {
1931 callout_stop(&sc->sc_ad_tmo);
1933 callout_stop(&sc->sc_md_tmo);
1936 callout_stop(&sc->sc_md6_tmo);
1938 carp_set_state(sc, INIT);
1940 if (!sc->sc_suppress)
1941 carp_demote_adj(carp_ifdown_adj, "interface down");
1942 sc->sc_suppress = 1;
1944 carp_set_state(sc, INIT);
1946 if (sc->sc_suppress)
1947 carp_demote_adj(-carp_ifdown_adj, "interface up");
1948 sc->sc_suppress = 0;
1953 carp_demote_adj(int adj, char *reason)
1955 carp_demotion += adj;
1956 CARP_LOG("demoted by %d to %d (%s)\n", adj, carp_demotion, reason);
1957 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
1961 extern struct domain inetdomain;
1962 static struct protosw in_carp_protosw = {
1963 .pr_type = SOCK_RAW,
1964 .pr_domain = &inetdomain,
1965 .pr_protocol = IPPROTO_CARP,
1966 .pr_flags = PR_ATOMIC|PR_ADDR,
1967 .pr_input = carp_input,
1968 .pr_output = (pr_output_t *)rip_output,
1969 .pr_ctloutput = rip_ctloutput,
1970 .pr_usrreqs = &rip_usrreqs
1975 extern struct domain inet6domain;
1976 static struct ip6protosw in6_carp_protosw = {
1977 .pr_type = SOCK_RAW,
1978 .pr_domain = &inet6domain,
1979 .pr_protocol = IPPROTO_CARP,
1980 .pr_flags = PR_ATOMIC|PR_ADDR,
1981 .pr_input = carp6_input,
1982 .pr_output = rip6_output,
1983 .pr_ctloutput = rip6_ctloutput,
1984 .pr_usrreqs = &rip6_usrreqs
1989 carp_mod_cleanup(void)
1993 if (proto_reg[CARP_INET] == 0) {
1994 (void)ipproto_unregister(IPPROTO_CARP);
1995 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
1996 proto_reg[CARP_INET] = -1;
1998 carp_iamatch_p = NULL;
2001 if (proto_reg[CARP_INET6] == 0) {
2002 (void)ip6proto_unregister(IPPROTO_CARP);
2003 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2004 proto_reg[CARP_INET6] = -1;
2006 carp_iamatch6_p = NULL;
2007 carp_macmatch6_p = NULL;
2009 carp_ioctl_p = NULL;
2010 carp_attach_p = NULL;
2011 carp_detach_p = NULL;
2012 carp_get_vhid_p = NULL;
2013 carp_linkstate_p = NULL;
2014 carp_forus_p = NULL;
2015 carp_output_p = NULL;
2016 carp_demote_adj_p = NULL;
2017 mtx_unlock(&carp_mtx);
2018 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2019 mtx_destroy(&carp_mtx);
2027 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2028 LIST_INIT(&carp_list);
2029 carp_get_vhid_p = carp_get_vhid;
2030 carp_forus_p = carp_forus;
2031 carp_output_p = carp_output;
2032 carp_linkstate_p = carp_linkstate;
2033 carp_ioctl_p = carp_ioctl;
2034 carp_attach_p = carp_attach;
2035 carp_detach_p = carp_detach;
2036 carp_demote_adj_p = carp_demote_adj;
2038 carp_iamatch6_p = carp_iamatch6;
2039 carp_macmatch6_p = carp_macmatch6;
2040 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2041 (struct protosw *)&in6_carp_protosw);
2042 if (proto_reg[CARP_INET6]) {
2043 printf("carp: error %d attaching to PF_INET6\n",
2044 proto_reg[CARP_INET6]);
2046 return (proto_reg[CARP_INET6]);
2048 err = ip6proto_register(IPPROTO_CARP);
2050 printf("carp: error %d registering with INET6\n", err);
2056 carp_iamatch_p = carp_iamatch;
2057 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2058 if (proto_reg[CARP_INET]) {
2059 printf("carp: error %d attaching to PF_INET\n",
2060 proto_reg[CARP_INET]);
2062 return (proto_reg[CARP_INET]);
2064 err = ipproto_register(IPPROTO_CARP);
2066 printf("carp: error %d registering with INET\n", err);
2075 carp_modevent(module_t mod, int type, void *data)
2079 return carp_mod_load();
2082 mtx_lock(&carp_mtx);
2083 if (LIST_EMPTY(&carp_list))
2086 mtx_unlock(&carp_mtx);
2098 static moduledata_t carp_mod = {
2104 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);