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/sbin/ifconfig/ifcarp.c,v 1.2 2005/02/22 14:07:47 glebius Exp $
28 * $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $
29 * $DragonFly: src/sbin/ifconfig/ifcarp.c,v 1.1 2007/08/16 20:03:55 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
40 #include <net/ethernet.h>
42 #include <netinet/in.h>
43 #include <netinet/ip_carp.h>
44 #include <net/route.h>
46 #include <arpa/inet.h>
58 static const char *carp_states[] = { CARP_STATES };
60 void carp_status(int s);
61 void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
62 void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
63 void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
64 void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
72 char devname[IFNAMSIZ];
74 memset((char *)&carpr, 0, sizeof(struct carpreq));
75 ifr.ifr_data = (caddr_t)&carpr;
77 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
80 if (carpr.carpr_vhid > 0) {
81 if (carpr.carpr_state > CARP_MAXSTATE)
84 state = carp_states[carpr.carpr_state];
86 printf("\tcarp: %s vhid %d advbase %d advskew %d\n",
87 state, carpr.carpr_vhid, carpr.carpr_advbase,
91 memset(&ifd, 0, sizeof(ifd));
92 strncpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
93 ifd.ifd_cmd = CARPGDEVNAME;
94 ifd.ifd_len = sizeof(devname);
95 ifd.ifd_data = devname;
96 if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0)
97 strlcpy(devname, "none", sizeof(devname));
98 if (devname[0] != '\0')
99 printf("\tcarpdev: %s\n", devname);
103 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
105 struct carpreq carpr;
107 memset((char *)&carpr, 0, sizeof(struct carpreq));
108 ifr.ifr_data = (caddr_t)&carpr;
110 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
113 /* XXX Should hash the password into the key here, perhaps? */
114 strlcpy(carpr.carpr_key, val, CARP_KEY_LEN);
116 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
121 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
124 struct carpreq carpr;
129 errx(1, "vhid must be greater than 0");
131 memset((char *)&carpr, 0, sizeof(struct carpreq));
132 ifr.ifr_data = (caddr_t)&carpr;
134 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
137 carpr.carpr_vhid = vhid;
139 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
144 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
147 struct carpreq carpr;
151 memset((char *)&carpr, 0, sizeof(struct carpreq));
152 ifr.ifr_data = (caddr_t)&carpr;
154 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
157 carpr.carpr_advskew = advskew;
159 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
164 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
167 struct carpreq carpr;
171 memset((char *)&carpr, 0, sizeof(struct carpreq));
172 ifr.ifr_data = (caddr_t)&carpr;
174 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
177 carpr.carpr_advbase = advbase;
179 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
184 getcarp_vhaddr(const char *val, int d, int s, const struct afswtch *afp)
186 #define VHADDR_PFMT "%-15s %-15s %s\n"
189 struct ifcarpvhaddr *carpa;
192 memset(&ifd, 0, sizeof(ifd));
193 strncpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
194 ifd.ifd_cmd = CARPGVHADDR;
195 if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0)
197 if (ifd.ifd_len != 0) {
198 carpa = malloc(ifd.ifd_len);
202 ifd.ifd_cmd = CARPGVHADDR;
203 ifd.ifd_data = carpa;
204 if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0) {
211 count = ifd.ifd_len / sizeof(*carpa);
213 printf(VHADDR_PFMT, "virtual addr", "backing addr", "flags");
214 for (i = 0; i < count; ++i) {
216 char baddr[INET_ADDRSTRLEN];
219 memset(flags, 0, sizeof(flags));
221 if (carpa[i].carpa_flags & CARP_VHAF_OWNER)
224 memset(baddr, 0, sizeof(baddr));
226 if (carpa[i].carpa_baddr.sin_addr.s_addr != INADDR_ANY) {
227 inet_ntop(AF_INET, &carpa[i].carpa_baddr.sin_addr,
228 baddr, sizeof(baddr));
231 printf(VHADDR_PFMT, inet_ntoa(carpa[i].carpa_addr.sin_addr),
240 static struct cmd carp_cmds[] = {
241 DEF_CMD_ARG("advbase", setcarp_advbase),
242 DEF_CMD_ARG("advskew", setcarp_advskew),
243 DEF_CMD_ARG("pass", setcarp_passwd),
244 DEF_CMD_ARG("vhid", setcarp_vhid),
245 DEF_CMD("vhaddr", 1, getcarp_vhaddr)
247 static struct afswtch af_carp = {
248 .af_name = "af_carp",
250 .af_other_status = carp_status,
253 static __constructor void
256 #define N(a) (sizeof(a) / sizeof(a[0]))
259 for (i = 0; i < N(carp_cmds); i++)
260 cmd_register(&carp_cmds[i]);
261 af_register(&af_carp);