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