carp.c revision 228577
124424Swosch/*	$FreeBSD: head/sbin/ifconfig/carp.c 228577 2011-12-16 14:30:58Z glebius $ */
224424Swosch/*	from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */
324424Swosch
424424Swosch/*
524424Swosch * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
624424Swosch * Copyright (c) 2003 Ryan McBride. All rights reserved.
724424Swosch *
824424Swosch * Redistribution and use in source and binary forms, with or without
924424Swosch * modification, are permitted provided that the following conditions
1024424Swosch * are met:
1124424Swosch * 1. Redistributions of source code must retain the above copyright
1224424Swosch *    notice, this list of conditions and the following disclaimer.
1324424Swosch * 2. Redistributions in binary form must reproduce the above copyright
1424424Swosch *    notice, this list of conditions and the following disclaimer in the
1542704Swosch *    documentation and/or other materials provided with the distribution.
1642704Swosch *
1742704Swosch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1824424Swosch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1942704Swosch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2042704Swosch * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
2142704Swosch * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2242704Swosch * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2342704Swosch * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2442704Swosch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2542704Swosch * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2642704Swosch * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2742704Swosch * THE POSSIBILITY OF SUCH DAMAGE.
2842704Swosch */
2942704Swosch
3042704Swosch#include <sys/param.h>
3142704Swosch#include <sys/ioctl.h>
3242704Swosch#include <sys/socket.h>
3324424Swosch#include <sys/sockio.h>
3425031Swosch
3525031Swosch#include <stdlib.h>
3635961Swosch#include <unistd.h>
3724424Swosch
3824424Swosch#include <net/if.h>
3924424Swosch#include <net/if_var.h>
4025031Swosch#include <netinet/in.h>
4125031Swosch#include <netinet/in_var.h>
4242704Swosch#include <netinet/ip_carp.h>
4324424Swosch
4442704Swosch#include <ctype.h>
4524424Swosch#include <stdio.h>
4642704Swosch#include <string.h>
4724424Swosch#include <stdlib.h>
4824424Swosch#include <unistd.h>
4924424Swosch#include <err.h>
5042704Swosch#include <errno.h>
5125031Swosch
5225031Swosch#include "ifconfig.h"
5325031Swosch
5425031Swoschstatic const char *carp_states[] = { CARP_STATES };
5524424Swosch
5624424Swoschstatic void carp_status(int s);
5724424Swoschstatic void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
5824424Swoschstatic void setcarp_callback(int, void *);
5924424Swoschstatic void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
6024424Swoschstatic void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
6124424Swoschstatic void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
6225031Swosch
6324424Swoschstatic int carpr_vhid = -1;
6424424Swoschstatic int carpr_advskew = -1;
6525031Swoschstatic int carpr_advbase = -1;
6625031Swoschstatic int carpr_state = -1;
6745349Swoschstatic unsigned char const *carpr_key;
6825031Swosch
6945349Swoschstatic void
7045349Swoschcarp_status(int s)
7145349Swosch{
7245349Swosch	struct carpreq carpr[CARP_MAXVHID];
7345349Swosch	int i;
7445349Swosch
7545349Swosch	bzero(carpr, sizeof(struct carpreq) * CARP_MAXVHID);
7645349Swosch	carpr[0].carpr_count = CARP_MAXVHID;
7745349Swosch	ifr.ifr_data = (caddr_t)&carpr;
7845349Swosch
7945349Swosch	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
8045349Swosch		return;
8145349Swosch
8245349Swosch	for (i = 0; i < carpr[0].carpr_count; i++) {
8346318Swosch		printf("\tcarp: %s vhid %d advbase %d advskew %d",
8445349Swosch		    carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
8545349Swosch		    carpr[i].carpr_advbase, carpr[i].carpr_advskew);
8645349Swosch		if (printkeys && carpr[i].carpr_key[0] != '\0')
8746318Swosch			printf(" key \"%s\"\n", carpr[i].carpr_key);
8846318Swosch		else
8946318Swosch			printf("\n");
9046318Swosch	}
9146318Swosch}
9246318Swosch
9346318Swoschstatic void
9445349Swoschsetcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
9545349Swosch{
9645349Swosch
9746318Swosch	carpr_vhid = atoi(val);
9846318Swosch
9946318Swosch	if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID)
10046318Swosch		errx(1, "vhid must be greater than 0 and less than %u",
10146318Swosch		    CARP_MAXVHID);
10246318Swosch
10346318Swosch	switch (afp->af_af) {
10446318Swosch#ifdef INET
10546318Swosch	case AF_INET:
10646318Swosch	    {
10746318Swosch		struct in_aliasreq *ifra;
10846318Swosch
10946318Swosch		ifra = (struct in_aliasreq *)afp->af_addreq;
11046318Swosch		ifra->ifra_vhid = carpr_vhid;
11146318Swosch		break;
11246318Swosch	    }
11346318Swosch#endif
11446321Swosch#ifdef INET6
11546321Swosch	case AF_INET6:
11646321Swosch	    {
11746318Swosch		struct in6_aliasreq *ifra;
11846318Swosch
11946318Swosch		ifra = (struct in6_aliasreq *)afp->af_addreq;
12046318Swosch		ifra->ifra_vhid = carpr_vhid;
12146318Swosch		break;
12246318Swosch	    }
12346318Swosch#endif
12424424Swosch	default:
12524424Swosch		errx(1, "%s doesn't support carp(4)", afp->af_name);
12624424Swosch	}
12724424Swosch
12824424Swosch	callback_register(setcarp_callback, NULL);
12925031Swosch}
13025031Swosch
13124424Swoschstatic void
13225031Swoschsetcarp_callback(int s, void *arg __unused)
13325031Swosch{
13425031Swosch	struct carpreq carpr;
13525031Swosch
13625031Swosch	bzero(&carpr, sizeof(struct carpreq));
13725031Swosch	carpr.carpr_vhid = carpr_vhid;
13825031Swosch	carpr.carpr_count = 1;
13925031Swosch	ifr.ifr_data = (caddr_t)&carpr;
14025031Swosch
14125031Swosch	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1 && errno != ENOENT)
14225031Swosch		err(1, "SIOCGVH");
14325031Swosch
14425031Swosch	if (carpr_key != NULL)
14525031Swosch		/* XXX Should hash the password into the key here? */
14625031Swosch		strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
14738440Sjkh	if (carpr_advskew > -1)
14845349Swosch		carpr.carpr_advskew = carpr_advskew;
14945349Swosch	if (carpr_advbase > -1)
15042704Swosch		carpr.carpr_advbase = carpr_advbase;
15125031Swosch	if (carpr_state > -1)
15225031Swosch		carpr.carpr_state = carpr_state;
15324424Swosch
15425031Swosch	if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
15525031Swosch		err(1, "SIOCSVH");
15625031Swosch}
15725031Swosch
15825031Swoschstatic void
15925031Swoschsetcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
16025031Swosch{
16125031Swosch
16225031Swosch	if (carpr_vhid == -1)
16325031Swosch		errx(1, "passwd requires vhid");
16424424Swosch
16525031Swosch	carpr_key = val;
16625031Swosch}
16725031Swosch
16825031Swoschstatic void
16925031Swoschsetcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
17025031Swosch{
17125031Swosch
17242704Swosch	if (carpr_vhid == -1)
17342704Swosch		errx(1, "advskew requires vhid");
17442704Swosch
17542704Swosch	carpr_advskew = atoi(val);
17642704Swosch}
17742704Swosch
17825031Swoschstatic void
17925031Swoschsetcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
18024424Swosch{
18125031Swosch
18225031Swosch	if (carpr_vhid == -1)
18325031Swosch		errx(1, "advbase requires vhid");
18425031Swosch
18525031Swosch	carpr_advbase = atoi(val);
18625031Swosch}
18725031Swosch
18825031Swoschstatic void
18925031Swoschsetcarp_state(const char *val, int d, int s, const struct afswtch *afp)
19024424Swosch{
19125031Swosch	int i;
19225031Swosch
19325031Swosch	if (carpr_vhid == -1)
19425031Swosch		errx(1, "state requires vhid");
19525031Swosch
19625031Swosch	for (i = 0; i <= CARP_MAXSTATE; i++)
19725031Swosch		if (strcasecmp(carp_states[i], val) == 0) {
19825031Swosch			carpr_state = i;
19925031Swosch			return;
20025031Swosch		}
20125031Swosch
20225031Swosch	errx(1, "unknown state");
20325031Swosch}
20425031Swosch
20525031Swoschstatic struct cmd carp_cmds[] = {
20625031Swosch	DEF_CMD_ARG("advbase",	setcarp_advbase),
20725031Swosch	DEF_CMD_ARG("advskew",	setcarp_advskew),
20825031Swosch	DEF_CMD_ARG("pass",	setcarp_passwd),
20925031Swosch	DEF_CMD_ARG("vhid",	setcarp_vhid),
21024424Swosch	DEF_CMD_ARG("state",	setcarp_state),
21125031Swosch};
21225031Swoschstatic struct afswtch af_carp = {
21325031Swosch	.af_name	= "af_carp",
21425031Swosch	.af_af		= AF_UNSPEC,
21525031Swosch	.af_other_status = carp_status,
21625031Swosch};
21725031Swosch
21825031Swoschstatic __constructor void
21925031Swoschcarp_ctor(void)
22025031Swosch{
22125031Swosch#define	N(a)	(sizeof(a) / sizeof(a[0]))
22245349Swosch	int i;
22325031Swosch
22445349Swosch	for (i = 0; i < N(carp_cmds);  i++)
22538440Sjkh		cmd_register(&carp_cmds[i]);
22625031Swosch	af_register(&af_carp);
22738440Sjkh#undef N
22838440Sjkh}
22938440Sjkh