summaryrefslogtreecommitdiff
path: root/usr.sbin/sasyncd/carp.c
blob: de876c0ea6f8f24a9a3915aa6affde92e3e2b51b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*	$OpenBSD: carp.c,v 1.8 2006/11/28 19:21:15 reyk Exp $	*/

/*
 * Copyright (c) 2005 Håkan Olsson.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This code was written under funding by Multicom Security AB.
 */


#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/route.h>

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "monitor.h"
#include "sasyncd.h"

int carp_demoted = 0;

static enum RUNSTATE
carp_map_state(u_char link_state)
{
	enum RUNSTATE state = FAIL;

	switch(link_state) {
	case LINK_STATE_UP:
	case LINK_STATE_HALF_DUPLEX:
	case LINK_STATE_FULL_DUPLEX:
		state = MASTER;
		break;
	case LINK_STATE_DOWN:
		state = SLAVE;
		break;
	case LINK_STATE_UNKNOWN:
		state = INIT;
		break;
	}

	return state;
}

/* Returns 1 for the CARP MASTER, 0 for BACKUP/INIT, -1 on error.  */
static enum RUNSTATE
carp_get_state(char *ifname)
{
	struct ifreq	ifr;
	struct if_data	ifrdat;
	int		s, saved_errno;

	if (!ifname || !*ifname) {
		errno = ENOENT;
		return FAIL;
	}

	memset(&ifr, 0, sizeof ifr);
	strlcpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);

	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s < 0)
		return FAIL;

	ifr.ifr_data = (caddr_t)&ifrdat;
	if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1) {
		saved_errno = errno;
		close(s);
		errno = saved_errno;
		return FAIL;
	}
	close(s);
	return carp_map_state(ifrdat.ifi_link_state);
}

void
carp_demote(int demote, int force)
{
	struct ifgroupreq        ifgr;
	int s;

	if (carp_demoted + demote < 0) {
		log_msg(1, "carp_demote: mismatched promotion");
		return;
	}

	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s < 0) {
		log_msg(1, "carp_demote: couldn't open socket");
		return;
	}

	bzero(&ifgr, sizeof(ifgr));
	strlcpy(ifgr.ifgr_name, cfgstate.carp_ifgroup, sizeof(ifgr.ifgr_name));

	/* Unless we force it, don't demote if we're not demoting already. */
	if (!force) {
		if (ioctl(s, SIOCGIFGATTR, (caddr_t)&ifgr) == -1) {
			log_msg(1, "carp_demote: unable to get "
			    "the demote state of group '%s'",
			    cfgstate.carp_ifgroup);
			    goto done;
		}

		if (ifgr.ifgr_attrib.ifg_carp_demoted == 0)
			goto done;
	}

	ifgr.ifgr_attrib.ifg_carp_demoted = demote;
	if (ioctl(s, SIOCSIFGATTR, (caddr_t)&ifgr) == -1)
		log_msg(1, "carp_demote: unable to %s the demote state "
		    "of group '%s'", (demote > 0) ?
		    "increment" : "decrement", cfgstate.carp_ifgroup);
	else {
		carp_demoted += demote;
		log_msg(1, "carp_demote: %sed the demote state "
		    "of group '%s'", (demote > 0) ?
		    "increment" : "decrement", cfgstate.carp_ifgroup);
	}
done:
	close(s);
}

const char*
carp_state_name(enum RUNSTATE state)
{
	static const char	*carpstate[] = CARPSTATES;

	if (state < 0 || state > FAIL)
		state = FAIL;
	return carpstate[state];
}

void
carp_update_state(enum RUNSTATE current_state)
{

	if (current_state < 0 || current_state > FAIL) {
		log_err("carp_update_state: invalid carp state, abort");
		cfgstate.runstate = FAIL;
		return;
	}

	if (current_state != cfgstate.runstate) {
		log_msg(1, "carp_update_state: switching state to %s",
		    carp_state_name(current_state));
		cfgstate.runstate = current_state;
		if (current_state == MASTER)
			pfkey_set_promisc();
		isakmpd_setrun();
		net_ctl_update_state();
	}
}

void
carp_check_state()
{
	carp_update_state(carp_get_state(cfgstate.carp_ifname));
}

void
carp_set_rfd(fd_set *fds)
{
	if (cfgstate.route_socket != -1)
		FD_SET(cfgstate.route_socket, fds);
}

static void
carp_read(void)
{
	char msg[2048];
	struct rt_msghdr *rtm = (struct rt_msghdr *)&msg;
	struct if_msghdr ifm;
	int len;

	len = read(cfgstate.route_socket, msg, sizeof(msg));

	if (len < sizeof(struct rt_msghdr) ||
	    rtm->rtm_version != RTM_VERSION ||
	    rtm->rtm_type != RTM_IFINFO)
		return;

	memcpy(&ifm, rtm, sizeof(ifm));

	if (ifm.ifm_index == cfgstate.carp_ifindex)
		carp_update_state(carp_map_state(ifm.ifm_data.ifi_link_state));
}

void
carp_read_message(fd_set *fds)
{
	if (cfgstate.route_socket != -1)
		if (FD_ISSET(cfgstate.route_socket, fds))
			(void)carp_read();
}

/* Initialize the CARP state. */
int
carp_init(void)
{
	cfgstate.route_socket = -1;

	if (cfgstate.lockedstate != INIT) {
		cfgstate.runstate = cfgstate.lockedstate;
		log_msg(1, "carp_init: locking runstate to %s",
		    carp_state_name(cfgstate.runstate));
		return 0;
	}

	if (!cfgstate.carp_ifname || !*cfgstate.carp_ifname) {
		fprintf(stderr, "No carp interface\n");
		return -1;
	}

	cfgstate.carp_ifindex = if_nametoindex(cfgstate.carp_ifname);
	if (!cfgstate.carp_ifindex) {
		fprintf(stderr, "No carp interface index\n");
		return -1;
	}

	cfgstate.route_socket = socket(PF_ROUTE, SOCK_RAW, 0);
	if (cfgstate.route_socket < 0) {
		fprintf(stderr, "No routing socket\n");
		return -1;
	}

	cfgstate.runstate = carp_get_state(cfgstate.carp_ifname);
	if (cfgstate.runstate == FAIL) {
		fprintf(stderr, "Failed to check interface \"%s\".\n",
		    cfgstate.carp_ifname);
		fprintf(stderr, "Correct or manually select runstate.\n");
		return -1;
	}
	log_msg(1, "carp_init: initializing runstate to %s",
	    carp_state_name(cfgstate.runstate));

	return 0;
}

/* Enable or disable isakmpd connection checker. */
void
isakmpd_setrun(void)
{
	if (cfgstate.runstate == MASTER) {
		if (monitor_isakmpd_active(1))
			log_msg(0, "failed to activate isakmpd");
	} else {
		if (monitor_isakmpd_active(0))
			log_msg(0, "failed to passivate isakmpd");
	}
}