Statistics
| Branch: | Tag: | Revision:

root / src / charon / sa / task_manager.c @ 8f0ab613e572f0f75cd32839920adb17f0c53534

History | View | Annotate | Download (25.7 KB)

1
/*
2
 * Copyright (C) 2007 Tobias Brunner
3
 * Copyright (C) 2007 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.h"
18
19
#include <math.h>
20
21
#include <daemon.h>
22
#include <sa/tasks/ike_init.h>
23
#include <sa/tasks/ike_natd.h>
24
#include <sa/tasks/ike_mobike.h>
25
#include <sa/tasks/ike_auth.h>
26
#include <sa/tasks/ike_auth_lifetime.h>
27
#include <sa/tasks/ike_cert_pre.h>
28
#include <sa/tasks/ike_cert_post.h>
29
#include <sa/tasks/ike_rekey.h>
30
#include <sa/tasks/ike_delete.h>
31
#include <sa/tasks/ike_config.h>
32
#include <sa/tasks/ike_dpd.h>
33
#include <sa/tasks/child_create.h>
34
#include <sa/tasks/child_rekey.h>
35
#include <sa/tasks/child_delete.h>
36
#include <encoding/payloads/delete_payload.h>
37
#include <processing/jobs/retransmit_job.h>
38
39
#ifdef ME
40
#include <sa/tasks/ike_me.h>
41
#endif
42
43
typedef struct exchange_t exchange_t;
44
45
/**
46
 * An exchange in the air, used do detect and handle retransmission
47
 */
48
struct exchange_t {
49
    
50
    /**
51
     * Message ID used for this transaction
52
     */
53
    u_int32_t mid;
54
    
55
    /**
56
     * generated packet for retransmission
57
     */
58
    packet_t *packet;
59
};
60
61
typedef struct private_task_manager_t private_task_manager_t;
62
63
/**
64
 * private data of the task manager
65
 */
66
struct private_task_manager_t {
67
    
68
    /**
69
     * public functions
70
     */
71
    task_manager_t public;
72
    
73
    /**
74
     * associated IKE_SA we are serving
75
     */
76
    ike_sa_t *ike_sa;
77
    
78
    /**
79
     * Exchange we are currently handling as responder
80
     */
81
    struct {
82
        /**
83
         * Message ID of the exchange
84
         */
85
        u_int32_t mid;
86
        
87
        /**
88
         * packet for retransmission
89
         */
90
        packet_t *packet;
91
        
92
    } responding;
93
    
94
    /**
95
     * Exchange we are currently handling as initiator
96
     */
97
    struct {
98
        /**
99
         * Message ID of the exchange
100
         */
101
        u_int32_t mid;
102
        
103
        /**
104
         * how many times we have retransmitted so far
105
         */
106
        u_int retransmitted;
107
108
        /**
109
         * packet for retransmission
110
         */
111
        packet_t *packet;
112
        
113
        /**
114
         * type of the initated exchange
115
         */
116
        exchange_type_t type;
117
    
118
    } initiating;
119
    
120
    /**
121
     * List of queued tasks not yet in action
122
     */
123
    linked_list_t *queued_tasks;
124
    
125
    /**
126
     * List of active tasks, initiated by ourselve
127
     */
128
    linked_list_t *active_tasks;
129
    
130
    /**
131
     * List of tasks initiated by peer
132
     */
133
    linked_list_t *passive_tasks;
134
    
135
    /**
136
     * the task manager has been reset 
137
     */
138
    bool reset;
139
};
140
141
/**
142
 * flush all tasks in the task manager
143
 */
144
static void flush(private_task_manager_t *this)
145
{
146
    this->queued_tasks->destroy_offset(this->queued_tasks, 
147
                                        offsetof(task_t, destroy));
148
    this->passive_tasks->destroy_offset(this->passive_tasks,
149
                                        offsetof(task_t, destroy));
150
    this->active_tasks->destroy_offset(this->active_tasks,
151
                                        offsetof(task_t, destroy));
152
    this->queued_tasks = linked_list_create();
153
    this->passive_tasks = linked_list_create();
154
    this->active_tasks = linked_list_create();
155
}
156
157
/**
158
 * move a task of a specific type from the queue to the active list
159
 */
160
static bool activate_task(private_task_manager_t *this, task_type_t type)
161
{
162
    iterator_t *iterator;
163
    task_t *task;
164
    bool found = FALSE;
165
    
166
    iterator = this->queued_tasks->create_iterator(this->queued_tasks, TRUE);
167
    while (iterator->iterate(iterator, (void**)&task))
168
    {
169
        if (task->get_type(task) == type)
170
        {
171
            DBG2(DBG_IKE, "  activating %N task", task_type_names, type);
172
            iterator->remove(iterator);
173
            this->active_tasks->insert_last(this->active_tasks, task);
174
            found = TRUE;
175
            break;
176
        }
177
    }
178
    iterator->destroy(iterator);
179
    return found;
180
}
181
182
/**
183
 * Implementation of task_manager_t.retransmit
184
 */
185
static status_t retransmit(private_task_manager_t *this, u_int32_t message_id)
186
{
187
    if (message_id == this->initiating.mid)
188
    {
189
        u_int32_t timeout;
190
        job_t *job;
191
        iterator_t *iterator;
192
        packet_t *packet;
193
        task_t *task;
194
        ike_mobike_t *mobike = NULL;
195
        
196
        /* check if we are retransmitting a MOBIKE routability check */
197
        iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
198
        while (iterator->iterate(iterator, (void*)&task))
199
        {
200
            if (task->get_type(task) == IKE_MOBIKE)
201
            {
202
                mobike = (ike_mobike_t*)task;
203
                if (!mobike->is_probing(mobike))
204
                {
205
                    mobike = NULL;
206
                }
207
                break;
208
            }
209
        }
210
        iterator->destroy(iterator);
211
212
        if (mobike == NULL)
213
        {
214
            if (this->initiating.retransmitted <= RETRANSMIT_TRIES)
215
            {
216
                timeout = (u_int32_t)(RETRANSMIT_TIMEOUT *
217
                            pow(RETRANSMIT_BASE, this->initiating.retransmitted));
218
            }
219
            else
220
            {
221
                DBG1(DBG_IKE, "giving up after %d retransmits",
222
                     this->initiating.retransmitted - 1);
223
                return DESTROY_ME;
224
            }
225
            
226
            if (this->initiating.retransmitted)
227
            {
228
                DBG1(DBG_IKE, "retransmit %d of request with message ID %d",
229
                     this->initiating.retransmitted, message_id);
230
            }
231
            packet = this->initiating.packet->clone(this->initiating.packet);
232
        }
233
        else
234
        {    /* for routeability checks, we use a more aggressive behavior */
235
            if (this->initiating.retransmitted <= ROUTEABILITY_CHECK_TRIES)
236
            {
237
                timeout = ROUTEABILITY_CHECK_INTERVAL;
238
            }
239
            else
240
            {
241
                DBG1(DBG_IKE, "giving up after %d path probings",
242
                     this->initiating.retransmitted - 1);
243
                return DESTROY_ME;
244
            }
245
            
246
            if (this->initiating.retransmitted)
247
            {
248
                DBG1(DBG_IKE, "path probing attempt %d",
249
                     this->initiating.retransmitted);
250
            }
251
            packet = this->initiating.packet->clone(this->initiating.packet);
252
            mobike->transmit(mobike, packet);
253
        }
254
        
255
        charon->sender->send(charon->sender, packet);
256
        
257
        this->initiating.retransmitted++;
258
        job = (job_t*)retransmit_job_create(this->initiating.mid,
259
                                            this->ike_sa->get_id(this->ike_sa));
260
        charon->scheduler->schedule_job_ms(charon->scheduler, job, timeout);
261
    }
262
    return SUCCESS;
263
}
264
265
/**
266
 * build a request using the active task list
267
 * Implementation of task_manager_t.initiate
268
 */
269
static status_t build_request(private_task_manager_t *this)
270
{
271
    iterator_t *iterator;
272
    task_t *task;
273
    message_t *message;
274
    host_t *me, *other;
275
    status_t status;
276
    exchange_type_t exchange = 0;
277
    
278
    if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
279
    {
280
        DBG2(DBG_IKE, "delaying task initiation, exchange in progress");
281
        /* do not initiate if we already have a message in the air */
282
        return SUCCESS;
283
    }
284
    
285
    if (this->active_tasks->get_count(this->active_tasks) == 0)
286
    {
287
        DBG2(DBG_IKE, "activating new tasks");
288
        switch (this->ike_sa->get_state(this->ike_sa))
289
        {
290
            case IKE_CREATED:
291
                if (activate_task(this, IKE_INIT))
292
                {
293
                    this->initiating.mid = 0;
294
                    exchange = IKE_SA_INIT;
295
                    activate_task(this, IKE_NATD);
296
                    activate_task(this, IKE_CERT_PRE);
297
#ifdef ME
298
                    /* this task has to be activated before the IKE_AUTHENTICATE
299
                     * task, because that task pregenerates the packet after
300
                     * which no payloads can be added to the message anymore.
301
                     */
302
                    activate_task(this, IKE_ME);
303
#endif /* ME */
304
                    activate_task(this, IKE_AUTHENTICATE);
305
                    activate_task(this, IKE_CERT_POST);
306
                    activate_task(this, IKE_CONFIG);
307
                    activate_task(this, CHILD_CREATE);
308
                    activate_task(this, IKE_AUTH_LIFETIME);
309
                    activate_task(this, IKE_MOBIKE);
310
                }
311
                break;
312
            case IKE_ESTABLISHED:
313
                if (activate_task(this, CHILD_CREATE))
314
                {
315
                    exchange = CREATE_CHILD_SA;
316
                    break;
317
                }
318
                if (activate_task(this, CHILD_DELETE))
319
                {
320
                    exchange = INFORMATIONAL;
321
                    break;
322
                }
323
                if (activate_task(this, CHILD_REKEY))
324
                {
325
                    exchange = CREATE_CHILD_SA;
326
                    break;
327
                }
328
                if (activate_task(this, IKE_DELETE))
329
                {
330
                    exchange = INFORMATIONAL;
331
                    break;
332
                }
333
                if (activate_task(this, IKE_REKEY))
334
                {
335
                    exchange = CREATE_CHILD_SA;
336
                    break;
337
                }
338
                if (activate_task(this, IKE_REAUTH))
339
                {
340
                    exchange = INFORMATIONAL;
341
                    break;
342
                }
343
                if (activate_task(this, IKE_MOBIKE))
344
                {
345
                    exchange = INFORMATIONAL;
346
                    break;
347
                }
348
                if (activate_task(this, IKE_DPD))
349
                {
350
                    exchange = INFORMATIONAL;
351
                    break;
352
                }
353
#ifdef ME
354
                if (activate_task(this, IKE_ME))
355
                {
356
                    exchange = ME_CONNECT;
357
                    break;
358
                }
359
#endif /* ME */
360
            case IKE_REKEYING:
361
                if (activate_task(this, IKE_DELETE))
362
                {
363
                    exchange = INFORMATIONAL;
364
                    break;
365
                }
366
            case IKE_DELETING:
367
            default:
368
                break;
369
        }
370
    }
371
    else
372
    {
373
        DBG2(DBG_IKE, "reinitiating already active tasks");
374
        iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
375
        while (iterator->iterate(iterator, (void**)&task))
376
        {
377
            DBG2(DBG_IKE, "  %N task", task_type_names, task->get_type(task));
378
            switch (task->get_type(task))
379
            {
380
                case IKE_INIT:
381
                    exchange = IKE_SA_INIT;
382
                    break;
383
                case IKE_AUTHENTICATE:
384
                    exchange = IKE_AUTH;
385
                    break;
386
                case CHILD_CREATE:
387
                case CHILD_REKEY:
388
                case IKE_REKEY:
389
                    exchange = CREATE_CHILD_SA;
390
                    break;
391
                case IKE_MOBIKE:
392
                    exchange = INFORMATIONAL;
393
                default:
394
                    continue;
395
            }
396
            break;
397
        }
398
        iterator->destroy(iterator);
399
    }
400
    
401
    if (exchange == 0)
402
    {
403
        DBG2(DBG_IKE, "nothing to initiate");
404
        /* nothing to do yet... */
405
        return SUCCESS;
406
    }
407
    
408
    me = this->ike_sa->get_my_host(this->ike_sa);
409
    other = this->ike_sa->get_other_host(this->ike_sa);
410
    
411
    message = message_create();
412
    message->set_message_id(message, this->initiating.mid);
413
    message->set_source(message, me->clone(me));
414
    message->set_destination(message, other->clone(other));
415
    message->set_exchange_type(message, exchange);
416
    this->initiating.type = exchange;
417
    this->initiating.retransmitted = 0;
418
    
419
    iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
420
    while (iterator->iterate(iterator, (void*)&task))
421
    {
422
        switch (task->build(task, message))
423
        {
424
            case SUCCESS:
425
                /* task completed, remove it */
426
                iterator->remove(iterator);
427
                task->destroy(task);
428
                break;
429
            case NEED_MORE:
430
                /* processed, but task needs another exchange */
431
                break;
432
            case FAILED:
433
            default:
434
                /* critical failure, destroy IKE_SA */
435
                iterator->destroy(iterator);
436
                message->destroy(message);
437
                flush(this);
438
                return DESTROY_ME;
439
        }
440
    }
441
    iterator->destroy(iterator);
442
    
443
    /* update exchange type if a task changed it */
444
    this->initiating.type = message->get_exchange_type(message);
445
    
446
    status = this->ike_sa->generate_message(this->ike_sa, message,
447
                                            &this->initiating.packet);
448
    if (status != SUCCESS)
449
    {
450
        /* message generation failed. There is nothing more to do than to
451
         * close the SA */
452
        message->destroy(message);
453
        flush(this);
454
        return DESTROY_ME;
455
    }
456
    
457
    charon->bus->message(charon->bus, message, FALSE);
458
    message->destroy(message);
459
    
460
    return retransmit(this, this->initiating.mid);
461
}
462
463
/**
464
 * handle an incoming response message
465
 */
466
static status_t process_response(private_task_manager_t *this,
467
                                 message_t *message)
468
{
469
    iterator_t *iterator;
470
    task_t *task;
471
    
472
    if (message->get_exchange_type(message) != this->initiating.type)
473
    {
474
        DBG1(DBG_IKE, "received %N response, but expected %N",
475
             exchange_type_names, message->get_exchange_type(message),
476
             exchange_type_names, this->initiating.type);
477
        return DESTROY_ME;
478
    }
479
    
480
    /* catch if we get resetted while processing */
481
    this->reset = FALSE;
482
    iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
483
    while (iterator->iterate(iterator, (void*)&task))
484
    {
485
        switch (task->process(task, message))
486
        {
487
            case SUCCESS:
488
                /* task completed, remove it */
489
                iterator->remove(iterator);
490
                task->destroy(task);
491
                break;
492
            case NEED_MORE:
493
                /* processed, but task needs another exchange */
494
                break;
495
            case FAILED:
496
            default:
497
                /* critical failure, destroy IKE_SA */
498
                iterator->remove(iterator);
499
                iterator->destroy(iterator);
500
                task->destroy(task);
501
                return DESTROY_ME;
502
        }
503
        if (this->reset)
504
        {    /* start all over again if we were reset */
505
            this->reset = FALSE;
506
            iterator->destroy(iterator);
507
            return build_request(this);
508
        }    
509
    }
510
    iterator->destroy(iterator);
511
    
512
    this->initiating.mid++;
513
    this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
514
    this->initiating.packet->destroy(this->initiating.packet);
515
    this->initiating.packet = NULL;
516
    
517
    return build_request(this);
518
}
519
520
/**
521
 * handle exchange collisions
522
 */
523
static void handle_collisions(private_task_manager_t *this, task_t *task)
524
{
525
    iterator_t *iterator;
526
    task_t *active;
527
    task_type_t type;
528
    
529
    type = task->get_type(task);
530
    
531
    /* do we have to check  */
532
    if (type == IKE_REKEY || type == CHILD_REKEY ||
533
        type == CHILD_DELETE || type == IKE_DELETE || type == IKE_REAUTH)
534
    {
535
        /* find an exchange collision, and notify these tasks */
536
        iterator = this->active_tasks->create_iterator(this->active_tasks, TRUE);
537
        while (iterator->iterate(iterator, (void**)&active))
538
        {
539
            switch (active->get_type(active))
540
            {
541
                case IKE_REKEY:
542
                    if (type == IKE_REKEY || type == IKE_DELETE ||
543
                        type == IKE_REAUTH)
544
                    {
545
                        ike_rekey_t *rekey = (ike_rekey_t*)active;
546
                        rekey->collide(rekey, task);
547
                        break;
548
                    }
549
                    continue;
550
                case CHILD_REKEY:
551
                    if (type == CHILD_REKEY || type == CHILD_DELETE)
552
                    {
553
                        child_rekey_t *rekey = (child_rekey_t*)active;
554
                        rekey->collide(rekey, task);
555
                        break;
556
                    }
557
                    continue;
558
                default:
559
                    continue;
560
            }
561
            iterator->destroy(iterator);
562
            return;
563
        }
564
        iterator->destroy(iterator);
565
    }
566
    /* destroy task if not registered in any active task */
567
    task->destroy(task);
568
}
569
570
/**
571
 * build a response depending on the "passive" task list
572
 */
573
static status_t build_response(private_task_manager_t *this, message_t *request)
574
{
575
    iterator_t *iterator;
576
    task_t *task;
577
    message_t *message;
578
    host_t *me, *other;
579
    bool delete = FALSE;
580
    status_t status;
581
    
582
    me = request->get_destination(request);
583
    other = request->get_source(request);
584
    
585
    message = message_create();
586
    message->set_exchange_type(message, request->get_exchange_type(request));
587
    /* send response along the path the request came in */
588
    message->set_source(message, me->clone(me));
589
    message->set_destination(message, other->clone(other));
590
    message->set_message_id(message, this->responding.mid);
591
    message->set_request(message, FALSE);
592
    
593
    iterator = this->passive_tasks->create_iterator(this->passive_tasks, TRUE);
594
    while (iterator->iterate(iterator, (void*)&task))
595
    {
596
        switch (task->build(task, message))
597
        {
598
            case SUCCESS:
599
                /* task completed, remove it */
600
                iterator->remove(iterator);
601
                handle_collisions(this, task);
602
            case NEED_MORE:
603
                /* processed, but task needs another exchange */
604
                break;
605
            case FAILED:
606
            default:
607
                /* destroy IKE_SA, but SEND response first */
608
                delete = TRUE;
609
                break;
610
        }
611
        if (delete)
612
        {
613
            break;
614
        }
615
    }
616
    iterator->destroy(iterator);
617
    
618
    /* remove resonder SPI if IKE_SA_INIT failed */
619
    if (delete && request->get_exchange_type(request) == IKE_SA_INIT)
620
    {
621
        ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa);
622
        id->set_responder_spi(id, 0);
623
    }
624
    
625
    /* message complete, send it */
626
    DESTROY_IF(this->responding.packet);
627
    this->responding.packet = NULL;
628
    status = this->ike_sa->generate_message(this->ike_sa, message,
629
                                            &this->responding.packet);
630
    charon->bus->message(charon->bus, message, FALSE);
631
    message->destroy(message);
632
    if (status != SUCCESS)
633
    {
634
        return DESTROY_ME;
635
    }
636
    
637
    charon->sender->send(charon->sender,
638
                         this->responding.packet->clone(this->responding.packet));
639
    if (delete)
640
    {
641
        return DESTROY_ME;
642
    }
643
    return SUCCESS;
644
}
645
646
/**
647
 * handle an incoming request message
648
 */
649
static status_t process_request(private_task_manager_t *this,
650
                                message_t *message)
651
{
652
    enumerator_t *enumerator;
653
    iterator_t *iterator;
654
    task_t *task = NULL;
655
    payload_t *payload;
656
    notify_payload_t *notify;
657
    delete_payload_t *delete;
658
    
659
    if (this->passive_tasks->get_count(this->passive_tasks) == 0)
660
    {    /* create tasks depending on request type, if not already some queued */
661
        switch (message->get_exchange_type(message))
662
        {
663
            case IKE_SA_INIT:
664
            {
665
                task = (task_t*)ike_init_create(this->ike_sa, FALSE, NULL);
666
                this->passive_tasks->insert_last(this->passive_tasks, task);
667
                task = (task_t*)ike_natd_create(this->ike_sa, FALSE);
668
                this->passive_tasks->insert_last(this->passive_tasks, task);
669
                task = (task_t*)ike_cert_pre_create(this->ike_sa, FALSE);
670
                this->passive_tasks->insert_last(this->passive_tasks, task);
671
#ifdef ME            
672
                task = (task_t*)ike_me_create(this->ike_sa, FALSE);
673
                this->passive_tasks->insert_last(this->passive_tasks, task);
674
#endif /* ME */
675
                task = (task_t*)ike_auth_create(this->ike_sa, FALSE);
676
                this->passive_tasks->insert_last(this->passive_tasks, task);
677
                task = (task_t*)ike_cert_post_create(this->ike_sa, FALSE);
678
                this->passive_tasks->insert_last(this->passive_tasks, task);
679
                task = (task_t*)ike_config_create(this->ike_sa, FALSE);
680
                this->passive_tasks->insert_last(this->passive_tasks, task);
681
                task = (task_t*)child_create_create(this->ike_sa, NULL);
682
                this->passive_tasks->insert_last(this->passive_tasks, task);
683
                task = (task_t*)ike_auth_lifetime_create(this->ike_sa, FALSE);
684
                this->passive_tasks->insert_last(this->passive_tasks, task);
685
                task = (task_t*)ike_mobike_create(this->ike_sa, FALSE);
686
                this->passive_tasks->insert_last(this->passive_tasks, task);
687
                break;
688
            }
689
            case CREATE_CHILD_SA:
690
            {    /* FIXME: we should prevent this on mediation connections */
691
                bool notify_found = FALSE, ts_found = FALSE;
692
                enumerator = message->create_payload_enumerator(message);
693
                while (enumerator->enumerate(enumerator, &payload))
694
                {
695
                    switch (payload->get_type(payload))
696
                    {
697
                        case NOTIFY:
698
                        {    /* if we find a rekey notify, its CHILD_SA rekeying */
699
                            notify = (notify_payload_t*)payload;
700
                            if (notify->get_notify_type(notify) == REKEY_SA &&
701
                                (notify->get_protocol_id(notify) == PROTO_AH ||
702
                                 notify->get_protocol_id(notify) == PROTO_ESP))
703
                            {
704
                                notify_found = TRUE;
705
                            }
706
                            break;
707
                        }
708
                        case TRAFFIC_SELECTOR_INITIATOR:
709
                        case TRAFFIC_SELECTOR_RESPONDER:
710
                        {    /* if we don't find a TS, its IKE rekeying */
711
                            ts_found = TRUE;
712
                            break;
713
                        }
714
                        default:
715
                            break;
716
                    }
717
                }
718
                enumerator->destroy(enumerator);
719
                
720
                if (ts_found)
721
                {
722
                    if (notify_found)
723
                    {
724
                        task = (task_t*)child_rekey_create(this->ike_sa,
725
                                                           PROTO_NONE, 0);
726
                    }
727
                    else
728
                    {
729
                        task = (task_t*)child_create_create(this->ike_sa, NULL);
730
                    }
731
                }
732
                else
733
                {
734
                    task = (task_t*)ike_rekey_create(this->ike_sa, FALSE);
735
                }
736
                this->passive_tasks->insert_last(this->passive_tasks, task);
737
                break;
738
            }
739
            case INFORMATIONAL:
740
            {
741
                enumerator = message->create_payload_enumerator(message);
742
                while (enumerator->enumerate(enumerator, &payload))
743
                {
744
                    switch (payload->get_type(payload))
745
                    {
746
                        case NOTIFY:
747
                        {
748
                            notify = (notify_payload_t*)payload;
749
                            switch (notify->get_notify_type(notify))
750
                            {
751
                                case ADDITIONAL_IP4_ADDRESS:
752
                                case ADDITIONAL_IP6_ADDRESS:
753
                                case NO_ADDITIONAL_ADDRESSES:
754
                                case UPDATE_SA_ADDRESSES:
755
                                case NO_NATS_ALLOWED:
756
                                case UNACCEPTABLE_ADDRESSES:
757
                                case UNEXPECTED_NAT_DETECTED:
758
                                case COOKIE2:
759
                                case NAT_DETECTION_SOURCE_IP:
760
                                case NAT_DETECTION_DESTINATION_IP:
761
                                    task = (task_t*)ike_mobike_create(
762
                                                            this->ike_sa, FALSE);
763
                                    break;
764
                                case AUTH_LIFETIME:
765
                                    task = (task_t*)ike_auth_lifetime_create(
766
                                                            this->ike_sa, FALSE);
767
                                    break;
768
                                default:
769
                                    break;
770
                            }
771
                            break;
772
                        }
773
                        case DELETE:
774
                        {
775
                            delete = (delete_payload_t*)payload;
776
                            if (delete->get_protocol_id(delete) == PROTO_IKE)
777
                            {
778
                                task = (task_t*)ike_delete_create(this->ike_sa,
779
                                                                FALSE);
780
                            }
781
                            else
782
                            {
783
                                task = (task_t*)child_delete_create(this->ike_sa,
784
                                                                PROTO_NONE, 0);
785
                            }
786
                            break;
787
                        }
788
                        default:
789
                            break;
790
                    }
791
                    if (task)
792
                    {
793
                        break;
794
                    }
795
                }
796
                enumerator->destroy(enumerator);
797
            
798
                if (task == NULL)
799
                {
800
                    task = (task_t*)ike_dpd_create(FALSE);
801
                }
802
                this->passive_tasks->insert_last(this->passive_tasks, task);
803
                break;
804
            }
805
#ifdef ME
806
            case ME_CONNECT:
807
            {
808
                task = (task_t*)ike_me_create(this->ike_sa, FALSE);
809
                this->passive_tasks->insert_last(this->passive_tasks, task);
810
            }
811
#endif /* ME */
812
            default:
813
                break;
814
        }
815
    }
816
    
817
    /* let the tasks process the message */
818
    iterator = this->passive_tasks->create_iterator(this->passive_tasks, TRUE);
819
    while (iterator->iterate(iterator, (void*)&task))
820
    {
821
        switch (task->process(task, message))
822
        {
823
            case SUCCESS:
824
                /* task completed, remove it */
825
                iterator->remove(iterator);
826
                task->destroy(task);
827
                break;
828
            case NEED_MORE:
829
                /* processed, but task needs at least another call to build() */
830
                break;
831
            case FAILED:
832
            default:
833
                /* critical failure, destroy IKE_SA */
834
                iterator->remove(iterator);
835
                iterator->destroy(iterator);
836
                task->destroy(task);
837
                return DESTROY_ME;
838
        }
839
    }
840
    iterator->destroy(iterator);
841
    
842
    return build_response(this, message);
843
}
844
845
/**
846
 * Implementation of task_manager_t.process_message
847
 */
848
static status_t process_message(private_task_manager_t *this, message_t *msg)
849
{
850
    u_int32_t mid = msg->get_message_id(msg);
851
    
852
    if (msg->get_request(msg))
853
    {
854
        if (mid == this->responding.mid)
855
        {
856
            charon->bus->message(charon->bus, msg, TRUE);
857
            if (process_request(this, msg) != SUCCESS)
858
            {
859
                flush(this);
860
                return DESTROY_ME;
861
            }
862
            this->responding.mid++;
863
        }
864
        else if ((mid == this->responding.mid - 1) && this->responding.packet)
865
        {
866
            packet_t *clone;
867
            host_t *me, *other;
868
            
869
            DBG1(DBG_IKE, "received retransmit of request with ID %d, "
870
                  "retransmitting response", mid);
871
            clone = this->responding.packet->clone(this->responding.packet);
872
            me = msg->get_destination(msg);
873
            other = msg->get_source(msg);
874
            clone->set_source(clone, me->clone(me));
875
            clone->set_destination(clone, other->clone(other));
876
            charon->sender->send(charon->sender, clone);
877
        }
878
        else
879
        {
880
            DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
881
                 mid, this->responding.mid);
882
        }
883
    }
884
    else
885
    {
886
        if (mid == this->initiating.mid)
887
        {
888
            if (process_response(this, msg) != SUCCESS)
889
            {
890
                flush(this);
891
                return DESTROY_ME;
892
            }
893
        }
894
        else
895
        {
896
            DBG1(DBG_IKE, "received message ID %d, expected %d. Ignored",
897
                 mid, this->initiating.mid);
898
            return SUCCESS;
899
        }
900
    }
901
    return SUCCESS;
902
}
903
904
/**
905
 * Implementation of task_manager_t.queue_task
906
 */
907
static void queue_task(private_task_manager_t *this, task_t *task)
908
{
909
    if (task->get_type(task) == IKE_MOBIKE)
910
    {    /*  there is no need to queue more than one mobike task */
911
        iterator_t *iterator;
912
        task_t *current;
913
        
914
        iterator = this->queued_tasks->create_iterator(this->queued_tasks, TRUE);
915
        while (iterator->iterate(iterator, (void**)&current))
916
        {
917
            if (current->get_type(current) == IKE_MOBIKE)
918
            {
919
                iterator->destroy(iterator);
920
                task->destroy(task);
921
                return;
922
            }
923
        }
924
        iterator->destroy(iterator);
925
    }
926
    DBG2(DBG_IKE, "queueing %N task", task_type_names, task->get_type(task));
927
    this->queued_tasks->insert_last(this->queued_tasks, task);
928
}
929
930
/**
931
 * Implementation of task_manager_t.adopt_tasks
932
 */
933
static void adopt_tasks(private_task_manager_t *this, private_task_manager_t *other)
934
{
935
    task_t *task;
936
    
937
    /* move queued tasks from other to this */
938
    while (other->queued_tasks->remove_last(other->queued_tasks,
939
                                                (void**)&task) == SUCCESS)
940
    {
941
        DBG2(DBG_IKE, "migrating %N task", task_type_names, task->get_type(task));
942
        task->migrate(task, this->ike_sa);
943
        this->queued_tasks->insert_first(this->queued_tasks, task);
944
    }
945
}
946
947
/**
948
 * Implementation of task_manager_t.busy
949
 */
950
static bool busy(private_task_manager_t *this)
951
{
952
    return (this->active_tasks->get_count(this->active_tasks) > 0);
953
}
954
955
/**
956
 * Implementation of task_manager_t.reset
957
 */
958
static void reset(private_task_manager_t *this,
959
                  u_int32_t initiate, u_int32_t respond)
960
{
961
    task_t *task;
962
    
963
    /* reset message counters and retransmit packets */
964
    DESTROY_IF(this->responding.packet);
965
    DESTROY_IF(this->initiating.packet);
966
    this->responding.packet = NULL;
967
    this->initiating.packet = NULL;
968
    if (initiate != UINT_MAX)
969
    {
970
        this->initiating.mid = initiate;
971
    }
972
    if (respond != UINT_MAX)
973
    {
974
        this->responding.mid = respond;
975
    }
976
    this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
977
    
978
    /* reset active tasks */
979
    while (this->active_tasks->remove_last(this->active_tasks,
980
                                           (void**)&task) == SUCCESS)
981
    {
982
        task->migrate(task, this->ike_sa);
983
        this->queued_tasks->insert_first(this->queued_tasks, task);
984
    }
985
    
986
    this->reset = TRUE;
987
}
988
989
/**
990
 * Implementation of task_manager_t.destroy
991
 */
992
static void destroy(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
    free(this);
1003
}
1004
1005
/*
1006
 * see header file
1007
 */
1008
task_manager_t *task_manager_create(ike_sa_t *ike_sa)
1009
{
1010
    private_task_manager_t *this = malloc_thing(private_task_manager_t);
1011
    
1012
    this->public.process_message = (status_t(*)(task_manager_t*,message_t*))process_message;
1013
    this->public.queue_task = (void(*)(task_manager_t*,task_t*))queue_task;
1014
    this->public.initiate = (status_t(*)(task_manager_t*))build_request;
1015
    this->public.retransmit = (status_t(*)(task_manager_t*,u_int32_t))retransmit;
1016
    this->public.reset = (void(*)(task_manager_t*,u_int32_t,u_int32_t))reset;
1017
    this->public.adopt_tasks = (void(*)(task_manager_t*,task_manager_t*))adopt_tasks;
1018
    this->public.busy = (bool(*)(task_manager_t*))busy;
1019
    this->public.destroy = (void(*)(task_manager_t*))destroy;
1020
    
1021
    this->ike_sa = ike_sa;
1022
    this->responding.packet = NULL;
1023
    this->initiating.packet = NULL;
1024
    this->responding.mid = 0;
1025
    this->initiating.mid = 0;
1026
    this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
1027
    this->queued_tasks = linked_list_create();
1028
    this->active_tasks = linked_list_create();
1029
    this->passive_tasks = linked_list_create();
1030
    this->reset = FALSE;
1031
    
1032
    return &this->public;
1033
}