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