2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3 * Copyright (c) 2003 Ryan McBride. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/sys/netinet/ip_carp.c,v 1.48 2007/02/02 09:39:09 glebius Exp $
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/in_cksum.h>
38 #include <sys/limits.h>
39 #include <sys/malloc.h>
41 #include <sys/msgport2.h>
45 #include <sys/sockio.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/thread.h>
51 #include <machine/stdarg.h>
52 #include <crypto/sha1.h>
55 #include <net/ethernet.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 #include <net/if_clone.h>
61 #include <net/if_var.h>
62 #include <net/ifq_var.h>
63 #include <net/netmsg2.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/if_ether.h>
75 #include <netinet/icmp6.h>
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/scope6_var.h>
79 #include <netinet6/nd6.h>
82 #include <netinet/ip_carp.h>
85 * Note about carp's MP safe approach:
87 * Brief: carp_softc (softc), carp_softc_container (scc)
89 * - All configuration operation, e.g. ioctl, add/delete inet addresses
90 * is serialized by netisr0; not by carp's serializer
92 * - Backing interface's if_carp and carp_softc's relationship:
95 * if_carp -->| carp_if |
101 * | scc |-->| softc |
107 * | scc |-->| softc |
111 * - if_carp creation, modification and deletion all happen in netisr0,
112 * as stated previously. Since if_carp is accessed by multiple netisrs,
113 * the modification to if_carp is conducted in the following way:
117 * 1) Duplicate the old carp_if to new carp_if (ncif), and insert the
118 * to-be-added carp_softc to the new carp_if (ncif):
123 * +---------+ +---------+
124 * | carp_if | | carp_if |
125 * +---------+ +---------+
129 * +-----+ | | +-----+
130 * | scc |---->| softc |<----| scc |
131 * +-----+ | | +-----+
135 * +-----+ | | +-----+
136 * | scc |---->| softc |<----| scc |
137 * +-----+ | | +-----+
142 * | softc |<----| scc |
146 * 2) Switch save if_carp into ocif and switch if_carp to ncif:
151 * +---------+ +---------+
152 * | carp_if | | carp_if |
153 * +---------+ +---------+
157 * +-----+ | | +-----+
158 * | scc |---->| softc |<----| scc |
159 * +-----+ | | +-----+
163 * +-----+ | | +-----+
164 * | scc |---->| softc |<----| scc |
165 * +-----+ | | +-----+
170 * | softc |<----| scc |
174 * 3) Run netmsg_service_sync(), which will make sure that
175 * ocif is no longer accessed (all network operations
176 * are happened only in network threads).
177 * 4) Free ocif -- only carp_if and scc are freed.
180 * Removing carp_softc:
182 * 1) Duplicate the old carp_if to new carp_if (ncif); the to-be-deleted
183 * carp_softc will not be duplicated.
188 * +---------+ +---------+
189 * | carp_if | | carp_if |
190 * +---------+ +---------+
194 * +-----+ | | +-----+
195 * | scc |---->| softc |<----| scc |
196 * +-----+ | | +-----+
201 * | scc |---->| softc | |
206 * +-----+ | | +-----+
207 * | scc |---->| softc |<----| scc |
208 * +-----+ | | +-----+
211 * 2) Switch save if_carp into ocif and switch if_carp to ncif:
216 * +---------+ +---------+
217 * | carp_if | | carp_if |
218 * +---------+ +---------+
222 * +-----+ | | +-----+
223 * | scc |---->| softc |<----| scc |
224 * +-----+ | | +-----+
229 * | scc |---->| softc | |
234 * +-----+ | | +-----+
235 * | scc |---->| softc |<----| scc |
236 * +-----+ | | +-----+
239 * 3) Run netmsg_service_sync(), which will make sure that
240 * ocif is no longer accessed (all network operations
241 * are happened only in network threads).
242 * 4) Free ocif -- only carp_if and scc are freed.
244 * - if_carp accessing:
245 * The accessing code should cache the if_carp in a local temporary
246 * variable and accessing the temporary variable along the code path
247 * instead of accessing if_carp later on.
250 #define CARP_IFNAME "carp"
251 #define CARP_IS_RUNNING(ifp) \
252 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
257 uint32_t vha_flags; /* CARP_VHAF_ */
258 struct in_ifaddr *vha_ia; /* carp address */
259 struct in_ifaddr *vha_iaback; /* backing address */
260 TAILQ_ENTRY(carp_vhaddr) vha_link;
262 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr);
265 struct netmsg_base base;
266 struct ifnet *nc_carpdev;
267 struct carp_softc *nc_softc;
273 struct arpcom arpcom;
274 struct ifnet *sc_carpdev; /* parent interface */
275 struct carp_vhaddr_list sc_vha_list; /* virtual addr list */
277 const struct in_ifaddr *sc_ia; /* primary iface address v4 */
278 struct ip_moptions sc_imo;
281 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
282 struct ip6_moptions sc_im6o;
285 enum { INIT = 0, BACKUP, MASTER }
291 int sc_sendad_errors;
292 #define CARP_SENDAD_MAX_ERRORS 3
293 int sc_sendad_success;
294 #define CARP_SENDAD_MIN_SUCCESS 3
298 int sc_naddrs; /* actually used IPv4 vha */
300 int sc_advbase; /* seconds */
305 #define CARP_HMAC_PAD 64
306 unsigned char sc_key[CARP_KEY_LEN];
307 unsigned char sc_pad[CARP_HMAC_PAD];
310 struct callout sc_ad_tmo; /* advertisement timeout */
311 struct netmsg_carp sc_ad_msg; /* adv timeout netmsg */
312 struct callout sc_md_tmo; /* ip4 master down timeout */
313 struct callout sc_md6_tmo; /* ip6 master down timeout */
314 struct netmsg_carp sc_md_msg; /* master down timeout netmsg */
316 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
319 #define sc_if arpcom.ac_if
321 struct carp_softc_container {
322 TAILQ_ENTRY(carp_softc_container) scc_link;
323 struct carp_softc *scc_softc;
325 TAILQ_HEAD(carp_if, carp_softc_container);
327 SYSCTL_DECL(_net_inet_carp);
329 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
330 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
331 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
332 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
333 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
334 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
335 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
336 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
337 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
339 static int carp_suppress_preempt = 0;
340 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
341 &carp_suppress_preempt, 0, "Preemption is suppressed");
343 static struct carpstats carpstats;
344 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
345 &carpstats, carpstats,
346 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
348 #define CARP_LOG(...) do { \
349 if (carp_opts[CARPCTL_LOG] > 0) \
350 log(LOG_INFO, __VA_ARGS__); \
353 #define CARP_DEBUG(...) do { \
354 if (carp_opts[CARPCTL_LOG] > 1) \
355 log(LOG_DEBUG, __VA_ARGS__); \
358 static struct lwkt_token carp_listtok = LWKT_TOKEN_INITIALIZER(carp_list_token);
360 static void carp_hmac_prepare(struct carp_softc *);
361 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
363 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
365 static void carp_setroute(struct carp_softc *, int);
366 static void carp_proto_input_c(struct carp_softc *, struct mbuf *,
367 struct carp_header *, sa_family_t);
368 static int carp_clone_create(struct if_clone *, int, caddr_t);
369 static int carp_clone_destroy(struct ifnet *);
370 static void carp_detach(struct carp_softc *, boolean_t, boolean_t);
371 static void carp_prepare_ad(struct carp_softc *, struct carp_header *);
372 static void carp_send_ad_all(void);
373 static void carp_send_ad_timeout(void *);
374 static void carp_send_ad(struct carp_softc *);
375 static void carp_send_arp(struct carp_softc *);
376 static void carp_master_down_timeout(void *);
377 static void carp_master_down(struct carp_softc *);
378 static void carp_setrun(struct carp_softc *, sa_family_t);
379 static void carp_set_state(struct carp_softc *, int);
380 static struct ifnet *carp_forus(struct carp_if *, const uint8_t *);
382 static void carp_init(void *);
383 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
384 static int carp_output(struct ifnet *, struct mbuf *, struct sockaddr *,
386 static void carp_start(struct ifnet *);
388 static void carp_multicast_cleanup(struct carp_softc *);
389 static void carp_add_addr(struct carp_softc *, struct ifaddr *);
390 static void carp_del_addr(struct carp_softc *, struct ifaddr *);
391 static void carp_config_addr(struct carp_softc *, struct ifaddr *);
392 static void carp_link_addrs(struct carp_softc *, struct ifnet *,
394 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *,
396 static void carp_update_addrs(struct carp_softc *, struct ifaddr *);
398 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *,
400 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
401 struct ifnet *, struct in_ifaddr *, int);
402 static void carp_deactivate_vhaddr(struct carp_softc *,
403 struct carp_vhaddr *, boolean_t);
404 static int carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *);
405 static void carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *,
409 static void carp_sc_state(struct carp_softc *);
412 static void carp_send_na(struct carp_softc *);
414 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
415 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
417 static void carp_multicast6_cleanup(struct carp_softc *);
419 static void carp_stop(struct carp_softc *, boolean_t);
420 static void carp_suspend(struct carp_softc *, boolean_t);
421 static void carp_ioctl_stop(struct carp_softc *);
422 static int carp_ioctl_setvh(struct carp_softc *, void *, struct ucred *);
423 static int carp_ioctl_getvh(struct carp_softc *, void *, struct ucred *);
424 static int carp_ioctl_getdevname(struct carp_softc *, struct ifdrv *);
425 static int carp_ioctl_getvhaddr(struct carp_softc *, struct ifdrv *);
427 static struct carp_if *carp_if_remove(struct carp_if *, struct carp_softc *);
428 static struct carp_if *carp_if_insert(struct carp_if *, struct carp_softc *);
429 static void carp_if_free(struct carp_if *);
431 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
433 static void carp_ifdetach(void *, struct ifnet *);
435 static void carp_ifdetach_dispatch(netmsg_t);
436 static void carp_clone_destroy_dispatch(netmsg_t);
437 static void carp_init_dispatch(netmsg_t);
438 static void carp_ioctl_stop_dispatch(netmsg_t);
439 static void carp_ioctl_setvh_dispatch(netmsg_t);
440 static void carp_ioctl_getvh_dispatch(netmsg_t);
441 static void carp_ioctl_getdevname_dispatch(netmsg_t);
442 static void carp_ioctl_getvhaddr_dispatch(netmsg_t);
443 static void carp_send_ad_timeout_dispatch(netmsg_t);
444 static void carp_master_down_timeout_dispatch(netmsg_t);
446 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
448 static LIST_HEAD(, carp_softc) carpif_list;
450 static struct if_clone carp_cloner =
451 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
454 static uint8_t carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 };
456 static eventhandler_tag carp_ifdetach_event;
457 static eventhandler_tag carp_ifaddr_event;
460 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
462 struct carp_vhaddr *vha;
463 u_long new_addr, addr;
465 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
468 * Virtual address list is sorted; smaller one first
470 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
472 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
473 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
479 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
481 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
482 vha_new->vha_flags |= CARP_VHAF_ONLIST;
486 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
488 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
489 vha->vha_flags &= ~CARP_VHAF_ONLIST;
490 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
494 carp_hmac_prepare(struct carp_softc *sc)
496 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
497 uint8_t vhid = sc->sc_vhid & 0xff;
500 struct ifaddr_container *ifac;
504 struct carp_vhaddr *vha;
507 /* XXX: possible race here */
509 /* compute ipad from key */
510 bzero(sc->sc_pad, sizeof(sc->sc_pad));
511 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
512 for (i = 0; i < sizeof(sc->sc_pad); i++)
513 sc->sc_pad[i] ^= 0x36;
515 /* precompute first part of inner hash */
516 SHA1Init(&sc->sc_sha1);
517 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
518 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
519 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
520 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
522 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
523 SHA1Update(&sc->sc_sha1,
524 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
525 sizeof(struct in_addr));
529 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
530 struct ifaddr *ifa = ifac->ifa;
532 if (ifa->ifa_addr->sa_family == AF_INET6) {
533 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
534 in6_clearscope(&in6);
535 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
540 /* convert ipad to opad */
541 for (i = 0; i < sizeof(sc->sc_pad); i++)
542 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
546 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
547 unsigned char md[20])
551 /* fetch first half of inner hash */
552 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
554 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
555 SHA1Final(md, &sha1ctx);
559 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
560 SHA1Update(&sha1ctx, md, 20);
561 SHA1Final(md, &sha1ctx);
565 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
566 unsigned char md[20])
568 unsigned char md2[20];
570 carp_hmac_generate(sc, counter, md2);
571 return (bcmp(md, md2, sizeof(md2)));
575 carp_setroute(struct carp_softc *sc, int cmd)
578 struct ifaddr_container *ifac;
580 struct carp_vhaddr *vha;
582 KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD);
584 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
585 if (vha->vha_iaback == NULL)
587 if (cmd == RTM_DELETE)
588 carp_delroute_vhaddr(sc, vha, FALSE);
590 carp_addroute_vhaddr(sc, vha);
594 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
595 struct ifaddr *ifa = ifac->ifa;
597 if (ifa->ifa_addr->sa_family == AF_INET6) {
608 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
610 struct carp_softc *sc;
613 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
617 sc->sc_advbase = CARP_DFLTINTV;
618 sc->sc_vhid = -1; /* required setting */
620 sc->sc_init_counter = 1;
624 TAILQ_INIT(&sc->sc_vha_list);
627 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
630 callout_init_mp(&sc->sc_ad_tmo);
631 netmsg_init(&sc->sc_ad_msg.base, NULL, &netisr_adone_rport,
632 MSGF_DROPABLE | MSGF_PRIORITY, carp_send_ad_timeout_dispatch);
633 sc->sc_ad_msg.nc_softc = sc;
635 callout_init_mp(&sc->sc_md_tmo);
636 callout_init_mp(&sc->sc_md6_tmo);
637 netmsg_init(&sc->sc_md_msg.base, NULL, &netisr_adone_rport,
638 MSGF_DROPABLE | MSGF_PRIORITY, carp_master_down_timeout_dispatch);
639 sc->sc_md_msg.nc_softc = sc;
641 if_initname(ifp, CARP_IFNAME, unit);
643 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
644 ifp->if_init = carp_init;
645 ifp->if_ioctl = carp_ioctl;
646 ifp->if_start = carp_start;
647 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
648 ifq_set_ready(&ifp->if_snd);
650 ether_ifattach(ifp, carp_etheraddr, NULL);
652 ifp->if_type = IFT_CARP;
653 ifp->if_output = carp_output;
655 lwkt_gettoken(&carp_listtok);
656 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
657 lwkt_reltoken(&carp_listtok);
663 carp_clone_destroy_dispatch(netmsg_t msg)
665 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
666 struct carp_softc *sc = cmsg->nc_softc;
669 carp_detach(sc, TRUE, FALSE);
671 callout_stop_sync(&sc->sc_ad_tmo);
672 callout_stop_sync(&sc->sc_md_tmo);
673 callout_stop_sync(&sc->sc_md6_tmo);
676 if ((sc->sc_ad_msg.base.lmsg.ms_flags & MSGF_DONE) == 0)
677 lwkt_dropmsg(&sc->sc_ad_msg.base.lmsg);
678 if ((sc->sc_md_msg.base.lmsg.ms_flags & MSGF_DONE) == 0)
679 lwkt_dropmsg(&sc->sc_md_msg.base.lmsg);
682 lwkt_replymsg(&cmsg->base.lmsg, 0);
686 carp_clone_destroy(struct ifnet *ifp)
688 struct carp_softc *sc = ifp->if_softc;
689 struct netmsg_carp cmsg;
691 bzero(&cmsg, sizeof(cmsg));
692 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
693 carp_clone_destroy_dispatch);
696 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
698 lwkt_gettoken(&carp_listtok);
699 LIST_REMOVE(sc, sc_next);
700 lwkt_reltoken(&carp_listtok);
705 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active\n"));
711 static struct carp_if *
712 carp_if_remove(struct carp_if *ocif, struct carp_softc *sc)
714 struct carp_softc_container *oscc, *scc;
721 TAILQ_FOREACH(oscc, ocif, scc_link) {
724 if (oscc->scc_softc == sc)
728 KASSERT(found, ("%s carp_softc is not on carp_if\n", __func__));
731 /* Last one is going to be unlinked */
735 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
738 TAILQ_FOREACH(oscc, ocif, scc_link) {
739 if (oscc->scc_softc == sc)
742 scc = kmalloc(sizeof(*scc), M_CARP, M_WAITOK | M_ZERO);
743 scc->scc_softc = oscc->scc_softc;
744 TAILQ_INSERT_TAIL(cif, scc, scc_link);
750 static struct carp_if *
751 carp_if_insert(struct carp_if *ocif, struct carp_softc *sc)
753 struct carp_softc_container *oscc;
758 TAILQ_FOREACH(oscc, ocif, scc_link) {
759 if (oscc->scc_softc == sc)
765 if (sc->sc_carpdev != NULL) {
766 KASSERT(onlist, ("%s is not on %s carp list\n",
767 sc->sc_if.if_xname, sc->sc_carpdev->if_xname));
769 KASSERT(!onlist, ("%s is already on carp list\n",
770 sc->sc_if.if_xname));
776 struct carp_softc_container *new_scc, *scc;
779 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
782 new_scc = kmalloc(sizeof(*new_scc), M_CARP, M_WAITOK | M_ZERO);
783 new_scc->scc_softc = sc;
786 TAILQ_FOREACH(oscc, ocif, scc_link) {
788 oscc->scc_softc->sc_vhid > sc->sc_vhid) {
789 TAILQ_INSERT_TAIL(cif, new_scc,
794 scc = kmalloc(sizeof(*scc), M_CARP,
796 scc->scc_softc = oscc->scc_softc;
797 TAILQ_INSERT_TAIL(cif, scc, scc_link);
801 TAILQ_INSERT_TAIL(cif, new_scc, scc_link);
810 carp_if_free(struct carp_if *cif)
812 struct carp_softc_container *scc;
814 while ((scc = TAILQ_FIRST(cif)) != NULL) {
815 TAILQ_REMOVE(cif, scc, scc_link);
822 carp_detach(struct carp_softc *sc, boolean_t detach, boolean_t del_iaback)
824 carp_suspend(sc, detach);
826 carp_multicast_cleanup(sc);
828 carp_multicast6_cleanup(sc);
831 if (!sc->sc_dead && detach) {
832 struct carp_vhaddr *vha;
834 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
835 carp_deactivate_vhaddr(sc, vha, del_iaback);
836 KKASSERT(sc->sc_naddrs == 0);
839 if (sc->sc_carpdev != NULL) {
840 struct ifnet *ifp = sc->sc_carpdev;
841 struct carp_if *ocif = ifp->if_carp;
843 ifp->if_carp = carp_if_remove(ocif, sc);
844 KASSERT(ifp->if_carp != ocif,
845 ("%s carp_if_remove failed\n", __func__));
847 sc->sc_carpdev = NULL;
851 * Make sure that all protocol threads see the
852 * sc_carpdev and if_carp changes
854 netmsg_service_sync();
856 if (ifp->if_carp == NULL) {
858 * No more carp interfaces using
859 * ifp as the backing interface,
860 * move it out of promiscous mode.
866 * The old carp list could be safely free now,
867 * since no one can access it.
874 carp_ifdetach_dispatch(netmsg_t msg)
876 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
877 struct ifnet *ifp = cmsg->nc_carpdev;
879 while (ifp->if_carp) {
880 struct carp_softc_container *scc;
882 scc = TAILQ_FIRST((struct carp_if *)(ifp->if_carp));
883 carp_detach(scc->scc_softc, TRUE, TRUE);
885 lwkt_replymsg(&cmsg->base.lmsg, 0);
888 /* Detach an interface from the carp. */
890 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
892 struct netmsg_carp cmsg;
894 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
896 bzero(&cmsg, sizeof(cmsg));
897 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
898 carp_ifdetach_dispatch);
899 cmsg.nc_carpdev = ifp;
901 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
905 * process input packet.
906 * we have rearranged checks order compared to the rfc,
907 * but it seems more efficient this way or not possible otherwise.
910 carp_proto_input(struct mbuf **mp, int *offp, int proto)
912 struct mbuf *m = *mp;
913 struct ip *ip = mtod(m, struct ip *);
914 struct ifnet *ifp = m->m_pkthdr.rcvif;
915 struct carp_header *ch;
916 struct carp_softc *sc;
922 carpstats.carps_ipackets++;
924 if (!carp_opts[CARPCTL_ALLOW]) {
929 /* Check if received on a valid carp interface */
930 if (ifp->if_type != IFT_CARP) {
931 carpstats.carps_badif++;
932 CARP_LOG("carp_proto_input: packet received on non-carp "
933 "interface: %s\n", ifp->if_xname);
938 if (!CARP_IS_RUNNING(ifp)) {
939 carpstats.carps_badif++;
940 CARP_LOG("carp_proto_input: packet received on stopped carp "
941 "interface: %s\n", ifp->if_xname);
947 if (sc->sc_carpdev == NULL) {
948 carpstats.carps_badif++;
949 CARP_LOG("carp_proto_input: packet received on defunc carp "
950 "interface: %s\n", ifp->if_xname);
955 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
956 carpstats.carps_badif++;
957 CARP_LOG("carp_proto_input: non-mcast packet on "
958 "interface: %s\n", ifp->if_xname);
963 /* Verify that the IP TTL is CARP_DFLTTL. */
964 if (ip->ip_ttl != CARP_DFLTTL) {
965 carpstats.carps_badttl++;
966 CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n",
967 ip->ip_ttl, CARP_DFLTTL, ifp->if_xname);
972 /* Minimal CARP packet size */
973 len = iphlen + sizeof(*ch);
976 * Verify that the received packet length is
977 * not less than the CARP header
979 if (m->m_pkthdr.len < len) {
980 carpstats.carps_badlen++;
981 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
987 /* Make sure that CARP header is contiguous */
988 if (len > m->m_len) {
989 m = m_pullup(m, len);
991 carpstats.carps_hdrops++;
992 CARP_LOG("carp_proto_input: m_pullup failed\n");
995 ip = mtod(m, struct ip *);
997 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
999 /* Verify the CARP checksum */
1000 if (in_cksum_skip(m, len, iphlen)) {
1001 carpstats.carps_badsum++;
1002 CARP_LOG("carp_proto_input: checksum failed on %s\n",
1007 carp_proto_input_c(sc, m, ch, AF_INET);
1009 return(IPPROTO_DONE);
1014 carp6_proto_input(struct mbuf **mp, int *offp, int proto)
1016 struct mbuf *m = *mp;
1017 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1018 struct ifnet *ifp = m->m_pkthdr.rcvif;
1019 struct carp_header *ch;
1020 struct carp_softc *sc;
1023 carpstats.carps_ipackets6++;
1025 if (!carp_opts[CARPCTL_ALLOW]) {
1030 /* check if received on a valid carp interface */
1031 if (ifp->if_type != IFT_CARP) {
1032 carpstats.carps_badif++;
1033 CARP_LOG("carp6_proto_input: packet received on non-carp "
1034 "interface: %s\n", ifp->if_xname);
1039 if (!CARP_IS_RUNNING(ifp)) {
1040 carpstats.carps_badif++;
1041 CARP_LOG("carp_proto_input: packet received on stopped carp "
1042 "interface: %s\n", ifp->if_xname);
1048 if (sc->sc_carpdev == NULL) {
1049 carpstats.carps_badif++;
1050 CARP_LOG("carp6_proto_input: packet received on defunc-carp "
1051 "interface: %s\n", ifp->if_xname);
1056 /* verify that the IP TTL is 255 */
1057 if (ip6->ip6_hlim != CARP_DFLTTL) {
1058 carpstats.carps_badttl++;
1059 CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n",
1060 ip6->ip6_hlim, ifp->if_xname);
1065 /* verify that we have a complete carp packet */
1067 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
1069 carpstats.carps_badlen++;
1070 CARP_LOG("carp6_proto_input: packet size %u too small\n", len);
1074 /* verify the CARP checksum */
1075 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
1076 carpstats.carps_badsum++;
1077 CARP_LOG("carp6_proto_input: checksum failed, on %s\n",
1083 carp_proto_input_c(sc, m, ch, AF_INET6);
1085 return (IPPROTO_DONE);
1090 carp_proto_input_c(struct carp_softc *sc, struct mbuf *m,
1091 struct carp_header *ch, sa_family_t af)
1094 uint64_t tmp_counter;
1095 struct timeval sc_tv, ch_tv;
1097 if (sc->sc_vhid != ch->carp_vhid) {
1099 * CARP uses multicast, however, multicast packets
1100 * are tapped to all CARP interfaces on the physical
1101 * interface receiving the CARP packets, so we don't
1102 * update any stats here.
1109 /* verify the CARP version. */
1110 if (ch->carp_version != CARP_VERSION) {
1111 carpstats.carps_badver++;
1112 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
1118 /* verify the hash */
1119 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
1120 carpstats.carps_badauth++;
1121 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
1126 tmp_counter = ntohl(ch->carp_counter[0]);
1127 tmp_counter = tmp_counter<<32;
1128 tmp_counter += ntohl(ch->carp_counter[1]);
1130 /* XXX Replay protection goes here */
1132 sc->sc_init_counter = 0;
1133 sc->sc_counter = tmp_counter;
1135 sc_tv.tv_sec = sc->sc_advbase;
1136 if (carp_suppress_preempt && sc->sc_advskew < 240)
1137 sc_tv.tv_usec = 240 * 1000000 / 256;
1139 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1140 ch_tv.tv_sec = ch->carp_advbase;
1141 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
1143 switch (sc->sc_state) {
1149 * If we receive an advertisement from a master who's going to
1150 * be more frequent than us, go into BACKUP state.
1152 if (timevalcmp(&sc_tv, &ch_tv, >) ||
1153 timevalcmp(&sc_tv, &ch_tv, ==)) {
1154 callout_stop(&sc->sc_ad_tmo);
1155 CARP_DEBUG("%s: MASTER -> BACKUP "
1156 "(more frequent advertisement received)\n",
1158 carp_set_state(sc, BACKUP);
1160 carp_setroute(sc, RTM_DELETE);
1166 * If we're pre-empting masters who advertise slower than us,
1167 * and this one claims to be slower, treat him as down.
1169 if (carp_opts[CARPCTL_PREEMPT] &&
1170 timevalcmp(&sc_tv, &ch_tv, <)) {
1171 CARP_DEBUG("%s: BACKUP -> MASTER "
1172 "(preempting a slower master)\n", cifp->if_xname);
1173 carp_master_down(sc);
1178 * If the master is going to advertise at such a low frequency
1179 * that he's guaranteed to time out, we'd might as well just
1180 * treat him as timed out now.
1182 sc_tv.tv_sec = sc->sc_advbase * 3;
1183 if (timevalcmp(&sc_tv, &ch_tv, <)) {
1184 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1186 carp_master_down(sc);
1191 * Otherwise, we reset the counter and wait for the next
1194 carp_setrun(sc, af);
1201 carp_input(void *v, struct mbuf *m)
1203 struct carp_if *cif = v;
1204 struct ether_header *eh;
1205 struct carp_softc_container *scc;
1208 eh = mtod(m, struct ether_header *);
1210 ifp = carp_forus(cif, eh->ether_dhost);
1212 ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF);
1216 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
1220 * XXX Should really check the list of multicast addresses
1221 * for each CARP interface _before_ copying.
1223 TAILQ_FOREACH(scc, cif, scc_link) {
1224 struct carp_softc *sc = scc->scc_softc;
1227 if ((sc->sc_if.if_flags & IFF_UP) == 0)
1230 m0 = m_dup(m, MB_DONTWAIT);
1234 ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF);
1240 carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch)
1242 if (sc->sc_init_counter) {
1243 /* this could also be seconds since unix epoch */
1244 sc->sc_counter = karc4random();
1245 sc->sc_counter = sc->sc_counter << 32;
1246 sc->sc_counter += karc4random();
1251 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
1252 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
1254 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
1258 carp_send_ad_all(void)
1260 struct carp_softc *sc;
1262 LIST_FOREACH(sc, &carpif_list, sc_next) {
1263 if (sc->sc_carpdev == NULL)
1266 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
1272 carp_send_ad_timeout(void *xsc)
1274 struct carp_softc *sc = xsc;
1275 struct netmsg_carp *cmsg = &sc->sc_ad_msg;
1277 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d\n",
1278 __func__, mycpuid));
1281 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1282 lwkt_sendmsg(cpu_portfn(0), &cmsg->base.lmsg);
1287 carp_send_ad_timeout_dispatch(netmsg_t msg)
1289 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1290 struct carp_softc *sc = cmsg->nc_softc;
1294 lwkt_replymsg(&cmsg->base.lmsg, 0);
1301 carp_send_ad(struct carp_softc *sc)
1303 struct ifnet *cifp = &sc->sc_if;
1304 struct carp_header ch;
1306 struct carp_header *ch_ptr;
1308 int len, advbase, advskew;
1310 if (!CARP_IS_RUNNING(cifp)) {
1315 advbase = sc->sc_advbase;
1316 if (!carp_suppress_preempt || sc->sc_advskew > 240)
1317 advskew = sc->sc_advskew;
1320 tv.tv_sec = advbase;
1321 tv.tv_usec = advskew * 1000000 / 256;
1324 ch.carp_version = CARP_VERSION;
1325 ch.carp_type = CARP_ADVERTISEMENT;
1326 ch.carp_vhid = sc->sc_vhid;
1327 ch.carp_advbase = advbase;
1328 ch.carp_advskew = advskew;
1329 ch.carp_authlen = 7; /* XXX DEFINE */
1330 ch.carp_pad1 = 0; /* must be zero */
1334 if (sc->sc_ia != NULL) {
1337 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1340 carpstats.carps_onomem++;
1341 /* XXX maybe less ? */
1342 if (advbase != 255 || advskew != 255)
1343 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1344 carp_send_ad_timeout, sc);
1347 len = sizeof(*ip) + sizeof(ch);
1348 m->m_pkthdr.len = len;
1349 m->m_pkthdr.rcvif = NULL;
1351 MH_ALIGN(m, m->m_len);
1352 m->m_flags |= M_MCAST;
1353 ip = mtod(m, struct ip *);
1354 ip->ip_v = IPVERSION;
1355 ip->ip_hl = sizeof(*ip) >> 2;
1356 ip->ip_tos = IPTOS_LOWDELAY;
1358 ip->ip_id = ip_newid();
1360 ip->ip_ttl = CARP_DFLTTL;
1361 ip->ip_p = IPPROTO_CARP;
1363 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
1364 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
1366 ch_ptr = (struct carp_header *)(&ip[1]);
1367 bcopy(&ch, ch_ptr, sizeof(ch));
1368 carp_prepare_ad(sc, ch_ptr);
1369 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
1371 getmicrotime(&cifp->if_lastchange);
1372 cifp->if_opackets++;
1373 cifp->if_obytes += len;
1374 carpstats.carps_opackets++;
1376 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
1378 if (sc->sc_sendad_errors < INT_MAX)
1379 sc->sc_sendad_errors++;
1380 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1381 carp_suppress_preempt++;
1382 if (carp_suppress_preempt == 1) {
1386 sc->sc_sendad_success = 0;
1388 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1389 if (++sc->sc_sendad_success >=
1390 CARP_SENDAD_MIN_SUCCESS) {
1391 carp_suppress_preempt--;
1392 sc->sc_sendad_errors = 0;
1395 sc->sc_sendad_errors = 0;
1402 struct ip6_hdr *ip6;
1404 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1407 carpstats.carps_onomem++;
1408 /* XXX maybe less ? */
1409 if (advbase != 255 || advskew != 255)
1410 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1411 carp_send_ad_timeout, sc);
1414 len = sizeof(*ip6) + sizeof(ch);
1415 m->m_pkthdr.len = len;
1416 m->m_pkthdr.rcvif = NULL;
1418 MH_ALIGN(m, m->m_len);
1419 m->m_flags |= M_MCAST;
1420 ip6 = mtod(m, struct ip6_hdr *);
1421 bzero(ip6, sizeof(*ip6));
1422 ip6->ip6_vfc |= IPV6_VERSION;
1423 ip6->ip6_hlim = CARP_DFLTTL;
1424 ip6->ip6_nxt = IPPROTO_CARP;
1425 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1426 sizeof(struct in6_addr));
1427 /* set the multicast destination */
1429 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1430 ip6->ip6_dst.s6_addr8[15] = 0x12;
1431 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1434 CARP_LOG("%s: in6_setscope failed\n", __func__);
1438 ch_ptr = (struct carp_header *)(&ip6[1]);
1439 bcopy(&ch, ch_ptr, sizeof(ch));
1440 carp_prepare_ad(sc, ch_ptr);
1441 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
1443 getmicrotime(&cifp->if_lastchange);
1444 cifp->if_opackets++;
1445 cifp->if_obytes += len;
1446 carpstats.carps_opackets6++;
1448 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1450 if (sc->sc_sendad_errors < INT_MAX)
1451 sc->sc_sendad_errors++;
1452 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1453 carp_suppress_preempt++;
1454 if (carp_suppress_preempt == 1) {
1458 sc->sc_sendad_success = 0;
1460 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1461 if (++sc->sc_sendad_success >=
1462 CARP_SENDAD_MIN_SUCCESS) {
1463 carp_suppress_preempt--;
1464 sc->sc_sendad_errors = 0;
1467 sc->sc_sendad_errors = 0;
1473 if (advbase != 255 || advskew != 255)
1474 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1475 carp_send_ad_timeout, sc);
1479 * Broadcast a gratuitous ARP request containing
1480 * the virtual router MAC address for each IP address
1481 * associated with the virtual router.
1484 carp_send_arp(struct carp_softc *sc)
1486 const struct carp_vhaddr *vha;
1488 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1489 if (vha->vha_iaback == NULL)
1491 arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa);
1497 carp_send_na(struct carp_softc *sc)
1499 struct ifaddr_container *ifac;
1500 struct in6_addr *in6;
1501 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1503 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1504 struct ifaddr *ifa = ifac->ifa;
1506 if (ifa->ifa_addr->sa_family != AF_INET6)
1509 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1510 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1511 ND_NA_FLAG_OVERRIDE, 1, NULL);
1512 DELAY(1000); /* XXX */
1517 static __inline const struct carp_vhaddr *
1518 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1520 struct carp_vhaddr *vha;
1522 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1523 if (vha->vha_iaback == NULL)
1526 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1534 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1535 const struct in_addr *isaddr, uint8_t **enaddr)
1537 const struct carp_softc *vh;
1538 int index, count = 0;
1541 * XXX proof of concept implementation.
1542 * We use the source ip to decide which virtual host should
1543 * handle the request. If we're master of that virtual host,
1544 * then we respond, otherwise, just drop the arp packet on
1548 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1549 if (!CARP_IS_RUNNING(&vh->sc_if))
1552 if (carp_find_addr(vh, itaddr) != NULL)
1558 /* this should be a hash, like pf_hash() */
1559 index = ntohl(isaddr->s_addr) % count;
1562 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1563 if (!CARP_IS_RUNNING(&vh->sc_if))
1566 if (carp_find_addr(vh, itaddr) == NULL)
1569 if (count == index) {
1570 if (vh->sc_state == MASTER) {
1571 *enaddr = IF_LLADDR(&vh->sc_if);
1584 carp_iamatch(const struct in_ifaddr *ia)
1586 const struct carp_softc *sc = ia->ia_ifp->if_softc;
1588 KASSERT(&curthread->td_msgport == cpu_portfn(0),
1589 ("not in netisr0"));
1592 if (carp_opts[CARPCTL_ARPBALANCE])
1593 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1596 if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER)
1604 carp_iamatch6(void *v, struct in6_addr *taddr)
1607 struct carp_if *cif = v;
1608 struct carp_softc *vh;
1610 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1611 struct ifaddr_container *ifac;
1613 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1615 struct ifaddr *ifa = ifac->ifa;
1617 if (IN6_ARE_ADDR_EQUAL(taddr,
1618 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1619 CARP_IS_RUNNING(&vh->sc_if) &&
1620 vh->sc_state == MASTER) {
1630 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1634 struct carp_if *cif = v;
1635 struct carp_softc *sc;
1637 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1638 struct ifaddr_container *ifac;
1640 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1642 struct ifaddr *ifa = ifac->ifa;
1644 if (IN6_ARE_ADDR_EQUAL(taddr,
1645 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1646 CARP_IS_RUNNING(&sc->sc_if)) {
1647 struct ifnet *ifp = &sc->sc_if;
1649 mtag = m_tag_get(PACKET_TAG_CARP,
1650 sizeof(struct ifnet *), MB_DONTWAIT);
1652 /* better a bit than nothing */
1653 return (IF_LLADDR(ifp));
1655 bcopy(&ifp, (caddr_t)(mtag + 1),
1656 sizeof(struct ifnet *));
1657 m_tag_prepend(m, mtag);
1659 return (IF_LLADDR(ifp));
1668 static struct ifnet *
1669 carp_forus(struct carp_if *cif, const uint8_t *dhost)
1671 struct carp_softc_container *scc;
1673 if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0)
1676 TAILQ_FOREACH(scc, cif, scc_link) {
1677 struct carp_softc *sc = scc->scc_softc;
1678 struct ifnet *ifp = &sc->sc_if;
1680 if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER &&
1681 !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN))
1688 carp_master_down_timeout(void *xsc)
1690 struct carp_softc *sc = xsc;
1691 struct netmsg_carp *cmsg = &sc->sc_md_msg;
1693 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d\n",
1694 __func__, mycpuid));
1697 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1698 lwkt_sendmsg(cpu_portfn(0), &cmsg->base.lmsg);
1703 carp_master_down_timeout_dispatch(netmsg_t msg)
1705 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1706 struct carp_softc *sc = cmsg->nc_softc;
1710 lwkt_replymsg(&cmsg->base.lmsg, 0);
1713 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1714 sc->sc_if.if_xname);
1715 carp_master_down(sc);
1719 carp_master_down(struct carp_softc *sc)
1721 switch (sc->sc_state) {
1723 kprintf("%s: master_down event in INIT state\n",
1724 sc->sc_if.if_xname);
1731 carp_set_state(sc, MASTER);
1738 carp_setroute(sc, RTM_ADD);
1744 * When in backup state, af indicates whether to reset the master down timer
1745 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1748 carp_setrun(struct carp_softc *sc, sa_family_t af)
1750 struct ifnet *cifp = &sc->sc_if;
1753 if (sc->sc_carpdev == NULL) {
1754 carp_set_state(sc, INIT);
1758 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1759 (sc->sc_naddrs || sc->sc_naddrs6)) {
1762 carp_setroute(sc, RTM_DELETE);
1766 switch (sc->sc_state) {
1768 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1774 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1776 carp_set_state(sc, MASTER);
1777 carp_setroute(sc, RTM_ADD);
1779 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1780 carp_set_state(sc, BACKUP);
1781 carp_setroute(sc, RTM_DELETE);
1787 callout_stop(&sc->sc_ad_tmo);
1788 tv.tv_sec = 3 * sc->sc_advbase;
1789 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1793 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1794 carp_master_down_timeout, sc);
1799 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1800 carp_master_down_timeout, sc);
1805 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1806 carp_master_down_timeout, sc);
1808 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1809 carp_master_down_timeout, sc);
1815 tv.tv_sec = sc->sc_advbase;
1816 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1817 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1818 carp_send_ad_timeout, sc);
1824 carp_multicast_cleanup(struct carp_softc *sc)
1826 struct ip_moptions *imo = &sc->sc_imo;
1828 if (imo->imo_num_memberships == 0)
1830 KKASSERT(imo->imo_num_memberships == 1);
1832 in_delmulti(imo->imo_membership[0]);
1833 imo->imo_membership[0] = NULL;
1834 imo->imo_num_memberships = 0;
1835 imo->imo_multicast_ifp = NULL;
1840 carp_multicast6_cleanup(struct carp_softc *sc)
1842 struct ip6_moptions *im6o = &sc->sc_im6o;
1844 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1845 struct in6_multi_mship *imm =
1846 LIST_FIRST(&im6o->im6o_memberships);
1848 LIST_REMOVE(imm, i6mm_chain);
1849 in6_leavegroup(imm);
1851 im6o->im6o_multicast_ifp = NULL;
1856 carp_ioctl_getvhaddr_dispatch(netmsg_t msg)
1858 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1859 struct carp_softc *sc = cmsg->nc_softc;
1860 const struct carp_vhaddr *vha;
1861 struct ifcarpvhaddr *carpa, *carpa0;
1862 int count, len, error = 0;
1865 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1868 if (cmsg->nc_datalen == 0) {
1869 cmsg->nc_datalen = count * sizeof(*carpa);
1871 } else if (count == 0 || cmsg->nc_datalen < sizeof(*carpa)) {
1872 cmsg->nc_datalen = 0;
1875 len = min(cmsg->nc_datalen, sizeof(*carpa) * count);
1876 KKASSERT(len >= sizeof(*carpa));
1878 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1879 if (carpa == NULL) {
1885 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1886 if (len < sizeof(*carpa))
1889 carpa->carpa_flags = vha->vha_flags;
1890 carpa->carpa_addr.sin_family = AF_INET;
1891 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1893 carpa->carpa_baddr.sin_family = AF_INET;
1894 if (vha->vha_iaback == NULL) {
1895 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1897 carpa->carpa_baddr.sin_addr =
1898 vha->vha_iaback->ia_addr.sin_addr;
1903 len -= sizeof(*carpa);
1905 cmsg->nc_datalen = sizeof(*carpa) * count;
1906 KKASSERT(cmsg->nc_datalen > 0);
1908 cmsg->nc_data = carpa0;
1911 lwkt_replymsg(&cmsg->base.lmsg, error);
1915 carp_ioctl_getvhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1917 struct ifnet *ifp = &sc->arpcom.ac_if;
1918 struct netmsg_carp cmsg;
1921 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1922 ifnet_deserialize_all(ifp);
1924 bzero(&cmsg, sizeof(cmsg));
1925 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
1926 carp_ioctl_getvhaddr_dispatch);
1928 cmsg.nc_datalen = ifd->ifd_len;
1930 error = lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
1933 if (cmsg.nc_data != NULL) {
1934 error = copyout(cmsg.nc_data, ifd->ifd_data,
1936 kfree(cmsg.nc_data, M_TEMP);
1938 ifd->ifd_len = cmsg.nc_datalen;
1940 KASSERT(cmsg.nc_data == NULL,
1941 ("%s temp vhaddr is alloc upon error\n", __func__));
1944 ifnet_serialize_all(ifp);
1949 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
1950 struct in_ifaddr *ia_del)
1953 struct in_ifaddr *ia_if;
1954 struct in_ifaddr_container *iac;
1955 const struct sockaddr_in *sin;
1959 KKASSERT(vha->vha_ia != NULL);
1961 sin = &vha->vha_ia->ia_addr;
1962 iaddr = ntohl(sin->sin_addr.s_addr);
1966 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1967 struct in_ifaddr *ia = iac->ia;
1972 if (ia->ia_ifp->if_type == IFT_CARP)
1975 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1978 /* and, yeah, we need a multicast-capable iface too */
1979 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1982 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1983 if (sin->sin_addr.s_addr ==
1984 ia->ia_addr.sin_addr.s_addr)
1988 else if (sc->sc_carpdev != NULL &&
1989 sc->sc_carpdev == ia->ia_ifp)
1994 carp_deactivate_vhaddr(sc, vha, FALSE);
1998 ifp = ia_if->ia_ifp;
2000 /* XXX Don't allow parent iface to be changed */
2001 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
2004 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2008 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2010 struct carp_vhaddr *vha_new;
2011 struct in_ifaddr *carp_ia;
2013 struct carp_vhaddr *vha;
2016 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2017 carp_ia = ifatoia(carp_ifa);
2020 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2021 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
2024 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
2025 vha_new->vha_ia = carp_ia;
2026 carp_insert_vhaddr(sc, vha_new);
2028 if (carp_config_vhaddr(sc, vha_new, NULL) != 0) {
2030 * If the above configuration fails, it may only mean
2031 * that the new address is problematic. However, the
2032 * carp(4) interface may already have several working
2033 * addresses. Since the expected behaviour of
2034 * SIOC[AS]IFADDR is to put the NIC into working state,
2035 * we try starting the state machine manually here with
2036 * the hope that the carp(4)'s previously working
2037 * addresses still could be brought up.
2039 carp_hmac_prepare(sc);
2040 carp_set_state(sc, INIT);
2046 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2048 struct carp_vhaddr *vha;
2049 struct in_ifaddr *carp_ia;
2051 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2052 carp_ia = ifatoia(carp_ifa);
2054 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2055 KKASSERT(vha->vha_ia != NULL);
2056 if (vha->vha_ia == carp_ia)
2059 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
2062 * Remove the vhaddr from the list before deactivating
2063 * the vhaddr, so that the HMAC could be correctly
2064 * updated in carp_deactivate_vhaddr()
2066 carp_remove_vhaddr(sc, vha);
2068 carp_deactivate_vhaddr(sc, vha, FALSE);
2073 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2075 struct carp_vhaddr *vha;
2076 struct in_ifaddr *carp_ia;
2078 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2079 carp_ia = ifatoia(carp_ifa);
2081 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2082 KKASSERT(vha->vha_ia != NULL);
2083 if (vha->vha_ia == carp_ia)
2086 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
2088 /* Remove then reinsert, to keep the vhaddr list sorted */
2089 carp_remove_vhaddr(sc, vha);
2090 carp_insert_vhaddr(sc, vha);
2092 if (carp_config_vhaddr(sc, vha, NULL) != 0) {
2093 /* See the comment in carp_add_addr() */
2094 carp_hmac_prepare(sc);
2095 carp_set_state(sc, INIT);
2104 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
2107 struct carp_if *cif;
2108 struct in6_ifaddr *ia, *ia_if;
2109 struct ip6_moptions *im6o = &sc->sc_im6o;
2110 struct in6_multi_mship *imm;
2111 struct in6_addr in6;
2114 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2119 /* we have to do it by hands to check we won't match on us */
2120 ia_if = NULL; own = 0;
2121 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
2124 for (i = 0; i < 4; i++) {
2125 if ((sin6->sin6_addr.s6_addr32[i] &
2126 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
2127 (ia->ia_addr.sin6_addr.s6_addr32[i] &
2128 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
2131 /* and, yeah, we need a multicast-capable iface too */
2132 if (ia->ia_ifp != &sc->sc_if &&
2133 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
2137 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2138 &ia->ia_addr.sin6_addr))
2144 return (EADDRNOTAVAIL);
2148 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
2149 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
2150 return (EADDRNOTAVAIL);
2152 if (!sc->sc_naddrs6) {
2153 im6o->im6o_multicast_ifp = ifp;
2155 /* join CARP multicast address */
2156 bzero(&in6, sizeof(in6));
2157 in6.s6_addr16[0] = htons(0xff02);
2158 in6.s6_addr8[15] = 0x12;
2159 if (in6_setscope(&in6, ifp, NULL) != 0)
2161 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2163 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2165 /* join solicited multicast address */
2166 bzero(&in6, sizeof(in6));
2167 in6.s6_addr16[0] = htons(0xff02);
2168 in6.s6_addr32[1] = 0;
2169 in6.s6_addr32[2] = htonl(1);
2170 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
2171 in6.s6_addr8[12] = 0xff;
2172 if (in6_setscope(&in6, ifp, NULL) != 0)
2174 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2176 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2180 if (!ifp->if_carp) {
2181 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2183 if ((error = ifpromisc(ifp, 1))) {
2188 TAILQ_INIT(&cif->vhif_vrs);
2191 struct carp_softc *vr;
2194 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2195 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
2203 sc->sc_carpdev = ifp;
2206 { /* XXX prevent endless loop if already in queue */
2207 struct carp_softc *vr, *after = NULL;
2211 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2214 if (vr->sc_vhid < sc->sc_vhid)
2219 /* We're trying to keep things in order */
2221 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2223 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2237 /* clean up multicast memberships */
2238 if (!sc->sc_naddrs6) {
2239 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2240 imm = LIST_FIRST(&im6o->im6o_memberships);
2241 LIST_REMOVE(imm, i6mm_chain);
2242 in6_leavegroup(imm);
2249 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
2253 if (!--sc->sc_naddrs6) {
2254 struct carp_if *cif = sc->sc_carpdev->if_carp;
2255 struct ip6_moptions *im6o = &sc->sc_im6o;
2257 callout_stop(&sc->sc_ad_tmo);
2259 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2260 struct in6_multi_mship *imm =
2261 LIST_FIRST(&im6o->im6o_memberships);
2263 LIST_REMOVE(imm, i6mm_chain);
2264 in6_leavegroup(imm);
2266 im6o->im6o_multicast_ifp = NULL;
2268 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
2269 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
2270 sc->sc_carpdev->if_carp = NULL;
2271 kfree(cif, M_IFADDR);
2282 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
2284 struct carp_softc *sc = ifp->if_softc;
2285 struct ifreq *ifr = (struct ifreq *)addr;
2286 struct ifdrv *ifd = (struct ifdrv *)addr;
2289 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2293 if (ifp->if_flags & IFF_UP) {
2294 if ((ifp->if_flags & IFF_RUNNING) == 0)
2296 } else if (ifp->if_flags & IFF_RUNNING) {
2297 carp_ioctl_stop(sc);
2302 error = carp_ioctl_setvh(sc, ifr->ifr_data, cr);
2306 error = carp_ioctl_getvh(sc, ifr->ifr_data, cr);
2310 switch (ifd->ifd_cmd) {
2312 error = carp_ioctl_getdevname(sc, ifd);
2316 error = carp_ioctl_getvhaddr(sc, ifd);
2326 error = ether_ioctl(ifp, cmd, addr);
2334 carp_ioctl_stop_dispatch(netmsg_t msg)
2336 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2337 struct carp_softc *sc = cmsg->nc_softc;
2339 carp_stop(sc, FALSE);
2340 lwkt_replymsg(&cmsg->base.lmsg, 0);
2344 carp_ioctl_stop(struct carp_softc *sc)
2346 struct ifnet *ifp = &sc->arpcom.ac_if;
2347 struct netmsg_carp cmsg;
2349 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2351 ifnet_deserialize_all(ifp);
2353 bzero(&cmsg, sizeof(cmsg));
2354 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2355 carp_ioctl_stop_dispatch);
2358 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2360 ifnet_serialize_all(ifp);
2364 carp_ioctl_setvh_dispatch(netmsg_t msg)
2366 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2367 struct carp_softc *sc = cmsg->nc_softc;
2368 struct ifnet *ifp = &sc->arpcom.ac_if;
2369 const struct carpreq *carpr = cmsg->nc_data;
2373 if ((ifp->if_flags & IFF_RUNNING) &&
2374 sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) {
2375 switch (carpr->carpr_state) {
2377 callout_stop(&sc->sc_ad_tmo);
2378 carp_set_state(sc, BACKUP);
2380 carp_setroute(sc, RTM_DELETE);
2384 carp_master_down(sc);
2391 if (carpr->carpr_vhid > 0) {
2392 if (carpr->carpr_vhid > 255) {
2396 if (sc->sc_carpdev) {
2397 struct carp_if *cif = sc->sc_carpdev->if_carp;
2398 struct carp_softc_container *scc;
2400 TAILQ_FOREACH(scc, cif, scc_link) {
2401 struct carp_softc *vr = scc->scc_softc;
2404 vr->sc_vhid == carpr->carpr_vhid) {
2410 sc->sc_vhid = carpr->carpr_vhid;
2412 IF_LLADDR(ifp)[5] = sc->sc_vhid;
2413 bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr,
2418 if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) {
2419 if (carpr->carpr_advskew >= 255) {
2423 if (carpr->carpr_advbase > 255) {
2427 sc->sc_advbase = carpr->carpr_advbase;
2428 sc->sc_advskew = carpr->carpr_advskew;
2431 bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key));
2439 carp_hmac_prepare(sc);
2441 lwkt_replymsg(&cmsg->base.lmsg, error);
2445 carp_ioctl_setvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2447 struct ifnet *ifp = &sc->arpcom.ac_if;
2448 struct netmsg_carp cmsg;
2449 struct carpreq carpr;
2452 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2453 ifnet_deserialize_all(ifp);
2455 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2459 error = copyin(udata, &carpr, sizeof(carpr));
2463 bzero(&cmsg, sizeof(cmsg));
2464 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2465 carp_ioctl_setvh_dispatch);
2467 cmsg.nc_data = &carpr;
2469 error = lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2472 ifnet_serialize_all(ifp);
2477 carp_ioctl_getvh_dispatch(netmsg_t msg)
2479 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2480 struct carp_softc *sc = cmsg->nc_softc;
2481 struct carpreq *carpr = cmsg->nc_data;
2483 carpr->carpr_state = sc->sc_state;
2484 carpr->carpr_vhid = sc->sc_vhid;
2485 carpr->carpr_advbase = sc->sc_advbase;
2486 carpr->carpr_advskew = sc->sc_advskew;
2487 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
2489 lwkt_replymsg(&cmsg->base.lmsg, 0);
2493 carp_ioctl_getvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2495 struct ifnet *ifp = &sc->arpcom.ac_if;
2496 struct netmsg_carp cmsg;
2497 struct carpreq carpr;
2500 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2501 ifnet_deserialize_all(ifp);
2503 bzero(&cmsg, sizeof(cmsg));
2504 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2505 carp_ioctl_getvh_dispatch);
2507 cmsg.nc_data = &carpr;
2509 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2511 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2513 bzero(carpr.carpr_key, sizeof(carpr.carpr_key));
2515 error = copyout(&carpr, udata, sizeof(carpr));
2517 ifnet_serialize_all(ifp);
2522 carp_ioctl_getdevname_dispatch(netmsg_t msg)
2524 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2525 struct carp_softc *sc = cmsg->nc_softc;
2526 char *devname = cmsg->nc_data;
2528 bzero(devname, sizeof(devname));
2529 if (sc->sc_carpdev != NULL)
2530 strlcpy(devname, sc->sc_carpdev->if_xname, sizeof(devname));
2532 lwkt_replymsg(&cmsg->base.lmsg, 0);
2536 carp_ioctl_getdevname(struct carp_softc *sc, struct ifdrv *ifd)
2538 struct ifnet *ifp = &sc->arpcom.ac_if;
2539 struct netmsg_carp cmsg;
2540 char devname[IFNAMSIZ];
2543 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2545 if (ifd->ifd_len != sizeof(devname))
2548 ifnet_deserialize_all(ifp);
2550 bzero(&cmsg, sizeof(cmsg));
2551 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2552 carp_ioctl_getdevname_dispatch);
2554 cmsg.nc_data = devname;
2556 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2558 error = copyout(devname, ifd->ifd_data, sizeof(devname));
2560 ifnet_serialize_all(ifp);
2565 carp_init_dispatch(netmsg_t msg)
2567 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2568 struct carp_softc *sc = cmsg->nc_softc;
2570 sc->sc_if.if_flags |= IFF_RUNNING;
2571 carp_hmac_prepare(sc);
2572 carp_set_state(sc, INIT);
2575 lwkt_replymsg(&cmsg->base.lmsg, 0);
2579 carp_init(void *xsc)
2581 struct carp_softc *sc = xsc;
2582 struct ifnet *ifp = &sc->arpcom.ac_if;
2583 struct netmsg_carp cmsg;
2585 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2587 ifnet_deserialize_all(ifp);
2589 bzero(&cmsg, sizeof(cmsg));
2590 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2591 carp_init_dispatch);
2594 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2596 ifnet_serialize_all(ifp);
2600 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2603 struct carp_softc *sc = ifp->if_softc;
2604 struct ifnet *carpdev;
2607 carpdev = sc->sc_carpdev;
2608 if (carpdev != NULL) {
2611 * CARP's ifp is passed to backing device's
2614 carpdev->if_output(ifp, m, dst, rt);
2617 error = ENETUNREACH;
2623 * Start output on carp interface. This function should never be called.
2626 carp_start(struct ifnet *ifp)
2628 panic("%s: start called\n", ifp->if_xname);
2632 carp_set_state(struct carp_softc *sc, int state)
2634 struct ifnet *cifp = &sc->sc_if;
2636 if (sc->sc_state == state)
2638 sc->sc_state = state;
2640 switch (sc->sc_state) {
2642 cifp->if_link_state = LINK_STATE_DOWN;
2646 cifp->if_link_state = LINK_STATE_UP;
2650 cifp->if_link_state = LINK_STATE_UNKNOWN;
2657 carp_group_demote_adj(struct ifnet *ifp, int adj)
2659 struct ifg_list *ifgl;
2662 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2663 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2665 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2672 if (adj > 0 && *dm == 1)
2674 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2675 ifgl->ifgl_group->ifg_group, *dm);
2681 carp_carpdev_state(void *v)
2683 struct carp_if *cif = v;
2684 struct carp_softc *sc;
2686 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2691 carp_sc_state(struct carp_softc *sc)
2693 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2694 callout_stop(&sc->sc_ad_tmo);
2695 callout_stop(&sc->sc_md_tmo);
2696 callout_stop(&sc->sc_md6_tmo);
2697 carp_set_state(sc, INIT);
2699 if (!sc->sc_suppress) {
2700 carp_suppress_preempt++;
2701 if (carp_suppress_preempt == 1)
2704 sc->sc_suppress = 1;
2706 carp_set_state(sc, INIT);
2708 if (sc->sc_suppress)
2709 carp_suppress_preempt--;
2710 sc->sc_suppress = 0;
2716 carp_stop(struct carp_softc *sc, boolean_t detach)
2718 sc->sc_if.if_flags &= ~IFF_RUNNING;
2720 callout_stop(&sc->sc_ad_tmo);
2721 callout_stop(&sc->sc_md_tmo);
2722 callout_stop(&sc->sc_md6_tmo);
2724 if (!detach && sc->sc_state == MASTER)
2727 if (sc->sc_suppress)
2728 carp_suppress_preempt--;
2729 sc->sc_suppress = 0;
2731 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2732 carp_suppress_preempt--;
2733 sc->sc_sendad_errors = 0;
2734 sc->sc_sendad_success = 0;
2736 carp_set_state(sc, INIT);
2741 carp_suspend(struct carp_softc *sc, boolean_t detach)
2743 struct ifnet *cifp = &sc->sc_if;
2745 carp_stop(sc, detach);
2747 /* Retain the running state, if we are not dead yet */
2748 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2749 cifp->if_flags |= IFF_RUNNING;
2753 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2754 struct ifnet *ifp, struct in_ifaddr *ia_if, int own)
2756 struct ip_moptions *imo = &sc->sc_imo;
2757 struct carp_if *ocif = ifp->if_carp;
2760 KKASSERT(vha->vha_ia != NULL);
2762 KASSERT(ia_if != NULL, ("NULL backing address\n"));
2763 KASSERT(vha->vha_iaback == NULL, ("%p is already activated\n", vha));
2764 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2765 ("inactive vhaddr %p is the address owner\n", vha));
2767 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2768 ("%s is already on %s\n", sc->sc_if.if_xname,
2769 sc->sc_carpdev->if_xname));
2772 KASSERT(sc->sc_carpdev == NULL,
2773 ("%s is already on %s\n", sc->sc_if.if_xname,
2774 sc->sc_carpdev->if_xname));
2776 error = ifpromisc(ifp, 1);
2780 struct carp_softc_container *scc;
2782 TAILQ_FOREACH(scc, ocif, scc_link) {
2783 struct carp_softc *vr = scc->scc_softc;
2785 if (vr != sc && vr->sc_vhid == sc->sc_vhid)
2790 ifp->if_carp = carp_if_insert(ocif, sc);
2791 KASSERT(ifp->if_carp != NULL, ("%s carp_if_insert failed\n", __func__));
2794 sc->sc_carpdev = ifp;
2797 * Make sure that all protocol threads see the sc_carpdev and
2800 netmsg_service_sync();
2802 if (ocif != NULL && ifp->if_carp != ocif) {
2804 * The old carp list could be safely free now,
2805 * since no one can access it.
2810 vha->vha_iaback = ia_if;
2814 vha->vha_flags |= CARP_VHAF_OWNER;
2816 /* XXX save user configured advskew? */
2820 carp_addroute_vhaddr(sc, vha);
2823 * Join the multicast group only after the backing interface
2824 * has been hooked with the CARP interface.
2826 KASSERT(imo->imo_multicast_ifp == NULL ||
2827 imo->imo_multicast_ifp == &sc->sc_if,
2828 ("%s didn't leave mcast group on %s\n",
2829 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2831 if (imo->imo_num_memberships == 0) {
2832 struct in_addr addr;
2834 addr.s_addr = htonl(INADDR_CARP_GROUP);
2835 imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if);
2836 if (imo->imo_membership[0] == NULL) {
2837 carp_deactivate_vhaddr(sc, vha, FALSE);
2841 imo->imo_num_memberships++;
2842 imo->imo_multicast_ifp = &sc->sc_if;
2843 imo->imo_multicast_ttl = CARP_DFLTTL;
2844 imo->imo_multicast_loop = 0;
2847 carp_hmac_prepare(sc);
2848 carp_set_state(sc, INIT);
2854 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2855 boolean_t del_iaback)
2857 KKASSERT(vha->vha_ia != NULL);
2859 carp_hmac_prepare(sc);
2861 if (vha->vha_iaback == NULL) {
2862 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2863 ("inactive vhaddr %p is the address owner\n", vha));
2867 vha->vha_flags &= ~CARP_VHAF_OWNER;
2868 carp_delroute_vhaddr(sc, vha, del_iaback);
2870 KKASSERT(sc->sc_naddrs > 0);
2871 vha->vha_iaback = NULL;
2873 if (!sc->sc_naddrs) {
2874 if (sc->sc_naddrs6) {
2875 carp_multicast_cleanup(sc);
2878 carp_detach(sc, FALSE, del_iaback);
2884 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2886 struct carp_vhaddr *vha;
2887 struct in_ifaddr *ia_if;
2889 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2890 ia_if = ifatoia(ifa_if);
2893 * Test each inactive vhaddr against the newly added address.
2894 * If the newly added address could be the backing address,
2895 * then activate the matching vhaddr.
2897 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2898 const struct in_ifaddr *ia;
2902 if (vha->vha_iaback != NULL)
2906 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr);
2908 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet)
2912 if (ia->ia_addr.sin_addr.s_addr ==
2913 ia_if->ia_addr.sin_addr.s_addr)
2916 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2921 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
2922 struct ifaddr *ifa_if)
2924 struct carp_vhaddr *vha;
2925 struct in_ifaddr *ia_if;
2927 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2928 ia_if = ifatoia(ifa_if);
2931 * Ad src address is deleted; set it to NULL.
2932 * Following loop will try pick up a new ad src address
2933 * if one of the vhaddr could retain its backing address.
2935 if (sc->sc_ia == ia_if)
2939 * Test each active vhaddr against the deleted address.
2940 * If the deleted address is vhaddr address's backing
2941 * address, then deactivate the vhaddr.
2943 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2944 if (vha->vha_iaback == NULL)
2947 if (vha->vha_iaback == ia_if)
2948 carp_deactivate_vhaddr(sc, vha, TRUE);
2949 else if (sc->sc_ia == NULL)
2950 sc->sc_ia = vha->vha_iaback;
2955 carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del)
2957 struct carp_vhaddr *vha;
2959 KKASSERT(sc->sc_carpdev == NULL);
2961 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2962 carp_config_vhaddr(sc, vha, ifatoia(ifa_del));
2966 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
2967 enum ifaddr_event event, struct ifaddr *ifa)
2969 struct carp_softc *sc;
2971 if (ifa->ifa_addr->sa_family != AF_INET)
2974 KASSERT(&curthread->td_msgport == cpu_portfn(0),
2975 ("not in netisr0"));
2977 if (ifp->if_type == IFT_CARP) {
2979 * Address is changed on carp(4) interface
2982 case IFADDR_EVENT_ADD:
2983 carp_add_addr(ifp->if_softc, ifa);
2986 case IFADDR_EVENT_CHANGE:
2987 carp_config_addr(ifp->if_softc, ifa);
2990 case IFADDR_EVENT_DELETE:
2991 carp_del_addr(ifp->if_softc, ifa);
2998 * Address is changed on non-carp(4) interface
3000 if ((ifp->if_flags & IFF_MULTICAST) == 0)
3003 LIST_FOREACH(sc, &carpif_list, sc_next) {
3004 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
3005 /* Not the parent iface; skip */
3010 case IFADDR_EVENT_ADD:
3011 carp_link_addrs(sc, ifp, ifa);
3014 case IFADDR_EVENT_DELETE:
3015 if (sc->sc_carpdev != NULL) {
3016 carp_unlink_addrs(sc, ifp, ifa);
3017 if (sc->sc_carpdev == NULL) {
3019 * We no longer have the parent
3020 * interface, however, certain
3021 * virtual addresses, which are
3022 * not used because they can't
3023 * match the previous parent
3024 * interface's addresses, may now
3025 * match different interface's
3028 carp_update_addrs(sc, ifa);
3032 * The carp(4) interface didn't have a
3033 * parent iface, so it is not possible
3034 * that it will contain any address to
3040 case IFADDR_EVENT_CHANGE:
3041 if (sc->sc_carpdev == NULL) {
3043 * The carp(4) interface didn't have a
3044 * parent iface, so it is not possible
3045 * that it will contain any address to
3048 carp_link_addrs(sc, ifp, ifa);
3051 * First try breaking tie with the old
3052 * address. Then see whether we could
3053 * link certain vhaddr to the new address.
3054 * If that fails, i.e. carpdev is NULL,
3055 * we try a global update.
3057 * NOTE: The above order is critical.
3059 carp_unlink_addrs(sc, ifp, ifa);
3060 carp_link_addrs(sc, ifp, ifa);
3061 if (sc->sc_carpdev == NULL) {
3063 * See the comment in the above
3064 * IFADDR_EVENT_DELETE block.
3066 carp_update_addrs(sc, NULL);
3075 carp_proto_ctlinput(netmsg_t msg)
3077 int cmd = msg->ctlinput.nm_cmd;
3078 struct sockaddr *sa = msg->ctlinput.nm_arg;
3079 struct in_ifaddr_container *iac;
3081 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
3082 struct in_ifaddr *ia = iac->ia;
3083 struct ifnet *ifp = ia->ia_ifp;
3085 if (ifp->if_type == IFT_CARP)
3088 if (ia->ia_ifa.ifa_addr == sa) {
3089 if (cmd == PRC_IFDOWN) {
3090 carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE,
3092 } else if (cmd == PRC_IFUP) {
3093 carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD,
3100 lwkt_replymsg(&msg->lmsg, 0);
3104 carp_parent(struct ifnet *cifp)
3106 struct carp_softc *sc;
3108 KKASSERT(cifp->if_type == IFT_CARP);
3109 sc = cifp->if_softc;
3111 return sc->sc_carpdev;
3114 #define rtinitflags(x) \
3115 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
3119 carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
3121 struct in_ifaddr *ia, *iaback;
3124 if (sc->sc_state != MASTER)
3128 KKASSERT(ia != NULL);
3130 iaback = vha->vha_iaback;
3131 KKASSERT(iaback != NULL);
3133 rtinit(&iaback->ia_ifa, RTM_DELETE, rtinitflags(iaback));
3134 in_ifadown(&iaback->ia_ifa, 1);
3135 iaback->ia_flags &= ~IFA_ROUTE;
3137 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
3139 ia->ia_flags |= IFA_ROUTE;
3144 carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
3145 boolean_t del_iaback)
3147 struct in_ifaddr *ia, *iaback;
3150 KKASSERT(ia != NULL);
3152 iaback = vha->vha_iaback;
3153 KKASSERT(iaback != NULL);
3155 rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia));
3156 in_ifadown(&ia->ia_ifa, 1);
3157 ia->ia_flags &= ~IFA_ROUTE;
3159 if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) {
3162 error = rtinit(&iaback->ia_ifa, RTM_ADD,
3163 rtinitflags(iaback) | RTF_UP);
3165 iaback->ia_flags |= IFA_ROUTE;
3170 carp_modevent(module_t mod, int type, void *data)
3174 LIST_INIT(&carpif_list);
3175 carp_ifdetach_event =
3176 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
3177 EVENTHANDLER_PRI_ANY);
3179 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
3180 EVENTHANDLER_PRI_FIRST);
3181 if_clone_attach(&carp_cloner);
3185 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
3186 carp_ifdetach_event);
3187 EVENTHANDLER_DEREGISTER(ifaddr_event,
3189 if_clone_detach(&carp_cloner);
3198 static moduledata_t carp_mod = {
3203 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);