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/ip6protosw.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/scope6_var.h>
85 #include <netinet6/nd6.h>
88 #include <crypto/sha1.h>
90 static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
93 struct ifnet *sc_carpdev; /* Pointer to parent ifnet. */
94 struct ifaddr **sc_ifas; /* Our ifaddrs. */
95 struct sockaddr_dl sc_addr; /* Our link level address. */
96 struct callout sc_ad_tmo; /* Advertising timeout. */
98 struct callout sc_md_tmo; /* Master down timeout. */
101 struct callout sc_md6_tmo; /* XXX: Master down timeout. */
112 enum { INIT = 0, BACKUP, MASTER } sc_state;
114 int sc_sendad_errors;
115 #define CARP_SENDAD_MAX_ERRORS 3
116 int sc_sendad_success;
117 #define CARP_SENDAD_MIN_SUCCESS 3
123 #define CARP_HMAC_PAD 64
124 unsigned char sc_key[CARP_KEY_LEN];
125 unsigned char sc_pad[CARP_HMAC_PAD];
128 TAILQ_ENTRY(carp_softc) sc_list; /* On the carp_if list. */
129 LIST_ENTRY(carp_softc) sc_next; /* On the global list. */
139 TAILQ_HEAD(, carp_softc) cif_vrs;
141 struct ip_moptions cif_imo;
144 struct ip6_moptions cif_im6o;
146 struct ifnet *cif_ifp;
152 static int proto_reg[] = {-1, -1};
155 * Brief design of carp(4).
157 * Any carp-capable ifnet may have a list of carp softcs hanging off
158 * its ifp->if_carp pointer. Each softc represents one unique virtual
159 * host id, or vhid. The softc has a back pointer to the ifnet. All
160 * softcs are joined in a global list, which has quite limited use.
162 * Any interface address that takes part in CARP negotiation has a
163 * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
164 * AF_INET or AF_INET6 address.
166 * Although, one can get the softc's backpointer to ifnet and traverse
167 * through its ifp->if_addrhead queue to find all interface addresses
168 * involved in CARP, we keep a growable array of ifaddr pointers. This
169 * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
170 * do calls into the network stack, thus avoiding LORs.
174 * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
175 * callout-driven events and ioctl()s.
177 * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to
178 * traverse the global list we use the mutex carp_mtx.
180 * Known issues with locking:
182 * - There is no protection for races between two ioctl() requests,
183 * neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all
184 * interface ioctl()s should be serialized right in net/if.c.
185 * - Sending ad, we put the pointer to the softc in an mtag, and no reference
186 * counting is done on the softc.
187 * - On module unload we may race (?) with packet processing thread
188 * dereferencing our function pointers.
191 /* Accept incoming CARP packets. */
192 static VNET_DEFINE(int, carp_allow) = 1;
193 #define V_carp_allow VNET(carp_allow)
195 /* Preempt slower nodes. */
196 static VNET_DEFINE(int, carp_preempt) = 0;
197 #define V_carp_preempt VNET(carp_preempt)
200 static VNET_DEFINE(int, carp_log) = 1;
201 #define V_carp_log VNET(carp_log)
203 /* Global advskew demotion. */
204 static VNET_DEFINE(int, carp_demotion) = 0;
205 #define V_carp_demotion VNET(carp_demotion)
207 /* Send error demotion factor. */
208 static VNET_DEFINE(int, carp_senderr_adj) = CARP_MAXSKEW;
209 #define V_carp_senderr_adj VNET(carp_senderr_adj)
211 /* Iface down demotion factor. */
212 static VNET_DEFINE(int, carp_ifdown_adj) = CARP_MAXSKEW;
213 #define V_carp_ifdown_adj VNET(carp_ifdown_adj)
215 static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
217 SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP");
218 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW,
219 &VNET_NAME(carp_allow), 0, "Accept incoming CARP packets");
220 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW,
221 &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
222 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW,
223 &VNET_NAME(carp_log), 0, "CARP log level");
224 SYSCTL_VNET_PROC(_net_inet_carp, OID_AUTO, demotion, CTLTYPE_INT|CTLFLAG_RW,
225 0, 0, carp_demote_adj_sysctl, "I",
226 "Adjust demotion factor (skew of advskew)");
227 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW,
228 &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
229 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW,
230 &VNET_NAME(carp_ifdown_adj), 0,
231 "Interface down demotion factor adjustment");
233 VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
234 VNET_PCPUSTAT_SYSINIT(carpstats);
235 VNET_PCPUSTAT_SYSUNINIT(carpstats);
237 #define CARPSTATS_ADD(name, val) \
238 counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
239 sizeof(uint64_t)], (val))
240 #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1)
242 SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
243 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
245 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
247 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
248 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
249 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
250 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
251 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
253 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
254 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
255 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
256 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
257 #define CIF_FREE(cif) do { \
258 CIF_LOCK_ASSERT(cif); \
259 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
265 #define CARP_LOG(...) do { \
266 if (V_carp_log > 0) \
267 log(LOG_INFO, "carp: " __VA_ARGS__); \
270 #define CARP_DEBUG(...) do { \
271 if (V_carp_log > 1) \
272 log(LOG_DEBUG, __VA_ARGS__); \
275 #define IFNET_FOREACH_IFA(ifp, ifa) \
276 IF_ADDR_LOCK_ASSERT(ifp); \
277 TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
278 if ((ifa)->ifa_carp != NULL)
280 #define CARP_FOREACH_IFA(sc, ifa) \
281 CARP_LOCK_ASSERT(sc); \
283 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
284 ((ifa) = sc->sc_ifas[_i]) != NULL; \
287 #define IFNET_FOREACH_CARP(ifp, sc) \
288 CIF_LOCK_ASSERT(ifp->if_carp); \
289 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
291 #define DEMOTE_ADVSKEW(sc) \
292 (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \
293 CARP_MAXSKEW : ((sc)->sc_advskew + V_carp_demotion))
295 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
296 static struct carp_softc
297 *carp_alloc(struct ifnet *);
298 static void carp_detach_locked(struct ifaddr *);
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);
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 *);
308 static void carp_send_ad(void *);
309 static void carp_send_ad_locked(struct carp_softc *);
310 static void carp_addroute(struct carp_softc *);
311 static void carp_ifa_addroute(struct ifaddr *);
312 static void carp_delroute(struct carp_softc *);
313 static void carp_ifa_delroute(struct ifaddr *);
314 static void carp_send_ad_all(void *, int);
315 static void carp_demote_adj(int, char *);
317 static LIST_HEAD(, carp_softc) carp_list;
318 static struct mtx carp_mtx;
319 static struct task carp_sendall_task =
320 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
323 carp_hmac_prepare(struct carp_softc *sc)
325 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
326 uint8_t vhid = sc->sc_vhid & 0xff;
330 struct in_addr last, cur, in;
333 struct in6_addr last6, cur6, in6;
336 CARP_LOCK_ASSERT(sc);
338 /* Compute ipad from key. */
339 bzero(sc->sc_pad, sizeof(sc->sc_pad));
340 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
341 for (i = 0; i < sizeof(sc->sc_pad); i++)
342 sc->sc_pad[i] ^= 0x36;
344 /* Precompute first part of inner hash. */
345 SHA1Init(&sc->sc_sha1);
346 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
347 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
348 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
349 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
355 cur.s_addr = 0xffffffff;
356 CARP_FOREACH_IFA(sc, ifa) {
357 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
358 if (ifa->ifa_addr->sa_family == AF_INET &&
359 ntohl(in.s_addr) > ntohl(last.s_addr) &&
360 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
361 cur.s_addr = in.s_addr;
366 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
370 memset(&cur6, 0, sizeof(cur6));
374 memset(&cur6, 0xff, sizeof(cur6));
375 CARP_FOREACH_IFA(sc, ifa) {
376 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
377 if (IN6_IS_SCOPE_EMBED(&in6))
378 in6.s6_addr16[1] = 0;
379 if (ifa->ifa_addr->sa_family == AF_INET6 &&
380 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
381 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
387 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
391 /* convert ipad to opad */
392 for (i = 0; i < sizeof(sc->sc_pad); i++)
393 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
397 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
398 unsigned char md[20])
402 CARP_LOCK_ASSERT(sc);
404 /* fetch first half of inner hash */
405 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
407 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
408 SHA1Final(md, &sha1ctx);
412 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
413 SHA1Update(&sha1ctx, md, 20);
414 SHA1Final(md, &sha1ctx);
418 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
419 unsigned char md[20])
421 unsigned char md2[20];
423 CARP_LOCK_ASSERT(sc);
425 carp_hmac_generate(sc, counter, md2);
427 return (bcmp(md, md2, sizeof(md2)));
431 * process input packet.
432 * we have rearranged checks order compared to the rfc,
433 * but it seems more efficient this way or not possible otherwise.
437 carp_input(struct mbuf *m, int hlen)
439 struct ip *ip = mtod(m, struct ip *);
440 struct carp_header *ch;
443 CARPSTATS_INC(carps_ipackets);
450 /* verify that the IP TTL is 255. */
451 if (ip->ip_ttl != CARP_DFLTTL) {
452 CARPSTATS_INC(carps_badttl);
453 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
455 m->m_pkthdr.rcvif->if_xname);
460 iplen = ip->ip_hl << 2;
462 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
463 CARPSTATS_INC(carps_badlen);
464 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
465 "on %s\n", __func__, m->m_len - sizeof(struct ip),
466 m->m_pkthdr.rcvif->if_xname);
471 if (iplen + sizeof(*ch) < m->m_len) {
472 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
473 CARPSTATS_INC(carps_hdrops);
474 CARP_DEBUG("%s: pullup failed\n", __func__);
477 ip = mtod(m, struct ip *);
479 ch = (struct carp_header *)((char *)ip + iplen);
482 * verify that the received packet length is
483 * equal to the CARP header
485 len = iplen + sizeof(*ch);
486 if (len > m->m_pkthdr.len) {
487 CARPSTATS_INC(carps_badlen);
488 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
490 m->m_pkthdr.rcvif->if_xname);
495 if ((m = m_pullup(m, len)) == NULL) {
496 CARPSTATS_INC(carps_hdrops);
499 ip = mtod(m, struct ip *);
500 ch = (struct carp_header *)((char *)ip + iplen);
502 /* verify the CARP checksum */
504 if (in_cksum(m, len - iplen)) {
505 CARPSTATS_INC(carps_badsum);
506 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
507 m->m_pkthdr.rcvif->if_xname);
513 carp_input_c(m, ch, AF_INET);
519 carp6_input(struct mbuf **mp, int *offp, int proto)
521 struct mbuf *m = *mp;
522 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
523 struct carp_header *ch;
526 CARPSTATS_INC(carps_ipackets6);
530 return (IPPROTO_DONE);
533 /* check if received on a valid carp interface */
534 if (m->m_pkthdr.rcvif->if_carp == NULL) {
535 CARPSTATS_INC(carps_badif);
536 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
537 __func__, m->m_pkthdr.rcvif->if_xname);
539 return (IPPROTO_DONE);
542 /* verify that the IP TTL is 255 */
543 if (ip6->ip6_hlim != CARP_DFLTTL) {
544 CARPSTATS_INC(carps_badttl);
545 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
546 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
548 return (IPPROTO_DONE);
551 /* verify that we have a complete carp packet */
553 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
555 CARPSTATS_INC(carps_badlen);
556 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
557 return (IPPROTO_DONE);
561 /* verify the CARP checksum */
563 if (in_cksum(m, sizeof(*ch))) {
564 CARPSTATS_INC(carps_badsum);
565 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
566 m->m_pkthdr.rcvif->if_xname);
568 return (IPPROTO_DONE);
572 carp_input_c(m, ch, AF_INET6);
573 return (IPPROTO_DONE);
578 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
580 struct ifnet *ifp = m->m_pkthdr.rcvif;
582 struct carp_softc *sc;
583 uint64_t tmp_counter;
584 struct timeval sc_tv, ch_tv;
586 /* verify that the VHID is valid on the receiving interface */
588 IFNET_FOREACH_IFA(ifp, ifa)
589 if (ifa->ifa_addr->sa_family == af &&
590 ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
594 IF_ADDR_RUNLOCK(ifp);
597 CARPSTATS_INC(carps_badvhid);
602 /* verify the CARP version. */
603 if (ch->carp_version != CARP_VERSION) {
604 CARPSTATS_INC(carps_badver);
605 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
616 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
617 CARPSTATS_INC(carps_badauth);
618 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
619 sc->sc_vhid, ifp->if_xname);
623 tmp_counter = ntohl(ch->carp_counter[0]);
624 tmp_counter = tmp_counter<<32;
625 tmp_counter += ntohl(ch->carp_counter[1]);
627 /* XXX Replay protection goes here */
629 sc->sc_init_counter = 0;
630 sc->sc_counter = tmp_counter;
632 sc_tv.tv_sec = sc->sc_advbase;
633 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
634 ch_tv.tv_sec = ch->carp_advbase;
635 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
637 switch (sc->sc_state) {
642 * If we receive an advertisement from a master who's going to
643 * be more frequent than us, go into BACKUP state.
645 if (timevalcmp(&sc_tv, &ch_tv, >) ||
646 timevalcmp(&sc_tv, &ch_tv, ==)) {
647 callout_stop(&sc->sc_ad_tmo);
648 CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
649 "(more frequent advertisement received)\n",
651 sc->sc_carpdev->if_xname);
652 carp_set_state(sc, BACKUP);
659 * If we're pre-empting masters who advertise slower than us,
660 * and this one claims to be slower, treat him as down.
662 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
663 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
664 "(preempting a slower master)\n",
666 sc->sc_carpdev->if_xname);
667 carp_master_down_locked(sc);
672 * If the master is going to advertise at such a low frequency
673 * that he's guaranteed to time out, we'd might as well just
674 * treat him as timed out now.
676 sc_tv.tv_sec = sc->sc_advbase * 3;
677 if (timevalcmp(&sc_tv, &ch_tv, <)) {
678 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
679 "(master timed out)\n",
681 sc->sc_carpdev->if_xname);
682 carp_master_down_locked(sc);
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_locked(struct carp_softc *sc)
767 struct carp_header ch;
771 struct carp_header *ch_ptr;
775 CARP_LOCK_ASSERT(sc);
777 advskew = DEMOTE_ADVSKEW(sc);
778 tv.tv_sec = sc->sc_advbase;
779 tv.tv_usec = advskew * 1000000 / 256;
781 ch.carp_version = CARP_VERSION;
782 ch.carp_type = CARP_ADVERTISEMENT;
783 ch.carp_vhid = sc->sc_vhid;
784 ch.carp_advbase = sc->sc_advbase;
785 ch.carp_advskew = advskew;
786 ch.carp_authlen = 7; /* XXX DEFINE */
787 ch.carp_pad1 = 0; /* must be zero */
790 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
796 m = m_gethdr(M_NOWAIT, MT_DATA);
798 CARPSTATS_INC(carps_onomem);
801 len = sizeof(*ip) + sizeof(ch);
802 m->m_pkthdr.len = len;
803 m->m_pkthdr.rcvif = NULL;
805 MH_ALIGN(m, m->m_len);
806 m->m_flags |= M_MCAST;
807 ip = mtod(m, struct ip *);
808 ip->ip_v = IPVERSION;
809 ip->ip_hl = sizeof(*ip) >> 2;
810 ip->ip_tos = IPTOS_LOWDELAY;
811 ip->ip_len = htons(len);
812 ip->ip_id = ip_newid();
813 ip->ip_off = htons(IP_DF);
814 ip->ip_ttl = CARP_DFLTTL;
815 ip->ip_p = IPPROTO_CARP;
818 bzero(&sa, sizeof(sa));
819 sa.sa_family = AF_INET;
820 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
823 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
826 ip->ip_src.s_addr = 0;
827 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
829 ch_ptr = (struct carp_header *)(&ip[1]);
830 bcopy(&ch, ch_ptr, sizeof(ch));
831 if (carp_prepare_ad(m, sc, ch_ptr))
834 m->m_data += sizeof(*ip);
835 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
836 m->m_data -= sizeof(*ip);
838 CARPSTATS_INC(carps_opackets);
840 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
841 &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
842 if (sc->sc_sendad_errors < INT_MAX)
843 sc->sc_sendad_errors++;
844 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
845 carp_demote_adj(V_carp_senderr_adj,
847 sc->sc_sendad_success = 0;
849 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
850 if (++sc->sc_sendad_success >=
851 CARP_SENDAD_MIN_SUCCESS) {
852 carp_demote_adj(-V_carp_senderr_adj,
854 sc->sc_sendad_errors = 0;
857 sc->sc_sendad_errors = 0;
862 if (sc->sc_naddrs6) {
865 m = m_gethdr(M_NOWAIT, MT_DATA);
867 CARPSTATS_INC(carps_onomem);
870 len = sizeof(*ip6) + sizeof(ch);
871 m->m_pkthdr.len = len;
872 m->m_pkthdr.rcvif = NULL;
874 MH_ALIGN(m, m->m_len);
875 m->m_flags |= M_MCAST;
876 ip6 = mtod(m, struct ip6_hdr *);
877 bzero(ip6, sizeof(*ip6));
878 ip6->ip6_vfc |= IPV6_VERSION;
879 ip6->ip6_hlim = CARP_DFLTTL;
880 ip6->ip6_nxt = IPPROTO_CARP;
881 bzero(&sa, sizeof(sa));
883 /* set the source address */
884 sa.sa_family = AF_INET6;
885 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
887 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
888 sizeof(struct in6_addr));
891 /* This should never happen with IPv6. */
892 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
894 /* Set the multicast destination. */
895 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
896 ip6->ip6_dst.s6_addr8[15] = 0x12;
897 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
899 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
903 ch_ptr = (struct carp_header *)(&ip6[1]);
904 bcopy(&ch, ch_ptr, sizeof(ch));
905 if (carp_prepare_ad(m, sc, ch_ptr))
908 m->m_data += sizeof(*ip6);
909 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
910 m->m_data -= sizeof(*ip6);
912 CARPSTATS_INC(carps_opackets6);
914 if (ip6_output(m, NULL, NULL, 0,
915 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
916 if (sc->sc_sendad_errors < INT_MAX)
917 sc->sc_sendad_errors++;
918 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
919 carp_demote_adj(V_carp_senderr_adj,
921 sc->sc_sendad_success = 0;
923 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
924 if (++sc->sc_sendad_success >=
925 CARP_SENDAD_MIN_SUCCESS) {
926 carp_demote_adj(-V_carp_senderr_adj,
928 sc->sc_sendad_errors = 0;
931 sc->sc_sendad_errors = 0;
937 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
941 carp_addroute(struct carp_softc *sc)
945 CARP_FOREACH_IFA(sc, ifa)
946 carp_ifa_addroute(ifa);
950 carp_ifa_addroute(struct ifaddr *ifa)
953 switch (ifa->ifa_addr->sa_family) {
956 in_addprefix(ifatoia(ifa), RTF_UP);
957 ifa_add_loopback_route(ifa,
958 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
963 ifa_add_loopback_route(ifa,
964 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
972 carp_delroute(struct carp_softc *sc)
976 CARP_FOREACH_IFA(sc, ifa)
977 carp_ifa_delroute(ifa);
981 carp_ifa_delroute(struct ifaddr *ifa)
984 switch (ifa->ifa_addr->sa_family) {
987 ifa_del_loopback_route(ifa,
988 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
989 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
994 ifa_del_loopback_route(ifa,
995 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1003 carp_master(struct ifaddr *ifa)
1005 struct carp_softc *sc = ifa->ifa_carp;
1007 return (sc->sc_state == MASTER);
1012 * Broadcast a gratuitous ARP request containing
1013 * the virtual router MAC address for each IP address
1014 * associated with the virtual router.
1017 carp_send_arp(struct carp_softc *sc)
1021 CARP_FOREACH_IFA(sc, ifa)
1022 if (ifa->ifa_addr->sa_family == AF_INET)
1023 arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
1027 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1029 struct carp_softc *sc = ifa->ifa_carp;
1031 if (sc->sc_state == MASTER) {
1032 *enaddr = LLADDR(&sc->sc_addr);
1042 carp_send_na(struct carp_softc *sc)
1044 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1046 struct in6_addr *in6;
1048 CARP_FOREACH_IFA(sc, ifa) {
1049 if (ifa->ifa_addr->sa_family != AF_INET6)
1053 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1054 ND_NA_FLAG_OVERRIDE, 1, NULL);
1055 DELAY(1000); /* XXX */
1060 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1061 * matches and is not a carp address. Returns NULL otherwise.
1064 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1070 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1071 if (ifa->ifa_addr->sa_family != AF_INET6)
1073 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1075 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1081 IF_ADDR_RUNLOCK(ifp);
1087 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1092 IFNET_FOREACH_IFA(ifp, ifa)
1093 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1094 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1095 struct carp_softc *sc = ifa->ifa_carp;
1098 IF_ADDR_RUNLOCK(ifp);
1100 mtag = m_tag_get(PACKET_TAG_CARP,
1101 sizeof(struct carp_softc *), M_NOWAIT);
1103 /* Better a bit than nothing. */
1104 return (LLADDR(&sc->sc_addr));
1106 bcopy(&sc, mtag + 1, sizeof(sc));
1107 m_tag_prepend(m, mtag);
1109 return (LLADDR(&sc->sc_addr));
1111 IF_ADDR_RUNLOCK(ifp);
1118 carp_forus(struct ifnet *ifp, u_char *dhost)
1120 struct carp_softc *sc;
1121 uint8_t *ena = dhost;
1123 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1126 CIF_LOCK(ifp->if_carp);
1127 IFNET_FOREACH_CARP(ifp, sc) {
1129 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1132 CIF_UNLOCK(ifp->if_carp);
1137 CIF_UNLOCK(ifp->if_carp);
1142 /* Master down timeout event, executed in callout context. */
1144 carp_master_down(void *v)
1146 struct carp_softc *sc = v;
1148 CARP_LOCK_ASSERT(sc);
1150 CURVNET_SET(sc->sc_carpdev->if_vnet);
1151 if (sc->sc_state == BACKUP) {
1152 CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
1154 sc->sc_carpdev->if_xname);
1155 carp_master_down_locked(sc);
1163 carp_master_down_locked(struct carp_softc *sc)
1166 CARP_LOCK_ASSERT(sc);
1168 switch (sc->sc_state) {
1170 carp_set_state(sc, MASTER);
1171 carp_send_ad_locked(sc);
1184 panic("carp: VHID %u@%s: master_down event in %s state\n",
1186 sc->sc_carpdev->if_xname,
1187 sc->sc_state ? "MASTER" : "INIT");
1194 * When in backup state, af indicates whether to reset the master down timer
1195 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1198 carp_setrun(struct carp_softc *sc, sa_family_t af)
1202 CARP_LOCK_ASSERT(sc);
1204 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1205 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1206 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1209 switch (sc->sc_state) {
1211 CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1213 sc->sc_carpdev->if_xname);
1214 carp_set_state(sc, BACKUP);
1218 callout_stop(&sc->sc_ad_tmo);
1219 tv.tv_sec = 3 * sc->sc_advbase;
1220 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1224 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1225 carp_master_down, sc);
1230 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1231 carp_master_down, sc);
1237 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1238 carp_master_down, sc);
1242 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1243 carp_master_down, sc);
1249 tv.tv_sec = sc->sc_advbase;
1250 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1251 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1258 * Setup multicast structures.
1261 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1263 struct ifnet *ifp = cif->cif_ifp;
1266 CIF_LOCK_ASSERT(cif);
1272 struct ip_moptions *imo = &cif->cif_imo;
1273 struct in_addr addr;
1275 if (imo->imo_membership)
1278 imo->imo_membership = (struct in_multi **)malloc(
1279 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1281 if (imo->imo_membership == NULL)
1283 imo->imo_mfilters = NULL;
1284 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1285 imo->imo_multicast_vif = -1;
1287 addr.s_addr = htonl(INADDR_CARP_GROUP);
1288 if ((error = in_joingroup(ifp, &addr, NULL,
1289 &imo->imo_membership[0])) != 0) {
1290 free(imo->imo_membership, M_CARP);
1293 imo->imo_num_memberships++;
1294 imo->imo_multicast_ifp = ifp;
1295 imo->imo_multicast_ttl = CARP_DFLTTL;
1296 imo->imo_multicast_loop = 0;
1303 struct ip6_moptions *im6o = &cif->cif_im6o;
1304 struct in6_addr in6;
1305 struct in6_multi *in6m;
1307 if (im6o->im6o_membership)
1310 im6o->im6o_membership = (struct in6_multi **)malloc(
1311 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1313 if (im6o->im6o_membership == NULL)
1315 im6o->im6o_mfilters = NULL;
1316 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1317 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1318 im6o->im6o_multicast_ifp = ifp;
1320 /* Join IPv6 CARP multicast group. */
1321 bzero(&in6, sizeof(in6));
1322 in6.s6_addr16[0] = htons(0xff02);
1323 in6.s6_addr8[15] = 0x12;
1324 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1325 free(im6o->im6o_membership, M_CARP);
1329 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1330 free(im6o->im6o_membership, M_CARP);
1333 im6o->im6o_membership[0] = in6m;
1334 im6o->im6o_num_memberships++;
1336 /* Join solicited multicast address. */
1337 bzero(&in6, sizeof(in6));
1338 in6.s6_addr16[0] = htons(0xff02);
1339 in6.s6_addr32[1] = 0;
1340 in6.s6_addr32[2] = htonl(1);
1341 in6.s6_addr32[3] = 0;
1342 in6.s6_addr8[12] = 0xff;
1343 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1344 in6_mc_leave(im6o->im6o_membership[0], NULL);
1345 free(im6o->im6o_membership, M_CARP);
1349 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1350 in6_mc_leave(im6o->im6o_membership[0], NULL);
1351 free(im6o->im6o_membership, M_CARP);
1354 im6o->im6o_membership[1] = in6m;
1355 im6o->im6o_num_memberships++;
1365 * Free multicast structures.
1368 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1371 CIF_LOCK_ASSERT(cif);
1375 if (cif->cif_naddrs == 0) {
1376 struct ip_moptions *imo = &cif->cif_imo;
1378 in_leavegroup(imo->imo_membership[0], NULL);
1379 KASSERT(imo->imo_mfilters == NULL,
1380 ("%s: imo_mfilters != NULL", __func__));
1381 free(imo->imo_membership, M_CARP);
1382 imo->imo_membership = NULL;
1389 if (cif->cif_naddrs6 == 0) {
1390 struct ip6_moptions *im6o = &cif->cif_im6o;
1392 in6_mc_leave(im6o->im6o_membership[0], NULL);
1393 in6_mc_leave(im6o->im6o_membership[1], NULL);
1394 KASSERT(im6o->im6o_mfilters == NULL,
1395 ("%s: im6o_mfilters != NULL", __func__));
1396 free(im6o->im6o_membership, M_CARP);
1397 im6o->im6o_membership = NULL;
1405 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1408 struct carp_softc *sc;
1413 switch (sa->sa_family) {
1426 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1430 bcopy(mtag + 1, &sc, sizeof(sc));
1432 /* Set the source MAC address to the Virtual Router MAC Address. */
1433 switch (ifp->if_type) {
1437 struct ether_header *eh;
1439 eh = mtod(m, struct ether_header *);
1440 eh->ether_shost[0] = 0;
1441 eh->ether_shost[1] = 0;
1442 eh->ether_shost[2] = 0x5e;
1443 eh->ether_shost[3] = 0;
1444 eh->ether_shost[4] = 1;
1445 eh->ether_shost[5] = sc->sc_vhid;
1449 struct fddi_header *fh;
1451 fh = mtod(m, struct fddi_header *);
1452 fh->fddi_shost[0] = 0;
1453 fh->fddi_shost[1] = 0;
1454 fh->fddi_shost[2] = 0x5e;
1455 fh->fddi_shost[3] = 0;
1456 fh->fddi_shost[4] = 1;
1457 fh->fddi_shost[5] = sc->sc_vhid;
1460 case IFT_ISO88025: {
1461 struct iso88025_header *th;
1462 th = mtod(m, struct iso88025_header *);
1463 th->iso88025_shost[0] = 3;
1464 th->iso88025_shost[1] = 0;
1465 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1466 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1467 th->iso88025_shost[4] = 0;
1468 th->iso88025_shost[5] = 0;
1472 printf("%s: carp is not supported for the %d interface type\n",
1473 ifp->if_xname, ifp->if_type);
1474 return (EOPNOTSUPP);
1480 static struct carp_softc*
1481 carp_alloc(struct ifnet *ifp)
1483 struct carp_softc *sc;
1484 struct carp_if *cif;
1486 if ((cif = ifp->if_carp) == NULL) {
1487 cif = carp_alloc_if(ifp);
1492 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1494 sc->sc_advbase = CARP_DFLTINTV;
1495 sc->sc_vhid = -1; /* required setting */
1496 sc->sc_init_counter = 1;
1497 sc->sc_state = INIT;
1499 sc->sc_ifasiz = sizeof(struct ifaddr *);
1500 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1501 sc->sc_carpdev = ifp;
1505 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1508 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1510 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1513 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1516 mtx_lock(&carp_mtx);
1517 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1518 mtx_unlock(&carp_mtx);
1524 carp_grow_ifas(struct carp_softc *sc)
1526 struct ifaddr **new;
1528 CARP_LOCK_ASSERT(sc);
1530 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1533 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1534 free(sc->sc_ifas, M_CARP);
1542 carp_destroy(struct carp_softc *sc)
1544 struct ifnet *ifp = sc->sc_carpdev;
1545 struct carp_if *cif = ifp->if_carp;
1547 CIF_LOCK_ASSERT(cif);
1549 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1551 mtx_lock(&carp_mtx);
1552 LIST_REMOVE(sc, sc_next);
1553 mtx_unlock(&carp_mtx);
1556 if (sc->sc_suppress)
1557 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1558 callout_drain(&sc->sc_ad_tmo);
1560 callout_drain(&sc->sc_md_tmo);
1563 callout_drain(&sc->sc_md6_tmo);
1565 CARP_LOCK_DESTROY(sc);
1567 free(sc->sc_ifas, M_CARP);
1571 static struct carp_if*
1572 carp_alloc_if(struct ifnet *ifp)
1574 struct carp_if *cif;
1576 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1578 if (ifpromisc(ifp, 1) != 0)
1583 TAILQ_INIT(&cif->cif_vrs);
1588 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);
1620 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1624 carpr->carpr_state = sc->sc_state;
1625 carpr->carpr_vhid = sc->sc_vhid;
1626 carpr->carpr_advbase = sc->sc_advbase;
1627 carpr->carpr_advskew = sc->sc_advskew;
1629 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1631 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1636 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1638 struct carpreq carpr;
1640 struct carp_softc *sc = NULL;
1641 int error = 0, locked = 0;
1643 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1646 ifp = ifunit_ref(ifr->ifr_name);
1650 switch (ifp->if_type) {
1662 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1663 error = EADDRNOTAVAIL;
1669 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1671 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1672 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1678 CIF_LOCK(ifp->if_carp);
1679 IFNET_FOREACH_CARP(ifp, sc)
1680 if (sc->sc_vhid == carpr.carpr_vhid)
1682 CIF_UNLOCK(ifp->if_carp);
1685 sc = carp_alloc(ifp);
1687 error = EINVAL; /* XXX: ifpromisc failed */
1692 sc->sc_vhid = carpr.carpr_vhid;
1693 LLADDR(&sc->sc_addr)[0] = 0;
1694 LLADDR(&sc->sc_addr)[1] = 0;
1695 LLADDR(&sc->sc_addr)[2] = 0x5e;
1696 LLADDR(&sc->sc_addr)[3] = 0;
1697 LLADDR(&sc->sc_addr)[4] = 1;
1698 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1702 if (carpr.carpr_advbase > 0) {
1703 if (carpr.carpr_advbase > 255 ||
1704 carpr.carpr_advbase < CARP_DFLTINTV) {
1708 sc->sc_advbase = carpr.carpr_advbase;
1710 if (carpr.carpr_advskew > 0) {
1711 if (carpr.carpr_advskew >= 255) {
1715 sc->sc_advskew = carpr.carpr_advskew;
1717 if (carpr.carpr_key[0] != '\0') {
1718 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1719 carp_hmac_prepare(sc);
1721 if (sc->sc_state != INIT &&
1722 carpr.carpr_state != sc->sc_state) {
1723 switch (carpr.carpr_state) {
1725 callout_stop(&sc->sc_ad_tmo);
1726 carp_set_state(sc, BACKUP);
1731 carp_master_down_locked(sc);
1743 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1747 if (carpr.carpr_count < 1) {
1751 if (ifp->if_carp == NULL) {
1756 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1757 if (carpr.carpr_vhid != 0) {
1758 CIF_LOCK(ifp->if_carp);
1759 IFNET_FOREACH_CARP(ifp, sc)
1760 if (sc->sc_vhid == carpr.carpr_vhid)
1762 CIF_UNLOCK(ifp->if_carp);
1767 carp_carprcp(&carpr, sc, priveleged);
1768 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1773 CIF_LOCK(ifp->if_carp);
1774 IFNET_FOREACH_CARP(ifp, sc)
1777 if (count > carpr.carpr_count) {
1778 CIF_UNLOCK(ifp->if_carp);
1784 IFNET_FOREACH_CARP(ifp, sc) {
1785 carp_carprcp(&carpr, sc, priveleged);
1786 carpr.carpr_count = count;
1787 error = copyout(&carpr, ifr->ifr_data +
1788 (i * sizeof(carpr)), sizeof(carpr));
1790 CIF_UNLOCK(ifp->if_carp);
1795 CIF_UNLOCK(ifp->if_carp);
1812 carp_get_vhid(struct ifaddr *ifa)
1815 if (ifa == NULL || ifa->ifa_carp == NULL)
1818 return (ifa->ifa_carp->sc_vhid);
1822 carp_attach(struct ifaddr *ifa, int vhid)
1824 struct ifnet *ifp = ifa->ifa_ifp;
1825 struct carp_if *cif = ifp->if_carp;
1826 struct carp_softc *sc;
1829 if (ifp->if_carp == NULL)
1830 return (ENOPROTOOPT);
1832 switch (ifa->ifa_addr->sa_family) {
1841 return (EPROTOTYPE);
1845 IFNET_FOREACH_CARP(ifp, sc)
1846 if (sc->sc_vhid == vhid)
1853 if (ifa->ifa_carp) {
1854 if (ifa->ifa_carp->sc_vhid != vhid)
1855 carp_detach_locked(ifa);
1862 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1869 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1870 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1871 if ((error = carp_grow_ifas(sc)) != 0) {
1872 carp_multicast_cleanup(cif,
1873 ifa->ifa_addr->sa_family);
1879 switch (ifa->ifa_addr->sa_family) {
1895 sc->sc_ifas[index - 1] = ifa;
1898 carp_hmac_prepare(sc);
1908 carp_detach(struct ifaddr *ifa)
1910 struct ifnet *ifp = ifa->ifa_ifp;
1911 struct carp_if *cif = ifp->if_carp;
1914 carp_detach_locked(ifa);
1919 carp_detach_locked(struct ifaddr *ifa)
1921 struct ifnet *ifp = ifa->ifa_ifp;
1922 struct carp_if *cif = ifp->if_carp;
1923 struct carp_softc *sc = ifa->ifa_carp;
1926 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1928 CIF_LOCK_ASSERT(cif);
1932 index = sc->sc_naddrs + sc->sc_naddrs6;
1933 for (i = 0; i < index; i++)
1934 if (sc->sc_ifas[i] == ifa)
1936 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1937 for (; i < index - 1; i++)
1938 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1939 sc->sc_ifas[index - 1] = NULL;
1941 switch (ifa->ifa_addr->sa_family) {
1956 carp_ifa_delroute(ifa);
1957 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1959 ifa->ifa_carp = NULL;
1962 carp_hmac_prepare(sc);
1965 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1973 carp_set_state(struct carp_softc *sc, int state)
1976 CARP_LOCK_ASSERT(sc);
1978 if (sc->sc_state != state) {
1979 const char *carp_states[] = { CARP_STATES };
1980 char subsys[IFNAMSIZ+5];
1982 sc->sc_state = state;
1984 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1985 sc->sc_carpdev->if_xname);
1986 devctl_notify("CARP", subsys, carp_states[state], NULL);
1991 carp_linkstate(struct ifnet *ifp)
1993 struct carp_softc *sc;
1995 CIF_LOCK(ifp->if_carp);
1996 IFNET_FOREACH_CARP(ifp, sc) {
2001 CIF_UNLOCK(ifp->if_carp);
2005 carp_sc_state(struct carp_softc *sc)
2008 CARP_LOCK_ASSERT(sc);
2010 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2011 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2012 callout_stop(&sc->sc_ad_tmo);
2014 callout_stop(&sc->sc_md_tmo);
2017 callout_stop(&sc->sc_md6_tmo);
2019 carp_set_state(sc, INIT);
2021 if (!sc->sc_suppress)
2022 carp_demote_adj(V_carp_ifdown_adj, "interface down");
2023 sc->sc_suppress = 1;
2025 carp_set_state(sc, INIT);
2027 if (sc->sc_suppress)
2028 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2029 sc->sc_suppress = 0;
2034 carp_demote_adj(int adj, char *reason)
2036 atomic_add_int(&V_carp_demotion, adj);
2037 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2038 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2042 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2046 new = V_carp_demotion;
2047 error = sysctl_handle_int(oidp, &new, 0, req);
2048 if (error || !req->newptr)
2051 carp_demote_adj(new, "sysctl");
2057 extern struct domain inetdomain;
2058 static struct protosw in_carp_protosw = {
2059 .pr_type = SOCK_RAW,
2060 .pr_domain = &inetdomain,
2061 .pr_protocol = IPPROTO_CARP,
2062 .pr_flags = PR_ATOMIC|PR_ADDR,
2063 .pr_input = carp_input,
2064 .pr_output = (pr_output_t *)rip_output,
2065 .pr_ctloutput = rip_ctloutput,
2066 .pr_usrreqs = &rip_usrreqs
2071 extern struct domain inet6domain;
2072 static struct ip6protosw in6_carp_protosw = {
2073 .pr_type = SOCK_RAW,
2074 .pr_domain = &inet6domain,
2075 .pr_protocol = IPPROTO_CARP,
2076 .pr_flags = PR_ATOMIC|PR_ADDR,
2077 .pr_input = carp6_input,
2078 .pr_output = rip6_output,
2079 .pr_ctloutput = rip6_ctloutput,
2080 .pr_usrreqs = &rip6_usrreqs
2085 carp_mod_cleanup(void)
2089 if (proto_reg[CARP_INET] == 0) {
2090 (void)ipproto_unregister(IPPROTO_CARP);
2091 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2092 proto_reg[CARP_INET] = -1;
2094 carp_iamatch_p = NULL;
2097 if (proto_reg[CARP_INET6] == 0) {
2098 (void)ip6proto_unregister(IPPROTO_CARP);
2099 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2100 proto_reg[CARP_INET6] = -1;
2102 carp_iamatch6_p = NULL;
2103 carp_macmatch6_p = NULL;
2105 carp_ioctl_p = NULL;
2106 carp_attach_p = NULL;
2107 carp_detach_p = NULL;
2108 carp_get_vhid_p = NULL;
2109 carp_linkstate_p = NULL;
2110 carp_forus_p = NULL;
2111 carp_output_p = NULL;
2112 carp_demote_adj_p = NULL;
2113 carp_master_p = NULL;
2114 mtx_unlock(&carp_mtx);
2115 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2116 mtx_destroy(&carp_mtx);
2124 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2125 LIST_INIT(&carp_list);
2126 carp_get_vhid_p = carp_get_vhid;
2127 carp_forus_p = carp_forus;
2128 carp_output_p = carp_output;
2129 carp_linkstate_p = carp_linkstate;
2130 carp_ioctl_p = carp_ioctl;
2131 carp_attach_p = carp_attach;
2132 carp_detach_p = carp_detach;
2133 carp_demote_adj_p = carp_demote_adj;
2134 carp_master_p = carp_master;
2136 carp_iamatch6_p = carp_iamatch6;
2137 carp_macmatch6_p = carp_macmatch6;
2138 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2139 (struct protosw *)&in6_carp_protosw);
2140 if (proto_reg[CARP_INET6]) {
2141 printf("carp: error %d attaching to PF_INET6\n",
2142 proto_reg[CARP_INET6]);
2144 return (proto_reg[CARP_INET6]);
2146 err = ip6proto_register(IPPROTO_CARP);
2148 printf("carp: error %d registering with INET6\n", err);
2154 carp_iamatch_p = carp_iamatch;
2155 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2156 if (proto_reg[CARP_INET]) {
2157 printf("carp: error %d attaching to PF_INET\n",
2158 proto_reg[CARP_INET]);
2160 return (proto_reg[CARP_INET]);
2162 err = ipproto_register(IPPROTO_CARP);
2164 printf("carp: error %d registering with INET\n", err);
2173 carp_modevent(module_t mod, int type, void *data)
2177 return carp_mod_load();
2180 mtx_lock(&carp_mtx);
2181 if (LIST_EMPTY(&carp_list))
2184 mtx_unlock(&carp_mtx);
2196 static moduledata_t carp_mod = {
2202 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);