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