aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/control/interface_manager.c
blob: a50b57412eeb9e4aff99cef12c11b218d6173737 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
 * @file interface_manager.c
 * 
 * @brief Implementation of interface_manager_t.
 * 
 */

/*
 * Copyright (C) 2007 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 "interface_manager.h"

#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <dlfcn.h>

#include <daemon.h>
#include <library.h>
#include <control/interfaces/interface.h>
#include <processing/job_queue.h>
#include <processing/jobs/initiate_job.h>


typedef struct private_interface_manager_t private_interface_manager_t;
typedef struct interface_bus_listener_t interface_bus_listener_t;

/**
 * Private data of an stroke_t object.
 */
struct private_interface_manager_t {

	/**
	 * Public part of stroke_t object.
	 */
	interface_manager_t public;
	
	/**
	 * a list of all loaded interfaces
	 */
	linked_list_t *interfaces;
	
	/**
	 * dlopen() handles of interfaces
	 */
	linked_list_t *handles;
};

/**
 * helper struct to map bus listener callbacks to interface callbacks
 */
struct interface_bus_listener_t {

	/**
	 * bus listener callback function (called)
	 */
	bus_listener_t listener;
	
	/**
	 * IKE_SA to use for message filtering
	 */
	ike_sa_t *ike_sa;
	
	/**
	 *  interface callback (listener gets redirected to here)
	 */
	interface_manager_cb_t callback;
	
	/**
	 * user parameter to pass to callback
	 */
	void *param;
};

/**
 * Implementation of interface_manager_t.create_ike_sa_iterator.
 */
static iterator_t* create_ike_sa_iterator(interface_manager_t *this)
{
	return charon->ike_sa_manager->create_iterator(charon->ike_sa_manager);
}

/**
 * Implementation of interface_manager_t.initiate.
 */
static status_t initiate(private_interface_manager_t *this,
						 peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
						 interface_manager_cb_t cb, void *param)
{
	ike_sa_t *ours = NULL;
	job_t *job;
	status_t retval;
	
	charon->bus->set_listen_state(charon->bus, TRUE);
	
	job = (job_t*)initiate_job_create(peer_cfg, child_cfg);
	charon->job_queue->add(charon->job_queue, job);
	
	while (TRUE)
	{
		level_t level;
		signal_t signal;
		int thread;
		ike_sa_t *ike_sa;
		char* format;
		va_list args;
		
		signal = charon->bus->listen(charon->bus, &level, &thread, 
									 &ike_sa, &format, &args);
		
		if (cb && (ike_sa == ours || ours == NULL))
		{
			if (!cb(param, signal, level, ike_sa, format, args))
			{
				charon->bus->set_listen_state(charon->bus, FALSE);
				return NEED_MORE;
			}
		}
		
		switch (signal)
		{
			case CHILD_UP_SUCCESS:
				if (ike_sa == ours)
				{
					retval = SUCCESS;
					break;
				}
				continue;
			case CHILD_UP_FAILED:
			case IKE_UP_FAILED:
				if (ike_sa == ours)
				{
					retval = FAILED;
					break;
				}
				continue;
			case CHILD_UP_START:
			case IKE_UP_START:
				if (ours == NULL)
				{
					ours = ike_sa;
				}
				continue;
			default:
				continue;
		}
		break;
	}
	charon->bus->set_listen_state(charon->bus, FALSE);
	return retval;
}

/**
 * listener function for terminate_ike
 */
static bool terminate_listener(interface_bus_listener_t *this, signal_t signal,
							   level_t level, int thread, ike_sa_t *ike_sa,
							   char* format, va_list args)
{
	if (this->ike_sa == ike_sa)
	{
		if (!this->callback(this->param, signal, level, ike_sa, format, args))
		{
			return FALSE;
		}
		switch (signal)
		{
			case IKE_DOWN_FAILED:
			case IKE_DOWN_SUCCESS:
			{
				return FALSE;
			}
			default:
				break;
		}
	}
	return TRUE;
}

/**
 * Implementation of interface_manager_t.terminate_ike.
 */
static status_t terminate_ike(interface_manager_t *this, u_int32_t unique_id, 
							  interface_manager_cb_t callback, void *param)
{
	ike_sa_t *ike_sa;
	status_t status;
	
	ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
													unique_id, FALSE);							
	if (ike_sa == NULL)
	{
		return NOT_FOUND;
	}
	
	/* we listen passively first, to catch the signals we are raising */
	if (callback)
	{
		interface_bus_listener_t listener;
	
		listener.listener.signal = (void*)terminate_listener;
		listener.callback = callback;
		listener.ike_sa = ike_sa;
		listener.param = param;
		charon->bus->add_listener(charon->bus, &listener.listener);
	}
	charon->bus->set_listen_state(charon->bus, TRUE);
	status = ike_sa->delete(ike_sa);
	if (status == DESTROY_ME)
	{
		charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
	}
	else
	{
		charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
		
		/* wait until IKE_SA is cleanly deleted using a delete message */
		while (TRUE)
		{
			level_t level;
			signal_t signal;
			int thread;
			ike_sa_t *current;
			char* format;
			va_list args;
			
			signal = charon->bus->listen(charon->bus, &level, &thread, 
										 &current, &format, &args);
			
			if (ike_sa == current)
			{
				switch (signal)
				{
					case IKE_DOWN_FAILED:
					case IKE_DOWN_SUCCESS:
					{
						 break;
					}
					default:
						continue;
				}
				break;
			}
		}
	}
	charon->bus->set_listen_state(charon->bus, FALSE);

	return SUCCESS;
}

/**
 * Implementation of interface_manager_t.terminate_child.
 */
static status_t terminate_child(interface_manager_t *this, u_int32_t reqid, 
								interface_manager_cb_t callback, void *param)
{
	return FAILED;
}

/**
 * Implementation of interface_manager_t.route.
 */
static status_t route(interface_manager_t *this,
					  peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
					  interface_manager_cb_t callback, void *param)
{
	return FAILED;
}

/**
 * Implementation of interface_manager_t.unroute.
 */
static status_t unroute(interface_manager_t *this, u_int32_t reqid, 
						interface_manager_cb_t callback, void *param)
{
	return FAILED;
}

/**
 * load the control interface modules
 */
static void load_interfaces(private_interface_manager_t *this)
{
	struct dirent* entry;
	struct stat stb;
	DIR* dir;
	
	if (stat(IPSEC_INTERFACEDIR, &stb) == -1 || !(stb.st_mode & S_IFDIR))
	{
		DBG1(DBG_CFG, "error opening interface modules directory "IPSEC_INTERFACEDIR);
		return;
	}

	dir = opendir(IPSEC_INTERFACEDIR);
	if (dir == NULL)
	{
		DBG1(DBG_CFG, "error opening interface modules directory "IPSEC_INTERFACEDIR);
		return;
	}
	
	DBG1(DBG_CFG, "loading control interface modules from '"IPSEC_INTERFACEDIR"'");

	while ((entry = readdir(dir)) != NULL)
	{
		char file[256];
		interface_t *interface;
		interface_constructor_t constructor;
		void *handle;
		char *ending;
		
		snprintf(file, sizeof(file), IPSEC_INTERFACEDIR"/%s", entry->d_name);
		
		if (stat(file, &stb) == -1 || !(stb.st_mode & S_IFREG))
		{
			DBG2(DBG_CFG, "  skipping %s, doesn't look like a file",
				 entry->d_name);
			continue;
		}
		ending = entry->d_name + strlen(entry->d_name) - 3;
		if (ending <= entry->d_name || !streq(ending, ".so"))
		{
			/* skip anything which does not look like a library */
			DBG2(DBG_CFG, "  skipping %s, doesn't look like a library",
				 entry->d_name);
			continue;
		}
		/* try to load the library */
		handle = dlopen(file, RTLD_LAZY);
		if (handle == NULL)
		{
			DBG1(DBG_CFG, "  opening control interface module %s failed: %s",
				 entry->d_name, dlerror());
			continue;
		}
		constructor = dlsym(handle, "interface_create");
		if (constructor == NULL)
		{
			DBG1(DBG_CFG, "  interface module %s has no interface_create() "
				 "function, skipped", entry->d_name);
			dlclose(handle);
			continue;
		}
		
		interface = constructor();
		if (interface == NULL)
		{
			DBG1(DBG_CFG, "  unable to create instance of interface "
				 "module %s, skipped", entry->d_name);
			dlclose(handle);
			continue;
		}
		DBG1(DBG_CFG, "  loaded control interface module successfully from %s", entry->d_name);
		this->interfaces->insert_last(this->interfaces, interface);
		this->handles->insert_last(this->handles, handle);
	}
	closedir(dir);
}


/**
 * Implementation of stroke_t.destroy.
 */
static void destroy(private_interface_manager_t *this)
{
	this->interfaces->destroy_offset(this->interfaces, offsetof(interface_t, destroy));
	this->handles->destroy_function(this->handles, (void*)dlclose);
	free(this);
}

/*
 * Described in header-file
 */
interface_manager_t *interface_manager_create(void)
{
	private_interface_manager_t *this = malloc_thing(private_interface_manager_t);
	
	this->public.create_ike_sa_iterator = (iterator_t*(*)(interface_manager_t*))create_ike_sa_iterator;
	this->public.initiate = (status_t(*)(interface_manager_t*,peer_cfg_t*,child_cfg_t*,bool(*)(void*,signal_t,level_t,ike_sa_t*,char*,va_list),void*))initiate;
	this->public.terminate_ike = (status_t(*)(interface_manager_t*,u_int32_t,interface_manager_cb_t, void*))terminate_ike;
	this->public.terminate_child = (status_t(*)(interface_manager_t*,u_int32_t,interface_manager_cb_t, void *param))terminate_child;
	this->public.route = (status_t(*)(interface_manager_t*,peer_cfg_t*, child_cfg_t*,interface_manager_cb_t,void*))route;
	this->public.unroute = (status_t(*)(interface_manager_t*,u_int32_t,interface_manager_cb_t,void*))unroute;
	this->public.destroy = (void (*)(interface_manager_t*))destroy;
	
	this->interfaces = linked_list_create();
	this->handles = linked_list_create();
	
	load_interfaces(this);
	
	return &this->public;
}