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