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 $
28 * $DragonFly: src/sys/netinet/ip_carp.c,v 1.10 2008/07/27 10:06:57 sephe Exp $
33 #include "opt_inet6.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/in_cksum.h>
39 #include <sys/limits.h>
40 #include <sys/malloc.h>
45 #include <sys/sockio.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
50 #include <machine/stdarg.h>
51 #include <crypto/sha1.h>
54 #include <net/ethernet.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 #include <net/if_clone.h>
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/if_ether.h>
71 #include <netinet/icmp6.h>
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/scope6_var.h>
75 #include <netinet6/nd6.h>
78 #include <netinet/ip_carp.h>
80 #define CARP_IFNAME "carp"
81 #define CARP_IS_RUNNING(ifp) \
82 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
85 uint32_t vha_flags
; /* CARP_VHAF_ */
86 const struct in_ifaddr
*vha_ia
; /* carp address */
87 const struct in_ifaddr
*vha_iaback
; /* backing address */
88 TAILQ_ENTRY(carp_vhaddr
) vha_link
;
90 TAILQ_HEAD(carp_vhaddr_list
, carp_vhaddr
);
94 struct ifnet
*sc_carpdev
; /* parent interface */
95 struct carp_vhaddr_list sc_vha_list
; /* virtual addr list */
97 const struct in_ifaddr
*sc_ia
; /* primary iface address v4 */
98 struct ip_moptions sc_imo
;
101 struct in6_ifaddr
*sc_ia6
; /* primary iface address v6 */
102 struct ip6_moptions sc_im6o
;
104 TAILQ_ENTRY(carp_softc
) sc_list
;
106 enum { INIT
= 0, BACKUP
, MASTER
}
112 int sc_sendad_errors
;
113 #define CARP_SENDAD_MAX_ERRORS 3
114 int sc_sendad_success
;
115 #define CARP_SENDAD_MIN_SUCCESS 3
119 int sc_naddrs
; /* actually used IPv4 vha */
121 int sc_advbase
; /* seconds */
126 #define CARP_HMAC_PAD 64
127 unsigned char sc_key
[CARP_KEY_LEN
];
128 unsigned char sc_pad
[CARP_HMAC_PAD
];
131 struct callout sc_ad_tmo
; /* advertisement timeout */
132 struct callout sc_md_tmo
; /* master down timeout */
133 struct callout sc_md6_tmo
; /* master down timeout */
135 LIST_ENTRY(carp_softc
) sc_next
; /* Interface clue */
139 TAILQ_HEAD(, carp_softc
) vhif_vrs
;
142 enum { CARP_COUNT_MASTER
, CARP_COUNT_RUNNING
};
144 SYSCTL_DECL(_net_inet_carp
);
146 static int carp_opts
[CARPCTL_MAXID
] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
147 SYSCTL_INT(_net_inet_carp
, CARPCTL_ALLOW
, allow
, CTLFLAG_RW
,
148 &carp_opts
[CARPCTL_ALLOW
], 0, "Accept incoming CARP packets");
149 SYSCTL_INT(_net_inet_carp
, CARPCTL_PREEMPT
, preempt
, CTLFLAG_RW
,
150 &carp_opts
[CARPCTL_PREEMPT
], 0, "high-priority backup preemption mode");
151 SYSCTL_INT(_net_inet_carp
, CARPCTL_LOG
, log
, CTLFLAG_RW
,
152 &carp_opts
[CARPCTL_LOG
], 0, "log bad carp packets");
153 SYSCTL_INT(_net_inet_carp
, CARPCTL_ARPBALANCE
, arpbalance
, CTLFLAG_RW
,
154 &carp_opts
[CARPCTL_ARPBALANCE
], 0, "balance arp responses");
156 static int carp_suppress_preempt
= 0;
157 SYSCTL_INT(_net_inet_carp
, OID_AUTO
, suppress_preempt
, CTLFLAG_RD
,
158 &carp_suppress_preempt
, 0, "Preemption is suppressed");
160 static struct carpstats carpstats
;
161 SYSCTL_STRUCT(_net_inet_carp
, CARPCTL_STATS
, stats
, CTLFLAG_RW
,
162 &carpstats
, carpstats
,
163 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
165 #define CARP_LOG(...) do { \
166 if (carp_opts[CARPCTL_LOG] > 0) \
167 log(LOG_INFO, __VA_ARGS__); \
170 #define CARP_DEBUG(...) do { \
171 if (carp_opts[CARPCTL_LOG] > 1) \
172 log(LOG_DEBUG, __VA_ARGS__); \
175 static void carp_hmac_prepare(struct carp_softc
*);
176 static void carp_hmac_generate(struct carp_softc
*, uint32_t *,
178 static int carp_hmac_verify(struct carp_softc
*, uint32_t *,
180 static void carp_setroute(struct carp_softc
*, int);
181 static void carp_input_c(struct mbuf
*, struct carp_header
*, sa_family_t
);
182 static int carp_clone_create(struct if_clone
*, int);
183 static void carp_clone_destroy(struct ifnet
*);
184 static void carp_detach(struct carp_softc
*, int);
185 static int carp_prepare_ad(struct mbuf
*, struct carp_softc
*,
186 struct carp_header
*);
187 static void carp_send_ad_all(void);
188 static void carp_send_ad_timeout(void *);
189 static void carp_send_ad(struct carp_softc
*);
190 static void carp_send_arp(struct carp_softc
*);
191 static void carp_master_down_timeout(void *);
192 static void carp_master_down(struct carp_softc
*);
193 static int carp_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
194 static int carp_looutput(struct ifnet
*, struct mbuf
*, struct sockaddr
*,
196 static void carp_start(struct ifnet
*);
197 static void carp_setrun(struct carp_softc
*, sa_family_t
);
198 static void carp_set_state(struct carp_softc
*, int);
200 static void carp_multicast_cleanup(struct carp_softc
*);
201 static void carp_add_addr(struct carp_softc
*, struct ifaddr
*);
202 static void carp_del_addr(struct carp_softc
*, struct ifaddr
*);
203 static void carp_config_addr(struct carp_softc
*, struct ifaddr
*);
204 static void carp_link_addrs(struct carp_softc
*, struct ifnet
*,
206 static void carp_unlink_addrs(struct carp_softc
*, struct ifnet
*,
209 static int carp_get_vhaddr(struct carp_softc
*, struct ifdrv
*);
210 static int carp_config_vhaddr(struct carp_softc
*, struct carp_vhaddr
*);
211 static int carp_activate_vhaddr(struct carp_softc
*, struct carp_vhaddr
*,
212 struct ifnet
*, const struct in_ifaddr
*, int);
213 static void carp_deactivate_vhaddr(struct carp_softc
*,
214 struct carp_vhaddr
*);
216 static void carp_sc_state(struct carp_softc
*);
218 static void carp_send_na(struct carp_softc
*);
219 static int carp_set_addr6(struct carp_softc
*, struct sockaddr_in6
*);
220 static int carp_del_addr6(struct carp_softc
*, struct sockaddr_in6
*);
221 static void carp_multicast6_cleanup(struct carp_softc
*);
223 static void carp_stop(struct carp_softc
*, int);
224 static void carp_reset(struct carp_softc
*, int);
226 static void carp_ifaddr(void *, struct ifnet
*, enum ifaddr_event
,
228 static void carp_ifdetach(void *, struct ifnet
*);
230 static MALLOC_DEFINE(M_CARP
, "CARP", "CARP interfaces");
232 static LIST_HEAD(, carp_softc
) carpif_list
;
234 static struct if_clone carp_cloner
=
235 IF_CLONE_INITIALIZER(CARP_IFNAME
, carp_clone_create
, carp_clone_destroy
,
238 static eventhandler_tag carp_ifdetach_event
;
239 static eventhandler_tag carp_ifaddr_event
;
242 carp_insert_vhaddr(struct carp_softc
*sc
, struct carp_vhaddr
*vha_new
)
244 struct carp_vhaddr
*vha
;
245 u_long new_addr
, addr
;
247 KKASSERT((vha_new
->vha_flags
& CARP_VHAF_ONLIST
) == 0);
250 * Virtual address list is sorted; smaller one first
252 new_addr
= ntohl(vha_new
->vha_ia
->ia_addr
.sin_addr
.s_addr
);
254 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
) {
255 addr
= ntohl(vha
->vha_ia
->ia_addr
.sin_addr
.s_addr
);
261 TAILQ_INSERT_TAIL(&sc
->sc_vha_list
, vha_new
, vha_link
);
263 TAILQ_INSERT_BEFORE(vha
, vha_new
, vha_link
);
264 vha_new
->vha_flags
|= CARP_VHAF_ONLIST
;
268 carp_remove_vhaddr(struct carp_softc
*sc
, struct carp_vhaddr
*vha
)
270 KKASSERT(vha
->vha_flags
& CARP_VHAF_ONLIST
);
271 vha
->vha_flags
&= ~CARP_VHAF_ONLIST
;
272 TAILQ_REMOVE(&sc
->sc_vha_list
, vha
, vha_link
);
276 carp_hmac_prepare(struct carp_softc
*sc
)
278 uint8_t version
= CARP_VERSION
, type
= CARP_ADVERTISEMENT
;
279 uint8_t vhid
= sc
->sc_vhid
& 0xff;
280 struct ifaddr_container
*ifac
;
286 struct carp_vhaddr
*vha
;
289 /* XXX: possible race here */
291 /* compute ipad from key */
292 bzero(sc
->sc_pad
, sizeof(sc
->sc_pad
));
293 bcopy(sc
->sc_key
, sc
->sc_pad
, sizeof(sc
->sc_key
));
294 for (i
= 0; i
< sizeof(sc
->sc_pad
); i
++)
295 sc
->sc_pad
[i
] ^= 0x36;
297 /* precompute first part of inner hash */
298 SHA1Init(&sc
->sc_sha1
);
299 SHA1Update(&sc
->sc_sha1
, sc
->sc_pad
, sizeof(sc
->sc_pad
));
300 SHA1Update(&sc
->sc_sha1
, (void *)&version
, sizeof(version
));
301 SHA1Update(&sc
->sc_sha1
, (void *)&type
, sizeof(type
));
302 SHA1Update(&sc
->sc_sha1
, (void *)&vhid
, sizeof(vhid
));
304 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
) {
305 SHA1Update(&sc
->sc_sha1
,
306 (const uint8_t *)&vha
->vha_ia
->ia_addr
.sin_addr
,
307 sizeof(struct in_addr
));
311 TAILQ_FOREACH(ifac
, &sc
->sc_if
.if_addrheads
[mycpuid
], ifa_link
) {
312 struct ifaddr
*ifa
= ifac
->ifa
;
314 if (ifa
->ifa_addr
->sa_family
== AF_INET6
) {
315 in6
= ifatoia6(ifa
)->ia_addr
.sin6_addr
;
316 in6_clearscope(&in6
);
317 SHA1Update(&sc
->sc_sha1
, (void *)&in6
, sizeof(in6
));
322 /* convert ipad to opad */
323 for (i
= 0; i
< sizeof(sc
->sc_pad
); i
++)
324 sc
->sc_pad
[i
] ^= 0x36 ^ 0x5c;
328 carp_hmac_generate(struct carp_softc
*sc
, uint32_t counter
[2],
329 unsigned char md
[20])
333 /* fetch first half of inner hash */
334 bcopy(&sc
->sc_sha1
, &sha1ctx
, sizeof(sha1ctx
));
336 SHA1Update(&sha1ctx
, (void *)counter
, sizeof(sc
->sc_counter
));
337 SHA1Final(md
, &sha1ctx
);
341 SHA1Update(&sha1ctx
, sc
->sc_pad
, sizeof(sc
->sc_pad
));
342 SHA1Update(&sha1ctx
, md
, 20);
343 SHA1Final(md
, &sha1ctx
);
347 carp_hmac_verify(struct carp_softc
*sc
, uint32_t counter
[2],
348 unsigned char md
[20])
350 unsigned char md2
[20];
352 carp_hmac_generate(sc
, counter
, md2
);
353 return (bcmp(md
, md2
, sizeof(md2
)));
357 carp_setroute(struct carp_softc
*sc
, int cmd
)
360 struct ifaddr_container
*ifac
;
363 TAILQ_FOREACH(ifac
, &sc
->sc_if
.if_addrheads
[mycpuid
], ifa_link
) {
364 struct ifaddr
*ifa
= ifac
->ifa
;
366 if (ifa
->ifa_addr
->sa_family
== AF_INET6
) {
378 carp_clone_create(struct if_clone
*ifc
, int unit
)
380 struct carp_softc
*sc
;
383 sc
= kmalloc(sizeof(*sc
), M_CARP
, M_WAITOK
| M_ZERO
);
387 sc
->sc_advbase
= CARP_DFLTINTV
;
388 sc
->sc_vhid
= -1; /* required setting */
390 sc
->sc_init_counter
= 1;
394 TAILQ_INIT(&sc
->sc_vha_list
);
397 sc
->sc_im6o
.im6o_multicast_hlim
= CARP_DFLTTL
;
400 callout_init(&sc
->sc_ad_tmo
);
401 callout_init(&sc
->sc_md_tmo
);
402 callout_init(&sc
->sc_md6_tmo
);
405 if_initname(ifp
, CARP_IFNAME
, unit
);
406 ifp
->if_mtu
= ETHERMTU
;
407 ifp
->if_flags
= IFF_LOOPBACK
;
408 ifp
->if_ioctl
= carp_ioctl
;
409 ifp
->if_output
= carp_looutput
;
410 ifp
->if_start
= carp_start
;
411 ifp
->if_type
= IFT_CARP
;
412 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
414 if_attach(ifp
, NULL
);
415 bpfattach(ifp
, DLT_NULL
, sizeof(u_int
));
418 LIST_INSERT_HEAD(&carpif_list
, sc
, sc_next
);
425 carp_clone_destroy(struct ifnet
*ifp
)
427 struct carp_softc
*sc
= ifp
->if_softc
;
433 LIST_REMOVE(sc
, sc_next
);
438 KASSERT(sc
->sc_naddrs
== 0, ("certain inet address is still active\n"));
443 carp_detach(struct carp_softc
*sc
, int detach
)
447 carp_reset(sc
, detach
);
449 carp_multicast_cleanup(sc
);
451 carp_multicast6_cleanup(sc
);
454 if (!sc
->sc_dead
&& detach
) {
455 struct carp_vhaddr
*vha
;
457 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
)
458 carp_deactivate_vhaddr(sc
, vha
);
459 KKASSERT(sc
->sc_naddrs
== 0);
462 if (sc
->sc_carpdev
!= NULL
) {
463 cif
= sc
->sc_carpdev
->if_carp
;
464 TAILQ_REMOVE(&cif
->vhif_vrs
, sc
, sc_list
);
465 if (TAILQ_EMPTY(&cif
->vhif_vrs
)) {
466 ifpromisc(sc
->sc_carpdev
, 0);
467 sc
->sc_carpdev
->if_carp
= NULL
;
470 sc
->sc_carpdev
= NULL
;
475 /* Detach an interface from the carp. */
477 carp_ifdetach(void *arg __unused
, struct ifnet
*ifp
)
479 struct carp_if
*cif
= ifp
->if_carp
;
480 struct carp_softc
*sc
;
482 while (ifp
->if_carp
&&
483 (sc
= TAILQ_FIRST(&cif
->vhif_vrs
)) != NULL
)
488 * process input packet.
489 * we have rearranged checks order compared to the rfc,
490 * but it seems more efficient this way or not possible otherwise.
493 carp_input(struct mbuf
*m
, ...)
495 struct ip
*ip
= mtod(m
, struct ip
*);
496 struct carp_header
*ch
;
501 iphlen
= __va_arg(ap
, int);
504 carpstats
.carps_ipackets
++;
506 if (!carp_opts
[CARPCTL_ALLOW
]) {
511 /* Check if received on a valid carp interface */
512 if (m
->m_pkthdr
.rcvif
->if_carp
== NULL
) {
513 carpstats
.carps_badif
++;
514 CARP_LOG("carp_input: packet received on non-carp "
516 m
->m_pkthdr
.rcvif
->if_xname
);
521 /* Verify that the IP TTL is CARP_DFLTTL. */
522 if (ip
->ip_ttl
!= CARP_DFLTTL
) {
523 carpstats
.carps_badttl
++;
524 CARP_LOG("carp_input: received ttl %d != %d on %s\n",
525 ip
->ip_ttl
, CARP_DFLTTL
,
526 m
->m_pkthdr
.rcvif
->if_xname
);
531 /* Minimal CARP packet size */
532 len
= iphlen
+ sizeof(*ch
);
535 * Verify that the received packet length is
536 * not less than the CARP header
538 if (m
->m_pkthdr
.len
< len
) {
539 carpstats
.carps_badlen
++;
540 CARP_LOG("packet too short %d on %s\n", m
->m_pkthdr
.len
,
541 m
->m_pkthdr
.rcvif
->if_xname
);
546 /* Make sure that CARP header is contiguous */
547 if (len
> m
->m_len
) {
548 m
= m_pullup(m
, len
);
550 carpstats
.carps_hdrops
++;
551 CARP_LOG("carp_input: m_pullup failed\n");
554 ip
= mtod(m
, struct ip
*);
556 ch
= (struct carp_header
*)((uint8_t *)ip
+ iphlen
);
558 /* Verify the CARP checksum */
559 if (in_cksum_skip(m
, len
, iphlen
)) {
560 carpstats
.carps_badsum
++;
561 CARP_LOG("carp_input: checksum failed on %s\n",
562 m
->m_pkthdr
.rcvif
->if_xname
);
566 carp_input_c(m
, ch
, AF_INET
);
571 carp6_input(struct mbuf
**mp
, int *offp
, int proto
)
573 struct mbuf
*m
= *mp
;
574 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
575 struct carp_header
*ch
;
578 carpstats
.carps_ipackets6
++;
580 if (!carp_opts
[CARPCTL_ALLOW
]) {
582 return (IPPROTO_DONE
);
585 /* check if received on a valid carp interface */
586 if (m
->m_pkthdr
.rcvif
->if_carp
== NULL
) {
587 carpstats
.carps_badif
++;
588 CARP_LOG("carp6_input: packet received on non-carp "
590 m
->m_pkthdr
.rcvif
->if_xname
);
592 return (IPPROTO_DONE
);
595 /* verify that the IP TTL is 255 */
596 if (ip6
->ip6_hlim
!= CARP_DFLTTL
) {
597 carpstats
.carps_badttl
++;
598 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
600 m
->m_pkthdr
.rcvif
->if_xname
);
602 return (IPPROTO_DONE
);
605 /* verify that we have a complete carp packet */
607 IP6_EXTHDR_GET(ch
, struct carp_header
*, m
, *offp
, sizeof(*ch
));
609 carpstats
.carps_badlen
++;
610 CARP_LOG("carp6_input: packet size %u too small\n", len
);
611 return (IPPROTO_DONE
);
614 /* verify the CARP checksum */
615 if (in_cksum_range(m
, 0, *offp
, sizeof(*ch
))) {
616 carpstats
.carps_badsum
++;
617 CARP_LOG("carp6_input: checksum failed, on %s\n",
618 m
->m_pkthdr
.rcvif
->if_xname
);
620 return (IPPROTO_DONE
);
623 carp_input_c(m
, ch
, AF_INET6
);
624 return (IPPROTO_DONE
);
629 carp_input_c(struct mbuf
*m
, struct carp_header
*ch
, sa_family_t af
)
631 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
633 struct carp_softc
*sc
;
634 uint64_t tmp_counter
;
635 struct timeval sc_tv
, ch_tv
;
637 /* verify that the VHID is valid on the receiving interface */
638 TAILQ_FOREACH(sc
, &((struct carp_if
*)ifp
->if_carp
)->vhif_vrs
, sc_list
)
639 if (sc
->sc_vhid
== ch
->carp_vhid
)
642 if (!sc
|| !CARP_IS_RUNNING(&sc
->sc_if
)) {
643 carpstats
.carps_badvhid
++;
649 getmicrotime(&cifp
->if_lastchange
);
651 cifp
->if_ibytes
+= m
->m_pkthdr
.len
;
654 struct ip
*ip
= mtod(m
, struct ip
*);
656 /* BPF wants net byte order */
657 ip
->ip_len
= htons(ip
->ip_len
+ (ip
->ip_hl
<< 2));
658 ip
->ip_off
= htons(ip
->ip_off
);
659 bpf_mtap(cifp
->if_bpf
, m
);
662 /* verify the CARP version. */
663 if (ch
->carp_version
!= CARP_VERSION
) {
664 carpstats
.carps_badver
++;
666 CARP_LOG("%s; invalid version %d\n", cifp
->if_xname
,
672 /* verify the hash */
673 if (carp_hmac_verify(sc
, ch
->carp_counter
, ch
->carp_md
)) {
674 carpstats
.carps_badauth
++;
676 CARP_LOG("%s: incorrect hash\n", cifp
->if_xname
);
681 tmp_counter
= ntohl(ch
->carp_counter
[0]);
682 tmp_counter
= tmp_counter
<<32;
683 tmp_counter
+= ntohl(ch
->carp_counter
[1]);
685 /* XXX Replay protection goes here */
687 sc
->sc_init_counter
= 0;
688 sc
->sc_counter
= tmp_counter
;
690 sc_tv
.tv_sec
= sc
->sc_advbase
;
691 if (carp_suppress_preempt
&& sc
->sc_advskew
< 240)
692 sc_tv
.tv_usec
= 240 * 1000000 / 256;
694 sc_tv
.tv_usec
= sc
->sc_advskew
* 1000000 / 256;
695 ch_tv
.tv_sec
= ch
->carp_advbase
;
696 ch_tv
.tv_usec
= ch
->carp_advskew
* 1000000 / 256;
698 switch (sc
->sc_state
) {
704 * If we receive an advertisement from a master who's going to
705 * be more frequent than us, go into BACKUP state.
707 if (timevalcmp(&sc_tv
, &ch_tv
, >) ||
708 timevalcmp(&sc_tv
, &ch_tv
, ==)) {
709 callout_stop(&sc
->sc_ad_tmo
);
710 CARP_DEBUG("%s: MASTER -> BACKUP "
711 "(more frequent advertisement received)\n",
713 carp_set_state(sc
, BACKUP
);
715 carp_setroute(sc
, RTM_DELETE
);
721 * If we're pre-empting masters who advertise slower than us,
722 * and this one claims to be slower, treat him as down.
724 if (carp_opts
[CARPCTL_PREEMPT
] &&
725 timevalcmp(&sc_tv
, &ch_tv
, <)) {
726 CARP_DEBUG("%s: BACKUP -> MASTER "
727 "(preempting a slower master)\n", cifp
->if_xname
);
728 carp_master_down(sc
);
733 * If the master is going to advertise at such a low frequency
734 * that he's guaranteed to time out, we'd might as well just
735 * treat him as timed out now.
737 sc_tv
.tv_sec
= sc
->sc_advbase
* 3;
738 if (timevalcmp(&sc_tv
, &ch_tv
, <)) {
739 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
741 carp_master_down(sc
);
746 * Otherwise, we reset the counter and wait for the next
756 carp_prepare_ad(struct mbuf
*m
, struct carp_softc
*sc
, struct carp_header
*ch
)
758 struct ifnet
*cifp
= &sc
->sc_if
;
761 if (sc
->sc_init_counter
) {
762 /* this could also be seconds since unix epoch */
763 sc
->sc_counter
= karc4random();
764 sc
->sc_counter
= sc
->sc_counter
<< 32;
765 sc
->sc_counter
+= karc4random();
770 ch
->carp_counter
[0] = htonl((sc
->sc_counter
>> 32) & 0xffffffff);
771 ch
->carp_counter
[1] = htonl(sc
->sc_counter
& 0xffffffff);
773 carp_hmac_generate(sc
, ch
->carp_counter
, ch
->carp_md
);
775 /* Tag packet for carp_output */
776 mtag
= m_tag_get(PACKET_TAG_CARP
, sizeof(struct ifnet
*), MB_DONTWAIT
);
782 bcopy(&cifp
, (caddr_t
)(mtag
+ 1), sizeof(struct ifnet
*));
783 m_tag_prepend(m
, mtag
);
789 carp_send_ad_all(void)
791 struct carp_softc
*sc
;
793 LIST_FOREACH(sc
, &carpif_list
, sc_next
) {
794 if (sc
->sc_carpdev
== NULL
)
797 if (CARP_IS_RUNNING(&sc
->sc_if
) && sc
->sc_state
== MASTER
)
803 carp_send_ad_timeout(void *xsc
)
809 carp_send_ad(struct carp_softc
*sc
)
811 struct ifnet
*cifp
= &sc
->sc_if
;
812 struct carp_header ch
;
814 struct carp_header
*ch_ptr
;
816 int len
, advbase
, advskew
;
818 if (!CARP_IS_RUNNING(cifp
)) {
823 advbase
= sc
->sc_advbase
;
824 if (!carp_suppress_preempt
|| sc
->sc_advskew
> 240)
825 advskew
= sc
->sc_advskew
;
829 tv
.tv_usec
= advskew
* 1000000 / 256;
832 ch
.carp_version
= CARP_VERSION
;
833 ch
.carp_type
= CARP_ADVERTISEMENT
;
834 ch
.carp_vhid
= sc
->sc_vhid
;
835 ch
.carp_advbase
= advbase
;
836 ch
.carp_advskew
= advskew
;
837 ch
.carp_authlen
= 7; /* XXX DEFINE */
838 ch
.carp_pad1
= 0; /* must be zero */
842 if (sc
->sc_ia
!= NULL
) {
845 MGETHDR(m
, MB_DONTWAIT
, MT_HEADER
);
848 carpstats
.carps_onomem
++;
849 /* XXX maybe less ? */
850 if (advbase
!= 255 || advskew
!= 255)
851 callout_reset(&sc
->sc_ad_tmo
, tvtohz_high(&tv
),
852 carp_send_ad_timeout
, sc
);
855 len
= sizeof(*ip
) + sizeof(ch
);
856 m
->m_pkthdr
.len
= len
;
857 m
->m_pkthdr
.rcvif
= NULL
;
859 MH_ALIGN(m
, m
->m_len
);
860 m
->m_flags
|= M_MCAST
;
861 ip
= mtod(m
, struct ip
*);
862 ip
->ip_v
= IPVERSION
;
863 ip
->ip_hl
= sizeof(*ip
) >> 2;
864 ip
->ip_tos
= IPTOS_LOWDELAY
;
866 ip
->ip_id
= ip_newid();
868 ip
->ip_ttl
= CARP_DFLTTL
;
869 ip
->ip_p
= IPPROTO_CARP
;
871 ip
->ip_src
= sc
->sc_ia
->ia_addr
.sin_addr
;
872 ip
->ip_dst
.s_addr
= htonl(INADDR_CARP_GROUP
);
874 ch_ptr
= (struct carp_header
*)(&ip
[1]);
875 bcopy(&ch
, ch_ptr
, sizeof(ch
));
876 if (carp_prepare_ad(m
, sc
, ch_ptr
))
878 ch_ptr
->carp_cksum
= in_cksum_skip(m
, len
, sizeof(*ip
));
880 getmicrotime(&cifp
->if_lastchange
);
882 cifp
->if_obytes
+= len
;
883 carpstats
.carps_opackets
++;
885 if (ip_output(m
, NULL
, NULL
, IP_RAWOUTPUT
, &sc
->sc_imo
, NULL
)) {
887 if (sc
->sc_sendad_errors
< INT_MAX
)
888 sc
->sc_sendad_errors
++;
889 if (sc
->sc_sendad_errors
== CARP_SENDAD_MAX_ERRORS
) {
890 carp_suppress_preempt
++;
891 if (carp_suppress_preempt
== 1) {
895 sc
->sc_sendad_success
= 0;
897 if (sc
->sc_sendad_errors
>= CARP_SENDAD_MAX_ERRORS
) {
898 if (++sc
->sc_sendad_success
>=
899 CARP_SENDAD_MIN_SUCCESS
) {
900 carp_suppress_preempt
--;
901 sc
->sc_sendad_errors
= 0;
904 sc
->sc_sendad_errors
= 0;
913 MGETHDR(m
, MB_DONTWAIT
, MT_HEADER
);
916 carpstats
.carps_onomem
++;
917 /* XXX maybe less ? */
918 if (advbase
!= 255 || advskew
!= 255)
919 callout_reset(&sc
->sc_ad_tmo
, tvtohz_high(&tv
),
920 carp_send_ad_timeout
, sc
);
923 len
= sizeof(*ip6
) + sizeof(ch
);
924 m
->m_pkthdr
.len
= len
;
925 m
->m_pkthdr
.rcvif
= NULL
;
927 MH_ALIGN(m
, m
->m_len
);
928 m
->m_flags
|= M_MCAST
;
929 ip6
= mtod(m
, struct ip6_hdr
*);
930 bzero(ip6
, sizeof(*ip6
));
931 ip6
->ip6_vfc
|= IPV6_VERSION
;
932 ip6
->ip6_hlim
= CARP_DFLTTL
;
933 ip6
->ip6_nxt
= IPPROTO_CARP
;
934 bcopy(&sc
->sc_ia6
->ia_addr
.sin6_addr
, &ip6
->ip6_src
,
935 sizeof(struct in6_addr
));
936 /* set the multicast destination */
938 ip6
->ip6_dst
.s6_addr16
[0] = htons(0xff02);
939 ip6
->ip6_dst
.s6_addr8
[15] = 0x12;
940 if (in6_setscope(&ip6
->ip6_dst
, sc
->sc_carpdev
, NULL
) != 0) {
943 CARP_LOG("%s: in6_setscope failed\n", __func__
);
947 ch_ptr
= (struct carp_header
*)(&ip6
[1]);
948 bcopy(&ch
, ch_ptr
, sizeof(ch
));
949 if (carp_prepare_ad(m
, sc
, ch_ptr
))
951 ch_ptr
->carp_cksum
= in_cksum_skip(m
, len
, sizeof(*ip6
));
953 getmicrotime(&cifp
->if_lastchange
);
955 cifp
->if_obytes
+= len
;
956 carpstats
.carps_opackets6
++;
958 if (ip6_output(m
, NULL
, NULL
, 0, &sc
->sc_im6o
, NULL
, NULL
)) {
960 if (sc
->sc_sendad_errors
< INT_MAX
)
961 sc
->sc_sendad_errors
++;
962 if (sc
->sc_sendad_errors
== CARP_SENDAD_MAX_ERRORS
) {
963 carp_suppress_preempt
++;
964 if (carp_suppress_preempt
== 1) {
968 sc
->sc_sendad_success
= 0;
970 if (sc
->sc_sendad_errors
>= CARP_SENDAD_MAX_ERRORS
) {
971 if (++sc
->sc_sendad_success
>=
972 CARP_SENDAD_MIN_SUCCESS
) {
973 carp_suppress_preempt
--;
974 sc
->sc_sendad_errors
= 0;
977 sc
->sc_sendad_errors
= 0;
983 if (advbase
!= 255 || advskew
!= 255)
984 callout_reset(&sc
->sc_ad_tmo
, tvtohz_high(&tv
),
985 carp_send_ad_timeout
, sc
);
989 * Broadcast a gratuitous ARP request containing
990 * the virtual router MAC address for each IP address
991 * associated with the virtual router.
994 carp_send_arp(struct carp_softc
*sc
)
996 const struct carp_vhaddr
*vha
;
998 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
) {
999 if (vha
->vha_iaback
== NULL
)
1002 arp_iainit(sc
->sc_carpdev
, &vha
->vha_ia
->ia_addr
.sin_addr
,
1003 IF_LLADDR(&sc
->sc_if
));
1009 carp_send_na(struct carp_softc
*sc
)
1011 struct ifaddr_container
*ifac
;
1012 struct in6_addr
*in6
;
1013 static struct in6_addr mcast
= IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
1015 TAILQ_FOREACH(ifac
, &sc
->sc_if
.if_addrheads
[mycpuid
], ifa_link
) {
1016 struct ifaddr
*ifa
= ifac
->ifa
;
1018 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1021 in6
= &ifatoia6(ifa
)->ia_addr
.sin6_addr
;
1022 nd6_na_output(sc
->sc_carpdev
, &mcast
, in6
,
1023 ND_NA_FLAG_OVERRIDE
, 1, NULL
);
1024 DELAY(1000); /* XXX */
1029 static __inline
const struct carp_vhaddr
*
1030 carp_find_addr(const struct carp_softc
*sc
, const struct in_addr
*addr
)
1032 struct carp_vhaddr
*vha
;
1034 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
) {
1035 if (vha
->vha_iaback
== NULL
)
1038 if (vha
->vha_ia
->ia_addr
.sin_addr
.s_addr
== addr
->s_addr
)
1045 carp_iamatch_balance(const struct carp_if
*cif
, const struct in_addr
*itaddr
,
1046 const struct in_addr
*isaddr
, uint8_t **enaddr
)
1048 const struct carp_softc
*vh
;
1049 int index
, count
= 0;
1052 * XXX proof of concept implementation.
1053 * We use the source ip to decide which virtual host should
1054 * handle the request. If we're master of that virtual host,
1055 * then we respond, otherwise, just drop the arp packet on
1059 TAILQ_FOREACH(vh
, &cif
->vhif_vrs
, sc_list
) {
1060 if (!CARP_IS_RUNNING(&vh
->sc_if
))
1063 if (carp_find_addr(vh
, itaddr
) != NULL
)
1069 /* this should be a hash, like pf_hash() */
1070 index
= ntohl(isaddr
->s_addr
) % count
;
1073 TAILQ_FOREACH(vh
, &cif
->vhif_vrs
, sc_list
) {
1074 if (!CARP_IS_RUNNING(&vh
->sc_if
))
1077 if (carp_find_addr(vh
, itaddr
) == NULL
)
1080 if (count
== index
) {
1081 if (vh
->sc_state
== MASTER
) {
1082 *enaddr
= IF_LLADDR(&vh
->sc_if
);
1094 carp_iamatch(const void *v
, const struct in_addr
*itaddr
,
1095 const struct in_addr
*isaddr
, uint8_t **enaddr
)
1097 const struct carp_if
*cif
= v
;
1098 const struct carp_softc
*vh
;
1100 if (carp_opts
[CARPCTL_ARPBALANCE
])
1101 return carp_iamatch_balance(cif
, itaddr
, isaddr
, enaddr
);
1103 TAILQ_FOREACH(vh
, &cif
->vhif_vrs
, sc_list
) {
1104 if (!CARP_IS_RUNNING(&vh
->sc_if
) || vh
->sc_state
!= MASTER
)
1107 if (carp_find_addr(vh
, itaddr
) != NULL
) {
1108 *enaddr
= IF_LLADDR(&vh
->sc_if
);
1117 carp_iamatch6(void *v
, struct in6_addr
*taddr
)
1119 struct carp_if
*cif
= v
;
1120 struct carp_softc
*vh
;
1122 TAILQ_FOREACH(vh
, &cif
->vhif_vrs
, sc_list
) {
1123 struct ifaddr_container
*ifac
;
1125 TAILQ_FOREACH(ifac
, &vh
->sc_if
.if_addrheads
[mycpuid
],
1127 struct ifaddr
*ifa
= ifac
->ifa
;
1129 if (IN6_ARE_ADDR_EQUAL(taddr
,
1130 &ifatoia6(ifa
)->ia_addr
.sin6_addr
) &&
1131 CARP_IS_RUNNING(&vh
->sc_if
) &&
1132 vh
->sc_state
== MASTER
) {
1141 carp_macmatch6(void *v
, struct mbuf
*m
, const struct in6_addr
*taddr
)
1144 struct carp_if
*cif
= v
;
1145 struct carp_softc
*sc
;
1147 TAILQ_FOREACH(sc
, &cif
->vhif_vrs
, sc_list
) {
1148 struct ifaddr_container
*ifac
;
1150 TAILQ_FOREACH(ifac
, &sc
->sc_if
.if_addrheads
[mycpuid
],
1152 struct ifaddr
*ifa
= ifac
->ifa
;
1154 if (IN6_ARE_ADDR_EQUAL(taddr
,
1155 &ifatoia6(ifa
)->ia_addr
.sin6_addr
) &&
1156 CARP_IS_RUNNING(&sc
->sc_if
)) {
1157 struct ifnet
*ifp
= &sc
->sc_if
;
1159 mtag
= m_tag_get(PACKET_TAG_CARP
,
1160 sizeof(struct ifnet
*), MB_DONTWAIT
);
1162 /* better a bit than nothing */
1163 return (IF_LLADDR(ifp
));
1165 bcopy(&ifp
, (caddr_t
)(mtag
+ 1),
1166 sizeof(struct ifnet
*));
1167 m_tag_prepend(m
, mtag
);
1169 return (IF_LLADDR(ifp
));
1178 carp_forus(const void *v
, const void *dhost
)
1180 const struct carp_if
*cif
= v
;
1181 const struct carp_softc
*vh
;
1182 const uint8_t *ena
= dhost
;
1184 if (ena
[0] || ena
[1] || ena
[2] != 0x5e || ena
[3] || ena
[4] != 1)
1187 TAILQ_FOREACH(vh
, &cif
->vhif_vrs
, sc_list
) {
1188 const struct ifnet
*cifp
= &vh
->sc_if
;
1190 if (CARP_IS_RUNNING(cifp
) && vh
->sc_state
== MASTER
&&
1191 !bcmp(dhost
, IF_LLADDR(cifp
), ETHER_ADDR_LEN
))
1198 carp_master_down_timeout(void *xsc
)
1200 struct carp_softc
*sc
= xsc
;
1202 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1203 sc
->sc_if
.if_xname
);
1204 carp_master_down(sc
);
1208 carp_master_down(struct carp_softc
*sc
)
1210 switch (sc
->sc_state
) {
1212 kprintf("%s: master_down event in INIT state\n",
1213 sc
->sc_if
.if_xname
);
1220 carp_set_state(sc
, MASTER
);
1227 carp_setroute(sc
, RTM_ADD
);
1233 * When in backup state, af indicates whether to reset the master down timer
1234 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1237 carp_setrun(struct carp_softc
*sc
, sa_family_t af
)
1239 struct ifnet
*cifp
= &sc
->sc_if
;
1242 if (sc
->sc_carpdev
== NULL
) {
1243 carp_set_state(sc
, INIT
);
1247 if ((cifp
->if_flags
& IFF_RUNNING
) && sc
->sc_vhid
> 0 &&
1248 (sc
->sc_naddrs
|| sc
->sc_naddrs6
)) {
1251 carp_setroute(sc
, RTM_DELETE
);
1255 switch (sc
->sc_state
) {
1257 if (carp_opts
[CARPCTL_PREEMPT
] && !carp_suppress_preempt
) {
1263 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1265 carp_set_state(sc
, MASTER
);
1266 carp_setroute(sc
, RTM_ADD
);
1268 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp
->if_xname
);
1269 carp_set_state(sc
, BACKUP
);
1270 carp_setroute(sc
, RTM_DELETE
);
1276 callout_stop(&sc
->sc_ad_tmo
);
1277 tv
.tv_sec
= 3 * sc
->sc_advbase
;
1278 tv
.tv_usec
= sc
->sc_advskew
* 1000000 / 256;
1282 callout_reset(&sc
->sc_md_tmo
, tvtohz_high(&tv
),
1283 carp_master_down_timeout
, sc
);
1288 callout_reset(&sc
->sc_md6_tmo
, tvtohz_high(&tv
),
1289 carp_master_down_timeout
, sc
);
1294 callout_reset(&sc
->sc_md_tmo
, tvtohz_high(&tv
),
1295 carp_master_down_timeout
, sc
);
1297 callout_reset(&sc
->sc_md6_tmo
, tvtohz_high(&tv
),
1298 carp_master_down_timeout
, sc
);
1304 tv
.tv_sec
= sc
->sc_advbase
;
1305 tv
.tv_usec
= sc
->sc_advskew
* 1000000 / 256;
1306 callout_reset(&sc
->sc_ad_tmo
, tvtohz_high(&tv
),
1307 carp_send_ad_timeout
, sc
);
1313 carp_multicast_cleanup(struct carp_softc
*sc
)
1315 struct ip_moptions
*imo
= &sc
->sc_imo
;
1317 if (imo
->imo_num_memberships
== 0)
1319 KKASSERT(imo
->imo_num_memberships
== 1);
1321 in_delmulti(imo
->imo_membership
[0]);
1322 imo
->imo_membership
[0] = NULL
;
1323 imo
->imo_num_memberships
= 0;
1324 imo
->imo_multicast_ifp
= NULL
;
1329 carp_multicast6_cleanup(struct carp_softc
*sc
)
1331 struct ip6_moptions
*im6o
= &sc
->sc_im6o
;
1333 while (!LIST_EMPTY(&im6o
->im6o_memberships
)) {
1334 struct in6_multi_mship
*imm
=
1335 LIST_FIRST(&im6o
->im6o_memberships
);
1337 LIST_REMOVE(imm
, i6mm_chain
);
1338 in6_leavegroup(imm
);
1340 im6o
->im6o_multicast_ifp
= NULL
;
1345 carp_get_vhaddr(struct carp_softc
*sc
, struct ifdrv
*ifd
)
1347 const struct carp_vhaddr
*vha
;
1348 struct ifcarpvhaddr
*carpa
, *carpa0
;
1349 int count
, len
, error
;
1352 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
)
1355 if (ifd
->ifd_len
== 0) {
1356 ifd
->ifd_len
= count
* sizeof(*carpa
);
1358 } else if (count
== 0 || ifd
->ifd_len
< sizeof(*carpa
)) {
1362 len
= min(ifd
->ifd_len
, sizeof(*carpa
) * count
);
1363 KKASSERT(len
>= sizeof(*carpa
));
1365 carpa0
= carpa
= kmalloc(len
, M_TEMP
, M_WAITOK
| M_NULLOK
| M_ZERO
);
1370 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
) {
1371 if (len
< sizeof(*carpa
))
1374 carpa
->carpa_flags
= vha
->vha_flags
;
1375 carpa
->carpa_addr
.sin_family
= AF_INET
;
1376 carpa
->carpa_addr
.sin_addr
= vha
->vha_ia
->ia_addr
.sin_addr
;
1378 carpa
->carpa_baddr
.sin_family
= AF_INET
;
1379 if (vha
->vha_iaback
== NULL
) {
1380 carpa
->carpa_baddr
.sin_addr
.s_addr
= INADDR_ANY
;
1382 carpa
->carpa_baddr
.sin_addr
=
1383 vha
->vha_iaback
->ia_addr
.sin_addr
;
1388 len
-= sizeof(*carpa
);
1390 ifd
->ifd_len
= sizeof(*carpa
) * count
;
1391 KKASSERT(ifd
->ifd_len
> 0);
1393 error
= copyout(carpa0
, ifd
->ifd_data
, ifd
->ifd_len
);
1394 kfree(carpa0
, M_TEMP
);
1399 carp_config_vhaddr(struct carp_softc
*sc
, struct carp_vhaddr
*vha
)
1402 struct in_ifaddr
*ia_if
;
1403 struct in_ifaddr_container
*iac
;
1404 const struct sockaddr_in
*sin
;
1408 KKASSERT(vha
->vha_ia
!= NULL
);
1410 sin
= &vha
->vha_ia
->ia_addr
;
1411 iaddr
= ntohl(sin
->sin_addr
.s_addr
);
1415 TAILQ_FOREACH(iac
, &in_ifaddrheads
[mycpuid
], ia_link
) {
1416 struct in_ifaddr
*ia
= iac
->ia
;
1418 if ((ia
->ia_flags
& IFA_ROUTE
) == 0)
1421 if (ia
->ia_ifp
->if_type
== IFT_CARP
)
1424 /* and, yeah, we need a multicast-capable iface too */
1425 if ((ia
->ia_ifp
->if_flags
& IFF_MULTICAST
) == 0)
1428 if ((iaddr
& ia
->ia_subnetmask
) == ia
->ia_subnet
) {
1429 if (sin
->sin_addr
.s_addr
==
1430 ia
->ia_addr
.sin_addr
.s_addr
)
1434 else if (sc
->sc_carpdev
!= NULL
&&
1435 sc
->sc_carpdev
== ia
->ia_ifp
)
1440 carp_deactivate_vhaddr(sc
, vha
);
1444 ifp
= ia_if
->ia_ifp
;
1446 /* XXX Don't allow parent iface to be changed */
1447 if (sc
->sc_carpdev
!= NULL
&& sc
->sc_carpdev
!= ifp
)
1450 return carp_activate_vhaddr(sc
, vha
, ifp
, ia_if
, own
);
1454 carp_add_addr(struct carp_softc
*sc
, struct ifaddr
*carp_ifa
)
1456 struct carp_vhaddr
*vha_new
;
1457 struct in_ifaddr
*carp_ia
;
1459 struct carp_vhaddr
*vha
;
1462 KKASSERT(carp_ifa
->ifa_addr
->sa_family
== AF_INET
);
1463 carp_ia
= ifatoia(carp_ifa
);
1466 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
)
1467 KKASSERT(vha
->vha_ia
!= NULL
&& vha
->vha_ia
!= carp_ia
);
1470 vha_new
= kmalloc(sizeof(*vha_new
), M_CARP
, M_WAITOK
| M_ZERO
);
1471 vha_new
->vha_ia
= carp_ia
;
1472 carp_insert_vhaddr(sc
, vha_new
);
1474 if (carp_config_vhaddr(sc
, vha_new
) != 0) {
1476 * If the above configuration fails, it may only mean
1477 * that the new address is problematic. However, the
1478 * carp(4) interface may already have several working
1479 * addresses. Since the expected behaviour of
1480 * SIOC[AS]IFADDR is to put the NIC into working state,
1481 * we try starting the state machine manually here with
1482 * the hope that the carp(4)'s previously working
1483 * addresses still could be brought up.
1485 carp_hmac_prepare(sc
);
1486 carp_set_state(sc
, INIT
);
1492 carp_del_addr(struct carp_softc
*sc
, struct ifaddr
*carp_ifa
)
1494 struct carp_vhaddr
*vha
;
1495 struct in_ifaddr
*carp_ia
;
1497 KKASSERT(carp_ifa
->ifa_addr
->sa_family
== AF_INET
);
1498 carp_ia
= ifatoia(carp_ifa
);
1500 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
) {
1501 KKASSERT(vha
->vha_ia
!= NULL
);
1502 if (vha
->vha_ia
== carp_ia
)
1505 KASSERT(vha
!= NULL
, ("no corresponding vhaddr %p\n", carp_ifa
));
1508 * Remove the vhaddr from the list before deactivating
1509 * the vhaddr, so that the HMAC could be correctly
1510 * updated in carp_deactivate_vhaddr()
1512 carp_remove_vhaddr(sc
, vha
);
1514 carp_deactivate_vhaddr(sc
, vha
);
1519 carp_config_addr(struct carp_softc
*sc
, struct ifaddr
*carp_ifa
)
1521 struct carp_vhaddr
*vha
;
1522 struct in_ifaddr
*carp_ia
;
1524 KKASSERT(carp_ifa
->ifa_addr
->sa_family
== AF_INET
);
1525 carp_ia
= ifatoia(carp_ifa
);
1527 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
) {
1528 KKASSERT(vha
->vha_ia
!= NULL
);
1529 if (vha
->vha_ia
== carp_ia
)
1532 KASSERT(vha
!= NULL
, ("no corresponding vhaddr %p\n", carp_ifa
));
1534 /* Remove then reinsert, to keep the vhaddr list sorted */
1535 carp_remove_vhaddr(sc
, vha
);
1536 carp_insert_vhaddr(sc
, vha
);
1538 if (carp_config_vhaddr(sc
, vha
) != 0) {
1539 /* See the comment in carp_add_addr() */
1540 carp_hmac_prepare(sc
);
1541 carp_set_state(sc
, INIT
);
1548 carp_set_addr6(struct carp_softc
*sc
, struct sockaddr_in6
*sin6
)
1551 struct carp_if
*cif
;
1552 struct in6_ifaddr
*ia
, *ia_if
;
1553 struct ip6_moptions
*im6o
= &sc
->sc_im6o
;
1554 struct in6_multi_mship
*imm
;
1555 struct in6_addr in6
;
1558 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
1563 /* we have to do it by hands to check we won't match on us */
1564 ia_if
= NULL
; own
= 0;
1565 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
1568 for (i
= 0; i
< 4; i
++) {
1569 if ((sin6
->sin6_addr
.s6_addr32
[i
] &
1570 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
]) !=
1571 (ia
->ia_addr
.sin6_addr
.s6_addr32
[i
] &
1572 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
]))
1575 /* and, yeah, we need a multicast-capable iface too */
1576 if (ia
->ia_ifp
!= &sc
->sc_if
&&
1577 (ia
->ia_ifp
->if_flags
& IFF_MULTICAST
) &&
1581 if (IN6_ARE_ADDR_EQUAL(&sin6
->sin6_addr
,
1582 &ia
->ia_addr
.sin6_addr
))
1588 return (EADDRNOTAVAIL
);
1592 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0 ||
1593 (im6o
->im6o_multicast_ifp
&& im6o
->im6o_multicast_ifp
!= ifp
))
1594 return (EADDRNOTAVAIL
);
1596 if (!sc
->sc_naddrs6
) {
1597 im6o
->im6o_multicast_ifp
= ifp
;
1599 /* join CARP multicast address */
1600 bzero(&in6
, sizeof(in6
));
1601 in6
.s6_addr16
[0] = htons(0xff02);
1602 in6
.s6_addr8
[15] = 0x12;
1603 if (in6_setscope(&in6
, ifp
, NULL
) != 0)
1605 if ((imm
= in6_joingroup(ifp
, &in6
, &error
)) == NULL
)
1607 LIST_INSERT_HEAD(&im6o
->im6o_memberships
, imm
, i6mm_chain
);
1609 /* join solicited multicast address */
1610 bzero(&in6
, sizeof(in6
));
1611 in6
.s6_addr16
[0] = htons(0xff02);
1612 in6
.s6_addr32
[1] = 0;
1613 in6
.s6_addr32
[2] = htonl(1);
1614 in6
.s6_addr32
[3] = sin6
->sin6_addr
.s6_addr32
[3];
1615 in6
.s6_addr8
[12] = 0xff;
1616 if (in6_setscope(&in6
, ifp
, NULL
) != 0)
1618 if ((imm
= in6_joingroup(ifp
, &in6
, &error
)) == NULL
)
1620 LIST_INSERT_HEAD(&im6o
->im6o_memberships
, imm
, i6mm_chain
);
1623 if (!ifp
->if_carp
) {
1624 cif
= kmalloc(sizeof(*cif
), M_CARP
, M_WAITOK
| M_ZERO
);
1626 if ((error
= ifpromisc(ifp
, 1))) {
1631 TAILQ_INIT(&cif
->vhif_vrs
);
1634 struct carp_softc
*vr
;
1637 TAILQ_FOREACH(vr
, &cif
->vhif_vrs
, sc_list
) {
1638 if (vr
!= sc
&& vr
->sc_vhid
== sc
->sc_vhid
) {
1645 sc
->sc_carpdev
= ifp
;
1647 { /* XXX prevent endless loop if already in queue */
1648 struct carp_softc
*vr
, *after
= NULL
;
1652 TAILQ_FOREACH(vr
, &cif
->vhif_vrs
, sc_list
) {
1655 if (vr
->sc_vhid
< sc
->sc_vhid
)
1660 /* We're trying to keep things in order */
1662 TAILQ_INSERT_TAIL(&cif
->vhif_vrs
, sc
, sc_list
);
1664 TAILQ_INSERT_AFTER(&cif
->vhif_vrs
, after
, sc
, sc_list
);
1677 /* clean up multicast memberships */
1678 if (!sc
->sc_naddrs6
) {
1679 while (!LIST_EMPTY(&im6o
->im6o_memberships
)) {
1680 imm
= LIST_FIRST(&im6o
->im6o_memberships
);
1681 LIST_REMOVE(imm
, i6mm_chain
);
1682 in6_leavegroup(imm
);
1689 carp_del_addr6(struct carp_softc
*sc
, struct sockaddr_in6
*sin6
)
1693 if (!--sc
->sc_naddrs6
) {
1694 struct carp_if
*cif
= sc
->sc_carpdev
->if_carp
;
1695 struct ip6_moptions
*im6o
= &sc
->sc_im6o
;
1697 callout_stop(&sc
->sc_ad_tmo
);
1699 while (!LIST_EMPTY(&im6o
->im6o_memberships
)) {
1700 struct in6_multi_mship
*imm
=
1701 LIST_FIRST(&im6o
->im6o_memberships
);
1703 LIST_REMOVE(imm
, i6mm_chain
);
1704 in6_leavegroup(imm
);
1706 im6o
->im6o_multicast_ifp
= NULL
;
1707 TAILQ_REMOVE(&cif
->vhif_vrs
, sc
, sc_list
);
1708 if (TAILQ_EMPTY(&cif
->vhif_vrs
)) {
1709 sc
->sc_carpdev
->if_carp
= NULL
;
1710 kfree(cif
, M_IFADDR
);
1718 carp_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t addr
, struct ucred
*cr
)
1720 struct carp_softc
*sc
= ifp
->if_softc
, *vr
;
1721 struct carpreq carpr
;
1724 struct ifaliasreq
*ifra
;
1726 char devname
[IFNAMSIZ
];
1729 ifa
= (struct ifaddr
*)addr
;
1730 ifra
= (struct ifaliasreq
*)addr
;
1731 ifr
= (struct ifreq
*)addr
;
1732 ifd
= (struct ifdrv
*)addr
;
1736 switch (ifa
->ifa_addr
->sa_family
) {
1739 ifp
->if_flags
|= IFF_UP
| IFF_RUNNING
;
1744 ifp
->if_flags
|= IFF_UP
| IFF_RUNNING
;
1745 error
= carp_set_addr6(sc
, satosin6(ifa
->ifa_addr
));
1749 error
= EAFNOSUPPORT
;
1755 switch (ifa
->ifa_addr
->sa_family
) {
1758 panic("SIOCAIFADDR should never be seen\n");
1762 ifp
->if_flags
|= IFF_UP
| IFF_RUNNING
;
1763 error
= carp_set_addr6(sc
, satosin6(&ifra
->ifra_addr
));
1767 error
= EAFNOSUPPORT
;
1773 switch (ifa
->ifa_addr
->sa_family
) {
1776 panic("SIOCDIFADDR should never be seen\n");
1780 error
= carp_del_addr6(sc
, satosin6(&ifra
->ifra_addr
));
1784 error
= EAFNOSUPPORT
;
1790 if (ifp
->if_flags
& IFF_UP
) {
1791 if ((ifp
->if_flags
& IFF_RUNNING
) == 0) {
1792 ifp
->if_flags
|= IFF_RUNNING
;
1793 carp_set_state(sc
, INIT
);
1796 } else if (ifp
->if_flags
& IFF_RUNNING
) {
1802 error
= priv_check_cred(cr
, PRIV_ROOT
, NULL_CRED_OKAY
);
1805 error
= copyin(ifr
->ifr_data
, &carpr
, sizeof(carpr
));
1810 if ((ifp
->if_flags
& IFF_RUNNING
) &&
1811 sc
->sc_state
!= INIT
&& carpr
.carpr_state
!= sc
->sc_state
) {
1812 switch (carpr
.carpr_state
) {
1814 callout_stop(&sc
->sc_ad_tmo
);
1815 carp_set_state(sc
, BACKUP
);
1817 carp_setroute(sc
, RTM_DELETE
);
1821 carp_master_down(sc
);
1828 if (carpr
.carpr_vhid
> 0) {
1829 if (carpr
.carpr_vhid
> 255) {
1833 if (sc
->sc_carpdev
) {
1834 struct carp_if
*cif
= sc
->sc_carpdev
->if_carp
;
1836 TAILQ_FOREACH(vr
, &cif
->vhif_vrs
, sc_list
) {
1838 vr
->sc_vhid
== carpr
.carpr_vhid
)
1842 sc
->sc_vhid
= carpr
.carpr_vhid
;
1843 IF_LLADDR(ifp
)[0] = 0;
1844 IF_LLADDR(ifp
)[1] = 0;
1845 IF_LLADDR(ifp
)[2] = 0x5e;
1846 IF_LLADDR(ifp
)[3] = 0;
1847 IF_LLADDR(ifp
)[4] = 1;
1848 IF_LLADDR(ifp
)[5] = sc
->sc_vhid
;
1851 if (carpr
.carpr_advbase
> 0 || carpr
.carpr_advskew
> 0) {
1852 if (carpr
.carpr_advskew
>= 255) {
1856 if (carpr
.carpr_advbase
> 255) {
1860 sc
->sc_advbase
= carpr
.carpr_advbase
;
1861 sc
->sc_advskew
= carpr
.carpr_advskew
;
1864 bcopy(carpr
.carpr_key
, sc
->sc_key
, sizeof(sc
->sc_key
));
1874 bzero(&carpr
, sizeof(carpr
));
1875 carpr
.carpr_state
= sc
->sc_state
;
1876 carpr
.carpr_vhid
= sc
->sc_vhid
;
1877 carpr
.carpr_advbase
= sc
->sc_advbase
;
1878 carpr
.carpr_advskew
= sc
->sc_advskew
;
1879 error
= priv_check_cred(cr
, PRIV_ROOT
, NULL_CRED_OKAY
);
1881 bcopy(sc
->sc_key
, carpr
.carpr_key
,
1882 sizeof(carpr
.carpr_key
));
1885 error
= copyout(&carpr
, ifr
->ifr_data
, sizeof(carpr
));
1889 switch (ifd
->ifd_cmd
) {
1891 if (ifd
->ifd_len
!= sizeof(devname
))
1905 switch (ifd
->ifd_cmd
) {
1907 error
= carp_get_vhaddr(sc
, ifd
);
1911 bzero(devname
, sizeof(devname
));
1912 if (sc
->sc_carpdev
!= NULL
) {
1913 strlcpy(devname
, sc
->sc_carpdev
->if_xname
,
1916 error
= copyout(devname
, ifd
->ifd_data
,
1926 carp_hmac_prepare(sc
);
1931 * XXX: this is looutput. We should eventually use it from there.
1934 carp_looutput(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*dst
,
1939 M_ASSERTPKTHDR(m
); /* check if we have the packet header */
1941 if (rt
&& rt
->rt_flags
& (RTF_REJECT
|RTF_BLACKHOLE
)) {
1943 return (rt
->rt_flags
& RTF_BLACKHOLE
? 0 :
1944 rt
->rt_flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
1948 ifp
->if_obytes
+= m
->m_pkthdr
.len
;
1950 /* BPF writes need to be handled specially. */
1951 if (dst
->sa_family
== AF_UNSPEC
) {
1952 bcopy(dst
->sa_data
, &af
, sizeof(af
));
1953 dst
->sa_family
= af
;
1957 switch (dst
->sa_family
) {
1966 return (EAFNOSUPPORT
);
1969 return (if_simloop(ifp
, m
, dst
->sa_family
, 0));
1973 * Start output on carp interface. This function should never be called.
1976 carp_start(struct ifnet
*ifp
)
1979 kprintf("%s: start called\n", ifp
->if_xname
);
1984 carp_output(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr
*sa
,
1988 struct carp_softc
*sc
;
1989 struct ifnet
*carp_ifp
;
1990 struct ether_header
*eh
;
1995 switch (sa
->sa_family
) {
2008 mtag
= m_tag_find(m
, PACKET_TAG_CARP
, NULL
);
2012 bcopy(mtag
+ 1, &carp_ifp
, sizeof(struct ifnet
*));
2013 sc
= carp_ifp
->if_softc
;
2015 /* Set the source MAC address to Virtual Router MAC Address */
2016 switch (ifp
->if_type
) {
2019 eh
= mtod(m
, struct ether_header
*);
2020 eh
->ether_shost
[0] = 0;
2021 eh
->ether_shost
[1] = 0;
2022 eh
->ether_shost
[2] = 0x5e;
2023 eh
->ether_shost
[3] = 0;
2024 eh
->ether_shost
[4] = 1;
2025 eh
->ether_shost
[5] = sc
->sc_vhid
;
2029 if_printf(ifp
, "carp is not supported for this "
2030 "interface type\n");
2031 return (EOPNOTSUPP
);
2037 carp_set_state(struct carp_softc
*sc
, int state
)
2039 struct ifnet
*cifp
= &sc
->sc_if
;
2041 if (sc
->sc_state
== state
)
2043 sc
->sc_state
= state
;
2045 switch (sc
->sc_state
) {
2047 cifp
->if_link_state
= LINK_STATE_DOWN
;
2051 cifp
->if_link_state
= LINK_STATE_UP
;
2055 cifp
->if_link_state
= LINK_STATE_UNKNOWN
;
2062 carp_carpdev_state(void *v
)
2064 struct carp_if
*cif
= v
;
2065 struct carp_softc
*sc
;
2067 TAILQ_FOREACH(sc
, &cif
->vhif_vrs
, sc_list
)
2072 carp_sc_state(struct carp_softc
*sc
)
2074 if (!(sc
->sc_carpdev
->if_flags
& IFF_UP
)) {
2075 callout_stop(&sc
->sc_ad_tmo
);
2076 callout_stop(&sc
->sc_md_tmo
);
2077 callout_stop(&sc
->sc_md6_tmo
);
2078 carp_set_state(sc
, INIT
);
2080 if (!sc
->sc_suppress
) {
2081 carp_suppress_preempt
++;
2082 if (carp_suppress_preempt
== 1)
2085 sc
->sc_suppress
= 1;
2087 carp_set_state(sc
, INIT
);
2089 if (sc
->sc_suppress
)
2090 carp_suppress_preempt
--;
2091 sc
->sc_suppress
= 0;
2096 carp_stop(struct carp_softc
*sc
, int detach
)
2098 sc
->sc_if
.if_flags
&= ~IFF_RUNNING
;
2100 callout_stop(&sc
->sc_ad_tmo
);
2101 callout_stop(&sc
->sc_md_tmo
);
2102 callout_stop(&sc
->sc_md6_tmo
);
2104 if (!detach
&& sc
->sc_state
== MASTER
)
2107 if (sc
->sc_suppress
)
2108 carp_suppress_preempt
--;
2109 sc
->sc_suppress
= 0;
2111 if (sc
->sc_sendad_errors
>= CARP_SENDAD_MAX_ERRORS
)
2112 carp_suppress_preempt
--;
2113 sc
->sc_sendad_errors
= 0;
2114 sc
->sc_sendad_success
= 0;
2116 carp_set_state(sc
, INIT
);
2121 carp_reset(struct carp_softc
*sc
, int detach
)
2123 struct ifnet
*cifp
= &sc
->sc_if
;
2125 carp_stop(sc
, detach
);
2126 if (!sc
->sc_dead
&& (cifp
->if_flags
& IFF_UP
))
2127 cifp
->if_flags
|= IFF_RUNNING
;
2131 carp_activate_vhaddr(struct carp_softc
*sc
, struct carp_vhaddr
*vha
,
2132 struct ifnet
*ifp
, const struct in_ifaddr
*ia_if
, int own
)
2134 struct ip_moptions
*imo
= &sc
->sc_imo
;
2135 struct carp_if
*cif
;
2136 struct carp_softc
*vr
, *after
= NULL
;
2142 KKASSERT(vha
->vha_ia
!= NULL
);
2144 KASSERT(ia_if
!= NULL
, ("NULL backing address\n"));
2145 KASSERT(vha
->vha_iaback
== NULL
, ("%p is already activated\n", vha
));
2146 KASSERT((vha
->vha_flags
& CARP_VHAF_OWNER
) == 0,
2147 ("inactive vhaddr %p is the address owner\n", vha
));
2149 KASSERT(sc
->sc_carpdev
== NULL
|| sc
->sc_carpdev
== ifp
,
2150 ("%s is already on %s\n", sc
->sc_if
.if_xname
,
2151 sc
->sc_carpdev
->if_xname
));
2153 KASSERT(imo
->imo_multicast_ifp
== NULL
||
2154 imo
->imo_multicast_ifp
== ifp
,
2155 ("%s didn't leave mcast group on %s\n",
2156 sc
->sc_if
.if_xname
, imo
->imo_multicast_ifp
->if_xname
));
2158 if (imo
->imo_num_memberships
== 0) {
2159 struct in_addr addr
;
2161 addr
.s_addr
= htonl(INADDR_CARP_GROUP
);
2162 if ((imo
->imo_membership
[0] = in_addmulti(&addr
, ifp
)) == NULL
)
2164 imo
->imo_num_memberships
++;
2165 imo
->imo_multicast_ifp
= ifp
;
2166 imo
->imo_multicast_ttl
= CARP_DFLTTL
;
2167 imo
->imo_multicast_loop
= 0;
2170 if (!ifp
->if_carp
) {
2171 KASSERT(sc
->sc_carpdev
== NULL
,
2172 ("%s is already on %s\n", sc
->sc_if
.if_xname
,
2173 sc
->sc_carpdev
->if_xname
));
2175 cif
= kmalloc(sizeof(*cif
), M_CARP
, M_WAITOK
| M_ZERO
);
2177 error
= ifpromisc(ifp
, 1);
2183 TAILQ_INIT(&cif
->vhif_vrs
);
2187 TAILQ_FOREACH(vr
, &cif
->vhif_vrs
, sc_list
) {
2188 if (vr
!= sc
&& vr
->sc_vhid
== sc
->sc_vhid
) {
2196 if (sc
->sc_carpdev
!= NULL
)
2202 sc
->sc_carpdev
= ifp
;
2206 TAILQ_FOREACH(vr
, &cif
->vhif_vrs
, sc_list
) {
2209 if (vr
->sc_vhid
< sc
->sc_vhid
)
2214 if (assert_onlist
) {
2215 KASSERT(onlist
, ("%s is not on %s carp list\n",
2216 sc
->sc_if
.if_xname
, ifp
->if_xname
));
2218 KASSERT(!onlist
, ("%s is already on %s carp list\n",
2219 sc
->sc_if
.if_xname
, ifp
->if_xname
));
2224 /* We're trying to keep things in order */
2226 TAILQ_INSERT_TAIL(&cif
->vhif_vrs
, sc
, sc_list
);
2228 TAILQ_INSERT_AFTER(&cif
->vhif_vrs
, after
, sc
, sc_list
);
2231 vha
->vha_iaback
= ia_if
;
2235 vha
->vha_flags
|= CARP_VHAF_OWNER
;
2237 /* XXX save user configured advskew? */
2241 carp_hmac_prepare(sc
);
2242 carp_set_state(sc
, INIT
);
2246 carp_multicast_cleanup(sc
);
2251 carp_deactivate_vhaddr(struct carp_softc
*sc
, struct carp_vhaddr
*vha
)
2253 KKASSERT(vha
->vha_ia
!= NULL
);
2255 carp_hmac_prepare(sc
);
2257 if (vha
->vha_iaback
== NULL
) {
2258 KASSERT((vha
->vha_flags
& CARP_VHAF_OWNER
) == 0,
2259 ("inactive vhaddr %p is the address owner\n", vha
));
2263 vha
->vha_flags
&= ~CARP_VHAF_OWNER
;
2265 KKASSERT(sc
->sc_naddrs
> 0);
2266 vha
->vha_iaback
= NULL
;
2268 if (!sc
->sc_naddrs
) {
2269 if (sc
->sc_naddrs6
) {
2270 carp_multicast_cleanup(sc
);
2279 carp_link_addrs(struct carp_softc
*sc
, struct ifnet
*ifp
, struct ifaddr
*ifa_if
)
2281 struct carp_vhaddr
*vha
;
2282 struct in_ifaddr
*ia_if
;
2284 KKASSERT(ifa_if
->ifa_addr
->sa_family
== AF_INET
);
2285 ia_if
= ifatoia(ifa_if
);
2287 if ((ia_if
->ia_flags
& IFA_ROUTE
) == 0)
2291 * Test each inactive vhaddr against the newly added address.
2292 * If the newly added address could be the backing address,
2293 * then activate the matching vhaddr.
2295 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
) {
2296 const struct in_ifaddr
*ia
;
2300 if (vha
->vha_iaback
!= NULL
)
2304 iaddr
= ntohl(ia
->ia_addr
.sin_addr
.s_addr
);
2306 if ((iaddr
& ia_if
->ia_subnetmask
) != ia_if
->ia_subnet
)
2310 if (ia
->ia_addr
.sin_addr
.s_addr
==
2311 ia_if
->ia_addr
.sin_addr
.s_addr
)
2314 carp_activate_vhaddr(sc
, vha
, ifp
, ia_if
, own
);
2319 carp_unlink_addrs(struct carp_softc
*sc
, struct ifnet
*ifp
,
2320 struct ifaddr
*ifa_if
)
2322 struct carp_vhaddr
*vha
;
2323 struct in_ifaddr
*ia_if
;
2325 KKASSERT(ifa_if
->ifa_addr
->sa_family
== AF_INET
);
2326 ia_if
= ifatoia(ifa_if
);
2329 * Ad src address is deleted; set it to NULL.
2330 * Following loop will try pick up a new ad src address
2331 * if one of the vhaddr could retain its backing address.
2333 if (sc
->sc_ia
== ia_if
)
2337 * Test each active vhaddr against the deleted address.
2338 * If the deleted address is vhaddr address's backing
2339 * address, then deactivate the vhaddr.
2341 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
) {
2342 if (vha
->vha_iaback
== NULL
)
2345 if (vha
->vha_iaback
== ia_if
)
2346 carp_deactivate_vhaddr(sc
, vha
);
2347 else if (sc
->sc_ia
== NULL
)
2348 sc
->sc_ia
= vha
->vha_iaback
;
2353 carp_update_addrs(struct carp_softc
*sc
)
2355 struct carp_vhaddr
*vha
;
2357 KKASSERT(sc
->sc_carpdev
== NULL
);
2359 TAILQ_FOREACH(vha
, &sc
->sc_vha_list
, vha_link
)
2360 carp_config_vhaddr(sc
, vha
);
2364 carp_ifaddr(void *arg __unused
, struct ifnet
*ifp
,
2365 enum ifaddr_event event
, struct ifaddr
*ifa
)
2367 struct carp_softc
*sc
;
2369 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
2372 if (ifp
->if_type
== IFT_CARP
) {
2374 * Address is changed on carp(4) interface
2377 case IFADDR_EVENT_ADD
:
2378 carp_add_addr(ifp
->if_softc
, ifa
);
2381 case IFADDR_EVENT_CHANGE
:
2382 carp_config_addr(ifp
->if_softc
, ifa
);
2385 case IFADDR_EVENT_DELETE
:
2386 carp_del_addr(ifp
->if_softc
, ifa
);
2393 * Address is changed on non-carp(4) interface
2395 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
2399 LIST_FOREACH(sc
, &carpif_list
, sc_next
) {
2400 if (sc
->sc_carpdev
!= NULL
&& sc
->sc_carpdev
!= ifp
) {
2401 /* Not the parent iface; skip */
2406 case IFADDR_EVENT_ADD
:
2407 carp_link_addrs(sc
, ifp
, ifa
);
2410 case IFADDR_EVENT_DELETE
:
2411 if (sc
->sc_carpdev
!= NULL
) {
2412 carp_unlink_addrs(sc
, ifp
, ifa
);
2413 if (sc
->sc_carpdev
== NULL
)
2414 carp_update_addrs(sc
);
2417 * The carp(4) interface didn't have a
2418 * parent iface, so it is not possible
2419 * that it will contain any address to
2425 case IFADDR_EVENT_CHANGE
:
2426 if (sc
->sc_carpdev
== NULL
) {
2428 * The carp(4) interface didn't have a
2429 * parent iface, so it is not possible
2430 * that it will contain any address to
2433 carp_link_addrs(sc
, ifp
, ifa
);
2436 * First try breaking tie with the old
2437 * address. Then see whether we could
2438 * link certain vhaddr to the new address.
2439 * If that fails, i.e. carpdev is NULL,
2440 * we try a global update.
2442 * NOTE: The above order is critical.
2444 carp_unlink_addrs(sc
, ifp
, ifa
);
2445 carp_link_addrs(sc
, ifp
, ifa
);
2446 if (sc
->sc_carpdev
== NULL
)
2447 carp_update_addrs(sc
);
2456 carp_modevent(module_t mod
, int type
, void *data
)
2460 LIST_INIT(&carpif_list
);
2461 carp_ifdetach_event
=
2462 EVENTHANDLER_REGISTER(ifnet_detach_event
, carp_ifdetach
, NULL
,
2463 EVENTHANDLER_PRI_ANY
);
2465 EVENTHANDLER_REGISTER(ifaddr_event
, carp_ifaddr
, NULL
,
2466 EVENTHANDLER_PRI_ANY
);
2467 if_clone_attach(&carp_cloner
);
2471 EVENTHANDLER_DEREGISTER(ifnet_detach_event
,
2472 carp_ifdetach_event
);
2473 EVENTHANDLER_DEREGISTER(ifaddr_event
,
2475 if_clone_detach(&carp_cloner
);
2484 static moduledata_t carp_mod
= {
2489 DECLARE_MODULE(carp
, carp_mod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);