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