xref: /dragonfly/sbin/ifconfig/ifcarp.c (revision b7367ef6)
1 /*
2  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3  * Copyright (c) 2003 Ryan McBride. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 /*
27  * $FreeBSD: src/sbin/ifconfig/ifcarp.c,v 1.2 2005/02/22 14:07:47 glebius Exp $
28  * $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $
29  * $DragonFly: src/sbin/ifconfig/ifcarp.c,v 1.1 2007/08/16 20:03:55 dillon Exp $
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/ethernet.h>
41 #include <net/if.h>
42 #include <netinet/ip_carp.h>
43 #include <net/route.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 "ifconfig.h"
54 
55 static const char *carp_states[] = { CARP_STATES };
56 
57 void carp_status(int s);
58 void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
59 void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
60 void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
61 void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
62 
63 void
64 carp_status(int s)
65 {
66 	const char *state;
67 	struct carpreq carpr;
68 
69 	memset((char *)&carpr, 0, sizeof(struct carpreq));
70 	ifr.ifr_data = (caddr_t)&carpr;
71 
72 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
73 		return;
74 
75 	if (carpr.carpr_vhid > 0) {
76 		if (carpr.carpr_state > CARP_MAXSTATE)
77 			state = "<UNKNOWN>";
78 		else
79 			state = carp_states[carpr.carpr_state];
80 
81 		printf("\tcarp: %s vhid %d advbase %d advskew %d\n",
82 		    state, carpr.carpr_vhid, carpr.carpr_advbase,
83 		    carpr.carpr_advskew);
84 	}
85 
86 	return;
87 
88 }
89 
90 void
91 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
92 {
93 	struct carpreq carpr;
94 
95 	memset((char *)&carpr, 0, sizeof(struct carpreq));
96 	ifr.ifr_data = (caddr_t)&carpr;
97 
98 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
99 		err(1, "SIOCGVH");
100 
101 	/* XXX Should hash the password into the key here, perhaps? */
102 	strlcpy(carpr.carpr_key, val, CARP_KEY_LEN);
103 
104 	if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
105 		err(1, "SIOCSVH");
106 
107 	return;
108 }
109 
110 void
111 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
112 {
113 	int vhid;
114 	struct carpreq carpr;
115 
116 	vhid = atoi(val);
117 
118 	if (vhid <= 0)
119 		errx(1, "vhid must be greater than 0");
120 
121 	memset((char *)&carpr, 0, sizeof(struct carpreq));
122 	ifr.ifr_data = (caddr_t)&carpr;
123 
124 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
125 		err(1, "SIOCGVH");
126 
127 	carpr.carpr_vhid = vhid;
128 
129 	if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
130 		err(1, "SIOCSVH");
131 
132 	return;
133 }
134 
135 void
136 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
137 {
138 	int advskew;
139 	struct carpreq carpr;
140 
141 	advskew = atoi(val);
142 
143 	memset((char *)&carpr, 0, sizeof(struct carpreq));
144 	ifr.ifr_data = (caddr_t)&carpr;
145 
146 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
147 		err(1, "SIOCGVH");
148 
149 	carpr.carpr_advskew = advskew;
150 
151 	if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
152 		err(1, "SIOCSVH");
153 
154 	return;
155 }
156 
157 void
158 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
159 {
160 	int advbase;
161 	struct carpreq carpr;
162 
163 	advbase = atoi(val);
164 
165 	memset((char *)&carpr, 0, sizeof(struct carpreq));
166 	ifr.ifr_data = (caddr_t)&carpr;
167 
168 	if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
169 		err(1, "SIOCGVH");
170 
171 	carpr.carpr_advbase = advbase;
172 
173 	if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
174 		err(1, "SIOCSVH");
175 
176 	return;
177 }
178 
179 static struct cmd carp_cmds[] = {
180 	DEF_CMD_ARG("advbase",	setcarp_advbase),
181 	DEF_CMD_ARG("advskew",	setcarp_advskew),
182 	DEF_CMD_ARG("pass",	setcarp_passwd),
183 	DEF_CMD_ARG("vhid",	setcarp_vhid),
184 };
185 static struct afswtch af_carp = {
186 	.af_name	= "af_carp",
187 	.af_af		= AF_UNSPEC,
188 	.af_other_status = carp_status,
189 };
190 
191 static __constructor void
192 carp_ctor(void)
193 {
194 #define	N(a)	(sizeof(a) / sizeof(a[0]))
195 	int i;
196 
197 	for (i = 0; i < N(carp_cmds);  i++)
198 		cmd_register(&carp_cmds[i]);
199 	af_register(&af_carp);
200 #undef N
201 }
202