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