f9df2564ee428ae05e45805b2eba0cc6d03049d6
[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 break;
319 default:
320 break;
321 }
322 }
323 else
324 {
325 DBG2(DBG_IKE, "reinitiating already active tasks");
326 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
327 while (enumerator->enumerate(enumerator, (void**)&task))
328 {
329 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
330 switch (task->get_type(task))
331 {
332 case TASK_MAIN_MODE:
333 exchange = ID_PROT;
334 break;
335 case TASK_QUICK_MODE:
336 exchange = QUICK_MODE;
337 break;
338 case TASK_XAUTH:
339 exchange = TRANSACTION;
340 new_mid = TRUE;
341 break;
342 default:
343 continue;
344 }
345 break;
346 }
347 enumerator->destroy(enumerator);
348 }
349
350 if (exchange == EXCHANGE_TYPE_UNDEFINED)
351 {
352 DBG2(DBG_IKE, "nothing to initiate");
353 /* nothing to do yet... */
354 return SUCCESS;
355 }
356
357 me = this->ike_sa->get_my_host(this->ike_sa);
358 other = this->ike_sa->get_other_host(this->ike_sa);
359
360 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
361 if (new_mid)
362 {
363 this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
364 (void*)&this->initiating.mid);
365 }
366 message->set_message_id(message, this->initiating.mid);
367 message->set_source(message, me->clone(me));
368 message->set_destination(message, other->clone(other));
369 message->set_exchange_type(message, exchange);
370 this->initiating.type = exchange;
371 this->initiating.retransmitted = 0;
372
373 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
374 while (enumerator->enumerate(enumerator, (void*)&task))
375 {
376 switch (task->build(task, message))
377 {
378 case SUCCESS:
379 /* task completed, remove it */
380 this->active_tasks->remove_at(this->active_tasks, enumerator);
381 task->destroy(task);
382 continue;
383 case NEED_MORE:
384 expect_response = TRUE;
385 /* processed, but task needs another exchange */
386 continue;
387 case ALREADY_DONE:
388 flush_queue(this, this->active_tasks);
389 break;
390 case FAILED:
391 default:
392 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
393 {
394 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
395 }
396 /* FALL */
397 case DESTROY_ME:
398 /* critical failure, destroy IKE_SA */
399 enumerator->destroy(enumerator);
400 message->destroy(message);
401 flush(this);
402 return DESTROY_ME;
403 }
404 break;
405 }
406 enumerator->destroy(enumerator);
407
408 /* update exchange type if a task changed it */
409 this->initiating.type = message->get_exchange_type(message);
410 this->initiating.seqnr++;
411
412 status = this->ike_sa->generate_message(this->ike_sa, message,
413 &this->initiating.packet);
414 if (status != SUCCESS)
415 {
416 /* message generation failed. There is nothing more to do than to
417 * close the SA */
418 message->destroy(message);
419 flush(this);
420 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
421 return DESTROY_ME;
422 }
423 message->destroy(message);
424
425 if (expect_response)
426 {
427 return retransmit(this, this->initiating.seqnr);
428 }
429 charon->sender->send(charon->sender,
430 this->initiating.packet->clone(this->initiating.packet));
431 this->initiating.packet->destroy(this->initiating.packet);
432 this->initiating.packet = NULL;
433
434 /* close after sending an INFORMATIONAL error but not yet established */
435 if (this->initiating.type == INFORMATIONAL_V1 &&
436 this->ike_sa->get_state(this->ike_sa) == IKE_CONNECTING)
437 {
438 return FAILED;
439 }
440 return SUCCESS;
441 }
442
443 /**
444 * handle exchange collisions
445 */
446 static bool handle_collisions(private_task_manager_t *this, task_t *task)
447 {
448 return FALSE;
449 }
450
451 /**
452 * build a response depending on the "passive" task list
453 */
454 static status_t build_response(private_task_manager_t *this, message_t *request)
455 {
456 enumerator_t *enumerator;
457 task_t *task;
458 message_t *message;
459 host_t *me, *other;
460 bool delete = FALSE;
461 status_t status;
462
463 me = request->get_destination(request);
464 other = request->get_source(request);
465
466 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
467 message->set_exchange_type(message, request->get_exchange_type(request));
468 /* send response along the path the request came in */
469 message->set_source(message, me->clone(me));
470 message->set_destination(message, other->clone(other));
471 message->set_message_id(message, request->get_message_id(request));
472 message->set_request(message, FALSE);
473
474 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
475 while (enumerator->enumerate(enumerator, (void*)&task))
476 {
477 switch (task->build(task, message))
478 {
479 case SUCCESS:
480 /* task completed, remove it */
481 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
482 if (!handle_collisions(this, task))
483 {
484 task->destroy(task);
485 }
486 continue;
487 case NEED_MORE:
488 /* processed, but task needs another exchange */
489 if (handle_collisions(this, task))
490 {
491 this->passive_tasks->remove_at(this->passive_tasks,
492 enumerator);
493 }
494 continue;
495 case ALREADY_DONE:
496 flush_queue(this, this->passive_tasks);
497 break;
498 case FAILED:
499 default:
500 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
501 /* FALL */
502 case DESTROY_ME:
503 /* destroy IKE_SA, but SEND response first */
504 delete = TRUE;
505 break;
506 }
507 break;
508 }
509 enumerator->destroy(enumerator);
510
511 /* message complete, send it */
512 DESTROY_IF(this->responding.packet);
513 this->responding.packet = NULL;
514 status = this->ike_sa->generate_message(this->ike_sa, message,
515 &this->responding.packet);
516 message->destroy(message);
517 if (status != SUCCESS)
518 {
519 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
520 return DESTROY_ME;
521 }
522
523 charon->sender->send(charon->sender,
524 this->responding.packet->clone(this->responding.packet));
525 if (delete)
526 {
527 return DESTROY_ME;
528 }
529 return SUCCESS;
530 }
531
532 /**
533 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
534 * The notify protocol_id is set to ISAKMP
535 */
536 static void send_notify(private_task_manager_t *this, message_t *request,
537 notify_type_t type)
538 {
539 message_t *response;
540 packet_t *packet;
541 host_t *me, *other;
542 u_int32_t mid;
543
544 if (request && request->get_exchange_type(request) == INFORMATIONAL_V1)
545 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
546 DBG1(DBG_IKE, "ignore malformed INFORMATIONAL request");
547 return;
548 }
549
550 response = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
551 response->set_exchange_type(response, INFORMATIONAL_V1);
552 response->set_request(response, TRUE);
553 this->rng->get_bytes(this->rng, sizeof(mid), (void*)&mid);
554 response->set_message_id(response, mid);
555 response->add_payload(response, (payload_t*)
556 notify_payload_create_from_protocol_and_type(NOTIFY_V1,
557 PROTO_IKE, type));
558
559 me = this->ike_sa->get_my_host(this->ike_sa);
560 if (me->is_anyaddr(me))
561 {
562 me = request->get_destination(request);
563 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
564 }
565 other = this->ike_sa->get_other_host(this->ike_sa);
566 if (other->is_anyaddr(other))
567 {
568 other = request->get_source(request);
569 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
570 }
571 response->set_source(response, me->clone(me));
572 response->set_destination(response, other->clone(other));
573 if (this->ike_sa->generate_message(this->ike_sa, response,
574 &packet) == SUCCESS)
575 {
576 charon->sender->send(charon->sender, packet);
577 }
578 response->destroy(response);
579 }
580
581 /**
582 * handle an incoming request message
583 */
584 static status_t process_request(private_task_manager_t *this,
585 message_t *message)
586 {
587 enumerator_t *enumerator;
588 task_t *task = NULL;
589 bool send_response = FALSE;
590
591 if (this->passive_tasks->get_count(this->passive_tasks) == 0)
592 { /* create tasks depending on request type, if not already some queued */
593 switch (message->get_exchange_type(message))
594 {
595 case ID_PROT:
596 task = (task_t *)ike_vendor_v1_create(this->ike_sa, FALSE);
597 this->passive_tasks->insert_last(this->passive_tasks, task);
598 task = (task_t*)ike_cert_pre_v1_create(this->ike_sa, FALSE);
599 this->passive_tasks->insert_last(this->passive_tasks, task);
600 task = (task_t *)main_mode_create(this->ike_sa, FALSE);
601 this->passive_tasks->insert_last(this->passive_tasks, task);
602 task = (task_t*)ike_cert_post_v1_create(this->ike_sa, FALSE);
603 this->passive_tasks->insert_last(this->passive_tasks, task);
604 task = (task_t *)ike_natd_v1_create(this->ike_sa, FALSE);
605 this->passive_tasks->insert_last(this->passive_tasks, task);
606 break;
607 case AGGRESSIVE:
608 /* TODO-IKEv1: agressive mode */
609 return FAILED;
610 case QUICK_MODE:
611 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
612 {
613 DBG1(DBG_IKE, "received quick mode request for "
614 "unestablished IKE_SA, ignored");
615 return FAILED;
616 }
617 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
618 NULL, NULL);
619 this->passive_tasks->insert_last(this->passive_tasks, task);
620 break;
621 case INFORMATIONAL_V1:
622 task = (task_t *)informational_create(this->ike_sa, NULL);
623 this->passive_tasks->insert_last(this->passive_tasks, task);
624 break;
625 case TRANSACTION:
626 if (this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
627 {
628 task = (task_t *)mode_config_create(this->ike_sa, FALSE);
629 }
630 else
631 {
632 task = (task_t *)xauth_create(this->ike_sa, FALSE);
633 }
634 this->passive_tasks->insert_last(this->passive_tasks, task);
635 break;
636 default:
637 return FAILED;
638 }
639 }
640 /* let the tasks process the message */
641 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
642 while (enumerator->enumerate(enumerator, (void*)&task))
643 {
644 switch (task->process(task, message))
645 {
646 case SUCCESS:
647 /* task completed, remove it */
648 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
649 task->destroy(task);
650 continue;
651 case NEED_MORE:
652 /* processed, but task needs at least another call to build() */
653 send_response = TRUE;
654 continue;
655 case ALREADY_DONE:
656 send_response = FALSE;
657 flush_queue(this, this->passive_tasks);
658 break;
659 case FAILED:
660 default:
661 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
662 /* FALL */
663 case DESTROY_ME:
664 /* critical failure, destroy IKE_SA */
665 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
666 enumerator->destroy(enumerator);
667 task->destroy(task);
668 return DESTROY_ME;
669 }
670 break;
671 }
672 enumerator->destroy(enumerator);
673
674 if (send_response)
675 {
676 if (build_response(this, message) != SUCCESS)
677 {
678 return DESTROY_ME;
679 }
680 }
681 if (this->passive_tasks->get_count(this->passive_tasks) == 0 &&
682 this->queued_tasks->get_count(this->queued_tasks) > 0)
683 {
684 /* passive tasks completed, check if an active task has been queued,
685 * such as XAUTH or modeconfig push */
686 return initiate(this);
687 }
688 return SUCCESS;
689 }
690
691 /**
692 * handle an incoming response message
693 */
694 static status_t process_response(private_task_manager_t *this,
695 message_t *message)
696 {
697 enumerator_t *enumerator;
698 task_t *task;
699
700 if (message->get_exchange_type(message) != this->initiating.type)
701 {
702 DBG1(DBG_IKE, "received %N response, but expected %N",
703 exchange_type_names, message->get_exchange_type(message),
704 exchange_type_names, this->initiating.type);
705 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
706 return DESTROY_ME;
707 }
708
709 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
710 while (enumerator->enumerate(enumerator, (void*)&task))
711 {
712 switch (task->process(task, message))
713 {
714 case SUCCESS:
715 /* task completed, remove it */
716 this->active_tasks->remove_at(this->active_tasks, enumerator);
717 task->destroy(task);
718 continue;
719 case NEED_MORE:
720 /* processed, but task needs another exchange */
721 continue;
722 case ALREADY_DONE:
723 flush_queue(this, this->active_tasks);
724 break;
725 case FAILED:
726 default:
727 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
728 /* FALL */
729 case DESTROY_ME:
730 /* critical failure, destroy IKE_SA */
731 this->active_tasks->remove_at(this->active_tasks, enumerator);
732 enumerator->destroy(enumerator);
733 task->destroy(task);
734 return DESTROY_ME;
735 }
736 break;
737 }
738 enumerator->destroy(enumerator);
739
740 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
741 this->initiating.packet->destroy(this->initiating.packet);
742 this->initiating.packet = NULL;
743
744 return initiate(this);
745 }
746
747 /**
748 * Parse the given message and verify that it is valid.
749 */
750 static status_t parse_message(private_task_manager_t *this, message_t *msg)
751 {
752 status_t status;
753
754 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
755
756 if (status != SUCCESS)
757 {
758 switch (status)
759 {
760 case NOT_SUPPORTED:
761 DBG1(DBG_IKE, "unsupported exchange type");
762 send_notify(this, msg, INVALID_EXCHANGE_TYPE);
763 break;
764 case PARSE_ERROR:
765 DBG1(DBG_IKE, "message parsing failed");
766 send_notify(this, msg, PAYLOAD_MALFORMED);
767 break;
768 case VERIFY_ERROR:
769 DBG1(DBG_IKE, "message verification failed");
770 send_notify(this, msg, PAYLOAD_MALFORMED);
771 break;
772 case FAILED:
773 DBG1(DBG_IKE, "integrity check failed");
774 send_notify(this, msg, INVALID_HASH_INFORMATION);
775 break;
776 case INVALID_STATE:
777 DBG1(DBG_IKE, "found encrypted message, but no keys available");
778 send_notify(this, msg, PAYLOAD_MALFORMED);
779 default:
780 break;
781 }
782 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
783 exchange_type_names, msg->get_exchange_type(msg),
784 msg->get_request(msg) ? "request" : "response",
785 msg->get_message_id(msg));
786
787 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED)
788 { /* invalid initiation attempt, close SA */
789 return DESTROY_ME;
790 }
791 }
792 return status;
793 }
794
795 METHOD(task_manager_t, process_message, status_t,
796 private_task_manager_t *this, message_t *msg)
797 {
798 u_int32_t hash, mid;
799 host_t *me, *other;
800 status_t status;
801
802 /* TODO-IKEv1: update hosts more selectively */
803 me = msg->get_destination(msg);
804 other = msg->get_source(msg);
805 mid = msg->get_message_id(msg);
806
807 if ((mid && mid == this->initiating.mid) ||
808 (this->initiating.mid == 0 &&
809 msg->get_exchange_type(msg) == this->initiating.type &&
810 this->active_tasks->get_count(this->active_tasks)))
811 {
812 msg->set_request(msg, FALSE);
813 status = parse_message(this, msg);
814 if (status != SUCCESS)
815 {
816 return status;
817 }
818 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
819 time_monotonic(NULL));
820 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
821 charon->bus->message(charon->bus, msg, FALSE);
822 if (process_response(this, msg) != SUCCESS)
823 {
824 flush(this);
825 return DESTROY_ME;
826 }
827 }
828 else
829 {
830 hash = chunk_hash(msg->get_packet_data(msg));
831 if (hash == this->responding.hash && this->responding.packet)
832 {
833 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
834 "retransmitting response", mid);
835 charon->sender->send(charon->sender,
836 this->responding.packet->clone(this->responding.packet));
837 return SUCCESS;
838 }
839 msg->set_request(msg, TRUE);
840 status = parse_message(this, msg);
841 if (status != SUCCESS)
842 {
843 return status;
844 }
845 /* if this IKE_SA is virgin, we check for a config */
846 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
847 {
848 ike_sa_id_t *ike_sa_id;
849 ike_cfg_t *ike_cfg;
850 job_t *job;
851 ike_cfg = charon->backends->get_ike_cfg(charon->backends, me, other);
852 if (ike_cfg == NULL)
853 {
854 /* no config found for these hosts, destroy */
855 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
856 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
857 send_notify(this, msg, NO_PROPOSAL_CHOSEN);
858 return DESTROY_ME;
859 }
860 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
861 ike_cfg->destroy(ike_cfg);
862 /* add a timeout if peer does not establish it completely */
863 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
864 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
865 lib->scheduler->schedule_job(lib->scheduler, job,
866 lib->settings->get_int(lib->settings,
867 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT));
868 }
869 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
870 time_monotonic(NULL));
871 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
872 charon->bus->message(charon->bus, msg, TRUE);
873 if (process_request(this, msg) != SUCCESS)
874 {
875 flush(this);
876 return DESTROY_ME;
877 }
878
879 this->responding.mid = mid;
880 this->responding.hash = hash;
881 }
882 return SUCCESS;
883 }
884
885 METHOD(task_manager_t, queue_task, void,
886 private_task_manager_t *this, task_t *task)
887 {
888 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
889 this->queued_tasks->insert_last(this->queued_tasks, task);
890 }
891
892 METHOD(task_manager_t, adopt_tasks, void,
893 private_task_manager_t *this, task_manager_t *other_public)
894 {
895 private_task_manager_t *other = (private_task_manager_t*)other_public;
896 task_t *task;
897
898 /* move queued tasks from other to this */
899 while (other->queued_tasks->remove_last(other->queued_tasks,
900 (void**)&task) == SUCCESS)
901 {
902 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
903 task->migrate(task, this->ike_sa);
904 this->queued_tasks->insert_first(this->queued_tasks, task);
905 }
906 }
907
908 METHOD(task_manager_t, busy, bool,
909 private_task_manager_t *this)
910 {
911 return (this->active_tasks->get_count(this->active_tasks) > 0);
912 }
913
914 METHOD(task_manager_t, incr_mid, void,
915 private_task_manager_t *this, bool initiate)
916 {
917 }
918
919 METHOD(task_manager_t, reset, void,
920 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
921 {
922 }
923
924 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
925 private_task_manager_t *this, task_queue_t queue)
926 {
927 switch (queue)
928 {
929 case TASK_QUEUE_ACTIVE:
930 return this->active_tasks->create_enumerator(this->active_tasks);
931 case TASK_QUEUE_PASSIVE:
932 return this->passive_tasks->create_enumerator(this->passive_tasks);
933 case TASK_QUEUE_QUEUED:
934 return this->queued_tasks->create_enumerator(this->queued_tasks);
935 default:
936 return enumerator_create_empty();
937 }
938 }
939
940 METHOD(task_manager_t, destroy, void,
941 private_task_manager_t *this)
942 {
943 flush(this);
944
945 this->active_tasks->destroy(this->active_tasks);
946 this->queued_tasks->destroy(this->queued_tasks);
947 this->passive_tasks->destroy(this->passive_tasks);
948
949 DESTROY_IF(this->responding.packet);
950 DESTROY_IF(this->initiating.packet);
951 DESTROY_IF(this->rng);
952 free(this);
953 }
954
955 /*
956 * see header file
957 */
958 task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
959 {
960 private_task_manager_t *this;
961
962 INIT(this,
963 .public = {
964 .task_manager = {
965 .process_message = _process_message,
966 .queue_task = _queue_task,
967 .initiate = _initiate,
968 .retransmit = _retransmit,
969 .incr_mid = _incr_mid,
970 .reset = _reset,
971 .adopt_tasks = _adopt_tasks,
972 .busy = _busy,
973 .create_task_enumerator = _create_task_enumerator,
974 .destroy = _destroy,
975 },
976 },
977 .ike_sa = ike_sa,
978 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
979 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
980 .queued_tasks = linked_list_create(),
981 .active_tasks = linked_list_create(),
982 .passive_tasks = linked_list_create(),
983 .retransmit_tries = lib->settings->get_int(lib->settings,
984 "charon.retransmit_tries", RETRANSMIT_TRIES),
985 .retransmit_timeout = lib->settings->get_double(lib->settings,
986 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT),
987 .retransmit_base = lib->settings->get_double(lib->settings,
988 "charon.retransmit_base", RETRANSMIT_BASE),
989 );
990
991 return &this->public;
992 }