Fixed task_manager_v1 compiler warnings
[strongswan.git] / src / libcharon / sa / task_manager_v1.c
1 /*
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2007-2010 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/main_mode.h>
23 #include <sa/tasks/quick_mode.h>
24 #include <processing/jobs/retransmit_job.h>
25
26 typedef struct exchange_t exchange_t;
27
28 /**
29 * An exchange in the air, used do detect and handle retransmission
30 */
31 struct exchange_t {
32
33 /**
34 * Message ID used for this transaction
35 */
36 u_int32_t mid;
37
38 /**
39 * generated packet for retransmission
40 */
41 packet_t *packet;
42 };
43
44 typedef struct private_task_manager_t private_task_manager_t;
45
46 /**
47 * private data of the task manager
48 */
49 struct private_task_manager_t {
50
51 /**
52 * public functions
53 */
54 task_manager_v1_t public;
55
56 /**
57 * associated IKE_SA we are serving
58 */
59 ike_sa_t *ike_sa;
60
61 /**
62 * RNG to create message IDs
63 */
64 rng_t *rng;
65
66 /**
67 * Exchange we are currently handling as responder
68 */
69 struct {
70 /**
71 * Message ID of the exchange
72 */
73 u_int32_t mid;
74
75 /**
76 * Hash of a previously received message
77 */
78 u_int32_t hash;
79
80 /**
81 * packet for retransmission
82 */
83 packet_t *packet;
84
85 } responding;
86
87 /**
88 * Exchange we are currently handling as initiator
89 */
90 struct {
91 /**
92 * Message ID of the exchange
93 */
94 u_int32_t mid;
95
96 /**
97 * Hash of a previously received message
98 */
99 u_int32_t hash;
100
101 /**
102 * how many times we have retransmitted so far
103 */
104 u_int retransmitted;
105
106 /**
107 * packet for retransmission
108 */
109 packet_t *packet;
110
111 /**
112 * type of the initated exchange
113 */
114 exchange_type_t type;
115
116 } initiating;
117
118 /**
119 * List of queued tasks not yet in action
120 */
121 linked_list_t *queued_tasks;
122
123 /**
124 * List of active tasks, initiated by ourselve
125 */
126 linked_list_t *active_tasks;
127
128 /**
129 * List of tasks initiated by peer
130 */
131 linked_list_t *passive_tasks;
132
133 /**
134 * Number of times we retransmit messages before giving up
135 */
136 u_int retransmit_tries;
137
138 /**
139 * Retransmission timeout
140 */
141 double retransmit_timeout;
142
143 /**
144 * Base to calculate retransmission timeout
145 */
146 double retransmit_base;
147 };
148
149 /**
150 * flush all tasks in the task manager
151 */
152 static void flush(private_task_manager_t *this)
153 {
154 this->queued_tasks->destroy_offset(this->queued_tasks,
155 offsetof(task_t, destroy));
156 this->queued_tasks = linked_list_create();
157 this->passive_tasks->destroy_offset(this->passive_tasks,
158 offsetof(task_t, destroy));
159 this->passive_tasks = linked_list_create();
160 this->active_tasks->destroy_offset(this->active_tasks,
161 offsetof(task_t, destroy));
162 this->active_tasks = linked_list_create();
163 }
164
165 /**
166 * move a task of a specific type from the queue to the active list
167 */
168 static bool activate_task(private_task_manager_t *this, task_type_t type)
169 {
170 enumerator_t *enumerator;
171 task_t *task;
172 bool found = FALSE;
173
174 enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
175 while (enumerator->enumerate(enumerator, (void**)&task))
176 {
177 if (task->get_type(task) == type)
178 {
179 DBG2(DBG_IKE, " activating %N task", task_type_names, type);
180 this->queued_tasks->remove_at(this->queued_tasks, enumerator);
181 this->active_tasks->insert_last(this->active_tasks, task);
182 found = TRUE;
183 break;
184 }
185 }
186 enumerator->destroy(enumerator);
187 return found;
188 }
189
190 METHOD(task_manager_t, retransmit, status_t,
191 private_task_manager_t *this, u_int32_t message_id)
192 {
193 if (message_id == this->initiating.mid)
194 {
195 u_int32_t timeout;
196 packet_t *packet;
197 job_t *job;
198
199 if (this->initiating.retransmitted <= this->retransmit_tries)
200 {
201 timeout = (u_int32_t)(this->retransmit_timeout * 1000.0 *
202 pow(this->retransmit_base, this->initiating.retransmitted));
203 }
204 else
205 {
206 DBG1(DBG_IKE, "giving up after %d retransmits",
207 this->initiating.retransmitted - 1);
208 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
209 {
210 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
211 }
212 return DESTROY_ME;
213 }
214
215 if (this->initiating.retransmitted)
216 {
217 DBG1(DBG_IKE, "retransmit %d of request with message ID %d",
218 this->initiating.retransmitted, message_id);
219 }
220 packet = this->initiating.packet->clone(this->initiating.packet);
221 charon->sender->send(charon->sender, packet);
222
223 this->initiating.retransmitted++;
224 job = (job_t*)retransmit_job_create(this->initiating.mid,
225 this->ike_sa->get_id(this->ike_sa));
226 lib->scheduler->schedule_job_ms(lib->scheduler, job, timeout);
227 }
228 return SUCCESS;
229 }
230
231 METHOD(task_manager_t, initiate, status_t,
232 private_task_manager_t *this)
233 {
234 enumerator_t *enumerator;
235 task_t *task;
236 message_t *message;
237 host_t *me, *other;
238 status_t status;
239 exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED;
240 bool new_mid = FALSE;
241
242 if (!this->rng)
243 {
244 DBG1(DBG_IKE, "no RNG supported");
245 return FAILED;
246 }
247
248 if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
249 {
250 DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
251 exchange_type_names, this->initiating.type);
252 /* do not initiate if we already have a message in the air */
253 return SUCCESS;
254 }
255
256 if (this->active_tasks->get_count(this->active_tasks) == 0)
257 {
258 DBG2(DBG_IKE, "activating new tasks");
259 switch (this->ike_sa->get_state(this->ike_sa))
260 {
261 case IKE_CREATED:
262 if (activate_task(this, TASK_MAIN_MODE))
263 {
264 exchange = ID_PROT;
265 }
266 break;
267 case IKE_ESTABLISHED:
268 if (activate_task(this, TASK_QUICK_MODE))
269 {
270 exchange = QUICK_MODE;
271 new_mid = TRUE;
272 break;
273 }
274 if (activate_task(this, TASK_XAUTH_REQUEST))
275 {
276 exchange = TRANSACTION;
277 new_mid = TRUE;
278 break;
279 }
280 break;
281 default:
282 break;
283 }
284 }
285 else
286 {
287 DBG2(DBG_IKE, "reinitiating already active tasks");
288 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
289 while (enumerator->enumerate(enumerator, (void**)&task))
290 {
291 DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task));
292 switch (task->get_type(task))
293 {
294 case TASK_MAIN_MODE:
295 exchange = ID_PROT;
296 break;
297 case TASK_QUICK_MODE:
298 exchange = QUICK_MODE;
299 break;
300 default:
301 continue;
302 }
303 break;
304 }
305 enumerator->destroy(enumerator);
306 }
307
308 if (exchange == EXCHANGE_TYPE_UNDEFINED)
309 {
310 DBG2(DBG_IKE, "nothing to initiate");
311 /* nothing to do yet... */
312 return SUCCESS;
313 }
314
315 me = this->ike_sa->get_my_host(this->ike_sa);
316 other = this->ike_sa->get_other_host(this->ike_sa);
317
318 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
319 if (new_mid)
320 {
321 this->rng->get_bytes(this->rng, sizeof(this->initiating.mid),
322 (void*)&this->initiating.mid);
323 }
324 message->set_message_id(message, this->initiating.mid);
325 message->set_source(message, me->clone(me));
326 message->set_destination(message, other->clone(other));
327 message->set_exchange_type(message, exchange);
328 this->initiating.type = exchange;
329 this->initiating.retransmitted = 0;
330
331 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
332 while (enumerator->enumerate(enumerator, (void*)&task))
333 {
334 switch (task->build(task, message))
335 {
336 case SUCCESS:
337 /* task completed, remove it */
338 this->active_tasks->remove_at(this->active_tasks, enumerator);
339 task->destroy(task);
340 break;
341 case NEED_MORE:
342 /* processed, but task needs another exchange */
343 break;
344 case FAILED:
345 default:
346 if (this->ike_sa->get_state(this->ike_sa) != IKE_CONNECTING)
347 {
348 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
349 }
350 /* FALL */
351 case DESTROY_ME:
352 /* critical failure, destroy IKE_SA */
353 enumerator->destroy(enumerator);
354 message->destroy(message);
355 flush(this);
356 return DESTROY_ME;
357 }
358 }
359 enumerator->destroy(enumerator);
360
361 /* update exchange type if a task changed it */
362 this->initiating.type = message->get_exchange_type(message);
363
364 status = this->ike_sa->generate_message(this->ike_sa, message,
365 &this->initiating.packet);
366 if (status != SUCCESS)
367 {
368 /* message generation failed. There is nothing more to do than to
369 * close the SA */
370 message->destroy(message);
371 flush(this);
372 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
373 return DESTROY_ME;
374 }
375 message->destroy(message);
376
377 charon->sender->send(charon->sender,
378 this->initiating.packet->clone(this->initiating.packet));
379
380 return SUCCESS;
381 }
382
383 /**
384 * handle exchange collisions
385 */
386 static bool handle_collisions(private_task_manager_t *this, task_t *task)
387 {
388 return FALSE;
389 }
390
391 /**
392 * build a response depending on the "passive" task list
393 */
394 static status_t build_response(private_task_manager_t *this, message_t *request)
395 {
396 enumerator_t *enumerator;
397 task_t *task;
398 message_t *message;
399 host_t *me, *other;
400 bool delete = FALSE;
401 status_t status;
402
403 me = request->get_destination(request);
404 other = request->get_source(request);
405
406 message = message_create(IKEV1_MAJOR_VERSION, IKEV1_MINOR_VERSION);
407 message->set_exchange_type(message, request->get_exchange_type(request));
408 /* send response along the path the request came in */
409 message->set_source(message, me->clone(me));
410 message->set_destination(message, other->clone(other));
411 message->set_message_id(message, request->get_message_id(request));
412 message->set_request(message, FALSE);
413
414 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
415 while (enumerator->enumerate(enumerator, (void*)&task))
416 {
417 switch (task->build(task, message))
418 {
419 case SUCCESS:
420 /* task completed, remove it */
421 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
422 if (!handle_collisions(this, task))
423 {
424 task->destroy(task);
425 }
426 break;
427 case NEED_MORE:
428 /* processed, but task needs another exchange */
429 if (handle_collisions(this, task))
430 {
431 this->passive_tasks->remove_at(this->passive_tasks,
432 enumerator);
433 }
434 break;
435 case FAILED:
436 default:
437 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
438 /* FALL */
439 case DESTROY_ME:
440 /* destroy IKE_SA, but SEND response first */
441 delete = TRUE;
442 break;
443 }
444 if (delete)
445 {
446 break;
447 }
448 }
449 enumerator->destroy(enumerator);
450
451 /* message complete, send it */
452 DESTROY_IF(this->responding.packet);
453 this->responding.packet = NULL;
454 status = this->ike_sa->generate_message(this->ike_sa, message,
455 &this->responding.packet);
456 message->destroy(message);
457 if (status != SUCCESS)
458 {
459 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
460 return DESTROY_ME;
461 }
462
463 charon->sender->send(charon->sender,
464 this->responding.packet->clone(this->responding.packet));
465 if (delete)
466 {
467 return DESTROY_ME;
468 }
469 return SUCCESS;
470 }
471
472 /**
473 * handle an incoming request message
474 */
475 static status_t process_request(private_task_manager_t *this,
476 message_t *message)
477 {
478 enumerator_t *enumerator;
479 task_t *task = NULL;
480
481 if (this->passive_tasks->get_count(this->passive_tasks) == 0)
482 { /* create tasks depending on request type, if not already some queued */
483 switch (message->get_exchange_type(message))
484 {
485 case ID_PROT:
486 task = (task_t *)main_mode_create(this->ike_sa, FALSE);
487 this->passive_tasks->insert_last(this->passive_tasks, task);
488 break;
489 case AGGRESSIVE:
490 /* TODO-IKEv1: agressive mode */
491 return FAILED;
492 case QUICK_MODE:
493 task = (task_t *)quick_mode_create(this->ike_sa, NULL,
494 NULL, NULL);
495 this->passive_tasks->insert_last(this->passive_tasks, task);
496 break;
497 case INFORMATIONAL_V1:
498 /* TODO-IKEv1: informational */
499 return FAILED;
500 default:
501 return FAILED;
502 }
503 }
504 /* let the tasks process the message */
505 enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
506 while (enumerator->enumerate(enumerator, (void*)&task))
507 {
508 switch (task->process(task, message))
509 {
510 case SUCCESS:
511 /* task completed, remove it */
512 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
513 task->destroy(task);
514 enumerator->destroy(enumerator);
515 return SUCCESS;
516 case NEED_MORE:
517 /* processed, but task needs at least another call to build() */
518 break;
519 case FAILED:
520 default:
521 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
522 /* FALL */
523 case DESTROY_ME:
524 /* critical failure, destroy IKE_SA */
525 this->passive_tasks->remove_at(this->passive_tasks, enumerator);
526 enumerator->destroy(enumerator);
527 task->destroy(task);
528 return DESTROY_ME;
529 }
530 }
531 enumerator->destroy(enumerator);
532
533 return build_response(this, message);
534 }
535
536 /**
537 * handle an incoming response message
538 */
539 static status_t process_response(private_task_manager_t *this,
540 message_t *message)
541 {
542 enumerator_t *enumerator;
543 task_t *task;
544
545 if (message->get_exchange_type(message) != this->initiating.type)
546 {
547 DBG1(DBG_IKE, "received %N response, but expected %N",
548 exchange_type_names, message->get_exchange_type(message),
549 exchange_type_names, this->initiating.type);
550 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
551 return DESTROY_ME;
552 }
553
554 enumerator = this->active_tasks->create_enumerator(this->active_tasks);
555 while (enumerator->enumerate(enumerator, (void*)&task))
556 {
557 switch (task->process(task, message))
558 {
559 case SUCCESS:
560 /* task completed, remove it */
561 this->active_tasks->remove_at(this->active_tasks, enumerator);
562 task->destroy(task);
563 break;
564 case NEED_MORE:
565 /* processed, but task needs another exchange */
566 break;
567 case FAILED:
568 default:
569 charon->bus->ike_updown(charon->bus, this->ike_sa, FALSE);
570 /* FALL */
571 case DESTROY_ME:
572 /* critical failure, destroy IKE_SA */
573 this->active_tasks->remove_at(this->active_tasks, enumerator);
574 enumerator->destroy(enumerator);
575 task->destroy(task);
576 return DESTROY_ME;
577 }
578 }
579 enumerator->destroy(enumerator);
580
581 this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
582 this->initiating.packet->destroy(this->initiating.packet);
583 this->initiating.packet = NULL;
584
585 return initiate(this);
586 }
587
588 METHOD(task_manager_t, process_message, status_t,
589 private_task_manager_t *this, message_t *msg)
590 {
591 u_int32_t hash, mid;
592 host_t *me, *other;
593
594 mid = msg->get_message_id(msg);
595
596 /* TODO-IKEv1: update hosts more selectively */
597 me = msg->get_destination(msg);
598 other = msg->get_source(msg);
599
600 if ((mid && mid == this->initiating.mid) ||
601 (this->initiating.mid == 0 &&
602 this->active_tasks->get_count(this->active_tasks)))
603 {
604 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
605 charon->bus->message(charon->bus, msg, FALSE);
606 if (process_response(this, msg) != SUCCESS)
607 {
608 flush(this);
609 return DESTROY_ME;
610 }
611 }
612 else
613 {
614 hash = chunk_hash(msg->get_packet_data(msg));
615 if (hash == this->responding.hash)
616 {
617 DBG1(DBG_IKE, "received retransmit of request with ID %d, "
618 "retransmitting response", mid);
619 charon->sender->send(charon->sender,
620 this->responding.packet->clone(this->responding.packet));
621 return SUCCESS;
622 }
623 this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
624 charon->bus->message(charon->bus, msg, TRUE);
625 if (process_request(this, msg) != SUCCESS)
626 {
627 flush(this);
628 return DESTROY_ME;
629 }
630
631 this->responding.mid = mid;
632 this->responding.hash = hash;
633 }
634 return SUCCESS;
635 }
636
637 METHOD(task_manager_t, queue_task, void,
638 private_task_manager_t *this, task_t *task)
639 {
640 DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
641 this->queued_tasks->insert_last(this->queued_tasks, task);
642 }
643
644 METHOD(task_manager_t, adopt_tasks, void,
645 private_task_manager_t *this, task_manager_t *other_public)
646 {
647 private_task_manager_t *other = (private_task_manager_t*)other_public;
648 task_t *task;
649
650 /* move queued tasks from other to this */
651 while (other->queued_tasks->remove_last(other->queued_tasks,
652 (void**)&task) == SUCCESS)
653 {
654 DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
655 task->migrate(task, this->ike_sa);
656 this->queued_tasks->insert_first(this->queued_tasks, task);
657 }
658 }
659
660 METHOD(task_manager_t, busy, bool,
661 private_task_manager_t *this)
662 {
663 return (this->active_tasks->get_count(this->active_tasks) > 0);
664 }
665
666 METHOD(task_manager_t, incr_mid, void,
667 private_task_manager_t *this, bool initiate)
668 {
669 }
670
671 METHOD(task_manager_t, reset, void,
672 private_task_manager_t *this, u_int32_t initiate, u_int32_t respond)
673 {
674 }
675
676 METHOD(task_manager_t, create_task_enumerator, enumerator_t*,
677 private_task_manager_t *this, task_queue_t queue)
678 {
679 switch (queue)
680 {
681 case TASK_QUEUE_ACTIVE:
682 return this->active_tasks->create_enumerator(this->active_tasks);
683 case TASK_QUEUE_PASSIVE:
684 return this->passive_tasks->create_enumerator(this->passive_tasks);
685 case TASK_QUEUE_QUEUED:
686 return this->queued_tasks->create_enumerator(this->queued_tasks);
687 default:
688 return enumerator_create_empty();
689 }
690 }
691
692 METHOD(task_manager_t, destroy, void,
693 private_task_manager_t *this)
694 {
695 flush(this);
696
697 this->active_tasks->destroy(this->active_tasks);
698 this->queued_tasks->destroy(this->queued_tasks);
699 this->passive_tasks->destroy(this->passive_tasks);
700
701 DESTROY_IF(this->responding.packet);
702 DESTROY_IF(this->initiating.packet);
703 DESTROY_IF(this->rng);
704 free(this);
705 }
706
707 /*
708 * see header file
709 */
710 task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
711 {
712 private_task_manager_t *this;
713
714 INIT(this,
715 .public = {
716 .task_manager = {
717 .process_message = _process_message,
718 .queue_task = _queue_task,
719 .initiate = _initiate,
720 .retransmit = _retransmit,
721 .incr_mid = _incr_mid,
722 .reset = _reset,
723 .adopt_tasks = _adopt_tasks,
724 .busy = _busy,
725 .create_task_enumerator = _create_task_enumerator,
726 .destroy = _destroy,
727 },
728 },
729 .ike_sa = ike_sa,
730 .initiating.type = EXCHANGE_TYPE_UNDEFINED,
731 .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
732 .queued_tasks = linked_list_create(),
733 .active_tasks = linked_list_create(),
734 .passive_tasks = linked_list_create(),
735 .retransmit_tries = lib->settings->get_int(lib->settings,
736 "charon.retransmit_tries", RETRANSMIT_TRIES),
737 .retransmit_timeout = lib->settings->get_double(lib->settings,
738 "charon.retransmit_timeout", RETRANSMIT_TIMEOUT),
739 .retransmit_base = lib->settings->get_double(lib->settings,
740 "charon.retransmit_base", RETRANSMIT_BASE),
741 );
742
743 return &this->public;
744 }