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