carp.c revision 1.6 1 1.6 dyoung /* $NetBSD: carp.c,v 1.6 2008/05/06 17:29:04 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.5 dyoung #endif
79 1.1 liamjfoy
80 1.1 liamjfoy void
81 1.6 dyoung carp_status(prop_dictionary_t env, prop_dictionary_t oenv)
82 1.1 liamjfoy {
83 1.1 liamjfoy const char *state;
84 1.5 dyoung struct ifreq ifr;
85 1.1 liamjfoy struct carpreq carpr;
86 1.5 dyoung int s;
87 1.5 dyoung const char *ifname;
88 1.1 liamjfoy
89 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
90 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
91 1.5 dyoung
92 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
93 1.5 dyoung memset(&carpr, 0, sizeof(carpr));
94 1.3 dyoung ifr.ifr_data = &carpr;
95 1.5 dyoung if ((ifname = getifname(env)) == NULL)
96 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
97 1.5 dyoung
98 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
99 1.1 liamjfoy
100 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
101 1.1 liamjfoy return;
102 1.1 liamjfoy
103 1.5 dyoung if (carpr.carpr_vhid <= 0)
104 1.5 dyoung return;
105 1.5 dyoung if (carpr.carpr_state > CARP_MAXSTATE)
106 1.5 dyoung state = "<UNKNOWN>";
107 1.5 dyoung else
108 1.5 dyoung state = carp_states[carpr.carpr_state];
109 1.5 dyoung
110 1.5 dyoung printf("\tcarp: %s carpdev %s vhid %d advbase %d advskew %d\n",
111 1.5 dyoung state, carpr.carpr_carpdev[0] != '\0' ?
112 1.5 dyoung carpr.carpr_carpdev : "none", carpr.carpr_vhid,
113 1.5 dyoung carpr.carpr_advbase, carpr.carpr_advskew);
114 1.1 liamjfoy }
115 1.1 liamjfoy
116 1.5 dyoung int
117 1.5 dyoung setcarp_passwd(prop_dictionary_t env, prop_dictionary_t xenv)
118 1.1 liamjfoy {
119 1.1 liamjfoy struct carpreq carpr;
120 1.5 dyoung struct ifreq ifr;
121 1.5 dyoung int s;
122 1.5 dyoung prop_data_t data;
123 1.5 dyoung const char *ifname;
124 1.5 dyoung
125 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
126 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
127 1.5 dyoung
128 1.5 dyoung data = (prop_data_t)prop_dictionary_get(env, "pass");
129 1.5 dyoung if (data == NULL) {
130 1.5 dyoung errno = ENOENT;
131 1.5 dyoung return -1;
132 1.5 dyoung }
133 1.1 liamjfoy
134 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
135 1.5 dyoung memset(&carpr, 0, sizeof(carpr));
136 1.3 dyoung ifr.ifr_data = &carpr;
137 1.1 liamjfoy
138 1.5 dyoung if ((ifname = getifname(env)) == NULL)
139 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
140 1.5 dyoung
141 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
142 1.5 dyoung
143 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
144 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
145 1.1 liamjfoy
146 1.5 dyoung memset(carpr.carpr_key, 0, sizeof(carpr.carpr_key));
147 1.1 liamjfoy /* XXX Should hash the password into the key here, perhaps? */
148 1.5 dyoung strlcpy((char *)carpr.carpr_key, prop_data_data_nocopy(data),
149 1.5 dyoung MIN(CARP_KEY_LEN, prop_data_size(data)));
150 1.1 liamjfoy
151 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
152 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
153 1.5 dyoung return 0;
154 1.1 liamjfoy }
155 1.1 liamjfoy
156 1.5 dyoung int
157 1.5 dyoung setcarp_vhid(prop_dictionary_t env, prop_dictionary_t xenv)
158 1.1 liamjfoy {
159 1.5 dyoung struct ifreq ifr;
160 1.1 liamjfoy struct carpreq carpr;
161 1.1 liamjfoy int vhid;
162 1.5 dyoung int s;
163 1.5 dyoung prop_number_t num;
164 1.1 liamjfoy
165 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
166 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
167 1.1 liamjfoy
168 1.5 dyoung num = (prop_number_t)prop_dictionary_get(env, "vhid");
169 1.5 dyoung if (num == NULL) {
170 1.5 dyoung errno = ENOENT;
171 1.5 dyoung return -1;
172 1.5 dyoung }
173 1.1 liamjfoy
174 1.5 dyoung vhid = (int)prop_number_integer_value(num);
175 1.1 liamjfoy
176 1.4 dyoung memset(&carpr, 0, sizeof(struct carpreq));
177 1.3 dyoung ifr.ifr_data = &carpr;
178 1.1 liamjfoy
179 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
180 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
181 1.1 liamjfoy
182 1.1 liamjfoy carpr.carpr_vhid = vhid;
183 1.1 liamjfoy
184 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
185 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
186 1.5 dyoung return 0;
187 1.1 liamjfoy }
188 1.1 liamjfoy
189 1.5 dyoung int
190 1.5 dyoung setcarp_advskew(prop_dictionary_t env, prop_dictionary_t xenv)
191 1.1 liamjfoy {
192 1.5 dyoung struct ifreq ifr;
193 1.1 liamjfoy struct carpreq carpr;
194 1.1 liamjfoy int advskew;
195 1.5 dyoung int s;
196 1.5 dyoung prop_number_t num;
197 1.5 dyoung const char *ifname;
198 1.5 dyoung
199 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
200 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
201 1.5 dyoung
202 1.5 dyoung num = (prop_number_t)prop_dictionary_get(env, "advskew");
203 1.5 dyoung if (num == NULL) {
204 1.5 dyoung errno = ENOENT;
205 1.5 dyoung return -1;
206 1.5 dyoung }
207 1.1 liamjfoy
208 1.5 dyoung advskew = (int)prop_number_integer_value(num);
209 1.1 liamjfoy
210 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
211 1.5 dyoung memset(&carpr, 0, sizeof(carpr));
212 1.5 dyoung ifr.ifr_data = &carpr;
213 1.5 dyoung if ((ifname = getifname(env)) == NULL)
214 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
215 1.1 liamjfoy
216 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
217 1.1 liamjfoy
218 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
219 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
220 1.1 liamjfoy
221 1.1 liamjfoy carpr.carpr_advskew = advskew;
222 1.1 liamjfoy
223 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
224 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
225 1.5 dyoung return 0;
226 1.1 liamjfoy }
227 1.1 liamjfoy
228 1.1 liamjfoy /* ARGSUSED */
229 1.5 dyoung int
230 1.5 dyoung setcarp_advbase(prop_dictionary_t env, prop_dictionary_t xenv)
231 1.1 liamjfoy {
232 1.1 liamjfoy struct carpreq carpr;
233 1.1 liamjfoy int advbase;
234 1.5 dyoung int s;
235 1.5 dyoung prop_number_t num;
236 1.5 dyoung struct ifreq ifr;
237 1.5 dyoung const char *ifname;
238 1.5 dyoung
239 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
240 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
241 1.5 dyoung
242 1.5 dyoung num = (prop_number_t)prop_dictionary_get(env, "advbase");
243 1.5 dyoung if (num == NULL) {
244 1.5 dyoung errno = ENOENT;
245 1.5 dyoung return -1;
246 1.5 dyoung }
247 1.1 liamjfoy
248 1.5 dyoung advbase = (int)prop_number_integer_value(num);
249 1.1 liamjfoy
250 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
251 1.5 dyoung memset(&carpr, 0, sizeof(carpr));
252 1.5 dyoung ifr.ifr_data = &carpr;
253 1.5 dyoung if ((ifname = getifname(env)) == NULL)
254 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
255 1.1 liamjfoy
256 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
257 1.1 liamjfoy
258 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
259 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
260 1.1 liamjfoy
261 1.1 liamjfoy carpr.carpr_advbase = advbase;
262 1.1 liamjfoy
263 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
264 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
265 1.5 dyoung return 0;
266 1.1 liamjfoy }
267 1.1 liamjfoy
268 1.1 liamjfoy /* ARGSUSED */
269 1.5 dyoung int
270 1.5 dyoung setcarp_state(prop_dictionary_t env, prop_dictionary_t xenv)
271 1.1 liamjfoy {
272 1.1 liamjfoy struct carpreq carpr;
273 1.5 dyoung int s;
274 1.5 dyoung prop_number_t num;
275 1.5 dyoung struct ifreq ifr;
276 1.5 dyoung const char *ifname;
277 1.5 dyoung
278 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
279 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
280 1.5 dyoung
281 1.5 dyoung num = (prop_number_t)prop_dictionary_get(env, "carp_state");
282 1.5 dyoung if (num == NULL) {
283 1.5 dyoung errno = ENOENT;
284 1.5 dyoung return -1;
285 1.5 dyoung }
286 1.1 liamjfoy
287 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
288 1.2 dyoung memset(&carpr, 0, sizeof(carpr));
289 1.3 dyoung ifr.ifr_data = &carpr;
290 1.5 dyoung if ((ifname = getifname(env)) == NULL)
291 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
292 1.5 dyoung
293 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
294 1.1 liamjfoy
295 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
296 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
297 1.1 liamjfoy
298 1.5 dyoung carpr.carpr_state = (int)prop_number_integer_value(num);
299 1.1 liamjfoy
300 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
301 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
302 1.5 dyoung return 0;
303 1.1 liamjfoy }
304 1.1 liamjfoy
305 1.1 liamjfoy /* ARGSUSED */
306 1.5 dyoung int
307 1.5 dyoung setcarpdev(prop_dictionary_t env, prop_dictionary_t xenv)
308 1.1 liamjfoy {
309 1.1 liamjfoy struct carpreq carpr;
310 1.5 dyoung int s;
311 1.5 dyoung prop_data_t data;
312 1.5 dyoung struct ifreq ifr;
313 1.5 dyoung const char *ifname;
314 1.5 dyoung
315 1.5 dyoung data = (prop_data_t)prop_dictionary_get(env, "carpdev");
316 1.5 dyoung if (data == NULL) {
317 1.5 dyoung errno = ENOENT;
318 1.5 dyoung return -1;
319 1.5 dyoung }
320 1.5 dyoung
321 1.5 dyoung if ((s = getsock(AF_UNSPEC)) == -1)
322 1.5 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
323 1.1 liamjfoy
324 1.5 dyoung memset(&ifr, 0, sizeof(ifr));
325 1.2 dyoung memset(&carpr, 0, sizeof(carpr));
326 1.3 dyoung ifr.ifr_data = &carpr;
327 1.5 dyoung if ((ifname = getifname(env)) == NULL)
328 1.5 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
329 1.1 liamjfoy
330 1.5 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
331 1.1 liamjfoy
332 1.3 dyoung if (ioctl(s, SIOCGVH, &ifr) == -1)
333 1.1 liamjfoy err(EXIT_FAILURE, "SIOCGVH");
334 1.1 liamjfoy
335 1.5 dyoung strlcpy(carpr.carpr_carpdev, prop_data_data_nocopy(data),
336 1.5 dyoung MIN(sizeof(carpr.carpr_carpdev), prop_data_size(data)));
337 1.1 liamjfoy
338 1.3 dyoung if (ioctl(s, SIOCSVH, &ifr) == -1)
339 1.1 liamjfoy err(EXIT_FAILURE, "SIOCSVH");
340 1.5 dyoung return 0;
341 1.1 liamjfoy }
342