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