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