2 /* from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
8 * Copyright (c) 2003 Ryan McBride. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
41 #include <netinet/in.h>
42 #include <netinet/in_var.h>
43 #include <netinet/ip_carp.h>
53 #include <libifconfig.h>
57 static const char *carp_states[] = { CARP_STATES };
59 static void carp_status(int s);
60 static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
61 static void setcarp_callback(int, void *);
62 static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
63 static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
64 static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
66 static int carpr_vhid = -1;
67 static int carpr_advskew = -1;
68 static int carpr_advbase = -1;
69 static int carpr_state = -1;
70 static unsigned char const *carpr_key;
75 struct carpreq carpr[CARP_MAXVHID];
77 if (ifconfig_carp_get_info(lifh, name, carpr, CARP_MAXVHID) == -1)
80 for (size_t i = 0; i < carpr[0].carpr_count; i++) {
81 printf("\tcarp: %s vhid %d advbase %d advskew %d",
82 carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
83 carpr[i].carpr_advbase, carpr[i].carpr_advskew);
84 if (printkeys && carpr[i].carpr_key[0] != '\0')
85 printf(" key \"%s\"\n", carpr[i].carpr_key);
92 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
95 carpr_vhid = atoi(val);
97 if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID)
98 errx(1, "vhid must be greater than 0 and less than %u",
101 switch (afp->af_af) {
105 struct in_aliasreq *ifra;
107 ifra = (struct in_aliasreq *)afp->af_addreq;
108 ifra->ifra_vhid = carpr_vhid;
115 struct in6_aliasreq *ifra;
117 ifra = (struct in6_aliasreq *)afp->af_addreq;
118 ifra->ifra_vhid = carpr_vhid;
123 errx(1, "%s doesn't support carp(4)", afp->af_name);
126 callback_register(setcarp_callback, NULL);
130 setcarp_callback(int s, void *arg __unused)
132 struct carpreq carpr;
134 bzero(&carpr, sizeof(struct carpreq));
135 carpr.carpr_vhid = carpr_vhid;
136 carpr.carpr_count = 1;
137 ifr.ifr_data = (caddr_t)&carpr;
139 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1 && errno != ENOENT)
142 if (carpr_key != NULL)
143 /* XXX Should hash the password into the key here? */
144 strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
145 if (carpr_advskew > -1)
146 carpr.carpr_advskew = carpr_advskew;
147 if (carpr_advbase > -1)
148 carpr.carpr_advbase = carpr_advbase;
149 if (carpr_state > -1)
150 carpr.carpr_state = carpr_state;
152 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
157 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
160 if (carpr_vhid == -1)
161 errx(1, "passwd requires vhid");
167 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
170 if (carpr_vhid == -1)
171 errx(1, "advskew requires vhid");
173 carpr_advskew = atoi(val);
177 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
180 if (carpr_vhid == -1)
181 errx(1, "advbase requires vhid");
183 carpr_advbase = atoi(val);
187 setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
191 if (carpr_vhid == -1)
192 errx(1, "state requires vhid");
194 for (i = 0; i <= CARP_MAXSTATE; i++)
195 if (strcasecmp(carp_states[i], val) == 0) {
200 errx(1, "unknown state");
203 static struct cmd carp_cmds[] = {
204 DEF_CMD_ARG("advbase", setcarp_advbase),
205 DEF_CMD_ARG("advskew", setcarp_advskew),
206 DEF_CMD_ARG("pass", setcarp_passwd),
207 DEF_CMD_ARG("vhid", setcarp_vhid),
208 DEF_CMD_ARG("state", setcarp_state),
210 static struct afswtch af_carp = {
211 .af_name = "af_carp",
213 .af_other_status = carp_status,
216 static __constructor void
221 for (i = 0; i < nitems(carp_cmds); i++)
222 cmd_register(&carp_cmds[i]);
223 af_register(&af_carp);