2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3 * Copyright (c) 2003 Ryan McBride. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/sys/netinet/ip_carp.c,v 1.48 2007/02/02 09:39:09 glebius Exp $
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/in_cksum.h>
38 #include <sys/limits.h>
39 #include <sys/malloc.h>
41 #include <sys/msgport2.h>
45 #include <sys/sockio.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/thread.h>
51 #include <machine/stdarg.h>
52 #include <crypto/sha1.h>
55 #include <net/ethernet.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 #include <net/if_clone.h>
61 #include <net/if_var.h>
62 #include <net/ifq_var.h>
63 #include <net/netmsg2.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/if_ether.h>
75 #include <netinet/icmp6.h>
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/scope6_var.h>
79 #include <netinet6/nd6.h>
82 #include <netinet/ip_carp.h>
84 #define CARP_IFNAME "carp"
85 #define CARP_IS_RUNNING(ifp) \
86 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
89 uint32_t vha_flags; /* CARP_VHAF_ */
90 struct in_ifaddr *vha_ia; /* carp address */
91 struct in_ifaddr *vha_iaback; /* backing address */
92 TAILQ_ENTRY(carp_vhaddr) vha_link;
94 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr);
98 struct ifnet *sc_carpdev; /* parent interface */
99 struct carp_vhaddr_list sc_vha_list; /* virtual addr list */
101 const struct in_ifaddr *sc_ia; /* primary iface address v4 */
102 struct ip_moptions sc_imo;
105 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
106 struct ip6_moptions sc_im6o;
108 TAILQ_ENTRY(carp_softc) sc_list;
110 enum { INIT = 0, BACKUP, MASTER }
116 int sc_sendad_errors;
117 #define CARP_SENDAD_MAX_ERRORS 3
118 int sc_sendad_success;
119 #define CARP_SENDAD_MIN_SUCCESS 3
123 int sc_naddrs; /* actually used IPv4 vha */
125 int sc_advbase; /* seconds */
130 #define CARP_HMAC_PAD 64
131 unsigned char sc_key[CARP_KEY_LEN];
132 unsigned char sc_pad[CARP_HMAC_PAD];
135 struct callout sc_ad_tmo; /* advertisement timeout */
136 struct callout sc_md_tmo; /* master down timeout */
137 struct callout sc_md6_tmo; /* master down timeout */
139 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
142 #define sc_if arpcom.ac_if
145 TAILQ_HEAD(, carp_softc) vhif_vrs;
149 struct netmsg_base base;
150 struct ifnet *nc_carpdev;
151 struct carp_softc *nc_softc;
156 SYSCTL_DECL(_net_inet_carp);
158 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
159 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
160 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
161 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
162 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
163 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
164 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
165 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
166 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
168 static int carp_suppress_preempt = 0;
169 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
170 &carp_suppress_preempt, 0, "Preemption is suppressed");
172 static struct carpstats carpstats;
173 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
174 &carpstats, carpstats,
175 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
177 #define CARP_LOG(...) do { \
178 if (carp_opts[CARPCTL_LOG] > 0) \
179 log(LOG_INFO, __VA_ARGS__); \
182 #define CARP_DEBUG(...) do { \
183 if (carp_opts[CARPCTL_LOG] > 1) \
184 log(LOG_DEBUG, __VA_ARGS__); \
187 static struct lwkt_token carp_tok = LWKT_TOKEN_INITIALIZER(carp_token);
189 static void carp_hmac_prepare(struct carp_softc *);
190 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
192 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
194 static void carp_setroute(struct carp_softc *, int);
195 static void carp_proto_input_c(struct carp_softc *, struct mbuf *,
196 struct carp_header *, sa_family_t);
197 static int carp_clone_create(struct if_clone *, int, caddr_t);
198 static int carp_clone_destroy(struct ifnet *);
199 static void carp_detach(struct carp_softc *, int, boolean_t);
200 static void carp_prepare_ad(struct carp_softc *, struct carp_header *);
201 static void carp_send_ad_all(void);
202 static void carp_send_ad_timeout(void *);
203 static void carp_send_ad(struct carp_softc *);
204 static void carp_send_arp(struct carp_softc *);
205 static void carp_master_down_timeout(void *);
206 static void carp_master_down(struct carp_softc *);
207 static void carp_setrun(struct carp_softc *, sa_family_t);
208 static void carp_set_state(struct carp_softc *, int);
209 static struct ifnet *carp_forus(struct carp_if *, const uint8_t *);
211 static void carp_init(void *);
212 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
213 static int carp_output(struct ifnet *, struct mbuf *, struct sockaddr *,
215 static void carp_start(struct ifnet *);
216 static void carp_serialize(struct ifnet *, enum ifnet_serialize);
217 static void carp_deserialize(struct ifnet *, enum ifnet_serialize);
218 static int carp_tryserialize(struct ifnet *, enum ifnet_serialize);
220 static void carp_serialize_assert(struct ifnet *, enum ifnet_serialize,
224 static void carp_multicast_cleanup(struct carp_softc *);
225 static void carp_add_addr(struct carp_softc *, struct ifaddr *);
226 static void carp_del_addr(struct carp_softc *, struct ifaddr *);
227 static void carp_config_addr(struct carp_softc *, struct ifaddr *);
228 static void carp_link_addrs(struct carp_softc *, struct ifnet *,
230 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *,
232 static void carp_update_addrs(struct carp_softc *, struct ifaddr *);
234 static int carp_get_vhaddr(struct carp_softc *, struct ifdrv *);
235 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *,
237 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
238 struct ifnet *, struct in_ifaddr *, int);
239 static void carp_deactivate_vhaddr(struct carp_softc *,
240 struct carp_vhaddr *, boolean_t);
241 static int carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *);
242 static void carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *,
245 static void carp_sc_state(struct carp_softc *);
247 static void carp_send_na(struct carp_softc *);
249 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
250 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
252 static void carp_multicast6_cleanup(struct carp_softc *);
254 static void carp_stop(struct carp_softc *, int);
255 static void carp_suspend(struct carp_softc *, int);
256 static void carp_ioctl_stop(struct carp_softc *);
257 static int carp_ioctl_setvh(struct carp_softc *, struct carpreq *);
259 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
261 static void carp_ifdetach(void *, struct ifnet *);
263 static void carp_ifdetach_dispatch(netmsg_t);
264 static void carp_clone_destroy_dispatch(netmsg_t);
265 static void carp_init_dispatch(netmsg_t);
266 static void carp_ioctl_stop_dispatch(netmsg_t);
267 static void carp_ioctl_setvh_dispatch(netmsg_t);
269 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
271 static LIST_HEAD(, carp_softc) carpif_list;
273 static struct if_clone carp_cloner =
274 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
277 static uint8_t carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 };
279 static eventhandler_tag carp_ifdetach_event;
280 static eventhandler_tag carp_ifaddr_event;
283 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
285 struct carp_vhaddr *vha;
286 u_long new_addr, addr;
288 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
291 * Virtual address list is sorted; smaller one first
293 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
295 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
296 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
302 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
304 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
305 vha_new->vha_flags |= CARP_VHAF_ONLIST;
309 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
311 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
312 vha->vha_flags &= ~CARP_VHAF_ONLIST;
313 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
317 carp_hmac_prepare(struct carp_softc *sc)
319 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
320 uint8_t vhid = sc->sc_vhid & 0xff;
323 struct ifaddr_container *ifac;
327 struct carp_vhaddr *vha;
330 /* XXX: possible race here */
332 /* compute ipad from key */
333 bzero(sc->sc_pad, sizeof(sc->sc_pad));
334 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
335 for (i = 0; i < sizeof(sc->sc_pad); i++)
336 sc->sc_pad[i] ^= 0x36;
338 /* precompute first part of inner hash */
339 SHA1Init(&sc->sc_sha1);
340 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
341 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
342 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
343 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
345 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
346 SHA1Update(&sc->sc_sha1,
347 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
348 sizeof(struct in_addr));
352 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
353 struct ifaddr *ifa = ifac->ifa;
355 if (ifa->ifa_addr->sa_family == AF_INET6) {
356 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
357 in6_clearscope(&in6);
358 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
363 /* convert ipad to opad */
364 for (i = 0; i < sizeof(sc->sc_pad); i++)
365 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
369 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
370 unsigned char md[20])
374 /* fetch first half of inner hash */
375 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
377 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
378 SHA1Final(md, &sha1ctx);
382 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
383 SHA1Update(&sha1ctx, md, 20);
384 SHA1Final(md, &sha1ctx);
388 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
389 unsigned char md[20])
391 unsigned char md2[20];
393 carp_hmac_generate(sc, counter, md2);
394 return (bcmp(md, md2, sizeof(md2)));
398 carp_setroute(struct carp_softc *sc, int cmd)
401 struct ifaddr_container *ifac;
403 struct carp_vhaddr *vha;
405 KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD);
407 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
408 if (vha->vha_iaback == NULL)
410 if (cmd == RTM_DELETE)
411 carp_delroute_vhaddr(sc, vha, FALSE);
413 carp_addroute_vhaddr(sc, vha);
417 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
418 struct ifaddr *ifa = ifac->ifa;
420 if (ifa->ifa_addr->sa_family == AF_INET6) {
431 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
433 struct carp_softc *sc;
436 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
440 sc->sc_advbase = CARP_DFLTINTV;
441 sc->sc_vhid = -1; /* required setting */
443 sc->sc_init_counter = 1;
447 TAILQ_INIT(&sc->sc_vha_list);
450 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
453 callout_init_mp(&sc->sc_ad_tmo);
454 callout_init_mp(&sc->sc_md_tmo);
455 callout_init_mp(&sc->sc_md6_tmo);
457 if_initname(ifp, CARP_IFNAME, unit);
459 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
460 ifp->if_init = carp_init;
461 ifp->if_ioctl = carp_ioctl;
462 ifp->if_start = carp_start;
463 ifp->if_serialize = carp_serialize;
464 ifp->if_deserialize = carp_deserialize;
465 ifp->if_tryserialize = carp_tryserialize;
467 ifp->if_serialize_assert = carp_serialize_assert;
469 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
470 ifq_set_ready(&ifp->if_snd);
472 ether_ifattach(ifp, carp_etheraddr, NULL);
474 ifp->if_type = IFT_CARP;
475 ifp->if_output = carp_output;
478 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
485 carp_clone_destroy_dispatch(netmsg_t msg)
487 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
488 struct carp_softc *sc = cmsg->nc_softc;
493 carp_detach(sc, 1, FALSE);
497 lwkt_replymsg(&cmsg->base.lmsg, 0);
501 carp_clone_destroy(struct ifnet *ifp)
503 struct carp_softc *sc = ifp->if_softc;
504 struct netmsg_carp cmsg;
506 bzero(&cmsg, sizeof(cmsg));
507 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
508 carp_clone_destroy_dispatch);
511 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
514 LIST_REMOVE(sc, sc_next);
520 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active\n"));
527 carp_detach(struct carp_softc *sc, int detach, boolean_t del_iaback)
531 carp_suspend(sc, detach);
533 carp_multicast_cleanup(sc);
535 carp_multicast6_cleanup(sc);
538 if (!sc->sc_dead && detach) {
539 struct carp_vhaddr *vha;
541 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
542 carp_deactivate_vhaddr(sc, vha, del_iaback);
543 KKASSERT(sc->sc_naddrs == 0);
546 if (sc->sc_carpdev != NULL) {
547 cif = sc->sc_carpdev->if_carp;
548 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
549 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
550 ifpromisc(sc->sc_carpdev, 0);
551 sc->sc_carpdev->if_carp = NULL;
554 sc->sc_carpdev = NULL;
560 carp_ifdetach_dispatch(netmsg_t msg)
562 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
563 struct ifnet *ifp = cmsg->nc_carpdev;
564 struct carp_if *cif = ifp->if_carp;
565 struct carp_softc *sc;
569 while (ifp->if_carp &&
570 (sc = TAILQ_FIRST(&cif->vhif_vrs)) != NULL)
571 carp_detach(sc, 1, TRUE);
575 lwkt_replymsg(&cmsg->base.lmsg, 0);
578 /* Detach an interface from the carp. */
580 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
582 struct netmsg_carp cmsg;
584 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
586 bzero(&cmsg, sizeof(cmsg));
587 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
588 carp_ifdetach_dispatch);
589 cmsg.nc_carpdev = ifp;
591 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
595 * process input packet.
596 * we have rearranged checks order compared to the rfc,
597 * but it seems more efficient this way or not possible otherwise.
600 carp_proto_input(struct mbuf **mp, int *offp, int proto)
602 struct mbuf *m = *mp;
603 struct ip *ip = mtod(m, struct ip *);
604 struct ifnet *ifp = m->m_pkthdr.rcvif;
605 struct carp_header *ch;
606 struct carp_softc *sc;
614 carpstats.carps_ipackets++;
616 if (!carp_opts[CARPCTL_ALLOW]) {
621 /* Check if received on a valid carp interface */
622 if (ifp->if_type != IFT_CARP) {
623 carpstats.carps_badif++;
624 CARP_LOG("carp_proto_input: packet received on non-carp "
625 "interface: %s\n", ifp->if_xname);
630 if (!CARP_IS_RUNNING(ifp)) {
631 carpstats.carps_badif++;
632 CARP_LOG("carp_proto_input: packet received on stopped carp "
633 "interface: %s\n", ifp->if_xname);
639 if (sc->sc_carpdev == NULL) {
640 carpstats.carps_badif++;
641 CARP_LOG("carp_proto_input: packet received on defunc carp "
642 "interface: %s\n", ifp->if_xname);
647 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
648 carpstats.carps_badif++;
649 CARP_LOG("carp_proto_input: non-mcast packet on "
650 "interface: %s\n", ifp->if_xname);
655 /* Verify that the IP TTL is CARP_DFLTTL. */
656 if (ip->ip_ttl != CARP_DFLTTL) {
657 carpstats.carps_badttl++;
658 CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n",
659 ip->ip_ttl, CARP_DFLTTL, ifp->if_xname);
664 /* Minimal CARP packet size */
665 len = iphlen + sizeof(*ch);
668 * Verify that the received packet length is
669 * not less than the CARP header
671 if (m->m_pkthdr.len < len) {
672 carpstats.carps_badlen++;
673 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
679 /* Make sure that CARP header is contiguous */
680 if (len > m->m_len) {
681 m = m_pullup(m, len);
683 carpstats.carps_hdrops++;
684 CARP_LOG("carp_proto_input: m_pullup failed\n");
687 ip = mtod(m, struct ip *);
689 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
691 /* Verify the CARP checksum */
692 if (in_cksum_skip(m, len, iphlen)) {
693 carpstats.carps_badsum++;
694 CARP_LOG("carp_proto_input: checksum failed on %s\n",
699 carp_proto_input_c(sc, m, ch, AF_INET);
702 return(IPPROTO_DONE);
707 carp6_proto_input(struct mbuf **mp, int *offp, int proto)
709 struct mbuf *m = *mp;
710 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
711 struct ifnet *ifp = m->m_pkthdr.rcvif;
712 struct carp_header *ch;
713 struct carp_softc *sc;
718 carpstats.carps_ipackets6++;
720 if (!carp_opts[CARPCTL_ALLOW]) {
725 /* check if received on a valid carp interface */
726 if (ifp->if_type != IFT_CARP) {
727 carpstats.carps_badif++;
728 CARP_LOG("carp6_proto_input: packet received on non-carp "
729 "interface: %s\n", ifp->if_xname);
734 if (!CARP_IS_RUNNING(ifp)) {
735 carpstats.carps_badif++;
736 CARP_LOG("carp_proto_input: packet received on stopped carp "
737 "interface: %s\n", ifp->if_xname);
743 if (sc->sc_carpdev == NULL) {
744 carpstats.carps_badif++;
745 CARP_LOG("carp6_proto_input: packet received on defunc-carp "
746 "interface: %s\n", ifp->if_xname);
751 /* verify that the IP TTL is 255 */
752 if (ip6->ip6_hlim != CARP_DFLTTL) {
753 carpstats.carps_badttl++;
754 CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n",
755 ip6->ip6_hlim, ifp->if_xname);
760 /* verify that we have a complete carp packet */
762 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
764 carpstats.carps_badlen++;
765 CARP_LOG("carp6_proto_input: packet size %u too small\n", len);
769 /* verify the CARP checksum */
770 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
771 carpstats.carps_badsum++;
772 CARP_LOG("carp6_proto_input: checksum failed, on %s\n",
778 carp_proto_input_c(sc, m, ch, AF_INET6);
781 return (IPPROTO_DONE);
786 carp_proto_input_c(struct carp_softc *sc, struct mbuf *m,
787 struct carp_header *ch, sa_family_t af)
790 uint64_t tmp_counter;
791 struct timeval sc_tv, ch_tv;
793 if (sc->sc_vhid != ch->carp_vhid) {
795 * CARP uses multicast, however, multicast packets
796 * are tapped to all CARP interfaces on the physical
797 * interface receiving the CARP packets, so we don't
798 * update any stats here.
805 /* verify the CARP version. */
806 if (ch->carp_version != CARP_VERSION) {
807 carpstats.carps_badver++;
808 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
814 /* verify the hash */
815 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
816 carpstats.carps_badauth++;
817 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
822 tmp_counter = ntohl(ch->carp_counter[0]);
823 tmp_counter = tmp_counter<<32;
824 tmp_counter += ntohl(ch->carp_counter[1]);
826 /* XXX Replay protection goes here */
828 sc->sc_init_counter = 0;
829 sc->sc_counter = tmp_counter;
831 sc_tv.tv_sec = sc->sc_advbase;
832 if (carp_suppress_preempt && sc->sc_advskew < 240)
833 sc_tv.tv_usec = 240 * 1000000 / 256;
835 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
836 ch_tv.tv_sec = ch->carp_advbase;
837 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
839 switch (sc->sc_state) {
845 * If we receive an advertisement from a master who's going to
846 * be more frequent than us, go into BACKUP state.
848 if (timevalcmp(&sc_tv, &ch_tv, >) ||
849 timevalcmp(&sc_tv, &ch_tv, ==)) {
850 callout_stop(&sc->sc_ad_tmo);
851 CARP_DEBUG("%s: MASTER -> BACKUP "
852 "(more frequent advertisement received)\n",
854 carp_set_state(sc, BACKUP);
856 carp_setroute(sc, RTM_DELETE);
862 * If we're pre-empting masters who advertise slower than us,
863 * and this one claims to be slower, treat him as down.
865 if (carp_opts[CARPCTL_PREEMPT] &&
866 timevalcmp(&sc_tv, &ch_tv, <)) {
867 CARP_DEBUG("%s: BACKUP -> MASTER "
868 "(preempting a slower master)\n", cifp->if_xname);
869 carp_master_down(sc);
874 * If the master is going to advertise at such a low frequency
875 * that he's guaranteed to time out, we'd might as well just
876 * treat him as timed out now.
878 sc_tv.tv_sec = sc->sc_advbase * 3;
879 if (timevalcmp(&sc_tv, &ch_tv, <)) {
880 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
882 carp_master_down(sc);
887 * Otherwise, we reset the counter and wait for the next
897 carp_input(void *v, struct mbuf *m)
899 struct carp_if *cif = v;
900 struct ether_header *eh;
901 struct carp_softc *sc;
904 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
906 eh = mtod(m, struct ether_header *);
908 ifp = carp_forus(cif, eh->ether_dhost);
910 ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF);
914 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
918 * XXX Should really check the list of multicast addresses
919 * for each CARP interface _before_ copying.
921 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
924 if ((sc->sc_if.if_flags & IFF_UP) == 0)
927 m0 = m_dup(m, MB_DONTWAIT);
931 ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF);
937 carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch)
939 if (sc->sc_init_counter) {
940 /* this could also be seconds since unix epoch */
941 sc->sc_counter = karc4random();
942 sc->sc_counter = sc->sc_counter << 32;
943 sc->sc_counter += karc4random();
948 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
949 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
951 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
955 carp_send_ad_all(void)
957 struct carp_softc *sc;
959 LIST_FOREACH(sc, &carpif_list, sc_next) {
960 if (sc->sc_carpdev == NULL)
963 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
969 carp_send_ad_timeout(void *xsc)
977 carp_send_ad(struct carp_softc *sc)
979 struct ifnet *cifp = &sc->sc_if;
980 struct carp_header ch;
982 struct carp_header *ch_ptr;
984 int len, advbase, advskew;
986 if (!CARP_IS_RUNNING(cifp)) {
991 advbase = sc->sc_advbase;
992 if (!carp_suppress_preempt || sc->sc_advskew > 240)
993 advskew = sc->sc_advskew;
997 tv.tv_usec = advskew * 1000000 / 256;
1000 ch.carp_version = CARP_VERSION;
1001 ch.carp_type = CARP_ADVERTISEMENT;
1002 ch.carp_vhid = sc->sc_vhid;
1003 ch.carp_advbase = advbase;
1004 ch.carp_advskew = advskew;
1005 ch.carp_authlen = 7; /* XXX DEFINE */
1006 ch.carp_pad1 = 0; /* must be zero */
1010 if (sc->sc_ia != NULL) {
1013 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1016 carpstats.carps_onomem++;
1017 /* XXX maybe less ? */
1018 if (advbase != 255 || advskew != 255)
1019 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1020 carp_send_ad_timeout, sc);
1023 len = sizeof(*ip) + sizeof(ch);
1024 m->m_pkthdr.len = len;
1025 m->m_pkthdr.rcvif = NULL;
1027 MH_ALIGN(m, m->m_len);
1028 m->m_flags |= M_MCAST;
1029 ip = mtod(m, struct ip *);
1030 ip->ip_v = IPVERSION;
1031 ip->ip_hl = sizeof(*ip) >> 2;
1032 ip->ip_tos = IPTOS_LOWDELAY;
1034 ip->ip_id = ip_newid();
1036 ip->ip_ttl = CARP_DFLTTL;
1037 ip->ip_p = IPPROTO_CARP;
1039 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
1040 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
1042 ch_ptr = (struct carp_header *)(&ip[1]);
1043 bcopy(&ch, ch_ptr, sizeof(ch));
1044 carp_prepare_ad(sc, ch_ptr);
1045 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
1047 getmicrotime(&cifp->if_lastchange);
1048 cifp->if_opackets++;
1049 cifp->if_obytes += len;
1050 carpstats.carps_opackets++;
1052 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
1054 if (sc->sc_sendad_errors < INT_MAX)
1055 sc->sc_sendad_errors++;
1056 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1057 carp_suppress_preempt++;
1058 if (carp_suppress_preempt == 1) {
1062 sc->sc_sendad_success = 0;
1064 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1065 if (++sc->sc_sendad_success >=
1066 CARP_SENDAD_MIN_SUCCESS) {
1067 carp_suppress_preempt--;
1068 sc->sc_sendad_errors = 0;
1071 sc->sc_sendad_errors = 0;
1078 struct ip6_hdr *ip6;
1080 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1083 carpstats.carps_onomem++;
1084 /* XXX maybe less ? */
1085 if (advbase != 255 || advskew != 255)
1086 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1087 carp_send_ad_timeout, sc);
1090 len = sizeof(*ip6) + sizeof(ch);
1091 m->m_pkthdr.len = len;
1092 m->m_pkthdr.rcvif = NULL;
1094 MH_ALIGN(m, m->m_len);
1095 m->m_flags |= M_MCAST;
1096 ip6 = mtod(m, struct ip6_hdr *);
1097 bzero(ip6, sizeof(*ip6));
1098 ip6->ip6_vfc |= IPV6_VERSION;
1099 ip6->ip6_hlim = CARP_DFLTTL;
1100 ip6->ip6_nxt = IPPROTO_CARP;
1101 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1102 sizeof(struct in6_addr));
1103 /* set the multicast destination */
1105 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1106 ip6->ip6_dst.s6_addr8[15] = 0x12;
1107 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1110 CARP_LOG("%s: in6_setscope failed\n", __func__);
1114 ch_ptr = (struct carp_header *)(&ip6[1]);
1115 bcopy(&ch, ch_ptr, sizeof(ch));
1116 carp_prepare_ad(sc, ch_ptr);
1117 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
1119 getmicrotime(&cifp->if_lastchange);
1120 cifp->if_opackets++;
1121 cifp->if_obytes += len;
1122 carpstats.carps_opackets6++;
1124 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1126 if (sc->sc_sendad_errors < INT_MAX)
1127 sc->sc_sendad_errors++;
1128 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1129 carp_suppress_preempt++;
1130 if (carp_suppress_preempt == 1) {
1134 sc->sc_sendad_success = 0;
1136 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1137 if (++sc->sc_sendad_success >=
1138 CARP_SENDAD_MIN_SUCCESS) {
1139 carp_suppress_preempt--;
1140 sc->sc_sendad_errors = 0;
1143 sc->sc_sendad_errors = 0;
1149 if (advbase != 255 || advskew != 255)
1150 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1151 carp_send_ad_timeout, sc);
1155 * Broadcast a gratuitous ARP request containing
1156 * the virtual router MAC address for each IP address
1157 * associated with the virtual router.
1160 carp_send_arp(struct carp_softc *sc)
1162 const struct carp_vhaddr *vha;
1164 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1165 if (vha->vha_iaback == NULL)
1167 arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa);
1173 carp_send_na(struct carp_softc *sc)
1175 struct ifaddr_container *ifac;
1176 struct in6_addr *in6;
1177 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1179 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1180 struct ifaddr *ifa = ifac->ifa;
1182 if (ifa->ifa_addr->sa_family != AF_INET6)
1185 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1186 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1187 ND_NA_FLAG_OVERRIDE, 1, NULL);
1188 DELAY(1000); /* XXX */
1193 static __inline const struct carp_vhaddr *
1194 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1196 struct carp_vhaddr *vha;
1198 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1199 if (vha->vha_iaback == NULL)
1202 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1210 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1211 const struct in_addr *isaddr, uint8_t **enaddr)
1213 const struct carp_softc *vh;
1214 int index, count = 0;
1217 * XXX proof of concept implementation.
1218 * We use the source ip to decide which virtual host should
1219 * handle the request. If we're master of that virtual host,
1220 * then we respond, otherwise, just drop the arp packet on
1224 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1225 if (!CARP_IS_RUNNING(&vh->sc_if))
1228 if (carp_find_addr(vh, itaddr) != NULL)
1234 /* this should be a hash, like pf_hash() */
1235 index = ntohl(isaddr->s_addr) % count;
1238 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1239 if (!CARP_IS_RUNNING(&vh->sc_if))
1242 if (carp_find_addr(vh, itaddr) == NULL)
1245 if (count == index) {
1246 if (vh->sc_state == MASTER) {
1247 *enaddr = IF_LLADDR(&vh->sc_if);
1260 carp_iamatch(const struct in_ifaddr *ia)
1262 const struct carp_softc *sc = ia->ia_ifp->if_softc;
1264 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1267 if (carp_opts[CARPCTL_ARPBALANCE])
1268 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1271 if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER)
1279 carp_iamatch6(void *v, struct in6_addr *taddr)
1281 struct carp_if *cif = v;
1282 struct carp_softc *vh;
1284 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1286 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1287 struct ifaddr_container *ifac;
1289 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1291 struct ifaddr *ifa = ifac->ifa;
1293 if (IN6_ARE_ADDR_EQUAL(taddr,
1294 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1295 CARP_IS_RUNNING(&vh->sc_if) &&
1296 vh->sc_state == MASTER) {
1305 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1308 struct carp_if *cif = v;
1309 struct carp_softc *sc;
1311 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1313 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1314 struct ifaddr_container *ifac;
1316 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1318 struct ifaddr *ifa = ifac->ifa;
1320 if (IN6_ARE_ADDR_EQUAL(taddr,
1321 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1322 CARP_IS_RUNNING(&sc->sc_if)) {
1323 struct ifnet *ifp = &sc->sc_if;
1325 mtag = m_tag_get(PACKET_TAG_CARP,
1326 sizeof(struct ifnet *), MB_DONTWAIT);
1328 /* better a bit than nothing */
1329 return (IF_LLADDR(ifp));
1331 bcopy(&ifp, (caddr_t)(mtag + 1),
1332 sizeof(struct ifnet *));
1333 m_tag_prepend(m, mtag);
1335 return (IF_LLADDR(ifp));
1343 static struct ifnet *
1344 carp_forus(struct carp_if *cif, const uint8_t *dhost)
1346 struct carp_softc *sc;
1348 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1350 if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0)
1353 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1354 struct ifnet *ifp = &sc->sc_if;
1356 if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER &&
1357 !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN))
1364 carp_master_down_timeout(void *xsc)
1366 struct carp_softc *sc = xsc;
1368 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1369 sc->sc_if.if_xname);
1371 carp_master_down(sc);
1376 carp_master_down(struct carp_softc *sc)
1378 switch (sc->sc_state) {
1380 kprintf("%s: master_down event in INIT state\n",
1381 sc->sc_if.if_xname);
1388 carp_set_state(sc, MASTER);
1395 carp_setroute(sc, RTM_ADD);
1401 * When in backup state, af indicates whether to reset the master down timer
1402 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1405 carp_setrun(struct carp_softc *sc, sa_family_t af)
1407 struct ifnet *cifp = &sc->sc_if;
1410 if (sc->sc_carpdev == NULL) {
1411 carp_set_state(sc, INIT);
1415 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1416 (sc->sc_naddrs || sc->sc_naddrs6)) {
1419 carp_setroute(sc, RTM_DELETE);
1423 switch (sc->sc_state) {
1425 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1431 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1433 carp_set_state(sc, MASTER);
1434 carp_setroute(sc, RTM_ADD);
1436 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1437 carp_set_state(sc, BACKUP);
1438 carp_setroute(sc, RTM_DELETE);
1444 callout_stop(&sc->sc_ad_tmo);
1445 tv.tv_sec = 3 * sc->sc_advbase;
1446 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1450 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1451 carp_master_down_timeout, sc);
1456 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1457 carp_master_down_timeout, sc);
1462 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1463 carp_master_down_timeout, sc);
1465 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1466 carp_master_down_timeout, sc);
1472 tv.tv_sec = sc->sc_advbase;
1473 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1474 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1475 carp_send_ad_timeout, sc);
1481 carp_multicast_cleanup(struct carp_softc *sc)
1483 struct ip_moptions *imo = &sc->sc_imo;
1485 if (imo->imo_num_memberships == 0)
1487 KKASSERT(imo->imo_num_memberships == 1);
1489 in_delmulti(imo->imo_membership[0]);
1490 imo->imo_membership[0] = NULL;
1491 imo->imo_num_memberships = 0;
1492 imo->imo_multicast_ifp = NULL;
1497 carp_multicast6_cleanup(struct carp_softc *sc)
1499 struct ip6_moptions *im6o = &sc->sc_im6o;
1501 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1502 struct in6_multi_mship *imm =
1503 LIST_FIRST(&im6o->im6o_memberships);
1505 LIST_REMOVE(imm, i6mm_chain);
1506 in6_leavegroup(imm);
1508 im6o->im6o_multicast_ifp = NULL;
1513 carp_get_vhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1515 const struct carp_vhaddr *vha;
1516 struct ifcarpvhaddr *carpa, *carpa0;
1517 int count, len, error;
1520 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1523 if (ifd->ifd_len == 0) {
1524 ifd->ifd_len = count * sizeof(*carpa);
1526 } else if (count == 0 || ifd->ifd_len < sizeof(*carpa)) {
1530 len = min(ifd->ifd_len, sizeof(*carpa) * count);
1531 KKASSERT(len >= sizeof(*carpa));
1533 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1538 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1539 if (len < sizeof(*carpa))
1542 carpa->carpa_flags = vha->vha_flags;
1543 carpa->carpa_addr.sin_family = AF_INET;
1544 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1546 carpa->carpa_baddr.sin_family = AF_INET;
1547 if (vha->vha_iaback == NULL) {
1548 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1550 carpa->carpa_baddr.sin_addr =
1551 vha->vha_iaback->ia_addr.sin_addr;
1556 len -= sizeof(*carpa);
1558 ifd->ifd_len = sizeof(*carpa) * count;
1559 KKASSERT(ifd->ifd_len > 0);
1561 error = copyout(carpa0, ifd->ifd_data, ifd->ifd_len);
1562 kfree(carpa0, M_TEMP);
1567 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
1568 struct in_ifaddr *ia_del)
1571 struct in_ifaddr *ia_if;
1572 struct in_ifaddr_container *iac;
1573 const struct sockaddr_in *sin;
1577 KKASSERT(vha->vha_ia != NULL);
1579 sin = &vha->vha_ia->ia_addr;
1580 iaddr = ntohl(sin->sin_addr.s_addr);
1584 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1585 struct in_ifaddr *ia = iac->ia;
1590 if (ia->ia_ifp->if_type == IFT_CARP)
1593 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1596 /* and, yeah, we need a multicast-capable iface too */
1597 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1600 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1601 if (sin->sin_addr.s_addr ==
1602 ia->ia_addr.sin_addr.s_addr)
1606 else if (sc->sc_carpdev != NULL &&
1607 sc->sc_carpdev == ia->ia_ifp)
1612 carp_deactivate_vhaddr(sc, vha, FALSE);
1616 ifp = ia_if->ia_ifp;
1618 /* XXX Don't allow parent iface to be changed */
1619 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
1622 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
1626 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1628 struct carp_vhaddr *vha_new;
1629 struct in_ifaddr *carp_ia;
1631 struct carp_vhaddr *vha;
1634 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1635 carp_ia = ifatoia(carp_ifa);
1638 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1639 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
1642 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
1643 vha_new->vha_ia = carp_ia;
1644 carp_insert_vhaddr(sc, vha_new);
1646 if (carp_config_vhaddr(sc, vha_new, NULL) != 0) {
1648 * If the above configuration fails, it may only mean
1649 * that the new address is problematic. However, the
1650 * carp(4) interface may already have several working
1651 * addresses. Since the expected behaviour of
1652 * SIOC[AS]IFADDR is to put the NIC into working state,
1653 * we try starting the state machine manually here with
1654 * the hope that the carp(4)'s previously working
1655 * addresses still could be brought up.
1657 carp_hmac_prepare(sc);
1658 carp_set_state(sc, INIT);
1664 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1666 struct carp_vhaddr *vha;
1667 struct in_ifaddr *carp_ia;
1669 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1670 carp_ia = ifatoia(carp_ifa);
1672 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1673 KKASSERT(vha->vha_ia != NULL);
1674 if (vha->vha_ia == carp_ia)
1677 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1680 * Remove the vhaddr from the list before deactivating
1681 * the vhaddr, so that the HMAC could be correctly
1682 * updated in carp_deactivate_vhaddr()
1684 carp_remove_vhaddr(sc, vha);
1686 carp_deactivate_vhaddr(sc, vha, FALSE);
1691 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1693 struct carp_vhaddr *vha;
1694 struct in_ifaddr *carp_ia;
1696 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1697 carp_ia = ifatoia(carp_ifa);
1699 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1700 KKASSERT(vha->vha_ia != NULL);
1701 if (vha->vha_ia == carp_ia)
1704 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1706 /* Remove then reinsert, to keep the vhaddr list sorted */
1707 carp_remove_vhaddr(sc, vha);
1708 carp_insert_vhaddr(sc, vha);
1710 if (carp_config_vhaddr(sc, vha, NULL) != 0) {
1711 /* See the comment in carp_add_addr() */
1712 carp_hmac_prepare(sc);
1713 carp_set_state(sc, INIT);
1722 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1725 struct carp_if *cif;
1726 struct in6_ifaddr *ia, *ia_if;
1727 struct ip6_moptions *im6o = &sc->sc_im6o;
1728 struct in6_multi_mship *imm;
1729 struct in6_addr in6;
1732 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1737 /* we have to do it by hands to check we won't match on us */
1738 ia_if = NULL; own = 0;
1739 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1742 for (i = 0; i < 4; i++) {
1743 if ((sin6->sin6_addr.s6_addr32[i] &
1744 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1745 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1746 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1749 /* and, yeah, we need a multicast-capable iface too */
1750 if (ia->ia_ifp != &sc->sc_if &&
1751 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1755 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1756 &ia->ia_addr.sin6_addr))
1762 return (EADDRNOTAVAIL);
1766 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1767 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1768 return (EADDRNOTAVAIL);
1770 if (!sc->sc_naddrs6) {
1771 im6o->im6o_multicast_ifp = ifp;
1773 /* join CARP multicast address */
1774 bzero(&in6, sizeof(in6));
1775 in6.s6_addr16[0] = htons(0xff02);
1776 in6.s6_addr8[15] = 0x12;
1777 if (in6_setscope(&in6, ifp, NULL) != 0)
1779 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1781 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1783 /* join solicited multicast address */
1784 bzero(&in6, sizeof(in6));
1785 in6.s6_addr16[0] = htons(0xff02);
1786 in6.s6_addr32[1] = 0;
1787 in6.s6_addr32[2] = htonl(1);
1788 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1789 in6.s6_addr8[12] = 0xff;
1790 if (in6_setscope(&in6, ifp, NULL) != 0)
1792 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1794 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1797 if (!ifp->if_carp) {
1798 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
1800 if ((error = ifpromisc(ifp, 1))) {
1805 TAILQ_INIT(&cif->vhif_vrs);
1808 struct carp_softc *vr;
1811 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1812 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1819 sc->sc_carpdev = ifp;
1821 { /* XXX prevent endless loop if already in queue */
1822 struct carp_softc *vr, *after = NULL;
1826 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1829 if (vr->sc_vhid < sc->sc_vhid)
1834 /* We're trying to keep things in order */
1836 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1838 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1851 /* clean up multicast memberships */
1852 if (!sc->sc_naddrs6) {
1853 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1854 imm = LIST_FIRST(&im6o->im6o_memberships);
1855 LIST_REMOVE(imm, i6mm_chain);
1856 in6_leavegroup(imm);
1863 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1867 if (!--sc->sc_naddrs6) {
1868 struct carp_if *cif = sc->sc_carpdev->if_carp;
1869 struct ip6_moptions *im6o = &sc->sc_im6o;
1871 callout_stop(&sc->sc_ad_tmo);
1873 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1874 struct in6_multi_mship *imm =
1875 LIST_FIRST(&im6o->im6o_memberships);
1877 LIST_REMOVE(imm, i6mm_chain);
1878 in6_leavegroup(imm);
1880 im6o->im6o_multicast_ifp = NULL;
1881 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1882 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
1883 sc->sc_carpdev->if_carp = NULL;
1884 kfree(cif, M_IFADDR);
1894 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
1896 struct carp_softc *sc = ifp->if_softc;
1897 struct carpreq carpr;
1900 struct ifaliasreq *ifra;
1902 char devname[IFNAMSIZ];
1905 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1909 ifa = (struct ifaddr *)addr;
1910 ifra = (struct ifaliasreq *)addr;
1911 ifr = (struct ifreq *)addr;
1912 ifd = (struct ifdrv *)addr;
1916 if (ifp->if_flags & IFF_UP) {
1917 if ((ifp->if_flags & IFF_RUNNING) == 0)
1919 } else if (ifp->if_flags & IFF_RUNNING) {
1920 carp_ioctl_stop(sc);
1925 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1928 error = copyin(ifr->ifr_data, &carpr, sizeof(carpr));
1932 error = carp_ioctl_setvh(sc, &carpr);
1936 bzero(&carpr, sizeof(carpr));
1937 carpr.carpr_state = sc->sc_state;
1938 carpr.carpr_vhid = sc->sc_vhid;
1939 carpr.carpr_advbase = sc->sc_advbase;
1940 carpr.carpr_advskew = sc->sc_advskew;
1941 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1943 bcopy(sc->sc_key, carpr.carpr_key,
1944 sizeof(carpr.carpr_key));
1947 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1951 switch (ifd->ifd_cmd) {
1953 if (ifd->ifd_len != sizeof(devname))
1967 switch (ifd->ifd_cmd) {
1969 error = carp_get_vhaddr(sc, ifd);
1973 bzero(devname, sizeof(devname));
1974 if (sc->sc_carpdev != NULL) {
1975 strlcpy(devname, sc->sc_carpdev->if_xname,
1978 error = copyout(devname, ifd->ifd_data,
1985 error = ether_ioctl(ifp, cmd, addr);
1988 carp_hmac_prepare(sc);
1995 carp_ioctl_stop_dispatch(netmsg_t msg)
1997 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1998 struct carp_softc *sc = cmsg->nc_softc;
2004 lwkt_replymsg(&cmsg->base.lmsg, 0);
2008 carp_ioctl_stop(struct carp_softc *sc)
2010 struct ifnet *ifp = &sc->arpcom.ac_if;
2011 struct netmsg_carp cmsg;
2013 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2015 ifnet_deserialize_all(ifp);
2017 bzero(&cmsg, sizeof(cmsg));
2018 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2019 carp_ioctl_stop_dispatch);
2022 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2024 ifnet_serialize_all(ifp);
2028 carp_ioctl_setvh_dispatch(netmsg_t msg)
2030 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2031 struct carp_softc *sc = cmsg->nc_softc, *vr;
2032 struct ifnet *ifp = &sc->arpcom.ac_if;
2033 const struct carpreq *carpr = cmsg->nc_data;
2039 if ((ifp->if_flags & IFF_RUNNING) &&
2040 sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) {
2041 switch (carpr->carpr_state) {
2043 callout_stop(&sc->sc_ad_tmo);
2044 carp_set_state(sc, BACKUP);
2046 carp_setroute(sc, RTM_DELETE);
2050 carp_master_down(sc);
2057 if (carpr->carpr_vhid > 0) {
2058 if (carpr->carpr_vhid > 255) {
2062 if (sc->sc_carpdev) {
2063 struct carp_if *cif = sc->sc_carpdev->if_carp;
2065 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2067 vr->sc_vhid == carpr->carpr_vhid) {
2073 sc->sc_vhid = carpr->carpr_vhid;
2075 IF_LLADDR(ifp)[5] = sc->sc_vhid;
2076 bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr,
2081 if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) {
2082 if (carpr->carpr_advskew >= 255) {
2086 if (carpr->carpr_advbase > 255) {
2090 sc->sc_advbase = carpr->carpr_advbase;
2091 sc->sc_advskew = carpr->carpr_advskew;
2094 bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key));
2104 lwkt_replymsg(&cmsg->base.lmsg, error);
2108 carp_ioctl_setvh(struct carp_softc *sc, struct carpreq *carpr)
2110 struct ifnet *ifp = &sc->arpcom.ac_if;
2111 struct netmsg_carp cmsg;
2114 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2116 ifnet_deserialize_all(ifp);
2118 bzero(&cmsg, sizeof(cmsg));
2119 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2120 carp_ioctl_setvh_dispatch);
2122 cmsg.nc_data = carpr;
2124 error = lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2126 ifnet_serialize_all(ifp);
2132 carp_init_dispatch(netmsg_t msg)
2134 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2135 struct carp_softc *sc = cmsg->nc_softc;
2139 sc->sc_if.if_flags |= IFF_RUNNING;
2140 carp_set_state(sc, INIT);
2145 lwkt_replymsg(&cmsg->base.lmsg, 0);
2149 carp_init(void *xsc)
2151 struct carp_softc *sc = xsc;
2152 struct ifnet *ifp = &sc->arpcom.ac_if;
2153 struct netmsg_carp cmsg;
2155 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2157 ifnet_deserialize_all(ifp);
2159 bzero(&cmsg, sizeof(cmsg));
2160 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2161 carp_init_dispatch);
2164 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2166 ifnet_serialize_all(ifp);
2170 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2173 struct carp_softc *sc = ifp->if_softc;
2177 if (sc->sc_carpdev) {
2180 * CARP's ifp is passed to backing device's
2183 sc->sc_carpdev->if_output(ifp, m, dst, rt);
2186 error = ENETUNREACH;
2194 * Start output on carp interface. This function should never be called.
2197 carp_start(struct ifnet *ifp)
2199 panic("%s: start called\n", ifp->if_xname);
2203 carp_serialize(struct ifnet *ifp __unused,
2204 enum ifnet_serialize slz __unused)
2209 carp_deserialize(struct ifnet *ifp __unused,
2210 enum ifnet_serialize slz __unused)
2215 carp_tryserialize(struct ifnet *ifp __unused,
2216 enum ifnet_serialize slz __unused)
2224 carp_serialize_assert(struct ifnet *ifp __unused,
2225 enum ifnet_serialize slz __unused, boolean_t serialized __unused)
2229 #endif /* INVARIANTS */
2232 carp_set_state(struct carp_softc *sc, int state)
2234 struct ifnet *cifp = &sc->sc_if;
2236 if (sc->sc_state == state)
2238 sc->sc_state = state;
2240 switch (sc->sc_state) {
2242 cifp->if_link_state = LINK_STATE_DOWN;
2246 cifp->if_link_state = LINK_STATE_UP;
2250 cifp->if_link_state = LINK_STATE_UNKNOWN;
2257 carp_group_demote_adj(struct ifnet *ifp, int adj)
2259 struct ifg_list *ifgl;
2264 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2265 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2267 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2274 if (adj > 0 && *dm == 1)
2276 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2277 ifgl->ifgl_group->ifg_group, *dm);
2284 carp_carpdev_state(void *v)
2286 struct carp_if *cif = v;
2287 struct carp_softc *sc;
2291 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2298 carp_sc_state(struct carp_softc *sc)
2300 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2301 callout_stop(&sc->sc_ad_tmo);
2302 callout_stop(&sc->sc_md_tmo);
2303 callout_stop(&sc->sc_md6_tmo);
2304 carp_set_state(sc, INIT);
2306 if (!sc->sc_suppress) {
2307 carp_suppress_preempt++;
2308 if (carp_suppress_preempt == 1)
2311 sc->sc_suppress = 1;
2313 carp_set_state(sc, INIT);
2315 if (sc->sc_suppress)
2316 carp_suppress_preempt--;
2317 sc->sc_suppress = 0;
2322 carp_stop(struct carp_softc *sc, int detach)
2324 sc->sc_if.if_flags &= ~IFF_RUNNING;
2326 callout_stop(&sc->sc_ad_tmo);
2327 callout_stop(&sc->sc_md_tmo);
2328 callout_stop(&sc->sc_md6_tmo);
2330 if (!detach && sc->sc_state == MASTER)
2333 if (sc->sc_suppress)
2334 carp_suppress_preempt--;
2335 sc->sc_suppress = 0;
2337 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2338 carp_suppress_preempt--;
2339 sc->sc_sendad_errors = 0;
2340 sc->sc_sendad_success = 0;
2342 carp_set_state(sc, INIT);
2347 carp_suspend(struct carp_softc *sc, int detach)
2349 struct ifnet *cifp = &sc->sc_if;
2351 carp_stop(sc, detach);
2353 /* Retain the running state, if we are not dead yet */
2354 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2355 cifp->if_flags |= IFF_RUNNING;
2359 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2360 struct ifnet *ifp, struct in_ifaddr *ia_if, int own)
2362 struct ip_moptions *imo = &sc->sc_imo;
2363 struct carp_if *cif;
2364 struct carp_softc *vr, *after = NULL;
2370 KKASSERT(vha->vha_ia != NULL);
2372 KASSERT(ia_if != NULL, ("NULL backing address\n"));
2373 KASSERT(vha->vha_iaback == NULL, ("%p is already activated\n", vha));
2374 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2375 ("inactive vhaddr %p is the address owner\n", vha));
2377 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2378 ("%s is already on %s\n", sc->sc_if.if_xname,
2379 sc->sc_carpdev->if_xname));
2381 if (!ifp->if_carp) {
2382 KASSERT(sc->sc_carpdev == NULL,
2383 ("%s is already on %s\n", sc->sc_if.if_xname,
2384 sc->sc_carpdev->if_xname));
2386 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2388 error = ifpromisc(ifp, 1);
2394 TAILQ_INIT(&cif->vhif_vrs);
2398 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2399 if (vr != sc && vr->sc_vhid == sc->sc_vhid)
2405 if (sc->sc_carpdev != NULL)
2411 sc->sc_carpdev = ifp;
2415 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2418 if (vr->sc_vhid < sc->sc_vhid)
2423 if (assert_onlist) {
2424 KASSERT(onlist, ("%s is not on %s carp list\n",
2425 sc->sc_if.if_xname, ifp->if_xname));
2427 KASSERT(!onlist, ("%s is already on %s carp list\n",
2428 sc->sc_if.if_xname, ifp->if_xname));
2433 /* We're trying to keep things in order */
2435 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2437 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2440 vha->vha_iaback = ia_if;
2444 vha->vha_flags |= CARP_VHAF_OWNER;
2446 /* XXX save user configured advskew? */
2450 carp_addroute_vhaddr(sc, vha);
2453 * Join the multicast group only after the backing interface
2454 * has been hooked with the CARP interface.
2456 KASSERT(imo->imo_multicast_ifp == NULL ||
2457 imo->imo_multicast_ifp == &sc->sc_if,
2458 ("%s didn't leave mcast group on %s\n",
2459 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2461 if (imo->imo_num_memberships == 0) {
2462 struct in_addr addr;
2464 addr.s_addr = htonl(INADDR_CARP_GROUP);
2465 imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if);
2466 if (imo->imo_membership[0] == NULL) {
2467 carp_deactivate_vhaddr(sc, vha, FALSE);
2471 imo->imo_num_memberships++;
2472 imo->imo_multicast_ifp = &sc->sc_if;
2473 imo->imo_multicast_ttl = CARP_DFLTTL;
2474 imo->imo_multicast_loop = 0;
2477 carp_hmac_prepare(sc);
2478 carp_set_state(sc, INIT);
2484 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2485 boolean_t del_iaback)
2487 KKASSERT(vha->vha_ia != NULL);
2489 carp_hmac_prepare(sc);
2491 if (vha->vha_iaback == NULL) {
2492 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2493 ("inactive vhaddr %p is the address owner\n", vha));
2497 vha->vha_flags &= ~CARP_VHAF_OWNER;
2498 carp_delroute_vhaddr(sc, vha, del_iaback);
2500 KKASSERT(sc->sc_naddrs > 0);
2501 vha->vha_iaback = NULL;
2503 if (!sc->sc_naddrs) {
2504 if (sc->sc_naddrs6) {
2505 carp_multicast_cleanup(sc);
2508 carp_detach(sc, 0, del_iaback);
2514 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2516 struct carp_vhaddr *vha;
2517 struct in_ifaddr *ia_if;
2519 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2520 ia_if = ifatoia(ifa_if);
2523 * Test each inactive vhaddr against the newly added address.
2524 * If the newly added address could be the backing address,
2525 * then activate the matching vhaddr.
2527 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2528 const struct in_ifaddr *ia;
2532 if (vha->vha_iaback != NULL)
2536 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr);
2538 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet)
2542 if (ia->ia_addr.sin_addr.s_addr ==
2543 ia_if->ia_addr.sin_addr.s_addr)
2546 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2551 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
2552 struct ifaddr *ifa_if)
2554 struct carp_vhaddr *vha;
2555 struct in_ifaddr *ia_if;
2557 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2558 ia_if = ifatoia(ifa_if);
2561 * Ad src address is deleted; set it to NULL.
2562 * Following loop will try pick up a new ad src address
2563 * if one of the vhaddr could retain its backing address.
2565 if (sc->sc_ia == ia_if)
2569 * Test each active vhaddr against the deleted address.
2570 * If the deleted address is vhaddr address's backing
2571 * address, then deactivate the vhaddr.
2573 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2574 if (vha->vha_iaback == NULL)
2577 if (vha->vha_iaback == ia_if)
2578 carp_deactivate_vhaddr(sc, vha, TRUE);
2579 else if (sc->sc_ia == NULL)
2580 sc->sc_ia = vha->vha_iaback;
2585 carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del)
2587 struct carp_vhaddr *vha;
2589 KKASSERT(sc->sc_carpdev == NULL);
2591 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2592 carp_config_vhaddr(sc, vha, ifatoia(ifa_del));
2596 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
2597 enum ifaddr_event event, struct ifaddr *ifa)
2599 struct carp_softc *sc;
2603 if (ifa->ifa_addr->sa_family != AF_INET)
2606 KASSERT(&curthread->td_msgport == cpu_portfn(0),
2607 ("not in netisr0"));
2609 if (ifp->if_type == IFT_CARP) {
2611 * Address is changed on carp(4) interface
2614 case IFADDR_EVENT_ADD:
2615 carp_add_addr(ifp->if_softc, ifa);
2618 case IFADDR_EVENT_CHANGE:
2619 carp_config_addr(ifp->if_softc, ifa);
2622 case IFADDR_EVENT_DELETE:
2623 carp_del_addr(ifp->if_softc, ifa);
2630 * Address is changed on non-carp(4) interface
2632 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2635 LIST_FOREACH(sc, &carpif_list, sc_next) {
2636 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
2637 /* Not the parent iface; skip */
2642 case IFADDR_EVENT_ADD:
2643 carp_link_addrs(sc, ifp, ifa);
2646 case IFADDR_EVENT_DELETE:
2647 if (sc->sc_carpdev != NULL) {
2648 carp_unlink_addrs(sc, ifp, ifa);
2649 if (sc->sc_carpdev == NULL) {
2651 * We no longer have the parent
2652 * interface, however, certain
2653 * virtual addresses, which are
2654 * not used because they can't
2655 * match the previous parent
2656 * interface's addresses, may now
2657 * match different interface's
2660 carp_update_addrs(sc, ifa);
2664 * The carp(4) interface didn't have a
2665 * parent iface, so it is not possible
2666 * that it will contain any address to
2672 case IFADDR_EVENT_CHANGE:
2673 if (sc->sc_carpdev == NULL) {
2675 * The carp(4) interface didn't have a
2676 * parent iface, so it is not possible
2677 * that it will contain any address to
2680 carp_link_addrs(sc, ifp, ifa);
2683 * First try breaking tie with the old
2684 * address. Then see whether we could
2685 * link certain vhaddr to the new address.
2686 * If that fails, i.e. carpdev is NULL,
2687 * we try a global update.
2689 * NOTE: The above order is critical.
2691 carp_unlink_addrs(sc, ifp, ifa);
2692 carp_link_addrs(sc, ifp, ifa);
2693 if (sc->sc_carpdev == NULL) {
2695 * See the comment in the above
2696 * IFADDR_EVENT_DELETE block.
2698 carp_update_addrs(sc, NULL);
2710 carp_proto_ctlinput(netmsg_t msg)
2712 int cmd = msg->ctlinput.nm_cmd;
2713 struct sockaddr *sa = msg->ctlinput.nm_arg;
2714 struct in_ifaddr_container *iac;
2718 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
2719 struct in_ifaddr *ia = iac->ia;
2720 struct ifnet *ifp = ia->ia_ifp;
2722 if (ifp->if_type == IFT_CARP)
2725 if (ia->ia_ifa.ifa_addr == sa) {
2726 if (cmd == PRC_IFDOWN) {
2727 carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE,
2729 } else if (cmd == PRC_IFUP) {
2730 carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD,
2738 lwkt_replymsg(&msg->lmsg, 0);
2744 lwkt_gettoken(&carp_tok);
2750 lwkt_reltoken(&carp_tok);
2754 carp_parent(struct ifnet *cifp)
2756 struct carp_softc *sc;
2758 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
2760 KKASSERT(cifp->if_type == IFT_CARP);
2761 sc = cifp->if_softc;
2763 return sc->sc_carpdev;
2766 #define rtinitflags(x) \
2767 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
2771 carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
2773 struct in_ifaddr *ia, *iaback;
2776 if (sc->sc_state != MASTER)
2780 KKASSERT(ia != NULL);
2782 iaback = vha->vha_iaback;
2783 KKASSERT(iaback != NULL);
2785 rtinit(&iaback->ia_ifa, RTM_DELETE, rtinitflags(iaback));
2786 in_ifadown(&iaback->ia_ifa, 1);
2787 iaback->ia_flags &= ~IFA_ROUTE;
2789 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
2791 ia->ia_flags |= IFA_ROUTE;
2796 carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2797 boolean_t del_iaback)
2799 struct in_ifaddr *ia, *iaback;
2802 KKASSERT(ia != NULL);
2804 iaback = vha->vha_iaback;
2805 KKASSERT(iaback != NULL);
2807 rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia));
2808 in_ifadown(&ia->ia_ifa, 1);
2809 ia->ia_flags &= ~IFA_ROUTE;
2811 if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) {
2814 error = rtinit(&iaback->ia_ifa, RTM_ADD,
2815 rtinitflags(iaback) | RTF_UP);
2817 iaback->ia_flags |= IFA_ROUTE;
2822 carp_modevent(module_t mod, int type, void *data)
2826 LIST_INIT(&carpif_list);
2827 carp_ifdetach_event =
2828 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
2829 EVENTHANDLER_PRI_ANY);
2831 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
2832 EVENTHANDLER_PRI_FIRST);
2833 if_clone_attach(&carp_cloner);
2837 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
2838 carp_ifdetach_event);
2839 EVENTHANDLER_DEREGISTER(ifaddr_event,
2841 if_clone_detach(&carp_cloner);
2850 static moduledata_t carp_mod = {
2855 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);