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