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_VNET_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW,
220 &VNET_NAME(carp_allow), 0, "Accept incoming CARP packets");
221 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW,
222 &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
223 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW,
224 &VNET_NAME(carp_log), 0, "CARP log level");
225 SYSCTL_VNET_PROC(_net_inet_carp, OID_AUTO, demotion, CTLTYPE_INT|CTLFLAG_RW,
226 0, 0, carp_demote_adj_sysctl, "I",
227 "Adjust demotion factor (skew of advskew)");
228 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW,
229 &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
230 SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW,
231 &VNET_NAME(carp_ifdown_adj), 0,
232 "Interface down demotion factor adjustment");
234 VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
235 VNET_PCPUSTAT_SYSINIT(carpstats);
236 VNET_PCPUSTAT_SYSUNINIT(carpstats);
238 #define CARPSTATS_ADD(name, val) \
239 counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
240 sizeof(uint64_t)], (val))
241 #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1)
243 SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
244 carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
246 #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \
248 #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx)
249 #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED)
250 #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
251 #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
252 #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \
254 #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx)
255 #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED)
256 #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx)
257 #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx)
258 #define CIF_FREE(cif) do { \
259 CIF_LOCK_ASSERT(cif); \
260 if (TAILQ_EMPTY(&(cif)->cif_vrs)) \
266 #define CARP_LOG(...) do { \
267 if (V_carp_log > 0) \
268 log(LOG_INFO, "carp: " __VA_ARGS__); \
271 #define CARP_DEBUG(...) do { \
272 if (V_carp_log > 1) \
273 log(LOG_DEBUG, __VA_ARGS__); \
276 #define IFNET_FOREACH_IFA(ifp, ifa) \
277 IF_ADDR_LOCK_ASSERT(ifp); \
278 TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \
279 if ((ifa)->ifa_carp != NULL)
281 #define CARP_FOREACH_IFA(sc, ifa) \
282 CARP_LOCK_ASSERT(sc); \
284 _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \
285 ((ifa) = sc->sc_ifas[_i]) != NULL; \
288 #define IFNET_FOREACH_CARP(ifp, sc) \
289 CIF_LOCK_ASSERT(ifp->if_carp); \
290 TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
292 #define DEMOTE_ADVSKEW(sc) \
293 (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \
294 CARP_MAXSKEW : ((sc)->sc_advskew + V_carp_demotion))
296 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
297 static struct carp_softc
298 *carp_alloc(struct ifnet *);
299 static void carp_detach_locked(struct ifaddr *);
300 static void carp_destroy(struct carp_softc *);
301 static struct carp_if
302 *carp_alloc_if(struct ifnet *);
303 static void carp_free_if(struct carp_if *);
304 static void carp_set_state(struct carp_softc *, int);
305 static void carp_sc_state(struct carp_softc *);
306 static void carp_setrun(struct carp_softc *, sa_family_t);
307 static void carp_master_down(void *);
308 static void carp_master_down_locked(struct carp_softc *);
309 static void carp_send_ad(void *);
310 static void carp_send_ad_locked(struct carp_softc *);
311 static void carp_addroute(struct carp_softc *);
312 static void carp_ifa_addroute(struct ifaddr *);
313 static void carp_delroute(struct carp_softc *);
314 static void carp_ifa_delroute(struct ifaddr *);
315 static void carp_send_ad_all(void *, int);
316 static void carp_demote_adj(int, char *);
318 static LIST_HEAD(, carp_softc) carp_list;
319 static struct mtx carp_mtx;
320 static struct task carp_sendall_task =
321 TASK_INITIALIZER(0, carp_send_ad_all, NULL);
324 carp_hmac_prepare(struct carp_softc *sc)
326 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
327 uint8_t vhid = sc->sc_vhid & 0xff;
331 struct in_addr last, cur, in;
334 struct in6_addr last6, cur6, in6;
337 CARP_LOCK_ASSERT(sc);
339 /* Compute ipad from key. */
340 bzero(sc->sc_pad, sizeof(sc->sc_pad));
341 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
342 for (i = 0; i < sizeof(sc->sc_pad); i++)
343 sc->sc_pad[i] ^= 0x36;
345 /* Precompute first part of inner hash. */
346 SHA1Init(&sc->sc_sha1);
347 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
348 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
349 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
350 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
356 cur.s_addr = 0xffffffff;
357 CARP_FOREACH_IFA(sc, ifa) {
358 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
359 if (ifa->ifa_addr->sa_family == AF_INET &&
360 ntohl(in.s_addr) > ntohl(last.s_addr) &&
361 ntohl(in.s_addr) < ntohl(cur.s_addr)) {
362 cur.s_addr = in.s_addr;
367 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
371 memset(&cur6, 0, sizeof(cur6));
375 memset(&cur6, 0xff, sizeof(cur6));
376 CARP_FOREACH_IFA(sc, ifa) {
377 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
378 if (IN6_IS_SCOPE_EMBED(&in6))
379 in6.s6_addr16[1] = 0;
380 if (ifa->ifa_addr->sa_family == AF_INET6 &&
381 memcmp(&in6, &last6, sizeof(in6)) > 0 &&
382 memcmp(&in6, &cur6, sizeof(in6)) < 0) {
388 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
392 /* convert ipad to opad */
393 for (i = 0; i < sizeof(sc->sc_pad); i++)
394 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
398 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
399 unsigned char md[20])
403 CARP_LOCK_ASSERT(sc);
405 /* fetch first half of inner hash */
406 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
408 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
409 SHA1Final(md, &sha1ctx);
413 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
414 SHA1Update(&sha1ctx, md, 20);
415 SHA1Final(md, &sha1ctx);
419 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
420 unsigned char md[20])
422 unsigned char md2[20];
424 CARP_LOCK_ASSERT(sc);
426 carp_hmac_generate(sc, counter, md2);
428 return (bcmp(md, md2, sizeof(md2)));
432 * process input packet.
433 * we have rearranged checks order compared to the rfc,
434 * but it seems more efficient this way or not possible otherwise.
438 carp_input(struct mbuf **mp, int *offp, int proto)
440 struct mbuf *m = *mp;
441 struct ip *ip = mtod(m, struct ip *);
442 struct carp_header *ch;
448 CARPSTATS_INC(carps_ipackets);
452 return (IPPROTO_DONE);
455 /* verify that the IP TTL is 255. */
456 if (ip->ip_ttl != CARP_DFLTTL) {
457 CARPSTATS_INC(carps_badttl);
458 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
460 m->m_pkthdr.rcvif->if_xname);
462 return (IPPROTO_DONE);
465 iplen = ip->ip_hl << 2;
467 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
468 CARPSTATS_INC(carps_badlen);
469 CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
470 "on %s\n", __func__, m->m_len - sizeof(struct ip),
471 m->m_pkthdr.rcvif->if_xname);
473 return (IPPROTO_DONE);
476 if (iplen + sizeof(*ch) < m->m_len) {
477 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
478 CARPSTATS_INC(carps_hdrops);
479 CARP_DEBUG("%s: pullup failed\n", __func__);
480 return (IPPROTO_DONE);
482 ip = mtod(m, struct ip *);
484 ch = (struct carp_header *)((char *)ip + iplen);
487 * verify that the received packet length is
488 * equal to the CARP header
490 len = iplen + sizeof(*ch);
491 if (len > m->m_pkthdr.len) {
492 CARPSTATS_INC(carps_badlen);
493 CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
495 m->m_pkthdr.rcvif->if_xname);
497 return (IPPROTO_DONE);
500 if ((m = m_pullup(m, len)) == NULL) {
501 CARPSTATS_INC(carps_hdrops);
502 return (IPPROTO_DONE);
504 ip = mtod(m, struct ip *);
505 ch = (struct carp_header *)((char *)ip + iplen);
507 /* verify the CARP checksum */
509 if (in_cksum(m, len - iplen)) {
510 CARPSTATS_INC(carps_badsum);
511 CARP_DEBUG("%s: checksum failed on %s\n", __func__,
512 m->m_pkthdr.rcvif->if_xname);
514 return (IPPROTO_DONE);
518 carp_input_c(m, ch, AF_INET);
519 return (IPPROTO_DONE);
525 carp6_input(struct mbuf **mp, int *offp, int proto)
527 struct mbuf *m = *mp;
528 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
529 struct carp_header *ch;
532 CARPSTATS_INC(carps_ipackets6);
536 return (IPPROTO_DONE);
539 /* check if received on a valid carp interface */
540 if (m->m_pkthdr.rcvif->if_carp == NULL) {
541 CARPSTATS_INC(carps_badif);
542 CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
543 __func__, m->m_pkthdr.rcvif->if_xname);
545 return (IPPROTO_DONE);
548 /* verify that the IP TTL is 255 */
549 if (ip6->ip6_hlim != CARP_DFLTTL) {
550 CARPSTATS_INC(carps_badttl);
551 CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
552 ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
554 return (IPPROTO_DONE);
557 /* verify that we have a complete carp packet */
559 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
561 CARPSTATS_INC(carps_badlen);
562 CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
563 return (IPPROTO_DONE);
567 /* verify the CARP checksum */
569 if (in_cksum(m, sizeof(*ch))) {
570 CARPSTATS_INC(carps_badsum);
571 CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
572 m->m_pkthdr.rcvif->if_xname);
574 return (IPPROTO_DONE);
578 carp_input_c(m, ch, AF_INET6);
579 return (IPPROTO_DONE);
584 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
586 struct ifnet *ifp = m->m_pkthdr.rcvif;
588 struct carp_softc *sc;
589 uint64_t tmp_counter;
590 struct timeval sc_tv, ch_tv;
592 /* verify that the VHID is valid on the receiving interface */
594 IFNET_FOREACH_IFA(ifp, ifa)
595 if (ifa->ifa_addr->sa_family == af &&
596 ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
600 IF_ADDR_RUNLOCK(ifp);
603 CARPSTATS_INC(carps_badvhid);
608 /* verify the CARP version. */
609 if (ch->carp_version != CARP_VERSION) {
610 CARPSTATS_INC(carps_badver);
611 CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
622 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
623 CARPSTATS_INC(carps_badauth);
624 CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
625 sc->sc_vhid, ifp->if_xname);
629 tmp_counter = ntohl(ch->carp_counter[0]);
630 tmp_counter = tmp_counter<<32;
631 tmp_counter += ntohl(ch->carp_counter[1]);
633 /* XXX Replay protection goes here */
635 sc->sc_init_counter = 0;
636 sc->sc_counter = tmp_counter;
638 sc_tv.tv_sec = sc->sc_advbase;
639 sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
640 ch_tv.tv_sec = ch->carp_advbase;
641 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
643 switch (sc->sc_state) {
648 * If we receive an advertisement from a master who's going to
649 * be more frequent than us, go into BACKUP state.
651 if (timevalcmp(&sc_tv, &ch_tv, >) ||
652 timevalcmp(&sc_tv, &ch_tv, ==)) {
653 callout_stop(&sc->sc_ad_tmo);
654 CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
655 "(more frequent advertisement received)\n",
657 sc->sc_carpdev->if_xname);
658 carp_set_state(sc, BACKUP);
665 * If we're pre-empting masters who advertise slower than us,
666 * and this one claims to be slower, treat him as down.
668 if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
669 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
670 "(preempting a slower master)\n",
672 sc->sc_carpdev->if_xname);
673 carp_master_down_locked(sc);
678 * If the master is going to advertise at such a low frequency
679 * that he's guaranteed to time out, we'd might as well just
680 * treat him as timed out now.
682 sc_tv.tv_sec = sc->sc_advbase * 3;
683 if (timevalcmp(&sc_tv, &ch_tv, <)) {
684 CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
685 "(master timed out)\n",
687 sc->sc_carpdev->if_xname);
688 carp_master_down_locked(sc);
693 * Otherwise, we reset the counter and wait for the next
706 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
710 if (sc->sc_init_counter) {
711 /* this could also be seconds since unix epoch */
712 sc->sc_counter = arc4random();
713 sc->sc_counter = sc->sc_counter << 32;
714 sc->sc_counter += arc4random();
718 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
719 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
721 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
723 /* Tag packet for carp_output */
724 if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
725 M_NOWAIT)) == NULL) {
727 CARPSTATS_INC(carps_onomem);
730 bcopy(&sc, mtag + 1, sizeof(sc));
731 m_tag_prepend(m, mtag);
737 * To avoid LORs and possible recursions this function shouldn't
738 * be called directly, but scheduled via taskqueue.
741 carp_send_ad_all(void *ctx __unused, int pending __unused)
743 struct carp_softc *sc;
746 LIST_FOREACH(sc, &carp_list, sc_next)
747 if (sc->sc_state == MASTER) {
749 CURVNET_SET(sc->sc_carpdev->if_vnet);
750 carp_send_ad_locked(sc);
754 mtx_unlock(&carp_mtx);
757 /* Send a periodic advertisement, executed in callout context. */
759 carp_send_ad(void *v)
761 struct carp_softc *sc = v;
763 CARP_LOCK_ASSERT(sc);
764 CURVNET_SET(sc->sc_carpdev->if_vnet);
765 carp_send_ad_locked(sc);
771 carp_send_ad_error(struct carp_softc *sc, int error)
775 if (sc->sc_sendad_errors < INT_MAX)
776 sc->sc_sendad_errors++;
777 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
778 static const char fmt[] = "send error %d on %s";
779 char msg[sizeof(fmt) + IFNAMSIZ];
781 sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
782 carp_demote_adj(V_carp_senderr_adj, msg);
784 sc->sc_sendad_success = 0;
786 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS &&
787 ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
788 static const char fmt[] = "send ok on %s";
789 char msg[sizeof(fmt) + IFNAMSIZ];
791 sprintf(msg, fmt, sc->sc_carpdev->if_xname);
792 carp_demote_adj(-V_carp_senderr_adj, msg);
793 sc->sc_sendad_errors = 0;
795 sc->sc_sendad_errors = 0;
800 carp_send_ad_locked(struct carp_softc *sc)
802 struct carp_header ch;
806 struct carp_header *ch_ptr;
810 CARP_LOCK_ASSERT(sc);
812 advskew = DEMOTE_ADVSKEW(sc);
813 tv.tv_sec = sc->sc_advbase;
814 tv.tv_usec = advskew * 1000000 / 256;
816 ch.carp_version = CARP_VERSION;
817 ch.carp_type = CARP_ADVERTISEMENT;
818 ch.carp_vhid = sc->sc_vhid;
819 ch.carp_advbase = sc->sc_advbase;
820 ch.carp_advskew = advskew;
821 ch.carp_authlen = 7; /* XXX DEFINE */
822 ch.carp_pad1 = 0; /* must be zero */
825 /* XXXGL: OpenBSD picks first ifaddr with needed family. */
831 m = m_gethdr(M_NOWAIT, MT_DATA);
833 CARPSTATS_INC(carps_onomem);
836 len = sizeof(*ip) + sizeof(ch);
837 m->m_pkthdr.len = len;
838 m->m_pkthdr.rcvif = NULL;
840 MH_ALIGN(m, m->m_len);
841 m->m_flags |= M_MCAST;
842 ip = mtod(m, struct ip *);
843 ip->ip_v = IPVERSION;
844 ip->ip_hl = sizeof(*ip) >> 2;
845 ip->ip_tos = IPTOS_LOWDELAY;
846 ip->ip_len = htons(len);
847 ip->ip_id = ip_newid();
848 ip->ip_off = htons(IP_DF);
849 ip->ip_ttl = CARP_DFLTTL;
850 ip->ip_p = IPPROTO_CARP;
853 bzero(&sa, sizeof(sa));
854 sa.sa_family = AF_INET;
855 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
858 ifatoia(ifa)->ia_addr.sin_addr.s_addr;
861 ip->ip_src.s_addr = 0;
862 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
864 ch_ptr = (struct carp_header *)(&ip[1]);
865 bcopy(&ch, ch_ptr, sizeof(ch));
866 if (carp_prepare_ad(m, sc, ch_ptr))
869 m->m_data += sizeof(*ip);
870 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
871 m->m_data -= sizeof(*ip);
873 CARPSTATS_INC(carps_opackets);
875 carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
876 &sc->sc_carpdev->if_carp->cif_imo, NULL));
880 if (sc->sc_naddrs6) {
883 m = m_gethdr(M_NOWAIT, MT_DATA);
885 CARPSTATS_INC(carps_onomem);
888 len = sizeof(*ip6) + sizeof(ch);
889 m->m_pkthdr.len = len;
890 m->m_pkthdr.rcvif = NULL;
892 MH_ALIGN(m, m->m_len);
893 m->m_flags |= M_MCAST;
894 ip6 = mtod(m, struct ip6_hdr *);
895 bzero(ip6, sizeof(*ip6));
896 ip6->ip6_vfc |= IPV6_VERSION;
897 ip6->ip6_hlim = CARP_DFLTTL;
898 ip6->ip6_nxt = IPPROTO_CARP;
899 bzero(&sa, sizeof(sa));
901 /* set the source address */
902 sa.sa_family = AF_INET6;
903 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
905 bcopy(IFA_IN6(ifa), &ip6->ip6_src,
906 sizeof(struct in6_addr));
909 /* This should never happen with IPv6. */
910 bzero(&ip6->ip6_src, sizeof(struct in6_addr));
912 /* Set the multicast destination. */
913 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
914 ip6->ip6_dst.s6_addr8[15] = 0x12;
915 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
917 CARP_DEBUG("%s: in6_setscope failed\n", __func__);
921 ch_ptr = (struct carp_header *)(&ip6[1]);
922 bcopy(&ch, ch_ptr, sizeof(ch));
923 if (carp_prepare_ad(m, sc, ch_ptr))
926 m->m_data += sizeof(*ip6);
927 ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
928 m->m_data -= sizeof(*ip6);
930 CARPSTATS_INC(carps_opackets6);
932 carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
933 &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
938 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
942 carp_addroute(struct carp_softc *sc)
946 CARP_FOREACH_IFA(sc, ifa)
947 carp_ifa_addroute(ifa);
951 carp_ifa_addroute(struct ifaddr *ifa)
954 switch (ifa->ifa_addr->sa_family) {
957 in_addprefix(ifatoia(ifa), RTF_UP);
958 ifa_add_loopback_route(ifa,
959 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
964 ifa_add_loopback_route(ifa,
965 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
966 nd6_add_ifa_lle(ifatoia6(ifa));
973 carp_delroute(struct carp_softc *sc)
977 CARP_FOREACH_IFA(sc, ifa)
978 carp_ifa_delroute(ifa);
982 carp_ifa_delroute(struct ifaddr *ifa)
985 switch (ifa->ifa_addr->sa_family) {
988 ifa_del_loopback_route(ifa,
989 (struct sockaddr *)&ifatoia(ifa)->ia_addr);
990 in_scrubprefix(ifatoia(ifa), LLE_STATIC);
995 ifa_del_loopback_route(ifa,
996 (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
997 nd6_rem_ifa_lle(ifatoia6(ifa));
1004 carp_master(struct ifaddr *ifa)
1006 struct carp_softc *sc = ifa->ifa_carp;
1008 return (sc->sc_state == MASTER);
1013 * Broadcast a gratuitous ARP request containing
1014 * the virtual router MAC address for each IP address
1015 * associated with the virtual router.
1018 carp_send_arp(struct carp_softc *sc)
1022 CARP_FOREACH_IFA(sc, ifa)
1023 if (ifa->ifa_addr->sa_family == AF_INET)
1024 arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
1028 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
1030 struct carp_softc *sc = ifa->ifa_carp;
1032 if (sc->sc_state == MASTER) {
1033 *enaddr = LLADDR(&sc->sc_addr);
1043 carp_send_na(struct carp_softc *sc)
1045 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1047 struct in6_addr *in6;
1049 CARP_FOREACH_IFA(sc, ifa) {
1050 if (ifa->ifa_addr->sa_family != AF_INET6)
1054 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1055 ND_NA_FLAG_OVERRIDE, 1, NULL);
1056 DELAY(1000); /* XXX */
1061 * Returns ifa in case it's a carp address and it is MASTER, or if the address
1062 * matches and is not a carp address. Returns NULL otherwise.
1065 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1071 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1072 if (ifa->ifa_addr->sa_family != AF_INET6)
1074 if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1076 if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1082 IF_ADDR_RUNLOCK(ifp);
1088 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1093 IFNET_FOREACH_IFA(ifp, ifa)
1094 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1095 IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1096 struct carp_softc *sc = ifa->ifa_carp;
1099 IF_ADDR_RUNLOCK(ifp);
1101 mtag = m_tag_get(PACKET_TAG_CARP,
1102 sizeof(struct carp_softc *), M_NOWAIT);
1104 /* Better a bit than nothing. */
1105 return (LLADDR(&sc->sc_addr));
1107 bcopy(&sc, mtag + 1, sizeof(sc));
1108 m_tag_prepend(m, mtag);
1110 return (LLADDR(&sc->sc_addr));
1112 IF_ADDR_RUNLOCK(ifp);
1119 carp_forus(struct ifnet *ifp, u_char *dhost)
1121 struct carp_softc *sc;
1122 uint8_t *ena = dhost;
1124 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1127 CIF_LOCK(ifp->if_carp);
1128 IFNET_FOREACH_CARP(ifp, sc) {
1130 if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1133 CIF_UNLOCK(ifp->if_carp);
1138 CIF_UNLOCK(ifp->if_carp);
1143 /* Master down timeout event, executed in callout context. */
1145 carp_master_down(void *v)
1147 struct carp_softc *sc = v;
1149 CARP_LOCK_ASSERT(sc);
1151 CURVNET_SET(sc->sc_carpdev->if_vnet);
1152 if (sc->sc_state == BACKUP) {
1153 CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
1155 sc->sc_carpdev->if_xname);
1156 carp_master_down_locked(sc);
1164 carp_master_down_locked(struct carp_softc *sc)
1167 CARP_LOCK_ASSERT(sc);
1169 switch (sc->sc_state) {
1171 carp_set_state(sc, MASTER);
1172 carp_send_ad_locked(sc);
1185 panic("carp: VHID %u@%s: master_down event in %s state\n",
1187 sc->sc_carpdev->if_xname,
1188 sc->sc_state ? "MASTER" : "INIT");
1195 * When in backup state, af indicates whether to reset the master down timer
1196 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1199 carp_setrun(struct carp_softc *sc, sa_family_t af)
1203 CARP_LOCK_ASSERT(sc);
1205 if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1206 sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1207 (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1210 switch (sc->sc_state) {
1212 CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1214 sc->sc_carpdev->if_xname);
1215 carp_set_state(sc, BACKUP);
1219 callout_stop(&sc->sc_ad_tmo);
1220 tv.tv_sec = 3 * sc->sc_advbase;
1221 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1225 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1226 carp_master_down, sc);
1231 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1232 carp_master_down, sc);
1238 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1239 carp_master_down, sc);
1243 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1244 carp_master_down, sc);
1250 tv.tv_sec = sc->sc_advbase;
1251 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1252 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1259 * Setup multicast structures.
1262 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1264 struct ifnet *ifp = cif->cif_ifp;
1267 CIF_LOCK_ASSERT(cif);
1273 struct ip_moptions *imo = &cif->cif_imo;
1274 struct in_addr addr;
1276 if (imo->imo_membership)
1279 imo->imo_membership = (struct in_multi **)malloc(
1280 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1282 if (imo->imo_membership == NULL)
1284 imo->imo_mfilters = NULL;
1285 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1286 imo->imo_multicast_vif = -1;
1288 addr.s_addr = htonl(INADDR_CARP_GROUP);
1289 if ((error = in_joingroup(ifp, &addr, NULL,
1290 &imo->imo_membership[0])) != 0) {
1291 free(imo->imo_membership, M_CARP);
1294 imo->imo_num_memberships++;
1295 imo->imo_multicast_ifp = ifp;
1296 imo->imo_multicast_ttl = CARP_DFLTTL;
1297 imo->imo_multicast_loop = 0;
1304 struct ip6_moptions *im6o = &cif->cif_im6o;
1305 struct in6_addr in6;
1306 struct in6_multi *in6m;
1308 if (im6o->im6o_membership)
1311 im6o->im6o_membership = (struct in6_multi **)malloc(
1312 (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1314 if (im6o->im6o_membership == NULL)
1316 im6o->im6o_mfilters = NULL;
1317 im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1318 im6o->im6o_multicast_hlim = CARP_DFLTTL;
1319 im6o->im6o_multicast_ifp = ifp;
1321 /* Join IPv6 CARP multicast group. */
1322 bzero(&in6, sizeof(in6));
1323 in6.s6_addr16[0] = htons(0xff02);
1324 in6.s6_addr8[15] = 0x12;
1325 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1326 free(im6o->im6o_membership, M_CARP);
1330 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1331 free(im6o->im6o_membership, M_CARP);
1334 im6o->im6o_membership[0] = in6m;
1335 im6o->im6o_num_memberships++;
1337 /* Join solicited multicast address. */
1338 bzero(&in6, sizeof(in6));
1339 in6.s6_addr16[0] = htons(0xff02);
1340 in6.s6_addr32[1] = 0;
1341 in6.s6_addr32[2] = htonl(1);
1342 in6.s6_addr32[3] = 0;
1343 in6.s6_addr8[12] = 0xff;
1344 if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1345 in6_mc_leave(im6o->im6o_membership[0], NULL);
1346 free(im6o->im6o_membership, M_CARP);
1350 if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1351 in6_mc_leave(im6o->im6o_membership[0], NULL);
1352 free(im6o->im6o_membership, M_CARP);
1355 im6o->im6o_membership[1] = in6m;
1356 im6o->im6o_num_memberships++;
1366 * Free multicast structures.
1369 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1372 CIF_LOCK_ASSERT(cif);
1376 if (cif->cif_naddrs == 0) {
1377 struct ip_moptions *imo = &cif->cif_imo;
1379 in_leavegroup(imo->imo_membership[0], NULL);
1380 KASSERT(imo->imo_mfilters == NULL,
1381 ("%s: imo_mfilters != NULL", __func__));
1382 free(imo->imo_membership, M_CARP);
1383 imo->imo_membership = NULL;
1390 if (cif->cif_naddrs6 == 0) {
1391 struct ip6_moptions *im6o = &cif->cif_im6o;
1393 in6_mc_leave(im6o->im6o_membership[0], NULL);
1394 in6_mc_leave(im6o->im6o_membership[1], NULL);
1395 KASSERT(im6o->im6o_mfilters == NULL,
1396 ("%s: im6o_mfilters != NULL", __func__));
1397 free(im6o->im6o_membership, M_CARP);
1398 im6o->im6o_membership = NULL;
1406 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1409 struct carp_softc *sc;
1414 switch (sa->sa_family) {
1427 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1431 bcopy(mtag + 1, &sc, sizeof(sc));
1433 /* Set the source MAC address to the Virtual Router MAC Address. */
1434 switch (ifp->if_type) {
1438 struct ether_header *eh;
1440 eh = mtod(m, struct ether_header *);
1441 eh->ether_shost[0] = 0;
1442 eh->ether_shost[1] = 0;
1443 eh->ether_shost[2] = 0x5e;
1444 eh->ether_shost[3] = 0;
1445 eh->ether_shost[4] = 1;
1446 eh->ether_shost[5] = sc->sc_vhid;
1450 struct fddi_header *fh;
1452 fh = mtod(m, struct fddi_header *);
1453 fh->fddi_shost[0] = 0;
1454 fh->fddi_shost[1] = 0;
1455 fh->fddi_shost[2] = 0x5e;
1456 fh->fddi_shost[3] = 0;
1457 fh->fddi_shost[4] = 1;
1458 fh->fddi_shost[5] = sc->sc_vhid;
1461 case IFT_ISO88025: {
1462 struct iso88025_header *th;
1463 th = mtod(m, struct iso88025_header *);
1464 th->iso88025_shost[0] = 3;
1465 th->iso88025_shost[1] = 0;
1466 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1467 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1468 th->iso88025_shost[4] = 0;
1469 th->iso88025_shost[5] = 0;
1473 printf("%s: carp is not supported for the %d interface type\n",
1474 ifp->if_xname, ifp->if_type);
1475 return (EOPNOTSUPP);
1481 static struct carp_softc*
1482 carp_alloc(struct ifnet *ifp)
1484 struct carp_softc *sc;
1485 struct carp_if *cif;
1487 if ((cif = ifp->if_carp) == NULL)
1488 cif = carp_alloc_if(ifp);
1490 sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1492 sc->sc_advbase = CARP_DFLTINTV;
1493 sc->sc_vhid = -1; /* required setting */
1494 sc->sc_init_counter = 1;
1495 sc->sc_state = INIT;
1497 sc->sc_ifasiz = sizeof(struct ifaddr *);
1498 sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1499 sc->sc_carpdev = ifp;
1503 callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1506 callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1508 callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1511 TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1514 mtx_lock(&carp_mtx);
1515 LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1516 mtx_unlock(&carp_mtx);
1522 carp_grow_ifas(struct carp_softc *sc)
1524 struct ifaddr **new;
1526 CARP_LOCK_ASSERT(sc);
1528 new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1531 bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1532 free(sc->sc_ifas, M_CARP);
1540 carp_destroy(struct carp_softc *sc)
1542 struct ifnet *ifp = sc->sc_carpdev;
1543 struct carp_if *cif = ifp->if_carp;
1545 CIF_LOCK_ASSERT(cif);
1547 TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1549 mtx_lock(&carp_mtx);
1550 LIST_REMOVE(sc, sc_next);
1551 mtx_unlock(&carp_mtx);
1554 if (sc->sc_suppress)
1555 carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
1556 callout_drain(&sc->sc_ad_tmo);
1558 callout_drain(&sc->sc_md_tmo);
1561 callout_drain(&sc->sc_md6_tmo);
1563 CARP_LOCK_DESTROY(sc);
1565 free(sc->sc_ifas, M_CARP);
1569 static struct carp_if*
1570 carp_alloc_if(struct ifnet *ifp)
1572 struct carp_if *cif;
1575 cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1577 if ((error = ifpromisc(ifp, 1)) != 0)
1578 printf("%s: ifpromisc(%s) failed: %d\n",
1579 __func__, ifp->if_xname, error);
1581 cif->cif_flags |= CIF_PROMISC;
1585 TAILQ_INIT(&cif->cif_vrs);
1590 IF_ADDR_WUNLOCK(ifp);
1596 carp_free_if(struct carp_if *cif)
1598 struct ifnet *ifp = cif->cif_ifp;
1600 CIF_LOCK_ASSERT(cif);
1601 KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1605 ifp->if_carp = NULL;
1606 IF_ADDR_WUNLOCK(ifp);
1608 CIF_LOCK_DESTROY(cif);
1610 if (cif->cif_flags & CIF_PROMISC)
1618 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1622 carpr->carpr_state = sc->sc_state;
1623 carpr->carpr_vhid = sc->sc_vhid;
1624 carpr->carpr_advbase = sc->sc_advbase;
1625 carpr->carpr_advskew = sc->sc_advskew;
1627 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1629 bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1634 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1636 struct carpreq carpr;
1638 struct carp_softc *sc = NULL;
1639 int error = 0, locked = 0;
1641 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1644 ifp = ifunit_ref(ifr->ifr_name);
1648 switch (ifp->if_type) {
1660 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1661 error = EADDRNOTAVAIL;
1667 if ((error = priv_check(td, PRIV_NETINET_CARP)))
1669 if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1670 carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1676 CIF_LOCK(ifp->if_carp);
1677 IFNET_FOREACH_CARP(ifp, sc)
1678 if (sc->sc_vhid == carpr.carpr_vhid)
1680 CIF_UNLOCK(ifp->if_carp);
1683 sc = carp_alloc(ifp);
1685 sc->sc_vhid = carpr.carpr_vhid;
1686 LLADDR(&sc->sc_addr)[0] = 0;
1687 LLADDR(&sc->sc_addr)[1] = 0;
1688 LLADDR(&sc->sc_addr)[2] = 0x5e;
1689 LLADDR(&sc->sc_addr)[3] = 0;
1690 LLADDR(&sc->sc_addr)[4] = 1;
1691 LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1695 if (carpr.carpr_advbase > 0) {
1696 if (carpr.carpr_advbase > 255 ||
1697 carpr.carpr_advbase < CARP_DFLTINTV) {
1701 sc->sc_advbase = carpr.carpr_advbase;
1703 if (carpr.carpr_advskew > 0) {
1704 if (carpr.carpr_advskew >= 255) {
1708 sc->sc_advskew = carpr.carpr_advskew;
1710 if (carpr.carpr_key[0] != '\0') {
1711 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1712 carp_hmac_prepare(sc);
1714 if (sc->sc_state != INIT &&
1715 carpr.carpr_state != sc->sc_state) {
1716 switch (carpr.carpr_state) {
1718 callout_stop(&sc->sc_ad_tmo);
1719 carp_set_state(sc, BACKUP);
1724 carp_master_down_locked(sc);
1736 if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1740 if (carpr.carpr_count < 1) {
1744 if (ifp->if_carp == NULL) {
1749 priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1750 if (carpr.carpr_vhid != 0) {
1751 CIF_LOCK(ifp->if_carp);
1752 IFNET_FOREACH_CARP(ifp, sc)
1753 if (sc->sc_vhid == carpr.carpr_vhid)
1755 CIF_UNLOCK(ifp->if_carp);
1760 carp_carprcp(&carpr, sc, priveleged);
1761 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1766 CIF_LOCK(ifp->if_carp);
1767 IFNET_FOREACH_CARP(ifp, sc)
1770 if (count > carpr.carpr_count) {
1771 CIF_UNLOCK(ifp->if_carp);
1777 IFNET_FOREACH_CARP(ifp, sc) {
1778 carp_carprcp(&carpr, sc, priveleged);
1779 carpr.carpr_count = count;
1780 error = copyout(&carpr, ifr->ifr_data +
1781 (i * sizeof(carpr)), sizeof(carpr));
1783 CIF_UNLOCK(ifp->if_carp);
1788 CIF_UNLOCK(ifp->if_carp);
1805 carp_get_vhid(struct ifaddr *ifa)
1808 if (ifa == NULL || ifa->ifa_carp == NULL)
1811 return (ifa->ifa_carp->sc_vhid);
1815 carp_attach(struct ifaddr *ifa, int vhid)
1817 struct ifnet *ifp = ifa->ifa_ifp;
1818 struct carp_if *cif = ifp->if_carp;
1819 struct carp_softc *sc;
1822 if (ifp->if_carp == NULL)
1823 return (ENOPROTOOPT);
1825 switch (ifa->ifa_addr->sa_family) {
1834 return (EPROTOTYPE);
1838 IFNET_FOREACH_CARP(ifp, sc)
1839 if (sc->sc_vhid == vhid)
1846 if (ifa->ifa_carp) {
1847 if (ifa->ifa_carp->sc_vhid != vhid)
1848 carp_detach_locked(ifa);
1855 error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1862 index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1863 if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1864 if ((error = carp_grow_ifas(sc)) != 0) {
1865 carp_multicast_cleanup(cif,
1866 ifa->ifa_addr->sa_family);
1872 switch (ifa->ifa_addr->sa_family) {
1888 sc->sc_ifas[index - 1] = ifa;
1891 carp_hmac_prepare(sc);
1901 carp_detach(struct ifaddr *ifa)
1903 struct ifnet *ifp = ifa->ifa_ifp;
1904 struct carp_if *cif = ifp->if_carp;
1907 carp_detach_locked(ifa);
1912 carp_detach_locked(struct ifaddr *ifa)
1914 struct ifnet *ifp = ifa->ifa_ifp;
1915 struct carp_if *cif = ifp->if_carp;
1916 struct carp_softc *sc = ifa->ifa_carp;
1919 KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1921 CIF_LOCK_ASSERT(cif);
1925 index = sc->sc_naddrs + sc->sc_naddrs6;
1926 for (i = 0; i < index; i++)
1927 if (sc->sc_ifas[i] == ifa)
1929 KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1930 for (; i < index - 1; i++)
1931 sc->sc_ifas[i] = sc->sc_ifas[i+1];
1932 sc->sc_ifas[index - 1] = NULL;
1934 switch (ifa->ifa_addr->sa_family) {
1949 carp_ifa_delroute(ifa);
1950 carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1952 ifa->ifa_carp = NULL;
1955 carp_hmac_prepare(sc);
1958 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1966 carp_set_state(struct carp_softc *sc, int state)
1969 CARP_LOCK_ASSERT(sc);
1971 if (sc->sc_state != state) {
1972 const char *carp_states[] = { CARP_STATES };
1973 char subsys[IFNAMSIZ+5];
1975 sc->sc_state = state;
1977 snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1978 sc->sc_carpdev->if_xname);
1979 devctl_notify("CARP", subsys, carp_states[state], NULL);
1984 carp_linkstate(struct ifnet *ifp)
1986 struct carp_softc *sc;
1988 CIF_LOCK(ifp->if_carp);
1989 IFNET_FOREACH_CARP(ifp, sc) {
1994 CIF_UNLOCK(ifp->if_carp);
1998 carp_sc_state(struct carp_softc *sc)
2001 CARP_LOCK_ASSERT(sc);
2003 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2004 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2005 callout_stop(&sc->sc_ad_tmo);
2007 callout_stop(&sc->sc_md_tmo);
2010 callout_stop(&sc->sc_md6_tmo);
2012 carp_set_state(sc, INIT);
2014 if (!sc->sc_suppress)
2015 carp_demote_adj(V_carp_ifdown_adj, "interface down");
2016 sc->sc_suppress = 1;
2018 carp_set_state(sc, INIT);
2020 if (sc->sc_suppress)
2021 carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2022 sc->sc_suppress = 0;
2027 carp_demote_adj(int adj, char *reason)
2029 atomic_add_int(&V_carp_demotion, adj);
2030 CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2031 taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2035 carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
2039 new = V_carp_demotion;
2040 error = sysctl_handle_int(oidp, &new, 0, req);
2041 if (error || !req->newptr)
2044 carp_demote_adj(new, "sysctl");
2050 extern struct domain inetdomain;
2051 static struct protosw in_carp_protosw = {
2052 .pr_type = SOCK_RAW,
2053 .pr_domain = &inetdomain,
2054 .pr_protocol = IPPROTO_CARP,
2055 .pr_flags = PR_ATOMIC|PR_ADDR,
2056 .pr_input = carp_input,
2057 .pr_output = rip_output,
2058 .pr_ctloutput = rip_ctloutput,
2059 .pr_usrreqs = &rip_usrreqs
2064 extern struct domain inet6domain;
2065 static struct protosw in6_carp_protosw = {
2066 .pr_type = SOCK_RAW,
2067 .pr_domain = &inet6domain,
2068 .pr_protocol = IPPROTO_CARP,
2069 .pr_flags = PR_ATOMIC|PR_ADDR,
2070 .pr_input = carp6_input,
2071 .pr_output = rip6_output,
2072 .pr_ctloutput = rip6_ctloutput,
2073 .pr_usrreqs = &rip6_usrreqs
2078 carp_mod_cleanup(void)
2082 if (proto_reg[CARP_INET] == 0) {
2083 (void)ipproto_unregister(IPPROTO_CARP);
2084 pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2085 proto_reg[CARP_INET] = -1;
2087 carp_iamatch_p = NULL;
2090 if (proto_reg[CARP_INET6] == 0) {
2091 (void)ip6proto_unregister(IPPROTO_CARP);
2092 pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2093 proto_reg[CARP_INET6] = -1;
2095 carp_iamatch6_p = NULL;
2096 carp_macmatch6_p = NULL;
2098 carp_ioctl_p = NULL;
2099 carp_attach_p = NULL;
2100 carp_detach_p = NULL;
2101 carp_get_vhid_p = NULL;
2102 carp_linkstate_p = NULL;
2103 carp_forus_p = NULL;
2104 carp_output_p = NULL;
2105 carp_demote_adj_p = NULL;
2106 carp_master_p = NULL;
2107 mtx_unlock(&carp_mtx);
2108 taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2109 mtx_destroy(&carp_mtx);
2117 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2118 LIST_INIT(&carp_list);
2119 carp_get_vhid_p = carp_get_vhid;
2120 carp_forus_p = carp_forus;
2121 carp_output_p = carp_output;
2122 carp_linkstate_p = carp_linkstate;
2123 carp_ioctl_p = carp_ioctl;
2124 carp_attach_p = carp_attach;
2125 carp_detach_p = carp_detach;
2126 carp_demote_adj_p = carp_demote_adj;
2127 carp_master_p = carp_master;
2129 carp_iamatch6_p = carp_iamatch6;
2130 carp_macmatch6_p = carp_macmatch6;
2131 proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2132 (struct protosw *)&in6_carp_protosw);
2133 if (proto_reg[CARP_INET6]) {
2134 printf("carp: error %d attaching to PF_INET6\n",
2135 proto_reg[CARP_INET6]);
2137 return (proto_reg[CARP_INET6]);
2139 err = ip6proto_register(IPPROTO_CARP);
2141 printf("carp: error %d registering with INET6\n", err);
2147 carp_iamatch_p = carp_iamatch;
2148 proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2149 if (proto_reg[CARP_INET]) {
2150 printf("carp: error %d attaching to PF_INET\n",
2151 proto_reg[CARP_INET]);
2153 return (proto_reg[CARP_INET]);
2155 err = ipproto_register(IPPROTO_CARP);
2157 printf("carp: error %d registering with INET\n", err);
2166 carp_modevent(module_t mod, int type, void *data)
2170 return carp_mod_load();
2173 mtx_lock(&carp_mtx);
2174 if (LIST_EMPTY(&carp_list))
2177 mtx_unlock(&carp_mtx);
2189 static moduledata_t carp_mod = {
2195 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);