xref: /freebsd/sbin/ifconfig/carp.c (revision caed7b1c399de04279822028e15b36367e84232f)
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 <ctype.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <err.h>
51 #include <errno.h>
52 
53 #include <libifconfig.h>
54 
55 #include "ifconfig.h"
56 
57 static const char *carp_states[] = { CARP_STATES };
58 
59 static void carp_status(int s);
60 static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
61 static void setcarp_callback(int, void *);
62 static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
63 static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
64 static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
65 
66 static int carpr_vhid = -1;
67 static int carpr_advskew = -1;
68 static int carpr_advbase = -1;
69 static int carpr_state = -1;
70 static unsigned char const *carpr_key;
71 
72 static void
73 carp_status(int s)
74 {
75 	struct carpreq carpr[CARP_MAXVHID];
76 	ifconfig_handle_t *lifh;
77 
78 	lifh = ifconfig_open();
79 	if (lifh == NULL)
80 		return;
81 
82 	if (ifconfig_carp_get_info(lifh, name, carpr, CARP_MAXVHID) == -1)
83 		goto close;
84 
85 	for (size_t i = 0; i < carpr[0].carpr_count; i++) {
86 		printf("\tcarp: %s vhid %d advbase %d advskew %d",
87 		    carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
88 		    carpr[i].carpr_advbase, carpr[i].carpr_advskew);
89 		if (printkeys && carpr[i].carpr_key[0] != '\0')
90 			printf(" key \"%s\"\n", carpr[i].carpr_key);
91 		else
92 			printf("\n");
93 	}
94 close:
95 	ifconfig_close(lifh);
96 }
97 
98 static void
99 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
100 {
101 
102 	carpr_vhid = atoi(val);
103 
104 	if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID)
105 		errx(1, "vhid must be greater than 0 and less than %u",
106 		    CARP_MAXVHID);
107 
108 	switch (afp->af_af) {
109 #ifdef INET
110 	case AF_INET:
111 	    {
112 		struct in_aliasreq *ifra;
113 
114 		ifra = (struct in_aliasreq *)afp->af_addreq;
115 		ifra->ifra_vhid = carpr_vhid;
116 		break;
117 	    }
118 #endif
119 #ifdef INET6
120 	case AF_INET6:
121 	    {
122 		struct in6_aliasreq *ifra;
123 
124 		ifra = (struct in6_aliasreq *)afp->af_addreq;
125 		ifra->ifra_vhid = carpr_vhid;
126 		break;
127 	    }
128 #endif
129 	default:
130 		errx(1, "%s doesn't support carp(4)", afp->af_name);
131 	}
132 
133 	callback_register(setcarp_callback, NULL);
134 }
135 
136 static void
137 setcarp_callback(int s, void *arg __unused)
138 {
139 	struct carpreq carpr;
140 
141 	bzero(&carpr, sizeof(struct carpreq));
142 	carpr.carpr_vhid = carpr_vhid;
143 	carpr.carpr_count = 1;
144 	ifr.ifr_data = (caddr_t)&carpr;
145 
146 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1 && errno != ENOENT)
147 		err(1, "SIOCGVH");
148 
149 	if (carpr_key != NULL)
150 		/* XXX Should hash the password into the key here? */
151 		strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
152 	if (carpr_advskew > -1)
153 		carpr.carpr_advskew = carpr_advskew;
154 	if (carpr_advbase > -1)
155 		carpr.carpr_advbase = carpr_advbase;
156 	if (carpr_state > -1)
157 		carpr.carpr_state = carpr_state;
158 
159 	if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
160 		err(1, "SIOCSVH");
161 }
162 
163 static void
164 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
165 {
166 
167 	if (carpr_vhid == -1)
168 		errx(1, "passwd requires vhid");
169 
170 	carpr_key = val;
171 }
172 
173 static void
174 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
175 {
176 
177 	if (carpr_vhid == -1)
178 		errx(1, "advskew requires vhid");
179 
180 	carpr_advskew = atoi(val);
181 }
182 
183 static void
184 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
185 {
186 
187 	if (carpr_vhid == -1)
188 		errx(1, "advbase requires vhid");
189 
190 	carpr_advbase = atoi(val);
191 }
192 
193 static void
194 setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
195 {
196 	int i;
197 
198 	if (carpr_vhid == -1)
199 		errx(1, "state requires vhid");
200 
201 	for (i = 0; i <= CARP_MAXSTATE; i++)
202 		if (strcasecmp(carp_states[i], val) == 0) {
203 			carpr_state = i;
204 			return;
205 		}
206 
207 	errx(1, "unknown state");
208 }
209 
210 static struct cmd carp_cmds[] = {
211 	DEF_CMD_ARG("advbase",	setcarp_advbase),
212 	DEF_CMD_ARG("advskew",	setcarp_advskew),
213 	DEF_CMD_ARG("pass",	setcarp_passwd),
214 	DEF_CMD_ARG("vhid",	setcarp_vhid),
215 	DEF_CMD_ARG("state",	setcarp_state),
216 };
217 static struct afswtch af_carp = {
218 	.af_name	= "af_carp",
219 	.af_af		= AF_UNSPEC,
220 	.af_other_status = carp_status,
221 };
222 
223 static __constructor void
224 carp_ctor(void)
225 {
226 	int i;
227 
228 	for (i = 0; i < nitems(carp_cmds);  i++)
229 		cmd_register(&carp_cmds[i]);
230 	af_register(&af_carp);
231 }
232