2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3 * Copyright (c) 2003 Ryan McBride. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/sys/netinet/ip_carp.c,v 1.48 2007/02/02 09:39:09 glebius Exp $
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/in_cksum.h>
38 #include <sys/limits.h>
39 #include <sys/malloc.h>
41 #include <sys/msgport2.h>
45 #include <sys/sockio.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/thread.h>
51 #include <machine/stdarg.h>
52 #include <crypto/sha1.h>
55 #include <net/ethernet.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 #include <net/if_clone.h>
61 #include <net/if_var.h>
62 #include <net/ifq_var.h>
63 #include <net/netmsg2.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/if_ether.h>
75 #include <netinet/icmp6.h>
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/scope6_var.h>
79 #include <netinet6/nd6.h>
82 #include <netinet/ip_carp.h>
84 #define CARP_IFNAME "carp"
85 #define CARP_IS_RUNNING(ifp) \
86 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
91 uint32_t vha_flags; /* CARP_VHAF_ */
92 struct in_ifaddr *vha_ia; /* carp address */
93 struct in_ifaddr *vha_iaback; /* backing address */
94 TAILQ_ENTRY(carp_vhaddr) vha_link;
96 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr);
99 struct netmsg_base base;
100 struct ifnet *nc_carpdev;
101 struct carp_softc *nc_softc;
107 struct arpcom arpcom;
108 struct ifnet *sc_carpdev; /* parent interface */
109 struct carp_vhaddr_list sc_vha_list; /* virtual addr list */
111 const struct in_ifaddr *sc_ia; /* primary iface address v4 */
112 struct ip_moptions sc_imo;
115 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
116 struct ip6_moptions sc_im6o;
119 enum { INIT = 0, BACKUP, MASTER }
125 int sc_sendad_errors;
126 #define CARP_SENDAD_MAX_ERRORS 3
127 int sc_sendad_success;
128 #define CARP_SENDAD_MIN_SUCCESS 3
132 int sc_naddrs; /* actually used IPv4 vha */
134 int sc_advbase; /* seconds */
139 #define CARP_HMAC_PAD 64
140 unsigned char sc_key[CARP_KEY_LEN];
141 unsigned char sc_pad[CARP_HMAC_PAD];
144 struct callout sc_ad_tmo; /* advertisement timeout */
145 struct netmsg_carp sc_ad_msg; /* adv timeout netmsg */
146 struct callout sc_md_tmo; /* ip4 master down timeout */
147 struct callout sc_md6_tmo; /* ip6 master down timeout */
148 struct netmsg_carp sc_md_msg; /* master down timeout netmsg */
150 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
153 #define sc_if arpcom.ac_if
155 struct carp_softc_container {
156 TAILQ_ENTRY(carp_softc_container) scc_link;
157 struct carp_softc *scc_softc;
159 TAILQ_HEAD(carp_if, carp_softc_container);
161 SYSCTL_DECL(_net_inet_carp);
163 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
164 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
165 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
166 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
167 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
168 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
169 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
170 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
171 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
173 static int carp_suppress_preempt = 0;
174 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
175 &carp_suppress_preempt, 0, "Preemption is suppressed");
177 static struct carpstats carpstats;
178 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
179 &carpstats, carpstats,
180 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
182 #define CARP_LOG(...) do { \
183 if (carp_opts[CARPCTL_LOG] > 0) \
184 log(LOG_INFO, __VA_ARGS__); \
187 #define CARP_DEBUG(...) do { \
188 if (carp_opts[CARPCTL_LOG] > 1) \
189 log(LOG_DEBUG, __VA_ARGS__); \
192 static struct lwkt_token carp_listtok = LWKT_TOKEN_INITIALIZER(carp_list_token);
194 static void carp_hmac_prepare(struct carp_softc *);
195 static void carp_hmac_generate(struct carp_softc *, uint32_t *,
197 static int carp_hmac_verify(struct carp_softc *, uint32_t *,
199 static void carp_setroute(struct carp_softc *, int);
200 static void carp_proto_input_c(struct carp_softc *, struct mbuf *,
201 struct carp_header *, sa_family_t);
202 static int carp_clone_create(struct if_clone *, int, caddr_t);
203 static int carp_clone_destroy(struct ifnet *);
204 static void carp_detach(struct carp_softc *, int, boolean_t);
205 static void carp_prepare_ad(struct carp_softc *, struct carp_header *);
206 static void carp_send_ad_all(void);
207 static void carp_send_ad_timeout(void *);
208 static void carp_send_ad(struct carp_softc *);
209 static void carp_send_arp(struct carp_softc *);
210 static void carp_master_down_timeout(void *);
211 static void carp_master_down(struct carp_softc *);
212 static void carp_setrun(struct carp_softc *, sa_family_t);
213 static void carp_set_state(struct carp_softc *, int);
214 static struct ifnet *carp_forus(struct carp_if *, const uint8_t *);
216 static void carp_init(void *);
217 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
218 static int carp_output(struct ifnet *, struct mbuf *, struct sockaddr *,
220 static void carp_start(struct ifnet *);
221 static void carp_serialize(struct ifnet *, enum ifnet_serialize);
222 static void carp_deserialize(struct ifnet *, enum ifnet_serialize);
223 static int carp_tryserialize(struct ifnet *, enum ifnet_serialize);
225 static void carp_serialize_assert(struct ifnet *, enum ifnet_serialize,
229 static void carp_multicast_cleanup(struct carp_softc *);
230 static void carp_add_addr(struct carp_softc *, struct ifaddr *);
231 static void carp_del_addr(struct carp_softc *, struct ifaddr *);
232 static void carp_config_addr(struct carp_softc *, struct ifaddr *);
233 static void carp_link_addrs(struct carp_softc *, struct ifnet *,
235 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *,
237 static void carp_update_addrs(struct carp_softc *, struct ifaddr *);
239 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *,
241 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
242 struct ifnet *, struct in_ifaddr *, int);
243 static void carp_deactivate_vhaddr(struct carp_softc *,
244 struct carp_vhaddr *, boolean_t);
245 static int carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *);
246 static void carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *,
250 static void carp_sc_state(struct carp_softc *);
253 static void carp_send_na(struct carp_softc *);
255 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
256 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
258 static void carp_multicast6_cleanup(struct carp_softc *);
260 static void carp_stop(struct carp_softc *, int);
261 static void carp_suspend(struct carp_softc *, int);
262 static void carp_ioctl_stop(struct carp_softc *);
263 static int carp_ioctl_setvh(struct carp_softc *, void *, struct ucred *);
264 static int carp_ioctl_getvh(struct carp_softc *, void *, struct ucred *);
265 static int carp_ioctl_getdevname(struct carp_softc *, struct ifdrv *);
266 static int carp_ioctl_getvhaddr(struct carp_softc *, struct ifdrv *);
268 static struct carp_if *carp_if_remove(struct carp_if *, struct carp_softc *);
269 static struct carp_if *carp_if_insert(struct carp_if *, struct carp_softc *);
270 static void carp_if_free(struct carp_if *);
272 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
274 static void carp_ifdetach(void *, struct ifnet *);
276 static void carp_ifdetach_dispatch(netmsg_t);
277 static void carp_clone_destroy_dispatch(netmsg_t);
278 static void carp_init_dispatch(netmsg_t);
279 static void carp_ioctl_stop_dispatch(netmsg_t);
280 static void carp_ioctl_setvh_dispatch(netmsg_t);
281 static void carp_ioctl_getvh_dispatch(netmsg_t);
282 static void carp_ioctl_getdevname_dispatch(netmsg_t);
283 static void carp_ioctl_getvhaddr_dispatch(netmsg_t);
284 static void carp_send_ad_timeout_dispatch(netmsg_t);
285 static void carp_master_down_timeout_dispatch(netmsg_t);
287 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
289 static LIST_HEAD(, carp_softc) carpif_list;
291 static struct if_clone carp_cloner =
292 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
295 static uint8_t carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 };
297 static eventhandler_tag carp_ifdetach_event;
298 static eventhandler_tag carp_ifaddr_event;
301 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
303 struct carp_vhaddr *vha;
304 u_long new_addr, addr;
306 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
309 * Virtual address list is sorted; smaller one first
311 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
313 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
314 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
320 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
322 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
323 vha_new->vha_flags |= CARP_VHAF_ONLIST;
327 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
329 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
330 vha->vha_flags &= ~CARP_VHAF_ONLIST;
331 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
335 carp_hmac_prepare(struct carp_softc *sc)
337 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
338 uint8_t vhid = sc->sc_vhid & 0xff;
341 struct ifaddr_container *ifac;
345 struct carp_vhaddr *vha;
348 /* XXX: possible race here */
350 /* compute ipad from key */
351 bzero(sc->sc_pad, sizeof(sc->sc_pad));
352 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
353 for (i = 0; i < sizeof(sc->sc_pad); i++)
354 sc->sc_pad[i] ^= 0x36;
356 /* precompute first part of inner hash */
357 SHA1Init(&sc->sc_sha1);
358 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
359 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
360 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
361 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
363 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
364 SHA1Update(&sc->sc_sha1,
365 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
366 sizeof(struct in_addr));
370 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
371 struct ifaddr *ifa = ifac->ifa;
373 if (ifa->ifa_addr->sa_family == AF_INET6) {
374 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
375 in6_clearscope(&in6);
376 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
381 /* convert ipad to opad */
382 for (i = 0; i < sizeof(sc->sc_pad); i++)
383 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
387 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
388 unsigned char md[20])
392 /* fetch first half of inner hash */
393 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
395 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
396 SHA1Final(md, &sha1ctx);
400 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
401 SHA1Update(&sha1ctx, md, 20);
402 SHA1Final(md, &sha1ctx);
406 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
407 unsigned char md[20])
409 unsigned char md2[20];
411 carp_hmac_generate(sc, counter, md2);
412 return (bcmp(md, md2, sizeof(md2)));
416 carp_setroute(struct carp_softc *sc, int cmd)
419 struct ifaddr_container *ifac;
421 struct carp_vhaddr *vha;
423 KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD);
425 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
426 if (vha->vha_iaback == NULL)
428 if (cmd == RTM_DELETE)
429 carp_delroute_vhaddr(sc, vha, FALSE);
431 carp_addroute_vhaddr(sc, vha);
435 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
436 struct ifaddr *ifa = ifac->ifa;
438 if (ifa->ifa_addr->sa_family == AF_INET6) {
449 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
451 struct carp_softc *sc;
454 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
458 sc->sc_advbase = CARP_DFLTINTV;
459 sc->sc_vhid = -1; /* required setting */
461 sc->sc_init_counter = 1;
465 TAILQ_INIT(&sc->sc_vha_list);
468 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
471 callout_init_mp(&sc->sc_ad_tmo);
472 netmsg_init(&sc->sc_ad_msg.base, NULL, &netisr_adone_rport,
473 MSGF_DROPABLE | MSGF_PRIORITY, carp_send_ad_timeout_dispatch);
474 sc->sc_ad_msg.nc_softc = sc;
476 callout_init_mp(&sc->sc_md_tmo);
477 callout_init_mp(&sc->sc_md6_tmo);
478 netmsg_init(&sc->sc_md_msg.base, NULL, &netisr_adone_rport,
479 MSGF_DROPABLE | MSGF_PRIORITY, carp_master_down_timeout_dispatch);
480 sc->sc_md_msg.nc_softc = sc;
482 if_initname(ifp, CARP_IFNAME, unit);
484 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
485 ifp->if_init = carp_init;
486 ifp->if_ioctl = carp_ioctl;
487 ifp->if_start = carp_start;
488 ifp->if_serialize = carp_serialize;
489 ifp->if_deserialize = carp_deserialize;
490 ifp->if_tryserialize = carp_tryserialize;
492 ifp->if_serialize_assert = carp_serialize_assert;
494 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
495 ifq_set_ready(&ifp->if_snd);
497 ether_ifattach(ifp, carp_etheraddr, NULL);
499 ifp->if_type = IFT_CARP;
500 ifp->if_output = carp_output;
502 lwkt_gettoken(&carp_listtok);
503 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
504 lwkt_reltoken(&carp_listtok);
510 carp_clone_destroy_dispatch(netmsg_t msg)
512 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
513 struct carp_softc *sc = cmsg->nc_softc;
516 carp_detach(sc, 1, FALSE);
518 callout_stop_sync(&sc->sc_ad_tmo);
519 callout_stop_sync(&sc->sc_md_tmo);
520 callout_stop_sync(&sc->sc_md6_tmo);
523 if ((sc->sc_ad_msg.base.lmsg.ms_flags & MSGF_DONE) == 0)
524 lwkt_dropmsg(&sc->sc_ad_msg.base.lmsg);
525 if ((sc->sc_md_msg.base.lmsg.ms_flags & MSGF_DONE) == 0)
526 lwkt_dropmsg(&sc->sc_md_msg.base.lmsg);
529 lwkt_replymsg(&cmsg->base.lmsg, 0);
533 carp_clone_destroy(struct ifnet *ifp)
535 struct carp_softc *sc = ifp->if_softc;
536 struct netmsg_carp cmsg;
538 bzero(&cmsg, sizeof(cmsg));
539 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
540 carp_clone_destroy_dispatch);
543 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
545 lwkt_gettoken(&carp_listtok);
546 LIST_REMOVE(sc, sc_next);
547 lwkt_reltoken(&carp_listtok);
552 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active\n"));
558 static struct carp_if *
559 carp_if_remove(struct carp_if *ocif, struct carp_softc *sc)
561 struct carp_softc_container *oscc, *scc;
568 TAILQ_FOREACH(oscc, ocif, scc_link) {
571 if (oscc->scc_softc == sc)
575 KASSERT(found, ("%s carp_softc is not on carp_if\n", __func__));
578 /* Last one is going to be unlinked */
582 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
585 TAILQ_FOREACH(oscc, ocif, scc_link) {
586 if (oscc->scc_softc == sc)
589 scc = kmalloc(sizeof(*scc), M_CARP, M_WAITOK | M_ZERO);
590 scc->scc_softc = oscc->scc_softc;
591 TAILQ_INSERT_TAIL(cif, scc, scc_link);
597 static struct carp_if *
598 carp_if_insert(struct carp_if *ocif, struct carp_softc *sc)
600 struct carp_softc_container *oscc;
605 TAILQ_FOREACH(oscc, ocif, scc_link) {
606 if (oscc->scc_softc == sc)
612 if (sc->sc_carpdev != NULL) {
613 KASSERT(onlist, ("%s is not on %s carp list\n",
614 sc->sc_if.if_xname, sc->sc_carpdev->if_xname));
616 KASSERT(!onlist, ("%s is already on carp list\n",
617 sc->sc_if.if_xname));
623 struct carp_softc_container *new_scc, *scc;
626 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
629 new_scc = kmalloc(sizeof(*new_scc), M_CARP, M_WAITOK | M_ZERO);
630 new_scc->scc_softc = sc;
633 TAILQ_FOREACH(oscc, ocif, scc_link) {
635 oscc->scc_softc->sc_vhid > sc->sc_vhid) {
636 TAILQ_INSERT_TAIL(cif, new_scc,
641 scc = kmalloc(sizeof(*scc), M_CARP,
643 scc->scc_softc = oscc->scc_softc;
644 TAILQ_INSERT_TAIL(cif, scc, scc_link);
648 TAILQ_INSERT_TAIL(cif, new_scc, scc_link);
657 carp_if_free(struct carp_if *cif)
659 struct carp_softc_container *scc;
661 while ((scc = TAILQ_FIRST(cif)) != NULL) {
662 TAILQ_REMOVE(cif, scc, scc_link);
669 carp_detach(struct carp_softc *sc, int detach, boolean_t del_iaback)
671 carp_suspend(sc, detach);
673 carp_multicast_cleanup(sc);
675 carp_multicast6_cleanup(sc);
678 if (!sc->sc_dead && detach) {
679 struct carp_vhaddr *vha;
681 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
682 carp_deactivate_vhaddr(sc, vha, del_iaback);
683 KKASSERT(sc->sc_naddrs == 0);
686 if (sc->sc_carpdev != NULL) {
687 struct ifnet *ifp = sc->sc_carpdev;
688 struct carp_if *ocif = ifp->if_carp;
690 ifp->if_carp = carp_if_remove(ocif, sc);
691 KASSERT(ifp->if_carp != ocif,
692 ("%s carp_if_remove failed\n", __func__));
694 sc->sc_carpdev = NULL;
698 * Make sure that all protocol threads see the
699 * sc_carpdev and if_carp changes
701 netmsg_service_sync();
703 if (ifp->if_carp == NULL) {
705 * No more carp interfaces using
706 * ifp as the backing interface,
707 * move it out of promiscous mode.
713 * The old carp list could be safely free now,
714 * since no one can access it.
721 carp_ifdetach_dispatch(netmsg_t msg)
723 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
724 struct ifnet *ifp = cmsg->nc_carpdev;
726 while (ifp->if_carp) {
727 struct carp_softc_container *scc;
729 scc = TAILQ_FIRST((struct carp_if *)(ifp->if_carp));
730 carp_detach(scc->scc_softc, 1, TRUE);
732 lwkt_replymsg(&cmsg->base.lmsg, 0);
735 /* Detach an interface from the carp. */
737 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
739 struct netmsg_carp cmsg;
741 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
743 bzero(&cmsg, sizeof(cmsg));
744 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
745 carp_ifdetach_dispatch);
746 cmsg.nc_carpdev = ifp;
748 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
752 * process input packet.
753 * we have rearranged checks order compared to the rfc,
754 * but it seems more efficient this way or not possible otherwise.
757 carp_proto_input(struct mbuf **mp, int *offp, int proto)
759 struct mbuf *m = *mp;
760 struct ip *ip = mtod(m, struct ip *);
761 struct ifnet *ifp = m->m_pkthdr.rcvif;
762 struct carp_header *ch;
763 struct carp_softc *sc;
769 carpstats.carps_ipackets++;
771 if (!carp_opts[CARPCTL_ALLOW]) {
776 /* Check if received on a valid carp interface */
777 if (ifp->if_type != IFT_CARP) {
778 carpstats.carps_badif++;
779 CARP_LOG("carp_proto_input: packet received on non-carp "
780 "interface: %s\n", ifp->if_xname);
785 if (!CARP_IS_RUNNING(ifp)) {
786 carpstats.carps_badif++;
787 CARP_LOG("carp_proto_input: packet received on stopped carp "
788 "interface: %s\n", ifp->if_xname);
794 if (sc->sc_carpdev == NULL) {
795 carpstats.carps_badif++;
796 CARP_LOG("carp_proto_input: packet received on defunc carp "
797 "interface: %s\n", ifp->if_xname);
802 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
803 carpstats.carps_badif++;
804 CARP_LOG("carp_proto_input: non-mcast packet on "
805 "interface: %s\n", ifp->if_xname);
810 /* Verify that the IP TTL is CARP_DFLTTL. */
811 if (ip->ip_ttl != CARP_DFLTTL) {
812 carpstats.carps_badttl++;
813 CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n",
814 ip->ip_ttl, CARP_DFLTTL, ifp->if_xname);
819 /* Minimal CARP packet size */
820 len = iphlen + sizeof(*ch);
823 * Verify that the received packet length is
824 * not less than the CARP header
826 if (m->m_pkthdr.len < len) {
827 carpstats.carps_badlen++;
828 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
834 /* Make sure that CARP header is contiguous */
835 if (len > m->m_len) {
836 m = m_pullup(m, len);
838 carpstats.carps_hdrops++;
839 CARP_LOG("carp_proto_input: m_pullup failed\n");
842 ip = mtod(m, struct ip *);
844 ch = (struct carp_header *)((uint8_t *)ip + iphlen);
846 /* Verify the CARP checksum */
847 if (in_cksum_skip(m, len, iphlen)) {
848 carpstats.carps_badsum++;
849 CARP_LOG("carp_proto_input: checksum failed on %s\n",
854 carp_proto_input_c(sc, m, ch, AF_INET);
856 return(IPPROTO_DONE);
861 carp6_proto_input(struct mbuf **mp, int *offp, int proto)
863 struct mbuf *m = *mp;
864 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
865 struct ifnet *ifp = m->m_pkthdr.rcvif;
866 struct carp_header *ch;
867 struct carp_softc *sc;
870 carpstats.carps_ipackets6++;
872 if (!carp_opts[CARPCTL_ALLOW]) {
877 /* check if received on a valid carp interface */
878 if (ifp->if_type != IFT_CARP) {
879 carpstats.carps_badif++;
880 CARP_LOG("carp6_proto_input: packet received on non-carp "
881 "interface: %s\n", ifp->if_xname);
886 if (!CARP_IS_RUNNING(ifp)) {
887 carpstats.carps_badif++;
888 CARP_LOG("carp_proto_input: packet received on stopped carp "
889 "interface: %s\n", ifp->if_xname);
895 if (sc->sc_carpdev == NULL) {
896 carpstats.carps_badif++;
897 CARP_LOG("carp6_proto_input: packet received on defunc-carp "
898 "interface: %s\n", ifp->if_xname);
903 /* verify that the IP TTL is 255 */
904 if (ip6->ip6_hlim != CARP_DFLTTL) {
905 carpstats.carps_badttl++;
906 CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n",
907 ip6->ip6_hlim, ifp->if_xname);
912 /* verify that we have a complete carp packet */
914 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
916 carpstats.carps_badlen++;
917 CARP_LOG("carp6_proto_input: packet size %u too small\n", len);
921 /* verify the CARP checksum */
922 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
923 carpstats.carps_badsum++;
924 CARP_LOG("carp6_proto_input: checksum failed, on %s\n",
930 carp_proto_input_c(sc, m, ch, AF_INET6);
932 return (IPPROTO_DONE);
937 carp_proto_input_c(struct carp_softc *sc, struct mbuf *m,
938 struct carp_header *ch, sa_family_t af)
941 uint64_t tmp_counter;
942 struct timeval sc_tv, ch_tv;
944 if (sc->sc_vhid != ch->carp_vhid) {
946 * CARP uses multicast, however, multicast packets
947 * are tapped to all CARP interfaces on the physical
948 * interface receiving the CARP packets, so we don't
949 * update any stats here.
956 /* verify the CARP version. */
957 if (ch->carp_version != CARP_VERSION) {
958 carpstats.carps_badver++;
959 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
965 /* verify the hash */
966 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
967 carpstats.carps_badauth++;
968 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
973 tmp_counter = ntohl(ch->carp_counter[0]);
974 tmp_counter = tmp_counter<<32;
975 tmp_counter += ntohl(ch->carp_counter[1]);
977 /* XXX Replay protection goes here */
979 sc->sc_init_counter = 0;
980 sc->sc_counter = tmp_counter;
982 sc_tv.tv_sec = sc->sc_advbase;
983 if (carp_suppress_preempt && sc->sc_advskew < 240)
984 sc_tv.tv_usec = 240 * 1000000 / 256;
986 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
987 ch_tv.tv_sec = ch->carp_advbase;
988 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
990 switch (sc->sc_state) {
996 * If we receive an advertisement from a master who's going to
997 * be more frequent than us, go into BACKUP state.
999 if (timevalcmp(&sc_tv, &ch_tv, >) ||
1000 timevalcmp(&sc_tv, &ch_tv, ==)) {
1001 callout_stop(&sc->sc_ad_tmo);
1002 CARP_DEBUG("%s: MASTER -> BACKUP "
1003 "(more frequent advertisement received)\n",
1005 carp_set_state(sc, BACKUP);
1007 carp_setroute(sc, RTM_DELETE);
1013 * If we're pre-empting masters who advertise slower than us,
1014 * and this one claims to be slower, treat him as down.
1016 if (carp_opts[CARPCTL_PREEMPT] &&
1017 timevalcmp(&sc_tv, &ch_tv, <)) {
1018 CARP_DEBUG("%s: BACKUP -> MASTER "
1019 "(preempting a slower master)\n", cifp->if_xname);
1020 carp_master_down(sc);
1025 * If the master is going to advertise at such a low frequency
1026 * that he's guaranteed to time out, we'd might as well just
1027 * treat him as timed out now.
1029 sc_tv.tv_sec = sc->sc_advbase * 3;
1030 if (timevalcmp(&sc_tv, &ch_tv, <)) {
1031 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1033 carp_master_down(sc);
1038 * Otherwise, we reset the counter and wait for the next
1041 carp_setrun(sc, af);
1048 carp_input(void *v, struct mbuf *m)
1050 struct carp_if *cif = v;
1051 struct ether_header *eh;
1052 struct carp_softc_container *scc;
1055 eh = mtod(m, struct ether_header *);
1057 ifp = carp_forus(cif, eh->ether_dhost);
1059 ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF);
1063 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
1067 * XXX Should really check the list of multicast addresses
1068 * for each CARP interface _before_ copying.
1070 TAILQ_FOREACH(scc, cif, scc_link) {
1071 struct carp_softc *sc = scc->scc_softc;
1074 if ((sc->sc_if.if_flags & IFF_UP) == 0)
1077 m0 = m_dup(m, MB_DONTWAIT);
1081 ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF);
1087 carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch)
1089 if (sc->sc_init_counter) {
1090 /* this could also be seconds since unix epoch */
1091 sc->sc_counter = karc4random();
1092 sc->sc_counter = sc->sc_counter << 32;
1093 sc->sc_counter += karc4random();
1098 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
1099 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
1101 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
1105 carp_send_ad_all(void)
1107 struct carp_softc *sc;
1109 LIST_FOREACH(sc, &carpif_list, sc_next) {
1110 if (sc->sc_carpdev == NULL)
1113 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
1119 carp_send_ad_timeout(void *xsc)
1121 struct carp_softc *sc = xsc;
1122 struct netmsg_carp *cmsg = &sc->sc_ad_msg;
1124 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d\n",
1125 __func__, mycpuid));
1128 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1129 lwkt_sendmsg(cpu_portfn(0), &cmsg->base.lmsg);
1134 carp_send_ad_timeout_dispatch(netmsg_t msg)
1136 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1137 struct carp_softc *sc = cmsg->nc_softc;
1141 lwkt_replymsg(&cmsg->base.lmsg, 0);
1148 carp_send_ad(struct carp_softc *sc)
1150 struct ifnet *cifp = &sc->sc_if;
1151 struct carp_header ch;
1153 struct carp_header *ch_ptr;
1155 int len, advbase, advskew;
1157 if (!CARP_IS_RUNNING(cifp)) {
1162 advbase = sc->sc_advbase;
1163 if (!carp_suppress_preempt || sc->sc_advskew > 240)
1164 advskew = sc->sc_advskew;
1167 tv.tv_sec = advbase;
1168 tv.tv_usec = advskew * 1000000 / 256;
1171 ch.carp_version = CARP_VERSION;
1172 ch.carp_type = CARP_ADVERTISEMENT;
1173 ch.carp_vhid = sc->sc_vhid;
1174 ch.carp_advbase = advbase;
1175 ch.carp_advskew = advskew;
1176 ch.carp_authlen = 7; /* XXX DEFINE */
1177 ch.carp_pad1 = 0; /* must be zero */
1181 if (sc->sc_ia != NULL) {
1184 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1187 carpstats.carps_onomem++;
1188 /* XXX maybe less ? */
1189 if (advbase != 255 || advskew != 255)
1190 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1191 carp_send_ad_timeout, sc);
1194 len = sizeof(*ip) + sizeof(ch);
1195 m->m_pkthdr.len = len;
1196 m->m_pkthdr.rcvif = NULL;
1198 MH_ALIGN(m, m->m_len);
1199 m->m_flags |= M_MCAST;
1200 ip = mtod(m, struct ip *);
1201 ip->ip_v = IPVERSION;
1202 ip->ip_hl = sizeof(*ip) >> 2;
1203 ip->ip_tos = IPTOS_LOWDELAY;
1205 ip->ip_id = ip_newid();
1207 ip->ip_ttl = CARP_DFLTTL;
1208 ip->ip_p = IPPROTO_CARP;
1210 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
1211 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
1213 ch_ptr = (struct carp_header *)(&ip[1]);
1214 bcopy(&ch, ch_ptr, sizeof(ch));
1215 carp_prepare_ad(sc, ch_ptr);
1216 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
1218 getmicrotime(&cifp->if_lastchange);
1219 cifp->if_opackets++;
1220 cifp->if_obytes += len;
1221 carpstats.carps_opackets++;
1223 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
1225 if (sc->sc_sendad_errors < INT_MAX)
1226 sc->sc_sendad_errors++;
1227 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1228 carp_suppress_preempt++;
1229 if (carp_suppress_preempt == 1) {
1233 sc->sc_sendad_success = 0;
1235 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1236 if (++sc->sc_sendad_success >=
1237 CARP_SENDAD_MIN_SUCCESS) {
1238 carp_suppress_preempt--;
1239 sc->sc_sendad_errors = 0;
1242 sc->sc_sendad_errors = 0;
1249 struct ip6_hdr *ip6;
1251 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1254 carpstats.carps_onomem++;
1255 /* XXX maybe less ? */
1256 if (advbase != 255 || advskew != 255)
1257 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1258 carp_send_ad_timeout, sc);
1261 len = sizeof(*ip6) + sizeof(ch);
1262 m->m_pkthdr.len = len;
1263 m->m_pkthdr.rcvif = NULL;
1265 MH_ALIGN(m, m->m_len);
1266 m->m_flags |= M_MCAST;
1267 ip6 = mtod(m, struct ip6_hdr *);
1268 bzero(ip6, sizeof(*ip6));
1269 ip6->ip6_vfc |= IPV6_VERSION;
1270 ip6->ip6_hlim = CARP_DFLTTL;
1271 ip6->ip6_nxt = IPPROTO_CARP;
1272 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1273 sizeof(struct in6_addr));
1274 /* set the multicast destination */
1276 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1277 ip6->ip6_dst.s6_addr8[15] = 0x12;
1278 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1281 CARP_LOG("%s: in6_setscope failed\n", __func__);
1285 ch_ptr = (struct carp_header *)(&ip6[1]);
1286 bcopy(&ch, ch_ptr, sizeof(ch));
1287 carp_prepare_ad(sc, ch_ptr);
1288 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
1290 getmicrotime(&cifp->if_lastchange);
1291 cifp->if_opackets++;
1292 cifp->if_obytes += len;
1293 carpstats.carps_opackets6++;
1295 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1297 if (sc->sc_sendad_errors < INT_MAX)
1298 sc->sc_sendad_errors++;
1299 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1300 carp_suppress_preempt++;
1301 if (carp_suppress_preempt == 1) {
1305 sc->sc_sendad_success = 0;
1307 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1308 if (++sc->sc_sendad_success >=
1309 CARP_SENDAD_MIN_SUCCESS) {
1310 carp_suppress_preempt--;
1311 sc->sc_sendad_errors = 0;
1314 sc->sc_sendad_errors = 0;
1320 if (advbase != 255 || advskew != 255)
1321 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1322 carp_send_ad_timeout, sc);
1326 * Broadcast a gratuitous ARP request containing
1327 * the virtual router MAC address for each IP address
1328 * associated with the virtual router.
1331 carp_send_arp(struct carp_softc *sc)
1333 const struct carp_vhaddr *vha;
1335 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1336 if (vha->vha_iaback == NULL)
1338 arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa);
1344 carp_send_na(struct carp_softc *sc)
1346 struct ifaddr_container *ifac;
1347 struct in6_addr *in6;
1348 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1350 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1351 struct ifaddr *ifa = ifac->ifa;
1353 if (ifa->ifa_addr->sa_family != AF_INET6)
1356 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1357 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1358 ND_NA_FLAG_OVERRIDE, 1, NULL);
1359 DELAY(1000); /* XXX */
1364 static __inline const struct carp_vhaddr *
1365 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1367 struct carp_vhaddr *vha;
1369 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1370 if (vha->vha_iaback == NULL)
1373 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1381 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1382 const struct in_addr *isaddr, uint8_t **enaddr)
1384 const struct carp_softc *vh;
1385 int index, count = 0;
1388 * XXX proof of concept implementation.
1389 * We use the source ip to decide which virtual host should
1390 * handle the request. If we're master of that virtual host,
1391 * then we respond, otherwise, just drop the arp packet on
1395 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1396 if (!CARP_IS_RUNNING(&vh->sc_if))
1399 if (carp_find_addr(vh, itaddr) != NULL)
1405 /* this should be a hash, like pf_hash() */
1406 index = ntohl(isaddr->s_addr) % count;
1409 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1410 if (!CARP_IS_RUNNING(&vh->sc_if))
1413 if (carp_find_addr(vh, itaddr) == NULL)
1416 if (count == index) {
1417 if (vh->sc_state == MASTER) {
1418 *enaddr = IF_LLADDR(&vh->sc_if);
1431 carp_iamatch(const struct in_ifaddr *ia)
1433 const struct carp_softc *sc = ia->ia_ifp->if_softc;
1435 KASSERT(&curthread->td_msgport == cpu_portfn(0),
1436 ("not in netisr0"));
1439 if (carp_opts[CARPCTL_ARPBALANCE])
1440 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1443 if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER)
1451 carp_iamatch6(void *v, struct in6_addr *taddr)
1454 struct carp_if *cif = v;
1455 struct carp_softc *vh;
1457 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1458 struct ifaddr_container *ifac;
1460 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1462 struct ifaddr *ifa = ifac->ifa;
1464 if (IN6_ARE_ADDR_EQUAL(taddr,
1465 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1466 CARP_IS_RUNNING(&vh->sc_if) &&
1467 vh->sc_state == MASTER) {
1477 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1481 struct carp_if *cif = v;
1482 struct carp_softc *sc;
1484 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1485 struct ifaddr_container *ifac;
1487 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1489 struct ifaddr *ifa = ifac->ifa;
1491 if (IN6_ARE_ADDR_EQUAL(taddr,
1492 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1493 CARP_IS_RUNNING(&sc->sc_if)) {
1494 struct ifnet *ifp = &sc->sc_if;
1496 mtag = m_tag_get(PACKET_TAG_CARP,
1497 sizeof(struct ifnet *), MB_DONTWAIT);
1499 /* better a bit than nothing */
1500 return (IF_LLADDR(ifp));
1502 bcopy(&ifp, (caddr_t)(mtag + 1),
1503 sizeof(struct ifnet *));
1504 m_tag_prepend(m, mtag);
1506 return (IF_LLADDR(ifp));
1515 static struct ifnet *
1516 carp_forus(struct carp_if *cif, const uint8_t *dhost)
1518 struct carp_softc_container *scc;
1520 if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0)
1523 TAILQ_FOREACH(scc, cif, scc_link) {
1524 struct carp_softc *sc = scc->scc_softc;
1525 struct ifnet *ifp = &sc->sc_if;
1527 if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER &&
1528 !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN))
1535 carp_master_down_timeout(void *xsc)
1537 struct carp_softc *sc = xsc;
1538 struct netmsg_carp *cmsg = &sc->sc_md_msg;
1540 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d\n",
1541 __func__, mycpuid));
1544 if (cmsg->base.lmsg.ms_flags & MSGF_DONE)
1545 lwkt_sendmsg(cpu_portfn(0), &cmsg->base.lmsg);
1550 carp_master_down_timeout_dispatch(netmsg_t msg)
1552 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1553 struct carp_softc *sc = cmsg->nc_softc;
1557 lwkt_replymsg(&cmsg->base.lmsg, 0);
1560 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1561 sc->sc_if.if_xname);
1562 carp_master_down(sc);
1566 carp_master_down(struct carp_softc *sc)
1568 switch (sc->sc_state) {
1570 kprintf("%s: master_down event in INIT state\n",
1571 sc->sc_if.if_xname);
1578 carp_set_state(sc, MASTER);
1585 carp_setroute(sc, RTM_ADD);
1591 * When in backup state, af indicates whether to reset the master down timer
1592 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1595 carp_setrun(struct carp_softc *sc, sa_family_t af)
1597 struct ifnet *cifp = &sc->sc_if;
1600 if (sc->sc_carpdev == NULL) {
1601 carp_set_state(sc, INIT);
1605 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1606 (sc->sc_naddrs || sc->sc_naddrs6)) {
1609 carp_setroute(sc, RTM_DELETE);
1613 switch (sc->sc_state) {
1615 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1621 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1623 carp_set_state(sc, MASTER);
1624 carp_setroute(sc, RTM_ADD);
1626 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1627 carp_set_state(sc, BACKUP);
1628 carp_setroute(sc, RTM_DELETE);
1634 callout_stop(&sc->sc_ad_tmo);
1635 tv.tv_sec = 3 * sc->sc_advbase;
1636 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1640 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1641 carp_master_down_timeout, sc);
1646 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1647 carp_master_down_timeout, sc);
1652 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1653 carp_master_down_timeout, sc);
1655 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1656 carp_master_down_timeout, sc);
1662 tv.tv_sec = sc->sc_advbase;
1663 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1664 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1665 carp_send_ad_timeout, sc);
1671 carp_multicast_cleanup(struct carp_softc *sc)
1673 struct ip_moptions *imo = &sc->sc_imo;
1675 if (imo->imo_num_memberships == 0)
1677 KKASSERT(imo->imo_num_memberships == 1);
1679 in_delmulti(imo->imo_membership[0]);
1680 imo->imo_membership[0] = NULL;
1681 imo->imo_num_memberships = 0;
1682 imo->imo_multicast_ifp = NULL;
1687 carp_multicast6_cleanup(struct carp_softc *sc)
1689 struct ip6_moptions *im6o = &sc->sc_im6o;
1691 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1692 struct in6_multi_mship *imm =
1693 LIST_FIRST(&im6o->im6o_memberships);
1695 LIST_REMOVE(imm, i6mm_chain);
1696 in6_leavegroup(imm);
1698 im6o->im6o_multicast_ifp = NULL;
1703 carp_ioctl_getvhaddr_dispatch(netmsg_t msg)
1705 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1706 struct carp_softc *sc = cmsg->nc_softc;
1707 const struct carp_vhaddr *vha;
1708 struct ifcarpvhaddr *carpa, *carpa0;
1709 int count, len, error = 0;
1712 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1715 if (cmsg->nc_datalen == 0) {
1716 cmsg->nc_datalen = count * sizeof(*carpa);
1718 } else if (count == 0 || cmsg->nc_datalen < sizeof(*carpa)) {
1719 cmsg->nc_datalen = 0;
1722 len = min(cmsg->nc_datalen, sizeof(*carpa) * count);
1723 KKASSERT(len >= sizeof(*carpa));
1725 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1726 if (carpa == NULL) {
1732 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1733 if (len < sizeof(*carpa))
1736 carpa->carpa_flags = vha->vha_flags;
1737 carpa->carpa_addr.sin_family = AF_INET;
1738 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1740 carpa->carpa_baddr.sin_family = AF_INET;
1741 if (vha->vha_iaback == NULL) {
1742 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1744 carpa->carpa_baddr.sin_addr =
1745 vha->vha_iaback->ia_addr.sin_addr;
1750 len -= sizeof(*carpa);
1752 cmsg->nc_datalen = sizeof(*carpa) * count;
1753 KKASSERT(cmsg->nc_datalen > 0);
1755 cmsg->nc_data = carpa0;
1758 lwkt_replymsg(&cmsg->base.lmsg, error);
1762 carp_ioctl_getvhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1764 struct ifnet *ifp = &sc->arpcom.ac_if;
1765 struct netmsg_carp cmsg;
1768 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1769 ifnet_deserialize_all(ifp);
1771 bzero(&cmsg, sizeof(cmsg));
1772 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
1773 carp_ioctl_getvhaddr_dispatch);
1775 cmsg.nc_datalen = ifd->ifd_len;
1777 error = lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
1780 if (cmsg.nc_data != NULL) {
1781 error = copyout(cmsg.nc_data, ifd->ifd_data,
1783 kfree(cmsg.nc_data, M_TEMP);
1785 ifd->ifd_len = cmsg.nc_datalen;
1787 KASSERT(cmsg.nc_data == NULL,
1788 ("%s temp vhaddr is alloc upon error\n", __func__));
1791 ifnet_serialize_all(ifp);
1796 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
1797 struct in_ifaddr *ia_del)
1800 struct in_ifaddr *ia_if;
1801 struct in_ifaddr_container *iac;
1802 const struct sockaddr_in *sin;
1806 KKASSERT(vha->vha_ia != NULL);
1808 sin = &vha->vha_ia->ia_addr;
1809 iaddr = ntohl(sin->sin_addr.s_addr);
1813 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1814 struct in_ifaddr *ia = iac->ia;
1819 if (ia->ia_ifp->if_type == IFT_CARP)
1822 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1825 /* and, yeah, we need a multicast-capable iface too */
1826 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1829 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1830 if (sin->sin_addr.s_addr ==
1831 ia->ia_addr.sin_addr.s_addr)
1835 else if (sc->sc_carpdev != NULL &&
1836 sc->sc_carpdev == ia->ia_ifp)
1841 carp_deactivate_vhaddr(sc, vha, FALSE);
1845 ifp = ia_if->ia_ifp;
1847 /* XXX Don't allow parent iface to be changed */
1848 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
1851 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
1855 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1857 struct carp_vhaddr *vha_new;
1858 struct in_ifaddr *carp_ia;
1860 struct carp_vhaddr *vha;
1863 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1864 carp_ia = ifatoia(carp_ifa);
1867 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1868 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
1871 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
1872 vha_new->vha_ia = carp_ia;
1873 carp_insert_vhaddr(sc, vha_new);
1875 if (carp_config_vhaddr(sc, vha_new, NULL) != 0) {
1877 * If the above configuration fails, it may only mean
1878 * that the new address is problematic. However, the
1879 * carp(4) interface may already have several working
1880 * addresses. Since the expected behaviour of
1881 * SIOC[AS]IFADDR is to put the NIC into working state,
1882 * we try starting the state machine manually here with
1883 * the hope that the carp(4)'s previously working
1884 * addresses still could be brought up.
1886 carp_hmac_prepare(sc);
1887 carp_set_state(sc, INIT);
1893 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1895 struct carp_vhaddr *vha;
1896 struct in_ifaddr *carp_ia;
1898 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1899 carp_ia = ifatoia(carp_ifa);
1901 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1902 KKASSERT(vha->vha_ia != NULL);
1903 if (vha->vha_ia == carp_ia)
1906 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1909 * Remove the vhaddr from the list before deactivating
1910 * the vhaddr, so that the HMAC could be correctly
1911 * updated in carp_deactivate_vhaddr()
1913 carp_remove_vhaddr(sc, vha);
1915 carp_deactivate_vhaddr(sc, vha, FALSE);
1920 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1922 struct carp_vhaddr *vha;
1923 struct in_ifaddr *carp_ia;
1925 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1926 carp_ia = ifatoia(carp_ifa);
1928 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1929 KKASSERT(vha->vha_ia != NULL);
1930 if (vha->vha_ia == carp_ia)
1933 KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1935 /* Remove then reinsert, to keep the vhaddr list sorted */
1936 carp_remove_vhaddr(sc, vha);
1937 carp_insert_vhaddr(sc, vha);
1939 if (carp_config_vhaddr(sc, vha, NULL) != 0) {
1940 /* See the comment in carp_add_addr() */
1941 carp_hmac_prepare(sc);
1942 carp_set_state(sc, INIT);
1951 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1954 struct carp_if *cif;
1955 struct in6_ifaddr *ia, *ia_if;
1956 struct ip6_moptions *im6o = &sc->sc_im6o;
1957 struct in6_multi_mship *imm;
1958 struct in6_addr in6;
1961 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1966 /* we have to do it by hands to check we won't match on us */
1967 ia_if = NULL; own = 0;
1968 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1971 for (i = 0; i < 4; i++) {
1972 if ((sin6->sin6_addr.s6_addr32[i] &
1973 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1974 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1975 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1978 /* and, yeah, we need a multicast-capable iface too */
1979 if (ia->ia_ifp != &sc->sc_if &&
1980 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1984 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1985 &ia->ia_addr.sin6_addr))
1991 return (EADDRNOTAVAIL);
1995 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1996 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1997 return (EADDRNOTAVAIL);
1999 if (!sc->sc_naddrs6) {
2000 im6o->im6o_multicast_ifp = ifp;
2002 /* join CARP multicast address */
2003 bzero(&in6, sizeof(in6));
2004 in6.s6_addr16[0] = htons(0xff02);
2005 in6.s6_addr8[15] = 0x12;
2006 if (in6_setscope(&in6, ifp, NULL) != 0)
2008 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2010 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2012 /* join solicited multicast address */
2013 bzero(&in6, sizeof(in6));
2014 in6.s6_addr16[0] = htons(0xff02);
2015 in6.s6_addr32[1] = 0;
2016 in6.s6_addr32[2] = htonl(1);
2017 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
2018 in6.s6_addr8[12] = 0xff;
2019 if (in6_setscope(&in6, ifp, NULL) != 0)
2021 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
2023 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2027 if (!ifp->if_carp) {
2028 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2030 if ((error = ifpromisc(ifp, 1))) {
2035 TAILQ_INIT(&cif->vhif_vrs);
2038 struct carp_softc *vr;
2041 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2042 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
2050 sc->sc_carpdev = ifp;
2053 { /* XXX prevent endless loop if already in queue */
2054 struct carp_softc *vr, *after = NULL;
2058 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2061 if (vr->sc_vhid < sc->sc_vhid)
2066 /* We're trying to keep things in order */
2068 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2070 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2084 /* clean up multicast memberships */
2085 if (!sc->sc_naddrs6) {
2086 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2087 imm = LIST_FIRST(&im6o->im6o_memberships);
2088 LIST_REMOVE(imm, i6mm_chain);
2089 in6_leavegroup(imm);
2096 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
2100 if (!--sc->sc_naddrs6) {
2101 struct carp_if *cif = sc->sc_carpdev->if_carp;
2102 struct ip6_moptions *im6o = &sc->sc_im6o;
2104 callout_stop(&sc->sc_ad_tmo);
2106 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2107 struct in6_multi_mship *imm =
2108 LIST_FIRST(&im6o->im6o_memberships);
2110 LIST_REMOVE(imm, i6mm_chain);
2111 in6_leavegroup(imm);
2113 im6o->im6o_multicast_ifp = NULL;
2115 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
2116 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
2117 sc->sc_carpdev->if_carp = NULL;
2118 kfree(cif, M_IFADDR);
2129 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
2131 struct carp_softc *sc = ifp->if_softc;
2132 struct ifreq *ifr = (struct ifreq *)addr;
2133 struct ifdrv *ifd = (struct ifdrv *)addr;
2136 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2140 if (ifp->if_flags & IFF_UP) {
2141 if ((ifp->if_flags & IFF_RUNNING) == 0)
2143 } else if (ifp->if_flags & IFF_RUNNING) {
2144 carp_ioctl_stop(sc);
2149 error = carp_ioctl_setvh(sc, ifr->ifr_data, cr);
2153 error = carp_ioctl_getvh(sc, ifr->ifr_data, cr);
2157 switch (ifd->ifd_cmd) {
2159 error = carp_ioctl_getdevname(sc, ifd);
2163 error = carp_ioctl_getvhaddr(sc, ifd);
2173 error = ether_ioctl(ifp, cmd, addr);
2181 carp_ioctl_stop_dispatch(netmsg_t msg)
2183 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2184 struct carp_softc *sc = cmsg->nc_softc;
2187 lwkt_replymsg(&cmsg->base.lmsg, 0);
2191 carp_ioctl_stop(struct carp_softc *sc)
2193 struct ifnet *ifp = &sc->arpcom.ac_if;
2194 struct netmsg_carp cmsg;
2196 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2198 ifnet_deserialize_all(ifp);
2200 bzero(&cmsg, sizeof(cmsg));
2201 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2202 carp_ioctl_stop_dispatch);
2205 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2207 ifnet_serialize_all(ifp);
2211 carp_ioctl_setvh_dispatch(netmsg_t msg)
2213 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2214 struct carp_softc *sc = cmsg->nc_softc;
2215 struct ifnet *ifp = &sc->arpcom.ac_if;
2216 const struct carpreq *carpr = cmsg->nc_data;
2220 if ((ifp->if_flags & IFF_RUNNING) &&
2221 sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) {
2222 switch (carpr->carpr_state) {
2224 callout_stop(&sc->sc_ad_tmo);
2225 carp_set_state(sc, BACKUP);
2227 carp_setroute(sc, RTM_DELETE);
2231 carp_master_down(sc);
2238 if (carpr->carpr_vhid > 0) {
2239 if (carpr->carpr_vhid > 255) {
2243 if (sc->sc_carpdev) {
2244 struct carp_if *cif = sc->sc_carpdev->if_carp;
2245 struct carp_softc_container *scc;
2247 TAILQ_FOREACH(scc, cif, scc_link) {
2248 struct carp_softc *vr = scc->scc_softc;
2251 vr->sc_vhid == carpr->carpr_vhid) {
2257 sc->sc_vhid = carpr->carpr_vhid;
2259 IF_LLADDR(ifp)[5] = sc->sc_vhid;
2260 bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr,
2265 if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) {
2266 if (carpr->carpr_advskew >= 255) {
2270 if (carpr->carpr_advbase > 255) {
2274 sc->sc_advbase = carpr->carpr_advbase;
2275 sc->sc_advskew = carpr->carpr_advskew;
2278 bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key));
2286 carp_hmac_prepare(sc);
2288 lwkt_replymsg(&cmsg->base.lmsg, error);
2292 carp_ioctl_setvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2294 struct ifnet *ifp = &sc->arpcom.ac_if;
2295 struct netmsg_carp cmsg;
2296 struct carpreq carpr;
2299 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2300 ifnet_deserialize_all(ifp);
2302 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2306 error = copyin(udata, &carpr, sizeof(carpr));
2310 bzero(&cmsg, sizeof(cmsg));
2311 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2312 carp_ioctl_setvh_dispatch);
2314 cmsg.nc_data = &carpr;
2316 error = lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2319 ifnet_serialize_all(ifp);
2324 carp_ioctl_getvh_dispatch(netmsg_t msg)
2326 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2327 struct carp_softc *sc = cmsg->nc_softc;
2328 struct carpreq *carpr = cmsg->nc_data;
2330 carpr->carpr_state = sc->sc_state;
2331 carpr->carpr_vhid = sc->sc_vhid;
2332 carpr->carpr_advbase = sc->sc_advbase;
2333 carpr->carpr_advskew = sc->sc_advskew;
2334 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
2336 lwkt_replymsg(&cmsg->base.lmsg, 0);
2340 carp_ioctl_getvh(struct carp_softc *sc, void *udata, struct ucred *cr)
2342 struct ifnet *ifp = &sc->arpcom.ac_if;
2343 struct netmsg_carp cmsg;
2344 struct carpreq carpr;
2347 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2348 ifnet_deserialize_all(ifp);
2350 bzero(&cmsg, sizeof(cmsg));
2351 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2352 carp_ioctl_getvh_dispatch);
2354 cmsg.nc_data = &carpr;
2356 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2358 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
2360 bzero(carpr.carpr_key, sizeof(carpr.carpr_key));
2362 error = copyout(&carpr, udata, sizeof(carpr));
2364 ifnet_serialize_all(ifp);
2369 carp_ioctl_getdevname_dispatch(netmsg_t msg)
2371 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2372 struct carp_softc *sc = cmsg->nc_softc;
2373 char *devname = cmsg->nc_data;
2375 bzero(devname, sizeof(devname));
2376 if (sc->sc_carpdev != NULL)
2377 strlcpy(devname, sc->sc_carpdev->if_xname, sizeof(devname));
2379 lwkt_replymsg(&cmsg->base.lmsg, 0);
2383 carp_ioctl_getdevname(struct carp_softc *sc, struct ifdrv *ifd)
2385 struct ifnet *ifp = &sc->arpcom.ac_if;
2386 struct netmsg_carp cmsg;
2387 char devname[IFNAMSIZ];
2390 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2392 if (ifd->ifd_len != sizeof(devname))
2395 ifnet_deserialize_all(ifp);
2397 bzero(&cmsg, sizeof(cmsg));
2398 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2399 carp_ioctl_getdevname_dispatch);
2401 cmsg.nc_data = devname;
2403 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2405 error = copyout(devname, ifd->ifd_data, sizeof(devname));
2407 ifnet_serialize_all(ifp);
2412 carp_init_dispatch(netmsg_t msg)
2414 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2415 struct carp_softc *sc = cmsg->nc_softc;
2417 sc->sc_if.if_flags |= IFF_RUNNING;
2418 carp_hmac_prepare(sc);
2419 carp_set_state(sc, INIT);
2422 lwkt_replymsg(&cmsg->base.lmsg, 0);
2426 carp_init(void *xsc)
2428 struct carp_softc *sc = xsc;
2429 struct ifnet *ifp = &sc->arpcom.ac_if;
2430 struct netmsg_carp cmsg;
2432 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2434 ifnet_deserialize_all(ifp);
2436 bzero(&cmsg, sizeof(cmsg));
2437 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2438 carp_init_dispatch);
2441 lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2443 ifnet_serialize_all(ifp);
2447 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2450 struct carp_softc *sc = ifp->if_softc;
2451 struct ifnet *carpdev;
2454 carpdev = sc->sc_carpdev;
2455 if (carpdev != NULL) {
2458 * CARP's ifp is passed to backing device's
2461 carpdev->if_output(ifp, m, dst, rt);
2464 error = ENETUNREACH;
2470 * Start output on carp interface. This function should never be called.
2473 carp_start(struct ifnet *ifp)
2475 panic("%s: start called\n", ifp->if_xname);
2479 carp_serialize(struct ifnet *ifp __unused,
2480 enum ifnet_serialize slz __unused)
2485 carp_deserialize(struct ifnet *ifp __unused,
2486 enum ifnet_serialize slz __unused)
2491 carp_tryserialize(struct ifnet *ifp __unused,
2492 enum ifnet_serialize slz __unused)
2500 carp_serialize_assert(struct ifnet *ifp __unused,
2501 enum ifnet_serialize slz __unused, boolean_t serialized __unused)
2505 #endif /* INVARIANTS */
2508 carp_set_state(struct carp_softc *sc, int state)
2510 struct ifnet *cifp = &sc->sc_if;
2512 if (sc->sc_state == state)
2514 sc->sc_state = state;
2516 switch (sc->sc_state) {
2518 cifp->if_link_state = LINK_STATE_DOWN;
2522 cifp->if_link_state = LINK_STATE_UP;
2526 cifp->if_link_state = LINK_STATE_UNKNOWN;
2533 carp_group_demote_adj(struct ifnet *ifp, int adj)
2535 struct ifg_list *ifgl;
2538 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2539 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2541 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2548 if (adj > 0 && *dm == 1)
2550 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2551 ifgl->ifgl_group->ifg_group, *dm);
2557 carp_carpdev_state(void *v)
2559 struct carp_if *cif = v;
2560 struct carp_softc *sc;
2562 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2567 carp_sc_state(struct carp_softc *sc)
2569 if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2570 callout_stop(&sc->sc_ad_tmo);
2571 callout_stop(&sc->sc_md_tmo);
2572 callout_stop(&sc->sc_md6_tmo);
2573 carp_set_state(sc, INIT);
2575 if (!sc->sc_suppress) {
2576 carp_suppress_preempt++;
2577 if (carp_suppress_preempt == 1)
2580 sc->sc_suppress = 1;
2582 carp_set_state(sc, INIT);
2584 if (sc->sc_suppress)
2585 carp_suppress_preempt--;
2586 sc->sc_suppress = 0;
2592 carp_stop(struct carp_softc *sc, int detach)
2594 sc->sc_if.if_flags &= ~IFF_RUNNING;
2596 callout_stop(&sc->sc_ad_tmo);
2597 callout_stop(&sc->sc_md_tmo);
2598 callout_stop(&sc->sc_md6_tmo);
2600 if (!detach && sc->sc_state == MASTER)
2603 if (sc->sc_suppress)
2604 carp_suppress_preempt--;
2605 sc->sc_suppress = 0;
2607 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2608 carp_suppress_preempt--;
2609 sc->sc_sendad_errors = 0;
2610 sc->sc_sendad_success = 0;
2612 carp_set_state(sc, INIT);
2617 carp_suspend(struct carp_softc *sc, int detach)
2619 struct ifnet *cifp = &sc->sc_if;
2621 carp_stop(sc, detach);
2623 /* Retain the running state, if we are not dead yet */
2624 if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2625 cifp->if_flags |= IFF_RUNNING;
2629 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2630 struct ifnet *ifp, struct in_ifaddr *ia_if, int own)
2632 struct ip_moptions *imo = &sc->sc_imo;
2633 struct carp_if *ocif = ifp->if_carp;
2636 KKASSERT(vha->vha_ia != NULL);
2638 KASSERT(ia_if != NULL, ("NULL backing address\n"));
2639 KASSERT(vha->vha_iaback == NULL, ("%p is already activated\n", vha));
2640 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2641 ("inactive vhaddr %p is the address owner\n", vha));
2643 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2644 ("%s is already on %s\n", sc->sc_if.if_xname,
2645 sc->sc_carpdev->if_xname));
2648 KASSERT(sc->sc_carpdev == NULL,
2649 ("%s is already on %s\n", sc->sc_if.if_xname,
2650 sc->sc_carpdev->if_xname));
2652 error = ifpromisc(ifp, 1);
2656 struct carp_softc_container *scc;
2658 TAILQ_FOREACH(scc, ocif, scc_link) {
2659 struct carp_softc *vr = scc->scc_softc;
2661 if (vr != sc && vr->sc_vhid == sc->sc_vhid)
2666 ifp->if_carp = carp_if_insert(ocif, sc);
2667 KASSERT(ifp->if_carp != NULL, ("%s carp_if_insert failed\n", __func__));
2670 sc->sc_carpdev = ifp;
2673 * Make sure that all protocol threads see the sc_carpdev and
2676 netmsg_service_sync();
2678 if (ocif != NULL && ifp->if_carp != ocif) {
2680 * The old carp list could be safely free now,
2681 * since no one can access it.
2686 vha->vha_iaback = ia_if;
2690 vha->vha_flags |= CARP_VHAF_OWNER;
2692 /* XXX save user configured advskew? */
2696 carp_addroute_vhaddr(sc, vha);
2699 * Join the multicast group only after the backing interface
2700 * has been hooked with the CARP interface.
2702 KASSERT(imo->imo_multicast_ifp == NULL ||
2703 imo->imo_multicast_ifp == &sc->sc_if,
2704 ("%s didn't leave mcast group on %s\n",
2705 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2707 if (imo->imo_num_memberships == 0) {
2708 struct in_addr addr;
2710 addr.s_addr = htonl(INADDR_CARP_GROUP);
2711 imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if);
2712 if (imo->imo_membership[0] == NULL) {
2713 carp_deactivate_vhaddr(sc, vha, FALSE);
2717 imo->imo_num_memberships++;
2718 imo->imo_multicast_ifp = &sc->sc_if;
2719 imo->imo_multicast_ttl = CARP_DFLTTL;
2720 imo->imo_multicast_loop = 0;
2723 carp_hmac_prepare(sc);
2724 carp_set_state(sc, INIT);
2730 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2731 boolean_t del_iaback)
2733 KKASSERT(vha->vha_ia != NULL);
2735 carp_hmac_prepare(sc);
2737 if (vha->vha_iaback == NULL) {
2738 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2739 ("inactive vhaddr %p is the address owner\n", vha));
2743 vha->vha_flags &= ~CARP_VHAF_OWNER;
2744 carp_delroute_vhaddr(sc, vha, del_iaback);
2746 KKASSERT(sc->sc_naddrs > 0);
2747 vha->vha_iaback = NULL;
2749 if (!sc->sc_naddrs) {
2750 if (sc->sc_naddrs6) {
2751 carp_multicast_cleanup(sc);
2754 carp_detach(sc, 0, del_iaback);
2760 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2762 struct carp_vhaddr *vha;
2763 struct in_ifaddr *ia_if;
2765 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2766 ia_if = ifatoia(ifa_if);
2769 * Test each inactive vhaddr against the newly added address.
2770 * If the newly added address could be the backing address,
2771 * then activate the matching vhaddr.
2773 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2774 const struct in_ifaddr *ia;
2778 if (vha->vha_iaback != NULL)
2782 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr);
2784 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet)
2788 if (ia->ia_addr.sin_addr.s_addr ==
2789 ia_if->ia_addr.sin_addr.s_addr)
2792 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2797 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
2798 struct ifaddr *ifa_if)
2800 struct carp_vhaddr *vha;
2801 struct in_ifaddr *ia_if;
2803 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2804 ia_if = ifatoia(ifa_if);
2807 * Ad src address is deleted; set it to NULL.
2808 * Following loop will try pick up a new ad src address
2809 * if one of the vhaddr could retain its backing address.
2811 if (sc->sc_ia == ia_if)
2815 * Test each active vhaddr against the deleted address.
2816 * If the deleted address is vhaddr address's backing
2817 * address, then deactivate the vhaddr.
2819 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2820 if (vha->vha_iaback == NULL)
2823 if (vha->vha_iaback == ia_if)
2824 carp_deactivate_vhaddr(sc, vha, TRUE);
2825 else if (sc->sc_ia == NULL)
2826 sc->sc_ia = vha->vha_iaback;
2831 carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del)
2833 struct carp_vhaddr *vha;
2835 KKASSERT(sc->sc_carpdev == NULL);
2837 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2838 carp_config_vhaddr(sc, vha, ifatoia(ifa_del));
2842 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
2843 enum ifaddr_event event, struct ifaddr *ifa)
2845 struct carp_softc *sc;
2847 if (ifa->ifa_addr->sa_family != AF_INET)
2850 KASSERT(&curthread->td_msgport == cpu_portfn(0),
2851 ("not in netisr0"));
2853 if (ifp->if_type == IFT_CARP) {
2855 * Address is changed on carp(4) interface
2858 case IFADDR_EVENT_ADD:
2859 carp_add_addr(ifp->if_softc, ifa);
2862 case IFADDR_EVENT_CHANGE:
2863 carp_config_addr(ifp->if_softc, ifa);
2866 case IFADDR_EVENT_DELETE:
2867 carp_del_addr(ifp->if_softc, ifa);
2874 * Address is changed on non-carp(4) interface
2876 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2879 LIST_FOREACH(sc, &carpif_list, sc_next) {
2880 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
2881 /* Not the parent iface; skip */
2886 case IFADDR_EVENT_ADD:
2887 carp_link_addrs(sc, ifp, ifa);
2890 case IFADDR_EVENT_DELETE:
2891 if (sc->sc_carpdev != NULL) {
2892 carp_unlink_addrs(sc, ifp, ifa);
2893 if (sc->sc_carpdev == NULL) {
2895 * We no longer have the parent
2896 * interface, however, certain
2897 * virtual addresses, which are
2898 * not used because they can't
2899 * match the previous parent
2900 * interface's addresses, may now
2901 * match different interface's
2904 carp_update_addrs(sc, ifa);
2908 * The carp(4) interface didn't have a
2909 * parent iface, so it is not possible
2910 * that it will contain any address to
2916 case IFADDR_EVENT_CHANGE:
2917 if (sc->sc_carpdev == NULL) {
2919 * The carp(4) interface didn't have a
2920 * parent iface, so it is not possible
2921 * that it will contain any address to
2924 carp_link_addrs(sc, ifp, ifa);
2927 * First try breaking tie with the old
2928 * address. Then see whether we could
2929 * link certain vhaddr to the new address.
2930 * If that fails, i.e. carpdev is NULL,
2931 * we try a global update.
2933 * NOTE: The above order is critical.
2935 carp_unlink_addrs(sc, ifp, ifa);
2936 carp_link_addrs(sc, ifp, ifa);
2937 if (sc->sc_carpdev == NULL) {
2939 * See the comment in the above
2940 * IFADDR_EVENT_DELETE block.
2942 carp_update_addrs(sc, NULL);
2951 carp_proto_ctlinput(netmsg_t msg)
2953 int cmd = msg->ctlinput.nm_cmd;
2954 struct sockaddr *sa = msg->ctlinput.nm_arg;
2955 struct in_ifaddr_container *iac;
2957 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
2958 struct in_ifaddr *ia = iac->ia;
2959 struct ifnet *ifp = ia->ia_ifp;
2961 if (ifp->if_type == IFT_CARP)
2964 if (ia->ia_ifa.ifa_addr == sa) {
2965 if (cmd == PRC_IFDOWN) {
2966 carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE,
2968 } else if (cmd == PRC_IFUP) {
2969 carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD,
2976 lwkt_replymsg(&msg->lmsg, 0);
2980 carp_parent(struct ifnet *cifp)
2982 struct carp_softc *sc;
2984 KKASSERT(cifp->if_type == IFT_CARP);
2985 sc = cifp->if_softc;
2987 return sc->sc_carpdev;
2990 #define rtinitflags(x) \
2991 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
2995 carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
2997 struct in_ifaddr *ia, *iaback;
3000 if (sc->sc_state != MASTER)
3004 KKASSERT(ia != NULL);
3006 iaback = vha->vha_iaback;
3007 KKASSERT(iaback != NULL);
3009 rtinit(&iaback->ia_ifa, RTM_DELETE, rtinitflags(iaback));
3010 in_ifadown(&iaback->ia_ifa, 1);
3011 iaback->ia_flags &= ~IFA_ROUTE;
3013 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
3015 ia->ia_flags |= IFA_ROUTE;
3020 carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
3021 boolean_t del_iaback)
3023 struct in_ifaddr *ia, *iaback;
3026 KKASSERT(ia != NULL);
3028 iaback = vha->vha_iaback;
3029 KKASSERT(iaback != NULL);
3031 rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia));
3032 in_ifadown(&ia->ia_ifa, 1);
3033 ia->ia_flags &= ~IFA_ROUTE;
3035 if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) {
3038 error = rtinit(&iaback->ia_ifa, RTM_ADD,
3039 rtinitflags(iaback) | RTF_UP);
3041 iaback->ia_flags |= IFA_ROUTE;
3046 carp_modevent(module_t mod, int type, void *data)
3050 LIST_INIT(&carpif_list);
3051 carp_ifdetach_event =
3052 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
3053 EVENTHANDLER_PRI_ANY);
3055 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
3056 EVENTHANDLER_PRI_FIRST);
3057 if_clone_attach(&carp_cloner);
3061 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
3062 carp_ifdetach_event);
3063 EVENTHANDLER_DEREGISTER(ifaddr_event,
3065 if_clone_detach(&carp_cloner);
3074 static moduledata_t carp_mod = {
3079 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);