Compiler warning fixed.
[strongswan.git] / src / libcharon / sa / ikev1 / task_manager_v1.c
old mode 100644 (file)
new mode 100755 (executable)
index f7415a5..1c9b43a
@@ -31,6 +31,8 @@
 #include <sa/ikev1/tasks/isakmp_cert_pre.h>
 #include <sa/ikev1/tasks/isakmp_cert_post.h>
 #include <sa/ikev1/tasks/isakmp_delete.h>
+#include <sa/ikev1/tasks/isakmp_dpd.h>
+
 #include <processing/jobs/retransmit_job.h>
 #include <processing/jobs/delete_ike_sa_job.h>
 
@@ -197,6 +199,16 @@ struct private_task_manager_t {
         * Base to calculate retransmission timeout
         */
        double retransmit_base;
+
+       /**
+        * Sequence number for sending DPD requests
+        */
+       u_int32_t dpd_send;
+
+       /**
+        * Sequence number for received DPD requests
+        */
+       u_int32_t dpd_recv;
 };
 
 /**
@@ -320,15 +332,10 @@ METHOD(task_manager_t, initiate, status_t,
        host_t *me, *other;
        status_t status;
        exchange_type_t exchange = EXCHANGE_TYPE_UNDEFINED;
-       bool new_mid = FALSE, expect_response = FALSE, flushed = FALSE;
+       bool new_mid = FALSE, expect_response = FALSE, flushed = FALSE, keep = FALSE;
 
-       if (!this->rng)
-       {
-               DBG1(DBG_IKE, "no RNG supported");
-               return FAILED;
-       }
-
-       if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED)
+       if (this->initiating.type != EXCHANGE_TYPE_UNDEFINED &&
+               this->initiating.type != INFORMATIONAL_V1)
        {
                DBG2(DBG_IKE, "delaying task initiation, %N exchange in progress",
                                exchange_type_names, this->initiating.type);
@@ -406,6 +413,12 @@ METHOD(task_manager_t, initiate, status_t,
                                        new_mid = TRUE;
                                        break;
                                }
+                               if (activate_task(this, TASK_ISAKMP_DPD))
+                               {
+                                       exchange = INFORMATIONAL_V1;
+                                       new_mid = TRUE;
+                                       break;
+                               }
                                break;
                        default:
                                break;
@@ -472,6 +485,11 @@ METHOD(task_manager_t, initiate, status_t,
                        case SUCCESS:
                                /* task completed, remove it */
                                this->active_tasks->remove_at(this->active_tasks, enumerator);
+                               if (task->get_type(task) == TASK_AGGRESSIVE_MODE ||
+                                       task->get_type(task) == TASK_QUICK_MODE)
+                               {       /* last message of three message exchange */
+                                       keep = TRUE;
+                               }
                                task->destroy(task);
                                continue;
                        case NEED_MORE:
@@ -500,10 +518,22 @@ METHOD(task_manager_t, initiate, status_t,
        }
        enumerator->destroy(enumerator);
 
-       if (this->active_tasks->get_count(this->active_tasks) == 0)
+       if (this->active_tasks->get_count(this->active_tasks) == 0 &&
+               (exchange == QUICK_MODE || exchange == AGGRESSIVE))
        {       /* tasks completed, no exchange active anymore */
                this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
        }
+       if (exchange == INFORMATIONAL_V1)
+       {
+               if (message->get_notify(message, DPD_R_U_THERE))
+               {
+                       expect_response = TRUE;
+               }
+               if (message->get_notify(message, DPD_R_U_THERE_ACK))
+               {
+                       keep = TRUE;
+               }
+       }
        if (flushed)
        {
                message->destroy(message);
@@ -524,15 +554,13 @@ METHOD(task_manager_t, initiate, status_t,
        }
 
        this->initiating.seqnr++;
-       if (expect_response)
+       if (expect_response )
        {
                message->destroy(message);
                return retransmit(this, this->initiating.seqnr);
        }
-       if (message->get_exchange_type(message) == QUICK_MODE ||
-               message->get_exchange_type(message) == AGGRESSIVE)
-       {       /* keep the packet for retransmission in quick/aggressive mode.
-                * The responder might request a retransmission */
+       if (keep)
+       {       /* keep the packet for retransmission, the responder might request it */
                charon->sender->send(charon->sender,
                                        this->initiating.packet->clone(this->initiating.packet));
        }
@@ -709,7 +737,9 @@ static status_t process_request(private_task_manager_t *this,
 {
        enumerator_t *enumerator;
        task_t *task = NULL;
-       bool send_response = FALSE;
+       bool send_response = FALSE, dpd = FALSE;
+       notify_payload_t *notify;
+       chunk_t data;
 
        if (message->get_exchange_type(message) == INFORMATIONAL_V1 ||
                this->passive_tasks->get_count(this->passive_tasks) == 0)
@@ -752,7 +782,28 @@ static status_t process_request(private_task_manager_t *this,
                                this->passive_tasks->insert_last(this->passive_tasks, task);
                                break;
                        case INFORMATIONAL_V1:
-                               task = (task_t *)informational_create(this->ike_sa, NULL);
+                               notify = message->get_notify(message, DPD_R_U_THERE);
+                               if (notify)
+                               {
+                                       data = notify->get_notification_data(notify);
+                                       if (this->dpd_recv == 0 && data.len == 4)
+                                       {       /* first DPD request, initialize counter */
+                                               this->dpd_recv = untoh32(data.ptr);
+                                       }
+                                       task = (task_t *)isakmp_dpd_create(this->ike_sa, FALSE,
+                                                                                                          this->dpd_recv++);
+                                       dpd = TRUE;
+                               }
+                               else if (message->get_notify(message, DPD_R_U_THERE_ACK))
+                               {
+                                       task = (task_t *)isakmp_dpd_create(this->ike_sa, TRUE,
+                                                                                                          this->dpd_send - 1);
+                                       dpd = TRUE;
+                               }
+                               else
+                               {
+                                       task = (task_t *)informational_create(this->ike_sa, NULL);
+                               }
                                this->passive_tasks->insert_first(this->passive_tasks, task);
                                break;
                        case TRANSACTION:
@@ -770,6 +821,11 @@ static status_t process_request(private_task_manager_t *this,
                                return FAILED;
                }
        }
+       if (!dpd)
+       {
+               this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
+                                                                       time_monotonic(NULL));
+       }
        /* let the tasks process the message */
        enumerator = this->passive_tasks->create_enumerator(this->passive_tasks);
        while (enumerator->enumerate(enumerator, (void*)&task))
@@ -804,6 +860,12 @@ static status_t process_request(private_task_manager_t *this,
        }
        enumerator->destroy(enumerator);
 
+       if (dpd && this->initiating.type == INFORMATIONAL_V1)
+       {       /* got a DPD reply, cancel any retransmission */
+               this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
+               DESTROY_IF(this->initiating.packet);
+               this->initiating.packet = NULL;
+       }
        if (send_response)
        {
                if (build_response(this, message) != SUCCESS)
@@ -983,6 +1045,7 @@ METHOD(task_manager_t, process_message, status_t,
                 this->active_tasks->get_count(this->active_tasks)))
        {
                msg->set_request(msg, FALSE);
+               charon->bus->message(charon->bus, msg, TRUE, FALSE);
                status = parse_message(this, msg);
                if (status != SUCCESS)
                {
@@ -991,7 +1054,7 @@ METHOD(task_manager_t, process_message, status_t,
                this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
                                                                        time_monotonic(NULL));
                this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
-               charon->bus->message(charon->bus, msg, FALSE);
+               charon->bus->message(charon->bus, msg, TRUE, TRUE);
                if (process_response(this, msg) != SUCCESS)
                {
                        flush(this);
@@ -1011,6 +1074,14 @@ METHOD(task_manager_t, process_message, status_t,
                                charon->sender->send(charon->sender,
                                                this->responding.packet->clone(this->responding.packet));
                        }
+                       else if (this->initiating.packet &&
+                                        this->initiating.type == INFORMATIONAL_V1)
+                       {
+                               DBG1(DBG_IKE, "received retransmit of DPD request, "
+                                        "retransmitting response");
+                               charon->sender->send(charon->sender,
+                                               this->initiating.packet->clone(this->initiating.packet));
+                       }
                        else
                        {
                                DBG1(DBG_IKE, "received retransmit of request with ID %u, "
@@ -1040,6 +1111,7 @@ METHOD(task_manager_t, process_message, status_t,
                }
 
                msg->set_request(msg, TRUE);
+               charon->bus->message(charon->bus, msg, TRUE, FALSE);
                status = parse_message(this, msg);
                if (status != SUCCESS)
                {
@@ -1068,12 +1140,10 @@ METHOD(task_manager_t, process_message, status_t,
                        job = (job_t*)delete_ike_sa_job_create(ike_sa_id, FALSE);
                        lib->scheduler->schedule_job(lib->scheduler, job,
                                        lib->settings->get_int(lib->settings,
-                                               "charon.half_open_timeout",  HALF_OPEN_IKE_SA_TIMEOUT));
+                                               "charon.half_open_timeout", HALF_OPEN_IKE_SA_TIMEOUT));
                }
-               this->ike_sa->set_statistic(this->ike_sa, STAT_INBOUND,
-                                                                       time_monotonic(NULL));
                this->ike_sa->update_hosts(this->ike_sa, me, other, TRUE);
-               charon->bus->message(charon->bus, msg, TRUE);
+               charon->bus->message(charon->bus, msg, TRUE, TRUE);
                if (process_request(this, msg) != SUCCESS)
                {
                        flush(this);
@@ -1258,6 +1328,7 @@ METHOD(task_manager_t, queue_child_rekey, void,
                cfg = child_sa->get_config(child_sa);
                task = quick_mode_create(this->ike_sa, cfg->get_ref(cfg), NULL, NULL);
                task->use_reqid(task, child_sa->get_reqid(child_sa));
+               task->rekey(task, child_sa->get_spi(child_sa, TRUE));
 
                queue_task(this, &task->task);
        }
@@ -1274,7 +1345,8 @@ METHOD(task_manager_t, queue_child_delete, void,
 METHOD(task_manager_t, queue_dpd, void,
        private_task_manager_t *this)
 {
-       /* TODO-IKEv1: DPD checking */
+       queue_task(this, (task_t*)isakmp_dpd_create(this->ike_sa, TRUE,
+                                                                                               this->dpd_send++));
 }
 
 METHOD(task_manager_t, adopt_tasks, void,
@@ -1321,6 +1393,14 @@ METHOD(task_manager_t, reset, void,
        this->initiating.seqnr = 0;
        this->initiating.retransmitted = 0;
        this->initiating.type = EXCHANGE_TYPE_UNDEFINED;
+       if (initiate != UINT_MAX)
+       {
+               this->dpd_send = initiate;
+       }
+       if (respond != UINT_MAX)
+       {
+               this->dpd_recv = respond;
+       }
 
        /* reset queued tasks */
        enumerator = this->queued_tasks->create_enumerator(this->queued_tasks);
@@ -1402,9 +1482,13 @@ task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
                                .destroy = _destroy,
                        },
                },
+               .initiating = {
+                       .type = EXCHANGE_TYPE_UNDEFINED,
+               },
+               .responding = {
+                       .seqnr = RESPONDING_SEQ,
+               },
                .ike_sa = ike_sa,
-               .initiating.type = EXCHANGE_TYPE_UNDEFINED,
-               .responding.seqnr = RESPONDING_SEQ,
                .rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK),
                .queued_tasks = linked_list_create(),
                .active_tasks = linked_list_create(),
@@ -1417,5 +1501,17 @@ task_manager_v1_t *task_manager_v1_create(ike_sa_t *ike_sa)
                                                                "charon.retransmit_base", RETRANSMIT_BASE),
        );
 
+       if (!this->rng)
+       {
+               DBG1(DBG_IKE, "no RNG found, unable to create IKE_SA");
+               destroy(this);
+               return NULL;
+       }
+
+       this->rng->get_bytes(this->rng, sizeof(this->dpd_send),
+                                                (void*)&this->dpd_send);
+       this->dpd_send &= 0x7FFFFFFF;
+
        return &this->public;
 }
+