Raise a bus alert when IKE message body parsing fails
[strongswan.git] / src / libcharon / sa / ikev1 / 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/ikev1/tasks/main_mode.h>
23 #include <sa/ikev1/tasks/aggressive_mode.h>
24 #include <sa/ikev1/tasks/quick_mode.h>
25 #include <sa/ikev1/tasks/quick_delete.h>
26 #include <sa/ikev1/tasks/xauth.h>
27 #include <sa/ikev1/tasks/mode_config.h>
28 #include <sa/ikev1/tasks/informational.h>
29 #include <sa/ikev1/tasks/isakmp_natd.h>
30 #include <sa/ikev1/tasks/isakmp_vendor.h>
31 #include <sa/ikev1/tasks/isakmp_cert_pre.h>
32 #include <sa/ikev1/tasks/isakmp_cert_post.h>
33 #include <sa/ikev1/tasks/isakmp_delete.h>
34 #include <sa/ikev1/tasks/isakmp_dpd.h>
35
36 #include <processing/jobs/retransmit_job.h>
37 #include <processing/jobs/delete_ike_sa_job.h>
38 #include <processing/jobs/dpd_timeout_job.h>
39
40 /**
41 * Number of old messages hashes we keep for retransmission.
42 *
43 * In Main Mode, we must ignore messages from a previous message pair if
44 * we already continued to the next. Otherwise a late retransmission
45 * could be considered as a reply to the newer request.
46 */
47 #define MAX_OLD_HASHES 2
48
49 /**
50 * First sequence number of responding packets.
51 *
52 * To distinguish retransmission jobs for initiating and responding packets,
53 * we split up the sequence counter and use the upper half for responding.
54 */
55 #define RESPONDING_SEQ INT_MAX
56
57 typedef struct exchange_t exchange_t;
58
59 /**
60 * An exchange in the air, used do detect and handle retransmission
61 */
62 struct exchange_t {
63
64 /**
65 * Message ID used for this transaction
66 */
67 u_int32_t mid;
68
69 /**
70 * generated packet for retransmission
71 */
72 packet_t *packet;
73 };
74
75 typedef struct private_task_manager_t private_task_manager_t;
76
77 /**
78 * private data of the task manager
79 */
80 struct private_task_manager_t {
81
82 /**
83 * public functions
84 */
85 task_manager_v1_t public;
86
87 /**
88 * associated IKE_SA we are serving
89 */
90 ike_sa_t *ike_sa;
91
92 /**
93 * RNG to create message IDs
94 */
95 rng_t *rng;
96
97 /**
98 * Exchange we are currently handling as responder
99 */
100 struct {
101 /**
102 * Message ID of the last response
103 */
104 u_int32_t mid;
105
106 /**
107 * Hash of a previously received message
108 */
109 u_int32_t hash;
110
111 /**
112 * packet for retransmission
113 */
114 packet_t *packet;
115
116 /**
117 * Sequence number of the last sent message
118 */
119 u_int32_t seqnr;
120
121 /**
122 * how many times we have retransmitted so far
123 */
124 u_int retransmitted;
125
126 } responding;
127
128 /**
129 * Exchange we are currently handling as initiator
130 */
131 struct {
132 /**
133 * Message ID of the exchange
134 */
135 u_int32_t mid;
136
137 /**
138 * Hashes of old responses we can ignore
139 */
140 u_int32_t old_hashes[MAX_OLD_HASHES];
141
142 /**
143 * Position in old hash array
144 */
145 int old_hash_pos;
146
147 /**
148 * Sequence number of the last sent message
149 */
150 u_int32_t seqnr;
151
152 /**
153 * how many times we have retransmitted so far
154 */
155 u_int retransmitted;
156
157 /**
158 * packet for retransmission
159 */
160 packet_t *packet;
161
162 /**
163 * type of the initated exchange
164 */
165 exchange_type_t type;
166
167 } initiating;
168
169 /**
170 * List of queued tasks not yet in action
171 */
172 linked_list_t *queued_tasks;
173
174 /**
175 * List of active tasks, initiated by ourselve
176 */
177 linked_list_t *active_tasks;
178
179 /**
180 * List of tasks initiated by peer
181 */
182 linked_list_t *passive_tasks;
183
184 /**
185 * Queued messages not yet ready to process
186 */
187 message_t *queued;
188
189 /**
190 * Number of times we retransmit messages before giving up
191 */
192 u_int retransmit_tries;
193
194 /**
195 * Retransmission timeout
196 */
197 double retransmit_timeout;
198
199 /**
200 * Base to calculate retransmission timeout
201 */
202 double retransmit_base;
203
204 /**
205 * Sequence number for sending DPD requests
206 */
207 u_int32_t dpd_send;
208
209 /**
210 * Sequence number for received DPD requests
211 */
212 u_int32_t dpd_recv;
213 };
214
215 METHOD(task_manager_t, flush_queue, void,
216 private_task_manager_t *this, task_queue_t queue)
217 {
218 linked_list_t *list;
219 task_t *task;
220
221 if (this->queued)
222 {
223 this->queued->destroy(this->queued);
224 this->queued = NULL;
225 }
226 switch (queue)
227 {
228 case TASK_QUEUE_ACTIVE:
229 list = this->active_tasks;
230 /* cancel pending retransmits */
231 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
232 DESTROY_IF(this->initiating.packet);
233 this->initiating.packet = NULL;
234 break;
235 case TASK_QUEUE_PASSIVE:
236 list = this->passive_tasks;
237 break;
238 case TASK_QUEUE_QUEUED:
239 list = this->queued_tasks;
240 break;
241 default:
242 return;
243 }
244 while (list->remove_last(list, (void**)&task) == SUCCESS)
245 {
246 task->destroy(task);
247 }
248 }
249
250 /**
251 * flush all tasks in the task manager
252 */
253 static void flush(private_task_manager_t *this)
254 {
255 flush_queue(this, TASK_QUEUE_QUEUED);
256 flush_queue(this, TASK_QUEUE_PASSIVE);
257 flush_queue(this, TASK_QUEUE_ACTIVE);
258 }
259
260 /**
261 * move a task of a specific type from the queue to the active list
262 */
263 static bool activate_task(private_task_manager_t *this, task_type_t type)
264 {
265 enumerator_t *enumerator;
266 task_t *task;
267 bool found = FALSE;
268
269 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
270 while (enumerator->enumerate(enumerator, (void**)&task))
271 {
272 if (task->get_type(task) == type)
273 {
274 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
275 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
276 this->active_tasks->insert_last(this->active_tasks, task);
277 found = TRUE;
278 break;
279 }
280 }
281 enumerator->destroy(enumerator);
282 return found;
283 }
284
285 /**
286 * Retransmit a packet, either as initiator or as responder
287 */
288 static status_t retransmit_packet(private_task_manager_t *this, u_int32_t seqnr,
289 u_int mid, u_int retransmitted, packet_t *packet)
290 {
291 u_int32_t t;
292
293 if (retransmitted > this->retransmit_tries)
294 {
295 DBG1(DBG_IKE, "giving up after %u retransmits", retransmitted - 1);
296 return DESTROY_ME;
297 }
298 t = (u_int32_t)(this->retransmit_timeout * 1000.0 *
299 pow(this->retransmit_base, retransmitted));
300 if (retransmitted)
301 {
302 DBG1(DBG_IKE, "sending retransmit %u of %s message ID %u, seq %u",
303 retransmitted, seqnr < RESPONDING_SEQ ? "request" : "response",
304 mid, seqnr < RESPONDING_SEQ ? seqnr : seqnr - RESPONDING_SEQ);
305 }
306 charon->sender->send(charon->sender, packet->clone(packet));
307 lib->scheduler->schedule_job_ms(lib->scheduler, (job_t*)
308 retransmit_job_create(seqnr, this->ike_sa->get_id(this->ike_sa)), t);
309 return NEED_MORE;
310 }
311
312 METHOD(task_manager_t, retransmit, status_t,
313 private_task_manager_t *this, u_int32_t seqnr)
314 {
315 status_t status = SUCCESS;
316
317 if (seqnr == this->initiating.seqnr && this->initiating.packet)
318 {
319 status = retransmit_packet(this, seqnr, this->initiating.mid,
320 this->initiating.retransmitted, this->initiating.packet);
321 if (status == NEED_MORE)
322 {
323 this->initiating.retransmitted++;
324 status = SUCCESS;
325 }
326 }
327 if (seqnr == this->responding.seqnr && this->responding.packet)
328 {
329 status = retransmit_packet(this, seqnr, this->responding.mid,
330 this->responding.retransmitted, this->responding.packet);
331 if (status == NEED_MORE)
332 {
333 this->responding.retransmitted++;
334 status = SUCCESS;
335 }
336 }
337 return status;
338 }
339
340 /**
341 * Check if we have to wait for a mode config before starting a quick mode
342 */
343 static bool mode_config_expected(private_task_manager_t *this)
344 {
345 enumerator_t *enumerator;
346 peer_cfg_t *peer_cfg;
347 char *pool;
348 host_t *host;
349
350 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
351 if (peer_cfg)
352 {
353 enumerator = peer_cfg->create_pool_enumerator(peer_cfg);
354 if (!enumerator->enumerate(enumerator, &pool))
355 { /* no pool configured */
356 enumerator->destroy(enumerator);
357 return FALSE;
358 }
359 enumerator->destroy(enumerator);
360
361 enumerator = this->ike_sa->create_virtual_ip_enumerator(this->ike_sa,
362 FALSE);
363 if (!enumerator->enumerate(enumerator, &host))
364 { /* have a pool, but no VIP assigned yet */
365 enumerator->destroy(enumerator);
366 return TRUE;
367 }
368 enumerator->destroy(enumerator);
369 }
370 return FALSE;
371 }
372
373 METHOD(task_manager_t, initiate, status_t,
374 private_task_manager_t *this)
375 {
376 enumerator_t *enumerator;
377 task_t *task;
378 message_t *message;
379 host_t *me, *other;
380 status_t status;
381 exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED;
382 bool new_mid = FALSE, expect_response = FALSE, cancelled = FALSE, keep = FALSE;
383
384 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED &&
385 this->initiating.type != INFORMATIONAL_V1)
386 {
387 DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
388 exchange_type_names, this->initiating.type);
389 /* do not initiate if we already have a message in the air */
390 return SUCCESS;
391 }
392
393 if (this->active_tasks->get_count(this->active_tasks) == 0)
394 {
395 DBG2(DBG_IKE, "activating new tasks");
396 switch (this->ike_sa->get_state(this->ike_sa))
397 {
398 case IKE_CREATED:
399 activate_task(this, TASK_ISAKMP_VENDOR);
400 activate_task(this, TASK_ISAKMP_CERT_PRE);
401 if (activate_task(this, TASK_MAIN_MODE))
402 {
403 exchange = ID_PROT;
404 }
405 else if (activate_task(this, TASK_AGGRESSIVE_MODE))
406 {
407 exchange = AGGRESSIVE;
408 }
409 activate_task(this, TASK_ISAKMP_CERT_POST);
410 activate_task(this, TASK_ISAKMP_NATD);
411 break;
412 case IKE_CONNECTING:
413 if (activate_task(this, TASK_ISAKMP_DELETE))
414 {
415 exchange = INFORMATIONAL_V1;
416 new_mid = TRUE;
417 break;
418 }
419 if (activate_task(this, TASK_XAUTH))
420 {
421 exchange = TRANSACTION;
422 new_mid = TRUE;
423 break;
424 }
425 if (activate_task(this, TASK_INFORMATIONAL))
426 {
427 exchange = INFORMATIONAL_V1;
428 new_mid = TRUE;
429 break;
430 }
431 break;
432 case IKE_ESTABLISHED:
433 if (activate_task(this, TASK_MODE_CONFIG))
434 {
435 exchange = TRANSACTION;
436 new_mid = TRUE;
437 break;
438 }
439 if (!mode_config_expected(this) &&
440 activate_task(this, TASK_QUICK_MODE))
441 {
442 exchange = QUICK_MODE;
443 new_mid = TRUE;
444 break;
445 }
446 if (activate_task(this, TASK_INFORMATIONAL))
447 {
448 exchange = INFORMATIONAL_V1;
449 new_mid = TRUE;
450 break;
451 }
452 if (activate_task(this, TASK_QUICK_DELETE))
453 {
454 exchange = INFORMATIONAL_V1;
455 new_mid = TRUE;
456 break;
457 }
458 if (activate_task(this, TASK_ISAKMP_DELETE))
459 {
460 exchange = INFORMATIONAL_V1;
461 new_mid = TRUE;
462 break;
463 }
464 if (activate_task(this, TASK_ISAKMP_DPD))
465 {
466 exchange = INFORMATIONAL_V1;
467 new_mid = TRUE;
468 break;
469 }
470 break;
471 default:
472 break;
473 }
474 }
475 else
476 {
477 DBG2(DBG_IKE, "reinitiating already active tasks");
478 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
479 while (enumerator->enumerate(enumerator, (void**)&task))
480 {
481 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
482 switch (task->get_type(task))
483 {
484 case TASK_MAIN_MODE:
485 exchange = ID_PROT;
486 break;
487 case TASK_AGGRESSIVE_MODE:
488 exchange = AGGRESSIVE;
489 break;
490 case TASK_QUICK_MODE:
491 exchange = QUICK_MODE;
492 break;
493 case TASK_XAUTH:
494 exchange = TRANSACTION;
495 new_mid = TRUE;
496 break;
497 default:
498 continue;
499 }
500 break;
501 }
502 enumerator->destroy(enumerator);
503 }
504
505 if (exchange == EXCHANGE_TYPE_UNDEFINED)
506 {
507 DBG2(DBG_IKE, "nothing to initiate");
508 /* nothing to do yet... */
509 return SUCCESS;
510 }
511
512 me = this->ike_sa->get_my_host(this->ike_sa);
513 other = this->ike_sa->get_other_host(this->ike_sa);
514
515 if (new_mid)
516 {
517 if (!this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
518 (void*)&this->initiating.mid))
519 {
520 DBG1(DBG_IKE, "failed to allocate message ID, destroying IKE_SA");
521 flush(this);
522 return DESTROY_ME;
523 }
524 }
525 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
526 message->set_message_id(message, this->initiating.mid);
527 message->set_source(message, me->clone(me));
528 message->set_destination(message, other->clone(other));
529 message->set_exchange_type(message, exchange);
530 this->initiating.type = exchange;
531 this->initiating.retransmitted = 0;
532
533 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
534 while (enumerator->enumerate(enumerator, (void*)&task))
535 {
536 switch (task->build(task, message))
537 {
538 case SUCCESS:
539 /* task completed, remove it */
540 this->active_tasks->remove_at(this->active_tasks, enumerator);
541 if (task->get_type(task) == TASK_AGGRESSIVE_MODE ||
542 task->get_type(task) == TASK_QUICK_MODE)
543 { /* last message of three message exchange */
544 keep = TRUE;
545 }
546 task->destroy(task);
547 continue;
548 case NEED_MORE:
549 expect_response = TRUE;
550 /* processed, but task needs another exchange */
551 continue;
552 case ALREADY_DONE:
553 cancelled = TRUE;
554 break;
555 case FAILED:
556 default:
557 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
558 {
559 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
560 }
561 /* FALL */
562 case DESTROY_ME:
563 /* critical failure, destroy IKE_SA */
564 enumerator->destroy(enumerator);
565 message->destroy(message);
566 flush(this);
567 return DESTROY_ME;
568 }
569 break;
570 }
571 enumerator->destroy(enumerator);
572
573 if (this->active_tasks->get_count(this->active_tasks) == 0 &&
574 (exchange == QUICK_MODE || exchange == AGGRESSIVE))
575 { /* tasks completed, no exchange active anymore */
576 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
577 }
578 if (cancelled)
579 {
580 message->destroy(message);
581 return initiate(this);
582 }
583
584 DESTROY_IF(this->initiating.packet);
585 status = this->ike_sa->generate_message(this->ike_sa, message,
586 &this->initiating.packet);
587 if (status != SUCCESS)
588 {
589 /* message generation failed. There is nothing more to do than to
590 * close the SA */
591 message->destroy(message);
592 flush(this);
593 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
594 return DESTROY_ME;
595 }
596
597 this->initiating.seqnr++;
598 if (expect_response)
599 {
600 message->destroy(message);
601 return retransmit(this, this->initiating.seqnr);
602 }
603 if (keep)
604 { /* keep the packet for retransmission, the responder might request it */
605 charon->sender->send(charon->sender,
606 this->initiating.packet->clone(this->initiating.packet));
607 }
608 else
609 {
610 charon->sender->send(charon->sender, this->initiating.packet);
611 this->initiating.packet = NULL;
612 }
613 message->destroy(message);
614
615 if (exchange == INFORMATIONAL_V1)
616 {
617 switch (this->ike_sa->get_state(this->ike_sa))
618 {
619 case IKE_CONNECTING:
620 /* close after sending an INFORMATIONAL when unestablished */
621 return FAILED;
622 case IKE_DELETING:
623 /* close after sending a DELETE */
624 return DESTROY_ME;
625 default:
626 break;
627 }
628 }
629 return initiate(this);
630 }
631
632 /**
633 * build a response depending on the "passive" task list
634 */
635 static status_t build_response(private_task_manager_t *this, message_t *request)
636 {
637 enumerator_t *enumerator;
638 task_t *task;
639 message_t *message;
640 host_t *me, *other;
641 bool delete = FALSE, cancelled = FALSE, expect_request = FALSE;
642 status_t status;
643
644 me = request->get_destination(request);
645 other = request->get_source(request);
646
647 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
648 message->set_exchange_type(message, request->get_exchange_type(request));
649 /* send response along the path the request came in */
650 message->set_source(message, me->clone(me));
651 message->set_destination(message, other->clone(other));
652 message->set_message_id(message, request->get_message_id(request));
653 message->set_request(message, FALSE);
654
655 this->responding.mid = request->get_message_id(request);
656 this->responding.retransmitted = 0;
657 this->responding.seqnr++;
658
659 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
660 while (enumerator->enumerate(enumerator, (void*)&task))
661 {
662 switch (task->build(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 continue;
669 case NEED_MORE:
670 /* processed, but task needs another exchange */
671 if (task->get_type(task) == TASK_QUICK_MODE ||
672 task->get_type(task) == TASK_AGGRESSIVE_MODE)
673 { /* we rely on initiator retransmission, except for
674 * three-message exchanges */
675 expect_request = TRUE;
676 }
677 continue;
678 case ALREADY_DONE:
679 cancelled = TRUE;
680 break;
681 case FAILED:
682 default:
683 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
684 /* FALL */
685 case DESTROY_ME:
686 /* destroy IKE_SA, but SEND response first */
687 delete = TRUE;
688 break;
689 }
690 break;
691 }
692 enumerator->destroy(enumerator);
693
694 DESTROY_IF(this->responding.packet);
695 this->responding.packet = NULL;
696 if (cancelled)
697 {
698 message->destroy(message);
699 return initiate(this);
700 }
701 status = this->ike_sa->generate_message(this->ike_sa, message,
702 &this->responding.packet);
703 message->destroy(message);
704 if (status != SUCCESS)
705 {
706 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
707 return DESTROY_ME;
708 }
709
710 if (expect_request && !delete)
711 {
712 return retransmit(this, this->responding.seqnr);
713 }
714 charon->sender->send(charon->sender,
715 this->responding.packet->clone(this->responding.packet));
716 if (delete)
717 {
718 return DESTROY_ME;
719 }
720 return SUCCESS;
721 }
722
723 /**
724 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
725 * The notify protocol_id is set to ISAKMP
726 */
727 static void send_notify(private_task_manager_t *this, message_t *request,
728 notify_type_t type)
729 {
730 message_t *response;
731 packet_t *packet;
732 host_t *me, *other;
733 u_int32_t mid;
734
735 if (request->get_exchange_type(request) == INFORMATIONAL_V1)
736 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
737 DBG1(DBG_IKE, "ignore malformed INFORMATIONAL request");
738 return;
739 }
740 if (!this->rng->get_bytes(this->rng, sizeof(mid), (void*)&mid))
741 {
742 DBG1(DBG_IKE, "failed to allocate message ID");
743 return;
744 }
745 response = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
746 response->set_exchange_type(response, INFORMATIONAL_V1);
747 response->set_request(response, TRUE);
748 response->set_message_id(response, mid);
749 response->add_payload(response, (payload_t*)
750 notify_payload_create_from_protocol_and_type(NOTIFY_V1,
751 PROTO_IKE, type));
752
753 me = this->ike_sa->get_my_host(this->ike_sa);
754 if (me->is_anyaddr(me))
755 {
756 me = request->get_destination(request);
757 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
758 }
759 other = this->ike_sa->get_other_host(this->ike_sa);
760 if (other->is_anyaddr(other))
761 {
762 other = request->get_source(request);
763 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
764 }
765 response->set_source(response, me->clone(me));
766 response->set_destination(response, other->clone(other));
767 if (this->ike_sa->generate_message(this->ike_sa, response,
768 &packet) == SUCCESS)
769 {
770 charon->sender->send(charon->sender, packet);
771 }
772 response->destroy(response);
773 }
774
775 /**
776 * Process a DPD request/response
777 */
778 static bool process_dpd(private_task_manager_t *this, message_t *message)
779 {
780 notify_payload_t *notify;
781 notify_type_t type;
782 u_int32_t seq;
783 chunk_t data;
784
785 type = DPD_R_U_THERE;
786 notify = message->get_notify(message, type);
787 if (!notify)
788 {
789 type = DPD_R_U_THERE_ACK;
790 notify = message->get_notify(message, type);
791 }
792 if (!notify)
793 {
794 return FALSE;
795 }
796 data = notify->get_notification_data(notify);
797 if (data.len != 4)
798 {
799 return FALSE;
800 }
801 seq = untoh32(data.ptr);
802
803 if (type == DPD_R_U_THERE)
804 {
805 if (this->dpd_recv == 0 || seq == this->dpd_recv)
806 { /* check sequence validity */
807 this->dpd_recv = seq + 1;
808 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
809 time_monotonic(NULL));
810 }
811 /* but respond anyway */
812 this->ike_sa->queue_task(this->ike_sa,
813 &isakmp_dpd_create(this->ike_sa, DPD_R_U_THERE_ACK, seq)->task);
814 }
815 else /* DPD_R_U_THERE_ACK */
816 {
817 if (seq == this->dpd_send - 1)
818 {
819 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
820 time_monotonic(NULL));
821 }
822 else
823 {
824 DBG1(DBG_IKE, "received invalid DPD sequence number %u "
825 "(expected %u), ignored", seq, this->dpd_send - 1);
826 }
827 }
828 return TRUE;
829 }
830
831 /**
832 * handle an incoming request message
833 */
834 static status_t process_request(private_task_manager_t *this,
835 message_t *message)
836 {
837 enumerator_t *enumerator;
838 task_t *task = NULL;
839 bool send_response = FALSE, dpd = FALSE;
840
841 if (message->get_exchange_type(message) == INFORMATIONAL_V1 ||
842 this->passive_tasks->get_count(this->passive_tasks) == 0)
843 { /* create tasks depending on request type, if not already some queued */
844 switch (message->get_exchange_type(message))
845 {
846 case ID_PROT:
847 task = (task_t *)isakmp_vendor_create(this->ike_sa, FALSE);
848 this->passive_tasks->insert_last(this->passive_tasks, task);
849 task = (task_t*)isakmp_cert_pre_create(this->ike_sa, FALSE);
850 this->passive_tasks->insert_last(this->passive_tasks, task);
851 task = (task_t *)main_mode_create(this->ike_sa, FALSE);
852 this->passive_tasks->insert_last(this->passive_tasks, task);
853 task = (task_t*)isakmp_cert_post_create(this->ike_sa, FALSE);
854 this->passive_tasks->insert_last(this->passive_tasks, task);
855 task = (task_t *)isakmp_natd_create(this->ike_sa, FALSE);
856 this->passive_tasks->insert_last(this->passive_tasks, task);
857 break;
858 case AGGRESSIVE:
859 task = (task_t *)isakmp_vendor_create(this->ike_sa, FALSE);
860 this->passive_tasks->insert_last(this->passive_tasks, task);
861 task = (task_t*)isakmp_cert_pre_create(this->ike_sa, FALSE);
862 this->passive_tasks->insert_last(this->passive_tasks, task);
863 task = (task_t *)aggressive_mode_create(this->ike_sa, FALSE);
864 this->passive_tasks->insert_last(this->passive_tasks, task);
865 task = (task_t*)isakmp_cert_post_create(this->ike_sa, FALSE);
866 this->passive_tasks->insert_last(this->passive_tasks, task);
867 task = (task_t *)isakmp_natd_create(this->ike_sa, FALSE);
868 this->passive_tasks->insert_last(this->passive_tasks, task);
869 break;
870 case QUICK_MODE:
871 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
872 {
873 DBG1(DBG_IKE, "received quick mode request for "
874 "unestablished IKE_SA, ignored");
875 return FAILED;
876 }
877 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
878 NULL, NULL);
879 this->passive_tasks->insert_last(this->passive_tasks, task);
880 break;
881 case INFORMATIONAL_V1:
882 if (process_dpd(this, message))
883 {
884 dpd = TRUE;
885 }
886 else
887 {
888 task = (task_t *)informational_create(this->ike_sa, NULL);
889 this->passive_tasks->insert_first(this->passive_tasks, task);
890 }
891 break;
892 case TRANSACTION:
893 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
894 {
895 task = (task_t *)mode_config_create(this->ike_sa, FALSE);
896 }
897 else
898 {
899 task = (task_t *)xauth_create(this->ike_sa, FALSE);
900 }
901 this->passive_tasks->insert_last(this->passive_tasks, task);
902 break;
903 default:
904 return FAILED;
905 }
906 }
907 if (dpd)
908 {
909 return initiate(this);
910 }
911 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND, time_monotonic(NULL));
912
913 /* let the tasks process the message */
914 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
915 while (enumerator->enumerate(enumerator, (void*)&task))
916 {
917 switch (task->process(task, message))
918 {
919 case SUCCESS:
920 /* task completed, remove it */
921 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
922 task->destroy(task);
923 continue;
924 case NEED_MORE:
925 /* processed, but task needs at least another call to build() */
926 send_response = TRUE;
927 continue;
928 case ALREADY_DONE:
929 send_response = FALSE;
930 break;
931 case FAILED:
932 default:
933 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
934 /* FALL */
935 case DESTROY_ME:
936 /* critical failure, destroy IKE_SA */
937 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
938 enumerator->destroy(enumerator);
939 task->destroy(task);
940 return DESTROY_ME;
941 }
942 break;
943 }
944 enumerator->destroy(enumerator);
945
946 if (send_response)
947 {
948 if (build_response(this, message) != SUCCESS)
949 {
950 return DESTROY_ME;
951 }
952 }
953 else
954 { /* We don't send a response, so don't retransmit one if we get
955 * the same message again. */
956 DESTROY_IF(this->responding.packet);
957 this->responding.packet = NULL;
958 }
959 if (this->passive_tasks->get_count(this->passive_tasks) == 0 &&
960 this->queued_tasks->get_count(this->queued_tasks) > 0)
961 {
962 /* passive tasks completed, check if an active task has been queued,
963 * such as XAUTH or modeconfig push */
964 return initiate(this);
965 }
966 return SUCCESS;
967 }
968
969 /**
970 * handle an incoming response message
971 */
972 static status_t process_response(private_task_manager_t *this,
973 message_t *message)
974 {
975 enumerator_t *enumerator;
976 message_t *queued;
977 status_t status;
978 task_t *task;
979
980 if (message->get_exchange_type(message) != this->initiating.type)
981 {
982 DBG1(DBG_IKE, "received %N response, but expected %N",
983 exchange_type_names, message->get_exchange_type(message),
984 exchange_type_names, this->initiating.type);
985 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
986 return DESTROY_ME;
987 }
988
989 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
990 while (enumerator->enumerate(enumerator, (void*)&task))
991 {
992 switch (task->process(task, message))
993 {
994 case SUCCESS:
995 /* task completed, remove it */
996 this->active_tasks->remove_at(this->active_tasks, enumerator);
997 task->destroy(task);
998 continue;
999 case NEED_MORE:
1000 /* processed, but task needs another exchange */
1001 continue;
1002 case ALREADY_DONE:
1003 break;
1004 case FAILED:
1005 default:
1006 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
1007 /* FALL */
1008 case DESTROY_ME:
1009 /* critical failure, destroy IKE_SA */
1010 this->active_tasks->remove_at(this->active_tasks, enumerator);
1011 enumerator->destroy(enumerator);
1012 task->destroy(task);
1013 return DESTROY_ME;
1014 }
1015 break;
1016 }
1017 enumerator->destroy(enumerator);
1018
1019 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
1020 DESTROY_IF(this->initiating.packet);
1021 this->initiating.packet = NULL;
1022
1023 if (this->queued && this->active_tasks->get_count(this->active_tasks) == 0)
1024 {
1025 queued = this->queued;
1026 this->queued = NULL;
1027 status = this->public.task_manager.process_message(
1028 &this->public.task_manager, queued);
1029 queued->destroy(queued);
1030 if (status == DESTROY_ME)
1031 {
1032 return status;
1033 }
1034 }
1035
1036 return initiate(this);
1037 }
1038
1039 /**
1040 * Parse the given message and verify that it is valid.
1041 */
1042 static status_t parse_message(private_task_manager_t *this, message_t *msg)
1043 {
1044 status_t status;
1045
1046 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
1047
1048 if (status != SUCCESS)
1049 {
1050 switch (status)
1051 {
1052 case NOT_SUPPORTED:
1053 DBG1(DBG_IKE, "unsupported exchange type");
1054 send_notify(this, msg, INVALID_EXCHANGE_TYPE);
1055 break;
1056 case PARSE_ERROR:
1057 DBG1(DBG_IKE, "message parsing failed");
1058 send_notify(this, msg, PAYLOAD_MALFORMED);
1059 break;
1060 case VERIFY_ERROR:
1061 DBG1(DBG_IKE, "message verification failed");
1062 send_notify(this, msg, PAYLOAD_MALFORMED);
1063 break;
1064 case FAILED:
1065 DBG1(DBG_IKE, "integrity check failed");
1066 send_notify(this, msg, INVALID_HASH_INFORMATION);
1067 break;
1068 case INVALID_STATE:
1069 DBG1(DBG_IKE, "found encrypted message, but no keys available");
1070 send_notify(this, msg, PAYLOAD_MALFORMED);
1071 default:
1072 break;
1073 }
1074 DBG1(DBG_IKE, "%N %s with message ID %u processing failed",
1075 exchange_type_names, msg->get_exchange_type(msg),
1076 msg->get_request(msg) ? "request" : "response",
1077 msg->get_message_id(msg));
1078
1079 charon->bus->alert(charon->bus, ALERT_PARSE_ERROR_BODY, msg, status);
1080
1081 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED)
1082 { /* invalid initiation attempt, close SA */
1083 return DESTROY_ME;
1084 }
1085 }
1086 return status;
1087 }
1088
1089 METHOD(task_manager_t, process_message, status_t,
1090 private_task_manager_t *this, message_t *msg)
1091 {
1092 u_int32_t hash, mid, i;
1093 host_t *me, *other;
1094 status_t status;
1095
1096 /* TODO-IKEv1: update hosts more selectively */
1097 me = msg->get_destination(msg);
1098 other = msg->get_source(msg);
1099 mid = msg->get_message_id(msg);
1100 hash = chunk_hash(msg->get_packet_data(msg));
1101 for (i = 0; i < MAX_OLD_HASHES; i++)
1102 {
1103 if (this->initiating.old_hashes[i] == hash)
1104 {
1105 if (this->initiating.packet &&
1106 i == (this->initiating.old_hash_pos % MAX_OLD_HASHES) &&
1107 (msg->get_exchange_type(msg) == QUICK_MODE ||
1108 msg->get_exchange_type(msg) == AGGRESSIVE))
1109 {
1110 DBG1(DBG_IKE, "received retransmit of response with ID %u, "
1111 "resending last request", mid);
1112 charon->sender->send(charon->sender,
1113 this->initiating.packet->clone(this->initiating.packet));
1114 return SUCCESS;
1115 }
1116 DBG1(DBG_IKE, "received retransmit of response with ID %u, "
1117 "but next request already sent", mid);
1118 return SUCCESS;
1119 }
1120 }
1121
1122 if ((mid && mid == this->initiating.mid) ||
1123 (this->initiating.mid == 0 &&
1124 msg->get_exchange_type(msg) == this->initiating.type &&
1125 this->active_tasks->get_count(this->active_tasks)))
1126 {
1127 msg->set_request(msg, FALSE);
1128 charon->bus->message(charon->bus, msg, TRUE, FALSE);
1129 status = parse_message(this, msg);
1130 if (status != SUCCESS)
1131 {
1132 return status;
1133 }
1134 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
1135 time_monotonic(NULL));
1136 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
1137 charon->bus->message(charon->bus, msg, TRUE, TRUE);
1138 if (process_response(this, msg) != SUCCESS)
1139 {
1140 flush(this);
1141 return DESTROY_ME;
1142 }
1143 this->initiating.old_hashes[(++this->initiating.old_hash_pos) %
1144 MAX_OLD_HASHES] = hash;
1145 }
1146 else
1147 {
1148 if (hash == this->responding.hash)
1149 {
1150 if (this->responding.packet)
1151 {
1152 DBG1(DBG_IKE, "received retransmit of request with ID %u, "
1153 "retransmitting response", mid);
1154 charon->sender->send(charon->sender,
1155 this->responding.packet->clone(this->responding.packet));
1156 }
1157 else if (this->initiating.packet &&
1158 this->initiating.type == INFORMATIONAL_V1)
1159 {
1160 DBG1(DBG_IKE, "received retransmit of DPD request, "
1161 "retransmitting response");
1162 charon->sender->send(charon->sender,
1163 this->initiating.packet->clone(this->initiating.packet));
1164 }
1165 else
1166 {
1167 DBG1(DBG_IKE, "received retransmit of request with ID %u, "
1168 "but no response to retransmit", mid);
1169 }
1170 return SUCCESS;
1171 }
1172 if (msg->get_exchange_type(msg) == TRANSACTION &&
1173 this->active_tasks->get_count(this->active_tasks))
1174 { /* main mode not yet complete, queue XAuth/Mode config tasks */
1175 if (this->queued)
1176 {
1177 DBG1(DBG_IKE, "ignoring additional %N request, queue full",
1178 exchange_type_names, TRANSACTION);
1179 return SUCCESS;
1180 }
1181 this->queued = message_create_from_packet(msg->get_packet(msg));
1182 if (this->queued->parse_header(this->queued) != SUCCESS)
1183 {
1184 this->queued->destroy(this->queued);
1185 this->queued = NULL;
1186 return FAILED;
1187 }
1188 DBG1(DBG_IKE, "queueing %N request as tasks still active",
1189 exchange_type_names, TRANSACTION);
1190 return SUCCESS;
1191 }
1192
1193 msg->set_request(msg, TRUE);
1194 charon->bus->message(charon->bus, msg, TRUE, FALSE);
1195 status = parse_message(this, msg);
1196 if (status != SUCCESS)
1197 {
1198 return status;
1199 }
1200 /* if this IKE_SA is virgin, we check for a config */
1201 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
1202 {
1203 ike_sa_id_t *ike_sa_id;
1204 ike_cfg_t *ike_cfg;
1205 job_t *job;
1206
1207 ike_cfg = charon->backends->get_ike_cfg(charon->backends,
1208 me, other, IKEV1);
1209 if (ike_cfg == NULL)
1210 {
1211 /* no config found for these hosts, destroy */
1212 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
1213 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
1214 send_notify(this, msg, NO_PROPOSAL_CHOSEN);
1215 return DESTROY_ME;
1216 }
1217 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
1218 ike_cfg->destroy(ike_cfg);
1219 /* add a timeout if peer does not establish it completely */
1220 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
1221 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
1222 lib->scheduler->schedule_job(lib->scheduler, job,
1223 lib->settings->get_int(lib->settings,
1224 "%s.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT,
1225 charon->name));
1226 }
1227 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
1228 charon->bus->message(charon->bus, msg, TRUE, TRUE);
1229 if (process_request(this, msg) != SUCCESS)
1230 {
1231 flush(this);
1232 return DESTROY_ME;
1233 }
1234 this->responding.hash = hash;
1235 }
1236 return SUCCESS;
1237 }
1238
1239 METHOD(task_manager_t, queue_task, void,
1240 private_task_manager_t *this, task_t *task)
1241 {
1242 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
1243 this->queued_tasks->insert_last(this->queued_tasks, task);
1244 }
1245
1246 /**
1247 * Check if a given task has been queued already
1248 */
1249 static bool has_queued(private_task_manager_t *this, task_type_t type)
1250 {
1251 enumerator_t *enumerator;
1252 bool found = FALSE;
1253 task_t *task;
1254
1255 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1256 while (enumerator->enumerate(enumerator, &task))
1257 {
1258 if (task->get_type(task) == type)
1259 {
1260 found = TRUE;
1261 break;
1262 }
1263 }
1264 enumerator->destroy(enumerator);
1265 return found;
1266 }
1267
1268 METHOD(task_manager_t, queue_ike, void,
1269 private_task_manager_t *this)
1270 {
1271 peer_cfg_t *peer_cfg;
1272
1273 if (!has_queued(this, TASK_ISAKMP_VENDOR))
1274 {
1275 queue_task(this, (task_t*)isakmp_vendor_create(this->ike_sa, TRUE));
1276 }
1277 if (!has_queued(this, TASK_ISAKMP_CERT_PRE))
1278 {
1279 queue_task(this, (task_t*)isakmp_cert_pre_create(this->ike_sa, TRUE));
1280 }
1281 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
1282 if (peer_cfg->use_aggressive(peer_cfg))
1283 {
1284 if (!has_queued(this, TASK_AGGRESSIVE_MODE))
1285 {
1286 queue_task(this, (task_t*)aggressive_mode_create(this->ike_sa, TRUE));
1287 }
1288 }
1289 else
1290 {
1291 if (!has_queued(this, TASK_MAIN_MODE))
1292 {
1293 queue_task(this, (task_t*)main_mode_create(this->ike_sa, TRUE));
1294 }
1295 }
1296 if (!has_queued(this, TASK_ISAKMP_CERT_POST))
1297 {
1298 queue_task(this, (task_t*)isakmp_cert_post_create(this->ike_sa, TRUE));
1299 }
1300 if (!has_queued(this, TASK_ISAKMP_NATD))
1301 {
1302 queue_task(this, (task_t*)isakmp_natd_create(this->ike_sa, TRUE));
1303 }
1304 }
1305
1306 METHOD(task_manager_t, queue_ike_reauth, void,
1307 private_task_manager_t *this)
1308 {
1309 enumerator_t *enumerator;
1310 child_sa_t *child_sa;
1311 ike_sa_t *new;
1312 host_t *host;
1313
1314 new = charon->ike_sa_manager->checkout_new(charon->ike_sa_manager,
1315 this->ike_sa->get_version(this->ike_sa), TRUE);
1316 if (!new)
1317 { /* shouldn't happen */
1318 return;
1319 }
1320
1321 new->set_peer_cfg(new, this->ike_sa->get_peer_cfg(this->ike_sa));
1322 host = this->ike_sa->get_other_host(this->ike_sa);
1323 new->set_other_host(new, host->clone(host));
1324 host = this->ike_sa->get_my_host(this->ike_sa);
1325 new->set_my_host(new, host->clone(host));
1326 enumerator = this->ike_sa->create_virtual_ip_enumerator(this->ike_sa, TRUE);
1327 while (enumerator->enumerate(enumerator, &host))
1328 {
1329 new->add_virtual_ip(new, TRUE, host);
1330 }
1331 enumerator->destroy(enumerator);
1332
1333 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
1334 while (enumerator->enumerate(enumerator, &child_sa))
1335 {
1336 this->ike_sa->remove_child_sa(this->ike_sa, enumerator);
1337 new->add_child_sa(new, child_sa);
1338 }
1339 enumerator->destroy(enumerator);
1340
1341 if (!new->get_child_count(new))
1342 { /* check if a Quick Mode task is queued (UNITY_LOAD_BALANCE case) */
1343 task_t *task;
1344
1345 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1346 while (enumerator->enumerate(enumerator, &task))
1347 {
1348 if (task->get_type(task) == TASK_QUICK_MODE)
1349 {
1350 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
1351 task->migrate(task, new);
1352 new->queue_task(new, task);
1353 }
1354 }
1355 enumerator->destroy(enumerator);
1356 }
1357
1358 if (new->initiate(new, NULL, 0, NULL, NULL) != DESTROY_ME)
1359 {
1360 charon->ike_sa_manager->checkin(charon->ike_sa_manager, new);
1361 this->ike_sa->set_state(this->ike_sa, IKE_REKEYING);
1362 }
1363 else
1364 {
1365 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, new);
1366 DBG1(DBG_IKE, "reauthenticating IKE_SA failed");
1367 }
1368 charon->bus->set_sa(charon->bus, this->ike_sa);
1369 }
1370
1371 METHOD(task_manager_t, queue_ike_rekey, void,
1372 private_task_manager_t *this)
1373 {
1374 queue_ike_reauth(this);
1375 }
1376
1377 METHOD(task_manager_t, queue_ike_delete, void,
1378 private_task_manager_t *this)
1379 {
1380 enumerator_t *enumerator;
1381 child_sa_t *child_sa;
1382
1383 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
1384 while (enumerator->enumerate(enumerator, &child_sa))
1385 {
1386 queue_task(this, (task_t*)
1387 quick_delete_create(this->ike_sa, child_sa->get_protocol(child_sa),
1388 child_sa->get_spi(child_sa, TRUE), FALSE, FALSE));
1389 }
1390 enumerator->destroy(enumerator);
1391
1392 queue_task(this, (task_t*)isakmp_delete_create(this->ike_sa, TRUE));
1393 }
1394
1395 METHOD(task_manager_t, queue_mobike, void,
1396 private_task_manager_t *this, bool roam, bool address)
1397 {
1398 /* Not supported in IKEv1 */
1399 }
1400
1401 METHOD(task_manager_t, queue_child, void,
1402 private_task_manager_t *this, child_cfg_t *cfg, u_int32_t reqid,
1403 traffic_selector_t *tsi, traffic_selector_t *tsr)
1404 {
1405 quick_mode_t *task;
1406
1407 task = quick_mode_create(this->ike_sa, cfg, tsi, tsr);
1408 task->use_reqid(task, reqid);
1409
1410 queue_task(this, &task->task);
1411 }
1412
1413 /**
1414 * Check if two CHILD_SAs have the same traffic selector
1415 */
1416 static bool have_equal_ts(child_sa_t *a, child_sa_t *b, bool local)
1417 {
1418 linked_list_t *list;
1419 traffic_selector_t *ts_a, *ts_b;
1420
1421 list = a->get_traffic_selectors(a, local);
1422 if (list->get_first(list, (void**)&ts_a) == SUCCESS)
1423 {
1424 list = b->get_traffic_selectors(b, local);
1425 if (list->get_first(list, (void**)&ts_b) == SUCCESS)
1426 {
1427 return ts_a->equals(ts_a, ts_b);
1428 }
1429 }
1430 return FALSE;
1431 }
1432
1433 /**
1434 * Check if a CHILD_SA is redundant and we should delete instead of rekey
1435 */
1436 static bool is_redundant(private_task_manager_t *this, child_sa_t *child_sa)
1437 {
1438 enumerator_t *enumerator;
1439 child_sa_t *current;
1440 bool redundant = FALSE;
1441
1442 enumerator = this->ike_sa->create_child_sa_enumerator(this->ike_sa);
1443 while (enumerator->enumerate(enumerator, &current))
1444 {
1445 if (current->get_state(current) == CHILD_INSTALLED &&
1446 streq(current->get_name(current), child_sa->get_name(child_sa)) &&
1447 have_equal_ts(current, child_sa, TRUE) &&
1448 have_equal_ts(current, child_sa, FALSE) &&
1449 current->get_lifetime(current, FALSE) >
1450 child_sa->get_lifetime(child_sa, FALSE))
1451 {
1452 DBG1(DBG_IKE, "deleting redundant CHILD_SA %s{%d}",
1453 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa));
1454 redundant = TRUE;
1455 break;
1456 }
1457 }
1458 enumerator->destroy(enumerator);
1459
1460 return redundant;
1461 }
1462
1463 /**
1464 * Get the first traffic selector of a CHILD_SA, local or remote
1465 */
1466 static traffic_selector_t* get_first_ts(child_sa_t *child_sa, bool local)
1467 {
1468 traffic_selector_t *ts = NULL;
1469 linked_list_t *list;
1470
1471 list = child_sa->get_traffic_selectors(child_sa, local);
1472 if (list->get_first(list, (void**)&ts) == SUCCESS)
1473 {
1474 return ts;
1475 }
1476 return NULL;
1477 }
1478
1479 METHOD(task_manager_t, queue_child_rekey, void,
1480 private_task_manager_t *this, protocol_id_t protocol, u_int32_t spi)
1481 {
1482 child_sa_t *child_sa;
1483 child_cfg_t *cfg;
1484 quick_mode_t *task;
1485
1486 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, TRUE);
1487 if (!child_sa)
1488 {
1489 child_sa = this->ike_sa->get_child_sa(this->ike_sa, protocol, spi, FALSE);
1490 }
1491 if (child_sa && child_sa->get_state(child_sa) == CHILD_INSTALLED)
1492 {
1493 if (is_redundant(this, child_sa))
1494 {
1495 queue_task(this, (task_t*)quick_delete_create(this->ike_sa,
1496 protocol, spi, FALSE, FALSE));
1497 }
1498 else
1499 {
1500 child_sa->set_state(child_sa, CHILD_REKEYING);
1501 cfg = child_sa->get_config(child_sa);
1502 task = quick_mode_create(this->ike_sa, cfg->get_ref(cfg),
1503 get_first_ts(child_sa, TRUE), get_first_ts(child_sa, FALSE));
1504 task->use_reqid(task, child_sa->get_reqid(child_sa));
1505 task->rekey(task, child_sa->get_spi(child_sa, TRUE));
1506
1507 queue_task(this, &task->task);
1508 }
1509 }
1510 }
1511
1512 METHOD(task_manager_t, queue_child_delete, void,
1513 private_task_manager_t *this, protocol_id_t protocol, u_int32_t spi,
1514 bool expired)
1515 {
1516 queue_task(this, (task_t*)quick_delete_create(this->ike_sa, protocol,
1517 spi, FALSE, expired));
1518 }
1519
1520 METHOD(task_manager_t, queue_dpd, void,
1521 private_task_manager_t *this)
1522 {
1523 peer_cfg_t *peer_cfg;
1524 u_int32_t t, retransmit;
1525
1526 queue_task(this, (task_t*)isakmp_dpd_create(this->ike_sa, DPD_R_U_THERE,
1527 this->dpd_send++));
1528 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
1529
1530 /* compute timeout in milliseconds */
1531 t = 1000 * peer_cfg->get_dpd_timeout(peer_cfg);
1532 if (t == 0)
1533 {
1534 /* use the same timeout as a retransmitting IKE message would have */
1535 for (retransmit = 0; retransmit <= this->retransmit_tries; retransmit++)
1536 {
1537 t += (u_int32_t)(this->retransmit_timeout * 1000.0 *
1538 pow(this->retransmit_base, retransmit));
1539 }
1540 }
1541
1542 /* schedule DPD timeout job */
1543 lib->scheduler->schedule_job_ms(lib->scheduler,
1544 (job_t*)dpd_timeout_job_create(this->ike_sa->get_id(this->ike_sa)), t);
1545 }
1546
1547 METHOD(task_manager_t, adopt_tasks, void,
1548 private_task_manager_t *this, task_manager_t *other_public)
1549 {
1550 private_task_manager_t *other = (private_task_manager_t*)other_public;
1551 task_t *task;
1552
1553 /* move queued tasks from other to this */
1554 while (other->queued_tasks->remove_last(other->queued_tasks,
1555 (void**)&task) == SUCCESS)
1556 {
1557 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
1558 task->migrate(task, this->ike_sa);
1559 this->queued_tasks->insert_first(this->queued_tasks, task);
1560 }
1561 }
1562
1563 METHOD(task_manager_t, busy, bool,
1564 private_task_manager_t *this)
1565 {
1566 return (this->active_tasks->get_count(this->active_tasks) > 0);
1567 }
1568
1569 METHOD(task_manager_t, incr_mid, void,
1570 private_task_manager_t *this, bool initiate)
1571 {
1572 }
1573
1574 METHOD(task_manager_t, reset, void,
1575 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
1576 {
1577 enumerator_t *enumerator;
1578 task_t *task;
1579
1580 /* reset message counters and retransmit packets */
1581 DESTROY_IF(this->responding.packet);
1582 DESTROY_IF(this->initiating.packet);
1583 this->responding.packet = NULL;
1584 this->responding.seqnr = RESPONDING_SEQ;
1585 this->responding.retransmitted = 0;
1586 this->initiating.packet = NULL;
1587 this->initiating.mid = 0;
1588 this->initiating.seqnr = 0;
1589 this->initiating.retransmitted = 0;
1590 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
1591 if (initiate != UINT_MAX)
1592 {
1593 this->dpd_send = initiate;
1594 }
1595 if (respond != UINT_MAX)
1596 {
1597 this->dpd_recv = respond;
1598 }
1599
1600 /* reset queued tasks */
1601 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1602 while (enumerator->enumerate(enumerator, &task))
1603 {
1604 task->migrate(task, this->ike_sa);
1605 }
1606 enumerator->destroy(enumerator);
1607
1608 /* reset active tasks */
1609 while (this->active_tasks->remove_last(this->active_tasks,
1610 (void**)&task) == SUCCESS)
1611 {
1612 task->migrate(task, this->ike_sa);
1613 this->queued_tasks->insert_first(this->queued_tasks, task);
1614 }
1615 }
1616
1617 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
1618 private_task_manager_t *this, task_queue_t queue)
1619 {
1620 switch (queue)
1621 {
1622 case TASK_QUEUE_ACTIVE:
1623 return this->active_tasks->create_enumerator(this->active_tasks);
1624 case TASK_QUEUE_PASSIVE:
1625 return this->passive_tasks->create_enumerator(this->passive_tasks);
1626 case TASK_QUEUE_QUEUED:
1627 return this->queued_tasks->create_enumerator(this->queued_tasks);
1628 default:
1629 return enumerator_create_empty();
1630 }
1631 }
1632
1633 METHOD(task_manager_t, destroy, void,
1634 private_task_manager_t *this)
1635 {
1636 flush(this);
1637
1638 this->active_tasks->destroy(this->active_tasks);
1639 this->queued_tasks->destroy(this->queued_tasks);
1640 this->passive_tasks->destroy(this->passive_tasks);
1641
1642 DESTROY_IF(this->queued);
1643 DESTROY_IF(this->responding.packet);
1644 DESTROY_IF(this->initiating.packet);
1645 DESTROY_IF(this->rng);
1646 free(this);
1647 }
1648
1649 /*
1650 * see header file
1651 */
1652 task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
1653 {
1654 private_task_manager_t *this;
1655
1656 INIT(this,
1657 .public = {
1658 .task_manager = {
1659 .process_message = _process_message,
1660 .queue_task = _queue_task,
1661 .queue_ike = _queue_ike,
1662 .queue_ike_rekey = _queue_ike_rekey,
1663 .queue_ike_reauth = _queue_ike_reauth,
1664 .queue_ike_delete = _queue_ike_delete,
1665 .queue_mobike = _queue_mobike,
1666 .queue_child = _queue_child,
1667 .queue_child_rekey = _queue_child_rekey,
1668 .queue_child_delete = _queue_child_delete,
1669 .queue_dpd = _queue_dpd,
1670 .initiate = _initiate,
1671 .retransmit = _retransmit,
1672 .incr_mid = _incr_mid,
1673 .reset = _reset,
1674 .adopt_tasks = _adopt_tasks,
1675 .busy = _busy,
1676 .create_task_enumerator = _create_task_enumerator,
1677 .flush_queue = _flush_queue,
1678 .destroy = _destroy,
1679 },
1680 },
1681 .initiating = {
1682 .type = EXCHANGE_TYPE_UNDEFINED,
1683 },
1684 .responding = {
1685 .seqnr = RESPONDING_SEQ,
1686 },
1687 .ike_sa = ike_sa,
1688 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
1689 .queued_tasks = linked_list_create(),
1690 .active_tasks = linked_list_create(),
1691 .passive_tasks = linked_list_create(),
1692 .retransmit_tries = lib->settings->get_int(lib->settings,
1693 "%s.retransmit_tries", RETRANSMIT_TRIES, charon->name),
1694 .retransmit_timeout = lib->settings->get_double(lib->settings,
1695 "%s.retransmit_timeout", RETRANSMIT_TIMEOUT, charon->name),
1696 .retransmit_base = lib->settings->get_double(lib->settings,
1697 "%s.retransmit_base", RETRANSMIT_BASE, charon->name),
1698 );
1699
1700 if (!this->rng)
1701 {
1702 DBG1(DBG_IKE, "no RNG found, unable to create IKE_SA");
1703 destroy(this);
1704 return NULL;
1705 }
1706 if (!this->rng->get_bytes(this->rng, sizeof(this->dpd_send),
1707 (void*)&this->dpd_send))
1708 {
1709 DBG1(DBG_IKE, "failed to allocate message ID, unable to create IKE_SA");
1710 destroy(this);
1711 return NULL;
1712 }
1713 this->dpd_send &= 0x7FFFFFFF;
1714
1715 return &this->public;
1716 }
1717