xref: /f-stack/tools/ifconfig/carp.c (revision ebf5cedb)
1 /*	$FreeBSD$ */
2 /*	from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */
3 
4 /*
5  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
6  * Copyright (c) 2003 Ryan McBride. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <netinet/in_var.h>
41 #include <netinet/ip_carp.h>
42 
43 #include <netinet6/in6_var.h>
44 
45 #include <ctype.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <err.h>
51 #include <errno.h>
52 
53 #include "ifconfig.h"
54 
55 static const char *carp_states[] = { CARP_STATES };
56 
57 static void carp_status(int s);
58 static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
59 static void setcarp_callback(int, void *);
60 static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
61 static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
62 static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
63 
64 static int carpr_vhid = -1;
65 static int carpr_advskew = -1;
66 static int carpr_advbase = -1;
67 static int carpr_state = -1;
68 static unsigned char const *carpr_key;
69 
70 static void
71 carp_status(int s)
72 {
73 	struct carpreq carpr[CARP_MAXVHID];
74 	int i;
75 
76 	bzero(carpr, sizeof(struct carpreq) * CARP_MAXVHID);
77 	carpr[0].carpr_count = CARP_MAXVHID;
78 	ifr.ifr_data = (caddr_t)&carpr;
79 
80 #ifndef FSTACK
81 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
82 #else
83 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
84 	size_t clen = sizeof(struct carpreq) * CARP_MAXVHID;
85 	if (ioctl_va(s, SIOCGVH, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1)
86 #endif
87 		return;
88 
89 	for (i = 0; i < carpr[0].carpr_count; i++) {
90 		printf("\tcarp: %s vhid %d advbase %d advskew %d",
91 		    carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
92 		    carpr[i].carpr_advbase, carpr[i].carpr_advskew);
93 		if (printkeys && carpr[i].carpr_key[0] != '\0')
94 			printf(" key \"%s\"\n", carpr[i].carpr_key);
95 		else
96 			printf("\n");
97 	}
98 }
99 
100 static void
101 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
102 {
103 
104 	carpr_vhid = atoi(val);
105 
106 	if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID)
107 		errx(1, "vhid must be greater than 0 and less than %u",
108 		    CARP_MAXVHID);
109 
110 	switch (afp->af_af) {
111 #ifdef INET
112 	case AF_INET:
113 	    {
114 		struct in_aliasreq *ifra;
115 
116 		ifra = (struct in_aliasreq *)afp->af_addreq;
117 		ifra->ifra_vhid = carpr_vhid;
118 		break;
119 	    }
120 #endif
121 #ifdef INET6
122 	case AF_INET6:
123 	    {
124 		struct in6_aliasreq *ifra;
125 
126 		ifra = (struct in6_aliasreq *)afp->af_addreq;
127 		ifra->ifra_vhid = carpr_vhid;
128 		break;
129 	    }
130 #endif
131 	default:
132 		errx(1, "%s doesn't support carp(4)", afp->af_name);
133 	}
134 
135 	callback_register(setcarp_callback, NULL);
136 }
137 
138 static void
139 setcarp_callback(int s, void *arg __unused)
140 {
141 	struct carpreq carpr;
142 
143 	bzero(&carpr, sizeof(struct carpreq));
144 	carpr.carpr_vhid = carpr_vhid;
145 	carpr.carpr_count = 1;
146 	ifr.ifr_data = (caddr_t)&carpr;
147 
148 #ifndef FSTACK
149 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1 && errno != ENOENT)
150 #else
151 	size_t offset, clen;
152 	offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
153 	clen = sizeof(carpr);
154 	if (ioctl_va(s, SIOCGVH, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1
155 		&& errno != ENOENT)
156 #endif
157 		err(1, "SIOCGVH");
158 
159 	if (carpr_key != NULL)
160 		/* XXX Should hash the password into the key here? */
161 #ifndef FSTACK
162 		strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
163 #else
164 		strlcpy((char *)carpr.carpr_key, (const char *)carpr_key, CARP_KEY_LEN);
165 #endif
166 	if (carpr_advskew > -1)
167 		carpr.carpr_advskew = carpr_advskew;
168 	if (carpr_advbase > -1)
169 		carpr.carpr_advbase = carpr_advbase;
170 	if (carpr_state > -1)
171 		carpr.carpr_state = carpr_state;
172 
173 #ifndef FSTACK
174 	if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
175 #else
176 	if (ioctl_va(s, SIOCSVH, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1)
177 #endif
178 		err(1, "SIOCSVH");
179 }
180 
181 static void
182 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
183 {
184 
185 	if (carpr_vhid == -1)
186 		errx(1, "passwd requires vhid");
187 
188 #ifndef FSTACK
189 	carpr_key = val;
190 #else
191 	carpr_key = (const unsigned char *)val;
192 #endif
193 }
194 
195 static void
196 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
197 {
198 
199 	if (carpr_vhid == -1)
200 		errx(1, "advskew requires vhid");
201 
202 	carpr_advskew = atoi(val);
203 }
204 
205 static void
206 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
207 {
208 
209 	if (carpr_vhid == -1)
210 		errx(1, "advbase requires vhid");
211 
212 	carpr_advbase = atoi(val);
213 }
214 
215 static void
216 setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
217 {
218 	int i;
219 
220 	if (carpr_vhid == -1)
221 		errx(1, "state requires vhid");
222 
223 	for (i = 0; i <= CARP_MAXSTATE; i++)
224 		if (strcasecmp(carp_states[i], val) == 0) {
225 			carpr_state = i;
226 			return;
227 		}
228 
229 	errx(1, "unknown state");
230 }
231 
232 static struct cmd carp_cmds[] = {
233 	DEF_CMD_ARG("advbase",	setcarp_advbase),
234 	DEF_CMD_ARG("advskew",	setcarp_advskew),
235 	DEF_CMD_ARG("pass",	setcarp_passwd),
236 	DEF_CMD_ARG("vhid",	setcarp_vhid),
237 	DEF_CMD_ARG("state",	setcarp_state),
238 };
239 static struct afswtch af_carp = {
240 	.af_name	= "af_carp",
241 	.af_af		= AF_UNSPEC,
242 	.af_other_status = carp_status,
243 };
244 
245 static __constructor void
246 carp_ctor(void)
247 {
248 	int i;
249 
250 	for (i = 0; i < nitems(carp_cmds);  i++)
251 		cmd_register(&carp_cmds[i]);
252 	af_register(&af_carp);
253 }
254