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