Home | History | Annotate | Line # | Download | only in ifconfig
carp.c revision 1.12.10.1
      1  1.12.10.1       riz /* $NetBSD: carp.c,v 1.12.10.1 2011/10/16 23:48:27 riz 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.12.10.1       riz __RCSID("$NetBSD: carp.c,v 1.12.10.1 2011/10/16 23:48:27 riz 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.5    dyoung struct kwinst carpstatekw[] = {
     71        1.5    dyoung 	  {.k_word = "INIT", .k_nextparser = &command_root.pb_parser}
     72        1.5    dyoung 	, {.k_word = "BACKUP", .k_nextparser = &command_root.pb_parser}
     73        1.5    dyoung 	, {.k_word = "MASTER", .k_nextparser = &command_root.pb_parser}
     74        1.5    dyoung };
     75        1.5    dyoung 
     76        1.5    dyoung struct pinteger parse_advbase = PINTEGER_INITIALIZER1(&parse_advbase, "advbase",
     77        1.5    dyoung     0, 255, 10, setcarp_advbase, "advbase", &command_root.pb_parser);
     78        1.5    dyoung 
     79        1.5    dyoung struct pinteger parse_advskew = PINTEGER_INITIALIZER1(&parse_advskew, "advskew",
     80        1.5    dyoung     0, 254, 10, setcarp_advskew, "advskew", &command_root.pb_parser);
     81        1.5    dyoung 
     82        1.5    dyoung struct piface carpdev = PIFACE_INITIALIZER(&carpdev, "carpdev", setcarpdev,
     83        1.5    dyoung     "carpdev", &command_root.pb_parser);
     84        1.5    dyoung 
     85       1.10    dyoung struct pkw carpstate = PKW_INITIALIZER(&carpstate, "carp state", setcarp_state,
     86        1.5    dyoung     "carp_state", carpstatekw, __arraycount(carpstatekw),
     87        1.5    dyoung     &command_root.pb_parser);
     88        1.5    dyoung 
     89        1.5    dyoung struct pstr pass = PSTR_INITIALIZER(&pass, "pass", setcarp_passwd,
     90        1.5    dyoung     "pass", &command_root.pb_parser);
     91        1.5    dyoung 
     92        1.5    dyoung struct pinteger parse_vhid = PINTEGER_INITIALIZER1(&vhid, "vhid",
     93        1.5    dyoung     0, 255, 10, setcarp_vhid, "vhid", &command_root.pb_parser);
     94        1.8    dyoung 
     95        1.8    dyoung static const struct kwinst carpkw[] = {
     96        1.8    dyoung 	  {.k_word = "advbase", .k_nextparser = &parse_advbase.pi_parser}
     97        1.8    dyoung 	, {.k_word = "advskew", .k_nextparser = &parse_advskew.pi_parser}
     98        1.8    dyoung 	, {.k_word = "carpdev", .k_nextparser = &carpdev.pif_parser}
     99        1.8    dyoung 	, {.k_word = "-carpdev", .k_key = "carpdev", .k_type = KW_T_STR,
    100        1.8    dyoung 	   .k_str = "", .k_exec = setcarpdev,
    101        1.8    dyoung 	   .k_nextparser = &command_root.pb_parser}
    102        1.8    dyoung 	, {.k_word = "pass", .k_nextparser = &pass.ps_parser}
    103        1.8    dyoung 	, {.k_word = "state", .k_nextparser = &carpstate.pk_parser}
    104        1.8    dyoung 	, {.k_word = "vhid", .k_nextparser = &parse_vhid.pi_parser}
    105        1.8    dyoung };
    106        1.8    dyoung 
    107        1.8    dyoung struct pkw carp = PKW_INITIALIZER(&carp, "CARP", NULL, NULL,
    108        1.8    dyoung     carpkw, __arraycount(carpkw), NULL);
    109        1.8    dyoung 
    110       1.10    dyoung static void
    111       1.10    dyoung carp_set(prop_dictionary_t env, struct carpreq *carpr)
    112       1.10    dyoung {
    113       1.10    dyoung 	if (indirect_ioctl(env, SIOCSVH, carpr) == -1)
    114       1.10    dyoung 		err(EXIT_FAILURE, "SIOCSVH");
    115       1.10    dyoung }
    116       1.10    dyoung 
    117       1.10    dyoung static int
    118       1.10    dyoung carp_get1(prop_dictionary_t env, struct carpreq *carpr)
    119       1.10    dyoung {
    120       1.10    dyoung 	memset(carpr, 0, sizeof(*carpr));
    121       1.10    dyoung 
    122       1.10    dyoung 	return indirect_ioctl(env, SIOCGVH, carpr);
    123       1.10    dyoung }
    124        1.1  liamjfoy 
    125       1.10    dyoung static void
    126       1.10    dyoung carp_get(prop_dictionary_t env, struct carpreq *carpr)
    127       1.10    dyoung {
    128       1.10    dyoung 	if (carp_get1(env, carpr) == -1)
    129       1.10    dyoung 		err(EXIT_FAILURE, "SIOCGVH");
    130       1.10    dyoung }
    131       1.10    dyoung 
    132       1.10    dyoung static void
    133        1.6    dyoung carp_status(prop_dictionary_t env, prop_dictionary_t oenv)
    134        1.1  liamjfoy {
    135        1.1  liamjfoy 	const char *state;
    136        1.1  liamjfoy 	struct carpreq carpr;
    137        1.1  liamjfoy 
    138       1.10    dyoung 	if (carp_get1(env, &carpr) == -1)
    139        1.1  liamjfoy 		return;
    140        1.1  liamjfoy 
    141        1.5    dyoung 	if (carpr.carpr_vhid <= 0)
    142        1.5    dyoung 		return;
    143        1.5    dyoung 	if (carpr.carpr_state > CARP_MAXSTATE)
    144        1.5    dyoung 		state = "<UNKNOWN>";
    145        1.5    dyoung 	else
    146        1.5    dyoung 		state = carp_states[carpr.carpr_state];
    147        1.5    dyoung 
    148        1.5    dyoung 	printf("\tcarp: %s carpdev %s vhid %d advbase %d advskew %d\n",
    149        1.5    dyoung 	    state, carpr.carpr_carpdev[0] != '\0' ?
    150        1.5    dyoung 	    carpr.carpr_carpdev : "none", carpr.carpr_vhid,
    151        1.5    dyoung 	    carpr.carpr_advbase, carpr.carpr_advskew);
    152        1.1  liamjfoy }
    153        1.1  liamjfoy 
    154        1.5    dyoung int
    155       1.12    dyoung setcarp_passwd(prop_dictionary_t env, prop_dictionary_t oenv)
    156        1.1  liamjfoy {
    157        1.1  liamjfoy 	struct carpreq carpr;
    158        1.5    dyoung 	prop_data_t data;
    159        1.5    dyoung 
    160        1.5    dyoung 	data = (prop_data_t)prop_dictionary_get(env, "pass");
    161        1.5    dyoung 	if (data == NULL) {
    162        1.5    dyoung 		errno = ENOENT;
    163        1.5    dyoung 		return -1;
    164        1.5    dyoung 	}
    165        1.1  liamjfoy 
    166       1.10    dyoung 	carp_get(env, &carpr);
    167        1.1  liamjfoy 
    168        1.5    dyoung 	memset(carpr.carpr_key, 0, sizeof(carpr.carpr_key));
    169        1.1  liamjfoy 	/* XXX Should hash the password into the key here, perhaps? */
    170        1.5    dyoung 	strlcpy((char *)carpr.carpr_key, prop_data_data_nocopy(data),
    171        1.5    dyoung 	    MIN(CARP_KEY_LEN, prop_data_size(data)));
    172        1.1  liamjfoy 
    173       1.10    dyoung 	carp_set(env, &carpr);
    174        1.5    dyoung 	return 0;
    175        1.1  liamjfoy }
    176        1.1  liamjfoy 
    177        1.5    dyoung int
    178       1.12    dyoung setcarp_vhid(prop_dictionary_t env, prop_dictionary_t oenv)
    179        1.1  liamjfoy {
    180        1.1  liamjfoy 	struct carpreq carpr;
    181        1.7    dyoung 	int64_t vhid;
    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.10    dyoung 	carp_get(env, &carpr);
    189        1.1  liamjfoy 
    190        1.1  liamjfoy 	carpr.carpr_vhid = vhid;
    191        1.1  liamjfoy 
    192       1.10    dyoung 	carp_set(env, &carpr);
    193        1.5    dyoung 	return 0;
    194        1.1  liamjfoy }
    195        1.1  liamjfoy 
    196        1.5    dyoung int
    197       1.12    dyoung setcarp_advskew(prop_dictionary_t env, prop_dictionary_t oenv)
    198        1.1  liamjfoy {
    199        1.1  liamjfoy 	struct carpreq carpr;
    200        1.7    dyoung 	int64_t advskew;
    201        1.5    dyoung 
    202        1.7    dyoung 	if (!prop_dictionary_get_int64(env, "advskew", &advskew)) {
    203        1.5    dyoung 		errno = ENOENT;
    204        1.5    dyoung 		return -1;
    205        1.5    dyoung 	}
    206        1.1  liamjfoy 
    207       1.10    dyoung 	carp_get(env, &carpr);
    208        1.1  liamjfoy 
    209        1.1  liamjfoy 	carpr.carpr_advskew = advskew;
    210        1.1  liamjfoy 
    211       1.10    dyoung 	carp_set(env, &carpr);
    212        1.5    dyoung 	return 0;
    213        1.1  liamjfoy }
    214        1.1  liamjfoy 
    215        1.1  liamjfoy /* ARGSUSED */
    216        1.5    dyoung int
    217       1.12    dyoung setcarp_advbase(prop_dictionary_t env, prop_dictionary_t oenv)
    218        1.1  liamjfoy {
    219        1.1  liamjfoy 	struct carpreq carpr;
    220        1.7    dyoung 	int64_t advbase;
    221        1.5    dyoung 
    222        1.7    dyoung 	if (!prop_dictionary_get_int64(env, "advbase", &advbase)) {
    223        1.5    dyoung 		errno = ENOENT;
    224        1.5    dyoung 		return -1;
    225        1.5    dyoung 	}
    226        1.1  liamjfoy 
    227       1.10    dyoung 	carp_get(env, &carpr);
    228        1.1  liamjfoy 
    229        1.1  liamjfoy 	carpr.carpr_advbase = advbase;
    230        1.1  liamjfoy 
    231       1.10    dyoung 	carp_set(env, &carpr);
    232        1.5    dyoung 	return 0;
    233        1.1  liamjfoy }
    234        1.1  liamjfoy 
    235        1.1  liamjfoy /* ARGSUSED */
    236       1.10    dyoung static int
    237       1.12    dyoung setcarp_state(prop_dictionary_t env, prop_dictionary_t oenv)
    238        1.1  liamjfoy {
    239        1.1  liamjfoy 	struct carpreq carpr;
    240        1.7    dyoung 	int64_t carp_state;
    241        1.5    dyoung 
    242        1.7    dyoung 	if (!prop_dictionary_get_int64(env, "carp_state", &carp_state)) {
    243        1.5    dyoung 		errno = ENOENT;
    244        1.5    dyoung 		return -1;
    245        1.5    dyoung 	}
    246        1.1  liamjfoy 
    247       1.10    dyoung 	carp_get(env, &carpr);
    248        1.1  liamjfoy 
    249        1.7    dyoung 	carpr.carpr_state = carp_state;
    250        1.1  liamjfoy 
    251       1.10    dyoung 	carp_set(env, &carpr);
    252        1.5    dyoung 	return 0;
    253        1.1  liamjfoy }
    254        1.1  liamjfoy 
    255        1.1  liamjfoy /* ARGSUSED */
    256        1.5    dyoung int
    257       1.12    dyoung setcarpdev(prop_dictionary_t env, prop_dictionary_t oenv)
    258        1.1  liamjfoy {
    259        1.1  liamjfoy 	struct carpreq carpr;
    260  1.12.10.1       riz 	prop_string_t s;
    261        1.5    dyoung 
    262  1.12.10.1       riz 	s = (prop_string_t)prop_dictionary_get(env, "carpdev");
    263  1.12.10.1       riz 	if (s == NULL) {
    264        1.5    dyoung 		errno = ENOENT;
    265        1.5    dyoung 		return -1;
    266        1.5    dyoung 	}
    267        1.5    dyoung 
    268       1.10    dyoung 	carp_get(env, &carpr);
    269        1.1  liamjfoy 
    270  1.12.10.1       riz 	strlcpy(carpr.carpr_carpdev, prop_string_cstring_nocopy(s),
    271  1.12.10.1       riz 	    sizeof(carpr.carpr_carpdev));
    272        1.1  liamjfoy 
    273       1.10    dyoung 	carp_set(env, &carpr);
    274        1.5    dyoung 	return 0;
    275        1.1  liamjfoy }
    276       1.10    dyoung 
    277       1.10    dyoung static void
    278       1.11    dyoung carp_usage(prop_dictionary_t env)
    279       1.11    dyoung {
    280       1.11    dyoung 	fprintf(stderr,
    281       1.11    dyoung 	    "\t[ advbase n ] [ advskew n ] [ carpdev iface ] "
    282       1.11    dyoung 	    "[ pass passphrase ] [ state state ] [ vhid n ]\n");
    283       1.11    dyoung 
    284       1.11    dyoung }
    285       1.11    dyoung 
    286       1.11    dyoung static void
    287       1.10    dyoung carp_constructor(void)
    288       1.10    dyoung {
    289       1.10    dyoung 	cmdloop_branch_init(&branch, &carp.pk_parser);
    290       1.10    dyoung 	register_cmdloop_branch(&branch);
    291       1.10    dyoung 	status_func_init(&status, carp_status);
    292       1.11    dyoung 	usage_func_init(&usage, carp_usage);
    293       1.10    dyoung 	register_status(&status);
    294       1.11    dyoung 	register_usage(&usage);
    295       1.10    dyoung }
    296