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