xref: /freebsd/sbin/ifconfig/carp.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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
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 setcarp_callback(if_ctx *, void *);
64 
65 static int carpr_vhid = -1;
66 static int carpr_advskew = -1;
67 static int carpr_advbase = -1;
68 static int carpr_state = -1;
69 static struct in_addr carp_addr;
70 static struct in6_addr carp_addr6;
71 static unsigned char const *carpr_key;
72 
73 static void
74 carp_status(if_ctx *ctx)
75 {
76 	struct ifconfig_carp carpr[CARP_MAXVHID];
77 	char addr_buf[NI_MAXHOST];
78 
79 	if (ifconfig_carp_get_info(lifh, ctx->ifname, carpr, CARP_MAXVHID) == -1)
80 		return;
81 
82 	for (size_t i = 0; i < carpr[0].carpr_count; i++) {
83 		printf("\tcarp: %s vhid %d advbase %d advskew %d",
84 		    carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
85 		    carpr[i].carpr_advbase, carpr[i].carpr_advskew);
86 		if (ctx->args->printkeys && carpr[i].carpr_key[0] != '\0')
87 			printf(" key \"%s\"\n", carpr[i].carpr_key);
88 		else
89 			printf("\n");
90 
91 		inet_ntop(AF_INET6, &carpr[i].carpr_addr6, addr_buf,
92 		    sizeof(addr_buf));
93 
94 		printf("\t      peer %s peer6 %s\n",
95 		    inet_ntoa(carpr[i].carpr_addr), addr_buf);
96 	}
97 }
98 
99 static void
100 setcarp_vhid(if_ctx *ctx, const char *val, int dummy __unused)
101 {
102 	const struct afswtch *afp = ctx->afp;
103 
104 	carpr_vhid = atoi(val);
105 
106 	if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID)
107 		errx(1, "vhid must be greater than 0 and less than %u",
108 		    CARP_MAXVHID);
109 
110 	if (afp->af_setvhid == NULL)
111 		errx(1, "%s doesn't support carp(4)", afp->af_name);
112 	afp->af_setvhid(carpr_vhid);
113 	callback_register(setcarp_callback, NULL);
114 }
115 
116 static void
117 setcarp_callback(if_ctx *ctx, void *arg __unused)
118 {
119 	struct ifconfig_carp carpr = { };
120 
121 	if (ifconfig_carp_get_vhid(lifh, ctx->ifname, &carpr, carpr_vhid) == -1) {
122 		if (ifconfig_err_errno(lifh) != ENOENT)
123 			return;
124 	}
125 
126 	carpr.carpr_vhid = carpr_vhid;
127 	if (carpr_key != NULL)
128 		/* XXX Should hash the password into the key here? */
129 		strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
130 	if (carpr_advskew > -1)
131 		carpr.carpr_advskew = carpr_advskew;
132 	if (carpr_advbase > -1)
133 		carpr.carpr_advbase = carpr_advbase;
134 	if (carpr_state > -1)
135 		carpr.carpr_state = carpr_state;
136 	if (carp_addr.s_addr != INADDR_ANY)
137 		carpr.carpr_addr = carp_addr;
138 	if (! IN6_IS_ADDR_UNSPECIFIED(&carp_addr6))
139 		memcpy(&carpr.carpr_addr6, &carp_addr6,
140 		    sizeof(carp_addr6));
141 
142 	if (ifconfig_carp_set_info(lifh, ctx->ifname, &carpr))
143 		err(1, "SIOCSVH");
144 }
145 
146 static void
147 setcarp_passwd(if_ctx *ctx __unused, const char *val, int dummy __unused)
148 {
149 
150 	if (carpr_vhid == -1)
151 		errx(1, "passwd requires vhid");
152 
153 	carpr_key = val;
154 }
155 
156 static void
157 setcarp_advskew(if_ctx *ctx __unused, const char *val, int dummy __unused)
158 {
159 
160 	if (carpr_vhid == -1)
161 		errx(1, "advskew requires vhid");
162 
163 	carpr_advskew = atoi(val);
164 }
165 
166 static void
167 setcarp_advbase(if_ctx *ctx __unused, const char *val, int dummy __unused)
168 {
169 
170 	if (carpr_vhid == -1)
171 		errx(1, "advbase requires vhid");
172 
173 	carpr_advbase = atoi(val);
174 }
175 
176 static void
177 setcarp_state(if_ctx *ctx __unused, const char *val, int dummy __unused)
178 {
179 	int i;
180 
181 	if (carpr_vhid == -1)
182 		errx(1, "state requires vhid");
183 
184 	for (i = 0; i <= CARP_MAXSTATE; i++)
185 		if (strcasecmp(carp_states[i], val) == 0) {
186 			carpr_state = i;
187 			return;
188 		}
189 
190 	errx(1, "unknown state");
191 }
192 
193 static void
194 setcarp_peer(if_ctx *ctx __unused, const char *val, int dummy __unused)
195 {
196 	carp_addr.s_addr = inet_addr(val);
197 }
198 
199 static void
200 setcarp_mcast(if_ctx *ctx __unused, const char *val __unused, int dummy __unused)
201 {
202 	carp_addr.s_addr = htonl(INADDR_CARP_GROUP);
203 }
204 
205 static void
206 setcarp_peer6(if_ctx *ctx __unused, const char *val, int dummy __unused)
207 {
208 	struct addrinfo hints, *res;
209 
210 	memset(&hints, 0, sizeof(hints));
211 	hints.ai_family = AF_INET6;
212 	hints.ai_flags = AI_NUMERICHOST;
213 
214 	if (getaddrinfo(val, NULL, &hints, &res) != 0)
215 		errx(1, "Invalid IPv6 address %s", val);
216 
217 	memcpy(&carp_addr6, &(satosin6(res->ai_addr))->sin6_addr, sizeof(carp_addr6));
218 	freeaddrinfo(res);
219 }
220 
221 static void
222 setcarp_mcast6(if_ctx *ctx __unused, const char *val __unused, int dummy __unused)
223 {
224 	bzero(&carp_addr6, sizeof(carp_addr6));
225 	carp_addr6.s6_addr[0] = 0xff;
226 	carp_addr6.s6_addr[1] = 0x02;
227 	carp_addr6.s6_addr[15] = 0x12;
228 }
229 
230 static struct cmd carp_cmds[] = {
231 	DEF_CMD_ARG("advbase",	setcarp_advbase),
232 	DEF_CMD_ARG("advskew",	setcarp_advskew),
233 	DEF_CMD_ARG("pass",	setcarp_passwd),
234 	DEF_CMD_ARG("vhid",	setcarp_vhid),
235 	DEF_CMD_ARG("state",	setcarp_state),
236 	DEF_CMD_ARG("peer",	setcarp_peer),
237 	DEF_CMD("mcast",	0,	setcarp_mcast),
238 	DEF_CMD_ARG("peer6",	setcarp_peer6),
239 	DEF_CMD("mcast6", 	0,	setcarp_mcast6),
240 };
241 static struct afswtch af_carp = {
242 	.af_name	= "af_carp",
243 	.af_af		= AF_UNSPEC,
244 	.af_other_status = carp_status,
245 };
246 
247 static __constructor void
248 carp_ctor(void)
249 {
250 	/* Default to multicast. */
251 	setcarp_mcast(NULL, NULL, 0);
252 	setcarp_mcast6(NULL, NULL, 0);
253 
254 	for (size_t i = 0; i < nitems(carp_cmds);  i++)
255 		cmd_register(&carp_cmds[i]);
256 	af_register(&af_carp);
257 }
258