89c664c8f926e2e57c145b41501dc7f4ffe089d9
[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/informational.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 if (activate_task(this, TASK_INFORMATIONAL))
297 {
298 exchange = INFORMATIONAL_V1;
299 new_mid = TRUE;
300 }
301 break;
302 case IKE_ESTABLISHED:
303 if (activate_task(this, TASK_MODE_CONFIG))
304 {
305 exchange = TRANSACTION;
306 new_mid = TRUE;
307 break;
308 }
309 if (activate_task(this, TASK_QUICK_MODE))
310 {
311 exchange = QUICK_MODE;
312 new_mid = TRUE;
313 break;
314 }
315 if (activate_task(this, TASK_INFORMATIONAL))
316 {
317 exchange = INFORMATIONAL_V1;
318 new_mid = TRUE;
319 }
320 break;
321 default:
322 break;
323 }
324 }
325 else
326 {
327 DBG2(DBG_IKE, "reinitiating already active tasks");
328 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
329 while (enumerator->enumerate(enumerator, (void**)&task))
330 {
331 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
332 switch (task->get_type(task))
333 {
334 case TASK_MAIN_MODE:
335 exchange = ID_PROT;
336 break;
337 case TASK_QUICK_MODE:
338 exchange = QUICK_MODE;
339 break;
340 case TASK_XAUTH:
341 exchange = TRANSACTION;
342 new_mid = TRUE;
343 break;
344 default:
345 continue;
346 }
347 break;
348 }
349 enumerator->destroy(enumerator);
350 }
351
352 if (exchange == EXCHANGE_TYPE_UNDEFINED)
353 {
354 DBG2(DBG_IKE, "nothing to initiate");
355 /* nothing to do yet... */
356 return SUCCESS;
357 }
358
359 me = this->ike_sa->get_my_host(this->ike_sa);
360 other = this->ike_sa->get_other_host(this->ike_sa);
361
362 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
363 if (new_mid)
364 {
365 this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
366 (void*)&this->initiating.mid);
367 }
368 message->set_message_id(message, this->initiating.mid);
369 message->set_source(message, me->clone(me));
370 message->set_destination(message, other->clone(other));
371 message->set_exchange_type(message, exchange);
372 this->initiating.type = exchange;
373 this->initiating.retransmitted = 0;
374
375 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
376 while (enumerator->enumerate(enumerator, (void*)&task))
377 {
378 switch (task->build(task, message))
379 {
380 case SUCCESS:
381 /* task completed, remove it */
382 this->active_tasks->remove_at(this->active_tasks, enumerator);
383 task->destroy(task);
384 continue;
385 case NEED_MORE:
386 expect_response = TRUE;
387 /* processed, but task needs another exchange */
388 continue;
389 case ALREADY_DONE:
390 flush_queue(this, this->active_tasks);
391 break;
392 case FAILED:
393 default:
394 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
395 {
396 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
397 }
398 /* FALL */
399 case DESTROY_ME:
400 /* critical failure, destroy IKE_SA */
401 enumerator->destroy(enumerator);
402 message->destroy(message);
403 flush(this);
404 return DESTROY_ME;
405 }
406 break;
407 }
408 enumerator->destroy(enumerator);
409
410 /* update exchange type if a task changed it */
411 this->initiating.type = message->get_exchange_type(message);
412 this->initiating.seqnr++;
413
414 status = this->ike_sa->generate_message(this->ike_sa, message,
415 &this->initiating.packet);
416 if (status != SUCCESS)
417 {
418 /* message generation failed. There is nothing more to do than to
419 * close the SA */
420 message->destroy(message);
421 flush(this);
422 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
423 return DESTROY_ME;
424 }
425 message->destroy(message);
426
427 if (expect_response)
428 {
429 return retransmit(this, this->initiating.seqnr);
430 }
431 charon->sender->send(charon->sender,
432 this->initiating.packet->clone(this->initiating.packet));
433 this->initiating.packet->destroy(this->initiating.packet);
434 this->initiating.packet = NULL;
435
436 /* close after sending an INFORMATIONAL error but not yet established */
437 if (this->initiating.type == INFORMATIONAL_V1 &&
438 this->ike_sa->get_state(this->ike_sa) == IKE_CONNECTING)
439 {
440 return FAILED;
441 }
442 return SUCCESS;
443 }
444
445 /**
446 * handle exchange collisions
447 */
448 static bool handle_collisions(private_task_manager_t *this, task_t *task)
449 {
450 return FALSE;
451 }
452
453 /**
454 * build a response depending on the "passive" task list
455 */
456 static status_t build_response(private_task_manager_t *this, message_t *request)
457 {
458 enumerator_t *enumerator;
459 task_t *task;
460 message_t *message;
461 host_t *me, *other;
462 bool delete = FALSE;
463 status_t status;
464
465 me = request->get_destination(request);
466 other = request->get_source(request);
467
468 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
469 message->set_exchange_type(message, request->get_exchange_type(request));
470 /* send response along the path the request came in */
471 message->set_source(message, me->clone(me));
472 message->set_destination(message, other->clone(other));
473 message->set_message_id(message, request->get_message_id(request));
474 message->set_request(message, FALSE);
475
476 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
477 while (enumerator->enumerate(enumerator, (void*)&task))
478 {
479 switch (task->build(task, message))
480 {
481 case SUCCESS:
482 /* task completed, remove it */
483 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
484 if (!handle_collisions(this, task))
485 {
486 task->destroy(task);
487 }
488 continue;
489 case NEED_MORE:
490 /* processed, but task needs another exchange */
491 if (handle_collisions(this, task))
492 {
493 this->passive_tasks->remove_at(this->passive_tasks,
494 enumerator);
495 }
496 continue;
497 case ALREADY_DONE:
498 flush_queue(this, this->passive_tasks);
499 break;
500 case FAILED:
501 default:
502 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
503 /* FALL */
504 case DESTROY_ME:
505 /* destroy IKE_SA, but SEND response first */
506 delete = TRUE;
507 break;
508 }
509 break;
510 }
511 enumerator->destroy(enumerator);
512
513 /* message complete, send it */
514 DESTROY_IF(this->responding.packet);
515 this->responding.packet = NULL;
516 status = this->ike_sa->generate_message(this->ike_sa, message,
517 &this->responding.packet);
518 message->destroy(message);
519 if (status != SUCCESS)
520 {
521 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
522 return DESTROY_ME;
523 }
524
525 charon->sender->send(charon->sender,
526 this->responding.packet->clone(this->responding.packet));
527 if (delete)
528 {
529 return DESTROY_ME;
530 }
531 return SUCCESS;
532 }
533
534 /**
535 * Send a notify in a separate INFORMATIONAL exchange back to the sender.
536 * The notify protocol_id is set to ISAKMP
537 */
538 static void send_notify(private_task_manager_t *this, message_t *request,
539 notify_type_t type)
540 {
541 message_t *response;
542 packet_t *packet;
543 host_t *me, *other;
544 u_int32_t mid;
545
546 if (request && request->get_exchange_type(request) == INFORMATIONAL_V1)
547 { /* don't respond to INFORMATIONAL requests to avoid a notify war */
548 DBG1(DBG_IKE, "ignore malformed INFORMATIONAL request");
549 return;
550 }
551
552 response = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
553 response->set_exchange_type(response, INFORMATIONAL_V1);
554 response->set_request(response, TRUE);
555 this->rng->get_bytes(this->rng, sizeof(mid), (void*)&mid);
556 response->set_message_id(response, mid);
557 response->add_payload(response, (payload_t*)
558 notify_payload_create_from_protocol_and_type(NOTIFY_V1,
559 PROTO_IKE, type));
560
561 me = this->ike_sa->get_my_host(this->ike_sa);
562 if (me->is_anyaddr(me))
563 {
564 me = request->get_destination(request);
565 this->ike_sa->set_my_host(this->ike_sa, me->clone(me));
566 }
567 other = this->ike_sa->get_other_host(this->ike_sa);
568 if (other->is_anyaddr(other))
569 {
570 other = request->get_source(request);
571 this->ike_sa->set_other_host(this->ike_sa, other->clone(other));
572 }
573 response->set_source(response, me->clone(me));
574 response->set_destination(response, other->clone(other));
575 if (this->ike_sa->generate_message(this->ike_sa, response,
576 &packet) == SUCCESS)
577 {
578 charon->sender->send(charon->sender, packet);
579 }
580 response->destroy(response);
581 }
582
583 /**
584 * handle an incoming request message
585 */
586 static status_t process_request(private_task_manager_t *this,
587 message_t *message)
588 {
589 enumerator_t *enumerator;
590 task_t *task = NULL;
591 bool send_response = FALSE;
592
593 if (this->passive_tasks->get_count(this->passive_tasks) == 0)
594 { /* create tasks depending on request type, if not already some queued */
595 switch (message->get_exchange_type(message))
596 {
597 case ID_PROT:
598 task = (task_t *)ike_vendor_v1_create(this->ike_sa, FALSE);
599 this->passive_tasks->insert_last(this->passive_tasks, task);
600 task = (task_t*)ike_cert_pre_v1_create(this->ike_sa, FALSE);
601 this->passive_tasks->insert_last(this->passive_tasks, task);
602 task = (task_t *)main_mode_create(this->ike_sa, FALSE);
603 this->passive_tasks->insert_last(this->passive_tasks, task);
604 task = (task_t*)ike_cert_post_v1_create(this->ike_sa, FALSE);
605 this->passive_tasks->insert_last(this->passive_tasks, task);
606 task = (task_t *)ike_natd_v1_create(this->ike_sa, FALSE);
607 this->passive_tasks->insert_last(this->passive_tasks, task);
608 break;
609 case AGGRESSIVE:
610 /* TODO-IKEv1: agressive mode */
611 return FAILED;
612 case QUICK_MODE:
613 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
614 {
615 DBG1(DBG_IKE, "received quick mode request for "
616 "unestablished IKE_SA, ignored");
617 return FAILED;
618 }
619 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
620 NULL, NULL);
621 this->passive_tasks->insert_last(this->passive_tasks, task);
622 break;
623 case INFORMATIONAL_V1:
624 task = (task_t *)informational_create(this->ike_sa, NULL);
625 this->passive_tasks->insert_last(this->passive_tasks, task);
626 break;
627 case TRANSACTION:
628 if (this->ike_sa->get_state(this->ike_sa) == IKE_ESTABLISHED)
629 {
630 task = (task_t *)mode_config_create(this->ike_sa, FALSE);
631 }
632 else
633 {
634 task = (task_t *)xauth_create(this->ike_sa, FALSE);
635 }
636 this->passive_tasks->insert_last(this->passive_tasks, task);
637 break;
638 default:
639 return FAILED;
640 }
641 }
642 /* let the tasks process the message */
643 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
644 while (enumerator->enumerate(enumerator, (void*)&task))
645 {
646 switch (task->process(task, message))
647 {
648 case SUCCESS:
649 /* task completed, remove it */
650 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
651 task->destroy(task);
652 continue;
653 case NEED_MORE:
654 /* processed, but task needs at least another call to build() */
655 send_response = TRUE;
656 continue;
657 case ALREADY_DONE:
658 send_response = FALSE;
659 flush_queue(this, this->passive_tasks);
660 break;
661 case FAILED:
662 default:
663 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
664 /* FALL */
665 case DESTROY_ME:
666 /* critical failure, destroy IKE_SA */
667 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
668 enumerator->destroy(enumerator);
669 task->destroy(task);
670 return DESTROY_ME;
671 }
672 break;
673 }
674 enumerator->destroy(enumerator);
675
676 if (send_response)
677 {
678 if (build_response(this, message) != SUCCESS)
679 {
680 return DESTROY_ME;
681 }
682 }
683 if (this->passive_tasks->get_count(this->passive_tasks) == 0 &&
684 this->queued_tasks->get_count(this->queued_tasks) > 0)
685 {
686 /* passive tasks completed, check if an active task has been queued,
687 * such as XAUTH or modeconfig push */
688 return initiate(this);
689 }
690 return SUCCESS;
691 }
692
693 /**
694 * handle an incoming response message
695 */
696 static status_t process_response(private_task_manager_t *this,
697 message_t *message)
698 {
699 enumerator_t *enumerator;
700 task_t *task;
701
702 if (message->get_exchange_type(message) != this->initiating.type)
703 {
704 DBG1(DBG_IKE, "received %N response, but expected %N",
705 exchange_type_names, message->get_exchange_type(message),
706 exchange_type_names, this->initiating.type);
707 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
708 return DESTROY_ME;
709 }
710
711 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
712 while (enumerator->enumerate(enumerator, (void*)&task))
713 {
714 switch (task->process(task, message))
715 {
716 case SUCCESS:
717 /* task completed, remove it */
718 this->active_tasks->remove_at(this->active_tasks, enumerator);
719 task->destroy(task);
720 continue;
721 case NEED_MORE:
722 /* processed, but task needs another exchange */
723 continue;
724 case ALREADY_DONE:
725 flush_queue(this, this->active_tasks);
726 break;
727 case FAILED:
728 default:
729 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
730 /* FALL */
731 case DESTROY_ME:
732 /* critical failure, destroy IKE_SA */
733 this->active_tasks->remove_at(this->active_tasks, enumerator);
734 enumerator->destroy(enumerator);
735 task->destroy(task);
736 return DESTROY_ME;
737 }
738 break;
739 }
740 enumerator->destroy(enumerator);
741
742 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
743 this->initiating.packet->destroy(this->initiating.packet);
744 this->initiating.packet = NULL;
745
746 return initiate(this);
747 }
748
749 /**
750 * Parse the given message and verify that it is valid.
751 */
752 static status_t parse_message(private_task_manager_t *this, message_t *msg)
753 {
754 status_t status;
755
756 status = msg->parse_body(msg, this->ike_sa->get_keymat(this->ike_sa));
757
758 if (status != SUCCESS)
759 {
760 switch (status)
761 {
762 case NOT_SUPPORTED:
763 DBG1(DBG_IKE, "unsupported exchange type");
764 send_notify(this, msg, INVALID_EXCHANGE_TYPE);
765 break;
766 case PARSE_ERROR:
767 DBG1(DBG_IKE, "message parsing failed");
768 send_notify(this, msg, PAYLOAD_MALFORMED);
769 break;
770 case VERIFY_ERROR:
771 DBG1(DBG_IKE, "message verification failed");
772 send_notify(this, msg, PAYLOAD_MALFORMED);
773 break;
774 case FAILED:
775 DBG1(DBG_IKE, "integrity check failed");
776 send_notify(this, msg, INVALID_HASH_INFORMATION);
777 break;
778 case INVALID_STATE:
779 DBG1(DBG_IKE, "found encrypted message, but no keys available");
780 send_notify(this, msg, PAYLOAD_MALFORMED);
781 default:
782 break;
783 }
784 DBG1(DBG_IKE, "%N %s with message ID %d processing failed",
785 exchange_type_names, msg->get_exchange_type(msg),
786 msg->get_request(msg) ? "request" : "response",
787 msg->get_message_id(msg));
788
789 if (this->ike_sa->get_state(this->ike_sa) == IKE_CREATED)
790 { /* invalid initiation attempt, close SA */
791 return DESTROY_ME;
792 }
793 }
794 return status;
795 }
796
797 METHOD(task_manager_t, process_message, status_t,
798 private_task_manager_t *this, message_t *msg)
799 {
800 u_int32_t hash, mid;
801 host_t *me, *other;
802 status_t status;
803
804 /* TODO-IKEv1: update hosts more selectively */
805 me = msg->get_destination(msg);
806 other = msg->get_source(msg);
807 mid = msg->get_message_id(msg);
808
809 if ((mid && mid == this->initiating.mid) ||
810 (this->initiating.mid == 0 &&
811 msg->get_exchange_type(msg) == this->initiating.type &&
812 this->active_tasks->get_count(this->active_tasks)))
813 {
814 msg->set_request(msg, FALSE);
815 status = parse_message(this, msg);
816 if (status != SUCCESS)
817 {
818 return status;
819 }
820 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
821 time_monotonic(NULL));
822 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
823 charon->bus->message(charon->bus, msg, FALSE);
824 if (process_response(this, msg) != SUCCESS)
825 {
826 flush(this);
827 return DESTROY_ME;
828 }
829 }
830 else
831 {
832 hash = chunk_hash(msg->get_packet_data(msg));
833 if (hash == this->responding.hash && this->responding.packet)
834 {
835 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
836 "retransmitting response", mid);
837 charon->sender->send(charon->sender,
838 this->responding.packet->clone(this->responding.packet));
839 return SUCCESS;
840 }
841 msg->set_request(msg, TRUE);
842 status = parse_message(this, msg);
843 if (status != SUCCESS)
844 {
845 return status;
846 }
847 /* if this IKE_SA is virgin, we check for a config */
848 if (this->ike_sa->get_ike_cfg(this->ike_sa) == NULL)
849 {
850 ike_sa_id_t *ike_sa_id;
851 ike_cfg_t *ike_cfg;
852 job_t *job;
853 ike_cfg = charon->backends->get_ike_cfg(charon->backends, me, other);
854 if (ike_cfg == NULL)
855 {
856 /* no config found for these hosts, destroy */
857 DBG1(DBG_IKE, "no IKE config found for %H...%H, sending %N",
858 me, other, notify_type_names, NO_PROPOSAL_CHOSEN);
859 send_notify(this, msg, NO_PROPOSAL_CHOSEN);
860 return DESTROY_ME;
861 }
862 this->ike_sa->set_ike_cfg(this->ike_sa, ike_cfg);
863 ike_cfg->destroy(ike_cfg);
864 /* add a timeout if peer does not establish it completely */
865 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
866 job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
867 lib->scheduler->schedule_job(lib->scheduler, job,
868 lib->settings->get_int(lib->settings,
869 "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT));
870 }
871 this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
872 time_monotonic(NULL));
873 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
874 charon->bus->message(charon->bus, msg, TRUE);
875 if (process_request(this, msg) != SUCCESS)
876 {
877 flush(this);
878 return DESTROY_ME;
879 }
880
881 this->responding.mid = mid;
882 this->responding.hash = hash;
883 }
884 return SUCCESS;
885 }
886
887 METHOD(task_manager_t, queue_task, void,
888 private_task_manager_t *this, task_t *task)
889 {
890 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
891 this->queued_tasks->insert_last(this->queued_tasks, task);
892 }
893
894 METHOD(task_manager_t, adopt_tasks, void,
895 private_task_manager_t *this, task_manager_t *other_public)
896 {
897 private_task_manager_t *other = (private_task_manager_t*)other_public;
898 task_t *task;
899
900 /* move queued tasks from other to this */
901 while (other->queued_tasks->remove_last(other->queued_tasks,
902 (void**)&task) == SUCCESS)
903 {
904 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
905 task->migrate(task, this->ike_sa);
906 this->queued_tasks->insert_first(this->queued_tasks, task);
907 }
908 }
909
910 METHOD(task_manager_t, busy, bool,
911 private_task_manager_t *this)
912 {
913 return (this->active_tasks->get_count(this->active_tasks) > 0);
914 }
915
916 METHOD(task_manager_t, incr_mid, void,
917 private_task_manager_t *this, bool initiate)
918 {
919 }
920
921 METHOD(task_manager_t, reset, void,
922 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
923 {
924 }
925
926 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
927 private_task_manager_t *this, task_queue_t queue)
928 {
929 switch (queue)
930 {
931 case TASK_QUEUE_ACTIVE:
932 return this->active_tasks->create_enumerator(this->active_tasks);
933 case TASK_QUEUE_PASSIVE:
934 return this->passive_tasks->create_enumerator(this->passive_tasks);
935 case TASK_QUEUE_QUEUED:
936 return this->queued_tasks->create_enumerator(this->queued_tasks);
937 default:
938 return enumerator_create_empty();
939 }
940 }
941
942 METHOD(task_manager_t, destroy, void,
943 private_task_manager_t *this)
944 {
945 flush(this);
946
947 this->active_tasks->destroy(this->active_tasks);
948 this->queued_tasks->destroy(this->queued_tasks);
949 this->passive_tasks->destroy(this->passive_tasks);
950
951 DESTROY_IF(this->responding.packet);
952 DESTROY_IF(this->initiating.packet);
953 DESTROY_IF(this->rng);
954 free(this);
955 }
956
957 /*
958 * see header file
959 */
960 task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
961 {
962 private_task_manager_t *this;
963
964 INIT(this,
965 .public = {
966 .task_manager = {
967 .process_message = _process_message,
968 .queue_task = _queue_task,
969 .initiate = _initiate,
970 .retransmit = _retransmit,
971 .incr_mid = _incr_mid,
972 .reset = _reset,
973 .adopt_tasks = _adopt_tasks,
974 .busy = _busy,
975 .create_task_enumerator = _create_task_enumerator,
976 .destroy = _destroy,
977 },
978 },
979 .ike_sa = ike_sa,
980 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
981 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
982 .queued_tasks = linked_list_create(),
983 .active_tasks = linked_list_create(),
984 .passive_tasks = linked_list_create(),
985 .retransmit_tries = lib->settings->get_int(lib->settings,
986 "charon.retransmit_tries", RETRANSMIT_TRIES),
987 .retransmit_timeout = lib->settings->get_double(lib->settings,
988 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT),
989 .retransmit_base = lib->settings->get_double(lib->settings,
990 "charon.retransmit_base", RETRANSMIT_BASE),
991 );
992
993 return &this->public;
994 }