summaryrefslogtreecommitdiff
path: root/src/libcharon/sa/ikev2/tasks/ike_rekey.c
blob: 1855517ce25bff9f2ecfa93c5a90b9d675d7502b (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
/*
 * Copyright (C) 2005-2008 Martin Willi
 * Copyright (C) 2005 Jan Hutter
 * 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 "ike_rekey.h"

#include <daemon.h>
#include <encoding/payloads/notify_payload.h>
#include <sa/ikev2/tasks/ike_init.h>
#include <sa/ikev2/tasks/ike_delete.h>
#include <processing/jobs/delete_ike_sa_job.h>
#include <processing/jobs/rekey_ike_sa_job.h>
#include <processing/jobs/initiate_tasks_job.h>


typedef struct private_ike_rekey_t private_ike_rekey_t;

/**
 * Private members of a ike_rekey_t task.
 */
struct private_ike_rekey_t {

	/**
	 * Public methods and task_t interface.
	 */
	ike_rekey_t public;

	/**
	 * Assigned IKE_SA.
	 */
	ike_sa_t *ike_sa;

	/**
	 * New IKE_SA which replaces the current one
	 */
	ike_sa_t *new_sa;

	/**
	 * Are we the initiator?
	 */
	bool initiator;

	/**
	 * the TASK_IKE_INIT task which is reused to simplify rekeying
	 */
	ike_init_t *ike_init;

	/**
	 * IKE_DELETE task to delete the old IKE_SA after rekeying was successful
	 */
	ike_delete_t *ike_delete;

	/**
	 * colliding task detected by the task manager
	 */
	task_t *collision;
};

/**
 * Check if an IKE_SA has any queued tasks, return initiation job
 */
static job_t* check_queued_tasks(ike_sa_t *ike_sa)
{
	enumerator_t *enumerator;
	task_t *task;
	job_t *job = NULL;

	enumerator = ike_sa->create_task_enumerator(ike_sa, TASK_QUEUE_QUEUED);
	if (enumerator->enumerate(enumerator, &task))
	{
		job = (job_t*)initiate_tasks_job_create(ike_sa->get_id(ike_sa));
	}
	enumerator->destroy(enumerator);

	return job;
}

/**
 * Establish the new replacement IKE_SA
 */
static void establish_new(private_ike_rekey_t *this)
{
	if (this->new_sa)
	{
		job_t *job;

		this->new_sa->set_state(this->new_sa, IKE_ESTABLISHED);
		DBG0(DBG_IKE, "IKE_SA %s[%d] rekeyed between %H[%Y]...%H[%Y]",
			 this->new_sa->get_name(this->new_sa),
			 this->new_sa->get_unique_id(this->new_sa),
			 this->ike_sa->get_my_host(this->ike_sa),
			 this->ike_sa->get_my_id(this->ike_sa),
			 this->ike_sa->get_other_host(this->ike_sa),
			 this->ike_sa->get_other_id(this->ike_sa));

		this->new_sa->inherit_post(this->new_sa, this->ike_sa);
		charon->bus->ike_rekey(charon->bus, this->ike_sa, this->new_sa);
		job = check_queued_tasks(this->new_sa);
		/* don't queue job before checkin(), as the IKE_SA is not yet
		 * registered at the manager */
		charon->ike_sa_manager->checkin(charon->ike_sa_manager, this->new_sa);
		if (job)
		{
			lib->processor->queue_job(lib->processor, job);
		}
		this->new_sa = NULL;
		/* set threads active IKE_SA after checkin */
		charon->bus->set_sa(charon->bus, this->ike_sa);
	}
}

METHOD(task_t, process_r_delete, status_t,
	private_ike_rekey_t *this, message_t *message)
{
	establish_new(this);
	return this->ike_delete->task.process(&this->ike_delete->task, message);
}

METHOD(task_t, build_r_delete, status_t,
	private_ike_rekey_t *this, message_t *message)
{
	return this->ike_delete->task.build(&this->ike_delete->task, message);
}

METHOD(task_t, build_i_delete, status_t,
	private_ike_rekey_t *this, message_t *message)
{
	/* update exchange type to INFORMATIONAL for the delete */
	message->set_exchange_type(message, INFORMATIONAL);

	return this->ike_delete->task.build(&this->ike_delete->task, message);
}

METHOD(task_t, process_i_delete, status_t,
	private_ike_rekey_t *this, message_t *message)
{
	return this->ike_delete->task.process(&this->ike_delete->task, message);
}

METHOD(task_t, build_i, status_t,
	private_ike_rekey_t *this, message_t *message)
{
	ike_version_t version;

	/* create new SA only on first try */
	if (this->new_sa == NULL)
	{
		version = this->ike_sa->get_version(this->ike_sa);
		this->new_sa = charon->ike_sa_manager->checkout_new(
										charon->ike_sa_manager, version, TRUE);
		if (!this->new_sa)
		{	/* shouldn't happen */
			return FAILED;
		}
		this->new_sa->inherit_pre(this->new_sa, this->ike_sa);
		this->ike_init = ike_init_create(this->new_sa, TRUE, this->ike_sa);
		this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
	}
	this->ike_init->task.build(&this->ike_init->task, message);

	return NEED_MORE;
}

METHOD(task_t, process_r, status_t,
	private_ike_rekey_t *this, message_t *message)
{
	enumerator_t *enumerator;
	child_sa_t *child_sa;

	if (this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
	{
		DBG1(DBG_IKE, "peer initiated rekeying, but we are deleting");
		return NEED_MORE;
	}

	enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
	while (enumerator->enumerate(enumerator, (void**)&child_sa))
	{
		switch (child_sa->get_state(child_sa))
		{
			case CHILD_CREATED:
			case CHILD_REKEYING:
			case CHILD_RETRYING:
			case CHILD_DELETING:
				/* we do not allow rekeying while we have children in-progress */
				DBG1(DBG_IKE, "peer initiated rekeying, but a child is half-open");
				enumerator->destroy(enumerator);
				return NEED_MORE;
			default:
				break;
		}
	}
	enumerator->destroy(enumerator);

	this->new_sa = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
							this->ike_sa->get_version(this->ike_sa), FALSE);
	if (!this->new_sa)
	{	/* shouldn't happen */
		return FAILED;
	}
	this->new_sa->inherit_pre(this->new_sa, this->ike_sa);
	this->ike_init = ike_init_create(this->new_sa, FALSE, this->ike_sa);
	this->ike_init->task.process(&this->ike_init->task, message);

	return NEED_MORE;
}

METHOD(task_t, build_r, status_t,
	private_ike_rekey_t *this, message_t *message)
{
	if (this->new_sa == NULL)
	{
		/* IKE_SA/a CHILD_SA is in an inacceptable state, deny rekeying */
		message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
		return SUCCESS;
	}

	if (this->ike_init->task.build(&this->ike_init->task, message) == FAILED)
	{
		return SUCCESS;
	}

	this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);

	/* rekeying successful, delete the IKE_SA using a subtask */
	this->ike_delete = ike_delete_create(this->ike_sa, FALSE);
	this->public.task.build = _build_r_delete;
	this->public.task.process = _process_r_delete;

	/* the peer does have to delete the IKE_SA. If it does not, we get a
	 * unusable IKE_SA in REKEYING state without a replacement. We consider
	 * this a timeout condition by the peer, and trigger a delete actively. */
	lib->scheduler->schedule_job(lib->scheduler, (job_t*)
		delete_ike_sa_job_create(this->ike_sa->get_id(this->ike_sa), TRUE), 90);

	return NEED_MORE;
}

METHOD(task_t, process_i, status_t,
	private_ike_rekey_t *this, message_t *message)
{
	if (message->get_notify(message, NO_ADDITIONAL_SAS))
	{
		DBG1(DBG_IKE, "peer seems to not support IKE rekeying, "
			 "starting reauthentication");
		this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
		lib->processor->queue_job(lib->processor,
				(job_t*)rekey_ike_sa_job_create(
							this->ike_sa->get_id(this->ike_sa), TRUE));
		return SUCCESS;
	}

	switch (this->ike_init->task.process(&this->ike_init->task, message))
	{
		case FAILED:
			/* rekeying failed, fallback to old SA */
			if (!(this->collision && (
				this->collision->get_type(this->collision) == TASK_IKE_DELETE ||
				this->collision->get_type(this->collision) == TASK_IKE_REAUTH)))
			{
				job_t *job;
				u_int32_t retry = RETRY_INTERVAL - (random() % RETRY_JITTER);
				job = (job_t*)rekey_ike_sa_job_create(
										this->ike_sa->get_id(this->ike_sa), FALSE);
				DBG1(DBG_IKE, "IKE_SA rekeying failed, "
										"trying again in %d seconds", retry);
				this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
				lib->scheduler->schedule_job(lib->scheduler, job, retry);
			}
			return SUCCESS;
		case NEED_MORE:
			/* bad dh group, try again */
			this->ike_init->task.migrate(&this->ike_init->task, this->new_sa);
			return NEED_MORE;
		default:
			break;
	}

	/* check for collisions */
	if (this->collision &&
		this->collision->get_type(this->collision) == TASK_IKE_REKEY)
	{
		private_ike_rekey_t *other = (private_ike_rekey_t*)this->collision;

		/* ike_init can be NULL, if child_sa is half-open */
		if (other->ike_init)
		{
			host_t *host;
			chunk_t this_nonce, other_nonce;

			this_nonce = this->ike_init->get_lower_nonce(this->ike_init);
			other_nonce = other->ike_init->get_lower_nonce(other->ike_init);

			/* if we have the lower nonce, delete rekeyed SA. If not, delete
			 * the redundant. */
			if (memcmp(this_nonce.ptr, other_nonce.ptr,
						min(this_nonce.len, other_nonce.len)) > 0)
			{
				/* peer should delete this SA. Add a timeout just in case. */
				job_t *job = (job_t*)delete_ike_sa_job_create(
						other->new_sa->get_id(other->new_sa), TRUE);
				lib->scheduler->schedule_job(lib->scheduler, job, 10);
				DBG1(DBG_IKE, "IKE_SA rekey collision won, waiting for delete");
				charon->ike_sa_manager->checkin(charon->ike_sa_manager, other->new_sa);
				other->new_sa = NULL;
			}
			else
			{
				DBG1(DBG_IKE, "IKE_SA rekey collision lost, "
					 "deleting redundant IKE_SA");
				/* apply host for a proper delete */
				host = this->ike_sa->get_my_host(this->ike_sa);
				this->new_sa->set_my_host(this->new_sa, host->clone(host));
				host = this->ike_sa->get_other_host(this->ike_sa);
				this->new_sa->set_other_host(this->new_sa, host->clone(host));
				this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
				this->new_sa->set_state(this->new_sa, IKE_REKEYING);
				if (this->new_sa->delete(this->new_sa) == DESTROY_ME)
				{
					this->new_sa->destroy(this->new_sa);
				}
				else
				{
					charon->ike_sa_manager->checkin(
								charon->ike_sa_manager, this->new_sa);
					/* set threads active IKE_SA after checkin */
					charon->bus->set_sa(charon->bus, this->ike_sa);
				}
				this->new_sa = NULL;
				establish_new(other);
				return SUCCESS;
			}
		}
		/* set threads active IKE_SA after checkin */
		charon->bus->set_sa(charon->bus, this->ike_sa);
	}

	establish_new(this);

	/* rekeying successful, delete the IKE_SA using a subtask */
	this->ike_delete = ike_delete_create(this->ike_sa, TRUE);
	this->public.task.build = _build_i_delete;
	this->public.task.process = _process_i_delete;

	return NEED_MORE;
}

METHOD(task_t, get_type, task_type_t,
	private_ike_rekey_t *this)
{
	return TASK_IKE_REKEY;
}

METHOD(ike_rekey_t, collide, void,
	private_ike_rekey_t* this, task_t *other)
{
	DBG1(DBG_IKE, "detected %N collision with %N", task_type_names,
		 TASK_IKE_REKEY, task_type_names, other->get_type(other));
	DESTROY_IF(this->collision);
	this->collision = other;
}

METHOD(task_t, migrate, void,
	private_ike_rekey_t *this, ike_sa_t *ike_sa)
{
	if (this->ike_init)
	{
		this->ike_init->task.destroy(&this->ike_init->task);
	}
	if (this->ike_delete)
	{
		this->ike_delete->task.destroy(&this->ike_delete->task);
	}
	DESTROY_IF(this->new_sa);
	DESTROY_IF(this->collision);

	this->collision = NULL;
	this->ike_sa = ike_sa;
	this->new_sa = NULL;
	this->ike_init = NULL;
	this->ike_delete = NULL;
}

METHOD(task_t, destroy, void,
	private_ike_rekey_t *this)
{
	if (this->ike_init)
	{
		this->ike_init->task.destroy(&this->ike_init->task);
	}
	if (this->ike_delete)
	{
		this->ike_delete->task.destroy(&this->ike_delete->task);
	}
	DESTROY_IF(this->new_sa);
	DESTROY_IF(this->collision);
	free(this);
}

/*
 * Described in header.
 */
ike_rekey_t *ike_rekey_create(ike_sa_t *ike_sa, bool initiator)
{
	private_ike_rekey_t *this;

	INIT(this,
		.public = {
			.task = {
				.get_type = _get_type,
				.build = _build_r,
				.process = _process_r,
				.migrate = _migrate,
				.destroy = _destroy,
			},
			.collide = _collide,
		},
		.ike_sa = ike_sa,
		.initiator = initiator,
	);
	if (initiator)
	{
		this->public.task.build = _build_i;
		this->public.task.process = _process_i;
	}

	return &this->public;
}