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
|
/*
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "ha_plugin.h"
#include "ha_ike.h"
#include "ha_child.h"
#include "ha_socket.h"
#include "ha_tunnel.h"
#include "ha_dispatcher.h"
#include "ha_segments.h"
#include "ha_ctl.h"
#include "ha_cache.h"
#include "ha_attribute.h"
#include <daemon.h>
#include <config/child_cfg.h>
typedef struct private_ha_plugin_t private_ha_plugin_t;
/**
* private data of ha plugin
*/
struct private_ha_plugin_t {
/**
* implements plugin interface
*/
ha_plugin_t public;
/**
* Communication socket
*/
ha_socket_t *socket;
/**
* Tunnel securing sync messages.
*/
ha_tunnel_t *tunnel;
/**
* IKE_SA synchronization
*/
ha_ike_t *ike;
/**
* CHILD_SA synchronization
*/
ha_child_t *child;
/**
* Dispatcher to process incoming messages
*/
ha_dispatcher_t *dispatcher;
/**
* Active/Passive segment management
*/
ha_segments_t *segments;
/**
* Interface to control segments at kernel level
*/
ha_kernel_t *kernel;
/**
* Segment control interface via FIFO
*/
ha_ctl_t *ctl;
/**
* Message cache for resynchronization
*/
ha_cache_t *cache;
/**
* Attribute provider
*/
ha_attribute_t *attr;
};
METHOD(plugin_t, get_name, char*,
private_ha_plugin_t *this)
{
return "ha";
}
/**
* Initialize plugin
*/
static bool initialize_plugin(private_ha_plugin_t *this)
{
char *local, *remote, *secret;
u_int count;
bool fifo, monitor, resync;
local = lib->settings->get_str(lib->settings,
"%s.plugins.ha.local", NULL, lib->ns);
remote = lib->settings->get_str(lib->settings,
"%s.plugins.ha.remote", NULL, lib->ns);
secret = lib->settings->get_str(lib->settings,
"%s.plugins.ha.secret", NULL, lib->ns);
fifo = lib->settings->get_bool(lib->settings,
"%s.plugins.ha.fifo_interface", TRUE, lib->ns);
monitor = lib->settings->get_bool(lib->settings,
"%s.plugins.ha.monitor", TRUE, lib->ns);
resync = lib->settings->get_bool(lib->settings,
"%s.plugins.ha.resync", TRUE, lib->ns);
count = min(SEGMENTS_MAX, lib->settings->get_int(lib->settings,
"%s.plugins.ha.segment_count", 1, lib->ns));
if (!local || !remote)
{
DBG1(DBG_CFG, "HA config misses local/remote address");
return FALSE;
}
if (secret)
{
this->tunnel = ha_tunnel_create(local, remote, secret);
}
this->socket = ha_socket_create(local, remote);
if (!this->socket)
{
return FALSE;
}
this->kernel = ha_kernel_create(count);
this->segments = ha_segments_create(this->socket, this->kernel, this->tunnel,
count, strcmp(local, remote) > 0, monitor);
this->cache = ha_cache_create(this->kernel, this->socket, this->tunnel,
resync, count);
if (fifo)
{
this->ctl = ha_ctl_create(this->segments, this->cache);
}
this->attr = ha_attribute_create(this->kernel, this->segments);
this->dispatcher = ha_dispatcher_create(this->socket, this->segments,
this->cache, this->kernel, this->attr);
this->ike = ha_ike_create(this->socket, this->tunnel, this->cache);
this->child = ha_child_create(this->socket, this->tunnel, this->segments,
this->kernel);
return TRUE;
}
/**
* Initialize plugin and register listener
*/
static bool plugin_cb(private_ha_plugin_t *this,
plugin_feature_t *feature, bool reg, void *cb_data)
{
if (reg)
{
if (!initialize_plugin(this))
{
return FALSE;
}
charon->bus->add_listener(charon->bus, &this->segments->listener);
charon->bus->add_listener(charon->bus, &this->ike->listener);
charon->bus->add_listener(charon->bus, &this->child->listener);
charon->attributes->add_provider(charon->attributes,
&this->attr->provider);
}
else
{
charon->attributes->remove_provider(charon->attributes,
&this->attr->provider);
charon->bus->remove_listener(charon->bus, &this->segments->listener);
charon->bus->remove_listener(charon->bus, &this->ike->listener);
charon->bus->remove_listener(charon->bus, &this->child->listener);
}
return TRUE;
}
METHOD(plugin_t, get_features, int,
private_ha_plugin_t *this, plugin_feature_t *features[])
{
static plugin_feature_t f[] = {
PLUGIN_CALLBACK((plugin_feature_callback_t)plugin_cb, NULL),
PLUGIN_PROVIDE(CUSTOM, "ha"),
PLUGIN_SDEPEND(CUSTOM, "kernel-ipsec"),
};
*features = f;
return countof(f);
}
METHOD(plugin_t, destroy, void,
private_ha_plugin_t *this)
{
DESTROY_IF(this->ctl);
DESTROY_IF(this->ike);
DESTROY_IF(this->child);
DESTROY_IF(this->dispatcher);
DESTROY_IF(this->attr);
DESTROY_IF(this->cache);
DESTROY_IF(this->segments);
DESTROY_IF(this->kernel);
DESTROY_IF(this->socket);
DESTROY_IF(this->tunnel);
free(this);
}
/**
* Plugin constructor
*/
plugin_t *ha_plugin_create()
{
private_ha_plugin_t *this;
if (!lib->caps->keep(lib->caps, CAP_CHOWN))
{ /* required to chown(2) control socket, ha_kernel also needs it at
* runtime */
DBG1(DBG_CFG, "ha plugin requires CAP_CHOWN capability");
return NULL;
}
INIT(this,
.public = {
.plugin = {
.get_name = _get_name,
.get_features = _get_features,
.destroy = _destroy,
},
},
);
return &this->public.plugin;
}
|