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