Destroy active task list before queued tasks
[strongswan.git] / src / libcharon / sa / task_manager.c
1 /*
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2007-2010 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.h"
18
19 #include <math.h>
20
21 #include <daemon.h>
22 #include <sa/tasks/ike_init.h>
23 #include <sa/tasks/ike_natd.h>
24 #include <sa/tasks/ike_mobike.h>
25 #include <sa/tasks/ike_auth.h>
26 #include <sa/tasks/ike_auth_lifetime.h>
27 #include <sa/tasks/ike_cert_pre.h>
28 #include <sa/tasks/ike_cert_post.h>
29 #include <sa/tasks/ike_rekey.h>
30 #include <sa/tasks/ike_delete.h>
31 #include <sa/tasks/ike_config.h>
32 #include <sa/tasks/ike_dpd.h>
33 #include <sa/tasks/ike_vendor.h>
34 #include <sa/tasks/child_create.h>
35 #include <sa/tasks/child_rekey.h>
36 #include <sa/tasks/child_delete.h>
37 #include <encoding/payloads/delete_payload.h>
38 #include <processing/jobs/retransmit_job.h>
39
40 #ifdef ME
41 #include <sa/tasks/ike_me.h>
42 #endif
43
44 typedef struct exchange_t exchange_t;
45
46 /**
47 * An exchange in the air, used do detect and handle retransmission
48 */
49 struct exchange_t {
50
51 /**
52 * Message ID used for this transaction
53 */
54 u_int32_t mid;
55
56 /**
57 * generated packet for retransmission
58 */
59 packet_t *packet;
60 };
61
62 typedef struct private_task_manager_t private_task_manager_t;
63
64 /**
65 * private data of the task manager
66 */
67 struct private_task_manager_t {
68
69 /**
70 * public functions
71 */
72 task_manager_t public;
73
74 /**
75 * associated IKE_SA we are serving
76 */
77 ike_sa_t *ike_sa;
78
79 /**
80 * Exchange we are currently handling as responder
81 */
82 struct {
83 /**
84 * Message ID of the exchange
85 */
86 u_int32_t mid;
87
88 /**
89 * packet for retransmission
90 */
91 packet_t *packet;
92
93 } responding;
94
95 /**
96 * Exchange we are currently handling as initiator
97 */
98 struct {
99 /**
100 * Message ID of the exchange
101 */
102 u_int32_t mid;
103
104 /**
105 * how many times we have retransmitted so far
106 */
107 u_int retransmitted;
108
109 /**
110 * packet for retransmission
111 */
112 packet_t *packet;
113
114 /**
115 * type of the initated exchange
116 */
117 exchange_type_t type;
118
119 } initiating;
120
121 /**
122 * List of queued tasks not yet in action
123 */
124 linked_list_t *queued_tasks;
125
126 /**
127 * List of active tasks, initiated by ourselve
128 */
129 linked_list_t *active_tasks;
130
131 /**
132 * List of tasks initiated by peer
133 */
134 linked_list_t *passive_tasks;
135
136 /**
137 * the task manager has been reset
138 */
139 bool reset;
140
141 /**
142 * Number of times we retransmit messages before giving up
143 */
144 u_int retransmit_tries;
145
146 /**
147 * Retransmission timeout
148 */
149 double retransmit_timeout;
150
151 /**
152 * Base to calculate retransmission timeout
153 */
154 double retransmit_base;
155 };
156
157 /**
158 * flush all tasks in the task manager
159 */
160 static void flush(private_task_manager_t *this)
161 {
162 this->passive_tasks->destroy_offset(this->passive_tasks,
163 offsetof(task_t, destroy));
164 this->passive_tasks = linked_list_create();
165 this->active_tasks->destroy_offset(this->active_tasks,
166 offsetof(task_t, destroy));
167 this->active_tasks = linked_list_create();
168 this->queued_tasks->destroy_offset(this->queued_tasks,
169 offsetof(task_t, destroy));
170 this->queued_tasks = linked_list_create();
171 }
172
173 /**
174 * move a task of a specific type from the queue to the active list
175 */
176 static bool activate_task(private_task_manager_t *this, task_type_t type)
177 {
178 enumerator_t *enumerator;
179 task_t *task;
180 bool found = FALSE;
181
182 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
183 while (enumerator->enumerate(enumerator, (void**)&task))
184 {
185 if (task->get_type(task) == type)
186 {
187 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
188 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
189 this->active_tasks->insert_last(this->active_tasks, task);
190 found = TRUE;
191 break;
192 }
193 }
194 enumerator->destroy(enumerator);
195 return found;
196 }
197
198 METHOD(task_manager_t, retransmit, status_t,
199 private_task_manager_t *this, u_int32_t message_id)
200 {
201 if (message_id == this->initiating.mid)
202 {
203 u_int32_t timeout;
204 job_t *job;
205 enumerator_t *enumerator;
206 packet_t *packet;
207 task_t *task;
208 ike_mobike_t *mobike = NULL;
209
210 /* check if we are retransmitting a MOBIKE routability check */
211 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
212 while (enumerator->enumerate(enumerator, (void*)&task))
213 {
214 if (task->get_type(task) == IKE_MOBIKE)
215 {
216 mobike = (ike_mobike_t*)task;
217 if (!mobike->is_probing(mobike))
218 {
219 mobike = NULL;
220 }
221 break;
222 }
223 }
224 enumerator->destroy(enumerator);
225
226 if (mobike == NULL)
227 {
228 if (this->initiating.retransmitted <= this->retransmit_tries)
229 {
230 timeout = (u_int32_t)(this->retransmit_timeout * 1000.0 *
231 pow(this->retransmit_base, this->initiating.retransmitted));
232 }
233 else
234 {
235 DBG1(DBG_IKE, "giving up after %d retransmits",
236 this->initiating.retransmitted - 1);
237 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
238 {
239 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
240 }
241 return DESTROY_ME;
242 }
243
244 if (this->initiating.retransmitted)
245 {
246 DBG1(DBG_IKE, "retransmit %d of request with message ID %d",
247 this->initiating.retransmitted, message_id);
248 }
249 packet = this->initiating.packet->clone(this->initiating.packet);
250 charon->sender->send(charon->sender, packet);
251 }
252 else
253 { /* for routeability checks, we use a more aggressive behavior */
254 if (this->initiating.retransmitted <= ROUTEABILITY_CHECK_TRIES)
255 {
256 timeout = ROUTEABILITY_CHECK_INTERVAL;
257 }
258 else
259 {
260 DBG1(DBG_IKE, "giving up after %d path probings",
261 this->initiating.retransmitted - 1);
262 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
263 return DESTROY_ME;
264 }
265
266 if (this->initiating.retransmitted)
267 {
268 DBG1(DBG_IKE, "path probing attempt %d",
269 this->initiating.retransmitted);
270 }
271 mobike->transmit(mobike, this->initiating.packet);
272 }
273
274 this->initiating.retransmitted++;
275 job = (job_t*)retransmit_job_create(this->initiating.mid,
276 this->ike_sa->get_id(this->ike_sa));
277 lib->scheduler->schedule_job_ms(lib->scheduler, job, timeout);
278 }
279 return SUCCESS;
280 }
281
282 METHOD(task_manager_t, initiate, status_t,
283 private_task_manager_t *this)
284 {
285 enumerator_t *enumerator;
286 task_t *task;
287 message_t *message;
288 host_t *me, *other;
289 status_t status;
290 exchange_type_t exchange = 0;
291
292 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
293 {
294 DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
295 exchange_type_names, this->initiating.type);
296 /* do not initiate if we already have a message in the air */
297 return SUCCESS;
298 }
299
300 if (this->active_tasks->get_count(this->active_tasks) == 0)
301 {
302 DBG2(DBG_IKE, "activating new tasks");
303 switch (this->ike_sa->get_state(this->ike_sa))
304 {
305 case IKE_CREATED:
306 activate_task(this, IKE_VENDOR);
307 if (activate_task(this, IKE_INIT))
308 {
309 this->initiating.mid = 0;
310 exchange = IKE_SA_INIT;
311 activate_task(this, IKE_NATD);
312 activate_task(this, IKE_CERT_PRE);
313 #ifdef ME
314 /* this task has to be activated before the IKE_AUTHENTICATE
315 * task, because that task pregenerates the packet after
316 * which no payloads can be added to the message anymore.
317 */
318 activate_task(this, IKE_ME);
319 #endif /* ME */
320 activate_task(this, IKE_AUTHENTICATE);
321 activate_task(this, IKE_CERT_POST);
322 activate_task(this, IKE_CONFIG);
323 activate_task(this, CHILD_CREATE);
324 activate_task(this, IKE_AUTH_LIFETIME);
325 activate_task(this, IKE_MOBIKE);
326 }
327 break;
328 case IKE_ESTABLISHED:
329 if (activate_task(this, CHILD_CREATE))
330 {
331 exchange = CREATE_CHILD_SA;
332 break;
333 }
334 if (activate_task(this, CHILD_DELETE))
335 {
336 exchange = INFORMATIONAL;
337 break;
338 }
339 if (activate_task(this, CHILD_REKEY))
340 {
341 exchange = CREATE_CHILD_SA;
342 break;
343 }
344 if (activate_task(this, IKE_DELETE))
345 {
346 exchange = INFORMATIONAL;
347 break;
348 }
349 if (activate_task(this, IKE_REKEY))
350 {
351 exchange = CREATE_CHILD_SA;
352 break;
353 }
354 if (activate_task(this, IKE_REAUTH))
355 {
356 exchange = INFORMATIONAL;
357 break;
358 }
359 if (activate_task(this, IKE_MOBIKE))
360 {
361 exchange = INFORMATIONAL;
362 break;
363 }
364 if (activate_task(this, IKE_DPD))
365 {
366 exchange = INFORMATIONAL;
367 break;
368 }
369 #ifdef ME
370 if (activate_task(this, IKE_ME))
371 {
372 exchange = ME_CONNECT;
373 break;
374 }
375 #endif /* ME */
376 case IKE_REKEYING:
377 if (activate_task(this, IKE_DELETE))
378 {
379 exchange = INFORMATIONAL;
380 break;
381 }
382 case IKE_DELETING:
383 default:
384 break;
385 }
386 }
387 else
388 {
389 DBG2(DBG_IKE, "reinitiating already active tasks");
390 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
391 while (enumerator->enumerate(enumerator, (void**)&task))
392 {
393 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
394 switch (task->get_type(task))
395 {
396 case IKE_INIT:
397 exchange = IKE_SA_INIT;
398 break;
399 case IKE_AUTHENTICATE:
400 exchange = IKE_AUTH;
401 break;
402 case CHILD_CREATE:
403 case CHILD_REKEY:
404 case IKE_REKEY:
405 exchange = CREATE_CHILD_SA;
406 break;
407 case IKE_MOBIKE:
408 exchange = INFORMATIONAL;
409 break;
410 default:
411 continue;
412 }
413 break;
414 }
415 enumerator->destroy(enumerator);
416 }
417
418 if (exchange == 0)
419 {
420 DBG2(DBG_IKE, "nothing to initiate");
421 /* nothing to do yet... */
422 return SUCCESS;
423 }
424
425 me = this->ike_sa->get_my_host(this->ike_sa);
426 other = this->ike_sa->get_other_host(this->ike_sa);
427
428 message = message_create();
429 message->set_message_id(message, this->initiating.mid);
430 message->set_source(message, me->clone(me));
431 message->set_destination(message, other->clone(other));
432 message->set_exchange_type(message, exchange);
433 this->initiating.type = exchange;
434 this->initiating.retransmitted = 0;
435
436 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
437 while (enumerator->enumerate(enumerator, (void*)&task))
438 {
439 switch (task->build(task, message))
440 {
441 case SUCCESS:
442 /* task completed, remove it */
443 this->active_tasks->remove_at(this->active_tasks, enumerator);
444 task->destroy(task);
445 break;
446 case NEED_MORE:
447 /* processed, but task needs another exchange */
448 break;
449 case FAILED:
450 default:
451 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
452 {
453 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
454 }
455 /* FALL */
456 case DESTROY_ME:
457 /* critical failure, destroy IKE_SA */
458 enumerator->destroy(enumerator);
459 message->destroy(message);
460 flush(this);
461 return DESTROY_ME;
462 }
463 }
464 enumerator->destroy(enumerator);
465
466 /* update exchange type if a task changed it */
467 this->initiating.type = message->get_exchange_type(message);
468
469 status = this->ike_sa->generate_message(this->ike_sa, message,
470 &this->initiating.packet);
471 if (status != SUCCESS)
472 {
473 /* message generation failed. There is nothing more to do than to
474 * close the SA */
475 message->destroy(message);
476 flush(this);
477 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
478 return DESTROY_ME;
479 }
480 message->destroy(message);
481
482 return retransmit(this, this->initiating.mid);
483 }
484
485 /**
486 * handle an incoming response message
487 */
488 static status_t process_response(private_task_manager_t *this,
489 message_t *message)
490 {
491 enumerator_t *enumerator;
492 task_t *task;
493
494 if (message->get_exchange_type(message) != this->initiating.type)
495 {
496 DBG1(DBG_IKE, "received %N response, but expected %N",
497 exchange_type_names, message->get_exchange_type(message),
498 exchange_type_names, this->initiating.type);
499 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
500 return DESTROY_ME;
501 }
502
503 /* catch if we get resetted while processing */
504 this->reset = FALSE;
505 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
506 while (enumerator->enumerate(enumerator, (void*)&task))
507 {
508 switch (task->process(task, message))
509 {
510 case SUCCESS:
511 /* task completed, remove it */
512 this->active_tasks->remove_at(this->active_tasks, enumerator);
513 task->destroy(task);
514 break;
515 case NEED_MORE:
516 /* processed, but task needs another exchange */
517 break;
518 case FAILED:
519 default:
520 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
521 /* FALL */
522 case DESTROY_ME:
523 /* critical failure, destroy IKE_SA */
524 this->active_tasks->remove_at(this->active_tasks, enumerator);
525 enumerator->destroy(enumerator);
526 task->destroy(task);
527 return DESTROY_ME;
528 }
529 if (this->reset)
530 { /* start all over again if we were reset */
531 this->reset = FALSE;
532 enumerator->destroy(enumerator);
533 return initiate(this);
534 }
535 }
536 enumerator->destroy(enumerator);
537
538 this->initiating.mid++;
539 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
540 this->initiating.packet->destroy(this->initiating.packet);
541 this->initiating.packet = NULL;
542
543 return initiate(this);
544 }
545
546 /**
547 * handle exchange collisions
548 */
549 static bool handle_collisions(private_task_manager_t *this, task_t *task)
550 {
551 enumerator_t *enumerator;
552 task_t *active;
553 task_type_t type;
554
555 type = task->get_type(task);
556
557 /* do we have to check */
558 if (type == IKE_REKEY || type == CHILD_REKEY ||
559 type == CHILD_DELETE || type == IKE_DELETE || type == IKE_REAUTH)
560 {
561 /* find an exchange collision, and notify these tasks */
562 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
563 while (enumerator->enumerate(enumerator, (void**)&active))
564 {
565 switch (active->get_type(active))
566 {
567 case IKE_REKEY:
568 if (type == IKE_REKEY || type == IKE_DELETE ||
569 type == IKE_REAUTH)
570 {
571 ike_rekey_t *rekey = (ike_rekey_t*)active;
572 rekey->collide(rekey, task);
573 break;
574 }
575 continue;
576 case CHILD_REKEY:
577 if (type == CHILD_REKEY || type == CHILD_DELETE)
578 {
579 child_rekey_t *rekey = (child_rekey_t*)active;
580 rekey->collide(rekey, task);
581 break;
582 }
583 continue;
584 default:
585 continue;
586 }
587 enumerator->destroy(enumerator);
588 return TRUE;
589 }
590 enumerator->destroy(enumerator);
591 }
592 return FALSE;
593 }
594
595 /**
596 * build a response depending on the "passive" task list
597 */
598 static status_t build_response(private_task_manager_t *this, message_t *request)
599 {
600 enumerator_t *enumerator;
601 task_t *task;
602 message_t *message;
603 host_t *me, *other;
604 bool delete = FALSE;
605 status_t status;
606
607 me = request->get_destination(request);
608 other = request->get_source(request);
609
610 message = message_create();
611 message->set_exchange_type(message, request->get_exchange_type(request));
612 /* send response along the path the request came in */
613 message->set_source(message, me->clone(me));
614 message->set_destination(message, other->clone(other));
615 message->set_message_id(message, this->responding.mid);
616 message->set_request(message, FALSE);
617
618 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
619 while (enumerator->enumerate(enumerator, (void*)&task))
620 {
621 switch (task->build(task, message))
622 {
623 case SUCCESS:
624 /* task completed, remove it */
625 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
626 if (!handle_collisions(this, task))
627 {
628 task->destroy(task);
629 }
630 break;
631 case NEED_MORE:
632 /* processed, but task needs another exchange */
633 if (handle_collisions(this, task))
634 {
635 this->passive_tasks->remove_at(this->passive_tasks,
636 enumerator);
637 }
638 break;
639 case FAILED:
640 default:
641 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
642 /* FALL */
643 case DESTROY_ME:
644 /* destroy IKE_SA, but SEND response first */
645 delete = TRUE;
646 break;
647 }
648 if (delete)
649 {
650 break;
651 }
652 }
653 enumerator->destroy(enumerator);
654
655 /* remove resonder SPI if IKE_SA_INIT failed */
656 if (delete && request->get_exchange_type(request) == IKE_SA_INIT)
657 {
658 ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa);
659 id->set_responder_spi(id, 0);
660 }
661
662 /* message complete, send it */
663 DESTROY_IF(this->responding.packet);
664 this->responding.packet = NULL;
665 status = this->ike_sa->generate_message(this->ike_sa, message,
666 &this->responding.packet);
667 message->destroy(message);
668 if (status != SUCCESS)
669 {
670 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
671 return DESTROY_ME;
672 }
673
674 charon->sender->send(charon->sender,
675 this->responding.packet->clone(this->responding.packet));
676 if (delete)
677 {
678 return DESTROY_ME;
679 }
680 return SUCCESS;
681 }
682
683 /**
684 * handle an incoming request message
685 */
686 static status_t process_request(private_task_manager_t *this,
687 message_t *message)
688 {
689 enumerator_t *enumerator;
690 task_t *task = NULL;
691 payload_t *payload;
692 notify_payload_t *notify;
693 delete_payload_t *delete;
694
695 if (this->passive_tasks->get_count(this->passive_tasks) == 0)
696 { /* create tasks depending on request type, if not already some queued */
697 switch (message->get_exchange_type(message))
698 {
699 case IKE_SA_INIT:
700 {
701 task = (task_t*)ike_vendor_create(this->ike_sa, FALSE);
702 this->passive_tasks->insert_last(this->passive_tasks, task);
703 task = (task_t*)ike_init_create(this->ike_sa, FALSE, NULL);
704 this->passive_tasks->insert_last(this->passive_tasks, task);
705 task = (task_t*)ike_natd_create(this->ike_sa, FALSE);
706 this->passive_tasks->insert_last(this->passive_tasks, task);
707 task = (task_t*)ike_cert_pre_create(this->ike_sa, FALSE);
708 this->passive_tasks->insert_last(this->passive_tasks, task);
709 #ifdef ME
710 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
711 this->passive_tasks->insert_last(this->passive_tasks, task);
712 #endif /* ME */
713 task = (task_t*)ike_auth_create(this->ike_sa, FALSE);
714 this->passive_tasks->insert_last(this->passive_tasks, task);
715 task = (task_t*)ike_cert_post_create(this->ike_sa, FALSE);
716 this->passive_tasks->insert_last(this->passive_tasks, task);
717 task = (task_t*)ike_config_create(this->ike_sa, FALSE);
718 this->passive_tasks->insert_last(this->passive_tasks, task);
719 task = (task_t*)child_create_create(this->ike_sa, NULL, FALSE,
720 NULL, NULL);
721 this->passive_tasks->insert_last(this->passive_tasks, task);
722 task = (task_t*)ike_auth_lifetime_create(this->ike_sa, FALSE);
723 this->passive_tasks->insert_last(this->passive_tasks, task);
724 task = (task_t*)ike_mobike_create(this->ike_sa, FALSE);
725 this->passive_tasks->insert_last(this->passive_tasks, task);
726 break;
727 }
728 case CREATE_CHILD_SA:
729 { /* FIXME: we should prevent this on mediation connections */
730 bool notify_found = FALSE, ts_found = FALSE;
731 enumerator = message->create_payload_enumerator(message);
732 while (enumerator->enumerate(enumerator, &payload))
733 {
734 switch (payload->get_type(payload))
735 {
736 case NOTIFY:
737 { /* if we find a rekey notify, its CHILD_SA rekeying */
738 notify = (notify_payload_t*)payload;
739 if (notify->get_notify_type(notify) == REKEY_SA &&
740 (notify->get_protocol_id(notify) == PROTO_AH ||
741 notify->get_protocol_id(notify) == PROTO_ESP))
742 {
743 notify_found = TRUE;
744 }
745 break;
746 }
747 case TRAFFIC_SELECTOR_INITIATOR:
748 case TRAFFIC_SELECTOR_RESPONDER:
749 { /* if we don't find a TS, its IKE rekeying */
750 ts_found = TRUE;
751 break;
752 }
753 default:
754 break;
755 }
756 }
757 enumerator->destroy(enumerator);
758
759 if (ts_found)
760 {
761 if (notify_found)
762 {
763 task = (task_t*)child_rekey_create(this->ike_sa,
764 PROTO_NONE, 0);
765 }
766 else
767 {
768 task = (task_t*)child_create_create(this->ike_sa, NULL,
769 FALSE, NULL, NULL);
770 }
771 }
772 else
773 {
774 task = (task_t*)ike_rekey_create(this->ike_sa, FALSE);
775 }
776 this->passive_tasks->insert_last(this->passive_tasks, task);
777 break;
778 }
779 case INFORMATIONAL:
780 {
781 enumerator = message->create_payload_enumerator(message);
782 while (enumerator->enumerate(enumerator, &payload))
783 {
784 switch (payload->get_type(payload))
785 {
786 case NOTIFY:
787 {
788 notify = (notify_payload_t*)payload;
789 switch (notify->get_notify_type(notify))
790 {
791 case ADDITIONAL_IP4_ADDRESS:
792 case ADDITIONAL_IP6_ADDRESS:
793 case NO_ADDITIONAL_ADDRESSES:
794 case UPDATE_SA_ADDRESSES:
795 case NO_NATS_ALLOWED:
796 case UNACCEPTABLE_ADDRESSES:
797 case UNEXPECTED_NAT_DETECTED:
798 case COOKIE2:
799 case NAT_DETECTION_SOURCE_IP:
800 case NAT_DETECTION_DESTINATION_IP:
801 task = (task_t*)ike_mobike_create(
802 this->ike_sa, FALSE);
803 break;
804 case AUTH_LIFETIME:
805 task = (task_t*)ike_auth_lifetime_create(
806 this->ike_sa, FALSE);
807 break;
808 default:
809 break;
810 }
811 break;
812 }
813 case DELETE:
814 {
815 delete = (delete_payload_t*)payload;
816 if (delete->get_protocol_id(delete) == PROTO_IKE)
817 {
818 task = (task_t*)ike_delete_create(this->ike_sa,
819 FALSE);
820 }
821 else
822 {
823 task = (task_t*)child_delete_create(this->ike_sa,
824 PROTO_NONE, 0);
825 }
826 break;
827 }
828 default:
829 break;
830 }
831 if (task)
832 {
833 break;
834 }
835 }
836 enumerator->destroy(enumerator);
837
838 if (task == NULL)
839 {
840 task = (task_t*)ike_dpd_create(FALSE);
841 }
842 this->passive_tasks->insert_last(this->passive_tasks, task);
843 break;
844 }
845 #ifdef ME
846 case ME_CONNECT:
847 {
848 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
849 this->passive_tasks->insert_last(this->passive_tasks, task);
850 }
851 #endif /* ME */
852 default:
853 break;
854 }
855 }
856
857 /* let the tasks process the message */
858 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
859 while (enumerator->enumerate(enumerator, (void*)&task))
860 {
861 switch (task->process(task, message))
862 {
863 case SUCCESS:
864 /* task completed, remove it */
865 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
866 task->destroy(task);
867 break;
868 case NEED_MORE:
869 /* processed, but task needs at least another call to build() */
870 break;
871 case FAILED:
872 default:
873 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
874 /* FALL */
875 case DESTROY_ME:
876 /* critical failure, destroy IKE_SA */
877 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
878 enumerator->destroy(enumerator);
879 task->destroy(task);
880 return DESTROY_ME;
881 }
882 }
883 enumerator->destroy(enumerator);
884
885 return build_response(this, message);
886 }
887
888 METHOD(task_manager_t, process_message, status_t,
889 private_task_manager_t *this, message_t *msg)
890 {
891 host_t *me, *other;
892 u_int32_t mid;
893
894 mid = msg->get_message_id(msg);
895 me = msg->get_destination(msg);
896 other = msg->get_source(msg);
897
898 if (msg->get_request(msg))
899 {
900 if (mid == this->responding.mid)
901 {
902 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED ||
903 this->ike_sa->get_state(this->ike_sa) == IKE_CONNECTING ||
904 msg->get_exchange_type(msg) != IKE_SA_INIT)
905 { /* only do host updates based on verified messages */
906 if (!this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
907 { /* with MOBIKE, we do no implicit updates */
908 this->ike_sa->update_hosts(this->ike_sa, me, other, mid == 1);
909 }
910 }
911 charon->bus->message(charon->bus, msg, TRUE);
912 if (msg->get_exchange_type(msg) == EXCHANGE_TYPE_UNDEFINED)
913 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
914 return SUCCESS;
915 }
916 if (process_request(this, msg) != SUCCESS)
917 {
918 flush(this);
919 return DESTROY_ME;
920 }
921 this->responding.mid++;
922 }
923 else if ((mid == this->responding.mid - 1) && this->responding.packet)
924 {
925 packet_t *clone;
926 host_t *host;
927
928 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
929 "retransmitting response", mid);
930 clone = this->responding.packet->clone(this->responding.packet);
931 host = msg->get_destination(msg);
932 clone->set_source(clone, host->clone(host));
933 host = msg->get_source(msg);
934 clone->set_destination(clone, host->clone(host));
935 charon->sender->send(charon->sender, clone);
936 }
937 else
938 {
939 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
940 mid, this->responding.mid);
941 }
942 }
943 else
944 {
945 if (mid == this->initiating.mid)
946 {
947 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED ||
948 this->ike_sa->get_state(this->ike_sa) == IKE_CONNECTING ||
949 msg->get_exchange_type(msg) != IKE_SA_INIT)
950 { /* only do host updates based on verified messages */
951 if (!this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
952 { /* with MOBIKE, we do no implicit updates */
953 this->ike_sa->update_hosts(this->ike_sa, me, other, FALSE);
954 }
955 }
956 charon->bus->message(charon->bus, msg, TRUE);
957 if (msg->get_exchange_type(msg) == EXCHANGE_TYPE_UNDEFINED)
958 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
959 return SUCCESS;
960 }
961 if (process_response(this, msg) != SUCCESS)
962 {
963 flush(this);
964 return DESTROY_ME;
965 }
966 }
967 else
968 {
969 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
970 mid, this->initiating.mid);
971 return SUCCESS;
972 }
973 }
974 return SUCCESS;
975 }
976
977 METHOD(task_manager_t, queue_task, void,
978 private_task_manager_t *this, task_t *task)
979 {
980 if (task->get_type(task) == IKE_MOBIKE)
981 { /* there is no need to queue more than one mobike task */
982 enumerator_t *enumerator;
983 task_t *current;
984
985 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
986 while (enumerator->enumerate(enumerator, (void**)&current))
987 {
988 if (current->get_type(current) == IKE_MOBIKE)
989 {
990 enumerator->destroy(enumerator);
991 task->destroy(task);
992 return;
993 }
994 }
995 enumerator->destroy(enumerator);
996 }
997 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
998 this->queued_tasks->insert_last(this->queued_tasks, task);
999 }
1000
1001 METHOD(task_manager_t, adopt_tasks, void,
1002 private_task_manager_t *this, task_manager_t *other_public)
1003 {
1004 private_task_manager_t *other = (private_task_manager_t*)other_public;
1005 task_t *task;
1006
1007 /* move queued tasks from other to this */
1008 while (other->queued_tasks->remove_last(other->queued_tasks,
1009 (void**)&task) == SUCCESS)
1010 {
1011 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
1012 task->migrate(task, this->ike_sa);
1013 this->queued_tasks->insert_first(this->queued_tasks, task);
1014 }
1015 }
1016
1017 METHOD(task_manager_t, busy, bool,
1018 private_task_manager_t *this)
1019 {
1020 return (this->active_tasks->get_count(this->active_tasks) > 0);
1021 }
1022
1023 METHOD(task_manager_t, incr_mid, void,
1024 private_task_manager_t *this, bool initiate)
1025 {
1026 if (initiate)
1027 {
1028 this->initiating.mid++;
1029 }
1030 else
1031 {
1032 this->responding.mid++;
1033 }
1034 }
1035
1036 METHOD(task_manager_t, reset, void,
1037 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
1038 {
1039 enumerator_t *enumerator;
1040 task_t *task;
1041
1042 /* reset message counters and retransmit packets */
1043 DESTROY_IF(this->responding.packet);
1044 DESTROY_IF(this->initiating.packet);
1045 this->responding.packet = NULL;
1046 this->initiating.packet = NULL;
1047 if (initiate != UINT_MAX)
1048 {
1049 this->initiating.mid = initiate;
1050 }
1051 if (respond != UINT_MAX)
1052 {
1053 this->responding.mid = respond;
1054 }
1055 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
1056
1057 /* reset queued tasks */
1058 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1059 while (enumerator->enumerate(enumerator, &task))
1060 {
1061 task->migrate(task, this->ike_sa);
1062 }
1063 enumerator->destroy(enumerator);
1064
1065 /* reset active tasks */
1066 while (this->active_tasks->remove_last(this->active_tasks,
1067 (void**)&task) == SUCCESS)
1068 {
1069 task->migrate(task, this->ike_sa);
1070 this->queued_tasks->insert_first(this->queued_tasks, task);
1071 }
1072
1073 this->reset = TRUE;
1074 }
1075
1076 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
1077 private_task_manager_t *this, task_queue_t queue)
1078 {
1079 switch (queue)
1080 {
1081 case TASK_QUEUE_ACTIVE:
1082 return this->active_tasks->create_enumerator(this->active_tasks);
1083 case TASK_QUEUE_PASSIVE:
1084 return this->passive_tasks->create_enumerator(this->passive_tasks);
1085 case TASK_QUEUE_QUEUED:
1086 return this->queued_tasks->create_enumerator(this->queued_tasks);
1087 default:
1088 return enumerator_create_empty();
1089 }
1090 }
1091
1092 METHOD(task_manager_t, destroy, void,
1093 private_task_manager_t *this)
1094 {
1095 flush(this);
1096
1097 this->active_tasks->destroy(this->active_tasks);
1098 this->queued_tasks->destroy(this->queued_tasks);
1099 this->passive_tasks->destroy(this->passive_tasks);
1100
1101 DESTROY_IF(this->responding.packet);
1102 DESTROY_IF(this->initiating.packet);
1103 free(this);
1104 }
1105
1106 /*
1107 * see header file
1108 */
1109 task_manager_t *task_manager_create(ike_sa_t *ike_sa)
1110 {
1111 private_task_manager_t *this;
1112
1113 INIT(this,
1114 .public = {
1115 .process_message = _process_message,
1116 .queue_task = _queue_task,
1117 .initiate = _initiate,
1118 .retransmit = _retransmit,
1119 .incr_mid = _incr_mid,
1120 .reset = _reset,
1121 .adopt_tasks = _adopt_tasks,
1122 .busy = _busy,
1123 .create_task_enumerator = _create_task_enumerator,
1124 .destroy = _destroy,
1125 },
1126 .ike_sa = ike_sa,
1127 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
1128 .queued_tasks = linked_list_create(),
1129 .active_tasks = linked_list_create(),
1130 .passive_tasks = linked_list_create(),
1131 .retransmit_tries = lib->settings->get_int(lib->settings,
1132 "charon.retransmit_tries", RETRANSMIT_TRIES),
1133 .retransmit_timeout = lib->settings->get_double(lib->settings,
1134 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT),
1135 .retransmit_base = lib->settings->get_double(lib->settings,
1136 "charon.retransmit_base", RETRANSMIT_BASE),
1137 );
1138
1139 return &this->public;
1140 }
1141