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