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 void carp_ioctl_ifcap(struct carp_softc *, int);
425 static int carp_ioctl_getvh(struct carp_softc *, void *, struct ucred *);
426 static int carp_ioctl_getdevname(struct carp_softc *, struct ifdrv *);
427 static int carp_ioctl_getvhaddr(struct carp_softc *, struct ifdrv *);
429 static struct carp_if *carp_if_remove(struct carp_if *, struct carp_softc *);
430 static struct carp_if *carp_if_insert(struct carp_if *, struct carp_softc *);
431 static void carp_if_free(struct carp_if *);
433 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
435 static void carp_ifdetach(void *, struct ifnet *);
437 static void carp_ifdetach_dispatch(netmsg_t);
438 static void carp_clone_destroy_dispatch(netmsg_t);
439 static void carp_init_dispatch(netmsg_t);
440 static void carp_ioctl_stop_dispatch(netmsg_t);
441 static void carp_ioctl_setvh_dispatch(netmsg_t);
442 static void carp_ioctl_ifcap_dispatch(netmsg_t);
443 static void carp_ioctl_getvh_dispatch(netmsg_t);
444 static void carp_ioctl_getdevname_dispatch(netmsg_t);
445 static void carp_ioctl_getvhaddr_dispatch(netmsg_t);
446 static void carp_send_ad_timeout_dispatch(netmsg_t);
447 static void carp_master_down_timeout_dispatch(netmsg_t);
449 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
451 static LIST_HEAD(, carp_softc) carpif_list;
453 static struct if_clone carp_cloner =
454 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
457 static uint8_t carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 };
459 static eventhandler_tag carp_ifdetach_event;
460 static eventhandler_tag carp_ifaddr_event;
463 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
465 struct carp_vhaddr *vha;
466 u_long new_addr, addr;
468 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
471 * Virtual address list is sorted; smaller one first
473 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
475 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
476 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
482 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
484 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
485 vha_new->vha_flags |= CARP_VHAF_ONLIST;
489 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
491 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
492 vha->vha_flags &= ~CARP_VHAF_ONLIST;
493 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
497 carp_hmac_prepare(struct carp_softc *sc)
499 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
500 uint8_t vhid = sc->sc_vhid & 0xff;
503 struct ifaddr_container *ifac;
507 struct carp_vhaddr *vha;
510 /* XXX: possible race here */
512 /* compute ipad from key */
513 bzero(sc->sc_pad, sizeof(sc->sc_pad));
514 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
515 for (i = 0; i < sizeof(sc->sc_pad); i++)
516 sc->sc_pad[i] ^= 0x36;
518 /* precompute first part of inner hash */
519 SHA1Init(&sc->sc_sha1);
520 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
521 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
522 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
523 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
525 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
526 SHA1Update(&sc->sc_sha1,
527 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
528 sizeof(struct in_addr));
532 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
533 struct ifaddr *ifa = ifac->ifa;
535 if (ifa->ifa_addr->sa_family == AF_INET6) {
536 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
537 in6_clearscope(&in6);
538 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
543 /* convert ipad to opad */
544 for (i = 0; i < sizeof(sc->sc_pad); i++)
545 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
549 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
550 unsigned char md[20])
554 /* fetch first half of inner hash */
555 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
557 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
558 SHA1Final(md, &sha1ctx);
562 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
563 SHA1Update(&sha1ctx, md, 20);
564 SHA1Final(md, &sha1ctx);
568 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
569 unsigned char md[20])
571 unsigned char md2[20];
573 carp_hmac_generate(sc, counter, md2);
574 return (bcmp(md, md2, sizeof(md2)));
578 carp_setroute(struct carp_softc *sc, int cmd)
581 struct ifaddr_container *ifac;
583 struct carp_vhaddr *vha;
585 KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD);
587 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
588 if (vha->vha_iaback == NULL)
590 if (cmd == RTM_DELETE)
591 carp_delroute_vhaddr(sc, vha, FALSE);
593 carp_addroute_vhaddr(sc, vha);
597 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
598 struct ifaddr *ifa = ifac->ifa;
600 if (ifa->ifa_addr->sa_family == AF_INET6) {
611 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
613 struct carp_softc *sc;
616 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
620 sc->sc_advbase = CARP_DFLTINTV;
621 sc->sc_vhid = -1; /* required setting */
623 sc->sc_init_counter = 1;
627 TAILQ_INIT(&sc->sc_vha_list);
630 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
633 callout_init_mp(&sc->sc_ad_tmo);
634 netmsg_init(&sc->sc_ad_msg.base, NULL, &netisr_adone_rport,
635 MSGF_DROPABLE | MSGF_PRIORITY, carp_send_ad_timeout_dispatch);
636 sc->sc_ad_msg.nc_softc = sc;
638 callout_init_mp(&sc->sc_md_tmo);
639 callout_init_mp(&sc->sc_md6_tmo);
640 netmsg_init(&sc->sc_md_msg.base, NULL, &netisr_adone_rport,
641 MSGF_DROPABLE | MSGF_PRIORITY, carp_master_down_timeout_dispatch);
642 sc->sc_md_msg.nc_softc = sc;
644 if_initname(ifp, CARP_IFNAME, unit);
646 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
647 ifp->if_init = carp_init;
648 ifp->if_ioctl = carp_ioctl;
649 ifp->if_start = carp_start;
651 ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_TSO;
652 ifp->if_capenable = ifp->if_capabilities;
654 * Leave if_hwassist as it is; if_hwassist will be
655 * setup when this carp interface has parent.
658 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
659 ifq_set_ready(&ifp->if_snd);
661 ether_ifattach(ifp, carp_etheraddr, NULL);
663 ifp->if_type = IFT_CARP;
664 ifp->if_output = carp_output;
666 lwkt_gettoken(&carp_listtok);
667 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
668 lwkt_reltoken(&carp_listtok);
674 carp_clone_destroy_dispatch(netmsg_t msg)
676 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
677 struct carp_softc *sc = cmsg->nc_softc;
680 carp_detach(sc, TRUE, FALSE);
682 callout_stop_sync(&sc->sc_ad_tmo);
683 callout_stop_sync(&sc->sc_md_tmo);
684 callout_stop_sync(&sc->sc_md6_tmo);
687 lwkt_dropmsg(&sc->sc_ad_msg.base.lmsg);
688 lwkt_dropmsg(&sc->sc_md_msg.base.lmsg);
691 lwkt_replymsg(&cmsg->base.lmsg, 0);
695 carp_clone_destroy(struct ifnet *ifp)
697 struct carp_softc *sc = ifp->if_softc;
698 struct netmsg_carp cmsg;
700 bzero(&cmsg, sizeof(cmsg));
701 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
702 carp_clone_destroy_dispatch);
705 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
707 lwkt_gettoken(&carp_listtok);
708 LIST_REMOVE(sc, sc_next);
709 lwkt_reltoken(&carp_listtok);
714 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active"));
720 static struct carp_if *
721 carp_if_remove(struct carp_if *ocif, struct carp_softc *sc)
723 struct carp_softc_container *oscc, *scc;
730 TAILQ_FOREACH(oscc, ocif, scc_link) {
733 if (oscc->scc_softc == sc)
737 KASSERT(found, ("%s carp_softc is not on carp_if", __func__));
740 /* Last one is going to be unlinked */
744 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
747 TAILQ_FOREACH(oscc, ocif, scc_link) {
748 if (oscc->scc_softc == sc)
751 scc = kmalloc(sizeof(*scc), M_CARP, M_WAITOK | M_ZERO);
752 scc->scc_softc = oscc->scc_softc;
753 TAILQ_INSERT_TAIL(cif, scc, scc_link);
759 static struct carp_if *
760 carp_if_insert(struct carp_if *ocif, struct carp_softc *sc)
762 struct carp_softc_container *oscc;
767 TAILQ_FOREACH(oscc, ocif, scc_link) {
768 if (oscc->scc_softc == sc)
774 if (sc->sc_carpdev != NULL) {
775 KASSERT(onlist, ("%s is not on %s carp list",
776 sc->sc_if.if_xname, sc->sc_carpdev->if_xname));
778 KASSERT(!onlist, ("%s is already on carp list",
779 sc->sc_if.if_xname));
785 struct carp_softc_container *new_scc, *scc;
788 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
791 new_scc = kmalloc(sizeof(*new_scc), M_CARP, M_WAITOK | M_ZERO);
792 new_scc->scc_softc = sc;
795 TAILQ_FOREACH(oscc, ocif, scc_link) {
797 oscc->scc_softc->sc_vhid > sc->sc_vhid) {
798 TAILQ_INSERT_TAIL(cif, new_scc,
803 scc = kmalloc(sizeof(*scc), M_CARP,
805 scc->scc_softc = oscc->scc_softc;
806 TAILQ_INSERT_TAIL(cif, scc, scc_link);
810 TAILQ_INSERT_TAIL(cif, new_scc, scc_link);
819 carp_if_free(struct carp_if *cif)
821 struct carp_softc_container *scc;
823 while ((scc = TAILQ_FIRST(cif)) != NULL) {
824 TAILQ_REMOVE(cif, scc, scc_link);
831 carp_detach(struct carp_softc *sc, boolean_t detach, boolean_t del_iaback)
833 carp_suspend(sc, detach);
835 carp_multicast_cleanup(sc);
837 carp_multicast6_cleanup(sc);
840 if (!sc->sc_dead && detach) {
841 struct carp_vhaddr *vha;
843 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
844 carp_deactivate_vhaddr(sc, vha, del_iaback);
845 KKASSERT(sc->sc_naddrs == 0);
848 if (sc->sc_carpdev != NULL) {
849 struct ifnet *ifp = sc->sc_carpdev;
850 struct carp_if *ocif = ifp->if_carp;
852 ifp->if_carp = carp_if_remove(ocif, sc);
853 KASSERT(ifp->if_carp != ocif,
854 ("%s carp_if_remove failed", __func__));
856 sc->sc_carpdev = NULL;
858 sc->arpcom.ac_if.if_hwassist = 0;
861 * Make sure that all protocol threads see the
862 * sc_carpdev and if_carp changes
864 netmsg_service_sync();
866 if (ifp->if_carp == NULL) {
868 * No more carp interfaces using
869 * ifp as the backing interface,
870 * move it out of promiscous mode.
876 * The old carp list could be safely free now,
877 * since no one can access it.
884 carp_ifdetach_dispatch(netmsg_t msg)
886 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
887 struct ifnet *ifp = cmsg->nc_carpdev;
889 while (ifp->if_carp) {
890 struct carp_softc_container *scc;
892 scc = TAILQ_FIRST((struct carp_if *)(ifp->if_carp));
893 carp_detach(scc->scc_softc, TRUE, TRUE);
895 lwkt_replymsg(&cmsg->base.lmsg, 0);
898 /* Detach an interface from the carp. */
900 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
902 struct netmsg_carp cmsg;
904 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
906 bzero(&cmsg, sizeof(cmsg));
907 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
908 carp_ifdetach_dispatch);
909 cmsg.nc_carpdev = ifp;
911 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
915 * process input packet.
916 * we have rearranged checks order compared to the rfc,
917 * but it seems more efficient this way or not possible otherwise.
920 carp_proto_input(struct mbuf **mp, int *offp, int proto)
922 struct mbuf *m = *mp;
923 struct ip *ip = mtod(m, struct ip *);
924 struct ifnet *ifp = m->m_pkthdr.rcvif;
925 struct carp_header *ch;
926 struct carp_softc *sc;
932 carpstats.carps_ipackets++;
934 if (!carp_opts[CARPCTL_ALLOW]) {
939 /* Check if received on a valid carp interface */
940 if (ifp->if_type != IFT_CARP) {
941 carpstats.carps_badif++;
942 CARP_LOG("carp_proto_input: packet received on non-carp "
943 "interface: %s\n", ifp->if_xname);
948 if (!CARP_IS_RUNNING(ifp)) {
949 carpstats.carps_badif++;
950 CARP_LOG("carp_proto_input: packet received on stopped carp "
951 "interface: %s\n", ifp->if_xname);
957 if (sc->sc_carpdev == NULL) {
958 carpstats.carps_badif++;
959 CARP_LOG("carp_proto_input: packet received on defunc carp "
960 "interface: %s\n", ifp->if_xname);
965 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
966 carpstats.carps_badif++;
967 CARP_LOG("carp_proto_input: non-mcast packet on "
968 "interface: %s\n", ifp->if_xname);
973 /* Verify that the IP TTL is CARP_DFLTTL. */
974 if (ip->ip_ttl != CARP_DFLTTL) {
975 carpstats.carps_badttl++;
976 CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n",
977 ip->ip_ttl, CARP_DFLTTL, ifp->if_xname);
982 /* Minimal CARP packet size */
983 len = iphlen + sizeof(*ch);
986 * Verify that the received packet length is
987 * not less than the CARP header
989 if (m->m_pkthdr.len < len) {
990 carpstats.carps_badlen++;
991 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
997 /* Make sure that CARP header is contiguous */
998 if (len > m->m_len) {
999 m = m_pullup(m, len);
1001 carpstats.carps_hdrops++;
1002 CARP_LOG("carp_proto_input: m_pullup failed\n");
1005 ip = mtod(m, struct ip *);
1007 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
1009 /* Verify the CARP checksum */
1010 if (in_cksum_skip(m, len, iphlen)) {
1011 carpstats.carps_badsum++;
1012 CARP_LOG("carp_proto_input: checksum failed on %s\n",
1017 carp_proto_input_c(sc, m, ch, AF_INET);
1019 return(IPPROTO_DONE);
1024 carp6_proto_input(struct mbuf **mp, int *offp, int proto)
1026 struct mbuf *m = *mp;
1027 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1028 struct ifnet *ifp = m->m_pkthdr.rcvif;
1029 struct carp_header *ch;
1030 struct carp_softc *sc;
1033 carpstats.carps_ipackets6++;
1035 if (!carp_opts[CARPCTL_ALLOW]) {
1040 /* check if received on a valid carp interface */
1041 if (ifp->if_type != IFT_CARP) {
1042 carpstats.carps_badif++;
1043 CARP_LOG("carp6_proto_input: packet received on non-carp "
1044 "interface: %s\n", ifp->if_xname);
1049 if (!CARP_IS_RUNNING(ifp)) {
1050 carpstats.carps_badif++;
1051 CARP_LOG("carp_proto_input: packet received on stopped carp "
1052 "interface: %s\n", ifp->if_xname);
1058 if (sc->sc_carpdev == NULL) {
1059 carpstats.carps_badif++;
1060 CARP_LOG("carp6_proto_input: packet received on defunc-carp "
1061 "interface: %s\n", ifp->if_xname);
1066 /* verify that the IP TTL is 255 */
1067 if (ip6->ip6_hlim != CARP_DFLTTL) {
1068 carpstats.carps_badttl++;
1069 CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n",
1070 ip6->ip6_hlim, ifp->if_xname);
1075 /* verify that we have a complete carp packet */
1077 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
1079 carpstats.carps_badlen++;
1080 CARP_LOG("carp6_proto_input: packet size %u too small\n", len);
1084 /* verify the CARP checksum */
1085 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
1086 carpstats.carps_badsum++;
1087 CARP_LOG("carp6_proto_input: checksum failed, on %s\n",
1093 carp_proto_input_c(sc, m, ch, AF_INET6);
1095 return (IPPROTO_DONE);
1100 carp_proto_input_c(struct carp_softc *sc, struct mbuf *m,
1101 struct carp_header *ch, sa_family_t af)
1104 uint64_t tmp_counter;
1105 struct timeval sc_tv, ch_tv;
1107 if (sc->sc_vhid != ch->carp_vhid) {
1109 * CARP uses multicast, however, multicast packets
1110 * are tapped to all CARP interfaces on the physical
1111 * interface receiving the CARP packets, so we don't
1112 * update any stats here.
1119 /* verify the CARP version. */
1120 if (ch->carp_version != CARP_VERSION) {
1121 carpstats.carps_badver++;
1122 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
1128 /* verify the hash */
1129 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
1130 carpstats.carps_badauth++;
1131 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
1136 tmp_counter = ntohl(ch->carp_counter[0]);
1137 tmp_counter = tmp_counter<<32;
1138 tmp_counter += ntohl(ch->carp_counter[1]);
1140 /* XXX Replay protection goes here */
1142 sc->sc_init_counter = 0;
1143 sc->sc_counter = tmp_counter;
1145 sc_tv.tv_sec = sc->sc_advbase;
1146 if (carp_suppress_preempt && sc->sc_advskew < 240)
1147 sc_tv.tv_usec = 240 * 1000000 / 256;
1149 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1150 ch_tv.tv_sec = ch->carp_advbase;
1151 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
1153 switch (sc->sc_state) {
1159 * If we receive an advertisement from a master who's going to
1160 * be more frequent than us, go into BACKUP state.
1162 if (timevalcmp(&sc_tv, &ch_tv, >) ||
1163 timevalcmp(&sc_tv, &ch_tv, ==)) {
1164 callout_stop(&sc->sc_ad_tmo);
1165 CARP_DEBUG("%s: MASTER -> BACKUP "
1166 "(more frequent advertisement received)\n",
1168 carp_set_state(sc, BACKUP);
1170 carp_setroute(sc, RTM_DELETE);
1176 * If we're pre-empting masters who advertise slower than us,
1177 * and this one claims to be slower, treat him as down.
1179 if (carp_opts[CARPCTL_PREEMPT] &&
1180 timevalcmp(&sc_tv, &ch_tv, <)) {
1181 CARP_DEBUG("%s: BACKUP -> MASTER "
1182 "(preempting a slower master)\n", cifp->if_xname);
1183 carp_master_down(sc);
1188 * If the master is going to advertise at such a low frequency
1189 * that he's guaranteed to time out, we'd might as well just
1190 * treat him as timed out now.
1192 sc_tv.tv_sec = sc->sc_advbase * 3;
1193 if (timevalcmp(&sc_tv, &ch_tv, <)) {
1194 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1196 carp_master_down(sc);
1201 * Otherwise, we reset the counter and wait for the next
1204 carp_setrun(sc, af);
1211 carp_input(void *v, struct mbuf *m)
1213 struct carp_if *cif = v;
1214 struct ether_header *eh;
1215 struct carp_softc_container *scc;
1218 eh = mtod(m, struct ether_header *);
1220 ifp = carp_forus(cif, eh->ether_dhost);
1222 ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF);
1226 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
1230 * XXX Should really check the list of multicast addresses
1231 * for each CARP interface _before_ copying.
1233 TAILQ_FOREACH(scc, cif, scc_link) {
1234 struct carp_softc *sc = scc->scc_softc;
1237 if ((sc->sc_if.if_flags & IFF_UP) == 0)
1240 m0 = m_dup(m, MB_DONTWAIT);
1244 ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF);
1250 carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch)
1252 if (sc->sc_init_counter) {
1253 /* this could also be seconds since unix epoch */
1254 sc->sc_counter = karc4random();
1255 sc->sc_counter = sc->sc_counter << 32;
1256 sc->sc_counter += karc4random();
1261 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
1262 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
1264 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
1268 carp_send_ad_all(void)
1270 struct carp_softc *sc;
1272 LIST_FOREACH(sc, &carpif_list, sc_next) {
1273 if (sc->sc_carpdev == NULL)
1276 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
1282 carp_send_ad_timeout(void *xsc)
1284 struct carp_softc *sc = xsc;
1285 struct netmsg_carp *cmsg = &sc->sc_ad_msg;
1287 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d",
1288 __func__, mycpuid));
1291 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1292 lwkt_sendmsg(netisr_cpuport(0), &cmsg->base.lmsg);
1297 carp_send_ad_timeout_dispatch(netmsg_t msg)
1299 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1300 struct carp_softc *sc = cmsg->nc_softc;
1304 lwkt_replymsg(&cmsg->base.lmsg, 0);
1311 carp_send_ad(struct carp_softc *sc)
1313 struct ifnet *cifp = &sc->sc_if;
1314 struct carp_header ch;
1316 struct carp_header *ch_ptr;
1318 int len, advbase, advskew;
1320 if (!CARP_IS_RUNNING(cifp)) {
1325 advbase = sc->sc_advbase;
1326 if (!carp_suppress_preempt || sc->sc_advskew > 240)
1327 advskew = sc->sc_advskew;
1330 tv.tv_sec = advbase;
1331 tv.tv_usec = advskew * 1000000 / 256;
1334 ch.carp_version = CARP_VERSION;
1335 ch.carp_type = CARP_ADVERTISEMENT;
1336 ch.carp_vhid = sc->sc_vhid;
1337 ch.carp_advbase = advbase;
1338 ch.carp_advskew = advskew;
1339 ch.carp_authlen = 7; /* XXX DEFINE */
1340 ch.carp_pad1 = 0; /* must be zero */
1344 if (sc->sc_ia != NULL) {
1347 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1349 IFNET_STAT_INC(cifp, oerrors, 1);
1350 carpstats.carps_onomem++;
1351 /* XXX maybe less ? */
1352 if (advbase != 255 || advskew != 255)
1353 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1354 carp_send_ad_timeout, sc);
1357 len = sizeof(*ip) + sizeof(ch);
1358 m->m_pkthdr.len = len;
1359 m->m_pkthdr.rcvif = NULL;
1361 MH_ALIGN(m, m->m_len);
1362 m->m_flags |= M_MCAST;
1363 ip = mtod(m, struct ip *);
1364 ip->ip_v = IPVERSION;
1365 ip->ip_hl = sizeof(*ip) >> 2;
1366 ip->ip_tos = IPTOS_LOWDELAY;
1368 ip->ip_id = ip_newid();
1370 ip->ip_ttl = CARP_DFLTTL;
1371 ip->ip_p = IPPROTO_CARP;
1373 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
1374 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
1376 ch_ptr = (struct carp_header *)(&ip[1]);
1377 bcopy(&ch, ch_ptr, sizeof(ch));
1378 carp_prepare_ad(sc, ch_ptr);
1379 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
1381 getmicrotime(&cifp->if_lastchange);
1382 IFNET_STAT_INC(cifp, opackets, 1);
1383 IFNET_STAT_INC(cifp, obytes, len);
1384 carpstats.carps_opackets++;
1386 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
1387 IFNET_STAT_INC(cifp, oerrors, 1);
1388 if (sc->sc_sendad_errors < INT_MAX)
1389 sc->sc_sendad_errors++;
1390 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1391 carp_suppress_preempt++;
1392 if (carp_suppress_preempt == 1) {
1396 sc->sc_sendad_success = 0;
1398 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1399 if (++sc->sc_sendad_success >=
1400 CARP_SENDAD_MIN_SUCCESS) {
1401 carp_suppress_preempt--;
1402 sc->sc_sendad_errors = 0;
1405 sc->sc_sendad_errors = 0;
1412 struct ip6_hdr *ip6;
1414 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1416 IFNET_STAT_INC(cifp, oerrors, 1);
1417 carpstats.carps_onomem++;
1418 /* XXX maybe less ? */
1419 if (advbase != 255 || advskew != 255)
1420 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1421 carp_send_ad_timeout, sc);
1424 len = sizeof(*ip6) + sizeof(ch);
1425 m->m_pkthdr.len = len;
1426 m->m_pkthdr.rcvif = NULL;
1428 MH_ALIGN(m, m->m_len);
1429 m->m_flags |= M_MCAST;
1430 ip6 = mtod(m, struct ip6_hdr *);
1431 bzero(ip6, sizeof(*ip6));
1432 ip6->ip6_vfc |= IPV6_VERSION;
1433 ip6->ip6_hlim = CARP_DFLTTL;
1434 ip6->ip6_nxt = IPPROTO_CARP;
1435 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1436 sizeof(struct in6_addr));
1437 /* set the multicast destination */
1439 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1440 ip6->ip6_dst.s6_addr8[15] = 0x12;
1441 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1442 IFNET_STAT_INC(cifp, oerrors, 1);
1444 CARP_LOG("%s: in6_setscope failed\n", __func__);
1448 ch_ptr = (struct carp_header *)(&ip6[1]);
1449 bcopy(&ch, ch_ptr, sizeof(ch));
1450 carp_prepare_ad(sc, ch_ptr);
1451 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
1453 getmicrotime(&cifp->if_lastchange);
1454 IFNET_STAT_INC(cifp, opackets, 1);
1455 IFNET_STAT_INC(cifp, obytes, len);
1456 carpstats.carps_opackets6++;
1458 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1459 IFNET_STAT_INC(cifp, oerrors, 1);
1460 if (sc->sc_sendad_errors < INT_MAX)
1461 sc->sc_sendad_errors++;
1462 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1463 carp_suppress_preempt++;
1464 if (carp_suppress_preempt == 1) {
1468 sc->sc_sendad_success = 0;
1470 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1471 if (++sc->sc_sendad_success >=
1472 CARP_SENDAD_MIN_SUCCESS) {
1473 carp_suppress_preempt--;
1474 sc->sc_sendad_errors = 0;
1477 sc->sc_sendad_errors = 0;
1483 if (advbase != 255 || advskew != 255)
1484 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1485 carp_send_ad_timeout, sc);
1489 * Broadcast a gratuitous ARP request containing
1490 * the virtual router MAC address for each IP address
1491 * associated with the virtual router.
1494 carp_send_arp(struct carp_softc *sc)
1496 const struct carp_vhaddr *vha;
1498 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1499 if (vha->vha_iaback == NULL)
1501 arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa);
1507 carp_send_na(struct carp_softc *sc)
1509 struct ifaddr_container *ifac;
1510 struct in6_addr *in6;
1511 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1513 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1514 struct ifaddr *ifa = ifac->ifa;
1516 if (ifa->ifa_addr->sa_family != AF_INET6)
1519 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1520 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1521 ND_NA_FLAG_OVERRIDE, 1, NULL);
1522 DELAY(1000); /* XXX */
1527 static __inline const struct carp_vhaddr *
1528 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1530 struct carp_vhaddr *vha;
1532 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1533 if (vha->vha_iaback == NULL)
1536 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1544 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1545 const struct in_addr *isaddr, uint8_t **enaddr)
1547 const struct carp_softc *vh;
1548 int index, count = 0;
1551 * XXX proof of concept implementation.
1552 * We use the source ip to decide which virtual host should
1553 * handle the request. If we're master of that virtual host,
1554 * then we respond, otherwise, just drop the arp packet on
1558 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1559 if (!CARP_IS_RUNNING(&vh->sc_if))
1562 if (carp_find_addr(vh, itaddr) != NULL)
1568 /* this should be a hash, like pf_hash() */
1569 index = ntohl(isaddr->s_addr) % count;
1572 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1573 if (!CARP_IS_RUNNING(&vh->sc_if))
1576 if (carp_find_addr(vh, itaddr) == NULL)
1579 if (count == index) {
1580 if (vh->sc_state == MASTER) {
1581 *enaddr = IF_LLADDR(&vh->sc_if);
1594 carp_iamatch(const struct in_ifaddr *ia)
1596 const struct carp_softc *sc = ia->ia_ifp->if_softc;
1598 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1599 ("not in netisr0"));
1602 if (carp_opts[CARPCTL_ARPBALANCE])
1603 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1606 if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER)
1614 carp_iamatch6(void *v, struct in6_addr *taddr)
1617 struct carp_if *cif = v;
1618 struct carp_softc *vh;
1620 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1621 struct ifaddr_container *ifac;
1623 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1625 struct ifaddr *ifa = ifac->ifa;
1627 if (IN6_ARE_ADDR_EQUAL(taddr,
1628 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1629 CARP_IS_RUNNING(&vh->sc_if) &&
1630 vh->sc_state == MASTER) {
1640 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1644 struct carp_if *cif = v;
1645 struct carp_softc *sc;
1647 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1648 struct ifaddr_container *ifac;
1650 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1652 struct ifaddr *ifa = ifac->ifa;
1654 if (IN6_ARE_ADDR_EQUAL(taddr,
1655 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1656 CARP_IS_RUNNING(&sc->sc_if)) {
1657 struct ifnet *ifp = &sc->sc_if;
1659 mtag = m_tag_get(PACKET_TAG_CARP,
1660 sizeof(struct ifnet *), MB_DONTWAIT);
1662 /* better a bit than nothing */
1663 return (IF_LLADDR(ifp));
1665 bcopy(&ifp, (caddr_t)(mtag + 1),
1666 sizeof(struct ifnet *));
1667 m_tag_prepend(m, mtag);
1669 return (IF_LLADDR(ifp));
1678 static struct ifnet *
1679 carp_forus(struct carp_if *cif, const uint8_t *dhost)
1681 struct carp_softc_container *scc;
1683 if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0)
1686 TAILQ_FOREACH(scc, cif, scc_link) {
1687 struct carp_softc *sc = scc->scc_softc;
1688 struct ifnet *ifp = &sc->sc_if;
1690 if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER &&
1691 !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN))
1698 carp_master_down_timeout(void *xsc)
1700 struct carp_softc *sc = xsc;
1701 struct netmsg_carp *cmsg = &sc->sc_md_msg;
1703 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d",
1704 __func__, mycpuid));
1707 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1708 lwkt_sendmsg(netisr_cpuport(0), &cmsg->base.lmsg);
1713 carp_master_down_timeout_dispatch(netmsg_t msg)
1715 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1716 struct carp_softc *sc = cmsg->nc_softc;
1720 lwkt_replymsg(&cmsg->base.lmsg, 0);
1723 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1724 sc->sc_if.if_xname);
1725 carp_master_down(sc);
1729 carp_master_down(struct carp_softc *sc)
1731 switch (sc->sc_state) {
1733 kprintf("%s: master_down event in INIT state\n",
1734 sc->sc_if.if_xname);
1741 carp_set_state(sc, MASTER);
1748 carp_setroute(sc, RTM_ADD);
1754 * When in backup state, af indicates whether to reset the master down timer
1755 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1758 carp_setrun(struct carp_softc *sc, sa_family_t af)
1760 struct ifnet *cifp = &sc->sc_if;
1763 if (sc->sc_carpdev == NULL) {
1764 carp_set_state(sc, INIT);
1768 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1769 (sc->sc_naddrs || sc->sc_naddrs6)) {
1772 carp_setroute(sc, RTM_DELETE);
1776 switch (sc->sc_state) {
1778 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1784 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1786 carp_set_state(sc, MASTER);
1787 carp_setroute(sc, RTM_ADD);
1789 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1790 carp_set_state(sc, BACKUP);
1791 carp_setroute(sc, RTM_DELETE);
1797 callout_stop(&sc->sc_ad_tmo);
1798 tv.tv_sec = 3 * sc->sc_advbase;
1799 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1803 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1804 carp_master_down_timeout, sc);
1809 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1810 carp_master_down_timeout, sc);
1815 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1816 carp_master_down_timeout, sc);
1818 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1819 carp_master_down_timeout, sc);
1825 tv.tv_sec = sc->sc_advbase;
1826 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1827 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1828 carp_send_ad_timeout, sc);
1834 carp_multicast_cleanup(struct carp_softc *sc)
1836 struct ip_moptions *imo = &sc->sc_imo;
1838 if (imo->imo_num_memberships == 0)
1840 KKASSERT(imo->imo_num_memberships == 1);
1842 in_delmulti(imo->imo_membership[0]);
1843 imo->imo_membership[0] = NULL;
1844 imo->imo_num_memberships = 0;
1845 imo->imo_multicast_ifp = NULL;
1850 carp_multicast6_cleanup(struct carp_softc *sc)
1852 struct ip6_moptions *im6o = &sc->sc_im6o;
1854 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1855 struct in6_multi_mship *imm =
1856 LIST_FIRST(&im6o->im6o_memberships);
1858 LIST_REMOVE(imm, i6mm_chain);
1859 in6_leavegroup(imm);
1861 im6o->im6o_multicast_ifp = NULL;
1866 carp_ioctl_getvhaddr_dispatch(netmsg_t msg)
1868 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1869 struct carp_softc *sc = cmsg->nc_softc;
1870 const struct carp_vhaddr *vha;
1871 struct ifcarpvhaddr *carpa, *carpa0;
1872 int count, len, error = 0;
1875 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1878 if (cmsg->nc_datalen == 0) {
1879 cmsg->nc_datalen = count * sizeof(*carpa);
1881 } else if (count == 0 || cmsg->nc_datalen < sizeof(*carpa)) {
1882 cmsg->nc_datalen = 0;
1885 len = min(cmsg->nc_datalen, sizeof(*carpa) * count);
1886 KKASSERT(len >= sizeof(*carpa));
1888 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1889 if (carpa == NULL) {
1895 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1896 if (len < sizeof(*carpa))
1899 carpa->carpa_flags = vha->vha_flags;
1900 carpa->carpa_addr.sin_family = AF_INET;
1901 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1903 carpa->carpa_baddr.sin_family = AF_INET;
1904 if (vha->vha_iaback == NULL) {
1905 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1907 carpa->carpa_baddr.sin_addr =
1908 vha->vha_iaback->ia_addr.sin_addr;
1913 len -= sizeof(*carpa);
1915 cmsg->nc_datalen = sizeof(*carpa) * count;
1916 KKASSERT(cmsg->nc_datalen > 0);
1918 cmsg->nc_data = carpa0;
1921 lwkt_replymsg(&cmsg->base.lmsg, error);
1925 carp_ioctl_getvhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1927 struct ifnet *ifp = &sc->arpcom.ac_if;
1928 struct netmsg_carp cmsg;
1931 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1932 ifnet_deserialize_all(ifp);
1934 bzero(&cmsg, sizeof(cmsg));
1935 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
1936 carp_ioctl_getvhaddr_dispatch);
1938 cmsg.nc_datalen = ifd->ifd_len;
1940 error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
1943 if (cmsg.nc_data != NULL) {
1944 error = copyout(cmsg.nc_data, ifd->ifd_data,
1946 kfree(cmsg.nc_data, M_TEMP);
1948 ifd->ifd_len = cmsg.nc_datalen;
1950 KASSERT(cmsg.nc_data == NULL,
1951 ("%s temp vhaddr is alloc upon error", __func__));
1954 ifnet_serialize_all(ifp);
1959 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
1960 struct in_ifaddr *ia_del)
1963 struct in_ifaddr *ia_if;
1964 const struct in_ifaddr *ia_vha;
1965 struct in_ifaddr_container *iac;
1966 int own, ia_match_carpdev;
1968 KKASSERT(vha->vha_ia != NULL);
1969 ia_vha = vha->vha_ia;
1973 ia_match_carpdev = 0;
1974 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1975 struct in_ifaddr *ia = iac->ia;
1980 if (ia->ia_ifp->if_type == IFT_CARP)
1983 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1986 /* and, yeah, we need a multicast-capable iface too */
1987 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1990 if (ia_vha->ia_subnetmask == ia->ia_subnetmask &&
1991 ia_vha->ia_subnet == ia->ia_subnet) {
1992 if (ia_vha->ia_addr.sin_addr.s_addr ==
1993 ia->ia_addr.sin_addr.s_addr)
1995 if (ia_if == NULL) {
1997 } else if (sc->sc_carpdev != NULL &&
1998 sc->sc_carpdev == ia->ia_ifp) {
2000 if (ia_if->ia_flags & IFA_ROUTE) {
2002 * Address with prefix route
2007 ia_match_carpdev = 1;
2008 } else if (!ia_match_carpdev) {
2009 if (ia->ia_flags & IFA_ROUTE) {
2011 * Address with prefix route
2012 * is prefered over others.
2020 carp_deactivate_vhaddr(sc, vha, FALSE);
2024 ifp = ia_if->ia_ifp;
2026 /* XXX Don't allow parent iface to be changed */
2027 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
2030 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2034 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2036 struct carp_vhaddr *vha_new;
2037 struct in_ifaddr *carp_ia;
2039 struct carp_vhaddr *vha;
2042 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2043 carp_ia = ifatoia(carp_ifa);
2046 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2047 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
2050 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
2051 vha_new->vha_ia = carp_ia;
2052 carp_insert_vhaddr(sc, vha_new);
2054 if (carp_config_vhaddr(sc, vha_new, NULL) != 0) {
2056 * If the above configuration fails, it may only mean
2057 * that the new address is problematic. However, the
2058 * carp(4) interface may already have several working
2059 * addresses. Since the expected behaviour of
2060 * SIOC[AS]IFADDR is to put the NIC into working state,
2061 * we try starting the state machine manually here with
2062 * the hope that the carp(4)'s previously working
2063 * addresses still could be brought up.
2065 carp_hmac_prepare(sc);
2066 carp_set_state(sc, INIT);
2072 carp_del_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));
2088 * Remove the vhaddr from the list before deactivating
2089 * the vhaddr, so that the HMAC could be correctly
2090 * updated in carp_deactivate_vhaddr()
2092 carp_remove_vhaddr(sc, vha);
2094 carp_deactivate_vhaddr(sc, vha, FALSE);
2099 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
2101 struct carp_vhaddr *vha;
2102 struct in_ifaddr *carp_ia;
2104 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
2105 carp_ia = ifatoia(carp_ifa);
2107 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2108 KKASSERT(vha->vha_ia != NULL);
2109 if (vha->vha_ia == carp_ia)
2112 KASSERT(vha != NULL, ("no corresponding vhaddr %p", carp_ifa));
2114 /* Remove then reinsert, to keep the vhaddr list sorted */
2115 carp_remove_vhaddr(sc, vha);
2116 carp_insert_vhaddr(sc, vha);
2118 if (carp_config_vhaddr(sc, vha, NULL) != 0) {
2119 /* See the comment in carp_add_addr() */
2120 carp_hmac_prepare(sc);
2121 carp_set_state(sc, INIT);
2130 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
2133 struct carp_if *cif;
2134 struct in6_ifaddr *ia, *ia_if;
2135 struct ip6_moptions *im6o = &sc->sc_im6o;
2136 struct in6_multi_mship *imm;
2137 struct in6_addr in6;
2140 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2145 /* we have to do it by hands to check we won't match on us */
2146 ia_if = NULL; own = 0;
2147 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
2150 for (i = 0; i < 4; i++) {
2151 if ((sin6->sin6_addr.s6_addr32[i] &
2152 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
2153 (ia->ia_addr.sin6_addr.s6_addr32[i] &
2154 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
2157 /* and, yeah, we need a multicast-capable iface too */
2158 if (ia->ia_ifp != &sc->sc_if &&
2159 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
2163 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2164 &ia->ia_addr.sin6_addr))
2170 return (EADDRNOTAVAIL);
2174 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
2175 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
2176 return (EADDRNOTAVAIL);
2178 if (!sc->sc_naddrs6) {
2179 im6o->im6o_multicast_ifp = ifp;
2181 /* join CARP multicast address */
2182 bzero(&in6, sizeof(in6));
2183 in6.s6_addr16[0] = htons(0xff02);
2184 in6.s6_addr8[15] = 0x12;
2185 if (in6_setscope(&in6, ifp, NULL) != 0)
2187 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2189 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2191 /* join solicited multicast address */
2192 bzero(&in6, sizeof(in6));
2193 in6.s6_addr16[0] = htons(0xff02);
2194 in6.s6_addr32[1] = 0;
2195 in6.s6_addr32[2] = htonl(1);
2196 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
2197 in6.s6_addr8[12] = 0xff;
2198 if (in6_setscope(&in6, ifp, NULL) != 0)
2200 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2202 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2206 if (!ifp->if_carp) {
2207 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2209 if ((error = ifpromisc(ifp, 1))) {
2214 TAILQ_INIT(&cif->vhif_vrs);
2217 struct carp_softc *vr;
2220 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2221 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
2229 sc->sc_carpdev = ifp;
2232 { /* XXX prevent endless loop if already in queue */
2233 struct carp_softc *vr, *after = NULL;
2237 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2240 if (vr->sc_vhid < sc->sc_vhid)
2245 /* We're trying to keep things in order */
2247 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2249 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2263 /* clean up multicast memberships */
2264 if (!sc->sc_naddrs6) {
2265 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2266 imm = LIST_FIRST(&im6o->im6o_memberships);
2267 LIST_REMOVE(imm, i6mm_chain);
2268 in6_leavegroup(imm);
2275 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
2279 if (!--sc->sc_naddrs6) {
2280 struct carp_if *cif = sc->sc_carpdev->if_carp;
2281 struct ip6_moptions *im6o = &sc->sc_im6o;
2283 callout_stop(&sc->sc_ad_tmo);
2285 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2286 struct in6_multi_mship *imm =
2287 LIST_FIRST(&im6o->im6o_memberships);
2289 LIST_REMOVE(imm, i6mm_chain);
2290 in6_leavegroup(imm);
2292 im6o->im6o_multicast_ifp = NULL;
2294 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
2295 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
2296 sc->sc_carpdev->if_carp = NULL;
2297 kfree(cif, M_IFADDR);
2308 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
2310 struct carp_softc *sc = ifp->if_softc;
2311 struct ifreq *ifr = (struct ifreq *)addr;
2312 struct ifdrv *ifd = (struct ifdrv *)addr;
2315 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2319 if (ifp->if_flags & IFF_UP) {
2320 if ((ifp->if_flags & IFF_RUNNING) == 0)
2322 } else if (ifp->if_flags & IFF_RUNNING) {
2323 carp_ioctl_stop(sc);
2328 carp_ioctl_ifcap(sc, ifr->ifr_reqcap);
2332 error = carp_ioctl_setvh(sc, ifr->ifr_data, cr);
2336 error = carp_ioctl_getvh(sc, ifr->ifr_data, cr);
2340 switch (ifd->ifd_cmd) {
2342 error = carp_ioctl_getdevname(sc, ifd);
2346 error = carp_ioctl_getvhaddr(sc, ifd);
2356 error = ether_ioctl(ifp, cmd, addr);
2364 carp_ioctl_stop_dispatch(netmsg_t msg)
2366 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2367 struct carp_softc *sc = cmsg->nc_softc;
2369 carp_stop(sc, FALSE);
2370 lwkt_replymsg(&cmsg->base.lmsg, 0);
2374 carp_ioctl_stop(struct carp_softc *sc)
2376 struct ifnet *ifp = &sc->arpcom.ac_if;
2377 struct netmsg_carp cmsg;
2379 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2381 ifnet_deserialize_all(ifp);
2383 bzero(&cmsg, sizeof(cmsg));
2384 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2385 carp_ioctl_stop_dispatch);
2388 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2390 ifnet_serialize_all(ifp);
2394 carp_ioctl_setvh_dispatch(netmsg_t msg)
2396 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2397 struct carp_softc *sc = cmsg->nc_softc;
2398 struct ifnet *ifp = &sc->arpcom.ac_if;
2399 const struct carpreq *carpr = cmsg->nc_data;
2403 if ((ifp->if_flags & IFF_RUNNING) &&
2404 sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) {
2405 switch (carpr->carpr_state) {
2407 callout_stop(&sc->sc_ad_tmo);
2408 carp_set_state(sc, BACKUP);
2410 carp_setroute(sc, RTM_DELETE);
2414 carp_master_down(sc);
2421 if (carpr->carpr_vhid > 0) {
2422 if (carpr->carpr_vhid > 255) {
2426 if (sc->sc_carpdev) {
2427 struct carp_if *cif = sc->sc_carpdev->if_carp;
2428 struct carp_softc_container *scc;
2430 TAILQ_FOREACH(scc, cif, scc_link) {
2431 struct carp_softc *vr = scc->scc_softc;
2434 vr->sc_vhid == carpr->carpr_vhid) {
2440 sc->sc_vhid = carpr->carpr_vhid;
2442 IF_LLADDR(ifp)[5] = sc->sc_vhid;
2443 bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr,
2448 if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) {
2449 if (carpr->carpr_advskew >= 255) {
2453 if (carpr->carpr_advbase > 255) {
2457 sc->sc_advbase = carpr->carpr_advbase;
2458 sc->sc_advskew = carpr->carpr_advskew;
2461 bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key));
2469 carp_hmac_prepare(sc);
2471 lwkt_replymsg(&cmsg->base.lmsg, error);
2475 carp_ioctl_setvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2477 struct ifnet *ifp = &sc->arpcom.ac_if;
2478 struct netmsg_carp cmsg;
2479 struct carpreq carpr;
2482 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2483 ifnet_deserialize_all(ifp);
2485 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2489 error = copyin(udata, &carpr, sizeof(carpr));
2493 bzero(&cmsg, sizeof(cmsg));
2494 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2495 carp_ioctl_setvh_dispatch);
2497 cmsg.nc_data = &carpr;
2499 error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2502 ifnet_serialize_all(ifp);
2507 carp_ioctl_ifcap_dispatch(netmsg_t msg)
2509 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2510 struct carp_softc *sc = cmsg->nc_softc;
2511 struct ifnet *ifp = &sc->arpcom.ac_if;
2512 int reqcap = *((const int *)(cmsg->nc_data));
2515 mask = reqcap ^ ifp->if_capenable;
2516 if (mask & IFCAP_TXCSUM) {
2517 ifp->if_capenable ^= IFCAP_TXCSUM;
2518 if ((ifp->if_capenable & IFCAP_TXCSUM) &&
2519 sc->sc_carpdev != NULL) {
2521 (sc->sc_carpdev->if_hwassist &
2522 (CSUM_IP | CSUM_UDP | CSUM_TCP));
2524 ifp->if_hwassist &= ~(CSUM_IP | CSUM_UDP | CSUM_TCP);
2527 if (mask & IFCAP_TSO) {
2528 ifp->if_capenable ^= IFCAP_TSO;
2529 if ((ifp->if_capenable & IFCAP_TSO) &&
2530 sc->sc_carpdev != NULL) {
2532 (sc->sc_carpdev->if_hwassist & CSUM_TSO);
2534 ifp->if_hwassist &= ~CSUM_TSO;
2538 lwkt_replymsg(&cmsg->base.lmsg, 0);
2542 carp_ioctl_ifcap(struct carp_softc *sc, int reqcap)
2544 struct ifnet *ifp = &sc->arpcom.ac_if;
2545 struct netmsg_carp cmsg;
2547 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2548 ifnet_deserialize_all(ifp);
2550 bzero(&cmsg, sizeof(cmsg));
2551 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2552 carp_ioctl_ifcap_dispatch);
2554 cmsg.nc_data = &reqcap;
2556 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2558 ifnet_serialize_all(ifp);
2562 carp_ioctl_getvh_dispatch(netmsg_t msg)
2564 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2565 struct carp_softc *sc = cmsg->nc_softc;
2566 struct carpreq *carpr = cmsg->nc_data;
2568 carpr->carpr_state = sc->sc_state;
2569 carpr->carpr_vhid = sc->sc_vhid;
2570 carpr->carpr_advbase = sc->sc_advbase;
2571 carpr->carpr_advskew = sc->sc_advskew;
2572 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
2574 lwkt_replymsg(&cmsg->base.lmsg, 0);
2578 carp_ioctl_getvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2580 struct ifnet *ifp = &sc->arpcom.ac_if;
2581 struct netmsg_carp cmsg;
2582 struct carpreq carpr;
2585 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_ioctl_getvh_dispatch);
2592 cmsg.nc_data = &carpr;
2594 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2596 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2598 bzero(carpr.carpr_key, sizeof(carpr.carpr_key));
2600 error = copyout(&carpr, udata, sizeof(carpr));
2602 ifnet_serialize_all(ifp);
2607 carp_ioctl_getdevname_dispatch(netmsg_t msg)
2609 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2610 struct carp_softc *sc = cmsg->nc_softc;
2611 char *devname = cmsg->nc_data;
2613 bzero(devname, IFNAMSIZ);
2614 if (sc->sc_carpdev != NULL)
2615 strlcpy(devname, sc->sc_carpdev->if_xname, IFNAMSIZ);
2617 lwkt_replymsg(&cmsg->base.lmsg, 0);
2621 carp_ioctl_getdevname(struct carp_softc *sc, struct ifdrv *ifd)
2623 struct ifnet *ifp = &sc->arpcom.ac_if;
2624 struct netmsg_carp cmsg;
2625 char devname[IFNAMSIZ];
2628 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2630 if (ifd->ifd_len != sizeof(devname))
2633 ifnet_deserialize_all(ifp);
2635 bzero(&cmsg, sizeof(cmsg));
2636 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2637 carp_ioctl_getdevname_dispatch);
2639 cmsg.nc_data = devname;
2641 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2643 error = copyout(devname, ifd->ifd_data, sizeof(devname));
2645 ifnet_serialize_all(ifp);
2650 carp_init_dispatch(netmsg_t msg)
2652 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2653 struct carp_softc *sc = cmsg->nc_softc;
2655 sc->sc_if.if_flags |= IFF_RUNNING;
2656 carp_hmac_prepare(sc);
2657 carp_set_state(sc, INIT);
2660 lwkt_replymsg(&cmsg->base.lmsg, 0);
2664 carp_init(void *xsc)
2666 struct carp_softc *sc = xsc;
2667 struct ifnet *ifp = &sc->arpcom.ac_if;
2668 struct netmsg_carp cmsg;
2670 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2672 ifnet_deserialize_all(ifp);
2674 bzero(&cmsg, sizeof(cmsg));
2675 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2676 carp_init_dispatch);
2679 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0);
2681 ifnet_serialize_all(ifp);
2685 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2688 struct carp_softc *sc = ifp->if_softc;
2689 struct ifnet *carpdev;
2692 carpdev = sc->sc_carpdev;
2693 if (carpdev != NULL) {
2694 if (m->m_flags & M_MCAST)
2695 IFNET_STAT_INC(ifp, omcasts, 1);
2696 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + ETHER_HDR_LEN);
2697 IFNET_STAT_INC(ifp, opackets, 1);
2701 * CARP's ifp is passed to backing device's
2704 carpdev->if_output(ifp, m, dst, rt);
2706 IFNET_STAT_INC(ifp, oerrors, 1);
2708 error = ENETUNREACH;
2714 * Start output on carp interface. This function should never be called.
2717 carp_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
2719 panic("%s: start called", ifp->if_xname);
2723 carp_set_state(struct carp_softc *sc, int state)
2725 struct ifnet *cifp = &sc->sc_if;
2727 if (sc->sc_state == state)
2729 sc->sc_state = state;
2731 switch (sc->sc_state) {
2733 cifp->if_link_state = LINK_STATE_DOWN;
2737 cifp->if_link_state = LINK_STATE_UP;
2741 cifp->if_link_state = LINK_STATE_UNKNOWN;
2748 carp_group_demote_adj(struct ifnet *ifp, int adj)
2750 struct ifg_list *ifgl;
2753 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2754 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2756 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2763 if (adj > 0 && *dm == 1)
2765 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2766 ifgl->ifgl_group->ifg_group, *dm);
2772 carp_carpdev_state(void *v)
2774 struct carp_if *cif = v;
2775 struct carp_softc *sc;
2777 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2782 carp_sc_state(struct carp_softc *sc)
2784 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2785 callout_stop(&sc->sc_ad_tmo);
2786 callout_stop(&sc->sc_md_tmo);
2787 callout_stop(&sc->sc_md6_tmo);
2788 carp_set_state(sc, INIT);
2790 if (!sc->sc_suppress) {
2791 carp_suppress_preempt++;
2792 if (carp_suppress_preempt == 1)
2795 sc->sc_suppress = 1;
2797 carp_set_state(sc, INIT);
2799 if (sc->sc_suppress)
2800 carp_suppress_preempt--;
2801 sc->sc_suppress = 0;
2807 carp_stop(struct carp_softc *sc, boolean_t detach)
2809 sc->sc_if.if_flags &= ~IFF_RUNNING;
2811 callout_stop(&sc->sc_ad_tmo);
2812 callout_stop(&sc->sc_md_tmo);
2813 callout_stop(&sc->sc_md6_tmo);
2815 if (!detach && sc->sc_state == MASTER)
2818 if (sc->sc_suppress)
2819 carp_suppress_preempt--;
2820 sc->sc_suppress = 0;
2822 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2823 carp_suppress_preempt--;
2824 sc->sc_sendad_errors = 0;
2825 sc->sc_sendad_success = 0;
2827 carp_set_state(sc, INIT);
2832 carp_suspend(struct carp_softc *sc, boolean_t detach)
2834 struct ifnet *cifp = &sc->sc_if;
2836 carp_stop(sc, detach);
2838 /* Retain the running state, if we are not dead yet */
2839 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2840 cifp->if_flags |= IFF_RUNNING;
2844 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2845 struct ifnet *ifp, struct in_ifaddr *ia_if, int own)
2847 struct ip_moptions *imo = &sc->sc_imo;
2848 struct carp_if *ocif = ifp->if_carp;
2851 KKASSERT(vha->vha_ia != NULL);
2853 KASSERT(ia_if != NULL, ("NULL backing address"));
2854 KASSERT(vha->vha_iaback == NULL, ("%p is already activated", vha));
2855 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2856 ("inactive vhaddr %p is the address owner", vha));
2858 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2859 ("%s is already on %s", sc->sc_if.if_xname,
2860 sc->sc_carpdev->if_xname));
2863 KASSERT(sc->sc_carpdev == NULL,
2864 ("%s is already on %s", sc->sc_if.if_xname,
2865 sc->sc_carpdev->if_xname));
2867 error = ifpromisc(ifp, 1);
2871 struct carp_softc_container *scc;
2873 TAILQ_FOREACH(scc, ocif, scc_link) {
2874 struct carp_softc *vr = scc->scc_softc;
2876 if (vr != sc && vr->sc_vhid == sc->sc_vhid)
2881 ifp->if_carp = carp_if_insert(ocif, sc);
2882 KASSERT(ifp->if_carp != NULL, ("%s carp_if_insert failed", __func__));
2885 sc->sc_carpdev = ifp;
2886 sc->arpcom.ac_if.if_hwassist = 0;
2887 if (sc->arpcom.ac_if.if_capenable & IFCAP_TXCSUM) {
2888 sc->arpcom.ac_if.if_hwassist |=
2889 (ifp->if_hwassist & (CSUM_IP | CSUM_UDP | CSUM_TCP));
2891 if (sc->arpcom.ac_if.if_capenable & IFCAP_TSO)
2892 sc->arpcom.ac_if.if_hwassist |= (ifp->if_hwassist & CSUM_TSO);
2895 * Make sure that all protocol threads see the sc_carpdev and
2898 netmsg_service_sync();
2900 if (ocif != NULL && ifp->if_carp != ocif) {
2902 * The old carp list could be safely free now,
2903 * since no one can access it.
2908 vha->vha_iaback = ia_if;
2912 vha->vha_flags |= CARP_VHAF_OWNER;
2914 /* XXX save user configured advskew? */
2918 carp_addroute_vhaddr(sc, vha);
2921 * Join the multicast group only after the backing interface
2922 * has been hooked with the CARP interface.
2924 KASSERT(imo->imo_multicast_ifp == NULL ||
2925 imo->imo_multicast_ifp == &sc->sc_if,
2926 ("%s didn't leave mcast group on %s",
2927 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2929 if (imo->imo_num_memberships == 0) {
2930 struct in_addr addr;
2932 addr.s_addr = htonl(INADDR_CARP_GROUP);
2933 imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if);
2934 if (imo->imo_membership[0] == NULL) {
2935 carp_deactivate_vhaddr(sc, vha, FALSE);
2939 imo->imo_num_memberships++;
2940 imo->imo_multicast_ifp = &sc->sc_if;
2941 imo->imo_multicast_ttl = CARP_DFLTTL;
2942 imo->imo_multicast_loop = 0;
2945 carp_hmac_prepare(sc);
2946 carp_set_state(sc, INIT);
2952 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2953 boolean_t del_iaback)
2955 KKASSERT(vha->vha_ia != NULL);
2957 carp_hmac_prepare(sc);
2959 if (vha->vha_iaback == NULL) {
2960 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2961 ("inactive vhaddr %p is the address owner", vha));
2965 vha->vha_flags &= ~CARP_VHAF_OWNER;
2966 carp_delroute_vhaddr(sc, vha, del_iaback);
2968 KKASSERT(sc->sc_naddrs > 0);
2969 vha->vha_iaback = NULL;
2971 if (!sc->sc_naddrs) {
2972 if (sc->sc_naddrs6) {
2973 carp_multicast_cleanup(sc);
2976 carp_detach(sc, FALSE, del_iaback);
2982 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2984 struct carp_vhaddr *vha;
2985 struct in_ifaddr *ia_if;
2987 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2988 ia_if = ifatoia(ifa_if);
2991 * Test each inactive vhaddr against the newly added address.
2992 * If the newly added address could be the backing address,
2993 * then activate the matching vhaddr.
2995 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2996 const struct in_ifaddr *ia;
2999 if (vha->vha_iaback != NULL)
3003 if (ia->ia_subnetmask != ia_if->ia_subnetmask ||
3004 ia->ia_subnet != ia_if->ia_subnet)
3008 if (ia->ia_addr.sin_addr.s_addr ==
3009 ia_if->ia_addr.sin_addr.s_addr)
3012 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
3017 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
3018 struct ifaddr *ifa_if)
3020 struct carp_vhaddr *vha;
3021 struct in_ifaddr *ia_if;
3023 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
3024 ia_if = ifatoia(ifa_if);
3027 * Ad src address is deleted; set it to NULL.
3028 * Following loop will try pick up a new ad src address
3029 * if one of the vhaddr could retain its backing address.
3031 if (sc->sc_ia == ia_if)
3035 * Test each active vhaddr against the deleted address.
3036 * If the deleted address is vhaddr address's backing
3037 * address, then deactivate the vhaddr.
3039 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
3040 if (vha->vha_iaback == NULL)
3043 if (vha->vha_iaback == ia_if)
3044 carp_deactivate_vhaddr(sc, vha, TRUE);
3045 else if (sc->sc_ia == NULL)
3046 sc->sc_ia = vha->vha_iaback;
3051 carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del)
3053 struct carp_vhaddr *vha;
3055 KKASSERT(sc->sc_carpdev == NULL);
3057 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
3058 carp_config_vhaddr(sc, vha, ifatoia(ifa_del));
3062 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
3063 enum ifaddr_event event, struct ifaddr *ifa)
3065 struct carp_softc *sc;
3067 if (ifa->ifa_addr->sa_family != AF_INET)
3070 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
3071 ("not in netisr0"));
3073 if (ifp->if_type == IFT_CARP) {
3075 * Address is changed on carp(4) interface
3078 case IFADDR_EVENT_ADD:
3079 carp_add_addr(ifp->if_softc, ifa);
3082 case IFADDR_EVENT_CHANGE:
3083 carp_config_addr(ifp->if_softc, ifa);
3086 case IFADDR_EVENT_DELETE:
3087 carp_del_addr(ifp->if_softc, ifa);
3094 * Address is changed on non-carp(4) interface
3096 if ((ifp->if_flags & IFF_MULTICAST) == 0)
3099 LIST_FOREACH(sc, &carpif_list, sc_next) {
3100 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
3101 /* Not the parent iface; skip */
3106 case IFADDR_EVENT_ADD:
3107 carp_link_addrs(sc, ifp, ifa);
3110 case IFADDR_EVENT_DELETE:
3111 if (sc->sc_carpdev != NULL) {
3112 carp_unlink_addrs(sc, ifp, ifa);
3113 if (sc->sc_carpdev == NULL) {
3115 * We no longer have the parent
3116 * interface, however, certain
3117 * virtual addresses, which are
3118 * not used because they can't
3119 * match the previous parent
3120 * interface's addresses, may now
3121 * match different interface's
3124 carp_update_addrs(sc, ifa);
3128 * The carp(4) interface didn't have a
3129 * parent iface, so it is not possible
3130 * that it will contain any address to
3136 case IFADDR_EVENT_CHANGE:
3137 if (sc->sc_carpdev == NULL) {
3139 * The carp(4) interface didn't have a
3140 * parent iface, so it is not possible
3141 * that it will contain any address to
3144 carp_link_addrs(sc, ifp, ifa);
3147 * First try breaking tie with the old
3148 * address. Then see whether we could
3149 * link certain vhaddr to the new address.
3150 * If that fails, i.e. carpdev is NULL,
3151 * we try a global update.
3153 * NOTE: The above order is critical.
3155 carp_unlink_addrs(sc, ifp, ifa);
3156 carp_link_addrs(sc, ifp, ifa);
3157 if (sc->sc_carpdev == NULL) {
3159 * See the comment in the above
3160 * IFADDR_EVENT_DELETE block.
3162 carp_update_addrs(sc, NULL);
3171 carp_proto_ctlinput(netmsg_t msg)
3173 int cmd = msg->ctlinput.nm_cmd;
3174 struct sockaddr *sa = msg->ctlinput.nm_arg;
3175 struct in_ifaddr_container *iac;
3177 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
3178 struct in_ifaddr *ia = iac->ia;
3179 struct ifnet *ifp = ia->ia_ifp;
3181 if (ifp->if_type == IFT_CARP)
3184 if (ia->ia_ifa.ifa_addr == sa) {
3185 if (cmd == PRC_IFDOWN) {
3186 carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE,
3188 } else if (cmd == PRC_IFUP) {
3189 carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD,
3196 lwkt_replymsg(&msg->lmsg, 0);
3200 carp_parent(struct ifnet *cifp)
3202 struct carp_softc *sc;
3204 KKASSERT(cifp->if_type == IFT_CARP);
3205 sc = cifp->if_softc;
3207 return sc->sc_carpdev;
3210 #define rtinitflags(x) \
3211 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
3215 carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
3217 struct in_ifaddr *ia, *iaback;
3219 if (sc->sc_state != MASTER)
3223 KKASSERT(ia != NULL);
3225 iaback = vha->vha_iaback;
3226 KKASSERT(iaback != NULL);
3228 return rtchange(&iaback->ia_ifa, &ia->ia_ifa);
3232 carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
3233 boolean_t del_iaback)
3235 struct in_ifaddr *ia, *iaback;
3238 KKASSERT(ia != NULL);
3240 iaback = vha->vha_iaback;
3241 KKASSERT(iaback != NULL);
3243 if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) {
3244 rtchange(&ia->ia_ifa, &iaback->ia_ifa);
3248 rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia));
3249 in_ifadown_force(&ia->ia_ifa, 1);
3250 ia->ia_flags &= ~IFA_ROUTE;
3254 carp_modevent(module_t mod, int type, void *data)
3258 LIST_INIT(&carpif_list);
3259 carp_ifdetach_event =
3260 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
3261 EVENTHANDLER_PRI_ANY);
3263 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
3264 EVENTHANDLER_PRI_FIRST);
3265 if_clone_attach(&carp_cloner);
3269 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
3270 carp_ifdetach_event);
3271 EVENTHANDLER_DEREGISTER(ifaddr_event,
3273 if_clone_detach(&carp_cloner);
3282 static moduledata_t carp_mod = {
3287 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);