carp.c revision 1.8 1 1.8 dyoung /* $NetBSD: carp.c,v 1.8 2008/05/07 20:45:01 dyoung Exp $ */
2 1.1 liamjfoy
3 1.1 liamjfoy /*
4 1.1 liamjfoy * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
5 1.1 liamjfoy * Copyright (c) 2003 Ryan McBride. All rights reserved.
6 1.1 liamjfoy *
7 1.1 liamjfoy * Redistribution and use in source and binary forms, with or without
8 1.1 liamjfoy * modification, are permitted provided that the following conditions
9 1.1 liamjfoy * are met:
10 1.1 liamjfoy * 1. Redistributions of source code must retain the above copyright
11 1.1 liamjfoy * notice, this list of conditions and the following disclaimer.
12 1.1 liamjfoy * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 liamjfoy * notice, this list of conditions and the following disclaimer in the
14 1.1 liamjfoy * documentation and/or other materials provided with the distribution.
15 1.1 liamjfoy *
16 1.1 liamjfoy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 liamjfoy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 liamjfoy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 liamjfoy * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20 1.1 liamjfoy * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 liamjfoy * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 liamjfoy * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 liamjfoy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.1 liamjfoy * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 1.1 liamjfoy * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 1.1 liamjfoy * THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 liamjfoy */
28 1.1 liamjfoy
29 1.1 liamjfoy #include <sys/param.h>
30 1.1 liamjfoy #include <sys/ioctl.h>
31 1.1 liamjfoy #include <sys/socket.h>
32 1.1 liamjfoy #include <sys/sockio.h>
33 1.1 liamjfoy
34 1.1 liamjfoy #include <net/if.h>
35 1.1 liamjfoy #include <netinet/ip_carp.h>
36 1.1 liamjfoy #include <net/route.h>
37 1.1 liamjfoy
38 1.1 liamjfoy #include <stdio.h>
39 1.1 liamjfoy #include <string.h>
40 1.1 liamjfoy #include <stdlib.h>
41 1.1 liamjfoy #include <unistd.h>
42 1.1 liamjfoy #include <err.h>
43 1.1 liamjfoy #include <errno.h>
44 1.5 dyoung #include <util.h>
45 1.1 liamjfoy
46 1.5 dyoung #include "env.h"
47 1.5 dyoung #include "parse.h"
48 1.1 liamjfoy #include "extern.h"
49 1.5 dyoung #include "carp.h"
50 1.1 liamjfoy
51 1.5 dyoung static const char *carp_states[] = { CARP_STATES };
52 1.1 liamjfoy
53 1.5 dyoung #ifndef INET_ONLY
54 1.5 dyoung struct kwinst carpstatekw[] = {
55 1.5 dyoung {.k_word = "INIT", .k_nextparser = &command_root.pb_parser}
56 1.5 dyoung , {.k_word = "BACKUP", .k_nextparser = &command_root.pb_parser}
57 1.5 dyoung , {.k_word = "MASTER", .k_nextparser = &command_root.pb_parser}
58 1.5 dyoung };
59 1.5 dyoung
60 1.5 dyoung struct pinteger parse_advbase = PINTEGER_INITIALIZER1(&parse_advbase, "advbase",
61 1.5 dyoung 0, 255, 10, setcarp_advbase, "advbase", &command_root.pb_parser);
62 1.5 dyoung
63 1.5 dyoung struct pinteger parse_advskew = PINTEGER_INITIALIZER1(&parse_advskew, "advskew",
64 1.5 dyoung 0, 254, 10, setcarp_advskew, "advskew", &command_root.pb_parser);
65 1.5 dyoung
66 1.5 dyoung struct piface carpdev = PIFACE_INITIALIZER(&carpdev, "carpdev", setcarpdev,
67 1.5 dyoung "carpdev", &command_root.pb_parser);
68 1.5 dyoung
69 1.5 dyoung struct pkw carpstate = PKW_INITIALIZER(&carpstate, "carp state", NULL,
70 1.5 dyoung "carp_state", carpstatekw, __arraycount(carpstatekw),
71 1.5 dyoung &command_root.pb_parser);
72 1.5 dyoung
73 1.5 dyoung struct pstr pass = PSTR_INITIALIZER(&pass, "pass", setcarp_passwd,
74 1.5 dyoung "pass", &command_root.pb_parser);
75 1.5 dyoung
76 1.5 dyoung struct pinteger parse_vhid = PINTEGER_INITIALIZER1(&vhid, "vhid",
77 1.5 dyoung 0, 255, 10, setcarp_vhid, "vhid", &command_root.pb_parser);
78 1.8 dyoung
79 1.8 dyoung static const struct kwinst carpkw[] = {
80 1.8 dyoung {.k_word = "advbase", .k_nextparser = &parse_advbase.pi_parser}
81 1.8 dyoung , {.k_word = "advskew", .k_nextparser = &parse_advskew.pi_parser}
82 1.8 dyoung , {.k_word = "carpdev", .k_nextparser = &carpdev.pif_parser}
83 1.8 dyoung , {.k_word = "-carpdev", .k_key = "carpdev", .k_type = KW_T_STR,
84 1.8 dyoung .k_str = "", .k_exec = setcarpdev,
85 1.8 dyoung .k_nextparser = &command_root.pb_parser}
86 1.8 dyoung , {.k_word = "pass", .k_nextparser = &pass.ps_parser}
87 1.8 dyoung , {.k_word = "state", .k_nextparser = &carpstate.pk_parser}
88 1.8 dyoung , {.k_word = "vhid", .k_nextparser = &parse_vhid.pi_parser}
89 1.8 dyoung };
90 1.8 dyoung
91 1.8 dyoung struct pkw carp = PKW_INITIALIZER(&carp, "CARP", NULL, NULL,
92 1.8 dyoung carpkw, __arraycount(carpkw), NULL);
93 1.8 dyoung
94 1.5 dyoung #endif
95 1.1 liamjfoy
96 1.1 liamjfoy void
97 1.6 dyoung carp_status(prop_dictionary_t env, prop_dictionary_t oenv)
98 1.1 liamjfoy {
99 1.1 liamjfoy const char *state;
100 1.5 dyoung struct ifreq ifr;
101 1.1 liamjfoy struct carpreq carpr;
102 1.5 dyoung int s;
103 1.5 dyoung const char *ifname;
104 1.1 liamjfoy
105 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
106 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
107 1.5 dyoung
108 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
109 1.5 dyoung memset(&carpr, 0, sizeof(carpr));
110 1.3 dyoung ifr.ifr_data = &carpr;
111 1.5 dyoung if ((ifname = getifname(env)) == NULL)
112 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
113 1.5 dyoung
114 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
115 1.1 liamjfoy
116 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
117 1.1 liamjfoy return;
118 1.1 liamjfoy
119 1.5 dyoung if (carpr.carpr_vhid <= 0)
120 1.5 dyoung return;
121 1.5 dyoung if (carpr.carpr_state > CARP_MAXSTATE)
122 1.5 dyoung state = "<UNKNOWN>";
123 1.5 dyoung else
124 1.5 dyoung state = carp_states[carpr.carpr_state];
125 1.5 dyoung
126 1.5 dyoung printf("\tcarp: %s carpdev %s vhid %d advbase %d advskew %d\n",
127 1.5 dyoung state, carpr.carpr_carpdev[0] != '\0' ?
128 1.5 dyoung carpr.carpr_carpdev : "none", carpr.carpr_vhid,
129 1.5 dyoung carpr.carpr_advbase, carpr.carpr_advskew);
130 1.1 liamjfoy }
131 1.1 liamjfoy
132 1.5 dyoung int
133 1.5 dyoung setcarp_passwd(prop_dictionary_t env, prop_dictionary_t xenv)
134 1.1 liamjfoy {
135 1.1 liamjfoy struct carpreq carpr;
136 1.5 dyoung struct ifreq ifr;
137 1.5 dyoung int s;
138 1.5 dyoung prop_data_t data;
139 1.5 dyoung const char *ifname;
140 1.5 dyoung
141 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
142 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
143 1.5 dyoung
144 1.5 dyoung data = (prop_data_t)prop_dictionary_get(env, "pass");
145 1.5 dyoung if (data == NULL) {
146 1.5 dyoung errno = ENOENT;
147 1.5 dyoung return -1;
148 1.5 dyoung }
149 1.1 liamjfoy
150 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
151 1.5 dyoung memset(&carpr, 0, sizeof(carpr));
152 1.3 dyoung ifr.ifr_data = &carpr;
153 1.1 liamjfoy
154 1.5 dyoung if ((ifname = getifname(env)) == NULL)
155 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
156 1.5 dyoung
157 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
158 1.5 dyoung
159 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
160 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
161 1.1 liamjfoy
162 1.5 dyoung memset(carpr.carpr_key, 0, sizeof(carpr.carpr_key));
163 1.1 liamjfoy /* XXX Should hash the password into the key here, perhaps? */
164 1.5 dyoung strlcpy((char *)carpr.carpr_key, prop_data_data_nocopy(data),
165 1.5 dyoung MIN(CARP_KEY_LEN, prop_data_size(data)));
166 1.1 liamjfoy
167 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
168 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
169 1.5 dyoung return 0;
170 1.1 liamjfoy }
171 1.1 liamjfoy
172 1.5 dyoung int
173 1.5 dyoung setcarp_vhid(prop_dictionary_t env, prop_dictionary_t xenv)
174 1.1 liamjfoy {
175 1.5 dyoung struct ifreq ifr;
176 1.1 liamjfoy struct carpreq carpr;
177 1.7 dyoung int64_t vhid;
178 1.5 dyoung int s;
179 1.1 liamjfoy
180 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
181 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
182 1.1 liamjfoy
183 1.7 dyoung if (!prop_dictionary_get_int64(env, "vhid", &vhid)) {
184 1.5 dyoung errno = ENOENT;
185 1.5 dyoung return -1;
186 1.5 dyoung }
187 1.1 liamjfoy
188 1.4 dyoung memset(&carpr, 0, sizeof(struct carpreq));
189 1.3 dyoung ifr.ifr_data = &carpr;
190 1.1 liamjfoy
191 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
192 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
193 1.1 liamjfoy
194 1.1 liamjfoy carpr.carpr_vhid = vhid;
195 1.1 liamjfoy
196 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
197 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
198 1.5 dyoung return 0;
199 1.1 liamjfoy }
200 1.1 liamjfoy
201 1.5 dyoung int
202 1.5 dyoung setcarp_advskew(prop_dictionary_t env, prop_dictionary_t xenv)
203 1.1 liamjfoy {
204 1.5 dyoung struct ifreq ifr;
205 1.1 liamjfoy struct carpreq carpr;
206 1.7 dyoung int64_t advskew;
207 1.5 dyoung int s;
208 1.5 dyoung const char *ifname;
209 1.5 dyoung
210 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
211 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
212 1.5 dyoung
213 1.7 dyoung if (!prop_dictionary_get_int64(env, "advskew", &advskew)) {
214 1.5 dyoung errno = ENOENT;
215 1.5 dyoung return -1;
216 1.5 dyoung }
217 1.1 liamjfoy
218 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
219 1.5 dyoung memset(&carpr, 0, sizeof(carpr));
220 1.5 dyoung ifr.ifr_data = &carpr;
221 1.5 dyoung if ((ifname = getifname(env)) == NULL)
222 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
223 1.1 liamjfoy
224 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
225 1.1 liamjfoy
226 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
227 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
228 1.1 liamjfoy
229 1.1 liamjfoy carpr.carpr_advskew = advskew;
230 1.1 liamjfoy
231 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
232 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
233 1.5 dyoung return 0;
234 1.1 liamjfoy }
235 1.1 liamjfoy
236 1.1 liamjfoy /* ARGSUSED */
237 1.5 dyoung int
238 1.5 dyoung setcarp_advbase(prop_dictionary_t env, prop_dictionary_t xenv)
239 1.1 liamjfoy {
240 1.1 liamjfoy struct carpreq carpr;
241 1.7 dyoung int64_t advbase;
242 1.5 dyoung int s;
243 1.5 dyoung struct ifreq ifr;
244 1.5 dyoung const char *ifname;
245 1.5 dyoung
246 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
247 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
248 1.5 dyoung
249 1.7 dyoung if (!prop_dictionary_get_int64(env, "advbase", &advbase)) {
250 1.5 dyoung errno = ENOENT;
251 1.5 dyoung return -1;
252 1.5 dyoung }
253 1.1 liamjfoy
254 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
255 1.5 dyoung memset(&carpr, 0, sizeof(carpr));
256 1.5 dyoung ifr.ifr_data = &carpr;
257 1.5 dyoung if ((ifname = getifname(env)) == NULL)
258 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
259 1.1 liamjfoy
260 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
261 1.1 liamjfoy
262 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
263 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
264 1.1 liamjfoy
265 1.1 liamjfoy carpr.carpr_advbase = advbase;
266 1.1 liamjfoy
267 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
268 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
269 1.5 dyoung return 0;
270 1.1 liamjfoy }
271 1.1 liamjfoy
272 1.1 liamjfoy /* ARGSUSED */
273 1.5 dyoung int
274 1.5 dyoung setcarp_state(prop_dictionary_t env, prop_dictionary_t xenv)
275 1.1 liamjfoy {
276 1.1 liamjfoy struct carpreq carpr;
277 1.5 dyoung int s;
278 1.5 dyoung struct ifreq ifr;
279 1.7 dyoung int64_t carp_state;
280 1.5 dyoung const char *ifname;
281 1.5 dyoung
282 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
283 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
284 1.5 dyoung
285 1.7 dyoung if (!prop_dictionary_get_int64(env, "carp_state", &carp_state)) {
286 1.5 dyoung errno = ENOENT;
287 1.5 dyoung return -1;
288 1.5 dyoung }
289 1.1 liamjfoy
290 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
291 1.2 dyoung memset(&carpr, 0, sizeof(carpr));
292 1.3 dyoung ifr.ifr_data = &carpr;
293 1.5 dyoung if ((ifname = getifname(env)) == NULL)
294 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
295 1.5 dyoung
296 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
297 1.1 liamjfoy
298 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
299 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
300 1.1 liamjfoy
301 1.7 dyoung carpr.carpr_state = carp_state;
302 1.1 liamjfoy
303 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
304 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
305 1.5 dyoung return 0;
306 1.1 liamjfoy }
307 1.1 liamjfoy
308 1.1 liamjfoy /* ARGSUSED */
309 1.5 dyoung int
310 1.5 dyoung setcarpdev(prop_dictionary_t env, prop_dictionary_t xenv)
311 1.1 liamjfoy {
312 1.1 liamjfoy struct carpreq carpr;
313 1.5 dyoung int s;
314 1.5 dyoung prop_data_t data;
315 1.5 dyoung struct ifreq ifr;
316 1.5 dyoung const char *ifname;
317 1.5 dyoung
318 1.5 dyoung data = (prop_data_t)prop_dictionary_get(env, "carpdev");
319 1.5 dyoung if (data == NULL) {
320 1.5 dyoung errno = ENOENT;
321 1.5 dyoung return -1;
322 1.5 dyoung }
323 1.5 dyoung
324 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
325 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
326 1.1 liamjfoy
327 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
328 1.2 dyoung memset(&carpr, 0, sizeof(carpr));
329 1.3 dyoung ifr.ifr_data = &carpr;
330 1.5 dyoung if ((ifname = getifname(env)) == NULL)
331 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
332 1.1 liamjfoy
333 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
334 1.1 liamjfoy
335 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
336 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
337 1.1 liamjfoy
338 1.5 dyoung strlcpy(carpr.carpr_carpdev, prop_data_data_nocopy(data),
339 1.5 dyoung MIN(sizeof(carpr.carpr_carpdev), prop_data_size(data)));
340 1.1 liamjfoy
341 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
342 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
343 1.5 dyoung return 0;
344 1.1 liamjfoy }
345