]>
Commit | Line | Data |
---|---|---|
c60c7694 MW |
1 | /** |
2 | * @file task_manager.c | |
3 | * | |
4 | * @brief Implementation of task_manager_t. | |
5 | * | |
6 | */ | |
7 | ||
8 | /* | |
9 | * Copyright (C) 2007 Martin Willi | |
10 | * Hochschule fuer Technik Rapperswil | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU General Public License as published by the | |
14 | * Free Software Foundation; either version 2 of the License, or (at your | |
15 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
19 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
21 | */ | |
22 | ||
23 | #include "task_manager.h" | |
24 | ||
25 | #include <daemon.h> | |
26 | #include <sa/tasks/ike_init.h> | |
27 | #include <sa/tasks/ike_natd.h> | |
28 | #include <sa/tasks/ike_auth.h> | |
29 | #include <sa/tasks/ike_cert.h> | |
30 | #include <sa/tasks/ike_rekey.h> | |
31 | #include <sa/tasks/ike_delete.h> | |
32 | #include <sa/tasks/ike_config.h> | |
33 | #include <sa/tasks/ike_dpd.h> | |
34 | #include <sa/tasks/child_create.h> | |
35 | #include <sa/tasks/child_rekey.h> | |
36 | #include <sa/tasks/child_delete.h> | |
37 | #include <encoding/payloads/delete_payload.h> | |
e0fe7651 | 38 | #include <processing/jobs/retransmit_job.h> |
c60c7694 MW |
39 | |
40 | typedef struct exchange_t exchange_t; | |
41 | ||
42 | /** | |
43 | * An exchange in the air, used do detect and handle retransmission | |
44 | */ | |
45 | struct exchange_t { | |
46 | ||
47 | /** | |
48 | * Message ID used for this transaction | |
49 | */ | |
50 | u_int32_t mid; | |
51 | ||
52 | /** | |
53 | * generated packet for retransmission | |
54 | */ | |
55 | packet_t *packet; | |
56 | }; | |
57 | ||
58 | typedef struct private_task_manager_t private_task_manager_t; | |
59 | ||
60 | /** | |
61 | * private data of the task manager | |
62 | */ | |
63 | struct private_task_manager_t { | |
64 | ||
65 | /** | |
66 | * public functions | |
67 | */ | |
68 | task_manager_t public; | |
69 | ||
70 | /** | |
71 | * associated IKE_SA we are serving | |
72 | */ | |
73 | ike_sa_t *ike_sa; | |
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 | * packet for retransmission | |
86 | */ | |
87 | packet_t *packet; | |
88 | ||
89 | } responding; | |
90 | ||
91 | /** | |
92 | * Exchange we are currently handling as initiator | |
93 | */ | |
94 | struct { | |
95 | /** | |
96 | * Message ID of the exchange | |
97 | */ | |
98 | u_int32_t mid; | |
99 | ||
100 | /** | |
101 | * how many times we have retransmitted so far | |
102 | */ | |
103 | u_int retransmitted; | |
104 | ||
105 | /** | |
106 | * packet for retransmission | |
107 | */ | |
108 | packet_t *packet; | |
109 | ||
110 | /** | |
111 | * type of the initated exchange | |
112 | */ | |
113 | exchange_type_t type; | |
114 | ||
115 | } initiating; | |
116 | ||
117 | /** | |
118 | * List of queued tasks not yet in action | |
119 | */ | |
120 | linked_list_t *queued_tasks; | |
121 | ||
122 | /** | |
123 | * List of active tasks, initiated by ourselve | |
124 | */ | |
125 | linked_list_t *active_tasks; | |
126 | ||
127 | /** | |
128 | * List of tasks initiated by peer | |
129 | */ | |
130 | linked_list_t *passive_tasks; | |
c60c7694 MW |
131 | }; |
132 | ||
0f33e826 MW |
133 | /** |
134 | * flush all tasks in the task manager | |
135 | */ | |
136 | static void flush(private_task_manager_t *this) | |
137 | { | |
138 | task_t *task; | |
139 | ||
140 | this->queued_tasks->destroy_offset(this->queued_tasks, | |
141 | offsetof(task_t, destroy)); | |
142 | this->passive_tasks->destroy_offset(this->passive_tasks, | |
143 | offsetof(task_t, destroy)); | |
144 | ||
145 | /* emmit outstanding signals for tasks */ | |
146 | while (this->active_tasks->remove_last(this->active_tasks, | |
147 | (void**)&task) == SUCCESS) | |
148 | { | |
149 | switch (task->get_type(task)) | |
150 | { | |
151 | case IKE_AUTH: | |
152 | SIG(IKE_UP_FAILED, "establishing IKE_SA failed"); | |
153 | break; | |
154 | case IKE_DELETE: | |
155 | SIG(IKE_DOWN_FAILED, "IKE_SA deleted"); | |
156 | break; | |
157 | case IKE_REKEY: | |
158 | SIG(IKE_REKEY_FAILED, "rekeying IKE_SA failed"); | |
159 | break; | |
160 | case CHILD_CREATE: | |
161 | SIG(CHILD_UP_FAILED, "establishing CHILD_SA failed"); | |
162 | break; | |
163 | case CHILD_DELETE: | |
164 | SIG(CHILD_DOWN_FAILED, "deleting CHILD_SA failed"); | |
165 | break; | |
166 | case CHILD_REKEY: | |
167 | SIG(IKE_REKEY_FAILED, "rekeying CHILD_SA failed"); | |
168 | break; | |
169 | default: | |
170 | break; | |
171 | } | |
172 | task->destroy(task); | |
173 | } | |
174 | this->queued_tasks = linked_list_create(); | |
175 | this->passive_tasks = linked_list_create(); | |
176 | } | |
177 | ||
c60c7694 MW |
178 | /** |
179 | * move a task of a specific type from the queue to the active list | |
180 | */ | |
181 | static bool activate_task(private_task_manager_t *this, task_type_t type) | |
182 | { | |
183 | iterator_t *iterator; | |
184 | task_t *task; | |
185 | bool found = FALSE; | |
186 | ||
187 | iterator = this->queued_tasks->create_iterator(this->queued_tasks, TRUE); | |
188 | while (iterator->iterate(iterator, (void**)&task)) | |
189 | { | |
190 | if (task->get_type(task) == type) | |
191 | { | |
192 | DBG2(DBG_IKE, " activating %N task", task_type_names, type); | |
193 | iterator->remove(iterator); | |
194 | this->active_tasks->insert_last(this->active_tasks, task); | |
195 | found = TRUE; | |
196 | break; | |
197 | } | |
198 | } | |
199 | iterator->destroy(iterator); | |
200 | return found; | |
201 | } | |
202 | ||
203 | /** | |
204 | * Implementation of task_manager_t.retransmit | |
205 | */ | |
206 | static status_t retransmit(private_task_manager_t *this, u_int32_t message_id) | |
207 | { | |
208 | if (message_id == this->initiating.mid) | |
209 | { | |
210 | u_int32_t timeout; | |
211 | job_t *job; | |
212 | ||
213 | timeout = charon->configuration->get_retransmit_timeout( | |
214 | charon->configuration, this->initiating.retransmitted); | |
215 | if (timeout == 0) | |
216 | { | |
217 | DBG1(DBG_IKE, "giving up after %d retransmits", | |
218 | this->initiating.retransmitted - 1); | |
219 | return DESTROY_ME; | |
220 | } | |
221 | ||
222 | if (this->initiating.retransmitted) | |
223 | { | |
224 | DBG1(DBG_IKE, "retransmit %d of request with message ID %d", | |
225 | this->initiating.retransmitted, message_id); | |
226 | } | |
227 | this->initiating.retransmitted++; | |
228 | ||
4deb8948 | 229 | charon->sender->send(charon->sender, |
c60c7694 MW |
230 | this->initiating.packet->clone(this->initiating.packet)); |
231 | job = (job_t*)retransmit_job_create(this->initiating.mid, | |
232 | this->ike_sa->get_id(this->ike_sa)); | |
233 | charon->event_queue->add_relative(charon->event_queue, job, timeout); | |
234 | } | |
235 | return SUCCESS; | |
236 | } | |
237 | ||
238 | /** | |
239 | * build a request using the active task list | |
240 | * Implementation of task_manager_t.initiate | |
241 | */ | |
242 | static status_t build_request(private_task_manager_t *this) | |
243 | { | |
244 | iterator_t *iterator; | |
245 | task_t *task; | |
246 | message_t *message; | |
247 | status_t status; | |
248 | exchange_type_t exchange = 0; | |
249 | ||
2540992a MW |
250 | if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED) |
251 | { | |
252 | DBG2(DBG_IKE, "delaying task initiation, exchange in progress"); | |
253 | /* do not initiate if we already have a message in the air */ | |
254 | return SUCCESS; | |
255 | } | |
256 | ||
c60c7694 MW |
257 | if (this->active_tasks->get_count(this->active_tasks) == 0) |
258 | { | |
259 | DBG2(DBG_IKE, "activating new tasks"); | |
260 | switch (this->ike_sa->get_state(this->ike_sa)) | |
261 | { | |
262 | case IKE_CREATED: | |
263 | if (activate_task(this, IKE_INIT)) | |
264 | { | |
265 | exchange = IKE_SA_INIT; | |
266 | activate_task(this, IKE_NATD); | |
267 | activate_task(this, IKE_CERT); | |
268 | activate_task(this, IKE_AUTHENTICATE); | |
269 | activate_task(this, IKE_CONFIG); | |
270 | activate_task(this, CHILD_CREATE); | |
271 | } | |
272 | break; | |
273 | case IKE_ESTABLISHED: | |
274 | if (activate_task(this, CHILD_CREATE)) | |
275 | { | |
276 | exchange = CREATE_CHILD_SA; | |
277 | activate_task(this, IKE_CONFIG); | |
278 | break; | |
279 | } | |
280 | if (activate_task(this, CHILD_DELETE)) | |
281 | { | |
282 | exchange = INFORMATIONAL; | |
283 | break; | |
284 | } | |
285 | if (activate_task(this, CHILD_REKEY)) | |
286 | { | |
287 | exchange = CREATE_CHILD_SA; | |
288 | break; | |
289 | } | |
290 | if (activate_task(this, IKE_DELETE)) | |
291 | { | |
292 | exchange = INFORMATIONAL; | |
293 | break; | |
294 | } | |
295 | if (activate_task(this, IKE_REKEY)) | |
296 | { | |
297 | exchange = CREATE_CHILD_SA; | |
298 | break; | |
299 | } | |
300 | if (activate_task(this, IKE_DEADPEER)) | |
301 | { | |
302 | exchange = INFORMATIONAL; | |
303 | break; | |
304 | } | |
305 | case IKE_REKEYING: | |
306 | if (activate_task(this, IKE_DELETE)) | |
307 | { | |
308 | exchange = INFORMATIONAL; | |
309 | break; | |
310 | } | |
311 | case IKE_DELETING: | |
312 | default: | |
313 | break; | |
314 | } | |
315 | } | |
316 | else | |
317 | { | |
318 | DBG2(DBG_IKE, "reinitiating already active tasks"); | |
319 | iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE); | |
320 | while (iterator->iterate(iterator, (void**)&task)) | |
321 | { | |
322 | DBG2(DBG_IKE, " %N task", task_type_names, task->get_type(task)); | |
323 | switch (task->get_type(task)) | |
324 | { | |
325 | case IKE_INIT: | |
326 | exchange = IKE_SA_INIT; | |
327 | break; | |
328 | case IKE_AUTHENTICATE: | |
329 | exchange = IKE_AUTH; | |
330 | break; | |
331 | default: | |
332 | continue; | |
333 | } | |
334 | break; | |
335 | } | |
336 | iterator->destroy(iterator); | |
337 | } | |
338 | ||
339 | if (exchange == 0) | |
340 | { | |
341 | DBG2(DBG_IKE, "nothing to initiate"); | |
342 | /* nothing to do yet... */ | |
343 | return SUCCESS; | |
344 | } | |
345 | ||
346 | message = message_create(); | |
347 | message->set_message_id(message, this->initiating.mid); | |
348 | message->set_exchange_type(message, exchange); | |
349 | this->initiating.type = exchange; | |
350 | this->initiating.retransmitted = 0; | |
351 | ||
352 | iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE); | |
353 | while (iterator->iterate(iterator, (void*)&task)) | |
354 | { | |
355 | switch (task->build(task, message)) | |
356 | { | |
357 | case SUCCESS: | |
358 | /* task completed, remove it */ | |
359 | iterator->remove(iterator); | |
360 | task->destroy(task); | |
361 | break; | |
362 | case NEED_MORE: | |
363 | /* processed, but task needs another exchange */ | |
364 | break; | |
365 | case FAILED: | |
366 | default: | |
367 | /* critical failure, destroy IKE_SA */ | |
368 | iterator->destroy(iterator); | |
369 | message->destroy(message); | |
0f33e826 | 370 | flush(this); |
c60c7694 MW |
371 | return DESTROY_ME; |
372 | } | |
373 | } | |
374 | iterator->destroy(iterator); | |
375 | ||
376 | DESTROY_IF(this->initiating.packet); | |
377 | status = this->ike_sa->generate_message(this->ike_sa, message, | |
378 | &this->initiating.packet); | |
379 | message->destroy(message); | |
380 | if (status != SUCCESS) | |
381 | { | |
382 | /* message generation failed. There is nothing more to do than to | |
383 | * close the SA */ | |
0f33e826 | 384 | flush(this); |
c60c7694 MW |
385 | return DESTROY_ME; |
386 | } | |
387 | ||
388 | return retransmit(this, this->initiating.mid); | |
389 | } | |
390 | ||
391 | /** | |
392 | * handle an incoming response message | |
393 | */ | |
394 | static status_t process_response(private_task_manager_t *this, | |
395 | message_t *message) | |
396 | { | |
397 | iterator_t *iterator; | |
398 | task_t *task; | |
399 | ||
400 | if (message->get_exchange_type(message) != this->initiating.type) | |
401 | { | |
402 | DBG1(DBG_IKE, "received %N response, but expected %N", | |
403 | exchange_type_names, message->get_exchange_type(message), | |
404 | exchange_type_names, this->initiating.type); | |
405 | return DESTROY_ME; | |
406 | } | |
407 | ||
408 | iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE); | |
409 | while (iterator->iterate(iterator, (void*)&task)) | |
410 | { | |
411 | switch (task->process(task, message)) | |
412 | { | |
413 | case SUCCESS: | |
414 | /* task completed, remove it */ | |
415 | iterator->remove(iterator); | |
416 | task->destroy(task); | |
417 | break; | |
418 | case NEED_MORE: | |
419 | /* processed, but task needs another exchange */ | |
420 | break; | |
421 | case FAILED: | |
422 | default: | |
423 | /* critical failure, destroy IKE_SA */ | |
424 | iterator->destroy(iterator); | |
425 | return DESTROY_ME; | |
426 | } | |
427 | } | |
428 | iterator->destroy(iterator); | |
429 | ||
430 | this->initiating.mid++; | |
2540992a | 431 | this->initiating.type = EXCHANGE_TYPE_UNDEFINED; |
c60c7694 MW |
432 | |
433 | return build_request(this); | |
434 | } | |
435 | ||
195ada0b MW |
436 | /** |
437 | * handle exchange collisions | |
438 | */ | |
439 | static void handle_collisions(private_task_manager_t *this, task_t *task) | |
440 | { | |
441 | iterator_t *iterator; | |
442 | task_t *active; | |
443 | task_type_t type; | |
444 | ||
445 | type = task->get_type(task); | |
446 | ||
4315f5c8 MW |
447 | /* do we have to check */ |
448 | if (type == IKE_REKEY || type == CHILD_REKEY || | |
449 | type == CHILD_DELETE || type == IKE_DELETE) | |
450 | { | |
451 | /* find an exchange collision, and notify these tasks */ | |
452 | iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE); | |
453 | while (iterator->iterate(iterator, (void**)&active)) | |
454 | { | |
455 | switch (active->get_type(active)) | |
456 | { | |
457 | case IKE_REKEY: | |
458 | if (type == IKE_REKEY || type == IKE_DELETE) | |
459 | { | |
460 | ike_rekey_t *rekey = (ike_rekey_t*)active; | |
461 | rekey->collide(rekey, task); | |
462 | break; | |
463 | } | |
464 | continue; | |
465 | case CHILD_REKEY: | |
4315f5c8 MW |
466 | if (type == CHILD_REKEY || type == CHILD_DELETE) |
467 | { | |
468 | child_rekey_t *rekey = (child_rekey_t*)active; | |
469 | rekey->collide(rekey, task); | |
470 | break; | |
471 | } | |
472 | continue; | |
473 | default: | |
474 | continue; | |
475 | } | |
476 | iterator->destroy(iterator); | |
477 | return; | |
478 | } | |
479 | iterator->destroy(iterator); | |
195ada0b | 480 | } |
4315f5c8 MW |
481 | /* destroy task if not registered in any active task */ |
482 | task->destroy(task); | |
195ada0b MW |
483 | } |
484 | ||
c60c7694 MW |
485 | /** |
486 | * build a response depending on the "passive" task list | |
487 | */ | |
488 | static status_t build_response(private_task_manager_t *this, | |
489 | exchange_type_t exchange) | |
490 | { | |
491 | iterator_t *iterator; | |
492 | task_t *task; | |
493 | message_t *message; | |
494 | bool delete = FALSE; | |
495 | status_t status; | |
496 | ||
497 | message = message_create(); | |
498 | message->set_exchange_type(message, exchange); | |
499 | message->set_message_id(message, this->responding.mid); | |
500 | message->set_request(message, FALSE); | |
501 | ||
502 | iterator = this->passive_tasks->create_iterator(this->passive_tasks, TRUE); | |
503 | while (iterator->iterate(iterator, (void*)&task)) | |
504 | { | |
505 | switch (task->build(task, message)) | |
506 | { | |
507 | case SUCCESS: | |
508 | /* task completed, remove it */ | |
509 | iterator->remove(iterator); | |
195ada0b | 510 | handle_collisions(this, task); |
c60c7694 MW |
511 | case NEED_MORE: |
512 | /* processed, but task needs another exchange */ | |
513 | break; | |
514 | case FAILED: | |
515 | default: | |
516 | /* destroy IKE_SA, but SEND response first */ | |
517 | delete = TRUE; | |
518 | break; | |
519 | } | |
520 | if (delete) | |
521 | { | |
522 | break; | |
523 | } | |
524 | } | |
525 | iterator->destroy(iterator); | |
ddae0f96 MW |
526 | |
527 | /* remove resonder SPI if IKE_SA_INIT failed */ | |
528 | if (delete && exchange == IKE_SA_INIT) | |
529 | { | |
530 | ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa); | |
531 | id->set_responder_spi(id, 0); | |
532 | } | |
c60c7694 MW |
533 | |
534 | /* message complete, send it */ | |
535 | DESTROY_IF(this->responding.packet); | |
536 | status = this->ike_sa->generate_message(this->ike_sa, message, | |
537 | &this->responding.packet); | |
538 | message->destroy(message); | |
539 | if (status != SUCCESS) | |
540 | { | |
541 | return DESTROY_ME; | |
542 | } | |
543 | ||
4deb8948 MW |
544 | charon->sender->send(charon->sender, |
545 | this->responding.packet->clone(this->responding.packet)); | |
c60c7694 MW |
546 | if (delete) |
547 | { | |
548 | return DESTROY_ME; | |
549 | } | |
550 | return SUCCESS; | |
551 | } | |
552 | ||
553 | /** | |
554 | * handle an incoming request message | |
555 | */ | |
556 | static status_t process_request(private_task_manager_t *this, | |
557 | message_t *message) | |
558 | { | |
559 | iterator_t *iterator; | |
560 | task_t *task = NULL; | |
561 | exchange_type_t exchange; | |
562 | payload_t *payload; | |
563 | notify_payload_t *notify; | |
564 | ||
565 | exchange = message->get_exchange_type(message); | |
566 | ||
567 | /* create tasks depending on request type */ | |
568 | switch (exchange) | |
569 | { | |
570 | case IKE_SA_INIT: | |
571 | { | |
572 | task = (task_t*)ike_init_create(this->ike_sa, FALSE, NULL); | |
573 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
574 | task = (task_t*)ike_natd_create(this->ike_sa, FALSE); | |
575 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
576 | task = (task_t*)ike_cert_create(this->ike_sa, FALSE); | |
577 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
c60c7694 MW |
578 | task = (task_t*)ike_auth_create(this->ike_sa, FALSE); |
579 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
e0fe7651 | 580 | task = (task_t*)ike_config_create(this->ike_sa, FALSE); |
5744d3e7 | 581 | this->passive_tasks->insert_last(this->passive_tasks, task); |
c60c7694 MW |
582 | task = (task_t*)child_create_create(this->ike_sa, NULL); |
583 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
584 | break; | |
585 | } | |
586 | case CREATE_CHILD_SA: | |
587 | { | |
588 | bool notify_found = FALSE, ts_found = FALSE; | |
589 | iterator = message->get_payload_iterator(message); | |
590 | while (iterator->iterate(iterator, (void**)&payload)) | |
591 | { | |
592 | switch (payload->get_type(payload)) | |
593 | { | |
594 | case NOTIFY: | |
595 | { | |
596 | /* if we find a rekey notify, its CHILD_SA rekeying */ | |
597 | notify = (notify_payload_t*)payload; | |
598 | if (notify->get_notify_type(notify) == REKEY_SA && | |
599 | (notify->get_protocol_id(notify) == PROTO_AH || | |
600 | notify->get_protocol_id(notify) == PROTO_ESP)) | |
601 | { | |
602 | notify_found = TRUE; | |
603 | } | |
604 | break; | |
605 | } | |
606 | case TRAFFIC_SELECTOR_INITIATOR: | |
607 | case TRAFFIC_SELECTOR_RESPONDER: | |
608 | { | |
609 | /* if we don't find a TS, its IKE rekeying */ | |
610 | ts_found = TRUE; | |
611 | break; | |
612 | } | |
613 | default: | |
614 | break; | |
615 | } | |
616 | } | |
617 | iterator->destroy(iterator); | |
618 | ||
619 | if (ts_found) | |
620 | { | |
621 | if (notify_found) | |
622 | { | |
623 | task = (task_t*)child_rekey_create(this->ike_sa, NULL); | |
624 | } | |
625 | else | |
626 | { | |
627 | task = (task_t*)child_create_create(this->ike_sa, NULL); | |
628 | } | |
629 | } | |
630 | else | |
631 | { | |
632 | task = (task_t*)ike_rekey_create(this->ike_sa, FALSE); | |
633 | } | |
634 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
c60c7694 MW |
635 | break; |
636 | } | |
637 | case INFORMATIONAL: | |
638 | { | |
639 | delete_payload_t *delete; | |
640 | ||
641 | delete = (delete_payload_t*)message->get_payload(message, DELETE); | |
642 | if (delete) | |
643 | { | |
644 | if (delete->get_protocol_id(delete) == PROTO_IKE) | |
645 | { | |
646 | task = (task_t*)ike_delete_create(this->ike_sa, FALSE); | |
647 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
648 | } | |
649 | else | |
650 | { | |
651 | task = (task_t*)child_delete_create(this->ike_sa, NULL); | |
652 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
653 | } | |
654 | } | |
655 | else | |
656 | { | |
657 | task = (task_t*)ike_dpd_create(FALSE); | |
658 | this->passive_tasks->insert_last(this->passive_tasks, task); | |
659 | } | |
660 | break; | |
661 | } | |
662 | default: | |
663 | break; | |
664 | } | |
665 | ||
666 | /* let the tasks process the message */ | |
667 | iterator = this->passive_tasks->create_iterator(this->passive_tasks, TRUE); | |
668 | while (iterator->iterate(iterator, (void*)&task)) | |
669 | { | |
670 | switch (task->process(task, message)) | |
671 | { | |
672 | case SUCCESS: | |
673 | /* task completed, remove it */ | |
674 | iterator->remove(iterator); | |
675 | task->destroy(task); | |
676 | break; | |
677 | case NEED_MORE: | |
678 | /* processed, but task needs at least another call to build() */ | |
679 | break; | |
680 | case FAILED: | |
681 | default: | |
682 | /* critical failure, destroy IKE_SA */ | |
683 | iterator->destroy(iterator); | |
684 | return DESTROY_ME; | |
685 | } | |
686 | } | |
687 | iterator->destroy(iterator); | |
688 | ||
689 | return build_response(this, exchange); | |
690 | } | |
691 | ||
692 | /** | |
693 | * Implementation of task_manager_t.process_message | |
694 | */ | |
695 | static status_t process_message(private_task_manager_t *this, message_t *msg) | |
696 | { | |
697 | u_int32_t mid = msg->get_message_id(msg); | |
698 | ||
699 | if (msg->get_request(msg)) | |
700 | { | |
701 | if (mid == this->responding.mid) | |
702 | { | |
703 | if (process_request(this, msg) != SUCCESS) | |
704 | { | |
0f33e826 | 705 | flush(this); |
c60c7694 MW |
706 | return DESTROY_ME; |
707 | } | |
708 | this->responding.mid++; | |
709 | } | |
710 | else if ((mid == this->responding.mid - 1) && this->responding.packet) | |
711 | { | |
712 | DBG1(DBG_IKE, "received retransmit of request with ID %d, " | |
713 | "retransmitting response", mid); | |
4deb8948 MW |
714 | charon->sender->send(charon->sender, |
715 | this->responding.packet->clone(this->responding.packet)); | |
c60c7694 MW |
716 | } |
717 | else | |
718 | { | |
719 | DBG1(DBG_IKE, "received message ID %d, excepted %d. Ignored", | |
720 | mid, this->responding.mid); | |
721 | } | |
722 | } | |
723 | else | |
724 | { | |
725 | if (mid == this->initiating.mid) | |
726 | { | |
727 | if (process_response(this, msg) != SUCCESS) | |
728 | { | |
0f33e826 | 729 | flush(this); |
c60c7694 MW |
730 | return DESTROY_ME; |
731 | } | |
732 | } | |
733 | else | |
734 | { | |
735 | DBG1(DBG_IKE, "received message ID %d, excepted %d. Ignored", | |
736 | mid, this->initiating.mid); | |
737 | return SUCCESS; | |
738 | } | |
739 | } | |
740 | return SUCCESS; | |
741 | } | |
742 | ||
743 | /** | |
744 | * Implementation of task_manager_t.queue_task | |
745 | */ | |
746 | static void queue_task(private_task_manager_t *this, task_t *task) | |
747 | { | |
748 | DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task)); | |
749 | this->queued_tasks->insert_last(this->queued_tasks, task); | |
750 | } | |
751 | ||
752 | /** | |
753 | * Implementation of task_manager_t.adopt_tasks | |
754 | */ | |
755 | static void adopt_tasks(private_task_manager_t *this, private_task_manager_t *other) | |
756 | { | |
757 | task_t *task; | |
758 | ||
759 | /* move queued tasks from other to this */ | |
760 | while (other->queued_tasks->remove_last(other->queued_tasks, | |
761 | (void**)&task) == SUCCESS) | |
762 | { | |
e23a59f6 | 763 | DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task)); |
c60c7694 MW |
764 | task->migrate(task, this->ike_sa); |
765 | this->queued_tasks->insert_first(this->queued_tasks, task); | |
766 | } | |
767 | ||
768 | /* reset active tasks and move them to others queued tasks */ | |
769 | while (other->active_tasks->remove_last(other->active_tasks, | |
770 | (void**)&task) == SUCCESS) | |
771 | { | |
e23a59f6 | 772 | DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task)); |
c60c7694 MW |
773 | task->migrate(task, this->ike_sa); |
774 | this->queued_tasks->insert_first(this->queued_tasks, task); | |
775 | } | |
776 | } | |
777 | ||
778 | /** | |
779 | * Implementation of task_manager_t.busy | |
780 | */ | |
781 | static bool busy(private_task_manager_t *this) | |
782 | { | |
783 | return (this->active_tasks->get_count(this->active_tasks) > 0); | |
784 | } | |
785 | ||
786 | /** | |
787 | * Implementation of task_manager_t.reset | |
788 | */ | |
789 | static void reset(private_task_manager_t *this) | |
790 | { | |
791 | task_t *task; | |
792 | ||
793 | /* reset message counters and retransmit packets */ | |
794 | DESTROY_IF(this->responding.packet); | |
795 | DESTROY_IF(this->initiating.packet); | |
c60c7694 MW |
796 | this->responding.packet = NULL; |
797 | this->initiating.packet = NULL; | |
c60c7694 | 798 | this->responding.mid = 0; |
361e6df8 | 799 | this->initiating.mid = -1; |
1edf116c | 800 | this->initiating.type = EXCHANGE_TYPE_UNDEFINED; |
c60c7694 MW |
801 | |
802 | /* reset active tasks */ | |
803 | while (this->active_tasks->remove_last(this->active_tasks, | |
804 | (void**)&task) == SUCCESS) | |
805 | { | |
806 | task->migrate(task, this->ike_sa); | |
807 | this->queued_tasks->insert_first(this->queued_tasks, task); | |
808 | } | |
809 | } | |
810 | ||
811 | /** | |
812 | * Implementation of task_manager_t.destroy | |
813 | */ | |
814 | static void destroy(private_task_manager_t *this) | |
815 | { | |
0f33e826 | 816 | flush(this); |
c60c7694 | 817 | |
c60c7694 | 818 | this->active_tasks->destroy(this->active_tasks); |
0f33e826 MW |
819 | this->queued_tasks->destroy(this->queued_tasks); |
820 | this->passive_tasks->destroy(this->passive_tasks); | |
821 | ||
c60c7694 MW |
822 | DESTROY_IF(this->responding.packet); |
823 | DESTROY_IF(this->initiating.packet); | |
c60c7694 MW |
824 | free(this); |
825 | } | |
826 | ||
827 | /* | |
828 | * see header file | |
829 | */ | |
830 | task_manager_t *task_manager_create(ike_sa_t *ike_sa) | |
831 | { | |
832 | private_task_manager_t *this = malloc_thing(private_task_manager_t); | |
833 | ||
834 | this->public.process_message = (status_t(*)(task_manager_t*,message_t*))process_message; | |
835 | this->public.queue_task = (void(*)(task_manager_t*,task_t*))queue_task; | |
836 | this->public.initiate = (status_t(*)(task_manager_t*))build_request; | |
837 | this->public.retransmit = (status_t(*)(task_manager_t*,u_int32_t))retransmit; | |
838 | this->public.reset = (void(*)(task_manager_t*))reset; | |
839 | this->public.adopt_tasks = (void(*)(task_manager_t*,task_manager_t*))adopt_tasks; | |
840 | this->public.busy = (bool(*)(task_manager_t*))busy; | |
841 | this->public.destroy = (void(*)(task_manager_t*))destroy; | |
842 | ||
843 | this->ike_sa = ike_sa; | |
844 | this->responding.packet = NULL; | |
845 | this->initiating.packet = NULL; | |
846 | this->responding.mid = 0; | |
847 | this->initiating.mid = 0; | |
2540992a | 848 | this->initiating.type = EXCHANGE_TYPE_UNDEFINED; |
c60c7694 MW |
849 | this->queued_tasks = linked_list_create(); |
850 | this->active_tasks = linked_list_create(); | |
851 | this->passive_tasks = linked_list_create(); | |
c60c7694 MW |
852 | |
853 | return &this->public; | |
854 | } |