carp.c revision 1.15 1 1.15 mlelstv /* $NetBSD: carp.c,v 1.15 2023/03/26 01:04:16 mlelstv 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.10 dyoung #include <sys/cdefs.h>
30 1.10 dyoung #ifndef lint
31 1.15 mlelstv __RCSID("$NetBSD: carp.c,v 1.15 2023/03/26 01:04:16 mlelstv Exp $");
32 1.10 dyoung #endif /* not lint */
33 1.10 dyoung
34 1.1 liamjfoy #include <sys/param.h>
35 1.1 liamjfoy #include <sys/ioctl.h>
36 1.1 liamjfoy #include <sys/socket.h>
37 1.1 liamjfoy #include <sys/sockio.h>
38 1.1 liamjfoy
39 1.1 liamjfoy #include <net/if.h>
40 1.1 liamjfoy #include <netinet/ip_carp.h>
41 1.1 liamjfoy #include <net/route.h>
42 1.1 liamjfoy
43 1.1 liamjfoy #include <stdio.h>
44 1.1 liamjfoy #include <string.h>
45 1.1 liamjfoy #include <stdlib.h>
46 1.1 liamjfoy #include <unistd.h>
47 1.1 liamjfoy #include <err.h>
48 1.1 liamjfoy #include <errno.h>
49 1.5 dyoung #include <util.h>
50 1.1 liamjfoy
51 1.5 dyoung #include "env.h"
52 1.5 dyoung #include "parse.h"
53 1.1 liamjfoy #include "extern.h"
54 1.10 dyoung
55 1.10 dyoung static status_func_t status;
56 1.11 dyoung static usage_func_t usage;
57 1.10 dyoung static cmdloop_branch_t branch;
58 1.10 dyoung
59 1.10 dyoung static void carp_constructor(void) __attribute__((constructor));
60 1.10 dyoung static void carp_status(prop_dictionary_t, prop_dictionary_t);
61 1.10 dyoung static int setcarp_advbase(prop_dictionary_t, prop_dictionary_t);
62 1.10 dyoung static int setcarp_advskew(prop_dictionary_t, prop_dictionary_t);
63 1.10 dyoung static int setcarp_passwd(prop_dictionary_t, prop_dictionary_t);
64 1.10 dyoung static int setcarp_vhid(prop_dictionary_t, prop_dictionary_t);
65 1.10 dyoung static int setcarp_state(prop_dictionary_t, prop_dictionary_t);
66 1.10 dyoung static int setcarpdev(prop_dictionary_t, prop_dictionary_t);
67 1.1 liamjfoy
68 1.5 dyoung static const char *carp_states[] = { CARP_STATES };
69 1.1 liamjfoy
70 1.15 mlelstv /* from if_carp.c */
71 1.15 mlelstv enum carpstateval { INIT = 0, BACKUP, MASTER };
72 1.15 mlelstv
73 1.5 dyoung struct kwinst carpstatekw[] = {
74 1.15 mlelstv {.k_word = "INIT", .k_type = KW_T_INT, .k_int = INIT,
75 1.15 mlelstv .k_nextparser = &command_root.pb_parser}
76 1.15 mlelstv , {.k_word = "BACKUP", .k_type = KW_T_INT, .k_int = BACKUP,
77 1.15 mlelstv .k_nextparser = &command_root.pb_parser}
78 1.15 mlelstv , {.k_word = "MASTER", .k_type = KW_T_INT, .k_int = MASTER,
79 1.15 mlelstv .k_nextparser = &command_root.pb_parser}
80 1.5 dyoung };
81 1.5 dyoung
82 1.5 dyoung struct pinteger parse_advbase = PINTEGER_INITIALIZER1(&parse_advbase, "advbase",
83 1.5 dyoung 0, 255, 10, setcarp_advbase, "advbase", &command_root.pb_parser);
84 1.5 dyoung
85 1.5 dyoung struct pinteger parse_advskew = PINTEGER_INITIALIZER1(&parse_advskew, "advskew",
86 1.5 dyoung 0, 254, 10, setcarp_advskew, "advskew", &command_root.pb_parser);
87 1.5 dyoung
88 1.5 dyoung struct piface carpdev = PIFACE_INITIALIZER(&carpdev, "carpdev", setcarpdev,
89 1.5 dyoung "carpdev", &command_root.pb_parser);
90 1.5 dyoung
91 1.10 dyoung struct pkw carpstate = PKW_INITIALIZER(&carpstate, "carp state", setcarp_state,
92 1.5 dyoung "carp_state", carpstatekw, __arraycount(carpstatekw),
93 1.5 dyoung &command_root.pb_parser);
94 1.5 dyoung
95 1.5 dyoung struct pstr pass = PSTR_INITIALIZER(&pass, "pass", setcarp_passwd,
96 1.5 dyoung "pass", &command_root.pb_parser);
97 1.5 dyoung
98 1.5 dyoung struct pinteger parse_vhid = PINTEGER_INITIALIZER1(&vhid, "vhid",
99 1.5 dyoung 0, 255, 10, setcarp_vhid, "vhid", &command_root.pb_parser);
100 1.8 dyoung
101 1.8 dyoung static const struct kwinst carpkw[] = {
102 1.8 dyoung {.k_word = "advbase", .k_nextparser = &parse_advbase.pi_parser}
103 1.8 dyoung , {.k_word = "advskew", .k_nextparser = &parse_advskew.pi_parser}
104 1.8 dyoung , {.k_word = "carpdev", .k_nextparser = &carpdev.pif_parser}
105 1.8 dyoung , {.k_word = "-carpdev", .k_key = "carpdev", .k_type = KW_T_STR,
106 1.8 dyoung .k_str = "", .k_exec = setcarpdev,
107 1.8 dyoung .k_nextparser = &command_root.pb_parser}
108 1.8 dyoung , {.k_word = "pass", .k_nextparser = &pass.ps_parser}
109 1.8 dyoung , {.k_word = "state", .k_nextparser = &carpstate.pk_parser}
110 1.8 dyoung , {.k_word = "vhid", .k_nextparser = &parse_vhid.pi_parser}
111 1.8 dyoung };
112 1.8 dyoung
113 1.8 dyoung struct pkw carp = PKW_INITIALIZER(&carp, "CARP", NULL, NULL,
114 1.8 dyoung carpkw, __arraycount(carpkw), NULL);
115 1.8 dyoung
116 1.10 dyoung static void
117 1.10 dyoung carp_set(prop_dictionary_t env, struct carpreq *carpr)
118 1.10 dyoung {
119 1.10 dyoung if (indirect_ioctl(env, SIOCSVH, carpr) == -1)
120 1.10 dyoung err(EXIT_FAILURE, "SIOCSVH");
121 1.10 dyoung }
122 1.10 dyoung
123 1.10 dyoung static int
124 1.10 dyoung carp_get1(prop_dictionary_t env, struct carpreq *carpr)
125 1.10 dyoung {
126 1.10 dyoung memset(carpr, 0, sizeof(*carpr));
127 1.10 dyoung
128 1.10 dyoung return indirect_ioctl(env, SIOCGVH, carpr);
129 1.10 dyoung }
130 1.1 liamjfoy
131 1.10 dyoung static void
132 1.10 dyoung carp_get(prop_dictionary_t env, struct carpreq *carpr)
133 1.10 dyoung {
134 1.10 dyoung if (carp_get1(env, carpr) == -1)
135 1.10 dyoung err(EXIT_FAILURE, "SIOCGVH");
136 1.10 dyoung }
137 1.10 dyoung
138 1.10 dyoung static void
139 1.6 dyoung carp_status(prop_dictionary_t env, prop_dictionary_t oenv)
140 1.1 liamjfoy {
141 1.1 liamjfoy const char *state;
142 1.1 liamjfoy struct carpreq carpr;
143 1.1 liamjfoy
144 1.10 dyoung if (carp_get1(env, &carpr) == -1)
145 1.1 liamjfoy return;
146 1.1 liamjfoy
147 1.5 dyoung if (carpr.carpr_vhid <= 0)
148 1.5 dyoung return;
149 1.5 dyoung if (carpr.carpr_state > CARP_MAXSTATE)
150 1.5 dyoung state = "<UNKNOWN>";
151 1.5 dyoung else
152 1.5 dyoung state = carp_states[carpr.carpr_state];
153 1.5 dyoung
154 1.5 dyoung printf("\tcarp: %s carpdev %s vhid %d advbase %d advskew %d\n",
155 1.5 dyoung state, carpr.carpr_carpdev[0] != '\0' ?
156 1.5 dyoung carpr.carpr_carpdev : "none", carpr.carpr_vhid,
157 1.5 dyoung carpr.carpr_advbase, carpr.carpr_advskew);
158 1.1 liamjfoy }
159 1.1 liamjfoy
160 1.5 dyoung int
161 1.12 dyoung setcarp_passwd(prop_dictionary_t env, prop_dictionary_t oenv)
162 1.1 liamjfoy {
163 1.1 liamjfoy struct carpreq carpr;
164 1.5 dyoung prop_data_t data;
165 1.5 dyoung
166 1.5 dyoung data = (prop_data_t)prop_dictionary_get(env, "pass");
167 1.5 dyoung if (data == NULL) {
168 1.5 dyoung errno = ENOENT;
169 1.5 dyoung return -1;
170 1.5 dyoung }
171 1.1 liamjfoy
172 1.10 dyoung carp_get(env, &carpr);
173 1.1 liamjfoy
174 1.5 dyoung memset(carpr.carpr_key, 0, sizeof(carpr.carpr_key));
175 1.1 liamjfoy /* XXX Should hash the password into the key here, perhaps? */
176 1.14 thorpej strlcpy((char *)carpr.carpr_key, prop_data_value(data),
177 1.5 dyoung MIN(CARP_KEY_LEN, prop_data_size(data)));
178 1.1 liamjfoy
179 1.10 dyoung carp_set(env, &carpr);
180 1.5 dyoung return 0;
181 1.1 liamjfoy }
182 1.1 liamjfoy
183 1.5 dyoung int
184 1.12 dyoung setcarp_vhid(prop_dictionary_t env, prop_dictionary_t oenv)
185 1.1 liamjfoy {
186 1.1 liamjfoy struct carpreq carpr;
187 1.7 dyoung int64_t vhid;
188 1.1 liamjfoy
189 1.7 dyoung if (!prop_dictionary_get_int64(env, "vhid", &vhid)) {
190 1.5 dyoung errno = ENOENT;
191 1.5 dyoung return -1;
192 1.5 dyoung }
193 1.1 liamjfoy
194 1.10 dyoung carp_get(env, &carpr);
195 1.1 liamjfoy
196 1.1 liamjfoy carpr.carpr_vhid = vhid;
197 1.1 liamjfoy
198 1.10 dyoung carp_set(env, &carpr);
199 1.5 dyoung return 0;
200 1.1 liamjfoy }
201 1.1 liamjfoy
202 1.5 dyoung int
203 1.12 dyoung setcarp_advskew(prop_dictionary_t env, prop_dictionary_t oenv)
204 1.1 liamjfoy {
205 1.1 liamjfoy struct carpreq carpr;
206 1.7 dyoung int64_t advskew;
207 1.5 dyoung
208 1.7 dyoung if (!prop_dictionary_get_int64(env, "advskew", &advskew)) {
209 1.5 dyoung errno = ENOENT;
210 1.5 dyoung return -1;
211 1.5 dyoung }
212 1.1 liamjfoy
213 1.10 dyoung carp_get(env, &carpr);
214 1.1 liamjfoy
215 1.1 liamjfoy carpr.carpr_advskew = advskew;
216 1.1 liamjfoy
217 1.10 dyoung carp_set(env, &carpr);
218 1.5 dyoung return 0;
219 1.1 liamjfoy }
220 1.1 liamjfoy
221 1.1 liamjfoy /* ARGSUSED */
222 1.5 dyoung int
223 1.12 dyoung setcarp_advbase(prop_dictionary_t env, prop_dictionary_t oenv)
224 1.1 liamjfoy {
225 1.1 liamjfoy struct carpreq carpr;
226 1.7 dyoung int64_t advbase;
227 1.5 dyoung
228 1.7 dyoung if (!prop_dictionary_get_int64(env, "advbase", &advbase)) {
229 1.5 dyoung errno = ENOENT;
230 1.5 dyoung return -1;
231 1.5 dyoung }
232 1.1 liamjfoy
233 1.10 dyoung carp_get(env, &carpr);
234 1.1 liamjfoy
235 1.1 liamjfoy carpr.carpr_advbase = advbase;
236 1.1 liamjfoy
237 1.10 dyoung carp_set(env, &carpr);
238 1.5 dyoung return 0;
239 1.1 liamjfoy }
240 1.1 liamjfoy
241 1.1 liamjfoy /* ARGSUSED */
242 1.10 dyoung static int
243 1.12 dyoung setcarp_state(prop_dictionary_t env, prop_dictionary_t oenv)
244 1.1 liamjfoy {
245 1.1 liamjfoy struct carpreq carpr;
246 1.7 dyoung int64_t carp_state;
247 1.5 dyoung
248 1.7 dyoung if (!prop_dictionary_get_int64(env, "carp_state", &carp_state)) {
249 1.5 dyoung errno = ENOENT;
250 1.5 dyoung return -1;
251 1.5 dyoung }
252 1.1 liamjfoy
253 1.10 dyoung carp_get(env, &carpr);
254 1.1 liamjfoy
255 1.7 dyoung carpr.carpr_state = carp_state;
256 1.1 liamjfoy
257 1.10 dyoung carp_set(env, &carpr);
258 1.5 dyoung return 0;
259 1.1 liamjfoy }
260 1.1 liamjfoy
261 1.1 liamjfoy /* ARGSUSED */
262 1.5 dyoung int
263 1.12 dyoung setcarpdev(prop_dictionary_t env, prop_dictionary_t oenv)
264 1.1 liamjfoy {
265 1.1 liamjfoy struct carpreq carpr;
266 1.13 dyoung prop_string_t s;
267 1.5 dyoung
268 1.13 dyoung s = (prop_string_t)prop_dictionary_get(env, "carpdev");
269 1.13 dyoung if (s == NULL) {
270 1.5 dyoung errno = ENOENT;
271 1.5 dyoung return -1;
272 1.5 dyoung }
273 1.5 dyoung
274 1.10 dyoung carp_get(env, &carpr);
275 1.1 liamjfoy
276 1.14 thorpej strlcpy(carpr.carpr_carpdev, prop_string_value(s),
277 1.13 dyoung sizeof(carpr.carpr_carpdev));
278 1.1 liamjfoy
279 1.10 dyoung carp_set(env, &carpr);
280 1.5 dyoung return 0;
281 1.1 liamjfoy }
282 1.10 dyoung
283 1.10 dyoung static void
284 1.11 dyoung carp_usage(prop_dictionary_t env)
285 1.11 dyoung {
286 1.11 dyoung fprintf(stderr,
287 1.11 dyoung "\t[ advbase n ] [ advskew n ] [ carpdev iface ] "
288 1.11 dyoung "[ pass passphrase ] [ state state ] [ vhid n ]\n");
289 1.11 dyoung
290 1.11 dyoung }
291 1.11 dyoung
292 1.11 dyoung static void
293 1.10 dyoung carp_constructor(void)
294 1.10 dyoung {
295 1.10 dyoung cmdloop_branch_init(&branch, &carp.pk_parser);
296 1.10 dyoung register_cmdloop_branch(&branch);
297 1.10 dyoung status_func_init(&status, carp_status);
298 1.11 dyoung usage_func_init(&usage, carp_usage);
299 1.10 dyoung register_status(&status);
300 1.11 dyoung register_usage(&usage);
301 1.10 dyoung }
302