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