xref: /freebsd/sbin/ifconfig/carp.c (revision d411c1d6)
1 /*	$FreeBSD$ */
2 /*	from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6  *
7  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
8  * Copyright (c) 2003 Ryan McBride. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36 
37 #include <stdlib.h>
38 #include <unistd.h>
39 
40 #include <net/if.h>
41 #include <netinet/in.h>
42 #include <netinet/in_var.h>
43 #include <netinet/ip_carp.h>
44 
45 #include <arpa/inet.h>
46 
47 #include <ctype.h>
48 #include <stdbool.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <netdb.h>
56 
57 #include <libifconfig.h>
58 
59 #include "ifconfig.h"
60 
61 static const char *carp_states[] = { CARP_STATES };
62 
63 static void carp_status(int s);
64 static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
65 static void setcarp_callback(int, void *);
66 static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
67 static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
68 static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
69 
70 static int carpr_vhid = -1;
71 static int carpr_advskew = -1;
72 static int carpr_advbase = -1;
73 static int carpr_state = -1;
74 static struct in_addr carp_addr;
75 static struct in6_addr carp_addr6;
76 static unsigned char const *carpr_key;
77 
78 static void
79 carp_status(int s)
80 {
81 	struct ifconfig_carp carpr[CARP_MAXVHID];
82 	char addr_buf[NI_MAXHOST];
83 
84 	if (ifconfig_carp_get_info(lifh, name, carpr, CARP_MAXVHID) == -1)
85 		return;
86 
87 	for (size_t i = 0; i < carpr[0].carpr_count; i++) {
88 		printf("\tcarp: %s vhid %d advbase %d advskew %d",
89 		    carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
90 		    carpr[i].carpr_advbase, carpr[i].carpr_advskew);
91 		if (printkeys && carpr[i].carpr_key[0] != '\0')
92 			printf(" key \"%s\"\n", carpr[i].carpr_key);
93 		else
94 			printf("\n");
95 
96 		inet_ntop(AF_INET6, &carpr[i].carpr_addr6, addr_buf,
97 		    sizeof(addr_buf));
98 
99 		printf("\t      peer %s peer6 %s\n",
100 		    inet_ntoa(carpr[i].carpr_addr), addr_buf);
101 	}
102 }
103 
104 static void
105 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
106 {
107 
108 	carpr_vhid = atoi(val);
109 
110 	if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID)
111 		errx(1, "vhid must be greater than 0 and less than %u",
112 		    CARP_MAXVHID);
113 
114 	switch (afp->af_af) {
115 #ifdef INET
116 	case AF_INET:
117 	    {
118 		struct in_aliasreq *ifra;
119 
120 		ifra = (struct in_aliasreq *)afp->af_addreq;
121 		ifra->ifra_vhid = carpr_vhid;
122 		break;
123 	    }
124 #endif
125 #ifdef INET6
126 	case AF_INET6:
127 	    {
128 		struct in6_aliasreq *ifra;
129 
130 		ifra = (struct in6_aliasreq *)afp->af_addreq;
131 		ifra->ifra_vhid = carpr_vhid;
132 		break;
133 	    }
134 #endif
135 	default:
136 		errx(1, "%s doesn't support carp(4)", afp->af_name);
137 	}
138 
139 	callback_register(setcarp_callback, NULL);
140 }
141 
142 static void
143 setcarp_callback(int s, void *arg __unused)
144 {
145 	struct ifconfig_carp carpr = { };
146 
147 	if (ifconfig_carp_get_vhid(lifh, name, &carpr, carpr_vhid) == -1) {
148 		if (ifconfig_err_errno(lifh) != ENOENT)
149 			return;
150 	}
151 
152 	carpr.carpr_vhid = carpr_vhid;
153 	if (carpr_key != NULL)
154 		/* XXX Should hash the password into the key here? */
155 		strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
156 	if (carpr_advskew > -1)
157 		carpr.carpr_advskew = carpr_advskew;
158 	if (carpr_advbase > -1)
159 		carpr.carpr_advbase = carpr_advbase;
160 	if (carpr_state > -1)
161 		carpr.carpr_state = carpr_state;
162 	if (carp_addr.s_addr != INADDR_ANY)
163 		carpr.carpr_addr = carp_addr;
164 	if (! IN6_IS_ADDR_UNSPECIFIED(&carp_addr6))
165 		memcpy(&carpr.carpr_addr6, &carp_addr6,
166 		    sizeof(carp_addr6));
167 
168 	if (ifconfig_carp_set_info(lifh, name, &carpr))
169 		err(1, "SIOCSVH");
170 }
171 
172 static void
173 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
174 {
175 
176 	if (carpr_vhid == -1)
177 		errx(1, "passwd requires vhid");
178 
179 	carpr_key = val;
180 }
181 
182 static void
183 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
184 {
185 
186 	if (carpr_vhid == -1)
187 		errx(1, "advskew requires vhid");
188 
189 	carpr_advskew = atoi(val);
190 }
191 
192 static void
193 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
194 {
195 
196 	if (carpr_vhid == -1)
197 		errx(1, "advbase requires vhid");
198 
199 	carpr_advbase = atoi(val);
200 }
201 
202 static void
203 setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
204 {
205 	int i;
206 
207 	if (carpr_vhid == -1)
208 		errx(1, "state requires vhid");
209 
210 	for (i = 0; i <= CARP_MAXSTATE; i++)
211 		if (strcasecmp(carp_states[i], val) == 0) {
212 			carpr_state = i;
213 			return;
214 		}
215 
216 	errx(1, "unknown state");
217 }
218 
219 static void
220 setcarp_peer(const char *val, int d, int s, const struct afswtch *afp)
221 {
222 	carp_addr.s_addr = inet_addr(val);
223 }
224 
225 static void
226 setcarp_mcast(const char *val, int d, int s, const struct afswtch *afp)
227 {
228 	carp_addr.s_addr = htonl(INADDR_CARP_GROUP);
229 }
230 
231 static void
232 setcarp_peer6(const char *val, int d, int s, const struct afswtch *afp)
233 {
234 	struct addrinfo hints, *res;
235 
236 	memset(&hints, 0, sizeof(hints));
237 	hints.ai_family = AF_INET6;
238 	hints.ai_flags = AI_NUMERICHOST;
239 
240 	if (getaddrinfo(val, NULL, &hints, &res) == 1)
241 		errx(1, "Invalid IPv6 address %s", val);
242 
243 	memcpy(&carp_addr6, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
244 	    sizeof(carp_addr6));
245 	freeaddrinfo(res);
246 }
247 
248 static void
249 setcarp_mcast6(const char *val, int d, int s, const struct afswtch *afp)
250 {
251 	bzero(&carp_addr6, sizeof(carp_addr6));
252 	carp_addr6.s6_addr[0] = 0xff;
253 	carp_addr6.s6_addr[1] = 0x02;
254 	carp_addr6.s6_addr[15] = 0x12;
255 }
256 
257 static struct cmd carp_cmds[] = {
258 	DEF_CMD_ARG("advbase",	setcarp_advbase),
259 	DEF_CMD_ARG("advskew",	setcarp_advskew),
260 	DEF_CMD_ARG("pass",	setcarp_passwd),
261 	DEF_CMD_ARG("vhid",	setcarp_vhid),
262 	DEF_CMD_ARG("state",	setcarp_state),
263 	DEF_CMD_ARG("peer",	setcarp_peer),
264 	DEF_CMD("mcast",	0,	setcarp_mcast),
265 	DEF_CMD_ARG("peer6",	setcarp_peer6),
266 	DEF_CMD("mcast6", 	0,	setcarp_mcast6),
267 };
268 static struct afswtch af_carp = {
269 	.af_name	= "af_carp",
270 	.af_af		= AF_UNSPEC,
271 	.af_other_status = carp_status,
272 };
273 
274 static __constructor void
275 carp_ctor(void)
276 {
277 	int i;
278 
279 	/* Default to multicast. */
280 	setcarp_mcast(NULL, 0, 0, NULL);
281 	setcarp_mcast6(NULL, 0, 0, NULL);
282 
283 	for (i = 0; i < nitems(carp_cmds);  i++)
284 		cmd_register(&carp_cmds[i]);
285 	af_register(&af_carp);
286 }
287