xref: /f-stack/tools/ifconfig/carp.c (revision d4a07e70)
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 #ifdef FSTACK
46 #include <netinet6/in6_var.h>
47 #endif
48 
49 #include <ctype.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <err.h>
55 #include <errno.h>
56 
57 #include "ifconfig.h"
58 
59 static const char *carp_states[] = { CARP_STATES };
60 
61 static void carp_status(int s);
62 static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
63 static void setcarp_callback(int, void *);
64 static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
65 static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
66 static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
67 
68 static int carpr_vhid = -1;
69 static int carpr_advskew = -1;
70 static int carpr_advbase = -1;
71 static int carpr_state = -1;
72 static unsigned char const *carpr_key;
73 
74 static void
carp_status(int s)75 carp_status(int s)
76 {
77 	struct carpreq carpr[CARP_MAXVHID];
78 	int i;
79 
80 	bzero(carpr, sizeof(struct carpreq) * CARP_MAXVHID);
81 	carpr[0].carpr_count = CARP_MAXVHID;
82 	ifr.ifr_data = (caddr_t)&carpr;
83 
84 #ifndef FSTACK
85 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
86 #else
87 	size_t offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
88 	size_t clen = sizeof(struct carpreq) * CARP_MAXVHID;
89 	if (ioctl_va(s, SIOCGVH, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1)
90 #endif
91 		return;
92 
93 	for (i = 0; i < carpr[0].carpr_count; i++) {
94 		printf("\tcarp: %s vhid %d advbase %d advskew %d",
95 		    carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
96 		    carpr[i].carpr_advbase, carpr[i].carpr_advskew);
97 		if (printkeys && carpr[i].carpr_key[0] != '\0')
98 			printf(" key \"%s\"\n", carpr[i].carpr_key);
99 		else
100 			printf("\n");
101 	}
102 }
103 
104 static void
setcarp_vhid(const char * val,int d,int s,const struct afswtch * afp)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
setcarp_callback(int s,void * arg __unused)143 setcarp_callback(int s, void *arg __unused)
144 {
145 	struct carpreq carpr;
146 
147 	bzero(&carpr, sizeof(struct carpreq));
148 	carpr.carpr_vhid = carpr_vhid;
149 	carpr.carpr_count = 1;
150 	ifr.ifr_data = (caddr_t)&carpr;
151 
152 #ifndef FSTACK
153 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1 && errno != ENOENT)
154 #else
155 	size_t offset, clen;
156 	offset = (char *)&(ifr.ifr_data) - (char *)&(ifr);
157 	clen = sizeof(carpr);
158 	if (ioctl_va(s, SIOCGVH, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1
159 	    && errno != ENOENT)
160 #endif
161 		err(1, "SIOCGVH");
162 
163 	if (carpr_key != NULL)
164 		/* XXX Should hash the password into the key here? */
165 #ifndef FSTACK
166 		strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
167 #else
168 		strlcpy((char *)carpr.carpr_key, (const char *)carpr_key, CARP_KEY_LEN);
169 #endif
170 	if (carpr_advskew > -1)
171 		carpr.carpr_advskew = carpr_advskew;
172 	if (carpr_advbase > -1)
173 		carpr.carpr_advbase = carpr_advbase;
174 	if (carpr_state > -1)
175 		carpr.carpr_state = carpr_state;
176 
177 #ifndef FSTACK
178 	if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
179 #else
180 	if (ioctl_va(s, SIOCSVH, (caddr_t)&ifr, 3, offset, ifr.ifr_data, clen) == -1)
181 #endif
182 		err(1, "SIOCSVH");
183 }
184 
185 static void
setcarp_passwd(const char * val,int d,int s,const struct afswtch * afp)186 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
187 {
188 
189 	if (carpr_vhid == -1)
190 		errx(1, "passwd requires vhid");
191 
192 #ifndef FSTACK
193 	carpr_key = val;
194 #else
195 	carpr_key = (const unsigned char *)val;
196 #endif
197 }
198 
199 static void
setcarp_advskew(const char * val,int d,int s,const struct afswtch * afp)200 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
201 {
202 
203 	if (carpr_vhid == -1)
204 		errx(1, "advskew requires vhid");
205 
206 	carpr_advskew = atoi(val);
207 }
208 
209 static void
setcarp_advbase(const char * val,int d,int s,const struct afswtch * afp)210 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
211 {
212 
213 	if (carpr_vhid == -1)
214 		errx(1, "advbase requires vhid");
215 
216 	carpr_advbase = atoi(val);
217 }
218 
219 static void
setcarp_state(const char * val,int d,int s,const struct afswtch * afp)220 setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
221 {
222 	int i;
223 
224 	if (carpr_vhid == -1)
225 		errx(1, "state requires vhid");
226 
227 	for (i = 0; i <= CARP_MAXSTATE; i++)
228 		if (strcasecmp(carp_states[i], val) == 0) {
229 			carpr_state = i;
230 			return;
231 		}
232 
233 	errx(1, "unknown state");
234 }
235 
236 static struct cmd carp_cmds[] = {
237 	DEF_CMD_ARG("advbase",	setcarp_advbase),
238 	DEF_CMD_ARG("advskew",	setcarp_advskew),
239 	DEF_CMD_ARG("pass",	setcarp_passwd),
240 	DEF_CMD_ARG("vhid",	setcarp_vhid),
241 	DEF_CMD_ARG("state",	setcarp_state),
242 };
243 static struct afswtch af_carp = {
244 	.af_name	= "af_carp",
245 	.af_af		= AF_UNSPEC,
246 	.af_other_status = carp_status,
247 };
248 
249 static __constructor void
carp_ctor(void)250 carp_ctor(void)
251 {
252 	int i;
253 
254 	for (i = 0; i < nitems(carp_cmds);  i++)
255 		cmd_register(&carp_cmds[i]);
256 	af_register(&af_carp);
257 }
258