]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/libcharon/sa/task_manager_v1.c
Don't respond to malformed INFORMATIONAL_V1 messages with another INFORMATIONAL_V1...
[thirdparty/strongswan.git] / src / libcharon / sa / task_manager_v1.c
CommitLineData
4a09d9ee 1/*
68c6863b
TB
2 * Copyright (C) 2007-2011 Tobias Brunner
3 * Copyright (C) 2007-2011 Martin Willi
4a09d9ee
MW
4 * Hochschule fuer Technik Rapperswil
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>
01685247 22#include <sa/tasks/ike_vendor.h>
c73c832c 23#include <sa/tasks/main_mode.h>
744c0801 24#include <sa/tasks/quick_mode.h>
b03c700d 25#include <sa/tasks/xauth_request.h>
a2f8fc97 26#include <sa/tasks/ike_vendor_v1.h>
da063ec9 27#include <processing/jobs/retransmit_job.h>
68c6863b 28#include <processing/jobs/delete_ike_sa_job.h>
4a09d9ee
MW
29
30typedef struct exchange_t exchange_t;
31
32/**
33 * An exchange in the air, used do detect and handle retransmission
34 */
35struct exchange_t {
36
37 /**
38 * Message ID used for this transaction
39 */
40 u_int32_t mid;
41
42 /**
43 * generated packet for retransmission
44 */
45 packet_t *packet;
46};
47
48typedef struct private_task_manager_t private_task_manager_t;
49
50/**
51 * private data of the task manager
52 */
53struct private_task_manager_t {
54
55 /**
56 * public functions
57 */
58 task_manager_v1_t public;
59
60 /**
61 * associated IKE_SA we are serving
62 */
63 ike_sa_t *ike_sa;
64
73aaf76b
MW
65 /**
66 * RNG to create message IDs
67 */
68 rng_t *rng;
69
4a09d9ee
MW
70 /**
71 * Exchange we are currently handling as responder
72 */
73 struct {
74 /**
75 * Message ID of the exchange
76 */
77 u_int32_t mid;
78
9cc8bd4f
MW
79 /**
80 * Hash of a previously received message
81 */
82 u_int32_t hash;
83
4a09d9ee
MW
84 /**
85 * packet for retransmission
86 */
87 packet_t *packet;
88
89 } responding;
90
91 /**
92 * Exchange we are currently handling as initiator
93 */
94 struct {
95 /**
96 * Message ID of the exchange
97 */
98 u_int32_t mid;
99
9cc8bd4f
MW
100 /**
101 * Hash of a previously received message
102 */
103 u_int32_t hash;
104
4a09d9ee
MW
105 /**
106 * how many times we have retransmitted so far
107 */
108 u_int retransmitted;
109
110 /**
111 * packet for retransmission
112 */
113 packet_t *packet;
114
115 /**
116 * type of the initated exchange
117 */
118 exchange_type_t type;
119
120 } initiating;
121
122 /**
123 * List of queued tasks not yet in action
124 */
125 linked_list_t *queued_tasks;
126
127 /**
128 * List of active tasks, initiated by ourselve
129 */
130 linked_list_t *active_tasks;
131
132 /**
133 * List of tasks initiated by peer
134 */
135 linked_list_t *passive_tasks;
136
4a09d9ee
MW
137 /**
138 * Number of times we retransmit messages before giving up
139 */
140 u_int retransmit_tries;
141
142 /**
143 * Retransmission timeout
144 */
145 double retransmit_timeout;
146
147 /**
148 * Base to calculate retransmission timeout
149 */
150 double retransmit_base;
151};
152
153/**
154 * flush all tasks in the task manager
155 */
156static void flush(private_task_manager_t *this)
157{
158 this->queued_tasks->destroy_offset(this->queued_tasks,
159 offsetof(task_t, destroy));
160 this->queued_tasks = linked_list_create();
161 this->passive_tasks->destroy_offset(this->passive_tasks,
162 offsetof(task_t, destroy));
163 this->passive_tasks = linked_list_create();
164 this->active_tasks->destroy_offset(this->active_tasks,
165 offsetof(task_t, destroy));
166 this->active_tasks = linked_list_create();
167}
168
26b55dc6
MW
169/**
170 * move a task of a specific type from the queue to the active list
171 */
172static bool activate_task(private_task_manager_t *this, task_type_t type)
173{
174 enumerator_t *enumerator;
175 task_t *task;
176 bool found = FALSE;
177
178 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
179 while (enumerator->enumerate(enumerator, (void**)&task))
180 {
181 if (task->get_type(task) == type)
182 {
183 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
184 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
185 this->active_tasks->insert_last(this->active_tasks, task);
186 found = TRUE;
187 break;
188 }
189 }
190 enumerator->destroy(enumerator);
191 return found;
192}
193
23f4e4b4
CO
194METHOD(task_manager_t, retransmit, status_t,
195 private_task_manager_t *this, u_int32_t message_id)
196{
197 if (message_id == this->initiating.mid)
198 {
199 u_int32_t timeout;
23f4e4b4 200 packet_t *packet;
da063ec9 201 job_t *job;
23f4e4b4
CO
202
203 if (this->initiating.retransmitted <= this->retransmit_tries)
204 {
205 timeout = (u_int32_t)(this->retransmit_timeout * 1000.0 *
206 pow(this->retransmit_base, this->initiating.retransmitted));
207 }
208 else
209 {
210 DBG1(DBG_IKE, "giving up after %d retransmits",
211 this->initiating.retransmitted - 1);
212 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
213 {
214 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
215 }
216 return DESTROY_ME;
217 }
218
219 if (this->initiating.retransmitted)
220 {
221 DBG1(DBG_IKE, "retransmit %d of request with message ID %d",
222 this->initiating.retransmitted, message_id);
223 }
224 packet = this->initiating.packet->clone(this->initiating.packet);
225 charon->sender->send(charon->sender, packet);
226
227 this->initiating.retransmitted++;
228 job = (job_t*)retransmit_job_create(this->initiating.mid,
229 this->ike_sa->get_id(this->ike_sa));
230 lib->scheduler->schedule_job_ms(lib->scheduler, job, timeout);
231 }
232 return SUCCESS;
233}
234
b94f248e
CO
235void migrate_tasks(linked_list_t *from, linked_list_t *to)
236{
237 enumerator_t *enumerator;
238 task_t *task;
239
240 enumerator = from->create_enumerator(from);
241 while(enumerator->enumerate(enumerator, (void**)&task))
242 {
243 DBG4(DBG_IKE, " Migrating %N task to new queue", task_type_names, task->get_type(task));
244 if(task->swap_initiator)
245 {
246 task->swap_initiator(task);
247 }
248 to->insert_last(to, task);
249 from->remove_at(from, enumerator);
250 }
251 enumerator->destroy(enumerator);
252}
253
4a09d9ee
MW
254METHOD(task_manager_t, initiate, status_t,
255 private_task_manager_t *this)
256{
26b55dc6
MW
257 enumerator_t *enumerator;
258 task_t *task;
259 message_t *message;
260 host_t *me, *other;
261 status_t status;
73aaf76b 262 exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED;
3e246c48 263 bool new_mid = FALSE;
73aaf76b
MW
264
265 if (!this->rng)
266 {
267 DBG1(DBG_IKE, "no RNG supported");
268 return FAILED;
269 }
26b55dc6
MW
270
271 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
272 {
273 DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
274 exchange_type_names, this->initiating.type);
275 /* do not initiate if we already have a message in the air */
276 return SUCCESS;
277 }
278
279 if (this->active_tasks->get_count(this->active_tasks) == 0)
280 {
281 DBG2(DBG_IKE, "activating new tasks");
282 switch (this->ike_sa->get_state(this->ike_sa))
283 {
284 case IKE_CREATED:
a2f8fc97 285 activate_task(this, TASK_VENDOR_V1);
a09972df 286 if (activate_task(this, TASK_MAIN_MODE))
26b55dc6
MW
287 {
288 exchange = ID_PROT;
289 }
290 break;
744c0801
MW
291 case IKE_ESTABLISHED:
292 if (activate_task(this, TASK_QUICK_MODE))
293 {
294 exchange = QUICK_MODE;
3e246c48
MW
295 new_mid = TRUE;
296 break;
744c0801 297 }
23f4e4b4
CO
298 if (activate_task(this, TASK_XAUTH_REQUEST))
299 {
300 exchange = TRANSACTION;
3e246c48
MW
301 new_mid = TRUE;
302 break;
23f4e4b4 303 }
744c0801 304 break;
26b55dc6
MW
305 default:
306 break;
307 }
308 }
309 else
310 {
311 DBG2(DBG_IKE, "reinitiating already active tasks");
312 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
313 while (enumerator->enumerate(enumerator, (void**)&task))
314 {
315 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
316 switch (task->get_type(task))
317 {
a09972df 318 case TASK_MAIN_MODE:
26b55dc6
MW
319 exchange = ID_PROT;
320 break;
744c0801
MW
321 case TASK_QUICK_MODE:
322 exchange = QUICK_MODE;
323 break;
52ac2ceb
CO
324 case TASK_XAUTH_REQUEST:
325 exchange = TRANSACTION;
326 new_mid = TRUE;
327 break;
26b55dc6
MW
328 default:
329 continue;
330 }
331 break;
332 }
333 enumerator->destroy(enumerator);
334 }
335
73aaf76b 336 if (exchange == EXCHANGE_TYPE_UNDEFINED)
26b55dc6
MW
337 {
338 DBG2(DBG_IKE, "nothing to initiate");
339 /* nothing to do yet... */
340 return SUCCESS;
341 }
342
343 me = this->ike_sa->get_my_host(this->ike_sa);
344 other = this->ike_sa->get_other_host(this->ike_sa);
345
346 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
3e246c48 347 if (new_mid)
26b55dc6 348 {
73aaf76b
MW
349 this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
350 (void*)&this->initiating.mid);
26b55dc6 351 }
3e246c48 352 message->set_message_id(message, this->initiating.mid);
26b55dc6
MW
353 message->set_source(message, me->clone(me));
354 message->set_destination(message, other->clone(other));
355 message->set_exchange_type(message, exchange);
356 this->initiating.type = exchange;
357 this->initiating.retransmitted = 0;
358
359 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
360 while (enumerator->enumerate(enumerator, (void*)&task))
361 {
362 switch (task->build(task, message))
363 {
364 case SUCCESS:
365 /* task completed, remove it */
366 this->active_tasks->remove_at(this->active_tasks, enumerator);
367 task->destroy(task);
368 break;
b94f248e
CO
369 case MIGRATE:
370 /* task completed, remove it */
371 this->active_tasks->remove_at(this->active_tasks, enumerator);
372 task->destroy(task);
373 /* migrate the remaining active tasks to the passive queue */
374 migrate_tasks(this->active_tasks, this->passive_tasks);
375 break;
26b55dc6
MW
376 case NEED_MORE:
377 /* processed, but task needs another exchange */
378 break;
379 case FAILED:
380 default:
381 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
382 {
383 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
384 }
385 /* FALL */
386 case DESTROY_ME:
387 /* critical failure, destroy IKE_SA */
388 enumerator->destroy(enumerator);
389 message->destroy(message);
390 flush(this);
391 return DESTROY_ME;
392 }
393 }
394 enumerator->destroy(enumerator);
395
396 /* update exchange type if a task changed it */
397 this->initiating.type = message->get_exchange_type(message);
398
399 status = this->ike_sa->generate_message(this->ike_sa, message,
400 &this->initiating.packet);
401 if (status != SUCCESS)
402 {
403 /* message generation failed. There is nothing more to do than to
404 * close the SA */
405 message->destroy(message);
406 flush(this);
407 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
408 return DESTROY_ME;
409 }
410 message->destroy(message);
411
412 charon->sender->send(charon->sender,
413 this->initiating.packet->clone(this->initiating.packet));
414
415 return SUCCESS;
4a09d9ee
MW
416}
417
418/**
419 * handle exchange collisions
420 */
421static bool handle_collisions(private_task_manager_t *this, task_t *task)
422{
423 return FALSE;
424}
425
426/**
427 * build a response depending on the "passive" task list
428 */
429static status_t build_response(private_task_manager_t *this, message_t *request)
430{
431 enumerator_t *enumerator;
432 task_t *task;
433 message_t *message;
434 host_t *me, *other;
435 bool delete = FALSE;
436 status_t status;
b94f248e 437 bool migrate = FALSE;
4a09d9ee
MW
438
439 me = request->get_destination(request);
440 other = request->get_source(request);
441
442 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
443 message->set_exchange_type(message, request->get_exchange_type(request));
444 /* send response along the path the request came in */
445 message->set_source(message, me->clone(me));
446 message->set_destination(message, other->clone(other));
73aaf76b 447 message->set_message_id(message, request->get_message_id(request));
4a09d9ee
MW
448 message->set_request(message, FALSE);
449
450 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
451 while (enumerator->enumerate(enumerator, (void*)&task))
452 {
453 switch (task->build(task, message))
454 {
b94f248e
CO
455 case MIGRATE:
456 migrate = TRUE;
457 /* FALL */
4a09d9ee
MW
458 case SUCCESS:
459 /* task completed, remove it */
460 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
461 if (!handle_collisions(this, task))
462 {
463 task->destroy(task);
464 }
465 break;
466 case NEED_MORE:
467 /* processed, but task needs another exchange */
468 if (handle_collisions(this, task))
469 {
470 this->passive_tasks->remove_at(this->passive_tasks,
471 enumerator);
472 }
473 break;
474 case FAILED:
475 default:
476 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
477 /* FALL */
478 case DESTROY_ME:
479 /* destroy IKE_SA, but SEND response first */
480 delete = TRUE;
481 break;
482 }
483 if (delete)
484 {
485 break;
486 }
487 }
488 enumerator->destroy(enumerator);
489
490 /* message complete, send it */
491 DESTROY_IF(this->responding.packet);
492 this->responding.packet = NULL;
493 status = this->ike_sa->generate_message(this->ike_sa, message,
494 &this->responding.packet);
495 message->destroy(message);
496 if (status != SUCCESS)
497 {
498 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
499 return DESTROY_ME;
500 }
501
502 charon->sender->send(charon->sender,
503 this->responding.packet->clone(this->responding.packet));
b94f248e
CO
504
505 if (migrate)
506 {
507 migrate_tasks(this->passive_tasks, this->queued_tasks);
508 /* Kick off the newly installed tasks */
509 initiate(this);
510 }
511
4a09d9ee
MW
512 if (delete)
513 {
514 return DESTROY_ME;
515 }
516 return SUCCESS;
517}
518
519/**
520 * handle an incoming request message
521 */
522static status_t process_request(private_task_manager_t *this,
523 message_t *message)
524{
525 enumerator_t *enumerator;
526 task_t *task = NULL;
527
528 if (this->passive_tasks->get_count(this->passive_tasks) == 0)
529 { /* create tasks depending on request type, if not already some queued */
530 switch (message->get_exchange_type(message))
531 {
532 case ID_PROT:
a2f8fc97 533 task = (task_t *)ike_vendor_v1_create(this->ike_sa, FALSE);
01685247 534 this->passive_tasks->insert_last(this->passive_tasks, task);
c73c832c
MW
535 task = (task_t *)main_mode_create(this->ike_sa, FALSE);
536 this->passive_tasks->insert_last(this->passive_tasks, task);
b03c700d
CO
537 task = (task_t *)xauth_request_create(this->ike_sa, FALSE);
538 this->passive_tasks->insert_last(this->passive_tasks, task);
4a09d9ee
MW
539 break;
540 case AGGRESSIVE:
541 /* TODO-IKEv1: agressive mode */
542 return FAILED;
543 case QUICK_MODE:
744c0801
MW
544 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
545 NULL, NULL);
546 this->passive_tasks->insert_last(this->passive_tasks, task);
547 break;
4a09d9ee
MW
548 case INFORMATIONAL_V1:
549 /* TODO-IKEv1: informational */
550 return FAILED;
551 default:
552 return FAILED;
553 }
554 }
555 /* let the tasks process the message */
556 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
557 while (enumerator->enumerate(enumerator, (void*)&task))
558 {
559 switch (task->process(task, message))
560 {
561 case SUCCESS:
562 /* task completed, remove it */
563 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
564 task->destroy(task);
21b7db99
MW
565 enumerator->destroy(enumerator);
566 return SUCCESS;
b94f248e
CO
567 case MIGRATE:
568 /* task completed, remove it */
569 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
570 task->destroy(task);
571 enumerator->destroy(enumerator);
572 /* migrate the remaining tasks */
573 migrate_tasks(this->passive_tasks, this->queued_tasks);
574 /* Kick off the newly installed tasks */
575 initiate(this);
576 return SUCCESS;
4a09d9ee
MW
577 case NEED_MORE:
578 /* processed, but task needs at least another call to build() */
579 break;
580 case FAILED:
581 default:
582 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
583 /* FALL */
584 case DESTROY_ME:
585 /* critical failure, destroy IKE_SA */
586 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
587 enumerator->destroy(enumerator);
588 task->destroy(task);
589 return DESTROY_ME;
590 }
591 }
592 enumerator->destroy(enumerator);
593
e63cb7f8 594 return build_response(this, message);
4a09d9ee
MW
595}
596
26b55dc6
MW
597/**
598 * handle an incoming response message
599 */
600static status_t process_response(private_task_manager_t *this,
601 message_t *message)
602{
603 enumerator_t *enumerator;
604 task_t *task;
605
606 if (message->get_exchange_type(message) != this->initiating.type)
607 {
608 DBG1(DBG_IKE, "received %N response, but expected %N",
609 exchange_type_names, message->get_exchange_type(message),
610 exchange_type_names, this->initiating.type);
611 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
612 return DESTROY_ME;
613 }
614
615 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
616 while (enumerator->enumerate(enumerator, (void*)&task))
617 {
618 switch (task->process(task, message))
619 {
620 case SUCCESS:
621 /* task completed, remove it */
622 this->active_tasks->remove_at(this->active_tasks, enumerator);
623 task->destroy(task);
624 break;
625 case NEED_MORE:
626 /* processed, but task needs another exchange */
627 break;
628 case FAILED:
629 default:
630 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
631 /* FALL */
632 case DESTROY_ME:
633 /* critical failure, destroy IKE_SA */
634 this->active_tasks->remove_at(this->active_tasks, enumerator);
635 enumerator->destroy(enumerator);
636 task->destroy(task);
637 return DESTROY_ME;
638 }
639 }
640 enumerator->destroy(enumerator);
641
642 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
643 this->initiating.packet->destroy(this->initiating.packet);
644 this->initiating.packet = NULL;
645
646 return initiate(this);
647}
648
b235e69c
TB
649/**
650 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
651 */
652static void send_notify_response(private_task_manager_t *this,
653 message_t *request, notify_type_t type,
654 chunk_t data)
655{
656 message_t *response;
657 packet_t *packet;
658 host_t *me, *other;
659 u_int32_t mid;
660
6be8d33d
TB
661 if (request->get_exchange_type(request) == INFORMATIONAL_V1)
662 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
663 DBG1(DBG_IKE, "ignore malformed INFORMATIONAL request");
664 return;
665 }
666
b235e69c
TB
667 response = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
668 response->set_exchange_type(response, INFORMATIONAL_V1);
669 response->set_request(response, TRUE);
670 this->rng->get_bytes(this->rng, sizeof(mid), (void*)&mid);
671 response->set_message_id(response, mid);
672 response->add_notify(response, FALSE, type, data);
673 me = this->ike_sa->get_my_host(this->ike_sa);
674 if (me->is_anyaddr(me))
675 {
676 me = request->get_destination(request);
677 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
678 }
679 other = this->ike_sa->get_other_host(this->ike_sa);
680 if (other->is_anyaddr(other))
681 {
682 other = request->get_source(request);
683 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
684 }
685 response->set_source(response, me->clone(me));
686 response->set_destination(response, other->clone(other));
687 if (this->ike_sa->generate_message(this->ike_sa, response,
688 &packet) == SUCCESS)
689 {
690 charon->sender->send(charon->sender, packet);
691 }
692 response->destroy(response);
693}
694
695/**
696 * Parse the given message and verify that it is valid.
697 */
698static status_t parse_message(private_task_manager_t *this, message_t *msg)
699{
700 status_t status;
701
702 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
703
704 if (status != SUCCESS)
705 {
706 switch (status)
707 {
29a5e070
TB
708 case NOT_SUPPORTED:
709 DBG1(DBG_IKE, "unsupported exchange type");
710 send_notify_response(this, msg,
711 INVALID_EXCHANGE_TYPE, chunk_empty);
712 break;
b235e69c
TB
713 case PARSE_ERROR:
714 DBG1(DBG_IKE, "message parsing failed");
715 send_notify_response(this, msg,
716 PAYLOAD_MALFORMED, chunk_empty);
717 break;
718 case VERIFY_ERROR:
719 DBG1(DBG_IKE, "message verification failed");
720 send_notify_response(this, msg,
721 PAYLOAD_MALFORMED, chunk_empty);
722 break;
723 case FAILED:
724 DBG1(DBG_IKE, "integrity check failed");
725 send_notify_response(this, msg,
37639e94 726 INVALID_HASH_INFORMATION, chunk_empty);
b235e69c
TB
727 break;
728 case INVALID_STATE:
729 DBG1(DBG_IKE, "found encrypted message, but no keys available");
730 send_notify_response(this, msg,
731 PAYLOAD_MALFORMED, chunk_empty);
732 default:
733 break;
734 }
735 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
736 exchange_type_names, msg->get_exchange_type(msg),
737 msg->get_request(msg) ? "request" : "response",
738 msg->get_message_id(msg));
739
740 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED)
741 { /* invalid initiation attempt, close SA */
742 return DESTROY_ME;
743 }
744 }
745 return status;
746}
747
4a09d9ee
MW
748METHOD(task_manager_t, process_message, status_t,
749 private_task_manager_t *this, message_t *msg)
750{
73aaf76b 751 u_int32_t hash, mid;
3d59c5c3 752 host_t *me, *other;
68c6863b 753 status_t status;
73aaf76b 754
1960312c 755 /* TODO-IKEv1: update hosts more selectively */
3d59c5c3
TB
756 me = msg->get_destination(msg);
757 other = msg->get_source(msg);
68c6863b 758 mid = msg->get_message_id(msg);
1960312c 759
73aaf76b
MW
760 if ((mid && mid == this->initiating.mid) ||
761 (this->initiating.mid == 0 &&
762 this->active_tasks->get_count(this->active_tasks)))
4a09d9ee 763 {
7519106d 764 msg->set_request(msg, FALSE);
b235e69c 765 status = parse_message(this, msg);
1960312c
TB
766 if (status != SUCCESS)
767 {
768 return status;
769 }
770 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
771 time_monotonic(NULL));
3d59c5c3 772 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
73aaf76b
MW
773 charon->bus->message(charon->bus, msg, FALSE);
774 if (process_response(this, msg) != SUCCESS)
4a09d9ee
MW
775 {
776 flush(this);
777 return DESTROY_ME;
778 }
779 }
780 else
781 {
9cc8bd4f
MW
782 hash = chunk_hash(msg->get_packet_data(msg));
783 if (hash == this->responding.hash)
73aaf76b
MW
784 {
785 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
786 "retransmitting response", mid);
787 charon->sender->send(charon->sender,
788 this->responding.packet->clone(this->responding.packet));
789 return SUCCESS;
790 }
7519106d 791 msg->set_request(msg, TRUE);
b235e69c 792 status = parse_message(this, msg);
1960312c
TB
793 if (status != SUCCESS)
794 {
795 return status;
796 }
797 /* if this IKE_SA is virgin, we check for a config */
798 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
799 {
800 ike_sa_id_t *ike_sa_id;
801 ike_cfg_t *ike_cfg;
802 job_t *job;
803 ike_cfg = charon->backends->get_ike_cfg(charon->backends, me, other);
804 if (ike_cfg == NULL)
805 {
806 /* no config found for these hosts, destroy */
807 DBG1(DBG_IKE, "no IKE config found for %H...%H", me, other);
808 return DESTROY_ME;
809 }
810 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
811 ike_cfg->destroy(ike_cfg);
812 /* add a timeout if peer does not establish it completely */
813 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
814 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
815 lib->scheduler->schedule_job(lib->scheduler, job,
816 lib->settings->get_int(lib->settings,
817 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT));
818 }
819 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
820 time_monotonic(NULL));
3d59c5c3 821 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
73aaf76b
MW
822 charon->bus->message(charon->bus, msg, TRUE);
823 if (process_request(this, msg) != SUCCESS)
26b55dc6
MW
824 {
825 flush(this);
826 return DESTROY_ME;
827 }
73aaf76b 828
73aaf76b 829 this->responding.mid = mid;
9cc8bd4f 830 this->responding.hash = hash;
4a09d9ee
MW
831 }
832 return SUCCESS;
833}
834
835METHOD(task_manager_t, queue_task, void,
836 private_task_manager_t *this, task_t *task)
837{
838 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
839 this->queued_tasks->insert_last(this->queued_tasks, task);
840}
841
842METHOD(task_manager_t, adopt_tasks, void,
843 private_task_manager_t *this, task_manager_t *other_public)
844{
845 private_task_manager_t *other = (private_task_manager_t*)other_public;
846 task_t *task;
847
848 /* move queued tasks from other to this */
849 while (other->queued_tasks->remove_last(other->queued_tasks,
850 (void**)&task) == SUCCESS)
851 {
852 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
853 task->migrate(task, this->ike_sa);
854 this->queued_tasks->insert_first(this->queued_tasks, task);
855 }
856}
857
858METHOD(task_manager_t, busy, bool,
859 private_task_manager_t *this)
860{
861 return (this->active_tasks->get_count(this->active_tasks) > 0);
862}
863
864METHOD(task_manager_t, incr_mid, void,
865 private_task_manager_t *this, bool initiate)
866{
4a09d9ee
MW
867}
868
869METHOD(task_manager_t, reset, void,
870 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
871{
4a09d9ee
MW
872}
873
874METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
875 private_task_manager_t *this, task_queue_t queue)
876{
877 switch (queue)
878 {
879 case TASK_QUEUE_ACTIVE:
880 return this->active_tasks->create_enumerator(this->active_tasks);
881 case TASK_QUEUE_PASSIVE:
882 return this->passive_tasks->create_enumerator(this->passive_tasks);
883 case TASK_QUEUE_QUEUED:
884 return this->queued_tasks->create_enumerator(this->queued_tasks);
885 default:
886 return enumerator_create_empty();
887 }
888}
889
890METHOD(task_manager_t, destroy, void,
891 private_task_manager_t *this)
892{
893 flush(this);
894
895 this->active_tasks->destroy(this->active_tasks);
896 this->queued_tasks->destroy(this->queued_tasks);
897 this->passive_tasks->destroy(this->passive_tasks);
898
899 DESTROY_IF(this->responding.packet);
900 DESTROY_IF(this->initiating.packet);
73aaf76b 901 DESTROY_IF(this->rng);
4a09d9ee
MW
902 free(this);
903}
904
905/*
906 * see header file
907 */
908task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
909{
910 private_task_manager_t *this;
911
912 INIT(this,
913 .public = {
914 .task_manager = {
915 .process_message = _process_message,
916 .queue_task = _queue_task,
917 .initiate = _initiate,
918 .retransmit = _retransmit,
919 .incr_mid = _incr_mid,
920 .reset = _reset,
921 .adopt_tasks = _adopt_tasks,
922 .busy = _busy,
923 .create_task_enumerator = _create_task_enumerator,
924 .destroy = _destroy,
925 },
926 },
927 .ike_sa = ike_sa,
928 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
73aaf76b 929 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
4a09d9ee
MW
930 .queued_tasks = linked_list_create(),
931 .active_tasks = linked_list_create(),
932 .passive_tasks = linked_list_create(),
933 .retransmit_tries = lib->settings->get_int(lib->settings,
934 "charon.retransmit_tries", RETRANSMIT_TRIES),
935 .retransmit_timeout = lib->settings->get_double(lib->settings,
936 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT),
937 .retransmit_base = lib->settings->get_double(lib->settings,
938 "charon.retransmit_base", RETRANSMIT_BASE),
939 );
940
941 return &this->public;
942}