aboutsummaryrefslogtreecommitdiff
path: root/sbin/ifconfig/ifcarp.c
diff options
context:
space:
mode:
authorGleb Smirnoff <glebius@FreeBSD.org>2011-12-16 12:16:56 +0000
committerGleb Smirnoff <glebius@FreeBSD.org>2011-12-16 12:16:56 +0000
commit08b68b0e4c6b132127919cfbaf7275c727ca7843 (patch)
tree059f66a20d251fa947969ff34292206d47ffc45c /sbin/ifconfig/ifcarp.c
parentec1c1d41b3e5d997dabe18a69ea5e7364b48d258 (diff)
downloadsrc-08b68b0e4c6b132127919cfbaf7275c727ca7843.tar.gz
src-08b68b0e4c6b132127919cfbaf7275c727ca7843.zip
A major overhaul of the CARP implementation. The ip_carp.c was started
from scratch, copying needed functionality from the old implemenation on demand, with a thorough review of all code. The main change is that interface layer has been removed from the CARP. Now redundant addresses are configured exactly on the interfaces, they run on. The CARP configuration itself is, as before, configured and read via SIOCSVH/SIOCGVH ioctls. A new prefix created with SIOCAIFADDR or SIOCAIFADDR_IN6 may now be configured to a particular virtual host id, which makes the prefix redundant. ifconfig(8) semantics has been changed too: now one doesn't need to clone carpXX interface, he/she should directly configure a vhid on a Ethernet interface. To supply vhid data from the kernel to an application the getifaddrs(8) function had been changed to pass ifam_data with each address. [1] The new implementation definitely closes all PRs related to carp(4) being an interface, and may close several others. It also allows to run a single redundant IP per interface. Big thanks to Bjoern Zeeb for his help with inet6 part of patch, for idea on using ifam_data and for several rounds of reviewing! PR: kern/117000, kern/126945, kern/126714, kern/120130, kern/117448 Reviewed by: bz Submitted by: bz [1]
Notes
Notes: svn path=/head/; revision=228571
Diffstat (limited to 'sbin/ifconfig/ifcarp.c')
-rw-r--r--sbin/ifconfig/ifcarp.c182
1 files changed, 105 insertions, 77 deletions
diff --git a/sbin/ifconfig/ifcarp.c b/sbin/ifconfig/ifcarp.c
index 2306717e63b8..2c58fcb1e931 100644
--- a/sbin/ifconfig/ifcarp.c
+++ b/sbin/ifconfig/ifcarp.c
@@ -35,10 +35,11 @@
#include <stdlib.h>
#include <unistd.h>
-#include <net/ethernet.h>
#include <net/if.h>
+#include <net/if_var.h>
+#include <netinet/in.h>
+#include <netinet/in_var.h>
#include <netinet/ip_carp.h>
-#include <net/route.h>
#include <ctype.h>
#include <stdio.h>
@@ -52,127 +53,153 @@
static const char *carp_states[] = { CARP_STATES };
-void carp_status(int s);
-void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
-void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
-void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
-void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
+static void carp_status(int s);
+static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
+static void setcarp_callback(int, void *);
+static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
+static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
+static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
-void
+static int carpr_vhid = -1;
+static int carpr_advskew = -1;
+static int carpr_advbase = -1;
+static int carpr_state = -1;
+static unsigned char const *carpr_key;
+
+static void
carp_status(int s)
{
- const char *state;
- struct carpreq carpr;
+ struct carpreq carpr[CARP_MAXVHID];
+ int i;
- memset((char *)&carpr, 0, sizeof(struct carpreq));
+ bzero(carpr, sizeof(struct carpreq) * CARP_MAXVHID);
+ carpr[0].carpr_count = CARP_MAXVHID;
ifr.ifr_data = (caddr_t)&carpr;
if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
return;
- if (carpr.carpr_vhid > 0) {
- if (carpr.carpr_state > CARP_MAXSTATE)
- state = "<UNKNOWN>";
+ for (i = 0; i < carpr[0].carpr_count; i++) {
+ printf("\tcarp: %s vhid %d advbase %d advskew %d",
+ carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
+ carpr[i].carpr_advbase, carpr[i].carpr_advskew);
+ if (printkeys && carpr[i].carpr_key[0] != '\0')
+ printf(" key \"%s\"\n", carpr[i].carpr_key);
else
- state = carp_states[carpr.carpr_state];
-
- printf("\tcarp: %s vhid %d advbase %d advskew %d\n",
- state, carpr.carpr_vhid, carpr.carpr_advbase,
- carpr.carpr_advskew);
+ printf("\n");
}
-
- return;
-
}
-void
-setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
+static void
+setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
{
- struct carpreq carpr;
-
- memset((char *)&carpr, 0, sizeof(struct carpreq));
- ifr.ifr_data = (caddr_t)&carpr;
- if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
- err(1, "SIOCGVH");
-
- memset(carpr.carpr_key, 0, sizeof(carpr.carpr_key));
- /* XXX Should hash the password into the key here, perhaps? */
- strlcpy(carpr.carpr_key, val, CARP_KEY_LEN);
-
- if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
- err(1, "SIOCSVH");
+ carpr_vhid = atoi(val);
+
+ if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID)
+ errx(1, "vhid must be greater than 0 and less than %u",
+ CARP_MAXVHID);
+
+ switch (afp->af_af) {
+#ifdef INET
+ case AF_INET:
+ {
+ struct in_aliasreq *ifra;
+
+ ifra = (struct in_aliasreq *)afp->af_addreq;
+ ifra->ifra_vhid = carpr_vhid;
+ break;
+ }
+#endif
+#ifdef INET6
+ case AF_INET6:
+ {
+ struct in6_aliasreq *ifra;
+
+ ifra = (struct in6_aliasreq *)afp->af_addreq;
+ ifra->ifra_vhid = carpr_vhid;
+ break;
+ }
+#endif
+ default:
+ errx(1, "%s doesn't support carp(4)", afp->af_name);
+ }
- return;
+ callback_register(setcarp_callback, NULL);
}
-void
-setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
+static void
+setcarp_callback(int s, void *arg __unused)
{
- int vhid;
struct carpreq carpr;
- vhid = atoi(val);
-
- if (vhid <= 0)
- errx(1, "vhid must be greater than 0");
-
- memset((char *)&carpr, 0, sizeof(struct carpreq));
+ bzero(&carpr, sizeof(struct carpreq));
+ carpr.carpr_vhid = carpr_vhid;
+ carpr.carpr_count = 1;
ifr.ifr_data = (caddr_t)&carpr;
- if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
+ if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1 && errno != ENOENT)
err(1, "SIOCGVH");
- carpr.carpr_vhid = vhid;
+ if (carpr_key != NULL)
+ /* XXX Should hash the password into the key here? */
+ strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
+ if (carpr_advskew > -1)
+ carpr.carpr_advskew = carpr_advskew;
+ if (carpr_advbase > -1)
+ carpr.carpr_advbase = carpr_advbase;
+ if (carpr_state > -1)
+ carpr.carpr_state = carpr_state;
if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
err(1, "SIOCSVH");
-
- return;
}
-void
-setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
+static void
+setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
{
- int advskew;
- struct carpreq carpr;
-
- advskew = atoi(val);
- memset((char *)&carpr, 0, sizeof(struct carpreq));
- ifr.ifr_data = (caddr_t)&carpr;
+ if (carpr_vhid == -1)
+ errx(1, "passwd requires vhid");
- if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
- err(1, "SIOCGVH");
+ carpr_key = val;
+}
- carpr.carpr_advskew = advskew;
+static void
+setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
+{
- if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
- err(1, "SIOCSVH");
+ if (carpr_vhid == -1)
+ errx(1, "advskew requires vhid");
- return;
+ carpr_advskew = atoi(val);
}
-void
+static void
setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
{
- int advbase;
- struct carpreq carpr;
- advbase = atoi(val);
+ if (carpr_vhid == -1)
+ errx(1, "advbase requires vhid");
- memset((char *)&carpr, 0, sizeof(struct carpreq));
- ifr.ifr_data = (caddr_t)&carpr;
+ carpr_advbase = atoi(val);
+}
- if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
- err(1, "SIOCGVH");
+static void
+setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
+{
+ int i;
- carpr.carpr_advbase = advbase;
+ if (carpr_vhid == -1)
+ errx(1, "state requires vhid");
- if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
- err(1, "SIOCSVH");
+ for (i = 0; i <= CARP_MAXSTATE; i++)
+ if (strcasecmp(carp_states[i], val) == 0) {
+ carpr_state = i;
+ return;
+ }
- return;
+ errx(1, "unknown state");
}
static struct cmd carp_cmds[] = {
@@ -180,6 +207,7 @@ static struct cmd carp_cmds[] = {
DEF_CMD_ARG("advskew", setcarp_advskew),
DEF_CMD_ARG("pass", setcarp_passwd),
DEF_CMD_ARG("vhid", setcarp_vhid),
+ DEF_CMD_ARG("state", setcarp_state),
};
static struct afswtch af_carp = {
.af_name = "af_carp",