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