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