summaryrefslogtreecommitdiff
path: root/usr.sbin/relayd/carp.c
blob: 25312497d5d6492db7a2fa48904c3e3f40c8fc04 (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
/*	$OpenBSD: carp.c,v 1.5 2007/12/07 17:17:00 reyk Exp $ */

/*
 * Copyright (c) 2006 Henning Brauer <henning@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#include <net/if.h>

#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <event.h>

#include <openssl/ssl.h>

#include "relayd.h"

struct carpgroup {
	TAILQ_ENTRY(carpgroup)	 entry;
	char			*group;
	int			 do_demote;
	int			 changed_by;
};

TAILQ_HEAD(carpgroups, carpgroup)	carpgroups =
    TAILQ_HEAD_INITIALIZER(carpgroups);

struct carpgroup	*carp_group_find(char *group);
int			 carp_demote_ioctl(char *, int);

struct carpgroup *
carp_group_find(char *group)
{
	struct carpgroup	*c;

	TAILQ_FOREACH(c, &carpgroups, entry)
		if (!strcmp(c->group, group))
			return (c);

	return (NULL);
}

int
carp_demote_init(char *group, int force)
{
	struct carpgroup	*c;
	int			 level;

	if ((c = carp_group_find(group)) == NULL) {
		if ((c = calloc(1, sizeof(struct carpgroup))) == NULL) {
			log_warn("carp_demote_init calloc");
			return (-1);
		}
		if ((c->group = strdup(group)) == NULL) {
			log_warn("carp_demote_init calloc");
			free(c);
			return (-1);
		}

		/* only demote if this group already is demoted */
		if ((level = carp_demote_get(group)) == -1)
			return (-1);
		if (level > 0 || force)
			c->do_demote = 1;

		TAILQ_INSERT_TAIL(&carpgroups, c, entry);
	}

	return (0);
}

void
carp_demote_shutdown(void)
{
	struct carpgroup	*c;

	while ((c = TAILQ_FIRST(&carpgroups)) != NULL) {
		TAILQ_REMOVE(&carpgroups, c, entry);
		for (; c->changed_by > 0; c->changed_by--)
			if (c->do_demote)
				carp_demote_ioctl(c->group, -1);

		free(c->group);
		free(c);
	}
}

int
carp_demote_get(char *group)
{
	int			s;
	struct ifgroupreq	ifgr;

	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		log_warn("carp_demote_get: socket");
		return (-1);
	}

	bzero(&ifgr, sizeof(ifgr));
	if (strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)) >=
	    sizeof(ifgr.ifgr_name)) {
		log_warn("carp_demote_get: invalid group");
		return (-1);
	}

	if (ioctl(s, SIOCGIFGATTR, (caddr_t)&ifgr) == -1) {
		if (errno == ENOENT)
			log_warnx("group \"%s\" does not exist", group);
		else
			log_warn("carp_demote_get: ioctl");
		close(s);
		return (-1);
	}

	close(s);
	return ((int)ifgr.ifgr_attrib.ifg_carp_demoted);
}

int
carp_demote_set(char *group, int demote)
{
	struct carpgroup	*c;

	if ((c = carp_group_find(group)) == NULL) {
		log_warnx("carp_group_find for %s returned NULL?!", group);
		return (-1);
	}

	if (c->changed_by + demote < 0) {
		log_warnx("carp_demote_set: changed_by + demote < 0");
		return (-1);
	}

	if (c->do_demote && carp_demote_ioctl(group, demote) == -1)
		return (-1);

	c->changed_by += demote;

	/* enable demotion when we return to 0, i. e. all sessions up */
	if (demote < 0 && c->changed_by == 0)
		c->do_demote = 1;

	return (0);
}

int
carp_demote_reset(char *group, int value)
{
	int			 level;
	int			 demote = 0;

	if (value < 0) {
		log_warnx("carp_demote_reset: value < 0");
		return (-1);
	}

	if ((level = carp_demote_get(group)) == -1)
		return (-1);
	if (level == value)
		return (0);

	demote -= level;
	demote += value;

	if (carp_demote_ioctl(group, demote) == -1)
		return (-1);

	return (0);
}

int
carp_demote_ioctl(char *group, int demote)
{
	int			s, res;
	struct ifgroupreq	ifgr;

	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		log_warn("carp_demote_get: socket");
		return (-1);
	}

	bzero(&ifgr, sizeof(ifgr));
	if (strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)) >=
	    sizeof(ifgr.ifgr_name)) {
		log_warn("carp_demote_ioctl: invalid group");
		return (-1);
	}
	ifgr.ifgr_attrib.ifg_carp_demoted = demote;

	if ((res = ioctl(s, SIOCSIFGATTR, (caddr_t)&ifgr)) == -1)
		log_warn("unable to %s the demote state "
		    "of group '%s'", (demote > 0) ? "increment" : "decrement",
		    group);
	else
		log_info("%s the demote state of group '%s'",
		    (demote > 0) ? "incremented" : "decremented", group);

	close(s);
	return (res);
}