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>
64 #include <net/netisr2.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/if_ether.h>
76 #include <netinet/icmp6.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet6/scope6_var.h>
80 #include <netinet6/nd6.h>
83 #include <netinet/ip_carp.h>
86 * Note about carp's MP safe approach:
88 * Brief: carp_softc (softc), carp_softc_container (scc)
90 * - All configuration operation, e.g. ioctl, add/delete inet addresses
91 * is serialized by netisr0; not by carp's serializer
93 * - Backing interface's if_carp and carp_softc's relationship:
96 * if_carp -->| carp_if |
102 * | scc |-->| softc |
108 * | scc |-->| softc |
112 * - if_carp creation, modification and deletion all happen in netisr0,
113 * as stated previously. Since if_carp is accessed by multiple netisrs,
114 * the modification to if_carp is conducted in the following way:
118 * 1) Duplicate the old carp_if to new carp_if (ncif), and insert the
119 * to-be-added carp_softc to the new carp_if (ncif):
124 * +---------+ +---------+
125 * | carp_if | | carp_if |
126 * +---------+ +---------+
130 * +-----+ | | +-----+
131 * | scc |---->| softc |<----| scc |
132 * +-----+ | | +-----+
136 * +-----+ | | +-----+
137 * | scc |---->| softc |<----| scc |
138 * +-----+ | | +-----+
143 * | softc |<----| scc |
147 * 2) Switch save if_carp into ocif and switch if_carp to ncif:
152 * +---------+ +---------+
153 * | carp_if | | carp_if |
154 * +---------+ +---------+
158 * +-----+ | | +-----+
159 * | scc |---->| softc |<----| scc |
160 * +-----+ | | +-----+
164 * +-----+ | | +-----+
165 * | scc |---->| softc |<----| scc |
166 * +-----+ | | +-----+
171 * | softc |<----| scc |
175 * 3) Run netmsg_service_sync(), which will make sure that
176 * ocif is no longer accessed (all network operations
177 * are happened only in network threads).
178 * 4) Free ocif -- only carp_if and scc are freed.
181 * Removing carp_softc:
183 * 1) Duplicate the old carp_if to new carp_if (ncif); the to-be-deleted
184 * carp_softc will not be duplicated.
189 * +---------+ +---------+
190 * | carp_if | | carp_if |
191 * +---------+ +---------+
195 * +-----+ | | +-----+
196 * | scc |---->| softc |<----| scc |
197 * +-----+ | | +-----+
202 * | scc |---->| softc | |
207 * +-----+ | | +-----+
208 * | scc |---->| softc |<----| scc |
209 * +-----+ | | +-----+
212 * 2) Switch save if_carp into ocif and switch if_carp to ncif:
217 * +---------+ +---------+
218 * | carp_if | | carp_if |
219 * +---------+ +---------+
223 * +-----+ | | +-----+
224 * | scc |---->| softc |<----| scc |
225 * +-----+ | | +-----+
230 * | scc |---->| softc | |
235 * +-----+ | | +-----+
236 * | scc |---->| softc |<----| scc |
237 * +-----+ | | +-----+
240 * 3) Run netmsg_service_sync(), which will make sure that
241 * ocif is no longer accessed (all network operations
242 * are happened only in network threads).
243 * 4) Free ocif -- only carp_if and scc are freed.
245 * - if_carp accessing:
246 * The accessing code should cache the if_carp in a local temporary
247 * variable and accessing the temporary variable along the code path
248 * instead of accessing if_carp later on.
251 #define CARP_IFNAME "carp"
252 #define CARP_IS_RUNNING(ifp) \
253 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
258 uint32_t vha_flags; /* CARP_VHAF_ */
259 struct in_ifaddr *vha_ia; /* carp address */
260 struct in_ifaddr *vha_iaback; /* backing address */
261 TAILQ_ENTRY(carp_vhaddr) vha_link;
263 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr);
266 struct netmsg_base base;
267 struct ifnet *nc_carpdev;
268 struct carp_softc *nc_softc;
274 struct arpcom arpcom;
275 struct ifnet *sc_carpdev; /* parent interface */
276 struct carp_vhaddr_list sc_vha_list; /* virtual addr list */
278 const struct in_ifaddr *sc_ia; /* primary iface address v4 */
279 struct ip_moptions sc_imo;
282 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
283 struct ip6_moptions sc_im6o;
286 enum { INIT = 0, BACKUP, MASTER }
292 int sc_sendad_errors;
293 #define CARP_SENDAD_MAX_ERRORS 3
294 int sc_sendad_success;
295 #define CARP_SENDAD_MIN_SUCCESS 3
299 int sc_naddrs; /* actually used IPv4 vha */
301 int sc_advbase; /* seconds */
306 #define CARP_HMAC_PAD 64
307 unsigned char sc_key[CARP_KEY_LEN];
308 unsigned char sc_pad[CARP_HMAC_PAD];
311 struct callout sc_ad_tmo; /* advertisement timeout */
312 struct netmsg_carp sc_ad_msg; /* adv timeout netmsg */
313 struct callout sc_md_tmo; /* ip4 master down timeout */
314 struct callout sc_md6_tmo; /* ip6 master down timeout */
315 struct netmsg_carp sc_md_msg; /* master down timeout netmsg */
317 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
320 #define sc_if arpcom.ac_if
322 struct carp_softc_container {
323 TAILQ_ENTRY(carp_softc_container) scc_link;
324 struct carp_softc *scc_softc;
326 TAILQ_HEAD(carp_if, carp_softc_container);
328 SYSCTL_DECL(_net_inet_carp);
330 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
331 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
332 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
333 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
334 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
335 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
336 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
337 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
338 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
340 static int carp_suppress_preempt = 0;
341 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
342 &carp_suppress_preempt, 0, "Preemption is suppressed");
344 static struct carpstats carpstats;
345 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
346 &carpstats, carpstats,
347 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
349 #define CARP_LOG(...) do { \
350 if (carp_opts[CARPCTL_LOG] > 0) \
351 log(LOG_INFO, __VA_ARGS__); \
354 #define CARP_DEBUG(...) do { \
355 if (carp_opts[CARPCTL_LOG] > 1) \
356 log(LOG_DEBUG, __VA_ARGS__); \
359 static struct lwkt_token carp_listtok = LWKT_TOKEN_INITIALIZER(carp_list_token);
361 static void carp_hmac_prepare(struct carp_softc *);
362 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
364 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
366 static void carp_setroute(struct carp_softc *, int);
367 static void carp_proto_input_c(struct carp_softc *, struct mbuf *,
368 struct carp_header *, sa_family_t);
369 static int carp_clone_create(struct if_clone *, int, caddr_t);
370 static int carp_clone_destroy(struct ifnet *);
371 static void carp_detach(struct carp_softc *, boolean_t, boolean_t);
372 static void carp_prepare_ad(struct carp_softc *, struct carp_header *);
373 static void carp_send_ad_all(void);
374 static void carp_send_ad_timeout(void *);
375 static void carp_send_ad(struct carp_softc *);
376 static void carp_send_arp(struct carp_softc *);
377 static void carp_master_down_timeout(void *);
378 static void carp_master_down(struct carp_softc *);
379 static void carp_setrun(struct carp_softc *, sa_family_t);
380 static void carp_set_state(struct carp_softc *, int);
381 static struct ifnet *carp_forus(struct carp_if *, const uint8_t *);
383 static void carp_init(void *);
384 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
385 static int carp_output(struct ifnet *, struct mbuf *, struct sockaddr *,
387 static void carp_start(struct ifnet *, struct ifaltq_subque *);
389 static void carp_multicast_cleanup(struct carp_softc *);
390 static void carp_add_addr(struct carp_softc *, struct ifaddr *);
391 static void carp_del_addr(struct carp_softc *, struct ifaddr *);
392 static void carp_config_addr(struct carp_softc *, struct ifaddr *);
393 static void carp_link_addrs(struct carp_softc *, struct ifnet *,
395 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *,
397 static void carp_update_addrs(struct carp_softc *, struct ifaddr *);
399 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *,
401 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
402 struct ifnet *, struct in_ifaddr *, int);
403 static void carp_deactivate_vhaddr(struct carp_softc *,
404 struct carp_vhaddr *, boolean_t);
405 static int carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *);
406 static void carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *,
410 static void carp_sc_state(struct carp_softc *);
413 static void carp_send_na(struct carp_softc *);
415 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
416 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
418 static void carp_multicast6_cleanup(struct carp_softc *);
420 static void carp_stop(struct carp_softc *, boolean_t);
421 static void carp_suspend(struct carp_softc *, boolean_t);
422 static void carp_ioctl_stop(struct carp_softc *);
423 static int carp_ioctl_setvh(struct carp_softc *, void *, struct ucred *);
424 static int carp_ioctl_getvh(struct carp_softc *, void *, struct ucred *);
425 static int carp_ioctl_getdevname(struct carp_softc *, struct ifdrv *);
426 static int carp_ioctl_getvhaddr(struct carp_softc *, struct ifdrv *);
428 static struct carp_if *carp_if_remove(struct carp_if *, struct carp_softc *);
429 static struct carp_if *carp_if_insert(struct carp_if *, struct carp_softc *);
430 static void carp_if_free(struct carp_if *);
432 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
434 static void carp_ifdetach(void *, struct ifnet *);
436 static void carp_ifdetach_dispatch(netmsg_t);
437 static void carp_clone_destroy_dispatch(netmsg_t);
438 static void carp_init_dispatch(netmsg_t);
439 static void carp_ioctl_stop_dispatch(netmsg_t);
440 static void carp_ioctl_setvh_dispatch(netmsg_t);
441 static void carp_ioctl_getvh_dispatch(netmsg_t);
442 static void carp_ioctl_getdevname_dispatch(netmsg_t);
443 static void carp_ioctl_getvhaddr_dispatch(netmsg_t);
444 static void carp_send_ad_timeout_dispatch(netmsg_t);
445 static void carp_master_down_timeout_dispatch(netmsg_t);
447 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
449 static LIST_HEAD(, carp_softc) carpif_list;
451 static struct if_clone carp_cloner =
452 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
455 static uint8_t carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 };
457 static eventhandler_tag carp_ifdetach_event;
458 static eventhandler_tag carp_ifaddr_event;
461 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
463 struct carp_vhaddr *vha;
464 u_long new_addr, addr;
466 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
469 * Virtual address list is sorted; smaller one first
471 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
473 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
474 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
480 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
482 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
483 vha_new->vha_flags |= CARP_VHAF_ONLIST;
487 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
489 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
490 vha->vha_flags &= ~CARP_VHAF_ONLIST;
491 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
495 carp_hmac_prepare(struct carp_softc *sc)
497 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
498 uint8_t vhid = sc->sc_vhid & 0xff;
501 struct ifaddr_container *ifac;
505 struct carp_vhaddr *vha;
508 /* XXX: possible race here */
510 /* compute ipad from key */
511 bzero(sc->sc_pad, sizeof(sc->sc_pad));
512 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
513 for (i = 0; i < sizeof(sc->sc_pad); i++)
514 sc->sc_pad[i] ^= 0x36;
516 /* precompute first part of inner hash */
517 SHA1Init(&sc->sc_sha1);
518 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
519 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
520 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
521 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
523 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
524 SHA1Update(&sc->sc_sha1,
525 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
526 sizeof(struct in_addr));
530 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
531 struct ifaddr *ifa = ifac->ifa;
533 if (ifa->ifa_addr->sa_family == AF_INET6) {
534 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
535 in6_clearscope(&in6);
536 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
541 /* convert ipad to opad */
542 for (i = 0; i < sizeof(sc->sc_pad); i++)
543 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
547 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
548 unsigned char md[20])
552 /* fetch first half of inner hash */
553 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
555 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
556 SHA1Final(md, &sha1ctx);
560 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
561 SHA1Update(&sha1ctx, md, 20);
562 SHA1Final(md, &sha1ctx);
566 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
567 unsigned char md[20])
569 unsigned char md2[20];
571 carp_hmac_generate(sc, counter, md2);
572 return (bcmp(md, md2, sizeof(md2)));
576 carp_setroute(struct carp_softc *sc, int cmd)
579 struct ifaddr_container *ifac;
581 struct carp_vhaddr *vha;
583 KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD);
585 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
586 if (vha->vha_iaback == NULL)
588 if (cmd == RTM_DELETE)
589 carp_delroute_vhaddr(sc, vha, FALSE);
591 carp_addroute_vhaddr(sc, vha);
595 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
596 struct ifaddr *ifa = ifac->ifa;
598 if (ifa->ifa_addr->sa_family == AF_INET6) {
609 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
611 struct carp_softc *sc;
614 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
618 sc->sc_advbase = CARP_DFLTINTV;
619 sc->sc_vhid = -1; /* required setting */
621 sc->sc_init_counter = 1;
625 TAILQ_INIT(&sc->sc_vha_list);
628 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
631 callout_init_mp(&sc->sc_ad_tmo);
632 netmsg_init(&sc->sc_ad_msg.base, NULL, &netisr_adone_rport,
633 MSGF_DROPABLE | MSGF_PRIORITY, carp_send_ad_timeout_dispatch);
634 sc->sc_ad_msg.nc_softc = sc;
636 callout_init_mp(&sc->sc_md_tmo);
637 callout_init_mp(&sc->sc_md6_tmo);
638 netmsg_init(&sc->sc_md_msg.base, NULL, &netisr_adone_rport,
639 MSGF_DROPABLE | MSGF_PRIORITY, carp_master_down_timeout_dispatch);
640 sc->sc_md_msg.nc_softc = sc;
642 if_initname(ifp, CARP_IFNAME, unit);
644 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
645 ifp->if_init = carp_init;
646 ifp->if_ioctl = carp_ioctl;
647 ifp->if_start = carp_start;
648 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
649 ifq_set_ready(&ifp->if_snd);
651 ether_ifattach(ifp, carp_etheraddr, NULL);
653 ifp->if_type = IFT_CARP;
654 ifp->if_output = carp_output;
656 lwkt_gettoken(&carp_listtok);
657 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
658 lwkt_reltoken(&carp_listtok);
664 carp_clone_destroy_dispatch(netmsg_t msg)
666 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
667 struct carp_softc *sc = cmsg->nc_softc;
670 carp_detach(sc, TRUE, FALSE);
672 callout_stop_sync(&sc->sc_ad_tmo);
673 callout_stop_sync(&sc->sc_md_tmo);
674 callout_stop_sync(&sc->sc_md6_tmo);
677 lwkt_dropmsg(&sc->sc_ad_msg.base.lmsg);
678 lwkt_dropmsg(&sc->sc_md_msg.base.lmsg);
681 lwkt_replymsg(&cmsg->base.lmsg, 0);
685 carp_clone_destroy(struct ifnet *ifp)
687 struct carp_softc *sc = ifp->if_softc;
688 struct netmsg_carp cmsg;
690 bzero(&cmsg, sizeof(cmsg));
691 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
692 carp_clone_destroy_dispatch);
695 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
697 lwkt_gettoken(&carp_listtok);
698 LIST_REMOVE(sc, sc_next);
699 lwkt_reltoken(&carp_listtok);
704 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active"));
710 static struct carp_if *
711 carp_if_remove(struct carp_if *ocif, struct carp_softc *sc)
713 struct carp_softc_container *oscc, *scc;
720 TAILQ_FOREACH(oscc, ocif, scc_link) {
723 if (oscc->scc_softc == sc)
727 KASSERT(found, ("%s carp_softc is not on carp_if", __func__));
730 /* Last one is going to be unlinked */
734 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
737 TAILQ_FOREACH(oscc, ocif, scc_link) {
738 if (oscc->scc_softc == sc)
741 scc = kmalloc(sizeof(*scc), M_CARP, M_WAITOK | M_ZERO);
742 scc->scc_softc = oscc->scc_softc;
743 TAILQ_INSERT_TAIL(cif, scc, scc_link);
749 static struct carp_if *
750 carp_if_insert(struct carp_if *ocif, struct carp_softc *sc)
752 struct carp_softc_container *oscc;
757 TAILQ_FOREACH(oscc, ocif, scc_link) {
758 if (oscc->scc_softc == sc)
764 if (sc->sc_carpdev != NULL) {
765 KASSERT(onlist, ("%s is not on %s carp list",
766 sc->sc_if.if_xname, sc->sc_carpdev->if_xname));
768 KASSERT(!onlist, ("%s is already on carp list",
769 sc->sc_if.if_xname));
775 struct carp_softc_container *new_scc, *scc;
778 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
781 new_scc = kmalloc(sizeof(*new_scc), M_CARP, M_WAITOK | M_ZERO);
782 new_scc->scc_softc = sc;
785 TAILQ_FOREACH(oscc, ocif, scc_link) {
787 oscc->scc_softc->sc_vhid > sc->sc_vhid) {
788 TAILQ_INSERT_TAIL(cif, new_scc,
793 scc = kmalloc(sizeof(*scc), M_CARP,
795 scc->scc_softc = oscc->scc_softc;
796 TAILQ_INSERT_TAIL(cif, scc, scc_link);
800 TAILQ_INSERT_TAIL(cif, new_scc, scc_link);
809 carp_if_free(struct carp_if *cif)
811 struct carp_softc_container *scc;
813 while ((scc = TAILQ_FIRST(cif)) != NULL) {
814 TAILQ_REMOVE(cif, scc, scc_link);
821 carp_detach(struct carp_softc *sc, boolean_t detach, boolean_t del_iaback)
823 carp_suspend(sc, detach);
825 carp_multicast_cleanup(sc);
827 carp_multicast6_cleanup(sc);
830 if (!sc->sc_dead && detach) {
831 struct carp_vhaddr *vha;
833 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
834 carp_deactivate_vhaddr(sc, vha, del_iaback);
835 KKASSERT(sc->sc_naddrs == 0);
838 if (sc->sc_carpdev != NULL) {
839 struct ifnet *ifp = sc->sc_carpdev;
840 struct carp_if *ocif = ifp->if_carp;
842 ifp->if_carp = carp_if_remove(ocif, sc);
843 KASSERT(ifp->if_carp != ocif,
844 ("%s carp_if_remove failed", __func__));
846 sc->sc_carpdev = NULL;
850 * Make sure that all protocol threads see the
851 * sc_carpdev and if_carp changes
853 netmsg_service_sync();
855 if (ifp->if_carp == NULL) {
857 * No more carp interfaces using
858 * ifp as the backing interface,
859 * move it out of promiscous mode.
865 * The old carp list could be safely free now,
866 * since no one can access it.
873 carp_ifdetach_dispatch(netmsg_t msg)
875 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
876 struct ifnet *ifp = cmsg->nc_carpdev;
878 while (ifp->if_carp) {
879 struct carp_softc_container *scc;
881 scc = TAILQ_FIRST((struct carp_if *)(ifp->if_carp));
882 carp_detach(scc->scc_softc, TRUE, TRUE);
884 lwkt_replymsg(&cmsg->base.lmsg, 0);
887 /* Detach an interface from the carp. */
889 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
891 struct netmsg_carp cmsg;
893 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
895 bzero(&cmsg, sizeof(cmsg));
896 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
897 carp_ifdetach_dispatch);
898 cmsg.nc_carpdev = ifp;
900 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
904 * process input packet.
905 * we have rearranged checks order compared to the rfc,
906 * but it seems more efficient this way or not possible otherwise.
909 carp_proto_input(struct mbuf **mp, int *offp, int proto)
911 struct mbuf *m = *mp;
912 struct ip *ip = mtod(m, struct ip *);
913 struct ifnet *ifp = m->m_pkthdr.rcvif;
914 struct carp_header *ch;
915 struct carp_softc *sc;
921 carpstats.carps_ipackets++;
923 if (!carp_opts[CARPCTL_ALLOW]) {
928 /* Check if received on a valid carp interface */
929 if (ifp->if_type != IFT_CARP) {
930 carpstats.carps_badif++;
931 CARP_LOG("carp_proto_input: packet received on non-carp "
932 "interface: %s\n", ifp->if_xname);
937 if (!CARP_IS_RUNNING(ifp)) {
938 carpstats.carps_badif++;
939 CARP_LOG("carp_proto_input: packet received on stopped carp "
940 "interface: %s\n", ifp->if_xname);
946 if (sc->sc_carpdev == NULL) {
947 carpstats.carps_badif++;
948 CARP_LOG("carp_proto_input: packet received on defunc carp "
949 "interface: %s\n", ifp->if_xname);
954 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
955 carpstats.carps_badif++;
956 CARP_LOG("carp_proto_input: non-mcast packet on "
957 "interface: %s\n", ifp->if_xname);
962 /* Verify that the IP TTL is CARP_DFLTTL. */
963 if (ip->ip_ttl != CARP_DFLTTL) {
964 carpstats.carps_badttl++;
965 CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n",
966 ip->ip_ttl, CARP_DFLTTL, ifp->if_xname);
971 /* Minimal CARP packet size */
972 len = iphlen + sizeof(*ch);
975 * Verify that the received packet length is
976 * not less than the CARP header
978 if (m->m_pkthdr.len < len) {
979 carpstats.carps_badlen++;
980 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
986 /* Make sure that CARP header is contiguous */
987 if (len > m->m_len) {
988 m = m_pullup(m, len);
990 carpstats.carps_hdrops++;
991 CARP_LOG("carp_proto_input: m_pullup failed\n");
994 ip = mtod(m, struct ip *);
996 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
998 /* Verify the CARP checksum */
999 if (in_cksum_skip(m, len, iphlen)) {
1000 carpstats.carps_badsum++;
1001 CARP_LOG("carp_proto_input: checksum failed on %s\n",
1006 carp_proto_input_c(sc, m, ch, AF_INET);
1008 return(IPPROTO_DONE);
1013 carp6_proto_input(struct mbuf **mp, int *offp, int proto)
1015 struct mbuf *m = *mp;
1016 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1017 struct ifnet *ifp = m->m_pkthdr.rcvif;
1018 struct carp_header *ch;
1019 struct carp_softc *sc;
1022 carpstats.carps_ipackets6++;
1024 if (!carp_opts[CARPCTL_ALLOW]) {
1029 /* check if received on a valid carp interface */
1030 if (ifp->if_type != IFT_CARP) {
1031 carpstats.carps_badif++;
1032 CARP_LOG("carp6_proto_input: packet received on non-carp "
1033 "interface: %s\n", ifp->if_xname);
1038 if (!CARP_IS_RUNNING(ifp)) {
1039 carpstats.carps_badif++;
1040 CARP_LOG("carp_proto_input: packet received on stopped carp "
1041 "interface: %s\n", ifp->if_xname);
1047 if (sc->sc_carpdev == NULL) {
1048 carpstats.carps_badif++;
1049 CARP_LOG("carp6_proto_input: packet received on defunc-carp "
1050 "interface: %s\n", ifp->if_xname);
1055 /* verify that the IP TTL is 255 */
1056 if (ip6->ip6_hlim != CARP_DFLTTL) {
1057 carpstats.carps_badttl++;
1058 CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n",
1059 ip6->ip6_hlim, ifp->if_xname);
1064 /* verify that we have a complete carp packet */
1066 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
1068 carpstats.carps_badlen++;
1069 CARP_LOG("carp6_proto_input: packet size %u too small\n", len);
1073 /* verify the CARP checksum */
1074 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
1075 carpstats.carps_badsum++;
1076 CARP_LOG("carp6_proto_input: checksum failed, on %s\n",
1082 carp_proto_input_c(sc, m, ch, AF_INET6);
1084 return (IPPROTO_DONE);
1089 carp_proto_input_c(struct carp_softc *sc, struct mbuf *m,
1090 struct carp_header *ch, sa_family_t af)
1093 uint64_t tmp_counter;
1094 struct timeval sc_tv, ch_tv;
1096 if (sc->sc_vhid != ch->carp_vhid) {
1098 * CARP uses multicast, however, multicast packets
1099 * are tapped to all CARP interfaces on the physical
1100 * interface receiving the CARP packets, so we don't
1101 * update any stats here.
1108 /* verify the CARP version. */
1109 if (ch->carp_version != CARP_VERSION) {
1110 carpstats.carps_badver++;
1111 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
1117 /* verify the hash */
1118 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
1119 carpstats.carps_badauth++;
1120 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
1125 tmp_counter = ntohl(ch->carp_counter[0]);
1126 tmp_counter = tmp_counter<<32;
1127 tmp_counter += ntohl(ch->carp_counter[1]);
1129 /* XXX Replay protection goes here */
1131 sc->sc_init_counter = 0;
1132 sc->sc_counter = tmp_counter;
1134 sc_tv.tv_sec = sc->sc_advbase;
1135 if (carp_suppress_preempt && sc->sc_advskew < 240)
1136 sc_tv.tv_usec = 240 * 1000000 / 256;
1138 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1139 ch_tv.tv_sec = ch->carp_advbase;
1140 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
1142 switch (sc->sc_state) {
1148 * If we receive an advertisement from a master who's going to
1149 * be more frequent than us, go into BACKUP state.
1151 if (timevalcmp(&sc_tv, &ch_tv, >) ||
1152 timevalcmp(&sc_tv, &ch_tv, ==)) {
1153 callout_stop(&sc->sc_ad_tmo);
1154 CARP_DEBUG("%s: MASTER -> BACKUP "
1155 "(more frequent advertisement received)\n",
1157 carp_set_state(sc, BACKUP);
1159 carp_setroute(sc, RTM_DELETE);
1165 * If we're pre-empting masters who advertise slower than us,
1166 * and this one claims to be slower, treat him as down.
1168 if (carp_opts[CARPCTL_PREEMPT] &&
1169 timevalcmp(&sc_tv, &ch_tv, <)) {
1170 CARP_DEBUG("%s: BACKUP -> MASTER "
1171 "(preempting a slower master)\n", cifp->if_xname);
1172 carp_master_down(sc);
1177 * If the master is going to advertise at such a low frequency
1178 * that he's guaranteed to time out, we'd might as well just
1179 * treat him as timed out now.
1181 sc_tv.tv_sec = sc->sc_advbase * 3;
1182 if (timevalcmp(&sc_tv, &ch_tv, <)) {
1183 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1185 carp_master_down(sc);
1190 * Otherwise, we reset the counter and wait for the next
1193 carp_setrun(sc, af);
1200 carp_input(void *v, struct mbuf *m)
1202 struct carp_if *cif = v;
1203 struct ether_header *eh;
1204 struct carp_softc_container *scc;
1207 eh = mtod(m, struct ether_header *);
1209 ifp = carp_forus(cif, eh->ether_dhost);
1211 ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF);
1215 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
1219 * XXX Should really check the list of multicast addresses
1220 * for each CARP interface _before_ copying.
1222 TAILQ_FOREACH(scc, cif, scc_link) {
1223 struct carp_softc *sc = scc->scc_softc;
1226 if ((sc->sc_if.if_flags & IFF_UP) == 0)
1229 m0 = m_dup(m, MB_DONTWAIT);
1233 ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF);
1239 carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch)
1241 if (sc->sc_init_counter) {
1242 /* this could also be seconds since unix epoch */
1243 sc->sc_counter = karc4random();
1244 sc->sc_counter = sc->sc_counter << 32;
1245 sc->sc_counter += karc4random();
1250 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
1251 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
1253 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
1257 carp_send_ad_all(void)
1259 struct carp_softc *sc;
1261 LIST_FOREACH(sc, &carpif_list, sc_next) {
1262 if (sc->sc_carpdev == NULL)
1265 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
1271 carp_send_ad_timeout(void *xsc)
1273 struct carp_softc *sc = xsc;
1274 struct netmsg_carp *cmsg = &sc->sc_ad_msg;
1276 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d",
1277 __func__, mycpuid));
1280 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1281 lwkt_sendmsg(netisr_cpuport(0), &cmsg->base.lmsg);
1286 carp_send_ad_timeout_dispatch(netmsg_t msg)
1288 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1289 struct carp_softc *sc = cmsg->nc_softc;
1293 lwkt_replymsg(&cmsg->base.lmsg, 0);
1300 carp_send_ad(struct carp_softc *sc)
1302 struct ifnet *cifp = &sc->sc_if;
1303 struct carp_header ch;
1305 struct carp_header *ch_ptr;
1307 int len, advbase, advskew;
1309 if (!CARP_IS_RUNNING(cifp)) {
1314 advbase = sc->sc_advbase;
1315 if (!carp_suppress_preempt || sc->sc_advskew > 240)
1316 advskew = sc->sc_advskew;
1319 tv.tv_sec = advbase;
1320 tv.tv_usec = advskew * 1000000 / 256;
1323 ch.carp_version = CARP_VERSION;
1324 ch.carp_type = CARP_ADVERTISEMENT;
1325 ch.carp_vhid = sc->sc_vhid;
1326 ch.carp_advbase = advbase;
1327 ch.carp_advskew = advskew;
1328 ch.carp_authlen = 7; /* XXX DEFINE */
1329 ch.carp_pad1 = 0; /* must be zero */
1333 if (sc->sc_ia != NULL) {
1336 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1338 IFNET_STAT_INC(cifp, oerrors, 1);
1339 carpstats.carps_onomem++;
1340 /* XXX maybe less ? */
1341 if (advbase != 255 || advskew != 255)
1342 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1343 carp_send_ad_timeout, sc);
1346 len = sizeof(*ip) + sizeof(ch);
1347 m->m_pkthdr.len = len;
1348 m->m_pkthdr.rcvif = NULL;
1350 MH_ALIGN(m, m->m_len);
1351 m->m_flags |= M_MCAST;
1352 ip = mtod(m, struct ip *);
1353 ip->ip_v = IPVERSION;
1354 ip->ip_hl = sizeof(*ip) >> 2;
1355 ip->ip_tos = IPTOS_LOWDELAY;
1357 ip->ip_id = ip_newid();
1359 ip->ip_ttl = CARP_DFLTTL;
1360 ip->ip_p = IPPROTO_CARP;
1362 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
1363 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
1365 ch_ptr = (struct carp_header *)(&ip[1]);
1366 bcopy(&ch, ch_ptr, sizeof(ch));
1367 carp_prepare_ad(sc, ch_ptr);
1368 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
1370 getmicrotime(&cifp->if_lastchange);
1371 IFNET_STAT_INC(cifp, opackets, 1);
1372 IFNET_STAT_INC(cifp, obytes, len);
1373 carpstats.carps_opackets++;
1375 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
1376 IFNET_STAT_INC(cifp, oerrors, 1);
1377 if (sc->sc_sendad_errors < INT_MAX)
1378 sc->sc_sendad_errors++;
1379 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1380 carp_suppress_preempt++;
1381 if (carp_suppress_preempt == 1) {
1385 sc->sc_sendad_success = 0;
1387 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1388 if (++sc->sc_sendad_success >=
1389 CARP_SENDAD_MIN_SUCCESS) {
1390 carp_suppress_preempt--;
1391 sc->sc_sendad_errors = 0;
1394 sc->sc_sendad_errors = 0;
1401 struct ip6_hdr *ip6;
1403 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1405 IFNET_STAT_INC(cifp, oerrors, 1);
1406 carpstats.carps_onomem++;
1407 /* XXX maybe less ? */
1408 if (advbase != 255 || advskew != 255)
1409 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1410 carp_send_ad_timeout, sc);
1413 len = sizeof(*ip6) + sizeof(ch);
1414 m->m_pkthdr.len = len;
1415 m->m_pkthdr.rcvif = NULL;
1417 MH_ALIGN(m, m->m_len);
1418 m->m_flags |= M_MCAST;
1419 ip6 = mtod(m, struct ip6_hdr *);
1420 bzero(ip6, sizeof(*ip6));
1421 ip6->ip6_vfc |= IPV6_VERSION;
1422 ip6->ip6_hlim = CARP_DFLTTL;
1423 ip6->ip6_nxt = IPPROTO_CARP;
1424 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1425 sizeof(struct in6_addr));
1426 /* set the multicast destination */
1428 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1429 ip6->ip6_dst.s6_addr8[15] = 0x12;
1430 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1431 IFNET_STAT_INC(cifp, oerrors, 1);
1433 CARP_LOG("%s: in6_setscope failed\n", __func__);
1437 ch_ptr = (struct carp_header *)(&ip6[1]);
1438 bcopy(&ch, ch_ptr, sizeof(ch));
1439 carp_prepare_ad(sc, ch_ptr);
1440 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
1442 getmicrotime(&cifp->if_lastchange);
1443 IFNET_STAT_INC(cifp, opackets, 1);
1444 IFNET_STAT_INC(cifp, obytes, len);
1445 carpstats.carps_opackets6++;
1447 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1448 IFNET_STAT_INC(cifp, oerrors, 1);
1449 if (sc->sc_sendad_errors < INT_MAX)
1450 sc->sc_sendad_errors++;
1451 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1452 carp_suppress_preempt++;
1453 if (carp_suppress_preempt == 1) {
1457 sc->sc_sendad_success = 0;
1459 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1460 if (++sc->sc_sendad_success >=
1461 CARP_SENDAD_MIN_SUCCESS) {
1462 carp_suppress_preempt--;
1463 sc->sc_sendad_errors = 0;
1466 sc->sc_sendad_errors = 0;
1472 if (advbase != 255 || advskew != 255)
1473 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1474 carp_send_ad_timeout, sc);
1478 * Broadcast a gratuitous ARP request containing
1479 * the virtual router MAC address for each IP address
1480 * associated with the virtual router.
1483 carp_send_arp(struct carp_softc *sc)
1485 const struct carp_vhaddr *vha;
1487 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1488 if (vha->vha_iaback == NULL)
1490 arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa);
1496 carp_send_na(struct carp_softc *sc)
1498 struct ifaddr_container *ifac;
1499 struct in6_addr *in6;
1500 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1502 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1503 struct ifaddr *ifa = ifac->ifa;
1505 if (ifa->ifa_addr->sa_family != AF_INET6)
1508 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1509 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1510 ND_NA_FLAG_OVERRIDE, 1, NULL);
1511 DELAY(1000); /* XXX */
1516 static __inline const struct carp_vhaddr *
1517 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1519 struct carp_vhaddr *vha;
1521 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1522 if (vha->vha_iaback == NULL)
1525 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1533 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1534 const struct in_addr *isaddr, uint8_t **enaddr)
1536 const struct carp_softc *vh;
1537 int index, count = 0;
1540 * XXX proof of concept implementation.
1541 * We use the source ip to decide which virtual host should
1542 * handle the request. If we're master of that virtual host,
1543 * then we respond, otherwise, just drop the arp packet on
1547 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1548 if (!CARP_IS_RUNNING(&vh->sc_if))
1551 if (carp_find_addr(vh, itaddr) != NULL)
1557 /* this should be a hash, like pf_hash() */
1558 index = ntohl(isaddr->s_addr) % count;
1561 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1562 if (!CARP_IS_RUNNING(&vh->sc_if))
1565 if (carp_find_addr(vh, itaddr) == NULL)
1568 if (count == index) {
1569 if (vh->sc_state == MASTER) {
1570 *enaddr = IF_LLADDR(&vh->sc_if);
1583 carp_iamatch(const struct in_ifaddr *ia)
1585 const struct carp_softc *sc = ia->ia_ifp->if_softc;
1587 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1588 ("not in netisr0"));
1591 if (carp_opts[CARPCTL_ARPBALANCE])
1592 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1595 if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER)
1603 carp_iamatch6(void *v, struct in6_addr *taddr)
1606 struct carp_if *cif = v;
1607 struct carp_softc *vh;
1609 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1610 struct ifaddr_container *ifac;
1612 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1614 struct ifaddr *ifa = ifac->ifa;
1616 if (IN6_ARE_ADDR_EQUAL(taddr,
1617 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1618 CARP_IS_RUNNING(&vh->sc_if) &&
1619 vh->sc_state == MASTER) {
1629 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1633 struct carp_if *cif = v;
1634 struct carp_softc *sc;
1636 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1637 struct ifaddr_container *ifac;
1639 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1641 struct ifaddr *ifa = ifac->ifa;
1643 if (IN6_ARE_ADDR_EQUAL(taddr,
1644 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1645 CARP_IS_RUNNING(&sc->sc_if)) {
1646 struct ifnet *ifp = &sc->sc_if;
1648 mtag = m_tag_get(PACKET_TAG_CARP,
1649 sizeof(struct ifnet *), MB_DONTWAIT);
1651 /* better a bit than nothing */
1652 return (IF_LLADDR(ifp));
1654 bcopy(&ifp, (caddr_t)(mtag + 1),
1655 sizeof(struct ifnet *));
1656 m_tag_prepend(m, mtag);
1658 return (IF_LLADDR(ifp));
1667 static struct ifnet *
1668 carp_forus(struct carp_if *cif, const uint8_t *dhost)
1670 struct carp_softc_container *scc;
1672 if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0)
1675 TAILQ_FOREACH(scc, cif, scc_link) {
1676 struct carp_softc *sc = scc->scc_softc;
1677 struct ifnet *ifp = &sc->sc_if;
1679 if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER &&
1680 !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN))
1687 carp_master_down_timeout(void *xsc)
1689 struct carp_softc *sc = xsc;
1690 struct netmsg_carp *cmsg = &sc->sc_md_msg;
1692 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d",
1693 __func__, mycpuid));
1696 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1697 lwkt_sendmsg(netisr_cpuport(0), &cmsg->base.lmsg);
1702 carp_master_down_timeout_dispatch(netmsg_t msg)
1704 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1705 struct carp_softc *sc = cmsg->nc_softc;
1709 lwkt_replymsg(&cmsg->base.lmsg, 0);
1712 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1713 sc->sc_if.if_xname);
1714 carp_master_down(sc);
1718 carp_master_down(struct carp_softc *sc)
1720 switch (sc->sc_state) {
1722 kprintf("%s: master_down event in INIT state\n",
1723 sc->sc_if.if_xname);
1730 carp_set_state(sc, MASTER);
1737 carp_setroute(sc, RTM_ADD);
1743 * When in backup state, af indicates whether to reset the master down timer
1744 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1747 carp_setrun(struct carp_softc *sc, sa_family_t af)
1749 struct ifnet *cifp = &sc->sc_if;
1752 if (sc->sc_carpdev == NULL) {
1753 carp_set_state(sc, INIT);
1757 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1758 (sc->sc_naddrs || sc->sc_naddrs6)) {
1761 carp_setroute(sc, RTM_DELETE);
1765 switch (sc->sc_state) {
1767 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1773 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1775 carp_set_state(sc, MASTER);
1776 carp_setroute(sc, RTM_ADD);
1778 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1779 carp_set_state(sc, BACKUP);
1780 carp_setroute(sc, RTM_DELETE);
1786 callout_stop(&sc->sc_ad_tmo);
1787 tv.tv_sec = 3 * sc->sc_advbase;
1788 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1792 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1793 carp_master_down_timeout, sc);
1798 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1799 carp_master_down_timeout, sc);
1804 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1805 carp_master_down_timeout, sc);
1807 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1808 carp_master_down_timeout, sc);
1814 tv.tv_sec = sc->sc_advbase;
1815 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1816 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1817 carp_send_ad_timeout, sc);
1823 carp_multicast_cleanup(struct carp_softc *sc)
1825 struct ip_moptions *imo = &sc->sc_imo;
1827 if (imo->imo_num_memberships == 0)
1829 KKASSERT(imo->imo_num_memberships == 1);
1831 in_delmulti(imo->imo_membership[0]);
1832 imo->imo_membership[0] = NULL;
1833 imo->imo_num_memberships = 0;
1834 imo->imo_multicast_ifp = NULL;
1839 carp_multicast6_cleanup(struct carp_softc *sc)
1841 struct ip6_moptions *im6o = &sc->sc_im6o;
1843 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1844 struct in6_multi_mship *imm =
1845 LIST_FIRST(&im6o->im6o_memberships);
1847 LIST_REMOVE(imm, i6mm_chain);
1848 in6_leavegroup(imm);
1850 im6o->im6o_multicast_ifp = NULL;
1855 carp_ioctl_getvhaddr_dispatch(netmsg_t msg)
1857 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1858 struct carp_softc *sc = cmsg->nc_softc;
1859 const struct carp_vhaddr *vha;
1860 struct ifcarpvhaddr *carpa, *carpa0;
1861 int count, len, error = 0;
1864 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1867 if (cmsg->nc_datalen == 0) {
1868 cmsg->nc_datalen = count * sizeof(*carpa);
1870 } else if (count == 0 || cmsg->nc_datalen < sizeof(*carpa)) {
1871 cmsg->nc_datalen = 0;
1874 len = min(cmsg->nc_datalen, sizeof(*carpa) * count);
1875 KKASSERT(len >= sizeof(*carpa));
1877 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1878 if (carpa == NULL) {
1884 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1885 if (len < sizeof(*carpa))
1888 carpa->carpa_flags = vha->vha_flags;
1889 carpa->carpa_addr.sin_family = AF_INET;
1890 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1892 carpa->carpa_baddr.sin_family = AF_INET;
1893 if (vha->vha_iaback == NULL) {
1894 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1896 carpa->carpa_baddr.sin_addr =
1897 vha->vha_iaback->ia_addr.sin_addr;
1902 len -= sizeof(*carpa);
1904 cmsg->nc_datalen = sizeof(*carpa) * count;
1905 KKASSERT(cmsg->nc_datalen > 0);
1907 cmsg->nc_data = carpa0;
1910 lwkt_replymsg(&cmsg->base.lmsg, error);
1914 carp_ioctl_getvhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1916 struct ifnet *ifp = &sc->arpcom.ac_if;
1917 struct netmsg_carp cmsg;
1920 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1921 ifnet_deserialize_all(ifp);
1923 bzero(&cmsg, sizeof(cmsg));
1924 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
1925 carp_ioctl_getvhaddr_dispatch);
1927 cmsg.nc_datalen = ifd->ifd_len;
1929 error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
1932 if (cmsg.nc_data != NULL) {
1933 error = copyout(cmsg.nc_data, ifd->ifd_data,
1935 kfree(cmsg.nc_data, M_TEMP);
1937 ifd->ifd_len = cmsg.nc_datalen;
1939 KASSERT(cmsg.nc_data == NULL,
1940 ("%s temp vhaddr is alloc upon error", __func__));
1943 ifnet_serialize_all(ifp);
1948 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
1949 struct in_ifaddr *ia_del)
1952 struct in_ifaddr *ia_if;
1953 struct in_ifaddr_container *iac;
1954 const struct sockaddr_in *sin;
1958 KKASSERT(vha->vha_ia != NULL);
1960 sin = &vha->vha_ia->ia_addr;
1961 iaddr = ntohl(sin->sin_addr.s_addr);
1965 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1966 struct in_ifaddr *ia = iac->ia;
1971 if (ia->ia_ifp->if_type == IFT_CARP)
1974 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1977 /* and, yeah, we need a multicast-capable iface too */
1978 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1981 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1982 if (sin->sin_addr.s_addr ==
1983 ia->ia_addr.sin_addr.s_addr)
1987 else if (sc->sc_carpdev != NULL &&
1988 sc->sc_carpdev == ia->ia_ifp)
1993 carp_deactivate_vhaddr(sc, vha, FALSE);
1997 ifp = ia_if->ia_ifp;
1999 /* XXX Don't allow parent iface to be changed */
2000 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
2003 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2007 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2009 struct carp_vhaddr *vha_new;
2010 struct in_ifaddr *carp_ia;
2012 struct carp_vhaddr *vha;
2015 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2016 carp_ia = ifatoia(carp_ifa);
2019 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2020 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
2023 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
2024 vha_new->vha_ia = carp_ia;
2025 carp_insert_vhaddr(sc, vha_new);
2027 if (carp_config_vhaddr(sc, vha_new, NULL) != 0) {
2029 * If the above configuration fails, it may only mean
2030 * that the new address is problematic. However, the
2031 * carp(4) interface may already have several working
2032 * addresses. Since the expected behaviour of
2033 * SIOC[AS]IFADDR is to put the NIC into working state,
2034 * we try starting the state machine manually here with
2035 * the hope that the carp(4)'s previously working
2036 * addresses still could be brought up.
2038 carp_hmac_prepare(sc);
2039 carp_set_state(sc, INIT);
2045 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2047 struct carp_vhaddr *vha;
2048 struct in_ifaddr *carp_ia;
2050 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2051 carp_ia = ifatoia(carp_ifa);
2053 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2054 KKASSERT(vha->vha_ia != NULL);
2055 if (vha->vha_ia == carp_ia)
2058 KASSERT(vha != NULL, ("no corresponding vhaddr %p", carp_ifa));
2061 * Remove the vhaddr from the list before deactivating
2062 * the vhaddr, so that the HMAC could be correctly
2063 * updated in carp_deactivate_vhaddr()
2065 carp_remove_vhaddr(sc, vha);
2067 carp_deactivate_vhaddr(sc, vha, FALSE);
2072 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2074 struct carp_vhaddr *vha;
2075 struct in_ifaddr *carp_ia;
2077 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2078 carp_ia = ifatoia(carp_ifa);
2080 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2081 KKASSERT(vha->vha_ia != NULL);
2082 if (vha->vha_ia == carp_ia)
2085 KASSERT(vha != NULL, ("no corresponding vhaddr %p", carp_ifa));
2087 /* Remove then reinsert, to keep the vhaddr list sorted */
2088 carp_remove_vhaddr(sc, vha);
2089 carp_insert_vhaddr(sc, vha);
2091 if (carp_config_vhaddr(sc, vha, NULL) != 0) {
2092 /* See the comment in carp_add_addr() */
2093 carp_hmac_prepare(sc);
2094 carp_set_state(sc, INIT);
2103 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
2106 struct carp_if *cif;
2107 struct in6_ifaddr *ia, *ia_if;
2108 struct ip6_moptions *im6o = &sc->sc_im6o;
2109 struct in6_multi_mship *imm;
2110 struct in6_addr in6;
2113 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2118 /* we have to do it by hands to check we won't match on us */
2119 ia_if = NULL; own = 0;
2120 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
2123 for (i = 0; i < 4; i++) {
2124 if ((sin6->sin6_addr.s6_addr32[i] &
2125 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
2126 (ia->ia_addr.sin6_addr.s6_addr32[i] &
2127 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
2130 /* and, yeah, we need a multicast-capable iface too */
2131 if (ia->ia_ifp != &sc->sc_if &&
2132 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
2136 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2137 &ia->ia_addr.sin6_addr))
2143 return (EADDRNOTAVAIL);
2147 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
2148 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
2149 return (EADDRNOTAVAIL);
2151 if (!sc->sc_naddrs6) {
2152 im6o->im6o_multicast_ifp = ifp;
2154 /* join CARP multicast address */
2155 bzero(&in6, sizeof(in6));
2156 in6.s6_addr16[0] = htons(0xff02);
2157 in6.s6_addr8[15] = 0x12;
2158 if (in6_setscope(&in6, ifp, NULL) != 0)
2160 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2162 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2164 /* join solicited multicast address */
2165 bzero(&in6, sizeof(in6));
2166 in6.s6_addr16[0] = htons(0xff02);
2167 in6.s6_addr32[1] = 0;
2168 in6.s6_addr32[2] = htonl(1);
2169 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
2170 in6.s6_addr8[12] = 0xff;
2171 if (in6_setscope(&in6, ifp, NULL) != 0)
2173 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2175 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2179 if (!ifp->if_carp) {
2180 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2182 if ((error = ifpromisc(ifp, 1))) {
2187 TAILQ_INIT(&cif->vhif_vrs);
2190 struct carp_softc *vr;
2193 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2194 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
2202 sc->sc_carpdev = ifp;
2205 { /* XXX prevent endless loop if already in queue */
2206 struct carp_softc *vr, *after = NULL;
2210 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2213 if (vr->sc_vhid < sc->sc_vhid)
2218 /* We're trying to keep things in order */
2220 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2222 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2236 /* clean up multicast memberships */
2237 if (!sc->sc_naddrs6) {
2238 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2239 imm = LIST_FIRST(&im6o->im6o_memberships);
2240 LIST_REMOVE(imm, i6mm_chain);
2241 in6_leavegroup(imm);
2248 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
2252 if (!--sc->sc_naddrs6) {
2253 struct carp_if *cif = sc->sc_carpdev->if_carp;
2254 struct ip6_moptions *im6o = &sc->sc_im6o;
2256 callout_stop(&sc->sc_ad_tmo);
2258 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2259 struct in6_multi_mship *imm =
2260 LIST_FIRST(&im6o->im6o_memberships);
2262 LIST_REMOVE(imm, i6mm_chain);
2263 in6_leavegroup(imm);
2265 im6o->im6o_multicast_ifp = NULL;
2267 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
2268 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
2269 sc->sc_carpdev->if_carp = NULL;
2270 kfree(cif, M_IFADDR);
2281 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
2283 struct carp_softc *sc = ifp->if_softc;
2284 struct ifreq *ifr = (struct ifreq *)addr;
2285 struct ifdrv *ifd = (struct ifdrv *)addr;
2288 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2292 if (ifp->if_flags & IFF_UP) {
2293 if ((ifp->if_flags & IFF_RUNNING) == 0)
2295 } else if (ifp->if_flags & IFF_RUNNING) {
2296 carp_ioctl_stop(sc);
2301 error = carp_ioctl_setvh(sc, ifr->ifr_data, cr);
2305 error = carp_ioctl_getvh(sc, ifr->ifr_data, cr);
2309 switch (ifd->ifd_cmd) {
2311 error = carp_ioctl_getdevname(sc, ifd);
2315 error = carp_ioctl_getvhaddr(sc, ifd);
2325 error = ether_ioctl(ifp, cmd, addr);
2333 carp_ioctl_stop_dispatch(netmsg_t msg)
2335 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2336 struct carp_softc *sc = cmsg->nc_softc;
2338 carp_stop(sc, FALSE);
2339 lwkt_replymsg(&cmsg->base.lmsg, 0);
2343 carp_ioctl_stop(struct carp_softc *sc)
2345 struct ifnet *ifp = &sc->arpcom.ac_if;
2346 struct netmsg_carp cmsg;
2348 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2350 ifnet_deserialize_all(ifp);
2352 bzero(&cmsg, sizeof(cmsg));
2353 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2354 carp_ioctl_stop_dispatch);
2357 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2359 ifnet_serialize_all(ifp);
2363 carp_ioctl_setvh_dispatch(netmsg_t msg)
2365 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2366 struct carp_softc *sc = cmsg->nc_softc;
2367 struct ifnet *ifp = &sc->arpcom.ac_if;
2368 const struct carpreq *carpr = cmsg->nc_data;
2372 if ((ifp->if_flags & IFF_RUNNING) &&
2373 sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) {
2374 switch (carpr->carpr_state) {
2376 callout_stop(&sc->sc_ad_tmo);
2377 carp_set_state(sc, BACKUP);
2379 carp_setroute(sc, RTM_DELETE);
2383 carp_master_down(sc);
2390 if (carpr->carpr_vhid > 0) {
2391 if (carpr->carpr_vhid > 255) {
2395 if (sc->sc_carpdev) {
2396 struct carp_if *cif = sc->sc_carpdev->if_carp;
2397 struct carp_softc_container *scc;
2399 TAILQ_FOREACH(scc, cif, scc_link) {
2400 struct carp_softc *vr = scc->scc_softc;
2403 vr->sc_vhid == carpr->carpr_vhid) {
2409 sc->sc_vhid = carpr->carpr_vhid;
2411 IF_LLADDR(ifp)[5] = sc->sc_vhid;
2412 bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr,
2417 if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) {
2418 if (carpr->carpr_advskew >= 255) {
2422 if (carpr->carpr_advbase > 255) {
2426 sc->sc_advbase = carpr->carpr_advbase;
2427 sc->sc_advskew = carpr->carpr_advskew;
2430 bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key));
2438 carp_hmac_prepare(sc);
2440 lwkt_replymsg(&cmsg->base.lmsg, error);
2444 carp_ioctl_setvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2446 struct ifnet *ifp = &sc->arpcom.ac_if;
2447 struct netmsg_carp cmsg;
2448 struct carpreq carpr;
2451 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2452 ifnet_deserialize_all(ifp);
2454 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2458 error = copyin(udata, &carpr, sizeof(carpr));
2462 bzero(&cmsg, sizeof(cmsg));
2463 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2464 carp_ioctl_setvh_dispatch);
2466 cmsg.nc_data = &carpr;
2468 error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2471 ifnet_serialize_all(ifp);
2476 carp_ioctl_getvh_dispatch(netmsg_t msg)
2478 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2479 struct carp_softc *sc = cmsg->nc_softc;
2480 struct carpreq *carpr = cmsg->nc_data;
2482 carpr->carpr_state = sc->sc_state;
2483 carpr->carpr_vhid = sc->sc_vhid;
2484 carpr->carpr_advbase = sc->sc_advbase;
2485 carpr->carpr_advskew = sc->sc_advskew;
2486 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
2488 lwkt_replymsg(&cmsg->base.lmsg, 0);
2492 carp_ioctl_getvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2494 struct ifnet *ifp = &sc->arpcom.ac_if;
2495 struct netmsg_carp cmsg;
2496 struct carpreq carpr;
2499 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2500 ifnet_deserialize_all(ifp);
2502 bzero(&cmsg, sizeof(cmsg));
2503 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2504 carp_ioctl_getvh_dispatch);
2506 cmsg.nc_data = &carpr;
2508 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2510 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2512 bzero(carpr.carpr_key, sizeof(carpr.carpr_key));
2514 error = copyout(&carpr, udata, sizeof(carpr));
2516 ifnet_serialize_all(ifp);
2521 carp_ioctl_getdevname_dispatch(netmsg_t msg)
2523 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2524 struct carp_softc *sc = cmsg->nc_softc;
2525 char *devname = cmsg->nc_data;
2527 bzero(devname, IFNAMSIZ);
2528 if (sc->sc_carpdev != NULL)
2529 strlcpy(devname, sc->sc_carpdev->if_xname, IFNAMSIZ);
2531 lwkt_replymsg(&cmsg->base.lmsg, 0);
2535 carp_ioctl_getdevname(struct carp_softc *sc, struct ifdrv *ifd)
2537 struct ifnet *ifp = &sc->arpcom.ac_if;
2538 struct netmsg_carp cmsg;
2539 char devname[IFNAMSIZ];
2542 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2544 if (ifd->ifd_len != sizeof(devname))
2547 ifnet_deserialize_all(ifp);
2549 bzero(&cmsg, sizeof(cmsg));
2550 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2551 carp_ioctl_getdevname_dispatch);
2553 cmsg.nc_data = devname;
2555 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2557 error = copyout(devname, ifd->ifd_data, sizeof(devname));
2559 ifnet_serialize_all(ifp);
2564 carp_init_dispatch(netmsg_t msg)
2566 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2567 struct carp_softc *sc = cmsg->nc_softc;
2569 sc->sc_if.if_flags |= IFF_RUNNING;
2570 carp_hmac_prepare(sc);
2571 carp_set_state(sc, INIT);
2574 lwkt_replymsg(&cmsg->base.lmsg, 0);
2578 carp_init(void *xsc)
2580 struct carp_softc *sc = xsc;
2581 struct ifnet *ifp = &sc->arpcom.ac_if;
2582 struct netmsg_carp cmsg;
2584 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2586 ifnet_deserialize_all(ifp);
2588 bzero(&cmsg, sizeof(cmsg));
2589 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2590 carp_init_dispatch);
2593 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2595 ifnet_serialize_all(ifp);
2599 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2602 struct carp_softc *sc = ifp->if_softc;
2603 struct ifnet *carpdev;
2606 carpdev = sc->sc_carpdev;
2607 if (carpdev != NULL) {
2610 * CARP's ifp is passed to backing device's
2613 carpdev->if_output(ifp, m, dst, rt);
2616 error = ENETUNREACH;
2622 * Start output on carp interface. This function should never be called.
2625 carp_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
2627 panic("%s: start called", ifp->if_xname);
2631 carp_set_state(struct carp_softc *sc, int state)
2633 struct ifnet *cifp = &sc->sc_if;
2635 if (sc->sc_state == state)
2637 sc->sc_state = state;
2639 switch (sc->sc_state) {
2641 cifp->if_link_state = LINK_STATE_DOWN;
2645 cifp->if_link_state = LINK_STATE_UP;
2649 cifp->if_link_state = LINK_STATE_UNKNOWN;
2656 carp_group_demote_adj(struct ifnet *ifp, int adj)
2658 struct ifg_list *ifgl;
2661 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2662 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2664 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2671 if (adj > 0 && *dm == 1)
2673 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2674 ifgl->ifgl_group->ifg_group, *dm);
2680 carp_carpdev_state(void *v)
2682 struct carp_if *cif = v;
2683 struct carp_softc *sc;
2685 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2690 carp_sc_state(struct carp_softc *sc)
2692 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2693 callout_stop(&sc->sc_ad_tmo);
2694 callout_stop(&sc->sc_md_tmo);
2695 callout_stop(&sc->sc_md6_tmo);
2696 carp_set_state(sc, INIT);
2698 if (!sc->sc_suppress) {
2699 carp_suppress_preempt++;
2700 if (carp_suppress_preempt == 1)
2703 sc->sc_suppress = 1;
2705 carp_set_state(sc, INIT);
2707 if (sc->sc_suppress)
2708 carp_suppress_preempt--;
2709 sc->sc_suppress = 0;
2715 carp_stop(struct carp_softc *sc, boolean_t detach)
2717 sc->sc_if.if_flags &= ~IFF_RUNNING;
2719 callout_stop(&sc->sc_ad_tmo);
2720 callout_stop(&sc->sc_md_tmo);
2721 callout_stop(&sc->sc_md6_tmo);
2723 if (!detach && sc->sc_state == MASTER)
2726 if (sc->sc_suppress)
2727 carp_suppress_preempt--;
2728 sc->sc_suppress = 0;
2730 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2731 carp_suppress_preempt--;
2732 sc->sc_sendad_errors = 0;
2733 sc->sc_sendad_success = 0;
2735 carp_set_state(sc, INIT);
2740 carp_suspend(struct carp_softc *sc, boolean_t detach)
2742 struct ifnet *cifp = &sc->sc_if;
2744 carp_stop(sc, detach);
2746 /* Retain the running state, if we are not dead yet */
2747 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2748 cifp->if_flags |= IFF_RUNNING;
2752 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2753 struct ifnet *ifp, struct in_ifaddr *ia_if, int own)
2755 struct ip_moptions *imo = &sc->sc_imo;
2756 struct carp_if *ocif = ifp->if_carp;
2759 KKASSERT(vha->vha_ia != NULL);
2761 KASSERT(ia_if != NULL, ("NULL backing address"));
2762 KASSERT(vha->vha_iaback == NULL, ("%p is already activated", vha));
2763 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2764 ("inactive vhaddr %p is the address owner", vha));
2766 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2767 ("%s is already on %s", sc->sc_if.if_xname,
2768 sc->sc_carpdev->if_xname));
2771 KASSERT(sc->sc_carpdev == NULL,
2772 ("%s is already on %s", sc->sc_if.if_xname,
2773 sc->sc_carpdev->if_xname));
2775 error = ifpromisc(ifp, 1);
2779 struct carp_softc_container *scc;
2781 TAILQ_FOREACH(scc, ocif, scc_link) {
2782 struct carp_softc *vr = scc->scc_softc;
2784 if (vr != sc && vr->sc_vhid == sc->sc_vhid)
2789 ifp->if_carp = carp_if_insert(ocif, sc);
2790 KASSERT(ifp->if_carp != NULL, ("%s carp_if_insert failed", __func__));
2793 sc->sc_carpdev = ifp;
2796 * Make sure that all protocol threads see the sc_carpdev and
2799 netmsg_service_sync();
2801 if (ocif != NULL && ifp->if_carp != ocif) {
2803 * The old carp list could be safely free now,
2804 * since no one can access it.
2809 vha->vha_iaback = ia_if;
2813 vha->vha_flags |= CARP_VHAF_OWNER;
2815 /* XXX save user configured advskew? */
2819 carp_addroute_vhaddr(sc, vha);
2822 * Join the multicast group only after the backing interface
2823 * has been hooked with the CARP interface.
2825 KASSERT(imo->imo_multicast_ifp == NULL ||
2826 imo->imo_multicast_ifp == &sc->sc_if,
2827 ("%s didn't leave mcast group on %s",
2828 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2830 if (imo->imo_num_memberships == 0) {
2831 struct in_addr addr;
2833 addr.s_addr = htonl(INADDR_CARP_GROUP);
2834 imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if);
2835 if (imo->imo_membership[0] == NULL) {
2836 carp_deactivate_vhaddr(sc, vha, FALSE);
2840 imo->imo_num_memberships++;
2841 imo->imo_multicast_ifp = &sc->sc_if;
2842 imo->imo_multicast_ttl = CARP_DFLTTL;
2843 imo->imo_multicast_loop = 0;
2846 carp_hmac_prepare(sc);
2847 carp_set_state(sc, INIT);
2853 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2854 boolean_t del_iaback)
2856 KKASSERT(vha->vha_ia != NULL);
2858 carp_hmac_prepare(sc);
2860 if (vha->vha_iaback == NULL) {
2861 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2862 ("inactive vhaddr %p is the address owner", vha));
2866 vha->vha_flags &= ~CARP_VHAF_OWNER;
2867 carp_delroute_vhaddr(sc, vha, del_iaback);
2869 KKASSERT(sc->sc_naddrs > 0);
2870 vha->vha_iaback = NULL;
2872 if (!sc->sc_naddrs) {
2873 if (sc->sc_naddrs6) {
2874 carp_multicast_cleanup(sc);
2877 carp_detach(sc, FALSE, del_iaback);
2883 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2885 struct carp_vhaddr *vha;
2886 struct in_ifaddr *ia_if;
2888 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2889 ia_if = ifatoia(ifa_if);
2892 * Test each inactive vhaddr against the newly added address.
2893 * If the newly added address could be the backing address,
2894 * then activate the matching vhaddr.
2896 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2897 const struct in_ifaddr *ia;
2901 if (vha->vha_iaback != NULL)
2905 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr);
2907 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet)
2911 if (ia->ia_addr.sin_addr.s_addr ==
2912 ia_if->ia_addr.sin_addr.s_addr)
2915 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2920 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
2921 struct ifaddr *ifa_if)
2923 struct carp_vhaddr *vha;
2924 struct in_ifaddr *ia_if;
2926 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2927 ia_if = ifatoia(ifa_if);
2930 * Ad src address is deleted; set it to NULL.
2931 * Following loop will try pick up a new ad src address
2932 * if one of the vhaddr could retain its backing address.
2934 if (sc->sc_ia == ia_if)
2938 * Test each active vhaddr against the deleted address.
2939 * If the deleted address is vhaddr address's backing
2940 * address, then deactivate the vhaddr.
2942 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2943 if (vha->vha_iaback == NULL)
2946 if (vha->vha_iaback == ia_if)
2947 carp_deactivate_vhaddr(sc, vha, TRUE);
2948 else if (sc->sc_ia == NULL)
2949 sc->sc_ia = vha->vha_iaback;
2954 carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del)
2956 struct carp_vhaddr *vha;
2958 KKASSERT(sc->sc_carpdev == NULL);
2960 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2961 carp_config_vhaddr(sc, vha, ifatoia(ifa_del));
2965 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
2966 enum ifaddr_event event, struct ifaddr *ifa)
2968 struct carp_softc *sc;
2970 if (ifa->ifa_addr->sa_family != AF_INET)
2973 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
2974 ("not in netisr0"));
2976 if (ifp->if_type == IFT_CARP) {
2978 * Address is changed on carp(4) interface
2981 case IFADDR_EVENT_ADD:
2982 carp_add_addr(ifp->if_softc, ifa);
2985 case IFADDR_EVENT_CHANGE:
2986 carp_config_addr(ifp->if_softc, ifa);
2989 case IFADDR_EVENT_DELETE:
2990 carp_del_addr(ifp->if_softc, ifa);
2997 * Address is changed on non-carp(4) interface
2999 if ((ifp->if_flags & IFF_MULTICAST) == 0)
3002 LIST_FOREACH(sc, &carpif_list, sc_next) {
3003 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
3004 /* Not the parent iface; skip */
3009 case IFADDR_EVENT_ADD:
3010 carp_link_addrs(sc, ifp, ifa);
3013 case IFADDR_EVENT_DELETE:
3014 if (sc->sc_carpdev != NULL) {
3015 carp_unlink_addrs(sc, ifp, ifa);
3016 if (sc->sc_carpdev == NULL) {
3018 * We no longer have the parent
3019 * interface, however, certain
3020 * virtual addresses, which are
3021 * not used because they can't
3022 * match the previous parent
3023 * interface's addresses, may now
3024 * match different interface's
3027 carp_update_addrs(sc, ifa);
3031 * The carp(4) interface didn't have a
3032 * parent iface, so it is not possible
3033 * that it will contain any address to
3039 case IFADDR_EVENT_CHANGE:
3040 if (sc->sc_carpdev == NULL) {
3042 * The carp(4) interface didn't have a
3043 * parent iface, so it is not possible
3044 * that it will contain any address to
3047 carp_link_addrs(sc, ifp, ifa);
3050 * First try breaking tie with the old
3051 * address. Then see whether we could
3052 * link certain vhaddr to the new address.
3053 * If that fails, i.e. carpdev is NULL,
3054 * we try a global update.
3056 * NOTE: The above order is critical.
3058 carp_unlink_addrs(sc, ifp, ifa);
3059 carp_link_addrs(sc, ifp, ifa);
3060 if (sc->sc_carpdev == NULL) {
3062 * See the comment in the above
3063 * IFADDR_EVENT_DELETE block.
3065 carp_update_addrs(sc, NULL);
3074 carp_proto_ctlinput(netmsg_t msg)
3076 int cmd = msg->ctlinput.nm_cmd;
3077 struct sockaddr *sa = msg->ctlinput.nm_arg;
3078 struct in_ifaddr_container *iac;
3080 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
3081 struct in_ifaddr *ia = iac->ia;
3082 struct ifnet *ifp = ia->ia_ifp;
3084 if (ifp->if_type == IFT_CARP)
3087 if (ia->ia_ifa.ifa_addr == sa) {
3088 if (cmd == PRC_IFDOWN) {
3089 carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE,
3091 } else if (cmd == PRC_IFUP) {
3092 carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD,
3099 lwkt_replymsg(&msg->lmsg, 0);
3103 carp_parent(struct ifnet *cifp)
3105 struct carp_softc *sc;
3107 KKASSERT(cifp->if_type == IFT_CARP);
3108 sc = cifp->if_softc;
3110 return sc->sc_carpdev;
3113 #define rtinitflags(x) \
3114 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
3118 carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
3120 struct in_ifaddr *ia, *iaback;
3123 if (sc->sc_state != MASTER)
3127 KKASSERT(ia != NULL);
3129 iaback = vha->vha_iaback;
3130 KKASSERT(iaback != NULL);
3132 rtinit(&iaback->ia_ifa, RTM_DELETE, rtinitflags(iaback));
3133 in_ifadown(&iaback->ia_ifa, 1);
3134 iaback->ia_flags &= ~IFA_ROUTE;
3136 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
3138 ia->ia_flags |= IFA_ROUTE;
3143 carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
3144 boolean_t del_iaback)
3146 struct in_ifaddr *ia, *iaback;
3149 KKASSERT(ia != NULL);
3151 iaback = vha->vha_iaback;
3152 KKASSERT(iaback != NULL);
3154 rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia));
3155 in_ifadown(&ia->ia_ifa, 1);
3156 ia->ia_flags &= ~IFA_ROUTE;
3158 if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) {
3161 error = rtinit(&iaback->ia_ifa, RTM_ADD,
3162 rtinitflags(iaback) | RTF_UP);
3164 iaback->ia_flags |= IFA_ROUTE;
3169 carp_modevent(module_t mod, int type, void *data)
3173 LIST_INIT(&carpif_list);
3174 carp_ifdetach_event =
3175 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
3176 EVENTHANDLER_PRI_ANY);
3178 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
3179 EVENTHANDLER_PRI_FIRST);
3180 if_clone_attach(&carp_cloner);
3184 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
3185 carp_ifdetach_event);
3186 EVENTHANDLER_DEREGISTER(ifaddr_event,
3188 if_clone_detach(&carp_cloner);
3197 static moduledata_t carp_mod = {
3202 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);