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 * - There is no protection for races between two ioctl() requests,
184 * neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all
185 * interface ioctl()s should be serialized right in net/if.c.
186 * - Sending ad, we put the pointer to the softc in an mtag, and no reference
187 * counting is done on the softc.
188 * - On module unload we may race (?) with packet processing thread
189 * dereferencing our function pointers.
192 /* Accept incoming CARP packets. */
193 static VNET_DEFINE(int, carp_allow) = 1;
194 #define V_carp_allow VNET(carp_allow)
196 /* Preempt slower nodes. */
197 static VNET_DEFINE(int, carp_preempt) = 0;
198 #define V_carp_preempt VNET(carp_preempt)
201 static VNET_DEFINE(int, carp_log) = 1;
202 #define V_carp_log VNET(carp_log)
204 /* Global advskew demotion. */
205 static VNET_DEFINE(int, carp_demotion) = 0;
206 #define V_carp_demotion VNET(carp_demotion)
208 /* Send error demotion factor. */
209 static VNET_DEFINE(int, carp_senderr_adj) = CARP_MAXSKEW;
210 #define V_carp_senderr_adj VNET(carp_senderr_adj)
212 /* Iface down demotion factor. */
213 static VNET_DEFINE(int, carp_ifdown_adj) = CARP_MAXSKEW;
214 #define V_carp_ifdown_adj VNET(carp_ifdown_adj)
216 static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
218 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP");
219 SYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_VNET | CTLFLAG_RW,
220 &VNET_NAME(carp_allow), 0, "Accept incoming CARP packets");
221 SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
222 &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
223 SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
224 &VNET_NAME(carp_log), 0, "CARP log level");
225 SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
226 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
227 0, 0, carp_demote_adj_sysctl, "I",
228 "Adjust demotion factor (skew of advskew)");
229 SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
230 CTLFLAG_VNET | CTLFLAG_RW,
231 &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
232 SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
233 CTLFLAG_VNET | CTLFLAG_RW,
234 &VNET_NAME(carp_ifdown_adj), 0,
235 "Interface down demotion factor adjustment");
237 VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
238 VNET_PCPUSTAT_SYSINIT(carpstats);
239 VNET_PCPUSTAT_SYSUNINIT(carpstats);
241 #define CARPSTATS_ADD(name, val) \
242 counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
243 sizeof(uint64_t)], (val))
244 #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1)
246 SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
247 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
249 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
251 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
252 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
253 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
254 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
255 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
257 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
258 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
259 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
260 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
261 #define CIF_FREE(cif) do { \
262 CIF_LOCK_ASSERT(cif); \
263 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
269 #define CARP_LOG(...) do { \
270 if (V_carp_log > 0) \
271 log(LOG_INFO, "carp: " __VA_ARGS__); \
274 #define CARP_DEBUG(...) do { \
275 if (V_carp_log > 1) \
276 log(LOG_DEBUG, __VA_ARGS__); \
279 #define IFNET_FOREACH_IFA(ifp, ifa) \
280 IF_ADDR_LOCK_ASSERT(ifp); \
281 TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
282 if ((ifa)->ifa_carp != NULL)
284 #define CARP_FOREACH_IFA(sc, ifa) \
285 CARP_LOCK_ASSERT(sc); \
287 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
288 ((ifa) = sc->sc_ifas[_i]) != NULL; \
291 #define IFNET_FOREACH_CARP(ifp, sc) \
292 CIF_LOCK_ASSERT(ifp->if_carp); \
293 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
295 #define DEMOTE_ADVSKEW(sc) \
296 (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \
297 CARP_MAXSKEW : ((sc)->sc_advskew + V_carp_demotion))
299 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
300 static struct carp_softc
301 *carp_alloc(struct ifnet *);
302 static void carp_detach_locked(struct ifaddr *);
303 static void carp_destroy(struct carp_softc *);
304 static struct carp_if
305 *carp_alloc_if(struct ifnet *);
306 static void carp_free_if(struct carp_if *);
307 static void carp_set_state(struct carp_softc *, int);
308 static void carp_sc_state(struct carp_softc *);
309 static void carp_setrun(struct carp_softc *, sa_family_t);
310 static void carp_master_down(void *);
311 static void carp_master_down_locked(struct carp_softc *);
312 static void carp_send_ad(void *);
313 static void carp_send_ad_locked(struct carp_softc *);
314 static void carp_addroute(struct carp_softc *);
315 static void carp_ifa_addroute(struct ifaddr *);
316 static void carp_delroute(struct carp_softc *);
317 static void carp_ifa_delroute(struct ifaddr *);
318 static void carp_send_ad_all(void *, int);
319 static void carp_demote_adj(int, char *);
321 static LIST_HEAD(, carp_softc) carp_list;
322 static struct mtx carp_mtx;
323 static struct task carp_sendall_task =
324 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
327 carp_hmac_prepare(struct carp_softc *sc)
329 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
330 uint8_t vhid = sc->sc_vhid & 0xff;
334 struct in_addr last, cur, in;
337 struct in6_addr last6, cur6, in6;
340 CARP_LOCK_ASSERT(sc);
342 /* Compute ipad from key. */
343 bzero(sc->sc_pad, sizeof(sc->sc_pad));
344 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
345 for (i = 0; i < sizeof(sc->sc_pad); i++)
346 sc->sc_pad[i] ^= 0x36;
348 /* Precompute first part of inner hash. */
349 SHA1Init(&sc->sc_sha1);
350 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
351 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
352 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
353 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
359 cur.s_addr = 0xffffffff;
360 CARP_FOREACH_IFA(sc, ifa) {
361 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
362 if (ifa->ifa_addr->sa_family == AF_INET &&
363 ntohl(in.s_addr) > ntohl(last.s_addr) &&
364 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
365 cur.s_addr = in.s_addr;
370 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
374 memset(&cur6, 0, sizeof(cur6));
378 memset(&cur6, 0xff, sizeof(cur6));
379 CARP_FOREACH_IFA(sc, ifa) {
380 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
381 if (IN6_IS_SCOPE_EMBED(&in6))
382 in6.s6_addr16[1] = 0;
383 if (ifa->ifa_addr->sa_family == AF_INET6 &&
384 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
385 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
391 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
395 /* convert ipad to opad */
396 for (i = 0; i < sizeof(sc->sc_pad); i++)
397 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
401 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
402 unsigned char md[20])
406 CARP_LOCK_ASSERT(sc);
408 /* fetch first half of inner hash */
409 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
411 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
412 SHA1Final(md, &sha1ctx);
416 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
417 SHA1Update(&sha1ctx, md, 20);
418 SHA1Final(md, &sha1ctx);
422 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
423 unsigned char md[20])
425 unsigned char md2[20];
427 CARP_LOCK_ASSERT(sc);
429 carp_hmac_generate(sc, counter, md2);
431 return (bcmp(md, md2, sizeof(md2)));
435 * process input packet.
436 * we have rearranged checks order compared to the rfc,
437 * but it seems more efficient this way or not possible otherwise.
441 carp_input(struct mbuf **mp, int *offp, int proto)
443 struct mbuf *m = *mp;
444 struct ip *ip = mtod(m, struct ip *);
445 struct carp_header *ch;
451 CARPSTATS_INC(carps_ipackets);
455 return (IPPROTO_DONE);
458 /* verify that the IP TTL is 255. */
459 if (ip->ip_ttl != CARP_DFLTTL) {
460 CARPSTATS_INC(carps_badttl);
461 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
463 m->m_pkthdr.rcvif->if_xname);
465 return (IPPROTO_DONE);
468 iplen = ip->ip_hl << 2;
470 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
471 CARPSTATS_INC(carps_badlen);
472 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
473 "on %s\n", __func__, m->m_len - sizeof(struct ip),
474 m->m_pkthdr.rcvif->if_xname);
476 return (IPPROTO_DONE);
479 if (iplen + sizeof(*ch) < m->m_len) {
480 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
481 CARPSTATS_INC(carps_hdrops);
482 CARP_DEBUG("%s: pullup failed\n", __func__);
483 return (IPPROTO_DONE);
485 ip = mtod(m, struct ip *);
487 ch = (struct carp_header *)((char *)ip + iplen);
490 * verify that the received packet length is
491 * equal to the CARP header
493 len = iplen + sizeof(*ch);
494 if (len > m->m_pkthdr.len) {
495 CARPSTATS_INC(carps_badlen);
496 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
498 m->m_pkthdr.rcvif->if_xname);
500 return (IPPROTO_DONE);
503 if ((m = m_pullup(m, len)) == NULL) {
504 CARPSTATS_INC(carps_hdrops);
505 return (IPPROTO_DONE);
507 ip = mtod(m, struct ip *);
508 ch = (struct carp_header *)((char *)ip + iplen);
510 /* verify the CARP checksum */
512 if (in_cksum(m, len - iplen)) {
513 CARPSTATS_INC(carps_badsum);
514 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
515 m->m_pkthdr.rcvif->if_xname);
517 return (IPPROTO_DONE);
521 carp_input_c(m, ch, AF_INET);
522 return (IPPROTO_DONE);
528 carp6_input(struct mbuf **mp, int *offp, int proto)
530 struct mbuf *m = *mp;
531 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
532 struct carp_header *ch;
535 CARPSTATS_INC(carps_ipackets6);
539 return (IPPROTO_DONE);
542 /* check if received on a valid carp interface */
543 if (m->m_pkthdr.rcvif->if_carp == NULL) {
544 CARPSTATS_INC(carps_badif);
545 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
546 __func__, m->m_pkthdr.rcvif->if_xname);
548 return (IPPROTO_DONE);
551 /* verify that the IP TTL is 255 */
552 if (ip6->ip6_hlim != CARP_DFLTTL) {
553 CARPSTATS_INC(carps_badttl);
554 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
555 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
557 return (IPPROTO_DONE);
560 /* verify that we have a complete carp packet */
562 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
564 CARPSTATS_INC(carps_badlen);
565 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
566 return (IPPROTO_DONE);
570 /* verify the CARP checksum */
572 if (in_cksum(m, sizeof(*ch))) {
573 CARPSTATS_INC(carps_badsum);
574 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
575 m->m_pkthdr.rcvif->if_xname);
577 return (IPPROTO_DONE);
581 carp_input_c(m, ch, AF_INET6);
582 return (IPPROTO_DONE);
587 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
589 struct ifnet *ifp = m->m_pkthdr.rcvif;
591 struct carp_softc *sc;
592 uint64_t tmp_counter;
593 struct timeval sc_tv, ch_tv;
595 /* verify that the VHID is valid on the receiving interface */
597 IFNET_FOREACH_IFA(ifp, ifa)
598 if (ifa->ifa_addr->sa_family == af &&
599 ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
603 IF_ADDR_RUNLOCK(ifp);
606 CARPSTATS_INC(carps_badvhid);
611 /* verify the CARP version. */
612 if (ch->carp_version != CARP_VERSION) {
613 CARPSTATS_INC(carps_badver);
614 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
625 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
626 CARPSTATS_INC(carps_badauth);
627 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
628 sc->sc_vhid, ifp->if_xname);
632 tmp_counter = ntohl(ch->carp_counter[0]);
633 tmp_counter = tmp_counter<<32;
634 tmp_counter += ntohl(ch->carp_counter[1]);
636 /* XXX Replay protection goes here */
638 sc->sc_init_counter = 0;
639 sc->sc_counter = tmp_counter;
641 sc_tv.tv_sec = sc->sc_advbase;
642 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
643 ch_tv.tv_sec = ch->carp_advbase;
644 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
646 switch (sc->sc_state) {
651 * If we receive an advertisement from a master who's going to
652 * be more frequent than us, go into BACKUP state.
654 if (timevalcmp(&sc_tv, &ch_tv, >) ||
655 timevalcmp(&sc_tv, &ch_tv, ==)) {
656 callout_stop(&sc->sc_ad_tmo);
657 CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
658 "(more frequent advertisement received)\n",
660 sc->sc_carpdev->if_xname);
661 carp_set_state(sc, BACKUP);
668 * If we're pre-empting masters who advertise slower than us,
669 * and this one claims to be slower, treat him as down.
671 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
672 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
673 "(preempting a slower master)\n",
675 sc->sc_carpdev->if_xname);
676 carp_master_down_locked(sc);
681 * If the master is going to advertise at such a low frequency
682 * that he's guaranteed to time out, we'd might as well just
683 * treat him as timed out now.
685 sc_tv.tv_sec = sc->sc_advbase * 3;
686 if (timevalcmp(&sc_tv, &ch_tv, <)) {
687 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
688 "(master timed out)\n",
690 sc->sc_carpdev->if_xname);
691 carp_master_down_locked(sc);
696 * Otherwise, we reset the counter and wait for the next
709 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
713 if (sc->sc_init_counter) {
714 /* this could also be seconds since unix epoch */
715 sc->sc_counter = arc4random();
716 sc->sc_counter = sc->sc_counter << 32;
717 sc->sc_counter += arc4random();
721 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
722 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
724 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
726 /* Tag packet for carp_output */
727 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
728 M_NOWAIT)) == NULL) {
730 CARPSTATS_INC(carps_onomem);
733 bcopy(&sc, mtag + 1, sizeof(sc));
734 m_tag_prepend(m, mtag);
740 * To avoid LORs and possible recursions this function shouldn't
741 * be called directly, but scheduled via taskqueue.
744 carp_send_ad_all(void *ctx __unused, int pending __unused)
746 struct carp_softc *sc;
749 LIST_FOREACH(sc, &carp_list, sc_next)
750 if (sc->sc_state == MASTER) {
752 CURVNET_SET(sc->sc_carpdev->if_vnet);
753 carp_send_ad_locked(sc);
757 mtx_unlock(&carp_mtx);
760 /* Send a periodic advertisement, executed in callout context. */
762 carp_send_ad(void *v)
764 struct carp_softc *sc = v;
766 CARP_LOCK_ASSERT(sc);
767 CURVNET_SET(sc->sc_carpdev->if_vnet);
768 carp_send_ad_locked(sc);
774 carp_send_ad_error(struct carp_softc *sc, int error)
778 if (sc->sc_sendad_errors < INT_MAX)
779 sc->sc_sendad_errors++;
780 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
781 static const char fmt[] = "send error %d on %s";
782 char msg[sizeof(fmt) + IFNAMSIZ];
784 sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
785 carp_demote_adj(V_carp_senderr_adj, msg);
787 sc->sc_sendad_success = 0;
789 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS &&
790 ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
791 static const char fmt[] = "send ok on %s";
792 char msg[sizeof(fmt) + IFNAMSIZ];
794 sprintf(msg, fmt, sc->sc_carpdev->if_xname);
795 carp_demote_adj(-V_carp_senderr_adj, msg);
796 sc->sc_sendad_errors = 0;
798 sc->sc_sendad_errors = 0;
803 carp_send_ad_locked(struct carp_softc *sc)
805 struct carp_header ch;
809 struct carp_header *ch_ptr;
813 CARP_LOCK_ASSERT(sc);
815 advskew = DEMOTE_ADVSKEW(sc);
816 tv.tv_sec = sc->sc_advbase;
817 tv.tv_usec = advskew * 1000000 / 256;
819 ch.carp_version = CARP_VERSION;
820 ch.carp_type = CARP_ADVERTISEMENT;
821 ch.carp_vhid = sc->sc_vhid;
822 ch.carp_advbase = sc->sc_advbase;
823 ch.carp_advskew = advskew;
824 ch.carp_authlen = 7; /* XXX DEFINE */
825 ch.carp_pad1 = 0; /* must be zero */
828 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
834 m = m_gethdr(M_NOWAIT, MT_DATA);
836 CARPSTATS_INC(carps_onomem);
839 len = sizeof(*ip) + sizeof(ch);
840 m->m_pkthdr.len = len;
841 m->m_pkthdr.rcvif = NULL;
843 MH_ALIGN(m, m->m_len);
844 m->m_flags |= M_MCAST;
845 ip = mtod(m, struct ip *);
846 ip->ip_v = IPVERSION;
847 ip->ip_hl = sizeof(*ip) >> 2;
848 ip->ip_tos = IPTOS_LOWDELAY;
849 ip->ip_len = htons(len);
850 ip->ip_id = ip_newid();
851 ip->ip_off = htons(IP_DF);
852 ip->ip_ttl = CARP_DFLTTL;
853 ip->ip_p = IPPROTO_CARP;
856 bzero(&sa, sizeof(sa));
857 sa.sa_family = AF_INET;
858 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
861 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
864 ip->ip_src.s_addr = 0;
865 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
867 ch_ptr = (struct carp_header *)(&ip[1]);
868 bcopy(&ch, ch_ptr, sizeof(ch));
869 if (carp_prepare_ad(m, sc, ch_ptr))
872 m->m_data += sizeof(*ip);
873 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
874 m->m_data -= sizeof(*ip);
876 CARPSTATS_INC(carps_opackets);
878 carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
879 &sc->sc_carpdev->if_carp->cif_imo, NULL));
883 if (sc->sc_naddrs6) {
886 m = m_gethdr(M_NOWAIT, MT_DATA);
888 CARPSTATS_INC(carps_onomem);
891 len = sizeof(*ip6) + sizeof(ch);
892 m->m_pkthdr.len = len;
893 m->m_pkthdr.rcvif = NULL;
895 MH_ALIGN(m, m->m_len);
896 m->m_flags |= M_MCAST;
897 ip6 = mtod(m, struct ip6_hdr *);
898 bzero(ip6, sizeof(*ip6));
899 ip6->ip6_vfc |= IPV6_VERSION;
900 ip6->ip6_hlim = CARP_DFLTTL;
901 ip6->ip6_nxt = IPPROTO_CARP;
902 bzero(&sa, sizeof(sa));
904 /* set the source address */
905 sa.sa_family = AF_INET6;
906 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
908 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
909 sizeof(struct in6_addr));
912 /* This should never happen with IPv6. */
913 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
915 /* Set the multicast destination. */
916 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
917 ip6->ip6_dst.s6_addr8[15] = 0x12;
918 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
920 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
924 ch_ptr = (struct carp_header *)(&ip6[1]);
925 bcopy(&ch, ch_ptr, sizeof(ch));
926 if (carp_prepare_ad(m, sc, ch_ptr))
929 m->m_data += sizeof(*ip6);
930 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
931 m->m_data -= sizeof(*ip6);
933 CARPSTATS_INC(carps_opackets6);
935 carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
936 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
941 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
945 carp_addroute(struct carp_softc *sc)
949 CARP_FOREACH_IFA(sc, ifa)
950 carp_ifa_addroute(ifa);
954 carp_ifa_addroute(struct ifaddr *ifa)
957 switch (ifa->ifa_addr->sa_family) {
960 in_addprefix(ifatoia(ifa), RTF_UP);
961 ifa_add_loopback_route(ifa,
962 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
967 ifa_add_loopback_route(ifa,
968 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
969 nd6_add_ifa_lle(ifatoia6(ifa));
976 carp_delroute(struct carp_softc *sc)
980 CARP_FOREACH_IFA(sc, ifa)
981 carp_ifa_delroute(ifa);
985 carp_ifa_delroute(struct ifaddr *ifa)
988 switch (ifa->ifa_addr->sa_family) {
991 ifa_del_loopback_route(ifa,
992 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
993 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
998 ifa_del_loopback_route(ifa,
999 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1000 nd6_rem_ifa_lle(ifatoia6(ifa));
1007 carp_master(struct ifaddr *ifa)
1009 struct carp_softc *sc = ifa->ifa_carp;
1011 return (sc->sc_state == MASTER);
1016 * Broadcast a gratuitous ARP request containing
1017 * the virtual router MAC address for each IP address
1018 * associated with the virtual router.
1021 carp_send_arp(struct carp_softc *sc)
1025 CARP_FOREACH_IFA(sc, ifa)
1026 if (ifa->ifa_addr->sa_family == AF_INET)
1027 arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
1031 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1033 struct carp_softc *sc = ifa->ifa_carp;
1035 if (sc->sc_state == MASTER) {
1036 *enaddr = LLADDR(&sc->sc_addr);
1046 carp_send_na(struct carp_softc *sc)
1048 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1050 struct in6_addr *in6;
1052 CARP_FOREACH_IFA(sc, ifa) {
1053 if (ifa->ifa_addr->sa_family != AF_INET6)
1057 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1058 ND_NA_FLAG_OVERRIDE, 1, NULL);
1059 DELAY(1000); /* XXX */
1064 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1065 * matches and is not a carp address. Returns NULL otherwise.
1068 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1074 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1075 if (ifa->ifa_addr->sa_family != AF_INET6)
1077 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1079 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1085 IF_ADDR_RUNLOCK(ifp);
1091 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1096 IFNET_FOREACH_IFA(ifp, ifa)
1097 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1098 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1099 struct carp_softc *sc = ifa->ifa_carp;
1102 IF_ADDR_RUNLOCK(ifp);
1104 mtag = m_tag_get(PACKET_TAG_CARP,
1105 sizeof(struct carp_softc *), M_NOWAIT);
1107 /* Better a bit than nothing. */
1108 return (LLADDR(&sc->sc_addr));
1110 bcopy(&sc, mtag + 1, sizeof(sc));
1111 m_tag_prepend(m, mtag);
1113 return (LLADDR(&sc->sc_addr));
1115 IF_ADDR_RUNLOCK(ifp);
1122 carp_forus(struct ifnet *ifp, u_char *dhost)
1124 struct carp_softc *sc;
1125 uint8_t *ena = dhost;
1127 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1130 CIF_LOCK(ifp->if_carp);
1131 IFNET_FOREACH_CARP(ifp, sc) {
1133 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1136 CIF_UNLOCK(ifp->if_carp);
1141 CIF_UNLOCK(ifp->if_carp);
1146 /* Master down timeout event, executed in callout context. */
1148 carp_master_down(void *v)
1150 struct carp_softc *sc = v;
1152 CARP_LOCK_ASSERT(sc);
1154 CURVNET_SET(sc->sc_carpdev->if_vnet);
1155 if (sc->sc_state == BACKUP) {
1156 CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
1158 sc->sc_carpdev->if_xname);
1159 carp_master_down_locked(sc);
1167 carp_master_down_locked(struct carp_softc *sc)
1170 CARP_LOCK_ASSERT(sc);
1172 switch (sc->sc_state) {
1174 carp_set_state(sc, MASTER);
1175 carp_send_ad_locked(sc);
1188 panic("carp: VHID %u@%s: master_down event in %s state\n",
1190 sc->sc_carpdev->if_xname,
1191 sc->sc_state ? "MASTER" : "INIT");
1198 * When in backup state, af indicates whether to reset the master down timer
1199 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1202 carp_setrun(struct carp_softc *sc, sa_family_t af)
1206 CARP_LOCK_ASSERT(sc);
1208 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1209 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1210 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1213 switch (sc->sc_state) {
1215 CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1217 sc->sc_carpdev->if_xname);
1218 carp_set_state(sc, BACKUP);
1222 callout_stop(&sc->sc_ad_tmo);
1223 tv.tv_sec = 3 * sc->sc_advbase;
1224 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1228 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1229 carp_master_down, sc);
1234 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1235 carp_master_down, sc);
1241 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1242 carp_master_down, sc);
1246 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1247 carp_master_down, sc);
1253 tv.tv_sec = sc->sc_advbase;
1254 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1255 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1262 * Setup multicast structures.
1265 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1267 struct ifnet *ifp = cif->cif_ifp;
1270 CIF_LOCK_ASSERT(cif);
1276 struct ip_moptions *imo = &cif->cif_imo;
1277 struct in_addr addr;
1279 if (imo->imo_membership)
1282 imo->imo_membership = (struct in_multi **)malloc(
1283 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1285 if (imo->imo_membership == NULL)
1287 imo->imo_mfilters = NULL;
1288 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1289 imo->imo_multicast_vif = -1;
1291 addr.s_addr = htonl(INADDR_CARP_GROUP);
1292 if ((error = in_joingroup(ifp, &addr, NULL,
1293 &imo->imo_membership[0])) != 0) {
1294 free(imo->imo_membership, M_CARP);
1297 imo->imo_num_memberships++;
1298 imo->imo_multicast_ifp = ifp;
1299 imo->imo_multicast_ttl = CARP_DFLTTL;
1300 imo->imo_multicast_loop = 0;
1307 struct ip6_moptions *im6o = &cif->cif_im6o;
1308 struct in6_addr in6;
1309 struct in6_multi *in6m;
1311 if (im6o->im6o_membership)
1314 im6o->im6o_membership = (struct in6_multi **)malloc(
1315 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1317 if (im6o->im6o_membership == NULL)
1319 im6o->im6o_mfilters = NULL;
1320 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1321 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1322 im6o->im6o_multicast_ifp = ifp;
1324 /* Join IPv6 CARP multicast group. */
1325 bzero(&in6, sizeof(in6));
1326 in6.s6_addr16[0] = htons(0xff02);
1327 in6.s6_addr8[15] = 0x12;
1328 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1329 free(im6o->im6o_membership, M_CARP);
1333 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1334 free(im6o->im6o_membership, M_CARP);
1337 im6o->im6o_membership[0] = in6m;
1338 im6o->im6o_num_memberships++;
1340 /* Join solicited multicast address. */
1341 bzero(&in6, sizeof(in6));
1342 in6.s6_addr16[0] = htons(0xff02);
1343 in6.s6_addr32[1] = 0;
1344 in6.s6_addr32[2] = htonl(1);
1345 in6.s6_addr32[3] = 0;
1346 in6.s6_addr8[12] = 0xff;
1347 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1348 in6_mc_leave(im6o->im6o_membership[0], NULL);
1349 free(im6o->im6o_membership, M_CARP);
1353 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1354 in6_mc_leave(im6o->im6o_membership[0], NULL);
1355 free(im6o->im6o_membership, M_CARP);
1358 im6o->im6o_membership[1] = in6m;
1359 im6o->im6o_num_memberships++;
1369 * Free multicast structures.
1372 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1375 CIF_LOCK_ASSERT(cif);
1379 if (cif->cif_naddrs == 0) {
1380 struct ip_moptions *imo = &cif->cif_imo;
1382 in_leavegroup(imo->imo_membership[0], NULL);
1383 KASSERT(imo->imo_mfilters == NULL,
1384 ("%s: imo_mfilters != NULL", __func__));
1385 free(imo->imo_membership, M_CARP);
1386 imo->imo_membership = NULL;
1393 if (cif->cif_naddrs6 == 0) {
1394 struct ip6_moptions *im6o = &cif->cif_im6o;
1396 in6_mc_leave(im6o->im6o_membership[0], NULL);
1397 in6_mc_leave(im6o->im6o_membership[1], NULL);
1398 KASSERT(im6o->im6o_mfilters == NULL,
1399 ("%s: im6o_mfilters != NULL", __func__));
1400 free(im6o->im6o_membership, M_CARP);
1401 im6o->im6o_membership = NULL;
1409 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1412 struct carp_softc *sc;
1417 switch (sa->sa_family) {
1430 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1434 bcopy(mtag + 1, &sc, sizeof(sc));
1436 /* Set the source MAC address to the Virtual Router MAC Address. */
1437 switch (ifp->if_type) {
1441 struct ether_header *eh;
1443 eh = mtod(m, struct ether_header *);
1444 eh->ether_shost[0] = 0;
1445 eh->ether_shost[1] = 0;
1446 eh->ether_shost[2] = 0x5e;
1447 eh->ether_shost[3] = 0;
1448 eh->ether_shost[4] = 1;
1449 eh->ether_shost[5] = sc->sc_vhid;
1453 struct fddi_header *fh;
1455 fh = mtod(m, struct fddi_header *);
1456 fh->fddi_shost[0] = 0;
1457 fh->fddi_shost[1] = 0;
1458 fh->fddi_shost[2] = 0x5e;
1459 fh->fddi_shost[3] = 0;
1460 fh->fddi_shost[4] = 1;
1461 fh->fddi_shost[5] = sc->sc_vhid;
1464 case IFT_ISO88025: {
1465 struct iso88025_header *th;
1466 th = mtod(m, struct iso88025_header *);
1467 th->iso88025_shost[0] = 3;
1468 th->iso88025_shost[1] = 0;
1469 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1470 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1471 th->iso88025_shost[4] = 0;
1472 th->iso88025_shost[5] = 0;
1476 printf("%s: carp is not supported for the %d interface type\n",
1477 ifp->if_xname, ifp->if_type);
1478 return (EOPNOTSUPP);
1484 static struct carp_softc*
1485 carp_alloc(struct ifnet *ifp)
1487 struct carp_softc *sc;
1488 struct carp_if *cif;
1490 if ((cif = ifp->if_carp) == NULL)
1491 cif = carp_alloc_if(ifp);
1493 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1495 sc->sc_advbase = CARP_DFLTINTV;
1496 sc->sc_vhid = -1; /* required setting */
1497 sc->sc_init_counter = 1;
1498 sc->sc_state = INIT;
1500 sc->sc_ifasiz = sizeof(struct ifaddr *);
1501 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1502 sc->sc_carpdev = ifp;
1506 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1509 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1511 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1514 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1517 mtx_lock(&carp_mtx);
1518 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1519 mtx_unlock(&carp_mtx);
1525 carp_grow_ifas(struct carp_softc *sc)
1527 struct ifaddr **new;
1529 CARP_LOCK_ASSERT(sc);
1531 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1534 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1535 free(sc->sc_ifas, M_CARP);
1543 carp_destroy(struct carp_softc *sc)
1545 struct ifnet *ifp = sc->sc_carpdev;
1546 struct carp_if *cif = ifp->if_carp;
1548 CIF_LOCK_ASSERT(cif);
1550 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1552 mtx_lock(&carp_mtx);
1553 LIST_REMOVE(sc, sc_next);
1554 mtx_unlock(&carp_mtx);
1557 if (sc->sc_suppress)
1558 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1559 callout_drain(&sc->sc_ad_tmo);
1561 callout_drain(&sc->sc_md_tmo);
1564 callout_drain(&sc->sc_md6_tmo);
1566 CARP_LOCK_DESTROY(sc);
1568 free(sc->sc_ifas, M_CARP);
1572 static struct carp_if*
1573 carp_alloc_if(struct ifnet *ifp)
1575 struct carp_if *cif;
1578 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1580 if ((error = ifpromisc(ifp, 1)) != 0)
1581 printf("%s: ifpromisc(%s) failed: %d\n",
1582 __func__, ifp->if_xname, error);
1584 cif->cif_flags |= CIF_PROMISC;
1588 TAILQ_INIT(&cif->cif_vrs);
1593 IF_ADDR_WUNLOCK(ifp);
1599 carp_free_if(struct carp_if *cif)
1601 struct ifnet *ifp = cif->cif_ifp;
1603 CIF_LOCK_ASSERT(cif);
1604 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1608 ifp->if_carp = NULL;
1609 IF_ADDR_WUNLOCK(ifp);
1611 CIF_LOCK_DESTROY(cif);
1613 if (cif->cif_flags & CIF_PROMISC)
1621 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1625 carpr->carpr_state = sc->sc_state;
1626 carpr->carpr_vhid = sc->sc_vhid;
1627 carpr->carpr_advbase = sc->sc_advbase;
1628 carpr->carpr_advskew = sc->sc_advskew;
1630 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1632 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1637 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1639 struct carpreq carpr;
1641 struct carp_softc *sc = NULL;
1642 int error = 0, locked = 0;
1644 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1647 ifp = ifunit_ref(ifr->ifr_name);
1651 switch (ifp->if_type) {
1663 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1664 error = EADDRNOTAVAIL;
1670 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1672 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1673 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1679 CIF_LOCK(ifp->if_carp);
1680 IFNET_FOREACH_CARP(ifp, sc)
1681 if (sc->sc_vhid == carpr.carpr_vhid)
1683 CIF_UNLOCK(ifp->if_carp);
1686 sc = carp_alloc(ifp);
1688 sc->sc_vhid = carpr.carpr_vhid;
1689 LLADDR(&sc->sc_addr)[0] = 0;
1690 LLADDR(&sc->sc_addr)[1] = 0;
1691 LLADDR(&sc->sc_addr)[2] = 0x5e;
1692 LLADDR(&sc->sc_addr)[3] = 0;
1693 LLADDR(&sc->sc_addr)[4] = 1;
1694 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1698 if (carpr.carpr_advbase > 0) {
1699 if (carpr.carpr_advbase > 255 ||
1700 carpr.carpr_advbase < CARP_DFLTINTV) {
1704 sc->sc_advbase = carpr.carpr_advbase;
1706 if (carpr.carpr_advskew > 0) {
1707 if (carpr.carpr_advskew >= 255) {
1711 sc->sc_advskew = carpr.carpr_advskew;
1713 if (carpr.carpr_key[0] != '\0') {
1714 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1715 carp_hmac_prepare(sc);
1717 if (sc->sc_state != INIT &&
1718 carpr.carpr_state != sc->sc_state) {
1719 switch (carpr.carpr_state) {
1721 callout_stop(&sc->sc_ad_tmo);
1722 carp_set_state(sc, BACKUP);
1727 carp_master_down_locked(sc);
1739 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1743 if (carpr.carpr_count < 1) {
1747 if (ifp->if_carp == NULL) {
1752 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1753 if (carpr.carpr_vhid != 0) {
1754 CIF_LOCK(ifp->if_carp);
1755 IFNET_FOREACH_CARP(ifp, sc)
1756 if (sc->sc_vhid == carpr.carpr_vhid)
1758 CIF_UNLOCK(ifp->if_carp);
1763 carp_carprcp(&carpr, sc, priveleged);
1764 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1769 CIF_LOCK(ifp->if_carp);
1770 IFNET_FOREACH_CARP(ifp, sc)
1773 if (count > carpr.carpr_count) {
1774 CIF_UNLOCK(ifp->if_carp);
1780 IFNET_FOREACH_CARP(ifp, sc) {
1781 carp_carprcp(&carpr, sc, priveleged);
1782 carpr.carpr_count = count;
1783 error = copyout(&carpr, ifr->ifr_data +
1784 (i * sizeof(carpr)), sizeof(carpr));
1786 CIF_UNLOCK(ifp->if_carp);
1791 CIF_UNLOCK(ifp->if_carp);
1808 carp_get_vhid(struct ifaddr *ifa)
1811 if (ifa == NULL || ifa->ifa_carp == NULL)
1814 return (ifa->ifa_carp->sc_vhid);
1818 carp_attach(struct ifaddr *ifa, int vhid)
1820 struct ifnet *ifp = ifa->ifa_ifp;
1821 struct carp_if *cif = ifp->if_carp;
1822 struct carp_softc *sc;
1825 if (ifp->if_carp == NULL)
1826 return (ENOPROTOOPT);
1828 switch (ifa->ifa_addr->sa_family) {
1837 return (EPROTOTYPE);
1841 IFNET_FOREACH_CARP(ifp, sc)
1842 if (sc->sc_vhid == vhid)
1849 if (ifa->ifa_carp) {
1850 if (ifa->ifa_carp->sc_vhid != vhid)
1851 carp_detach_locked(ifa);
1858 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1865 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1866 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1867 if ((error = carp_grow_ifas(sc)) != 0) {
1868 carp_multicast_cleanup(cif,
1869 ifa->ifa_addr->sa_family);
1875 switch (ifa->ifa_addr->sa_family) {
1891 sc->sc_ifas[index - 1] = ifa;
1894 carp_hmac_prepare(sc);
1904 carp_detach(struct ifaddr *ifa)
1906 struct ifnet *ifp = ifa->ifa_ifp;
1907 struct carp_if *cif = ifp->if_carp;
1910 carp_detach_locked(ifa);
1915 carp_detach_locked(struct ifaddr *ifa)
1917 struct ifnet *ifp = ifa->ifa_ifp;
1918 struct carp_if *cif = ifp->if_carp;
1919 struct carp_softc *sc = ifa->ifa_carp;
1922 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1924 CIF_LOCK_ASSERT(cif);
1928 index = sc->sc_naddrs + sc->sc_naddrs6;
1929 for (i = 0; i < index; i++)
1930 if (sc->sc_ifas[i] == ifa)
1932 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1933 for (; i < index - 1; i++)
1934 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1935 sc->sc_ifas[index - 1] = NULL;
1937 switch (ifa->ifa_addr->sa_family) {
1952 carp_ifa_delroute(ifa);
1953 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1955 ifa->ifa_carp = NULL;
1958 carp_hmac_prepare(sc);
1961 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1969 carp_set_state(struct carp_softc *sc, int state)
1972 CARP_LOCK_ASSERT(sc);
1974 if (sc->sc_state != state) {
1975 const char *carp_states[] = { CARP_STATES };
1976 char subsys[IFNAMSIZ+5];
1978 sc->sc_state = state;
1980 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1981 sc->sc_carpdev->if_xname);
1982 devctl_notify("CARP", subsys, carp_states[state], NULL);
1987 carp_linkstate(struct ifnet *ifp)
1989 struct carp_softc *sc;
1991 CIF_LOCK(ifp->if_carp);
1992 IFNET_FOREACH_CARP(ifp, sc) {
1997 CIF_UNLOCK(ifp->if_carp);
2001 carp_sc_state(struct carp_softc *sc)
2004 CARP_LOCK_ASSERT(sc);
2006 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2007 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2008 callout_stop(&sc->sc_ad_tmo);
2010 callout_stop(&sc->sc_md_tmo);
2013 callout_stop(&sc->sc_md6_tmo);
2015 carp_set_state(sc, INIT);
2017 if (!sc->sc_suppress)
2018 carp_demote_adj(V_carp_ifdown_adj, "interface down");
2019 sc->sc_suppress = 1;
2021 carp_set_state(sc, INIT);
2023 if (sc->sc_suppress)
2024 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2025 sc->sc_suppress = 0;
2030 carp_demote_adj(int adj, char *reason)
2032 atomic_add_int(&V_carp_demotion, adj);
2033 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2034 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2038 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2042 new = V_carp_demotion;
2043 error = sysctl_handle_int(oidp, &new, 0, req);
2044 if (error || !req->newptr)
2047 carp_demote_adj(new, "sysctl");
2053 extern struct domain inetdomain;
2054 static struct protosw in_carp_protosw = {
2055 .pr_type = SOCK_RAW,
2056 .pr_domain = &inetdomain,
2057 .pr_protocol = IPPROTO_CARP,
2058 .pr_flags = PR_ATOMIC|PR_ADDR,
2059 .pr_input = carp_input,
2060 .pr_output = rip_output,
2061 .pr_ctloutput = rip_ctloutput,
2062 .pr_usrreqs = &rip_usrreqs
2067 extern struct domain inet6domain;
2068 static struct protosw in6_carp_protosw = {
2069 .pr_type = SOCK_RAW,
2070 .pr_domain = &inet6domain,
2071 .pr_protocol = IPPROTO_CARP,
2072 .pr_flags = PR_ATOMIC|PR_ADDR,
2073 .pr_input = carp6_input,
2074 .pr_output = rip6_output,
2075 .pr_ctloutput = rip6_ctloutput,
2076 .pr_usrreqs = &rip6_usrreqs
2081 carp_mod_cleanup(void)
2085 if (proto_reg[CARP_INET] == 0) {
2086 (void)ipproto_unregister(IPPROTO_CARP);
2087 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2088 proto_reg[CARP_INET] = -1;
2090 carp_iamatch_p = NULL;
2093 if (proto_reg[CARP_INET6] == 0) {
2094 (void)ip6proto_unregister(IPPROTO_CARP);
2095 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2096 proto_reg[CARP_INET6] = -1;
2098 carp_iamatch6_p = NULL;
2099 carp_macmatch6_p = NULL;
2101 carp_ioctl_p = NULL;
2102 carp_attach_p = NULL;
2103 carp_detach_p = NULL;
2104 carp_get_vhid_p = NULL;
2105 carp_linkstate_p = NULL;
2106 carp_forus_p = NULL;
2107 carp_output_p = NULL;
2108 carp_demote_adj_p = NULL;
2109 carp_master_p = NULL;
2110 mtx_unlock(&carp_mtx);
2111 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2112 mtx_destroy(&carp_mtx);
2120 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2121 LIST_INIT(&carp_list);
2122 carp_get_vhid_p = carp_get_vhid;
2123 carp_forus_p = carp_forus;
2124 carp_output_p = carp_output;
2125 carp_linkstate_p = carp_linkstate;
2126 carp_ioctl_p = carp_ioctl;
2127 carp_attach_p = carp_attach;
2128 carp_detach_p = carp_detach;
2129 carp_demote_adj_p = carp_demote_adj;
2130 carp_master_p = carp_master;
2132 carp_iamatch6_p = carp_iamatch6;
2133 carp_macmatch6_p = carp_macmatch6;
2134 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2135 (struct protosw *)&in6_carp_protosw);
2136 if (proto_reg[CARP_INET6]) {
2137 printf("carp: error %d attaching to PF_INET6\n",
2138 proto_reg[CARP_INET6]);
2140 return (proto_reg[CARP_INET6]);
2142 err = ip6proto_register(IPPROTO_CARP);
2144 printf("carp: error %d registering with INET6\n", err);
2150 carp_iamatch_p = carp_iamatch;
2151 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2152 if (proto_reg[CARP_INET]) {
2153 printf("carp: error %d attaching to PF_INET\n",
2154 proto_reg[CARP_INET]);
2156 return (proto_reg[CARP_INET]);
2158 err = ipproto_register(IPPROTO_CARP);
2160 printf("carp: error %d registering with INET\n", err);
2169 carp_modevent(module_t mod, int type, void *data)
2173 return carp_mod_load();
2176 mtx_lock(&carp_mtx);
2177 if (LIST_EMPTY(&carp_list))
2180 mtx_unlock(&carp_mtx);
2192 static moduledata_t carp_mod = {
2198 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);