xref: /freebsd/sbin/ifconfig/carp.c (revision a0ee8cc6)
1 /*	$FreeBSD$ */
2 /*	from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */
3 
4 /*
5  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
6  * Copyright (c) 2003 Ryan McBride. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <netinet/in_var.h>
41 #include <netinet/ip_carp.h>
42 
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <err.h>
49 #include <errno.h>
50 
51 #include "ifconfig.h"
52 
53 static const char *carp_states[] = { CARP_STATES };
54 
55 static void carp_status(int s);
56 static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
57 static void setcarp_callback(int, void *);
58 static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
59 static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
60 static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
61 
62 static int carpr_vhid = -1;
63 static int carpr_advskew = -1;
64 static int carpr_advbase = -1;
65 static int carpr_state = -1;
66 static unsigned char const *carpr_key;
67 
68 static void
69 carp_status(int s)
70 {
71 	struct carpreq carpr[CARP_MAXVHID];
72 	int i;
73 
74 	bzero(carpr, sizeof(struct carpreq) * CARP_MAXVHID);
75 	carpr[0].carpr_count = CARP_MAXVHID;
76 	ifr.ifr_data = (caddr_t)&carpr;
77 
78 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
79 		return;
80 
81 	for (i = 0; i < carpr[0].carpr_count; i++) {
82 		printf("\tcarp: %s vhid %d advbase %d advskew %d",
83 		    carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
84 		    carpr[i].carpr_advbase, carpr[i].carpr_advskew);
85 		if (printkeys && carpr[i].carpr_key[0] != '\0')
86 			printf(" key \"%s\"\n", carpr[i].carpr_key);
87 		else
88 			printf("\n");
89 	}
90 }
91 
92 static void
93 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
94 {
95 
96 	carpr_vhid = atoi(val);
97 
98 	if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID)
99 		errx(1, "vhid must be greater than 0 and less than %u",
100 		    CARP_MAXVHID);
101 
102 	switch (afp->af_af) {
103 #ifdef INET
104 	case AF_INET:
105 	    {
106 		struct in_aliasreq *ifra;
107 
108 		ifra = (struct in_aliasreq *)afp->af_addreq;
109 		ifra->ifra_vhid = carpr_vhid;
110 		break;
111 	    }
112 #endif
113 #ifdef INET6
114 	case AF_INET6:
115 	    {
116 		struct in6_aliasreq *ifra;
117 
118 		ifra = (struct in6_aliasreq *)afp->af_addreq;
119 		ifra->ifra_vhid = carpr_vhid;
120 		break;
121 	    }
122 #endif
123 	default:
124 		errx(1, "%s doesn't support carp(4)", afp->af_name);
125 	}
126 
127 	callback_register(setcarp_callback, NULL);
128 }
129 
130 static void
131 setcarp_callback(int s, void *arg __unused)
132 {
133 	struct carpreq carpr;
134 
135 	bzero(&carpr, sizeof(struct carpreq));
136 	carpr.carpr_vhid = carpr_vhid;
137 	carpr.carpr_count = 1;
138 	ifr.ifr_data = (caddr_t)&carpr;
139 
140 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1 && errno != ENOENT)
141 		err(1, "SIOCGVH");
142 
143 	if (carpr_key != NULL)
144 		/* XXX Should hash the password into the key here? */
145 		strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
146 	if (carpr_advskew > -1)
147 		carpr.carpr_advskew = carpr_advskew;
148 	if (carpr_advbase > -1)
149 		carpr.carpr_advbase = carpr_advbase;
150 	if (carpr_state > -1)
151 		carpr.carpr_state = carpr_state;
152 
153 	if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
154 		err(1, "SIOCSVH");
155 }
156 
157 static void
158 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
159 {
160 
161 	if (carpr_vhid == -1)
162 		errx(1, "passwd requires vhid");
163 
164 	carpr_key = val;
165 }
166 
167 static void
168 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
169 {
170 
171 	if (carpr_vhid == -1)
172 		errx(1, "advskew requires vhid");
173 
174 	carpr_advskew = atoi(val);
175 }
176 
177 static void
178 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
179 {
180 
181 	if (carpr_vhid == -1)
182 		errx(1, "advbase requires vhid");
183 
184 	carpr_advbase = atoi(val);
185 }
186 
187 static void
188 setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
189 {
190 	int i;
191 
192 	if (carpr_vhid == -1)
193 		errx(1, "state requires vhid");
194 
195 	for (i = 0; i <= CARP_MAXSTATE; i++)
196 		if (strcasecmp(carp_states[i], val) == 0) {
197 			carpr_state = i;
198 			return;
199 		}
200 
201 	errx(1, "unknown state");
202 }
203 
204 static struct cmd carp_cmds[] = {
205 	DEF_CMD_ARG("advbase",	setcarp_advbase),
206 	DEF_CMD_ARG("advskew",	setcarp_advskew),
207 	DEF_CMD_ARG("pass",	setcarp_passwd),
208 	DEF_CMD_ARG("vhid",	setcarp_vhid),
209 	DEF_CMD_ARG("state",	setcarp_state),
210 };
211 static struct afswtch af_carp = {
212 	.af_name	= "af_carp",
213 	.af_af		= AF_UNSPEC,
214 	.af_other_status = carp_status,
215 };
216 
217 static __constructor void
218 carp_ctor(void)
219 {
220 	int i;
221 
222 	for (i = 0; i < nitems(carp_cmds);  i++)
223 		cmd_register(&carp_cmds[i]);
224 	af_register(&af_carp);
225 }
226