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_var.h>
59 #include <net/if_dl.h>
60 #include <net/if_llatbl.h>
61 #include <net/if_types.h>
62 #include <net/iso88025.h>
63 #include <net/route.h>
66 #if defined(INET) || defined(INET6)
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_carp.h>
70 #include <netinet/ip.h>
71 #include <machine/in_cksum.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/if_ether.h>
79 #include <netinet/icmp6.h>
80 #include <netinet/ip6.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;
148 #define CIF_PROMISC 0x00000001
153 static int proto_reg[] = {-1, -1};
156 * Brief design of carp(4).
158 * Any carp-capable ifnet may have a list of carp softcs hanging off
159 * its ifp->if_carp pointer. Each softc represents one unique virtual
160 * host id, or vhid. The softc has a back pointer to the ifnet. All
161 * softcs are joined in a global list, which has quite limited use.
163 * Any interface address that takes part in CARP negotiation has a
164 * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
165 * AF_INET or AF_INET6 address.
167 * Although, one can get the softc's backpointer to ifnet and traverse
168 * through its ifp->if_addrhead queue to find all interface addresses
169 * involved in CARP, we keep a growable array of ifaddr pointers. This
170 * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
171 * do calls into the network stack, thus avoiding LORs.
175 * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
176 * callout-driven events and ioctl()s.
178 * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to
179 * traverse the global list we use the mutex carp_mtx.
181 * Known issues with locking:
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 /* Accept incoming CARP packets. */
190 static VNET_DEFINE(int, carp_allow) = 1;
191 #define V_carp_allow VNET(carp_allow)
193 /* Preempt slower nodes. */
194 static VNET_DEFINE(int, carp_preempt) = 0;
195 #define V_carp_preempt VNET(carp_preempt)
198 static VNET_DEFINE(int, carp_log) = 1;
199 #define V_carp_log VNET(carp_log)
201 /* Global advskew demotion. */
202 static VNET_DEFINE(int, carp_demotion) = 0;
203 #define V_carp_demotion VNET(carp_demotion)
205 /* Send error demotion factor. */
206 static VNET_DEFINE(int, carp_senderr_adj) = CARP_MAXSKEW;
207 #define V_carp_senderr_adj VNET(carp_senderr_adj)
209 /* Iface down demotion factor. */
210 static VNET_DEFINE(int, carp_ifdown_adj) = CARP_MAXSKEW;
211 #define V_carp_ifdown_adj VNET(carp_ifdown_adj)
213 static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
215 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP");
216 SYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_VNET | CTLFLAG_RW,
217 &VNET_NAME(carp_allow), 0, "Accept incoming CARP packets");
218 SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
219 &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
220 SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
221 &VNET_NAME(carp_log), 0, "CARP log level");
222 SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
223 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
224 0, 0, carp_demote_adj_sysctl, "I",
225 "Adjust demotion factor (skew of advskew)");
226 SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
227 CTLFLAG_VNET | CTLFLAG_RW,
228 &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
229 SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
230 CTLFLAG_VNET | CTLFLAG_RW,
231 &VNET_NAME(carp_ifdown_adj), 0,
232 "Interface down demotion factor adjustment");
234 VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
235 VNET_PCPUSTAT_SYSINIT(carpstats);
236 VNET_PCPUSTAT_SYSUNINIT(carpstats);
238 #define CARPSTATS_ADD(name, val) \
239 counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
240 sizeof(uint64_t)], (val))
241 #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1)
243 SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
244 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
246 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
248 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
249 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
250 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
251 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
252 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
254 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
255 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
256 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
257 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
258 #define CIF_FREE(cif) do { \
260 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
266 #define CARP_LOG(...) do { \
267 if (V_carp_log > 0) \
268 log(LOG_INFO, "carp: " __VA_ARGS__); \
271 #define CARP_DEBUG(...) do { \
272 if (V_carp_log > 1) \
273 log(LOG_DEBUG, __VA_ARGS__); \
276 #define IFNET_FOREACH_IFA(ifp, ifa) \
277 IF_ADDR_LOCK_ASSERT(ifp); \
278 TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
279 if ((ifa)->ifa_carp != NULL)
281 #define CARP_FOREACH_IFA(sc, ifa) \
282 CARP_LOCK_ASSERT(sc); \
284 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
285 ((ifa) = sc->sc_ifas[_i]) != NULL; \
288 #define IFNET_FOREACH_CARP(ifp, sc) \
289 CIF_LOCK_ASSERT(ifp->if_carp); \
290 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
292 #define DEMOTE_ADVSKEW(sc) \
293 (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \
294 CARP_MAXSKEW : ((sc)->sc_advskew + V_carp_demotion))
296 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
297 static struct carp_softc
298 *carp_alloc(struct ifnet *);
299 static void carp_destroy(struct carp_softc *);
300 static struct carp_if
301 *carp_alloc_if(struct ifnet *);
302 static void carp_free_if(struct carp_if *);
303 static void carp_set_state(struct carp_softc *, int, const char* reason);
304 static void carp_sc_state(struct carp_softc *);
305 static void carp_setrun(struct carp_softc *, sa_family_t);
306 static void carp_master_down(void *);
307 static void carp_master_down_locked(struct carp_softc *,
309 static void carp_send_ad(void *);
310 static void carp_send_ad_locked(struct carp_softc *);
311 static void carp_addroute(struct carp_softc *);
312 static void carp_ifa_addroute(struct ifaddr *);
313 static void carp_delroute(struct carp_softc *);
314 static void carp_ifa_delroute(struct ifaddr *);
315 static void carp_send_ad_all(void *, int);
316 static void carp_demote_adj(int, char *);
318 static LIST_HEAD(, carp_softc) carp_list;
319 static struct mtx carp_mtx;
320 static struct sx carp_sx;
321 static struct task carp_sendall_task =
322 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
325 carp_hmac_prepare(struct carp_softc *sc)
327 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
328 uint8_t vhid = sc->sc_vhid & 0xff;
332 struct in_addr last, cur, in;
335 struct in6_addr last6, cur6, in6;
338 CARP_LOCK_ASSERT(sc);
340 /* Compute ipad from key. */
341 bzero(sc->sc_pad, sizeof(sc->sc_pad));
342 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
343 for (i = 0; i < sizeof(sc->sc_pad); i++)
344 sc->sc_pad[i] ^= 0x36;
346 /* Precompute first part of inner hash. */
347 SHA1Init(&sc->sc_sha1);
348 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
349 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
350 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
351 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
357 cur.s_addr = 0xffffffff;
358 CARP_FOREACH_IFA(sc, ifa) {
359 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
360 if (ifa->ifa_addr->sa_family == AF_INET &&
361 ntohl(in.s_addr) > ntohl(last.s_addr) &&
362 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
363 cur.s_addr = in.s_addr;
368 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
372 memset(&cur6, 0, sizeof(cur6));
376 memset(&cur6, 0xff, sizeof(cur6));
377 CARP_FOREACH_IFA(sc, ifa) {
378 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
379 if (IN6_IS_SCOPE_EMBED(&in6))
380 in6.s6_addr16[1] = 0;
381 if (ifa->ifa_addr->sa_family == AF_INET6 &&
382 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
383 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
389 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
393 /* convert ipad to opad */
394 for (i = 0; i < sizeof(sc->sc_pad); i++)
395 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
399 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
400 unsigned char md[20])
404 CARP_LOCK_ASSERT(sc);
406 /* fetch first half of inner hash */
407 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
409 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
410 SHA1Final(md, &sha1ctx);
414 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
415 SHA1Update(&sha1ctx, md, 20);
416 SHA1Final(md, &sha1ctx);
420 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
421 unsigned char md[20])
423 unsigned char md2[20];
425 CARP_LOCK_ASSERT(sc);
427 carp_hmac_generate(sc, counter, md2);
429 return (bcmp(md, md2, sizeof(md2)));
433 * process input packet.
434 * we have rearranged checks order compared to the rfc,
435 * but it seems more efficient this way or not possible otherwise.
439 carp_input(struct mbuf **mp, int *offp, int proto)
441 struct mbuf *m = *mp;
442 struct ip *ip = mtod(m, struct ip *);
443 struct carp_header *ch;
449 CARPSTATS_INC(carps_ipackets);
453 return (IPPROTO_DONE);
456 /* verify that the IP TTL is 255. */
457 if (ip->ip_ttl != CARP_DFLTTL) {
458 CARPSTATS_INC(carps_badttl);
459 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
461 m->m_pkthdr.rcvif->if_xname);
463 return (IPPROTO_DONE);
466 iplen = ip->ip_hl << 2;
468 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
469 CARPSTATS_INC(carps_badlen);
470 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
471 "on %s\n", __func__, m->m_len - sizeof(struct ip),
472 m->m_pkthdr.rcvif->if_xname);
474 return (IPPROTO_DONE);
477 if (iplen + sizeof(*ch) < m->m_len) {
478 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
479 CARPSTATS_INC(carps_hdrops);
480 CARP_DEBUG("%s: pullup failed\n", __func__);
481 return (IPPROTO_DONE);
483 ip = mtod(m, struct ip *);
485 ch = (struct carp_header *)((char *)ip + iplen);
488 * verify that the received packet length is
489 * equal to the CARP header
491 len = iplen + sizeof(*ch);
492 if (len > m->m_pkthdr.len) {
493 CARPSTATS_INC(carps_badlen);
494 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
496 m->m_pkthdr.rcvif->if_xname);
498 return (IPPROTO_DONE);
501 if ((m = m_pullup(m, len)) == NULL) {
502 CARPSTATS_INC(carps_hdrops);
503 return (IPPROTO_DONE);
505 ip = mtod(m, struct ip *);
506 ch = (struct carp_header *)((char *)ip + iplen);
508 /* verify the CARP checksum */
510 if (in_cksum(m, len - iplen)) {
511 CARPSTATS_INC(carps_badsum);
512 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
513 m->m_pkthdr.rcvif->if_xname);
515 return (IPPROTO_DONE);
519 carp_input_c(m, ch, AF_INET);
520 return (IPPROTO_DONE);
526 carp6_input(struct mbuf **mp, int *offp, int proto)
528 struct mbuf *m = *mp;
529 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
530 struct carp_header *ch;
533 CARPSTATS_INC(carps_ipackets6);
537 return (IPPROTO_DONE);
540 /* check if received on a valid carp interface */
541 if (m->m_pkthdr.rcvif->if_carp == NULL) {
542 CARPSTATS_INC(carps_badif);
543 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
544 __func__, m->m_pkthdr.rcvif->if_xname);
546 return (IPPROTO_DONE);
549 /* verify that the IP TTL is 255 */
550 if (ip6->ip6_hlim != CARP_DFLTTL) {
551 CARPSTATS_INC(carps_badttl);
552 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
553 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
555 return (IPPROTO_DONE);
558 /* verify that we have a complete carp packet */
560 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
562 CARPSTATS_INC(carps_badlen);
563 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
564 return (IPPROTO_DONE);
568 /* verify the CARP checksum */
570 if (in_cksum(m, sizeof(*ch))) {
571 CARPSTATS_INC(carps_badsum);
572 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
573 m->m_pkthdr.rcvif->if_xname);
575 return (IPPROTO_DONE);
579 carp_input_c(m, ch, AF_INET6);
580 return (IPPROTO_DONE);
585 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
587 struct ifnet *ifp = m->m_pkthdr.rcvif;
589 struct carp_softc *sc;
590 uint64_t tmp_counter;
591 struct timeval sc_tv, ch_tv;
593 /* verify that the VHID is valid on the receiving interface */
595 IFNET_FOREACH_IFA(ifp, ifa)
596 if (ifa->ifa_addr->sa_family == af &&
597 ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
601 IF_ADDR_RUNLOCK(ifp);
604 CARPSTATS_INC(carps_badvhid);
609 /* verify the CARP version. */
610 if (ch->carp_version != CARP_VERSION) {
611 CARPSTATS_INC(carps_badver);
612 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
623 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
624 CARPSTATS_INC(carps_badauth);
625 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
626 sc->sc_vhid, ifp->if_xname);
630 tmp_counter = ntohl(ch->carp_counter[0]);
631 tmp_counter = tmp_counter<<32;
632 tmp_counter += ntohl(ch->carp_counter[1]);
634 /* XXX Replay protection goes here */
636 sc->sc_init_counter = 0;
637 sc->sc_counter = tmp_counter;
639 sc_tv.tv_sec = sc->sc_advbase;
640 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
641 ch_tv.tv_sec = ch->carp_advbase;
642 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
644 switch (sc->sc_state) {
649 * If we receive an advertisement from a master who's going to
650 * be more frequent than us, go into BACKUP state.
652 if (timevalcmp(&sc_tv, &ch_tv, >) ||
653 timevalcmp(&sc_tv, &ch_tv, ==)) {
654 callout_stop(&sc->sc_ad_tmo);
655 carp_set_state(sc, BACKUP,
656 "more frequent advertisement received");
663 * If we're pre-empting masters who advertise slower than us,
664 * and this one claims to be slower, treat him as down.
666 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
667 carp_master_down_locked(sc,
668 "preempting a slower master");
673 * If the master is going to advertise at such a low frequency
674 * that he's guaranteed to time out, we'd might as well just
675 * treat him as timed out now.
677 sc_tv.tv_sec = sc->sc_advbase * 3;
678 if (timevalcmp(&sc_tv, &ch_tv, <)) {
679 carp_master_down_locked(sc, "master will time out");
684 * Otherwise, we reset the counter and wait for the next
697 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
701 if (sc->sc_init_counter) {
702 /* this could also be seconds since unix epoch */
703 sc->sc_counter = arc4random();
704 sc->sc_counter = sc->sc_counter << 32;
705 sc->sc_counter += arc4random();
709 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
710 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
712 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
714 /* Tag packet for carp_output */
715 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
716 M_NOWAIT)) == NULL) {
718 CARPSTATS_INC(carps_onomem);
721 bcopy(&sc, mtag + 1, sizeof(sc));
722 m_tag_prepend(m, mtag);
728 * To avoid LORs and possible recursions this function shouldn't
729 * be called directly, but scheduled via taskqueue.
732 carp_send_ad_all(void *ctx __unused, int pending __unused)
734 struct carp_softc *sc;
737 LIST_FOREACH(sc, &carp_list, sc_next)
738 if (sc->sc_state == MASTER) {
740 CURVNET_SET(sc->sc_carpdev->if_vnet);
741 carp_send_ad_locked(sc);
745 mtx_unlock(&carp_mtx);
748 /* Send a periodic advertisement, executed in callout context. */
750 carp_send_ad(void *v)
752 struct carp_softc *sc = v;
754 CARP_LOCK_ASSERT(sc);
755 CURVNET_SET(sc->sc_carpdev->if_vnet);
756 carp_send_ad_locked(sc);
762 carp_send_ad_error(struct carp_softc *sc, int error)
766 if (sc->sc_sendad_errors < INT_MAX)
767 sc->sc_sendad_errors++;
768 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
769 static const char fmt[] = "send error %d on %s";
770 char msg[sizeof(fmt) + IFNAMSIZ];
772 sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
773 carp_demote_adj(V_carp_senderr_adj, msg);
775 sc->sc_sendad_success = 0;
777 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS &&
778 ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
779 static const char fmt[] = "send ok on %s";
780 char msg[sizeof(fmt) + IFNAMSIZ];
782 sprintf(msg, fmt, sc->sc_carpdev->if_xname);
783 carp_demote_adj(-V_carp_senderr_adj, msg);
784 sc->sc_sendad_errors = 0;
786 sc->sc_sendad_errors = 0;
791 carp_send_ad_locked(struct carp_softc *sc)
793 struct carp_header ch;
797 struct carp_header *ch_ptr;
801 CARP_LOCK_ASSERT(sc);
803 advskew = DEMOTE_ADVSKEW(sc);
804 tv.tv_sec = sc->sc_advbase;
805 tv.tv_usec = advskew * 1000000 / 256;
807 ch.carp_version = CARP_VERSION;
808 ch.carp_type = CARP_ADVERTISEMENT;
809 ch.carp_vhid = sc->sc_vhid;
810 ch.carp_advbase = sc->sc_advbase;
811 ch.carp_advskew = advskew;
812 ch.carp_authlen = 7; /* XXX DEFINE */
813 ch.carp_pad1 = 0; /* must be zero */
816 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
822 m = m_gethdr(M_NOWAIT, MT_DATA);
824 CARPSTATS_INC(carps_onomem);
827 len = sizeof(*ip) + sizeof(ch);
828 m->m_pkthdr.len = len;
829 m->m_pkthdr.rcvif = NULL;
831 M_ALIGN(m, m->m_len);
832 m->m_flags |= M_MCAST;
833 ip = mtod(m, struct ip *);
834 ip->ip_v = IPVERSION;
835 ip->ip_hl = sizeof(*ip) >> 2;
836 ip->ip_tos = IPTOS_LOWDELAY;
837 ip->ip_len = htons(len);
838 ip->ip_off = htons(IP_DF);
839 ip->ip_ttl = CARP_DFLTTL;
840 ip->ip_p = IPPROTO_CARP;
844 bzero(&sa, sizeof(sa));
845 sa.sa_family = AF_INET;
846 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
849 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
852 ip->ip_src.s_addr = 0;
853 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
855 ch_ptr = (struct carp_header *)(&ip[1]);
856 bcopy(&ch, ch_ptr, sizeof(ch));
857 if (carp_prepare_ad(m, sc, ch_ptr))
860 m->m_data += sizeof(*ip);
861 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
862 m->m_data -= sizeof(*ip);
864 CARPSTATS_INC(carps_opackets);
866 carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
867 &sc->sc_carpdev->if_carp->cif_imo, NULL));
871 if (sc->sc_naddrs6) {
874 m = m_gethdr(M_NOWAIT, MT_DATA);
876 CARPSTATS_INC(carps_onomem);
879 len = sizeof(*ip6) + sizeof(ch);
880 m->m_pkthdr.len = len;
881 m->m_pkthdr.rcvif = NULL;
883 M_ALIGN(m, m->m_len);
884 m->m_flags |= M_MCAST;
885 ip6 = mtod(m, struct ip6_hdr *);
886 bzero(ip6, sizeof(*ip6));
887 ip6->ip6_vfc |= IPV6_VERSION;
888 ip6->ip6_hlim = CARP_DFLTTL;
889 ip6->ip6_nxt = IPPROTO_CARP;
890 bzero(&sa, sizeof(sa));
892 /* set the source address */
893 sa.sa_family = AF_INET6;
894 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
896 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
897 sizeof(struct in6_addr));
900 /* This should never happen with IPv6. */
901 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
903 /* Set the multicast destination. */
904 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
905 ip6->ip6_dst.s6_addr8[15] = 0x12;
906 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
908 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
912 ch_ptr = (struct carp_header *)(&ip6[1]);
913 bcopy(&ch, ch_ptr, sizeof(ch));
914 if (carp_prepare_ad(m, sc, ch_ptr))
917 m->m_data += sizeof(*ip6);
918 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
919 m->m_data -= sizeof(*ip6);
921 CARPSTATS_INC(carps_opackets6);
923 carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
924 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
929 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
933 carp_addroute(struct carp_softc *sc)
937 CARP_FOREACH_IFA(sc, ifa)
938 carp_ifa_addroute(ifa);
942 carp_ifa_addroute(struct ifaddr *ifa)
945 switch (ifa->ifa_addr->sa_family) {
948 in_addprefix(ifatoia(ifa), RTF_UP);
949 ifa_add_loopback_route(ifa,
950 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
955 ifa_add_loopback_route(ifa,
956 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
957 nd6_add_ifa_lle(ifatoia6(ifa));
964 carp_delroute(struct carp_softc *sc)
968 CARP_FOREACH_IFA(sc, ifa)
969 carp_ifa_delroute(ifa);
973 carp_ifa_delroute(struct ifaddr *ifa)
976 switch (ifa->ifa_addr->sa_family) {
979 ifa_del_loopback_route(ifa,
980 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
981 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
986 ifa_del_loopback_route(ifa,
987 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
988 nd6_rem_ifa_lle(ifatoia6(ifa), 1);
995 carp_master(struct ifaddr *ifa)
997 struct carp_softc *sc = ifa->ifa_carp;
999 return (sc->sc_state == MASTER);
1004 * Broadcast a gratuitous ARP request containing
1005 * the virtual router MAC address for each IP address
1006 * associated with the virtual router.
1009 carp_send_arp(struct carp_softc *sc)
1012 struct in_addr addr;
1014 CARP_FOREACH_IFA(sc, ifa) {
1015 if (ifa->ifa_addr->sa_family != AF_INET)
1017 addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1018 arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr));
1023 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1025 struct carp_softc *sc = ifa->ifa_carp;
1027 if (sc->sc_state == MASTER) {
1028 *enaddr = LLADDR(&sc->sc_addr);
1038 carp_send_na(struct carp_softc *sc)
1040 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1042 struct in6_addr *in6;
1044 CARP_FOREACH_IFA(sc, ifa) {
1045 if (ifa->ifa_addr->sa_family != AF_INET6)
1049 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1050 ND_NA_FLAG_OVERRIDE, 1, NULL);
1051 DELAY(1000); /* XXX */
1056 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1057 * matches and is not a carp address. Returns NULL otherwise.
1060 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1066 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1067 if (ifa->ifa_addr->sa_family != AF_INET6)
1069 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1071 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1077 IF_ADDR_RUNLOCK(ifp);
1083 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1088 IFNET_FOREACH_IFA(ifp, ifa)
1089 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1090 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1091 struct carp_softc *sc = ifa->ifa_carp;
1094 IF_ADDR_RUNLOCK(ifp);
1096 mtag = m_tag_get(PACKET_TAG_CARP,
1097 sizeof(struct carp_softc *), M_NOWAIT);
1099 /* Better a bit than nothing. */
1100 return (LLADDR(&sc->sc_addr));
1102 bcopy(&sc, mtag + 1, sizeof(sc));
1103 m_tag_prepend(m, mtag);
1105 return (LLADDR(&sc->sc_addr));
1107 IF_ADDR_RUNLOCK(ifp);
1114 carp_forus(struct ifnet *ifp, u_char *dhost)
1116 struct carp_softc *sc;
1117 uint8_t *ena = dhost;
1119 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1122 CIF_LOCK(ifp->if_carp);
1123 IFNET_FOREACH_CARP(ifp, sc) {
1125 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1128 CIF_UNLOCK(ifp->if_carp);
1133 CIF_UNLOCK(ifp->if_carp);
1138 /* Master down timeout event, executed in callout context. */
1140 carp_master_down(void *v)
1142 struct carp_softc *sc = v;
1144 CARP_LOCK_ASSERT(sc);
1146 CURVNET_SET(sc->sc_carpdev->if_vnet);
1147 if (sc->sc_state == BACKUP) {
1148 carp_master_down_locked(sc, "master timed out");
1156 carp_master_down_locked(struct carp_softc *sc, const char *reason)
1159 CARP_LOCK_ASSERT(sc);
1161 switch (sc->sc_state) {
1163 carp_set_state(sc, MASTER, reason);
1164 carp_send_ad_locked(sc);
1177 panic("carp: VHID %u@%s: master_down event in %s state\n",
1179 sc->sc_carpdev->if_xname,
1180 sc->sc_state ? "MASTER" : "INIT");
1187 * When in backup state, af indicates whether to reset the master down timer
1188 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1191 carp_setrun(struct carp_softc *sc, sa_family_t af)
1195 CARP_LOCK_ASSERT(sc);
1197 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1198 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1199 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1202 switch (sc->sc_state) {
1204 carp_set_state(sc, BACKUP, "initialization complete");
1208 callout_stop(&sc->sc_ad_tmo);
1209 tv.tv_sec = 3 * sc->sc_advbase;
1210 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1214 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1215 carp_master_down, sc);
1220 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1221 carp_master_down, sc);
1227 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1228 carp_master_down, sc);
1232 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1233 carp_master_down, sc);
1239 tv.tv_sec = sc->sc_advbase;
1240 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1241 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1248 * Setup multicast structures.
1251 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1253 struct ifnet *ifp = cif->cif_ifp;
1260 struct ip_moptions *imo = &cif->cif_imo;
1261 struct in_addr addr;
1263 if (imo->imo_membership)
1266 imo->imo_membership = (struct in_multi **)malloc(
1267 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1269 imo->imo_mfilters = NULL;
1270 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1271 imo->imo_multicast_vif = -1;
1273 addr.s_addr = htonl(INADDR_CARP_GROUP);
1274 if ((error = in_joingroup(ifp, &addr, NULL,
1275 &imo->imo_membership[0])) != 0) {
1276 free(imo->imo_membership, M_CARP);
1279 imo->imo_num_memberships++;
1280 imo->imo_multicast_ifp = ifp;
1281 imo->imo_multicast_ttl = CARP_DFLTTL;
1282 imo->imo_multicast_loop = 0;
1289 struct ip6_moptions *im6o = &cif->cif_im6o;
1290 struct in6_addr in6;
1291 struct in6_multi *in6m;
1293 if (im6o->im6o_membership)
1296 im6o->im6o_membership = (struct in6_multi **)malloc(
1297 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1299 im6o->im6o_mfilters = NULL;
1300 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1301 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1302 im6o->im6o_multicast_ifp = ifp;
1304 /* Join IPv6 CARP multicast group. */
1305 bzero(&in6, sizeof(in6));
1306 in6.s6_addr16[0] = htons(0xff02);
1307 in6.s6_addr8[15] = 0x12;
1308 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1309 free(im6o->im6o_membership, M_CARP);
1313 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1314 free(im6o->im6o_membership, M_CARP);
1317 im6o->im6o_membership[0] = in6m;
1318 im6o->im6o_num_memberships++;
1320 /* Join solicited multicast address. */
1321 bzero(&in6, sizeof(in6));
1322 in6.s6_addr16[0] = htons(0xff02);
1323 in6.s6_addr32[1] = 0;
1324 in6.s6_addr32[2] = htonl(1);
1325 in6.s6_addr32[3] = 0;
1326 in6.s6_addr8[12] = 0xff;
1327 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1328 in6_mc_leave(im6o->im6o_membership[0], NULL);
1329 free(im6o->im6o_membership, M_CARP);
1333 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1334 in6_mc_leave(im6o->im6o_membership[0], NULL);
1335 free(im6o->im6o_membership, M_CARP);
1338 im6o->im6o_membership[1] = in6m;
1339 im6o->im6o_num_memberships++;
1349 * Free multicast structures.
1352 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1355 sx_assert(&carp_sx, SA_XLOCKED);
1360 if (cif->cif_naddrs == 0) {
1361 struct ip_moptions *imo = &cif->cif_imo;
1363 in_leavegroup(imo->imo_membership[0], NULL);
1364 KASSERT(imo->imo_mfilters == NULL,
1365 ("%s: imo_mfilters != NULL", __func__));
1366 free(imo->imo_membership, M_CARP);
1367 imo->imo_membership = NULL;
1374 if (cif->cif_naddrs6 == 0) {
1375 struct ip6_moptions *im6o = &cif->cif_im6o;
1377 in6_mc_leave(im6o->im6o_membership[0], NULL);
1378 in6_mc_leave(im6o->im6o_membership[1], NULL);
1379 KASSERT(im6o->im6o_mfilters == NULL,
1380 ("%s: im6o_mfilters != NULL", __func__));
1381 free(im6o->im6o_membership, M_CARP);
1382 im6o->im6o_membership = NULL;
1390 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1393 struct carp_softc *sc;
1398 switch (sa->sa_family) {
1411 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1415 bcopy(mtag + 1, &sc, sizeof(sc));
1417 /* Set the source MAC address to the Virtual Router MAC Address. */
1418 switch (ifp->if_type) {
1422 struct ether_header *eh;
1424 eh = mtod(m, struct ether_header *);
1425 eh->ether_shost[0] = 0;
1426 eh->ether_shost[1] = 0;
1427 eh->ether_shost[2] = 0x5e;
1428 eh->ether_shost[3] = 0;
1429 eh->ether_shost[4] = 1;
1430 eh->ether_shost[5] = sc->sc_vhid;
1434 struct fddi_header *fh;
1436 fh = mtod(m, struct fddi_header *);
1437 fh->fddi_shost[0] = 0;
1438 fh->fddi_shost[1] = 0;
1439 fh->fddi_shost[2] = 0x5e;
1440 fh->fddi_shost[3] = 0;
1441 fh->fddi_shost[4] = 1;
1442 fh->fddi_shost[5] = sc->sc_vhid;
1445 case IFT_ISO88025: {
1446 struct iso88025_header *th;
1447 th = mtod(m, struct iso88025_header *);
1448 th->iso88025_shost[0] = 3;
1449 th->iso88025_shost[1] = 0;
1450 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1451 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1452 th->iso88025_shost[4] = 0;
1453 th->iso88025_shost[5] = 0;
1457 printf("%s: carp is not supported for the %d interface type\n",
1458 ifp->if_xname, ifp->if_type);
1459 return (EOPNOTSUPP);
1465 static struct carp_softc*
1466 carp_alloc(struct ifnet *ifp)
1468 struct carp_softc *sc;
1469 struct carp_if *cif;
1471 if ((cif = ifp->if_carp) == NULL)
1472 cif = carp_alloc_if(ifp);
1474 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1476 sc->sc_advbase = CARP_DFLTINTV;
1477 sc->sc_vhid = -1; /* required setting */
1478 sc->sc_init_counter = 1;
1479 sc->sc_state = INIT;
1481 sc->sc_ifasiz = sizeof(struct ifaddr *);
1482 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1483 sc->sc_carpdev = ifp;
1487 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1490 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1492 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1495 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1498 mtx_lock(&carp_mtx);
1499 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1500 mtx_unlock(&carp_mtx);
1506 carp_grow_ifas(struct carp_softc *sc)
1508 struct ifaddr **new;
1510 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO);
1512 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1513 free(sc->sc_ifas, M_CARP);
1520 carp_destroy(struct carp_softc *sc)
1522 struct ifnet *ifp = sc->sc_carpdev;
1523 struct carp_if *cif = ifp->if_carp;
1525 sx_assert(&carp_sx, SA_XLOCKED);
1527 if (sc->sc_suppress)
1528 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1532 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1535 mtx_lock(&carp_mtx);
1536 LIST_REMOVE(sc, sc_next);
1537 mtx_unlock(&carp_mtx);
1539 callout_drain(&sc->sc_ad_tmo);
1541 callout_drain(&sc->sc_md_tmo);
1544 callout_drain(&sc->sc_md6_tmo);
1546 CARP_LOCK_DESTROY(sc);
1548 free(sc->sc_ifas, M_CARP);
1552 static struct carp_if*
1553 carp_alloc_if(struct ifnet *ifp)
1555 struct carp_if *cif;
1558 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1560 if ((error = ifpromisc(ifp, 1)) != 0)
1561 printf("%s: ifpromisc(%s) failed: %d\n",
1562 __func__, ifp->if_xname, error);
1564 cif->cif_flags |= CIF_PROMISC;
1568 TAILQ_INIT(&cif->cif_vrs);
1573 IF_ADDR_WUNLOCK(ifp);
1579 carp_free_if(struct carp_if *cif)
1581 struct ifnet *ifp = cif->cif_ifp;
1583 CIF_LOCK_ASSERT(cif);
1584 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1588 ifp->if_carp = NULL;
1589 IF_ADDR_WUNLOCK(ifp);
1591 CIF_LOCK_DESTROY(cif);
1593 if (cif->cif_flags & CIF_PROMISC)
1601 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1605 carpr->carpr_state = sc->sc_state;
1606 carpr->carpr_vhid = sc->sc_vhid;
1607 carpr->carpr_advbase = sc->sc_advbase;
1608 carpr->carpr_advskew = sc->sc_advskew;
1610 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1612 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1617 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1619 struct carpreq carpr;
1621 struct carp_softc *sc = NULL;
1622 int error = 0, locked = 0;
1624 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1627 ifp = ifunit_ref(ifr->ifr_name);
1631 switch (ifp->if_type) {
1643 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1644 error = EADDRNOTAVAIL;
1651 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1653 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1654 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1660 CIF_LOCK(ifp->if_carp);
1661 IFNET_FOREACH_CARP(ifp, sc)
1662 if (sc->sc_vhid == carpr.carpr_vhid)
1664 CIF_UNLOCK(ifp->if_carp);
1667 sc = carp_alloc(ifp);
1669 sc->sc_vhid = carpr.carpr_vhid;
1670 LLADDR(&sc->sc_addr)[0] = 0;
1671 LLADDR(&sc->sc_addr)[1] = 0;
1672 LLADDR(&sc->sc_addr)[2] = 0x5e;
1673 LLADDR(&sc->sc_addr)[3] = 0;
1674 LLADDR(&sc->sc_addr)[4] = 1;
1675 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1679 if (carpr.carpr_advbase > 0) {
1680 if (carpr.carpr_advbase > 255 ||
1681 carpr.carpr_advbase < CARP_DFLTINTV) {
1685 sc->sc_advbase = carpr.carpr_advbase;
1687 if (carpr.carpr_advskew >= 255) {
1691 sc->sc_advskew = carpr.carpr_advskew;
1692 if (carpr.carpr_key[0] != '\0') {
1693 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1694 carp_hmac_prepare(sc);
1696 if (sc->sc_state != INIT &&
1697 carpr.carpr_state != sc->sc_state) {
1698 switch (carpr.carpr_state) {
1700 callout_stop(&sc->sc_ad_tmo);
1701 carp_set_state(sc, BACKUP,
1702 "user requested via ifconfig");
1707 carp_master_down_locked(sc,
1708 "user requested via ifconfig");
1720 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1724 if (carpr.carpr_count < 1) {
1728 if (ifp->if_carp == NULL) {
1733 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1734 if (carpr.carpr_vhid != 0) {
1735 CIF_LOCK(ifp->if_carp);
1736 IFNET_FOREACH_CARP(ifp, sc)
1737 if (sc->sc_vhid == carpr.carpr_vhid)
1739 CIF_UNLOCK(ifp->if_carp);
1744 carp_carprcp(&carpr, sc, priveleged);
1745 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1750 CIF_LOCK(ifp->if_carp);
1751 IFNET_FOREACH_CARP(ifp, sc)
1754 if (count > carpr.carpr_count) {
1755 CIF_UNLOCK(ifp->if_carp);
1761 IFNET_FOREACH_CARP(ifp, sc) {
1762 carp_carprcp(&carpr, sc, priveleged);
1763 carpr.carpr_count = count;
1764 error = copyout(&carpr, ifr->ifr_data +
1765 (i * sizeof(carpr)), sizeof(carpr));
1767 CIF_UNLOCK(ifp->if_carp);
1772 CIF_UNLOCK(ifp->if_carp);
1779 sx_xunlock(&carp_sx);
1790 carp_get_vhid(struct ifaddr *ifa)
1793 if (ifa == NULL || ifa->ifa_carp == NULL)
1796 return (ifa->ifa_carp->sc_vhid);
1800 carp_attach(struct ifaddr *ifa, int vhid)
1802 struct ifnet *ifp = ifa->ifa_ifp;
1803 struct carp_if *cif = ifp->if_carp;
1804 struct carp_softc *sc;
1807 KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa));
1809 switch (ifa->ifa_addr->sa_family) {
1818 return (EPROTOTYPE);
1822 if (ifp->if_carp == NULL) {
1823 sx_xunlock(&carp_sx);
1824 return (ENOPROTOOPT);
1828 IFNET_FOREACH_CARP(ifp, sc)
1829 if (sc->sc_vhid == vhid)
1833 sx_xunlock(&carp_sx);
1837 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1840 sx_xunlock(&carp_sx);
1844 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1845 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1848 switch (ifa->ifa_addr->sa_family) {
1866 sc->sc_ifas[index - 1] = ifa;
1868 carp_hmac_prepare(sc);
1872 sx_xunlock(&carp_sx);
1878 carp_detach(struct ifaddr *ifa)
1880 struct ifnet *ifp = ifa->ifa_ifp;
1881 struct carp_if *cif = ifp->if_carp;
1882 struct carp_softc *sc = ifa->ifa_carp;
1885 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1891 index = sc->sc_naddrs + sc->sc_naddrs6;
1892 for (i = 0; i < index; i++)
1893 if (sc->sc_ifas[i] == ifa)
1895 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1896 for (; i < index - 1; i++)
1897 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1898 sc->sc_ifas[index - 1] = NULL;
1900 switch (ifa->ifa_addr->sa_family) {
1915 carp_ifa_delroute(ifa);
1916 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1918 ifa->ifa_carp = NULL;
1921 carp_hmac_prepare(sc);
1924 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0)
1931 sx_xunlock(&carp_sx);
1935 carp_set_state(struct carp_softc *sc, int state, const char *reason)
1938 CARP_LOCK_ASSERT(sc);
1940 if (sc->sc_state != state) {
1941 const char *carp_states[] = { CARP_STATES };
1942 char subsys[IFNAMSIZ+5];
1944 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1945 sc->sc_carpdev->if_xname);
1947 CARP_LOG("%s: %s -> %s (%s)\n", subsys,
1948 carp_states[sc->sc_state], carp_states[state], reason);
1950 sc->sc_state = state;
1952 devctl_notify("CARP", subsys, carp_states[state], NULL);
1957 carp_linkstate(struct ifnet *ifp)
1959 struct carp_softc *sc;
1961 CIF_LOCK(ifp->if_carp);
1962 IFNET_FOREACH_CARP(ifp, sc) {
1967 CIF_UNLOCK(ifp->if_carp);
1971 carp_sc_state(struct carp_softc *sc)
1974 CARP_LOCK_ASSERT(sc);
1976 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1977 !(sc->sc_carpdev->if_flags & IFF_UP)) {
1978 callout_stop(&sc->sc_ad_tmo);
1980 callout_stop(&sc->sc_md_tmo);
1983 callout_stop(&sc->sc_md6_tmo);
1985 carp_set_state(sc, INIT, "hardware interface down");
1987 if (!sc->sc_suppress)
1988 carp_demote_adj(V_carp_ifdown_adj, "interface down");
1989 sc->sc_suppress = 1;
1991 carp_set_state(sc, INIT, "hardware interface up");
1993 if (sc->sc_suppress)
1994 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
1995 sc->sc_suppress = 0;
2000 carp_demote_adj(int adj, char *reason)
2002 atomic_add_int(&V_carp_demotion, adj);
2003 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2004 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2008 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2012 new = V_carp_demotion;
2013 error = sysctl_handle_int(oidp, &new, 0, req);
2014 if (error || !req->newptr)
2017 carp_demote_adj(new, "sysctl");
2023 extern struct domain inetdomain;
2024 static struct protosw in_carp_protosw = {
2025 .pr_type = SOCK_RAW,
2026 .pr_domain = &inetdomain,
2027 .pr_protocol = IPPROTO_CARP,
2028 .pr_flags = PR_ATOMIC|PR_ADDR,
2029 .pr_input = carp_input,
2030 .pr_output = rip_output,
2031 .pr_ctloutput = rip_ctloutput,
2032 .pr_usrreqs = &rip_usrreqs
2037 extern struct domain inet6domain;
2038 static struct protosw in6_carp_protosw = {
2039 .pr_type = SOCK_RAW,
2040 .pr_domain = &inet6domain,
2041 .pr_protocol = IPPROTO_CARP,
2042 .pr_flags = PR_ATOMIC|PR_ADDR,
2043 .pr_input = carp6_input,
2044 .pr_output = rip6_output,
2045 .pr_ctloutput = rip6_ctloutput,
2046 .pr_usrreqs = &rip6_usrreqs
2051 carp_mod_cleanup(void)
2055 if (proto_reg[CARP_INET] == 0) {
2056 (void)ipproto_unregister(IPPROTO_CARP);
2057 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2058 proto_reg[CARP_INET] = -1;
2060 carp_iamatch_p = NULL;
2063 if (proto_reg[CARP_INET6] == 0) {
2064 (void)ip6proto_unregister(IPPROTO_CARP);
2065 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2066 proto_reg[CARP_INET6] = -1;
2068 carp_iamatch6_p = NULL;
2069 carp_macmatch6_p = NULL;
2071 carp_ioctl_p = NULL;
2072 carp_attach_p = NULL;
2073 carp_detach_p = NULL;
2074 carp_get_vhid_p = NULL;
2075 carp_linkstate_p = NULL;
2076 carp_forus_p = NULL;
2077 carp_output_p = NULL;
2078 carp_demote_adj_p = NULL;
2079 carp_master_p = NULL;
2080 mtx_unlock(&carp_mtx);
2081 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2082 mtx_destroy(&carp_mtx);
2083 sx_destroy(&carp_sx);
2091 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2092 sx_init(&carp_sx, "carp_sx");
2093 LIST_INIT(&carp_list);
2094 carp_get_vhid_p = carp_get_vhid;
2095 carp_forus_p = carp_forus;
2096 carp_output_p = carp_output;
2097 carp_linkstate_p = carp_linkstate;
2098 carp_ioctl_p = carp_ioctl;
2099 carp_attach_p = carp_attach;
2100 carp_detach_p = carp_detach;
2101 carp_demote_adj_p = carp_demote_adj;
2102 carp_master_p = carp_master;
2104 carp_iamatch6_p = carp_iamatch6;
2105 carp_macmatch6_p = carp_macmatch6;
2106 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2107 (struct protosw *)&in6_carp_protosw);
2108 if (proto_reg[CARP_INET6]) {
2109 printf("carp: error %d attaching to PF_INET6\n",
2110 proto_reg[CARP_INET6]);
2112 return (proto_reg[CARP_INET6]);
2114 err = ip6proto_register(IPPROTO_CARP);
2116 printf("carp: error %d registering with INET6\n", err);
2122 carp_iamatch_p = carp_iamatch;
2123 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2124 if (proto_reg[CARP_INET]) {
2125 printf("carp: error %d attaching to PF_INET\n",
2126 proto_reg[CARP_INET]);
2128 return (proto_reg[CARP_INET]);
2130 err = ipproto_register(IPPROTO_CARP);
2132 printf("carp: error %d registering with INET\n", err);
2141 carp_modevent(module_t mod, int type, void *data)
2145 return carp_mod_load();
2148 mtx_lock(&carp_mtx);
2149 if (LIST_EMPTY(&carp_list))
2152 mtx_unlock(&carp_mtx);
2164 static moduledata_t carp_mod = {
2170 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);