summaryrefslogtreecommitdiff
path: root/src/charon/processing/jobs/initiate_mediation_job.c
blob: d78f8a202a76b1d53d39e9f8162038489ff9f149 (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
/**
 * @file initiate_mediation_job.c
 * 
 * @brief Implementation of initiate_mediation_job_t.
 * 
 */

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

#include <sa/ike_sa.h>
#include <daemon.h>


typedef struct private_initiate_mediation_job_t private_initiate_mediation_job_t;

/**
 * Private data of an initiate_mediation_job_t Object
 */
struct private_initiate_mediation_job_t {
	/**
	 * public initiate_mediation_job_t interface
	 */
	initiate_mediation_job_t public;
	
	/**
	 * ID of the IKE_SA of the mediated connection.
	 */
	ike_sa_id_t *mediated_sa_id;
	
	/**
	 * Child config of the CHILD_SA of the mediated connection.
	 */
	child_cfg_t *mediated_child;
	
	/**
	 * ID of the IKE_SA of the mediation connection.
	 */
	ike_sa_id_t *mediation_sa_id;
};

/**
 * Implements job_t.destroy.
 */
static void destroy(private_initiate_mediation_job_t *this)
{
	DESTROY_IF(this->mediation_sa_id);
	DESTROY_IF(this->mediated_sa_id);
	DESTROY_IF(this->mediated_child);
	free(this);
}

/**
 * Callback to handle initiation of mediation connection
 */
static bool initiate_callback(private_initiate_mediation_job_t *this, signal_t signal, level_t level,
					 ike_sa_t *ike_sa, char *format, va_list args)
{
	if (signal == CHILD_UP_SUCCESS)
	{
		// mediation connection is up
		this->mediation_sa_id = ike_sa->get_id(ike_sa);
		this->mediation_sa_id = this->mediation_sa_id->clone(this->mediation_sa_id);
		return FALSE;
	}
	return TRUE;
}

/**
 * Implementation of job_t.execute.
 */ 
static void initiate(private_initiate_mediation_job_t *this)
{//FIXME: check the logging 
	ike_sa_t *mediated_sa, *mediation_sa;
	peer_cfg_t *mediated_cfg, *mediation_cfg;
	
	mediated_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
											  this->mediated_sa_id);
	if (mediated_sa)
	{
		mediated_cfg = mediated_sa->get_peer_cfg(mediated_sa);
		mediated_cfg->get_ref(mediated_cfg); // get_peer_cfg returns an internal object
		
		charon->ike_sa_manager->checkin(charon->ike_sa_manager, mediated_sa);
		
		mediation_cfg = mediated_cfg->get_mediated_by(mediated_cfg);
		
		if (charon->connect_manager->check_and_register(charon->connect_manager,
				mediation_cfg->get_my_id(mediation_cfg),
				mediated_cfg->get_peer_id(mediated_cfg),
				this->mediated_sa_id, this->mediated_child))
		{
			mediated_cfg->destroy(mediated_cfg);
			mediation_cfg->destroy(mediation_cfg);
			charon->bus->set_sa(charon->bus, mediated_sa); // this pointer should still be valid
			DBG1(DBG_IKE, "mediation with the same peer is already in progress, queued");
			destroy(this);
			return;
		}
		
		mediation_cfg->get_ref(mediation_cfg); // we need an additional reference because initiate consumes one

		// this function call blocks until the connection is up or failed
		// we do not check the status, but NEED_MORE would be returned on success
		// because the registered callback returns FALSE then
		// this->mediation_sa_id is set in the callback
		charon->interfaces->initiate(charon->interfaces,
				mediation_cfg, NULL, (interface_manager_cb_t)initiate_callback, this);
		if (!this->mediation_sa_id)
		{
			DBG1(DBG_JOB, "initiating mediation connection '%s' failed",
					mediation_cfg->get_name(mediation_cfg));
			mediation_cfg->destroy(mediation_cfg);
			mediated_cfg->destroy(mediated_cfg);
			charon->bus->set_sa(charon->bus, mediated_sa); // this pointer should still be valid
			SIG(IKE_UP_FAILED, "mediation failed");
			destroy(this);
			return;
		}
		mediation_cfg->destroy(mediation_cfg);

		mediation_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
				this->mediation_sa_id);
		
		if (mediation_sa)
		{
			if (mediation_sa->initiate_mediation(mediation_sa, mediated_cfg) != SUCCESS)
			{
				DBG1(DBG_JOB, "initiating mediated connection '%s' failed",
						mediated_cfg->get_name(mediated_cfg));
				mediated_cfg->destroy(mediated_cfg);
				charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, mediation_sa);
				
				charon->bus->set_sa(charon->bus, mediated_sa); // this pointer should still be valid
				SIG(IKE_UP_FAILED, "mediation failed");
				destroy(this);
				return;
			}
			
			charon->ike_sa_manager->checkin(charon->ike_sa_manager, mediation_sa);
		}
		
		mediated_cfg->destroy(mediated_cfg);
	}
	destroy(this);
}

/**
 * Implementation of job_t.execute.
 */ 
static void reinitiate(private_initiate_mediation_job_t *this)
{//FIXME: check the logging 
	ike_sa_t *mediated_sa, *mediation_sa;
	peer_cfg_t *mediated_cfg;
	
	mediated_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
											  this->mediated_sa_id);
	if (mediated_sa)
	{
		mediated_cfg = mediated_sa->get_peer_cfg(mediated_sa);
		mediated_cfg->get_ref(mediated_cfg); // get_peer_cfg returns an internal object
		charon->ike_sa_manager->checkin(charon->ike_sa_manager, mediated_sa);
		
		mediation_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager,
				this->mediation_sa_id);
		if (mediation_sa)
		{
			if (mediation_sa->initiate_mediation(mediation_sa, mediated_cfg) != SUCCESS)
			{
				DBG1(DBG_JOB, "initiating mediated connection '%s' failed",
						mediated_cfg->get_name(mediated_cfg));
				mediated_cfg->destroy(mediated_cfg);
				charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, mediation_sa);
				
				charon->bus->set_sa(charon->bus, mediated_sa); // this pointer should still be valid
				SIG(IKE_UP_FAILED, "mediation failed");
				destroy(this);
				return;
			}
			
			charon->ike_sa_manager->checkin(charon->ike_sa_manager, mediation_sa);
		}
		
		mediated_cfg->destroy(mediated_cfg);
	}
	destroy(this);
}

/**
 * Creates an empty job
 */
static private_initiate_mediation_job_t *initiate_mediation_job_create_empty()
{
	private_initiate_mediation_job_t *this = malloc_thing(private_initiate_mediation_job_t);
	
	/* interface functions */
	this->public.job_interface.destroy = (void (*) (job_t *)) destroy;
	
	/* private variables */
	this->mediation_sa_id = NULL;
	this->mediated_sa_id = NULL;
	this->mediated_child = NULL;

	return this;
}

/*
 * Described in header
 */
initiate_mediation_job_t *initiate_mediation_job_create(ike_sa_id_t *ike_sa_id,
		child_cfg_t *child_cfg)
{
	private_initiate_mediation_job_t *this = initiate_mediation_job_create_empty();
	
	this->public.job_interface.execute = (void (*) (job_t *)) initiate;
	
	this->mediated_sa_id = ike_sa_id->clone(ike_sa_id);
	child_cfg->get_ref(child_cfg);
	this->mediated_child = child_cfg;

	return &this->public;
}

/*
 * Described in header
 */
initiate_mediation_job_t *reinitiate_mediation_job_create(ike_sa_id_t *mediation_sa_id,
		ike_sa_id_t *mediated_sa_id)
{
	private_initiate_mediation_job_t *this = initiate_mediation_job_create_empty();
	
	this->public.job_interface.execute = (void (*) (job_t *)) reinitiate;
	
	this->mediation_sa_id = mediation_sa_id->clone(mediation_sa_id);
	this->mediated_sa_id = mediated_sa_id->clone(mediated_sa_id);
	
	return &this->public; 
}