Allow IKEv1 tasks to return ALREADY_DONE to flush all active or passive tasks
[strongswan.git] / src / libcharon / sa / 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/tasks/child_delete.h>
23 #include <sa/tasks/main_mode.h>
24 #include <sa/tasks/quick_mode.h>
25 #include <sa/tasks/xauth.h>
26 #include <sa/tasks/mode_config.h>
27 #include <sa/tasks/ike_delete.h>
28 #include <sa/tasks/ike_natd_v1.h>
29 #include <sa/tasks/ike_vendor_v1.h>
30 #include <sa/tasks/ike_cert_pre_v1.h>
31 #include <sa/tasks/ike_cert_post_v1.h>
32 #include <encoding/payloads/delete_payload.h>
33 #include <processing/jobs/retransmit_job.h>
34 #include <processing/jobs/delete_ike_sa_job.h>
35
36 typedef struct exchange_t exchange_t;
37
38 /**
39 * An exchange in the air, used do detect and handle retransmission
40 */
41 struct exchange_t {
42
43 /**
44 * Message ID used for this transaction
45 */
46 u_int32_t mid;
47
48 /**
49 * generated packet for retransmission
50 */
51 packet_t *packet;
52 };
53
54 typedef struct private_task_manager_t private_task_manager_t;
55
56 /**
57 * private data of the task manager
58 */
59 struct private_task_manager_t {
60
61 /**
62 * public functions
63 */
64 task_manager_v1_t public;
65
66 /**
67 * associated IKE_SA we are serving
68 */
69 ike_sa_t *ike_sa;
70
71 /**
72 * RNG to create message IDs
73 */
74 rng_t *rng;
75
76 /**
77 * Exchange we are currently handling as responder
78 */
79 struct {
80 /**
81 * Message ID of the exchange
82 */
83 u_int32_t mid;
84
85 /**
86 * Hash of a previously received message
87 */
88 u_int32_t hash;
89
90 /**
91 * packet for retransmission
92 */
93 packet_t *packet;
94
95 } responding;
96
97 /**
98 * Exchange we are currently handling as initiator
99 */
100 struct {
101 /**
102 * Message ID of the exchange
103 */
104 u_int32_t mid;
105
106 /**
107 * Sequence number of the last sent message
108 */
109 u_int32_t seqnr;
110
111 /**
112 * how many times we have retransmitted so far
113 */
114 u_int retransmitted;
115
116 /**
117 * packet for retransmission
118 */
119 packet_t *packet;
120
121 /**
122 * type of the initated exchange
123 */
124 exchange_type_t type;
125
126 } initiating;
127
128 /**
129 * List of queued tasks not yet in action
130 */
131 linked_list_t *queued_tasks;
132
133 /**
134 * List of active tasks, initiated by ourselve
135 */
136 linked_list_t *active_tasks;
137
138 /**
139 * List of tasks initiated by peer
140 */
141 linked_list_t *passive_tasks;
142
143 /**
144 * Number of times we retransmit messages before giving up
145 */
146 u_int retransmit_tries;
147
148 /**
149 * Retransmission timeout
150 */
151 double retransmit_timeout;
152
153 /**
154 * Base to calculate retransmission timeout
155 */
156 double retransmit_base;
157 };
158
159 /**
160 * Flush a single task queue
161 */
162 static void flush_queue(private_task_manager_t *this, linked_list_t *list)
163 {
164 task_t *task;
165
166 while (list->remove_last(list, (void**)&task) == SUCCESS)
167 {
168 task->destroy(task);
169 }
170 }
171
172 /**
173 * flush all tasks in the task manager
174 */
175 static void flush(private_task_manager_t *this)
176 {
177 flush_queue(this, this->queued_tasks);
178 flush_queue(this, this->passive_tasks);
179 flush_queue(this, this->active_tasks);
180 }
181
182 /**
183 * move a task of a specific type from the queue to the active list
184 */
185 static bool activate_task(private_task_manager_t *this, task_type_t type)
186 {
187 enumerator_t *enumerator;
188 task_t *task;
189 bool found = FALSE;
190
191 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
192 while (enumerator->enumerate(enumerator, (void**)&task))
193 {
194 if (task->get_type(task) == type)
195 {
196 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
197 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
198 this->active_tasks->insert_last(this->active_tasks, task);
199 found = TRUE;
200 break;
201 }
202 }
203 enumerator->destroy(enumerator);
204 return found;
205 }
206
207 METHOD(task_manager_t, retransmit, status_t,
208 private_task_manager_t *this, u_int32_t message_seqnr)
209 {
210 /* this.initiating packet used as marker for received response */
211 if (message_seqnr == this->initiating.seqnr && this->initiating.packet )
212 {
213 u_int32_t timeout;
214 packet_t *packet;
215 job_t *job;
216
217 if (this->initiating.retransmitted <= this->retransmit_tries)
218 {
219 timeout = (u_int32_t)(this->retransmit_timeout * 1000.0 *
220 pow(this->retransmit_base, this->initiating.retransmitted));
221 }
222 else
223 {
224 DBG1(DBG_IKE, "giving up after %d retransmits",
225 this->initiating.retransmitted - 1);
226 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
227 {
228 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
229 }
230 return DESTROY_ME;
231 }
232
233 if (this->initiating.retransmitted)
234 {
235 DBG1(DBG_IKE, "retransmit %d of request with message ID %d seqnr (%d)",
236 this->initiating.retransmitted, this->initiating.mid, message_seqnr);
237 }
238 packet = this->initiating.packet->clone(this->initiating.packet);
239 charon->sender->send(charon->sender, packet);
240
241 this->initiating.retransmitted++;
242 job = (job_t*)retransmit_job_create(this->initiating.seqnr,
243 this->ike_sa->get_id(this->ike_sa));
244 lib->scheduler->schedule_job_ms(lib->scheduler, job, timeout);
245 }
246 return SUCCESS;
247 }
248
249 METHOD(task_manager_t, initiate, status_t,
250 private_task_manager_t *this)
251 {
252 enumerator_t *enumerator;
253 task_t *task;
254 message_t *message;
255 host_t *me, *other;
256 status_t status;
257 exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED;
258 bool new_mid = FALSE;
259 bool expect_response = FALSE;
260
261 if (!this->rng)
262 {
263 DBG1(DBG_IKE, "no RNG supported");
264 return FAILED;
265 }
266
267 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
268 {
269 DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
270 exchange_type_names, this->initiating.type);
271 /* do not initiate if we already have a message in the air */
272 return SUCCESS;
273 }
274
275 if (this->active_tasks->get_count(this->active_tasks) == 0)
276 {
277 DBG2(DBG_IKE, "activating new tasks");
278 switch (this->ike_sa->get_state(this->ike_sa))
279 {
280 case IKE_CREATED:
281 activate_task(this, TASK_VENDOR_V1);
282 activate_task(this, TASK_IKE_CERT_PRE_V1);
283 if (activate_task(this, TASK_MAIN_MODE))
284 {
285 exchange = ID_PROT;
286 activate_task(this, TASK_IKE_CERT_POST_V1);
287 activate_task(this, TASK_IKE_NATD_V1);
288 }
289 break;
290 case IKE_CONNECTING:
291 if (activate_task(this, TASK_XAUTH))
292 {
293 exchange = TRANSACTION;
294 new_mid = TRUE;
295 }
296 break;
297 case IKE_ESTABLISHED:
298 if (activate_task(this, TASK_MODE_CONFIG))
299 {
300 exchange = TRANSACTION;
301 new_mid = TRUE;
302 break;
303 }
304 if (activate_task(this, TASK_QUICK_MODE))
305 {
306 exchange = QUICK_MODE;
307 new_mid = TRUE;
308 break;
309 }
310 break;
311 default:
312 break;
313 }
314 }
315 else
316 {
317 DBG2(DBG_IKE, "reinitiating already active tasks");
318 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
319 while (enumerator->enumerate(enumerator, (void**)&task))
320 {
321 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
322 switch (task->get_type(task))
323 {
324 case TASK_MAIN_MODE:
325 exchange = ID_PROT;
326 break;
327 case TASK_QUICK_MODE:
328 exchange = QUICK_MODE;
329 break;
330 case TASK_XAUTH:
331 exchange = TRANSACTION;
332 new_mid = TRUE;
333 break;
334 default:
335 continue;
336 }
337 break;
338 }
339 enumerator->destroy(enumerator);
340 }
341
342 if (exchange == EXCHANGE_TYPE_UNDEFINED)
343 {
344 DBG2(DBG_IKE, "nothing to initiate");
345 /* nothing to do yet... */
346 return SUCCESS;
347 }
348
349 me = this->ike_sa->get_my_host(this->ike_sa);
350 other = this->ike_sa->get_other_host(this->ike_sa);
351
352 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
353 if (new_mid)
354 {
355 this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
356 (void*)&this->initiating.mid);
357 }
358 message->set_message_id(message, this->initiating.mid);
359 message->set_source(message, me->clone(me));
360 message->set_destination(message, other->clone(other));
361 message->set_exchange_type(message, exchange);
362 this->initiating.type = exchange;
363 this->initiating.retransmitted = 0;
364
365 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
366 while (enumerator->enumerate(enumerator, (void*)&task))
367 {
368 switch (task->build(task, message))
369 {
370 case SUCCESS:
371 /* task completed, remove it */
372 this->active_tasks->remove_at(this->active_tasks, enumerator);
373 task->destroy(task);
374 continue;
375 case NEED_MORE:
376 expect_response = TRUE;
377 /* processed, but task needs another exchange */
378 continue;
379 case ALREADY_DONE:
380 flush_queue(this, this->active_tasks);
381 break;
382 case FAILED:
383 default:
384 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
385 {
386 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
387 }
388 /* FALL */
389 case DESTROY_ME:
390 /* critical failure, destroy IKE_SA */
391 enumerator->destroy(enumerator);
392 message->destroy(message);
393 flush(this);
394 return DESTROY_ME;
395 }
396 break;
397 }
398 enumerator->destroy(enumerator);
399
400 /* update exchange type if a task changed it */
401 this->initiating.type = message->get_exchange_type(message);
402 this->initiating.seqnr++;
403
404 status = this->ike_sa->generate_message(this->ike_sa, message,
405 &this->initiating.packet);
406 if (status != SUCCESS)
407 {
408 /* message generation failed. There is nothing more to do than to
409 * close the SA */
410 message->destroy(message);
411 flush(this);
412 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
413 return DESTROY_ME;
414 }
415 message->destroy(message);
416
417 if (expect_response)
418 {
419 return retransmit(this, this->initiating.seqnr);
420 }
421 charon->sender->send(charon->sender,
422 this->initiating.packet->clone(this->initiating.packet));
423 this->initiating.packet->destroy(this->initiating.packet);
424 this->initiating.packet = NULL;
425 return SUCCESS;
426 }
427
428 /**
429 * handle exchange collisions
430 */
431 static bool handle_collisions(private_task_manager_t *this, task_t *task)
432 {
433 return FALSE;
434 }
435
436 /**
437 * build a response depending on the "passive" task list
438 */
439 static status_t build_response(private_task_manager_t *this, message_t *request)
440 {
441 enumerator_t *enumerator;
442 task_t *task;
443 message_t *message;
444 host_t *me, *other;
445 bool delete = FALSE;
446 status_t status;
447
448 me = request->get_destination(request);
449 other = request->get_source(request);
450
451 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
452 message->set_exchange_type(message, request->get_exchange_type(request));
453 /* send response along the path the request came in */
454 message->set_source(message, me->clone(me));
455 message->set_destination(message, other->clone(other));
456 message->set_message_id(message, request->get_message_id(request));
457 message->set_request(message, FALSE);
458
459 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
460 while (enumerator->enumerate(enumerator, (void*)&task))
461 {
462 switch (task->build(task, message))
463 {
464 case SUCCESS:
465 /* task completed, remove it */
466 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
467 if (!handle_collisions(this, task))
468 {
469 task->destroy(task);
470 }
471 continue;
472 case NEED_MORE:
473 /* processed, but task needs another exchange */
474 if (handle_collisions(this, task))
475 {
476 this->passive_tasks->remove_at(this->passive_tasks,
477 enumerator);
478 }
479 continue;
480 case ALREADY_DONE:
481 flush_queue(this, this->passive_tasks);
482 break;
483 case FAILED:
484 default:
485 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
486 /* FALL */
487 case DESTROY_ME:
488 /* destroy IKE_SA, but SEND response first */
489 delete = TRUE;
490 break;
491 }
492 break;
493 }
494 enumerator->destroy(enumerator);
495
496 /* message complete, send it */
497 DESTROY_IF(this->responding.packet);
498 this->responding.packet = NULL;
499 status = this->ike_sa->generate_message(this->ike_sa, message,
500 &this->responding.packet);
501 message->destroy(message);
502 if (status != SUCCESS)
503 {
504 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
505 return DESTROY_ME;
506 }
507
508 charon->sender->send(charon->sender,
509 this->responding.packet->clone(this->responding.packet));
510 if (delete)
511 {
512 return DESTROY_ME;
513 }
514 return SUCCESS;
515 }
516
517 /**
518 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
519 * The notify protocol_id is set to ISAKMP
520 */
521 static void send_notify_response(private_task_manager_t *this,
522 message_t *request, notify_type_t type,
523 chunk_t data, task_t *task)
524 {
525 message_t *response;
526 packet_t *packet;
527 host_t *me, *other;
528 u_int32_t mid;
529
530 if (request && request->get_exchange_type(request) == INFORMATIONAL_V1)
531 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
532 DBG1(DBG_IKE, "ignore malformed INFORMATIONAL request");
533 return;
534 }
535
536 response = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
537 response->set_exchange_type(response, INFORMATIONAL_V1);
538 response->set_request(response, TRUE);
539 this->rng->get_bytes(this->rng, sizeof(mid), (void*)&mid);
540 response->set_message_id(response, mid);
541
542 if (task)
543 {
544 /* Let the task build the response */
545 if (task->build(task,response) != SUCCESS)
546 {
547 response->destroy(response);
548 return;
549 }
550 }
551 else
552 {
553 notify_payload_t *notify;
554
555 notify = notify_payload_create_from_protocol_and_type(NOTIFY_V1,
556 PROTO_IKE, type);
557
558 if (data.ptr)
559 {
560 notify->set_notification_data(notify, data);
561 }
562
563 response->add_payload(response, (payload_t*)notify);
564 }
565
566 me = this->ike_sa->get_my_host(this->ike_sa);
567 if (me->is_anyaddr(me))
568 {
569 me = request->get_destination(request);
570 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
571 }
572 other = this->ike_sa->get_other_host(this->ike_sa);
573 if (other->is_anyaddr(other))
574 {
575 other = request->get_source(request);
576 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
577 }
578 response->set_source(response, me->clone(me));
579 response->set_destination(response, other->clone(other));
580 if (this->ike_sa->generate_message(this->ike_sa, response,
581 &packet) == SUCCESS)
582 {
583 charon->sender->send(charon->sender, packet);
584 }
585 response->destroy(response);
586 }
587
588 /**
589 * handle an incoming request message
590 */
591 static status_t process_request(private_task_manager_t *this,
592 message_t *message)
593 {
594 enumerator_t *enumerator;
595 task_t *task = NULL;
596 bool send_response = FALSE;
597 payload_t *payload;
598 notify_payload_t *notify;
599
600 if (this->passive_tasks->get_count(this->passive_tasks) == 0)
601 { /* create tasks depending on request type, if not already some queued */
602 switch (message->get_exchange_type(message))
603 {
604 case ID_PROT:
605 task = (task_t *)ike_vendor_v1_create(this->ike_sa, FALSE);
606 this->passive_tasks->insert_last(this->passive_tasks, task);
607 task = (task_t*)ike_cert_pre_v1_create(this->ike_sa, FALSE);
608 this->passive_tasks->insert_last(this->passive_tasks, task);
609 task = (task_t *)main_mode_create(this->ike_sa, FALSE);
610 this->passive_tasks->insert_last(this->passive_tasks, task);
611 task = (task_t*)ike_cert_post_v1_create(this->ike_sa, FALSE);
612 this->passive_tasks->insert_last(this->passive_tasks, task);
613 task = (task_t *)ike_natd_v1_create(this->ike_sa, FALSE);
614 this->passive_tasks->insert_last(this->passive_tasks, task);
615 break;
616 case AGGRESSIVE:
617 /* TODO-IKEv1: agressive mode */
618 return FAILED;
619 case QUICK_MODE:
620 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
621 {
622 DBG1(DBG_IKE, "received quick mode request for "
623 "unestablished IKE_SA, ignored");
624 return FAILED;
625 }
626 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
627 NULL, NULL);
628 this->passive_tasks->insert_last(this->passive_tasks, task);
629 break;
630 case INFORMATIONAL_V1:
631 enumerator = message->create_payload_enumerator(message);
632 while (enumerator->enumerate(enumerator, &payload))
633 {
634 switch (payload->get_type(payload))
635 {
636 case NOTIFY_V1:
637 {
638 notify = (notify_payload_t*)payload;
639 switch (notify->get_notify_type(notify))
640 {
641 /* TODO-IKEv1: Add notification types here as needed */
642 case INITIAL_CONTACT_IKEV1:
643 break;
644 default:
645 if(notify->get_notify_type(notify) < 16384)
646 {
647 DBG1(DBG_IKE, "Received %N error notification.", notify_type_names, notify->get_notify_type(notify));
648 enumerator->destroy(enumerator);
649 return FAILED;
650 }
651 break;
652 }
653 break;
654 }
655 case DELETE_V1:
656 {
657 delete_payload_t *delete;
658 delete = (delete_payload_t*)payload;
659
660 if (delete->get_protocol_id(delete) == PROTO_IKE)
661 {
662 task = (task_t*)ike_delete_create(this->ike_sa,
663 FALSE);
664 }
665 else
666 {
667 task = (task_t*)child_delete_create(this->ike_sa,
668 PROTO_NONE, 0);
669 }
670 break;
671 }
672 default:
673 break;
674 }
675 if (task)
676 {
677 this->passive_tasks->insert_last(this->passive_tasks, task);
678 }
679 }
680 enumerator->destroy(enumerator);
681 break;
682 case TRANSACTION:
683 if (this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
684 {
685 task = (task_t *)mode_config_create(this->ike_sa, FALSE);
686 }
687 else
688 {
689 task = (task_t *)xauth_create(this->ike_sa, FALSE);
690 }
691 this->passive_tasks->insert_last(this->passive_tasks, task);
692 break;
693 default:
694 return FAILED;
695 }
696 }
697 /* let the tasks process the message */
698 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
699 while (enumerator->enumerate(enumerator, (void*)&task))
700 {
701 switch (task->process(task, message))
702 {
703 case SUCCESS:
704 /* task completed, remove it */
705 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
706 task->destroy(task);
707 continue;
708 case NEED_MORE:
709 /* processed, but task needs at least another call to build() */
710 send_response = TRUE;
711 continue;
712 case ALREADY_DONE:
713 send_response = FALSE;
714 flush_queue(this, this->passive_tasks);
715 break;
716 case FAILED:
717 default:
718 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
719 /* FALL */
720 case DESTROY_ME:
721 /* critical failure, destroy IKE_SA */
722 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
723 enumerator->destroy(enumerator);
724 task->destroy(task);
725 return DESTROY_ME;
726 }
727 break;
728 }
729 enumerator->destroy(enumerator);
730
731 if (send_response)
732 {
733 if (build_response(this, message) != SUCCESS)
734 {
735 return DESTROY_ME;
736 }
737 }
738 if (this->passive_tasks->get_count(this->passive_tasks) == 0 &&
739 this->queued_tasks->get_count(this->queued_tasks) > 0)
740 {
741 /* passive tasks completed, check if an active task has been queued,
742 * such as XAUTH or modeconfig push */
743 return initiate(this);
744 }
745 return SUCCESS;
746 }
747
748 /**
749 * handle an incoming response message
750 */
751 static status_t process_response(private_task_manager_t *this,
752 message_t *message)
753 {
754 enumerator_t *enumerator;
755 task_t *task;
756
757 if (message->get_exchange_type(message) != this->initiating.type)
758 {
759 DBG1(DBG_IKE, "received %N response, but expected %N",
760 exchange_type_names, message->get_exchange_type(message),
761 exchange_type_names, this->initiating.type);
762 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
763 return DESTROY_ME;
764 }
765
766 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
767 while (enumerator->enumerate(enumerator, (void*)&task))
768 {
769 switch (task->process(task, message))
770 {
771 case SUCCESS:
772 /* task completed, remove it */
773 this->active_tasks->remove_at(this->active_tasks, enumerator);
774 task->destroy(task);
775 continue;
776 case NEED_MORE:
777 /* processed, but task needs another exchange */
778 continue;
779 case ALREADY_DONE:
780 flush_queue(this, this->active_tasks);
781 break;
782 case FAILED:
783 default:
784 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
785 /* FALL */
786 case DESTROY_ME:
787 /* critical failure, destroy IKE_SA */
788 this->active_tasks->remove_at(this->active_tasks, enumerator);
789 enumerator->destroy(enumerator);
790 task->destroy(task);
791 return DESTROY_ME;
792 }
793 break;
794 }
795 enumerator->destroy(enumerator);
796
797 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
798 this->initiating.packet->destroy(this->initiating.packet);
799 this->initiating.packet = NULL;
800
801 return initiate(this);
802 }
803
804 /**
805 * Parse the given message and verify that it is valid.
806 */
807 static status_t parse_message(private_task_manager_t *this, message_t *msg)
808 {
809 status_t status;
810
811 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
812
813 if (status != SUCCESS)
814 {
815 switch (status)
816 {
817 case NOT_SUPPORTED:
818 DBG1(DBG_IKE, "unsupported exchange type");
819 send_notify_response(this, msg,
820 INVALID_EXCHANGE_TYPE, chunk_empty, NULL);
821 break;
822 case PARSE_ERROR:
823 DBG1(DBG_IKE, "message parsing failed");
824 send_notify_response(this, msg,
825 PAYLOAD_MALFORMED, chunk_empty, NULL);
826 break;
827 case VERIFY_ERROR:
828 DBG1(DBG_IKE, "message verification failed");
829 send_notify_response(this, msg,
830 PAYLOAD_MALFORMED, chunk_empty, NULL);
831 break;
832 case FAILED:
833 DBG1(DBG_IKE, "integrity check failed");
834 send_notify_response(this, msg,
835 INVALID_HASH_INFORMATION, chunk_empty, NULL);
836 break;
837 case INVALID_STATE:
838 DBG1(DBG_IKE, "found encrypted message, but no keys available");
839 send_notify_response(this, msg,
840 PAYLOAD_MALFORMED, chunk_empty, NULL);
841 default:
842 break;
843 }
844 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
845 exchange_type_names, msg->get_exchange_type(msg),
846 msg->get_request(msg) ? "request" : "response",
847 msg->get_message_id(msg));
848
849 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED)
850 { /* invalid initiation attempt, close SA */
851 return DESTROY_ME;
852 }
853 }
854 return status;
855 }
856
857 METHOD(task_manager_t, process_message, status_t,
858 private_task_manager_t *this, message_t *msg)
859 {
860 u_int32_t hash, mid;
861 host_t *me, *other;
862 status_t status;
863
864 /* TODO-IKEv1: update hosts more selectively */
865 me = msg->get_destination(msg);
866 other = msg->get_source(msg);
867 mid = msg->get_message_id(msg);
868
869 if ((mid && mid == this->initiating.mid) ||
870 (this->initiating.mid == 0 &&
871 this->active_tasks->get_count(this->active_tasks)))
872 {
873 msg->set_request(msg, FALSE);
874 status = parse_message(this, msg);
875 if (status != SUCCESS)
876 {
877 return status;
878 }
879 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
880 time_monotonic(NULL));
881 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
882 charon->bus->message(charon->bus, msg, FALSE);
883 if (process_response(this, msg) != SUCCESS)
884 {
885 flush(this);
886 return DESTROY_ME;
887 }
888 }
889 else
890 {
891 hash = chunk_hash(msg->get_packet_data(msg));
892 if (hash == this->responding.hash && this->responding.packet)
893 {
894 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
895 "retransmitting response", mid);
896 charon->sender->send(charon->sender,
897 this->responding.packet->clone(this->responding.packet));
898 return SUCCESS;
899 }
900 msg->set_request(msg, TRUE);
901 status = parse_message(this, msg);
902 if (status != SUCCESS)
903 {
904 return status;
905 }
906 /* if this IKE_SA is virgin, we check for a config */
907 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
908 {
909 ike_sa_id_t *ike_sa_id;
910 ike_cfg_t *ike_cfg;
911 job_t *job;
912 ike_cfg = charon->backends->get_ike_cfg(charon->backends, me, other);
913 if (ike_cfg == NULL)
914 {
915 /* no config found for these hosts, destroy */
916 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
917 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
918 send_notify_response(this, msg,
919 NO_PROPOSAL_CHOSEN, chunk_empty, NULL);
920 return DESTROY_ME;
921 }
922 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
923 ike_cfg->destroy(ike_cfg);
924 /* add a timeout if peer does not establish it completely */
925 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
926 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
927 lib->scheduler->schedule_job(lib->scheduler, job,
928 lib->settings->get_int(lib->settings,
929 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT));
930 }
931 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
932 time_monotonic(NULL));
933 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
934 charon->bus->message(charon->bus, msg, TRUE);
935 if (process_request(this, msg) != SUCCESS)
936 {
937 flush(this);
938 return DESTROY_ME;
939 }
940
941 this->responding.mid = mid;
942 this->responding.hash = hash;
943 }
944 return SUCCESS;
945 }
946
947 METHOD(task_manager_t, queue_task, void,
948 private_task_manager_t *this, task_t *task)
949 {
950 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
951 this->queued_tasks->insert_last(this->queued_tasks, task);
952 }
953
954 METHOD(task_manager_t, adopt_tasks, void,
955 private_task_manager_t *this, task_manager_t *other_public)
956 {
957 private_task_manager_t *other = (private_task_manager_t*)other_public;
958 task_t *task;
959
960 /* move queued tasks from other to this */
961 while (other->queued_tasks->remove_last(other->queued_tasks,
962 (void**)&task) == SUCCESS)
963 {
964 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
965 task->migrate(task, this->ike_sa);
966 this->queued_tasks->insert_first(this->queued_tasks, task);
967 }
968 }
969
970 METHOD(task_manager_t, busy, bool,
971 private_task_manager_t *this)
972 {
973 return (this->active_tasks->get_count(this->active_tasks) > 0);
974 }
975
976 METHOD(task_manager_t, incr_mid, void,
977 private_task_manager_t *this, bool initiate)
978 {
979 }
980
981 METHOD(task_manager_t, reset, void,
982 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
983 {
984 }
985
986 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
987 private_task_manager_t *this, task_queue_t queue)
988 {
989 switch (queue)
990 {
991 case TASK_QUEUE_ACTIVE:
992 return this->active_tasks->create_enumerator(this->active_tasks);
993 case TASK_QUEUE_PASSIVE:
994 return this->passive_tasks->create_enumerator(this->passive_tasks);
995 case TASK_QUEUE_QUEUED:
996 return this->queued_tasks->create_enumerator(this->queued_tasks);
997 default:
998 return enumerator_create_empty();
999 }
1000 }
1001
1002 METHOD(task_manager_t, destroy, void,
1003 private_task_manager_t *this)
1004 {
1005 flush(this);
1006
1007 this->active_tasks->destroy(this->active_tasks);
1008 this->queued_tasks->destroy(this->queued_tasks);
1009 this->passive_tasks->destroy(this->passive_tasks);
1010
1011 DESTROY_IF(this->responding.packet);
1012 DESTROY_IF(this->initiating.packet);
1013 DESTROY_IF(this->rng);
1014 free(this);
1015 }
1016
1017 /*
1018 * see header file
1019 */
1020 task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
1021 {
1022 private_task_manager_t *this;
1023
1024 INIT(this,
1025 .public = {
1026 .task_manager = {
1027 .process_message = _process_message,
1028 .queue_task = _queue_task,
1029 .initiate = _initiate,
1030 .retransmit = _retransmit,
1031 .incr_mid = _incr_mid,
1032 .reset = _reset,
1033 .adopt_tasks = _adopt_tasks,
1034 .busy = _busy,
1035 .create_task_enumerator = _create_task_enumerator,
1036 .destroy = _destroy,
1037 },
1038 },
1039 .ike_sa = ike_sa,
1040 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
1041 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
1042 .queued_tasks = linked_list_create(),
1043 .active_tasks = linked_list_create(),
1044 .passive_tasks = linked_list_create(),
1045 .retransmit_tries = lib->settings->get_int(lib->settings,
1046 "charon.retransmit_tries", RETRANSMIT_TRIES),
1047 .retransmit_timeout = lib->settings->get_double(lib->settings,
1048 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT),
1049 .retransmit_base = lib->settings->get_double(lib->settings,
1050 "charon.retransmit_base", RETRANSMIT_BASE),
1051 );
1052
1053 return &this->public;
1054 }