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>
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/if_ether.h>
74 #include <netinet/icmp6.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/scope6_var.h>
78 #include <netinet6/nd6.h>
81 #include <netinet/ip_carp.h>
83 #define CARP_IFNAME "carp"
84 #define CARP_IS_RUNNING(ifp) \
85 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
88 uint32_t vha_flags; /* CARP_VHAF_ */
89 struct in_ifaddr *vha_ia; /* carp address */
90 struct in_ifaddr *vha_iaback; /* backing address */
91 TAILQ_ENTRY(carp_vhaddr) vha_link;
93 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr);
97 struct ifnet *sc_carpdev; /* parent interface */
98 struct carp_vhaddr_list sc_vha_list; /* virtual addr list */
100 const struct in_ifaddr *sc_ia; /* primary iface address v4 */
101 struct ip_moptions sc_imo;
104 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
105 struct ip6_moptions sc_im6o;
107 TAILQ_ENTRY(carp_softc) sc_list;
109 enum { INIT = 0, BACKUP, MASTER }
115 int sc_sendad_errors;
116 #define CARP_SENDAD_MAX_ERRORS 3
117 int sc_sendad_success;
118 #define CARP_SENDAD_MIN_SUCCESS 3
122 int sc_naddrs; /* actually used IPv4 vha */
124 int sc_advbase; /* seconds */
129 #define CARP_HMAC_PAD 64
130 unsigned char sc_key[CARP_KEY_LEN];
131 unsigned char sc_pad[CARP_HMAC_PAD];
134 struct callout sc_ad_tmo; /* advertisement timeout */
135 struct callout sc_md_tmo; /* master down timeout */
136 struct callout sc_md6_tmo; /* master down timeout */
138 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
141 #define sc_if arpcom.ac_if
144 TAILQ_HEAD(, carp_softc) vhif_vrs;
147 SYSCTL_DECL(_net_inet_carp);
149 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
150 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
151 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
152 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
153 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
154 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
155 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
156 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
157 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
159 static int carp_suppress_preempt = 0;
160 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
161 &carp_suppress_preempt, 0, "Preemption is suppressed");
163 static struct carpstats carpstats;
164 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
165 &carpstats, carpstats,
166 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
168 #define CARP_LOG(...) do { \
169 if (carp_opts[CARPCTL_LOG] > 0) \
170 log(LOG_INFO, __VA_ARGS__); \
173 #define CARP_DEBUG(...) do { \
174 if (carp_opts[CARPCTL_LOG] > 1) \
175 log(LOG_DEBUG, __VA_ARGS__); \
178 static struct lwkt_token carp_tok = LWKT_TOKEN_INITIALIZER(carp_token);
180 static void carp_hmac_prepare(struct carp_softc *);
181 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
183 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
185 static void carp_setroute(struct carp_softc *, int);
186 static void carp_proto_input_c(struct carp_softc *, struct mbuf *,
187 struct carp_header *, sa_family_t);
188 static int carp_clone_create(struct if_clone *, int, caddr_t);
189 static int carp_clone_destroy(struct ifnet *);
190 static void carp_detach(struct carp_softc *, int, boolean_t);
191 static void carp_prepare_ad(struct carp_softc *, struct carp_header *);
192 static void carp_send_ad_all(void);
193 static void carp_send_ad_timeout(void *);
194 static void carp_send_ad(struct carp_softc *);
195 static void carp_send_arp(struct carp_softc *);
196 static void carp_master_down_timeout(void *);
197 static void carp_master_down(struct carp_softc *);
198 static void carp_setrun(struct carp_softc *, sa_family_t);
199 static void carp_set_state(struct carp_softc *, int);
200 static struct ifnet *carp_forus(struct carp_if *, const uint8_t *);
202 static void carp_init(void *);
203 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
204 static int carp_output(struct ifnet *, struct mbuf *, struct sockaddr *,
206 static void carp_start(struct ifnet *);
207 static void carp_serialize(struct ifnet *, enum ifnet_serialize);
208 static void carp_deserialize(struct ifnet *, enum ifnet_serialize);
209 static int carp_tryserialize(struct ifnet *, enum ifnet_serialize);
211 static void carp_serialize_assert(struct ifnet *, enum ifnet_serialize,
215 static void carp_multicast_cleanup(struct carp_softc *);
216 static void carp_add_addr(struct carp_softc *, struct ifaddr *);
217 static void carp_del_addr(struct carp_softc *, struct ifaddr *);
218 static void carp_config_addr(struct carp_softc *, struct ifaddr *);
219 static void carp_link_addrs(struct carp_softc *, struct ifnet *,
221 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *,
223 static void carp_update_addrs(struct carp_softc *, struct ifaddr *);
225 static int carp_get_vhaddr(struct carp_softc *, struct ifdrv *);
226 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *,
228 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
229 struct ifnet *, struct in_ifaddr *, int);
230 static void carp_deactivate_vhaddr(struct carp_softc *,
231 struct carp_vhaddr *, boolean_t);
232 static int carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *);
233 static void carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *,
236 static void carp_sc_state(struct carp_softc *);
238 static void carp_send_na(struct carp_softc *);
240 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
241 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
243 static void carp_multicast6_cleanup(struct carp_softc *);
245 static void carp_stop(struct carp_softc *, int);
246 static void carp_suspend(struct carp_softc *, int);
248 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
250 static void carp_ifdetach(void *, struct ifnet *);
252 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
254 static LIST_HEAD(, carp_softc) carpif_list;
256 static struct if_clone carp_cloner =
257 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
260 static uint8_t carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 };
262 static eventhandler_tag carp_ifdetach_event;
263 static eventhandler_tag carp_ifaddr_event;
266 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
268 struct carp_vhaddr *vha;
269 u_long new_addr, addr;
271 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
274 * Virtual address list is sorted; smaller one first
276 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
278 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
279 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
285 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
287 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
288 vha_new->vha_flags |= CARP_VHAF_ONLIST;
292 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
294 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
295 vha->vha_flags &= ~CARP_VHAF_ONLIST;
296 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
300 carp_hmac_prepare(struct carp_softc *sc)
302 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
303 uint8_t vhid = sc->sc_vhid & 0xff;
306 struct ifaddr_container *ifac;
310 struct carp_vhaddr *vha;
313 /* XXX: possible race here */
315 /* compute ipad from key */
316 bzero(sc->sc_pad, sizeof(sc->sc_pad));
317 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
318 for (i = 0; i < sizeof(sc->sc_pad); i++)
319 sc->sc_pad[i] ^= 0x36;
321 /* precompute first part of inner hash */
322 SHA1Init(&sc->sc_sha1);
323 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
324 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
325 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
326 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
328 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
329 SHA1Update(&sc->sc_sha1,
330 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
331 sizeof(struct in_addr));
335 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
336 struct ifaddr *ifa = ifac->ifa;
338 if (ifa->ifa_addr->sa_family == AF_INET6) {
339 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
340 in6_clearscope(&in6);
341 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
346 /* convert ipad to opad */
347 for (i = 0; i < sizeof(sc->sc_pad); i++)
348 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
352 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
353 unsigned char md[20])
357 /* fetch first half of inner hash */
358 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
360 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
361 SHA1Final(md, &sha1ctx);
365 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
366 SHA1Update(&sha1ctx, md, 20);
367 SHA1Final(md, &sha1ctx);
371 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
372 unsigned char md[20])
374 unsigned char md2[20];
376 carp_hmac_generate(sc, counter, md2);
377 return (bcmp(md, md2, sizeof(md2)));
381 carp_setroute(struct carp_softc *sc, int cmd)
384 struct ifaddr_container *ifac;
386 struct carp_vhaddr *vha;
388 KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD);
390 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
391 if (vha->vha_iaback == NULL)
393 if (cmd == RTM_DELETE)
394 carp_delroute_vhaddr(sc, vha, FALSE);
396 carp_addroute_vhaddr(sc, vha);
400 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
401 struct ifaddr *ifa = ifac->ifa;
403 if (ifa->ifa_addr->sa_family == AF_INET6) {
414 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
416 struct carp_softc *sc;
419 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
423 sc->sc_advbase = CARP_DFLTINTV;
424 sc->sc_vhid = -1; /* required setting */
426 sc->sc_init_counter = 1;
430 TAILQ_INIT(&sc->sc_vha_list);
433 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
436 callout_init_mp(&sc->sc_ad_tmo);
437 callout_init_mp(&sc->sc_md_tmo);
438 callout_init_mp(&sc->sc_md6_tmo);
440 if_initname(ifp, CARP_IFNAME, unit);
442 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
443 ifp->if_init = carp_init;
444 ifp->if_ioctl = carp_ioctl;
445 ifp->if_start = carp_start;
446 ifp->if_serialize = carp_serialize;
447 ifp->if_deserialize = carp_deserialize;
448 ifp->if_tryserialize = carp_tryserialize;
450 ifp->if_serialize_assert = carp_serialize_assert;
452 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
453 ifq_set_ready(&ifp->if_snd);
455 ether_ifattach(ifp, carp_etheraddr, NULL);
457 ifp->if_type = IFT_CARP;
458 ifp->if_output = carp_output;
461 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
468 carp_clone_destroy(struct ifnet *ifp)
470 struct carp_softc *sc = ifp->if_softc;
475 carp_detach(sc, 1, FALSE);
476 LIST_REMOVE(sc, sc_next);
483 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active\n"));
490 carp_detach(struct carp_softc *sc, int detach, boolean_t del_iaback)
494 carp_suspend(sc, detach);
496 carp_multicast_cleanup(sc);
498 carp_multicast6_cleanup(sc);
501 if (!sc->sc_dead && detach) {
502 struct carp_vhaddr *vha;
504 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
505 carp_deactivate_vhaddr(sc, vha, del_iaback);
506 KKASSERT(sc->sc_naddrs == 0);
509 if (sc->sc_carpdev != NULL) {
510 cif = sc->sc_carpdev->if_carp;
511 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
512 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
513 ifpromisc(sc->sc_carpdev, 0);
514 sc->sc_carpdev->if_carp = NULL;
517 sc->sc_carpdev = NULL;
522 /* Detach an interface from the carp. */
524 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
526 struct carp_if *cif = ifp->if_carp;
527 struct carp_softc *sc;
531 while (ifp->if_carp &&
532 (sc = TAILQ_FIRST(&cif->vhif_vrs)) != NULL)
533 carp_detach(sc, 1, TRUE);
539 * process input packet.
540 * we have rearranged checks order compared to the rfc,
541 * but it seems more efficient this way or not possible otherwise.
544 carp_proto_input(struct mbuf **mp, int *offp, int proto)
546 struct mbuf *m = *mp;
547 struct ip *ip = mtod(m, struct ip *);
548 struct ifnet *ifp = m->m_pkthdr.rcvif;
549 struct carp_header *ch;
550 struct carp_softc *sc;
558 carpstats.carps_ipackets++;
560 if (!carp_opts[CARPCTL_ALLOW]) {
565 /* Check if received on a valid carp interface */
566 if (ifp->if_type != IFT_CARP) {
567 carpstats.carps_badif++;
568 CARP_LOG("carp_proto_input: packet received on non-carp "
569 "interface: %s\n", ifp->if_xname);
574 if (!CARP_IS_RUNNING(ifp)) {
575 carpstats.carps_badif++;
576 CARP_LOG("carp_proto_input: packet received on stopped carp "
577 "interface: %s\n", ifp->if_xname);
583 if (sc->sc_carpdev == NULL) {
584 carpstats.carps_badif++;
585 CARP_LOG("carp_proto_input: packet received on defunc carp "
586 "interface: %s\n", ifp->if_xname);
591 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
592 carpstats.carps_badif++;
593 CARP_LOG("carp_proto_input: non-mcast packet on "
594 "interface: %s\n", ifp->if_xname);
599 /* Verify that the IP TTL is CARP_DFLTTL. */
600 if (ip->ip_ttl != CARP_DFLTTL) {
601 carpstats.carps_badttl++;
602 CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n",
603 ip->ip_ttl, CARP_DFLTTL, ifp->if_xname);
608 /* Minimal CARP packet size */
609 len = iphlen + sizeof(*ch);
612 * Verify that the received packet length is
613 * not less than the CARP header
615 if (m->m_pkthdr.len < len) {
616 carpstats.carps_badlen++;
617 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
623 /* Make sure that CARP header is contiguous */
624 if (len > m->m_len) {
625 m = m_pullup(m, len);
627 carpstats.carps_hdrops++;
628 CARP_LOG("carp_proto_input: m_pullup failed\n");
631 ip = mtod(m, struct ip *);
633 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
635 /* Verify the CARP checksum */
636 if (in_cksum_skip(m, len, iphlen)) {
637 carpstats.carps_badsum++;
638 CARP_LOG("carp_proto_input: checksum failed on %s\n",
643 carp_proto_input_c(sc, m, ch, AF_INET);
646 return(IPPROTO_DONE);
651 carp6_proto_input(struct mbuf **mp, int *offp, int proto)
653 struct mbuf *m = *mp;
654 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
655 struct ifnet *ifp = m->m_pkthdr.rcvif;
656 struct carp_header *ch;
657 struct carp_softc *sc;
662 carpstats.carps_ipackets6++;
664 if (!carp_opts[CARPCTL_ALLOW]) {
669 /* check if received on a valid carp interface */
670 if (ifp->if_type != IFT_CARP) {
671 carpstats.carps_badif++;
672 CARP_LOG("carp6_proto_input: packet received on non-carp "
673 "interface: %s\n", ifp->if_xname);
678 if (!CARP_IS_RUNNING(ifp)) {
679 carpstats.carps_badif++;
680 CARP_LOG("carp_proto_input: packet received on stopped carp "
681 "interface: %s\n", ifp->if_xname);
687 if (sc->sc_carpdev == NULL) {
688 carpstats.carps_badif++;
689 CARP_LOG("carp6_proto_input: packet received on defunc-carp "
690 "interface: %s\n", ifp->if_xname);
695 /* verify that the IP TTL is 255 */
696 if (ip6->ip6_hlim != CARP_DFLTTL) {
697 carpstats.carps_badttl++;
698 CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n",
699 ip6->ip6_hlim, ifp->if_xname);
704 /* verify that we have a complete carp packet */
706 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
708 carpstats.carps_badlen++;
709 CARP_LOG("carp6_proto_input: packet size %u too small\n", len);
713 /* verify the CARP checksum */
714 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
715 carpstats.carps_badsum++;
716 CARP_LOG("carp6_proto_input: checksum failed, on %s\n",
722 carp_proto_input_c(sc, m, ch, AF_INET6);
725 return (IPPROTO_DONE);
730 carp_proto_input_c(struct carp_softc *sc, struct mbuf *m,
731 struct carp_header *ch, sa_family_t af)
734 uint64_t tmp_counter;
735 struct timeval sc_tv, ch_tv;
737 if (sc->sc_vhid != ch->carp_vhid) {
739 * CARP uses multicast, however, multicast packets
740 * are tapped to all CARP interfaces on the physical
741 * interface receiving the CARP packets, so we don't
742 * update any stats here.
749 /* verify the CARP version. */
750 if (ch->carp_version != CARP_VERSION) {
751 carpstats.carps_badver++;
752 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
758 /* verify the hash */
759 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
760 carpstats.carps_badauth++;
761 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
766 tmp_counter = ntohl(ch->carp_counter[0]);
767 tmp_counter = tmp_counter<<32;
768 tmp_counter += ntohl(ch->carp_counter[1]);
770 /* XXX Replay protection goes here */
772 sc->sc_init_counter = 0;
773 sc->sc_counter = tmp_counter;
775 sc_tv.tv_sec = sc->sc_advbase;
776 if (carp_suppress_preempt && sc->sc_advskew < 240)
777 sc_tv.tv_usec = 240 * 1000000 / 256;
779 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
780 ch_tv.tv_sec = ch->carp_advbase;
781 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
783 switch (sc->sc_state) {
789 * If we receive an advertisement from a master who's going to
790 * be more frequent than us, go into BACKUP state.
792 if (timevalcmp(&sc_tv, &ch_tv, >) ||
793 timevalcmp(&sc_tv, &ch_tv, ==)) {
794 callout_stop(&sc->sc_ad_tmo);
795 CARP_DEBUG("%s: MASTER -> BACKUP "
796 "(more frequent advertisement received)\n",
798 carp_set_state(sc, BACKUP);
800 carp_setroute(sc, RTM_DELETE);
806 * If we're pre-empting masters who advertise slower than us,
807 * and this one claims to be slower, treat him as down.
809 if (carp_opts[CARPCTL_PREEMPT] &&
810 timevalcmp(&sc_tv, &ch_tv, <)) {
811 CARP_DEBUG("%s: BACKUP -> MASTER "
812 "(preempting a slower master)\n", cifp->if_xname);
813 carp_master_down(sc);
818 * If the master is going to advertise at such a low frequency
819 * that he's guaranteed to time out, we'd might as well just
820 * treat him as timed out now.
822 sc_tv.tv_sec = sc->sc_advbase * 3;
823 if (timevalcmp(&sc_tv, &ch_tv, <)) {
824 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
826 carp_master_down(sc);
831 * Otherwise, we reset the counter and wait for the next
841 carp_input(void *v, struct mbuf *m)
843 struct carp_if *cif = v;
844 struct ether_header *eh;
845 struct carp_softc *sc;
848 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
850 eh = mtod(m, struct ether_header *);
852 ifp = carp_forus(cif, eh->ether_dhost);
854 ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF);
858 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
862 * XXX Should really check the list of multicast addresses
863 * for each CARP interface _before_ copying.
865 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
868 if ((sc->sc_if.if_flags & IFF_UP) == 0)
871 m0 = m_dup(m, MB_DONTWAIT);
875 ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF);
881 carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch)
883 if (sc->sc_init_counter) {
884 /* this could also be seconds since unix epoch */
885 sc->sc_counter = karc4random();
886 sc->sc_counter = sc->sc_counter << 32;
887 sc->sc_counter += karc4random();
892 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
893 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
895 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
899 carp_send_ad_all(void)
901 struct carp_softc *sc;
903 LIST_FOREACH(sc, &carpif_list, sc_next) {
904 if (sc->sc_carpdev == NULL)
907 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
913 carp_send_ad_timeout(void *xsc)
921 carp_send_ad(struct carp_softc *sc)
923 struct ifnet *cifp = &sc->sc_if;
924 struct carp_header ch;
926 struct carp_header *ch_ptr;
928 int len, advbase, advskew;
930 if (!CARP_IS_RUNNING(cifp)) {
935 advbase = sc->sc_advbase;
936 if (!carp_suppress_preempt || sc->sc_advskew > 240)
937 advskew = sc->sc_advskew;
941 tv.tv_usec = advskew * 1000000 / 256;
944 ch.carp_version = CARP_VERSION;
945 ch.carp_type = CARP_ADVERTISEMENT;
946 ch.carp_vhid = sc->sc_vhid;
947 ch.carp_advbase = advbase;
948 ch.carp_advskew = advskew;
949 ch.carp_authlen = 7; /* XXX DEFINE */
950 ch.carp_pad1 = 0; /* must be zero */
954 if (sc->sc_ia != NULL) {
957 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
960 carpstats.carps_onomem++;
961 /* XXX maybe less ? */
962 if (advbase != 255 || advskew != 255)
963 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
964 carp_send_ad_timeout, sc);
967 len = sizeof(*ip) + sizeof(ch);
968 m->m_pkthdr.len = len;
969 m->m_pkthdr.rcvif = NULL;
971 MH_ALIGN(m, m->m_len);
972 m->m_flags |= M_MCAST;
973 ip = mtod(m, struct ip *);
974 ip->ip_v = IPVERSION;
975 ip->ip_hl = sizeof(*ip) >> 2;
976 ip->ip_tos = IPTOS_LOWDELAY;
978 ip->ip_id = ip_newid();
980 ip->ip_ttl = CARP_DFLTTL;
981 ip->ip_p = IPPROTO_CARP;
983 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
984 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
986 ch_ptr = (struct carp_header *)(&ip[1]);
987 bcopy(&ch, ch_ptr, sizeof(ch));
988 carp_prepare_ad(sc, ch_ptr);
989 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
991 getmicrotime(&cifp->if_lastchange);
993 cifp->if_obytes += len;
994 carpstats.carps_opackets++;
996 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
998 if (sc->sc_sendad_errors < INT_MAX)
999 sc->sc_sendad_errors++;
1000 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1001 carp_suppress_preempt++;
1002 if (carp_suppress_preempt == 1) {
1006 sc->sc_sendad_success = 0;
1008 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1009 if (++sc->sc_sendad_success >=
1010 CARP_SENDAD_MIN_SUCCESS) {
1011 carp_suppress_preempt--;
1012 sc->sc_sendad_errors = 0;
1015 sc->sc_sendad_errors = 0;
1022 struct ip6_hdr *ip6;
1024 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1027 carpstats.carps_onomem++;
1028 /* XXX maybe less ? */
1029 if (advbase != 255 || advskew != 255)
1030 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1031 carp_send_ad_timeout, sc);
1034 len = sizeof(*ip6) + sizeof(ch);
1035 m->m_pkthdr.len = len;
1036 m->m_pkthdr.rcvif = NULL;
1038 MH_ALIGN(m, m->m_len);
1039 m->m_flags |= M_MCAST;
1040 ip6 = mtod(m, struct ip6_hdr *);
1041 bzero(ip6, sizeof(*ip6));
1042 ip6->ip6_vfc |= IPV6_VERSION;
1043 ip6->ip6_hlim = CARP_DFLTTL;
1044 ip6->ip6_nxt = IPPROTO_CARP;
1045 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1046 sizeof(struct in6_addr));
1047 /* set the multicast destination */
1049 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1050 ip6->ip6_dst.s6_addr8[15] = 0x12;
1051 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1054 CARP_LOG("%s: in6_setscope failed\n", __func__);
1058 ch_ptr = (struct carp_header *)(&ip6[1]);
1059 bcopy(&ch, ch_ptr, sizeof(ch));
1060 carp_prepare_ad(sc, ch_ptr);
1061 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
1063 getmicrotime(&cifp->if_lastchange);
1064 cifp->if_opackets++;
1065 cifp->if_obytes += len;
1066 carpstats.carps_opackets6++;
1068 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1070 if (sc->sc_sendad_errors < INT_MAX)
1071 sc->sc_sendad_errors++;
1072 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1073 carp_suppress_preempt++;
1074 if (carp_suppress_preempt == 1) {
1078 sc->sc_sendad_success = 0;
1080 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1081 if (++sc->sc_sendad_success >=
1082 CARP_SENDAD_MIN_SUCCESS) {
1083 carp_suppress_preempt--;
1084 sc->sc_sendad_errors = 0;
1087 sc->sc_sendad_errors = 0;
1093 if (advbase != 255 || advskew != 255)
1094 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1095 carp_send_ad_timeout, sc);
1099 * Broadcast a gratuitous ARP request containing
1100 * the virtual router MAC address for each IP address
1101 * associated with the virtual router.
1104 carp_send_arp(struct carp_softc *sc)
1106 const struct carp_vhaddr *vha;
1108 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1109 if (vha->vha_iaback == NULL)
1111 arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa);
1117 carp_send_na(struct carp_softc *sc)
1119 struct ifaddr_container *ifac;
1120 struct in6_addr *in6;
1121 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1123 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1124 struct ifaddr *ifa = ifac->ifa;
1126 if (ifa->ifa_addr->sa_family != AF_INET6)
1129 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1130 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1131 ND_NA_FLAG_OVERRIDE, 1, NULL);
1132 DELAY(1000); /* XXX */
1137 static __inline const struct carp_vhaddr *
1138 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1140 struct carp_vhaddr *vha;
1142 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1143 if (vha->vha_iaback == NULL)
1146 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1154 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1155 const struct in_addr *isaddr, uint8_t **enaddr)
1157 const struct carp_softc *vh;
1158 int index, count = 0;
1161 * XXX proof of concept implementation.
1162 * We use the source ip to decide which virtual host should
1163 * handle the request. If we're master of that virtual host,
1164 * then we respond, otherwise, just drop the arp packet on
1168 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1169 if (!CARP_IS_RUNNING(&vh->sc_if))
1172 if (carp_find_addr(vh, itaddr) != NULL)
1178 /* this should be a hash, like pf_hash() */
1179 index = ntohl(isaddr->s_addr) % count;
1182 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1183 if (!CARP_IS_RUNNING(&vh->sc_if))
1186 if (carp_find_addr(vh, itaddr) == NULL)
1189 if (count == index) {
1190 if (vh->sc_state == MASTER) {
1191 *enaddr = IF_LLADDR(&vh->sc_if);
1204 carp_iamatch(const struct in_ifaddr *ia)
1206 const struct carp_softc *sc = ia->ia_ifp->if_softc;
1208 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1211 if (carp_opts[CARPCTL_ARPBALANCE])
1212 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1215 if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER)
1223 carp_iamatch6(void *v, struct in6_addr *taddr)
1225 struct carp_if *cif = v;
1226 struct carp_softc *vh;
1228 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1230 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1231 struct ifaddr_container *ifac;
1233 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1235 struct ifaddr *ifa = ifac->ifa;
1237 if (IN6_ARE_ADDR_EQUAL(taddr,
1238 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1239 CARP_IS_RUNNING(&vh->sc_if) &&
1240 vh->sc_state == MASTER) {
1249 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1252 struct carp_if *cif = v;
1253 struct carp_softc *sc;
1255 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1257 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1258 struct ifaddr_container *ifac;
1260 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1262 struct ifaddr *ifa = ifac->ifa;
1264 if (IN6_ARE_ADDR_EQUAL(taddr,
1265 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1266 CARP_IS_RUNNING(&sc->sc_if)) {
1267 struct ifnet *ifp = &sc->sc_if;
1269 mtag = m_tag_get(PACKET_TAG_CARP,
1270 sizeof(struct ifnet *), MB_DONTWAIT);
1272 /* better a bit than nothing */
1273 return (IF_LLADDR(ifp));
1275 bcopy(&ifp, (caddr_t)(mtag + 1),
1276 sizeof(struct ifnet *));
1277 m_tag_prepend(m, mtag);
1279 return (IF_LLADDR(ifp));
1287 static struct ifnet *
1288 carp_forus(struct carp_if *cif, const uint8_t *dhost)
1290 struct carp_softc *sc;
1292 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1294 if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0)
1297 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1298 struct ifnet *ifp = &sc->sc_if;
1300 if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER &&
1301 !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN))
1308 carp_master_down_timeout(void *xsc)
1310 struct carp_softc *sc = xsc;
1312 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1313 sc->sc_if.if_xname);
1315 carp_master_down(sc);
1320 carp_master_down(struct carp_softc *sc)
1322 switch (sc->sc_state) {
1324 kprintf("%s: master_down event in INIT state\n",
1325 sc->sc_if.if_xname);
1332 carp_set_state(sc, MASTER);
1339 carp_setroute(sc, RTM_ADD);
1345 * When in backup state, af indicates whether to reset the master down timer
1346 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1349 carp_setrun(struct carp_softc *sc, sa_family_t af)
1351 struct ifnet *cifp = &sc->sc_if;
1354 if (sc->sc_carpdev == NULL) {
1355 carp_set_state(sc, INIT);
1359 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1360 (sc->sc_naddrs || sc->sc_naddrs6)) {
1363 carp_setroute(sc, RTM_DELETE);
1367 switch (sc->sc_state) {
1369 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1375 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1377 carp_set_state(sc, MASTER);
1378 carp_setroute(sc, RTM_ADD);
1380 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1381 carp_set_state(sc, BACKUP);
1382 carp_setroute(sc, RTM_DELETE);
1388 callout_stop(&sc->sc_ad_tmo);
1389 tv.tv_sec = 3 * sc->sc_advbase;
1390 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1394 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1395 carp_master_down_timeout, sc);
1400 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1401 carp_master_down_timeout, sc);
1406 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1407 carp_master_down_timeout, sc);
1409 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1410 carp_master_down_timeout, sc);
1416 tv.tv_sec = sc->sc_advbase;
1417 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1418 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1419 carp_send_ad_timeout, sc);
1425 carp_multicast_cleanup(struct carp_softc *sc)
1427 struct ip_moptions *imo = &sc->sc_imo;
1429 if (imo->imo_num_memberships == 0)
1431 KKASSERT(imo->imo_num_memberships == 1);
1433 in_delmulti(imo->imo_membership[0]);
1434 imo->imo_membership[0] = NULL;
1435 imo->imo_num_memberships = 0;
1436 imo->imo_multicast_ifp = NULL;
1441 carp_multicast6_cleanup(struct carp_softc *sc)
1443 struct ip6_moptions *im6o = &sc->sc_im6o;
1445 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1446 struct in6_multi_mship *imm =
1447 LIST_FIRST(&im6o->im6o_memberships);
1449 LIST_REMOVE(imm, i6mm_chain);
1450 in6_leavegroup(imm);
1452 im6o->im6o_multicast_ifp = NULL;
1457 carp_get_vhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1459 const struct carp_vhaddr *vha;
1460 struct ifcarpvhaddr *carpa, *carpa0;
1461 int count, len, error;
1464 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1467 if (ifd->ifd_len == 0) {
1468 ifd->ifd_len = count * sizeof(*carpa);
1470 } else if (count == 0 || ifd->ifd_len < sizeof(*carpa)) {
1474 len = min(ifd->ifd_len, sizeof(*carpa) * count);
1475 KKASSERT(len >= sizeof(*carpa));
1477 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1482 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1483 if (len < sizeof(*carpa))
1486 carpa->carpa_flags = vha->vha_flags;
1487 carpa->carpa_addr.sin_family = AF_INET;
1488 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1490 carpa->carpa_baddr.sin_family = AF_INET;
1491 if (vha->vha_iaback == NULL) {
1492 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1494 carpa->carpa_baddr.sin_addr =
1495 vha->vha_iaback->ia_addr.sin_addr;
1500 len -= sizeof(*carpa);
1502 ifd->ifd_len = sizeof(*carpa) * count;
1503 KKASSERT(ifd->ifd_len > 0);
1505 error = copyout(carpa0, ifd->ifd_data, ifd->ifd_len);
1506 kfree(carpa0, M_TEMP);
1511 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
1512 struct in_ifaddr *ia_del)
1515 struct in_ifaddr *ia_if;
1516 struct in_ifaddr_container *iac;
1517 const struct sockaddr_in *sin;
1521 KKASSERT(vha->vha_ia != NULL);
1523 sin = &vha->vha_ia->ia_addr;
1524 iaddr = ntohl(sin->sin_addr.s_addr);
1528 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1529 struct in_ifaddr *ia = iac->ia;
1534 if (ia->ia_ifp->if_type == IFT_CARP)
1537 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1540 /* and, yeah, we need a multicast-capable iface too */
1541 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1544 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1545 if (sin->sin_addr.s_addr ==
1546 ia->ia_addr.sin_addr.s_addr)
1550 else if (sc->sc_carpdev != NULL &&
1551 sc->sc_carpdev == ia->ia_ifp)
1556 carp_deactivate_vhaddr(sc, vha, FALSE);
1560 ifp = ia_if->ia_ifp;
1562 /* XXX Don't allow parent iface to be changed */
1563 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
1566 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
1570 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1572 struct carp_vhaddr *vha_new;
1573 struct in_ifaddr *carp_ia;
1575 struct carp_vhaddr *vha;
1578 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1579 carp_ia = ifatoia(carp_ifa);
1582 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1583 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
1586 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
1587 vha_new->vha_ia = carp_ia;
1588 carp_insert_vhaddr(sc, vha_new);
1590 if (carp_config_vhaddr(sc, vha_new, NULL) != 0) {
1592 * If the above configuration fails, it may only mean
1593 * that the new address is problematic. However, the
1594 * carp(4) interface may already have several working
1595 * addresses. Since the expected behaviour of
1596 * SIOC[AS]IFADDR is to put the NIC into working state,
1597 * we try starting the state machine manually here with
1598 * the hope that the carp(4)'s previously working
1599 * addresses still could be brought up.
1601 carp_hmac_prepare(sc);
1602 carp_set_state(sc, INIT);
1608 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1610 struct carp_vhaddr *vha;
1611 struct in_ifaddr *carp_ia;
1613 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1614 carp_ia = ifatoia(carp_ifa);
1616 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1617 KKASSERT(vha->vha_ia != NULL);
1618 if (vha->vha_ia == carp_ia)
1621 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1624 * Remove the vhaddr from the list before deactivating
1625 * the vhaddr, so that the HMAC could be correctly
1626 * updated in carp_deactivate_vhaddr()
1628 carp_remove_vhaddr(sc, vha);
1630 carp_deactivate_vhaddr(sc, vha, FALSE);
1635 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1637 struct carp_vhaddr *vha;
1638 struct in_ifaddr *carp_ia;
1640 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1641 carp_ia = ifatoia(carp_ifa);
1643 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1644 KKASSERT(vha->vha_ia != NULL);
1645 if (vha->vha_ia == carp_ia)
1648 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1650 /* Remove then reinsert, to keep the vhaddr list sorted */
1651 carp_remove_vhaddr(sc, vha);
1652 carp_insert_vhaddr(sc, vha);
1654 if (carp_config_vhaddr(sc, vha, NULL) != 0) {
1655 /* See the comment in carp_add_addr() */
1656 carp_hmac_prepare(sc);
1657 carp_set_state(sc, INIT);
1666 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1669 struct carp_if *cif;
1670 struct in6_ifaddr *ia, *ia_if;
1671 struct ip6_moptions *im6o = &sc->sc_im6o;
1672 struct in6_multi_mship *imm;
1673 struct in6_addr in6;
1676 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1681 /* we have to do it by hands to check we won't match on us */
1682 ia_if = NULL; own = 0;
1683 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1686 for (i = 0; i < 4; i++) {
1687 if ((sin6->sin6_addr.s6_addr32[i] &
1688 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1689 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1690 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1693 /* and, yeah, we need a multicast-capable iface too */
1694 if (ia->ia_ifp != &sc->sc_if &&
1695 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1699 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1700 &ia->ia_addr.sin6_addr))
1706 return (EADDRNOTAVAIL);
1710 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1711 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1712 return (EADDRNOTAVAIL);
1714 if (!sc->sc_naddrs6) {
1715 im6o->im6o_multicast_ifp = ifp;
1717 /* join CARP multicast address */
1718 bzero(&in6, sizeof(in6));
1719 in6.s6_addr16[0] = htons(0xff02);
1720 in6.s6_addr8[15] = 0x12;
1721 if (in6_setscope(&in6, ifp, NULL) != 0)
1723 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1725 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1727 /* join solicited multicast address */
1728 bzero(&in6, sizeof(in6));
1729 in6.s6_addr16[0] = htons(0xff02);
1730 in6.s6_addr32[1] = 0;
1731 in6.s6_addr32[2] = htonl(1);
1732 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1733 in6.s6_addr8[12] = 0xff;
1734 if (in6_setscope(&in6, ifp, NULL) != 0)
1736 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1738 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1741 if (!ifp->if_carp) {
1742 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
1744 if ((error = ifpromisc(ifp, 1))) {
1749 TAILQ_INIT(&cif->vhif_vrs);
1752 struct carp_softc *vr;
1755 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1756 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1763 sc->sc_carpdev = ifp;
1765 { /* XXX prevent endless loop if already in queue */
1766 struct carp_softc *vr, *after = NULL;
1770 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1773 if (vr->sc_vhid < sc->sc_vhid)
1778 /* We're trying to keep things in order */
1780 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1782 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1795 /* clean up multicast memberships */
1796 if (!sc->sc_naddrs6) {
1797 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1798 imm = LIST_FIRST(&im6o->im6o_memberships);
1799 LIST_REMOVE(imm, i6mm_chain);
1800 in6_leavegroup(imm);
1807 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1811 if (!--sc->sc_naddrs6) {
1812 struct carp_if *cif = sc->sc_carpdev->if_carp;
1813 struct ip6_moptions *im6o = &sc->sc_im6o;
1815 callout_stop(&sc->sc_ad_tmo);
1817 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1818 struct in6_multi_mship *imm =
1819 LIST_FIRST(&im6o->im6o_memberships);
1821 LIST_REMOVE(imm, i6mm_chain);
1822 in6_leavegroup(imm);
1824 im6o->im6o_multicast_ifp = NULL;
1825 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1826 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
1827 sc->sc_carpdev->if_carp = NULL;
1828 kfree(cif, M_IFADDR);
1838 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
1840 struct carp_softc *sc = ifp->if_softc, *vr;
1841 struct carpreq carpr;
1844 struct ifaliasreq *ifra;
1846 char devname[IFNAMSIZ];
1851 ifa = (struct ifaddr *)addr;
1852 ifra = (struct ifaliasreq *)addr;
1853 ifr = (struct ifreq *)addr;
1854 ifd = (struct ifdrv *)addr;
1858 if (ifp->if_flags & IFF_UP) {
1859 if ((ifp->if_flags & IFF_RUNNING) == 0)
1861 } else if (ifp->if_flags & IFF_RUNNING) {
1867 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1870 error = copyin(ifr->ifr_data, &carpr, sizeof(carpr));
1875 if ((ifp->if_flags & IFF_RUNNING) &&
1876 sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1877 switch (carpr.carpr_state) {
1879 callout_stop(&sc->sc_ad_tmo);
1880 carp_set_state(sc, BACKUP);
1882 carp_setroute(sc, RTM_DELETE);
1886 carp_master_down(sc);
1893 if (carpr.carpr_vhid > 0) {
1894 if (carpr.carpr_vhid > 255) {
1898 if (sc->sc_carpdev) {
1899 struct carp_if *cif = sc->sc_carpdev->if_carp;
1901 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1903 vr->sc_vhid == carpr.carpr_vhid) {
1909 sc->sc_vhid = carpr.carpr_vhid;
1911 IF_LLADDR(ifp)[5] = sc->sc_vhid;
1912 bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr,
1917 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1918 if (carpr.carpr_advskew >= 255) {
1922 if (carpr.carpr_advbase > 255) {
1926 sc->sc_advbase = carpr.carpr_advbase;
1927 sc->sc_advskew = carpr.carpr_advskew;
1930 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1940 bzero(&carpr, sizeof(carpr));
1941 carpr.carpr_state = sc->sc_state;
1942 carpr.carpr_vhid = sc->sc_vhid;
1943 carpr.carpr_advbase = sc->sc_advbase;
1944 carpr.carpr_advskew = sc->sc_advskew;
1945 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1947 bcopy(sc->sc_key, carpr.carpr_key,
1948 sizeof(carpr.carpr_key));
1951 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1955 switch (ifd->ifd_cmd) {
1957 if (ifd->ifd_len != sizeof(devname))
1971 switch (ifd->ifd_cmd) {
1973 error = carp_get_vhaddr(sc, ifd);
1977 bzero(devname, sizeof(devname));
1978 if (sc->sc_carpdev != NULL) {
1979 strlcpy(devname, sc->sc_carpdev->if_xname,
1982 error = copyout(devname, ifd->ifd_data,
1989 error = ether_ioctl(ifp, cmd, addr);
1992 carp_hmac_prepare(sc);
1999 carp_init(void *xsc)
2001 struct carp_softc *sc = xsc;
2005 sc->sc_if.if_flags |= IFF_RUNNING;
2006 carp_set_state(sc, INIT);
2013 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2016 struct carp_softc *sc = ifp->if_softc;
2020 if (sc->sc_carpdev) {
2023 * CARP's ifp is passed to backing device's
2026 sc->sc_carpdev->if_output(ifp, m, dst, rt);
2029 error = ENETUNREACH;
2037 * Start output on carp interface. This function should never be called.
2040 carp_start(struct ifnet *ifp)
2042 panic("%s: start called\n", ifp->if_xname);
2046 carp_serialize(struct ifnet *ifp __unused,
2047 enum ifnet_serialize slz __unused)
2052 carp_deserialize(struct ifnet *ifp __unused,
2053 enum ifnet_serialize slz __unused)
2058 carp_tryserialize(struct ifnet *ifp __unused,
2059 enum ifnet_serialize slz __unused)
2067 carp_serialize_assert(struct ifnet *ifp __unused,
2068 enum ifnet_serialize slz __unused, boolean_t serialized __unused)
2072 #endif /* INVARIANTS */
2075 carp_set_state(struct carp_softc *sc, int state)
2077 struct ifnet *cifp = &sc->sc_if;
2079 if (sc->sc_state == state)
2081 sc->sc_state = state;
2083 switch (sc->sc_state) {
2085 cifp->if_link_state = LINK_STATE_DOWN;
2089 cifp->if_link_state = LINK_STATE_UP;
2093 cifp->if_link_state = LINK_STATE_UNKNOWN;
2100 carp_group_demote_adj(struct ifnet *ifp, int adj)
2102 struct ifg_list *ifgl;
2107 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2108 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2110 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2117 if (adj > 0 && *dm == 1)
2119 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2120 ifgl->ifgl_group->ifg_group, *dm);
2127 carp_carpdev_state(void *v)
2129 struct carp_if *cif = v;
2130 struct carp_softc *sc;
2134 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2141 carp_sc_state(struct carp_softc *sc)
2143 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2144 callout_stop(&sc->sc_ad_tmo);
2145 callout_stop(&sc->sc_md_tmo);
2146 callout_stop(&sc->sc_md6_tmo);
2147 carp_set_state(sc, INIT);
2149 if (!sc->sc_suppress) {
2150 carp_suppress_preempt++;
2151 if (carp_suppress_preempt == 1)
2154 sc->sc_suppress = 1;
2156 carp_set_state(sc, INIT);
2158 if (sc->sc_suppress)
2159 carp_suppress_preempt--;
2160 sc->sc_suppress = 0;
2165 carp_stop(struct carp_softc *sc, int detach)
2167 sc->sc_if.if_flags &= ~IFF_RUNNING;
2169 callout_stop(&sc->sc_ad_tmo);
2170 callout_stop(&sc->sc_md_tmo);
2171 callout_stop(&sc->sc_md6_tmo);
2173 if (!detach && sc->sc_state == MASTER)
2176 if (sc->sc_suppress)
2177 carp_suppress_preempt--;
2178 sc->sc_suppress = 0;
2180 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2181 carp_suppress_preempt--;
2182 sc->sc_sendad_errors = 0;
2183 sc->sc_sendad_success = 0;
2185 carp_set_state(sc, INIT);
2190 carp_suspend(struct carp_softc *sc, int detach)
2192 struct ifnet *cifp = &sc->sc_if;
2194 carp_stop(sc, detach);
2196 /* Retain the running state, if we are not dead yet */
2197 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2198 cifp->if_flags |= IFF_RUNNING;
2202 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2203 struct ifnet *ifp, struct in_ifaddr *ia_if, int own)
2205 struct ip_moptions *imo = &sc->sc_imo;
2206 struct carp_if *cif;
2207 struct carp_softc *vr, *after = NULL;
2213 KKASSERT(vha->vha_ia != NULL);
2215 KASSERT(ia_if != NULL, ("NULL backing address\n"));
2216 KASSERT(vha->vha_iaback == NULL, ("%p is already activated\n", vha));
2217 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2218 ("inactive vhaddr %p is the address owner\n", vha));
2220 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2221 ("%s is already on %s\n", sc->sc_if.if_xname,
2222 sc->sc_carpdev->if_xname));
2224 if (!ifp->if_carp) {
2225 KASSERT(sc->sc_carpdev == NULL,
2226 ("%s is already on %s\n", sc->sc_if.if_xname,
2227 sc->sc_carpdev->if_xname));
2229 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2231 error = ifpromisc(ifp, 1);
2237 TAILQ_INIT(&cif->vhif_vrs);
2241 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2242 if (vr != sc && vr->sc_vhid == sc->sc_vhid)
2248 if (sc->sc_carpdev != NULL)
2254 sc->sc_carpdev = ifp;
2258 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2261 if (vr->sc_vhid < sc->sc_vhid)
2266 if (assert_onlist) {
2267 KASSERT(onlist, ("%s is not on %s carp list\n",
2268 sc->sc_if.if_xname, ifp->if_xname));
2270 KASSERT(!onlist, ("%s is already on %s carp list\n",
2271 sc->sc_if.if_xname, ifp->if_xname));
2276 /* We're trying to keep things in order */
2278 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2280 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2283 vha->vha_iaback = ia_if;
2287 vha->vha_flags |= CARP_VHAF_OWNER;
2289 /* XXX save user configured advskew? */
2293 carp_addroute_vhaddr(sc, vha);
2296 * Join the multicast group only after the backing interface
2297 * has been hooked with the CARP interface.
2299 KASSERT(imo->imo_multicast_ifp == NULL ||
2300 imo->imo_multicast_ifp == &sc->sc_if,
2301 ("%s didn't leave mcast group on %s\n",
2302 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2304 if (imo->imo_num_memberships == 0) {
2305 struct in_addr addr;
2307 addr.s_addr = htonl(INADDR_CARP_GROUP);
2308 imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if);
2309 if (imo->imo_membership[0] == NULL) {
2310 carp_deactivate_vhaddr(sc, vha, FALSE);
2314 imo->imo_num_memberships++;
2315 imo->imo_multicast_ifp = &sc->sc_if;
2316 imo->imo_multicast_ttl = CARP_DFLTTL;
2317 imo->imo_multicast_loop = 0;
2320 carp_hmac_prepare(sc);
2321 carp_set_state(sc, INIT);
2327 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2328 boolean_t del_iaback)
2330 KKASSERT(vha->vha_ia != NULL);
2332 carp_hmac_prepare(sc);
2334 if (vha->vha_iaback == NULL) {
2335 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2336 ("inactive vhaddr %p is the address owner\n", vha));
2340 vha->vha_flags &= ~CARP_VHAF_OWNER;
2341 carp_delroute_vhaddr(sc, vha, del_iaback);
2343 KKASSERT(sc->sc_naddrs > 0);
2344 vha->vha_iaback = NULL;
2346 if (!sc->sc_naddrs) {
2347 if (sc->sc_naddrs6) {
2348 carp_multicast_cleanup(sc);
2351 carp_detach(sc, 0, del_iaback);
2357 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2359 struct carp_vhaddr *vha;
2360 struct in_ifaddr *ia_if;
2362 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2363 ia_if = ifatoia(ifa_if);
2366 * Test each inactive vhaddr against the newly added address.
2367 * If the newly added address could be the backing address,
2368 * then activate the matching vhaddr.
2370 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2371 const struct in_ifaddr *ia;
2375 if (vha->vha_iaback != NULL)
2379 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr);
2381 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet)
2385 if (ia->ia_addr.sin_addr.s_addr ==
2386 ia_if->ia_addr.sin_addr.s_addr)
2389 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2394 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
2395 struct ifaddr *ifa_if)
2397 struct carp_vhaddr *vha;
2398 struct in_ifaddr *ia_if;
2400 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2401 ia_if = ifatoia(ifa_if);
2404 * Ad src address is deleted; set it to NULL.
2405 * Following loop will try pick up a new ad src address
2406 * if one of the vhaddr could retain its backing address.
2408 if (sc->sc_ia == ia_if)
2412 * Test each active vhaddr against the deleted address.
2413 * If the deleted address is vhaddr address's backing
2414 * address, then deactivate the vhaddr.
2416 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2417 if (vha->vha_iaback == NULL)
2420 if (vha->vha_iaback == ia_if)
2421 carp_deactivate_vhaddr(sc, vha, TRUE);
2422 else if (sc->sc_ia == NULL)
2423 sc->sc_ia = vha->vha_iaback;
2428 carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del)
2430 struct carp_vhaddr *vha;
2432 KKASSERT(sc->sc_carpdev == NULL);
2434 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2435 carp_config_vhaddr(sc, vha, ifatoia(ifa_del));
2439 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
2440 enum ifaddr_event event, struct ifaddr *ifa)
2442 struct carp_softc *sc;
2446 if (ifa->ifa_addr->sa_family != AF_INET)
2449 KASSERT(&curthread->td_msgport == cpu_portfn(0),
2450 ("not in netisr0"));
2452 if (ifp->if_type == IFT_CARP) {
2454 * Address is changed on carp(4) interface
2457 case IFADDR_EVENT_ADD:
2458 carp_add_addr(ifp->if_softc, ifa);
2461 case IFADDR_EVENT_CHANGE:
2462 carp_config_addr(ifp->if_softc, ifa);
2465 case IFADDR_EVENT_DELETE:
2466 carp_del_addr(ifp->if_softc, ifa);
2473 * Address is changed on non-carp(4) interface
2475 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2478 LIST_FOREACH(sc, &carpif_list, sc_next) {
2479 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
2480 /* Not the parent iface; skip */
2485 case IFADDR_EVENT_ADD:
2486 carp_link_addrs(sc, ifp, ifa);
2489 case IFADDR_EVENT_DELETE:
2490 if (sc->sc_carpdev != NULL) {
2491 carp_unlink_addrs(sc, ifp, ifa);
2492 if (sc->sc_carpdev == NULL) {
2494 * We no longer have the parent
2495 * interface, however, certain
2496 * virtual addresses, which are
2497 * not used because they can't
2498 * match the previous parent
2499 * interface's addresses, may now
2500 * match different interface's
2503 carp_update_addrs(sc, ifa);
2507 * The carp(4) interface didn't have a
2508 * parent iface, so it is not possible
2509 * that it will contain any address to
2515 case IFADDR_EVENT_CHANGE:
2516 if (sc->sc_carpdev == NULL) {
2518 * The carp(4) interface didn't have a
2519 * parent iface, so it is not possible
2520 * that it will contain any address to
2523 carp_link_addrs(sc, ifp, ifa);
2526 * First try breaking tie with the old
2527 * address. Then see whether we could
2528 * link certain vhaddr to the new address.
2529 * If that fails, i.e. carpdev is NULL,
2530 * we try a global update.
2532 * NOTE: The above order is critical.
2534 carp_unlink_addrs(sc, ifp, ifa);
2535 carp_link_addrs(sc, ifp, ifa);
2536 if (sc->sc_carpdev == NULL) {
2538 * See the comment in the above
2539 * IFADDR_EVENT_DELETE block.
2541 carp_update_addrs(sc, NULL);
2553 carp_proto_ctlinput(netmsg_t msg)
2555 int cmd = msg->ctlinput.nm_cmd;
2556 struct sockaddr *sa = msg->ctlinput.nm_arg;
2557 struct in_ifaddr_container *iac;
2561 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
2562 struct in_ifaddr *ia = iac->ia;
2563 struct ifnet *ifp = ia->ia_ifp;
2565 if (ifp->if_type == IFT_CARP)
2568 if (ia->ia_ifa.ifa_addr == sa) {
2569 if (cmd == PRC_IFDOWN) {
2570 carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE,
2572 } else if (cmd == PRC_IFUP) {
2573 carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD,
2581 lwkt_replymsg(&msg->lmsg, 0);
2587 lwkt_gettoken(&carp_tok);
2593 lwkt_reltoken(&carp_tok);
2597 carp_parent(struct ifnet *cifp)
2599 struct carp_softc *sc;
2601 ASSERT_LWKT_TOKEN_HELD(&carp_tok);
2603 KKASSERT(cifp->if_type == IFT_CARP);
2604 sc = cifp->if_softc;
2606 return sc->sc_carpdev;
2609 #define rtinitflags(x) \
2610 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
2614 carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
2616 struct in_ifaddr *ia, *iaback;
2619 if (sc->sc_state != MASTER)
2623 KKASSERT(ia != NULL);
2625 iaback = vha->vha_iaback;
2626 KKASSERT(iaback != NULL);
2628 rtinit(&iaback->ia_ifa, RTM_DELETE, rtinitflags(iaback));
2629 in_ifadown(&iaback->ia_ifa, 1);
2630 iaback->ia_flags &= ~IFA_ROUTE;
2632 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
2634 ia->ia_flags |= IFA_ROUTE;
2639 carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2640 boolean_t del_iaback)
2642 struct in_ifaddr *ia, *iaback;
2645 KKASSERT(ia != NULL);
2647 iaback = vha->vha_iaback;
2648 KKASSERT(iaback != NULL);
2650 rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia));
2651 in_ifadown(&ia->ia_ifa, 1);
2652 ia->ia_flags &= ~IFA_ROUTE;
2654 if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) {
2657 error = rtinit(&iaback->ia_ifa, RTM_ADD,
2658 rtinitflags(iaback) | RTF_UP);
2660 iaback->ia_flags |= IFA_ROUTE;
2665 carp_modevent(module_t mod, int type, void *data)
2669 LIST_INIT(&carpif_list);
2670 carp_ifdetach_event =
2671 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
2672 EVENTHANDLER_PRI_ANY);
2674 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
2675 EVENTHANDLER_PRI_FIRST);
2676 if_clone_attach(&carp_cloner);
2680 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
2681 carp_ifdetach_event);
2682 EVENTHANDLER_DEREGISTER(ifaddr_event,
2684 if_clone_detach(&carp_cloner);
2693 static moduledata_t carp_mod = {
2698 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);