1 /* $NetBSD: carp.c,v 1.13 2009/09/11 23:22:28 dyoung Exp $ */
4 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
5 * Copyright (c) 2003 Ryan McBride. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: carp.c,v 1.13 2009/09/11 23:22:28 dyoung Exp $");
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
40 #include <netinet/ip_carp.h>
41 #include <net/route.h>
55 static status_func_t status
;
56 static usage_func_t usage
;
57 static cmdloop_branch_t branch
;
59 static void carp_constructor(void) __attribute__((constructor
));
60 static void carp_status(prop_dictionary_t
, prop_dictionary_t
);
61 static int setcarp_advbase(prop_dictionary_t
, prop_dictionary_t
);
62 static int setcarp_advskew(prop_dictionary_t
, prop_dictionary_t
);
63 static int setcarp_passwd(prop_dictionary_t
, prop_dictionary_t
);
64 static int setcarp_vhid(prop_dictionary_t
, prop_dictionary_t
);
65 static int setcarp_state(prop_dictionary_t
, prop_dictionary_t
);
66 static int setcarpdev(prop_dictionary_t
, prop_dictionary_t
);
68 static const char *carp_states
[] = { CARP_STATES
};
70 struct kwinst carpstatekw
[] = {
71 {.k_word
= "INIT", .k_nextparser
= &command_root
.pb_parser
}
72 , {.k_word
= "BACKUP", .k_nextparser
= &command_root
.pb_parser
}
73 , {.k_word
= "MASTER", .k_nextparser
= &command_root
.pb_parser
}
76 struct pinteger parse_advbase
= PINTEGER_INITIALIZER1(&parse_advbase
, "advbase",
77 0, 255, 10, setcarp_advbase
, "advbase", &command_root
.pb_parser
);
79 struct pinteger parse_advskew
= PINTEGER_INITIALIZER1(&parse_advskew
, "advskew",
80 0, 254, 10, setcarp_advskew
, "advskew", &command_root
.pb_parser
);
82 struct piface carpdev
= PIFACE_INITIALIZER(&carpdev
, "carpdev", setcarpdev
,
83 "carpdev", &command_root
.pb_parser
);
85 struct pkw carpstate
= PKW_INITIALIZER(&carpstate
, "carp state", setcarp_state
,
86 "carp_state", carpstatekw
, __arraycount(carpstatekw
),
87 &command_root
.pb_parser
);
89 struct pstr pass
= PSTR_INITIALIZER(&pass
, "pass", setcarp_passwd
,
90 "pass", &command_root
.pb_parser
);
92 struct pinteger parse_vhid
= PINTEGER_INITIALIZER1(&vhid
, "vhid",
93 0, 255, 10, setcarp_vhid
, "vhid", &command_root
.pb_parser
);
95 static const struct kwinst carpkw
[] = {
96 {.k_word
= "advbase", .k_nextparser
= &parse_advbase
.pi_parser
}
97 , {.k_word
= "advskew", .k_nextparser
= &parse_advskew
.pi_parser
}
98 , {.k_word
= "carpdev", .k_nextparser
= &carpdev
.pif_parser
}
99 , {.k_word
= "-carpdev", .k_key
= "carpdev", .k_type
= KW_T_STR
,
100 .k_str
= "", .k_exec
= setcarpdev
,
101 .k_nextparser
= &command_root
.pb_parser
}
102 , {.k_word
= "pass", .k_nextparser
= &pass
.ps_parser
}
103 , {.k_word
= "state", .k_nextparser
= &carpstate
.pk_parser
}
104 , {.k_word
= "vhid", .k_nextparser
= &parse_vhid
.pi_parser
}
107 struct pkw carp
= PKW_INITIALIZER(&carp
, "CARP", NULL
, NULL
,
108 carpkw
, __arraycount(carpkw
), NULL
);
111 carp_set(prop_dictionary_t env
, struct carpreq
*carpr
)
113 if (indirect_ioctl(env
, SIOCSVH
, carpr
) == -1)
114 err(EXIT_FAILURE
, "SIOCSVH");
118 carp_get1(prop_dictionary_t env
, struct carpreq
*carpr
)
120 memset(carpr
, 0, sizeof(*carpr
));
122 return indirect_ioctl(env
, SIOCGVH
, carpr
);
126 carp_get(prop_dictionary_t env
, struct carpreq
*carpr
)
128 if (carp_get1(env
, carpr
) == -1)
129 err(EXIT_FAILURE
, "SIOCGVH");
133 carp_status(prop_dictionary_t env
, prop_dictionary_t oenv
)
136 struct carpreq carpr
;
138 if (carp_get1(env
, &carpr
) == -1)
141 if (carpr
.carpr_vhid
<= 0)
143 if (carpr
.carpr_state
> CARP_MAXSTATE
)
146 state
= carp_states
[carpr
.carpr_state
];
148 printf("\tcarp: %s carpdev %s vhid %d advbase %d advskew %d\n",
149 state
, carpr
.carpr_carpdev
[0] != '\0' ?
150 carpr
.carpr_carpdev
: "none", carpr
.carpr_vhid
,
151 carpr
.carpr_advbase
, carpr
.carpr_advskew
);
155 setcarp_passwd(prop_dictionary_t env
, prop_dictionary_t oenv
)
157 struct carpreq carpr
;
160 data
= (prop_data_t
)prop_dictionary_get(env
, "pass");
166 carp_get(env
, &carpr
);
168 memset(carpr
.carpr_key
, 0, sizeof(carpr
.carpr_key
));
169 /* XXX Should hash the password into the key here, perhaps? */
170 strlcpy((char *)carpr
.carpr_key
, prop_data_data_nocopy(data
),
171 MIN(CARP_KEY_LEN
, prop_data_size(data
)));
173 carp_set(env
, &carpr
);
178 setcarp_vhid(prop_dictionary_t env
, prop_dictionary_t oenv
)
180 struct carpreq carpr
;
183 if (!prop_dictionary_get_int64(env
, "vhid", &vhid
)) {
188 carp_get(env
, &carpr
);
190 carpr
.carpr_vhid
= vhid
;
192 carp_set(env
, &carpr
);
197 setcarp_advskew(prop_dictionary_t env
, prop_dictionary_t oenv
)
199 struct carpreq carpr
;
202 if (!prop_dictionary_get_int64(env
, "advskew", &advskew
)) {
207 carp_get(env
, &carpr
);
209 carpr
.carpr_advskew
= advskew
;
211 carp_set(env
, &carpr
);
217 setcarp_advbase(prop_dictionary_t env
, prop_dictionary_t oenv
)
219 struct carpreq carpr
;
222 if (!prop_dictionary_get_int64(env
, "advbase", &advbase
)) {
227 carp_get(env
, &carpr
);
229 carpr
.carpr_advbase
= advbase
;
231 carp_set(env
, &carpr
);
237 setcarp_state(prop_dictionary_t env
, prop_dictionary_t oenv
)
239 struct carpreq carpr
;
242 if (!prop_dictionary_get_int64(env
, "carp_state", &carp_state
)) {
247 carp_get(env
, &carpr
);
249 carpr
.carpr_state
= carp_state
;
251 carp_set(env
, &carpr
);
257 setcarpdev(prop_dictionary_t env
, prop_dictionary_t oenv
)
259 struct carpreq carpr
;
262 s
= (prop_string_t
)prop_dictionary_get(env
, "carpdev");
268 carp_get(env
, &carpr
);
270 strlcpy(carpr
.carpr_carpdev
, prop_string_cstring_nocopy(s
),
271 sizeof(carpr
.carpr_carpdev
));
273 carp_set(env
, &carpr
);
278 carp_usage(prop_dictionary_t env
)
281 "\t[ advbase n ] [ advskew n ] [ carpdev iface ] "
282 "[ pass passphrase ] [ state state ] [ vhid n ]\n");
287 carp_constructor(void)
289 cmdloop_branch_init(&branch
, &carp
.pk_parser
);
290 register_cmdloop_branch(&branch
);
291 status_func_init(&status
, carp_status
);
292 usage_func_init(&usage
, carp_usage
);
293 register_status(&status
);
294 register_usage(&usage
);