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