Fix init message arrival check.
[strongswan.git] / src / libcharon / sa / ike_sa_manager.c
index 7f3ca7d..6b2d173 100644 (file)
@@ -970,6 +970,7 @@ METHOD(ike_sa_manager_t, checkout_by_message, ike_sa_t*,
        entry_t *entry;
        ike_sa_t *ike_sa = NULL;
        ike_sa_id_t *id;
+       bool is_init = FALSE;
 
        id = message->get_ike_sa_id(message);
        id = id->clone(id);
@@ -977,11 +978,29 @@ METHOD(ike_sa_manager_t, checkout_by_message, ike_sa_t*,
 
        DBG2(DBG_MGR, "checkout IKE_SA by message");
 
-       if (message->get_request(message) &&
-               message->get_exchange_type(message) == IKE_SA_INIT &&
-               this->hasher)
+       if (id->get_responder_spi(id) == 0)
        {
-               /* IKE_SA_INIT request. Check for an IKE_SA with such a message hash. */
+               if (message->get_major_version(message) == IKEV2_MAJOR_VERSION)
+               {
+                       if (message->get_exchange_type(message) == IKE_SA_INIT &&
+                               message->get_request(message))
+                       {
+                               is_init = TRUE;
+                       }
+               }
+               else
+               {
+                       if (message->get_exchange_type(message) == ID_PROT ||
+                               message->get_exchange_type(message) == AGGRESSIVE)
+                       {
+                               is_init = TRUE;
+                       }
+               }
+       }
+
+       if (is_init && this->hasher)
+       {
+               /* First request. Check for an IKE_SA with such a message hash. */
                chunk_t data, hash;
 
                data = message->get_packet_data(message);
@@ -990,7 +1009,8 @@ METHOD(ike_sa_manager_t, checkout_by_message, ike_sa_t*,
 
                if (get_entry_by_hash(this, id, hash, &entry, &segment) == SUCCESS)
                {
-                       if (entry->message_id == 0)
+                       if (message->get_exchange_type(message) == IKE_SA_INIT &&
+                               entry->message_id == 0)
                        {
                                unlock_single_segment(this, segment);
                                chunk_free(&hash);
@@ -1011,35 +1031,27 @@ METHOD(ike_sa_manager_t, checkout_by_message, ike_sa_t*,
 
                if (ike_sa == NULL)
                {
-                       if (id->get_responder_spi(id) == 0 &&
-                               message->get_exchange_type(message) == IKE_SA_INIT)
-                       {
-                               /* no IKE_SA found, create a new one */
-                               id->set_responder_spi(id, get_spi(this));
-                               entry = entry_create();
-                               entry->ike_sa = ike_sa_create(id);
-                               entry->ike_sa_id = id->clone(id);
+                       /* no IKE_SA found, create a new one */
+                       id->set_responder_spi(id, get_spi(this));
+                       entry = entry_create();
+                       entry->ike_sa = ike_sa_create(id);
+                       entry->ike_sa_id = id->clone(id);
 
-                               segment = put_entry(this, entry);
-                               entry->checked_out = TRUE;
-                               unlock_single_segment(this, segment);
+                       segment = put_entry(this, entry);
+                       entry->checked_out = TRUE;
+                       unlock_single_segment(this, segment);
 
-                               entry->message_id = message->get_message_id(message);
-                               entry->init_hash = hash;
-                               ike_sa = entry->ike_sa;
+                       entry->message_id = message->get_message_id(message);
+                       entry->init_hash = hash;
+                       ike_sa = entry->ike_sa;
 
-                               DBG2(DBG_MGR, "created IKE_SA %s[%u]",
-                                               ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa));
-                       }
-                       else
-                       {
-                               chunk_free(&hash);
-                               DBG1(DBG_MGR, "ignoring message, no such IKE_SA");
-                       }
+                       DBG2(DBG_MGR, "created IKE_SA %s[%u]",
+                                       ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa));
                }
                else
                {
                        chunk_free(&hash);
+                       DBG1(DBG_MGR, "ignoring message, no such IKE_SA");
                }
                id->destroy(id);
                charon->bus->set_sa(charon->bus, ike_sa);
@@ -1048,7 +1060,7 @@ METHOD(ike_sa_manager_t, checkout_by_message, ike_sa_t*,
 
        if (get_entry_by_id(this, id, &entry, &segment) == SUCCESS)
        {
-               /* only check out if we are not processing this request */
+               /* only check out in IKEv2 if we are not already processing it */
                if (message->get_request(message) &&
                        message->get_message_id(message) == entry->message_id)
                {
@@ -1057,7 +1069,9 @@ METHOD(ike_sa_manager_t, checkout_by_message, ike_sa_t*,
                }
                else if (wait_for_entry(this, entry, segment))
                {
-                       ike_sa_id_t *ike_id = entry->ike_sa->get_id(entry->ike_sa);
+                       ike_sa_id_t *ike_id;
+
+                       ike_id = entry->ike_sa->get_id(entry->ike_sa);
                        entry->checked_out = TRUE;
                        entry->message_id = message->get_message_id(message);
                        if (ike_id->get_responder_spi(ike_id) == 0)
@@ -1134,8 +1148,7 @@ METHOD(ike_sa_manager_t, checkout_by_config, ike_sa_t*,
 METHOD(ike_sa_manager_t, checkout_by_id, ike_sa_t*,
        private_ike_sa_manager_t *this, u_int32_t id, bool child)
 {
-       enumerator_t *enumerator;
-       iterator_t *children;
+       enumerator_t *enumerator, *children;
        entry_t *entry;
        ike_sa_t *ike_sa = NULL;
        child_sa_t *child_sa;
@@ -1151,8 +1164,8 @@ METHOD(ike_sa_manager_t, checkout_by_id, ike_sa_t*,
                        /* look for a child with such a reqid ... */
                        if (child)
                        {
-                               children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
-                               while (children->iterate(children, (void**)&child_sa))
+                               children = entry->ike_sa->create_child_sa_enumerator(entry->ike_sa);
+                               while (children->enumerate(children, (void**)&child_sa))
                                {
                                        if (child_sa->get_reqid(child_sa) == id)
                                        {
@@ -1188,8 +1201,7 @@ METHOD(ike_sa_manager_t, checkout_by_id, ike_sa_t*,
 METHOD(ike_sa_manager_t, checkout_by_name, ike_sa_t*,
        private_ike_sa_manager_t *this, char *name, bool child)
 {
-       enumerator_t *enumerator;
-       iterator_t *children;
+       enumerator_t *enumerator, *children;
        entry_t *entry;
        ike_sa_t *ike_sa = NULL;
        child_sa_t *child_sa;
@@ -1203,8 +1215,8 @@ METHOD(ike_sa_manager_t, checkout_by_name, ike_sa_t*,
                        /* look for a child with such a policy name ... */
                        if (child)
                        {
-                               children = entry->ike_sa->create_child_sa_iterator(entry->ike_sa);
-                               while (children->iterate(children, (void**)&child_sa))
+                               children = entry->ike_sa->create_child_sa_enumerator(entry->ike_sa);
+                               while (children->enumerate(children, (void**)&child_sa))
                                {
                                        if (streq(child_sa->get_name(child_sa), name))
                                        {