summaryrefslogtreecommitdiff
path: root/src/charon/control/interface_manager.c
blob: c14903c7de91a570bbe67f9446d1a15851e759d4 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/**
 * @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>


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 {

	/**
	 * public bus listener interface
	 */
	bus_listener_t public;
	
	/**
	 * status of the operation, return to method callers
	 */
	status_t status;
	
	/**
	 * IKE SA to filter log output
	 */
	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;
	
	/**
	 * child configuration, used for initiate
	 */
	child_cfg_t *child_cfg;
	
	/**
	 * peer configuration, used for initiate
	 */
	peer_cfg_t *peer_cfg;
	
	/**
	 * unique ID, used for various methods
	 */
	u_int32_t id;
};


typedef struct interface_job_t interface_job_t;

/** 
 * job for asynchronous listen operations
 */
struct interface_job_t {
	/** 
	 * job interface 
	 */
	job_t public;
	
	/** 
	 * associated listener 
	 */
	interface_bus_listener_t listener;
};

/**
 * Implements the famous nop operation
 */
static void nop(job_t *job)
{
	/* NOP */
}

/**
 * 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);
}

/**
 * listener function for initiate
 */
static bool initiate_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 CHILD_UP_SUCCESS:
				this->status = SUCCESS;
				return FALSE;
			case IKE_UP_FAILED:
			case CHILD_UP_FAILED:
				return FALSE;
			default:
				break;
		}
	}
	return TRUE;
}

/**
 * execute function for initiate
 */
static status_t initiate_execute(interface_job_t *job)
{
	ike_sa_t *ike_sa;
	ike_cfg_t *ike_cfg;
	interface_bus_listener_t *listener = &job->listener;
	peer_cfg_t *peer_cfg = listener->peer_cfg;

	ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
	ike_sa = charon->ike_sa_manager->checkout_by_peer(charon->ike_sa_manager,
				ike_cfg->get_my_host(ike_cfg), ike_cfg->get_other_host(ike_cfg),
				peer_cfg->get_my_id(peer_cfg), peer_cfg->get_other_id(peer_cfg));
	listener->ike_sa = ike_sa;

	if (ike_sa->get_peer_cfg(ike_sa) == NULL)
	{
		ike_sa->set_peer_cfg(ike_sa, peer_cfg);
	}
	peer_cfg->destroy(peer_cfg);
	
	if (ike_sa->initiate(ike_sa, listener->child_cfg) != SUCCESS)
	{
		return charon->ike_sa_manager->checkin_and_destroy(
												charon->ike_sa_manager, ike_sa);
	}
	return charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
}

/**
 * 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 callback, void *param)
{
	interface_job_t job;

	job.listener.public.signal = (void*)initiate_listener;
	job.listener.ike_sa = NULL;
	job.listener.callback = callback;
	job.listener.param = param;
	job.listener.status = FAILED;
	job.listener.child_cfg = child_cfg;
	job.listener.peer_cfg = peer_cfg;
	job.public.execute = (void*)initiate_execute;
	job.public.destroy = nop;

	if (callback == NULL)
	{
		return initiate_execute(&job);
	}
	charon->bus->listen(charon->bus, (bus_listener_t*)&job.listener, (job_t*)&job);
	return job.listener.status;
}

/**
 * listener function for terminate_ike
 */
static bool terminate_ike_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_SUCCESS:
				this->status = SUCCESS;
				return FALSE;
			case IKE_DOWN_FAILED:
				return FALSE;
			default:
				break;
		}
	}
	return TRUE;
}

/**
 * execute function for terminate_ike
 */
static status_t terminate_ike_execute(interface_job_t *job)
{
	ike_sa_t *ike_sa;
	interface_bus_listener_t *listener = &job->listener;
	
	ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
													listener->id, FALSE);
	if (ike_sa == NULL)
	{
		SIG(IKE_DOWN_FAILED, "unable to terminate, IKE_SA with "
			"ID %d not found", listener->id);
		return NOT_FOUND;
	}	
	listener->ike_sa = ike_sa;						
	
	if (ike_sa->delete(ike_sa) == DESTROY_ME)
	{
		return charon->ike_sa_manager->checkin_and_destroy(
												charon->ike_sa_manager, ike_sa);
	}
	return charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
}

/**
 * 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)
{
	interface_job_t job;
	
	job.listener.public.signal = (void*)terminate_ike_listener;
	job.listener.ike_sa = NULL;
	job.listener.callback = callback;
	job.listener.param = param;
	job.listener.status = FAILED;
	job.listener.id = unique_id;
	job.public.execute = (void*)terminate_ike_execute;
	job.public.destroy = nop;

	if (callback == NULL)
	{
		return terminate_ike_execute(&job);
	}
	charon->bus->listen(charon->bus, (bus_listener_t*)&job.listener, (job_t*)&job);
	return job.listener.status;
}
/**
 * listener function for terminate_child
 */
static bool terminate_child_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 CHILD_DOWN_SUCCESS:
			case IKE_DOWN_SUCCESS:
				this->status = SUCCESS;
				return FALSE;
			case IKE_DOWN_FAILED:
			case CHILD_DOWN_FAILED:
				return FALSE;
			default:
				break;
		}
	}
	return TRUE;
}

/**
 * execute function for terminate_child
 */
static status_t terminate_child_execute(interface_job_t *job)
{
	ike_sa_t *ike_sa;
	child_sa_t *child_sa;
	iterator_t *iterator;
	interface_bus_listener_t *listener = &job->listener;
	
	ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
													listener->id, TRUE);							
	if (ike_sa == NULL)
	{
		SIG(CHILD_DOWN_FAILED, "unable to terminate, CHILD_SA with "
			"ID %d not found", listener->id);
		return NOT_FOUND;
	}
	listener->ike_sa = ike_sa;
	
	iterator = ike_sa->create_child_sa_iterator(ike_sa);
	while (iterator->iterate(iterator, (void**)&child_sa))
	{
		if (child_sa->get_state(child_sa) != CHILD_ROUTED &&
			child_sa->get_reqid(child_sa) == listener->id)
		{
			break;
		}
		child_sa = NULL;
	}
	iterator->destroy(iterator);
	
	if (child_sa == NULL)
	{
		SIG(CHILD_DOWN_FAILED, "unable to terminate, established CHILD_SA with "
			"ID %d not found", listener->id);
		charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
		return NOT_FOUND;
	}
	
	if (ike_sa->delete_child_sa(ike_sa, child_sa->get_protocol(child_sa),
								child_sa->get_spi(child_sa, TRUE)) == DESTROY_ME)
	{
		return charon->ike_sa_manager->checkin_and_destroy(
												charon->ike_sa_manager, ike_sa);
	}
	return charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
}

/**
 * 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)
{
	interface_job_t job;
	
	job.listener.public.signal = (void*)terminate_child_listener;
	job.listener.ike_sa = NULL;
	job.listener.callback = callback;
	job.listener.param = param;
	job.listener.status = FAILED;
	job.listener.id = reqid;
	job.public.execute = (void*)terminate_child_execute;
	job.public.destroy = nop;

	if (callback == NULL)
	{
		return terminate_child_execute(&job);
	}
	charon->bus->listen(charon->bus, (bus_listener_t*)&job.listener, (job_t*)&job);	
	return job.listener.status;
}

/**
 * listener function for route
 */
static bool route_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 CHILD_ROUTE_SUCCESS:
				this->status = SUCCESS;
				return FALSE;
			case CHILD_ROUTE_FAILED:
				return FALSE;
			default:
				break;
		}
	}
	return TRUE;
}

/**
 * execute function for route
 */
static status_t route_execute(interface_job_t *job)
{
	ike_sa_t *ike_sa;
	ike_cfg_t *ike_cfg;
	interface_bus_listener_t *listener = &job->listener;
	peer_cfg_t *peer_cfg = listener->peer_cfg;
	
	ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
	
	ike_sa = charon->ike_sa_manager->checkout_by_peer(charon->ike_sa_manager,
				ike_cfg->get_my_host(ike_cfg), ike_cfg->get_other_host(ike_cfg),
				peer_cfg->get_my_id(peer_cfg), peer_cfg->get_other_id(peer_cfg));
	listener->ike_sa = ike_sa;
	
	if (ike_sa->get_peer_cfg(ike_sa) == NULL)
	{
		ike_sa->set_peer_cfg(ike_sa, peer_cfg);
	}
	if (ike_sa->route(ike_sa, listener->child_cfg) == DESTROY_ME)
	{
		return charon->ike_sa_manager->checkin_and_destroy(
												charon->ike_sa_manager, ike_sa);
	}
	return charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
}

/**
 * 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)
{
	interface_job_t job;
	
	job.listener.public.signal = (void*)route_listener;
	job.listener.ike_sa = NULL;
	job.listener.callback = callback;
	job.listener.param = param;
	job.listener.status = FAILED;
	job.listener.peer_cfg = peer_cfg;
	job.listener.child_cfg = child_cfg;
	job.public.execute = (void*)route_execute;
	job.public.destroy = nop;

	if (callback == NULL)
	{
		return route_execute(&job);
	}
	charon->bus->listen(charon->bus, (bus_listener_t*)&job.listener, (job_t*)&job);
	return job.listener.status;
}

/**
 * listener function for unroute
 */
static bool unroute_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 CHILD_UNROUTE_SUCCESS:
				this->status = SUCCESS;
				return FALSE;
			case CHILD_UNROUTE_FAILED:
				return FALSE;
			default:
				break;
		}
	}
	return TRUE;
}
/**
 * execute function for unroute
 */
static status_t unroute_execute(interface_job_t *job)
{
	ike_sa_t *ike_sa;
	interface_bus_listener_t *listener = &job->listener;
	
	ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
													listener->id, TRUE);
	if (ike_sa == NULL)
	{
		SIG(CHILD_DOWN_FAILED, "unable to unroute, CHILD_SA with "
			"ID %d not found", listener->id);
		return NOT_FOUND;
	}
	listener->ike_sa = ike_sa;
	if (ike_sa->unroute(ike_sa, listener->id) == DESTROY_ME)
	{
		return charon->ike_sa_manager->checkin_and_destroy(
												charon->ike_sa_manager, ike_sa);
	}
	return charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
}

/**
 * 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)
{
	interface_job_t job;
	
	job.listener.public.signal = (void*)unroute_listener;
	job.listener.ike_sa = NULL;
	job.listener.callback = callback;
	job.listener.param = param;
	job.listener.status = FAILED;
	job.listener.id = reqid;
	job.public.execute = (void*)unroute_execute;
	job.public.destroy = nop;

	if (callback == NULL)
	{
		return unroute_execute(&job);
	}
	charon->bus->listen(charon->bus, (bus_listener_t*)&job.listener, (job_t*)&job);	
	return job.listener.status;
}

/**
 * load the control interface modules
 */
static void load_interfaces(private_interface_manager_t *this)
{
	struct dirent* entry;
	DIR* dir;

	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);
		
		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);
}

/**
 * See header
 */
bool interface_manager_cb_empty(void *param, signal_t signal, level_t level,
								ike_sa_t *ike_sa, char *format, va_list args)
{
	return TRUE;
}

/**
 * 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;
}