2 /* from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */
5 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
6 * Copyright (c) 2003 Ryan McBride. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
38 #include <net/ethernet.h>
40 #include <netinet/ip_carp.h>
41 #include <net/route.h>
53 static const char *carp_states[] = { CARP_STATES };
55 void carp_status(int s);
56 void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
57 void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
58 void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
59 void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
60 void setcarp_state(const char *, int, int, const struct afswtch *rafp);
68 memset((char *)&carpr, 0, sizeof(struct carpreq));
69 ifr.ifr_data = (caddr_t)&carpr;
71 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
74 if (carpr.carpr_vhid > 0) {
75 if (carpr.carpr_state > CARP_MAXSTATE)
78 state = carp_states[carpr.carpr_state];
80 printf("\tcarp: %s vhid %d advbase %d advskew %d\n",
81 state, carpr.carpr_vhid, carpr.carpr_advbase,
90 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
94 memset((char *)&carpr, 0, sizeof(struct carpreq));
95 ifr.ifr_data = (caddr_t)&carpr;
97 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
100 memset(carpr.carpr_key, 0, sizeof(carpr.carpr_key));
101 /* XXX Should hash the password into the key here, perhaps? */
102 strlcpy(carpr.carpr_key, val, CARP_KEY_LEN);
104 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
111 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
114 struct carpreq carpr;
119 errx(1, "vhid must be greater than 0");
121 memset((char *)&carpr, 0, sizeof(struct carpreq));
122 ifr.ifr_data = (caddr_t)&carpr;
124 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
127 carpr.carpr_vhid = vhid;
129 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
136 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
139 struct carpreq carpr;
143 memset((char *)&carpr, 0, sizeof(struct carpreq));
144 ifr.ifr_data = (caddr_t)&carpr;
146 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
149 carpr.carpr_advskew = advskew;
151 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
158 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
161 struct carpreq carpr;
165 memset((char *)&carpr, 0, sizeof(struct carpreq));
166 ifr.ifr_data = (caddr_t)&carpr;
168 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
171 carpr.carpr_advbase = advbase;
173 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
179 void setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
181 struct carpreq carpr;
184 bzero((char *)&carpr, sizeof(struct carpreq));
185 ifr.ifr_data = (caddr_t)&carpr;
187 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
190 for (i = 0; i <= CARP_MAXSTATE; i++) {
191 if (!strcasecmp(val, carp_states[i])) {
192 carpr.carpr_state = i;
197 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
201 static struct cmd carp_cmds[] = {
202 DEF_CMD_ARG("advbase", setcarp_advbase),
203 DEF_CMD_ARG("advskew", setcarp_advskew),
204 DEF_CMD_ARG("pass", setcarp_passwd),
205 DEF_CMD_ARG("vhid", setcarp_vhid),
206 DEF_CMD_ARG("state", setcarp_state),
208 static struct afswtch af_carp = {
209 .af_name = "af_carp",
211 .af_other_status = carp_status,
214 static __constructor void
217 #define N(a) (sizeof(a) / sizeof(a[0]))
220 for (i = 0; i < N(carp_cmds); i++)
221 cmd_register(&carp_cmds[i]);
222 af_register(&af_carp);