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, const char* reason);
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 *,
313 static void carp_send_ad(void *);
314 static void carp_send_ad_locked(struct carp_softc *);
315 static void carp_addroute(struct carp_softc *);
316 static void carp_ifa_addroute(struct ifaddr *);
317 static void carp_delroute(struct carp_softc *);
318 static void carp_ifa_delroute(struct ifaddr *);
319 static void carp_send_ad_all(void *, int);
320 static void carp_demote_adj(int, char *);
322 static LIST_HEAD(, carp_softc) carp_list;
323 static struct mtx carp_mtx;
324 static struct task carp_sendall_task =
325 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
328 carp_hmac_prepare(struct carp_softc *sc)
330 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
331 uint8_t vhid = sc->sc_vhid & 0xff;
335 struct in_addr last, cur, in;
338 struct in6_addr last6, cur6, in6;
341 CARP_LOCK_ASSERT(sc);
343 /* Compute ipad from key. */
344 bzero(sc->sc_pad, sizeof(sc->sc_pad));
345 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
346 for (i = 0; i < sizeof(sc->sc_pad); i++)
347 sc->sc_pad[i] ^= 0x36;
349 /* Precompute first part of inner hash. */
350 SHA1Init(&sc->sc_sha1);
351 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
352 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
353 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
354 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
360 cur.s_addr = 0xffffffff;
361 CARP_FOREACH_IFA(sc, ifa) {
362 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
363 if (ifa->ifa_addr->sa_family == AF_INET &&
364 ntohl(in.s_addr) > ntohl(last.s_addr) &&
365 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
366 cur.s_addr = in.s_addr;
371 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
375 memset(&cur6, 0, sizeof(cur6));
379 memset(&cur6, 0xff, sizeof(cur6));
380 CARP_FOREACH_IFA(sc, ifa) {
381 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
382 if (IN6_IS_SCOPE_EMBED(&in6))
383 in6.s6_addr16[1] = 0;
384 if (ifa->ifa_addr->sa_family == AF_INET6 &&
385 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
386 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
392 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
396 /* convert ipad to opad */
397 for (i = 0; i < sizeof(sc->sc_pad); i++)
398 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
402 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
403 unsigned char md[20])
407 CARP_LOCK_ASSERT(sc);
409 /* fetch first half of inner hash */
410 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
412 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
413 SHA1Final(md, &sha1ctx);
417 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
418 SHA1Update(&sha1ctx, md, 20);
419 SHA1Final(md, &sha1ctx);
423 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
424 unsigned char md[20])
426 unsigned char md2[20];
428 CARP_LOCK_ASSERT(sc);
430 carp_hmac_generate(sc, counter, md2);
432 return (bcmp(md, md2, sizeof(md2)));
436 * process input packet.
437 * we have rearranged checks order compared to the rfc,
438 * but it seems more efficient this way or not possible otherwise.
442 carp_input(struct mbuf **mp, int *offp, int proto)
444 struct mbuf *m = *mp;
445 struct ip *ip = mtod(m, struct ip *);
446 struct carp_header *ch;
452 CARPSTATS_INC(carps_ipackets);
456 return (IPPROTO_DONE);
459 /* verify that the IP TTL is 255. */
460 if (ip->ip_ttl != CARP_DFLTTL) {
461 CARPSTATS_INC(carps_badttl);
462 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
464 m->m_pkthdr.rcvif->if_xname);
466 return (IPPROTO_DONE);
469 iplen = ip->ip_hl << 2;
471 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
472 CARPSTATS_INC(carps_badlen);
473 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
474 "on %s\n", __func__, m->m_len - sizeof(struct ip),
475 m->m_pkthdr.rcvif->if_xname);
477 return (IPPROTO_DONE);
480 if (iplen + sizeof(*ch) < m->m_len) {
481 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
482 CARPSTATS_INC(carps_hdrops);
483 CARP_DEBUG("%s: pullup failed\n", __func__);
484 return (IPPROTO_DONE);
486 ip = mtod(m, struct ip *);
488 ch = (struct carp_header *)((char *)ip + iplen);
491 * verify that the received packet length is
492 * equal to the CARP header
494 len = iplen + sizeof(*ch);
495 if (len > m->m_pkthdr.len) {
496 CARPSTATS_INC(carps_badlen);
497 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
499 m->m_pkthdr.rcvif->if_xname);
501 return (IPPROTO_DONE);
504 if ((m = m_pullup(m, len)) == NULL) {
505 CARPSTATS_INC(carps_hdrops);
506 return (IPPROTO_DONE);
508 ip = mtod(m, struct ip *);
509 ch = (struct carp_header *)((char *)ip + iplen);
511 /* verify the CARP checksum */
513 if (in_cksum(m, len - iplen)) {
514 CARPSTATS_INC(carps_badsum);
515 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
516 m->m_pkthdr.rcvif->if_xname);
518 return (IPPROTO_DONE);
522 carp_input_c(m, ch, AF_INET);
523 return (IPPROTO_DONE);
529 carp6_input(struct mbuf **mp, int *offp, int proto)
531 struct mbuf *m = *mp;
532 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
533 struct carp_header *ch;
536 CARPSTATS_INC(carps_ipackets6);
540 return (IPPROTO_DONE);
543 /* check if received on a valid carp interface */
544 if (m->m_pkthdr.rcvif->if_carp == NULL) {
545 CARPSTATS_INC(carps_badif);
546 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
547 __func__, m->m_pkthdr.rcvif->if_xname);
549 return (IPPROTO_DONE);
552 /* verify that the IP TTL is 255 */
553 if (ip6->ip6_hlim != CARP_DFLTTL) {
554 CARPSTATS_INC(carps_badttl);
555 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
556 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
558 return (IPPROTO_DONE);
561 /* verify that we have a complete carp packet */
563 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
565 CARPSTATS_INC(carps_badlen);
566 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
567 return (IPPROTO_DONE);
571 /* verify the CARP checksum */
573 if (in_cksum(m, sizeof(*ch))) {
574 CARPSTATS_INC(carps_badsum);
575 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
576 m->m_pkthdr.rcvif->if_xname);
578 return (IPPROTO_DONE);
582 carp_input_c(m, ch, AF_INET6);
583 return (IPPROTO_DONE);
588 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
590 struct ifnet *ifp = m->m_pkthdr.rcvif;
592 struct carp_softc *sc;
593 uint64_t tmp_counter;
594 struct timeval sc_tv, ch_tv;
596 /* verify that the VHID is valid on the receiving interface */
598 IFNET_FOREACH_IFA(ifp, ifa)
599 if (ifa->ifa_addr->sa_family == af &&
600 ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
604 IF_ADDR_RUNLOCK(ifp);
607 CARPSTATS_INC(carps_badvhid);
612 /* verify the CARP version. */
613 if (ch->carp_version != CARP_VERSION) {
614 CARPSTATS_INC(carps_badver);
615 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
626 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
627 CARPSTATS_INC(carps_badauth);
628 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
629 sc->sc_vhid, ifp->if_xname);
633 tmp_counter = ntohl(ch->carp_counter[0]);
634 tmp_counter = tmp_counter<<32;
635 tmp_counter += ntohl(ch->carp_counter[1]);
637 /* XXX Replay protection goes here */
639 sc->sc_init_counter = 0;
640 sc->sc_counter = tmp_counter;
642 sc_tv.tv_sec = sc->sc_advbase;
643 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
644 ch_tv.tv_sec = ch->carp_advbase;
645 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
647 switch (sc->sc_state) {
652 * If we receive an advertisement from a master who's going to
653 * be more frequent than us, go into BACKUP state.
655 if (timevalcmp(&sc_tv, &ch_tv, >) ||
656 timevalcmp(&sc_tv, &ch_tv, ==)) {
657 callout_stop(&sc->sc_ad_tmo);
658 carp_set_state(sc, BACKUP,
659 "more frequent advertisement received");
666 * If we're pre-empting masters who advertise slower than us,
667 * and this one claims to be slower, treat him as down.
669 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
670 carp_master_down_locked(sc,
671 "preempting a slower master");
676 * If the master is going to advertise at such a low frequency
677 * that he's guaranteed to time out, we'd might as well just
678 * treat him as timed out now.
680 sc_tv.tv_sec = sc->sc_advbase * 3;
681 if (timevalcmp(&sc_tv, &ch_tv, <)) {
682 carp_master_down_locked(sc, "master will time out");
687 * Otherwise, we reset the counter and wait for the next
700 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
704 if (sc->sc_init_counter) {
705 /* this could also be seconds since unix epoch */
706 sc->sc_counter = arc4random();
707 sc->sc_counter = sc->sc_counter << 32;
708 sc->sc_counter += arc4random();
712 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
713 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
715 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
717 /* Tag packet for carp_output */
718 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
719 M_NOWAIT)) == NULL) {
721 CARPSTATS_INC(carps_onomem);
724 bcopy(&sc, mtag + 1, sizeof(sc));
725 m_tag_prepend(m, mtag);
731 * To avoid LORs and possible recursions this function shouldn't
732 * be called directly, but scheduled via taskqueue.
735 carp_send_ad_all(void *ctx __unused, int pending __unused)
737 struct carp_softc *sc;
740 LIST_FOREACH(sc, &carp_list, sc_next)
741 if (sc->sc_state == MASTER) {
743 CURVNET_SET(sc->sc_carpdev->if_vnet);
744 carp_send_ad_locked(sc);
748 mtx_unlock(&carp_mtx);
751 /* Send a periodic advertisement, executed in callout context. */
753 carp_send_ad(void *v)
755 struct carp_softc *sc = v;
757 CARP_LOCK_ASSERT(sc);
758 CURVNET_SET(sc->sc_carpdev->if_vnet);
759 carp_send_ad_locked(sc);
765 carp_send_ad_error(struct carp_softc *sc, int error)
769 if (sc->sc_sendad_errors < INT_MAX)
770 sc->sc_sendad_errors++;
771 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
772 static const char fmt[] = "send error %d on %s";
773 char msg[sizeof(fmt) + IFNAMSIZ];
775 sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
776 carp_demote_adj(V_carp_senderr_adj, msg);
778 sc->sc_sendad_success = 0;
780 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS &&
781 ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
782 static const char fmt[] = "send ok on %s";
783 char msg[sizeof(fmt) + IFNAMSIZ];
785 sprintf(msg, fmt, sc->sc_carpdev->if_xname);
786 carp_demote_adj(-V_carp_senderr_adj, msg);
787 sc->sc_sendad_errors = 0;
789 sc->sc_sendad_errors = 0;
794 carp_send_ad_locked(struct carp_softc *sc)
796 struct carp_header ch;
800 struct carp_header *ch_ptr;
804 CARP_LOCK_ASSERT(sc);
806 advskew = DEMOTE_ADVSKEW(sc);
807 tv.tv_sec = sc->sc_advbase;
808 tv.tv_usec = advskew * 1000000 / 256;
810 ch.carp_version = CARP_VERSION;
811 ch.carp_type = CARP_ADVERTISEMENT;
812 ch.carp_vhid = sc->sc_vhid;
813 ch.carp_advbase = sc->sc_advbase;
814 ch.carp_advskew = advskew;
815 ch.carp_authlen = 7; /* XXX DEFINE */
816 ch.carp_pad1 = 0; /* must be zero */
819 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
825 m = m_gethdr(M_NOWAIT, MT_DATA);
827 CARPSTATS_INC(carps_onomem);
830 len = sizeof(*ip) + sizeof(ch);
831 m->m_pkthdr.len = len;
832 m->m_pkthdr.rcvif = NULL;
834 M_ALIGN(m, m->m_len);
835 m->m_flags |= M_MCAST;
836 ip = mtod(m, struct ip *);
837 ip->ip_v = IPVERSION;
838 ip->ip_hl = sizeof(*ip) >> 2;
839 ip->ip_tos = IPTOS_LOWDELAY;
840 ip->ip_len = htons(len);
841 ip->ip_off = htons(IP_DF);
842 ip->ip_ttl = CARP_DFLTTL;
843 ip->ip_p = IPPROTO_CARP;
847 bzero(&sa, sizeof(sa));
848 sa.sa_family = AF_INET;
849 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
852 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
855 ip->ip_src.s_addr = 0;
856 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
858 ch_ptr = (struct carp_header *)(&ip[1]);
859 bcopy(&ch, ch_ptr, sizeof(ch));
860 if (carp_prepare_ad(m, sc, ch_ptr))
863 m->m_data += sizeof(*ip);
864 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
865 m->m_data -= sizeof(*ip);
867 CARPSTATS_INC(carps_opackets);
869 carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
870 &sc->sc_carpdev->if_carp->cif_imo, NULL));
874 if (sc->sc_naddrs6) {
877 m = m_gethdr(M_NOWAIT, MT_DATA);
879 CARPSTATS_INC(carps_onomem);
882 len = sizeof(*ip6) + sizeof(ch);
883 m->m_pkthdr.len = len;
884 m->m_pkthdr.rcvif = NULL;
886 M_ALIGN(m, m->m_len);
887 m->m_flags |= M_MCAST;
888 ip6 = mtod(m, struct ip6_hdr *);
889 bzero(ip6, sizeof(*ip6));
890 ip6->ip6_vfc |= IPV6_VERSION;
891 ip6->ip6_hlim = CARP_DFLTTL;
892 ip6->ip6_nxt = IPPROTO_CARP;
893 bzero(&sa, sizeof(sa));
895 /* set the source address */
896 sa.sa_family = AF_INET6;
897 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
899 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
900 sizeof(struct in6_addr));
903 /* This should never happen with IPv6. */
904 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
906 /* Set the multicast destination. */
907 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
908 ip6->ip6_dst.s6_addr8[15] = 0x12;
909 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
911 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
915 ch_ptr = (struct carp_header *)(&ip6[1]);
916 bcopy(&ch, ch_ptr, sizeof(ch));
917 if (carp_prepare_ad(m, sc, ch_ptr))
920 m->m_data += sizeof(*ip6);
921 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
922 m->m_data -= sizeof(*ip6);
924 CARPSTATS_INC(carps_opackets6);
926 carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
927 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
932 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
936 carp_addroute(struct carp_softc *sc)
940 CARP_FOREACH_IFA(sc, ifa)
941 carp_ifa_addroute(ifa);
945 carp_ifa_addroute(struct ifaddr *ifa)
948 switch (ifa->ifa_addr->sa_family) {
951 in_addprefix(ifatoia(ifa), RTF_UP);
952 ifa_add_loopback_route(ifa,
953 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
958 ifa_add_loopback_route(ifa,
959 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
960 nd6_add_ifa_lle(ifatoia6(ifa));
967 carp_delroute(struct carp_softc *sc)
971 CARP_FOREACH_IFA(sc, ifa)
972 carp_ifa_delroute(ifa);
976 carp_ifa_delroute(struct ifaddr *ifa)
979 switch (ifa->ifa_addr->sa_family) {
982 ifa_del_loopback_route(ifa,
983 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
984 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
989 ifa_del_loopback_route(ifa,
990 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
991 nd6_rem_ifa_lle(ifatoia6(ifa));
998 carp_master(struct ifaddr *ifa)
1000 struct carp_softc *sc = ifa->ifa_carp;
1002 return (sc->sc_state == MASTER);
1007 * Broadcast a gratuitous ARP request containing
1008 * the virtual router MAC address for each IP address
1009 * associated with the virtual router.
1012 carp_send_arp(struct carp_softc *sc)
1016 CARP_FOREACH_IFA(sc, ifa)
1017 if (ifa->ifa_addr->sa_family == AF_INET)
1018 arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
1022 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1024 struct carp_softc *sc = ifa->ifa_carp;
1026 if (sc->sc_state == MASTER) {
1027 *enaddr = LLADDR(&sc->sc_addr);
1037 carp_send_na(struct carp_softc *sc)
1039 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1041 struct in6_addr *in6;
1043 CARP_FOREACH_IFA(sc, ifa) {
1044 if (ifa->ifa_addr->sa_family != AF_INET6)
1048 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1049 ND_NA_FLAG_OVERRIDE, 1, NULL);
1050 DELAY(1000); /* XXX */
1055 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1056 * matches and is not a carp address. Returns NULL otherwise.
1059 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1065 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1066 if (ifa->ifa_addr->sa_family != AF_INET6)
1068 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1070 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1076 IF_ADDR_RUNLOCK(ifp);
1082 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1087 IFNET_FOREACH_IFA(ifp, ifa)
1088 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1089 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1090 struct carp_softc *sc = ifa->ifa_carp;
1093 IF_ADDR_RUNLOCK(ifp);
1095 mtag = m_tag_get(PACKET_TAG_CARP,
1096 sizeof(struct carp_softc *), M_NOWAIT);
1098 /* Better a bit than nothing. */
1099 return (LLADDR(&sc->sc_addr));
1101 bcopy(&sc, mtag + 1, sizeof(sc));
1102 m_tag_prepend(m, mtag);
1104 return (LLADDR(&sc->sc_addr));
1106 IF_ADDR_RUNLOCK(ifp);
1113 carp_forus(struct ifnet *ifp, u_char *dhost)
1115 struct carp_softc *sc;
1116 uint8_t *ena = dhost;
1118 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1121 CIF_LOCK(ifp->if_carp);
1122 IFNET_FOREACH_CARP(ifp, sc) {
1124 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1127 CIF_UNLOCK(ifp->if_carp);
1132 CIF_UNLOCK(ifp->if_carp);
1137 /* Master down timeout event, executed in callout context. */
1139 carp_master_down(void *v)
1141 struct carp_softc *sc = v;
1143 CARP_LOCK_ASSERT(sc);
1145 CURVNET_SET(sc->sc_carpdev->if_vnet);
1146 if (sc->sc_state == BACKUP) {
1147 carp_master_down_locked(sc, "master timed out");
1155 carp_master_down_locked(struct carp_softc *sc, const char *reason)
1158 CARP_LOCK_ASSERT(sc);
1160 switch (sc->sc_state) {
1162 carp_set_state(sc, MASTER, reason);
1163 carp_send_ad_locked(sc);
1176 panic("carp: VHID %u@%s: master_down event in %s state\n",
1178 sc->sc_carpdev->if_xname,
1179 sc->sc_state ? "MASTER" : "INIT");
1186 * When in backup state, af indicates whether to reset the master down timer
1187 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1190 carp_setrun(struct carp_softc *sc, sa_family_t af)
1194 CARP_LOCK_ASSERT(sc);
1196 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1197 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1198 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1201 switch (sc->sc_state) {
1203 carp_set_state(sc, BACKUP, "initialization complete");
1207 callout_stop(&sc->sc_ad_tmo);
1208 tv.tv_sec = 3 * sc->sc_advbase;
1209 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1213 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1214 carp_master_down, sc);
1219 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1220 carp_master_down, sc);
1226 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1227 carp_master_down, sc);
1231 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1232 carp_master_down, sc);
1238 tv.tv_sec = sc->sc_advbase;
1239 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1240 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1247 * Setup multicast structures.
1250 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1252 struct ifnet *ifp = cif->cif_ifp;
1255 CIF_LOCK_ASSERT(cif);
1261 struct ip_moptions *imo = &cif->cif_imo;
1262 struct in_addr addr;
1264 if (imo->imo_membership)
1267 imo->imo_membership = (struct in_multi **)malloc(
1268 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1270 if (imo->imo_membership == NULL)
1272 imo->imo_mfilters = NULL;
1273 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1274 imo->imo_multicast_vif = -1;
1276 addr.s_addr = htonl(INADDR_CARP_GROUP);
1277 if ((error = in_joingroup(ifp, &addr, NULL,
1278 &imo->imo_membership[0])) != 0) {
1279 free(imo->imo_membership, M_CARP);
1282 imo->imo_num_memberships++;
1283 imo->imo_multicast_ifp = ifp;
1284 imo->imo_multicast_ttl = CARP_DFLTTL;
1285 imo->imo_multicast_loop = 0;
1292 struct ip6_moptions *im6o = &cif->cif_im6o;
1293 struct in6_addr in6;
1294 struct in6_multi *in6m;
1296 if (im6o->im6o_membership)
1299 im6o->im6o_membership = (struct in6_multi **)malloc(
1300 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1302 if (im6o->im6o_membership == NULL)
1304 im6o->im6o_mfilters = NULL;
1305 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1306 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1307 im6o->im6o_multicast_ifp = ifp;
1309 /* Join IPv6 CARP multicast group. */
1310 bzero(&in6, sizeof(in6));
1311 in6.s6_addr16[0] = htons(0xff02);
1312 in6.s6_addr8[15] = 0x12;
1313 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1314 free(im6o->im6o_membership, M_CARP);
1318 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1319 free(im6o->im6o_membership, M_CARP);
1322 im6o->im6o_membership[0] = in6m;
1323 im6o->im6o_num_memberships++;
1325 /* Join solicited multicast address. */
1326 bzero(&in6, sizeof(in6));
1327 in6.s6_addr16[0] = htons(0xff02);
1328 in6.s6_addr32[1] = 0;
1329 in6.s6_addr32[2] = htonl(1);
1330 in6.s6_addr32[3] = 0;
1331 in6.s6_addr8[12] = 0xff;
1332 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1333 in6_mc_leave(im6o->im6o_membership[0], NULL);
1334 free(im6o->im6o_membership, M_CARP);
1338 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1339 in6_mc_leave(im6o->im6o_membership[0], NULL);
1340 free(im6o->im6o_membership, M_CARP);
1343 im6o->im6o_membership[1] = in6m;
1344 im6o->im6o_num_memberships++;
1354 * Free multicast structures.
1357 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1360 CIF_LOCK_ASSERT(cif);
1364 if (cif->cif_naddrs == 0) {
1365 struct ip_moptions *imo = &cif->cif_imo;
1367 in_leavegroup(imo->imo_membership[0], NULL);
1368 KASSERT(imo->imo_mfilters == NULL,
1369 ("%s: imo_mfilters != NULL", __func__));
1370 free(imo->imo_membership, M_CARP);
1371 imo->imo_membership = NULL;
1378 if (cif->cif_naddrs6 == 0) {
1379 struct ip6_moptions *im6o = &cif->cif_im6o;
1381 in6_mc_leave(im6o->im6o_membership[0], NULL);
1382 in6_mc_leave(im6o->im6o_membership[1], NULL);
1383 KASSERT(im6o->im6o_mfilters == NULL,
1384 ("%s: im6o_mfilters != NULL", __func__));
1385 free(im6o->im6o_membership, M_CARP);
1386 im6o->im6o_membership = NULL;
1394 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1397 struct carp_softc *sc;
1402 switch (sa->sa_family) {
1415 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1419 bcopy(mtag + 1, &sc, sizeof(sc));
1421 /* Set the source MAC address to the Virtual Router MAC Address. */
1422 switch (ifp->if_type) {
1426 struct ether_header *eh;
1428 eh = mtod(m, struct ether_header *);
1429 eh->ether_shost[0] = 0;
1430 eh->ether_shost[1] = 0;
1431 eh->ether_shost[2] = 0x5e;
1432 eh->ether_shost[3] = 0;
1433 eh->ether_shost[4] = 1;
1434 eh->ether_shost[5] = sc->sc_vhid;
1438 struct fddi_header *fh;
1440 fh = mtod(m, struct fddi_header *);
1441 fh->fddi_shost[0] = 0;
1442 fh->fddi_shost[1] = 0;
1443 fh->fddi_shost[2] = 0x5e;
1444 fh->fddi_shost[3] = 0;
1445 fh->fddi_shost[4] = 1;
1446 fh->fddi_shost[5] = sc->sc_vhid;
1449 case IFT_ISO88025: {
1450 struct iso88025_header *th;
1451 th = mtod(m, struct iso88025_header *);
1452 th->iso88025_shost[0] = 3;
1453 th->iso88025_shost[1] = 0;
1454 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1455 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1456 th->iso88025_shost[4] = 0;
1457 th->iso88025_shost[5] = 0;
1461 printf("%s: carp is not supported for the %d interface type\n",
1462 ifp->if_xname, ifp->if_type);
1463 return (EOPNOTSUPP);
1469 static struct carp_softc*
1470 carp_alloc(struct ifnet *ifp)
1472 struct carp_softc *sc;
1473 struct carp_if *cif;
1475 if ((cif = ifp->if_carp) == NULL)
1476 cif = carp_alloc_if(ifp);
1478 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1480 sc->sc_advbase = CARP_DFLTINTV;
1481 sc->sc_vhid = -1; /* required setting */
1482 sc->sc_init_counter = 1;
1483 sc->sc_state = INIT;
1485 sc->sc_ifasiz = sizeof(struct ifaddr *);
1486 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1487 sc->sc_carpdev = ifp;
1491 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1494 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1496 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1499 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1502 mtx_lock(&carp_mtx);
1503 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1504 mtx_unlock(&carp_mtx);
1510 carp_grow_ifas(struct carp_softc *sc)
1512 struct ifaddr **new;
1514 CARP_LOCK_ASSERT(sc);
1516 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1519 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1520 free(sc->sc_ifas, M_CARP);
1528 carp_destroy(struct carp_softc *sc)
1530 struct ifnet *ifp = sc->sc_carpdev;
1531 struct carp_if *cif = ifp->if_carp;
1533 CIF_LOCK_ASSERT(cif);
1535 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1537 mtx_lock(&carp_mtx);
1538 LIST_REMOVE(sc, sc_next);
1539 mtx_unlock(&carp_mtx);
1542 if (sc->sc_suppress)
1543 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1544 callout_drain(&sc->sc_ad_tmo);
1546 callout_drain(&sc->sc_md_tmo);
1549 callout_drain(&sc->sc_md6_tmo);
1551 CARP_LOCK_DESTROY(sc);
1553 free(sc->sc_ifas, M_CARP);
1557 static struct carp_if*
1558 carp_alloc_if(struct ifnet *ifp)
1560 struct carp_if *cif;
1563 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1565 if ((error = ifpromisc(ifp, 1)) != 0)
1566 printf("%s: ifpromisc(%s) failed: %d\n",
1567 __func__, ifp->if_xname, error);
1569 cif->cif_flags |= CIF_PROMISC;
1573 TAILQ_INIT(&cif->cif_vrs);
1578 IF_ADDR_WUNLOCK(ifp);
1584 carp_free_if(struct carp_if *cif)
1586 struct ifnet *ifp = cif->cif_ifp;
1588 CIF_LOCK_ASSERT(cif);
1589 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1593 ifp->if_carp = NULL;
1594 IF_ADDR_WUNLOCK(ifp);
1596 CIF_LOCK_DESTROY(cif);
1598 if (cif->cif_flags & CIF_PROMISC)
1606 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1610 carpr->carpr_state = sc->sc_state;
1611 carpr->carpr_vhid = sc->sc_vhid;
1612 carpr->carpr_advbase = sc->sc_advbase;
1613 carpr->carpr_advskew = sc->sc_advskew;
1615 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1617 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1622 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1624 struct carpreq carpr;
1626 struct carp_softc *sc = NULL;
1627 int error = 0, locked = 0;
1629 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1632 ifp = ifunit_ref(ifr->ifr_name);
1636 switch (ifp->if_type) {
1648 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1649 error = EADDRNOTAVAIL;
1655 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1657 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1658 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1664 CIF_LOCK(ifp->if_carp);
1665 IFNET_FOREACH_CARP(ifp, sc)
1666 if (sc->sc_vhid == carpr.carpr_vhid)
1668 CIF_UNLOCK(ifp->if_carp);
1671 sc = carp_alloc(ifp);
1673 sc->sc_vhid = carpr.carpr_vhid;
1674 LLADDR(&sc->sc_addr)[0] = 0;
1675 LLADDR(&sc->sc_addr)[1] = 0;
1676 LLADDR(&sc->sc_addr)[2] = 0x5e;
1677 LLADDR(&sc->sc_addr)[3] = 0;
1678 LLADDR(&sc->sc_addr)[4] = 1;
1679 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1683 if (carpr.carpr_advbase > 0) {
1684 if (carpr.carpr_advbase > 255 ||
1685 carpr.carpr_advbase < CARP_DFLTINTV) {
1689 sc->sc_advbase = carpr.carpr_advbase;
1691 if (carpr.carpr_advskew >= 255) {
1695 sc->sc_advskew = carpr.carpr_advskew;
1696 if (carpr.carpr_key[0] != '\0') {
1697 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1698 carp_hmac_prepare(sc);
1700 if (sc->sc_state != INIT &&
1701 carpr.carpr_state != sc->sc_state) {
1702 switch (carpr.carpr_state) {
1704 callout_stop(&sc->sc_ad_tmo);
1705 carp_set_state(sc, BACKUP,
1706 "user requested via ifconfig");
1711 carp_master_down_locked(sc,
1712 "user requested via ifconfig");
1724 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1728 if (carpr.carpr_count < 1) {
1732 if (ifp->if_carp == NULL) {
1737 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1738 if (carpr.carpr_vhid != 0) {
1739 CIF_LOCK(ifp->if_carp);
1740 IFNET_FOREACH_CARP(ifp, sc)
1741 if (sc->sc_vhid == carpr.carpr_vhid)
1743 CIF_UNLOCK(ifp->if_carp);
1748 carp_carprcp(&carpr, sc, priveleged);
1749 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1754 CIF_LOCK(ifp->if_carp);
1755 IFNET_FOREACH_CARP(ifp, sc)
1758 if (count > carpr.carpr_count) {
1759 CIF_UNLOCK(ifp->if_carp);
1765 IFNET_FOREACH_CARP(ifp, sc) {
1766 carp_carprcp(&carpr, sc, priveleged);
1767 carpr.carpr_count = count;
1768 error = copyout(&carpr, ifr->ifr_data +
1769 (i * sizeof(carpr)), sizeof(carpr));
1771 CIF_UNLOCK(ifp->if_carp);
1776 CIF_UNLOCK(ifp->if_carp);
1793 carp_get_vhid(struct ifaddr *ifa)
1796 if (ifa == NULL || ifa->ifa_carp == NULL)
1799 return (ifa->ifa_carp->sc_vhid);
1803 carp_attach(struct ifaddr *ifa, int vhid)
1805 struct ifnet *ifp = ifa->ifa_ifp;
1806 struct carp_if *cif = ifp->if_carp;
1807 struct carp_softc *sc;
1810 if (ifp->if_carp == NULL)
1811 return (ENOPROTOOPT);
1813 switch (ifa->ifa_addr->sa_family) {
1822 return (EPROTOTYPE);
1826 IFNET_FOREACH_CARP(ifp, sc)
1827 if (sc->sc_vhid == vhid)
1834 if (ifa->ifa_carp) {
1835 if (ifa->ifa_carp->sc_vhid != vhid)
1836 carp_detach_locked(ifa);
1843 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1850 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1851 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1852 if ((error = carp_grow_ifas(sc)) != 0) {
1853 carp_multicast_cleanup(cif,
1854 ifa->ifa_addr->sa_family);
1860 switch (ifa->ifa_addr->sa_family) {
1876 sc->sc_ifas[index - 1] = ifa;
1879 carp_hmac_prepare(sc);
1889 carp_detach(struct ifaddr *ifa)
1891 struct ifnet *ifp = ifa->ifa_ifp;
1892 struct carp_if *cif = ifp->if_carp;
1895 carp_detach_locked(ifa);
1900 carp_detach_locked(struct ifaddr *ifa)
1902 struct ifnet *ifp = ifa->ifa_ifp;
1903 struct carp_if *cif = ifp->if_carp;
1904 struct carp_softc *sc = ifa->ifa_carp;
1907 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1909 CIF_LOCK_ASSERT(cif);
1913 index = sc->sc_naddrs + sc->sc_naddrs6;
1914 for (i = 0; i < index; i++)
1915 if (sc->sc_ifas[i] == ifa)
1917 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1918 for (; i < index - 1; i++)
1919 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1920 sc->sc_ifas[index - 1] = NULL;
1922 switch (ifa->ifa_addr->sa_family) {
1937 carp_ifa_delroute(ifa);
1938 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1940 ifa->ifa_carp = NULL;
1943 carp_hmac_prepare(sc);
1946 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1954 carp_set_state(struct carp_softc *sc, int state, const char *reason)
1957 CARP_LOCK_ASSERT(sc);
1959 if (sc->sc_state != state) {
1960 const char *carp_states[] = { CARP_STATES };
1961 char subsys[IFNAMSIZ+5];
1963 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1964 sc->sc_carpdev->if_xname);
1966 CARP_LOG("%s: %s -> %s (%s)\n", subsys,
1967 carp_states[sc->sc_state], carp_states[state], reason);
1969 sc->sc_state = state;
1971 devctl_notify("CARP", subsys, carp_states[state], NULL);
1976 carp_linkstate(struct ifnet *ifp)
1978 struct carp_softc *sc;
1980 CIF_LOCK(ifp->if_carp);
1981 IFNET_FOREACH_CARP(ifp, sc) {
1986 CIF_UNLOCK(ifp->if_carp);
1990 carp_sc_state(struct carp_softc *sc)
1993 CARP_LOCK_ASSERT(sc);
1995 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1996 !(sc->sc_carpdev->if_flags & IFF_UP)) {
1997 callout_stop(&sc->sc_ad_tmo);
1999 callout_stop(&sc->sc_md_tmo);
2002 callout_stop(&sc->sc_md6_tmo);
2004 carp_set_state(sc, INIT, "hardware interface down");
2006 if (!sc->sc_suppress)
2007 carp_demote_adj(V_carp_ifdown_adj, "interface down");
2008 sc->sc_suppress = 1;
2010 carp_set_state(sc, INIT, "hardware interface up");
2012 if (sc->sc_suppress)
2013 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2014 sc->sc_suppress = 0;
2019 carp_demote_adj(int adj, char *reason)
2021 atomic_add_int(&V_carp_demotion, adj);
2022 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2023 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2027 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2031 new = V_carp_demotion;
2032 error = sysctl_handle_int(oidp, &new, 0, req);
2033 if (error || !req->newptr)
2036 carp_demote_adj(new, "sysctl");
2042 extern struct domain inetdomain;
2043 static struct protosw in_carp_protosw = {
2044 .pr_type = SOCK_RAW,
2045 .pr_domain = &inetdomain,
2046 .pr_protocol = IPPROTO_CARP,
2047 .pr_flags = PR_ATOMIC|PR_ADDR,
2048 .pr_input = carp_input,
2049 .pr_output = rip_output,
2050 .pr_ctloutput = rip_ctloutput,
2051 .pr_usrreqs = &rip_usrreqs
2056 extern struct domain inet6domain;
2057 static struct protosw in6_carp_protosw = {
2058 .pr_type = SOCK_RAW,
2059 .pr_domain = &inet6domain,
2060 .pr_protocol = IPPROTO_CARP,
2061 .pr_flags = PR_ATOMIC|PR_ADDR,
2062 .pr_input = carp6_input,
2063 .pr_output = rip6_output,
2064 .pr_ctloutput = rip6_ctloutput,
2065 .pr_usrreqs = &rip6_usrreqs
2070 carp_mod_cleanup(void)
2074 if (proto_reg[CARP_INET] == 0) {
2075 (void)ipproto_unregister(IPPROTO_CARP);
2076 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2077 proto_reg[CARP_INET] = -1;
2079 carp_iamatch_p = NULL;
2082 if (proto_reg[CARP_INET6] == 0) {
2083 (void)ip6proto_unregister(IPPROTO_CARP);
2084 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2085 proto_reg[CARP_INET6] = -1;
2087 carp_iamatch6_p = NULL;
2088 carp_macmatch6_p = NULL;
2090 carp_ioctl_p = NULL;
2091 carp_attach_p = NULL;
2092 carp_detach_p = NULL;
2093 carp_get_vhid_p = NULL;
2094 carp_linkstate_p = NULL;
2095 carp_forus_p = NULL;
2096 carp_output_p = NULL;
2097 carp_demote_adj_p = NULL;
2098 carp_master_p = NULL;
2099 mtx_unlock(&carp_mtx);
2100 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2101 mtx_destroy(&carp_mtx);
2109 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2110 LIST_INIT(&carp_list);
2111 carp_get_vhid_p = carp_get_vhid;
2112 carp_forus_p = carp_forus;
2113 carp_output_p = carp_output;
2114 carp_linkstate_p = carp_linkstate;
2115 carp_ioctl_p = carp_ioctl;
2116 carp_attach_p = carp_attach;
2117 carp_detach_p = carp_detach;
2118 carp_demote_adj_p = carp_demote_adj;
2119 carp_master_p = carp_master;
2121 carp_iamatch6_p = carp_iamatch6;
2122 carp_macmatch6_p = carp_macmatch6;
2123 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2124 (struct protosw *)&in6_carp_protosw);
2125 if (proto_reg[CARP_INET6]) {
2126 printf("carp: error %d attaching to PF_INET6\n",
2127 proto_reg[CARP_INET6]);
2129 return (proto_reg[CARP_INET6]);
2131 err = ip6proto_register(IPPROTO_CARP);
2133 printf("carp: error %d registering with INET6\n", err);
2139 carp_iamatch_p = carp_iamatch;
2140 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2141 if (proto_reg[CARP_INET]) {
2142 printf("carp: error %d attaching to PF_INET\n",
2143 proto_reg[CARP_INET]);
2145 return (proto_reg[CARP_INET]);
2147 err = ipproto_register(IPPROTO_CARP);
2149 printf("carp: error %d registering with INET\n", err);
2158 carp_modevent(module_t mod, int type, void *data)
2162 return carp_mod_load();
2165 mtx_lock(&carp_mtx);
2166 if (LIST_EMPTY(&carp_list))
2169 mtx_unlock(&carp_mtx);
2181 static moduledata_t carp_mod = {
2187 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);