Invoke ike_updown hook on authentication failure not before response sent
[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 if (activate_task(this, IKE_AUTH_LIFETIME))
370 {
371 exchange = INFORMATIONAL;
372 break;
373 }
374 #ifdef ME
375 if (activate_task(this, IKE_ME))
376 {
377 exchange = ME_CONNECT;
378 break;
379 }
380 #endif /* ME */
381 case IKE_REKEYING:
382 if (activate_task(this, IKE_DELETE))
383 {
384 exchange = INFORMATIONAL;
385 break;
386 }
387 case IKE_DELETING:
388 default:
389 break;
390 }
391 }
392 else
393 {
394 DBG2(DBG_IKE, "reinitiating already active tasks");
395 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
396 while (enumerator->enumerate(enumerator, (void**)&task))
397 {
398 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
399 switch (task->get_type(task))
400 {
401 case IKE_INIT:
402 exchange = IKE_SA_INIT;
403 break;
404 case IKE_AUTHENTICATE:
405 exchange = IKE_AUTH;
406 break;
407 case CHILD_CREATE:
408 case CHILD_REKEY:
409 case IKE_REKEY:
410 exchange = CREATE_CHILD_SA;
411 break;
412 case IKE_MOBIKE:
413 exchange = INFORMATIONAL;
414 break;
415 default:
416 continue;
417 }
418 break;
419 }
420 enumerator->destroy(enumerator);
421 }
422
423 if (exchange == 0)
424 {
425 DBG2(DBG_IKE, "nothing to initiate");
426 /* nothing to do yet... */
427 return SUCCESS;
428 }
429
430 me = this->ike_sa->get_my_host(this->ike_sa);
431 other = this->ike_sa->get_other_host(this->ike_sa);
432
433 message = message_create();
434 message->set_message_id(message, this->initiating.mid);
435 message->set_source(message, me->clone(me));
436 message->set_destination(message, other->clone(other));
437 message->set_exchange_type(message, exchange);
438 this->initiating.type = exchange;
439 this->initiating.retransmitted = 0;
440
441 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
442 while (enumerator->enumerate(enumerator, (void*)&task))
443 {
444 switch (task->build(task, message))
445 {
446 case SUCCESS:
447 /* task completed, remove it */
448 this->active_tasks->remove_at(this->active_tasks, enumerator);
449 task->destroy(task);
450 break;
451 case NEED_MORE:
452 /* processed, but task needs another exchange */
453 break;
454 case FAILED:
455 default:
456 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
457 {
458 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
459 }
460 /* FALL */
461 case DESTROY_ME:
462 /* critical failure, destroy IKE_SA */
463 enumerator->destroy(enumerator);
464 message->destroy(message);
465 flush(this);
466 return DESTROY_ME;
467 }
468 }
469 enumerator->destroy(enumerator);
470
471 /* update exchange type if a task changed it */
472 this->initiating.type = message->get_exchange_type(message);
473
474 status = this->ike_sa->generate_message(this->ike_sa, message,
475 &this->initiating.packet);
476 if (status != SUCCESS)
477 {
478 /* message generation failed. There is nothing more to do than to
479 * close the SA */
480 message->destroy(message);
481 flush(this);
482 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
483 return DESTROY_ME;
484 }
485 message->destroy(message);
486
487 return retransmit(this, this->initiating.mid);
488 }
489
490 /**
491 * handle an incoming response message
492 */
493 static status_t process_response(private_task_manager_t *this,
494 message_t *message)
495 {
496 enumerator_t *enumerator;
497 task_t *task;
498
499 if (message->get_exchange_type(message) != this->initiating.type)
500 {
501 DBG1(DBG_IKE, "received %N response, but expected %N",
502 exchange_type_names, message->get_exchange_type(message),
503 exchange_type_names, this->initiating.type);
504 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
505 return DESTROY_ME;
506 }
507
508 /* catch if we get resetted while processing */
509 this->reset = FALSE;
510 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
511 while (enumerator->enumerate(enumerator, (void*)&task))
512 {
513 switch (task->process(task, message))
514 {
515 case SUCCESS:
516 /* task completed, remove it */
517 this->active_tasks->remove_at(this->active_tasks, enumerator);
518 task->destroy(task);
519 break;
520 case NEED_MORE:
521 /* processed, but task needs another exchange */
522 break;
523 case FAILED:
524 default:
525 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
526 /* FALL */
527 case DESTROY_ME:
528 /* critical failure, destroy IKE_SA */
529 this->active_tasks->remove_at(this->active_tasks, enumerator);
530 enumerator->destroy(enumerator);
531 task->destroy(task);
532 return DESTROY_ME;
533 }
534 if (this->reset)
535 { /* start all over again if we were reset */
536 this->reset = FALSE;
537 enumerator->destroy(enumerator);
538 return initiate(this);
539 }
540 }
541 enumerator->destroy(enumerator);
542
543 this->initiating.mid++;
544 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
545 this->initiating.packet->destroy(this->initiating.packet);
546 this->initiating.packet = NULL;
547
548 return initiate(this);
549 }
550
551 /**
552 * handle exchange collisions
553 */
554 static bool handle_collisions(private_task_manager_t *this, task_t *task)
555 {
556 enumerator_t *enumerator;
557 task_t *active;
558 task_type_t type;
559
560 type = task->get_type(task);
561
562 /* do we have to check */
563 if (type == IKE_REKEY || type == CHILD_REKEY ||
564 type == CHILD_DELETE || type == IKE_DELETE || type == IKE_REAUTH)
565 {
566 /* find an exchange collision, and notify these tasks */
567 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
568 while (enumerator->enumerate(enumerator, (void**)&active))
569 {
570 switch (active->get_type(active))
571 {
572 case IKE_REKEY:
573 if (type == IKE_REKEY || type == IKE_DELETE ||
574 type == IKE_REAUTH)
575 {
576 ike_rekey_t *rekey = (ike_rekey_t*)active;
577 rekey->collide(rekey, task);
578 break;
579 }
580 continue;
581 case CHILD_REKEY:
582 if (type == CHILD_REKEY || type == CHILD_DELETE)
583 {
584 child_rekey_t *rekey = (child_rekey_t*)active;
585 rekey->collide(rekey, task);
586 break;
587 }
588 continue;
589 default:
590 continue;
591 }
592 enumerator->destroy(enumerator);
593 return TRUE;
594 }
595 enumerator->destroy(enumerator);
596 }
597 return FALSE;
598 }
599
600 /**
601 * build a response depending on the "passive" task list
602 */
603 static status_t build_response(private_task_manager_t *this, message_t *request)
604 {
605 enumerator_t *enumerator;
606 task_t *task;
607 message_t *message;
608 host_t *me, *other;
609 bool delete = FALSE;
610 status_t status;
611
612 me = request->get_destination(request);
613 other = request->get_source(request);
614
615 message = message_create();
616 message->set_exchange_type(message, request->get_exchange_type(request));
617 /* send response along the path the request came in */
618 message->set_source(message, me->clone(me));
619 message->set_destination(message, other->clone(other));
620 message->set_message_id(message, this->responding.mid);
621 message->set_request(message, FALSE);
622
623 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
624 while (enumerator->enumerate(enumerator, (void*)&task))
625 {
626 switch (task->build(task, message))
627 {
628 case SUCCESS:
629 /* task completed, remove it */
630 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
631 if (!handle_collisions(this, task))
632 {
633 task->destroy(task);
634 }
635 break;
636 case NEED_MORE:
637 /* processed, but task needs another exchange */
638 if (handle_collisions(this, task))
639 {
640 this->passive_tasks->remove_at(this->passive_tasks,
641 enumerator);
642 }
643 break;
644 case DESTROY_ME:
645 case FAILED:
646 default:
647 /* destroy IKE_SA, but SEND response first */
648 delete = TRUE;
649 break;
650 }
651 if (delete)
652 {
653 break;
654 }
655 }
656 enumerator->destroy(enumerator);
657
658 /* remove resonder SPI if IKE_SA_INIT failed */
659 if (delete && request->get_exchange_type(request) == IKE_SA_INIT)
660 {
661 ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa);
662 id->set_responder_spi(id, 0);
663 }
664
665 /* message complete, send it */
666 DESTROY_IF(this->responding.packet);
667 this->responding.packet = NULL;
668 status = this->ike_sa->generate_message(this->ike_sa, message,
669 &this->responding.packet);
670 message->destroy(message);
671 if (status != SUCCESS)
672 {
673 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
674 return DESTROY_ME;
675 }
676
677 charon->sender->send(charon->sender,
678 this->responding.packet->clone(this->responding.packet));
679 if (delete)
680 {
681 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
682 return DESTROY_ME;
683 }
684 return SUCCESS;
685 }
686
687 /**
688 * handle an incoming request message
689 */
690 static status_t process_request(private_task_manager_t *this,
691 message_t *message)
692 {
693 enumerator_t *enumerator;
694 task_t *task = NULL;
695 payload_t *payload;
696 notify_payload_t *notify;
697 delete_payload_t *delete;
698
699 if (this->passive_tasks->get_count(this->passive_tasks) == 0)
700 { /* create tasks depending on request type, if not already some queued */
701 switch (message->get_exchange_type(message))
702 {
703 case IKE_SA_INIT:
704 {
705 task = (task_t*)ike_vendor_create(this->ike_sa, FALSE);
706 this->passive_tasks->insert_last(this->passive_tasks, task);
707 task = (task_t*)ike_init_create(this->ike_sa, FALSE, NULL);
708 this->passive_tasks->insert_last(this->passive_tasks, task);
709 task = (task_t*)ike_natd_create(this->ike_sa, FALSE);
710 this->passive_tasks->insert_last(this->passive_tasks, task);
711 task = (task_t*)ike_cert_pre_create(this->ike_sa, FALSE);
712 this->passive_tasks->insert_last(this->passive_tasks, task);
713 #ifdef ME
714 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
715 this->passive_tasks->insert_last(this->passive_tasks, task);
716 #endif /* ME */
717 task = (task_t*)ike_auth_create(this->ike_sa, FALSE);
718 this->passive_tasks->insert_last(this->passive_tasks, task);
719 task = (task_t*)ike_cert_post_create(this->ike_sa, FALSE);
720 this->passive_tasks->insert_last(this->passive_tasks, task);
721 task = (task_t*)ike_config_create(this->ike_sa, FALSE);
722 this->passive_tasks->insert_last(this->passive_tasks, task);
723 task = (task_t*)child_create_create(this->ike_sa, NULL, FALSE,
724 NULL, NULL);
725 this->passive_tasks->insert_last(this->passive_tasks, task);
726 task = (task_t*)ike_auth_lifetime_create(this->ike_sa, FALSE);
727 this->passive_tasks->insert_last(this->passive_tasks, task);
728 task = (task_t*)ike_mobike_create(this->ike_sa, FALSE);
729 this->passive_tasks->insert_last(this->passive_tasks, task);
730 break;
731 }
732 case CREATE_CHILD_SA:
733 { /* FIXME: we should prevent this on mediation connections */
734 bool notify_found = FALSE, ts_found = FALSE;
735 enumerator = message->create_payload_enumerator(message);
736 while (enumerator->enumerate(enumerator, &payload))
737 {
738 switch (payload->get_type(payload))
739 {
740 case NOTIFY:
741 { /* if we find a rekey notify, its CHILD_SA rekeying */
742 notify = (notify_payload_t*)payload;
743 if (notify->get_notify_type(notify) == REKEY_SA &&
744 (notify->get_protocol_id(notify) == PROTO_AH ||
745 notify->get_protocol_id(notify) == PROTO_ESP))
746 {
747 notify_found = TRUE;
748 }
749 break;
750 }
751 case TRAFFIC_SELECTOR_INITIATOR:
752 case TRAFFIC_SELECTOR_RESPONDER:
753 { /* if we don't find a TS, its IKE rekeying */
754 ts_found = TRUE;
755 break;
756 }
757 default:
758 break;
759 }
760 }
761 enumerator->destroy(enumerator);
762
763 if (ts_found)
764 {
765 if (notify_found)
766 {
767 task = (task_t*)child_rekey_create(this->ike_sa,
768 PROTO_NONE, 0);
769 }
770 else
771 {
772 task = (task_t*)child_create_create(this->ike_sa, NULL,
773 FALSE, NULL, NULL);
774 }
775 }
776 else
777 {
778 task = (task_t*)ike_rekey_create(this->ike_sa, FALSE);
779 }
780 this->passive_tasks->insert_last(this->passive_tasks, task);
781 break;
782 }
783 case INFORMATIONAL:
784 {
785 enumerator = message->create_payload_enumerator(message);
786 while (enumerator->enumerate(enumerator, &payload))
787 {
788 switch (payload->get_type(payload))
789 {
790 case NOTIFY:
791 {
792 notify = (notify_payload_t*)payload;
793 switch (notify->get_notify_type(notify))
794 {
795 case ADDITIONAL_IP4_ADDRESS:
796 case ADDITIONAL_IP6_ADDRESS:
797 case NO_ADDITIONAL_ADDRESSES:
798 case UPDATE_SA_ADDRESSES:
799 case NO_NATS_ALLOWED:
800 case UNACCEPTABLE_ADDRESSES:
801 case UNEXPECTED_NAT_DETECTED:
802 case COOKIE2:
803 case NAT_DETECTION_SOURCE_IP:
804 case NAT_DETECTION_DESTINATION_IP:
805 task = (task_t*)ike_mobike_create(
806 this->ike_sa, FALSE);
807 break;
808 case AUTH_LIFETIME:
809 task = (task_t*)ike_auth_lifetime_create(
810 this->ike_sa, FALSE);
811 break;
812 default:
813 break;
814 }
815 break;
816 }
817 case DELETE:
818 {
819 delete = (delete_payload_t*)payload;
820 if (delete->get_protocol_id(delete) == PROTO_IKE)
821 {
822 task = (task_t*)ike_delete_create(this->ike_sa,
823 FALSE);
824 }
825 else
826 {
827 task = (task_t*)child_delete_create(this->ike_sa,
828 PROTO_NONE, 0);
829 }
830 break;
831 }
832 default:
833 break;
834 }
835 if (task)
836 {
837 break;
838 }
839 }
840 enumerator->destroy(enumerator);
841
842 if (task == NULL)
843 {
844 task = (task_t*)ike_dpd_create(FALSE);
845 }
846 this->passive_tasks->insert_last(this->passive_tasks, task);
847 break;
848 }
849 #ifdef ME
850 case ME_CONNECT:
851 {
852 task = (task_t*)ike_me_create(this->ike_sa, FALSE);
853 this->passive_tasks->insert_last(this->passive_tasks, task);
854 }
855 #endif /* ME */
856 default:
857 break;
858 }
859 }
860
861 /* let the tasks process the message */
862 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
863 while (enumerator->enumerate(enumerator, (void*)&task))
864 {
865 switch (task->process(task, message))
866 {
867 case SUCCESS:
868 /* task completed, remove it */
869 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
870 task->destroy(task);
871 break;
872 case NEED_MORE:
873 /* processed, but task needs at least another call to build() */
874 break;
875 case FAILED:
876 default:
877 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
878 /* FALL */
879 case DESTROY_ME:
880 /* critical failure, destroy IKE_SA */
881 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
882 enumerator->destroy(enumerator);
883 task->destroy(task);
884 return DESTROY_ME;
885 }
886 }
887 enumerator->destroy(enumerator);
888
889 return build_response(this, message);
890 }
891
892 METHOD(task_manager_t, process_message, status_t,
893 private_task_manager_t *this, message_t *msg)
894 {
895 host_t *me, *other;
896 u_int32_t mid;
897
898 mid = msg->get_message_id(msg);
899 me = msg->get_destination(msg);
900 other = msg->get_source(msg);
901
902 if (msg->get_request(msg))
903 {
904 if (mid == this->responding.mid)
905 {
906 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED ||
907 this->ike_sa->get_state(this->ike_sa) == IKE_CONNECTING ||
908 msg->get_exchange_type(msg) != IKE_SA_INIT)
909 { /* only do host updates based on verified messages */
910 if (!this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
911 { /* with MOBIKE, we do no implicit updates */
912 this->ike_sa->update_hosts(this->ike_sa, me, other, mid == 1);
913 }
914 }
915 charon->bus->message(charon->bus, msg, TRUE);
916 if (msg->get_exchange_type(msg) == EXCHANGE_TYPE_UNDEFINED)
917 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
918 return SUCCESS;
919 }
920 if (process_request(this, msg) != SUCCESS)
921 {
922 flush(this);
923 return DESTROY_ME;
924 }
925 this->responding.mid++;
926 }
927 else if ((mid == this->responding.mid - 1) && this->responding.packet)
928 {
929 packet_t *clone;
930 host_t *host;
931
932 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
933 "retransmitting response", mid);
934 clone = this->responding.packet->clone(this->responding.packet);
935 host = msg->get_destination(msg);
936 clone->set_source(clone, host->clone(host));
937 host = msg->get_source(msg);
938 clone->set_destination(clone, host->clone(host));
939 charon->sender->send(charon->sender, clone);
940 }
941 else
942 {
943 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
944 mid, this->responding.mid);
945 }
946 }
947 else
948 {
949 if (mid == this->initiating.mid)
950 {
951 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED ||
952 this->ike_sa->get_state(this->ike_sa) == IKE_CONNECTING ||
953 msg->get_exchange_type(msg) != IKE_SA_INIT)
954 { /* only do host updates based on verified messages */
955 if (!this->ike_sa->supports_extension(this->ike_sa, EXT_MOBIKE))
956 { /* with MOBIKE, we do no implicit updates */
957 this->ike_sa->update_hosts(this->ike_sa, me, other, FALSE);
958 }
959 }
960 charon->bus->message(charon->bus, msg, TRUE);
961 if (msg->get_exchange_type(msg) == EXCHANGE_TYPE_UNDEFINED)
962 { /* ignore messages altered to EXCHANGE_TYPE_UNDEFINED */
963 return SUCCESS;
964 }
965 if (process_response(this, msg) != SUCCESS)
966 {
967 flush(this);
968 return DESTROY_ME;
969 }
970 }
971 else
972 {
973 DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
974 mid, this->initiating.mid);
975 return SUCCESS;
976 }
977 }
978 return SUCCESS;
979 }
980
981 METHOD(task_manager_t, queue_task, void,
982 private_task_manager_t *this, task_t *task)
983 {
984 if (task->get_type(task) == IKE_MOBIKE)
985 { /* there is no need to queue more than one mobike task */
986 enumerator_t *enumerator;
987 task_t *current;
988
989 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
990 while (enumerator->enumerate(enumerator, (void**)&current))
991 {
992 if (current->get_type(current) == IKE_MOBIKE)
993 {
994 enumerator->destroy(enumerator);
995 task->destroy(task);
996 return;
997 }
998 }
999 enumerator->destroy(enumerator);
1000 }
1001 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
1002 this->queued_tasks->insert_last(this->queued_tasks, task);
1003 }
1004
1005 METHOD(task_manager_t, adopt_tasks, void,
1006 private_task_manager_t *this, task_manager_t *other_public)
1007 {
1008 private_task_manager_t *other = (private_task_manager_t*)other_public;
1009 task_t *task;
1010
1011 /* move queued tasks from other to this */
1012 while (other->queued_tasks->remove_last(other->queued_tasks,
1013 (void**)&task) == SUCCESS)
1014 {
1015 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
1016 task->migrate(task, this->ike_sa);
1017 this->queued_tasks->insert_first(this->queued_tasks, task);
1018 }
1019 }
1020
1021 METHOD(task_manager_t, busy, bool,
1022 private_task_manager_t *this)
1023 {
1024 return (this->active_tasks->get_count(this->active_tasks) > 0);
1025 }
1026
1027 METHOD(task_manager_t, incr_mid, void,
1028 private_task_manager_t *this, bool initiate)
1029 {
1030 if (initiate)
1031 {
1032 this->initiating.mid++;
1033 }
1034 else
1035 {
1036 this->responding.mid++;
1037 }
1038 }
1039
1040 METHOD(task_manager_t, reset, void,
1041 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
1042 {
1043 enumerator_t *enumerator;
1044 task_t *task;
1045
1046 /* reset message counters and retransmit packets */
1047 DESTROY_IF(this->responding.packet);
1048 DESTROY_IF(this->initiating.packet);
1049 this->responding.packet = NULL;
1050 this->initiating.packet = NULL;
1051 if (initiate != UINT_MAX)
1052 {
1053 this->initiating.mid = initiate;
1054 }
1055 if (respond != UINT_MAX)
1056 {
1057 this->responding.mid = respond;
1058 }
1059 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
1060
1061 /* reset queued tasks */
1062 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
1063 while (enumerator->enumerate(enumerator, &task))
1064 {
1065 task->migrate(task, this->ike_sa);
1066 }
1067 enumerator->destroy(enumerator);
1068
1069 /* reset active tasks */
1070 while (this->active_tasks->remove_last(this->active_tasks,
1071 (void**)&task) == SUCCESS)
1072 {
1073 task->migrate(task, this->ike_sa);
1074 this->queued_tasks->insert_first(this->queued_tasks, task);
1075 }
1076
1077 this->reset = TRUE;
1078 }
1079
1080 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
1081 private_task_manager_t *this, task_queue_t queue)
1082 {
1083 switch (queue)
1084 {
1085 case TASK_QUEUE_ACTIVE:
1086 return this->active_tasks->create_enumerator(this->active_tasks);
1087 case TASK_QUEUE_PASSIVE:
1088 return this->passive_tasks->create_enumerator(this->passive_tasks);
1089 case TASK_QUEUE_QUEUED:
1090 return this->queued_tasks->create_enumerator(this->queued_tasks);
1091 default:
1092 return enumerator_create_empty();
1093 }
1094 }
1095
1096 METHOD(task_manager_t, destroy, void,
1097 private_task_manager_t *this)
1098 {
1099 flush(this);
1100
1101 this->active_tasks->destroy(this->active_tasks);
1102 this->queued_tasks->destroy(this->queued_tasks);
1103 this->passive_tasks->destroy(this->passive_tasks);
1104
1105 DESTROY_IF(this->responding.packet);
1106 DESTROY_IF(this->initiating.packet);
1107 free(this);
1108 }
1109
1110 /*
1111 * see header file
1112 */
1113 task_manager_t *task_manager_create(ike_sa_t *ike_sa)
1114 {
1115 private_task_manager_t *this;
1116
1117 INIT(this,
1118 .public = {
1119 .process_message = _process_message,
1120 .queue_task = _queue_task,
1121 .initiate = _initiate,
1122 .retransmit = _retransmit,
1123 .incr_mid = _incr_mid,
1124 .reset = _reset,
1125 .adopt_tasks = _adopt_tasks,
1126 .busy = _busy,
1127 .create_task_enumerator = _create_task_enumerator,
1128 .destroy = _destroy,
1129 },
1130 .ike_sa = ike_sa,
1131 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
1132 .queued_tasks = linked_list_create(),
1133 .active_tasks = linked_list_create(),
1134 .passive_tasks = linked_list_create(),
1135 .retransmit_tries = lib->settings->get_int(lib->settings,
1136 "charon.retransmit_tries", RETRANSMIT_TRIES),
1137 .retransmit_timeout = lib->settings->get_double(lib->settings,
1138 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT),
1139 .retransmit_base = lib->settings->get_double(lib->settings,
1140 "charon.retransmit_base", RETRANSMIT_BASE),
1141 );
1142
1143 return &this->public;
1144 }
1145