]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/libcharon/sa/ikev1/task_manager_v1.c
aggressive-mode: Trigger alerts for authentication failures
[thirdparty/strongswan.git] / src / libcharon / sa / ikev1 / task_manager_v1.c
CommitLineData
4a09d9ee 1/*
1b4e7fe1 2 * Copyright (C) 2007-2016 Tobias Brunner
68c6863b 3 * Copyright (C) 2007-2011 Martin Willi
208678e6 4 * HSR Hochschule fuer Technik Rapperswil
4a09d9ee
MW
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17#include "task_manager_v1.h"
18
da063ec9
MW
19#include <math.h>
20
4a09d9ee 21#include <daemon.h>
15a682f4 22#include <sa/ikev1/tasks/main_mode.h>
830ab2ae 23#include <sa/ikev1/tasks/aggressive_mode.h>
15a682f4 24#include <sa/ikev1/tasks/quick_mode.h>
83c5fda0 25#include <sa/ikev1/tasks/quick_delete.h>
15a682f4
MW
26#include <sa/ikev1/tasks/xauth.h>
27#include <sa/ikev1/tasks/mode_config.h>
28#include <sa/ikev1/tasks/informational.h>
29#include <sa/ikev1/tasks/isakmp_natd.h>
30#include <sa/ikev1/tasks/isakmp_vendor.h>
31#include <sa/ikev1/tasks/isakmp_cert_pre.h>
32#include <sa/ikev1/tasks/isakmp_cert_post.h>
3ed148b3 33#include <sa/ikev1/tasks/isakmp_delete.h>
3e6b7403
CO
34#include <sa/ikev1/tasks/isakmp_dpd.h>
35
da063ec9 36#include <processing/jobs/retransmit_job.h>
68c6863b 37#include <processing/jobs/delete_ike_sa_job.h>
4b38c22c 38#include <processing/jobs/dpd_timeout_job.h>
30216000
TB
39#include <processing/jobs/process_message_job.h>
40
127a98dc 41#include <collections/array.h>
4a09d9ee 42
fce566a8
MW
43/**
44 * Number of old messages hashes we keep for retransmission.
45 *
46 * In Main Mode, we must ignore messages from a previous message pair if
47 * we already continued to the next. Otherwise a late retransmission
48 * could be considered as a reply to the newer request.
49 */
50#define MAX_OLD_HASHES 2
51
f5a84055
MW
52/**
53 * First sequence number of responding packets.
54 *
55 * To distinguish retransmission jobs for initiating and responding packets,
56 * we split up the sequence counter and use the upper half for responding.
57 */
58#define RESPONDING_SEQ INT_MAX
59
4a09d9ee
MW
60typedef struct exchange_t exchange_t;
61
62/**
63 * An exchange in the air, used do detect and handle retransmission
64 */
65struct exchange_t {
66
67 /**
68 * Message ID used for this transaction
69 */
b12c53ce 70 uint32_t mid;
4a09d9ee
MW
71
72 /**
73 * generated packet for retransmission
74 */
75 packet_t *packet;
76};
77
78typedef struct private_task_manager_t private_task_manager_t;
79
80/**
81 * private data of the task manager
82 */
83struct private_task_manager_t {
84
85 /**
86 * public functions
87 */
88 task_manager_v1_t public;
89
90 /**
91 * associated IKE_SA we are serving
92 */
93 ike_sa_t *ike_sa;
94
73aaf76b
MW
95 /**
96 * RNG to create message IDs
97 */
98 rng_t *rng;
99
4a09d9ee
MW
100 /**
101 * Exchange we are currently handling as responder
102 */
103 struct {
f5a84055
MW
104 /**
105 * Message ID of the last response
106 */
b12c53ce 107 uint32_t mid;
f5a84055 108
9cc8bd4f
MW
109 /**
110 * Hash of a previously received message
111 */
b12c53ce 112 uint32_t hash;
9cc8bd4f 113
4a09d9ee 114 /**
127a98dc 115 * packet(s) for retransmission
4a09d9ee 116 */
127a98dc 117 array_t *packets;
4a09d9ee 118
f5a84055
MW
119 /**
120 * Sequence number of the last sent message
121 */
b12c53ce 122 uint32_t seqnr;
f5a84055
MW
123
124 /**
125 * how many times we have retransmitted so far
126 */
127 u_int retransmitted;
128
4a09d9ee
MW
129 } responding;
130
131 /**
132 * Exchange we are currently handling as initiator
133 */
134 struct {
135 /**
136 * Message ID of the exchange
137 */
b12c53ce 138 uint32_t mid;
4a09d9ee 139
fce566a8
MW
140 /**
141 * Hashes of old responses we can ignore
142 */
b12c53ce 143 uint32_t old_hashes[MAX_OLD_HASHES];
fce566a8
MW
144
145 /**
146 * Position in old hash array
147 */
148 int old_hash_pos;
149
9cc8bd4f 150 /**
751bd02e 151 * Sequence number of the last sent message
9cc8bd4f 152 */
b12c53ce 153 uint32_t seqnr;
9cc8bd4f 154
4a09d9ee
MW
155 /**
156 * how many times we have retransmitted so far
157 */
158 u_int retransmitted;
159
160 /**
127a98dc 161 * packet(s) for retransmission
4a09d9ee 162 */
127a98dc 163 array_t *packets;
4a09d9ee
MW
164
165 /**
30216000 166 * type of the initiated exchange
4a09d9ee
MW
167 */
168 exchange_type_t type;
169
170 } initiating;
171
30216000 172 /**
70e0be07 173 * Message we are currently defragmenting, if any (only one at a time)
30216000 174 */
70e0be07 175 message_t *defrag;
30216000 176
4a09d9ee
MW
177 /**
178 * List of queued tasks not yet in action
179 */
180 linked_list_t *queued_tasks;
181
182 /**
30216000 183 * List of active tasks, initiated by ourselves
4a09d9ee
MW
184 */
185 linked_list_t *active_tasks;
186
187 /**
188 * List of tasks initiated by peer
189 */
190 linked_list_t *passive_tasks;
191
f91b6ac7
MW
192 /**
193 * Queued messages not yet ready to process
194 */
195 message_t *queued;
196
4a09d9ee
MW
197 /**
198 * Number of times we retransmit messages before giving up
199 */
200 u_int retransmit_tries;
201
202 /**
203 * Retransmission timeout
204 */
205 double retransmit_timeout;
206
207 /**
208 * Base to calculate retransmission timeout
209 */
210 double retransmit_base;
3e6b7403 211
389e4b8e
TB
212 /**
213 * Jitter to apply to calculated retransmit timeout (in percent)
214 */
215 u_int retransmit_jitter;
216
217 /**
218 * Limit retransmit timeout to this value
219 */
220 uint32_t retransmit_limit;
221
3e6b7403
CO
222 /**
223 * Sequence number for sending DPD requests
224 */
b12c53ce 225 uint32_t dpd_send;
3e6b7403
CO
226
227 /**
228 * Sequence number for received DPD requests
229 */
b12c53ce 230 uint32_t dpd_recv;
4a09d9ee
MW
231};
232
127a98dc
TB
233/**
234 * Reset retransmission packet list
235 */
236static void clear_packets(array_t *array)
237{
238 packet_t *packet;
239
240 while (array_remove(array, ARRAY_TAIL, &packet))
241 {
242 packet->destroy(packet);
243 }
244}
245
a5c79960
MW
246METHOD(task_manager_t, flush_queue, void,
247 private_task_manager_t *this, task_queue_t queue)
0f61964e 248{
a5c79960 249 linked_list_t *list;
0f61964e
MW
250 task_t *task;
251
429d95fe
MW
252 if (this->queued)
253 {
254 this->queued->destroy(this->queued);
255 this->queued = NULL;
256 }
a5c79960
MW
257 switch (queue)
258 {
259 case TASK_QUEUE_ACTIVE:
260 list = this->active_tasks;
17949695
MW
261 /* cancel pending retransmits */
262 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
127a98dc 263 clear_packets(this->initiating.packets);
a5c79960
MW
264 break;
265 case TASK_QUEUE_PASSIVE:
266 list = this->passive_tasks;
267 break;
268 case TASK_QUEUE_QUEUED:
269 list = this->queued_tasks;
270 break;
271 default:
272 return;
273 }
0f61964e
MW
274 while (list->remove_last(list, (void**)&task) == SUCCESS)
275 {
276 task->destroy(task);
277 }
278}
279
b1908994
TE
280METHOD(task_manager_t, flush, void,
281 private_task_manager_t *this)
4a09d9ee 282{
a5c79960
MW
283 flush_queue(this, TASK_QUEUE_QUEUED);
284 flush_queue(this, TASK_QUEUE_PASSIVE);
285 flush_queue(this, TASK_QUEUE_ACTIVE);
4a09d9ee
MW
286}
287
26b55dc6
MW
288/**
289 * move a task of a specific type from the queue to the active list
290 */
291static bool activate_task(private_task_manager_t *this, task_type_t type)
292{
293 enumerator_t *enumerator;
294 task_t *task;
295 bool found = FALSE;
296
297 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
298 while (enumerator->enumerate(enumerator, (void**)&task))
299 {
300 if (task->get_type(task) == type)
301 {
302 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
303 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
304 this->active_tasks->insert_last(this->active_tasks, task);
305 found = TRUE;
306 break;
307 }
308 }
309 enumerator->destroy(enumerator);
310 return found;
311}
312
5cafefcd 313/**
127a98dc 314 * Send packets in the given array (they get cloned)
5cafefcd 315 */
127a98dc 316static void send_packets(private_task_manager_t *this, array_t *packets)
5cafefcd 317{
127a98dc 318 enumerator_t *enumerator;
5cafefcd 319 packet_t *packet;
5cafefcd 320
127a98dc
TB
321 enumerator = array_create_enumerator(packets);
322 while (enumerator->enumerate(enumerator, &packet))
5cafefcd 323 {
127a98dc 324 charon->sender->send(charon->sender, packet->clone(packet));
5cafefcd 325 }
127a98dc 326 enumerator->destroy(enumerator);
5cafefcd
TB
327}
328
329/**
127a98dc 330 * Generates the given message and stores packet(s) in the given array
5cafefcd 331 */
127a98dc
TB
332static bool generate_message(private_task_manager_t *this, message_t *message,
333 array_t **packets)
5cafefcd 334{
127a98dc 335 enumerator_t *fragments;
40bab9a1 336 packet_t *fragment;
127a98dc 337
40bab9a1
TB
338 if (this->ike_sa->generate_message_fragmented(this->ike_sa, message,
339 &fragments) != SUCCESS)
127a98dc 340 {
127a98dc
TB
341 return FALSE;
342 }
40bab9a1 343 while (fragments->enumerate(fragments, &fragment))
127a98dc 344 {
40bab9a1 345 array_insert_create(packets, ARRAY_TAIL, fragment);
5cafefcd 346 }
127a98dc 347 fragments->destroy(fragments);
40bab9a1 348 return TRUE;
5cafefcd
TB
349}
350
f5a84055 351/**
127a98dc 352 * Retransmit a packet (or its fragments)
f5a84055 353 */
b12c53ce 354static status_t retransmit_packet(private_task_manager_t *this, uint32_t seqnr,
127a98dc 355 u_int mid, u_int retransmitted, array_t *packets)
23f4e4b4 356{
127a98dc 357 packet_t *packet;
389e4b8e 358 uint32_t t, max_jitter;
23f4e4b4 359
127a98dc 360 array_get(packets, 0, &packet);
f5a84055
MW
361 if (retransmitted > this->retransmit_tries)
362 {
363 DBG1(DBG_IKE, "giving up after %u retransmits", retransmitted - 1);
3c79b7b7 364 charon->bus->alert(charon->bus, ALERT_RETRANSMIT_SEND_TIMEOUT, packet);
f5a84055
MW
365 return DESTROY_ME;
366 }
b12c53ce 367 t = (uint32_t)(this->retransmit_timeout * 1000.0 *
f5a84055 368 pow(this->retransmit_base, retransmitted));
00d54711
TB
369 if (this->retransmit_limit)
370 {
371 t = min(t, this->retransmit_limit);
372 }
389e4b8e
TB
373 if (this->retransmit_jitter)
374 {
375 max_jitter = (t / 100.0) * this->retransmit_jitter;
376 t -= max_jitter * (random() / (RAND_MAX + 1.0));
377 }
f5a84055
MW
378 if (retransmitted)
379 {
380 DBG1(DBG_IKE, "sending retransmit %u of %s message ID %u, seq %u",
381 retransmitted, seqnr < RESPONDING_SEQ ? "request" : "response",
382 mid, seqnr < RESPONDING_SEQ ? seqnr : seqnr - RESPONDING_SEQ);
9da65eae
TE
383 charon->bus->alert(charon->bus, ALERT_RETRANSMIT_SEND, packet,
384 retransmitted);
f5a84055 385 }
127a98dc 386 send_packets(this, packets);
f5a84055
MW
387 lib->scheduler->schedule_job_ms(lib->scheduler, (job_t*)
388 retransmit_job_create(seqnr, this->ike_sa->get_id(this->ike_sa)), t);
8c50bb6c 389 return SUCCESS;
f5a84055
MW
390}
391
392METHOD(task_manager_t, retransmit, status_t,
b12c53ce 393 private_task_manager_t *this, uint32_t seqnr)
f5a84055
MW
394{
395 status_t status = SUCCESS;
396
127a98dc
TB
397 if (seqnr == this->initiating.seqnr &&
398 array_count(this->initiating.packets))
f5a84055 399 {
127a98dc
TB
400 status = retransmit_packet(this, seqnr, this->initiating.mid,
401 this->initiating.retransmitted, this->initiating.packets);
8c50bb6c 402 if (status == SUCCESS)
23f4e4b4 403 {
f5a84055 404 this->initiating.retransmitted++;
23f4e4b4 405 }
f5a84055 406 }
127a98dc
TB
407 if (seqnr == this->responding.seqnr &&
408 array_count(this->responding.packets))
f5a84055 409 {
127a98dc
TB
410 status = retransmit_packet(this, seqnr, this->responding.mid,
411 this->responding.retransmitted, this->responding.packets);
8c50bb6c 412 if (status == SUCCESS)
23f4e4b4 413 {
f5a84055 414 this->responding.retransmitted++;
23f4e4b4 415 }
23f4e4b4 416 }
f5a84055 417 return status;
23f4e4b4
CO
418}
419
27137790
MW
420/**
421 * Check if we have to wait for a mode config before starting a quick mode
422 */
423static bool mode_config_expected(private_task_manager_t *this)
424{
101d26ba 425 enumerator_t *enumerator;
27137790 426 peer_cfg_t *peer_cfg;
497ce2cf 427 char *pool;
e3311e9b 428 bool local;
101d26ba 429 host_t *host;
27137790
MW
430
431 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
497ce2cf 432 if (peer_cfg)
27137790 433 {
e3311e9b
MW
434 if (peer_cfg->use_pull_mode(peer_cfg))
435 {
436 enumerator = peer_cfg->create_pool_enumerator(peer_cfg);
437 if (!enumerator->enumerate(enumerator, &pool))
438 { /* no pool configured */
439 enumerator->destroy(enumerator);
440 return FALSE;
441 }
497ce2cf 442 enumerator->destroy(enumerator);
e3311e9b
MW
443
444 local = FALSE;
497ce2cf 445 }
e3311e9b
MW
446 else
447 {
448 enumerator = peer_cfg->create_virtual_ip_enumerator(peer_cfg);
449 if (!enumerator->enumerate(enumerator, &host))
450 { /* not requesting a vip */
451 enumerator->destroy(enumerator);
452 return FALSE;
453 }
454 enumerator->destroy(enumerator);
497ce2cf 455
e3311e9b
MW
456 local = TRUE;
457 }
101d26ba 458 enumerator = this->ike_sa->create_virtual_ip_enumerator(this->ike_sa,
e3311e9b 459 local);
101d26ba 460 if (!enumerator->enumerate(enumerator, &host))
e3311e9b 461 { /* expecting a VIP exchange, but no VIP assigned yet */
101d26ba 462 enumerator->destroy(enumerator);
27137790
MW
463 return TRUE;
464 }
101d26ba 465 enumerator->destroy(enumerator);
27137790
MW
466 }
467 return FALSE;
468}
469
4a09d9ee
MW
470METHOD(task_manager_t, initiate, status_t,
471 private_task_manager_t *this)
472{
26b55dc6
MW
473 enumerator_t *enumerator;
474 task_t *task;
475 message_t *message;
476 host_t *me, *other;
73aaf76b 477 exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED;
7ce504e1 478 bool new_mid = FALSE, expect_response = FALSE, cancelled = FALSE, keep = FALSE;
73aaf76b 479
477559ca
MW
480 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED &&
481 this->initiating.type != INFORMATIONAL_V1)
26b55dc6
MW
482 {
483 DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
484 exchange_type_names, this->initiating.type);
485 /* do not initiate if we already have a message in the air */
486 return SUCCESS;
487 }
488
489 if (this->active_tasks->get_count(this->active_tasks) == 0)
490 {
491 DBG2(DBG_IKE, "activating new tasks");
492 switch (this->ike_sa->get_state(this->ike_sa))
493 {
494 case IKE_CREATED:
2e3c9f87 495 activate_task(this, TASK_ISAKMP_VENDOR);
824dc0ad 496 activate_task(this, TASK_ISAKMP_CERT_PRE);
a09972df 497 if (activate_task(this, TASK_MAIN_MODE))
26b55dc6
MW
498 {
499 exchange = ID_PROT;
500 }
830ab2ae
MW
501 else if (activate_task(this, TASK_AGGRESSIVE_MODE))
502 {
503 exchange = AGGRESSIVE;
504 }
505 activate_task(this, TASK_ISAKMP_CERT_POST);
506 activate_task(this, TASK_ISAKMP_NATD);
26b55dc6 507 break;
c5dc9d33 508 case IKE_CONNECTING:
429d95fe
MW
509 if (activate_task(this, TASK_ISAKMP_DELETE))
510 {
511 exchange = INFORMATIONAL_V1;
512 new_mid = TRUE;
513 break;
514 }
69adeb5b 515 if (activate_task(this, TASK_XAUTH))
744c0801 516 {
c5dc9d33 517 exchange = TRANSACTION;
3e246c48 518 new_mid = TRUE;
429d95fe 519 break;
744c0801 520 }
accf4612
MW
521 if (activate_task(this, TASK_INFORMATIONAL))
522 {
523 exchange = INFORMATIONAL_V1;
524 new_mid = TRUE;
429d95fe 525 break;
accf4612 526 }
c5dc9d33
CO
527 break;
528 case IKE_ESTABLISHED:
156b8662
MW
529 if (activate_task(this, TASK_MODE_CONFIG))
530 {
531 exchange = TRANSACTION;
532 new_mid = TRUE;
533 break;
534 }
2e06179f 535 if (activate_task(this, TASK_QUICK_DELETE))
23f4e4b4 536 {
612fe541 537 exchange = INFORMATIONAL_V1;
3e246c48
MW
538 new_mid = TRUE;
539 break;
23f4e4b4 540 }
2e06179f 541 if (activate_task(this, TASK_ISAKMP_DELETE))
accf4612
MW
542 {
543 exchange = INFORMATIONAL_V1;
544 new_mid = TRUE;
429d95fe 545 break;
accf4612 546 }
612fe541
TB
547 if (!mode_config_expected(this) &&
548 activate_task(this, TASK_QUICK_MODE))
8ef2bae4 549 {
612fe541 550 exchange = QUICK_MODE;
8ef2bae4 551 new_mid = TRUE;
429d95fe 552 break;
8ef2bae4 553 }
612fe541 554 if (activate_task(this, TASK_INFORMATIONAL))
8ef2bae4
MW
555 {
556 exchange = INFORMATIONAL_V1;
557 new_mid = TRUE;
429d95fe 558 break;
8ef2bae4 559 }
3e6b7403
CO
560 if (activate_task(this, TASK_ISAKMP_DPD))
561 {
562 exchange = INFORMATIONAL_V1;
563 new_mid = TRUE;
564 break;
565 }
744c0801 566 break;
b56b8502
TB
567 case IKE_REKEYING:
568 if (activate_task(this, TASK_ISAKMP_DELETE))
569 {
570 exchange = INFORMATIONAL_V1;
571 new_mid = TRUE;
572 break;
573 }
f15c85a4
TB
574 if (activate_task(this, TASK_ISAKMP_DPD))
575 {
576 exchange = INFORMATIONAL_V1;
577 new_mid = TRUE;
578 break;
579 }
b56b8502 580 break;
26b55dc6
MW
581 default:
582 break;
583 }
584 }
585 else
586 {
587 DBG2(DBG_IKE, "reinitiating already active tasks");
588 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
589 while (enumerator->enumerate(enumerator, (void**)&task))
590 {
591 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
592 switch (task->get_type(task))
593 {
a09972df 594 case TASK_MAIN_MODE:
26b55dc6
MW
595 exchange = ID_PROT;
596 break;
830ab2ae
MW
597 case TASK_AGGRESSIVE_MODE:
598 exchange = AGGRESSIVE;
599 break;
744c0801
MW
600 case TASK_QUICK_MODE:
601 exchange = QUICK_MODE;
602 break;
69adeb5b 603 case TASK_XAUTH:
52ac2ceb
CO
604 exchange = TRANSACTION;
605 new_mid = TRUE;
606 break;
26b55dc6
MW
607 default:
608 continue;
609 }
610 break;
611 }
612 enumerator->destroy(enumerator);
613 }
614
73aaf76b 615 if (exchange == EXCHANGE_TYPE_UNDEFINED)
26b55dc6
MW
616 {
617 DBG2(DBG_IKE, "nothing to initiate");
618 /* nothing to do yet... */
619 return SUCCESS;
620 }
621
622 me = this->ike_sa->get_my_host(this->ike_sa);
623 other = this->ike_sa->get_other_host(this->ike_sa);
624
3e246c48 625 if (new_mid)
26b55dc6 626 {
1bb9c51e
TB
627 if (!this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
628 (void*)&this->initiating.mid))
629 {
630 DBG1(DBG_IKE, "failed to allocate message ID, destroying IKE_SA");
631 flush(this);
632 return DESTROY_ME;
633 }
26b55dc6 634 }
1bb9c51e 635 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
3e246c48 636 message->set_message_id(message, this->initiating.mid);
26b55dc6
MW
637 message->set_source(message, me->clone(me));
638 message->set_destination(message, other->clone(other));
639 message->set_exchange_type(message, exchange);
640 this->initiating.type = exchange;
641 this->initiating.retransmitted = 0;
642
643 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
644 while (enumerator->enumerate(enumerator, (void*)&task))
645 {
646 switch (task->build(task, message))
647 {
648 case SUCCESS:
649 /* task completed, remove it */
650 this->active_tasks->remove_at(this->active_tasks, enumerator);
182d55b2
MW
651 if (task->get_type(task) == TASK_AGGRESSIVE_MODE ||
652 task->get_type(task) == TASK_QUICK_MODE)
1b82eb23
MW
653 { /* last message of three message exchange */
654 keep = TRUE;
655 }
26b55dc6 656 task->destroy(task);
590ca1d4 657 continue;
26b55dc6 658 case NEED_MORE:
751bd02e 659 expect_response = TRUE;
26b55dc6 660 /* processed, but task needs another exchange */
590ca1d4
MW
661 continue;
662 case ALREADY_DONE:
7ce504e1 663 cancelled = TRUE;
26b55dc6
MW
664 break;
665 case FAILED:
666 default:
667 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
668 {
669 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
670 }
671 /* FALL */
672 case DESTROY_ME:
673 /* critical failure, destroy IKE_SA */
674 enumerator->destroy(enumerator);
675 message->destroy(message);
676 flush(this);
677 return DESTROY_ME;
678 }
590ca1d4 679 break;
26b55dc6
MW
680 }
681 enumerator->destroy(enumerator);
682
1b82eb23
MW
683 if (this->active_tasks->get_count(this->active_tasks) == 0 &&
684 (exchange == QUICK_MODE || exchange == AGGRESSIVE))
41fbde45
MW
685 { /* tasks completed, no exchange active anymore */
686 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
687 }
7ce504e1 688 if (cancelled)
b64d6423
MW
689 {
690 message->destroy(message);
691 return initiate(this);
692 }
26b55dc6 693
127a98dc
TB
694 clear_packets(this->initiating.packets);
695 if (!generate_message(this, message, &this->initiating.packets))
26b55dc6
MW
696 {
697 /* message generation failed. There is nothing more to do than to
698 * close the SA */
699 message->destroy(message);
700 flush(this);
701 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
702 return DESTROY_ME;
703 }
26b55dc6 704
f5a84055 705 this->initiating.seqnr++;
3babde90 706 if (expect_response)
751bd02e 707 {
f5a84055 708 message->destroy(message);
751bd02e
CO
709 return retransmit(this, this->initiating.seqnr);
710 }
8c50bb6c
TE
711 send_packets(this, this->initiating.packets);
712 if (!keep)
f5a84055 713 {
127a98dc 714 clear_packets(this->initiating.packets);
f5a84055
MW
715 }
716 message->destroy(message);
46505067 717
07095794 718 if (exchange == INFORMATIONAL_V1)
46505067 719 {
07095794
MW
720 switch (this->ike_sa->get_state(this->ike_sa))
721 {
722 case IKE_CONNECTING:
723 /* close after sending an INFORMATIONAL when unestablished */
724 return FAILED;
725 case IKE_DELETING:
726 /* close after sending a DELETE */
727 return DESTROY_ME;
728 default:
729 break;
730 }
46505067 731 }
daee47ba 732 return initiate(this);
4a09d9ee
MW
733}
734
4a09d9ee
MW
735/**
736 * build a response depending on the "passive" task list
737 */
738static status_t build_response(private_task_manager_t *this, message_t *request)
739{
740 enumerator_t *enumerator;
741 task_t *task;
742 message_t *message;
743 host_t *me, *other;
7ce504e1 744 bool delete = FALSE, cancelled = FALSE, expect_request = FALSE;
4a09d9ee
MW
745
746 me = request->get_destination(request);
747 other = request->get_source(request);
748
749 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
750 message->set_exchange_type(message, request->get_exchange_type(request));
751 /* send response along the path the request came in */
752 message->set_source(message, me->clone(me));
753 message->set_destination(message, other->clone(other));
1b82eb23 754 message->set_message_id(message, request->get_message_id(request));
4a09d9ee
MW
755 message->set_request(message, FALSE);
756
f5a84055
MW
757 this->responding.mid = request->get_message_id(request);
758 this->responding.retransmitted = 0;
759 this->responding.seqnr++;
760
4a09d9ee
MW
761 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
762 while (enumerator->enumerate(enumerator, (void*)&task))
763 {
764 switch (task->build(task, message))
765 {
766 case SUCCESS:
767 /* task completed, remove it */
768 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
f5a84055 769 task->destroy(task);
590ca1d4 770 continue;
4a09d9ee
MW
771 case NEED_MORE:
772 /* processed, but task needs another exchange */
346dad30
MW
773 if (task->get_type(task) == TASK_QUICK_MODE ||
774 task->get_type(task) == TASK_AGGRESSIVE_MODE)
f5a84055
MW
775 { /* we rely on initiator retransmission, except for
776 * three-message exchanges */
777 expect_request = TRUE;
4a09d9ee 778 }
590ca1d4
MW
779 continue;
780 case ALREADY_DONE:
7ce504e1 781 cancelled = TRUE;
4a09d9ee 782 break;
37a22a16
TB
783 case INVALID_ARG:
784 if (task->get_type(task) == TASK_QUICK_MODE)
785 { /* not responsible for this exchange */
786 continue;
787 }
788 /* FALL */
4a09d9ee
MW
789 case FAILED:
790 default:
791 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
792 /* FALL */
793 case DESTROY_ME:
794 /* destroy IKE_SA, but SEND response first */
795 delete = TRUE;
796 break;
797 }
590ca1d4 798 break;
4a09d9ee
MW
799 }
800 enumerator->destroy(enumerator);
801
127a98dc 802 clear_packets(this->responding.packets);
7ce504e1 803 if (cancelled)
b64d6423
MW
804 {
805 message->destroy(message);
806 return initiate(this);
807 }
127a98dc 808 if (!generate_message(this, message, &this->responding.packets))
4a09d9ee 809 {
127a98dc 810 message->destroy(message);
4a09d9ee
MW
811 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
812 return DESTROY_ME;
813 }
127a98dc 814 message->destroy(message);
4a09d9ee 815
f5a84055
MW
816 if (expect_request && !delete)
817 {
818 return retransmit(this, this->responding.seqnr);
819 }
127a98dc 820 send_packets(this, this->responding.packets);
4a09d9ee
MW
821 if (delete)
822 {
823 return DESTROY_ME;
824 }
825 return SUCCESS;
826}
827
fbbd439b
CO
828/**
829 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
e647c98a 830 * The notify protocol_id is set to ISAKMP
fbbd439b 831 */
b4705269
MW
832static void send_notify(private_task_manager_t *this, message_t *request,
833 notify_type_t type)
fbbd439b
CO
834{
835 message_t *response;
127a98dc 836 array_t *packets = NULL;
fbbd439b 837 host_t *me, *other;
b12c53ce 838 uint32_t mid;
fbbd439b 839
3973ffa4 840 if (request->get_exchange_type(request) == INFORMATIONAL_V1)
fbbd439b
CO
841 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
842 DBG1(DBG_IKE, "ignore malformed INFORMATIONAL request");
843 return;
844 }
1bb9c51e
TB
845 if (!this->rng->get_bytes(this->rng, sizeof(mid), (void*)&mid))
846 {
847 DBG1(DBG_IKE, "failed to allocate message ID");
848 return;
849 }
fbbd439b
CO
850 response = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
851 response->set_exchange_type(response, INFORMATIONAL_V1);
852 response->set_request(response, TRUE);
fbbd439b 853 response->set_message_id(response, mid);
b4705269 854 response->add_payload(response, (payload_t*)
3ecfc83c 855 notify_payload_create_from_protocol_and_type(PLV1_NOTIFY,
b4705269 856 PROTO_IKE, type));
fbbd439b
CO
857
858 me = this->ike_sa->get_my_host(this->ike_sa);
859 if (me->is_anyaddr(me))
860 {
861 me = request->get_destination(request);
862 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
863 }
864 other = this->ike_sa->get_other_host(this->ike_sa);
865 if (other->is_anyaddr(other))
866 {
867 other = request->get_source(request);
868 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
869 }
870 response->set_source(response, me->clone(me));
871 response->set_destination(response, other->clone(other));
127a98dc 872 if (generate_message(this, response, &packets))
fbbd439b 873 {
127a98dc 874 send_packets(this, packets);
fbbd439b 875 }
127a98dc
TB
876 clear_packets(packets);
877 array_destroy(packets);
fbbd439b
CO
878 response->destroy(response);
879}
880
24742c0f
MW
881/**
882 * Process a DPD request/response
883 */
884static bool process_dpd(private_task_manager_t *this, message_t *message)
885{
886 notify_payload_t *notify;
887 notify_type_t type;
b12c53ce 888 uint32_t seq;
24742c0f
MW
889 chunk_t data;
890
891 type = DPD_R_U_THERE;
892 notify = message->get_notify(message, type);
893 if (!notify)
894 {
895 type = DPD_R_U_THERE_ACK;
896 notify = message->get_notify(message, type);
897 }
898 if (!notify)
899 {
900 return FALSE;
901 }
902 data = notify->get_notification_data(notify);
903 if (data.len != 4)
904 {
905 return FALSE;
906 }
907 seq = untoh32(data.ptr);
908
909 if (type == DPD_R_U_THERE)
910 {
911 if (this->dpd_recv == 0 || seq == this->dpd_recv)
912 { /* check sequence validity */
913 this->dpd_recv = seq + 1;
914 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
915 time_monotonic(NULL));
916 }
917 /* but respond anyway */
918 this->ike_sa->queue_task(this->ike_sa,
919 &isakmp_dpd_create(this->ike_sa, DPD_R_U_THERE_ACK, seq)->task);
920 }
921 else /* DPD_R_U_THERE_ACK */
922 {
923 if (seq == this->dpd_send - 1)
924 {
925 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
926 time_monotonic(NULL));
927 }
928 else
929 {
930 DBG1(DBG_IKE, "received invalid DPD sequence number %u "
931 "(expected %u), ignored", seq, this->dpd_send - 1);
932 }
933 }
934 return TRUE;
935}
936
4de361d9
TB
937/**
938 * Check if we already have a quick mode task queued for the exchange with the
939 * given message ID
940 */
b12c53ce 941static bool have_quick_mode_task(private_task_manager_t *this, uint32_t mid)
4de361d9
TB
942{
943 enumerator_t *enumerator;
944 quick_mode_t *qm;
945 task_t *task;
946 bool found = FALSE;
947
948 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
949 while (enumerator->enumerate(enumerator, &task))
950 {
951 if (task->get_type(task) == TASK_QUICK_MODE)
952 {
953 qm = (quick_mode_t*)task;
954 if (qm->get_mid(qm) == mid)
955 {
956 found = TRUE;
957 break;
958 }
959 }
960 }
961 enumerator->destroy(enumerator);
962 return found;
963}
964
b0b0c913 965/**
1b4e7fe1 966 * Check if we still have a specific task queued
b0b0c913 967 */
1b4e7fe1 968static bool have_task_queued(private_task_manager_t *this, task_type_t type)
b0b0c913
TB
969{
970 enumerator_t *enumerator;
971 task_t *task;
972 bool found = FALSE;
973
974 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
975 while (enumerator->enumerate(enumerator, &task))
976 {
1b4e7fe1 977 if (task->get_type(task) == type)
b0b0c913
TB
978 {
979 found = TRUE;
980 break;
981 }
982 }
983 enumerator->destroy(enumerator);
984 return found;
985}
986
4a09d9ee
MW
987/**
988 * handle an incoming request message
989 */
990static status_t process_request(private_task_manager_t *this,
991 message_t *message)
992{
993 enumerator_t *enumerator;
994 task_t *task = NULL;
ff6b084a 995 bool send_response = FALSE, dpd = FALSE;
4a09d9ee 996
9276f712 997 if (message->get_exchange_type(message) == INFORMATIONAL_V1 ||
4de361d9 998 message->get_exchange_type(message) == QUICK_MODE ||
9276f712 999 this->passive_tasks->get_count(this->passive_tasks) == 0)
4a09d9ee
MW
1000 { /* create tasks depending on request type, if not already some queued */
1001 switch (message->get_exchange_type(message))
1002 {
1003 case ID_PROT:
2e3c9f87 1004 task = (task_t *)isakmp_vendor_create(this->ike_sa, FALSE);
01685247 1005 this->passive_tasks->insert_last(this->passive_tasks, task);
824dc0ad 1006 task = (task_t*)isakmp_cert_pre_create(this->ike_sa, FALSE);
8ad5cd1f 1007 this->passive_tasks->insert_last(this->passive_tasks, task);
c73c832c
MW
1008 task = (task_t *)main_mode_create(this->ike_sa, FALSE);
1009 this->passive_tasks->insert_last(this->passive_tasks, task);
0aa2af5e 1010 task = (task_t*)isakmp_cert_post_create(this->ike_sa, FALSE);
c64a4b4f 1011 this->passive_tasks->insert_last(this->passive_tasks, task);
79d6fc7f 1012 task = (task_t *)isakmp_natd_create(this->ike_sa, FALSE);
1cc4ec46 1013 this->passive_tasks->insert_last(this->passive_tasks, task);
4a09d9ee
MW
1014 break;
1015 case AGGRESSIVE:
830ab2ae
MW
1016 task = (task_t *)isakmp_vendor_create(this->ike_sa, FALSE);
1017 this->passive_tasks->insert_last(this->passive_tasks, task);
1018 task = (task_t*)isakmp_cert_pre_create(this->ike_sa, FALSE);
1019 this->passive_tasks->insert_last(this->passive_tasks, task);
1020 task = (task_t *)aggressive_mode_create(this->ike_sa, FALSE);
1021 this->passive_tasks->insert_last(this->passive_tasks, task);
1022 task = (task_t*)isakmp_cert_post_create(this->ike_sa, FALSE);
1023 this->passive_tasks->insert_last(this->passive_tasks, task);
1024 task = (task_t *)isakmp_natd_create(this->ike_sa, FALSE);
1025 this->passive_tasks->insert_last(this->passive_tasks, task);
1026 break;
4a09d9ee 1027 case QUICK_MODE:
a22b9e4f
MW
1028 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
1029 {
1030 DBG1(DBG_IKE, "received quick mode request for "
1031 "unestablished IKE_SA, ignored");
1032 return FAILED;
1033 }
4de361d9
TB
1034 if (have_quick_mode_task(this, message->get_message_id(message)))
1035 {
1036 break;
1037 }
744c0801
MW
1038 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
1039 NULL, NULL);
1040 this->passive_tasks->insert_last(this->passive_tasks, task);
1041 break;
4a09d9ee 1042 case INFORMATIONAL_V1:
24742c0f 1043 if (process_dpd(this, message))
2ddd45c9 1044 {
ff6b084a 1045 dpd = TRUE;
2ddd45c9
MW
1046 }
1047 else
1048 {
1049 task = (task_t *)informational_create(this->ike_sa, NULL);
24742c0f 1050 this->passive_tasks->insert_first(this->passive_tasks, task);
2ddd45c9 1051 }
07abb470 1052 break;
c5dc9d33 1053 case TRANSACTION:
764035d5 1054 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
156b8662 1055 {
e3311e9b
MW
1056 task = (task_t *)mode_config_create(this->ike_sa,
1057 FALSE, TRUE);
156b8662
MW
1058 }
1059 else
1060 {
1061 task = (task_t *)xauth_create(this->ike_sa, FALSE);
1062 }
c5dc9d33
CO
1063 this->passive_tasks->insert_last(this->passive_tasks, task);
1064 break;
4a09d9ee
MW
1065 default:
1066 return FAILED;
1067 }
1068 }
24742c0f 1069 if (dpd)
ff6b084a 1070 {
24742c0f 1071 return initiate(this);
ff6b084a 1072 }
24742c0f
MW
1073 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND, time_monotonic(NULL));
1074
4a09d9ee
MW
1075 /* let the tasks process the message */
1076 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
1077 while (enumerator->enumerate(enumerator, (void*)&task))
1078 {
1079 switch (task->process(task, message))
1080 {
1081 case SUCCESS:
1082 /* task completed, remove it */
1083 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
1084 task->destroy(task);
590ca1d4 1085 continue;
4a09d9ee
MW
1086 case NEED_MORE:
1087 /* processed, but task needs at least another call to build() */
8cb6f4f9 1088 send_response = TRUE;
590ca1d4
MW
1089 continue;
1090 case ALREADY_DONE:
1091 send_response = FALSE;
4a09d9ee 1092 break;
37a22a16
TB
1093 case INVALID_ARG:
1094 if (task->get_type(task) == TASK_QUICK_MODE)
1095 { /* not responsible for this exchange */
1096 continue;
1097 }
1098 /* FALL */
4a09d9ee
MW
1099 case FAILED:
1100 default:
1101 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
1102 /* FALL */
1103 case DESTROY_ME:
1104 /* critical failure, destroy IKE_SA */
1105 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
1106 enumerator->destroy(enumerator);
1107 task->destroy(task);
1108 return DESTROY_ME;
1109 }
590ca1d4 1110 break;
4a09d9ee
MW
1111 }
1112 enumerator->destroy(enumerator);
1113
8cb6f4f9
TB
1114 if (send_response)
1115 {
69adeb5b
MW
1116 if (build_response(this, message) != SUCCESS)
1117 {
1118 return DESTROY_ME;
1119 }
1120 }
7b34de45
MW
1121 else
1122 { /* We don't send a response, so don't retransmit one if we get
1123 * the same message again. */
127a98dc 1124 clear_packets(this->responding.packets);
7b34de45 1125 }
25863fe1
TB
1126 if (this->queued &&
1127 this->queued->get_exchange_type(this->queued) == INFORMATIONAL_V1)
1128 {
1129 message_t *queued;
1130 status_t status;
1131
1132 queued = this->queued;
1133 this->queued = NULL;
1134 status = this->public.task_manager.process_message(
1135 &this->public.task_manager, queued);
1136 queued->destroy(queued);
1137 if (status == DESTROY_ME)
1138 {
1139 return status;
1140 }
1141 }
69adeb5b
MW
1142 if (this->passive_tasks->get_count(this->passive_tasks) == 0 &&
1143 this->queued_tasks->get_count(this->queued_tasks) > 0)
1144 {
1145 /* passive tasks completed, check if an active task has been queued,
1146 * such as XAUTH or modeconfig push */
1147 return initiate(this);
8cb6f4f9
TB
1148 }
1149 return SUCCESS;
4a09d9ee
MW
1150}
1151
26b55dc6
MW
1152/**
1153 * handle an incoming response message
1154 */
1155static status_t process_response(private_task_manager_t *this,
1156 message_t *message)
1157{
1158 enumerator_t *enumerator;
4e98ca18 1159 message_t *queued;
f91b6ac7 1160 status_t status;
26b55dc6
MW
1161 task_t *task;
1162
1163 if (message->get_exchange_type(message) != this->initiating.type)
1164 {
0235914d
MW
1165 /* Windows server sends a fourth quick mode message having an initial
1166 * contact notify. Ignore this message for compatibility. */
1167 if (this->initiating.type == EXCHANGE_TYPE_UNDEFINED &&
1168 message->get_exchange_type(message) == QUICK_MODE &&
1169 message->get_notify(message, INITIAL_CONTACT))
1170 {
1171 DBG1(DBG_IKE, "ignoring fourth Quick Mode message");
1172 return SUCCESS;
1173 }
26b55dc6
MW
1174 DBG1(DBG_IKE, "received %N response, but expected %N",
1175 exchange_type_names, message->get_exchange_type(message),
1176 exchange_type_names, this->initiating.type);
1177 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
1178 return DESTROY_ME;
1179 }
1180
1181 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
1182 while (enumerator->enumerate(enumerator, (void*)&task))
1183 {
1184 switch (task->process(task, message))
1185 {
1186 case SUCCESS:
1187 /* task completed, remove it */
1188 this->active_tasks->remove_at(this->active_tasks, enumerator);
1189 task->destroy(task);
590ca1d4 1190 continue;
26b55dc6
MW
1191 case NEED_MORE:
1192 /* processed, but task needs another exchange */
590ca1d4
MW
1193 continue;
1194 case ALREADY_DONE:
26b55dc6
MW
1195 break;
1196 case FAILED:
1197 default:
1198 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
1199 /* FALL */
1200 case DESTROY_ME:
1201 /* critical failure, destroy IKE_SA */
1202 this->active_tasks->remove_at(this->active_tasks, enumerator);
1203 enumerator->destroy(enumerator);
1204 task->destroy(task);
1205 return DESTROY_ME;
1206 }
590ca1d4 1207 break;
26b55dc6
MW
1208 }
1209 enumerator->destroy(enumerator);
1210
0ddda7ca 1211 if (this->initiating.retransmitted > 1)
bd71ba0f
TB
1212 {
1213 packet_t *packet = NULL;
1214 array_get(this->initiating.packets, 0, &packet);
1215 charon->bus->alert(charon->bus, ALERT_RETRANSMIT_SEND_CLEARED, packet);
1216 }
26b55dc6 1217 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
127a98dc 1218 clear_packets(this->initiating.packets);
26b55dc6 1219
3805880c
TB
1220 if (this->queued && !this->active_tasks->get_count(this->active_tasks) &&
1221 this->queued->get_exchange_type(this->queued) == TRANSACTION)
f91b6ac7 1222 {
4e98ca18 1223 queued = this->queued;
f91b6ac7 1224 this->queued = NULL;
4e98ca18
MW
1225 status = this->public.task_manager.process_message(
1226 &this->public.task_manager, queued);
1227 queued->destroy(queued);
f91b6ac7
MW
1228 if (status == DESTROY_ME)
1229 {
1230 return status;
1231 }
1232 }
1233
26b55dc6
MW
1234 return initiate(this);
1235}
1236
30216000
TB
1237static status_t handle_fragment(private_task_manager_t *this, message_t *msg)
1238{
70e0be07 1239 status_t status;
30216000 1240
70e0be07 1241 if (!this->defrag)
30216000 1242 {
70e0be07
TB
1243 this->defrag = message_create_defrag(msg);
1244 if (!this->defrag)
30216000 1245 {
70e0be07 1246 return FAILED;
30216000
TB
1247 }
1248 }
70e0be07
TB
1249 status = this->defrag->add_fragment(this->defrag, msg);
1250 if (status == SUCCESS)
8fc83834 1251 {
70e0be07
TB
1252 lib->processor->queue_job(lib->processor,
1253 (job_t*)process_message_job_create(this->defrag));
1254 this->defrag = NULL;
1255 /* do not process the last fragment */
30216000
TB
1256 status = NEED_MORE;
1257 }
1258 return status;
1259}
1260
b235e69c
TB
1261/**
1262 * Parse the given message and verify that it is valid.
1263 */
1264static status_t parse_message(private_task_manager_t *this, message_t *msg)
1265{
1266 status_t status;
1267
1268 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
1269
1270 if (status != SUCCESS)
1271 {
1272 switch (status)
1273 {
29a5e070
TB
1274 case NOT_SUPPORTED:
1275 DBG1(DBG_IKE, "unsupported exchange type");
b4705269 1276 send_notify(this, msg, INVALID_EXCHANGE_TYPE);
29a5e070 1277 break;
b235e69c
TB
1278 case PARSE_ERROR:
1279 DBG1(DBG_IKE, "message parsing failed");
b4705269 1280 send_notify(this, msg, PAYLOAD_MALFORMED);
b235e69c
TB
1281 break;
1282 case VERIFY_ERROR:
1283 DBG1(DBG_IKE, "message verification failed");
b4705269 1284 send_notify(this, msg, PAYLOAD_MALFORMED);
b235e69c
TB
1285 break;
1286 case FAILED:
1287 DBG1(DBG_IKE, "integrity check failed");
b4705269 1288 send_notify(this, msg, INVALID_HASH_INFORMATION);
b235e69c
TB
1289 break;
1290 case INVALID_STATE:
1291 DBG1(DBG_IKE, "found encrypted message, but no keys available");
b4705269 1292 send_notify(this, msg, PAYLOAD_MALFORMED);
b235e69c
TB
1293 default:
1294 break;
1295 }
f5ef3577 1296 DBG1(DBG_IKE, "%N %s with message ID %u processing failed",
b235e69c
TB
1297 exchange_type_names, msg->get_exchange_type(msg),
1298 msg->get_request(msg) ? "request" : "response",
1299 msg->get_message_id(msg));
1300
418f4bc7
MW
1301 charon->bus->alert(charon->bus, ALERT_PARSE_ERROR_BODY, msg, status);
1302
b235e69c
TB
1303 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED)
1304 { /* invalid initiation attempt, close SA */
1305 return DESTROY_ME;
1306 }
1307 }
30216000 1308
3ecfc83c 1309 if (msg->get_first_payload_type(msg) == PLV1_FRAGMENT)
30216000
TB
1310 {
1311 return handle_fragment(this, msg);
1312 }
b235e69c
TB
1313 return status;
1314}
1315
3f56f731
TB
1316/**
1317 * Queue the given message if possible
1318 */
1319static status_t queue_message(private_task_manager_t *this, message_t *msg)
1320{
1321 if (this->queued)
1322 {
1323 DBG1(DBG_IKE, "ignoring %N request, queue full",
1324 exchange_type_names, msg->get_exchange_type(msg));
1325 return FAILED;
1326 }
1327 this->queued = message_create_from_packet(msg->get_packet(msg));
1328 if (this->queued->parse_header(this->queued) != SUCCESS)
1329 {
1330 this->queued->destroy(this->queued);
1331 this->queued = NULL;
1332 return FAILED;
1333 }
1334 DBG1(DBG_IKE, "queueing %N request as tasks still active",
1335 exchange_type_names, msg->get_exchange_type(msg));
1336 return SUCCESS;
1337}
1338
4a09d9ee
MW
1339METHOD(task_manager_t, process_message, status_t,
1340 private_task_manager_t *this, message_t *msg)
1341{
b12c53ce 1342 uint32_t hash, mid, i;
3d59c5c3 1343 host_t *me, *other;
68c6863b 1344 status_t status;
73aaf76b 1345
1960312c 1346 /* TODO-IKEv1: update hosts more selectively */
3d59c5c3
TB
1347 me = msg->get_destination(msg);
1348 other = msg->get_source(msg);
68c6863b 1349 mid = msg->get_message_id(msg);
fce566a8
MW
1350 hash = chunk_hash(msg->get_packet_data(msg));
1351 for (i = 0; i < MAX_OLD_HASHES; i++)
1352 {
1353 if (this->initiating.old_hashes[i] == hash)
1354 {
127a98dc 1355 if (array_count(this->initiating.packets) &&
f5a84055 1356 i == (this->initiating.old_hash_pos % MAX_OLD_HASHES) &&
346dad30
MW
1357 (msg->get_exchange_type(msg) == QUICK_MODE ||
1358 msg->get_exchange_type(msg) == AGGRESSIVE))
f5a84055
MW
1359 {
1360 DBG1(DBG_IKE, "received retransmit of response with ID %u, "
1361 "resending last request", mid);
127a98dc 1362 send_packets(this, this->initiating.packets);
f5a84055
MW
1363 return SUCCESS;
1364 }
fce566a8
MW
1365 DBG1(DBG_IKE, "received retransmit of response with ID %u, "
1366 "but next request already sent", mid);
1367 return SUCCESS;
1368 }
1369 }
1960312c 1370
2ddd45c9 1371 if ((mid && mid == this->initiating.mid) ||
73aaf76b 1372 (this->initiating.mid == 0 &&
1cc3e92c 1373 msg->get_exchange_type(msg) == this->initiating.type &&
dc8e9647 1374 this->active_tasks->get_count(this->active_tasks)))
4a09d9ee 1375 {
7519106d 1376 msg->set_request(msg, FALSE);
47b8f6ef 1377 charon->bus->message(charon->bus, msg, TRUE, FALSE);
b235e69c 1378 status = parse_message(this, msg);
30216000
TB
1379 if (status == NEED_MORE)
1380 {
1381 return SUCCESS;
1382 }
1960312c
TB
1383 if (status != SUCCESS)
1384 {
1385 return status;
1386 }
1387 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
1388 time_monotonic(NULL));
3d59c5c3 1389 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
47b8f6ef 1390 charon->bus->message(charon->bus, msg, TRUE, TRUE);
73aaf76b 1391 if (process_response(this, msg) != SUCCESS)
4a09d9ee
MW
1392 {
1393 flush(this);
1394 return DESTROY_ME;
1395 }
f5a84055 1396 this->initiating.old_hashes[(++this->initiating.old_hash_pos) %
fce566a8 1397 MAX_OLD_HASHES] = hash;
4a09d9ee
MW
1398 }
1399 else
1400 {
7b34de45 1401 if (hash == this->responding.hash)
73aaf76b 1402 {
127a98dc 1403 if (array_count(this->responding.packets))
7b34de45
MW
1404 {
1405 DBG1(DBG_IKE, "received retransmit of request with ID %u, "
1406 "retransmitting response", mid);
127a98dc 1407 send_packets(this, this->responding.packets);
7b34de45 1408 }
127a98dc 1409 else if (array_count(this->initiating.packets) &&
1b82eb23
MW
1410 this->initiating.type == INFORMATIONAL_V1)
1411 {
1412 DBG1(DBG_IKE, "received retransmit of DPD request, "
1413 "retransmitting response");
127a98dc 1414 send_packets(this, this->initiating.packets);
1b82eb23 1415 }
7b34de45
MW
1416 else
1417 {
1418 DBG1(DBG_IKE, "received retransmit of request with ID %u, "
1419 "but no response to retransmit", mid);
1420 }
3c79b7b7 1421 charon->bus->alert(charon->bus, ALERT_RETRANSMIT_RECEIVE, msg);
73aaf76b
MW
1422 return SUCCESS;
1423 }
cdf75a39 1424
cf95d292 1425 /* reject Main/Aggressive Modes once established */
cdf75a39
MW
1426 if (msg->get_exchange_type(msg) == ID_PROT ||
1427 msg->get_exchange_type(msg) == AGGRESSIVE)
1428 {
1429 if (this->ike_sa->get_state(this->ike_sa) != IKE_CREATED &&
1430 this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING &&
3ecfc83c 1431 msg->get_first_payload_type(msg) != PLV1_FRAGMENT)
cdf75a39
MW
1432 {
1433 DBG1(DBG_IKE, "ignoring %N in established IKE_SA state",
1434 exchange_type_names, msg->get_exchange_type(msg));
1435 return FAILED;
1436 }
1437 }
1438
b0b0c913 1439 /* drop XAuth/Mode Config/Quick Mode messages until we received the last
1c8dfa30
TB
1440 * Aggressive Mode message. since Informational messages are not
1441 * retransmitted we queue them. */
1b4e7fe1 1442 if (have_task_queued(this, TASK_AGGRESSIVE_MODE))
b0b0c913 1443 {
1c8dfa30
TB
1444 if (msg->get_exchange_type(msg) == INFORMATIONAL_V1)
1445 {
1446 return queue_message(this, msg);
1447 }
1448 else if (msg->get_exchange_type(msg) != AGGRESSIVE)
1449 {
1450 DBG1(DBG_IKE, "ignoring %N request while phase 1 is incomplete",
1451 exchange_type_names, msg->get_exchange_type(msg));
1452 return FAILED;
1453 }
b0b0c913
TB
1454 }
1455
3f56f731
TB
1456 /* queue XAuth/Mode Config messages unless the Main Mode exchange we
1457 * initiated is complete */
f91b6ac7 1458 if (msg->get_exchange_type(msg) == TRANSACTION &&
0b0191e1 1459 this->active_tasks->get_count(this->active_tasks))
3f56f731
TB
1460 {
1461 return queue_message(this, msg);
f91b6ac7
MW
1462 }
1463
1b4e7fe1
TB
1464 /* some peers send INITIAL_CONTACT notifies during XAuth, cache it */
1465 if (have_task_queued(this, TASK_XAUTH) &&
1466 msg->get_exchange_type(msg) == INFORMATIONAL_V1)
1467 {
1468 return queue_message(this, msg);
1469 }
1470
7519106d 1471 msg->set_request(msg, TRUE);
47b8f6ef 1472 charon->bus->message(charon->bus, msg, TRUE, FALSE);
b235e69c 1473 status = parse_message(this, msg);
30216000
TB
1474 if (status == NEED_MORE)
1475 {
1476 return SUCCESS;
1477 }
1960312c
TB
1478 if (status != SUCCESS)
1479 {
1480 return status;
1481 }
1482 /* if this IKE_SA is virgin, we check for a config */
1483 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
1484 {
1485 ike_sa_id_t *ike_sa_id;
1486 ike_cfg_t *ike_cfg;
1487 job_t *job;
f5a84055 1488
79101163
MW
1489 ike_cfg = charon->backends->get_ike_cfg(charon->backends,
1490 me, other, IKEV1);
1960312c
TB
1491 if (ike_cfg == NULL)
1492 {
1493 /* no config found for these hosts, destroy */
4cfd0db8
TB
1494 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
1495 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
b4705269 1496 send_notify(this, msg, NO_PROPOSAL_CHOSEN);
1960312c
TB
1497 return DESTROY_ME;
1498 }
1499 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
1500 ike_cfg->destroy(ike_cfg);
1501 /* add a timeout if peer does not establish it completely */
1502 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
1503 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
1504 lib->scheduler->schedule_job(lib->scheduler, job,
1505 lib->settings->get_int(lib->settings,
42500c27 1506 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT,
d223fe80 1507 lib->ns));
1960312c 1508 }
3d59c5c3 1509 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
47b8f6ef 1510 charon->bus->message(charon->bus, msg, TRUE, TRUE);
73aaf76b 1511 if (process_request(this, msg) != SUCCESS)
26b55dc6
MW
1512 {
1513 flush(this);
1514 return DESTROY_ME;
1515 }
9cc8bd4f 1516 this->responding.hash = hash;
4a09d9ee
MW
1517 }
1518 return SUCCESS;
1519}
1520
8ed976c0
MW
1521/**
1522 * Check if a given task has been queued already
1523 */
1524static bool has_queued(private_task_manager_t *this, task_type_t type)
1525{
1526 enumerator_t *enumerator;
1527 bool found = FALSE;
1528 task_t *task;
1529
1530 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1531 while (enumerator->enumerate(enumerator, &task))
1532 {
1533 if (task->get_type(task) == type)
1534 {
1535 found = TRUE;
1536 break;
1537 }
1538 }
1539 enumerator->destroy(enumerator);
1540 return found;
1541}
1542
208678e6
TB
1543METHOD(task_manager_t, queue_task_delayed, void,
1544 private_task_manager_t *this, task_t *task, uint32_t delay)
d4828f51
TB
1545{
1546 task_type_t type = task->get_type(task);
1547
1548 switch (type)
1549 {
1550 case TASK_MODE_CONFIG:
1551 case TASK_XAUTH:
1552 if (has_queued(this, type))
1553 {
1554 task->destroy(task);
1555 return;
1556 }
1557 break;
1558 default:
1559 break;
1560 }
1561 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
1562 this->queued_tasks->insert_last(this->queued_tasks, task);
1563}
1564
208678e6
TB
1565METHOD(task_manager_t, queue_task, void,
1566 private_task_manager_t *this, task_t *task)
1567{
1568 queue_task_delayed(this, task, 0);
1569}
1570
a60daa07
MW
1571METHOD(task_manager_t, queue_ike, void,
1572 private_task_manager_t *this)
1573{
830ab2ae
MW
1574 peer_cfg_t *peer_cfg;
1575
8ed976c0
MW
1576 if (!has_queued(this, TASK_ISAKMP_VENDOR))
1577 {
1578 queue_task(this, (task_t*)isakmp_vendor_create(this->ike_sa, TRUE));
1579 }
1580 if (!has_queued(this, TASK_ISAKMP_CERT_PRE))
1581 {
1582 queue_task(this, (task_t*)isakmp_cert_pre_create(this->ike_sa, TRUE));
1583 }
830ab2ae
MW
1584 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
1585 if (peer_cfg->use_aggressive(peer_cfg))
8ed976c0 1586 {
830ab2ae
MW
1587 if (!has_queued(this, TASK_AGGRESSIVE_MODE))
1588 {
1589 queue_task(this, (task_t*)aggressive_mode_create(this->ike_sa, TRUE));
1590 }
1591 }
1592 else
1593 {
1594 if (!has_queued(this, TASK_MAIN_MODE))
1595 {
1596 queue_task(this, (task_t*)main_mode_create(this->ike_sa, TRUE));
1597 }
8ed976c0
MW
1598 }
1599 if (!has_queued(this, TASK_ISAKMP_CERT_POST))
1600 {
1601 queue_task(this, (task_t*)isakmp_cert_post_create(this->ike_sa, TRUE));
1602 }
1603 if (!has_queued(this, TASK_ISAKMP_NATD))
1604 {
1605 queue_task(this, (task_t*)isakmp_natd_create(this->ike_sa, TRUE));
1606 }
a60daa07
MW
1607}
1608
4f49b068 1609METHOD(task_manager_t, queue_ike_reauth, void,
dab60d64
MW
1610 private_task_manager_t *this)
1611{
4f49b068
MW
1612 enumerator_t *enumerator;
1613 child_sa_t *child_sa;
1614 ike_sa_t *new;
1615 host_t *host;
1616
1617 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1618 this->ike_sa->get_version(this->ike_sa), TRUE);
1619 if (!new)
1620 { /* shouldn't happen */
1621 return;
1622 }
1623
1624 new->set_peer_cfg(new, this->ike_sa->get_peer_cfg(this->ike_sa));
1625 host = this->ike_sa->get_other_host(this->ike_sa);
1626 new->set_other_host(new, host->clone(host));
1627 host = this->ike_sa->get_my_host(this->ike_sa);
1628 new->set_my_host(new, host->clone(host));
101d26ba
MW
1629 enumerator = this->ike_sa->create_virtual_ip_enumerator(this->ike_sa, TRUE);
1630 while (enumerator->enumerate(enumerator, &host))
4f49b068 1631 {
101d26ba 1632 new->add_virtual_ip(new, TRUE, host);
4f49b068 1633 }
101d26ba 1634 enumerator->destroy(enumerator);
4f49b068 1635
6a9a69ae
TB
1636 charon->bus->children_migrate(charon->bus, new->get_id(new),
1637 new->get_unique_id(new));
4f49b068
MW
1638 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
1639 while (enumerator->enumerate(enumerator, &child_sa))
1640 {
1641 this->ike_sa->remove_child_sa(this->ike_sa, enumerator);
1642 new->add_child_sa(new, child_sa);
1643 }
1644 enumerator->destroy(enumerator);
6a9a69ae
TB
1645 charon->bus->set_sa(charon->bus, new);
1646 charon->bus->children_migrate(charon->bus, NULL, 0);
1647 charon->bus->set_sa(charon->bus, this->ike_sa);
4f49b068 1648
8254e7ec
TB
1649 if (!new->get_child_count(new))
1650 { /* check if a Quick Mode task is queued (UNITY_LOAD_BALANCE case) */
1651 task_t *task;
1652
1653 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1654 while (enumerator->enumerate(enumerator, &task))
1655 {
1656 if (task->get_type(task) == TASK_QUICK_MODE)
1657 {
1658 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
1659 task->migrate(task, new);
1660 new->queue_task(new, task);
1661 }
1662 }
1663 enumerator->destroy(enumerator);
1664 }
1665
4f49b068
MW
1666 if (new->initiate(new, NULL, 0, NULL, NULL) != DESTROY_ME)
1667 {
1668 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1b79299b 1669 this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
4f49b068
MW
1670 }
1671 else
1672 {
1673 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1674 DBG1(DBG_IKE, "reauthenticating IKE_SA failed");
1675 }
1676 charon->bus->set_sa(charon->bus, this->ike_sa);
dab60d64
MW
1677}
1678
4f49b068 1679METHOD(task_manager_t, queue_ike_rekey, void,
cedb412e
MW
1680 private_task_manager_t *this)
1681{
4f49b068 1682 queue_ike_reauth(this);
cedb412e
MW
1683}
1684
3ed148b3
MW
1685METHOD(task_manager_t, queue_ike_delete, void,
1686 private_task_manager_t *this)
1687{
daee47ba
MW
1688 enumerator_t *enumerator;
1689 child_sa_t *child_sa;
1690
60d0f52f
TB
1691 /* cancel any currently active task to get the DELETE done quickly */
1692 flush_queue(this, TASK_QUEUE_ACTIVE);
1693
daee47ba
MW
1694 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
1695 while (enumerator->enumerate(enumerator, &child_sa))
1696 {
1697 queue_task(this, (task_t*)
1698 quick_delete_create(this->ike_sa, child_sa->get_protocol(child_sa),
3a925f74 1699 child_sa->get_spi(child_sa, TRUE), FALSE, FALSE));
daee47ba
MW
1700 }
1701 enumerator->destroy(enumerator);
1702
3ed148b3
MW
1703 queue_task(this, (task_t*)isakmp_delete_create(this->ike_sa, TRUE));
1704}
1705
873df908
MW
1706METHOD(task_manager_t, queue_mobike, void,
1707 private_task_manager_t *this, bool roam, bool address)
1708{
1709 /* Not supported in IKEv1 */
1710}
1711
fe43d9a2 1712METHOD(task_manager_t, queue_child, void,
b12c53ce 1713 private_task_manager_t *this, child_cfg_t *cfg, uint32_t reqid,
fe43d9a2
MW
1714 traffic_selector_t *tsi, traffic_selector_t *tsr)
1715{
14dc7941
MW
1716 quick_mode_t *task;
1717
1718 task = quick_mode_create(this->ike_sa, cfg, tsi, tsr);
1719 task->use_reqid(task, reqid);
1720
1721 queue_task(this, &task->task);
fe43d9a2
MW
1722}
1723
2d4c347a
MW
1724/**
1725 * Check if two CHILD_SAs have the same traffic selector
1726 */
553bb787 1727static bool have_equal_ts(child_sa_t *child1, child_sa_t *child2, bool local)
2d4c347a 1728{
553bb787
MW
1729 enumerator_t *e1, *e2;
1730 traffic_selector_t *ts1, *ts2;
1731 bool equal = FALSE;
2d4c347a 1732
553bb787
MW
1733 e1 = child1->create_ts_enumerator(child1, local);
1734 e2 = child2->create_ts_enumerator(child2, local);
1735 if (e1->enumerate(e1, &ts1) && e2->enumerate(e2, &ts2))
2d4c347a 1736 {
553bb787 1737 equal = ts1->equals(ts1, ts2);
2d4c347a 1738 }
fafa7684 1739 e2->destroy(e2);
553bb787
MW
1740 e1->destroy(e1);
1741
1742 return equal;
2d4c347a
MW
1743}
1744
1e24fa46
MW
1745/**
1746 * Check if a CHILD_SA is redundant and we should delete instead of rekey
1747 */
1748static bool is_redundant(private_task_manager_t *this, child_sa_t *child_sa)
1749{
1750 enumerator_t *enumerator;
1751 child_sa_t *current;
1752 bool redundant = FALSE;
1753
1754 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
1755 while (enumerator->enumerate(enumerator, &current))
1756 {
1757 if (current->get_state(current) == CHILD_INSTALLED &&
1758 streq(current->get_name(current), child_sa->get_name(child_sa)) &&
2d4c347a
MW
1759 have_equal_ts(current, child_sa, TRUE) &&
1760 have_equal_ts(current, child_sa, FALSE) &&
1e24fa46
MW
1761 current->get_lifetime(current, FALSE) >
1762 child_sa->get_lifetime(child_sa, FALSE))
1763 {
1764 DBG1(DBG_IKE, "deleting redundant CHILD_SA %s{%d}",
246c969d
MW
1765 child_sa->get_name(child_sa),
1766 child_sa->get_unique_id(child_sa));
1e24fa46
MW
1767 redundant = TRUE;
1768 break;
1769 }
1770 }
1771 enumerator->destroy(enumerator);
1772
1773 return redundant;
1774}
1775
7a5f372c
MW
1776/**
1777 * Get the first traffic selector of a CHILD_SA, local or remote
1778 */
1779static traffic_selector_t* get_first_ts(child_sa_t *child_sa, bool local)
1780{
1781 traffic_selector_t *ts = NULL;
553bb787 1782 enumerator_t *enumerator;
7a5f372c 1783
553bb787
MW
1784 enumerator = child_sa->create_ts_enumerator(child_sa, local);
1785 enumerator->enumerate(enumerator, &ts);
1786 enumerator->destroy(enumerator);
1787
1788 return ts;
7a5f372c
MW
1789}
1790
463a73cc 1791METHOD(task_manager_t, queue_child_rekey, void,
b12c53ce 1792 private_task_manager_t *this, protocol_id_t protocol, uint32_t spi)
463a73cc 1793{
23eb447c
MW
1794 child_sa_t *child_sa;
1795 child_cfg_t *cfg;
1796 quick_mode_t *task;
1797
1798 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, TRUE);
1799 if (!child_sa)
1800 {
1801 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, FALSE);
1802 }
1803 if (child_sa && child_sa->get_state(child_sa) == CHILD_INSTALLED)
1804 {
1e24fa46
MW
1805 if (is_redundant(this, child_sa))
1806 {
a01eb5e4 1807 child_sa->set_state(child_sa, CHILD_REKEYED);
083208e8
TB
1808 if (lib->settings->get_bool(lib->settings, "%s.delete_rekeyed",
1809 FALSE, lib->ns))
1810 {
1811 queue_task(this, (task_t*)quick_delete_create(this->ike_sa,
1e24fa46 1812 protocol, spi, FALSE, FALSE));
083208e8 1813 }
1e24fa46
MW
1814 }
1815 else
1816 {
1817 child_sa->set_state(child_sa, CHILD_REKEYING);
1818 cfg = child_sa->get_config(child_sa);
7a5f372c
MW
1819 task = quick_mode_create(this->ike_sa, cfg->get_ref(cfg),
1820 get_first_ts(child_sa, TRUE), get_first_ts(child_sa, FALSE));
1e24fa46 1821 task->use_reqid(task, child_sa->get_reqid(child_sa));
85b23888
MW
1822 task->use_marks(task, child_sa->get_mark(child_sa, TRUE).value,
1823 child_sa->get_mark(child_sa, FALSE).value);
1e24fa46 1824 task->rekey(task, child_sa->get_spi(child_sa, TRUE));
23eb447c 1825
1e24fa46
MW
1826 queue_task(this, &task->task);
1827 }
23eb447c 1828 }
463a73cc
MW
1829}
1830
83c5fda0 1831METHOD(task_manager_t, queue_child_delete, void,
b12c53ce 1832 private_task_manager_t *this, protocol_id_t protocol, uint32_t spi,
3a925f74 1833 bool expired)
83c5fda0
MW
1834{
1835 queue_task(this, (task_t*)quick_delete_create(this->ike_sa, protocol,
3a925f74 1836 spi, FALSE, expired));
83c5fda0
MW
1837}
1838
244d715d
MW
1839METHOD(task_manager_t, queue_dpd, void,
1840 private_task_manager_t *this)
1841{
80c5b17d 1842 peer_cfg_t *peer_cfg;
b12c53ce 1843 uint32_t t, retransmit;
4b38c22c 1844
24742c0f 1845 queue_task(this, (task_t*)isakmp_dpd_create(this->ike_sa, DPD_R_U_THERE,
2ddd45c9 1846 this->dpd_send++));
80c5b17d 1847 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
4b38c22c 1848
80c5b17d
AS
1849 /* compute timeout in milliseconds */
1850 t = 1000 * peer_cfg->get_dpd_timeout(peer_cfg);
1851 if (t == 0)
4b38c22c 1852 {
80c5b17d
AS
1853 /* use the same timeout as a retransmitting IKE message would have */
1854 for (retransmit = 0; retransmit <= this->retransmit_tries; retransmit++)
1855 {
b12c53ce 1856 t += (uint32_t)(this->retransmit_timeout * 1000.0 *
80c5b17d
AS
1857 pow(this->retransmit_base, retransmit));
1858 }
4b38c22c 1859 }
747b6487
TB
1860 /* compensate for the already elapsed dpd delay */
1861 t -= 1000 * peer_cfg->get_dpd(peer_cfg);
80c5b17d
AS
1862
1863 /* schedule DPD timeout job */
4b38c22c
MW
1864 lib->scheduler->schedule_job_ms(lib->scheduler,
1865 (job_t*)dpd_timeout_job_create(this->ike_sa->get_id(this->ike_sa)), t);
244d715d
MW
1866}
1867
4a09d9ee
MW
1868METHOD(task_manager_t, adopt_tasks, void,
1869 private_task_manager_t *this, task_manager_t *other_public)
1870{
1871 private_task_manager_t *other = (private_task_manager_t*)other_public;
1872 task_t *task;
1873
1874 /* move queued tasks from other to this */
1875 while (other->queued_tasks->remove_last(other->queued_tasks,
1876 (void**)&task) == SUCCESS)
1877 {
1878 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
1879 task->migrate(task, this->ike_sa);
1880 this->queued_tasks->insert_first(this->queued_tasks, task);
1881 }
1882}
1883
68db844f
TB
1884/**
1885 * Migrates child-creating tasks from src to dst
1886 */
1887static void migrate_child_tasks(private_task_manager_t *this,
1888 linked_list_t *src, linked_list_t *dst)
1889{
1890 enumerator_t *enumerator;
1891 task_t *task;
1892
1893 enumerator = src->create_enumerator(src);
1894 while (enumerator->enumerate(enumerator, &task))
1895 {
1896 if (task->get_type(task) == TASK_QUICK_MODE)
1897 {
1898 src->remove_at(src, enumerator);
1899 task->migrate(task, this->ike_sa);
1900 dst->insert_last(dst, task);
1901 }
1902 }
1903 enumerator->destroy(enumerator);
1904}
1905
1906METHOD(task_manager_t, adopt_child_tasks, void,
1907 private_task_manager_t *this, task_manager_t *other_public)
1908{
1909 private_task_manager_t *other = (private_task_manager_t*)other_public;
1910
1911 /* move active child tasks from other to this */
1912 migrate_child_tasks(this, other->active_tasks, this->queued_tasks);
1913 /* do the same for queued tasks */
1914 migrate_child_tasks(this, other->queued_tasks, this->queued_tasks);
1915}
1916
4a09d9ee
MW
1917METHOD(task_manager_t, busy, bool,
1918 private_task_manager_t *this)
1919{
1920 return (this->active_tasks->get_count(this->active_tasks) > 0);
1921}
1922
1923METHOD(task_manager_t, incr_mid, void,
1924 private_task_manager_t *this, bool initiate)
1925{
4a09d9ee
MW
1926}
1927
05a2be82
TB
1928METHOD(task_manager_t, get_mid, uint32_t,
1929 private_task_manager_t *this, bool initiate)
1930{
1931 return initiate ? this->initiating.mid : this->responding.mid;
1932}
1933
4a09d9ee 1934METHOD(task_manager_t, reset, void,
b12c53ce 1935 private_task_manager_t *this, uint32_t initiate, uint32_t respond)
4a09d9ee 1936{
d9c1dae2
MW
1937 enumerator_t *enumerator;
1938 task_t *task;
1939
1940 /* reset message counters and retransmit packets */
127a98dc
TB
1941 clear_packets(this->responding.packets);
1942 clear_packets(this->initiating.packets);
f5a84055
MW
1943 this->responding.seqnr = RESPONDING_SEQ;
1944 this->responding.retransmitted = 0;
d9c1dae2
MW
1945 this->initiating.mid = 0;
1946 this->initiating.seqnr = 0;
1947 this->initiating.retransmitted = 0;
1948 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
70e0be07
TB
1949 DESTROY_IF(this->defrag);
1950 this->defrag = NULL;
fd6fbf17
MW
1951 if (initiate != UINT_MAX)
1952 {
1953 this->dpd_send = initiate;
1954 }
1955 if (respond != UINT_MAX)
1956 {
1957 this->dpd_recv = respond;
1958 }
d9c1dae2
MW
1959
1960 /* reset queued tasks */
1961 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1962 while (enumerator->enumerate(enumerator, &task))
1963 {
1964 task->migrate(task, this->ike_sa);
1965 }
1966 enumerator->destroy(enumerator);
1967
1968 /* reset active tasks */
1969 while (this->active_tasks->remove_last(this->active_tasks,
1970 (void**)&task) == SUCCESS)
1971 {
1972 task->migrate(task, this->ike_sa);
1973 this->queued_tasks->insert_first(this->queued_tasks, task);
1974 }
4a09d9ee
MW
1975}
1976
1977METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
1978 private_task_manager_t *this, task_queue_t queue)
1979{
1980 switch (queue)
1981 {
1982 case TASK_QUEUE_ACTIVE:
1983 return this->active_tasks->create_enumerator(this->active_tasks);
1984 case TASK_QUEUE_PASSIVE:
1985 return this->passive_tasks->create_enumerator(this->passive_tasks);
1986 case TASK_QUEUE_QUEUED:
1987 return this->queued_tasks->create_enumerator(this->queued_tasks);
1988 default:
1989 return enumerator_create_empty();
1990 }
1991}
1992
1993METHOD(task_manager_t, destroy, void,
1994 private_task_manager_t *this)
1995{
1996 flush(this);
1997
1998 this->active_tasks->destroy(this->active_tasks);
1999 this->queued_tasks->destroy(this->queued_tasks);
2000 this->passive_tasks->destroy(this->passive_tasks);
70e0be07 2001 DESTROY_IF(this->defrag);
4a09d9ee 2002
f91b6ac7 2003 DESTROY_IF(this->queued);
127a98dc
TB
2004 clear_packets(this->responding.packets);
2005 array_destroy(this->responding.packets);
2006 clear_packets(this->initiating.packets);
2007 array_destroy(this->initiating.packets);
73aaf76b 2008 DESTROY_IF(this->rng);
4a09d9ee
MW
2009 free(this);
2010}
2011
2012/*
2013 * see header file
2014 */
2015task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
2016{
2017 private_task_manager_t *this;
2018
2019 INIT(this,
2020 .public = {
2021 .task_manager = {
2022 .process_message = _process_message,
2023 .queue_task = _queue_task,
208678e6 2024 .queue_task_delayed = _queue_task_delayed,
a60daa07 2025 .queue_ike = _queue_ike,
dab60d64 2026 .queue_ike_rekey = _queue_ike_rekey,
cedb412e 2027 .queue_ike_reauth = _queue_ike_reauth,
3ed148b3 2028 .queue_ike_delete = _queue_ike_delete,
873df908 2029 .queue_mobike = _queue_mobike,
fe43d9a2 2030 .queue_child = _queue_child,
463a73cc 2031 .queue_child_rekey = _queue_child_rekey,
83c5fda0 2032 .queue_child_delete = _queue_child_delete,
244d715d 2033 .queue_dpd = _queue_dpd,
4a09d9ee
MW
2034 .initiate = _initiate,
2035 .retransmit = _retransmit,
2036 .incr_mid = _incr_mid,
05a2be82 2037 .get_mid = _get_mid,
4a09d9ee
MW
2038 .reset = _reset,
2039 .adopt_tasks = _adopt_tasks,
68db844f 2040 .adopt_child_tasks = _adopt_child_tasks,
4a09d9ee
MW
2041 .busy = _busy,
2042 .create_task_enumerator = _create_task_enumerator,
b1908994 2043 .flush = _flush,
a5c79960 2044 .flush_queue = _flush_queue,
4a09d9ee
MW
2045 .destroy = _destroy,
2046 },
2047 },
dcbdc914
TB
2048 .initiating = {
2049 .type = EXCHANGE_TYPE_UNDEFINED,
2050 },
2051 .responding = {
2052 .seqnr = RESPONDING_SEQ,
2053 },
4a09d9ee 2054 .ike_sa = ike_sa,
73aaf76b 2055 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
4a09d9ee
MW
2056 .queued_tasks = linked_list_create(),
2057 .active_tasks = linked_list_create(),
2058 .passive_tasks = linked_list_create(),
2059 .retransmit_tries = lib->settings->get_int(lib->settings,
389e4b8e 2060 "%s.retransmit_tries", RETRANSMIT_TRIES, lib->ns),
4a09d9ee 2061 .retransmit_timeout = lib->settings->get_double(lib->settings,
389e4b8e 2062 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT, lib->ns),
4a09d9ee 2063 .retransmit_base = lib->settings->get_double(lib->settings,
389e4b8e
TB
2064 "%s.retransmit_base", RETRANSMIT_BASE, lib->ns),
2065 .retransmit_jitter = min(lib->settings->get_int(lib->settings,
2066 "%s.retransmit_jitter", 0, lib->ns), RETRANSMIT_JITTER_MAX),
2067 .retransmit_limit = lib->settings->get_int(lib->settings,
2068 "%s.retransmit_limit", 0, lib->ns) * 1000,
4a09d9ee
MW
2069 );
2070
3fca5bd1
MW
2071 if (!this->rng)
2072 {
2073 DBG1(DBG_IKE, "no RNG found, unable to create IKE_SA");
2074 destroy(this);
2075 return NULL;
2076 }
1bb9c51e
TB
2077 if (!this->rng->get_bytes(this->rng, sizeof(this->dpd_send),
2078 (void*)&this->dpd_send))
2079 {
2080 DBG1(DBG_IKE, "failed to allocate message ID, unable to create IKE_SA");
2081 destroy(this);
2082 return NULL;
2083 }
2ddd45c9 2084 this->dpd_send &= 0x7FFFFFFF;
3e6b7403 2085
4a09d9ee
MW
2086 return &this->public;
2087}